Skip to content

Commit

Permalink
Remove requirement for bundler custom config (#489)
Browse files Browse the repository at this point in the history
* feat: use buffer, events, process dependencies and require it

* fix: added missing process file for browserify bug workaround

* fix: patch code after use strict
  • Loading branch information
bergos committed Sep 20, 2022
1 parent e95c761 commit 5eb402c
Show file tree
Hide file tree
Showing 45 changed files with 195 additions and 28 deletions.
7 changes: 6 additions & 1 deletion build/build.mjs
Expand Up @@ -15,6 +15,7 @@ import { headers } from './headers.mjs'
import { replacements } from './replacements.mjs'

const baseMatcher = /^(?:lib|test)/
const strictMatcher = /^(['"']use strict.+)/

function highlightFile(file, color) {
return `\x1b[${color}m${file.replace(process.cwd() + '/', '')}\x1b[0m`
Expand Down Expand Up @@ -101,7 +102,11 @@ async function processFiles(contents) {

for (const footerKey of matchingHeaders) {
for (const header of headers[footerKey]) {
content = header + content
if (content.match(strictMatcher)) {
content = content.replace(strictMatcher, `$&;${header}`)
} else {
content = header + content
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions build/headers.mjs
@@ -1,3 +1,19 @@
const bufferRequire = `
/* replacement start */
const { Buffer } = require('buffer')
/* replacement end */
`

const processRequire = `
/* replacement start */
const process = require('process')
/* replacement end */
`

const testPolyfills = `
/* replacement start */
const AbortController = globalThis.AbortController || require('abort-controller').AbortController;
Expand All @@ -16,6 +32,16 @@ const testPolyfills = `
`

export const headers = {
'lib/stream.js':
[bufferRequire],
'lib/internal/streams/(destroy|duplexify|end-of-stream|from|pipeline|readable|writable).js':
[processRequire],
'test/browser/test-stream-(big-packet|pipe-cleanup-pause|pipeline|readable-event|transform-constructor-set-methods|transform-split-objectmode|unshift-empty-chunk|unshift-read-race|writable-change-default-encoding|writable-constructor-set-methods|writable-decoded-encoding|writev).js':
[bufferRequire],
'test/browser/test-stream2-(base64-single-char-read-end|compatibility|large-read-stall|pipe-error-handling|readable-empty-buffer-no-eof|readable-from-list|readable-legacy-drain|readable-non-empty-end|readable-wrap|set-encoding|transform|writable).js':
[bufferRequire],
'test/browser/test-stream3-pause-then-read.js':
[bufferRequire],
'test/parallel/test-stream-(add-abort-signal|drop-take|duplex-destroy|flatMap|forEach|filter|finished|readable-destroy|reduce|toArray|writable-destroy).js':
[testPolyfills]
}
3 changes: 0 additions & 3 deletions build/replacements.mjs
Expand Up @@ -34,8 +34,6 @@ const internalStreamsNoRequireAbortController = [

const internalStreamsRequireInternal = ["require\\('internal/([^']+)'\\)", "require('../$1')"]

const internalStreamsNoRequireBuffer = ["const \\{ Buffer \\} = require\\('buffer'\\);", '']

const internalStreamsRequireErrors = ["require\\('internal/errors'\\)", "require('../../ours/errors')"]

const internalStreamsRequireEventEmitter = ['const EE =', 'const { EventEmitter: EE } =']
Expand Down Expand Up @@ -233,7 +231,6 @@ export const replacements = {
internalStreamsNoRequireAbortController
],
'lib/internal/streams/.+': [
internalStreamsNoRequireBuffer,
internalStreamsRequireErrors,
internalStreamsRequireEventEmitter,
internalStreamsRequirePrimordials,
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/streams/buffer_list.js
Expand Up @@ -2,6 +2,8 @@

const { StringPrototypeSlice, SymbolIterator, TypedArrayPrototypeSet, Uint8Array } = require('../../ours/primordials')

const { Buffer } = require('buffer')

const { inspect } = require('../../ours/util')

module.exports = class BufferList {
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/streams/destroy.js
@@ -1,5 +1,10 @@
'use strict'

/* replacement start */

const process = require('process')
/* replacement end */

const {
aggregateTwoErrors,
codes: { ERR_MULTIPLE_CALLBACK },
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/streams/duplexify.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */
const process = require('process')
/* replacement end */

;('use strict')

const bufferModule = require('buffer')

Expand Down
6 changes: 5 additions & 1 deletion lib/internal/streams/end-of-stream.js
@@ -1,6 +1,10 @@
/* replacement start */
const process = require('process')
/* replacement end */
// Ported from https://github.com/mafintosh/end-of-stream with
// permission from the author, Mathias Buus (@mafintosh).
'use strict'

;('use strict')

const { AbortError, codes } = require('../../ours/errors')

Expand Down
7 changes: 7 additions & 0 deletions lib/internal/streams/from.js
@@ -1,7 +1,14 @@
'use strict'

/* replacement start */

const process = require('process')
/* replacement end */

const { PromisePrototypeThen, SymbolAsyncIterator, SymbolIterator } = require('../../ours/primordials')

const { Buffer } = require('buffer')

const { ERR_INVALID_ARG_TYPE, ERR_STREAM_NULL_VALUES } = require('../../ours/errors').codes

function from(Readable, iterable, opts) {
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/streams/pipeline.js
@@ -1,6 +1,10 @@
/* replacement start */
const process = require('process')
/* replacement end */
// Ported from https://github.com/mafintosh/pump with
// permission from the author, Mathias Buus (@mafintosh).
'use strict'

;('use strict')

const { ArrayIsArray, Promise, SymbolAsyncIterator } = require('../../ours/primordials')

Expand Down
8 changes: 7 additions & 1 deletion lib/internal/streams/readable.js
@@ -1,3 +1,6 @@
/* replacement start */
const process = require('process')
/* replacement end */
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -18,7 +21,8 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'

;('use strict')

const {
ArrayPrototypeIndexOf,
Expand All @@ -41,6 +45,8 @@ const { EventEmitter: EE } = require('events')

const { Stream, prependListener } = require('./legacy')

const { Buffer } = require('buffer')

const { addAbortSignal } = require('./add-abort-signal')

const eos = require('./end-of-stream')
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/streams/writable.js
@@ -1,3 +1,6 @@
/* replacement start */
const process = require('process')
/* replacement end */
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -21,7 +24,8 @@
// A bit simpler than readable streams.
// Implement an async ._write(chunk, encoding, cb), and it'll handle all
// the drain event emission and buffering.
'use strict'

;('use strict')

const {
ArrayPrototypeSlice,
Expand All @@ -42,6 +46,8 @@ const { EventEmitter: EE } = require('events')

const Stream = require('./legacy').Stream

const { Buffer } = require('buffer')

const destroyImpl = require('./destroy')

const { addAbortSignal } = require('./add-abort-signal')
Expand Down
6 changes: 5 additions & 1 deletion lib/stream.js
@@ -1,3 +1,6 @@
/* replacement start */
const { Buffer } = require('buffer')
/* replacement end */
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -18,7 +21,8 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'

;('use strict')

const { ObjectDefineProperty, ObjectKeys, ReflectApply } = require('./ours/primordials')

Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -44,7 +44,10 @@
"lint": "eslint src"
},
"dependencies": {
"abort-controller": "^3.0.0"
"abort-controller": "^3.0.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"process": "^0.11.10"
},
"devDependencies": {
"@babel/core": "^7.17.10",
Expand All @@ -55,7 +58,6 @@
"@rollup/plugin-node-resolve": "^13.3.0",
"@sinonjs/fake-timers": "^9.1.2",
"browserify": "^17.0.0",
"buffer-es6": "^4.9.3",
"c8": "^7.11.2",
"esbuild": "^0.14.39",
"esbuild-plugin-alias": "^0.2.1",
Expand All @@ -66,7 +68,6 @@
"eslint-plugin-promise": "^6.0.0",
"playwright": "^1.21.1",
"prettier": "^2.6.2",
"process-es6": "^0.11.6",
"rollup": "^2.72.1",
"rollup-plugin-polyfill-node": "^0.9.0",
"tap": "^16.2.0",
Expand Down
3 changes: 3 additions & 0 deletions process
@@ -0,0 +1,3 @@
// workaround for browserify bug: https://github.com/browserify/browserify/issues/1986

module.exports = require('./node_modules/process')
4 changes: 1 addition & 3 deletions src/test/browser/fixtures/esbuild-browsers-shims.mjs
@@ -1,8 +1,6 @@
import * as bufferModule from 'buffer-es6'
import * as processModule from 'process-es6'
import * as processModule from 'process'

export const process = processModule
export const Buffer = bufferModule.Buffer

export function setImmediate(fn, ...args) {
setTimeout(() => fn(...args), 1)
Expand Down
3 changes: 1 addition & 2 deletions src/test/browser/fixtures/rollup.browser.config.mjs
Expand Up @@ -16,8 +16,7 @@ export default {
commonjs(),
nodePolyfill(),
inject({
process: resolve('node_modules/process-es6/browser.js'),
Buffer: [resolve('node_modules/buffer-es6/index.js'), 'Buffer']
process: resolve('node_modules/process/browser.js')
}),
nodeResolve({
browser: true,
Expand Down
3 changes: 1 addition & 2 deletions src/test/browser/fixtures/webpack.browser.config.mjs
Expand Up @@ -21,8 +21,7 @@ export default {
raw: true
}),
new webpack.ProvidePlugin({
process: require.resolve('process-es6'),
Buffer: [require.resolve('buffer-es6'), 'Buffer']
process: require.resolve('process')
})
],
resolve: {
Expand Down
4 changes: 1 addition & 3 deletions test/browser/fixtures/esbuild-browsers-shims.mjs
@@ -1,8 +1,6 @@
import * as bufferModule from 'buffer-es6'
import * as processModule from 'process-es6'
import * as processModule from 'process'

export const process = processModule
export const Buffer = bufferModule.Buffer

export function setImmediate(fn, ...args) {
setTimeout(() => fn(...args), 1)
Expand Down
3 changes: 1 addition & 2 deletions test/browser/fixtures/rollup.browser.config.mjs
Expand Up @@ -16,8 +16,7 @@ export default {
commonjs(),
nodePolyfill(),
inject({
process: resolve('node_modules/process-es6/browser.js'),
Buffer: [resolve('node_modules/buffer-es6/index.js'), 'Buffer']
process: resolve('node_modules/process/browser.js')
}),
nodeResolve({
browser: true,
Expand Down
3 changes: 1 addition & 2 deletions test/browser/fixtures/webpack.browser.config.mjs
Expand Up @@ -21,8 +21,7 @@ export default {
raw: true
}),
new webpack.ProvidePlugin({
process: require.resolve('process-es6'),
Buffer: [require.resolve('buffer-es6'), 'Buffer']
process: require.resolve('process')
})
],
resolve: {
Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-big-packet.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const inherits = require('inherits')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-pipe-cleanup-pause.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const stream = require('../../lib/ours/index')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-pipeline.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Readable, Writable, pipeline } = require('../../lib/ours/index')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-readable-event.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Readable } = require('../../lib/ours/index')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-transform-constructor-set-methods.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Transform } = require('../../lib/ours/index')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-transform-split-objectmode.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Transform } = require('../../lib/ours/index')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-unshift-empty-chunk.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Readable } = require('../../lib/ours/index')

Expand Down
7 changes: 6 additions & 1 deletion test/browser/test-stream-unshift-read-race.js
@@ -1,4 +1,9 @@
'use strict' // This test verifies that:
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */
// This test verifies that:
// 1. unshift() does not cause colliding _read() calls.
// 2. unshift() after the 'end' event is an error, but after the EOF
// signalling null, it is ok, and just creates a new readable chunk.
Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-writable-change-default-encoding.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const inherits = require('inherits')

Expand Down
4 changes: 4 additions & 0 deletions test/browser/test-stream-writable-constructor-set-methods.js
@@ -1,4 +1,8 @@
'use strict'
/* replacement start */

const { Buffer } = require('buffer')
/* replacement end */

const { Writable } = require('../../lib/ours/index')

Expand Down

0 comments on commit 5eb402c

Please sign in to comment.