output-sync.js
This file
Path
lib/io/output-sync.js
160 lines · 5.6 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption54d7404
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–6why, one step back1import {writeFileSync, appendFileSync} from 'node:fs';2import {shouldLogOutput, logLinesSync} from '../verbose/output.js';3import {runGeneratorsSync} from '../transform/generator.js';4import {splitLinesSync} from '../transform/split.js';5import {joinToString, joinToUint8Array, bufferToUint8Array} from '../utils/uint-array.js';6import {FILE_TYPES} from '../stdio/type.js';Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
line 7why, one step back7import {getFileTarget} from '../stdio/duplicate.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–10why, one step back8import {truncateMaxBufferSync} from './max-buffer.js';910// Apply `stdout`/`stderr` options, after spawning, in sync modeFix `execaSync()` returning `undefined` output when the subprocess fails to spawnSindre Sorhusbefore adoptionbfe4633
lines 11–12why, one step back11export const transformOutputSync = ({fileDescriptors, syncResult, options, verboseInfo}) => {12 const output = syncResult.output ?? getSpawnFailureOutput(options);Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 13why, one step back13 const outputFiles = new Set();Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 14–15why, one step back14 // Transforms, `verbose` and file writes apply to a single file descriptor each, so their errors must not affect the other ones15 const errors = [];100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 16–20why, one step back16 const transformedOutput = output.map((result, fdNumber) =>17 transformOutputResultSync({18 result,19 fileDescriptors,20 fdNumber,Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 21why, one step back21 errors,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 22–24why, one step back22 outputFiles,23 verboseInfo,24 }, options));Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 25why, one step back25 return {output: transformedOutput, error: errors.find(error => error !== undefined)};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 26–27why, one step back26};27Fix `execaSync()` returning `undefined` output when the subprocess fails to spawnSindre Sorhusbefore adoptionbfe4633
lines 28–31why, one step back28// When the subprocess fails to spawn, `spawnSync()` returns no output. Like with asynchronous methods, each piped output is empty instead.29// This is the output `spawnSync()` returns when the subprocess succeeds without printing anything: `stdin` and non-piped file descriptors are `null`. Input pipes on other file descriptors cannot be used with synchronous methods.30const getSpawnFailureOutput = ({stdio}) => stdio.map((stdioOption, fdNumber) => fdNumber !== 0 && stdioOption === 'pipe' ? new Uint8Array() : null);31100% test coverage (#1018)ehmickybefore adoption9f65b86
line 32why, one step back32const transformOutputResultSync = (Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 33why, one step back33 {result, fileDescriptors, fdNumber, errors, outputFiles, verboseInfo},100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 34–39why, one step back34 {buffer, encoding, lines, stripFinalNewline, maxBuffer},35) => {36 if (result === null) {37 return;38 }39Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
line 40why, one step back40 const truncatedResult = truncateMaxBufferSync(result, fdNumber, {maxBuffer, buffer});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 41–42why, one step back41 const uint8ArrayResult = bufferToUint8Array(truncatedResult);42 const {stdioItems, objectMode} = fileDescriptors[fdNumber];Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 43–44why, one step back43 // Empty output must not be passed as a chunk: with a binary encoding, no line is split, so transforms would wrongly run on it44 const {chunks, error: transformError} = runOutputGeneratorsSync(uint8ArrayResult.length === 0 ? [] : [uint8ArrayResult], stdioItems, encoding);TweaksSindre Sorhusbefore adoption54d7404
line 45why, one step back45 const {serializedResult, finalResult} = serializeChunks({100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–53why, one step back46 chunks,47 objectMode,48 encoding,49 lines,50 stripFinalNewline,51 fdNumber,52 });53Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 54why, one step back54 const logError = logOutputSync({Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 55why, one step back55 serializedResult,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 56why, one step back56 fdNumber,Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 57–61why, one step back57 verboseInfo,58 encoding,59 stdioItems,60 objectMode,61 });100% test coverage (#1018)ehmickybefore adoption9f65b86
line 62why, one step back62Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 63–65why, one step back63 const error = transformError ?? logError;64 errors.push(error);65100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 66–68why, one step back66 const returnedResult = buffer[fdNumber] ? finalResult : undefined;6768 try {Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 69why, one step back69 if (error === undefined) {Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
line 70why, one step back70 writeToFiles(chunks, stdioItems, outputFiles);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 71–73why, one step back71 }7273 return returnedResult;Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
lines 74–75why, one step back74 } catch (writeError) {75 errors.push(writeError);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 76–80why, one step back76 return returnedResult;77 }78};7980// Applies transform generators to `stdout`/`stderr`Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 81why, one step back81const runOutputGeneratorsSync = (chunks, stdioItems, encoding) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 82why, one step back82 try {Fix `execaSync()` returning `undefined` output when the subprocess fails to spawnSindre Sorhusbefore adoptionbfe4633
lines 83–84why, one step back83 // `error` is set, so that a polluted `Object.prototype.error` is not read when destructuring84 return {chunks: runGeneratorsSync(chunks, stdioItems, encoding, false), error: undefined};100% test coverage (#1018)ehmickybefore adoption9f65b86
line 85why, one step back85 } catch (error) {Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 86why, one step back86 return {chunks, error};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 87–90why, one step back87 }88};8990// The contents is converted to three stages:Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
line 91why, one step back91// - serializedResult: used by `verbose`100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 92–94why, one step back92// - finalResult/returnedResult: returned as `result.std*`93const serializeChunks = ({chunks, objectMode, encoding, lines, stripFinalNewline, fdNumber}) => {94 if (objectMode) {TweaksSindre Sorhusbefore adoption54d7404
line 95why, one step back95 return {serializedResult: chunks, finalResult: chunks};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 96–98why, one step back96 }9798 if (encoding === 'buffer') {TweaksSindre Sorhusbefore adoption54d7404
lines 99–100why, one step back99 const serializedResult = joinToUint8Array(chunks);100 return {serializedResult, finalResult: serializedResult};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 101–103why, one step back101 }102103 const serializedResult = joinToString(chunks, encoding);TweaksSindre Sorhusbefore adoption54d7404
lines 104–105why, one step back104 const finalResult = lines[fdNumber] ? splitLinesSync(serializedResult, !stripFinalNewline[fdNumber], objectMode) : serializedResult;105 return {serializedResult, finalResult};Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 106–107why, one step back106};107Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 108why, one step back108const logOutputSync = ({serializedResult, fdNumber, verboseInfo, encoding, stdioItems, objectMode}) => {Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 109–118why, one step back109 if (!shouldLogOutput({110 stdioItems,111 encoding,112 verboseInfo,113 fdNumber,114 })) {115 return;116 }117118 const linesArray = splitLinesSync(serializedResult, false, objectMode);Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 119–122why, one step back119120 try {121 logLinesSync(linesArray, fdNumber, verboseInfo);122 } catch (error) {Fix `execaSync()` output transformsSindre Sorhusbefore adoptione0b5dbc
line 123why, one step back123 return error;Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 124why, one step back124 }100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 125–127why, one step back125};126127// When the `std*` target is a file path/URL or a file descriptorFix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
lines 128–129why, one step back128// Object mode cannot be used with those targets.129const writeToFiles = (chunks, stdioItems, outputFiles) => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 130why, one step back130 const fileItems = stdioItems.filter(({type}) => FILE_TYPES.has(type));Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
lines 131–136why, one step back131 if (fileItems.length === 0) {132 return;133 }134135 // Like with asynchronous methods, the bytes are written as is, not the text decoded with the `encoding` option, which would change them136 const contents = joinToUint8Array(chunks);Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 137–152why, one step back137 const writtenPaths = new Set();138139 for (const stdioItem of fileItems) {140 const {type, append} = stdioItem;141 // A `filePath` and a `fileUrl` can target the same file, so they must share the same key142 const path = type === 'fileNumber' ? stdioItem.path : getFileTarget(stdioItem);143144 // A single file descriptor can target the same file twice, e.g. `stdout: [{file}, {file}]`.145 // Its output is a single stream, so it must only be written once.146 if (writtenPaths.has(path)) {147 continue;148 }149150 writtenPaths.add(path);151 // Like with asynchronous methods, when several file descriptors target the same file, the first one decides whether it is appended to152 if (append || outputFiles.has(path)) {Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
line 153why, one step back153 appendFileSync(path, contents);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 154why, one step back154 } else {Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a
line 155why, one step back155 writeFileSync(path, contents);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 156why, one step back156 }Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 157–158why, one step back157158 outputFiles.add(path);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 159–160why, one step back159 }160};