@sssb/kit 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/core/webhook-hmac.d.ts +69 -0
- package/dist/release.json +1 -0
- package/dist/testing/e2e.d.ts +15 -0
- package/dist/testing/e2e.js +195 -0
- package/package.json +209 -5
- package/src/adapters/README.md +6 -0
- package/src/adapters/dev-server.ts +21 -0
- package/src/adapters/fake/agreements.ts +81 -0
- package/src/adapters/fake/analytics.ts +93 -0
- package/src/adapters/fake/atomic-write.ts +161 -0
- package/src/adapters/fake/audit.ts +58 -0
- package/src/adapters/fake/auth.ts +312 -0
- package/src/adapters/fake/clock.ts +54 -0
- package/src/adapters/fake/data-dir.ts +27 -0
- package/src/adapters/fake/db.ts +88 -0
- package/src/adapters/fake/email.ts +46 -0
- package/src/adapters/fake/jobs.ts +270 -0
- package/src/adapters/fake/llm.ts +230 -0
- package/src/adapters/fake/pglite-dialect.ts +124 -0
- package/src/adapters/fake/service-auth.ts +145 -0
- package/src/adapters/fake/simulator-admin.ts +203 -0
- package/src/adapters/fake/simulator.ts +94 -0
- package/src/adapters/fake/sms.ts +43 -0
- package/src/adapters/fake/storage.ts +288 -0
- package/src/adapters/fake/webhooks.ts +98 -0
- package/src/adapters/index.ts +115 -0
- package/src/adapters/real/accept-invite-form.tsx +77 -0
- package/src/adapters/real/analytics.ts +28 -0
- package/src/adapters/real/auth.ts +216 -0
- package/src/adapters/real/contract-data-api.ts +104 -0
- package/src/adapters/real/contract-postgres.ts +167 -0
- package/src/adapters/real/database-transport.ts +34 -0
- package/src/adapters/real/db-data-api.ts +61 -0
- package/src/adapters/real/db.ts +88 -0
- package/src/adapters/real/email.ts +22 -0
- package/src/adapters/real/index.ts +89 -0
- package/src/adapters/real/jobs.ts +32 -0
- package/src/adapters/real/llm.ts +108 -0
- package/src/adapters/real/migrate-handler.ts +66 -0
- package/src/adapters/real/providers.tsx +12 -0
- package/src/adapters/real/rds-data-codec.ts +508 -0
- package/src/adapters/real/rds-data-dialect.ts +470 -0
- package/src/adapters/real/rds-data-emulator.ts +351 -0
- package/src/adapters/real/s3-storage.ts +233 -0
- package/src/adapters/real/sign-in-form.tsx +59 -0
- package/src/adapters/real/storage.ts +10 -0
- package/src/adapters/sentry/baseline.ts +90 -0
- package/src/adapters/sentry/client.ts +17 -0
- package/src/adapters/sentry/init.ts +17 -0
- package/src/authz/app-exemptions.ts +226 -0
- package/src/authz/authorize.ts +214 -0
- package/src/authz/route-policy.ts +153 -0
- package/src/authz/route-scan.ts +1516 -0
- package/src/components/agreements/acceptances-section.tsx +55 -0
- package/src/components/agreements/agreement-advisory-banner.tsx +33 -0
- package/src/components/agreements/agreement-body.tsx +34 -0
- package/src/components/agreements/agreement-gate.tsx +45 -0
- package/src/components/app-header.tsx +45 -0
- package/src/components/auth/accept-invite-screen.tsx +126 -0
- package/src/components/auth/auth-icons.tsx +68 -0
- package/src/components/auth/org-switcher.tsx +76 -0
- package/src/components/auth/people-picker.tsx +50 -0
- package/src/components/auth/preview-row.tsx +64 -0
- package/src/components/auth/sign-in-screen.tsx +24 -0
- package/src/components/auth/user-menu.tsx +64 -0
- package/src/components/demo-badge.tsx +20 -0
- package/src/components/demo-banner.tsx +14 -0
- package/src/components/gates/gate-interstitial.tsx +23 -0
- package/src/components/job-timeline.tsx +64 -0
- package/src/components/notifications/notification-bell.tsx +120 -0
- package/src/components/notifications/notification-prefs-section.tsx +112 -0
- package/src/components/org-screen.tsx +197 -0
- package/src/components/profile-screen.tsx +100 -0
- package/src/components/simulator/actor-runtime.ts +180 -0
- package/src/components/simulator/actor-shell.tsx +275 -0
- package/src/components/simulator/actors-app.tsx +119 -0
- package/src/components/simulator/errors-app.tsx +84 -0
- package/src/components/simulator/events-app.tsx +210 -0
- package/src/components/simulator/format-when.ts +20 -0
- package/src/components/simulator/hooks-app.tsx +319 -0
- package/src/components/simulator/inbound-app.tsx +210 -0
- package/src/components/simulator/jobs-app.tsx +471 -0
- package/src/components/simulator/mail-app.tsx +290 -0
- package/src/components/simulator/messages-app.tsx +64 -0
- package/src/components/simulator/people-app.tsx +140 -0
- package/src/components/simulator/people-dimensions.ts +16 -0
- package/src/components/simulator/simulator-panel.tsx +716 -0
- package/src/components/simulator/snapshots-app.tsx +398 -0
- package/src/components/simulator/tab-mount.ts +25 -0
- package/src/components/simulator/tours-app.tsx +105 -0
- package/src/components/webhook-endpoints-card.tsx +172 -0
- package/src/core/abilities.ts +459 -0
- package/src/core/agreements.ts +139 -0
- package/src/core/gates.ts +157 -0
- package/src/core/greeting.ts +11 -0
- package/src/core/inbound-email.ts +129 -0
- package/src/core/jobs.ts +51 -0
- package/src/core/keyset.ts +227 -0
- package/src/core/llm-models.ts +10 -0
- package/src/core/locale.ts +3 -0
- package/src/core/notifications.ts +182 -0
- package/src/core/presets.ts +456 -0
- package/src/core/roles.ts +47 -0
- package/src/core/schedules.ts +147 -0
- package/src/core/standard-schema.ts +56 -0
- package/src/core/state-machine.ts +43 -0
- package/src/core/webhook-events.ts +86 -0
- package/src/core/webhook-hmac.ts +248 -0
- package/src/core/webhook-signing.ts +27 -0
- package/src/cutover/rows.json +101 -0
- package/src/db/agreements.ts +176 -0
- package/src/db/app-roles.ts +197 -0
- package/src/db/audit.ts +356 -0
- package/src/db/inbound-email.ts +145 -0
- package/src/db/jobs.ts +300 -0
- package/src/db/jsonb.ts +18 -0
- package/src/db/keyset.ts +179 -0
- package/src/db/migrate.ts +73 -0
- package/src/db/migrations/0001_tenants.ts +25 -0
- package/src/db/migrations/0003_organizations.ts +27 -0
- package/src/db/migrations/0004_jobs.ts +64 -0
- package/src/db/migrations/0005_service_keys.ts +33 -0
- package/src/db/migrations/0008_audit_events.ts +51 -0
- package/src/db/migrations/0009_job_schedules.ts +52 -0
- package/src/db/migrations/0010_webhooks.ts +100 -0
- package/src/db/migrations/0011_notifications.ts +98 -0
- package/src/db/migrations/0012_inbound_email.ts +74 -0
- package/src/db/migrations/0013_agreements.ts +98 -0
- package/src/db/migrations/0014_audit_credential.ts +29 -0
- package/src/db/migrations/0015_app_user_grant.ts +67 -0
- package/src/db/migrations/0016_audit_detail.ts +26 -0
- package/src/db/migrations/index.ts +49 -0
- package/src/db/notifications.ts +185 -0
- package/src/db/org-lookup.ts +104 -0
- package/src/db/owner-pass.ts +252 -0
- package/src/db/rls-coverage.ts +259 -0
- package/src/db/rls-proof-runner.ts +94 -0
- package/src/db/rls-proofs.ts +1176 -0
- package/src/db/schedules.ts +187 -0
- package/src/db/schema.ts +235 -0
- package/src/db/seed.ts +176 -0
- package/src/db/service-keys.ts +50 -0
- package/src/db/tenant-lookup.ts +13 -0
- package/src/db/webhooks.ts +483 -0
- package/src/db/with-tenant.ts +26 -0
- package/src/demo-static/actor-drivers.ts +161 -0
- package/src/demo-static/assistant-replay.ts +61 -0
- package/src/demo-static/contracts.ts +282 -0
- package/src/demo-static/email-html.ts +58 -0
- package/src/demo-static/hash-route.ts +24 -0
- package/src/demo-static/rows.ts +105 -0
- package/src/demo-static/shell.tsx +440 -0
- package/src/demo-static/tour/contracts.ts +97 -0
- package/src/demo-static/tour/driver.ts +320 -0
- package/src/demo-static/tour/tour-overlay.tsx +226 -0
- package/src/demo-static/tour/use-tours.tsx +394 -0
- package/src/demo-static/world.ts +1479 -0
- package/src/email/send.ts +12 -0
- package/src/email/templates/digest-email.tsx +61 -0
- package/src/email/templates/invite-email.tsx +57 -0
- package/src/email/templates/notification-email.tsx +39 -0
- package/src/guide.md +118 -0
- package/src/i18n/messages/en.json +373 -0
- package/src/i18n/messages/es.json +373 -0
- package/src/i18n/messages.ts +34 -0
- package/src/i18n/namespaces.ts +32 -0
- package/src/i18n/request.ts +34 -0
- package/src/i18n/routing.ts +7 -0
- package/src/inbound-email/handlers.ts +67 -0
- package/src/inbound-email/intake.ts +180 -0
- package/src/jobs/digest-email.ts +97 -0
- package/src/observability/scrub.ts +42 -0
- package/src/ports/README.md +8 -0
- package/src/ports/analytics.ts +8 -0
- package/src/ports/auth.ts +87 -0
- package/src/ports/db.ts +27 -0
- package/src/ports/email.ts +14 -0
- package/src/ports/errors.ts +46 -0
- package/src/ports/jobs.ts +48 -0
- package/src/ports/llm.ts +85 -0
- package/src/ports/storage.ts +132 -0
- package/src/seed/contracts.ts +129 -0
- package/src/server-lib/assistant.ts +45 -0
- package/src/server-lib/defer.ts +53 -0
- package/src/server-lib/demo-presets.ts +85 -0
- package/src/server-lib/invite.ts +92 -0
- package/src/server-lib/notify-deps.ts +15 -0
- package/src/server-lib/notify.ts +252 -0
- package/src/server-lib/preset-operations.ts +121 -0
- package/src/server-lib/scheduled-tick.ts +38 -0
- package/src/server-lib/sms.ts +43 -0
- package/src/server-lib/webhook-dispatch.ts +75 -0
- package/src/service-auth/mailgun.ts +70 -0
- package/src/service-auth/verify.ts +73 -0
- package/src/service-auth/webhook.ts +60 -0
- package/src/testing/checks.json +103 -0
- package/src/testing/conformance/authz.ts +46 -0
- package/src/testing/conformance/db.ts +258 -0
- package/src/testing/conformance/demo-presets.ts +295 -0
- package/src/testing/conformance/i18n.ts +80 -0
- package/src/testing/conformance/register.ts +200 -0
- package/src/testing/conformance/seam-map.ts +205 -0
- package/src/testing/conformance/suite.ts +59 -0
- package/src/testing/conformance/types.ts +18 -0
- package/src/testing/contract.ts +200 -0
- package/src/testing/e2e.ts +15 -0
- package/src/testing/index.ts +6 -0
- package/src/testing/seam-specifiers.json +22 -0
- package/src/testing/tmp-dir-marker.d.mts +4 -0
- package/src/testing/tmp-dir-marker.mjs +5 -0
- package/src/testing/tmp-dir.ts +182 -0
- package/src/theme.ts +28 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rice University
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webhook HMAC — PURE, ISOMORPHIC TypeScript (ADR-0006, lint-enforced): no framework, no
|
|
3
|
+
* `node:crypto`, no `crypto.subtle` global. It implements HMAC-SHA256 in plain TS over bytes so the
|
|
4
|
+
* SAME helper signs on the server (packages/kit/src/db/webhooks.ts real + fake dispatch), verifies in tests, AND
|
|
5
|
+
* runs inside the file:// static-demo twin — where a node builtin can't be bundled and Web Crypto's
|
|
6
|
+
* secure-context/async caveats don't apply. Correctness is pinned by a colocated test that cross-checks
|
|
7
|
+
* every output against `node:crypto` (see webhook-hmac.test.ts); the storage-upload HMAC
|
|
8
|
+
* (packages/kit/src/adapters/fake/storage.ts) is the shape precedent, but it lives in a node-only adapter and so
|
|
9
|
+
* could use node:crypto — this can't.
|
|
10
|
+
*
|
|
11
|
+
* SEAM-FREE, and that is load-bearing: this module imports nothing at all. The header NAMES are derived from
|
|
12
|
+
* the app's `APP_SLUG`, which means reading the `@sssb/app-config/*` seam when the module loads, so they live
|
|
13
|
+
* in ./webhook-signing.ts, which re-exports everything here (importers of `@sssb/kit/core/webhook-signing`
|
|
14
|
+
* see one module). This half is what `@sssb/kit/testing/e2e` ships compiled to Playwright specs, which Node
|
|
15
|
+
* loads without the app's seam (ADR-0014 part 2); its build fails if that entry ever reaches the seam.
|
|
16
|
+
*
|
|
17
|
+
* Scheme (Stripe-style): the signature covers `{timestamp}.{body}`; the header value is
|
|
18
|
+
* `t=<unix-seconds>,v1=<hex>` under the signature header (WEBHOOK_SIGNATURE_HEADER in ./webhook-signing.ts).
|
|
19
|
+
* The template teaches BOTH halves — signWebhookBody / webhookSignatureHeader for egress, and
|
|
20
|
+
* verifyWebhookSignature (with a replay window) for the counterparty that receives it.
|
|
21
|
+
*/
|
|
22
|
+
/** Default replay window the verifier tolerates between the signed timestamp and "now" (5 minutes). */
|
|
23
|
+
export declare const WEBHOOK_REPLAY_TOLERANCE_MS: number;
|
|
24
|
+
/** The raw v1 signature (hex HMAC-SHA256 over `{timestampSec}.{body}`) under `secret`. */
|
|
25
|
+
export declare function signWebhookBody(secret: string, body: string, timestampSec: number): string;
|
|
26
|
+
/** The full WEBHOOK_SIGNATURE_HEADER value: `t=<unix-seconds>,v1=<hex>`. */
|
|
27
|
+
export declare function webhookSignatureHeader(secret: string, body: string, timestampSec: number): string;
|
|
28
|
+
export type WebhookVerifyResult = {
|
|
29
|
+
ok: true;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
reason: 'malformed' | 'stale' | 'signature';
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Verify an inbound webhook — the counterparty half the template teaches. Checks, in order: the header
|
|
36
|
+
* parses (`malformed`); the signed timestamp is within `toleranceMs` of now, BOTH directions, so a
|
|
37
|
+
* replayed OR future-dated request is rejected (`stale`); and the recomputed v1 matches in constant
|
|
38
|
+
* time (`signature`). `now` is injectable for deterministic tests. The timestamp is checked only AFTER
|
|
39
|
+
* it's structurally present but BEFORE trusting the signature body — a stale request is cheap to reject.
|
|
40
|
+
*/
|
|
41
|
+
export declare function verifyWebhookSignature(body: string, header: string | null | undefined, secret: string, opts: {
|
|
42
|
+
toleranceMs: number;
|
|
43
|
+
now?: Date;
|
|
44
|
+
}): WebhookVerifyResult;
|
|
45
|
+
/** Raw hex HMAC-SHA256 over an arbitrary (key, message) — the exposed pure primitive. */
|
|
46
|
+
export declare function hmacSha256Hex(key: string, message: string): string;
|
|
47
|
+
/** Mailgun's expected signature: HMAC-SHA256(signingKey, `${timestamp}${token}`) as hex. */
|
|
48
|
+
export declare function mailgunSignature(signingKey: string, timestamp: string, token: string): string;
|
|
49
|
+
export type MailgunVerifyResult = {
|
|
50
|
+
ok: true;
|
|
51
|
+
} | {
|
|
52
|
+
ok: false;
|
|
53
|
+
reason: 'malformed' | 'stale' | 'signature';
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Verify a Mailgun inbound signature — the pure half (no replay state). Checks, in order: the fields
|
|
57
|
+
* are structurally present and the timestamp is numeric (`malformed`); the timestamp is within
|
|
58
|
+
* `toleranceMs` of `now` BOTH directions, so a stale OR future-dated request is rejected (`stale`);
|
|
59
|
+
* and the recomputed HMAC matches in constant time (`signature`). `now` is injectable for tests. The
|
|
60
|
+
* caller (service-auth) additionally rejects a re-used token within the window.
|
|
61
|
+
*/
|
|
62
|
+
export declare function verifyMailgunSignature(params: {
|
|
63
|
+
signingKey: string;
|
|
64
|
+
timestamp: string;
|
|
65
|
+
token: string;
|
|
66
|
+
signature: string;
|
|
67
|
+
toleranceMs: number;
|
|
68
|
+
now?: Date;
|
|
69
|
+
}): MailgunVerifyResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"commit":"e8b1dbfad8d5f7a59c1a49fd39714d73229320c1"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sssb/kit/testing/e2e` — everything SSSB offers a Playwright spec, and the ONLY way a spec may reach the kit
|
|
3
|
+
* (ADR-0014 part 2).
|
|
4
|
+
*
|
|
5
|
+
* Playwright loads specs with Node, and Node neither strips types under `node_modules` nor resolves the app's
|
|
6
|
+
* `@sssb/app-config/*` seam the way a bundler does. So in the packed package this subpath resolves to compiled
|
|
7
|
+
* JavaScript (`dist/testing/e2e.js` and its `.d.ts`, built by `scripts/build-testing-e2e.mjs` when the package
|
|
8
|
+
* is packed), and everything it reaches must be SEAM-FREE: that build fails if any file in this
|
|
9
|
+
* entry's closure imports `@sssb/app-config/*`, or if the closure grows past the files it allows. Inside this repo
|
|
10
|
+
* the apps' tsconfig `paths` resolve `@sssb/kit/*` to source, so a spec here runs this file through Playwright's
|
|
11
|
+
* own TypeScript transform; the compiled copy is what an adopter's spec loads.
|
|
12
|
+
*
|
|
13
|
+
* Add a name here only if it is seam-free, and add its file to the build's allowed set deliberately.
|
|
14
|
+
*/
|
|
15
|
+
export { WEBHOOK_REPLAY_TOLERANCE_MS, verifyWebhookSignature } from '../core/webhook-hmac.js';
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// src/core/webhook-hmac.ts
|
|
2
|
+
var K = new Uint32Array([
|
|
3
|
+
1116352408,
|
|
4
|
+
1899447441,
|
|
5
|
+
3049323471,
|
|
6
|
+
3921009573,
|
|
7
|
+
961987163,
|
|
8
|
+
1508970993,
|
|
9
|
+
2453635748,
|
|
10
|
+
2870763221,
|
|
11
|
+
3624381080,
|
|
12
|
+
310598401,
|
|
13
|
+
607225278,
|
|
14
|
+
1426881987,
|
|
15
|
+
1925078388,
|
|
16
|
+
2162078206,
|
|
17
|
+
2614888103,
|
|
18
|
+
3248222580,
|
|
19
|
+
3835390401,
|
|
20
|
+
4022224774,
|
|
21
|
+
264347078,
|
|
22
|
+
604807628,
|
|
23
|
+
770255983,
|
|
24
|
+
1249150122,
|
|
25
|
+
1555081692,
|
|
26
|
+
1996064986,
|
|
27
|
+
2554220882,
|
|
28
|
+
2821834349,
|
|
29
|
+
2952996808,
|
|
30
|
+
3210313671,
|
|
31
|
+
3336571891,
|
|
32
|
+
3584528711,
|
|
33
|
+
113926993,
|
|
34
|
+
338241895,
|
|
35
|
+
666307205,
|
|
36
|
+
773529912,
|
|
37
|
+
1294757372,
|
|
38
|
+
1396182291,
|
|
39
|
+
1695183700,
|
|
40
|
+
1986661051,
|
|
41
|
+
2177026350,
|
|
42
|
+
2456956037,
|
|
43
|
+
2730485921,
|
|
44
|
+
2820302411,
|
|
45
|
+
3259730800,
|
|
46
|
+
3345764771,
|
|
47
|
+
3516065817,
|
|
48
|
+
3600352804,
|
|
49
|
+
4094571909,
|
|
50
|
+
275423344,
|
|
51
|
+
430227734,
|
|
52
|
+
506948616,
|
|
53
|
+
659060556,
|
|
54
|
+
883997877,
|
|
55
|
+
958139571,
|
|
56
|
+
1322822218,
|
|
57
|
+
1537002063,
|
|
58
|
+
1747873779,
|
|
59
|
+
1955562222,
|
|
60
|
+
2024104815,
|
|
61
|
+
2227730452,
|
|
62
|
+
2361852424,
|
|
63
|
+
2428436474,
|
|
64
|
+
2756734187,
|
|
65
|
+
3204031479,
|
|
66
|
+
3329325298
|
|
67
|
+
]);
|
|
68
|
+
function rotr(x, n) {
|
|
69
|
+
return x >>> n | x << 32 - n;
|
|
70
|
+
}
|
|
71
|
+
var SHA256_BLOCK_BYTES = 64;
|
|
72
|
+
function sha256(message) {
|
|
73
|
+
const h = new Uint32Array([
|
|
74
|
+
1779033703,
|
|
75
|
+
3144134277,
|
|
76
|
+
1013904242,
|
|
77
|
+
2773480762,
|
|
78
|
+
1359893119,
|
|
79
|
+
2600822924,
|
|
80
|
+
528734635,
|
|
81
|
+
1541459225
|
|
82
|
+
]);
|
|
83
|
+
const bitLen = message.length * 8;
|
|
84
|
+
const withPadLen = (message.length + 8 >> 6) + 1 << 6;
|
|
85
|
+
const padded = new Uint8Array(withPadLen);
|
|
86
|
+
padded.set(message);
|
|
87
|
+
padded[message.length] = 128;
|
|
88
|
+
const dv = new DataView(padded.buffer);
|
|
89
|
+
dv.setUint32(withPadLen - 4, bitLen >>> 0, false);
|
|
90
|
+
dv.setUint32(withPadLen - 8, Math.floor(bitLen / 4294967296), false);
|
|
91
|
+
const w = new Uint32Array(64);
|
|
92
|
+
for (let offset = 0; offset < padded.length; offset += SHA256_BLOCK_BYTES) {
|
|
93
|
+
for (let i = 0; i < 16; i++) w[i] = dv.getUint32(offset + i * 4, false);
|
|
94
|
+
for (let i = 16; i < 64; i++) {
|
|
95
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
|
|
96
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
|
|
97
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
98
|
+
}
|
|
99
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
100
|
+
for (let i = 0; i < 64; i++) {
|
|
101
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
102
|
+
const ch = e & f ^ ~e & g;
|
|
103
|
+
const t1 = hh + S1 + ch + K[i] + w[i] >>> 0;
|
|
104
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
105
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
106
|
+
const t2 = S0 + maj >>> 0;
|
|
107
|
+
hh = g;
|
|
108
|
+
g = f;
|
|
109
|
+
f = e;
|
|
110
|
+
e = d + t1 >>> 0;
|
|
111
|
+
d = c;
|
|
112
|
+
c = b;
|
|
113
|
+
b = a;
|
|
114
|
+
a = t1 + t2 >>> 0;
|
|
115
|
+
}
|
|
116
|
+
h[0] = h[0] + a >>> 0;
|
|
117
|
+
h[1] = h[1] + b >>> 0;
|
|
118
|
+
h[2] = h[2] + c >>> 0;
|
|
119
|
+
h[3] = h[3] + d >>> 0;
|
|
120
|
+
h[4] = h[4] + e >>> 0;
|
|
121
|
+
h[5] = h[5] + f >>> 0;
|
|
122
|
+
h[6] = h[6] + g >>> 0;
|
|
123
|
+
h[7] = h[7] + hh >>> 0;
|
|
124
|
+
}
|
|
125
|
+
const out = new Uint8Array(32);
|
|
126
|
+
const outView = new DataView(out.buffer);
|
|
127
|
+
for (let i = 0; i < 8; i++) outView.setUint32(i * 4, h[i], false);
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
function hmacSha256(key, message) {
|
|
131
|
+
let normKey = key;
|
|
132
|
+
if (normKey.length > SHA256_BLOCK_BYTES) normKey = sha256(normKey);
|
|
133
|
+
const block = new Uint8Array(SHA256_BLOCK_BYTES);
|
|
134
|
+
block.set(normKey);
|
|
135
|
+
const inner = new Uint8Array(SHA256_BLOCK_BYTES + message.length);
|
|
136
|
+
const outerPrefix = new Uint8Array(SHA256_BLOCK_BYTES);
|
|
137
|
+
for (let i = 0; i < SHA256_BLOCK_BYTES; i++) {
|
|
138
|
+
inner[i] = block[i] ^ 54;
|
|
139
|
+
outerPrefix[i] = block[i] ^ 92;
|
|
140
|
+
}
|
|
141
|
+
inner.set(message, SHA256_BLOCK_BYTES);
|
|
142
|
+
const innerHash = sha256(inner);
|
|
143
|
+
const outer = new Uint8Array(SHA256_BLOCK_BYTES + innerHash.length);
|
|
144
|
+
outer.set(outerPrefix);
|
|
145
|
+
outer.set(innerHash, SHA256_BLOCK_BYTES);
|
|
146
|
+
return sha256(outer);
|
|
147
|
+
}
|
|
148
|
+
var HEX = "0123456789abcdef";
|
|
149
|
+
function toHex(bytes) {
|
|
150
|
+
let out = "";
|
|
151
|
+
for (const byte of bytes) out += HEX[byte >> 4] + HEX[byte & 15];
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
var encoder = new TextEncoder();
|
|
155
|
+
var WEBHOOK_REPLAY_TOLERANCE_MS = 5 * 6e4;
|
|
156
|
+
function signWebhookBody(secret, body, timestampSec) {
|
|
157
|
+
const signed = `${timestampSec}.${body}`;
|
|
158
|
+
return toHex(hmacSha256(encoder.encode(secret), encoder.encode(signed)));
|
|
159
|
+
}
|
|
160
|
+
function parseSignatureHeader(header) {
|
|
161
|
+
let t = null;
|
|
162
|
+
let v1 = null;
|
|
163
|
+
for (const part of header.split(",")) {
|
|
164
|
+
const eq = part.indexOf("=");
|
|
165
|
+
if (eq < 0) continue;
|
|
166
|
+
const key = part.slice(0, eq).trim();
|
|
167
|
+
const value = part.slice(eq + 1).trim();
|
|
168
|
+
if (key === "t") {
|
|
169
|
+
const n = Number(value);
|
|
170
|
+
if (Number.isFinite(n)) t = n;
|
|
171
|
+
} else if (key === "v1") {
|
|
172
|
+
v1 = value;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return t !== null && v1 !== null ? { t, v1 } : null;
|
|
176
|
+
}
|
|
177
|
+
function timingSafeHexEqual(a, b) {
|
|
178
|
+
if (a.length !== b.length) return false;
|
|
179
|
+
let diff = 0;
|
|
180
|
+
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
181
|
+
return diff === 0;
|
|
182
|
+
}
|
|
183
|
+
function verifyWebhookSignature(body, header, secret, opts) {
|
|
184
|
+
if (!header) return { ok: false, reason: "malformed" };
|
|
185
|
+
const parsed = parseSignatureHeader(header);
|
|
186
|
+
if (!parsed) return { ok: false, reason: "malformed" };
|
|
187
|
+
const nowMs = opts.now?.getTime() ?? Date.now();
|
|
188
|
+
if (Math.abs(nowMs - parsed.t * 1e3) > opts.toleranceMs) return { ok: false, reason: "stale" };
|
|
189
|
+
const expected = signWebhookBody(secret, body, parsed.t);
|
|
190
|
+
return timingSafeHexEqual(expected, parsed.v1) ? { ok: true } : { ok: false, reason: "signature" };
|
|
191
|
+
}
|
|
192
|
+
export {
|
|
193
|
+
WEBHOOK_REPLAY_TOLERANCE_MS,
|
|
194
|
+
verifyWebhookSignature
|
|
195
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,212 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sssb/kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Reserved for SSSB, a web app foundation maintained by SafeInsights. Not yet released.",
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"license": "MIT",
|
|
6
|
-
"repository": {
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/safeinsights/sssb.git",
|
|
8
|
+
"directory": "packages/kit"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"description": "SSSB — the framework half of this template: ports/adapters, RLS tenancy, authz, jobs, service auth, i18n and the Simulator harness. Apps extend it through the @sssb/app-config/* seam (ADR-0012).",
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!src/**/*.stories.*",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
"./i18n/messages/en.json": "./src/i18n/messages/en.json",
|
|
21
|
+
"./i18n/messages/es.json": "./src/i18n/messages/es.json",
|
|
22
|
+
"./adapters/fake/agreements": "./src/adapters/fake/agreements.ts",
|
|
23
|
+
"./adapters/fake/analytics": "./src/adapters/fake/analytics.ts",
|
|
24
|
+
"./adapters/fake/audit": "./src/adapters/fake/audit.ts",
|
|
25
|
+
"./adapters/fake/auth": "./src/adapters/fake/auth.ts",
|
|
26
|
+
"./adapters/fake/simulator": "./src/adapters/fake/simulator.ts",
|
|
27
|
+
"./adapters/fake/simulator-admin": "./src/adapters/fake/simulator-admin.ts",
|
|
28
|
+
"./adapters/fake/clock": "./src/adapters/fake/clock.ts",
|
|
29
|
+
"./adapters/fake/db": "./src/adapters/fake/db.ts",
|
|
30
|
+
"./adapters/fake/email": "./src/adapters/fake/email.ts",
|
|
31
|
+
"./adapters/fake/jobs": "./src/adapters/fake/jobs.ts",
|
|
32
|
+
"./adapters/fake/llm": "./src/adapters/fake/llm.ts",
|
|
33
|
+
"./adapters/fake/service-auth": "./src/adapters/fake/service-auth.ts",
|
|
34
|
+
"./adapters/fake/sms": "./src/adapters/fake/sms.ts",
|
|
35
|
+
"./adapters/fake/storage": "./src/adapters/fake/storage.ts",
|
|
36
|
+
"./adapters/fake/webhooks": "./src/adapters/fake/webhooks.ts",
|
|
37
|
+
"./adapters/index": "./src/adapters/index.ts",
|
|
38
|
+
"./adapters/real/accept-invite-form": "./src/adapters/real/accept-invite-form.tsx",
|
|
39
|
+
"./adapters/real/index": "./src/adapters/real/index.ts",
|
|
40
|
+
"./adapters/real/llm": "./src/adapters/real/llm.ts",
|
|
41
|
+
"./adapters/real/migrate-handler": "./src/adapters/real/migrate-handler.ts",
|
|
42
|
+
"./adapters/real/providers": "./src/adapters/real/providers.tsx",
|
|
43
|
+
"./adapters/real/s3-storage": "./src/adapters/real/s3-storage.ts",
|
|
44
|
+
"./adapters/real/sign-in-form": "./src/adapters/real/sign-in-form.tsx",
|
|
45
|
+
"./adapters/sentry/client": "./src/adapters/sentry/client.ts",
|
|
46
|
+
"./adapters/sentry/init": "./src/adapters/sentry/init.ts",
|
|
47
|
+
"./authz/authorize": "./src/authz/authorize.ts",
|
|
48
|
+
"./components/agreements/acceptances-section": "./src/components/agreements/acceptances-section.tsx",
|
|
49
|
+
"./components/agreements/agreement-advisory-banner": "./src/components/agreements/agreement-advisory-banner.tsx",
|
|
50
|
+
"./components/agreements/agreement-gate": "./src/components/agreements/agreement-gate.tsx",
|
|
51
|
+
"./components/app-header": "./src/components/app-header.tsx",
|
|
52
|
+
"./components/auth/accept-invite-screen": "./src/components/auth/accept-invite-screen.tsx",
|
|
53
|
+
"./components/auth/org-switcher": "./src/components/auth/org-switcher.tsx",
|
|
54
|
+
"./components/auth/people-picker": "./src/components/auth/people-picker.tsx",
|
|
55
|
+
"./components/auth/sign-in-screen": "./src/components/auth/sign-in-screen.tsx",
|
|
56
|
+
"./components/auth/user-menu": "./src/components/auth/user-menu.tsx",
|
|
57
|
+
"./components/simulator/actor-runtime": "./src/components/simulator/actor-runtime.ts",
|
|
58
|
+
"./components/simulator/actor-shell": "./src/components/simulator/actor-shell.tsx",
|
|
59
|
+
"./components/simulator/actors-app": "./src/components/simulator/actors-app.tsx",
|
|
60
|
+
"./components/simulator/simulator-panel": "./src/components/simulator/simulator-panel.tsx",
|
|
61
|
+
"./components/simulator/people-app": "./src/components/simulator/people-app.tsx",
|
|
62
|
+
"./components/simulator/errors-app": "./src/components/simulator/errors-app.tsx",
|
|
63
|
+
"./components/simulator/events-app": "./src/components/simulator/events-app.tsx",
|
|
64
|
+
"./components/simulator/hooks-app": "./src/components/simulator/hooks-app.tsx",
|
|
65
|
+
"./components/simulator/inbound-app": "./src/components/simulator/inbound-app.tsx",
|
|
66
|
+
"./components/simulator/jobs-app": "./src/components/simulator/jobs-app.tsx",
|
|
67
|
+
"./components/simulator/mail-app": "./src/components/simulator/mail-app.tsx",
|
|
68
|
+
"./components/simulator/messages-app": "./src/components/simulator/messages-app.tsx",
|
|
69
|
+
"./components/simulator/snapshots-app": "./src/components/simulator/snapshots-app.tsx",
|
|
70
|
+
"./components/demo-badge": "./src/components/demo-badge.tsx",
|
|
71
|
+
"./components/demo-banner": "./src/components/demo-banner.tsx",
|
|
72
|
+
"./components/gates/gate-interstitial": "./src/components/gates/gate-interstitial.tsx",
|
|
73
|
+
"./components/job-timeline": "./src/components/job-timeline.tsx",
|
|
74
|
+
"./components/notifications/notification-bell": "./src/components/notifications/notification-bell.tsx",
|
|
75
|
+
"./components/notifications/notification-prefs-section": "./src/components/notifications/notification-prefs-section.tsx",
|
|
76
|
+
"./components/org-screen": "./src/components/org-screen.tsx",
|
|
77
|
+
"./components/profile-screen": "./src/components/profile-screen.tsx",
|
|
78
|
+
"./components/webhook-endpoints-card": "./src/components/webhook-endpoints-card.tsx",
|
|
79
|
+
"./core/abilities": "./src/core/abilities.ts",
|
|
80
|
+
"./core/agreements": "./src/core/agreements.ts",
|
|
81
|
+
"./core/gates": "./src/core/gates.ts",
|
|
82
|
+
"./core/greeting": "./src/core/greeting.ts",
|
|
83
|
+
"./core/inbound-email": "./src/core/inbound-email.ts",
|
|
84
|
+
"./core/jobs": "./src/core/jobs.ts",
|
|
85
|
+
"./core/keyset": "./src/core/keyset.ts",
|
|
86
|
+
"./core/llm-models": "./src/core/llm-models.ts",
|
|
87
|
+
"./core/locale": "./src/core/locale.ts",
|
|
88
|
+
"./core/notifications": "./src/core/notifications.ts",
|
|
89
|
+
"./core/presets": "./src/core/presets.ts",
|
|
90
|
+
"./core/roles": "./src/core/roles.ts",
|
|
91
|
+
"./core/schedules": "./src/core/schedules.ts",
|
|
92
|
+
"./core/state-machine": "./src/core/state-machine.ts",
|
|
93
|
+
"./core/webhook-events": "./src/core/webhook-events.ts",
|
|
94
|
+
"./core/webhook-signing": "./src/core/webhook-signing.ts",
|
|
95
|
+
"./cutover/rows.json": "./src/cutover/rows.json",
|
|
96
|
+
"./db/agreements": "./src/db/agreements.ts",
|
|
97
|
+
"./db/audit": "./src/db/audit.ts",
|
|
98
|
+
"./db/inbound-email": "./src/db/inbound-email.ts",
|
|
99
|
+
"./db/jobs": "./src/db/jobs.ts",
|
|
100
|
+
"./db/jsonb": "./src/db/jsonb.ts",
|
|
101
|
+
"./db/keyset": "./src/db/keyset.ts",
|
|
102
|
+
"./db/migrate": "./src/db/migrate.ts",
|
|
103
|
+
"./db/notifications": "./src/db/notifications.ts",
|
|
104
|
+
"./db/org-lookup": "./src/db/org-lookup.ts",
|
|
105
|
+
"./db/owner-pass": "./src/db/owner-pass.ts",
|
|
106
|
+
"./db/rls-proof-runner": "./src/db/rls-proof-runner.ts",
|
|
107
|
+
"./db/rls-proofs": "./src/db/rls-proofs.ts",
|
|
108
|
+
"./db/schedules": "./src/db/schedules.ts",
|
|
109
|
+
"./db/schema": "./src/db/schema.ts",
|
|
110
|
+
"./db/tenant-lookup": "./src/db/tenant-lookup.ts",
|
|
111
|
+
"./db/webhooks": "./src/db/webhooks.ts",
|
|
112
|
+
"./db/with-tenant": "./src/db/with-tenant.ts",
|
|
113
|
+
"./demo-static/assistant-replay": "./src/demo-static/assistant-replay.ts",
|
|
114
|
+
"./demo-static/contracts": "./src/demo-static/contracts.ts",
|
|
115
|
+
"./demo-static/hash-route": "./src/demo-static/hash-route.ts",
|
|
116
|
+
"./demo-static/shell": "./src/demo-static/shell.tsx",
|
|
117
|
+
"./demo-static/tour/contracts": "./src/demo-static/tour/contracts.ts",
|
|
118
|
+
"./demo-static/tour/use-tours": "./src/demo-static/tour/use-tours.tsx",
|
|
119
|
+
"./demo-static/world": "./src/demo-static/world.ts",
|
|
120
|
+
"./email/send": "./src/email/send.ts",
|
|
121
|
+
"./email/templates/digest-email": "./src/email/templates/digest-email.tsx",
|
|
122
|
+
"./email/templates/invite-email": "./src/email/templates/invite-email.tsx",
|
|
123
|
+
"./email/templates/notification-email": "./src/email/templates/notification-email.tsx",
|
|
124
|
+
"./guide.md": "./src/guide.md",
|
|
125
|
+
"./i18n/messages": "./src/i18n/messages.ts",
|
|
126
|
+
"./i18n/request": "./src/i18n/request.ts",
|
|
127
|
+
"./i18n/routing": "./src/i18n/routing.ts",
|
|
128
|
+
"./inbound-email/handlers": "./src/inbound-email/handlers.ts",
|
|
129
|
+
"./inbound-email/intake": "./src/inbound-email/intake.ts",
|
|
130
|
+
"./jobs/digest-email": "./src/jobs/digest-email.ts",
|
|
131
|
+
"./observability/scrub": "./src/observability/scrub.ts",
|
|
132
|
+
"./ports/analytics": "./src/ports/analytics.ts",
|
|
133
|
+
"./ports/auth": "./src/ports/auth.ts",
|
|
134
|
+
"./ports/db": "./src/ports/db.ts",
|
|
135
|
+
"./ports/email": "./src/ports/email.ts",
|
|
136
|
+
"./ports/errors": "./src/ports/errors.ts",
|
|
137
|
+
"./ports/jobs": "./src/ports/jobs.ts",
|
|
138
|
+
"./ports/llm": "./src/ports/llm.ts",
|
|
139
|
+
"./ports/storage": "./src/ports/storage.ts",
|
|
140
|
+
"./seed/contracts": "./src/seed/contracts.ts",
|
|
141
|
+
"./server-lib/assistant": "./src/server-lib/assistant.ts",
|
|
142
|
+
"./server-lib/defer": "./src/server-lib/defer.ts",
|
|
143
|
+
"./server-lib/demo-presets": "./src/server-lib/demo-presets.ts",
|
|
144
|
+
"./server-lib/invite": "./src/server-lib/invite.ts",
|
|
145
|
+
"./server-lib/notify": "./src/server-lib/notify.ts",
|
|
146
|
+
"./server-lib/notify-deps": "./src/server-lib/notify-deps.ts",
|
|
147
|
+
"./server-lib/preset-operations": "./src/server-lib/preset-operations.ts",
|
|
148
|
+
"./server-lib/scheduled-tick": "./src/server-lib/scheduled-tick.ts",
|
|
149
|
+
"./server-lib/sms": "./src/server-lib/sms.ts",
|
|
150
|
+
"./server-lib/webhook-dispatch": "./src/server-lib/webhook-dispatch.ts",
|
|
151
|
+
"./service-auth/mailgun": "./src/service-auth/mailgun.ts",
|
|
152
|
+
"./service-auth/verify": "./src/service-auth/verify.ts",
|
|
153
|
+
"./service-auth/webhook": "./src/service-auth/webhook.ts",
|
|
154
|
+
"./testing": "./src/testing/index.ts",
|
|
155
|
+
"./testing/checks.json": "./src/testing/checks.json",
|
|
156
|
+
"./testing/contract": "./src/testing/contract.ts",
|
|
157
|
+
"./testing/e2e": "./dist/testing/e2e.js",
|
|
158
|
+
"./theme": "./src/theme.ts"
|
|
159
|
+
},
|
|
160
|
+
"dependencies": {
|
|
161
|
+
"@anthropic-ai/sdk": "^1.8.0",
|
|
162
|
+
"@aws-sdk/client-codebuild": "^3.1155.0",
|
|
163
|
+
"@aws-sdk/client-rds-data": "^3.1169.0",
|
|
164
|
+
"@aws-sdk/client-s3": "^3.1155.0",
|
|
165
|
+
"@aws-sdk/client-secrets-manager": "^3.1155.0",
|
|
166
|
+
"@aws-sdk/s3-presigned-post": "^3.1155.0",
|
|
167
|
+
"@aws-sdk/s3-request-presigner": "^3.1155.0",
|
|
168
|
+
"@clerk/nextjs": "^7.9.19",
|
|
169
|
+
"@electric-sql/pglite": "^0.5.11",
|
|
170
|
+
"@mantine/form": "^9.7.3",
|
|
171
|
+
"@react-email/components": "^1.0.12",
|
|
172
|
+
"@react-email/render": "^2.1.1",
|
|
173
|
+
"@sentry/nextjs": "^11.12.0",
|
|
174
|
+
"@types/pg": "^8.23.1",
|
|
175
|
+
"jose": "^6.2.13",
|
|
176
|
+
"mailgun.js": "^14.1.1",
|
|
177
|
+
"pg": "^8.23.1",
|
|
178
|
+
"posthog-node": "^5.56.4",
|
|
179
|
+
"server-only": "^0.0.1",
|
|
180
|
+
"valibot": "^1.5.0"
|
|
181
|
+
},
|
|
182
|
+
"peerDependencies": {
|
|
183
|
+
"@mantine/core": "^9.7.3",
|
|
184
|
+
"@types/node": "^26.6.5",
|
|
185
|
+
"@types/react": "~19.3.0",
|
|
186
|
+
"embedded-postgres": "^18.4.0-beta.17",
|
|
187
|
+
"kysely": "^0.29.7",
|
|
188
|
+
"next": "^16.4.6",
|
|
189
|
+
"next-intl": "^4.14.9",
|
|
190
|
+
"react": "~19.3.1",
|
|
191
|
+
"typescript": "~6.0.3",
|
|
192
|
+
"vitest": "^4.1.12"
|
|
193
|
+
},
|
|
194
|
+
"peerDependenciesMeta": {
|
|
195
|
+
"@sssb/app-config": {
|
|
196
|
+
"optional": true
|
|
197
|
+
},
|
|
198
|
+
"embedded-postgres": {
|
|
199
|
+
"optional": true
|
|
200
|
+
},
|
|
201
|
+
"vitest": {
|
|
202
|
+
"optional": true
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
"devDependencies": {
|
|
206
|
+
"embedded-postgres": "18.4.0-beta.17",
|
|
207
|
+
"esbuild": "^0.28.2",
|
|
208
|
+
"typescript": "~6.0.3",
|
|
209
|
+
"vitest": "^4.1.12"
|
|
210
|
+
},
|
|
211
|
+
"scripts": {}
|
|
212
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# packages/kit/src/adapters
|
|
2
|
+
|
|
3
|
+
SSSB's adapters: one of the two directories allowed to import vendor SDKs (lint-enforced). The other is
|
|
4
|
+
an app's own `apps/<app>/src/adapters/real/`, for a vendor SSSB has no port for
|
|
5
|
+
(`docs/recipes/app-owned-adapter.md`). Each port gets a real adapter and a fake adapter; fakes are
|
|
6
|
+
first-class (dev, demo, and E2E all run on them).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The run-mode facts `simulatedDevServer` decides from, all read once in ./index.ts (where process.env is read). */
|
|
2
|
+
export interface RunFacts {
|
|
3
|
+
isSimulated: boolean
|
|
4
|
+
/** `process.env.NODE_ENV`: `next dev` sets `development`; every `next build` (deployed, demo, e2e) is `production`. */
|
|
5
|
+
nodeEnv: string | undefined
|
|
6
|
+
isDemoMode: boolean
|
|
7
|
+
isE2eBuild: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether this server is a developer's own `next dev` of a simulated world: the one place a dev-only Simulator
|
|
12
|
+
* affordance (the dev ticker control, `apps/showcase/src/app/api/simulator/ticker`) may exist.
|
|
13
|
+
*
|
|
14
|
+
* Every condition is load-bearing, and each is a place the affordance must never reach: real mode (a deployment,
|
|
15
|
+
* or `dev:real` against cloud resources); a production build, which is what a deployment, the hosted demo and CI's
|
|
16
|
+
* e2e run all are; and the demo and e2e switches themselves, so `DEMO_MODE=1 next dev` or `E2E_BUILD=1 next dev`
|
|
17
|
+
* stays out too. The static demo has no server, so it never asks.
|
|
18
|
+
*/
|
|
19
|
+
export function simulatedDevServer(run: RunFacts): boolean {
|
|
20
|
+
return run.isSimulated && run.nodeEnv === 'development' && !run.isDemoMode && !run.isE2eBuild
|
|
21
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { bumpAgreementVersion } from '../../db/agreements'
|
|
2
|
+
import { fakeDb } from './db'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Simulated-mode Simulator god operations for agreements — the Snapshots tab's Agreements section.
|
|
6
|
+
* Reads are a raw cross-tenant getDb() view (the listWorldJobs / listWorldAuditEvents precedent,
|
|
7
|
+
* deliberately reaching past the tenant RLS scope, simulated-mode-only); the bump reuses the real
|
|
8
|
+
* tenant-scoped db module (withTenant → app_user UPDATE), so the demo drives the SAME write path a
|
|
9
|
+
* real admin agreements UI would. Neither is part of any port.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** One row of the simulated-mode cross-tenant agreements god view. */
|
|
13
|
+
export interface WorldAgreement {
|
|
14
|
+
id: string
|
|
15
|
+
tenantSlug: string
|
|
16
|
+
kind: string
|
|
17
|
+
title: string
|
|
18
|
+
version: number
|
|
19
|
+
gating: string
|
|
20
|
+
/** How many acceptances exist at the CURRENT version (i.e. who is up to date). */
|
|
21
|
+
currentAcceptances: number
|
|
22
|
+
/** Total acceptance rows ever recorded for this agreement (across versions). */
|
|
23
|
+
totalAcceptances: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Every agreement across ALL tenants with acceptance tallies, for the Snapshots Agreements section. */
|
|
27
|
+
export async function listWorldAgreements(): Promise<WorldAgreement[]> {
|
|
28
|
+
await fakeDb.ready()
|
|
29
|
+
const db = fakeDb.getDb()
|
|
30
|
+
const agreements = await db
|
|
31
|
+
.selectFrom('agreements')
|
|
32
|
+
.innerJoin('tenants', 'tenants.id', 'agreements.tenant_id')
|
|
33
|
+
.select([
|
|
34
|
+
'agreements.id as id',
|
|
35
|
+
'agreements.kind as kind',
|
|
36
|
+
'agreements.title as title',
|
|
37
|
+
'agreements.version as version',
|
|
38
|
+
'agreements.gating as gating',
|
|
39
|
+
'tenants.slug as tenant_slug',
|
|
40
|
+
])
|
|
41
|
+
.orderBy('tenants.slug', 'asc')
|
|
42
|
+
.orderBy('agreements.created_at', 'asc')
|
|
43
|
+
.execute()
|
|
44
|
+
|
|
45
|
+
const acceptances = await db
|
|
46
|
+
.selectFrom('agreement_acceptances')
|
|
47
|
+
.select(['agreement_id', 'agreement_version'])
|
|
48
|
+
.execute()
|
|
49
|
+
|
|
50
|
+
return agreements.map((a) => {
|
|
51
|
+
const version = Number(a.version)
|
|
52
|
+
const rows = acceptances.filter((x) => x.agreement_id === a.id)
|
|
53
|
+
return {
|
|
54
|
+
id: a.id,
|
|
55
|
+
tenantSlug: a.tenant_slug,
|
|
56
|
+
kind: a.kind,
|
|
57
|
+
title: a.title,
|
|
58
|
+
version,
|
|
59
|
+
gating: a.gating,
|
|
60
|
+
currentAcceptances: rows.filter((x) => Number(x.agreement_version) >= version).length,
|
|
61
|
+
totalAcceptances: rows.length,
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Bump one agreement's version (the demo story). Resolves the agreement's tenant with a raw god read,
|
|
68
|
+
* then delegates to the real tenant-scoped db bump. Returns the new version, or null when the id is
|
|
69
|
+
* unknown.
|
|
70
|
+
*/
|
|
71
|
+
export async function bumpWorldAgreement(agreementId: string): Promise<{ version: number } | null> {
|
|
72
|
+
await fakeDb.ready()
|
|
73
|
+
const row = await fakeDb
|
|
74
|
+
.getDb()
|
|
75
|
+
.selectFrom('agreements')
|
|
76
|
+
.select('tenant_id')
|
|
77
|
+
.where('id', '=', agreementId)
|
|
78
|
+
.executeTakeFirst()
|
|
79
|
+
if (!row) return null
|
|
80
|
+
return bumpAgreementVersion(fakeDb, row.tenant_id, agreementId, new Date())
|
|
81
|
+
}
|