Skip to content

Instantly share code, notes, and snippets.

@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
@ForbesLindesay
ForbesLindesay / iter.js
Last active October 20, 2017 18:11 — forked from nanot1m/iter.js
Push iterator
const Queue = require('then-queue'); // async queue that doesn't care what order you call push and pop.
const queue = new Queue();
// push sends to the first waiting `pop` if available, otherwise it acts as a buffer
listenToNewMessages(message => queue.push(message));
async function* MessagesGenerator() {
try {
while (true) {