Skip to content

Commit 8c9c011

Browse files
authored
fix: don't require stream package in codebase (#520)
* fix: dont have require('stream') * chore: add stream importing regression tests to CI * chore: fix webpacking dist * chore: fix runner-prepare script
1 parent ba27fd8 commit 8c9c011

File tree

8 files changed

+102
-3
lines changed

8 files changed

+102
-3
lines changed

.github/workflows/browsers.yml

+2
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ jobs:
3838
run: ./node_modules/.bin/playwright install ${{ fromJSON('{"chrome":"chromium","edge":"msedge","firefox":"firefox","safari":"webkit"}')[matrix.browser] }}
3939
- name: Bundle code
4040
run: npm run test:prepare ${{ matrix.bundler }}
41+
- name: Bundle readable-stream code with readable-stream specific bundlers
42+
run: npm run test:readable-stream-only ${{ matrix.bundler }}
4143
- name: Run Tests on Browsers
4244
run: npm run test:browsers ${{ matrix.browser }} ${{ matrix.bundler }}

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ coverage/
22
node_modules/
33
node-*.tar.gz
44
package-lock.json
5-
tmp/
5+
tmp/
6+
readable-stream-test/dist/

build/replacements.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ const readmeInfo = ['(This package is a mirror of the streams implementations in
236236

237237
const readmeLink = ['(\\[Node.js website\\]\\(https://nodejs.org/dist/v)(\\d+.\\d+.\\d+)', '$1$2']
238238

239+
const streamRequire = [ "require\\('stream'\\)", "require('../../lib/stream.js')" ]
240+
239241
export const replacements = {
240242
'lib/_stream.+': [legacyStreamsRequireStream],
241243
'lib/internal/streams/duplexify.+': [
@@ -288,7 +290,7 @@ export const replacements = {
288290
streamIndexRequireUtil,
289291
streamIndexRequireInternal
290292
],
291-
'lib/stream/.+': [streamsRequireErrors, streamsRequirePrimordials, streamsRequireInternal],
293+
'lib/stream/.+': [streamsRequireErrors, streamsRequirePrimordials, streamsRequireInternal, streamRequire],
292294
'test/common/index.js': [testCommonKnownGlobals],
293295
'test/parallel/.+': [
294296
testParallelIncludeTap,

lib/stream/promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { ArrayPrototypePop, Promise } = require('../ours/primordials')
44
const { isIterable, isNodeStream, isWebStream } = require('../internal/streams/utils')
55
const { pipelineImpl: pl } = require('../internal/streams/pipeline')
66
const { finished } = require('../internal/streams/end-of-stream')
7-
require('stream')
7+
require('../../lib/stream.js')
88
function pipeline(...streams) {
99
return new Promise((resolve, reject) => {
1010
let signal

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"test:prepare": "node test/browser/runner-prepare.mjs",
4040
"test:browsers": "node test/browser/runner-browser.mjs",
4141
"test:bundlers": "node test/browser/runner-node.mjs",
42+
"test:readable-stream-only": "node readable-stream-test/runner-prepare.mjs",
4243
"coverage": "c8 -c ./c8.json tap --rcfile=./tap.yml test/parallel/test-*.js test/ours/test-*.js",
4344
"format": "prettier -w src lib test",
4445
"lint": "eslint src"

readable-stream-test/import.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as all from '../lib/ours'
2+
import * as allBrowser from '../lib/ours/browser'
3+
import * as allRoot from '../lib/ours'
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { exec } from 'child_process'
2+
import util from '../lib/ours/util.js'
3+
4+
function info(message) {
5+
console.log(`\x1b[34m[INFO]\x1b[0m ${message}`)
6+
}
7+
8+
function error(message) {
9+
console.log(`\x1b[31m[INFO]\x1b[0m ${message}`)
10+
}
11+
12+
async function run(command) {
13+
info(`Executing \x1b[33m${command}\x1b[0m ...`)
14+
const { promise, reject, resolve } = util.createDeferredPromise()
15+
16+
let hasOutput = false
17+
function logOutput(chunk) {
18+
if (!hasOutput) {
19+
hasOutput = true
20+
console.log('')
21+
}
22+
23+
console.log(chunk.toString('utf-8').trim().replace(/^/gm, ' '))
24+
}
25+
26+
try {
27+
const process = exec(command, { stdio: 'pipe' }, (error) => {
28+
if (error) {
29+
return reject(error)
30+
}
31+
32+
resolve(error)
33+
})
34+
35+
process.stdout.on('data', logOutput)
36+
process.stderr.on('data', logOutput)
37+
await promise
38+
39+
if (hasOutput) {
40+
console.log('')
41+
}
42+
} catch (e) {
43+
if (hasOutput) {
44+
console.log('')
45+
}
46+
47+
error(`Command failed with status code ${e.code}.`)
48+
process.exit(1)
49+
}
50+
}
51+
52+
async function main() {
53+
const validBundlers = ['browserify', 'esbuild', 'rollup', 'webpack']
54+
const bundler = process.argv[2] || process.env.BUNDLER
55+
56+
if (!validBundlers.includes(bundler)) {
57+
error(`Usage: node await runner-prepare.mjs [${validBundlers.join('|')}]`)
58+
error('You can also use the BUNDLER environment variable.')
59+
process.exit(1)
60+
}
61+
62+
switch (bundler) {
63+
case 'browserify':
64+
break
65+
case 'esbuild':
66+
break
67+
case 'rollup':
68+
break
69+
case 'webpack':
70+
await run('webpack -c readable-stream-test/webpack.config.mjs')
71+
}
72+
}
73+
74+
main().catch((e) => {
75+
error(e)
76+
process.exit(1)
77+
})
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { resolve } from 'path'
2+
import { fileURLToPath } from 'url'
3+
4+
const rootDir = resolve(fileURLToPath(new URL('.', import.meta.url)), '../')
5+
6+
export default {
7+
mode: 'development',
8+
entry: './readable-stream-test/import.js',
9+
output: {
10+
path: resolve(rootDir, 'readable-stream-test', 'dist'),
11+
filename: 'import.js'
12+
}
13+
}

0 commit comments

Comments
 (0)