Skip to content
Myr

output-sync.js

This file

Path

lib/io/output-sync.js

116 lines · 3.5 KiB · JavaScript

Last changed by

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

History of this file

Take it

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 1why, one step back
1import {writeFileSync, appendFileSync} from 'node:fs';

Rename files (#977)ehmickybefore adoption4b2e316

lines 2–6why, one step back
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';

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 7why, one step back
7import {truncateMaxBufferSync} from './max-buffer.js';

Rename files (#977)ehmickybefore adoption4b2e316

lines 8–15why, one step back
8
9// Apply `stdout`/`stderr` options, after spawning, in sync mode10export const transformOutputSync = ({fileDescriptors, syncResult: {output}, options, isMaxBuffer, verboseInfo}) => {11 if (output === null) {12 return {output: Array.from({length: 3})};13 }1415 const state = {};

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 16why, one step back
16 const outputFiles = new Set([]);

Rename files (#977)ehmickybefore adoption4b2e316

line 17why, one step back
17 const transformedOutput = output.map((result, fdNumber) =>

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 18–22why, one step back
18 transformOutputResultSync({
19 result,20 fileDescriptors,21 fdNumber,22 state,

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 23why, one step back
23 outputFiles,

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 24–26why, one step back
24 isMaxBuffer,
25 verboseInfo,26 }, options));

Rename files (#977)ehmickybefore adoption4b2e316

lines 27–29why, one step back
27 return {output: transformedOutput, ...state};
28};29

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 30–33why, one step back
30const transformOutputResultSync = (
31 {result, fileDescriptors, fdNumber, state, outputFiles, isMaxBuffer, verboseInfo},32 {buffer, encoding, lines, stripFinalNewline, maxBuffer},33) => {

Rename files (#977)ehmickybefore adoption4b2e316

lines 34–37why, one step back
34 if (result === null) {
35 return;36 }37

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 38why, one step back
38 const truncatedResult = truncateMaxBufferSync(result, isMaxBuffer, maxBuffer);

Rename files (#977)ehmickybefore adoption4b2e316

lines 39–41why, one step back
39 const uint8ArrayResult = bufferToUint8Array(truncatedResult);
40 const {stdioItems, objectMode} = fileDescriptors[fdNumber];41 const chunks = runOutputGeneratorsSync([uint8ArrayResult], stdioItems, encoding, state);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 42–49why, one step back
42 const {serializedResult, finalResult = serializedResult} = serializeChunks({
43 chunks,44 objectMode,45 encoding,46 lines,47 stripFinalNewline,48 fdNumber,49 });

Rename files (#977)ehmickybefore adoption4b2e316

line 50why, one step back
50

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 51–56why, one step back
51 if (shouldLogOutput({
52 stdioItems,53 encoding,54 verboseInfo,55 fdNumber,56 })) {

Rename files (#977)ehmickybefore adoption4b2e316

lines 57–64why, one step back
57 const linesArray = splitLinesSync(serializedResult, false, objectMode);
58 logLinesSync(linesArray, verboseInfo);59 }6061 const returnedResult = buffer[fdNumber] ? finalResult : undefined;6263 try {64 if (state.error === undefined) {

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 65why, one step back
65 writeToFiles(serializedResult, stdioItems, outputFiles);

Rename files (#977)ehmickybefore adoption4b2e316

lines 66–74why, one step back
66 }
6768 return returnedResult;69 } catch (error) {70 state.error = error;71 return returnedResult;72 }73};74

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 75why, one step back
75// Applies transform generators to `stdout`/`stderr`

Rename files (#977)ehmickybefore adoption4b2e316

lines 76–84why, one step back
76const runOutputGeneratorsSync = (chunks, stdioItems, encoding, state) => {
77 try {78 return runGeneratorsSync(chunks, stdioItems, encoding, false);79 } catch (error) {80 state.error = error;81 return chunks;82 }83};84

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 85–87why, one step back
85// The contents is converted to three stages:
86// - serializedResult: used when the target is a file path/URL or a file descriptor (including 'inherit')87// - finalResult/returnedResult: returned as `result.std*`

Rename files (#977)ehmickybefore adoption4b2e316

lines 88–104why, one step back
88const serializeChunks = ({chunks, objectMode, encoding, lines, stripFinalNewline, fdNumber}) => {
89 if (objectMode) {90 return {serializedResult: chunks};91 }9293 if (encoding === 'buffer') {94 return {serializedResult: joinToUint8Array(chunks)};95 }9697 const serializedResult = joinToString(chunks, encoding);98 if (lines[fdNumber]) {99 return {serializedResult, finalResult: splitLinesSync(serializedResult, !stripFinalNewline[fdNumber], objectMode)};100 }101102 return {serializedResult};103};104

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 105why, one step back
105// When the `std*` target is a file path/URL or a file descriptor

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 106–112why, one step back
106const writeToFiles = (serializedResult, stdioItems, outputFiles) => {
107 for (const {path} of stdioItems.filter(({type}) => FILE_TYPES.has(type))) {108 const pathString = typeof path === 'string' ? path : path.toString();109 if (outputFiles.has(pathString)) {110 appendFileSync(path, serializedResult);111 } else {112 outputFiles.add(pathString);

Rename files (#977)ehmickybefore adoption4b2e316

lines 113–116why, one step back
113 writeFileSync(path, serializedResult);
114 }115 }116};