@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.
- package/LICENSE +21 -0
- package/README.md +93 -2
- package/devkit/README.md +301 -0
- package/devkit/cli.test.ts +211 -0
- package/devkit/cli.ts +120 -0
- package/devkit/commands/build.ts +127 -0
- package/devkit/commands/checks.ts +123 -0
- package/devkit/commands/context.ts +47 -0
- package/devkit/commands/db.ts +154 -0
- package/devkit/commands/deploy.ts +46 -0
- package/devkit/commands/dev.ts +191 -0
- package/devkit/commands/doctor.ts +25 -0
- package/devkit/commands/kit.ts +45 -0
- package/devkit/commands/new.ts +81 -0
- package/devkit/commands/registry.ts +20 -0
- package/devkit/commands/secrets.ts +254 -0
- package/devkit/lib/args.ts +65 -0
- package/devkit/lib/audit.test.ts +405 -0
- package/devkit/lib/audit.ts +813 -0
- package/devkit/lib/config.test.ts +203 -0
- package/devkit/lib/config.ts +417 -0
- package/devkit/lib/database/index.test.ts +223 -0
- package/devkit/lib/database/index.ts +314 -0
- package/devkit/lib/database/neon.test.ts +233 -0
- package/devkit/lib/database/neon.ts +357 -0
- package/devkit/lib/database/prepare.ts +54 -0
- package/devkit/lib/deploy-config.test.ts +217 -0
- package/devkit/lib/deploy-config.ts +133 -0
- package/devkit/lib/deploy.test.ts +275 -0
- package/devkit/lib/deploy.ts +177 -0
- package/devkit/lib/dev-gateway.test.ts +128 -0
- package/devkit/lib/dev-gateway.ts +162 -0
- package/devkit/lib/dev-menu.test.ts +114 -0
- package/devkit/lib/dev-menu.ts +332 -0
- package/devkit/lib/dev-secrets.test.ts +156 -0
- package/devkit/lib/dev-secrets.ts +97 -0
- package/devkit/lib/dev.test.ts +194 -0
- package/devkit/lib/dev.ts +119 -0
- package/devkit/lib/doctor.test.ts +144 -0
- package/devkit/lib/doctor.ts +264 -0
- package/devkit/lib/env-file.test.ts +102 -0
- package/devkit/lib/env-file.ts +96 -0
- package/devkit/lib/glob.ts +31 -0
- package/devkit/lib/infisical.test.ts +208 -0
- package/devkit/lib/infisical.ts +139 -0
- package/devkit/lib/json-schema.ts +220 -0
- package/devkit/lib/jsonc.test.ts +53 -0
- package/devkit/lib/jsonc.ts +93 -0
- package/devkit/lib/kit-migrate.test.ts +215 -0
- package/devkit/lib/kit-migrate.ts +624 -0
- package/devkit/lib/kit-package.test.ts +83 -0
- package/devkit/lib/kit-package.ts +57 -0
- package/devkit/lib/kit-version.ts +22 -0
- package/devkit/lib/naming.test.ts +547 -0
- package/devkit/lib/naming.ts +796 -0
- package/devkit/lib/paths.ts +7 -0
- package/devkit/lib/process.ts +78 -0
- package/devkit/lib/project-checks.ts +86 -0
- package/devkit/lib/scaffold.test.ts +302 -0
- package/devkit/lib/scaffold.ts +315 -0
- package/devkit/lib/secrets.test.ts +101 -0
- package/devkit/lib/secrets.ts +125 -0
- package/devkit/lib/sourcemaps.test.ts +86 -0
- package/devkit/lib/sourcemaps.ts +98 -0
- package/devkit/lib/test-utils.ts +68 -0
- package/devkit/lib/workspace.ts +87 -0
- package/devkit/lib/wrangler.ts +231 -0
- package/docs/rc-config.md +93 -0
- package/package.json +86 -4
- package/presets/build.d.ts +15 -0
- package/presets/build.js +53 -0
- package/presets/vite-plus.d.ts +28 -0
- package/presets/vite-plus.js +153 -0
- package/registry/README.md +294 -0
- package/registry/public/r/avatar-stack.json +18 -0
- package/registry/public/r/avatar.json +20 -0
- package/registry/public/r/back-link.json +21 -0
- package/registry/public/r/badge.json +17 -0
- package/registry/public/r/button.json +18 -0
- package/registry/public/r/calendar-days.json +14 -0
- package/registry/public/r/calendar-popover.json +24 -0
- package/registry/public/r/calendar.json +23 -0
- package/registry/public/r/callout.json +20 -0
- package/registry/public/r/card.json +18 -0
- package/registry/public/r/character-counter.json +17 -0
- package/registry/public/r/checkbox.json +22 -0
- package/registry/public/r/choice-cards.json +22 -0
- package/registry/public/r/color-tag.json +17 -0
- package/registry/public/r/combobox.json +24 -0
- package/registry/public/r/confirm-button.json +24 -0
- package/registry/public/r/confirm-dialog.json +23 -0
- package/registry/public/r/copy-button.json +24 -0
- package/registry/public/r/date-picker.json +24 -0
- package/registry/public/r/date-range-picker.json +28 -0
- package/registry/public/r/detail-header.json +17 -0
- package/registry/public/r/detail-row.json +17 -0
- package/registry/public/r/dev-inbox-api.json +33 -0
- package/registry/public/r/dev-toolbar.json +109 -0
- package/registry/public/r/dev-tools-contract.json +15 -0
- package/registry/public/r/dialog.json +23 -0
- package/registry/public/r/empty-state.json +20 -0
- package/registry/public/r/error-state.json +23 -0
- package/registry/public/r/eyebrow.json +17 -0
- package/registry/public/r/fab.json +20 -0
- package/registry/public/r/field-error.json +17 -0
- package/registry/public/r/field-grid.json +17 -0
- package/registry/public/r/field.json +19 -0
- package/registry/public/r/file-picker.json +22 -0
- package/registry/public/r/filter-chips.json +17 -0
- package/registry/public/r/form-section.json +17 -0
- package/registry/public/r/form-sheet.json +22 -0
- package/registry/public/r/icon-button.json +23 -0
- package/registry/public/r/input.json +18 -0
- package/registry/public/r/label.json +18 -0
- package/registry/public/r/list-row.json +17 -0
- package/registry/public/r/loading-state.json +18 -0
- package/registry/public/r/localized-field.json +23 -0
- package/registry/public/r/markdown-text.json +17 -0
- package/registry/public/r/menu.json +21 -0
- package/registry/public/r/money-input.json +17 -0
- package/registry/public/r/month-day-select.json +18 -0
- package/registry/public/r/numbered-step.json +20 -0
- package/registry/public/r/page-actions.json +14 -0
- package/registry/public/r/page-header.json +17 -0
- package/registry/public/r/pending.json +17 -0
- package/registry/public/r/person-option.json +21 -0
- package/registry/public/r/popover.json +20 -0
- package/registry/public/r/progress-bar.json +17 -0
- package/registry/public/r/query-view.json +22 -0
- package/registry/public/r/registry.json +1352 -0
- package/registry/public/r/retry-button.json +21 -0
- package/registry/public/r/rich-text-editor.json +32 -0
- package/registry/public/r/save-bar.json +22 -0
- package/registry/public/r/search-input.json +23 -0
- package/registry/public/r/search-select.json +24 -0
- package/registry/public/r/section-tabs.json +17 -0
- package/registry/public/r/segmented.json +18 -0
- package/registry/public/r/select.json +23 -0
- package/registry/public/r/sheet.json +24 -0
- package/registry/public/r/skeleton.json +17 -0
- package/registry/public/r/spinner.json +20 -0
- package/registry/public/r/stale-notice.json +23 -0
- package/registry/public/r/stat-tile.json +23 -0
- package/registry/public/r/stepper.json +23 -0
- package/registry/public/r/suggestion-strip.json +21 -0
- package/registry/public/r/swatch-picker.json +22 -0
- package/registry/public/r/switch.json +21 -0
- package/registry/public/r/tabs.json +20 -0
- package/registry/public/r/textarea.json +18 -0
- package/registry/public/r/theme.json +15 -0
- package/registry/public/r/timezone-select.json +19 -0
- package/registry/public/r/toaster.json +21 -0
- package/registry/public/r/tooltip.json +17 -0
- package/registry/public/r/ui-labels.json +14 -0
- package/registry/public/r/use-copy-to-clipboard.json +20 -0
- package/registry/public/r/use-online.json +14 -0
- package/registry/public/r/use-stale-queries.json +17 -0
- package/registry/public/r/utils.json +18 -0
- package/registry/public/r/weekday-picker.json +19 -0
- package/registry/public/r/yes-no.json +20 -0
- package/registry/serve.ts +29 -0
- package/rules/architecture.md +47 -0
- package/rules/cloudflare.md +74 -0
- package/rules/code.md +12 -0
- package/rules/git.md +10 -0
- package/rules/index.md +14 -0
- package/rules/observability.md +13 -0
- package/rules/performance.md +39 -0
- package/rules/runtime.md +9 -0
- package/rules/secrets.md +11 -0
- package/rules/stack.md +33 -0
- package/rules/ui.md +23 -0
- package/runtime/ai.test.ts +106 -0
- package/runtime/ai.ts +125 -0
- package/runtime/observability/allowlists.ts +42 -0
- package/runtime/observability/anonymize.test.ts +111 -0
- package/runtime/observability/anonymize.ts +249 -0
- package/runtime/observability/browser.test.ts +46 -0
- package/runtime/observability/browser.ts +126 -0
- package/runtime/observability/server.test.ts +84 -0
- package/runtime/observability/server.ts +152 -0
- package/runtime/observability/tunnel.test.ts +276 -0
- package/runtime/observability/tunnel.ts +360 -0
- package/runtime/observability/user-agent.ts +51 -0
- package/schema/rc.config.schema.json +333 -0
- package/template/.gitattributes +1 -0
- package/template/.github/workflows/ci.yml +64 -0
- package/template/CLAUDE.md +48 -0
- package/template/README.md +50 -0
- package/template/apps/admin/.dev.vars.example +5 -0
- package/template/apps/admin/components.json +24 -0
- package/template/apps/admin/package.json +48 -0
- package/template/apps/admin/public/favicon.svg +1 -0
- package/template/apps/admin/src/components/app-shell.tsx +41 -0
- package/template/apps/admin/src/components/dev-sign-in-links.tsx +57 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-context.ts +13 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-environment-panel.tsx +105 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-list.tsx +54 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-message.tsx +104 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-inbox-panel.tsx +130 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-launcher.tsx +45 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-panel.tsx +90 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar-types.ts +11 -0
- package/template/apps/admin/src/components/dev-toolbar/dev-toolbar.tsx +61 -0
- package/template/apps/admin/src/components/dev-toolbar/development-devtools.tsx +32 -0
- package/template/apps/admin/src/components/login-form.tsx +115 -0
- package/template/apps/admin/src/components/login-sent.tsx +23 -0
- package/template/apps/admin/src/components/message-list.tsx +44 -0
- package/template/apps/admin/src/components/turnstile-challenge.tsx +140 -0
- package/template/apps/admin/src/components/ui/button.tsx +87 -0
- package/template/apps/admin/src/components/ui/callout.tsx +84 -0
- package/template/apps/admin/src/components/ui/card.tsx +48 -0
- package/template/apps/admin/src/components/ui/eyebrow.tsx +33 -0
- package/template/apps/admin/src/components/ui/field-error.tsx +24 -0
- package/template/apps/admin/src/components/ui/field.tsx +100 -0
- package/template/apps/admin/src/components/ui/input.tsx +73 -0
- package/template/apps/admin/src/components/ui/label.tsx +39 -0
- package/template/apps/admin/src/components/ui/skeleton.tsx +225 -0
- package/template/apps/admin/src/components/ui/spinner.tsx +12 -0
- package/template/apps/admin/src/components/ui/toaster.tsx +33 -0
- package/template/apps/admin/src/components/ui/tooltip.tsx +51 -0
- package/template/apps/admin/src/hooks/use-dev-inbox.ts +25 -0
- package/template/apps/admin/src/hooks/use-dev-toolbar-state.ts +48 -0
- package/template/apps/admin/src/lib/api.ts +39 -0
- package/template/apps/admin/src/lib/auth-client.ts +7 -0
- package/template/apps/admin/src/lib/dev-inbox-links.ts +66 -0
- package/template/apps/admin/src/lib/dev-tools-client.ts +54 -0
- package/template/apps/admin/src/lib/dev-tools-contract.ts +52 -0
- package/template/apps/admin/src/lib/messages.ts +5 -0
- package/template/apps/admin/src/lib/query-keys.ts +3 -0
- package/template/apps/admin/src/lib/security-headers.ts +18 -0
- package/template/apps/admin/src/lib/session.ts +27 -0
- package/template/apps/admin/src/lib/site-url.ts +7 -0
- package/template/apps/admin/src/lib/turnstile.ts +70 -0
- package/template/apps/admin/src/lib/ui-labels.tsx +182 -0
- package/template/apps/admin/src/lib/utils.ts +6 -0
- package/template/apps/admin/src/routeTree.gen.ts +109 -0
- package/template/apps/admin/src/router.tsx +29 -0
- package/template/apps/admin/src/routes/__root.tsx +56 -0
- package/template/apps/admin/src/routes/_app/index.tsx +38 -0
- package/template/apps/admin/src/routes/_app.tsx +26 -0
- package/template/apps/admin/src/routes/login.tsx +48 -0
- package/template/apps/admin/src/styles/app.css +11 -0
- package/template/apps/admin/src/worker.ts +88 -0
- package/template/apps/admin/tsconfig.json +19 -0
- package/template/apps/admin/vite.config.ts +69 -0
- package/template/apps/admin/wrangler.jsonc +92 -0
- package/template/apps/web/.env.example +1 -0
- package/template/apps/web/astro.config.ts +46 -0
- package/template/apps/web/package.json +33 -0
- package/template/apps/web/public/favicon.svg +1 -0
- package/template/apps/web/src/components/ContactForm.astro +78 -0
- package/template/apps/web/src/env.d.ts +11 -0
- package/template/apps/web/src/layouts/BaseLayout.astro +32 -0
- package/template/apps/web/src/lib/api.ts +8 -0
- package/template/apps/web/src/pages/api/messages.ts +32 -0
- package/template/apps/web/src/pages/index.astro +26 -0
- package/template/apps/web/src/pages/media/[...key].ts +4 -0
- package/template/apps/web/src/scripts/contact-form.ts +105 -0
- package/template/apps/web/src/scripts/error-reporting.ts +4 -0
- package/template/apps/web/src/scripts/turnstile.ts +177 -0
- package/template/apps/web/src/styles/global.css +11 -0
- package/template/apps/web/src/worker.ts +34 -0
- package/template/apps/web/tsconfig.json +11 -0
- package/template/apps/web/wrangler.jsonc +45 -0
- package/template/bun.lock +1925 -0
- package/template/bunfig.toml +2 -0
- package/template/gitignore +23 -0
- package/template/package.json +74 -0
- package/template/packages/api/components.json +21 -0
- package/template/packages/api/package.json +30 -0
- package/template/packages/api/src/client.ts +13 -0
- package/template/packages/api/src/cloudflare-env.d.ts +26 -0
- package/template/packages/api/src/emails/escape-html.ts +13 -0
- package/template/packages/api/src/emails/layout.ts +22 -0
- package/template/packages/api/src/emails/magic-link.ts +32 -0
- package/template/packages/api/src/emails/new-message.ts +32 -0
- package/template/packages/api/src/emails/send.ts +14 -0
- package/template/packages/api/src/lib/auth-guard.ts +16 -0
- package/template/packages/api/src/lib/auth.ts +108 -0
- package/template/packages/api/src/lib/context.ts +7 -0
- package/template/packages/api/src/lib/dev-inbox/routes.ts +77 -0
- package/template/packages/api/src/lib/dev-inbox/send.ts +57 -0
- package/template/packages/api/src/lib/dev-inbox/store.ts +185 -0
- package/template/packages/api/src/lib/dev-inbox.ts +6 -0
- package/template/packages/api/src/lib/dev-tools-contract.ts +52 -0
- package/template/packages/api/src/lib/environment.ts +17 -0
- package/template/packages/api/src/lib/turnstile.ts +33 -0
- package/template/packages/api/src/media.ts +45 -0
- package/template/packages/api/src/routes/admin-messages.ts +21 -0
- package/template/packages/api/src/routes/dev.ts +31 -0
- package/template/packages/api/src/routes/health.ts +7 -0
- package/template/packages/api/src/routes/messages.ts +58 -0
- package/template/packages/api/src/schemas.ts +18 -0
- package/template/packages/api/src/server.ts +29 -0
- package/template/packages/api/tsconfig.json +10 -0
- package/template/packages/db/migrations/0001_init.sql +79 -0
- package/template/packages/db/package.json +34 -0
- package/template/packages/db/prisma/schema.prisma +95 -0
- package/template/packages/db/prisma.config.ts +9 -0
- package/template/packages/db/scripts/migrate.ts +15 -0
- package/template/packages/db/scripts/new-migration.ts +185 -0
- package/template/packages/db/scripts/reset.ts +54 -0
- package/template/packages/db/scripts/seed.ts +44 -0
- package/template/packages/db/scripts/wrangler.ts +35 -0
- package/template/packages/db/src/d1.ts +7 -0
- package/template/packages/db/src/migration-guard.test.ts +43 -0
- package/template/packages/db/src/migration-guard.ts +51 -0
- package/template/packages/db/src/new-migration.test.ts +16 -0
- package/template/packages/db/src/scope.ts +38 -0
- package/template/packages/db/src/seed.ts +30 -0
- package/template/packages/db/src/sql.ts +42 -0
- package/template/packages/db/tsconfig.json +8 -0
- package/template/packages/shared/components.json +21 -0
- package/template/packages/shared/package.json +33 -0
- package/template/packages/shared/src/edge/origins.ts +20 -0
- package/template/packages/shared/src/edge/turnstile.ts +11 -0
- package/template/packages/shared/src/locales.ts +13 -0
- package/template/packages/shared/src/message-rules.ts +16 -0
- package/template/packages/shared/src/messages.test.ts +26 -0
- package/template/packages/shared/src/messages.ts +30 -0
- package/template/packages/shared/src/observability/build-release.ts +9 -0
- package/template/packages/shared/src/observability/error-reporting.ts +31 -0
- package/template/packages/shared/src/observability/error-tunnel.ts +20 -0
- package/template/packages/shared/src/observability/sentry-browser.ts +1 -0
- package/template/packages/shared/src/observability/server-errors.ts +74 -0
- package/template/packages/shared/src/project.ts +1 -0
- package/template/packages/shared/src/styles/rc-theme.css +138 -0
- package/template/packages/shared/src/styles/theme.css +9 -0
- package/template/packages/shared/tsconfig.json +10 -0
- package/template/rc.config.json +25 -0
- package/template/scripts/bootstrap-local.ts +42 -0
- package/template/tsconfig.base.json +20 -0
- package/template/tsconfig.json +8 -0
- package/template/tsconfig.worker.json +7 -0
- package/template/turbo.json +40 -0
- package/template/vite.config.ts +8 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ANONYMOUS_SESSION_ID_LENGTH,
|
|
3
|
+
anonymizeEvent,
|
|
4
|
+
type TAnonymizableEvent,
|
|
5
|
+
} from "./anonymize"
|
|
6
|
+
import { CLIENT_ERROR_CONTEXT_KEYS, CLIENT_ERROR_TAG_KEYS } from "./allowlists"
|
|
7
|
+
import { coarseUserAgent } from "./user-agent"
|
|
8
|
+
|
|
9
|
+
type TJsonObject = Record<string, unknown>
|
|
10
|
+
|
|
11
|
+
export interface TEnvelopeItem {
|
|
12
|
+
header: TJsonObject
|
|
13
|
+
payload: Uint8Array
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface TEnvelope {
|
|
17
|
+
header: TJsonObject
|
|
18
|
+
items: TEnvelopeItem[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface TTunnelTarget {
|
|
22
|
+
publicKey: string
|
|
23
|
+
url: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface TForwardedEnvelope {
|
|
27
|
+
body: Uint8Array<ArrayBuffer>
|
|
28
|
+
target: TTunnelTarget
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const MAX_ERROR_ENVELOPE_BYTES = 200 * 1024
|
|
32
|
+
|
|
33
|
+
const NEWLINE = 0x0a
|
|
34
|
+
const SESSION_ID_RE = new RegExp(`^[0-9a-f]{${ANONYMOUS_SESSION_ID_LENGTH}}$`)
|
|
35
|
+
|
|
36
|
+
const encoder = new TextEncoder()
|
|
37
|
+
const decoder = new TextDecoder()
|
|
38
|
+
|
|
39
|
+
function parseJsonObject(bytes: Uint8Array): TJsonObject | null {
|
|
40
|
+
try {
|
|
41
|
+
const value: unknown = JSON.parse(decoder.decode(bytes))
|
|
42
|
+
|
|
43
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
44
|
+
? (value as TJsonObject)
|
|
45
|
+
: null
|
|
46
|
+
} catch {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function lineEnd(bytes: Uint8Array, from: number): number {
|
|
52
|
+
const index = bytes.indexOf(NEWLINE, from)
|
|
53
|
+
|
|
54
|
+
return index === -1 ? bytes.length : index
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function parseEnvelope(bytes: Uint8Array): TEnvelope | null {
|
|
58
|
+
const headerEnd = lineEnd(bytes, 0)
|
|
59
|
+
const header = parseJsonObject(bytes.subarray(0, headerEnd))
|
|
60
|
+
|
|
61
|
+
if (!header) {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const items: TEnvelopeItem[] = []
|
|
66
|
+
let offset = headerEnd + 1
|
|
67
|
+
|
|
68
|
+
while (offset < bytes.length) {
|
|
69
|
+
const itemHeaderEnd = lineEnd(bytes, offset)
|
|
70
|
+
const itemHeader = parseJsonObject(bytes.subarray(offset, itemHeaderEnd))
|
|
71
|
+
|
|
72
|
+
if (!itemHeader) {
|
|
73
|
+
if (itemHeaderEnd === offset) {
|
|
74
|
+
offset += 1
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
offset = itemHeaderEnd + 1
|
|
82
|
+
|
|
83
|
+
const { length } = itemHeader
|
|
84
|
+
let payloadEnd: number
|
|
85
|
+
|
|
86
|
+
if (typeof length === "number") {
|
|
87
|
+
payloadEnd = offset + length
|
|
88
|
+
|
|
89
|
+
if (
|
|
90
|
+
!Number.isInteger(length) ||
|
|
91
|
+
length < 0 ||
|
|
92
|
+
payloadEnd > bytes.length
|
|
93
|
+
) {
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
payloadEnd = lineEnd(bytes, offset)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
items.push({
|
|
101
|
+
header: itemHeader,
|
|
102
|
+
payload: bytes.subarray(offset, payloadEnd),
|
|
103
|
+
})
|
|
104
|
+
offset = payloadEnd + 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { header, items }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function parseTunnelDsns(raw: string | undefined): Set<string> {
|
|
111
|
+
return new Set(
|
|
112
|
+
(raw ?? "")
|
|
113
|
+
.split(",")
|
|
114
|
+
.map((dsn) => dsn.trim())
|
|
115
|
+
.filter(Boolean)
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function tunnelTarget(dsn: string): TTunnelTarget | null {
|
|
120
|
+
try {
|
|
121
|
+
const url = new URL(dsn)
|
|
122
|
+
const segments = url.pathname.split("/").filter(Boolean)
|
|
123
|
+
const projectId = segments.pop()
|
|
124
|
+
|
|
125
|
+
if (!(url.username && projectId && url.protocol === "https:")) {
|
|
126
|
+
return null
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const prefix = segments.length > 0 ? `/${segments.join("/")}` : ""
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
publicKey: url.username,
|
|
133
|
+
url: `${url.origin}${prefix}/api/${projectId}/envelope/`,
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
return null
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function sessionIdOf(event: TAnonymizableEvent): string | null {
|
|
141
|
+
const id = event.user?.id
|
|
142
|
+
|
|
143
|
+
return typeof id === "string" && SESSION_ID_RE.test(id) ? id : null
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface TTunnelAllowlists {
|
|
147
|
+
contextKeys?: ReadonlySet<string>
|
|
148
|
+
keptBreadcrumbCategories?: ReadonlySet<string>
|
|
149
|
+
tagKeys?: ReadonlySet<string>
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function anonymizeTunnelEvent(
|
|
153
|
+
event: TAnonymizableEvent,
|
|
154
|
+
userAgent: string | null,
|
|
155
|
+
allowlists: TTunnelAllowlists = {}
|
|
156
|
+
): TAnonymizableEvent {
|
|
157
|
+
const anonymized = anonymizeEvent(event, {
|
|
158
|
+
allowedContextKeys: allowlists.contextKeys ?? CLIENT_ERROR_CONTEXT_KEYS,
|
|
159
|
+
allowedTagKeys: allowlists.tagKeys ?? CLIENT_ERROR_TAG_KEYS,
|
|
160
|
+
keptBreadcrumbCategories: allowlists.keptBreadcrumbCategories,
|
|
161
|
+
sessionId: sessionIdOf(event),
|
|
162
|
+
})
|
|
163
|
+
const { browser, os } = coarseUserAgent(userAgent)
|
|
164
|
+
const contexts = { ...anonymized.contexts }
|
|
165
|
+
|
|
166
|
+
if (browser) {
|
|
167
|
+
contexts.browser = browser
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (os && !contexts.os) {
|
|
171
|
+
contexts.os = os
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
anonymized.contexts = contexts
|
|
175
|
+
|
|
176
|
+
return anonymized
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function concat(chunks: Uint8Array[]): Uint8Array<ArrayBuffer> {
|
|
180
|
+
const body = new Uint8Array(
|
|
181
|
+
chunks.reduce((total, chunk) => total + chunk.length, 0)
|
|
182
|
+
)
|
|
183
|
+
let offset = 0
|
|
184
|
+
|
|
185
|
+
for (const chunk of chunks) {
|
|
186
|
+
body.set(chunk, offset)
|
|
187
|
+
offset += chunk.length
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return body
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function rebuiltHeader(header: TJsonObject, dsn: string): TJsonObject {
|
|
194
|
+
const kept: TJsonObject = { dsn }
|
|
195
|
+
|
|
196
|
+
if (typeof header.event_id === "string") {
|
|
197
|
+
kept.event_id = header.event_id
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (typeof header.sent_at === "string") {
|
|
201
|
+
kept.sent_at = header.sent_at
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (typeof header.sdk === "object" && header.sdk !== null) {
|
|
205
|
+
const { name, version } = header.sdk as TJsonObject
|
|
206
|
+
kept.sdk = { name, version }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return kept
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function prepareForward(input: {
|
|
213
|
+
allowedDsns: ReadonlySet<string>
|
|
214
|
+
allowlists?: TTunnelAllowlists
|
|
215
|
+
body: Uint8Array
|
|
216
|
+
userAgent: string | null
|
|
217
|
+
}): TForwardedEnvelope | null {
|
|
218
|
+
const envelope = parseEnvelope(input.body)
|
|
219
|
+
const dsn = envelope?.header.dsn
|
|
220
|
+
|
|
221
|
+
if (!(envelope && typeof dsn === "string" && input.allowedDsns.has(dsn))) {
|
|
222
|
+
return null
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const target = tunnelTarget(dsn)
|
|
226
|
+
if (!target) {
|
|
227
|
+
return null
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const chunks: Uint8Array[] = [
|
|
231
|
+
encoder.encode(JSON.stringify(rebuiltHeader(envelope.header, dsn))),
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
for (const item of envelope.items) {
|
|
235
|
+
if (item.header.type !== "event") {
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const event = parseJsonObject(item.payload)
|
|
240
|
+
if (!event) {
|
|
241
|
+
continue
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const payload = encoder.encode(
|
|
245
|
+
JSON.stringify(
|
|
246
|
+
anonymizeTunnelEvent(
|
|
247
|
+
event as TAnonymizableEvent,
|
|
248
|
+
input.userAgent,
|
|
249
|
+
input.allowlists
|
|
250
|
+
)
|
|
251
|
+
)
|
|
252
|
+
)
|
|
253
|
+
const itemHeader = {
|
|
254
|
+
content_type: "application/json",
|
|
255
|
+
length: payload.length,
|
|
256
|
+
type: "event",
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
chunks.push(encoder.encode(`\n${JSON.stringify(itemHeader)}\n`), payload)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (chunks.length === 1) {
|
|
263
|
+
return null
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return { body: concat(chunks), target }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export async function forwardEnvelope(
|
|
270
|
+
forwarded: TForwardedEnvelope
|
|
271
|
+
): Promise<void> {
|
|
272
|
+
const response = await fetch(forwarded.target.url, {
|
|
273
|
+
body: forwarded.body,
|
|
274
|
+
headers: {
|
|
275
|
+
"Content-Type": "application/x-sentry-envelope",
|
|
276
|
+
"X-Sentry-Auth": `Sentry sentry_version=7, sentry_key=${forwarded.target.publicKey}, sentry_client=rc-kit-tunnel/1.0`,
|
|
277
|
+
},
|
|
278
|
+
method: "POST",
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
await response.body?.cancel().catch(() => undefined)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export async function readCappedBody(
|
|
285
|
+
request: Request,
|
|
286
|
+
maxBytes: number
|
|
287
|
+
): Promise<Uint8Array | null> {
|
|
288
|
+
const declared = Number(request.headers.get("content-length") ?? 0)
|
|
289
|
+
if (declared > maxBytes || !request.body) {
|
|
290
|
+
return null
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const reader = request.body.getReader()
|
|
294
|
+
const chunks: Uint8Array[] = []
|
|
295
|
+
let size = 0
|
|
296
|
+
|
|
297
|
+
for (;;) {
|
|
298
|
+
const { done, value } = await reader.read()
|
|
299
|
+
if (done) {
|
|
300
|
+
break
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
size += value.byteLength
|
|
304
|
+
if (size > maxBytes) {
|
|
305
|
+
await reader.cancel().catch(() => undefined)
|
|
306
|
+
return null
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
chunks.push(value)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const body = new Uint8Array(size)
|
|
313
|
+
let offset = 0
|
|
314
|
+
|
|
315
|
+
for (const chunk of chunks) {
|
|
316
|
+
body.set(chunk, offset)
|
|
317
|
+
offset += chunk.byteLength
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return body
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export const ERROR_TUNNEL_PATH = "/api/telemetry/errors"
|
|
324
|
+
|
|
325
|
+
export interface TErrorTunnelOptions {
|
|
326
|
+
allowedDsns: string | undefined
|
|
327
|
+
allowlists?: TTunnelAllowlists
|
|
328
|
+
enabled: boolean
|
|
329
|
+
waitUntil: (promise: Promise<unknown>) => void
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Always 200: an error report must never become a second error on the client.
|
|
333
|
+
export async function handleErrorTunnel(
|
|
334
|
+
request: Request,
|
|
335
|
+
options: TErrorTunnelOptions
|
|
336
|
+
): Promise<Response> {
|
|
337
|
+
const accepted = new Response(null, { status: 200 })
|
|
338
|
+
|
|
339
|
+
if (!options.enabled || request.method !== "POST") {
|
|
340
|
+
return accepted
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const body = await readCappedBody(request, MAX_ERROR_ENVELOPE_BYTES)
|
|
344
|
+
if (!body) {
|
|
345
|
+
return accepted
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const forwarded = prepareForward({
|
|
349
|
+
allowedDsns: parseTunnelDsns(options.allowedDsns),
|
|
350
|
+
allowlists: options.allowlists,
|
|
351
|
+
body,
|
|
352
|
+
userAgent: request.headers.get("user-agent"),
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
if (forwarded) {
|
|
356
|
+
options.waitUntil(forwardEnvelope(forwarded).catch(() => undefined))
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return accepted
|
|
360
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface TCoarseVersioned {
|
|
2
|
+
name: string
|
|
3
|
+
version?: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface TCoarseUserAgent {
|
|
7
|
+
browser?: TCoarseVersioned
|
|
8
|
+
os?: TCoarseVersioned
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const BROWSER_PATTERNS: [string, RegExp][] = [
|
|
12
|
+
["Edge", /\bEdg(?:e|A|iOS)?\/(\d+)/],
|
|
13
|
+
["Opera", /\bOPR\/(\d+)/],
|
|
14
|
+
["Samsung Internet", /\bSamsungBrowser\/(\d+)/],
|
|
15
|
+
["Firefox", /\b(?:Firefox|FxiOS)\/(\d+)/],
|
|
16
|
+
["Chrome", /\b(?:Chrome|CriOS)\/(\d+)/],
|
|
17
|
+
["Safari", /\bVersion\/(\d+)[^ ]* (?:Mobile\/\S+ )?Safari\//],
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
const OS_PATTERNS: [string, RegExp][] = [
|
|
21
|
+
["Windows", /\bWindows NT (\d+)/],
|
|
22
|
+
["iOS", /\b(?:iPhone|iPad|iPod).*? OS (\d+)/],
|
|
23
|
+
["Android", /\bAndroid (\d+)/],
|
|
24
|
+
["macOS", /\bMac OS X (\d+)/],
|
|
25
|
+
["Chrome OS", /\bCrOS\b()/],
|
|
26
|
+
["Linux", /\bLinux\b()/],
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
function firstMatch(
|
|
30
|
+
userAgent: string,
|
|
31
|
+
patterns: [string, RegExp][]
|
|
32
|
+
): TCoarseVersioned | undefined {
|
|
33
|
+
for (const [name, pattern] of patterns) {
|
|
34
|
+
const match = pattern.exec(userAgent)
|
|
35
|
+
|
|
36
|
+
if (match) {
|
|
37
|
+
return match[1] ? { name, version: match[1] } : { name }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function coarseUserAgent(userAgent: string | null): TCoarseUserAgent {
|
|
43
|
+
if (!userAgent) {
|
|
44
|
+
return {}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
browser: firstMatch(userAgent, BROWSER_PATTERNS),
|
|
49
|
+
os: firstMatch(userAgent, OS_PATTERNS),
|
|
50
|
+
}
|
|
51
|
+
}
|