Skip to content

Instantly share code, notes, and snippets.

@geersch
Last active April 17, 2024 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geersch/d7d2c04a3d5730b86065fe8ba92150d3 to your computer and use it in GitHub Desktop.
Save geersch/d7d2c04a3d5730b86065fe8ba92150d3 to your computer and use it in GitHub Desktop.
Streams: backpressure
import { blue, bold, cyan, green, yellow } from 'kleur';
import { clear, log } from 'node:console';
import { createReadStream } from 'node:fs';
import { join } from 'node:path';
import { PassThrough, Readable } from 'node:stream';
clear();
// const path = join(__dirname, '../files/13KB.pdf');
// const path = join(__dirname, '../files/1MB.pdf');
const path = join(__dirname, '../files/1.5MB.pdf');
const stream = createReadStream(path);
const passThrough = new PassThrough({ highWaterMark: 1024 });
listenToEvents('stream', stream);
listenToEvents('passThrough', passThrough);
stream.pipe(passThrough);
// - Comment this to create backpressure and pause the stream as soon as the PassThrough's internal "bucket" fills up.
// - Uncomment this to drain the "bucket" and keep the stream flowing.
// passThrough.on('data', (chunk: Uint8Array) => {
// log(
// `${blue('i')} ${bold('passThrough')}: data received (length: ${
// chunk.length
// })`
// );
// });
function listenToEvents(name: string, stream: Readable) {
stream
.on('pause', () => log(`${yellow('‼')} ${bold(cyan(name))}: paused`))
.on('resume', () => log(`${blue('i')} ${bold(cyan(name))}: resumed`))
.on('drain', () => log(`${blue('i')} ${bold(yellow(name))}: drained`))
.on('end', () => log(`${green('✔')} ${bold(green(name))}: ended`));
}
{
"name": "streams",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.10.6",
"nodemon": "^3.0.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {
"kleur": "^4.1.5"
}
}
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment