This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(context) { | |
'use strict'; | |
var modName = 'stuff'; // used only if mod is a function and no CommonJS or AMD supported | |
// define your module here... can be a function or an object | |
var mod = {}; | |
// export | |
if (typeof module === 'object' && module.exports) { // CommonJS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// array of [outcome, weight] | |
export function weightedRandom(arr) { | |
const outcomeCuts = []; | |
let accum = 0; | |
for (const [out, weight] of arr) { | |
accum += weight; | |
outcomeCuts.push([out, accum]); | |
} | |
const r = accum * Math.random(); | |
let best; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import { webcrypto as crypto } from 'node:crypto'; // NODE | |
// eslint-disable-next-line no-undef | |
const crypto = globalThis.crypto; // BROWSER | |
const subtle = crypto.subtle; | |
const ALGO = 'AES-CBC'; | |
// HELPER FUNCTIONS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function defer<T>(): { | |
promise: Promise<T>; | |
resolve: (resp: T) => void; | |
reject: (err: any) => void; | |
} { | |
const answer: any = {}; | |
answer.promise = new Promise((resolve, reject) => { | |
answer.resolve = resolve; | |
answer.reject = reject; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { test } from 'node:test'; | |
import { strictEqual, deepEqual } from 'node:assert'; | |
/* | |
inspired by https://www.youtube.com/watch?v=umSuLpjFUf8 | |
3 3 * 4 4 * + sqrt | |
equivalent to | |
Math.sqrt(3*3 + 4*4) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function spy(fn: Function, prot = fn.prototype) { | |
function proxyFn() { | |
const args = Array.prototype.slice.call(arguments); | |
const call = { args, result: undefined, error: undefined, real: true }; | |
proxyFn.calls.push(call); | |
let responded = false; | |
// check if preparedCalls can answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'http'; | |
import https from 'https'; | |
import fetch, { Response } from 'node-fetch'; | |
import { AbortController } from 'abort-controller'; | |
const DEFAULT_TIMEOUT = 10000; | |
const httpAgent = new http.Agent({ | |
keepAlive: true, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReadStream } from 'fs'; | |
export function streamSplitBy(file, onMatch, splitBy='\n') { | |
return new Promise((resolve) => { | |
const stream = createReadStream(file); | |
let ongoing = ''; | |
stream.on('data', buff => { | |
const chunk = buff.toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SortableType = number | string | boolean; | |
type SortFn<T> = (el: T) => SortableType; | |
interface SortHelperApi<T> { | |
sortBy(fn: SortFn<T>, decrescent?: boolean): SortHelperApi<T>; | |
execute(): T[]; | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://gist.github.com/jhass/652dd780d23c1e236ff913e8a2b77eb2 | |
# http://jsbin.com/wonitaqode/edit?js,output | |
# mitmproxy -s cors.py | |
# mitmdump -s cors.py | |
from mitmproxy import http | |
def response(flow): | |
h = flow.request.headers |
NewerOlder