Skip to content

Commit

Permalink
Upgrade get-stream (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Aug 18, 2023
1 parent f31fbdb commit 88ee3bd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
22 changes: 18 additions & 4 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {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 => {
Expand Down Expand Up @@ -79,6 +80,9 @@ const getBufferedData = async (stream, streamPromise) => {
return;
}

// Wait for the `all` stream to receive the last chunk before destroying the stream
await setTimeout(0);

stream.destroy();

try {
Expand All @@ -93,11 +97,21 @@ const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => {
return;
}

if (encoding) {
return getStream(stream, {encoding, maxBuffer});
// eslint-disable-next-line unicorn/text-encoding-identifier-case
if (encoding === 'utf8' || encoding === 'utf-8') {
return getStream(stream, {maxBuffer});
}

if (encoding === null) {
return getStreamAsBuffer(stream, {maxBuffer});
}

return getStream.buffer(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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
],
"dependencies": {
"cross-spawn": "^7.0.3",
"get-stream": "^6.0.1",
"get-stream": "^8.0.1",
"human-signals": "^5.0.0",
"is-stream": "^3.0.0",
"merge-stream": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/noop-no-newline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
import process from 'node:process';

process.stdout.write(process.argv[2]);
28 changes: 27 additions & 1 deletion test/stream.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {Buffer} from 'node:buffer';
import {exec} from 'node:child_process';
import process from 'node:process';
import fs from 'node:fs';
import Stream from 'node:stream';
import {promisify} from 'node:util';
import test from 'ava';
import getStream from 'get-stream';
import {pEvent} from 'p-event';
import tempfile from 'tempfile';
import {execa, execaSync, $} from '../index.js';
import {setFixtureDir} from './helpers/fixtures-dir.js';
import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';

setFixtureDir();

Expand All @@ -17,6 +19,30 @@ test('buffer', async t => {
t.is(stdout.toString(), 'foo');
});

const checkEncoding = async (t, encoding) => {
const {stdout} = await execa('noop-no-newline.js', [STRING_TO_ENCODE], {encoding});
t.is(stdout, Buffer.from(STRING_TO_ENCODE).toString(encoding));

const {stdout: nativeStdout} = await promisify(exec)(`node noop-no-newline.js ${STRING_TO_ENCODE}`, {encoding, cwd: FIXTURES_DIR});
t.is(stdout, nativeStdout);
};

// This string gives different outputs with each encoding type
const STRING_TO_ENCODE = '\u1000.';

test('can pass encoding "utf8"', checkEncoding, 'utf8');
test('can pass encoding "utf-8"', checkEncoding, 'utf8');
test('can pass encoding "utf16le"', checkEncoding, 'utf16le');
test('can pass encoding "utf-16le"', checkEncoding, 'utf16le');
test('can pass encoding "ucs2"', checkEncoding, 'utf16le');
test('can pass encoding "ucs-2"', checkEncoding, 'utf16le');
test('can pass encoding "latin1"', checkEncoding, 'latin1');
test('can pass encoding "binary"', checkEncoding, 'latin1');
test('can pass encoding "ascii"', checkEncoding, 'ascii');
test('can pass encoding "hex"', checkEncoding, 'hex');
test('can pass encoding "base64"', checkEncoding, 'base64');
test('can pass encoding "base64url"', checkEncoding, 'base64url');

test('pass `stdout` to a file descriptor', async t => {
const file = tempfile({extension: '.txt'});
await execa('noop.js', ['foo bar'], {stdout: fs.openSync(file, 'w')});
Expand Down

0 comments on commit 88ee3bd

Please sign in to comment.