Skip to content

Instantly share code, notes, and snippets.

View watson's full-sized avatar

Thomas Watson watson

View GitHub Profile
@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 / 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: {
'use strict'
const asyncHooks = require('async_hooks')
const asyncIds = new Set()
const asyncHook = asyncHooks.createHook({
init (asyncId, type, triggerAsyncId, resource) {
const eid = asyncHooks.executionAsyncId()
process._rawDebug(`${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}`, resource)
@watson
watson / destroy-test.js
Last active September 1, 2018 19:58
Testing write on corked stream in combination with destroy for readable-stream@3
'use strict'
const { Writable } = require('readable-stream')
let stream
console.log('write')
stream = getStream()
stream.write('foo')
@watson
watson / server.js
Created August 27, 2018 14:17
Mock Elastic APM Server that always responds with an error before bothering to read the entire request body
'use strict'
const zlib = require('zlib')
const http = require('http')
const body = zlib.gzipSync(JSON.stringify({
errors: {
ERR_QUEUE_FULL: {
count: 23,
message: 'queue is full'
@watson
watson / server.js
Created August 27, 2018 12:49
HTTP server that half-closes the socket when an incoming HTTP request is received
'use strict'
const http = require('http')
const server = http.createServer(function (req, res) {
console.log(req.method, req.url)
res.socket.end() // Half-close the socket by sending FIN packet
})
server.listen(8200, function () {
@watson
watson / boom.js
Last active February 22, 2018 21:01
Breaks in Node.js 9.6.0
var assert = require('assert')
var events = require('events')
assert(events.usingDomains)
@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
@watson
watson / install-redis.ps1
Last active January 23, 2018 09:59 — forked from FeodorFitsner/install-redis.ps1
Install Redis
Write-Host "Downloading Redis..." -ForegroundColor Cyan
$redisRoot = "$env:SYSTEMDRIVE\Redis"
$zipPath = "$($env:USERPROFILE)\redis-2.8.19.zip"
(New-Object Net.WebClient).DownloadFile('https://github.com/MSOpenTech/redis/releases/download/win-2.8.19/redis-2.8.19.zip', $zipPath)
7z x $zipPath -y -o"$redisRoot" | Out-Null
del $zipPath
Write-Host "Installing Redis as a Windows service..."
& "$redisRoot\redis-server.exe" --service-install