TransformStream Rewrites Chunks Via pipeThrough

pipeThrough slots a TransformStream between readable and writable so each chunk passes through transform while backpressure flows down the whole chain.

JavascriptData Fetching

Code

javascriptsnippet
24 lines
// 转换流:每个 chunk 都过手 transform,可改写可拦截
const upper = new TransformStream({
  transform(chunk, controller) {
    controller.enqueue(String(chunk).toUpperCase());
  },
});

const sink = new WritableStream({
  write(chunk) {
    console.log("out:", chunk);
  },
});

// 管道链接:readable → transform → writable,背压自动传递
const source = new ReadableStream({
  start(controller) {
    for (const word of ["deploy", "rollback", "health"]) {
      controller.enqueue(word);
    }
    controller.close();
  },
});

source.pipeThrough(upper).pipeTo(sink).then(() => console.log("done"));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

out: DEPLOY
out: ROLLBACK
out: HEALTH
done

Inside the snippet

A single loop drives the file — for (const word of ["deploy", "rollback", "health"]).

The file’s state is a handful of top-level bindings: upper, sink, source.

Points of care

  • The package manager never enters the picture for this snippet.

Putting it to use

Expect labeled output — out:, done — when the file runs.

Core lines

controller.enqueue(String(chunk).toUpperCase());
controller.enqueue(word);
controller.close();

FAQ

What does the package manager see?
An empty dependency file. The snippet imports nothing that needs fetching. Its state lives in module-level bindings: `upper`, `sink`, `source`.