Skip to content

Commit 265a630

Browse files
committed
Avoid importing types from node inbuilts on vercel-edge, vendor them in
1 parent 1bad3e1 commit 265a630

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

packages/sveltekit/src/client/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefi
6464
* @returns the function that was previously on `window.fetch`.
6565
*/
6666
function switchToFetchProxy(): typeof fetch | undefined {
67-
const globalWithSentryFetchProxy: WindowWithSentryFetchProxy = WINDOW as WindowWithSentryFetchProxy;
67+
const globalWithSentryFetchProxy: WindowWithSentryFetchProxy = WINDOW;
6868

6969
// eslint-disable-next-line @typescript-eslint/unbound-method
7070
const actualFetch = globalWithSentryFetchProxy.fetch;
@@ -81,7 +81,7 @@ function switchToFetchProxy(): typeof fetch | undefined {
8181
* and puts our fetch proxy back onto `window._sentryFetchProxy`.
8282
*/
8383
function restoreFetch(actualFetch: typeof fetch): void {
84-
const globalWithSentryFetchProxy: WindowWithSentryFetchProxy = WINDOW as WindowWithSentryFetchProxy;
84+
const globalWithSentryFetchProxy: WindowWithSentryFetchProxy = WINDOW;
8585

8686
// eslint-disable-next-line @typescript-eslint/unbound-method
8787
globalWithSentryFetchProxy._sentryFetchProxy = globalWithSentryFetchProxy.fetch;

packages/vercel-edge/src/vendored/abstract-async-hooks-context-manager.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@
3232
/* eslint-disable @typescript-eslint/no-this-alias */
3333

3434
import type { Context, ContextManager } from '@opentelemetry/api';
35-
import type { EventEmitter } from 'events';
3635

3736
type Func<T> = (...args: unknown[]) => T;
3837

38+
// Inline EventEmitter interface to avoid Node.js module dependency
39+
// This prevents Node.js type leaks in edge runtime environments
40+
interface EventEmitter {
41+
addListener?(event: string, listener: Func<void>): this;
42+
on?(event: string, listener: Func<void>): this;
43+
once?(event: string, listener: Func<void>): this;
44+
prependListener?(event: string, listener: Func<void>): this;
45+
prependOnceListener?(event: string, listener: Func<void>): this;
46+
removeListener?(event: string, listener: Func<void>): this;
47+
off?(event: string, listener: Func<void>): this;
48+
removeAllListeners?(event?: string): this;
49+
}
50+
3951
/**
4052
* Store a map for each event of all original listeners and their "patched"
4153
* version. So when a listener is removed by the user, the corresponding

packages/vercel-edge/src/vendored/async-local-storage-context-manager.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@
2828
import type { Context } from '@opentelemetry/api';
2929
import { ROOT_CONTEXT } from '@opentelemetry/api';
3030
import { debug, GLOBAL_OBJ } from '@sentry/core';
31-
import type { AsyncLocalStorage } from 'async_hooks';
3231
import { DEBUG_BUILD } from '../debug-build';
3332
import { AbstractAsyncHooksContextManager } from './abstract-async-hooks-context-manager';
3433

34+
// Inline AsyncLocalStorage interface to avoid Node.js module dependency
35+
// This prevents Node.js type leaks in edge runtime environments
36+
interface AsyncLocalStorage<T> {
37+
getStore(): T | undefined;
38+
run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R;
39+
disable(): void;
40+
}
41+
3542
export class AsyncLocalStorageContextManager extends AbstractAsyncHooksContextManager {
3643
private _asyncLocalStorage: AsyncLocalStorage<Context>;
3744

@@ -46,12 +53,11 @@ export class AsyncLocalStorageContextManager extends AbstractAsyncHooksContextMa
4653
"Tried to register AsyncLocalStorage async context strategy in a runtime that doesn't support AsyncLocalStorage.",
4754
);
4855

49-
// @ts-expect-error Vendored type shenanigans
5056
this._asyncLocalStorage = {
5157
getStore() {
5258
return undefined;
5359
},
54-
run(_store: unknown, callback: () => Context, ...args: unknown[]) {
60+
run<R>(_store: Context, callback: (...args: any[]) => R, ...args: any[]): R {
5561
return callback.apply(this, args);
5662
},
5763
disable() {

0 commit comments

Comments
 (0)