* 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)
| 930 | * @returns {number} Total bytes written |
| 931 | */ |
| 932 | function 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. |
no test coverage detected
searching dependent graphs…