|
| 1 | +import type { Client, Scope as ScopeInterface } from '@sentry/types'; |
| 2 | +import { isThenable } from '@sentry/utils'; |
| 3 | +import { getDefaultCurrentScope, getDefaultIsolationScope } from '../currentScopes'; |
| 4 | +import { Scope } from '../scope'; |
| 5 | + |
| 6 | +import { getMainCarrier, getSentryCarrier } from './../carrier'; |
| 7 | +import type { AsyncContextStrategy } from './types'; |
| 8 | + |
| 9 | +interface Layer { |
| 10 | + client?: Client; |
| 11 | + scope: ScopeInterface; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * This is an object that holds a stack of scopes. |
| 16 | + */ |
| 17 | +export class AsyncContextStack { |
| 18 | + private readonly _stack: Layer[]; |
| 19 | + private _isolationScope: ScopeInterface; |
| 20 | + |
| 21 | + public constructor(scope?: ScopeInterface, isolationScope?: ScopeInterface) { |
| 22 | + let assignedScope; |
| 23 | + if (!scope) { |
| 24 | + assignedScope = new Scope(); |
| 25 | + } else { |
| 26 | + assignedScope = scope; |
| 27 | + } |
| 28 | + |
| 29 | + let assignedIsolationScope; |
| 30 | + if (!isolationScope) { |
| 31 | + assignedIsolationScope = new Scope(); |
| 32 | + } else { |
| 33 | + assignedIsolationScope = isolationScope; |
| 34 | + } |
| 35 | + |
| 36 | + this._stack = [{ scope: assignedScope }]; |
| 37 | + this._isolationScope = assignedIsolationScope; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Fork a scope for the stack. |
| 42 | + */ |
| 43 | + public withScope<T>(callback: (scope: ScopeInterface) => T): T { |
| 44 | + const scope = this._pushScope(); |
| 45 | + |
| 46 | + let maybePromiseResult: T; |
| 47 | + try { |
| 48 | + maybePromiseResult = callback(scope); |
| 49 | + } catch (e) { |
| 50 | + this._popScope(); |
| 51 | + throw e; |
| 52 | + } |
| 53 | + |
| 54 | + if (isThenable(maybePromiseResult)) { |
| 55 | + // @ts-expect-error - isThenable returns the wrong type |
| 56 | + return maybePromiseResult.then( |
| 57 | + res => { |
| 58 | + this._popScope(); |
| 59 | + return res; |
| 60 | + }, |
| 61 | + e => { |
| 62 | + this._popScope(); |
| 63 | + throw e; |
| 64 | + }, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + this._popScope(); |
| 69 | + return maybePromiseResult; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the client of the stack. |
| 74 | + */ |
| 75 | + public getClient<C extends Client>(): C | undefined { |
| 76 | + return this.getStackTop().client as C; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the scope of the top stack. |
| 81 | + */ |
| 82 | + public getScope(): ScopeInterface { |
| 83 | + return this.getStackTop().scope; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the isolation scope for the stack. |
| 88 | + */ |
| 89 | + public getIsolationScope(): ScopeInterface { |
| 90 | + return this._isolationScope; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the scope stack for domains or the process. |
| 95 | + */ |
| 96 | + public getStack(): Layer[] { |
| 97 | + return this._stack; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the topmost scope layer in the order domain > local > process. |
| 102 | + */ |
| 103 | + public getStackTop(): Layer { |
| 104 | + return this._stack[this._stack.length - 1]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Push a scope to the stack. |
| 109 | + */ |
| 110 | + private _pushScope(): ScopeInterface { |
| 111 | + // We want to clone the content of prev scope |
| 112 | + const scope = this.getScope().clone(); |
| 113 | + this.getStack().push({ |
| 114 | + client: this.getClient(), |
| 115 | + scope, |
| 116 | + }); |
| 117 | + return scope; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Pop a scope from the stack. |
| 122 | + */ |
| 123 | + private _popScope(): boolean { |
| 124 | + if (this.getStack().length <= 1) return false; |
| 125 | + return !!this.getStack().pop(); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Get the global async context stack. |
| 131 | + * This will be removed during the v8 cycle and is only here to make migration easier. |
| 132 | + */ |
| 133 | +function getAsyncContextStack(): AsyncContextStack { |
| 134 | + const registry = getMainCarrier(); |
| 135 | + const sentry = getSentryCarrier(registry) as { hub?: AsyncContextStack }; |
| 136 | + |
| 137 | + // If there's no hub, or its an old API, assign a new one |
| 138 | + if (sentry.hub) { |
| 139 | + return sentry.hub; |
| 140 | + } |
| 141 | + |
| 142 | + sentry.hub = new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()); |
| 143 | + return sentry.hub; |
| 144 | +} |
| 145 | + |
| 146 | +function withScope<T>(callback: (scope: ScopeInterface) => T): T { |
| 147 | + return getAsyncContextStack().withScope(callback); |
| 148 | +} |
| 149 | + |
| 150 | +function withSetScope<T>(scope: ScopeInterface, callback: (scope: ScopeInterface) => T): T { |
| 151 | + const hub = getAsyncContextStack() as AsyncContextStack; |
| 152 | + return hub.withScope(() => { |
| 153 | + hub.getStackTop().scope = scope; |
| 154 | + return callback(scope); |
| 155 | + }); |
| 156 | +} |
| 157 | + |
| 158 | +function withIsolationScope<T>(callback: (isolationScope: ScopeInterface) => T): T { |
| 159 | + return getAsyncContextStack().withScope(() => { |
| 160 | + return callback(getAsyncContextStack().getIsolationScope()); |
| 161 | + }); |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Get the stack-based async context strategy. |
| 166 | + */ |
| 167 | +export function getStackAsyncContextStrategy(): AsyncContextStrategy { |
| 168 | + return { |
| 169 | + withIsolationScope, |
| 170 | + withScope, |
| 171 | + withSetScope, |
| 172 | + withSetIsolationScope: <T>(_isolationScope: ScopeInterface, callback: (isolationScope: ScopeInterface) => T) => { |
| 173 | + return withIsolationScope(callback); |
| 174 | + }, |
| 175 | + getCurrentScope: () => getAsyncContextStack().getScope(), |
| 176 | + getIsolationScope: () => getAsyncContextStack().getIsolationScope(), |
| 177 | + }; |
| 178 | +} |
0 commit comments