Repository navigation
Expand file tree
/
Copy pathhooks.ts
More file actions
48 lines (38 loc) · 1.71 KB
/
Copy pathhooks.ts
File metadata and controls
48 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { Carrier, Hub, RunWithAsyncContextOptions } from '@sentry/core';
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy } from '@sentry/core';
import * as async_hooks from 'async_hooks';
interface AsyncLocalStorage<T> {
getStore(): T | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
run<R, TArgs extends any[]>(store: T, callback: (...args: TArgs) => R, ...args: TArgs): R;
}
type AsyncLocalStorageConstructor = { new <T>(): AsyncLocalStorage<T> };
// AsyncLocalStorage only exists in async_hook after Node v12.17.0 or v13.10.0
type NewerAsyncHooks = typeof async_hooks & { AsyncLocalStorage: AsyncLocalStorageConstructor };
/**
* Sets the async context strategy to use AsyncLocalStorage which requires Node v12.17.0 or v13.10.0.
*/
export function setHooksAsyncContextStrategy(): void {
const asyncStorage = new (async_hooks as NewerAsyncHooks).AsyncLocalStorage<Hub>();
function getCurrentHub(): Hub | undefined {
return asyncStorage.getStore();
}
function createNewHub(parent: Hub | undefined): Hub {
const carrier: Carrier = {};
ensureHubOnCarrier(carrier, parent);
return getHubFromCarrier(carrier);
}
function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions): T {
const existingHub = getCurrentHub();
if (existingHub && options?.reuseExisting) {
// We're already in an async context, so we don't need to create a new one
// just call the callback with the current hub
return callback();
}
const newHub = createNewHub(existingHub);
return asyncStorage.run(newHub, () => {
return callback();
});
}
setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext });
}