MCPcopy Index your code
hub / github.com/nodejs/node / makeZlibTransformSync

Function makeZlibTransformSync

lib/internal/streams/iter/transform.js:535–665  ·  view source on GitHub ↗
(createHandleFn, processFlag, finishFlag)

Source from the content-addressed store, hash-verified

533// Returns a stateful sync transform (generator function).
534// ---------------------------------------------------------------------------
535function makeZlibTransformSync(createHandleFn, processFlag, finishFlag) {
536 return {
537 __proto__: null,
538 transform: function*(source) {
539 // The processCallback is never called in sync mode, but handle.init()
540 // requires it. Pass a no-op.
541 let error = null;
542 function onError(message, errno, code) {
543 error = genericNodeError(message, { __proto__: null, errno, code });
544 error.errno = errno;
545 error.code = code;
546 }
547
548 const result = createHandleFn(() => {}, onError);
549 const handle = result.handle;
550 const writeState = result.writeState;
551 const chunkSize = result.chunkSize;
552 let outBuf = Buffer.allocUnsafe(chunkSize);
553 let outOffset = 0;
554 let pending = [];
555 let pendingBytes = 0;
556
557 function processSyncInput(input, flushFlag) {
558 let inOff = 0;
559 let availIn = TypedArrayPrototypeGetByteLength(input);
560 let availOutBefore = chunkSize - outOffset;
561
562 handle.writeSync(flushFlag,
563 input, inOff, availIn,
564 outBuf, outOffset, availOutBefore);
565 if (error) throw error;
566
567 while (true) {
568 const availOut = writeState[0];
569 const availInAfter = writeState[1];
570 const have = availOutBefore - availOut;
571 const bufferExhausted = availOut === 0 ||
572 outOffset + have >= chunkSize;
573
574 if (have > 0) {
575 if (bufferExhausted && outOffset === 0) {
576 // Entire buffer filled - yield directly, no copy.
577 ArrayPrototypePush(pending, outBuf);
578 } else if (bufferExhausted) {
579 // Tail filled, buffer being replaced - subarray is safe.
580 ArrayPrototypePush(pending,
581 outBuf.subarray(outOffset, outOffset + have));
582 } else {
583 // Partial fill, buffer reused - must copy.
584 ArrayPrototypePush(pending,
585 TypedArrayPrototypeSlice(outBuf,
586 outOffset,
587 outOffset + have));
588 }
589 pendingBytes += have;
590 outOffset += have;
591 }
592

Callers 8

compressGzipSyncFunction · 0.85
compressDeflateSyncFunction · 0.85
compressBrotliSyncFunction · 0.85
compressZstdSyncFunction · 0.85
decompressGzipSyncFunction · 0.85
decompressDeflateSyncFunction · 0.85
decompressBrotliSyncFunction · 0.85
decompressZstdSyncFunction · 0.85

Calls 4

processSyncInputFunction · 0.85
drainBatchFunction · 0.85
allocMethod · 0.80
closeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…