Skip to content

Instantly share code, notes, and snippets.

View ken107's full-sized avatar
💭
meditating hard

Sarsa Parilla ken107

💭
meditating hard
View GitHub Profile
@ken107
ken107 / get-stream.ts
Created August 26, 2024 03:48
The NPM library is garbage
function getStream(stream: Readable): Promise<Buffer> {
return new Promise((fulfill, reject) => {
const chunks: Buffer[] = []
let totalLength = 0
stream.on("data", chunk => {
chunks.push(chunk)
totalLength += chunk.length
})
stream.once("end", () => fulfill(Buffer.concat(chunks, totalLength)))
stream.once("error", reject)
@ken107
ken107 / createWavHeader.ts
Created February 25, 2024 19:59
Create WAV header for PCM data
export function createWavHeader(dataLength: number, sampleRate: number, bitDepth: number, channels: number) {
const headerLength = 44; // Standard size of WAV header
const totalLength = headerLength + dataLength;
const buffer = new ArrayBuffer(headerLength);
const view = new DataView(buffer);
// RIFF chunk descriptor
writeString(view, 0, 'RIFF'); // ChunkID
view.setUint32(4, totalLength - 8, true); // ChunkSize (file size - 8 bytes)
function makeAudioPlayer(audio) {
audio.oncanplay = () => sm.trigger("onCanPlay")
audio.onerror = () => sm.trigger("onError", new Error(audio.error.message || audio.error.code))
audio.onended = () => sm.trigger("onEnd")
const sm = new StateMachine({
IDLE: {
load(url) {
audio.src = url
return "LOADING_PAUSED"
@ken107
ken107 / valid-getter.ts
Last active February 9, 2023 15:42
A getter that calls generator() to create a new instance and subsequently returns the same instance while it isValid()
export function makeValidGetter<T extends {isValid(): boolean}>(generator: () => Promise<T>): () => Promise<T> {
let value: T|undefined
let promise: Promise<T>|undefined
return async function() {
if (value?.isValid()) return value
if (promise) return await promise
promise = generator()
.then(result => {
value = result
promise = undefined
function simulate(element, eventName, options) {
var opt = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
@ken107
ken107 / repeat.js
Last active September 8, 2021 17:46
Repeat an action `max` number of times or `until` some condition, with optional `delay` between repetitions
/**
* Repeat an action
* @param {Object} opt - options
* @param {Function} opt.action - action to repeat
* @param {Function} opt.until - termination condition
* @param {Number} opt.delay - delay between actions
* @param {Number} opt.max - maximum number of repetitions
* @returns {Promise}
*/
function repeat(opt) {
@ken107
ken107 / detect-user-idle.js
Last active December 16, 2024 20:28
Return an boolean observable reflecting the user idle state
function detectUserIdle(periodOfInactivity) {
return rxjs.merge(
rxjs.timer(periodOfInactivity),
rxjs.merge(
rxjs.fromEvent(document, "mousemove"),
rxjs.fromEvent(document, "keydown"),
rxjs.fromEvent(document, "touchstart")
)
).pipe(
rxjs.switchMap(event => !event ? rxjs.of(true) : rxjs.concat(rxjs.of(false), rxjs.throwError(() => "reset"))),