Skip to content

Commit ca76768

Browse files
committed
only print the 'no abort controller' warning if actually used
1 parent dfd7f2f commit ca76768

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/index.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,7 @@ let AC = globalThis.AbortController
3737
let AS = globalThis.AbortSignal
3838

3939
/* c8 ignore start */
40-
if (
41-
typeof AC === 'undefined' &&
42-
PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1'
43-
) {
44-
emitWarning(
45-
'AbortController is not defined. If using lru-cache in ' +
46-
'node 14, load an AbortController polyfill from the ' +
47-
'`node-abort-controller` package. A minimal polyfill is ' +
48-
'provided for use by LRUCache.fetch(), but it should not be ' +
49-
'relied upon in other contexts (eg, passing it to other APIs that ' +
50-
'use AbortController/AbortSignal might have undesirable effects). ' +
51-
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.',
52-
'NO_ABORT_CONTROLLER',
53-
'ENOTSUP',
54-
() => {}
55-
)
40+
if (typeof AC === 'undefined') {
5641
//@ts-ignore
5742
AS = class AbortSignal {
5843
onabort?: (...a: any[]) => any
@@ -65,6 +50,9 @@ if (
6550
}
6651
//@ts-ignore
6752
AC = class AbortController {
53+
constructor() {
54+
warnACPolyfill()
55+
}
6856
signal = new AS()
6957
abort(reason: any) {
7058
if (this.signal.aborted) return
@@ -79,6 +67,24 @@ if (
7967
this.signal.onabort?.(reason)
8068
}
8169
}
70+
let printACPolyfillWarning =
71+
PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1'
72+
const warnACPolyfill = () => {
73+
if (!printACPolyfillWarning) return
74+
printACPolyfillWarning = false
75+
emitWarning(
76+
'AbortController is not defined. If using lru-cache in ' +
77+
'node 14, load an AbortController polyfill from the ' +
78+
'`node-abort-controller` package. A minimal polyfill is ' +
79+
'provided for use by LRUCache.fetch(), but it should not be ' +
80+
'relied upon in other contexts (eg, passing it to other APIs that ' +
81+
'use AbortController/AbortSignal might have undesirable effects). ' +
82+
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.',
83+
'NO_ABORT_CONTROLLER',
84+
'ENOTSUP',
85+
warnACPolyfill
86+
)
87+
}
8288
}
8389
/* c8 ignore stop */
8490

test/warn-missing-ac.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const main = async () => {
44
const { spawn } = await import('child_process')
55

66
// need to run both tests in parallel so we don't miss the close event
7-
t.jobs = 2
7+
t.jobs = 3
88

9-
const tsNode = process.platform === 'win32' ? 'ts-node.cmd' : 'ts-node'
9+
const tsNode =
10+
process.platform === 'win32' ? 'ts-node.cmd' : 'ts-node'
1011

1112
const warn = spawn(tsNode, [__filename, 'child'])
1213
const warnErr: Buffer[] = []
@@ -21,6 +22,10 @@ const main = async () => {
2122
const noWarnErr: Buffer[] = []
2223
noWarn.stderr.on('data', c => noWarnErr.push(c))
2324

25+
const noFetch = spawn(tsNode, [__filename, 'child-no-fetch'])
26+
const noFetchErr: Buffer[] = []
27+
noFetch.stderr.on('data', c => noFetchErr.push(c))
28+
2429
t.test('no warning', async t => {
2530
await new Promise<void>(r =>
2631
noWarn.on('close', (code, signal) => {
@@ -32,6 +37,17 @@ const main = async () => {
3237
t.equal(Buffer.concat(noWarnErr).toString().trim(), '')
3338
})
3439

40+
t.test('no warning (because no fetch)', async t => {
41+
await new Promise<void>(r =>
42+
noFetch.on('close', (code, signal) => {
43+
t.equal(code, 0)
44+
t.equal(signal, null)
45+
r()
46+
})
47+
)
48+
t.equal(Buffer.concat(noFetchErr).toString().trim(), '')
49+
})
50+
3551
t.test('warning', async t => {
3652
await new Promise<void>(r =>
3753
warn.on('close', (code, signal) => {
@@ -46,6 +62,15 @@ const main = async () => {
4662

4763
switch (process.argv[2]) {
4864
case 'child':
65+
//@ts-ignore
66+
globalThis.AbortController = undefined
67+
//@ts-ignore
68+
globalThis.AbortSignal = undefined
69+
import('../').then(({ LRUCache }) => {
70+
new LRUCache({ max: 1, fetchMethod: async () => 1 }).fetch(1)
71+
})
72+
break
73+
case 'child-no-fetch':
4974
//@ts-ignore
5075
globalThis.AbortController = undefined
5176
//@ts-ignore

0 commit comments

Comments
 (0)