@react-consulting/rc-kit 0.0.0-stage → 0.13.1

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.
Files changed (337) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -2
  3. package/devkit/README.md +301 -0
  4. package/devkit/cli.test.ts +211 -0
  5. package/devkit/cli.ts +120 -0
  6. package/devkit/commands/build.ts +127 -0
  7. package/devkit/commands/checks.ts +123 -0
  8. package/devkit/commands/context.ts +47 -0
  9. package/devkit/commands/db.ts +154 -0
  10. package/devkit/commands/deploy.ts +46 -0
  11. package/devkit/commands/dev.ts +191 -0
  12. package/devkit/commands/doctor.ts +25 -0
  13. package/devkit/commands/kit.ts +45 -0
  14. package/devkit/commands/new.ts +81 -0
  15. package/devkit/commands/registry.ts +20 -0
  16. package/devkit/commands/secrets.ts +254 -0
  17. package/devkit/lib/args.ts +65 -0
  18. package/devkit/lib/audit.test.ts +405 -0
  19. package/devkit/lib/audit.ts +813 -0
  20. package/devkit/lib/config.test.ts +203 -0
  21. package/devkit/lib/config.ts +417 -0
  22. package/devkit/lib/database/index.test.ts +223 -0
  23. package/devkit/lib/database/index.ts +314 -0
  24. package/devkit/lib/database/neon.test.ts +233 -0
  25. package/devkit/lib/database/neon.ts +357 -0
  26. package/devkit/lib/database/prepare.ts +54 -0
  27. package/devkit/lib/deploy-config.test.ts +217 -0
  28. package/devkit/lib/deploy-config.ts +133 -0
  29. package/devkit/lib/deploy.test.ts +275 -0
  30. package/devkit/lib/deploy.ts +177 -0
  31. package/devkit/lib/dev-gateway.test.ts +128 -0
  32. package/devkit/lib/dev-gateway.ts +162 -0
  33. package/devkit/lib/dev-menu.test.ts +114 -0
  34. package/devkit/lib/dev-menu.ts +332 -0
  35. package/devkit/lib/dev-secrets.test.ts +156 -0
  36. package/devkit/lib/dev-secrets.ts +97 -0
  37. package/devkit/lib/dev.test.ts +194 -0
  38. package/devkit/lib/dev.ts +119 -0
  39. package/devkit/lib/doctor.test.ts +144 -0
  40. package/devkit/lib/doctor.ts +264 -0
  41. package/devkit/lib/env-file.test.ts +102 -0
  42. package/devkit/lib/env-file.ts +96 -0
  43. package/devkit/lib/glob.ts +31 -0
  44. package/devkit/lib/infisical.test.ts +208 -0
  45. package/devkit/lib/infisical.ts +139 -0
  46. package/devkit/lib/json-schema.ts +220 -0
  47. package/devkit/lib/jsonc.test.ts +53 -0
  48. package/devkit/lib/jsonc.ts +93 -0
  49. package/devkit/lib/kit-migrate.test.ts +215 -0
  50. package/devkit/lib/kit-migrate.ts +624 -0
  51. package/devkit/lib/kit-package.test.ts +83 -0
  52. package/devkit/lib/kit-package.ts +57 -0
  53. package/devkit/lib/kit-version.ts +22 -0
  54. package/devkit/lib/naming.test.ts +547 -0
  55. package/devkit/lib/naming.ts +796 -0
  56. package/devkit/lib/paths.ts +7 -0
  57. package/devkit/lib/process.ts +78 -0
  58. package/devkit/lib/project-checks.ts +86 -0
  59. package/devkit/lib/scaffold.test.ts +302 -0
  60. package/devkit/lib/scaffold.ts +315 -0
  61. package/devkit/lib/secrets.test.ts +101 -0
  62. package/devkit/lib/secrets.ts +125 -0
  63. package/devkit/lib/sourcemaps.test.ts +86 -0
  64. package/devkit/lib/sourcemaps.ts +98 -0
  65. package/devkit/lib/test-utils.ts +68 -0
  66. package/devkit/lib/workspace.ts +87 -0
  67. package/devkit/lib/wrangler.ts +231 -0
  68. package/docs/rc-config.md +93 -0
  69. package/package.json +86 -4
  70. package/presets/build.d.ts +15 -0
  71. package/presets/build.js +53 -0
  72. package/presets/vite-plus.d.ts +28 -0
  73. package/presets/vite-plus.js +153 -0
  74. package/registry/README.md +294 -0
  75. package/registry/public/r/avatar-stack.json +18 -0
  76. package/registry/public/r/avatar.json +20 -0
  77. package/registry/public/r/back-link.json +21 -0
  78. package/registry/public/r/badge.json +17 -0
  79. package/registry/public/r/button.json +18 -0
  80. package/registry/public/r/calendar-days.json +14 -0
  81. package/registry/public/r/calendar-popover.json +24 -0
  82. package/registry/public/r/calendar.json +23 -0
  83. package/registry/public/r/callout.json +20 -0
  84. package/registry/public/r/card.json +18 -0
  85. package/registry/public/r/character-counter.json +17 -0
  86. package/registry/public/r/checkbox.json +22 -0
  87. package/registry/public/r/choice-cards.json +22 -0
  88. package/registry/public/r/color-tag.json +17 -0
  89. package/registry/public/r/combobox.json +24 -0
  90. package/registry/public/r/confirm-button.json +24 -0
  91. package/registry/public/r/confirm-dialog.json +23 -0
  92. package/registry/public/r/copy-button.json +24 -0
  93. package/registry/public/r/date-picker.json +24 -0
  94. package/registry/public/r/date-range-picker.json +28 -0
  95. package/registry/public/r/detail-header.json +17 -0
  96. package/registry/public/r/detail-row.json +17 -0
  97. package/registry/public/r/dev-inbox-api.json +33 -0
  98. package/registry/public/r/dev-toolbar.json +109 -0
  99. package/registry/public/r/dev-tools-contract.json +15 -0
  100. package/registry/public/r/dialog.json +23 -0
  101. package/registry/public/r/empty-state.json +20 -0
  102. package/registry/public/r/error-state.json +23 -0
  103. package/registry/public/r/eyebrow.json +17 -0
  104. package/registry/public/r/fab.json +20 -0
  105. package/registry/public/r/field-error.json +17 -0
  106. package/registry/public/r/field-grid.json +17 -0
  107. package/registry/public/r/field.json +19 -0
  108. package/registry/public/r/file-picker.json +22 -0
  109. package/registry/public/r/filter-chips.json +17 -0
  110. package/registry/public/r/form-section.json +17 -0
  111. package/registry/public/r/form-sheet.json +22 -0
  112. package/registry/public/r/icon-button.json +23 -0
  113. package/registry/public/r/input.json +18 -0
  114. package/registry/public/r/label.json +18 -0
  115. package/registry/public/r/list-row.json +17 -0
  116. package/registry/public/r/loading-state.json +18 -0
  117. package/registry/public/r/localized-field.json +23 -0
  118. package/registry/public/r/markdown-text.json +17 -0
  119. package/registry/public/r/menu.json +21 -0
  120. package/registry/public/r/money-input.json +17 -0
  121. package/registry/public/r/month-day-select.json +18 -0
  122. package/registry/public/r/numbered-step.json +20 -0
  123. package/registry/public/r/page-actions.json +14 -0
  124. package/registry/public/r/page-header.json +17 -0
  125. package/registry/public/r/pending.json +17 -0
  126. package/registry/public/r/person-option.json +21 -0
  127. package/registry/public/r/popover.json +20 -0
  128. package/registry/public/r/progress-bar.json +17 -0
  129. package/registry/public/r/query-view.json +22 -0
  130. package/registry/public/r/registry.json +1352 -0
  131. package/registry/public/r/retry-button.json +21 -0
  132. package/registry/public/r/rich-text-editor.json +32 -0
  133. package/registry/public/r/save-bar.json +22 -0
  134. package/registry/public/r/search-input.json +23 -0
  135. package/registry/public/r/search-select.json +24 -0
  136. package/registry/public/r/section-tabs.json +17 -0
  137. package/registry/public/r/segmented.json +18 -0
  138. package/registry/public/r/select.json +23 -0
  139. package/registry/public/r/sheet.json +24 -0
  140. package/registry/public/r/skeleton.json +17 -0
  141. package/registry/public/r/spinner.json +20 -0
  142. package/registry/public/r/stale-notice.json +23 -0
  143. package/registry/public/r/stat-tile.json +23 -0
  144. package/registry/public/r/stepper.json +23 -0
  145. package/registry/public/r/suggestion-strip.json +21 -0
  146. package/registry/public/r/swatch-picker.json +22 -0
  147. package/registry/public/r/switch.json +21 -0
  148. package/registry/public/r/tabs.json +20 -0
  149. package/registry/public/r/textarea.json +18 -0
  150. package/registry/public/r/theme.json +15 -0
  151. package/registry/public/r/timezone-select.json +19 -0
  152. package/registry/public/r/toaster.json +21 -0
  153. package/registry/public/r/tooltip.json +17 -0
  154. package/registry/public/r/ui-labels.json +14 -0
  155. package/registry/public/r/use-copy-to-clipboard.json +20 -0
  156. package/registry/public/r/use-online.json +14 -0
  157. package/registry/public/r/use-stale-queries.json +17 -0
  158. package/registry/public/r/utils.json +18 -0
  159. package/registry/public/r/weekday-picker.json +19 -0
  160. package/registry/public/r/yes-no.json +20 -0
  161. package/registry/serve.ts +29 -0
  162. package/rules/architecture.md +47 -0
  163. package/rules/cloudflare.md +74 -0
  164. package/rules/code.md +12 -0
  165. package/rules/git.md +10 -0
  166. package/rules/index.md +14 -0
  167. package/rules/observability.md +13 -0
  168. package/rules/performance.md +39 -0
  169. package/rules/runtime.md +9 -0
  170. package/rules/secrets.md +11 -0
  171. package/rules/stack.md +33 -0
  172. package/rules/ui.md +23 -0
  173. package/runtime/ai.test.ts +106 -0
  174. package/runtime/ai.ts +125 -0
  175. package/runtime/observability/allowlists.ts +42 -0
  176. package/runtime/observability/anonymize.test.ts +111 -0
  177. package/runtime/observability/anonymize.ts +249 -0
  178. package/runtime/observability/browser.test.ts +46 -0
  179. package/runtime/observability/browser.ts +126 -0
  180. package/runtime/observability/server.test.ts +84 -0
  181. package/runtime/observability/server.ts +152 -0
  182. package/runtime/observability/tunnel.test.ts +276 -0
  183. package/runtime/observability/tunnel.ts +360 -0
  184. package/runtime/observability/user-agent.ts +51 -0
  185. package/schema/rc.config.schema.json +333 -0
  186. package/template/.gitattributes +1 -0
  187. package/template/.github/workflows/ci.yml +64 -0
  188. package/template/CLAUDE.md +48 -0
  189. package/template/README.md +50 -0
  190. package/template/apps/admin/.dev.vars.example +5 -0
  191. package/template/apps/admin/components.json +24 -0
  192. package/template/apps/admin/package.json +48 -0
  193. package/template/apps/admin/public/favicon.svg +1 -0
  194. package/template/apps/admin/src/components/app-shell.tsx +41 -0
  195. package/template/apps/admin/src/components/dev-sign-in-links.tsx +57 -0
  196. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-context.ts +13 -0
  197. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-environment-panel.tsx +105 -0
  198. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-list.tsx +54 -0
  199. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-message.tsx +104 -0
  200. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-panel.tsx +130 -0
  201. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-launcher.tsx +45 -0
  202. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-panel.tsx +90 -0
  203. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-types.ts +11 -0
  204. package/template/apps/admin/src/components/dev-toolbar/dev-toolbar.tsx +61 -0
  205. package/template/apps/admin/src/components/dev-toolbar/development-devtools.tsx +32 -0
  206. package/template/apps/admin/src/components/login-form.tsx +115 -0
  207. package/template/apps/admin/src/components/login-sent.tsx +23 -0
  208. package/template/apps/admin/src/components/message-list.tsx +44 -0
  209. package/template/apps/admin/src/components/turnstile-challenge.tsx +140 -0
  210. package/template/apps/admin/src/components/ui/button.tsx +87 -0
  211. package/template/apps/admin/src/components/ui/callout.tsx +84 -0
  212. package/template/apps/admin/src/components/ui/card.tsx +48 -0
  213. package/template/apps/admin/src/components/ui/eyebrow.tsx +33 -0
  214. package/template/apps/admin/src/components/ui/field-error.tsx +24 -0
  215. package/template/apps/admin/src/components/ui/field.tsx +100 -0
  216. package/template/apps/admin/src/components/ui/input.tsx +73 -0
  217. package/template/apps/admin/src/components/ui/label.tsx +39 -0
  218. package/template/apps/admin/src/components/ui/skeleton.tsx +225 -0
  219. package/template/apps/admin/src/components/ui/spinner.tsx +12 -0
  220. package/template/apps/admin/src/components/ui/toaster.tsx +33 -0
  221. package/template/apps/admin/src/components/ui/tooltip.tsx +51 -0
  222. package/template/apps/admin/src/hooks/use-dev-inbox.ts +25 -0
  223. package/template/apps/admin/src/hooks/use-dev-toolbar-state.ts +48 -0
  224. package/template/apps/admin/src/lib/api.ts +39 -0
  225. package/template/apps/admin/src/lib/auth-client.ts +7 -0
  226. package/template/apps/admin/src/lib/dev-inbox-links.ts +66 -0
  227. package/template/apps/admin/src/lib/dev-tools-client.ts +54 -0
  228. package/template/apps/admin/src/lib/dev-tools-contract.ts +52 -0
  229. package/template/apps/admin/src/lib/messages.ts +5 -0
  230. package/template/apps/admin/src/lib/query-keys.ts +3 -0
  231. package/template/apps/admin/src/lib/security-headers.ts +18 -0
  232. package/template/apps/admin/src/lib/session.ts +27 -0
  233. package/template/apps/admin/src/lib/site-url.ts +7 -0
  234. package/template/apps/admin/src/lib/turnstile.ts +70 -0
  235. package/template/apps/admin/src/lib/ui-labels.tsx +182 -0
  236. package/template/apps/admin/src/lib/utils.ts +6 -0
  237. package/template/apps/admin/src/routeTree.gen.ts +109 -0
  238. package/template/apps/admin/src/router.tsx +29 -0
  239. package/template/apps/admin/src/routes/__root.tsx +56 -0
  240. package/template/apps/admin/src/routes/_app/index.tsx +38 -0
  241. package/template/apps/admin/src/routes/_app.tsx +26 -0
  242. package/template/apps/admin/src/routes/login.tsx +48 -0
  243. package/template/apps/admin/src/styles/app.css +11 -0
  244. package/template/apps/admin/src/worker.ts +88 -0
  245. package/template/apps/admin/tsconfig.json +19 -0
  246. package/template/apps/admin/vite.config.ts +69 -0
  247. package/template/apps/admin/wrangler.jsonc +92 -0
  248. package/template/apps/web/.env.example +1 -0
  249. package/template/apps/web/astro.config.ts +46 -0
  250. package/template/apps/web/package.json +33 -0
  251. package/template/apps/web/public/favicon.svg +1 -0
  252. package/template/apps/web/src/components/ContactForm.astro +78 -0
  253. package/template/apps/web/src/env.d.ts +11 -0
  254. package/template/apps/web/src/layouts/BaseLayout.astro +32 -0
  255. package/template/apps/web/src/lib/api.ts +8 -0
  256. package/template/apps/web/src/pages/api/messages.ts +32 -0
  257. package/template/apps/web/src/pages/index.astro +26 -0
  258. package/template/apps/web/src/pages/media/[...key].ts +4 -0
  259. package/template/apps/web/src/scripts/contact-form.ts +105 -0
  260. package/template/apps/web/src/scripts/error-reporting.ts +4 -0
  261. package/template/apps/web/src/scripts/turnstile.ts +177 -0
  262. package/template/apps/web/src/styles/global.css +11 -0
  263. package/template/apps/web/src/worker.ts +34 -0
  264. package/template/apps/web/tsconfig.json +11 -0
  265. package/template/apps/web/wrangler.jsonc +45 -0
  266. package/template/bun.lock +1925 -0
  267. package/template/bunfig.toml +2 -0
  268. package/template/gitignore +23 -0
  269. package/template/package.json +74 -0
  270. package/template/packages/api/components.json +21 -0
  271. package/template/packages/api/package.json +30 -0
  272. package/template/packages/api/src/client.ts +13 -0
  273. package/template/packages/api/src/cloudflare-env.d.ts +26 -0
  274. package/template/packages/api/src/emails/escape-html.ts +13 -0
  275. package/template/packages/api/src/emails/layout.ts +22 -0
  276. package/template/packages/api/src/emails/magic-link.ts +32 -0
  277. package/template/packages/api/src/emails/new-message.ts +32 -0
  278. package/template/packages/api/src/emails/send.ts +14 -0
  279. package/template/packages/api/src/lib/auth-guard.ts +16 -0
  280. package/template/packages/api/src/lib/auth.ts +108 -0
  281. package/template/packages/api/src/lib/context.ts +7 -0
  282. package/template/packages/api/src/lib/dev-inbox/routes.ts +77 -0
  283. package/template/packages/api/src/lib/dev-inbox/send.ts +57 -0
  284. package/template/packages/api/src/lib/dev-inbox/store.ts +185 -0
  285. package/template/packages/api/src/lib/dev-inbox.ts +6 -0
  286. package/template/packages/api/src/lib/dev-tools-contract.ts +52 -0
  287. package/template/packages/api/src/lib/environment.ts +17 -0
  288. package/template/packages/api/src/lib/turnstile.ts +33 -0
  289. package/template/packages/api/src/media.ts +45 -0
  290. package/template/packages/api/src/routes/admin-messages.ts +21 -0
  291. package/template/packages/api/src/routes/dev.ts +31 -0
  292. package/template/packages/api/src/routes/health.ts +7 -0
  293. package/template/packages/api/src/routes/messages.ts +58 -0
  294. package/template/packages/api/src/schemas.ts +18 -0
  295. package/template/packages/api/src/server.ts +29 -0
  296. package/template/packages/api/tsconfig.json +10 -0
  297. package/template/packages/db/migrations/0001_init.sql +79 -0
  298. package/template/packages/db/package.json +34 -0
  299. package/template/packages/db/prisma/schema.prisma +95 -0
  300. package/template/packages/db/prisma.config.ts +9 -0
  301. package/template/packages/db/scripts/migrate.ts +15 -0
  302. package/template/packages/db/scripts/new-migration.ts +185 -0
  303. package/template/packages/db/scripts/reset.ts +54 -0
  304. package/template/packages/db/scripts/seed.ts +44 -0
  305. package/template/packages/db/scripts/wrangler.ts +35 -0
  306. package/template/packages/db/src/d1.ts +7 -0
  307. package/template/packages/db/src/migration-guard.test.ts +43 -0
  308. package/template/packages/db/src/migration-guard.ts +51 -0
  309. package/template/packages/db/src/new-migration.test.ts +16 -0
  310. package/template/packages/db/src/scope.ts +38 -0
  311. package/template/packages/db/src/seed.ts +30 -0
  312. package/template/packages/db/src/sql.ts +42 -0
  313. package/template/packages/db/tsconfig.json +8 -0
  314. package/template/packages/shared/components.json +21 -0
  315. package/template/packages/shared/package.json +33 -0
  316. package/template/packages/shared/src/edge/origins.ts +20 -0
  317. package/template/packages/shared/src/edge/turnstile.ts +11 -0
  318. package/template/packages/shared/src/locales.ts +13 -0
  319. package/template/packages/shared/src/message-rules.ts +16 -0
  320. package/template/packages/shared/src/messages.test.ts +26 -0
  321. package/template/packages/shared/src/messages.ts +30 -0
  322. package/template/packages/shared/src/observability/build-release.ts +9 -0
  323. package/template/packages/shared/src/observability/error-reporting.ts +31 -0
  324. package/template/packages/shared/src/observability/error-tunnel.ts +20 -0
  325. package/template/packages/shared/src/observability/sentry-browser.ts +1 -0
  326. package/template/packages/shared/src/observability/server-errors.ts +74 -0
  327. package/template/packages/shared/src/project.ts +1 -0
  328. package/template/packages/shared/src/styles/rc-theme.css +138 -0
  329. package/template/packages/shared/src/styles/theme.css +9 -0
  330. package/template/packages/shared/tsconfig.json +10 -0
  331. package/template/rc.config.json +25 -0
  332. package/template/scripts/bootstrap-local.ts +42 -0
  333. package/template/tsconfig.base.json +20 -0
  334. package/template/tsconfig.json +8 -0
  335. package/template/tsconfig.worker.json +7 -0
  336. package/template/turbo.json +40 -0
  337. package/template/vite.config.ts +8 -0
@@ -0,0 +1,126 @@
1
+ import {
2
+ anonymizeEvent,
3
+ anonymizePath,
4
+ createAnonymousSessionId,
5
+ type TAnonymizableEvent,
6
+ } from "./anonymize"
7
+ import { CLIENT_ERROR_CONTEXT_KEYS, CLIENT_ERROR_TAG_KEYS } from "./allowlists"
8
+ import { ERROR_TUNNEL_PATH } from "./tunnel"
9
+
10
+ interface TIntegration {
11
+ name: string
12
+ }
13
+
14
+ interface TEventHint {
15
+ originalException?: unknown
16
+ }
17
+
18
+ export interface TBrowserErrorOptionsInput {
19
+ dsn: string
20
+ front: string
21
+ isDiscarded?: (hint: TEventHint) => boolean
22
+ release?: string | null
23
+ tunnel?: string
24
+ }
25
+
26
+ // Chrome reports a ResizeObserver callback that resizes its own target as an uncaught error; it is benign.
27
+ const BENIGN_ERROR_PREFIXES = ["ResizeObserver loop"] as const
28
+
29
+ const DROPPED_INTEGRATIONS = new Set([
30
+ "Breadcrumbs",
31
+ "BrowserSession",
32
+ "Console",
33
+ "ConversationId",
34
+ ])
35
+
36
+ function isBenignMessage(value: unknown): boolean {
37
+ return (
38
+ typeof value === "string" &&
39
+ BENIGN_ERROR_PREFIXES.some((prefix) => value.startsWith(prefix))
40
+ )
41
+ }
42
+
43
+ export function isApiRefusal(error: unknown): boolean {
44
+ if (typeof error !== "object" || error === null || !("status" in error)) {
45
+ return false
46
+ }
47
+
48
+ const { status } = error as { status: unknown }
49
+
50
+ return typeof status === "number" && status >= 400 && status < 500
51
+ }
52
+
53
+ function isBenignEvent(event: TAnonymizableEvent): boolean {
54
+ const messages = [
55
+ event.message,
56
+ ...(event.exception?.values ?? []).map((exception) => exception.value),
57
+ ]
58
+
59
+ return messages.some(isBenignMessage)
60
+ }
61
+
62
+ function anonymizeBrowserEvent<TEvent extends TAnonymizableEvent>(
63
+ event: TEvent,
64
+ sessionId: string
65
+ ): TEvent {
66
+ const pathname = event.extra?.pathname
67
+
68
+ if (typeof pathname === "string") {
69
+ event.extra = { ...event.extra, pathname: anonymizePath(pathname) }
70
+ }
71
+
72
+ return anonymizeEvent(event, {
73
+ allowedContextKeys: CLIENT_ERROR_CONTEXT_KEYS,
74
+ allowedTagKeys: CLIENT_ERROR_TAG_KEYS,
75
+ sessionId,
76
+ })
77
+ }
78
+
79
+ // Plain options for `init` of @sentry/browser (or @sentry/react): reports go through the same-origin tunnel.
80
+ export function browserErrorOptions({
81
+ dsn,
82
+ front,
83
+ isDiscarded,
84
+ release,
85
+ tunnel = ERROR_TUNNEL_PATH,
86
+ }: TBrowserErrorOptionsInput) {
87
+ const sessionId = createAnonymousSessionId()
88
+
89
+ return {
90
+ beforeBreadcrumb: () => null,
91
+ beforeSend: <TEvent extends TAnonymizableEvent>(
92
+ event: TEvent,
93
+ hint: TEventHint
94
+ ): TEvent | null => {
95
+ if (
96
+ isApiRefusal(hint.originalException) ||
97
+ isDiscarded?.(hint) ||
98
+ isBenignEvent(event)
99
+ ) {
100
+ return null
101
+ }
102
+
103
+ return anonymizeBrowserEvent(event, sessionId)
104
+ },
105
+ dataCollection: {
106
+ cookies: false,
107
+ httpBodies: [],
108
+ httpHeaders: false,
109
+ urlQueryParams: false,
110
+ userInfo: false,
111
+ },
112
+ dsn,
113
+ environment: "production",
114
+ initialScope: {
115
+ tags: { front },
116
+ user: { id: sessionId },
117
+ },
118
+ integrations: <TItem extends TIntegration>(defaults: TItem[]) =>
119
+ defaults.filter(
120
+ (integration) => !DROPPED_INTEGRATIONS.has(integration.name)
121
+ ),
122
+ maxBreadcrumbs: 0,
123
+ ...(release ? { release } : {}),
124
+ tunnel,
125
+ }
126
+ }
@@ -0,0 +1,84 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import type { TAnonymizableEvent } from "./anonymize"
3
+ import { reportableContext, serverErrorOptions } from "./server"
4
+
5
+ const DSN = "https://public@o1.ingest.de.sentry.io/2"
6
+
7
+ describe("serverErrorOptions", () => {
8
+ test("is enabled only in production with a DSN", () => {
9
+ expect(
10
+ serverErrorOptions({
11
+ env: { ENVIRONMENT: "production", SENTRY_DSN: DSN },
12
+ service: "tpl-admin",
13
+ }).enabled
14
+ ).toBe(true)
15
+ expect(
16
+ serverErrorOptions({
17
+ env: { ENVIRONMENT: "local", SENTRY_DSN: DSN },
18
+ service: "tpl-admin",
19
+ }).enabled
20
+ ).toBe(false)
21
+ expect(
22
+ serverErrorOptions({
23
+ env: { ENVIRONMENT: "production", SENTRY_DSN: "" },
24
+ service: "tpl-admin",
25
+ }).enabled
26
+ ).toBe(false)
27
+ })
28
+
29
+ test("tags the service, sets the release and drops console and fetch breadcrumbs", () => {
30
+ const options = serverErrorOptions({
31
+ env: { ENVIRONMENT: "production", SENTRY_DSN: DSN },
32
+ release: "abc123",
33
+ service: "tpl-admin",
34
+ })
35
+ const kept = options.integrations([
36
+ { name: "Console" },
37
+ { name: "Fetch" },
38
+ { name: "LinkedErrors" },
39
+ ])
40
+
41
+ expect(options.initialScope).toEqual({ tags: { service: "tpl-admin" } })
42
+ expect(options.release).toBe("abc123")
43
+ expect(kept).toEqual([{ name: "LinkedErrors" }])
44
+ })
45
+
46
+ test("scrubs events with the server allowlists plus the project's own keys", () => {
47
+ const options = serverErrorOptions({
48
+ allowlists: { tagKeys: ["listing_kind"] },
49
+ env: { ENVIRONMENT: "production", SENTRY_DSN: DSN },
50
+ service: "tpl-admin",
51
+ })
52
+ const raw: TAnonymizableEvent = {
53
+ message: "failed for jane@example.com",
54
+ request: {
55
+ headers: { cookie: "x" },
56
+ url: "https://example.com/u/42?q=1",
57
+ },
58
+ tags: { account_id: "a1", listing_kind: "villa", service: "tpl-admin" },
59
+ user: { email: "jane@example.com" },
60
+ }
61
+ const event = options.beforeSend(raw)
62
+
63
+ expect(event.message).toBe("failed for [email]")
64
+ expect(event.request).toEqual({ url: "https://example.com/u/:id" })
65
+ expect(event.tags).toEqual({ listing_kind: "villa", service: "tpl-admin" })
66
+ expect(event.user).toEqual({})
67
+ })
68
+ })
69
+
70
+ describe("reportableContext", () => {
71
+ test("keeps allowlisted keys and templates paths", () => {
72
+ expect(
73
+ reportableContext({
74
+ accountId: "acc_1",
75
+ job_name: "nightly",
76
+ pathname: "/clients/123/edit",
77
+ status: 500,
78
+ })
79
+ ).toEqual({
80
+ extra: { pathname: "/clients/:id/edit", status: 500 },
81
+ tags: { job_name: "nightly" },
82
+ })
83
+ })
84
+ })
@@ -0,0 +1,152 @@
1
+ import {
2
+ anonymizeEvent,
3
+ anonymizePath,
4
+ type TAnonymizableEvent,
5
+ } from "./anonymize"
6
+ import {
7
+ SERVER_ERROR_CONTEXT_KEYS,
8
+ SERVER_ERROR_TAG_KEYS,
9
+ withKeys,
10
+ } from "./allowlists"
11
+
12
+ export interface TServerErrorEnv {
13
+ ENVIRONMENT?: string
14
+ SENTRY_DSN?: string
15
+ }
16
+
17
+ export interface TServerAllowlists {
18
+ contextKeys?: Iterable<string>
19
+ tagKeys?: Iterable<string>
20
+ }
21
+
22
+ interface TIntegration {
23
+ name: string
24
+ }
25
+
26
+ export interface TServerErrorOptionsInput {
27
+ allowlists?: TServerAllowlists
28
+ env: unknown
29
+ release?: string | null
30
+ service: string
31
+ }
32
+
33
+ // Console lines and outgoing URLs (presigned keys, provider calls) stay out of breadcrumbs.
34
+ const DROPPED_INTEGRATIONS = new Set(["Console", "Fetch"])
35
+
36
+ const PATH_CONTEXT_KEYS = new Set(["pathname", "route"])
37
+
38
+ export const PRIVATE_SERVER_DATA_COLLECTION: {
39
+ cookies: false
40
+ databaseQueryData: false
41
+ genAI: { inputs: false; outputs: false }
42
+ graphQL: { document: false; variables: false }
43
+ httpBodies: never[]
44
+ httpHeaders: false
45
+ queues: false
46
+ stackFrameVariables: false
47
+ urlQueryParams: false
48
+ userInfo: false
49
+ } = {
50
+ cookies: false,
51
+ databaseQueryData: false,
52
+ genAI: { inputs: false, outputs: false },
53
+ graphQL: { document: false, variables: false },
54
+ httpBodies: [],
55
+ httpHeaders: false,
56
+ queues: false,
57
+ stackFrameVariables: false,
58
+ urlQueryParams: false,
59
+ userInfo: false,
60
+ }
61
+
62
+ function serverKeys(allowlists: TServerAllowlists = {}) {
63
+ return {
64
+ contextKeys: withKeys(SERVER_ERROR_CONTEXT_KEYS, allowlists.contextKeys),
65
+ tagKeys: withKeys(SERVER_ERROR_TAG_KEYS, allowlists.tagKeys),
66
+ }
67
+ }
68
+
69
+ export function scrubServerEvent<TEvent extends TAnonymizableEvent>(
70
+ event: TEvent,
71
+ allowlists?: TServerAllowlists
72
+ ): TEvent {
73
+ const { contextKeys, tagKeys } = serverKeys(allowlists)
74
+
75
+ return anonymizeEvent(event, {
76
+ allowedContextKeys: contextKeys,
77
+ allowedTagKeys: tagKeys,
78
+ sessionId: null,
79
+ })
80
+ }
81
+
82
+ export function isServerErrorTrackingEnabled(env: unknown): boolean {
83
+ const { ENVIRONMENT, SENTRY_DSN } = (env ?? {}) as TServerErrorEnv
84
+
85
+ return ENVIRONMENT === "production" && Boolean(SENTRY_DSN)
86
+ }
87
+
88
+ // Plain options for `withSentry` of @sentry/cloudflare: the kit stays free of SDK dependencies.
89
+ export function serverErrorOptions({
90
+ allowlists,
91
+ env,
92
+ release,
93
+ service,
94
+ }: TServerErrorOptionsInput) {
95
+ const { ENVIRONMENT: environment, SENTRY_DSN: dsn } = (env ??
96
+ {}) as TServerErrorEnv
97
+
98
+ return {
99
+ beforeSend: <TEvent extends TAnonymizableEvent>(event: TEvent) =>
100
+ scrubServerEvent(event, allowlists),
101
+ dataCollection: PRIVATE_SERVER_DATA_COLLECTION,
102
+ dsn: dsn || undefined,
103
+ enabled: isServerErrorTrackingEnabled(env),
104
+ environment,
105
+ initialScope: { tags: { service } },
106
+ integrations: <TItem extends TIntegration>(defaults: TItem[]) =>
107
+ defaults.filter(
108
+ (integration) => !DROPPED_INTEGRATIONS.has(integration.name)
109
+ ),
110
+ ...(release ? { release } : {}),
111
+ }
112
+ }
113
+
114
+ export interface TReportableContext {
115
+ extra: Record<string, unknown>
116
+ tags: Record<string, string | number | boolean>
117
+ }
118
+
119
+ function isTagValue(value: unknown): value is string | number | boolean {
120
+ return (
121
+ typeof value === "string" ||
122
+ typeof value === "number" ||
123
+ typeof value === "boolean"
124
+ )
125
+ }
126
+
127
+ // Only allowlisted keys travel; a path is templated before it becomes context.
128
+ export function reportableContext(
129
+ context: Record<string, unknown>,
130
+ allowlists?: TServerAllowlists
131
+ ): TReportableContext {
132
+ const { contextKeys, tagKeys } = serverKeys(allowlists)
133
+ const tags: TReportableContext["tags"] = {}
134
+ const extra: TReportableContext["extra"] = {}
135
+
136
+ for (const [key, value] of Object.entries(context)) {
137
+ if (value === undefined || value === null) {
138
+ continue
139
+ }
140
+
141
+ if (tagKeys.has(key) && isTagValue(value)) {
142
+ tags[key] = value
143
+ } else if (contextKeys.has(key)) {
144
+ extra[key] =
145
+ PATH_CONTEXT_KEYS.has(key) && typeof value === "string"
146
+ ? anonymizePath(value)
147
+ : value
148
+ }
149
+ }
150
+
151
+ return { extra, tags }
152
+ }
@@ -0,0 +1,276 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import {
3
+ handleErrorTunnel,
4
+ parseEnvelope,
5
+ parseTunnelDsns,
6
+ prepareForward,
7
+ tunnelTarget,
8
+ } from "./tunnel"
9
+
10
+ const DSN = "https://publickey@o1234.ingest.de.sentry.io/1"
11
+ const SESSION_ID = "0123456789abcdef"
12
+ const CHROME_UA =
13
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.54 Safari/537.36"
14
+
15
+ const encoder = new TextEncoder()
16
+ const decoder = new TextDecoder()
17
+
18
+ function envelope(
19
+ header: Record<string, unknown>,
20
+ items: [Record<string, unknown>, string][]
21
+ ): Uint8Array {
22
+ const lines = [JSON.stringify(header)]
23
+
24
+ for (const [itemHeader, payload] of items) {
25
+ lines.push(JSON.stringify(itemHeader), payload)
26
+ }
27
+
28
+ return encoder.encode(lines.join("\n"))
29
+ }
30
+
31
+ function errorEvent(overrides: Record<string, unknown> = {}): string {
32
+ return JSON.stringify({
33
+ breadcrumbs: [
34
+ {
35
+ category: "navigation",
36
+ message: "/dashboard/clients/cm1abcdefghijklmnopqrstu",
37
+ },
38
+ { category: "session", message: "refreshed" },
39
+ ],
40
+ exception: {
41
+ values: [{ type: "Error", value: "No member jane@example.com" }],
42
+ },
43
+ extra: { fileName: "Facture Dupont.pdf", pathname: "/dashboard" },
44
+ request: {
45
+ headers: { "User-Agent": CHROME_UA },
46
+ url: "https://www.example.com/dashboard/invoices/cm1abcdefghijklmnopqrstu?tab=lines",
47
+ },
48
+ tags: { front: "app", organization_id: "org_1" },
49
+ user: {
50
+ email: "jane@example.com",
51
+ id: SESSION_ID,
52
+ ip_address: "203.0.113.9",
53
+ },
54
+ ...overrides,
55
+ })
56
+ }
57
+
58
+ function forwardedEvents(body: Uint8Array): Record<string, unknown>[] {
59
+ const parsed = parseEnvelope(body)
60
+
61
+ return (parsed?.items ?? []).map((item) =>
62
+ JSON.parse(decoder.decode(item.payload))
63
+ )
64
+ }
65
+
66
+ describe("parseEnvelope", () => {
67
+ test("reads items delimited by newlines", () => {
68
+ const parsed = parseEnvelope(
69
+ envelope({ dsn: DSN }, [[{ type: "event" }, '{"message":"a"}']])
70
+ )
71
+
72
+ expect(parsed?.header).toEqual({ dsn: DSN })
73
+ expect(parsed?.items).toHaveLength(1)
74
+ expect(decoder.decode(parsed?.items[0]?.payload)).toBe('{"message":"a"}')
75
+ })
76
+
77
+ test("reads a payload by its declared byte length, newlines and multibyte text included", () => {
78
+ const payload = '{"message":"é\\nà"}\n'
79
+ const length = encoder.encode(payload).length
80
+ const body = encoder.encode(
81
+ `${JSON.stringify({ dsn: DSN })}\n${JSON.stringify({ type: "attachment", length })}\n${payload}\n${JSON.stringify({ type: "event" })}\n{"message":"b"}`
82
+ )
83
+
84
+ const parsed = parseEnvelope(body)
85
+
86
+ expect(parsed?.items.map((item) => item.header.type)).toEqual([
87
+ "attachment",
88
+ "event",
89
+ ])
90
+ expect(decoder.decode(parsed?.items[0]?.payload)).toBe(payload)
91
+ })
92
+
93
+ test("refuses a malformed header or a length past the end", () => {
94
+ expect(parseEnvelope(encoder.encode("not json\n{}"))).toBeNull()
95
+ expect(
96
+ parseEnvelope(
97
+ envelope({ dsn: DSN }, [[{ type: "event", length: 999 }, "{}"]])
98
+ )
99
+ ).toBeNull()
100
+ })
101
+ })
102
+
103
+ describe("tunnelTarget", () => {
104
+ test("derives the project's envelope endpoint and public key", () => {
105
+ expect(tunnelTarget(DSN)).toEqual({
106
+ publicKey: "publickey",
107
+ url: "https://o1234.ingest.de.sentry.io/api/1/envelope/",
108
+ })
109
+ })
110
+
111
+ test("refuses a DSN without key or project, or over plain http", () => {
112
+ expect(tunnelTarget("https://host.example/1")).toBeNull()
113
+ expect(tunnelTarget("https://key@host.example/")).toBeNull()
114
+ expect(tunnelTarget("http://key@host.example/1")).toBeNull()
115
+ })
116
+
117
+ test("parses the comma-separated allowlist", () => {
118
+ expect([...parseTunnelDsns(` ${DSN} ,,https://k@h/2`)]).toEqual([
119
+ DSN,
120
+ "https://k@h/2",
121
+ ])
122
+ expect(parseTunnelDsns(undefined).size).toBe(0)
123
+ })
124
+ })
125
+
126
+ describe("prepareForward", () => {
127
+ const allowedDsns = new Set([DSN])
128
+
129
+ test("relays nothing for a DSN outside the allowlist", () => {
130
+ const body = envelope({ dsn: "https://other@evil.example/9" }, [
131
+ [{ type: "event" }, errorEvent()],
132
+ ])
133
+
134
+ expect(
135
+ prepareForward({ allowedDsns, body, userAgent: CHROME_UA })
136
+ ).toBeNull()
137
+ })
138
+
139
+ test("relays nothing when the envelope holds no error event", () => {
140
+ const body = envelope({ dsn: DSN }, [
141
+ [{ type: "session" }, '{"sid":"x"}'],
142
+ [{ type: "client_report" }, "{}"],
143
+ ])
144
+
145
+ expect(
146
+ prepareForward({ allowedDsns, body, userAgent: CHROME_UA })
147
+ ).toBeNull()
148
+ })
149
+
150
+ test("keeps only the event items and rebuilds a minimal envelope header", () => {
151
+ const body = envelope(
152
+ {
153
+ dsn: DSN,
154
+ event_id: "abc",
155
+ sdk: { name: "sentry.javascript.browser", version: "10.0.0" },
156
+ trace: { transaction: "/dashboard/clients/cm1abcdefghijklmnopqrstu" },
157
+ },
158
+ [
159
+ [{ type: "session" }, '{"sid":"x"}'],
160
+ [{ type: "event" }, errorEvent()],
161
+ ]
162
+ )
163
+
164
+ const forwarded = prepareForward({
165
+ allowedDsns,
166
+ body,
167
+ userAgent: CHROME_UA,
168
+ })
169
+ const parsed = parseEnvelope(forwarded?.body ?? new Uint8Array())
170
+
171
+ expect(forwarded?.target.url).toBe(
172
+ "https://o1234.ingest.de.sentry.io/api/1/envelope/"
173
+ )
174
+ expect(parsed?.header).toEqual({
175
+ dsn: DSN,
176
+ event_id: "abc",
177
+ sdk: { name: "sentry.javascript.browser", version: "10.0.0" },
178
+ })
179
+ expect(parsed?.items.map((item) => item.header.type)).toEqual(["event"])
180
+ })
181
+
182
+ test("re-anonymizes the event and swaps the user agent for coarse contexts", () => {
183
+ const body = envelope({ dsn: DSN }, [[{ type: "event" }, errorEvent()]])
184
+
185
+ const forwarded = prepareForward({
186
+ allowedDsns,
187
+ allowlists: { keptBreadcrumbCategories: new Set(["session"]) },
188
+ body,
189
+ userAgent: CHROME_UA,
190
+ })
191
+ const [event] = forwardedEvents(forwarded?.body ?? new Uint8Array())
192
+ const sent = JSON.stringify(event)
193
+
194
+ expect(event?.user).toEqual({ id: SESSION_ID })
195
+ expect(event?.request).toEqual({
196
+ url: "https://www.example.com/dashboard/invoices/:id",
197
+ })
198
+ expect(event?.tags).toEqual({ front: "app" })
199
+ expect(event?.extra).toEqual({ pathname: "/dashboard" })
200
+ expect(event?.breadcrumbs).toEqual([
201
+ { category: "session", message: "refreshed" },
202
+ ])
203
+ expect(event?.contexts).toEqual({
204
+ browser: { name: "Chrome", version: "141" },
205
+ os: { name: "Windows", version: "10" },
206
+ })
207
+ expect(sent).not.toContain("jane@example.com")
208
+ expect(sent).not.toContain("203.0.113.9")
209
+ expect(sent).not.toContain("Dupont")
210
+ expect(sent).not.toContain("org_1")
211
+ expect(sent).not.toContain("AppleWebKit")
212
+ })
213
+
214
+ test("drops a user id that is not an anonymous session id", () => {
215
+ const body = envelope({ dsn: DSN }, [
216
+ [{ type: "event" }, errorEvent({ user: { id: "user_42" } })],
217
+ ])
218
+
219
+ const forwarded = prepareForward({ allowedDsns, body, userAgent: null })
220
+ const [event] = forwardedEvents(forwarded?.body ?? new Uint8Array())
221
+
222
+ expect(event?.user).toEqual({})
223
+ })
224
+
225
+ test("keeps the operating system the mobile SDK reported", () => {
226
+ const body = envelope({ dsn: DSN }, [
227
+ [
228
+ { type: "event" },
229
+ errorEvent({
230
+ contexts: {
231
+ device: { id: "device-uuid", model: "iPhone16,1" },
232
+ os: { name: "iOS", version: "26.0" },
233
+ },
234
+ }),
235
+ ],
236
+ ])
237
+
238
+ const forwarded = prepareForward({
239
+ allowedDsns,
240
+ body,
241
+ userAgent: "ReactBox/412 CFNetwork/3826 Darwin/25.0.0",
242
+ })
243
+ const [event] = forwardedEvents(forwarded?.body ?? new Uint8Array())
244
+
245
+ expect(event?.contexts).toEqual({
246
+ device: { model: "iPhone16,1" },
247
+ os: { name: "iOS", version: "26.0" },
248
+ })
249
+ })
250
+ })
251
+
252
+ describe("handleErrorTunnel", () => {
253
+ test("always answers 200 and forwards only when enabled", async () => {
254
+ const pending: Promise<unknown>[] = []
255
+ const request = () =>
256
+ new Request("https://example.com/api/telemetry/errors", {
257
+ body: "{}\n",
258
+ method: "POST",
259
+ })
260
+
261
+ const disabled = await handleErrorTunnel(request(), {
262
+ allowedDsns: DSN,
263
+ enabled: false,
264
+ waitUntil: (promise: Promise<unknown>) => pending.push(promise),
265
+ })
266
+ const invalid = await handleErrorTunnel(request(), {
267
+ allowedDsns: DSN,
268
+ enabled: true,
269
+ waitUntil: (promise: Promise<unknown>) => pending.push(promise),
270
+ })
271
+
272
+ expect(disabled.status).toBe(200)
273
+ expect(invalid.status).toBe(200)
274
+ expect(pending).toHaveLength(0)
275
+ })
276
+ })