Skip to content

Commit

Permalink
only print the 'no abort controller' warning if actually used
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 14, 2023
1 parent dfd7f2f commit ca76768
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
38 changes: 22 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,7 @@ let AC = globalThis.AbortController
let AS = globalThis.AbortSignal

/* c8 ignore start */
if (
typeof AC === 'undefined' &&
PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1'
) {
emitWarning(
'AbortController is not defined. If using lru-cache in ' +
'node 14, load an AbortController polyfill from the ' +
'`node-abort-controller` package. A minimal polyfill is ' +
'provided for use by LRUCache.fetch(), but it should not be ' +
'relied upon in other contexts (eg, passing it to other APIs that ' +
'use AbortController/AbortSignal might have undesirable effects). ' +
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.',
'NO_ABORT_CONTROLLER',
'ENOTSUP',
() => {}
)
if (typeof AC === 'undefined') {
//@ts-ignore
AS = class AbortSignal {
onabort?: (...a: any[]) => any
Expand All @@ -65,6 +50,9 @@ if (
}
//@ts-ignore
AC = class AbortController {
constructor() {
warnACPolyfill()
}
signal = new AS()
abort(reason: any) {
if (this.signal.aborted) return
Expand All @@ -79,6 +67,24 @@ if (
this.signal.onabort?.(reason)
}
}
let printACPolyfillWarning =
PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1'
const warnACPolyfill = () => {
if (!printACPolyfillWarning) return
printACPolyfillWarning = false
emitWarning(
'AbortController is not defined. If using lru-cache in ' +
'node 14, load an AbortController polyfill from the ' +
'`node-abort-controller` package. A minimal polyfill is ' +
'provided for use by LRUCache.fetch(), but it should not be ' +
'relied upon in other contexts (eg, passing it to other APIs that ' +
'use AbortController/AbortSignal might have undesirable effects). ' +
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.',
'NO_ABORT_CONTROLLER',
'ENOTSUP',
warnACPolyfill
)
}
}
/* c8 ignore stop */

Expand Down
29 changes: 27 additions & 2 deletions test/warn-missing-ac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const main = async () => {
const { spawn } = await import('child_process')

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

const tsNode = process.platform === 'win32' ? 'ts-node.cmd' : 'ts-node'
const tsNode =
process.platform === 'win32' ? 'ts-node.cmd' : 'ts-node'

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

const noFetch = spawn(tsNode, [__filename, 'child-no-fetch'])
const noFetchErr: Buffer[] = []
noFetch.stderr.on('data', c => noFetchErr.push(c))

t.test('no warning', async t => {
await new Promise<void>(r =>
noWarn.on('close', (code, signal) => {
Expand All @@ -32,6 +37,17 @@ const main = async () => {
t.equal(Buffer.concat(noWarnErr).toString().trim(), '')
})

t.test('no warning (because no fetch)', async t => {
await new Promise<void>(r =>
noFetch.on('close', (code, signal) => {
t.equal(code, 0)
t.equal(signal, null)
r()
})
)
t.equal(Buffer.concat(noFetchErr).toString().trim(), '')
})

t.test('warning', async t => {
await new Promise<void>(r =>
warn.on('close', (code, signal) => {
Expand All @@ -46,6 +62,15 @@ const main = async () => {

switch (process.argv[2]) {
case 'child':
//@ts-ignore
globalThis.AbortController = undefined
//@ts-ignore
globalThis.AbortSignal = undefined
import('../').then(({ LRUCache }) => {
new LRUCache({ max: 1, fetchMethod: async () => 1 }).fetch(1)
})
break
case 'child-no-fetch':
//@ts-ignore
globalThis.AbortController = undefined
//@ts-ignore
Expand Down

0 comments on commit ca76768

Please sign in to comment.