Skip to content

Instantly share code, notes, and snippets.

const LOOPS = 10
const [tiny, small, medium, large, huge] = [1e1, 1e2, 1e4, 1e6, 1e7]
const configs = [
{ size: tiny, tests: huge },
{ size: small, tests: large },
{ size: medium, tests: medium },
{ size: large, tests: small },
]
import { TransformStream } from "stream/web"
// Based on https://github.com/jremi/twilio-media-stream-save-audio-file/blob/69141d4ca4de3807dae5282ce5e600b110592f40/index.js
// and on https://en.wikipedia.org/wiki/WAV#WAV_file_header
const textEncoder = new TextEncoder()
const text = (input: string) => textEncoder.encode(input)
const int16 = (value: number) => new Uint8Array([value & 0xff, (value >> 8) & 0xff])
const int32 = (value: number) =>
new Uint8Array([
@ForbesLindesay
ForbesLindesay / peek.ts
Created August 5, 2025 18:09 — forked from samwho/peek.ts
Peek a node.js stream in TypeScript
/**
* Peek at the next chunk of data in the stream without consuming it.
*
* NOTE: no data will be available to read from the output stream until
* at least `length` bytes have been read from the input stream.
*
* @param input The input stream to read from.
* @param length The number of bytes to peek.
* @returns A promise that resolves to the peeked data and an output stream
/**
* Requires a stream marked as `type "bytes"`, this can support zero-copy mode,
* which might make it slightly more efficient, but it's a little more complex
* than the code for not using `{ mode: "byob" }`
*/
async function peekByob(input: ReadableStream<Uint8Array>, length: number) {
const [peekable, output] = input.tee()
const reader = peekable.getReader({ mode: "byob" })
const peeked = await readByobBytes(reader, length)
reader.cancel()

Saxes Parser State Machine

stateDiagram-v2

  # Consume optional 0xFEFF char
  BEGIN

  # Only space characters are allowed outside root element
 TEXT
export EDITOR='code_wait_new_window'
export VISUAL='code_wait_new_window'

There are now 3 modes for versioning

  • UNAMBIGUOUS - This is the default now, it will succeed only if it would make no difference which of the other two modes you chose. If the other two modes would have different outcomes, it will fail the release and force you to specify a mode.
  • ALWAYS_INCREASING - This mode is best for publishing applications, it also most closely resembles the behaviour of Rolling Versions before this change. It will treat the maximum published version as the current version, regardless of which branch you are releasing from. This means that each new release is guaranteed to have a higher version number than the previous release.
  • BY_BRANCH - This mode can be useful for libraries. It allows you to create separate branches for different major versions, and release patches to old major versions. When determining the current version, it will only consider git tags/versions that were released on the current branch. This makes it possible to release 1.0.1 after having already releas
{
"attributes": {"hello": "world"},
"data": {"my": "event"}
}
@ForbesLindesay
ForbesLindesay / pg-typed.ts
Created February 25, 2021 18:07
pg-typed doesn't have docs yet, here's the types with some comments to get you started.
import type { SQLQuery, Queryable } from '@databases/pg';
/**
* A query for multiple records that has not yet been sent to the database
*/
export interface SelectQuery<TRecord> {
all(): Promise<TRecord[]>;
orderByAsc(key: keyof TRecord): OrderedSelectQuery<TRecord>;
orderByDesc(key: keyof TRecord): OrderedSelectQuery<TRecord>;
select<TKeys extends (keyof TRecord)[]>(...fields: TKeys): SelectQuery<Pick<TRecord, TKeys[number]>>;
@ForbesLindesay
ForbesLindesay / benchmark.js
Created January 30, 2021 14:08
Array vs. Double Array for Queue
function testSimpleArray(size) {
const results = [];
let queue = [];
for (let run = 0; run < 10000; run++) {
for (let i = 0; i < size; i++) {
queue.push(i);
}
for (let i = 0; i < size; i++) {
results.push(queue.shift());
}