GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Directory
  • API
  • SDKs
  • Changelog
  • Log inDashboard
  • Workers
  • Runtime APIs
  • Node.js compatibility
  • View as Markdown
  • Agent setup
  • Previousprocess
  • NextStringDecoder
  • Edit page

This view

  • Original page
  • New address

GeezerVison is showing developers.cloudflare.com — Streams

Streams

Note

For compatibility dates of 2026-08-04 or later, Workers enables both nodejs_compat and nodejs_compat_v2 by default. These flags are not used for these compatibility dates. Existing projects do not need to remove them when updating their compatibility date. For earlier dates, add nodejs_compat to your Wrangler configuration file to opt in. For instructions to turn off Node.js compatibility, refer to the Node.js compatibility flag.

The Node.js streams API ↗︎ is the original API for working with streaming data in JavaScript, predating the WHATWG ReadableStream standard ↗︎. A stream is an abstract interface for working with streaming data in Node.js. Streams can be readable, writable, or both. All streams are instances of EventEmitter.

Where possible, you should use the WHATWG standard "Web Streams" API ↗︎, which is supported in Workers ↗︎.

import { Readable, Transform } from "node:stream";

import { text } from "node:stream/consumers";

import { pipeline } from "node:stream/promises";

// A Node.js-style Transform that converts data to uppercase
// and appends a newline to the end of the output.
class MyTransform extends Transform {
	constructor() {
		super({ encoding: "utf8" });
	}
	_transform(chunk, _, cb) {
		this.push(chunk.toString().toUpperCase());
		cb();
	}
	_flush(cb) {
		this.push("\n");
		cb();
	}
}

export default {
	async fetch() {
		const chunks = [
			"hello ",
			"from ",
			"the ",
			"wonderful ",
			"world ",
			"of ",
			"node.js ",
			"streams!",
		];

		function nextChunk(readable) {
			readable.push(chunks.shift());
			if (chunks.length === 0) readable.push(null);
			else queueMicrotask(() => nextChunk(readable));
		}

		// A Node.js-style Readable that emits chunks from the
		// array...
		const readable = new Readable({
			encoding: "utf8",
			read() {
				nextChunk(readable);
			},
		});

		const transform = new MyTransform();
		await pipeline(readable, transform);
		return new Response(await text(transform));
	},
};

Refer to the Node.js documentation for stream ↗︎ for more information.

Was this helpful?

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.