Skip to content

Commit c04c4db

Browse files
authored
Lazily require abort-controller (#478)
* Lazily require abort-controller, since it throws when it has been bundled, then loaded in node * Make changes to build script and re-run them. Only did 18.0.0 to minimize the changeset.
1 parent 69708c7 commit c04c4db

17 files changed

+315
-282
lines changed

build/replacements.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const internalStreamsAbortControllerPolyfill = [
1414
`
1515
'use strict'
1616
17-
const abortControllerModule = require('abort-controller');
1817
`
1918
]
2019

@@ -30,7 +29,7 @@ const internalStreamsInspectCustom = ['inspect.custom', "Symbol.for('nodejs.util
3029

3130
const internalStreamsNoRequireAbortController = [
3231
'const \\{ AbortController \\} = .+',
33-
'const AbortController = globalThis.AbortController || abortControllerModule.AbortController;'
32+
'const AbortController = globalThis.AbortController || require(\'abort-controller\').AbortController;'
3433
]
3534

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

lib/internal/streams/duplexify.js

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

3-
const abortControllerModule = require('abort-controller')
4-
53
const bufferModule = require('buffer')
64

75
const {
@@ -40,7 +38,8 @@ const isBlob =
4038
: function isBlob(b) {
4139
return false
4240
}
43-
const AbortController = globalThis.AbortController || abortControllerModule.AbortController
41+
42+
const AbortController = globalThis.AbortController || require('abort-controller').AbortController
4443

4544
const { FunctionPrototypeCall } = require('../../ours/primordials') // This is needed for pre node 17.
4645

lib/internal/streams/operators.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
22

3-
const abortControllerModule = require('abort-controller')
4-
5-
const AbortController = globalThis.AbortController || abortControllerModule.AbortController
3+
const AbortController = globalThis.AbortController || require('abort-controller').AbortController
64

75
const {
86
codes: { ERR_INVALID_ARG_TYPE, ERR_MISSING_ARGS, ERR_OUT_OF_RANGE },

lib/internal/streams/pipeline.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// permission from the author, Mathias Buus (@mafintosh).
33
'use strict'
44

5-
const abortControllerModule = require('abort-controller')
6-
75
const { ArrayIsArray, Promise, SymbolAsyncIterator } = require('../../ours/primordials')
86

97
const eos = require('./end-of-stream')
@@ -24,7 +22,8 @@ const { validateFunction, validateAbortSignal } = require('../validators')
2422

2523
const { isIterable, isReadable, isReadableNodeStream, isNodeStream } = require('./utils')
2624

27-
const AbortController = globalThis.AbortController || abortControllerModule.AbortController
25+
const AbortController = globalThis.AbortController || require('abort-controller').AbortController
26+
2827
let PassThrough
2928
let Readable
3029

lib/ours/errors.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,25 @@ function E(code, message, Base) {
8282
}
8383
}
8484

85-
NodeError.prototype.name = Base.name
85+
Object.defineProperties(NodeError.prototype, {
86+
name: {
87+
value: Base.name,
88+
writable: true,
89+
enumerable: false,
90+
configurable: true
91+
},
92+
toString: {
93+
value() {
94+
return `${this.name} [${code}]: ${this.message}`
95+
},
96+
97+
writable: true,
98+
enumerable: false,
99+
configurable: true
100+
}
101+
})
86102
NodeError.prototype.code = code
87103
NodeError.prototype[kIsNodeError] = true
88-
89-
NodeError.prototype.toString = function () {
90-
return `${this.name} [${code}]: ${this.message}`
91-
}
92-
93104
codes[code] = NodeError
94105
}
95106

test/browser/test-stream3-pause-then-read.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ module.exports = function (t) {
4747
function readn(n, then) {
4848
// console.error('read %d', n);
4949
expectEndingData -= n
50-
5150
;(function read() {
5251
const c = r.read(n)
5352

test/common/fixtures.mjs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import fixtures from './fixtures.js';
1+
import fixtures from './fixtures.js'
22

3-
const {
4-
fixturesDir,
5-
path,
6-
fileURL,
7-
readSync,
8-
readKey,
9-
} = fixtures;
3+
const { fixturesDir, path, fileURL, readSync, readKey } = fixtures
104

11-
export {
12-
fixturesDir,
13-
path,
14-
fileURL,
15-
readSync,
16-
readKey,
17-
};
5+
export { fixturesDir, path, fileURL, readSync, readKey }

test/common/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const isOSX = process.platform === 'darwin'
124124

125125
const isPi = (() => {
126126
try {
127-
var _$exec
127+
var _exec
128128

129129
// Normal Raspberry Pi detection is to find the `Raspberry Pi` string in
130130
// the contents of `/sys/firmware/devicetree/base/model` but that doesn't
@@ -133,7 +133,7 @@ const isPi = (() => {
133133
encoding: 'utf8'
134134
})
135135
return (
136-
((_$exec = /^Hardware\s*:\s*(.*)$/im.exec(cpuinfo)) === null || _$exec === undefined ? undefined : _$exec[1]) ===
136+
((_exec = /^Hardware\s*:\s*(.*)$/im.exec(cpuinfo)) === null || _exec === undefined ? undefined : _exec[1]) ===
137137
'BCM2835'
138138
)
139139
} catch {

test/common/index.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createRequire } from 'module';
1+
import { createRequire } from 'module'
22

3-
const require = createRequire(import.meta.url);
4-
const common = require('./index.js');
3+
const require = createRequire(import.meta.url)
4+
const common = require('./index.js')
55

66
const {
77
isMainThread,
@@ -47,7 +47,7 @@ const {
4747
getBufferSources,
4848
getTTYfd,
4949
runWithInvalidFD
50-
} = common;
50+
} = common
5151

5252
export {
5353
isMainThread,
@@ -94,4 +94,4 @@ export {
9494
getTTYfd,
9595
runWithInvalidFD,
9696
createRequire
97-
};
97+
}
Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,82 @@
1-
import '../common/index.mjs';
2-
import { Readable }from '../../lib/ours/index.js';
3-
import { deepStrictEqual, rejects, throws } from 'assert';
4-
import tap from 'tap';
1+
import '../common/index.mjs'
2+
import { Readable } from '../../lib/ours/index.js'
3+
import { deepStrictEqual, rejects, throws } from 'assert'
4+
import tap from 'tap'
55

66
{
77
// asIndexedPairs with a synchronous stream
8-
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray();
9-
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
10-
const empty = await Readable.from([]).asIndexedPairs().toArray();
11-
deepStrictEqual(empty, []);
8+
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray()
9+
deepStrictEqual(pairs, [
10+
[0, 1],
11+
[1, 2],
12+
[2, 3]
13+
])
14+
const empty = await Readable.from([]).asIndexedPairs().toArray()
15+
deepStrictEqual(empty, [])
1216
}
1317

1418
{
1519
// asIndexedPairs works an asynchronous streams
16-
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x);
17-
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray();
18-
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
19-
const empty = await asyncFrom([]).asIndexedPairs().toArray();
20-
deepStrictEqual(empty, []);
20+
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x)
21+
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray()
22+
deepStrictEqual(pairs, [
23+
[0, 1],
24+
[1, 2],
25+
[2, 3]
26+
])
27+
const empty = await asyncFrom([]).asIndexedPairs().toArray()
28+
deepStrictEqual(empty, [])
2129
}
2230

2331
{
2432
// Does not enumerate an infinite stream
25-
const infinite = () => Readable.from(async function* () {
26-
while (true) yield 1;
27-
}());
28-
const pairs = await infinite().asIndexedPairs().take(3).toArray();
29-
deepStrictEqual(pairs, [[0, 1], [1, 1], [2, 1]]);
30-
const empty = await infinite().asIndexedPairs().take(0).toArray();
31-
deepStrictEqual(empty, []);
33+
const infinite = () =>
34+
Readable.from(
35+
(async function* () {
36+
while (true) yield 1
37+
})()
38+
)
39+
const pairs = await infinite().asIndexedPairs().take(3).toArray()
40+
deepStrictEqual(pairs, [
41+
[0, 1],
42+
[1, 1],
43+
[2, 1]
44+
])
45+
const empty = await infinite().asIndexedPairs().take(0).toArray()
46+
deepStrictEqual(empty, [])
3247
}
3348

3449
{
3550
// AbortSignal
36-
await rejects(async () => {
37-
const ac = new AbortController();
38-
const { signal } = ac;
39-
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
40-
ac.abort();
41-
await p;
42-
}, { name: 'AbortError' });
51+
await rejects(
52+
async () => {
53+
const ac = new AbortController()
54+
const { signal } = ac
55+
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
56+
ac.abort()
57+
await p
58+
},
59+
{ name: 'AbortError' }
60+
)
4361

4462
await rejects(async () => {
45-
const signal = AbortSignal.abort();
46-
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
47-
}, /AbortError/);
63+
const signal = AbortSignal.abort()
64+
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
65+
}, /AbortError/)
4866
}
4967

5068
{
5169
// Error cases
52-
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/);
53-
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/);
70+
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/)
71+
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/)
5472
}
5573

56-
/* replacement start */
57-
process.on('beforeExit', (code) => {
58-
if(code === 0) {
59-
tap.pass('test succeeded');
60-
} else {
61-
tap.fail(`test failed - exited code ${code}`);
62-
}
63-
});
64-
/* replacement end */
74+
/* replacement start */
75+
process.on('beforeExit', (code) => {
76+
if (code === 0) {
77+
tap.pass('test succeeded')
78+
} else {
79+
tap.fail(`test failed - exited code ${code}`)
80+
}
81+
})
82+
/* replacement end */

0 commit comments

Comments
 (0)