Skip to content

Instantly share code, notes, and snippets.

View macedd's full-sized avatar
🐋
Looking for growth opportunities

Thiago F Macedo macedd

🐋
Looking for growth opportunities
View GitHub Profile
@macedd
macedd / express-middleware.js
Last active February 5, 2019 05:55
Nodejs measure execution time
async function logRouteExecTime(req, res, next) {
const startTime = process.hrtime();
res.once('finish', () => {
const endTime = parseHrtimeToSeconds(process.hrtime(startTime));
const payload = {
server_method: req.method,
server_url: `${req.baseUrl}${req.route ? req.route.path : ''}`,
server_params: req.params,
server_time: endTime,
@macedd
macedd / wordpress-http-debug.php
Last active January 31, 2019 14:31
Wordpress Http Requests Debug Log
<?php
class WordPress_Http_Debug
{
function __construct()
{
/**
* Uses the filter bellow to create a timer for the request
*/
add_filter('https_ssl_verify', [$this, 'request_timer']);
/**
@macedd
macedd / websocket-heartbeat-client.js
Created January 18, 2019 16:04
WebSockets heartbeat implementation (nodejs + browser)
/**
* Tracks server pings for determining if the connection dropped.
* Will terminate non-responsive connections.
* This close event should initiate the process of recreating the connection in the ws module manager (eg ws/user.js and modules/ws-user.js)
*/
function setupWsHeartbeat(ws) {
// will close the connection if there's no ping from the server
function heartbeat() {
clearTimeout(this.pingTimeout);
@macedd
macedd / LumenHorizonServiceProvider.php
Last active August 6, 2018 18:56
Lumen Horizon integration Service Provider
<?php
/**
* Horizon Provider
**/
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
@macedd
macedd / zeplin-url.js
Last active October 10, 2018 17:13
Zeplin: Convert Desktop links into Web links
(function() {
var z = window.prompt("Zeplin Link", "");
if (!z) return;
var pid = (/pid=(\w+)/).exec(z)[1];
var sid = (/sid=(\w+)/).exec(z)[1];
var u = `https://app.zeplin.io/project/${pid}/screen/${sid}`;
window.open(u, '_blank');
})();
// Oneliner to create a Bookmarklet
@macedd
macedd / mocha-timer.js
Created May 16, 2018 23:23
Mocha Sinon Fake Timer
const sinon = require('sinon');
module.exports = function() {
/**
* Fake timers (setImmediate, setTimeout)
*/
before(function() {
this.clock = sinon.useFakeTimers(new Date());
});
@macedd
macedd / global-singleton.js
Created May 9, 2018 00:21
Javascript Global App Singleton
/**
* Creates a app object in the global scope
*/
function initAppSingleton() {
global = global || window;
if (!global.app) {
global.app = {};
}
return global.app;
@macedd
macedd / server-up.sh
Created October 30, 2017 19:43
WordPress router script for running with PHP embedded server
php -S 0.0.0.0 -t /home/site /home/site/wp-router.php
@macedd
macedd / config-env.php
Created October 6, 2017 18:06
Simplest implementation of feature flags
<?php
const FEATURE_FLAGS = [
'MY_NEW_FEATURE' => false,
'MY_RELEASED_FEATURE' => true,
];
@macedd
macedd / php-ga-example.php
Last active September 14, 2017 17:48
PHP Google Analytics WordPress Example
<?php
require_once dirname( __FILE__ ) . '/lib/php-ga/src/autoload.php';
use UnitedPrototype\GoogleAnalytics;
if (!function_exists('ga_tracker')) {
function ga_tracker() {
// Initilize GA Tracker
$tracker = new GoogleAnalytics\Tracker('UA-12345678-9', 'website.com');
return $tracker;