Skip to content

Commit 28e4dac

Browse files
committed
Remove TLA, feature is DOA :(
Fix: #397 Fix: #398
1 parent 36204cc commit 28e4dac

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,31 @@ performance penalty. Creating data objects is not ever free; do
276276
not believe anyone who tells you otherwise. But it is as small as
277277
possible.
278278

279+
### Platform Compatibility Caveat
280+
281+
Not all platforms support the `node:diagnostics_channel` module.
282+
Currently, this is only available in Node, Bun, and Deno, and
283+
some edge computing platforms that provide a Node compatibility
284+
layer.
285+
286+
To work around this, if you are loading in a non-Node
287+
environment, the package.json exports will direct your module
288+
loader to pull in a version that starts out with a dummy
289+
implementation, then does a conditional dynamic `import` of the
290+
`node:diagnostics_channel` module, and then swaps out those
291+
dummy objects with the real thing if it succeeds. This means that
292+
cache metrics and tracing channels started in the first load-time
293+
tick of your application will _not_ be covered, except in
294+
environments that load using the `require` import
295+
condition, or both the `node` and `esm` import conditions
296+
together.
297+
298+
Top-level await _could_ be used to remove this caveat, but that
299+
feature is dead on arrival, unfortunately. See
300+
[#397](https://github.com/isaacs/node-lru-cache/issues/397) and
301+
[#398](https://github.com/isaacs/node-lru-cache/issues/398) for
302+
more details.
303+
279304
## Performance
280305

281306
As of April 2026, version 11 of this library is one of the most

src/diagnostics-channel-esm.mts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ export type { TracingChannel, Channel }
88

99
/**
1010
* no-op polyfills for non-node environments. tries to load the actual
11-
* diagnostics_channel module on platforms (bun, deno) that support it, but
12-
* fails gracefully if not found. This means that the first tick of metrics
11+
* diagnostics_channel module on platforms that support it, but fails
12+
* gracefully if not found. This means that the first tick of metrics
1313
* and tracing will be missed, but that probably doesn't matter much.
1414
*/
1515

1616
// conditionally import from diagnostic_channel, fall back to dummyfill
1717
// all we actually have to mock is the hasSubscribers, since we alwasy check
1818
/* v8 ignore next */
1919
const dummy = { hasSubscribers: false }
20-
type LRUCacheDC = [Channel<unknown>, TracingChannel<unknown>]
21-
export const [metrics, tracing]: LRUCacheDC =
22-
await import('node:diagnostics_channel')
23-
.then(
24-
dc =>
25-
[
26-
dc.channel('lru-cache:metrics'),
27-
dc.tracingChannel('lru-cache'),
28-
] as LRUCacheDC,
29-
)
30-
.catch(() => [dummy, dummy] as unknown as LRUCacheDC)
20+
export let metrics = dummy as Channel<unknown>
21+
export let tracing = dummy as TracingChannel<unknown>
22+
import('node:diagnostics_channel')
23+
.then(dc => {
24+
metrics = dc.channel('lru-cache:metrics')
25+
tracing = dc.tracingChannel('lru-cache')
26+
})
27+
.catch(() => {})

test/dummy-diagnostics-channel.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ t.test('diagnostics channel loads polyfills then fills in', async t => {
1717
end: () => {},
1818
})
1919

20-
// verify that the dummies were not used
20+
// verify that the dummies were only used until being loaded
21+
t.equal(dc.metrics.hasSubscribers, false)
22+
t.equal(dc.tracing.hasSubscribers, false)
23+
t.equal(actualDC.channel('lru-cache:metrics').hasSubscribers, true)
24+
t.equal(tc.hasSubscribers, true)
25+
await new Promise<void>(res => setTimeout(res))
2126
t.equal(dc.metrics.hasSubscribers, true)
2227
t.equal(dc.tracing.hasSubscribers, true)
2328
t.equal(actualDC.channel('lru-cache:metrics').hasSubscribers, true)
@@ -39,4 +44,8 @@ t.test('dummy dc that just says no subs', async t => {
3944
// these still have subs from previous test though
4045
t.equal(actualDC.channel('lru-cache:metrics').hasSubscribers, true)
4146
t.equal(actualDC.tracingChannel('lru-cache').hasSubscribers, true)
47+
await new Promise<void>(res => setTimeout(res))
48+
// still dummies
49+
t.equal(dc.metrics.hasSubscribers, false)
50+
t.equal(dc.tracing.hasSubscribers, false)
4251
})

0 commit comments

Comments
 (0)