Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / opensearch.xml
Last active February 19, 2023 02:33
opensearch.xml for HTML Standard
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>HTML Standard</ShortName>
<Description>Search the WHATWG HTML Living Standard.</Description>
<Image width="16" height="16" type="image/x-icon">data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC2klEQVR4Xm3TbUxWZRjA8f95eY4PvrTGB0UGYxUuKjVYoRauBa0aai5rxbKcLxvSEBZogG5uQc4NncUe1HDU+uBMCiLUKKmRUGLzDQJdKQjqHLpswOppPC/nHJ6rcz6c7Ynx+3B/uq/r3nVf16Ws3DWbadJEpGAqZq+1Y9YjCKqmamOa5utUFfU4cIE4Ov+3IWpH9iMkJT2YStr8DHy6wb2J2/NHx4cfD5uhrQlGQj0o1UAYQMUDlc6Fo+lJS5L2rD9Ga9UQBws7+GjTSZp2DPBp8Vlyl6zzh6LhypjEWoF58QnecYL35WTk01D0E88vXseJC42Uf76GLYeeZW/LVkLRILUbmtmcV4Uds/NFYkcAXQVSTSsaWLRwKe7Lhs/Pu0dyqWkuxbItnkjNpneky0mUR5uTtGR1LW88U0zYjKwH3iJnV0JF9vuanO49Jq6OviZ5rASpb68Qz+Bon7h3Cg8/J67xf+/Lqj0psqLSdwrn+O6VvWkSnJwQ193xW9Jz7XsJhibE0/NHuywtQ0obXxbPh19tkazt3ObpHVzb1viizMQLfqk6WZZXzpYzV1rFc/znOsks5x9VwGfos5jJ+aEfnY98
@domenic
domenic / README.md
Last active February 1, 2023 17:17
Generic zero-copy ArrayBuffer

Generic zero-copy ArrayBuffer usage

Most APIs which accept binary data need to ensure that the data is not modified while they read from it. (Without loss of generality, let's only analyze ArrayBuffer instances for now.) Modifications can come about due to the API processing the data asynchronously, or due to the API processing the data on some other thread which runs in parallel to the main thread. (E.g., an OS API which reads from the provided ArrayBuffer and writes it to a file.)

On the web platform, APIs generally solve this by immediately making a copy of the incoming data. The code is essentially:

function someAPI(arrayBuffer) {
  arrayBuffer = arrayBuffer.slice(); // make a copy
@domenic
domenic / q-nexttick-times.md
Created May 27, 2012 06:30
Q test suite nextTick results

Comparing Different Implementations of process.nextTick using the Q test suite.

Used: q-spec.js@a1a416.

Implementations compared:

Based on MessageChannel

This is currently in Q:

@domenic
domenic / 1-service-worker.js
Last active October 20, 2022 11:10
Service worker stream transferring
"use strict";
const worker = new Worker("worker.js");
self.onfetch = e => {
const transform = new TransformStream(); // creates an identity transform
e.respondWith(new Response(transform.readable));
// Give the worker the writable end. An identity transform stream will just shuffle
// bytes written there into transform.readable.
@domenic
domenic / async-generators.js
Last active September 21, 2022 10:46
Push vs. pull async generators
async function* foo() {
console.log("1");
await Q.delay(1000);
console.log("2");
yield "a";
console.log("3");
await Q.delay(1000);
console.log("4");
yield "b";
}
@domenic
domenic / 1-DOMPointReadOnly.idl
Created February 29, 2016 12:34
webidl2js DOMPoint example
[Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double z = 0, optional unrestricted double w = 1),
Exposed=(Window,Worker)]
interface DOMPointReadOnly {
[NewObject] static DOMPointReadOnly fromPoint(optional DOMPointInit other);
readonly attribute unrestricted double x;
readonly attribute unrestricted double y;
readonly attribute unrestricted double z;
readonly attribute unrestricted double w;
@domenic
domenic / interop.md
Last active July 7, 2022 19:47
`module.exports =` and ES6 Module Interop in Node.js

module.exports = and ES6 Module Interop in Node.js

The question: how can we use ES6 modules in Node.js, where modules-as-functions is very common? That is, given a future in which V8 supports ES6 modules:

  • How can authors of function-modules convert to ES6 export syntax, without breaking consumers that do require("function-module")()?
  • How can consumers of function-modules use ES6 import syntax, while not demanding that the module author rewrites his code to ES6 export?

@wycats showed me a solution. It involves hooking into the loader API to do some rewriting, and using a distinguished name for the single export.

This is me eating crow for lots of false statements I've made all over Twitter today. Here it goes.

@domenic
domenic / default-anon.md
Last active June 29, 2022 01:05
Default anonymous export = module instance object

Kevin's "default anonymous export" idea.

Let anon be the symbol denoting anonymous export.

Assume the following syntaxes:

export default [Expression]; // sets the anonymous export
export [Declaration] // sets a named export
@domenic
domenic / oauth2-restify.js
Created June 2, 2012 06:25
OAuth2 with Restify
"use strict";
var restify = require("restify");
var users = require("./users");
// The users module will have a getAuthorizationFromAccessTokenAsync promise-returning export. (Convert to callbacks if you wish).
// It rejects in cause of not authorized, or fulfills with a { scope, customerId } object if the user is authorized.
// The scope property indicates which scopes the user corresponding to a given access token has.
module.exports = function authPlugin(serverRequest, serverResponse, next) {
@domenic
domenic / event-emitter.js
Last active March 11, 2022 15:25
Revealing constructor pattern event-emitter
// This event emitter emits events, but reserves the right to publish events to
// for its creator. It uses a WeakMap for true encapsulation.
const eesToEventMaps = new WeakMap();
export default class EventEmitter {
constructor(publisher) {
const eventMap = Object.create(null);
eesToEventMaps.set(this, eventMap);