mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-27 06:43:19 +08:00
Cipheriv and Decipheriv values are Transforms, as in Node: they can be written and ended, piped into, used as pipeline() and stream/promises pipeline stages, composed, read, iterated with for await, read by stream/consumers, passed to finished(), and listened to, and their stream properties read as in Node. Each chunk goes through the cipher's update(), and the end pushes final() or reports its error through 'error'; writing after final() throws out of write(). The stream unit builds a cipher's Transform on its first stream use from operations the cipher's handle provides (ScrDynHandleOps.transform), so programs that use ciphers without streams link as before. The cipher is a collector-headered object behind a traced handle: its Transform's steps hold it, and the pair is reclaimed as a cycle. Through untyped values, in a program that uses streams, a cipher's stream properties and its on, once, off, pipe, unpipe, read, pause, resume, setEncoding, write, end, destroy, cork and uncork members run on its Transform; other stream members throw a coded SC2020 error. A Transform holds a write's callback while its readable side is at the high-water mark and runs it at the next read, and runs it on a tick once the readable side has ended, as Transform._write does. write() on a PassThrough, a Transform, a Hash or a cipher used to return true however much the readable side buffered. zlib streams applied the rule already and keep their timing. pipeline() accepts function stages anywhere among its streams, so runs of async generator stages can sit on both sides of a stream. A stage can be named by a function declaration, or a const bound to a function expression, that nothing reassigns; its body lowers again at the pipeline with its source typed by the pipeline. The source can also be an array, a string, a synchronous generator object, or a synchronous generator function without parameters: an async generator steps the source's iterator and yields each element, and the next stage receives each element on the same tick as with Node's for await over the source. The runtime's function-stage entry points take a bitmask of the function positions and one (next, read, return) helper triple for each generator pumped into a stream, so RUNTIME_ABI_VERSION is now 25. In JavaScript, the chunks a stage receives from the previous stage's generator read as the strings or Buffers it yields, so their string and Buffer methods compile. Corpus 7055-7059 cover cipher streams in TypeScript and JavaScript, their errors, close order and backpressure, the pipeline stages and sources with their tick order, and Transform backpressure.