Upgrade `get-stream` (#571)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent f31fbdb · Browse files · 4 files·+50 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+50 added−6 removed
- tests:+31 added−1 removed
- other:+19 added−5 removed
Provenance
- Commit
88ee3bd- Tree
12d6b75- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f31fbdb
4 files changed+50 added−6 removed
modified:lib/stream.js+18 added-4 removed
12334567870 unmodified lines79808182838485868788896 unmodified lines96979899100969710110210310410510010610710810911011111211311411511611711811912013 unmodified linesimport {createReadStream, readFileSync} from 'node:fs';import {setTimeout} from 'node:timers/promises';import {isStream} from 'is-stream';import getStream from 'get-stream';import getStream, {getStreamAsBuffer} from 'get-stream';import mergeStream from 'merge-stream';
const validateInputOptions = input => { if (input !== undefined) {70 unmodified lines if (!stream || streamPromise === undefined) { return; }
// Wait for the `all` stream to receive the last chunk before destroying the stream await setTimeout(0);
stream.destroy();
try { return await streamPromise;6 unmodified lines if (!stream || !buffer) { return; }
// eslint-disable-next-line unicorn/text-encoding-identifier-case if (encoding) { return getStream(stream, {encoding, maxBuffer}); if (encoding === 'utf8' || encoding === 'utf-8') { return getStream(stream, {maxBuffer}); }
if (encoding === null) { return getStream.buffer(stream, {maxBuffer}); return getStreamAsBuffer(stream, {maxBuffer}); }
return applyEncoding(stream, maxBuffer, encoding);};
const applyEncoding = async (stream, maxBuffer, encoding) => { const buffer = await getStreamAsBuffer(stream, {maxBuffer}); return buffer.toString(encoding);};
// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)export const getSpawnedResult = async ({stdout, stderr, all}, {encoding, buffer, maxBuffer}, processDone) => { const stdoutPromise = getStreamPromise(stdout, {encoding, buffer, maxBuffer}); const stderrPromise = getStreamPromise(stderr, {encoding, buffer, maxBuffer});13 unmodified linesadded:test/fixtures/noop-no-newline.js+4 added
1234#!/usr/bin/env nodeimport process from 'node:process';
process.stdout.write(process.argv[2]);modified:test/stream.js+27 added-1 removed
1 import {Buffer} from 'node:buffer';2+import {exec} from 'node:child_process';3 import process from 'node:process';4 import fs from 'node:fs';5 import Stream from 'node:stream';6+import {promisify} from 'node:util';7 import test from 'ava';8 import getStream from 'get-stream';9 import {pEvent} from 'p-event';10 import tempfile from 'tempfile';11 import {execa, execaSync, $} from '../index.js';10-import {setFixtureDir} from './helpers/fixtures-dir.js';12+import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';13 14 setFixtureDir();15 16 test('buffer', async t => {1 unmodified line18 t.true(Buffer.isBuffer(stdout));19 t.is(stdout.toString(), 'foo');20 });21 22+const checkEncoding = async (t, encoding) => {23+ const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});24+ t.is(stdout, Buffer.from(STRING_TO_ENCODE).toString(encoding));25+26+ const {stdout: nativeStdout} = await promisify(exec)(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});27+ t.is(stdout, nativeStdout);28+};29+30+// This string gives different outputs with each encoding type31+const STRING_TO_ENCODE = '\u1000.';32+33+test('can pass encoding "utf8"', checkEncoding, 'utf8');34+test('can pass encoding "utf-8"', checkEncoding, 'utf8');35+test('can pass encoding "utf16le"', checkEncoding, 'utf16le');36+test('can pass encoding "utf-16le"', checkEncoding, 'utf16le');37+test('can pass encoding "ucs2"', checkEncoding, 'utf16le');38+test('can pass encoding "ucs-2"', checkEncoding, 'utf16le');39+test('can pass encoding "latin1"', checkEncoding, 'latin1');40+test('can pass encoding "binary"', checkEncoding, 'latin1');41+test('can pass encoding "ascii"', checkEncoding, 'ascii');42+test('can pass encoding "hex"', checkEncoding, 'hex');43+test('can pass encoding "base64"', checkEncoding, 'base64');44+test('can pass encoding "base64url"', checkEncoding, 'base64url');45+46 test('pass `stdout` to a file descriptor', async t => {47 const file = tempfile({extension: '.txt'});48 await execa('noop.js', ['foo bar'], {stdout: fs.openSync(file, 'w')});49 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');233 unmodified linesmodified:package.json+1 added-1 removed
44 unmodified lines45 "zx"46 ],47 "dependencies": {48 "cross-spawn": "^7.0.3",49- "get-stream": "^6.0.1",49+ "get-stream": "^8.0.1",50 "human-signals": "^5.0.0",51 "is-stream": "^3.0.0",52 "merge-stream": "^2.0.0",53 "npm-run-path": "^5.1.0",37 unmodified lines