@belvador/cloudflare 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,14 @@ each attempt times out after 10 s, failed sends retry at most 5 times, `429` rat
44
44
  respected, and a revoked key (`401`/`403`) stops sending for the life of the page or isolate.
45
45
  Every public function catches its own errors.
46
46
 
47
+ ## Check the connection
48
+
49
+ "No errors" is not proof that errors arrive. Call `verifyIntegration()` from `@belvador/cloudflare` inside a request handled by `withBelvador`:
50
+ it sends one marked test error (never an issue), reads it back and resolves to
51
+ `{ outcome: "verified" | "problem" | "unconfirmed", checks, docsUrl, … }`. It never throws.
52
+ Requests the visitor cancelled (`AbortError`) are not reported unless you set `captureAborted: true`.
53
+ See https://belvador.dev/docs/sdk/verify
54
+
47
55
  ## Status
48
56
 
49
57
  `0.x` — used by Belvador's own apps first. The API may still change before `1.0`.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { CloudflareOptions as EngineCloudflareOptions } from "@sentry/cloudflare";
2
- import { type BelvadorOptions } from "@belvador/core";
3
- export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, lastEventId, setContext, setTag, setTags, setUser, withScope, type BelvadorBreadcrumb, type BelvadorEvent, type BelvadorEventHint, type Scope, type SeverityLevel, } from "@belvador/core";
2
+ import { type BelvadorOptions, type VerificationResult, type VerifyOptions } from "@belvador/core";
3
+ export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, getDiagnostics, lastEventId, setContext, setTag, setTags, setUser, withScope, type BelvadorBreadcrumb, type BelvadorEvent, type BelvadorEventHint, type Diagnostic, type DiagnosticCode, type DiagnosticsSnapshot, type Scope, type SeverityLevel, type VerificationResult, type VerifyOptions, } from "@belvador/core";
4
4
  export type WorkerOptions = Omit<BelvadorOptions, "dsn"> & {
5
5
  /** Defaults to the BELVADOR_DSN binding. */
6
6
  dsn?: string;
@@ -27,3 +27,9 @@ export declare function wrapHandler<Env, H>(optionsCallback: (env: Env) => Worke
27
27
  * untouched, sending finished in waitUntil after the response.
28
28
  */
29
29
  export declare function withBelvador<Env, H>(optionsCallback: (env: Env) => WorkerOptions | undefined, handler: H): H;
30
+ /**
31
+ * Proves this Worker reports to Belvador. Call it inside a request handled by
32
+ * withBelvador (for example a temporary `/verify-belvador` path): it checks the wrapper,
33
+ * sends one verification error and reads it back. Never automatic; always resolves.
34
+ */
35
+ export declare function verifyIntegration(options?: VerifyOptions): Promise<VerificationResult>;
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@
3
3
  // privacy and sending rules. Needs the `nodejs_compat` (or `nodejs_als`)
4
4
  // compatibility flag so every request gets its own scope.
5
5
  import { consoleIntegration, dedupeIntegration, eventFiltersIntegration, fetchIntegration, linkedErrorsIntegration, requestDataIntegration, withSentry, } from "@sentry/cloudflare";
6
- import { buildEngineOptions } from "@belvador/core";
7
- export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, lastEventId, setContext, setTag, setTags, setUser, withScope, } from "@belvador/core";
6
+ import { buildEngineOptions, currentHooks, markHooked, noteClientAbort, runVerification, } from "@belvador/core";
7
+ export { SDK_VERSION, addBreadcrumb, captureException, captureMessage, close, flush, getDiagnostics, lastEventId, setContext, setTag, setTags, setUser, withScope, } from "@belvador/core";
8
8
  function binding(env, name) {
9
9
  if (env === null || typeof env !== "object")
10
10
  return undefined;
@@ -89,6 +89,55 @@ export function workerEngineOptions(options, env, runtime = "cloudflare") {
89
89
  return OFF;
90
90
  }
91
91
  }
92
+ const HANDLER_METHODS = ["fetch", "scheduled", "queue", "email", "tail"];
93
+ /**
94
+ * Marks each invocation of an object handler as running inside the wrapper (integration
95
+ * health: `verifyIntegration` can tell), and notes a fetch that failed after its client
96
+ * disconnected so the error counts as a cancellation. Class handlers pass through unchanged.
97
+ */
98
+ function marked(handler) {
99
+ if (handler === null || typeof handler !== "object")
100
+ return handler;
101
+ const out = { ...handler };
102
+ for (const name of HANDLER_METHODS) {
103
+ const fn = handler[name];
104
+ if (typeof fn !== "function")
105
+ continue;
106
+ out[name] = function (...args) {
107
+ markHooked("wrapper");
108
+ const signal = name === "fetch" ? signalOf(args[0]) : undefined;
109
+ if (!signal)
110
+ return fn.apply(handler, args);
111
+ const failed = (error) => {
112
+ noteClientAbort(error, signal);
113
+ throw error;
114
+ };
115
+ let result;
116
+ try {
117
+ result = fn.apply(handler, args);
118
+ }
119
+ catch (error) {
120
+ return failed(error);
121
+ }
122
+ return isThenable(result) ? Promise.resolve(result).then(undefined, failed) : result;
123
+ };
124
+ }
125
+ return out;
126
+ }
127
+ function signalOf(request) {
128
+ try {
129
+ const signal = request?.signal;
130
+ return typeof AbortSignal !== "undefined" && signal instanceof AbortSignal ? signal : undefined;
131
+ }
132
+ catch {
133
+ return undefined;
134
+ }
135
+ }
136
+ function isThenable(value) {
137
+ return (value !== null &&
138
+ (typeof value === "object" || typeof value === "function") &&
139
+ typeof value.then === "function");
140
+ }
92
141
  /** Shared with @belvador/tanstack-start so its server events carry its own SDK name. */
93
142
  export function wrapHandler(optionsCallback, handler, runtime) {
94
143
  return withSentry((env) => {
@@ -100,7 +149,7 @@ export function wrapHandler(optionsCallback, handler, runtime) {
100
149
  return OFF;
101
150
  }
102
151
  return workerEngineOptions(options, env, runtime);
103
- }, handler);
152
+ }, marked(handler));
104
153
  }
105
154
  /**
106
155
  * Wraps a Worker's exported handler (fetch, scheduled, queue, email, tail):
@@ -110,3 +159,17 @@ export function wrapHandler(optionsCallback, handler, runtime) {
110
159
  export function withBelvador(optionsCallback, handler) {
111
160
  return wrapHandler(optionsCallback, handler, "cloudflare");
112
161
  }
162
+ /**
163
+ * Proves this Worker reports to Belvador. Call it inside a request handled by
164
+ * withBelvador (for example a temporary `/verify-belvador` path): it checks the wrapper,
165
+ * sends one verification error and reads it back. Never automatic; always resolves.
166
+ */
167
+ export function verifyIntegration(options) {
168
+ return runVerification({
169
+ surface: "server",
170
+ hook: () => {
171
+ const hooks = currentHooks();
172
+ return { ok: hooks.has("wrapper") || hooks.has("middleware") };
173
+ },
174
+ }, options);
175
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belvador/cloudflare",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Belvador error tracking for Cloudflare Workers: per-request scope, errors only, private by default.",
5
5
  "license": "MIT",
6
6
  "author": "Belvador (https://belvador.dev)",
@@ -32,7 +32,7 @@
32
32
  "node": ">=18"
33
33
  },
34
34
  "dependencies": {
35
- "@belvador/core": "0.1.0",
35
+ "@belvador/core": "0.2.0",
36
36
  "@sentry/cloudflare": "10.76.2"
37
37
  },
38
38
  "publishConfig": {