Skip to content

Commit 5eb402c

Browse files
authored
Remove requirement for bundler custom config (#489)
* feat: use buffer, events, process dependencies and require it * fix: added missing process file for browserify bug workaround * fix: patch code after use strict
1 parent e95c761 commit 5eb402c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+195
-28
lines changed

build/build.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { headers } from './headers.mjs'
1515
import { replacements } from './replacements.mjs'
1616

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

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

102103
for (const footerKey of matchingHeaders) {
103104
for (const header of headers[footerKey]) {
104-
content = header + content
105+
if (content.match(strictMatcher)) {
106+
content = content.replace(strictMatcher, `$&;${header}`)
107+
} else {
108+
content = header + content
109+
}
105110
}
106111
}
107112
}

build/headers.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
const bufferRequire = `
2+
/* replacement start */
3+
4+
const { Buffer } = require('buffer')
5+
6+
/* replacement end */
7+
`
8+
9+
const processRequire = `
10+
/* replacement start */
11+
12+
const process = require('process')
13+
14+
/* replacement end */
15+
`
16+
117
const testPolyfills = `
218
/* replacement start */
319
const AbortController = globalThis.AbortController || require('abort-controller').AbortController;
@@ -16,6 +32,16 @@ const testPolyfills = `
1632
`
1733

1834
export const headers = {
35+
'lib/stream.js':
36+
[bufferRequire],
37+
'lib/internal/streams/(destroy|duplexify|end-of-stream|from|pipeline|readable|writable).js':
38+
[processRequire],
39+
'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':
40+
[bufferRequire],
41+
'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':
42+
[bufferRequire],
43+
'test/browser/test-stream3-pause-then-read.js':
44+
[bufferRequire],
1945
'test/parallel/test-stream-(add-abort-signal|drop-take|duplex-destroy|flatMap|forEach|filter|finished|readable-destroy|reduce|toArray|writable-destroy).js':
2046
[testPolyfills]
2147
}

build/replacements.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ const internalStreamsNoRequireAbortController = [
3434

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

37-
const internalStreamsNoRequireBuffer = ["const \\{ Buffer \\} = require\\('buffer'\\);", '']
38-
3937
const internalStreamsRequireErrors = ["require\\('internal/errors'\\)", "require('../../ours/errors')"]
4038

4139
const internalStreamsRequireEventEmitter = ['const EE =', 'const { EventEmitter: EE } =']
@@ -233,7 +231,6 @@ export const replacements = {
233231
internalStreamsNoRequireAbortController
234232
],
235233
'lib/internal/streams/.+': [
236-
internalStreamsNoRequireBuffer,
237234
internalStreamsRequireErrors,
238235
internalStreamsRequireEventEmitter,
239236
internalStreamsRequirePrimordials,

lib/internal/streams/buffer_list.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
const { Buffer } = require('buffer')
6+
57
const { inspect } = require('../../ours/util')
68

79
module.exports = class BufferList {

lib/internal/streams/destroy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict'
22

3+
/* replacement start */
4+
5+
const process = require('process')
6+
/* replacement end */
7+
38
const {
49
aggregateTwoErrors,
510
codes: { ERR_MULTIPLE_CALLBACK },

lib/internal/streams/duplexify.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
'use strict'
1+
/* replacement start */
2+
const process = require('process')
3+
/* replacement end */
4+
5+
;('use strict')
26

37
const bufferModule = require('buffer')
48

lib/internal/streams/end-of-stream.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
/* replacement start */
2+
const process = require('process')
3+
/* replacement end */
14
// Ported from https://github.com/mafintosh/end-of-stream with
25
// permission from the author, Mathias Buus (@mafintosh).
3-
'use strict'
6+
7+
;('use strict')
48

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

lib/internal/streams/from.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
'use strict'
22

3+
/* replacement start */
4+
5+
const process = require('process')
6+
/* replacement end */
7+
38
const { PromisePrototypeThen, SymbolAsyncIterator, SymbolIterator } = require('../../ours/primordials')
49

10+
const { Buffer } = require('buffer')
11+
512
const { ERR_INVALID_ARG_TYPE, ERR_STREAM_NULL_VALUES } = require('../../ours/errors').codes
613

714
function from(Readable, iterable, opts) {

lib/internal/streams/pipeline.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
/* replacement start */
2+
const process = require('process')
3+
/* replacement end */
14
// Ported from https://github.com/mafintosh/pump with
25
// permission from the author, Mathias Buus (@mafintosh).
3-
'use strict'
6+
7+
;('use strict')
48

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

lib/internal/streams/readable.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* replacement start */
2+
const process = require('process')
3+
/* replacement end */
14
// Copyright Joyent, Inc. and other Node contributors.
25
//
36
// Permission is hereby granted, free of charge, to any person obtaining a
@@ -18,7 +21,8 @@
1821
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1922
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2023
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
'use strict'
24+
25+
;('use strict')
2226

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

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

48+
const { Buffer } = require('buffer')
49+
4450
const { addAbortSignal } = require('./add-abort-signal')
4551

4652
const eos = require('./end-of-stream')

0 commit comments

Comments
 (0)