Skip to content

Instantly share code, notes, and snippets.

// fight me
@kjin
kjin / gen.sh
Created January 4, 2018 19:30
$ git diff v1.7.0^..v1.8.0 packages/grpc-native-core/**/*.js
git clone https://github.com/grpc/grpc-node
cd grpc-node
git diff v1.7.0^..v1.8.0 packages/grpc-native-core/**/*.js > output.diff
@kjin
kjin / random.js
Last active January 19, 2018 20:42
const crypto = require('crypto');
const [_bin, _script, lenStr, algo] = process.argv;
const array = [];
const length = Number(lenStr);
if (algo === 'crypto-random-fill') { // ~23100ms for length-10000000 array
const bytes = Buffer.alloc(6);
const coeffs = [40, 32, 24, 16, 8].map(a => Math.pow(2, a));
for (let i = 0; i < length; i++) {
crypto.randomFillSync(bytes);
@kjin
kjin / cls-ah.js
Last active January 24, 2018 23:30
// async_hooks-based continuation-local storage (simplified)
const asyncHooks = require('async_hooks')
const contexts = {}
let current = {}
asyncHooks.createHook({
init: uid => {
contexts[uid] = current
},
node --require cnysa/register -e "fs.readFile('package.json', (err, contents) => { console.log('done reading') })"
+ [  0] FSREQWRAP 6:1
* [  0] FSREQWRAP 6|ex 0|tr 0 {
  + [  0] FSREQWRAP 7:6:1
}
- [ 0] FSREQWRAP 6
require('@google-cloud/trace-agent').start()
const Koa = require('koa');
const app = new Koa();
app.use(function* (next) {
yield next;
this.body = 'hi\n';
});
/* DEFAULT */
// koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md node_modules/@google-cloud/trace-agent/build/src/plugins/plugin-koa.js:99:24
@kjin
kjin / output
Created February 19, 2018 23:42
$ node ritm-1
{ name: 'express/lib/router/layer.js',
basedir: '/Users/kelvinjin/Documents/experiments/node_modules/express' }
{ name: 'express/lib/router/route.js',
basedir: '/Users/kelvinjin/Documents/experiments/node_modules/express' }
{ name: 'express/lib/router/index.js',
basedir: '/Users/kelvinjin/Documents/experiments/node_modules/express' }
{ name: 'express/lib/middleware/init.js',
basedir: '/Users/kelvinjin/Documents/experiments/node_modules/express' }
{ name: 'express/lib/middleware/query.js',
basedir: '/Users/kelvinjin/Documents/experiments/node_modules/express' }
import { AsyncResource, executionAsyncId } from 'async_hooks';
type AsyncTaskCallback<T> = (err: Error, output: T) => void;
type AsyncTask<I, O> = (input: I, cb: AsyncTaskCallback<O>) => void;
let ID = 0;
/**
* Wraps a function that represents an asynchronous task so that only a certain
* number of instances of that task can be happening at a time. If the function
kelvinjin@spicydays:experiments$ node --require cnysa/register try-http.js
+ [ 0] RANDOMBYTESREQUEST 6
+ [ 0] PROMISE 7
: [ 0] PROMISE 7
+ [ 0] PROMISE 8:7
* [ 0] PROMISE 8 {
+ [ 0] PROMISE 9
+ [ 0] TCPWRAP 10
+ [ 0] GETADDRINFOREQWRAP 11:10
+ [ 0] TickObject 12
@kjin
kjin / after.ts
Last active March 22, 2018 00:31
import * as assert from 'assert';
import { AsyncResource } from "async_hooks";
import * as express from 'express';
type AsyncTaskCallback<T> = (err: Error|null, output?: T) => void;
type AsyncTask<I, O> = (input: I, cb: AsyncTaskCallback<O>) => void;
/** USERSPACE QUEUEING LIBRARY */
// Takes an arbitrary function that represents an async task with an array of inputs and