Skip to content

Instantly share code, notes, and snippets.

@i-like-robots
i-like-robots / fileUtils.js
Created April 27, 2021 10:23
Mocking a callback with Jest
const fs = require('fs');
const glob = require('glob');
/**
* Find files
* @param {String} globPattern
* @returns {Promise<String[]>}
*/
function findFiles(globPattern) {
return new Promise((resolve, reject) => {
@i-like-robots
i-like-robots / instrumentRequests.js
Last active June 6, 2023 13:18
Instrument Node.js HTTP requests
// Based on <https://blog.bearer.sh/http-api-instrumentation-nodejs/>
const http = require('http')
const https = require('https')
const { URL } = require('url')
const Metrics = require('metrics')
const { performance } = require('perf_hooks')
const metrics = {}
const metricTypes = {
@i-like-robots
i-like-robots / upload-to-s3.sh
Last active October 1, 2019 10:52
Upload client-side assets to S3 via AWS CLI
#!/bin/bash
SOURCE_FILES=($(find public/*.{js,css,gz,br,map} 2>/dev/null))
DESTINATION_BUCKET="hashed-assets-eu"
DESTINATION_FOLDER="page-kit"
for FILE in ${SOURCE_FILES[@]}; do
if [[ $FILE == *".gz" ]]; then
@i-like-robots
i-like-robots / fetch.js
Created March 17, 2019 11:47
Signed AWS requests with node fetch
const nodeFetch = require('node-fetch');
const httpsAgent = require('./httpsAgent');
const handleResponse = require('./handleResponse');
module.exports = async (url, options = {}) => {
const response = await nodeFetch(url, {
...options,
agent: httpsAgent
});
@i-like-robots
i-like-robots / hyperons.js
Last active January 29, 2019 19:33
A quick experiment refactoring Hyperons to output real DOM nodes
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.hyperons = {}));
}(this, function (exports) {
'use strict';
/**
* Base Component class
* @param {object} props The initial component props
@i-like-robots
i-like-robots / highlight.scss
Created December 19, 2017 18:22
Monokai Pro Pygments/Rouge theme
.highlight { background: #282A3A; color: #EAF2F1 }
.ge { font-style: italic } // Generic.Emph
.gs { font-weight: bold } // Generic.Strong
.gi { color: #BAD761 } // Generic.Inserted
.gd { color: #FF657A } // Generic.Deleted
.err { color: #FF657A } // Error
.nc, // Name.Class
@i-like-robots
i-like-robots / index.html
Created September 15, 2017 10:27
Normalize onChangeCapture
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Preact Compat onChange normalization</title>
</head>
<body>
<div id="app"></div>
<script src="dist/main.js"></script>
</body>
@i-like-robots
i-like-robots / ads.js
Last active January 28, 2021 14:16
Minimum viable IMA implementation for desktop and mobile with support for autoplay when available and basic error handling.
'use strict'
const CLASSNAME_WAITING = 'is-waiting'
const CLASSNAME_LOADING = 'is-loading'
const CLASSNAME_PREROLL = 'is-preroll'
const CLASSNAME_PLAYING = 'is-playing'
const CLASSNAME_PROBLEM = 'is-problem'
// <https://developers.google.com/interactive-media-ads/docs/sdks/html5/sdk-player>
function ads (target) {
@i-like-robots
i-like-robots / toggle.js
Created July 19, 2016 16:08
universal accessible toggle button
module.exports = function (buttons, target, callback) {
buttons = buttons.length === undefined ? [ buttons ] : Array.from(buttons);
const toggle = function (e) {
const state = target.classList.toggle('is-active');
buttons.forEach(button => {
button.setAttribute('aria-expanded', state);
});
@i-like-robots
i-like-robots / webpack.config.js
Created May 9, 2016 16:25
Minimum Webpack config to output separate stylesheets with sourcemaps
'use strict'
const ExtractTextPlugin = require('extract-text-webpack-plugin')
module.exports = {
entry: {
'./public/main.css': './src/styles.scss'
},
output: {
filename: '[name]'