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

Function pipeToSync

lib/internal/streams/iter/pull.js:932–983  ·  view source on GitHub ↗

* Write a sync source through transforms to a sync writer. * @param {Iterable } source * @param {...(Function|object)} args - Transforms, writer, and optional options * @returns {number} Total bytes written

(source, ...args)

Source from the content-addressed store, hash-verified

930 * @returns {number} Total bytes written
931 */
932function pipeToSync(source, ...args) {
933 const { transforms, writer, options } = parsePipeToArgs(args, 'writeSync');
934
935 // Normalize source and create pipeline
936 const normalized = fromSync(source);
937 const pipeline = transforms.length > 0 ?
938 createSyncPipeline(normalized, transforms) :
939 normalized;
940
941 let totalBytes = 0;
942 const hasWritevSync = typeof writer.writevSync === 'function';
943 const hasEndSync = typeof writer.endSync === 'function';
944
945 try {
946 let canContinue = true;
947 for (const batch of pipeline) {
948 if (!canContinue) {
949 break;
950 }
951 if (hasWritevSync && batch.length > 1) {
952 if (writer.writevSync(batch) === false) {
953 break;
954 }
955 for (let i = 0; i < batch.length; i++) {
956 totalBytes += TypedArrayPrototypeGetByteLength(batch[i]);
957 }
958 } else {
959 for (let i = 0; i < batch.length; i++) {
960 const chunk = batch[i];
961 if (writer.writeSync(chunk) === false) {
962 canContinue = false;
963 break;
964 }
965 totalBytes += TypedArrayPrototypeGetByteLength(chunk);
966 }
967 }
968 }
969
970 if (!options?.preventClose) {
971 if (!hasEndSync || writer.endSync() < 0) {
972 writer.end?.();
973 }
974 }
975 } catch (error) {
976 if (!options?.preventFail) {
977 writer.fail?.(wrapError(error));
978 }
979 throw error;
980 }
981
982 return totalBytes;
983}
984
985/**
986 * Write an async source through transforms to a writer.

Callers 15

benchFromSyncWritevFunction · 0.85
benchIterSyncFunction · 0.85
benchIterSyncFunction · 0.85
benchIterSyncFunction · 0.85
runSyncFunction · 0.85
testPipeToSyncNoEndSyncFunction · 0.85
testPipeToSyncSABFunction · 0.85
testPipeToSyncFunction · 0.85

Calls 9

parsePipeToArgsFunction · 0.85
createSyncPipelineFunction · 0.85
wrapErrorFunction · 0.85
fromSyncFunction · 0.70
writevSyncMethod · 0.45
writeSyncMethod · 0.45
endSyncMethod · 0.45
endMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…