Skip to content

Instantly share code, notes, and snippets.

Avatar

Zach Bjornson zbjornson

View GitHub Profile
View test.js
//@ts-check
"use strict";
const TOTP = require("../../app/util/totp.js");
const assert = require("chai").assert;
describe("TOTP", function () {
describe("verify", function () {
it("works according to RFC6238 test vectors", function () {
// From https://www.rfc-editor.org/rfc/rfc6238.html#appendix-B
@zbjornson
zbjornson / fastvector.cc
Created Aug 31, 2019
Fast vector experiments
View fastvector.cc
// Essentially a std::vector<uint8_t>, but uses mmap+mremap to manipulate the
// Linux page table. Not portable. Memory is not freed when the class is
// destructed; use FastVector::free.
// In some basic benchmarks, this is not faster than realloc (with glibc),
// although in principle it should provide better guarantees that data movement
// is avoided (regardless of alloc size).
#include <sys/mman.h>
@zbjornson
zbjornson / accurate.cc
Last active Apr 16, 2021
Code for fast, accurate float summation
View accurate.cc
#include <cstddef>
#include <cmath>
#ifdef __FAST_MATH__
#error Compensated summation is unsafe with -ffast-math (/fp:fast)
#endif
inline static void kadd(double& sum, double& c, double y) {
y -= c;
auto t = sum + y;
c = (t - sum) - y;
View jest-native-idempotent.js
/**
* Jest's require() cache is reset between tests (modules are re-evaluated)
* for most, but not all modules. In the "not all" group are native modules,
* so all interactions with them must be idempotent.
* https://github.com/facebook/jest/issues/4413
*
* Encountered in https://github.com/Automattic/node-canvas/issues/1310,
* https://github.com/Automattic/node-canvas/issues/1294,
* https://github.com/Automattic/node-canvas/issues/1250
*/
View dns.js
const name = "<some domain>";
const AWS = require("aws-sdk");
const route53 = new AWS.Route53();
const DNS = require("@google-cloud/dns");
const dns = new DNS({projectId: "<your project id>"});
const dnsZone = dns.zone("<zone name>");
const resolvers = [ // See note below -- set to the NS for your zone
"<dns server 1>",
"<dns server 2>"//, ...
View hoistDotArgs.R
hoistDotArgs = function(fn) {
dotCalls = list()
findDotArgs = function(exp) {
if (typeof(exp) == "closure") { # entry point
expL = as.list(exp)
args = names(expL)
if (!("..." %in% args)) return(invisible()) # no dots to hoist
body = expL[[length(expL)]]
findDotArgs(body)
View styler.js
/**
* Tool to change styles with dynamic CSS. Use instead of setting style
* properties on individual elements.
*/
function Styler() {
this.styleEl = document.createElement("style");
document.head.appendChild(this.styleEl);
// Map for quickly looking up an existing rule by its selector.
this.ruleMap = new Map();
}
@zbjornson
zbjornson / github-issues.wl
Last active Dec 14, 2016
GitHub Issue Charts in Mathematica
View github-issues.wl
(* Common: *)
ToJSON[x_] := ImportString[FromCharacterCode[x, "Unicode"], "RawJSON"](*Ignore emoji*)
getIssues[repo_] := getIssues[repo, "*"]
getIssues[repo_, milestone_] :=
getIssues[repo, milestone] = Block[{url, head, pageCount, issues},
url = URLBuild[{"https://api.github.com/repos", repo, "issues"},
{"state" -> "all",
(*"access_token" -> "...",*) (* use for private repos, larger API quota*)
"sort" -> "created",
View EmbeddedTLB
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: VWorksCPPATL.dll
[
uuid(83ED4BB2-4E00-4349-AD9E-493801A0FC62),
version(1.0),
custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 134218331),
custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1471199125),
custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, "Created by MIDL version 8.00.0603 at Sun Aug 14 11:25:24 2016
View example.js
var http = require('http');
var server = http.createServer(function (req, res) {
req.connection.unref(); console.log("Got request. Connection unref'ed again");
res.writeHead(200, {"Content-Type": "text/plain"});
setTimeout(function () {
console.log("Handler timeout invoked");
res.end("Hello", function (err) {;