Skip to content

Instantly share code, notes, and snippets.

@nichoth
nichoth / index.ts
Last active February 19, 2025 04:03
Blake3 streaming + hashing
import * as blake3 from 'blake3';
async function streamHash(stream:ReadableStream<Uint8Array>):Promise<string> {
const hasher = blake3.createHash();
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
@nichoth
nichoth / demo.ts
Created December 27, 2024 04:19 — forked from vedantroy/demo.ts
SQLite-backed key-value store with JS-like object manipulation and automatic JSON serialization.
import Database from 'better-sqlite3';
import { createDatabaseClient } from './proxy.ts';
// 1) Create an in-memory DB and your table(s).
const db = new Database(':memory:');
db.exec(`
CREATE TABLE users (
id TEXT PRIMARY KEY,
data JSON
);
@nichoth
nichoth / webtorrent_demo.js
Created December 11, 2024 19:01 — forked from swapnilshrikhande/webtorrent_demo.js
WebTorrent Configure Custom STUN / TURN Server
var client = window.client = new WebTorrent({
tracker: {
rtcConfig: rtcConfig
}
})
client.on('warning', onWarning)
client.on('error', onError)
torrent = client.add(TORRENT, onTorrent)
@nichoth
nichoth / fflate.md
Last active November 30, 2024 00:06 — forked from 101arrowz/README.md
How FFlate works

FFlate is the fastest, smallest, and most effective JavaScript compressor/decompressor currently; to create it, I used a variety of modified or optimized algorithms.

Part 1: The DEFLATE spec

The core of most popular compressed file formats is DEFLATE, or RFC1951. GZIP data, Zlib data, .zip files, PNG images, and more all use some variant of DEFLATE under the hood. At its core, the DEFLATE format is actually not too complex: it's merely a combination of Huffman coding and LZ77 compression.

If you don't understand either of these concepts, feel free to read the following subsections, or skip to Part 2 if you already know what these are and just want to get to implementation details.

Section I: Huffman Coding

Computers think of basically everything as numbers. The smallest unit of information in a computer is a single bit, which can only be either a 0 or a 1. When multiple bits are stringed together, they can be interpreted as a ba

@nichoth
nichoth / force-sync-jazz-state.ts
Created November 19, 2024 19:00 — forked from Schniz/force-sync-jazz-state.ts
force sync jazz state
async function forceSync(account: Account) {
const { syncManager } = account._raw.core.node;
const peer = Object.values(syncManager.peers)[0];
if (!peer) {
throw new Error("no peer");
}
const values = Object.values(account._raw.core.node.coValues).flatMap((x) => {
if ("coValue" in x.state) {
@nichoth
nichoth / crypto-pbkdf2.js
Created October 25, 2024 21:03 — forked from chrisveness/crypto-pbkdf2.js
Uses the SubtleCrypto interface of the Web Cryptography API to hash a password using PBKDF2, and validate a stored password hash against a subsequently supplied password. Note that both bcrypt and scrypt offer better defence against ASIC/GPU attacks, but are not available within WebCrypto.
/**
* Returns PBKDF2 derived key from supplied password.
*
* Stored key can subsequently be used to verify that a password matches the original password used
* to derive the key, using pbkdf2Verify().
*
* @param {String} password - Password to be hashed using key derivation function.
* @param {Number} [iterations=1e6] - Number of iterations of HMAC function to apply.
* @returns {String} Derived key as base64 string.
*
@nichoth
nichoth / recipe.js
Last active October 7, 2024 18:03
Find how many ingredients you need to buy
//
// I always like an opportunity to use `Set` in JS.
// There should be `difference` and `intersection` operators
// in new environments, but my Node didn't have them,
// so we're doing it slightly more laboriously.
// (`node -v` = 20.16.0)
//
const recipe = ['eggs', 'flour', 'sugar', 'butter']
const pantry = ['sugar', 'butter', 'milk']
@nichoth
nichoth / web-components.md
Last active August 9, 2024 21:35
web components TIL

web components

CSS

Use the ::part selector

sl-dialog::part(close-button) {
  display: none;
}
@nichoth
nichoth / json-set-map.md
Created August 4, 2024 05:46
JSON + Set & Map

JSON + Set & Map

Serialize + deserialize sets and maps. See this blog post.

function replacer(key, value) {
  if (value instanceof Map) {
    return { __type: 'Map', value: Object.fromEntries(value) }
  }
 if (value instanceof Set) {
@nichoth
nichoth / FOUC.js
Last active August 1, 2024 17:12
FOUC — flash of unstyled content
// see https://stackoverflow.com/questions/3221561/eliminate-flash-of-unstyled-content
//
// The problem with using a css style to initially hide some page
// elements, and then using javascript to change the style back to
// visible after page load, is that people who don't have javascript
// enabled will never get to see those elements.
//
// so, use javascript to both initially hide as well as redisplay
// those elements after page load
//