Streaming import: bounded atomic acceptance
Project milestone
Finish the import as a bounded all-or-nothing result. Reject invalid UTF-8, excess bytes, duplicate SKUs or excess rows, and return the validated collection only after the entire pipeline succeeds.
Run the examples
In projects/stream-import/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {importRows,chunks}=require('./import.cjs');(async()=>{try{await importRows(chunks(['A\t1\nA\t2']));}catch(e){console.log(e.name);}})();Expected console output:
RangeErrorThe second occurrence violates unique SKU policy.
In projects/stream-import/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {importRows,chunks}=require('./import.cjs');(async()=>{try{await importRows(chunks(['A\t1']),{maxBytes:2});}catch(e){console.log(e.message);}})();Expected console output:
Too many bytesThe byte limit is enforced before decoding or row parsing.
Implementation and contract
importRows() counts incoming bytes before decoding, then accumulates validated rows in a private array. The supplied limits are checked themselves. pipeTo() rejects on a transform or sink error, so callers do not receive partially accepted results. Memory is bounded by the configured input and row limits plus implementation overhead, not guaranteed constant. This does not provide a database transaction.
Common mistakes
Never publish partially accumulated rows before completion if the contract promises all-or-nothing acceptance.
Acceptance task
Modify the test to send a valid row followed by a duplicate, then verify rejection without changing a caller-owned collection.
Project source, setup and tests: projects/stream-import/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Streaming import: bounded atomic acceptance — Knowledge Check
Assess implementation decisions and project behavior.
When may the caller commit imported data?
Does the import provide a database transaction?
Which boundary limits raw input before decoding?
In the Streaming import: bounded atomic acceptance lab, what does this independent example log? const {importRows,chunks}=require('./import.cjs'); (async()=>{try{await importRows(chunks(['A\t1\nA\t2']));}catch(e){console.log(e.name);}})();
In the Streaming import: bounded atomic acceptance lab, what does this independent example log? const {importRows,chunks}=require('./import.cjs'); (async()=>{try{await importRows(chunks(['A\t1']),{maxBytes:2});}catch(e){console.log(e.message);}})();