Skip to content

Instantly share code, notes, and snippets.

@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);
@domenic
domenic / escape-vm.js
Created August 17, 2015 20:20
Escaping the vm sandbox
"use strict";
const vm = require("vm");
const sandbox = { anObject: {} };
const whatIsThis = vm.runInNewContext(`
const ForeignObject = anObject.constructor;
const ForeignFunction = ForeignObject.constructor;
const process = ForeignFunction("return process")();
const require = process.mainModule.require;
require("fs");
@domenic
domenic / 1-Domenic.js
Last active September 30, 2021 15:43
How DI container config should work (in JS)
"use strict";
// Domenic needs a Tweeter
function Domenic(tweeter) {
this.tweeter = tweeter;
}
Domenic.inject = ["tweeter"];
Domenic.prototype.doSomethingCool = function () {
return this.tweeter.tweet("Did something cool!");
@domenic
domenic / README.md
Last active June 24, 2021 16:37
Using promises in a UI context

The scenario:

  • We are writing a digital textbook-reading app.
  • Most of the time you have a "basic" license for your textbook, but one (and only one) of your computers can request an "enhanced" license.
  • You can only print from the computer with the enhanced license.

The problem statement:

@domenic
domenic / v8-versions.md
Last active December 11, 2020 01:45
V8 versions for embedders

Embedders of V8 should generally use the head of the branch corresponding to the minor version of V8 that ships in Chrome.

Finding the minor version of V8 corresponding to the latest stable Chrome

To find out what version this is,

  1. Go to https://omahaproxy.appspot.com/
  2. Find the latest stable Chrome version in the table
  3. Enter it into the "Translate a Chrome verison to a V8 version" box below the table.
  4. Ignore the last two parts.
@domenic
domenic / web-components-reflection.md
Last active November 11, 2020 15:37
Web components reflection

Web components reflection

What is a subset of HTML's reflection rules which we should make easy for web components?

Summary of HTML

HTML's reflection algorithms include:

  • URL strings
  • Enumeration strings ("limited to only known values")
@domenic
domenic / same-site.md
Created September 28, 2020 15:40
Alternate same-site algorithms

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 / 1-CharacterData.webidl
Last active September 9, 2020 22:16
webidl2js example
[Exposed=Window]
interface CharacterData : Node {
attribute [TreatNullAs=EmptyString] DOMString data;
readonly attribute unsigned long length;
DOMString substringData(unsigned long offset, unsigned long count);
void appendData(DOMString data);
void insertData(unsigned long offset, DOMString data);
void deleteData(unsigned long offset, unsigned long count);
void replaceData(unsigned long offset, unsigned long count, DOMString data);
};
@domenic
domenic / not-bad-code.js
Last active July 4, 2020 09:07
Avoiding explicit promise construction antipattern
function getUserDetail(username) {
if (userCache[username]) {
return Promise.resolve(userCache[username]);
}
// Use the fetch API to get the information
return fetch('users/' + username + '.json')
.then(function(result) {
userCache[username] = result;
return result;
@domenic
domenic / library-consumer.mjs
Created July 11, 2019 15:32
Readonly Sets attempt
import { languagesSet } from "./some-library.mjs";
// Allowed
languagesSet.keys();
languagesSet.entries();
languagesSet.forEach(l => console.log);
languagesSet.size;
languagesSet.values();
// Will throw TypeError