Skip to content

Instantly share code, notes, and snippets.

@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
//
@nichoth
nichoth / CSS.md
Last active July 19, 2024 06:00
CSS — tips and things
@nichoth
nichoth / bash.md
Last active July 19, 2025 20:29
Bash commands cli

bash

rename

Change the file extension for all .js files in dir

for file in ./dir/*.js; do mv ${file} ${file/.js/.ts}; done
@nichoth
nichoth / node.js
Last active September 19, 2024 05:41
Node tips
#!/usr/bin/node // shebang
//
// mkdir -p
//
const fs = require('node:fs/promises')
// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
await fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
@nichoth
nichoth / building-sync-systems.md
Created June 24, 2024 17:28 — forked from pesterhazy/building-sync-systems.md
Building an offline realtime sync engine

So you want to write a sync system for a web app with offline and realtime support? Good luck. You might find the following resources useful.

Overview articles