Skip to content

Instantly share code, notes, and snippets.

View watson's full-sized avatar

Thomas Watson watson

View GitHub Profile
@watson
watson / four-byte-emojis.json
Last active March 26, 2024 13:35
Emoji's sorted by byte-size
[
"😁",
"😂",
"😃",
"😄",
"😅",
"😆",
"😉",
"😊",
"😋",
@watson
watson / README.md
Last active July 13, 2023 22:42
A list of search and replace unix commands to help make a node repository 'standard' compliant

The standard code style linter is a great tool by Feross - check it out!

Remove trailing semicolons:

find . -path ./node_modules -prune -o -type f -name '*.js' -exec sed -i '' -e 's/;$//' {} \;

Ensure space between function and opening bracket:

@watson
watson / redos.js
Last active December 15, 2022 17:11
Node.js ReDoS example
// for more info about ReDoS, see:
// https://en.wikipedia.org/wiki/ReDoS
var r = /([a-z]+)+$/
var s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa!'
console.log('Running regular expression... please wait')
console.time('benchmark')
r.test(s)
@watson
watson / finished.js
Last active November 2, 2022 10:06
Is this expected behaviour of stream.finished?
const { PassThrough, finished } = require('stream');
console.log('Waiting for stream to finish...');
finished(new PassThrough(), (err) => {
// This callback is never called and the process exits with exit code 0 right away
console.log('Stream has finished!', { err });
});
@watson
watson / airplay.md
Last active February 5, 2022 03:41
An overview over my AirPlay related modules
@watson
watson / bisect.sh
Last active January 27, 2022 10:55
Automated git bisect to find breaking commit in Node core. This example was used to find a specific issue, so it should of course be modified to fit your needs. These files are ment to be put inside the root of your locally cloned node repo.
#!/usr/bin/env bash
# This script is an easy way to bisect between two releases of Node.js
if [ $# -ne 2 ]; then
echo "Error: No arguments supplied!"
echo "Usage:"
echo " ./bisect.sh <git-ref> <git-ref>"
echo
echo "Example:"
@watson
watson / ability.rb
Created October 5, 2011 09:50
Active Admin CanCan integration with shared front/backend User model and multi-level autherization
# app/models/ability.rb
# All front end users are authorized using this class
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
@watson
watson / html5_audio.js
Created October 25, 2010 22:31
Get a HTML5 audio element to replay without loading it from the server again
var audio = document.getElementById('my_audio');
audio.currentTime = 0;
audio.play();
@watson
watson / worker_thread.js
Last active January 15, 2020 08:42
Using Node.js worker threads to guard against ReDoS attacks
'use strict'
const { Worker } = require('worker_threads')
const worker = new Worker(`
const { workerData, parentPort } = require('worker_threads')
const result = workerData.str.match(workerData.regex)
parentPort.postMessage(result)
`, {
workerData: {
@watson
watson / install-elasticsearch.ps1
Last active January 13, 2020 17:39
Install and run Elasticsearch in Windows PowerShell
Write-Host "Preparing to download and install Elasticsearch..." -ForegroundColor Cyan
$esVersion = "6.1.2"
$downloadUrl = "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$($esVersion).zip"
$zipPath = "$($env:USERPROFILE)\elasticsearch-$esVersion.zip"
$extractRoot = "$env:SYSTEMDRIVE\Elasticsearch"
$esRoot = "$extractRoot\elasticsearch-$esVersion"
Write-Host "Downloading Elasticsearch..."
(New-Object Net.WebClient).DownloadFile($downloadUrl, $zipPath)
7z x $zipPath -y -o"$extractRoot" | Out-Null