workers-start 0.9.0 → 0.10.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/CHANGELOG.md +5 -0
- package/README.md +76 -13
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/mail.d.ts +71 -0
- package/dist/mail.d.ts.map +1 -0
- package/dist/mail.js +157 -0
- package/dist/mail.js.map +1 -0
- package/dist/sentry.d.ts +96 -0
- package/dist/sentry.d.ts.map +1 -0
- package/dist/sentry.js +119 -0
- package/dist/sentry.js.map +1 -0
- package/package.json +15 -2
- package/src/index.ts +34 -0
- package/src/mail.ts +227 -0
- package/src/sentry.ts +178 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.10.0 (2026-11-20)
|
|
4
|
+
|
|
5
|
+
- `workers-start/sentry`: `workerSentryOptions(env, options)` and `browserSentryOptions(options)` build Sentry options without importing an SDK. No usable DSN turns Sentry off, events from local servers are dropped, and the browser options also drop network, extension, wallet and user-rejection noise (`isBrowserNoise`, `BROWSER_DENY_URLS`). Release and environment are left to `@sentry/cloudflare`'s own env reading unless given.
|
|
6
|
+
- `workers-start/mail`: `cloudflareMailer(binding)` and `resendMailer({ apiKey })` behind one `send(message)`, which refuses a message without both a text and an HTML body. `MailError` carries the status and whether a retry can help; `recordingMailer()` is the test fake; `escapeHtml` for bodies.
|
|
7
|
+
|
|
3
8
|
## 0.9.0 (2026-11-20)
|
|
4
9
|
|
|
5
10
|
- `workers-start/google`: Google APIs as a service account on WebCrypto. `googleAccessToken(account, scopes)` signs the JWT and caches the access token per account and scope (shared exchanges, failures not cached), `parseServiceAccount` reads the key file JSON, and `importServiceAccountKey` names a PKCS#1 or non-PEM key. `ga4RunReport`, `ga4BatchRunReports`, `ga4Rows` and `inListFilter` for the GA4 Data API, and `searchConsoleQuery` (with `all: true` paging) for Search Console. Errors throw `GoogleApiError` with Google's status and message.
|
package/README.md
CHANGED
|
@@ -8,19 +8,21 @@ It has no runtime dependencies. Each piece is its own entry point, so an app tak
|
|
|
8
8
|
|
|
9
9
|
Check this list before writing a helper in an app. Anything generic to TanStack Start on Workers that is missing belongs here, with a test, rather than in one app.
|
|
10
10
|
|
|
11
|
-
| Entry point | Exports | For
|
|
12
|
-
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
13
|
-
| `workers-start/entry` | `createEntry`, `withHeaders`, `wantsMarkdown`, `acceptsHtml` | The Worker entry: Markdown for agents, Accept fixes, 405s, headers on every response
|
|
14
|
-
| `workers-start/head` | `createPageHead`, `jsonLdScript` | Per-route head tags and escaped JSON-LD
|
|
15
|
-
| `workers-start/scripts` | `themeScript`, `analyticsScript`, `tagManagerScript`, `gaEvent`, `inlineJson` | Inline bootstraps, analytics events, JSON safe inside a script tag
|
|
16
|
-
| `workers-start/cache` | `withEdgeCache`, `sharedMaxAge`, `withoutBrowserMaxAge` | Rendered pages kept in the Workers Cache API
|
|
17
|
-
| `workers-start/workers` | `getBinding`, `getEnvString`, `afterResponse`, `isKvNamespace` | Bindings and `waitUntil` without a static `cloudflare:workers` import
|
|
18
|
-
| `workers-start/access` | `verifyAccessToken`, `createAccessVerifier`, `parseAccessConfig`, `accessToken` | Cloudflare Access sign-ins checked in the Worker, failing closed
|
|
19
|
-
| `workers-start/access/testing` | `createTestAccessTeam` | A fake Access team for tests: keys, certs and signed tokens
|
|
20
|
-
| `workers-start/google` | `googleAccessToken`, `parseServiceAccount`, `ga4RunReport`, `ga4BatchRunReports`, `ga4Rows`, `inListFilter`, `searchConsoleQuery` | Google APIs as a service account: cached tokens, GA4 reports, Search Console
|
|
21
|
-
| `workers-start/
|
|
22
|
-
| `workers-start/
|
|
23
|
-
| `workers-start/
|
|
11
|
+
| Entry point | Exports | For |
|
|
12
|
+
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
13
|
+
| `workers-start/entry` | `createEntry`, `withHeaders`, `wantsMarkdown`, `acceptsHtml` | The Worker entry: Markdown for agents, Accept fixes, 405s, headers on every response |
|
|
14
|
+
| `workers-start/head` | `createPageHead`, `jsonLdScript` | Per-route head tags and escaped JSON-LD |
|
|
15
|
+
| `workers-start/scripts` | `themeScript`, `analyticsScript`, `tagManagerScript`, `gaEvent`, `inlineJson` | Inline bootstraps, analytics events, JSON safe inside a script tag |
|
|
16
|
+
| `workers-start/cache` | `withEdgeCache`, `sharedMaxAge`, `withoutBrowserMaxAge` | Rendered pages kept in the Workers Cache API |
|
|
17
|
+
| `workers-start/workers` | `getBinding`, `getEnvString`, `afterResponse`, `isKvNamespace` | Bindings and `waitUntil` without a static `cloudflare:workers` import |
|
|
18
|
+
| `workers-start/access` | `verifyAccessToken`, `createAccessVerifier`, `parseAccessConfig`, `accessToken` | Cloudflare Access sign-ins checked in the Worker, failing closed |
|
|
19
|
+
| `workers-start/access/testing` | `createTestAccessTeam` | A fake Access team for tests: keys, certs and signed tokens |
|
|
20
|
+
| `workers-start/google` | `googleAccessToken`, `parseServiceAccount`, `ga4RunReport`, `ga4BatchRunReports`, `ga4Rows`, `inListFilter`, `searchConsoleQuery` | Google APIs as a service account: cached tokens, GA4 reports, Search Console |
|
|
21
|
+
| `workers-start/sentry` | `workerSentryOptions`, `browserSentryOptions`, `dropLocalEvents`, `isBrowserNoise`, `BROWSER_DENY_URLS` | Sentry options for a Worker and its pages: off without a DSN, no local events, no browser noise |
|
|
22
|
+
| `workers-start/mail` | `cloudflareMailer`, `resendMailer`, `recordingMailer`, `MailError`, `escapeHtml` | One `send` over Email Sending or Resend, text and HTML always, and a fake for tests |
|
|
23
|
+
| `workers-start/seo` | `sitemapXml`, `robotsTxt`, `escapeXml` | `/sitemap.xml` and `/robots.txt` bodies |
|
|
24
|
+
| `workers-start/accept` | `parseAccept`, `quality` | Accept header parsing |
|
|
25
|
+
| `workers-start/check-bundle` | `workers-start-check-bundle` (bin) | Fails a build that would ship a local secret |
|
|
24
26
|
|
|
25
27
|
## Why
|
|
26
28
|
|
|
@@ -280,6 +282,67 @@ const queries = await searchConsoleQuery(token, "sc-domain:example.com", {
|
|
|
280
282
|
- `searchConsoleQuery` fills missing fields with zeros and treats a reply with no `rows` as no rows. `{ all: true }` pages with `startRow` until a short page.
|
|
281
283
|
- A Google error status throws `GoogleApiError` with `status`, Google's message and its JSON `body`.
|
|
282
284
|
|
|
285
|
+
## Sentry
|
|
286
|
+
|
|
287
|
+
`workers-start/sentry` builds the options objects for `@sentry/cloudflare` and a browser SDK without importing either, so it adds nothing to a bundle that skips Sentry.
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
import * as Sentry from "@sentry/cloudflare";
|
|
291
|
+
import { workerSentryOptions } from "workers-start/sentry";
|
|
292
|
+
|
|
293
|
+
export default Sentry.withSentry(
|
|
294
|
+
(env: Env) => workerSentryOptions(env, { tags: { worker: "api" } }),
|
|
295
|
+
handler,
|
|
296
|
+
);
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
```ts
|
|
300
|
+
import * as Sentry from "@sentry/react";
|
|
301
|
+
import { browserSentryOptions } from "workers-start/sentry";
|
|
302
|
+
|
|
303
|
+
Sentry.init({
|
|
304
|
+
...browserSentryOptions({
|
|
305
|
+
dsn: import.meta.env.VITE_SENTRY_DSN,
|
|
306
|
+
enabled: import.meta.env.PROD,
|
|
307
|
+
}),
|
|
308
|
+
integrations: [Sentry.browserTracingIntegration()],
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
- No DSN, or one that is not an `https://` URL (`off`, a placeholder), means `enabled: false` and no traffic.
|
|
313
|
+
- Both drop events whose request came from this machine (`localhost`, `127.0.0.1`, `[::1]`, `*.localhost`), so `wrangler dev` and `vite preview` never post into the production project. Events with no request, such as a cron run, are kept.
|
|
314
|
+
- The browser options also drop requests that never left the device ("Failed to fetch", "Load failed", Firefox's NetworkError), errors from extensions and injected wallets, and a person declining a wallet prompt, and ignore scripts from extension URLs and Google Translate's proxy (`BROWSER_DENY_URLS`). Pass `noise` and `denyUrls` to add patterns.
|
|
315
|
+
- The Worker options get no message filters: browser wording describes nothing workerd says, so a filter there could only hide a real failure.
|
|
316
|
+
- `tracesSampleRate` defaults to 0. `release` and `environment` are left unset unless given, so `@sentry/cloudflare` reads `SENTRY_RELEASE` (or `CF_VERSION_METADATA`) and `SENTRY_ENVIRONMENT` itself. `beforeSend` runs after the built-in filters; spread the result to add integrations or a sampler.
|
|
317
|
+
|
|
318
|
+
## Mail
|
|
319
|
+
|
|
320
|
+
`workers-start/mail` sends over Cloudflare Email Sending or Resend through one `send`, and every message must carry a subject, a recipient, and both a plain text and an HTML body.
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
import { cloudflareMailer, resendMailer } from "workers-start/mail";
|
|
324
|
+
|
|
325
|
+
const mail = env.NOTIFY
|
|
326
|
+
? cloudflareMailer(env.NOTIFY)
|
|
327
|
+
: resendMailer({ apiKey: env.RESEND_API_KEY });
|
|
328
|
+
|
|
329
|
+
await mail.send({
|
|
330
|
+
from: { email: "hello@example.com", name: "Example" },
|
|
331
|
+
to: "someone@example.com",
|
|
332
|
+
subject: "Welcome",
|
|
333
|
+
text: "Thanks for signing up.",
|
|
334
|
+
html: "<p>Thanks for signing up.</p>",
|
|
335
|
+
replyTo: "team@example.com",
|
|
336
|
+
listUnsubscribeUrl: "https://example.com/unsubscribe?t=...", // bulk and marketing mail
|
|
337
|
+
idempotencyKey: "welcome/usr_123", // Resend only
|
|
338
|
+
});
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
- `cloudflareMailer(env.BINDING)` uses the binding's structured form; its senders and recipients must be allowed in the binding's wrangler config. `resendMailer({ apiKey })` posts to the Resend API with `fetch`, no SDK.
|
|
342
|
+
- A failure throws `MailError` with Resend's `status` and a `permanent` flag (a 4xx other than 409 and 429), so a job step can retry only what might succeed.
|
|
343
|
+
- `listUnsubscribeUrl` adds `List-Unsubscribe` and the one-click `List-Unsubscribe-Post` header. `headers` adds any others, such as `In-Reply-To`.
|
|
344
|
+
- `recordingMailer()` applies the same checks and keeps every message in `sent`, for tests. `escapeHtml` escapes text for an HTML body.
|
|
345
|
+
|
|
283
346
|
## Sitemap and robots.txt
|
|
284
347
|
|
|
285
348
|
`workers-start/seo` builds the bodies for `/sitemap.xml` and `/robots.txt` routes. The entries and options take the same shape as Next's `MetadataRoute.Sitemap` and `MetadataRoute.Robots`.
|
package/dist/index.d.ts
CHANGED
|
@@ -10,8 +10,12 @@ export type { Ga4DateRange, Ga4FilterExpression, Ga4Report, Ga4ReportRequest, Ga
|
|
|
10
10
|
export { createGoogleAuth, GA4_SCOPE, GOOGLE_TOKEN_URL, GoogleApiError, ga4BatchRunReports, ga4Rows, ga4RunReport, googleAccessToken, importServiceAccountKey, inListFilter, parseServiceAccount, SEARCH_CONSOLE_SCOPE, searchConsoleQuery, serviceAccountJwt, } from "./google.js";
|
|
11
11
|
export type { HeadLink, HeadScript, HeadTags, MetaTag, PageImage, PageSeo, SiteHead, } from "./head.js";
|
|
12
12
|
export { createPageHead, jsonLdScript } from "./head.js";
|
|
13
|
+
export type { MailAddress, MailAttachment, Mailer, MailMessage, MailResult, ResendOptions, SendEmailBinding, } from "./mail.js";
|
|
14
|
+
export { checkMessage, cloudflareMailer, escapeHtml, MailError, recordingMailer, resendMailer, } from "./mail.js";
|
|
13
15
|
export type { AnalyticsScriptOptions, GaEventParams, TagManagerScriptOptions, ThemeScriptOptions, } from "./scripts.js";
|
|
14
16
|
export { analyticsScript, gaEvent, inlineJson, tagManagerScript, themeScript, } from "./scripts.js";
|
|
17
|
+
export type { BrowserSentryOptions, SentryEventLike, WorkerSentryOptions, } from "./sentry.js";
|
|
18
|
+
export { BROWSER_DENY_URLS, browserSentryOptions, dropLocalEvents, EXTENSION_ERROR, isBrowserNoise, isLocalEvent, NETWORK_ERROR, USER_REJECTION, validDsn, workerSentryOptions, } from "./sentry.js";
|
|
15
19
|
export type { RobotsOptions, RobotsRule, SitemapEntry } from "./seo.js";
|
|
16
20
|
export { escapeXml, robotsTxt, sitemapXml } from "./seo.js";
|
|
17
21
|
export type { WorkersModule } from "./workers.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACnD,YAAY,EACV,YAAY,EACZ,SAAS,EACT,aAAa,EACb,YAAY,EACZ,cAAc,EACd,qBAAqB,GACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EACL,YAAY,EACZ,aAAa,EACb,oBAAoB,GACrB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,WAAW,EACX,aAAa,EACb,WAAW,GACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EAClB,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,iBAAiB,EACjB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,YAAY,EACV,oBAAoB,EACpB,eAAe,EACf,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,cAAc,EACd,YAAY,EACZ,aAAa,EACb,cAAc,EACd,QAAQ,EACR,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC5D,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,aAAa,EACb,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,EACX,gBAAgB,GACjB,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,9 @@ export { sharedMaxAge, withEdgeCache, withoutBrowserMaxAge, } from "./cache.js";
|
|
|
4
4
|
export { acceptsHtml, createEntry, defaultIsPage, MARKDOWN_HEADERS, mergeVary, normalizePath, underPrefix, wantsMarkdown, withHeaders, } from "./entry.js";
|
|
5
5
|
export { createGoogleAuth, GA4_SCOPE, GOOGLE_TOKEN_URL, GoogleApiError, ga4BatchRunReports, ga4Rows, ga4RunReport, googleAccessToken, importServiceAccountKey, inListFilter, parseServiceAccount, SEARCH_CONSOLE_SCOPE, searchConsoleQuery, serviceAccountJwt, } from "./google.js";
|
|
6
6
|
export { createPageHead, jsonLdScript } from "./head.js";
|
|
7
|
+
export { checkMessage, cloudflareMailer, escapeHtml, MailError, recordingMailer, resendMailer, } from "./mail.js";
|
|
7
8
|
export { analyticsScript, gaEvent, inlineJson, tagManagerScript, themeScript, } from "./scripts.js";
|
|
9
|
+
export { BROWSER_DENY_URLS, browserSentryOptions, dropLocalEvents, EXTENSION_ERROR, isBrowserNoise, isLocalEvent, NETWORK_ERROR, USER_REJECTION, validDsn, workerSentryOptions, } from "./sentry.js";
|
|
8
10
|
export { escapeXml, robotsTxt, sitemapXml } from "./seo.js";
|
|
9
11
|
export { afterResponse, getBinding, getEnvString, isKvNamespace, loadWorkers, setWorkersModule, } from "./workers.js";
|
|
10
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AASnD,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAOpB,OAAO,EACL,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,WAAW,EACX,aAAa,EACb,WAAW,GACZ,MAAM,YAAY,CAAC;AAcpB,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,iBAAiB,EACjB,uBAAuB,EACvB,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAUrB,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AASnD,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAOpB,OAAO,EACL,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,WAAW,EACX,aAAa,EACb,WAAW,GACZ,MAAM,YAAY,CAAC;AAcpB,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,OAAO,EACP,YAAY,EACZ,iBAAiB,EACjB,uBAAuB,EACvB,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAUrB,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAUzD,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,SAAS,EACT,eAAe,EACf,YAAY,GACb,MAAM,WAAW,CAAC;AAOnB,OAAO,EACL,eAAe,EACf,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,WAAW,GACZ,MAAM,cAAc,CAAC;AAMtB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,cAAc,EACd,YAAY,EACZ,aAAa,EACb,cAAc,EACd,QAAQ,EACR,mBAAmB,GACpB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE5D,OAAO,EACL,aAAa,EACb,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,EACX,gBAAgB,GACjB,MAAM,cAAc,CAAC"}
|
package/dist/mail.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One way to send mail from a Worker, over Cloudflare Email Sending (the
|
|
3
|
+
* `send_email` binding) or Resend, with a recording fake for tests. Every
|
|
4
|
+
* message carries both a plain text and an HTML body: some clients show only
|
|
5
|
+
* one, and a message with only HTML scores worse with spam filters.
|
|
6
|
+
*/
|
|
7
|
+
export type MailAddress = string | {
|
|
8
|
+
email: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
};
|
|
11
|
+
export interface MailAttachment {
|
|
12
|
+
filename: string;
|
|
13
|
+
/** Base64. */
|
|
14
|
+
content: string;
|
|
15
|
+
contentType?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface MailMessage {
|
|
18
|
+
from: MailAddress;
|
|
19
|
+
to: string | string[];
|
|
20
|
+
subject: string;
|
|
21
|
+
text: string;
|
|
22
|
+
html: string;
|
|
23
|
+
replyTo?: MailAddress;
|
|
24
|
+
/** Extra headers, such as `In-Reply-To`. */
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
/** Adds `List-Unsubscribe` and one-click `List-Unsubscribe-Post`, for bulk and marketing mail. */
|
|
27
|
+
listUnsubscribeUrl?: string;
|
|
28
|
+
/** Resend only: repeating a send with the same key within 24 hours sends once. */
|
|
29
|
+
idempotencyKey?: string;
|
|
30
|
+
attachments?: MailAttachment[];
|
|
31
|
+
}
|
|
32
|
+
export interface MailResult {
|
|
33
|
+
/** The provider's message id. */
|
|
34
|
+
id?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface Mailer {
|
|
37
|
+
send(message: MailMessage): Promise<MailResult>;
|
|
38
|
+
}
|
|
39
|
+
/** A send that failed. `permanent` is true when repeating it cannot help (a 4xx other than 409 and 429). */
|
|
40
|
+
export declare class MailError extends Error {
|
|
41
|
+
readonly status?: number | undefined;
|
|
42
|
+
readonly body?: unknown;
|
|
43
|
+
readonly name = "MailError";
|
|
44
|
+
constructor(message: string, status?: number | undefined, body?: unknown);
|
|
45
|
+
get permanent(): boolean;
|
|
46
|
+
}
|
|
47
|
+
/** Throws unless the message has recipients, a subject and both bodies. */
|
|
48
|
+
export declare function checkMessage(message: MailMessage): void;
|
|
49
|
+
/** The `send_email` binding, structurally, so this module needs no Workers types. */
|
|
50
|
+
export interface SendEmailBinding {
|
|
51
|
+
send(message: any): Promise<{
|
|
52
|
+
messageId?: string;
|
|
53
|
+
} | undefined | void>;
|
|
54
|
+
}
|
|
55
|
+
/** Sends through Cloudflare Email Sending. Senders and recipients must be allowed in the binding's wrangler config. */
|
|
56
|
+
export declare function cloudflareMailer(binding: SendEmailBinding): Mailer;
|
|
57
|
+
export interface ResendOptions {
|
|
58
|
+
apiKey: string | undefined;
|
|
59
|
+
fetch?: (url: string, init?: RequestInit) => Promise<Response>;
|
|
60
|
+
/** Default 10 seconds. */
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
}
|
|
63
|
+
/** Sends through the Resend API with `fetch`, no SDK. */
|
|
64
|
+
export declare function resendMailer(options: ResendOptions): Mailer;
|
|
65
|
+
/** A mailer that records what it would send, for tests. It applies the same checks as the real ones. */
|
|
66
|
+
export declare function recordingMailer(): Mailer & {
|
|
67
|
+
sent: MailMessage[];
|
|
68
|
+
};
|
|
69
|
+
/** Escapes text for an HTML body. */
|
|
70
|
+
export declare function escapeHtml(text: string): string;
|
|
71
|
+
//# sourceMappingURL=mail.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mail.d.ts","sourceRoot":"","sources":["../src/mail.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEpE,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,WAAW,CAAC;IAClB,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,4CAA4C;IAC5C,OAAO,CAAC,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AAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,yDAAyD;AACzD,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAiD3D;AAED,wGAAwG;AACxG,wBAAgB,eAAe,IAAI,MAAM,GAAG;IAAE,IAAI,EAAE,WAAW,EAAE,CAAA;CAAE,CAUlE;AAED,qCAAqC;AACrC,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAQ/C"}
|
package/dist/mail.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One way to send mail from a Worker, over Cloudflare Email Sending (the
|
|
3
|
+
* `send_email` binding) or Resend, with a recording fake for tests. Every
|
|
4
|
+
* message carries both a plain text and an HTML body: some clients show only
|
|
5
|
+
* one, and a message with only HTML scores worse with spam filters.
|
|
6
|
+
*/
|
|
7
|
+
/** A send that failed. `permanent` is true when repeating it cannot help (a 4xx other than 409 and 429). */
|
|
8
|
+
export class MailError extends Error {
|
|
9
|
+
status;
|
|
10
|
+
body;
|
|
11
|
+
name = "MailError";
|
|
12
|
+
constructor(message, status, body) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.body = body;
|
|
16
|
+
}
|
|
17
|
+
get permanent() {
|
|
18
|
+
return (this.status !== undefined &&
|
|
19
|
+
this.status >= 400 &&
|
|
20
|
+
this.status < 500 &&
|
|
21
|
+
this.status !== 409 &&
|
|
22
|
+
this.status !== 429);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Throws unless the message has recipients, a subject and both bodies. */
|
|
26
|
+
export function checkMessage(message) {
|
|
27
|
+
const to = Array.isArray(message.to) ? message.to : [message.to];
|
|
28
|
+
if (to.length === 0 || to.some((t) => !t.trim()))
|
|
29
|
+
throw new MailError("A message needs at least one recipient.");
|
|
30
|
+
if (!message.subject.trim())
|
|
31
|
+
throw new MailError("A message needs a subject.");
|
|
32
|
+
if (!message.text.trim() || !message.html.trim())
|
|
33
|
+
throw new MailError("A message needs both a text and an HTML body.");
|
|
34
|
+
}
|
|
35
|
+
function headersFor(message) {
|
|
36
|
+
const headers = { ...message.headers };
|
|
37
|
+
if (message.listUnsubscribeUrl) {
|
|
38
|
+
headers["List-Unsubscribe"] = `<${message.listUnsubscribeUrl}>`;
|
|
39
|
+
headers["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click";
|
|
40
|
+
}
|
|
41
|
+
return headers;
|
|
42
|
+
}
|
|
43
|
+
const formatAddress = (a) => typeof a === "string"
|
|
44
|
+
? a
|
|
45
|
+
: a.name
|
|
46
|
+
? `${a.name.replace(/["\\]/g, "")} <${a.email}>`
|
|
47
|
+
: a.email;
|
|
48
|
+
/** Sends through Cloudflare Email Sending. Senders and recipients must be allowed in the binding's wrangler config. */
|
|
49
|
+
export function cloudflareMailer(binding) {
|
|
50
|
+
return {
|
|
51
|
+
async send(message) {
|
|
52
|
+
checkMessage(message);
|
|
53
|
+
const headers = headersFor(message);
|
|
54
|
+
const from = typeof message.from === "string"
|
|
55
|
+
? message.from
|
|
56
|
+
: { email: message.from.email, name: message.from.name ?? "" };
|
|
57
|
+
try {
|
|
58
|
+
const result = await binding.send({
|
|
59
|
+
from,
|
|
60
|
+
to: message.to,
|
|
61
|
+
subject: message.subject,
|
|
62
|
+
text: message.text,
|
|
63
|
+
html: message.html,
|
|
64
|
+
...(message.replyTo
|
|
65
|
+
? {
|
|
66
|
+
replyTo: typeof message.replyTo === "string"
|
|
67
|
+
? message.replyTo
|
|
68
|
+
: {
|
|
69
|
+
email: message.replyTo.email,
|
|
70
|
+
name: message.replyTo.name ?? "",
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
: {}),
|
|
74
|
+
...(Object.keys(headers).length ? { headers } : {}),
|
|
75
|
+
...(message.attachments?.length
|
|
76
|
+
? {
|
|
77
|
+
attachments: message.attachments.map((a) => ({
|
|
78
|
+
disposition: "attachment",
|
|
79
|
+
filename: a.filename,
|
|
80
|
+
type: a.contentType ?? "application/octet-stream",
|
|
81
|
+
content: a.content,
|
|
82
|
+
})),
|
|
83
|
+
}
|
|
84
|
+
: {}),
|
|
85
|
+
});
|
|
86
|
+
return result?.messageId ? { id: result.messageId } : {};
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
throw new MailError(`Email Sending failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Sends through the Resend API with `fetch`, no SDK. */
|
|
95
|
+
export function resendMailer(options) {
|
|
96
|
+
const fetcher = options.fetch ?? ((url, init) => fetch(url, init));
|
|
97
|
+
return {
|
|
98
|
+
async send(message) {
|
|
99
|
+
checkMessage(message);
|
|
100
|
+
if (!options.apiKey)
|
|
101
|
+
throw new MailError("RESEND_API_KEY is not set.");
|
|
102
|
+
const headers = headersFor(message);
|
|
103
|
+
const body = {
|
|
104
|
+
from: formatAddress(message.from),
|
|
105
|
+
to: message.to,
|
|
106
|
+
subject: message.subject,
|
|
107
|
+
text: message.text,
|
|
108
|
+
html: message.html,
|
|
109
|
+
};
|
|
110
|
+
if (message.replyTo)
|
|
111
|
+
body.reply_to = formatAddress(message.replyTo);
|
|
112
|
+
if (Object.keys(headers).length)
|
|
113
|
+
body.headers = headers;
|
|
114
|
+
if (message.attachments?.length) {
|
|
115
|
+
body.attachments = message.attachments.map((a) => ({
|
|
116
|
+
filename: a.filename,
|
|
117
|
+
content: a.content,
|
|
118
|
+
...(a.contentType ? { content_type: a.contentType } : {}),
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
const res = await fetcher("https://api.resend.com/emails", {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: {
|
|
124
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
125
|
+
"content-type": "application/json",
|
|
126
|
+
...(message.idempotencyKey
|
|
127
|
+
? { "idempotency-key": message.idempotencyKey }
|
|
128
|
+
: {}),
|
|
129
|
+
},
|
|
130
|
+
body: JSON.stringify(body),
|
|
131
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
|
|
132
|
+
});
|
|
133
|
+
const json = (await res.json().catch(() => null));
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
throw new MailError(`Resend failed (${res.status}): ${json?.message ?? res.statusText}`, res.status, json);
|
|
136
|
+
}
|
|
137
|
+
return json?.id ? { id: json.id } : {};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/** A mailer that records what it would send, for tests. It applies the same checks as the real ones. */
|
|
142
|
+
export function recordingMailer() {
|
|
143
|
+
const sent = [];
|
|
144
|
+
return {
|
|
145
|
+
sent,
|
|
146
|
+
async send(message) {
|
|
147
|
+
checkMessage(message);
|
|
148
|
+
sent.push(message);
|
|
149
|
+
return { id: `test-${sent.length}` };
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Escapes text for an HTML body. */
|
|
154
|
+
export function escapeHtml(text) {
|
|
155
|
+
return text.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] ?? c);
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=mail.js.map
|
package/dist/mail.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../src/mail.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAoCH,4GAA4G;AAC5G,MAAM,OAAO,SAAU,SAAQ,KAAK;IAIvB,MAAM;IACN,IAAI;IAJG,IAAI,GAAG,WAAW,CAAC;IACrC,YACE,OAAe,EACN,MAAe,EACf,IAAc;QAEvB,KAAK,CAAC,OAAO,CAAC,CAAC;sBAHN,MAAM;oBACN,IAAI;IAGf,CAAC;IACD,IAAI,SAAS;QACX,OAAO,CACL,IAAI,CAAC,MAAM,KAAK,SAAS;YACzB,IAAI,CAAC,MAAM,IAAI,GAAG;YAClB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"}
|
package/dist/sentry.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentry options for a Worker and its pages, without importing an SDK: the
|
|
3
|
+
* objects here go straight into `withSentry` from `@sentry/cloudflare` and
|
|
4
|
+
* `Sentry.init` from a browser SDK.
|
|
5
|
+
*
|
|
6
|
+
* What every app had written for itself: no DSN means no SDK traffic, events
|
|
7
|
+
* from a local server (`wrangler dev`, `vite preview`) never reach the
|
|
8
|
+
* production project, and in the browser the noise that is not the app's to
|
|
9
|
+
* fix (requests that never left the device, extension and injected wallet
|
|
10
|
+
* scripts, users declining a wallet prompt) is dropped. The Worker side gets
|
|
11
|
+
* no message filters: browser wording describes nothing workerd says, so a
|
|
12
|
+
* filter there could only hide a real failure.
|
|
13
|
+
*
|
|
14
|
+
* Release and environment are left unset unless given, so
|
|
15
|
+
* `@sentry/cloudflare` reads `SENTRY_RELEASE` (or `CF_VERSION_METADATA`) and
|
|
16
|
+
* `SENTRY_ENVIRONMENT` from the Worker's env itself.
|
|
17
|
+
*/
|
|
18
|
+
/** The part of a Sentry event the filters read. */
|
|
19
|
+
export interface SentryEventLike {
|
|
20
|
+
request?: {
|
|
21
|
+
url?: string;
|
|
22
|
+
};
|
|
23
|
+
exception?: {
|
|
24
|
+
values?: {
|
|
25
|
+
value?: string;
|
|
26
|
+
type?: string;
|
|
27
|
+
}[];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
type AnyEvent = any;
|
|
31
|
+
type BeforeSend = (event: AnyEvent, hint: AnyEvent) => AnyEvent;
|
|
32
|
+
/** Whether the event's request was served from this machine. An event with no request (a cron run, a workflow step) is not local. */
|
|
33
|
+
export declare function isLocalEvent(event: SentryEventLike): boolean;
|
|
34
|
+
/** A `beforeSend` that drops local events and keeps the rest. */
|
|
35
|
+
export declare function dropLocalEvents<T extends SentryEventLike>(event: T): T | null;
|
|
36
|
+
/** Browser wording for a request that never left the device: Chrome, Safari, Firefox. */
|
|
37
|
+
export declare const NETWORK_ERROR: RegExp;
|
|
38
|
+
/** Errors thrown by extension content scripts and injected wallets. */
|
|
39
|
+
export declare const EXTENSION_ERROR: RegExp;
|
|
40
|
+
/** A person declining a wallet prompt. */
|
|
41
|
+
export declare const USER_REJECTION: RegExp;
|
|
42
|
+
/** Scripts other people inject into the page: extensions, wallets and Google Translate's proxy. */
|
|
43
|
+
export declare const BROWSER_DENY_URLS: readonly RegExp[];
|
|
44
|
+
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against the message. */
|
|
45
|
+
export declare function isBrowserNoise(event: SentryEventLike, extra?: readonly RegExp[]): boolean;
|
|
46
|
+
/** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
|
|
47
|
+
export declare function validDsn(dsn: string | null | undefined): string | undefined;
|
|
48
|
+
interface CommonOptions {
|
|
49
|
+
/** Overrides the DSN read from the env. */
|
|
50
|
+
dsn?: string | undefined;
|
|
51
|
+
/** Default 0: no tracing. */
|
|
52
|
+
tracesSampleRate?: number;
|
|
53
|
+
/** Runs after the built-in filters. */
|
|
54
|
+
beforeSend?: BeforeSend;
|
|
55
|
+
/** Tags set on every event, such as `{ runtime: "worker", worker: "api" }`. */
|
|
56
|
+
tags?: Record<string, string>;
|
|
57
|
+
release?: string;
|
|
58
|
+
environment?: string;
|
|
59
|
+
}
|
|
60
|
+
export interface WorkerSentryOptions {
|
|
61
|
+
dsn: string | undefined;
|
|
62
|
+
enabled: boolean;
|
|
63
|
+
tracesSampleRate: number;
|
|
64
|
+
beforeSend: BeforeSend;
|
|
65
|
+
beforeSendTransaction: BeforeSend;
|
|
66
|
+
initialScope?: {
|
|
67
|
+
tags: Record<string, string>;
|
|
68
|
+
};
|
|
69
|
+
release?: string;
|
|
70
|
+
environment?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Options for `withSentry`, `instrumentWorkflowWithSentry` and
|
|
74
|
+
* `instrumentDurableObjectWithSentry`. Reads `SENTRY_DSN` from the env;
|
|
75
|
+
* spread the result and add anything app-specific (integrations, samplers).
|
|
76
|
+
*/
|
|
77
|
+
export declare function workerSentryOptions(env: {
|
|
78
|
+
SENTRY_DSN?: string | undefined;
|
|
79
|
+
}, options?: CommonOptions): WorkerSentryOptions;
|
|
80
|
+
export interface BrowserSentryOptions extends WorkerSentryOptions {
|
|
81
|
+
denyUrls: RegExp[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Options for a browser SDK's `Sentry.init`. Pass the DSN (from the build),
|
|
85
|
+
* and `enabled: false` for dev builds, which the browser cannot tell from
|
|
86
|
+
* the env.
|
|
87
|
+
*/
|
|
88
|
+
export declare function browserSentryOptions(options?: CommonOptions & {
|
|
89
|
+
enabled?: boolean;
|
|
90
|
+
/** More script URLs to ignore. */
|
|
91
|
+
denyUrls?: readonly RegExp[];
|
|
92
|
+
/** More messages that are noise. */
|
|
93
|
+
noise?: readonly RegExp[];
|
|
94
|
+
}): BrowserSentryOptions;
|
|
95
|
+
export {};
|
|
96
|
+
//# sourceMappingURL=sentry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sentry.d.ts","sourceRoot":"","sources":["../src/sentry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,mDAAmD;AACnD,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3B,SAAS,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE;YAAE,KAAK,CAAC,EAAE,MAAM,CAAC;YAAC,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,EAAE,CAAA;KAAE,CAAC;CAC9D;AAGD,KAAK,QAAQ,GAAG,GAAG,CAAC;AACpB,KAAK,UAAU,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,KAAK,QAAQ,CAAC;AAIhE,qIAAqI;AACrI,w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EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,EACxC,OAAO,GAAE,aAAkB,GAC1B,mBAAmB,CAarB;AAED,MAAM,WAAW,oBAAqB,SAAQ,mBAAmB;IAC/D,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,GAAE,aAAa,GAAG;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kCAAkC;IAClC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,oCAAoC;IACpC,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACtB,GACL,oBAAoB,CAmBtB"}
|
package/dist/sentry.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentry options for a Worker and its pages, without importing an SDK: the
|
|
3
|
+
* objects here go straight into `withSentry` from `@sentry/cloudflare` and
|
|
4
|
+
* `Sentry.init` from a browser SDK.
|
|
5
|
+
*
|
|
6
|
+
* What every app had written for itself: no DSN means no SDK traffic, events
|
|
7
|
+
* from a local server (`wrangler dev`, `vite preview`) never reach the
|
|
8
|
+
* production project, and in the browser the noise that is not the app's to
|
|
9
|
+
* fix (requests that never left the device, extension and injected wallet
|
|
10
|
+
* scripts, users declining a wallet prompt) is dropped. The Worker side gets
|
|
11
|
+
* no message filters: browser wording describes nothing workerd says, so a
|
|
12
|
+
* filter there could only hide a real failure.
|
|
13
|
+
*
|
|
14
|
+
* Release and environment are left unset unless given, so
|
|
15
|
+
* `@sentry/cloudflare` reads `SENTRY_RELEASE` (or `CF_VERSION_METADATA`) and
|
|
16
|
+
* `SENTRY_ENVIRONMENT` from the Worker's env itself.
|
|
17
|
+
*/
|
|
18
|
+
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
19
|
+
/** Whether the event's request was served from this machine. An event with no request (a cron run, a workflow step) is not local. */
|
|
20
|
+
export function isLocalEvent(event) {
|
|
21
|
+
const url = event.request?.url;
|
|
22
|
+
if (!url)
|
|
23
|
+
return false;
|
|
24
|
+
try {
|
|
25
|
+
const host = new URL(url).hostname;
|
|
26
|
+
return LOCAL_HOSTS.has(host) || host.endsWith(".localhost");
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** A `beforeSend` that drops local events and keeps the rest. */
|
|
33
|
+
export function dropLocalEvents(event) {
|
|
34
|
+
return isLocalEvent(event) ? null : event;
|
|
35
|
+
}
|
|
36
|
+
/** Browser wording for a request that never left the device: Chrome, Safari, Firefox. */
|
|
37
|
+
export const NETWORK_ERROR = /failed to fetch|load failed|networkerror/i;
|
|
38
|
+
/** Errors thrown by extension content scripts and injected wallets. */
|
|
39
|
+
export const EXTENSION_ERROR = /func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys: code, (data, )?message/i;
|
|
40
|
+
/** A person declining a wallet prompt. */
|
|
41
|
+
export const USER_REJECTION = /user rejected|user denied|rejected the request/i;
|
|
42
|
+
/** Scripts other people inject into the page: extensions, wallets and Google Translate's proxy. */
|
|
43
|
+
export const BROWSER_DENY_URLS = [
|
|
44
|
+
/\/_\/translate_http\//i,
|
|
45
|
+
/injected\.js/i,
|
|
46
|
+
/inpage\.js/i,
|
|
47
|
+
/contentscript/i,
|
|
48
|
+
/chrome-extension:\/\//i,
|
|
49
|
+
/moz-extension:\/\//i,
|
|
50
|
+
/safari-(web-)?extension:\/\//i,
|
|
51
|
+
];
|
|
52
|
+
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against the message. */
|
|
53
|
+
export function isBrowserNoise(event, extra = []) {
|
|
54
|
+
const message = event.exception?.values?.[0]?.value ?? "";
|
|
55
|
+
return [NETWORK_ERROR, EXTENSION_ERROR, USER_REJECTION, ...extra].some((re) => re.test(message));
|
|
56
|
+
}
|
|
57
|
+
/** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
|
|
58
|
+
export function validDsn(dsn) {
|
|
59
|
+
const value = dsn?.trim();
|
|
60
|
+
return value?.startsWith("https://") ? value : undefined;
|
|
61
|
+
}
|
|
62
|
+
function chain(...steps) {
|
|
63
|
+
return (event, hint) => {
|
|
64
|
+
let current = event;
|
|
65
|
+
for (const step of steps) {
|
|
66
|
+
if (!step || current === null)
|
|
67
|
+
continue;
|
|
68
|
+
current = step(current, hint);
|
|
69
|
+
}
|
|
70
|
+
return current;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Options for `withSentry`, `instrumentWorkflowWithSentry` and
|
|
75
|
+
* `instrumentDurableObjectWithSentry`. Reads `SENTRY_DSN` from the env;
|
|
76
|
+
* spread the result and add anything app-specific (integrations, samplers).
|
|
77
|
+
*/
|
|
78
|
+
export function workerSentryOptions(env, options = {}) {
|
|
79
|
+
const dsn = validDsn(options.dsn ?? env.SENTRY_DSN);
|
|
80
|
+
const out = {
|
|
81
|
+
dsn,
|
|
82
|
+
enabled: dsn !== undefined,
|
|
83
|
+
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
84
|
+
beforeSend: chain(dropLocalEvents, options.beforeSend),
|
|
85
|
+
beforeSendTransaction: dropLocalEvents,
|
|
86
|
+
};
|
|
87
|
+
if (options.tags)
|
|
88
|
+
out.initialScope = { tags: options.tags };
|
|
89
|
+
if (options.release)
|
|
90
|
+
out.release = options.release;
|
|
91
|
+
if (options.environment)
|
|
92
|
+
out.environment = options.environment;
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Options for a browser SDK's `Sentry.init`. Pass the DSN (from the build),
|
|
97
|
+
* and `enabled: false` for dev builds, which the browser cannot tell from
|
|
98
|
+
* the env.
|
|
99
|
+
*/
|
|
100
|
+
export function browserSentryOptions(options = {}) {
|
|
101
|
+
const dsn = validDsn(options.dsn);
|
|
102
|
+
const noise = options.noise ?? [];
|
|
103
|
+
const out = {
|
|
104
|
+
dsn,
|
|
105
|
+
enabled: dsn !== undefined && options.enabled !== false,
|
|
106
|
+
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
107
|
+
denyUrls: [...BROWSER_DENY_URLS, ...(options.denyUrls ?? [])],
|
|
108
|
+
beforeSend: chain((event) => isLocalEvent(event) || isBrowserNoise(event, noise) ? null : event, options.beforeSend),
|
|
109
|
+
beforeSendTransaction: dropLocalEvents,
|
|
110
|
+
};
|
|
111
|
+
if (options.tags)
|
|
112
|
+
out.initialScope = { tags: options.tags };
|
|
113
|
+
if (options.release)
|
|
114
|
+
out.release = options.release;
|
|
115
|
+
if (options.environment)
|
|
116
|
+
out.environment = options.environment;
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=sentry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sentry.js","sourceRoot":"","sources":["../src/sentry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAYH,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;AAExE,qIAAqI;AACrI,MAAM,UAAU,YAAY,CAAC,KAAsB;IACjD,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,EAAE,GAAG,CAAC;IAC/B,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;QACnC,OAAO,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC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,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;IAClC,MAAM,GAAG,GAAyB;QAChC,GAAG;QACH,OAAO,EAAE,GAAG,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK;QACvD,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,CAAC;QAC/C,QAAQ,EAAE,CAAC,GAAG,iBAAiB,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;QAC7D,UAAU,EAAE,KAAK,CACf,CAAC,KAAK,EAAE,EAAE,CACR,YAAY,CAAC,KAAK,CAAC,IAAI,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,EACpE,OAAO,CAAC,UAAU,CACnB;QACD,qBAAqB,EAAE,eAAe;KACvC,CAAC;IACF,IAAI,OAAO,CAAC,IAAI;QAAE,GAAG,CAAC,YAAY,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;IAC5D,IAAI,OAAO,CAAC,OAAO;QAAE,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IACnD,IAAI,OAAO,CAAC,WAAW;QAAE,GAAG,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAC/D,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "workers-start",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "The parts every TanStack Start app on Cloudflare Workers ends up writing: Markdown for agents on the same URL, a head helper that stops share cards leaking between pages, lazy Workers bindings, and a check that no local secret ships in the build.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ryan Ghods",
|
|
@@ -23,7 +23,10 @@
|
|
|
23
23
|
"markdown",
|
|
24
24
|
"llms",
|
|
25
25
|
"google-analytics",
|
|
26
|
-
"search-console"
|
|
26
|
+
"search-console",
|
|
27
|
+
"sentry",
|
|
28
|
+
"email",
|
|
29
|
+
"resend"
|
|
27
30
|
],
|
|
28
31
|
"type": "module",
|
|
29
32
|
"sideEffects": false,
|
|
@@ -88,6 +91,16 @@
|
|
|
88
91
|
"import": "./dist/google.js",
|
|
89
92
|
"default": "./dist/google.js"
|
|
90
93
|
},
|
|
94
|
+
"./sentry": {
|
|
95
|
+
"types": "./dist/sentry.d.ts",
|
|
96
|
+
"import": "./dist/sentry.js",
|
|
97
|
+
"default": "./dist/sentry.js"
|
|
98
|
+
},
|
|
99
|
+
"./mail": {
|
|
100
|
+
"types": "./dist/mail.d.ts",
|
|
101
|
+
"import": "./dist/mail.js",
|
|
102
|
+
"default": "./dist/mail.js"
|
|
103
|
+
},
|
|
91
104
|
"./package.json": "./package.json"
|
|
92
105
|
},
|
|
93
106
|
"bin": {
|
package/src/index.ts
CHANGED
|
@@ -76,6 +76,23 @@ export type {
|
|
|
76
76
|
SiteHead,
|
|
77
77
|
} from "./head.js";
|
|
78
78
|
export { createPageHead, jsonLdScript } from "./head.js";
|
|
79
|
+
export type {
|
|
80
|
+
MailAddress,
|
|
81
|
+
MailAttachment,
|
|
82
|
+
Mailer,
|
|
83
|
+
MailMessage,
|
|
84
|
+
MailResult,
|
|
85
|
+
ResendOptions,
|
|
86
|
+
SendEmailBinding,
|
|
87
|
+
} from "./mail.js";
|
|
88
|
+
export {
|
|
89
|
+
checkMessage,
|
|
90
|
+
cloudflareMailer,
|
|
91
|
+
escapeHtml,
|
|
92
|
+
MailError,
|
|
93
|
+
recordingMailer,
|
|
94
|
+
resendMailer,
|
|
95
|
+
} from "./mail.js";
|
|
79
96
|
export type {
|
|
80
97
|
AnalyticsScriptOptions,
|
|
81
98
|
GaEventParams,
|
|
@@ -89,6 +106,23 @@ export {
|
|
|
89
106
|
tagManagerScript,
|
|
90
107
|
themeScript,
|
|
91
108
|
} from "./scripts.js";
|
|
109
|
+
export type {
|
|
110
|
+
BrowserSentryOptions,
|
|
111
|
+
SentryEventLike,
|
|
112
|
+
WorkerSentryOptions,
|
|
113
|
+
} from "./sentry.js";
|
|
114
|
+
export {
|
|
115
|
+
BROWSER_DENY_URLS,
|
|
116
|
+
browserSentryOptions,
|
|
117
|
+
dropLocalEvents,
|
|
118
|
+
EXTENSION_ERROR,
|
|
119
|
+
isBrowserNoise,
|
|
120
|
+
isLocalEvent,
|
|
121
|
+
NETWORK_ERROR,
|
|
122
|
+
USER_REJECTION,
|
|
123
|
+
validDsn,
|
|
124
|
+
workerSentryOptions,
|
|
125
|
+
} from "./sentry.js";
|
|
92
126
|
export type { RobotsOptions, RobotsRule, SitemapEntry } from "./seo.js";
|
|
93
127
|
export { escapeXml, robotsTxt, sitemapXml } from "./seo.js";
|
|
94
128
|
export type { WorkersModule } from "./workers.js";
|
package/src/mail.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One way to send mail from a Worker, over Cloudflare Email Sending (the
|
|
3
|
+
* `send_email` binding) or Resend, with a recording fake for tests. Every
|
|
4
|
+
* message carries both a plain text and an HTML body: some clients show only
|
|
5
|
+
* one, and a message with only HTML scores worse with spam filters.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type MailAddress = string | { email: string; name?: string };
|
|
9
|
+
|
|
10
|
+
export interface MailAttachment {
|
|
11
|
+
filename: string;
|
|
12
|
+
/** Base64. */
|
|
13
|
+
content: string;
|
|
14
|
+
contentType?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface MailMessage {
|
|
18
|
+
from: MailAddress;
|
|
19
|
+
to: string | string[];
|
|
20
|
+
subject: string;
|
|
21
|
+
text: string;
|
|
22
|
+
html: string;
|
|
23
|
+
replyTo?: MailAddress;
|
|
24
|
+
/** Extra headers, such as `In-Reply-To`. */
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
/** Adds `List-Unsubscribe` and one-click `List-Unsubscribe-Post`, for bulk and marketing mail. */
|
|
27
|
+
listUnsubscribeUrl?: string;
|
|
28
|
+
/** Resend only: repeating a send with the same key within 24 hours sends once. */
|
|
29
|
+
idempotencyKey?: string;
|
|
30
|
+
attachments?: MailAttachment[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface MailResult {
|
|
34
|
+
/** The provider's message id. */
|
|
35
|
+
id?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface Mailer {
|
|
39
|
+
send(message: MailMessage): Promise<MailResult>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A send that failed. `permanent` is true when repeating it cannot help (a 4xx other than 409 and 429). */
|
|
43
|
+
export class MailError extends Error {
|
|
44
|
+
override readonly name = "MailError";
|
|
45
|
+
constructor(
|
|
46
|
+
message: string,
|
|
47
|
+
readonly status?: number,
|
|
48
|
+
readonly body?: unknown,
|
|
49
|
+
) {
|
|
50
|
+
super(message);
|
|
51
|
+
}
|
|
52
|
+
get permanent(): boolean {
|
|
53
|
+
return (
|
|
54
|
+
this.status !== undefined &&
|
|
55
|
+
this.status >= 400 &&
|
|
56
|
+
this.status < 500 &&
|
|
57
|
+
this.status !== 409 &&
|
|
58
|
+
this.status !== 429
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Throws unless the message has recipients, a subject and both bodies. */
|
|
64
|
+
export function checkMessage(message: MailMessage): void {
|
|
65
|
+
const to = Array.isArray(message.to) ? message.to : [message.to];
|
|
66
|
+
if (to.length === 0 || to.some((t) => !t.trim()))
|
|
67
|
+
throw new MailError("A message needs at least one recipient.");
|
|
68
|
+
if (!message.subject.trim())
|
|
69
|
+
throw new MailError("A message needs a subject.");
|
|
70
|
+
if (!message.text.trim() || !message.html.trim())
|
|
71
|
+
throw new MailError("A message needs both a text and an HTML body.");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function headersFor(message: MailMessage): Record<string, string> {
|
|
75
|
+
const headers = { ...message.headers };
|
|
76
|
+
if (message.listUnsubscribeUrl) {
|
|
77
|
+
headers["List-Unsubscribe"] = `<${message.listUnsubscribeUrl}>`;
|
|
78
|
+
headers["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click";
|
|
79
|
+
}
|
|
80
|
+
return headers;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const formatAddress = (a: MailAddress) =>
|
|
84
|
+
typeof a === "string"
|
|
85
|
+
? a
|
|
86
|
+
: a.name
|
|
87
|
+
? `${a.name.replace(/["\\]/g, "")} <${a.email}>`
|
|
88
|
+
: a.email;
|
|
89
|
+
|
|
90
|
+
/** The `send_email` binding, structurally, so this module needs no Workers types. */
|
|
91
|
+
export interface SendEmailBinding {
|
|
92
|
+
// biome-ignore lint/suspicious/noExplicitAny: the binding's builder type lives in @cloudflare/workers-types.
|
|
93
|
+
send(message: any): Promise<{ messageId?: string } | undefined | void>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Sends through Cloudflare Email Sending. Senders and recipients must be allowed in the binding's wrangler config. */
|
|
97
|
+
export function cloudflareMailer(binding: SendEmailBinding): Mailer {
|
|
98
|
+
return {
|
|
99
|
+
async send(message) {
|
|
100
|
+
checkMessage(message);
|
|
101
|
+
const headers = headersFor(message);
|
|
102
|
+
const from =
|
|
103
|
+
typeof message.from === "string"
|
|
104
|
+
? message.from
|
|
105
|
+
: { email: message.from.email, name: message.from.name ?? "" };
|
|
106
|
+
try {
|
|
107
|
+
const result = await binding.send({
|
|
108
|
+
from,
|
|
109
|
+
to: message.to,
|
|
110
|
+
subject: message.subject,
|
|
111
|
+
text: message.text,
|
|
112
|
+
html: message.html,
|
|
113
|
+
...(message.replyTo
|
|
114
|
+
? {
|
|
115
|
+
replyTo:
|
|
116
|
+
typeof message.replyTo === "string"
|
|
117
|
+
? message.replyTo
|
|
118
|
+
: {
|
|
119
|
+
email: message.replyTo.email,
|
|
120
|
+
name: message.replyTo.name ?? "",
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
: {}),
|
|
124
|
+
...(Object.keys(headers).length ? { headers } : {}),
|
|
125
|
+
...(message.attachments?.length
|
|
126
|
+
? {
|
|
127
|
+
attachments: message.attachments.map((a) => ({
|
|
128
|
+
disposition: "attachment",
|
|
129
|
+
filename: a.filename,
|
|
130
|
+
type: a.contentType ?? "application/octet-stream",
|
|
131
|
+
content: a.content,
|
|
132
|
+
})),
|
|
133
|
+
}
|
|
134
|
+
: {}),
|
|
135
|
+
});
|
|
136
|
+
return result?.messageId ? { id: result.messageId } : {};
|
|
137
|
+
} catch (err) {
|
|
138
|
+
throw new MailError(
|
|
139
|
+
`Email Sending failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface ResendOptions {
|
|
147
|
+
apiKey: string | undefined;
|
|
148
|
+
fetch?: (url: string, init?: RequestInit) => Promise<Response>;
|
|
149
|
+
/** Default 10 seconds. */
|
|
150
|
+
timeoutMs?: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Sends through the Resend API with `fetch`, no SDK. */
|
|
154
|
+
export function resendMailer(options: ResendOptions): Mailer {
|
|
155
|
+
const fetcher = options.fetch ?? ((url, init) => fetch(url, init));
|
|
156
|
+
return {
|
|
157
|
+
async send(message) {
|
|
158
|
+
checkMessage(message);
|
|
159
|
+
if (!options.apiKey) throw new MailError("RESEND_API_KEY is not set.");
|
|
160
|
+
const headers = headersFor(message);
|
|
161
|
+
const body: Record<string, unknown> = {
|
|
162
|
+
from: formatAddress(message.from),
|
|
163
|
+
to: message.to,
|
|
164
|
+
subject: message.subject,
|
|
165
|
+
text: message.text,
|
|
166
|
+
html: message.html,
|
|
167
|
+
};
|
|
168
|
+
if (message.replyTo) body.reply_to = formatAddress(message.replyTo);
|
|
169
|
+
if (Object.keys(headers).length) body.headers = headers;
|
|
170
|
+
if (message.attachments?.length) {
|
|
171
|
+
body.attachments = message.attachments.map((a) => ({
|
|
172
|
+
filename: a.filename,
|
|
173
|
+
content: a.content,
|
|
174
|
+
...(a.contentType ? { content_type: a.contentType } : {}),
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
const res = await fetcher("https://api.resend.com/emails", {
|
|
178
|
+
method: "POST",
|
|
179
|
+
headers: {
|
|
180
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
181
|
+
"content-type": "application/json",
|
|
182
|
+
...(message.idempotencyKey
|
|
183
|
+
? { "idempotency-key": message.idempotencyKey }
|
|
184
|
+
: {}),
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify(body),
|
|
187
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
|
|
188
|
+
});
|
|
189
|
+
const json = (await res.json().catch(() => null)) as {
|
|
190
|
+
id?: string;
|
|
191
|
+
message?: string;
|
|
192
|
+
} | null;
|
|
193
|
+
if (!res.ok) {
|
|
194
|
+
throw new MailError(
|
|
195
|
+
`Resend failed (${res.status}): ${json?.message ?? res.statusText}`,
|
|
196
|
+
res.status,
|
|
197
|
+
json,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
return json?.id ? { id: json.id } : {};
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A mailer that records what it would send, for tests. It applies the same checks as the real ones. */
|
|
206
|
+
export function recordingMailer(): Mailer & { sent: MailMessage[] } {
|
|
207
|
+
const sent: MailMessage[] = [];
|
|
208
|
+
return {
|
|
209
|
+
sent,
|
|
210
|
+
async send(message) {
|
|
211
|
+
checkMessage(message);
|
|
212
|
+
sent.push(message);
|
|
213
|
+
return { id: `test-${sent.length}` };
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Escapes text for an HTML body. */
|
|
219
|
+
export function escapeHtml(text: string): string {
|
|
220
|
+
return text.replace(
|
|
221
|
+
/[&<>"']/g,
|
|
222
|
+
(c) =>
|
|
223
|
+
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
|
224
|
+
c
|
|
225
|
+
] ?? c,
|
|
226
|
+
);
|
|
227
|
+
}
|
package/src/sentry.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentry options for a Worker and its pages, without importing an SDK: the
|
|
3
|
+
* objects here go straight into `withSentry` from `@sentry/cloudflare` and
|
|
4
|
+
* `Sentry.init` from a browser SDK.
|
|
5
|
+
*
|
|
6
|
+
* What every app had written for itself: no DSN means no SDK traffic, events
|
|
7
|
+
* from a local server (`wrangler dev`, `vite preview`) never reach the
|
|
8
|
+
* production project, and in the browser the noise that is not the app's to
|
|
9
|
+
* fix (requests that never left the device, extension and injected wallet
|
|
10
|
+
* scripts, users declining a wallet prompt) is dropped. The Worker side gets
|
|
11
|
+
* no message filters: browser wording describes nothing workerd says, so a
|
|
12
|
+
* filter there could only hide a real failure.
|
|
13
|
+
*
|
|
14
|
+
* Release and environment are left unset unless given, so
|
|
15
|
+
* `@sentry/cloudflare` reads `SENTRY_RELEASE` (or `CF_VERSION_METADATA`) and
|
|
16
|
+
* `SENTRY_ENVIRONMENT` from the Worker's env itself.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The part of a Sentry event the filters read. */
|
|
20
|
+
export interface SentryEventLike {
|
|
21
|
+
request?: { url?: string };
|
|
22
|
+
exception?: { values?: { value?: string; type?: string }[] };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// biome-ignore lint/suspicious/noExplicitAny: each SDK types its events differently; these options must be assignable to all of them.
|
|
26
|
+
type AnyEvent = any;
|
|
27
|
+
type BeforeSend = (event: AnyEvent, hint: AnyEvent) => AnyEvent;
|
|
28
|
+
|
|
29
|
+
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
30
|
+
|
|
31
|
+
/** Whether the event's request was served from this machine. An event with no request (a cron run, a workflow step) is not local. */
|
|
32
|
+
export function isLocalEvent(event: SentryEventLike): boolean {
|
|
33
|
+
const url = event.request?.url;
|
|
34
|
+
if (!url) return false;
|
|
35
|
+
try {
|
|
36
|
+
const host = new URL(url).hostname;
|
|
37
|
+
return LOCAL_HOSTS.has(host) || host.endsWith(".localhost");
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A `beforeSend` that drops local events and keeps the rest. */
|
|
44
|
+
export function dropLocalEvents<T extends SentryEventLike>(event: T): T | null {
|
|
45
|
+
return isLocalEvent(event) ? null : event;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Browser wording for a request that never left the device: Chrome, Safari, Firefox. */
|
|
49
|
+
export const NETWORK_ERROR = /failed to fetch|load failed|networkerror/i;
|
|
50
|
+
/** Errors thrown by extension content scripts and injected wallets. */
|
|
51
|
+
export const EXTENSION_ERROR =
|
|
52
|
+
/func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys: code, (data, )?message/i;
|
|
53
|
+
/** A person declining a wallet prompt. */
|
|
54
|
+
export const USER_REJECTION = /user rejected|user denied|rejected the request/i;
|
|
55
|
+
|
|
56
|
+
/** Scripts other people inject into the page: extensions, wallets and Google Translate's proxy. */
|
|
57
|
+
export const BROWSER_DENY_URLS: readonly RegExp[] = [
|
|
58
|
+
/\/_\/translate_http\//i,
|
|
59
|
+
/injected\.js/i,
|
|
60
|
+
/inpage\.js/i,
|
|
61
|
+
/contentscript/i,
|
|
62
|
+
/chrome-extension:\/\//i,
|
|
63
|
+
/moz-extension:\/\//i,
|
|
64
|
+
/safari-(web-)?extension:\/\//i,
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
/** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against the message. */
|
|
68
|
+
export function isBrowserNoise(
|
|
69
|
+
event: SentryEventLike,
|
|
70
|
+
extra: readonly RegExp[] = [],
|
|
71
|
+
): boolean {
|
|
72
|
+
const message = event.exception?.values?.[0]?.value ?? "";
|
|
73
|
+
return [NETWORK_ERROR, EXTENSION_ERROR, USER_REJECTION, ...extra].some((re) =>
|
|
74
|
+
re.test(message),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
|
|
79
|
+
export function validDsn(dsn: string | null | undefined): string | undefined {
|
|
80
|
+
const value = dsn?.trim();
|
|
81
|
+
return value?.startsWith("https://") ? value : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function chain(...steps: (BeforeSend | undefined)[]): BeforeSend {
|
|
85
|
+
return (event, hint) => {
|
|
86
|
+
let current = event;
|
|
87
|
+
for (const step of steps) {
|
|
88
|
+
if (!step || current === null) continue;
|
|
89
|
+
current = step(current, hint);
|
|
90
|
+
}
|
|
91
|
+
return current;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface CommonOptions {
|
|
96
|
+
/** Overrides the DSN read from the env. */
|
|
97
|
+
dsn?: string | undefined;
|
|
98
|
+
/** Default 0: no tracing. */
|
|
99
|
+
tracesSampleRate?: number;
|
|
100
|
+
/** Runs after the built-in filters. */
|
|
101
|
+
beforeSend?: BeforeSend;
|
|
102
|
+
/** Tags set on every event, such as `{ runtime: "worker", worker: "api" }`. */
|
|
103
|
+
tags?: Record<string, string>;
|
|
104
|
+
release?: string;
|
|
105
|
+
environment?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface WorkerSentryOptions {
|
|
109
|
+
dsn: string | undefined;
|
|
110
|
+
enabled: boolean;
|
|
111
|
+
tracesSampleRate: number;
|
|
112
|
+
beforeSend: BeforeSend;
|
|
113
|
+
beforeSendTransaction: BeforeSend;
|
|
114
|
+
initialScope?: { tags: Record<string, string> };
|
|
115
|
+
release?: string;
|
|
116
|
+
environment?: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Options for `withSentry`, `instrumentWorkflowWithSentry` and
|
|
121
|
+
* `instrumentDurableObjectWithSentry`. Reads `SENTRY_DSN` from the env;
|
|
122
|
+
* spread the result and add anything app-specific (integrations, samplers).
|
|
123
|
+
*/
|
|
124
|
+
export function workerSentryOptions(
|
|
125
|
+
env: { SENTRY_DSN?: string | undefined },
|
|
126
|
+
options: CommonOptions = {},
|
|
127
|
+
): WorkerSentryOptions {
|
|
128
|
+
const dsn = validDsn(options.dsn ?? env.SENTRY_DSN);
|
|
129
|
+
const out: WorkerSentryOptions = {
|
|
130
|
+
dsn,
|
|
131
|
+
enabled: dsn !== undefined,
|
|
132
|
+
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
133
|
+
beforeSend: chain(dropLocalEvents, options.beforeSend),
|
|
134
|
+
beforeSendTransaction: dropLocalEvents,
|
|
135
|
+
};
|
|
136
|
+
if (options.tags) out.initialScope = { tags: options.tags };
|
|
137
|
+
if (options.release) out.release = options.release;
|
|
138
|
+
if (options.environment) out.environment = options.environment;
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface BrowserSentryOptions extends WorkerSentryOptions {
|
|
143
|
+
denyUrls: RegExp[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Options for a browser SDK's `Sentry.init`. Pass the DSN (from the build),
|
|
148
|
+
* and `enabled: false` for dev builds, which the browser cannot tell from
|
|
149
|
+
* the env.
|
|
150
|
+
*/
|
|
151
|
+
export function browserSentryOptions(
|
|
152
|
+
options: CommonOptions & {
|
|
153
|
+
enabled?: boolean;
|
|
154
|
+
/** More script URLs to ignore. */
|
|
155
|
+
denyUrls?: readonly RegExp[];
|
|
156
|
+
/** More messages that are noise. */
|
|
157
|
+
noise?: readonly RegExp[];
|
|
158
|
+
} = {},
|
|
159
|
+
): BrowserSentryOptions {
|
|
160
|
+
const dsn = validDsn(options.dsn);
|
|
161
|
+
const noise = options.noise ?? [];
|
|
162
|
+
const out: BrowserSentryOptions = {
|
|
163
|
+
dsn,
|
|
164
|
+
enabled: dsn !== undefined && options.enabled !== false,
|
|
165
|
+
tracesSampleRate: options.tracesSampleRate ?? 0,
|
|
166
|
+
denyUrls: [...BROWSER_DENY_URLS, ...(options.denyUrls ?? [])],
|
|
167
|
+
beforeSend: chain(
|
|
168
|
+
(event) =>
|
|
169
|
+
isLocalEvent(event) || isBrowserNoise(event, noise) ? null : event,
|
|
170
|
+
options.beforeSend,
|
|
171
|
+
),
|
|
172
|
+
beforeSendTransaction: dropLocalEvents,
|
|
173
|
+
};
|
|
174
|
+
if (options.tags) out.initialScope = { tags: options.tags };
|
|
175
|
+
if (options.release) out.release = options.release;
|
|
176
|
+
if (options.environment) out.environment = options.environment;
|
|
177
|
+
return out;
|
|
178
|
+
}
|