Skip to content

Commit 437fb0c

Browse files
committed
fix(cloudflare): Import types explicitly from worker-types
1 parent b94f652 commit 437fb0c

File tree

9 files changed

+51
-16
lines changed

9 files changed

+51
-16
lines changed

dev-packages/e2e-tests/test-applications/cloudflare-hono/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Hono } from 'hono';
2+
import { DurableObject } from 'cloudflare:workers';
23
import * as Sentry from '@sentry/cloudflare';
34

45
const app = new Hono();
@@ -25,6 +26,19 @@ app.notFound(ctx => {
2526
return ctx.json({ message: 'Not Found' }, 404);
2627
});
2728

29+
class MyDurableObjectBase extends DurableObject<Env> {
30+
// impl
31+
}
32+
33+
// Typecheck that the instrumented durable object is valid
34+
export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry(
35+
(env: Env) => ({
36+
dsn: env?.E2E_TEST_DSN,
37+
tracesSampleRate: 1.0,
38+
}),
39+
MyDurableObjectBase,
40+
);
41+
2842
export default Sentry.withSentry(
2943
(env: Env) => ({
3044
dsn: env?.E2E_TEST_DSN,

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
}
6161
},
6262
"devDependencies": {
63-
"@cloudflare/workers-types": "4.20240725.0",
63+
"@cloudflare/workers-types": "^4.20250618.0",
6464
"@types/node": "^18.19.1",
6565
"wrangler": "^3.67.1"
6666
},

packages/cloudflare/src/durableobject.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
2+
import type { DurableObject, DurableObjectState, ExecutionContext, Rpc } from '@cloudflare/workers-types';
23
import {
34
captureException,
45
flush,
@@ -9,7 +10,6 @@ import {
910
withIsolationScope,
1011
withScope,
1112
} from '@sentry/core';
12-
import type { DurableObject } from 'cloudflare:workers';
1313
import { setAsyncLocalStorageAsyncContextStrategy } from './async';
1414
import type { CloudflareOptions } from './client';
1515
import { isInstrumented, markAsInstrumented } from './instrument';
@@ -135,8 +135,8 @@ function wrapMethodWithSentry<T extends (...args: any[]) => any>(
135135
*/
136136
export function instrumentDurableObjectWithSentry<
137137
E,
138-
T extends DurableObject<E>,
139-
C extends new (state: DurableObjectState, env: E) => T,
138+
T extends DurableObject & Rpc.DurableObjectBranded,
139+
C extends new (ctx: DurableObjectState, env: E) => T,
140140
>(optionsCallback: (env: E) => CloudflareOptions, DurableObjectClass: C): C {
141141
return new Proxy(DurableObjectClass, {
142142
construct(target, [context, env]) {

packages/cloudflare/src/handler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import type {
2+
EmailExportedHandler,
3+
ExportedHandler,
4+
ExportedHandlerFetchHandler,
5+
ExportedHandlerQueueHandler,
6+
ExportedHandlerScheduledHandler,
7+
ExportedHandlerTailHandler,
8+
} from '@cloudflare/workers-types';
19
import {
210
captureException,
311
flush,

packages/cloudflare/src/pages-plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EventPluginContext, PagesPluginFunction } from '@cloudflare/workers-types';
12
import { setAsyncLocalStorageAsyncContextStrategy } from './async';
23
import type { CloudflareOptions } from './client';
34
import { wrapRequestHandler } from './request';

packages/cloudflare/src/request.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { ExecutionContext, IncomingRequestCfProperties } from '@cloudflare/workers-types';
1+
import type {
2+
EventPluginContext,
3+
ExecutionContext,
4+
IncomingRequestCfProperties,
5+
Request,
6+
Response,
7+
} from '@cloudflare/workers-types';
28
import {
39
captureException,
410
continueTrace,
@@ -14,10 +20,18 @@ import type { CloudflareOptions } from './client';
1420
import { addCloudResourceContext, addCultureContext, addRequest } from './scope-utils';
1521
import { init } from './sdk';
1622

17-
interface RequestHandlerWrapperOptions {
23+
interface RequestHandlerWrapperOptions<
24+
Env = unknown,
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
Params extends string = any,
27+
Data extends Record<string, unknown> = Record<string, unknown>,
28+
// Although it is not ideal to use `any` here, it makes usage more flexible for different setups.
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
PluginParams = any,
31+
> {
1832
options: CloudflareOptions;
1933
request: Request<unknown, IncomingRequestCfProperties<unknown>>;
20-
context: ExecutionContext;
34+
context: ExecutionContext | EventPluginContext<Env, Params, Data, PluginParams>;
2135
}
2236

2337
/**
@@ -33,7 +47,7 @@ export function wrapRequestHandler(
3347
// In certain situations, the passed context can become undefined.
3448
// For example, for Astro while prerendering pages at build time.
3549
// see: https://github.com/getsentry/sentry-javascript/issues/13217
36-
const context = wrapperOptions.context as ExecutionContext | undefined;
50+
const context = wrapperOptions.context as RequestHandlerWrapperOptions['context'] | undefined;
3751

3852
const client = init(options);
3953
isolationScope.setClient(client);

packages/cloudflare/src/scope-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IncomingRequestCfProperties } from '@cloudflare/workers-types';
1+
import type { IncomingRequestCfProperties, Request } from '@cloudflare/workers-types';
22
import type { Scope } from '@sentry/core';
33
import { winterCGRequestToRequestData } from '@sentry/core';
44

packages/cloudflare/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"include": ["src/**/*"],
55

66
"compilerOptions": {
7-
"module": "esnext",
8-
"types": ["node", "@cloudflare/workers-types"]
7+
"module": "esnext"
98
}
109
}

yarn.lock

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,10 +2671,10 @@
26712671
resolved "https://registry.yarnpkg.com/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20240718.0.tgz#940893e62df7f5a8ec895572b834c95c1e256fbd"
26722672
integrity sha512-YpCRvvT47XanFum7C3SedOZKK6BfVhqmwdAAVAQFyc4gsCdegZo0JkUkdloC/jwuWlbCACOG2HTADHOqyeolzQ==
26732673

2674-
"@cloudflare/workers-types@4.20240725.0":
2675-
version "4.20240725.0"
2676-
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-4.20240725.0.tgz#e151e0c069c0070b4d7168c7b0d4ae6c50d3f237"
2677-
integrity sha512-L6T/Bg50zm9IIACQVQ0CdVcQL+2nLkRXdPz6BsXF3SlzgjyWR5ndVctAbfr/HLV7aKYxWnnEZsIORsTWb+FssA==
2674+
"@cloudflare/workers-types@^4.20250618.0":
2675+
version "4.20250618.0"
2676+
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-4.20250618.0.tgz#58b794beab67c13a38fee987e7c7de078a519ab5"
2677+
integrity sha512-5c26+nZEXDRrlzsIaVt3npuElwLi6bhLZHyDfMX5JmcM9sogeBMyyXrU3gJG0DII3QenE7DuyMC8uFzJHTcnDA==
26782678

26792679
"@cnakazawa/watch@^1.0.3":
26802680
version "1.0.4"
@@ -27148,7 +27148,6 @@ stylus@0.59.0, stylus@^0.59.0:
2714827148

2714927149
sucrase@^3.27.0, sucrase@^3.35.0, sucrase@getsentry/sucrase#es2020-polyfills:
2715027150
version "3.36.0"
27151-
uid fd682f6129e507c00bb4e6319cc5d6b767e36061
2715227151
resolved "https://codeload.github.com/getsentry/sucrase/tar.gz/fd682f6129e507c00bb4e6319cc5d6b767e36061"
2715327152
dependencies:
2715427153
"@jridgewell/gen-mapping" "^0.3.2"

0 commit comments

Comments
 (0)