Skip to content

Instantly share code, notes, and snippets.

View domenic's full-sized avatar

Domenic Denicola domenic

View GitHub Profile
@domenic
domenic / custom-element-super-swap.js
Last active October 23, 2015 19:29 — forked from esprehn/custom-element-super-swap.js
Custom element super swap algorithm
/*
This is a rough approximation of what the algorithm woud do. In an
implementation you might make the JS Wrapper point to a different
C++ Element for the duration of the constructor, then make it
point back at the original C++ element, and move data between them.
This means that the C++ side remains consistent, if you querySelector
from the document you can still find your element where the constructor
is running, but if you look at the parentNode/firstChild or attributes
properties of the element they all appear empty. This means in the
(function(global, binding, v8) {
'use strict';
const defineProperty = global.Object.defineProperty;
class ByteLengthQueuingStrategy {
constructor(options)
{
defineProperty(this, 'highWaterMark', {
value : options.highWaterMark,
@domenic
domenic / asyncdefer.svg
Created September 10, 2015 19:54
Async/defer SVG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@domenic
domenic / readable-stream-progress.js
Last active February 10, 2020 17:05
XHR-esque progress events on top of streams
function processBodyChunkwiseWithProgress(res, processChunk) {
const dummyEventTarget = document.createElement("div"); // why isn't EventTarget constructible? :(
const lengthComputable = res.headers.has("Content-Length");
const total = res.headers.get("Content-Length") || 0;
let loaded = 0;
// Using http://underscorejs.org/#throttle
const fireProgressThrottled = _.throttle(fireProgress, 50, { trailing: false });
var promise = paymentRequest(supportedInstruments, details, schemeData);
promise.then(function (paymentConfigurator) { // needs a slightly better name
paymentConfigurator.addEventListener("shippingAddressChange", function () { // no need for event
var newAddress = paymentConfigurator.address;
console.log(newAddress.street, newAddress.city, newAddress.state, newAddress.zip, newAddress.country);
// Return back new shipping options
paymentConfigurator.updateShippingOptions(newShippingOpions);
});
@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 / jsdom6.md
Last active August 29, 2015 14:26
jsdom 6.0 changelog draft

6.0.0

This major release is focused on massive improvements in speed, URL parsing, and error handling. The potential breaking changes are highlighted in bold below; the largest ones are around the jsdom.env error-handling paradigm.

This release also welcomes long-time contributer @Joris-van-der-Wel to the core team. You may recognize him from earlier changelogs. We're very happy to have his help in making jsdom awesome!

  • io.js 2.0 onward is now required, as we have begun using ES2015 features only present there.
  • Improved performance dramatically, by ~10000x in some cases, due to the following changes:
    • Overhauled the named properties tracker to not walk the entire tree, thus greatly speeding up the setting of id and name attributes (including during parsing).
@domenic
domenic / image-flipper.js
Created June 24, 2015 06:49
Image-flipping server
"use strict";
const http = require("http");
const url = require("url");
const request = require("request");
const jsdom = require("jsdom");
const Canvas = require("canvas");
http.createServer(function (req, res) {
console.log(req.url);
const pageUrl = url.parse(req.url, true).query["proxy-me"];
@domenic
domenic / emergency-fund.md
Last active August 29, 2015 14:20
Emergency fund model

Inputs (defaults in parens):

  • F = An investment fund (SPX, or 70/30 split, or, ...)
  • Y = years until retirement (40)
  • S = starting year when you make the decision between strategies. At least Y years in the past since we want to draw on historical data. (now - Y)
  • T = total starting capital. This should be the amount of accumulated lifetime savings you have in non-retirement accounts, and you need to decide what strategy to use on it. ($100K)
  • E = emergency fund value. Sources suggest 3-6 months living expenses. ($20K)
  • P = probability that you need to access $E in an emergency, some time between S and S+Y. (10%???)
  • X = the year you need access to $E, in P*100% of universes (see below)
  • strategy = { invest, emergency fund }
  • invest = put all of T into F
@domenic
domenic / ReadableStream.js
Created April 28, 2015 14:36
ReadableStream.js
(function() {
'use strict';
const readableStreamClosedPromise = %CreatePrivateOwnSymbol('[[closedPromise]]');
const readableStreamCloseRequested = %CreatePrivateOwnSymbol('[[closeRequested]]');
const readableStreamController = %CreatePrivateOwnSymbol('[[controller]]');
const readableStreamPullAgain = %CreatePrivateOwnSymbol('[[pullAgain]]');
const readableStreamPulling = %CreatePrivateOwnSymbol('[[pulling]]');
const readableStreamQueue = %CreatePrivateOwnSymbol('[[queue]]');
const readableStreamReader = %CreatePrivateOwnSymbol('[[reader]]');