Skip to content
Myr

Type the `std*` generator transform `chunk` argument (#1247)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent 487a3a4 · Browse files · 12 files+469 added−98 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

12 files+469 added−98 removed

  • other:+469 added−98 removed

12 files+469 added−98 removed

modified:docs/api.md+1 added-1 removed
1286 unmodified lines
12871288_Type:_ `boolean`\1289_Default:_ `false`12901291If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.1291If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produce a single line.12921293[More info.](lines.md#transforms)12941295### transformOptions.objectMode
12 unmodified lines
modified:test-d/methods/script.test-d.ts+7 added
added:test-d/stdio/option/generator-binary-uint-array.test-d.ts+202 added
1import {expectError, expectAssignable, expectNotAssignable} from 'tsd';2import {3 execa,4 execaSync,5 type StdinOption,6 type StdinSyncOption,7 type StdoutStderrOption,8 type StdoutStderrSyncOption,9} from '../../../index.js';1011// With `binary: true`, the `chunk` argument is an `Uint8Array`.12const uint8ArrayBinary = {13 * transform(chunk: Uint8Array) {14 yield chunk;15 },16 binary: true,17} as const;1819await execa('unicorns', {stdin: uint8ArrayBinary});20execaSync('unicorns', {stdin: uint8ArrayBinary});21await execa('unicorns', {stdin: [uint8ArrayBinary]});22execaSync('unicorns', {stdin: [uint8ArrayBinary]});23await execa('unicorns', {stdout: uint8ArrayBinary});24execaSync('unicorns', {stdout: uint8ArrayBinary});25await execa('unicorns', {stderr: uint8ArrayBinary});26execaSync('unicorns', {stderr: uint8ArrayBinary});27await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayBinary]});2829expectAssignable<StdinOption>(uint8ArrayBinary);30expectAssignable<StdinSyncOption>(uint8ArrayBinary);31expectAssignable<StdinOption>([uint8ArrayBinary]);32expectAssignable<StdinSyncOption>([uint8ArrayBinary]);33expectAssignable<StdoutStderrOption>(uint8ArrayBinary);34expectAssignable<StdoutStderrSyncOption>(uint8ArrayBinary);35expectAssignable<StdoutStderrOption>([uint8ArrayBinary]);36expectAssignable<StdoutStderrSyncOption>([uint8ArrayBinary]);3738// `objectMode: true` takes precedence over `binary: true` for stdin, so the `chunk` argument is `unknown`.39const uint8ArrayBinaryObjectMode = {40 * transform(chunk: Uint8Array) {41 yield chunk;42 },43 binary: true,44 objectMode: true,45} as const;4647const stdinObjectModeBinary = {48 * transform(chunk: unknown) {49 yield chunk;50 },51 binary: true,52 objectMode: true,53} as const;5455expectError(await execa('unicorns', {stdin: uint8ArrayBinaryObjectMode}));56expectError(execaSync('unicorns', {stdin: uint8ArrayBinaryObjectMode}));57await execa('unicorns', {stdout: uint8ArrayBinaryObjectMode});58execaSync('unicorns', {stdout: uint8ArrayBinaryObjectMode});59await execa('unicorns', {stderr: uint8ArrayBinaryObjectMode});60execaSync('unicorns', {stderr: uint8ArrayBinaryObjectMode});6162await execa('unicorns', {stdin: stdinObjectModeBinary});63execaSync('unicorns', {stdin: stdinObjectModeBinary});6465expectNotAssignable<StdinOption>(uint8ArrayBinaryObjectMode);66expectNotAssignable<StdinSyncOption>(uint8ArrayBinaryObjectMode);67expectAssignable<StdoutStderrOption>(uint8ArrayBinaryObjectMode);68expectAssignable<StdoutStderrSyncOption>(uint8ArrayBinaryObjectMode);69expectAssignable<StdinOption>(stdinObjectModeBinary);70expectAssignable<StdinSyncOption>(stdinObjectModeBinary);7172const stdoutAnnotatedBinaryObjectMode: StdoutStderrOption = {73 * transform(chunk: Uint8Array) {74 yield chunk;75 },76 binary: true,77 objectMode: true,78};7980expectAssignable<StdoutStderrOption>(stdoutAnnotatedBinaryObjectMode);8182// A `string` argument is invalid when `binary: true`.83const stringBinary = {84 * transform(line: string) {85 yield line;86 },87 binary: true,88} as const;8990const stringBinaryObjectMode = {91 * transform(line: string) {92 yield line;93 },94 binary: true,95 objectMode: true,96} as const;9798expectError(await execa('unicorns', {stdin: stringBinary}));99expectError(execaSync('unicorns', {stdin: stringBinary}));100expectError(await execa('unicorns', {stdin: [stringBinary]}));101expectError(execaSync('unicorns', {stdin: [stringBinary]}));102expectError(await execa('unicorns', {stdout: stringBinary}));103expectError(execaSync('unicorns', {stdout: stringBinary}));104expectError(await execa('unicorns', {stderr: stringBinary}));105expectError(execaSync('unicorns', {stderr: stringBinary}));106expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringBinary]}));107expectError(await execa('unicorns', {stdout: stringBinaryObjectMode}));108expectError(execaSync('unicorns', {stdout: stringBinaryObjectMode}));109expectError(await execa('unicorns', {stderr: stringBinaryObjectMode}));110expectError(execaSync('unicorns', {stderr: stringBinaryObjectMode}));111112expectNotAssignable<StdinOption>(stringBinary);113expectNotAssignable<StdinSyncOption>(stringBinary);114expectNotAssignable<StdinOption>([stringBinary]);115expectNotAssignable<StdinSyncOption>([stringBinary]);116expectNotAssignable<StdoutStderrOption>(stringBinary);117expectNotAssignable<StdoutStderrSyncOption>(stringBinary);118expectNotAssignable<StdoutStderrOption>([stringBinary]);119expectNotAssignable<StdoutStderrSyncOption>([stringBinary]);120expectNotAssignable<StdoutStderrOption>(stringBinaryObjectMode);121expectNotAssignable<StdoutStderrSyncOption>(stringBinaryObjectMode);122123// An `Uint8Array` argument is invalid without `binary: true`, since the `chunk` is then a `string`.124const uint8ArrayLine = {125 * transform(chunk: Uint8Array) {126 yield chunk;127 },128} as const;129130expectError(await execa('unicorns', {stdin: uint8ArrayLine}));131expectError(execaSync('unicorns', {stdin: uint8ArrayLine}));132expectError(await execa('unicorns', {stdin: [uint8ArrayLine]}));133expectError(execaSync('unicorns', {stdin: [uint8ArrayLine]}));134expectError(await execa('unicorns', {stdout: uint8ArrayLine}));135expectError(execaSync('unicorns', {stdout: uint8ArrayLine}));136expectError(await execa('unicorns', {stderr: uint8ArrayLine}));137expectError(execaSync('unicorns', {stderr: uint8ArrayLine}));138expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayLine]}));139140expectNotAssignable<StdinOption>(uint8ArrayLine);141expectNotAssignable<StdinSyncOption>(uint8ArrayLine);142expectNotAssignable<StdinOption>([uint8ArrayLine]);143expectNotAssignable<StdinSyncOption>([uint8ArrayLine]);144expectNotAssignable<StdoutStderrOption>(uint8ArrayLine);145expectNotAssignable<StdoutStderrSyncOption>(uint8ArrayLine);146expectNotAssignable<StdoutStderrOption>([uint8ArrayLine]);147expectNotAssignable<StdoutStderrSyncOption>([uint8ArrayLine]);148149// Binary encodings also make output transforms receive binary chunks.150await execa('unicorns', {encoding: 'buffer', stdin: uint8ArrayLine});151execaSync('unicorns', {encoding: 'buffer', stdin: uint8ArrayLine});152await execa('unicorns', {encoding: 'buffer', stdout: uint8ArrayLine});153execaSync('unicorns', {encoding: 'buffer', stdout: uint8ArrayLine});154await execa('unicorns', {encoding: 'buffer', stderr: uint8ArrayLine});155execaSync('unicorns', {encoding: 'buffer', stderr: uint8ArrayLine});156await execa('unicorns', {encoding: 'buffer', stdio: [uint8ArrayLine, uint8ArrayLine, uint8ArrayLine, uint8ArrayLine]});157execaSync('unicorns', {encoding: 'buffer', stdio: [uint8ArrayLine, uint8ArrayLine, uint8ArrayLine, uint8ArrayLine]});158159// A bare generator function always receives string lines unless `encoding` is binary.160const uint8ArrayGenerator = function * (chunk: Uint8Array) {161 yield chunk;162};163164expectError(await execa('unicorns', {stdin: uint8ArrayGenerator}));165expectError(execaSync('unicorns', {stdin: uint8ArrayGenerator}));166expectError(await execa('unicorns', {stdout: uint8ArrayGenerator}));167expectError(execaSync('unicorns', {stdout: uint8ArrayGenerator}));168expectError(await execa('unicorns', {stderr: uint8ArrayGenerator}));169expectError(execaSync('unicorns', {stderr: uint8ArrayGenerator}));170expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', uint8ArrayGenerator]}));171172expectNotAssignable<StdinOption>(uint8ArrayGenerator);173expectNotAssignable<StdinSyncOption>(uint8ArrayGenerator);174expectNotAssignable<StdoutStderrOption>(uint8ArrayGenerator);175expectNotAssignable<StdoutStderrSyncOption>(uint8ArrayGenerator);176177await execa('unicorns', {encoding: 'buffer', stdin: uint8ArrayGenerator});178execaSync('unicorns', {encoding: 'buffer', stdin: uint8ArrayGenerator});179await execa('unicorns', {encoding: 'buffer', stdout: uint8ArrayGenerator});180execaSync('unicorns', {encoding: 'buffer', stdout: uint8ArrayGenerator});181await execa('unicorns', {encoding: 'buffer', stderr: uint8ArrayGenerator});182execaSync('unicorns', {encoding: 'buffer', stderr: uint8ArrayGenerator});183await execa('unicorns', {encoding: 'buffer', stdio: [uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator]});184execaSync('unicorns', {encoding: 'buffer', stdio: [uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator, uint8ArrayGenerator]});185186// Conversely, a binary `encoding` makes the `chunk` a `Uint8Array`, so a `string` transform is rejected.187const stringGenerator = function * (line: string) {188 yield line;189};190191expectError(await execa('unicorns', {encoding: 'buffer', stdin: stringGenerator}));192expectError(execaSync('unicorns', {encoding: 'buffer', stdin: stringGenerator}));193expectError(await execa('unicorns', {encoding: 'buffer', stdout: stringGenerator}));194expectError(execaSync('unicorns', {encoding: 'buffer', stdout: stringGenerator}));195196// This applies to every binary `encoding`, not just `'buffer'`.197await execa('unicorns', {encoding: 'hex', stdout: uint8ArrayGenerator});198execaSync('unicorns', {encoding: 'hex', stdout: uint8ArrayGenerator});199await execa('unicorns', {encoding: 'base64', stdout: uint8ArrayGenerator});200execaSync('unicorns', {encoding: 'base64', stdout: uint8ArrayGenerator});201expectError(await execa('unicorns', {encoding: 'hex', stdout: stringGenerator}));202expectError(execaSync('unicorns', {encoding: 'hex', stdout: stringGenerator}));
modified:test-d/stdio/option/generator-object-full.test-d.ts+50 added-1 removed
modified:test-d/stdio/option/generator-string-full.test-d.ts+25 added-25 removed
modified:test-d/stdio/option/generator-string.test-d.ts+36 added-25 removed
1import {expectError, expectNotAssignable} from 'tsd';1import {expectError, expectAssignable} from 'tsd';2import {3 execa,4 execaSync,5 type StdinOption,
5 unmodified lines
11const stringGenerator = function * (line: string) {12 yield line;13};1415expectError(await execa('unicorns', {stdin: stringGenerator}));16expectError(execaSync('unicorns', {stdin: stringGenerator}));17expectError(await execa('unicorns', {stdin: [stringGenerator]}));18expectError(execaSync('unicorns', {stdin: [stringGenerator]}));15await execa('unicorns', {stdin: stringGenerator});16execaSync('unicorns', {stdin: stringGenerator});17await execa('unicorns', {stdin: [stringGenerator]});18execaSync('unicorns', {stdin: [stringGenerator]});1920expectError(await execa('unicorns', {stdout: stringGenerator}));21expectError(execaSync('unicorns', {stdout: stringGenerator}));22expectError(await execa('unicorns', {stdout: [stringGenerator]}));23expectError(execaSync('unicorns', {stdout: [stringGenerator]}));20await execa('unicorns', {stdout: stringGenerator});21execaSync('unicorns', {stdout: stringGenerator});22await execa('unicorns', {stdout: [stringGenerator]});23execaSync('unicorns', {stdout: [stringGenerator]});2425expectError(await execa('unicorns', {stderr: stringGenerator}));26expectError(execaSync('unicorns', {stderr: stringGenerator}));27expectError(await execa('unicorns', {stderr: [stringGenerator]}));28expectError(execaSync('unicorns', {stderr: [stringGenerator]}));25await execa('unicorns', {stderr: stringGenerator});26execaSync('unicorns', {stderr: stringGenerator});27await execa('unicorns', {stderr: [stringGenerator]});28execaSync('unicorns', {stderr: [stringGenerator]});2930expectError(await execa('unicorns', {stdio: stringGenerator}));31expectError(execaSync('unicorns', {stdio: stringGenerator}));3233expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]}));34expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]}));35expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]}));36expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]}));33await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]});34execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', stringGenerator]});35await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]});36execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [stringGenerator]]});3738expectNotAssignable<StdinOption>(stringGenerator);39expectNotAssignable<StdinSyncOption>(stringGenerator);40expectNotAssignable<StdinOption>([stringGenerator]);41expectNotAssignable<StdinSyncOption>([stringGenerator]);38expectAssignable<StdinOption>(stringGenerator);39expectAssignable<StdinSyncOption>(stringGenerator);40expectAssignable<StdinOption>([stringGenerator]);41expectAssignable<StdinSyncOption>([stringGenerator]);4243expectNotAssignable<StdoutStderrOption>(stringGenerator);44expectNotAssignable<StdoutStderrSyncOption>(stringGenerator);45expectNotAssignable<StdoutStderrOption>([stringGenerator]);46expectNotAssignable<StdoutStderrSyncOption>([stringGenerator]);43expectAssignable<StdoutStderrOption>(stringGenerator);44expectAssignable<StdoutStderrSyncOption>(stringGenerator);45expectAssignable<StdoutStderrOption>([stringGenerator]);46expectAssignable<StdoutStderrSyncOption>([stringGenerator]);4748// The `chunk` argument is typed, but the return type stays `unknown`: a transform can yield both `string` and `Uint8Array`.49const mixedYieldGenerator = function * (line: string) {50 yield line;51 yield new TextEncoder().encode(line);52};5354expectAssignable<StdinOption>(mixedYieldGenerator);55expectAssignable<StdinSyncOption>(mixedYieldGenerator);56expectAssignable<StdoutStderrOption>(mixedYieldGenerator);57expectAssignable<StdoutStderrSyncOption>(mixedYieldGenerator);
added:test-d/stdio/option/generator-widened.test-d.ts+45 added
1import {expectAssignable, expectNotAssignable} from 'tsd';2import {3 type StdinOption,4 type StdinSyncOption,5 type StdoutStderrOption,6 type StdoutStderrSyncOption,7} from '../../../index.js';89// Without `as const`, `binary`/`objectMode` widen to `boolean`. Since the transform mode is unknown at compile time, the `chunk` argument must be `unknown`.1011const widenedBinary = {12 * transform(line: unknown) {13 yield line;14 },15 binary: true,16};1718expectAssignable<StdinOption>(widenedBinary);19expectAssignable<StdinSyncOption>(widenedBinary);20expectAssignable<StdoutStderrOption>(widenedBinary);21expectAssignable<StdoutStderrSyncOption>(widenedBinary);2223const widenedObjectMode = {24 * transform(line: unknown) {25 yield line;26 },27 objectMode: true,28};2930expectAssignable<StdinOption>(widenedObjectMode);31expectAssignable<StdinSyncOption>(widenedObjectMode);32expectAssignable<StdoutStderrOption>(widenedObjectMode);33expectAssignable<StdoutStderrSyncOption>(widenedObjectMode);3435const widenedBinaryString = {36 * transform(line: string) {37 yield line;38 },39 binary: true,40};4142expectNotAssignable<StdinOption>(widenedBinaryString);43expectNotAssignable<StdinSyncOption>(widenedBinaryString);44expectNotAssignable<StdoutStderrOption>(widenedBinaryString);45expectNotAssignable<StdoutStderrSyncOption>(widenedBinaryString);
modified:types/arguments/encoding-option.d.ts+1 added-1 removed
1type DefaultEncodingOption = 'utf8';2type TextEncodingOption =2export type TextEncodingOption =3 | DefaultEncodingOption4 | 'utf16le';56export type BufferEncodingOption = 'buffer';
15 unmodified lines
modified:types/arguments/options.d.ts+19 added-9 removed
3 unmodified lines
4import type {Message} from '../ipc.js';5import type {StdinOptionCommon, StdoutStderrOptionCommon, StdioOptionsProperty} from '../stdio/type.js';6import type {VerboseOption} from '../verbose.js';7import type {FdGenericOption} from './specific.js';8import type {EncodingOption} from './encoding-option.js';8import type {BinaryEncodingOption, EncodingOption, TextEncodingOption} from './encoding-option.js';910export type CommonOptions<IsSync extends boolean = boolean> = {10type ChunkForEncoding<Encoding extends EncodingOption> = Encoding extends BinaryEncodingOption ? Uint8Array : string;1112export type CommonOptions<13 IsSync extends boolean = boolean,14 Encoding extends EncodingOption = EncodingOption,15> = {16 /**17 Prefer locally installed binaries when looking for a binary to execute.1819 @default `true` with `$`, `false` otherwise
91 unmodified lines
111 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.112113 @default `'inherit'` with `$`, `'pipe'` otherwise114 */110 readonly stdin?: StdinOptionCommon<IsSync>;115 readonly stdin?: StdinOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;116117 /**118 How to setup the subprocess' [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.119120 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.121122 @default 'pipe'123 */119 readonly stdout?: StdoutStderrOptionCommon<IsSync>;124 readonly stdout?: StdoutStderrOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;125126 /**127 How to setup the subprocess' [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). This can be `'pipe'`, `'overlapped'`, `'ignore`, `'inherit'`, a file descriptor integer, a Node.js `Writable` stream, a web `WritableStream`, a `{ file: 'path' }` object, a file URL, a generator function, a `Duplex` or a web `TransformStream`.128129 This can be an array of values such as `['inherit', 'pipe']` or `[fileUrl, 'pipe']`.130131 @default 'pipe'132 */128 readonly stderr?: StdoutStderrOptionCommon<IsSync>;133 readonly stderr?: StdoutStderrOptionCommon<IsSync, boolean, ChunkForEncoding<Encoding>>;134135 /**136 Like the `stdin`, `stdout` and `stderr` options but for all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.137
2 unmodified lines
140 The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`.141142 @default 'pipe'143 */139 readonly stdio?: StdioOptionsProperty<IsSync>;144 readonly stdio?: StdioOptionsProperty<IsSync, ChunkForEncoding<Encoding>>;145146 /**147 Add a `subprocess.all` stream and a `result.all` property. They contain the combined/interleaved output of the subprocess' `stdout` and `stderr`.148
11 unmodified lines
160 The output is available with `result.stdout`, `result.stderr` and `result.stdio`.161162 @default 'utf8'163 */159 readonly encoding?: EncodingOption;164 readonly encoding?: Encoding;165166 /**167 Set `result.stdout`, `result.stderr`, `result.all` and `result.stdio` as arrays of strings, splitting the subprocess' output into lines.168
210 unmodified lines
379// Different values for stdout and stderr380await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;381```382*/378export type Options = CommonOptions<false>;383type TextOptions<IsSync extends boolean> = CommonOptions<IsSync, TextEncodingOption | undefined>;384type BinaryOptions<IsSync extends boolean> = Omit<CommonOptions<IsSync, BinaryEncodingOption>, 'encoding'> & {385 readonly encoding: BinaryEncodingOption;386};387388export type Options = TextOptions<false> | BinaryOptions<false>;389390/**391Subprocess options, with synchronous methods.392393Some options are related to the subprocess output: `verbose`, `lines`, `stripFinalNewline`, `buffer`, `maxBuffer`. By default, those options apply to all file descriptors (`stdout`, `stderr`, etc.). A plain object can be passed instead to apply them to only `stdout`, `stderr`, `all` (both stdout and stderr), `ipc`, `fd3`, etc.
7 unmodified lines
401// Different values for stdout and stderr402execaSync({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;403```404*/395export type SyncOptions = CommonOptions<true>;405export type SyncOptions = TextOptions<true> | BinaryOptions<true>;406407export type StricterOptions<408 WideOptions extends CommonOptions,409 StrictOptions extends CommonOptions,
3 unmodified lines
modified:types/methods/script.d.ts+1 added-1 removed
63 unmodified lines
64 & {s: ExecaScriptSyncMethod<OptionsType>};6566// `$(options)` binding67type ExecaScriptBind<OptionsType extends CommonOptions> =68 <NewOptionsType extends CommonOptions = {}>(options: NewOptionsType)68 <NewOptionsType extends Options = {}>(options: NewOptionsType)69 => ExecaScriptMethod<OptionsType & NewOptionsType>;7071// `$`command`` template syntax72type ExecaScriptTemplate<OptionsType extends CommonOptions> =
45 unmodified lines
modified:types/stdio/type.d.ts+37 added-25 removed
50 unmodified lines
51type CommonStdioOption<52 IsSync extends boolean,53 IsExtra extends boolean,54 IsArray extends boolean,55 ObjectModeChunk = unknown,56 TransformChunk = string,57> =58 // TypeScript cannot contextually type inline full generator transform objects through this broad stdio union because the union also includes `{transform: Duplex | TransformStream}` wrapper objects. Users should annotate `chunk` when assigning an inline object directly to `StdinOption`, `StdoutStderrOption`, `Options['stdout']`, etc. Keep `GeneratorTransformFull` mode branches narrow, but do not add a broad full-object fallback here to try to recover contextual typing.56 SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync> | GeneratorTransformFull<IsSync> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | NativePipeStdioOption<IsSync, IsExtra> | {readonly file: string; readonly append?: boolean};59 SimpleStdioOption<IsSync, IsExtra, IsArray> | URL | GeneratorTransform<IsSync, TransformChunk> | GeneratorTransformFull<IsSync, ObjectModeChunk, TransformChunk> | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9> | Unless<IsSync, DuplexTransform | WebTransform | TransformStream> | NativePipeStdioOption<IsSync, IsExtra> | {readonly file: string; readonly append?: boolean};6061// Synchronous iterables excluding strings, Uint8Arrays and Arrays62type IterableObject<IsArray extends boolean> = Iterable<unknown>63 & object
30 unmodified lines
94type StdinSingleOption<95 IsSync extends boolean,96 IsExtra extends boolean,97 IsArray extends boolean,98 TransformChunk = string,99> =96 | CommonStdioOption<IsSync, IsExtra, IsArray>100 | CommonStdioOption<IsSync, IsExtra, IsArray, unknown, TransformChunk>101 | InputStdioOption<IsSync, IsExtra, IsArray>;102103// `options.stdin`104export type StdinOptionCommon<105 IsSync extends boolean = boolean,106 IsExtra extends boolean = boolean,103> =104 | StdinSingleOption<IsSync, IsExtra, false>105 | ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true>>;107 TransformChunk = string,108> = TransformChunk extends unknown109 ? | StdinSingleOption<IsSync, IsExtra, false, TransformChunk>110 | ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true, TransformChunk>>111 : never;112113// `options.stdin`, async114export type StdinOption = StdinOptionCommon<false, false>;115// `options.stdin`, sync
3 unmodified lines
119type StdoutStderrSingleOption<120 IsSync extends boolean,121 IsExtra extends boolean,122 IsArray extends boolean,123 TransformChunk = string,124> =118 | CommonStdioOption<IsSync, IsExtra, IsArray>125 | CommonStdioOption<IsSync, IsExtra, IsArray, TransformChunk, TransformChunk>126 | OutputStdioOption<IsSync, IsArray>;127128// `options.stdout|stderr`129// In `objectMode`, the `chunk` argument of every array item is typed as `string`, even though only the first transform in the pipeline receives subprocess lines. The array index cannot be used to infer the pipeline position, since non-transform items are filtered out and transforms are reordered at runtime.130export type StdoutStderrOptionCommon<131 IsSync extends boolean = boolean,132 IsExtra extends boolean = boolean,125> =126 | StdoutStderrSingleOption<IsSync, IsExtra, false>127 | ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true>>;133 TransformChunk = string,134> = TransformChunk extends unknown135 ? | StdoutStderrSingleOption<IsSync, IsExtra, false, TransformChunk>136 | ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true, TransformChunk>>137 : never;138139// `options.stdout|stderr`, async140export type StdoutStderrOption = StdoutStderrOptionCommon<false, false>;141// `options.stdout|stderr`, sync142export type StdoutStderrSyncOption = StdoutStderrOptionCommon<true, false>;143144// `options.stdio[3+]`135type StdioExtraOptionCommon<IsSync extends boolean> =136 | StdinOptionCommon<IsSync, true>137 | StdoutStderrOptionCommon<IsSync, true>;145type StdioExtraOptionCommon<IsSync extends boolean, TransformChunk = string> =146 | StdinOptionCommon<IsSync, true, TransformChunk>147 | StdoutStderrOptionCommon<IsSync, true, TransformChunk>;148149// `options.stdin|stdout|stderr|stdio` array items150type StdioSingleOption<151 IsSync extends boolean = boolean,152 IsExtra extends boolean = boolean,153 IsArray extends boolean = boolean,154 TransformChunk = string,155> =145 | StdinSingleOption<IsSync, IsExtra, IsArray>146 | StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;156 | StdinSingleOption<IsSync, IsExtra, IsArray, TransformChunk>157 | StdoutStderrSingleOption<IsSync, IsExtra, IsArray, TransformChunk>;158159// Get `options.stdin|stdout|stderr|stdio` items if it is an array, else keep as is160export type StdioSingleOptionItems<StdioOptionType> = StdioOptionType extends readonly StdioSingleOption[]161 ? StdioOptionType[number]162 : StdioOptionType;163164// `options.stdin|stdout|stderr|stdio`154export type StdioOptionCommon<IsSync extends boolean = boolean> =155 | StdinOptionCommon<IsSync>156 | StdoutStderrOptionCommon<IsSync>;165export type StdioOptionCommon<IsSync extends boolean = boolean, TransformChunk = string> =166 | StdinOptionCommon<IsSync, boolean, TransformChunk>167 | StdoutStderrOptionCommon<IsSync, boolean, TransformChunk>;168169// `options.stdio` when it is an array159export type StdioOptionsArray<IsSync extends boolean = boolean> = readonly [160 StdinOptionCommon<IsSync, false>,161 StdoutStderrOptionCommon<IsSync, false>,162 StdoutStderrOptionCommon<IsSync, false>,163 ...ReadonlyArray<StdioExtraOptionCommon<IsSync>>,170export type StdioOptionsArray<IsSync extends boolean = boolean, TransformChunk = string> = readonly [171 StdinOptionCommon<IsSync, false, TransformChunk>,172 StdoutStderrOptionCommon<IsSync, false, TransformChunk>,173 StdoutStderrOptionCommon<IsSync, false, TransformChunk>,174 ...ReadonlyArray<StdioExtraOptionCommon<IsSync, TransformChunk>>,175];176177// `options.stdio`167export type StdioOptionsProperty<IsSync extends boolean = boolean> =168 | SimpleStdioOption<IsSync, false, false>169 | StdioOptionsArray<IsSync>;178export type StdioOptionsProperty<IsSync extends boolean = boolean, TransformChunk = string> = TransformChunk extends unknown179 ? | SimpleStdioOption<IsSync, false, false>180 | StdioOptionsArray<IsSync, TransformChunk>181 : never;182183export {};
modified:types/transform/normalize.d.ts+45 added-10 removed
1 unmodified line
2import type {Duplex} from 'node:stream';3import type {Unless} from '../utils.js';45// `options.std*: Generator`6// @todo Use `string`, `Uint8Array` or `unknown` for both the argument and the return type, based on whether `encoding: 'buffer'` and `objectMode: true` are used.6// The `chunk` argument's type is based on the transform's mode:7// - `binary: true` or binary `encoding`, with `objectMode: true` -> `unknown` for stdin, `Uint8Array` for stdout/stderr8// - `binary: true` or binary `encoding` -> `Uint8Array`9// - `objectMode: true` -> `unknown` for stdin, `string` for stdout/stderr10// - otherwise -> `string`11// The return type is kept as `unknown` since a transform can always yield either a `string` or an `Uint8Array`.12// See https://github.com/sindresorhus/execa/issues/6948export type GeneratorTransform<IsSync extends boolean> = (chunk: unknown) =>13export type GeneratorTransform<IsSync extends boolean, Chunk = string> = (chunk: Chunk) =>14 | Unless<IsSync, AsyncGenerator<unknown, void, void>>15 | Generator<unknown, void, void>;16type GeneratorTransformReturn<IsSync extends boolean> = ReturnType<GeneratorTransform<IsSync>>;17type GeneratorFinal<IsSync extends boolean> = () =>18 | Unless<IsSync, AsyncGenerator<unknown, void, void>>19 | Generator<unknown, void, void>;20
3 unmodified lines
24 */25 readonly objectMode?: boolean;26};2722/**23A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.2425A transform is either a generator function or a plain object with the following members.26*/27export type GeneratorTransformFull<IsSync extends boolean> = TransformCommon & {28// A `GeneratorTransformFull` shape whose `transform` narrows the `chunk` argument to `Chunk`.29// The mode-independent documentation lives here.30type GeneratorTransformBase<IsSync extends boolean, Chunk> = TransformCommon & {31 /**32 Map or filter the input or output of the subprocess.33 */31 readonly transform: GeneratorTransform<IsSync>;34 readonly transform: GeneratorTransform<IsSync, Chunk>;3536 /**37 Create additional lines after the last one.38 */
4 unmodified lines
43 */44 readonly binary?: boolean;4546 /**44 If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.47 If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produce a single line.48 */49 readonly preserveNewlines?: boolean;50};5152// For stdout/stderr, this literal option pair accepts explicitly typed `Uint8Array` callbacks, matching runtime binary chunks, while still allowing stdin-style `unknown` object-mode callbacks.53type GeneratorTransformBinaryObjectMode<IsSync extends boolean, ObjectModeChunk> = ObjectModeChunk extends string54 ? Omit<GeneratorTransformBase<IsSync, unknown>, 'transform' | 'objectMode' | 'binary'> & {55 /**56 Map or filter the input or output of the subprocess.57 */58 transform(chunk: Uint8Array): GeneratorTransformReturn<IsSync>;59 // eslint-disable-next-line @typescript-eslint/unified-signatures -- Combining these as `unknown | Uint8Array` would collapse to `unknown` and reject explicitly typed `Uint8Array` callbacks.60 transform(chunk: unknown): GeneratorTransformReturn<IsSync>;6162 readonly objectMode: true;63 readonly binary: true;64 }65 : GeneratorTransformBase<IsSync, unknown> & {readonly objectMode: true; readonly binary: true};6667/**68A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.6970A transform is either a generator function or a plain object with the following members.71*/72export type GeneratorTransformFull<IsSync extends boolean, ObjectModeChunk = unknown, TransformChunk = string> =73 // Non-literal `binary` or `objectMode` values are accepted at runtime, but their mode is unknown at compile time.74 | (GeneratorTransformBase<IsSync, unknown> & {readonly objectMode?: boolean; readonly binary?: boolean})75 // `binary: true` without `objectMode` -> the `chunk` argument is an `Uint8Array`.76 | (GeneratorTransformBase<IsSync, Uint8Array> & {readonly objectMode?: false; readonly binary: true})77 // `binary: true` with `objectMode` -> the `chunk` argument is `unknown` for stdin, `Uint8Array` for stdout/stderr.78 | GeneratorTransformBinaryObjectMode<IsSync, ObjectModeChunk>79 // `binary: false` without `objectMode` -> the `chunk` argument depends on the subprocess encoding.80 | (GeneratorTransformBase<IsSync, TransformChunk> & {readonly objectMode?: false; readonly binary?: false})81 // `objectMode: true` with `binary: false` -> the `chunk` argument depends on the stdio direction.82 | (GeneratorTransformBase<IsSync, ObjectModeChunk> & {readonly objectMode: true; readonly binary?: false});8384// `options.std*: Duplex`85export type DuplexTransform = TransformCommon & {86 readonly transform: Duplex;87};
7 unmodified lines