Repository navigation
Expand file tree
/
Copy pathhooks.ts
More file actions
103 lines (84 loc) · 3.46 KB
/
Copy pathhooks.ts
File metadata and controls
103 lines (84 loc) · 3.46 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Hub as HubClass, getGlobalHub } from '@sentry/core';
import { setAsyncContextStrategy } from '@sentry/core';
import type { Hub, Scope } from '@sentry/types';
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 };
let asyncStorage: AsyncLocalStorage<Hub>;
/**
* Sets the async context strategy to use AsyncLocalStorage which requires Node v12.17.0 or v13.10.0.
*/
export function setHooksAsyncContextStrategy(): void {
if (!asyncStorage) {
asyncStorage = new (async_hooks as NewerAsyncHooks).AsyncLocalStorage<Hub>();
}
function getCurrentHooksHub(): Hub | undefined {
return asyncStorage.getStore();
}
function getCurrentHub(): Hub {
return getCurrentHooksHub() || getGlobalHub();
}
function withScope<T>(callback: (scope: Scope) => T): T {
const parentHub = getCurrentHub();
/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const isolationScope = parentHub.getIsolationScope();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */
return asyncStorage.run(newHub, () => {
return callback(scope);
});
}
function withSetScope<T>(scope: Scope, callback: (scope: Scope) => T): T {
const parentHub = getCurrentHub();
/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const isolationScope = parentHub.getIsolationScope();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */
return asyncStorage.run(newHub, () => {
return callback(scope);
});
}
function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T {
const parentHub = getCurrentHub();
/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const isolationScope = parentHub.getIsolationScope().clone();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */
return asyncStorage.run(newHub, () => {
return callback(isolationScope);
});
}
function withSetIsolationScope<T>(isolationScope: Scope, callback: (isolationScope: Scope) => T): T {
const parentHub = getCurrentHub();
/* eslint-disable deprecation/deprecation */
const client = parentHub.getClient();
const scope = parentHub.getScope().clone();
const newHub = new HubClass(client, scope, isolationScope);
/* eslint-enable deprecation/deprecation */
return asyncStorage.run(newHub, () => {
return callback(isolationScope);
});
}
setAsyncContextStrategy({
getCurrentHub,
withScope,
withSetScope,
withIsolationScope,
withSetIsolationScope,
// eslint-disable-next-line deprecation/deprecation
getCurrentScope: () => getCurrentHub().getScope(),
// eslint-disable-next-line deprecation/deprecation
getIsolationScope: () => getCurrentHub().getIsolationScope(),
});
}