Skip to content

Instantly share code, notes, and snippets.

Avatar

Kris Kowal kriskowal

View GitHub Profile
View stream-with-context.js
const streamWithContext = (context, stream) => ({
async next(value) {
return Promise.race([context.cancelled, stream.next(await value)]);
},
async return(value) {
return Promise.race([context.cancelled, stream.return(await value)]);
},
async throw(error) {
return Promise.race([context.cancelled, stream.throw(await error)]);
},
View interleaving.js
async function avery() {
await null;
for (let i = 0; i < 10; i += 1) {
console.log('await');
await null;
}
}
function blake() {
View metered-input.js
// @ts-check
/**
* @template T
* @typedef {{
* promise: Promise<T>,
* resolve: (value:T) => void,
* reject: (reason:Error) => void
* }} Deferred
*/
@kriskowal
kriskowal / gist:f25c81725f0d5c98d41c1e6b7ee8f8fe
Created Oct 19, 2020
Debugging a zip library refactor. The bug was a uint32 where a uint16 belonged.
View gist:f25c81725f0d5c98d41c1e6b7ee8f8fe
00000000: 504b 0304 0a00 0000 0000 3514 5151 849e PK........5.QQ..
^^^^ ^^^^ LOCAL_FILE_HEADER
^^^^ version 10
^^^^ bitFlag
^^^^ compression method
^^^^ ^^^^ date
00000010: e8b4 0e00 0000 0e00 0000 0900 0000 6865 ..............he
^^^^ crc32
^^^^ ^^^^ compressed size (14)
^^^^ ^^^^ uncompressed size (14)
View compartment-dependency.js
const dependency = new Compartment({}, {}, {
resolveHook: (moduleSpecifier, moduleReferrer) =>
resolve(moduleSpecifier, moduleReferrer),
importHook: async moduleSpecifier => {
const moduleLocation = locate(moduleSpecifier);
const moduleText = await retrieve(moduleLocation);
return new ModuleStaticRecord(moduleText, moduleLocation);
},
});
const application = new Compartment({}, {
View compartment-import.js
compartment
.import('./main.js')
.then(({ namespace: main }) => {
// …
});
View lockdown.js
import 'ses';
lockdown();
let compartment = new Compartment();
compartment.evaluate(code);
View async-composition-2020.js
function *count(n) {
for (let i = 0; i < n; i++) {
yield i;
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const asyncForEach = async (values, callback) => {
@kriskowal
kriskowal / assemble.js
Created May 15, 2020
Compartment assembler demonstration.
View assemble.js
import 'ses';
import { resolveNode, makeNodeImporter } from './node.js';
const { values, entries } = Object;
// gathers all the packages in a lockfile into a flat representation keyed by
// name and version.
const gather = (pkg, map, location) => {
const key = `${pkg.name}/${pkg.version}`;
if (pkg.dependencies != null) {
View band-names.txt
20 points above the monkey
A Dearth of Crosswalks (via ahuster)
Angry Brain Explosion (via fudco)
Anonymous
Apparently Music? (via ahuster)
Awsomosis
Bad Math
Banned Name (via ahuster)
Bdemonic Devices
Betrayed by the Bagel (via ahuster)