Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / generate-urltestdata.js
Created December 30, 2022 09:53
urltestdata.json generator using jsdom/whatwg-url
View generate-urltestdata.js
"use strict";
const { URL } = require(".");
const inputs = [
"https://\u0000y",
"https://x/\u0000y",
"https://x/?\u0000y",
"https://x/?#\u0000y"
];
@domenic
domenic / README.md
Last active February 1, 2023 17:17
Generic zero-copy ArrayBuffer
View README.md

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 / same-site.md
Created September 28, 2020 15:40
Alternate same-site algorithms
View same-site.md

Two origins, A and B, are said to be same site if the following returns true:

  1. Let siteA be the result of obtaining a site from A.
  2. Let siteB be the result of obtaining a site from B.
  3. Return true if siteA equals siteB.

Two origins, A and B, are said to be schemelessly-same site if the following returns true:

  1. Let siteA be the result of obtaining a site from A.
  2. Let siteB be the result of obtaining a site from B.
@domenic
domenic / library-consumer.mjs
Created July 11, 2019 15:32
Readonly Sets attempt
View library-consumer.mjs
import { languagesSet } from "./some-library.mjs";
// Allowed
languagesSet.keys();
languagesSet.entries();
languagesSet.forEach(l => console.log);
languagesSet.size;
languagesSet.values();
// Will throw TypeError
@domenic
domenic / engine-ua-sniffer.js
Last active September 6, 2019 16:40
Rendering engine UA sniffer
View engine-ua-sniffer.js
// This is a minimal UA sniffer, that only cares about the rendering/JS engine
// name and version, which should be enough to do feature discrimination and
// differential code loading.
//
// This is distinct from things like https://www.npmjs.com/package/ua-parser-js
// which distinguish between different branded browsers that use the same rendering
// engine. That sort of distinction is maybe useful for analytics purposes, but
// for differential code loading it is overcomplicated.
//
// This is meant to demonstrate that UA sniffing is not really that hard if you're
@domenic
domenic / tostring-in-js.md
Last active July 9, 2019 21:21
ToString() in JavaScript
View tostring-in-js.md

ToString() in JavaScript

I am posting this as a gist because I am too lazy to figure out my blog. TODO: move it to https://domenic.me/ one day.

A lot of specifications use the ToString() abstract operation. (What's an abstract operation?) For example, any web specification which uses Web IDL's DOMString type (i.e., its basic string type) will convert incoming values using ToString(). Similarly, various parts of the JS specification itself perform ToString(). One example is the Error constructor, which we will refer to going forward.

If you are trying to implement or polyfill such a specification in JavaScript, how do you do it? For example, given

function ErrorConstructorPolyfill(message) {
@domenic
domenic / after-markdown-conversion.html
Created April 26, 2019 21:39
Working mode before and after
View after-markdown-conversion.html
<!DOCTYPE HTML>
<html lang="en">
<meta charset="utf-8">
<title>Working Mode — WHATWG</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#3A7908">
<link rel="icon" href="https://resources.whatwg.org/logo.svg">
<link rel="stylesheet" href="/style/shared.css">
<link rel="stylesheet" href="/style/subpages.css">
@domenic
domenic / difference.md
Last active January 16, 2019 20:19
Set.prototype.difference, simplified
View difference.md

Context: tc39/proposal-set-methods#50

Proposed spec texts

Set.prototype.difference(arg), minimalist

  1. If Type(this) is not Object, or this does not have a [[SetData]] internal slot, throw a new TypeError exception.
  2. If Type(arg) is not Object, or arg does not have a [[SetData]] internal slot, throw a new TypeError exception.
  3. Let diffList be a List containing all elements that are in this.[[SetData]] but are not in arg.[[SetData]], using SameValueZero to determine identity. (Optional: formalize this with a loop that operates on the Lists.)
  4. Let newSet be ? OrdinaryCreateFromConstructor(%Set%, "%SetPrototype%", « [[SetData]] »).
@domenic
domenic / link-stuff.md
Last active December 22, 2018 17:42
Revamping link fetching and stuff
View link-stuff.md
@domenic
domenic / html-style.html
Created July 31, 2018 16:49
HTML Standard inline stylesheet
View html-style.html
<style>
.status { min-height: 0.6em; font: 1em sans-serif; width: 9em; padding: 0.3em; position: absolute; z-index: 8; right: 0.3em; background: #EEE; color: black; box-shadow: 0 0 3px #999; overflow: hidden; margin: -2em 0 0 0; border-collapse: initial; border-spacing: initial; }
.status:hover { z-index: 9; }
.status:focus-within { z-index: 9; }
.status.wrapped > :not(input) { display: none; }
.status > input { position: absolute; left: 0; top: 0; width: 1em; height: 1em; border: none; background: transparent; padding: 0; margin: 0; }
.status > p { font-size: 0.6em; margin: 0; padding: 0; }
.status > p + p { padding-top: 0.5em; }
.status > p > strong { margin-left: 1.5em; }
.status > .support { display: block; }