Node.js / Editorial solution guide
A stream transfer stops or appears to succeed early
Trace source, destination, completion, and failure ownership when a Node stream transfer is incomplete.
Read-only knowledge base · No public comments
Recognize the symptoms
A file or response contains only part of its expected content, a job finishes before writing ends, or the process reports success despite a stream error. Preserve a synthetic reproducer and the exact error and stack. Partial output alone does not identify which stage failed.
Understand the likely causes
- The caller may not await pipeline completion, a stream may emit an unhandled error, or a destination may close or fail before all chunks are written. A stalled custom writable can also omit its write callback.
Locate the first failed stage
Identify the source, each transform, destination, and owner of completion. Record safe lifecycle events and the first error without logging private content. Reproduce with a small in-memory source and an isolated sink. If an external destination is involved, check its documented status and capacity rather than assuming the source caused every short output. Determine whether any partial artifact was already made visible.
Connect completion and errors
Use pipeline to connect compatible stages and await its promise at the owning request or job boundary. The example injects a sink failure and observes the rejection. Do not attach a success message immediately after starting asynchronous work. In a custom writable, call the write callback exactly once with either success or an error; otherwise the pipeline may stall or report incorrect completion.
const { Readable, Writable } = require("node:stream");
const { pipeline } = require("node:stream/promises");
const source = Readable.from(["sample"]);
const sink = new Writable({
write(chunk, encoding, callback) { callback(new Error("simulated sink failure")); }
});
pipeline(source, sink)
.then(() => console.log("complete"))
.catch(error => console.log("failed:", error.message));
The sink reports failure through its callback, and the pipeline promise rejects to the owner.
Define recovery for partial effects
pipeline coordinates connected stream failure, but it cannot reverse a file already exposed or a remote side effect. For file output, consider writing to an isolated temporary path and publishing only after successful completion, according to the filesystem and application contract. Remove or quarantine partial artifacts when appropriate, and ensure cleanup does not erase evidence needed for diagnosis. Avoid retrying a non-idempotent destination without a documented recovery policy.
Verify controlled success and failure
Run a successful fixture and assert all expected bytes arrive before success is reported. Inject source and destination failures after some chunks, and assert that the owning operation observes failure and closes its resources. Add a slow sink to exercise flow control, plus a custom writable whose callback is delayed to expose premature completion. Check the final artifact or remote effect, not only the returned promise.
Verify the fix
Confirm the success case completes fully, each injected failure reaches the owner, and the documented partial-output policy is applied. Verify cleanup with an isolated fixture.
Keep it from recurring
- Await owned pipelines, define destination and retry semantics, and retain regression tests for slow consumers and partial failures. Avoid logging stream contents when metadata suffices.