From 62ba2bc77f6c30e9252abaadd89d6eedeae736ca Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 8 Sep 2026 17:20:18 -0400 Subject: [PATCH 01/18] Add Resend email submodule and example --- pnpm-lock.yaml | 122 +++ spacetime-resend-ts/.npmrc | 1 + spacetime-resend-ts/LICENSE.txt | 759 +++++++++++++++ spacetime-resend-ts/README.md | 234 +++++ spacetime-resend-ts/example/.env.example | 28 + spacetime-resend-ts/example/.npmrc | 1 + spacetime-resend-ts/example/README.md | 181 ++++ spacetime-resend-ts/example/package.json | 30 + spacetime-resend-ts/example/public/index.html | 151 +++ spacetime-resend-ts/example/public/styles.css | 885 ++++++++++++++++++ .../example/scripts/test-message.ts | 15 + spacetime-resend-ts/example/server.ts | 237 +++++ .../example/spacetimedb/.npmrc | 1 + .../example/spacetimedb/package.json | 20 + .../example/spacetimedb/src/index.ts | 267 ++++++ .../example/spacetimedb/src/message.ts | 20 + .../example/spacetimedb/tsconfig.json | 15 + spacetime-resend-ts/example/src/app.ts | 673 +++++++++++++ .../clear_dispatches_procedure.ts | 19 + .../delete_dispatch_procedure.ts | 20 + .../example/src/module_bindings/index.ts | 239 +++++ .../my_dispatch_delivery_events_table.ts | 20 + .../my_dispatch_emails_table.ts | 45 + .../rateLimit/add_rate_limit_admin_reducer.ts | 15 + .../admin_rate_limit_buckets_table.ts | 20 + .../rateLimit/consume_procedure.ts | 24 + .../rateLimit/rate_limit_config_table.ts | 17 + .../rateLimit/reset_buckets_reducer.ts | 15 + .../rateLimit/run_sweep_procedure.ts | 16 + .../src/module_bindings/rateLimit/types.ts | 56 ++ .../rateLimit/update_config_reducer.ts | 15 + .../resend/add_admin_identity_procedure.ts | 16 + .../resend/cancel_email_procedure.ts | 16 + .../resend/get_email_procedure.ts | 20 + .../get_resend_config_status_procedure.ts | 19 + .../resend/ingest_resend_webhook_reducer.ts | 19 + ...ist_delivery_events_for_email_procedure.ts | 20 + .../resend/list_emails_by_org_id_procedure.ts | 20 + .../resend/list_emails_by_status_procedure.ts | 23 + .../list_emails_by_user_id_procedure.ts | 20 + .../resend/remove_admin_identity_procedure.ts | 16 + .../resend/replay_webhook_event_reducer.ts | 15 + .../resend/resend_api_request_procedure.ts | 23 + .../resend/send_email_procedure.ts | 31 + .../resend/set_resend_config_procedure.ts | 18 + .../src/module_bindings/resend/types.ts | 123 +++ .../send_dispatch_procedure.ts | 22 + .../set_dispatch_policy_procedure.ts | 16 + .../example/src/module_bindings/types.ts | 91 ++ .../src/module_bindings/types/procedures.ts | 22 + .../src/module_bindings/types/reducers.ts | 10 + spacetime-resend-ts/example/tsconfig.json | 17 + spacetime-resend-ts/package.json | 71 ++ .../scripts/test-resend-smoke.ts | 466 +++++++++ spacetime-resend-ts/scripts/test-unit.ts | 98 ++ spacetime-resend-ts/src/index.ts | 21 + spacetime-resend-ts/src/submodule.ts | 15 + spacetime-resend-ts/src/submodule/auth.ts | 80 ++ spacetime-resend-ts/src/submodule/config.ts | 112 +++ .../src/submodule/email-input.ts | 90 ++ .../src/submodule/email_writes.ts | 77 ++ spacetime-resend-ts/src/submodule/http.ts | 66 ++ spacetime-resend-ts/src/submodule/install.ts | 9 + .../src/submodule/operations.ts | 376 ++++++++ spacetime-resend-ts/src/submodule/request.ts | 67 ++ spacetime-resend-ts/src/submodule/schema.ts | 293 ++++++ .../src/submodule/text-validation.ts | 7 + .../src/submodule/validation.ts | 45 + .../src/submodule/webhook-metadata.ts | 13 + spacetime-resend-ts/src/submodule/webhooks.ts | 396 ++++++++ spacetime-resend-ts/tsconfig.json | 22 + 71 files changed, 7062 insertions(+) create mode 100644 spacetime-resend-ts/.npmrc create mode 100644 spacetime-resend-ts/LICENSE.txt create mode 100644 spacetime-resend-ts/README.md create mode 100644 spacetime-resend-ts/example/.env.example create mode 100644 spacetime-resend-ts/example/.npmrc create mode 100644 spacetime-resend-ts/example/README.md create mode 100644 spacetime-resend-ts/example/package.json create mode 100644 spacetime-resend-ts/example/public/index.html create mode 100644 spacetime-resend-ts/example/public/styles.css create mode 100644 spacetime-resend-ts/example/scripts/test-message.ts create mode 100644 spacetime-resend-ts/example/server.ts create mode 100644 spacetime-resend-ts/example/spacetimedb/.npmrc create mode 100644 spacetime-resend-ts/example/spacetimedb/package.json create mode 100644 spacetime-resend-ts/example/spacetimedb/src/index.ts create mode 100644 spacetime-resend-ts/example/spacetimedb/src/message.ts create mode 100644 spacetime-resend-ts/example/spacetimedb/tsconfig.json create mode 100644 spacetime-resend-ts/example/src/app.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/clear_dispatches_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/delete_dispatch_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/index.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/my_dispatch_delivery_events_table.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/my_dispatch_emails_table.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/add_rate_limit_admin_reducer.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/admin_rate_limit_buckets_table.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/rate_limit_config_table.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/reset_buckets_reducer.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/run_sweep_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/update_config_reducer.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/add_admin_identity_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/cancel_email_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/get_email_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/get_resend_config_status_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/ingest_resend_webhook_reducer.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/list_delivery_events_for_email_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_org_id_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_status_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_user_id_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/remove_admin_identity_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/replay_webhook_event_reducer.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/resend_api_request_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/send_email_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/set_resend_config_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/types.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/send_dispatch_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/set_dispatch_policy_procedure.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/types.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/types/procedures.ts create mode 100644 spacetime-resend-ts/example/src/module_bindings/types/reducers.ts create mode 100644 spacetime-resend-ts/example/tsconfig.json create mode 100644 spacetime-resend-ts/package.json create mode 100644 spacetime-resend-ts/scripts/test-resend-smoke.ts create mode 100644 spacetime-resend-ts/scripts/test-unit.ts create mode 100644 spacetime-resend-ts/src/index.ts create mode 100644 spacetime-resend-ts/src/submodule.ts create mode 100644 spacetime-resend-ts/src/submodule/auth.ts create mode 100644 spacetime-resend-ts/src/submodule/config.ts create mode 100644 spacetime-resend-ts/src/submodule/email-input.ts create mode 100644 spacetime-resend-ts/src/submodule/email_writes.ts create mode 100644 spacetime-resend-ts/src/submodule/http.ts create mode 100644 spacetime-resend-ts/src/submodule/install.ts create mode 100644 spacetime-resend-ts/src/submodule/operations.ts create mode 100644 spacetime-resend-ts/src/submodule/request.ts create mode 100644 spacetime-resend-ts/src/submodule/schema.ts create mode 100644 spacetime-resend-ts/src/submodule/text-validation.ts create mode 100644 spacetime-resend-ts/src/submodule/validation.ts create mode 100644 spacetime-resend-ts/src/submodule/webhook-metadata.ts create mode 100644 spacetime-resend-ts/src/submodule/webhooks.ts create mode 100644 spacetime-resend-ts/tsconfig.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2ad8187bffa..b1b3152a410 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -534,6 +534,87 @@ importers: specifier: ^5.9.3 version: 5.9.3 + spacetime-resend-ts: + dependencies: + '@spacetimedb/crypto': + specifier: workspace:^ + version: link:../spacetime-crypto-ts + valibot: + specifier: ^1.4.2 + version: 1.4.2(typescript@5.9.3) + devDependencies: + '@types/node': + specifier: ^22.10.2 + version: 22.18.0 + eslint: + specifier: ^9.17.0 + version: 9.33.0(jiti@2.6.1) + prettier: + specifier: ^3.3.3 + version: 3.6.2 + spacetimedb: + specifier: workspace:* + version: link:../crates/bindings-typescript + tsx: + specifier: ^4.21.0 + version: 4.23.12 + typescript: + specifier: ^5.9.3 + version: 5.9.3 + + spacetime-resend-ts/example: + dependencies: + '@spacetimedb/submodule-shared': + specifier: workspace:* + version: link:../../spacetime-submodule-shared-ts + dotenv: + specifier: ^16.4.7 + version: 16.6.1 + express: + specifier: ^4.21.2 + version: 4.21.2 + spacetimedb: + specifier: workspace:* + version: link:../../crates/bindings-typescript + devDependencies: + '@types/express': + specifier: ^4.17.21 + version: 4.17.23 + '@types/node': + specifier: ^22.10.2 + version: 22.18.0 + esbuild: + specifier: ^0.28.0 + version: 0.28.2 + resend: + specifier: ^6.12.2 + version: 6.22.1 + tsx: + specifier: ^4.21.0 + version: 4.23.12 + typescript: + specifier: ^5.9.3 + version: 5.9.3 + + spacetime-resend-ts/example/spacetimedb: + dependencies: + '@spacetimedb/rate-limit': + specifier: workspace:* + version: link:../../../spacetime-rate-limit-ts + '@spacetimedb/resend': + specifier: workspace:* + version: link:../.. + spacetimedb: + specifier: workspace:* + version: link:../../../crates/bindings-typescript + devDependencies: + '@types/node': + specifier: ^22.10.2 + version: 22.18.0 + typescript: + specifier: ^5.9.3 + version: 5.9.3 + spacetime-retry-ts: devDependencies: '@types/node': @@ -6225,6 +6306,9 @@ packages: '@speed-highlight/core@1.2.14': resolution: {integrity: sha512-G4ewlBNhUtlLvrJTb88d2mdy2KRijzs4UhnlrOSRT4bmjh/IqNElZa3zkrZ+TC47TwtlDWzVLFADljF1Ijp5hA==} + '@stablelib/base64@1.0.1': + resolution: {integrity: sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==} + '@standard-schema/spec@1.0.0': resolution: {integrity: sha512-m2bOd0f2RT9k8QJx1JN85cZYyH1RqFBdlwtkSlf4tBDYLCiiZnv1fIIwacK6cqwXavOydf0NPToMQgpKq+dVlA==} @@ -9420,6 +9504,9 @@ packages: fast-npm-meta@0.4.8: resolution: {integrity: sha512-ybZVlDZ2PkO79dosM+6CLZfKWRH8MF0PiWlw8M4mVWJl8IEJrPfxYc7Tsu830Dwj/R96LKXfePGTSzKWbPJ08w==} + fast-sha256@1.3.0: + resolution: {integrity: sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==} + fast-uri@3.1.0: resolution: {integrity: sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA==} @@ -12070,6 +12157,9 @@ packages: resolution: {integrity: sha512-/+5VFTchJDoVj3bhoqi6UeymcD00DAwb1nJwamzPvHEszJ4FpF6SNNbUbOS8yI56qHzdV8eK0qEfOSiodkTdxg==} engines: {node: '>= 0.4'} + postal-mime@2.7.5: + resolution: {integrity: sha512-GNEXKvWFQnbgO5NlrGzVa0FmWzBZ24PersAWErttSg1Hjpf0ATxTwS5DOMGaOpTG6bUh5cTr7xi0jAD942wCJA==} + postcss-attribute-case-insensitive@7.0.1: resolution: {integrity: sha512-Uai+SupNSqzlschRyNx3kbCTWgY/2hcwtHEI/ej2LJWc9JJ77qKgGptd8DHwY1mXtZ7Aoh4z4yxfwMBue9eNgw==} engines: {node: '>=18'} @@ -13227,6 +13317,15 @@ packages: requires-port@1.0.0: resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==} + resend@6.22.1: + resolution: {integrity: sha512-VqYhB6zqsifujJfZsiTKBVcZaKih0XitdXHCte4x4d7KH4D0QvPx5GvqcypCn2y64tK0C/6OskpQvsaSJaHHBw==} + engines: {node: '>=20'} + peerDependencies: + '@react-email/render': '*' + peerDependenciesMeta: + '@react-email/render': + optional: true + reserved-identifiers@1.2.0: resolution: {integrity: sha512-yE7KUfFvaBFzGPs5H3Ops1RevfUEsDc5Iz65rOwWg4lE8HJSYtle77uul3+573457oHvBKuHYDl/xqUkKpEEdw==} engines: {node: '>=18'} @@ -13719,6 +13818,9 @@ packages: standard-as-callback@2.1.0: resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} + standardwebhooks@1.0.0: + resolution: {integrity: sha512-BbHGOQK9olHPMvQNHWul6MYlrRTAOKn03rOe4A8O3CLWhNf4YHBqq2HJKKC+sfqpxiBY52pNeesD6jIiLDz8jg==} + statuses@1.5.0: resolution: {integrity: sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==} engines: {node: '>= 0.6'} @@ -21436,6 +21538,8 @@ snapshots: '@speed-highlight/core@1.2.14': {} + '@stablelib/base64@1.0.1': {} + '@standard-schema/spec@1.0.0': {} '@supabase/auth-js@2.97.0': @@ -25903,6 +26007,8 @@ snapshots: fast-npm-meta@0.4.8: {} + fast-sha256@1.3.0: {} + fast-uri@3.1.0: {} fastq@1.19.1: @@ -29458,6 +29564,8 @@ snapshots: possible-typed-array-names@1.1.0: {} + postal-mime@2.7.5: {} + postcss-attribute-case-insensitive@7.0.1(postcss@8.5.6): dependencies: postcss: 8.5.6 @@ -30757,6 +30865,11 @@ snapshots: requires-port@1.0.0: {} + resend@6.22.1: + dependencies: + postal-mime: 2.7.5 + standardwebhooks: 1.0.0 + reserved-identifiers@1.2.0: {} resolve-alpn@1.2.1: {} @@ -31374,6 +31487,11 @@ snapshots: standard-as-callback@2.1.0: {} + standardwebhooks@1.0.0: + dependencies: + '@stablelib/base64': 1.0.1 + fast-sha256: 1.3.0 + statuses@1.5.0: {} statuses@2.0.1: {} @@ -32355,6 +32473,10 @@ snapshots: optionalDependencies: typescript: 5.6.3 + valibot@1.4.2(typescript@5.9.3): + optionalDependencies: + typescript: 5.9.3 + validate-npm-package-license@3.0.4: dependencies: spdx-correct: 3.2.0 diff --git a/spacetime-resend-ts/.npmrc b/spacetime-resend-ts/.npmrc new file mode 100644 index 00000000000..44bdf80d1df --- /dev/null +++ b/spacetime-resend-ts/.npmrc @@ -0,0 +1 @@ +minimum-release-age=1440 diff --git a/spacetime-resend-ts/LICENSE.txt b/spacetime-resend-ts/LICENSE.txt new file mode 100644 index 00000000000..ea0cb1c5e9e --- /dev/null +++ b/spacetime-resend-ts/LICENSE.txt @@ -0,0 +1,759 @@ +SPACETIMEDB BUSINESS SOURCE LICENSE AGREEMENT + +Business Source License 1.1 + +Parameters + +Licensor: Clockwork Laboratories, Inc. +Licensed Work: SpacetimeDB 2.8.3 + The Licensed Work is + (c) 2023 Clockwork Laboratories, Inc. + +Additional Use Grant: You may make use of the Licensed Work provided your + application or service uses the Licensed Work with no + more than one SpacetimeDB instance in production and + provided that you do not use the Licensed Work for a + Database Service. + + A “Database Service” is a commercial offering that + allows third parties (other than your employees and + contractors) to access the functionality of the + Licensed Work by creating tables whose schemas are + controlled by such third parties. + +Change Date: 2031-08-18 + +Change License: GNU Affero General Public License v3.0 with a linking + exception + +For information about alternative licensing arrangements for the Software, +please visit: https://spacetimedb.com + +Notice + +The Business Source License (this document, or the “License”) is not an Open +Source license. However, the Licensed Work will eventually be made available +under an Open Source License, as stated in this License. + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. +“Business Source License” is a trademark of MariaDB Corporation Ab. + +----------------------------------------------------------------------------- + +Base License and Subdirectory Specific Licenses + +1. Repository-Wide License +Except as provided in Section 2 below, the contents of this repository are licensed under the Business Source License (“BSL”), which includes a change date resulting in a licensing change to the GNU Affero General Public License v3.0 with Linking Exception on that date. See the full text of the BSL and AGPL with Linking Exception in this file below. + +2. Subdirectory-Specific Licenses +Certain subdirectories within this repository are licensed under different terms. + +If a subdirectory contains its own LICENSE or LICENSE.txt file, the terms in that file apply exclusively to all files and subfolders within that subdirectory. + +In the event of any conflict between this base license and a subdirectory’s license, the base license will govern for that subdirectory’s contents. + +3. Contributor Acknowledgement +By contributing to this repository, you agree that: + +Your contributions will be licensed under the license applicable to the directory or subdirectory in which your contribution is made. + +If you contribute to multiple subdirectories, the applicable license for each subdirectory will apply to your contributions in that subdirectory. + +4. Reading the Applicable License +Before using, modifying, or distributing code from this repository, you must read: + +This base LICENSE.txt file for the overall repository license. + +Any LICENSE or LICENSE.txt file in a subdirectory that you intend to use or contribute to. + +----------------------------------------------------------------------------- + +Business Source License 1.1 + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative +works, redistribute, and make non-production use of the Licensed Work. The +Licensor may make an Additional Use Grant, above, permitting limited +production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly +available distribution of a specific version of the Licensed Work under this +License, whichever comes first, the Licensor hereby grants you rights under +the terms of the Change License, and the rights granted in the paragraph +above terminate. + +If your use of the Licensed Work does not comply with the requirements +currently in effect as described in this License, you must purchase a +commercial license from the Licensor, its affiliated entities, or authorized +resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works +of the Licensed Work, are subject to this License. This License applies +separately for each version of the Licensed Work and the Change Date may vary +for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy +of the Licensed Work. If you receive the Licensed Work in original or +modified form from a third party, the terms and conditions set forth in this +License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically +terminate your rights under this License for the current and all other +versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of +Licensor or its affiliates (provided that you may use a trademark or logo of +Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +TITLE. + +MariaDB hereby grants you permission to use this License’s text to license +your works, and to refer to it using the trademark “Business Source License”, +as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the “Business +Source License” name and trademark, Licensor covenants to MariaDB, and to all +other recipients of the licensed work to be provided by Licensor: + +1. To specify as the Change License the GPL Version 2.0 or any later version, + or a license that is compatible with GPL Version 2.0 or a later version, + where “compatible” means that software provided under the Change License can + be included in a program with software provided under GPL Version 2.0 or a + later version. Licensor may specify additional Change Licenses without + limitation. + +2. To either: (a) specify an additional grant of rights to use that does not + impose any additional restriction on the right granted in this License, as + the Additional Use Grant; or (b) insert the text “None”. + +3. To specify a Change Date. + +4. Not to modify this License in any other way. + +----------------------------------------------------------------------------- + +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Affero General Public License, version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, see . + +Additional permission under GNU GPL version 3 section 7 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission to convey the resulting work. + +Additional permission under GNU AGPL version 3 section 13 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission that, notwithstanding any other provision of this +License, you need not prominently offer all users interacting with your +modified version remotely through a computer network an opportunity to +receive the Corresponding Source of your version from a network server at no +charge, if your version supports such interaction. This permission does not +waive or modify any other obligations or terms of the AGPL v3.0, except for +the specific requirement set forth in section 13. + +A copy of the AGPL v3.0 license is reproduced below. + + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + +Copyright © 2007 Free Software Foundation, Inc. +Everyone is permitted to copy and distribute verbatim copies of this license +document, but changing it is not allowed. + +Preamble +The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed to take +away your freedom to share and change the works. By contrast, our General +Public Licenses are intended to guarantee your freedom to share and change +all versions of a program--to make sure it remains free software for all its +users. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the freedom +to distribute copies of free software (and charge for them if you wish), that +you receive source code or can get it if you want it, that you can change the +software or use pieces of it in new free programs, and that you know you can +do these things. + +Developers that use our General Public Licenses protect your rights with two +steps: (1) assert copyright on the software, and (2) offer you this License +which gives you legal permission to copy, distribute and/or modify the +software. + +A secondary benefit of defending all users' freedom is that improvements made +in alternate versions of the program, if they receive widespread use, become +available for other developers to incorporate. Many developers of free +software are heartened and encouraged by the resulting cooperation. However, +in the case of software used on network servers, this result may fail to come +about. The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its source +code to the public. + +The GNU Affero General Public License is designed specifically to ensure +that, in such cases, the modified source code becomes available to the +community. It requires the operator of a network server to provide the source +code of the modified version running there to the users of that server. +Therefore, public use of a modified version, on a publicly accessible server, +gives the public access to the source code of the modified version. + +An older license, called the Affero General Public License and published by +Affero, was designed to accomplish similar goals. This is a different +license, not a version of the Affero GPL, but Affero has released a new +version of the Affero GPL which permits relicensing under this license. + +The precise terms and conditions for copying, distribution and modification +follow. + +TERMS AND CONDITIONS +0. Definitions. +"This License" refers to version 3 of the GNU Affero General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. +Each licensee is addressed as "you". "Licensees" and "recipients" may be +individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a +fashion requiring copyright permission, other than the making of an exact +copy. The resulting work is called a "modified version" of the earlier work +or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the +Program. + +To "propagate" a work means to do anything with it that, without permission, +would make you directly or secondarily liable for infringement under +applicable copyright law, except executing it on a computer or modifying a +private copy. Propagation includes copying, distribution (with or without +modification), making available to the public, and in some countries other +activities as well. + +To "convey" a work means any kind of propagation that enables other parties +to make or receive copies. Mere interaction with a user through a computer +network, with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the +extent that it includes a convenient and prominently visible feature that (1) +displays an appropriate copyright notice, and (2) tells the user that there +is no warranty for the work (except to the extent that warranties are +provided), that licensees may convey the work under this License, and how to +view a copy of this License. If the interface presents a list of user +commands or options, such as a menu, a prominent item in the list meets this +criterion. + +1. Source Code. +The "source code" for a work means the preferred form of the work for making +modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard +defined by a recognized standards body, or, in the case of interfaces +specified for a particular programming language, one that is widely used +among developers working in that language. + +The "System Libraries" of an executable work include anything, other than the +work as a whole, that (a) is included in the normal form of packaging a Major +Component, but which is not part of that Major Component, and (b) serves only +to enable use of the work with that Major Component, or to implement a +Standard Interface for which an implementation is available to the public in +source code form. A "Major Component", in this context, means a major +essential component (kernel, window system, and so on) of the specific +operating system (if any) on which the executable work runs, or a compiler +used to produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the +source code needed to generate, install, and (for an executable work) run the +object code and to modify the work, including scripts to control those +activities. However, it does not include the work's System Libraries, or +general-purpose tools or generally available free programs which are used +unmodified in performing those activities but which are not part of the work. +For example, Corresponding Source includes interface definition files +associated with source files for the work, and the source code for shared +libraries and dynamically linked subprograms that the work is specifically +designed to require, such as by intimate data communication or control flow +between those subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate +automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +2. Basic Permissions. +All rights granted under this License are granted for the term of copyright +on the Program, and are irrevocable provided the stated conditions are met. +This License explicitly affirms your unlimited permission to run the +unmodified Program. The output from running a covered work is covered by this +License only if the output, given its content, constitutes a covered work. +This License acknowledges your rights of fair use or other equivalent, as +provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without +conditions so long as your license otherwise remains in force. You may convey +covered works to others for the sole purpose of having them make +modifications exclusively for you, or provide you with facilities for running +those works, provided that you comply with the terms of this License in +conveying all material for which you do not control copyright. Those thus +making or running the covered works for you must do so exclusively on your +behalf, under your direction and control, on terms that prohibit them from +making any copies of your copyrighted material outside their relationship +with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes it +unnecessary. + +3. Protecting Users' Legal Rights From Anti-Circumvention Law. +No covered work shall be deemed part of an effective technological measure +under any applicable law fulfilling obligations under article 11 of the WIPO +copyright treaty adopted on 20 December 1996, or similar laws prohibiting or +restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention is +effected by exercising rights under this License with respect to the covered +work, and you disclaim any intention to limit operation or modification of +the work as a means of enforcing, against the work's users, your or third +parties' legal rights to forbid circumvention of technological measures. + +4. Conveying Verbatim Copies. +You may convey verbatim copies of the Program's source code as you receive +it, in any medium, provided that you conspicuously and appropriately publish +on each copy an appropriate copyright notice; keep intact all notices stating +that this License and any non-permissive terms added in accord with section 7 +apply to the code; keep intact all notices of the absence of any warranty; +and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you +may offer support or warranty protection for a fee. + +5. Conveying Modified Source Versions. +You may convey a work based on the Program, or the modifications to produce +it from the Program, in the form of source code under the terms of section 4, +provided that you also meet all of these conditions: + +a) The work must carry prominent notices stating that you modified it, and +giving a relevant date. +b) The work must carry prominent notices stating that it is released under +this License and any conditions added under section 7. This requirement +modifies the requirement in section 4 to "keep intact all notices". +c) You must license the entire work, as a whole, under this License to anyone +who comes into possession of a copy. This License will therefore apply, along +with any applicable section 7 additional terms, to the whole of the work, and +all its parts, regardless of how they are packaged. This License gives no +permission to license the work in any other way, but it does not invalidate +such permission if you have separately received it. +d) If the work has interactive user interfaces, each must display Appropriate +Legal Notices; however, if the Program has interactive interfaces that do not +display Appropriate Legal Notices, your work need not make them do so. +A compilation of a covered work with other separate and independent works, +which are not by their nature extensions of the covered work, and which are +not combined with it such as to form a larger program, in or on a volume of a +storage or distribution medium, is called an "aggregate" if the compilation +and its resulting copyright are not used to limit the access or legal rights +of the compilation's users beyond what the individual works permit. Inclusion +of a covered work in an aggregate does not cause this License to apply to the +other parts of the aggregate. + +6. Conveying Non-Source Forms. +You may convey a covered work in object code form under the terms of sections +4 and 5, provided that you also convey the machine-readable Corresponding +Source under the terms of this License, in one of these ways: + +a) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by the Corresponding Source fixed +on a durable physical medium customarily used for software interchange. +b) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by a written offer, valid for at +least three years and valid for as long as you offer spare parts or customer +support for that product model, to give anyone who possesses the object code +either (1) a copy of the Corresponding Source for all the software in the +product that is covered by this License, on a durable physical medium +customarily used for software interchange, for a price no more than your +reasonable cost of physically performing this conveying of source, or (2) +access to copy the Corresponding Source from a network server at no charge. +c) Convey individual copies of the object code with a copy of the written +offer to provide the Corresponding Source. This alternative is allowed only +occasionally and noncommercially, and only if you received the object code +with such an offer, in accord with subsection 6b. +d) Convey the object code by offering access from a designated place (gratis +or for a charge), and offer equivalent access to the Corresponding Source in +the same way through the same place at no further charge. You need not +require recipients to copy the Corresponding Source along with the object +code. If the place to copy the object code is a network server, the +Corresponding Source may be on a different server (operated by you or a third +party) that supports equivalent copying facilities, provided you maintain +clear directions next to the object code saying where to find the +Corresponding Source. Regardless of what server hosts the Corresponding +Source, you remain obligated to ensure that it is available for as long as +needed to satisfy these requirements. +e) Convey the object code using peer-to-peer transmission, provided you +inform other peers where the object code and Corresponding Source of the work +are being offered to the general public at no charge under subsection 6d. +A separable portion of the object code, whose source code is excluded from +the Corresponding Source as a System Library, need not be included in +conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any tangible +personal property which is normally used for personal, family, or household +purposes, or (2) anything designed or sold for incorporation into a dwelling. +In determining whether a product is a consumer product, doubtful cases shall +be resolved in favor of coverage. For a particular product received by a +particular user, "normally used" refers to a typical or common use of that +class of product, regardless of the status of the particular user or of the +way in which the particular user actually uses, or expects or is expected to +use, the product. A product is a consumer product regardless of whether the +product has substantial commercial, industrial or non-consumer uses, unless +such uses represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, +authorization keys, or other information required to install and execute +modified versions of a covered work in that User Product from a modified +version of its Corresponding Source. The information must suffice to ensure +that the continued functioning of the modified object code is in no case +prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as part of +a transaction in which the right of possession and use of the User Product is +transferred to the recipient in perpetuity or for a fixed term (regardless of +how the transaction is characterized), the Corresponding Source conveyed +under this section must be accompanied by the Installation Information. But +this requirement does not apply if neither you nor any third party retains +the ability to install modified object code on the User Product (for example, +the work has been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates for +a work that has been modified or installed by the recipient, or for the User +Product in which it has been modified or installed. Access to a network may +be denied when the modification itself materially and adversely affects the +operation of the network or violates the rules and protocols for +communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in +accord with this section must be in a format that is publicly documented (and +with an implementation available to the public in source code form), and must +require no special password or key for unpacking, reading or copying. + +7. Additional Terms. +"Additional permissions" are terms that supplement the terms of this License +by making exceptions from one or more of its conditions. Additional +permissions that are applicable to the entire Program shall be treated as +though they were included in this License, to the extent that they are valid +under applicable law. If additional permissions apply only to part of the +Program, that part may be used separately under those permissions, but the +entire Program remains governed by this License without regard to the +additional permissions. + +When you convey a copy of a covered work, you may at your option remove any +additional permissions from that copy, or from any part of it. (Additional +permissions may be written to require their own removal in certain cases when +you modify the work.) You may place additional permissions on material, added +by you to a covered work, for which you have or can give appropriate +copyright permission. + +Notwithstanding any other provision of this License, for material you add to +a covered work, you may (if authorized by the copyright holders of that +material) supplement the terms of this License with terms: + +a) Disclaiming warranty or limiting liability differently from the terms of +sections 15 and 16 of this License; or +b) Requiring preservation of specified reasonable legal notices or author +attributions in that material or in the Appropriate Legal Notices displayed +by works containing it; or +c) Prohibiting misrepresentation of the origin of that material, or requiring +that modified versions of such material be marked in reasonable ways as +different from the original version; or +d) Limiting the use for publicity purposes of names of licensors or authors +of the material; or +e) Declining to grant rights under trademark law for use of some trade names, +trademarks, or service marks; or +f) Requiring indemnification of licensors and authors of that material by +anyone who conveys the material (or modified versions of it) with contractual +assumptions of liability to the recipient, for any liability that these +contractual assumptions directly impose on those licensors and authors. +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is governed +by this License along with a term that is a further restriction, you may +remove that term. If a license document contains a further restriction but +permits relicensing or conveying under this License, you may add to a covered +work material governed by the terms of that license document, provided that +the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must +place, in the relevant source files, a statement of the additional terms that +apply to those files, or a notice indicating where to find the applicable +terms. + +Additional terms, permissive or non-permissive, may be stated in the form of +a separately written license, or stated as exceptions; the above requirements +apply either way. + +8. Termination. +You may not propagate or modify a covered work except as expressly provided +under this License. Any attempt otherwise to propagate or modify it is void, +and will automatically terminate your rights under this License (including +any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a +particular copyright holder is reinstated (a) provisionally, unless and until +the copyright holder explicitly and finally terminates your license, and (b) +permanently, if the copyright holder fails to notify you of the violation by +some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated +permanently if the copyright holder notifies you of the violation by some +reasonable means, this is the first time you have received notice of +violation of this License (for any work) from that copyright holder, and you +cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses +of parties who have received copies or rights from you under this License. If +your rights have been terminated and not permanently reinstated, you do not +qualify to receive new licenses for the same material under section 10. + +9. Acceptance Not Required for Having Copies. +You are not required to accept this License in order to receive or run a copy +of the Program. Ancillary propagation of a covered work occurring solely as a +consequence of using peer-to-peer transmission to receive a copy likewise +does not require acceptance. However, nothing other than this License grants +you permission to propagate or modify any covered work. These actions +infringe copyright if you do not accept this License. Therefore, by modifying +or propagating a covered work, you indicate your acceptance of this License +to do so. + +10. Automatic Licensing of Downstream Recipients. +Each time you convey a covered work, the recipient automatically receives a +license from the original licensors, to run, modify and propagate that work, +subject to this License. You are not responsible for enforcing compliance by +third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered work +results from an entity transaction, each party to that transaction who +receives a copy of the work also receives whatever licenses to the work the +party's predecessor in interest had or could give under the previous +paragraph, plus a right to possession of the Corresponding Source of the work +from the predecessor in interest, if the predecessor has it or can get it +with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights +granted or affirmed under this License. For example, you may not impose a +license fee, royalty, or other charge for exercise of rights granted under +this License, and you may not initiate litigation (including a cross-claim or +counterclaim in a lawsuit) alleging that any patent claim is infringed by +making, using, selling, offering for sale, or importing the Program or any +portion of it. + +11. Patents. +A "contributor" is a copyright holder who authorizes use under this License +of the Program or a work on which the Program is based. The work thus +licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or +controlled by the contributor, whether already acquired or hereafter +acquired, that would be infringed by some manner, permitted by this License, +of making, using, or selling its contributor version, but do not include +claims that would be infringed only as a consequence of further modification +of the contributor version. For purposes of this definition, "control" +includes the right to grant patent sublicenses in a manner consistent with +the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent +license under the contributor's essential patent claims, to make, use, sell, +offer for sale, import and otherwise run, modify and propagate the contents +of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent (such +as an express permission to practice a patent or covenant not to sue for +patent infringement). To "grant" such a patent license to a party means to +make such an agreement or commitment not to enforce a patent against the +party. + +If you convey a covered work, knowingly relying on a patent license, and the +Corresponding Source of the work is not available for anyone to copy, free of +charge and under the terms of this License, through a publicly available +network server or other readily accessible means, then you must either (1) +cause the Corresponding Source to be so available, or (2) arrange to deprive +yourself of the benefit of the patent license for this particular work, or +(3) arrange, in a manner consistent with the requirements of this License, to +extend the patent license to downstream recipients. "Knowingly relying" means +you have actual knowledge that, but for the patent license, your conveying +the covered work in a country, or your recipient's use of the covered work in +a country, would infringe one or more identifiable patents in that country +that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, +you convey, or propagate by procuring conveyance of, a covered work, and +grant a patent license to some of the parties receiving the covered work +authorizing them to use, propagate, modify or convey a specific copy of the +covered work, then the patent license you grant is automatically extended to +all recipients of the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope +of its coverage, prohibits the exercise of, or is conditioned on the +non-exercise of one or more of the rights that are specifically granted under +this License. You may not convey a covered work if you are a party to an +arrangement with a third party that is in the business of distributing +software, under which you make payment to the third party based on the extent +of your activity of conveying the work, and under which the third party +grants, to any of the parties who would receive the covered work from you, a +discriminatory patent license (a) in connection with copies of the covered +work conveyed by you (or copies made from those copies), or (b) primarily for +and in connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any +implied license or other defenses to infringement that may otherwise be +available to you under applicable patent law. + +12. No Surrender of Others' Freedom. +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not excuse +you from the conditions of this License. If you cannot convey a covered work +so as to satisfy simultaneously your obligations under this License and any +other pertinent obligations, then as a consequence you may not convey it at +all. For example, if you agree to terms that obligate you to collect a +royalty for further conveying from those to whom you convey the Program, the +only way you could satisfy both those terms and this License would be to +refrain entirely from conveying the Program. + +13. Remote Network Interaction; Use with the GNU General Public License. +Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users interacting +with it remotely through a computer network (if your version supports such +interaction) an opportunity to receive the Corresponding Source of your +version by providing access to the Corresponding Source from a network server +at no charge, through some standard or customary means of facilitating +copying of software. This Corresponding Source shall include the +Corresponding Source for any work covered by version 3 of the GNU General +Public License that is incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have permission to +link or combine any covered work with a work licensed under version 3 of the +GNU General Public License into a single combined work, and to convey the +resulting work. The terms of this License will continue to apply to the part +which is the covered work, but the work with which it is combined will remain +governed by version 3 of the GNU General Public License. + +14. Revised Versions of this License. +The Free Software Foundation may publish revised and/or new versions of the +GNU Affero General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU Affero General Public +License "or any later version" applies to it, you have the option of +following the terms and conditions either of that numbered version or of any +later version published by the Free Software Foundation. If the Program does +not specify a version number of the GNU Affero General Public License, you +may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the +GNU Affero General Public License can be used, that proxy's public statement +of acceptance of a version permanently authorizes you to choose that version +for the Program. + +Later license versions may give you additional or different permissions. +However, no additional obligations are imposed on any author or copyright +holder as a result of your choosing to follow a later version. + +15. Disclaimer of Warranty. +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE +LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, +EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE +ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. +SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY +SERVICING, REPAIR OR CORRECTION. + +16. Limitation of Liability. +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL +ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE +PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE +OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR +DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR +A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH +HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +17. Interpretation of Sections 15 and 16. +If the disclaimer of warranty and limitation of liability provided above +cannot be given local legal effect according to their terms, reviewing courts +shall apply local law that most closely approximates an absolute waiver of +all civil liability in connection with the Program, unless a warranty or +assumption of liability accompanies a copy of the Program in return for a +fee. + +END OF TERMS AND CONDITIONS + +How to Apply These Terms to Your New Programs +If you develop a new program, and you want it to be of the greatest possible +use to the public, the best way to achieve this is to make it free software +which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach +them to the start of each source file to most effectively state the exclusion +of warranty; and each file should have at least the "copyright" line and a +pointer to where the full notice is found. + +SpacetimeDB: A database which replaces your server. +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +Also add information on how to contact you by electronic and paper mail. + +If your software can interact with users remotely through a computer network, +you should also make sure that it provides a way for users to get its source. +For example, if your program is a web application, its interface could +display a "Source" link that leads users to an archive of the code. There are +many ways you could offer source, and different solutions will be better for +different programs; see section 13 for the specific requirements. + +You should also get your employer (if you work as a programmer) or school, if +any, to sign a "copyright disclaimer" for the program, if necessary. For more +information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md new file mode 100644 index 00000000000..12e3274ccb1 --- /dev/null +++ b/spacetime-resend-ts/README.md @@ -0,0 +1,234 @@ +# @spacetimedb/resend + +A SpacetimeDB submodule for transactional email via [Resend](https://resend.com): +admin-gated outbound delivery, idempotent webhook ingest, private delivery +state, synchronous procedures, and valibot-validated webhook payloads. + +--- + +## Install + +```bash +npm install @spacetimedb/resend spacetimedb@^2.8.3 +``` + +Requires SpacetimeDB 2.8.3 or later for submodule mounting. + +For the install-to-publish workflow, see +[Getting started](https://spacetimedb.com/docs/). + +This submodule can be published directly as its own SpacetimeDB module from the root entry point. + +## Usage + +### Integrate into an application + +Register Resend in the host schema, initialize its private state, and expose only +application-authorized send procedures and caller-scoped delivery views: + +```ts +import { schema } from 'spacetimedb/server'; +import * as resend from '@spacetimedb/resend/submodule'; + +const spacetimedb = schema({ resend }); +export default spacetimedb; + +export const init = spacetimedb.init(ctx => { + resend.installResend(ctx.as.resend); +}); +``` + +Provider configuration must run as the publishing owner or a registered Resend +administrator. See the +[Dispatch host module](./example/spacetimedb/) +for a narrow send procedure, scoped views, and signed webhook routing. + +### Standalone configuration + +Resend credentials live in a private `resend_config` singleton. During `init`, a +fresh database seeds the owner into the private `resend_admin_identity` table. + +```bash +spacetime call --server http://127.0.0.1:3000 resend-ts set_resend_config \ + '"re_..."' \ + '{"some":"whsec_..."}' \ + '{"some":"onboarding@resend.dev"}' +``` + +Args: `apiKey`, `webhookSigningSecret` (required for webhook ingest), `defaultFrom` (optional). +For `t.option(...)` CLI arguments, use `null` for no value and +`{"some":"value"}` for a string value. + +Verify: + +```bash +spacetime call --server http://127.0.0.1:3000 resend-ts get_resend_config_status '{}' +``` + +`send_email` reads the Resend API key from private module state. + +## Private tables + +| Table | Key | Notes | +| ----------------------- | ----------- | ------------------------------------------------------------ | +| `resend_email` | `resend_id` | one row per outbound email; `status` reflects latest webhook | +| `resend_delivery_event` | `event_id` | append-only audit log of every event for an email | + +- `resend_webhook_event` - idempotency log +- `resend_config` - credentials singleton +- `resend_admin_identity` - admin allowlist + +None of the Resend base tables are subscribable. Email recipients, subject, +body, tracking state, webhook payloads, and signature headers remain private. +Host modules should expose caller- or tenant-scoped views over `userId` or +`orgId`; the included example demonstrates this pattern. + +## API + +**Setup** + +- `set_resend_config(apiKey, webhookSigningSecret, defaultFrom)` +- `get_resend_config_status()` - `{ isConfigured, hasWebhookSigningSecret, apiKeyLength, ... }` +- `add_admin_identity(identity)` / `remove_admin_identity(identity)` + +**Outbound** + +- `send_email({ from, to, subject, html, text, cc, bcc, replyTo, tagsJson, headersJson, scheduledAt, idempotencyKey})` - admin-gated; inserts a `resend_email` row with `status = queued`, returns `{ resendId }`. The first webhook flips it to `sent`/`delivered`. +- `cancel_email({ resendId })` - admin-gated +- `resend_api_request({ method, path, jsonBody, idempotencyKey })` - admin-gated + request to a relative `api.resend.com` path; accepted methods are `GET`, `POST`, + `PATCH`, and `DELETE` + +Email sends accept up to 100 combined `to`, `cc`, and `bcc` recipients. Address +fields are capped at 320 characters, subjects at 998 characters, HTML and text +at 200,000 characters each, and tag or header JSON at 16 KiB. Control characters +in address, subject, and schedule fields are rejected before provider HTTP. + +Host modules should prefer the submodule helper export: + +```ts +import * as resend from '@spacetimedb/resend/submodule'; + +resend.sendEmail(ctx.as.resend, { + to: ['delivered@resend.dev'], + subject: 'Welcome', + html: '

Hello.

', + tagsJson: JSON.stringify({ userId: 'u_123', orgId: 'launch' }), +}); +``` + +That lets the host app own product-specific authorization and workflow while +the submodule owns config, delivery rows, and webhook ingest. + +Expose that helper through a product-facing procedure with recipient policy and +rate limits. The generated client then calls the wrapper: + +```ts +const result = await conn.procedures.sendDispatch({ + to: 'delivered@resend.dev', + subject: 'Welcome', + message: 'Your workspace is ready.', +}); + +if (!result.ok) throw new Error(result.message); +``` + +Subscribe to a caller-scoped host view for delivery status. Keep the private +Resend tables and generic administrative send operation restricted to +operators. + +**Webhook ingest / replay** + +- `ingest_resend_webhook(eventId, eventType, payloadJson, signatureHeader, timestampHeader)` - idempotent +- `replay_webhook_event(eventId)` - re-applies a stored event +- `makeResendWebhookHandler()` builds a direct HTTP webhook handler for a host + router. + +Webhook application is atomic. Invalid event data rolls back the event row and +email-state changes so Resend can redeliver the event. + +**Admin queries** + +- `get_email`, `list_emails_by_user_id`, `list_emails_by_org_id`, `list_emails_by_status` +- `list_delivery_events_for_email` + +List procedures return at most 1,000 rows. Host applications should expose +caller-scoped, paginated views for product-facing history. + +Package entrypoints: + +- `@spacetimedb/resend` can run as a standalone email database. +- `@spacetimedb/resend/submodule` supplies submodule configuration, delivery, + webhook, and query helpers. + +## Webhook events handled + +``` +email.sent email.delivered +email.delivery_delayed email.bounced +email.complained email.failed +email.opened email.clicked +``` + +`opened` / `clicked` / `complained` are recorded as **flags + timestamps** and leave `status` unchanged. Terminal states (`delivered`, `bounced`, `failed`, `cancelled`) and in-flight states (`queued`, `sent`, `delivery_delayed`) live in the `status` column. + +## Webhook signature verification + +The module's `ingest_resend_webhook` reducer verifies Standard Webhooks signatures (svix) using the configured `webhookSigningSecret`. `signatureHeader` and `timestampHeader` are stored on `resend_webhook_event` for forensic replay. + +## Tagging + +`userId` / `orgId` are extracted from Resend `tags` and indexed for per-user / per-org listing. Both tag shapes are accepted: + +- Object form: `{"userId": "u_123", "orgId": "o_456"}` +- Array form (Resend's webhook output): `[{"name":"userId","value":"u_123"}]` + +Pass `tagsJson` as a JSON string when calling `send_email`. + +## Integration testing + +```bash +# Build + publish + event paths, idempotency, replay, signed-type checks, and authorization checks +pnpm run test:smoke +``` + +The smoke test publishes only to the dedicated `resend-ts-smoke-test` database. + +For real Resend test-mode: + +```bash +# Bootstrap once with your real key: +spacetime call --server http://127.0.0.1:3000 resend-ts set_resend_config '"re_..."' null '{"some":"onboarding@resend.dev"}' + +# Then send to one of Resend's test addresses (delivered@/bounced@/complained@): +spacetime call --server http://127.0.0.1:3000 resend-ts send_email \ + null \ + '["delivered@resend.dev"]' \ + '"Test from SpacetimeDB"' \ + '{"some":"

Hello.

"}' \ + null null null null \ + '{"some":"{\"userId\":\"u_123\"}"}' \ + null null null +``` + +To exercise inbound webhooks end-to-end, expose your local relay via ngrok, register the URL in Resend's dashboard with the same `whsec_...` you passed to `set_resend_config`, and forward verified events to `ingest_resend_webhook`. + +## Architecture notes + +- **valibot for runtime validation.** `vEmailEvent` is a `v.variant('type', [...])` over the 8 supported event types. The unit and smoke suites lock down the accepted wire shapes. +- **Synchronous HTTP.** Procedures are synchronous and `ctx.http.fetch` returns a `SyncResponse`, so `callResend` in `src/submodule/http.ts` implements the required API surface directly. +- **Wire format.** The public input uses SDK-style camelCase (`replyTo`, `scheduledAt`), and `buildSendEmailBody` emits the provider's snake_case JSON fields. +- **Idempotency.** Each webhook event is keyed by `event.id`; re-ingest is a no-op. Status-changing events ratchet forward, so `email.complained` preserves a terminal `delivered` status. + +## Testing + +```bash +pnpm test +pnpm run lint +``` + +Credentialed smoke coverage is described in **Integration testing** above. + +## License + +[BUSL-1.1](./LICENSE.txt) - same as SpacetimeDB. diff --git a/spacetime-resend-ts/example/.env.example b/spacetime-resend-ts/example/.env.example new file mode 100644 index 00000000000..c162013b626 --- /dev/null +++ b/spacetime-resend-ts/example/.env.example @@ -0,0 +1,28 @@ +# Copy to .env. The example server loads this on startup. + +# ---------------- Resend ---------------- +# Required to send email from Dispatch. +RESEND_API_KEY=re_... + +# Required to accept inbound webhooks (Resend dashboard, Webhooks -> your endpoint -> Signing secret). +# The module rejects requests when this is unset or the signature is invalid. +RESEND_WEBHOOK_SECRET=whsec_... + +# Optional comma-separated external recipients. Provider test recipients remain +# available automatically. The module rejects every address outside this list. +RESEND_ALLOWED_RECIPIENTS=you@example.com + +# ---------------- SpacetimeDB ---------------- +STDB_URI=ws://127.0.0.1:3000 +STDB_HTTP=http://127.0.0.1:3000 +SPACETIMEDB_DB_NAME=spacetime-resend-example +# Optional. When unset, the server creates a persistent local identity token in +# .stdb-server-token and the logged-in publishing identity authorizes it. +# STDB_SERVER_TOKEN= + +# ---------------- Static server ---------------- +PORT=8790 +HOST=127.0.0.1 + +# Default "from" address (must be a verified sender in your Resend account, or onboarding@resend.dev for testing). +DEFAULT_FROM=onboarding@resend.dev diff --git a/spacetime-resend-ts/example/.npmrc b/spacetime-resend-ts/example/.npmrc new file mode 100644 index 00000000000..44bdf80d1df --- /dev/null +++ b/spacetime-resend-ts/example/.npmrc @@ -0,0 +1 @@ +minimum-release-age=1440 diff --git a/spacetime-resend-ts/example/README.md b/spacetime-resend-ts/example/README.md new file mode 100644 index 00000000000..da29091f8c2 --- /dev/null +++ b/spacetime-resend-ts/example/README.md @@ -0,0 +1,181 @@ +# Dispatch + +Dispatch demonstrates a host module that mounts +`@spacetimedb/resend/submodule`: compose an email, send it through Resend, and +watch verified delivery events stream back through SpacetimeDB. Provider credentials +and submodule administration remain outside the browser. + +## What this demonstrates + +- Mounting the Resend submodule under the `resend` namespace. +- Calling a host `send_dispatch` procedure backed by a private API key. +- Showing caller-scoped email and delivery-event views in real time. +- Receiving Resend webhooks through a native SpacetimeDB HTTP route. +- Verifying Svix signatures inside the module with `@spacetimedb/crypto`. +- Persisting and explicitly authorizing a dedicated local server identity. + +## Prerequisites + +- Node.js 20 or later and pnpm 10. +- The released SpacetimeDB 2.8 CLI. +- A local SpacetimeDB server reachable as `local`. +- A logged-in CLI identity that publishes the database. +- A Resend API key. +- A Resend webhook signing secret for delivery-status updates. + +Select the supported CLI release, then keep the local server running in a +separate terminal: + +```powershell +spacetime version install 2.8.3 +spacetime version use 2.8.3 +spacetime start +``` + +```powershell +spacetime server ping local +spacetime login show +``` + +## Quick start + +From `spacetime-resend-ts/example`: + +```powershell +pnpm install +pnpm --dir spacetimedb install +node -e "require('node:fs').copyFileSync('.env.example', '.env')" +``` + +Set `RESEND_API_KEY` and, for webhooks, `RESEND_WEBHOOK_SECRET` in `.env`. + +```powershell +pnpm run build:module:fresh +pnpm run dev +``` + +Open . The **Delivered**, **Bounced**, and **Complaint** +buttons fill Resend's test addresses. Sending creates a real Resend test request; +delivery-state transitions require a reachable webhook. + +`build:module:fresh` deletes and recreates only the local `spacetime-resend-example` +database. Use `pnpm run build:module` to preserve existing data. + +## Use in your project + +This workspace tests the submodule source in this repository. Consumer applications install published releases: + +```bash +npm install @spacetimedb/resend @spacetimedb/rate-limit spacetimedb@^2.8.3 +``` + +Follow the package's +[integration guide](../README.md#integrate-into-an-application). Copy the +authorized send procedure, caller-scoped views, and signed webhook route; +replace the example's development identity bootstrap in production. + +## Configuration + +| Variable | Default | Purpose | +| --------------------------- | -------------------------- | ----------------------------------------------------------------------------------------------- | +| `RESEND_API_KEY` | empty | Required to send mail. | +| `RESEND_WEBHOOK_SECRET` | empty | Required to accept webhook requests. Missing or invalid signatures are rejected. | +| `RESEND_ALLOWED_RECIPIENTS` | empty | Optional comma-separated external recipients. Resend test addresses are included automatically. | +| `DEFAULT_FROM` | `onboarding@resend.dev` | Default sender; use a verified address outside Resend's test flow. | +| `STDB_URI` | `ws://127.0.0.1:3000` | Browser and server WebSocket endpoint. | +| `STDB_HTTP` | `http://127.0.0.1:3000` | CLI and native-route HTTP endpoint. Must match `STDB_URI`. | +| `SPACETIMEDB_DB_NAME` | `spacetime-resend-example` | Published database name. | +| `STDB_SERVER_TOKEN` | generated locally | Optional pre-provisioned server identity token. | +| `HOST` | `127.0.0.1` | Static-server bind address. | +| `PORT` | `8790` | Static-server port. | + +When `STDB_SERVER_TOKEN` is unset, the server stores its identity token in the +ignored `.stdb-server-token` file. The logged-in publishing identity registers it +with `resend.add_admin_identity` before the server writes private configuration. +Configuration failure is fatal when an API key was supplied; the server will not +pretend to be ready with an unauthorized identity. + +## Webhooks + +The native module endpoint is: + +```text +POST http://127.0.0.1:3000/v1/database/spacetime-resend-example/route/webhook/resend +``` + +The Node server also exposes: + +```text +POST http://127.0.0.1:8790/webhook/resend +``` + +That route is a raw-body passthrough for stable tunnel URLs. Signature verification +and ingestion still occur inside the SpacetimeDB module. The module rejects requests +when no signing secret is configured or when the signature is invalid; there is no +unverified development fallback. + +Without a publicly reachable webhook, sending still creates a queued email row, but +Delivered, Opened, Clicked, Bounced, and Complaint transitions cannot arrive. + +## Architecture + +```text +Browser + -> send_dispatch host procedure + -> recipient allowlist and caller/global quotas + -> resend submodule namespace + -> Resend API + +Resend webhook + -> native SpacetimeDB HTTP route + -> signature verification and ingest + -> caller-scoped live views + +Authorized example server + -> private Resend configuration during startup + -> static UI and optional raw webhook passthrough +``` + +## Security and deployment boundaries + +- The browser never receives the Resend API key, signing secret, server token, or + submodule administrator role. +- `send_dispatch` accepts only server-configured recipients. It allows five sends + per caller every ten minutes and 25 sends globally per hour. +- Provider failures return a stable application error while detailed delivery + state remains in private submodule tables. +- `.env` and `.stdb-server-token` are ignored and must not be committed. +- The development server binds to loopback by default. +- A production deployment should provision its service identity and secret store + through deployment infrastructure. + +## Verification + +```powershell +pnpm --dir spacetimedb run build +pnpm run build +pnpm exec tsc -p tsconfig.json +``` + +For a provider smoke test, send to `delivered@resend.dev` and confirm a queued row +appears. With a reachable signed webhook, confirm it advances to Delivered. Invalid +or unsigned webhook fixtures must return a non-success status. + +## Troubleshooting + +- **`resend.not_authorized`:** publish with the logged-in CLI identity and restart + so it can authorize the persistent server identity. +- **Send remains Queued:** confirm the webhook is publicly reachable and its signing + secret matches `RESEND_WEBHOOK_SECRET`. +- **Connection targets disagree:** make `STDB_URI`, `STDB_HTTP`, and the publish + target refer to the same SpacetimeDB instance. +- **Replace the server identity:** stop the server, remove + `.stdb-server-token`, and restart while logged in as a database administrator. + +## Important files + +- `spacetimedb/src/index.ts`: host procedures, caller-scoped views, and native route. +- `server.ts`: safe startup configuration, server identity, and webhook passthrough. +- `src/app.ts`: browser connection and Dispatch UI behavior. +- `public/index.html`: Dispatch interface structure. +- `public/styles.css`: Dispatch presentation. diff --git a/spacetime-resend-ts/example/package.json b/spacetime-resend-ts/example/package.json new file mode 100644 index 00000000000..d7ced031655 --- /dev/null +++ b/spacetime-resend-ts/example/package.json @@ -0,0 +1,30 @@ +{ + "name": "spacetime-resend-example", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "spacetime:generate": "spacetime generate --lang typescript --out-dir src/module_bindings --module-path ./spacetimedb -y", + "build:module": "pnpm --dir spacetimedb run build && pnpm --dir spacetimedb run publish:local && pnpm run spacetime:generate && pnpm run build:app", + "build:module:fresh": "pnpm --dir spacetimedb run build && pnpm --dir spacetimedb run publish:local:reset && pnpm run spacetime:generate && pnpm run build:app", + "check": "tsc --noEmit", + "test": "tsx scripts/test-message.ts", + "build:app": "pnpm run check && esbuild src/app.ts --bundle --format=esm --outfile=public/app.js --target=es2022 --sourcemap", + "build": "pnpm run spacetime:generate && pnpm run build:app", + "dev": "pnpm run build && tsx server.ts" + }, + "dependencies": { + "@spacetimedb/submodule-shared": "workspace:*", + "dotenv": "^16.4.7", + "express": "^4.21.2", + "spacetimedb": "workspace:*" + }, + "devDependencies": { + "@types/express": "^4.17.21", + "@types/node": "^22.10.2", + "esbuild": "^0.28.0", + "resend": "^6.12.2", + "tsx": "^4.21.0", + "typescript": "^5.9.3" + } +} diff --git a/spacetime-resend-ts/example/public/index.html b/spacetime-resend-ts/example/public/index.html new file mode 100644 index 00000000000..c788c2ea747 --- /dev/null +++ b/spacetime-resend-ts/example/public/index.html @@ -0,0 +1,151 @@ + + + + + + + Dispatch + + + +
+
+
+ +
+

Dispatch

+ Send an email, watch its delivery resolve live +
+
+
+ +
+
+
+

Compose

+

+ Dispatched through the mounted Resend submodule. +

+
+ +
+ + +
+
+ Message +
+ + +
+
+ + +
+ +
+
+ +
+ Resend test addresses +
+ + + +
+
+
+ +
+
+
+

Live delivery

+

+ Every dispatch and its lifecycle, streamed from SpacetimeDB. +

+
+
+ 0 sent + +
+
+ +
+
+ 0Sent +
+
+ 0Delivered +
+
+ 0Opened +
+
+ 0Bounced +
+
+ +
+
+
+ +
+ Built on + SpacetimeDB +
+
+ + + + diff --git a/spacetime-resend-ts/example/public/styles.css b/spacetime-resend-ts/example/public/styles.css new file mode 100644 index 00000000000..91cc3c7532b --- /dev/null +++ b/spacetime-resend-ts/example/public/styles.css @@ -0,0 +1,885 @@ +@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=IBM+Plex+Mono:wght@400;500;600&display=swap'); + +:root { + /* Tokens from spacetimedb.com/app/styles/variables.css */ + --font-inter: 'Inter', ui-sans-serif, system-ui, -apple-system, sans-serif; + --font-ibm: 'IBM Plex Mono', ui-monospace, monospace; + + --color-green: #4cf490; + --color-green-10: #4cf4901a; + --color-green-20: #4cf49033; + --color-white: #d7d8d9; + --color-yellow: #fbdc8e; + --color-purple: #a880ff; + --color-orange: #ff9e9e; + --color-blue: #02befa; + --color-pink: #ff80fb; + --color-teal: #00ccb4; + --color-red: #ff4c4c; + + --color-n1: #e6e9f0; + --color-n2: #ced3e0; + --color-n3: #b6c0cf; + --color-n4: #6f7987; + --color-n5: #363840; + --color-n8: #060606; + + --color-shade1: #162d38; + --color-shade4: #121e24; + --color-shade5: #0f191f; + --color-shade6: #0e161a; + --color-shade7: #0b1114; + --color-shade8: #0b0e12; + + --radius-sm: 6px; + --radius: 10px; + --radius-lg: 14px; +} + +* { + box-sizing: border-box; +} +html, +body { + margin: 0; + min-height: 100%; +} +body { + color: var(--color-white); + background: var(--color-shade7); + font-family: var(--font-inter); + -webkit-font-smoothing: antialiased; +} +::selection { + background: var(--color-green); + color: var(--color-n8); +} +a { + color: var(--color-green); + text-decoration: none; +} + +* { + scrollbar-width: thin; + scrollbar-color: var(--color-shade4) transparent; +} +*::-webkit-scrollbar { + width: 6px; + height: 6px; +} +*::-webkit-scrollbar-track { + background: transparent; +} +*::-webkit-scrollbar-thumb { + background: var(--color-shade4); + border-radius: 3px; +} +*::-webkit-scrollbar-thumb:hover { + background: var(--color-shade1); +} + +button, +input, +textarea { + font: inherit; +} +button { + height: 34px; + padding: 0 16px; + border: 1px solid var(--color-shade4); + border-radius: var(--radius-sm); + background: var(--color-shade7); + color: var(--color-n2); + font-family: var(--font-inter); + font-size: 13px; + font-weight: 600; + cursor: pointer; + transition: + background 0.18s, + border-color 0.18s, + color 0.18s; +} +button:hover:not(:disabled) { + background: var(--color-shade4); + color: var(--color-white); +} +button:focus-visible { + outline: 2px solid var(--color-green); + outline-offset: 2px; +} +button.primary { + background: var(--color-n3); + border-color: var(--color-n3); + color: var(--color-n8); +} +button.primary:hover:not(:disabled) { + background: var(--color-white); + border-color: var(--color-white); + color: var(--color-n8); +} +button.primary:active:not(:disabled) { + background: var(--color-green); + border-color: var(--color-green); +} +button.primary.sent { + background: var(--color-green); + border-color: var(--color-green); + color: var(--color-n8); + opacity: 1; +} +button:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +input, +textarea { + width: 100%; + padding: 10px 12px; + border: 1px solid var(--color-shade4); + border-radius: var(--radius-sm); + background: var(--color-shade8); + color: var(--color-white); + outline: none; + transition: + border-color 0.15s, + box-shadow 0.15s; +} +input { + height: 40px; + padding: 0 12px; +} +textarea { + min-height: 120px; + resize: none; + line-height: 1.5; + font-size: 14px; +} +input::placeholder, +textarea::placeholder { + color: var(--color-n4); +} +input:focus, +textarea:focus { + border-color: var(--color-green); + box-shadow: 0 0 0 3px var(--color-green-10); +} +label { + display: grid; + gap: 7px; + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 11px; + letter-spacing: 0.07em; + text-transform: uppercase; +} +h1, +h2, +h3, +p { + margin: 0; +} +.muted { + color: var(--color-n4); +} + +.shell { + width: min(1200px, calc(100% - 32px)); + margin: 0 auto; + padding: 16px 0 0; + /* Viewport height gives the feed its own scroll area. Mobile uses auto + height in the media query. */ + height: 100vh; + display: flex; + flex-direction: column; +} + +.topbar { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + border: 1px solid #17303b; + border-radius: var(--radius-lg); + background: linear-gradient(180deg, #0d1920, #0b1319); + box-shadow: inset 0 1px 0 #26435166; + padding: 11px 16px; + margin-bottom: 14px; +} +.brand { + display: flex; + align-items: center; + gap: 12px; + min-width: 0; +} +.brand-mark { + flex: 0 0 auto; + width: 34px; + height: 34px; + border-radius: var(--radius); + display: grid; + place-items: center; + color: var(--color-green); + background: var(--color-green-10); + border: 1px solid var(--color-green-20); +} +.brand-mark svg { + width: 18px; + height: 18px; +} +.brand-text { + display: grid; + gap: 1px; + min-width: 0; +} +.brand-text h1 { + margin: 0; + font-size: 15px; + font-weight: 700; + line-height: 1.2; + color: var(--color-n1); +} +.brand-sub { + color: var(--color-n4); + font-size: 12px; +} + +.workspace { + flex: 1; + display: grid; + grid-template-columns: 372px minmax(0, 1fr); + /* One row that is at least 520px and otherwise fills the viewport, so both + panels are always equal height and grow when the window grows. */ + grid-template-rows: minmax(520px, 1fr); + gap: 14px; + align-items: stretch; + min-height: 0; + margin-bottom: 16px; +} +.panel { + border: 1px solid var(--color-shade4); + border-radius: var(--radius-lg); + background: linear-gradient(180deg, var(--color-shade5), var(--color-shade6)); +} + +/* Compose */ +.compose { + padding: 18px; + display: flex; + flex-direction: column; + gap: 16px; +} +.compose-head { + display: grid; + gap: 3px; +} +.compose-head h2 { + font-size: 15px; + font-weight: 700; + color: var(--color-n1); +} +.compose-head p { + font-size: 13px; +} +.form-grid { + display: flex; + flex-direction: column; + gap: 13px; + flex: 1; + min-height: 0; +} +.form-grid > button.primary { + height: 42px; + font-size: 14px; +} +.field { + display: flex; + flex-direction: column; + gap: 7px; + flex: 1; + min-height: 0; +} +.field textarea, +.field .mail-preview { + flex: 1; + min-height: 120px; +} + +.test-block { + display: grid; + gap: 9px; +} +.test-label { + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 11px; + letter-spacing: 0.07em; + text-transform: uppercase; +} +.test-row { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: 8px; +} +.test-row button { + min-width: 0; + height: 32px; + padding: 0 6px; + font-size: 12px; +} +.test-row button b { + display: block; + font-size: 12px; + font-weight: 600; + color: var(--color-n2); +} + +/* Message field with write/preview tabs */ +.field-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; +} +.field-label { + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 11px; + letter-spacing: 0.07em; + text-transform: uppercase; +} +.msg-tabs { + display: inline-flex; + gap: 2px; + padding: 2px; + border: 1px solid var(--color-shade4); + border-radius: var(--radius-sm); + background: var(--color-shade8); +} +.msg-tab { + height: 24px; + padding: 0 12px; + border: 0; + border-radius: 4px; + background: transparent; + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 11px; + font-weight: 600; + letter-spacing: 0.04em; + text-transform: uppercase; +} +.msg-tab:hover:not(.active) { + background: transparent; + color: var(--color-n2); +} +.msg-tab.active { + background: var(--color-shade4); + color: var(--color-n1); +} + +.mail-preview { + min-height: 128px; + border: 1px solid var(--color-shade4); + border-radius: var(--radius-sm); + background: #ffffff; + overflow: hidden; +} +.mp-head { + padding: 12px 14px; + border-bottom: 1px solid #e6e8ec; + background: #f6f7f9; +} +.mp-subject { + font-size: 14px; + font-weight: 600; + color: #1f2933; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.mp-from { + margin-top: 3px; + font-size: 12px; + color: #6b7280; +} +.mp-body { + padding: 14px; + font-size: 14px; + color: #1f2933; + line-height: 1.6; + word-break: break-word; +} +.mp-body .mp-empty { + color: #9aa3af; + font-style: italic; +} + +.form-error { + color: var(--color-orange); + font-family: var(--font-ibm); + font-size: 12px; + line-height: 1.4; +} +.form-error:empty { + display: none; +} + +/* Live delivery console */ +.console { + display: grid; + grid-template-rows: auto auto minmax(0, 1fr); + min-height: 0; +} +.console-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 14px; + padding: 16px 18px; + border-bottom: 1px solid var(--color-shade4); +} +.console-copy { + display: grid; + gap: 3px; +} +.console-copy h2 { + font-size: 15px; + font-weight: 700; + color: var(--color-n1); +} +.console-copy p { + font-size: 13px; +} +.console-right { + display: flex; + align-items: center; + gap: 12px; +} +.feed-count { + color: var(--color-n5); + font-family: var(--font-ibm); + font-size: 11px; +} +#clear-btn { + height: 30px; + padding: 0 12px; + font-size: 12px; +} +#clear-btn:disabled { + opacity: 0.4; +} + +.metrics { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 10px; + padding: 16px 18px; + border-bottom: 1px solid var(--color-shade4); +} +.metric { + display: grid; + gap: 7px; + padding: 13px; + border: 1px solid var(--color-shade4); + border-radius: var(--radius); + background: var(--color-shade7); +} +.metric strong { + font-size: 25px; + font-weight: 700; + line-height: 1; + color: var(--color-white); + font-variant-numeric: tabular-nums; +} +.metric span { + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 10px; + letter-spacing: 0.06em; + text-transform: uppercase; +} +.metric.delivered strong { + color: var(--color-green); +} +.metric.opened strong { + color: var(--color-green); +} +.metric.bounced strong { + color: var(--color-red); +} + +.feed { + min-height: 0; + overflow-y: auto; + padding: 14px 18px 18px; + display: grid; + gap: 11px; + align-content: start; +} + +.mail { + border: 1px solid var(--color-shade4); + border-radius: var(--radius); + background: var(--color-shade7); + padding: 14px 15px 16px; + transition: border-color 0.15s; +} +.mail:hover { + border-color: var(--color-shade1); +} +.mail.bad { + border-color: #4a2626; +} +.mail-head { + display: grid; + grid-template-columns: auto minmax(0, 1fr) auto auto; + gap: 11px; + align-items: center; +} +.mail-del { + width: 26px; + height: 26px; + padding: 0; + flex: 0 0 auto; + border: 1px solid transparent; + border-radius: var(--radius-sm); + background: transparent; + color: var(--color-n5); + font-size: 17px; + line-height: 1; + display: grid; + place-items: center; +} +.mail-del:hover:not(:disabled) { + background: #1a0f0f; + border-color: #4a2626; + color: var(--color-red); +} +.avatar { + width: 36px; + height: 36px; + flex: 0 0 auto; + border-radius: var(--radius); + display: grid; + place-items: center; + font-weight: 700; + font-size: 12px; + color: var(--av, var(--color-n3)); + background: color-mix(in srgb, var(--av, #555) 15%, transparent); + border: 1px solid color-mix(in srgb, var(--av, #555) 32%, transparent); +} +.mail-who { + min-width: 0; +} +.mail-to { + font-size: 14px; + color: var(--color-n1); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.mail-sub { + margin-top: 2px; + font-size: 13px; + color: var(--color-n4); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.mail-when { + flex: 0 0 auto; + text-align: right; + color: var(--color-n5); + font-family: var(--font-ibm); + font-size: 11px; +} + +/* Delivery timeline */ +.track { + display: flex; + margin-top: 15px; +} +.node { + flex: 1; + position: relative; + text-align: center; + min-width: 0; +} +.node::before { + content: ''; + position: absolute; + top: 6px; + left: -50%; + width: 100%; + height: 2px; + background: var(--color-shade4); +} +.node:first-child::before { + display: none; +} +.bead { + position: relative; + z-index: 1; + display: block; + width: 14px; + height: 14px; + margin: 0 auto; + border-radius: 50%; + background: var(--color-shade4); + border: 2px solid var(--color-shade6); +} +.node-label { + display: block; + margin-top: 7px; + font-family: var(--font-ibm); + font-size: 10px; + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--color-n5); +} +.node-time { + display: block; + margin-top: 2px; + font-family: var(--font-ibm); + font-size: 10px; + color: var(--color-n5); + min-height: 12px; +} + +.node.done::before { + background: var(--color-green); +} +.node.done .bead { + background: var(--color-green); + box-shadow: 0 0 0 3px var(--color-green-10); +} +.node.done .node-label { + color: var(--color-n2); +} +.node.done .node-time { + color: var(--color-n4); +} + +.node.live::before { + background: var(--color-green); +} +.node.live .bead { + background: var(--color-green); + box-shadow: 0 0 0 5px color-mix(in srgb, var(--color-green) 26%, transparent); + animation: live-pulse 1.8s ease-in-out infinite; +} +.node.live .node-label { + color: var(--color-green); +} + +.node.bad::before { + background: var(--color-red); +} +.node.bad .bead { + background: var(--color-red); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-red) 20%, transparent); +} +.node.bad .node-label { + color: var(--color-red); +} + +.node.warn::before { + background: var(--color-yellow); +} +.node.warn .bead { + background: var(--color-yellow); +} +.node.warn .node-label { + color: var(--color-yellow); +} + +/* One-shot: the node that just reached its state ripples out from the bead. + Applied only to the node that changed this render. */ +.node.just-lit .bead::after { + content: ''; + position: absolute; + inset: -3px; + border-radius: 50%; + border: 2px solid var(--color-green); + animation: bead-ripple 0.65s ease-out forwards; +} +.node.just-lit.bad .bead::after { + border-color: var(--color-red); +} +.node.just-lit.warn .bead::after { + border-color: var(--color-yellow); +} +.node.just-lit::before { + animation: connector-sweep 0.5s ease-out; +} +.node.just-lit .node-label, +.node.just-lit .node-time { + animation: label-in 0.5s ease-out; +} + +.mail.card-enter { + animation: card-enter 0.4s cubic-bezier(0.2, 0.8, 0.2, 1); +} +/* Odometer roll: the current number exits as the incoming value enters. + JavaScript injects the clip window for the animation. */ +.metric strong .odo-clip { + display: block; + overflow: hidden; + height: 1em; +} +.metric strong .odo { + display: block; +} +.metric strong .odo-line { + height: 1em; + line-height: 1; +} +.metric strong .odo.roll-up { + animation: odo-up 0.42s cubic-bezier(0.3, 0.9, 0.3, 1) forwards; +} +.metric strong .odo.roll-down { + animation: odo-down 0.42s cubic-bezier(0.3, 0.9, 0.3, 1) forwards; +} + +@keyframes live-pulse { + 0%, + 100% { + box-shadow: 0 0 0 5px + color-mix(in srgb, var(--color-green) 26%, transparent); + } + 50% { + box-shadow: 0 0 0 8px color-mix(in srgb, var(--color-green) 8%, transparent); + } +} +@keyframes bead-ripple { + 0% { + transform: scale(1); + opacity: 0.9; + } + 100% { + transform: scale(2.6); + opacity: 0; + } +} +@keyframes connector-sweep { + 0% { + clip-path: inset(0 100% 0 0); + } + 100% { + clip-path: inset(0 0 0 0); + } +} +@keyframes label-in { + 0% { + opacity: 0.35; + } + 100% { + opacity: 1; + } +} +@keyframes card-enter { + 0% { + opacity: 0; + transform: translateY(-8px) scale(0.985); + } + 100% { + opacity: 1; + transform: none; + } +} +@keyframes odo-up { + from { + transform: translateY(0); + } + to { + transform: translateY(-1em); + } +} +@keyframes odo-down { + from { + transform: translateY(-1em); + } + to { + transform: translateY(0); + } +} +@media (prefers-reduced-motion: reduce) { + .node.live .bead, + .node.just-lit .bead::after, + .node.just-lit::before, + .node.just-lit .node-label, + .node.just-lit .node-time, + .mail.card-enter, + .metric strong .odo { + animation: none; + } +} + +.mail-err { + margin-top: 12px; + padding: 8px 10px; + border: 1px solid #4a2626; + border-radius: var(--radius-sm); + background: #1a0f0f; + color: var(--color-orange); + font-family: var(--font-ibm); + font-size: 11px; + line-height: 1.4; +} + +.empty { + border: 1px dashed var(--color-shade4); + border-radius: var(--radius); + padding: 34px 18px; + color: var(--color-n4); + text-align: center; + font-size: 13px; + line-height: 1.5; +} + +.page-foot { + min-height: 58px; + display: flex; + align-items: center; + justify-content: center; + gap: 11px; + border-top: 1px solid var(--color-shade4); +} +.page-foot .by { + color: var(--color-n4); + font-family: var(--font-ibm); + font-size: 10px; + font-weight: 600; + letter-spacing: 0.12em; + text-transform: uppercase; +} +.page-foot img { + height: 26px; + width: auto; + opacity: 0.85; +} + +@media (max-width: 1040px) { + /* Stack the panels at their natural heights and use page-level scrolling. */ + .shell { + height: auto; + min-height: 100vh; + } + .workspace { + grid-template-columns: 1fr; + grid-template-rows: none; + align-items: start; + } + .console { + min-height: 560px; + } +} +@media (max-width: 560px) { + .metrics { + grid-template-columns: repeat(2, 1fr); + } + .node-label { + font-size: 9px; + } +} diff --git a/spacetime-resend-ts/example/scripts/test-message.ts b/spacetime-resend-ts/example/scripts/test-message.ts new file mode 100644 index 00000000000..c7d2169c2c0 --- /dev/null +++ b/spacetime-resend-ts/example/scripts/test-message.ts @@ -0,0 +1,15 @@ +import assert from 'node:assert/strict'; +import { messageHtml } from '../spacetimedb/src/message'; + +const html = messageHtml( + 'Hello & friends\nNext line\n\nSecond paragraph' +); + +assert.match(html, /BlinkMacSystemFont/); +assert.match(html, /max-width:560px/); +assert.match(html, /Hello <team> & friends
Next line/); +assert.match(html, /Second paragraph/); +assert.equal((html.match(/

/); + +console.log('resend message tests passed'); diff --git a/spacetime-resend-ts/example/server.ts b/spacetime-resend-ts/example/server.ts new file mode 100644 index 00000000000..4d3412e9dde --- /dev/null +++ b/spacetime-resend-ts/example/server.ts @@ -0,0 +1,237 @@ +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { existsSync, readFileSync } from 'node:fs'; +import express, { type Request, type Response } from 'express'; +import dotenv from 'dotenv'; +import { + discardStoredServerToken, + exampleUiAssetsDir, + grantServerIdentity, + loadServerToken, + saveServerToken, +} from '@spacetimedb/submodule-shared/server'; +import { DbConnection, type ErrorContext } from './src/module_bindings'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const inheritedEnv = new Set(Object.keys(process.env)); + +function loadEnv(pathname: string, override: boolean): void { + if (!existsSync(pathname)) return; + + const parsed = dotenv.parse(readFileSync(pathname)); + for (const [key, value] of Object.entries(parsed)) { + if (value.trim() === '') continue; + if (inheritedEnv.has(key)) continue; + if (override || process.env[key] === undefined) { + process.env[key] = value; + } + } +} + +// Shared env supplies secrets/defaults; example-local env wins for app settings. +// Explicit process environment has highest priority. +loadEnv(path.resolve(__dirname, '..', '..', '.env'), false); +loadEnv(path.resolve(__dirname, '..', '.env'), false); +loadEnv(path.resolve(__dirname, '.env'), true); + +const PORT = Number.parseInt(process.env.PORT ?? '8790', 10); +const HOST = process.env.HOST?.trim() || '127.0.0.1'; +const STDB_URI = process.env.STDB_URI ?? 'ws://127.0.0.1:3000'; +const STDB_HTTP = process.env.STDB_HTTP ?? 'http://127.0.0.1:3000'; +const DB_NAME = process.env.SPACETIMEDB_DB_NAME ?? 'spacetime-resend-example'; +const SPACETIME_BIN = process.env.SPACETIME_BIN?.trim() || 'spacetime'; +const RESEND_API_KEY = process.env.RESEND_API_KEY ?? ''; +const RESEND_WEBHOOK_SECRET = process.env.RESEND_WEBHOOK_SECRET ?? ''; +const DEFAULT_FROM = process.env.DEFAULT_FROM ?? 'onboarding@resend.dev'; +const RESEND_TEST_RECIPIENTS = [ + 'delivered@resend.dev', + 'bounced@resend.dev', + 'complained@resend.dev', +]; +const ALLOWED_RECIPIENTS = [ + ...new Set([ + ...RESEND_TEST_RECIPIENTS, + ...(process.env.RESEND_ALLOWED_RECIPIENTS ?? '') + .split(',') + .map(value => value.trim().toLowerCase()) + .filter(Boolean), + ]), +]; +const SERVER_TOKEN_PATH = path.resolve(__dirname, '.stdb-server-token'); + +let stdb: DbConnection | null = null; +let resendConfigured = false; + +type ConnectedServer = { + connection: DbConnection; + identity: string; +}; + +function connectAttempt(token: string | undefined): Promise { + return new Promise((resolve, reject) => { + let builder = DbConnection.builder() + .withUri(STDB_URI) + .withDatabaseName(DB_NAME) + .onConnect((connection, identity, nextToken) => { + if (!process.env.STDB_SERVER_TOKEN?.trim()) { + saveServerToken(SERVER_TOKEN_PATH, nextToken); + } + resolve({ connection, identity: identity.toHexString() }); + }) + .onDisconnect((_ctx, err) => { + console.error( + `[stdb] disconnected: ${err?.message ?? 'unknown'} - exiting for supervisor restart` + ); + process.exit(1); + }) + .onConnectError((_ctx: ErrorContext, err) => reject(err)); + if (token) builder = builder.withToken(token); + builder.build(); + }); +} + +async function connect(): Promise { + const stored = loadServerToken( + SERVER_TOKEN_PATH, + process.env.STDB_SERVER_TOKEN + ); + try { + return await connectAttempt(stored.token); + } catch (error) { + if (stored.source !== 'file') throw error; + discardStoredServerToken(SERVER_TOKEN_PATH); + console.warn( + '[stdb] stored server token was rejected; creating a new identity' + ); + return connectAttempt(undefined); + } +} + +function requireStdb(): DbConnection { + if (!stdb) throw new Error('STDB not connected yet'); + return stdb; +} + +const app = express(); + +// Webhook uses the raw body because Svix signs raw bytes. Register it before express.json(). +app.post( + '/webhook/resend', + express.raw({ type: '*/*', limit: '512kb' }), + handleResendWebhook +); + +app.use(express.json({ limit: '512kb' })); +app.use('/assets', express.static(exampleUiAssetsDir)); +app.use(express.static(path.join(__dirname, 'public'))); + +app.get('/api/health', (_req: Request, res: Response) => { + res.json({ ok: true, databaseName: DB_NAME }); +}); + +app.get('/api/config', (_req: Request, res: Response) => { + res.json({ + spacetimeUri: STDB_URI, + databaseName: DB_NAME, + resendConfigured, + defaultFrom: DEFAULT_FROM, + allowedRecipients: ALLOWED_RECIPIENTS, + }); +}); + +async function handleResendWebhook(req: Request, res: Response): Promise { + const rawBody = + req.body instanceof Buffer ? req.body : Buffer.from(String(req.body ?? '')); + const url = `${STDB_HTTP}/v1/database/${DB_NAME}/route/webhook/resend`; + + const headers: Record = { + 'content-type': 'application/json', + }; + for (const name of ['svix-id', 'svix-timestamp', 'svix-signature']) { + const value = req.headers[name]; + if (typeof value === 'string') headers[name] = value; + else if (Array.isArray(value)) headers[name] = value.join(','); + } + + try { + const upstream = await fetch(url, { + method: 'POST', + headers, + body: rawBody, + }); + const text = await upstream.text(); + res.status(upstream.status).send(text); + } catch (err) { + const reason = err instanceof Error ? err.message : String(err); + console.error(`[webhook] passthrough to STDB route failed: ${reason}`); + res.status(502).send(`passthrough failed: ${reason}`); + } +} + +async function bootstrapResendConfig(): Promise { + if (!RESEND_API_KEY) { + resendConfigured = false; + process.stdout.write( + ' ! RESEND_API_KEY missing: Dispatch connects; email sends require configuration\n' + ); + return; + } + + await requireStdb().procedures['resend.setResendConfig']({ + apiKey: RESEND_API_KEY, + webhookSigningSecret: RESEND_WEBHOOK_SECRET || undefined, + defaultFrom: DEFAULT_FROM, + }); + await requireStdb().procedures.setDispatchPolicy({ + allowedRecipientsJson: JSON.stringify(ALLOWED_RECIPIENTS), + }); + resendConfigured = true; + process.stdout.write(' + Resend config loaded from server environment\n'); +} + +(async () => { + console.log(`[stdb] connecting to ${STDB_URI}/${DB_NAME} ...`); + try { + const connected = await connect(); + stdb = connected.connection; + grantServerIdentity({ + spacetimeBin: SPACETIME_BIN, + server: STDB_HTTP, + database: DB_NAME, + procedure: 'resend.add_admin_identity', + identity: connected.identity, + }); + console.log(`[stdb] connected as authorized server ${connected.identity}`); + } catch (err) { + console.error( + `[stdb] connection or authorization failed: ${err instanceof Error ? err.message : String(err)}` + ); + console.error( + '[stdb] is the SpacetimeDB host running and the module published?' + ); + process.exit(1); + } + + try { + await bootstrapResendConfig(); + } catch (err) { + console.error( + `[resend] configuration failed: ${err instanceof Error ? err.message : String(err)}` + ); + process.exit(1); + } + + app.listen(PORT, HOST, () => { + process.stdout.write(`\nDispatch running at http://${HOST}:${PORT}\n`); + if (!RESEND_WEBHOOK_SECRET) { + process.stdout.write( + ' ! RESEND_WEBHOOK_SECRET not set - incoming webhooks are rejected\n' + ); + } + process.stdout.write( + ` webhook endpoint: POST http://127.0.0.1:${PORT}/webhook/resend\n` + ); + process.stdout.write(` database: ${STDB_URI}/${DB_NAME}\n\n`); + }); +})(); diff --git a/spacetime-resend-ts/example/spacetimedb/.npmrc b/spacetime-resend-ts/example/spacetimedb/.npmrc new file mode 100644 index 00000000000..44bdf80d1df --- /dev/null +++ b/spacetime-resend-ts/example/spacetimedb/.npmrc @@ -0,0 +1 @@ +minimum-release-age=1440 diff --git a/spacetime-resend-ts/example/spacetimedb/package.json b/spacetime-resend-ts/example/spacetimedb/package.json new file mode 100644 index 00000000000..375d324832a --- /dev/null +++ b/spacetime-resend-ts/example/spacetimedb/package.json @@ -0,0 +1,20 @@ +{ + "name": "spacetime-resend-example-module", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "build": "spacetime build", + "publish:local": "spacetime publish --server local --yes spacetime-resend-example", + "publish:local:reset": "spacetime publish --server local --yes --delete-data=always spacetime-resend-example" + }, + "dependencies": { + "@spacetimedb/rate-limit": "workspace:*", + "@spacetimedb/resend": "workspace:*", + "spacetimedb": "workspace:*" + }, + "devDependencies": { + "@types/node": "^22.10.2", + "typescript": "^5.9.3" + } +} diff --git a/spacetime-resend-ts/example/spacetimedb/src/index.ts b/spacetime-resend-ts/example/spacetimedb/src/index.ts new file mode 100644 index 00000000000..9dabbf7cbf2 --- /dev/null +++ b/spacetime-resend-ts/example/spacetimedb/src/index.ts @@ -0,0 +1,267 @@ +// The browser never talks to Resend directly and is never granted admin. It calls +// host procedures that forward to the submodule through `ctx.as.resend`. Resend's +// base tables stay private; caller-scoped views below expose only the current +// connection's dispatches. + +import { schema, table, t, SenderError, Router } from 'spacetimedb/server'; +import * as resend from '@spacetimedb/resend/submodule'; +import * as rateLimit from '@spacetimedb/rate-limit/submodule'; +import { messageHtml } from './message'; + +const dispatchPolicy = table( + { name: 'dispatch_policy', public: false }, + { + singleton: t.bool().primaryKey(), + allowedRecipientsJson: t.string(), + updatedAt: t.timestamp(), + } +); + +const spacetimedb = schema({ + resend, + rateLimit, + dispatchPolicy, +}); + +function subjectFor(ctx: { sender: { toHexString(): string } }): string { + return ctx.sender.toHexString(); +} + +export const myDispatchEmails = spacetimedb.view( + { name: 'my_dispatch_emails', public: true }, + resend.t.array(resend.resendEmailTable.rowType), + ctx => [...ctx.db.resend.resendEmail.byUserId.filter(subjectFor(ctx))] +); + +export const myDispatchDeliveryEvents = spacetimedb.view( + { name: 'my_dispatch_delivery_events', public: true }, + resend.t.array(resend.resendDeliveryEventTable.rowType), + ctx => { + const out = []; + for (const email of ctx.db.resend.resendEmail.byUserId.filter( + subjectFor(ctx) + )) { + for (const event of ctx.db.resend.resendDeliveryEvent.byResendId.filter( + email.resendId + )) { + out.push(event); + } + } + return out; + } +); + +const MAX_SUBJECT = 200; +const MAX_MESSAGE = 5000; +const MAX_ALLOWED_RECIPIENTS = 10; +const MAX_RECIPIENT_POLICY_LENGTH = 4096; +const CALLER_SEND_LIMIT = 5; +const CALLER_WINDOW_SECONDS = 10 * 60; +const GLOBAL_SEND_LIMIT = 25; +const GLOBAL_WINDOW_SECONDS = 60 * 60; + +function fail(message: string): never { + throw new SenderError(`dispatch.${message}`); +} + +function normalizeEmail(email: string): string { + const out = email.trim().toLowerCase(); + if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(out)) fail('invalid_email'); + return out; +} + +function parseAllowedRecipients(value: string): string[] { + if (value.length > MAX_RECIPIENT_POLICY_LENGTH) { + fail('recipient_policy_too_large'); + } + let parsed: unknown; + try { + parsed = JSON.parse(value); + } catch { + fail('invalid_recipient_policy'); + } + if (!Array.isArray(parsed) || parsed.length === 0) { + fail('invalid_recipient_policy'); + } + const recipients = [ + ...new Set(parsed.map(value => normalizeEmail(String(value)))), + ]; + if (recipients.length > MAX_ALLOWED_RECIPIENTS) { + fail('too_many_allowed_recipients'); + } + return recipients; +} + +function clean( + value: string | undefined, + fallback: string, + max: number +): string { + const out = (value ?? '').trim().replace(/\s+/g, ' '); + return (out || fallback).slice(0, max); +} + +const dispatchSendResult = t.object('DispatchSendResult', { + ok: t.bool(), + resendId: t.option(t.string()), + message: t.string(), +}); + +export const set_dispatch_policy = spacetimedb.procedure( + { allowedRecipientsJson: t.string() }, + t.bool(), + (ctx, args) => { + const isAdmin = ctx.withTx( + tx => tx.db.resend.resendAdminIdentity.identity.find(ctx.sender) != null + ); + if (!isAdmin) fail('not_authorized'); + const recipients = parseAllowedRecipients(args.allowedRecipientsJson); + ctx.withTx(tx => { + const existing = tx.db.dispatchPolicy.singleton.find(true); + const row = { + singleton: true, + allowedRecipientsJson: JSON.stringify(recipients), + updatedAt: ctx.timestamp, + }; + if (existing) tx.db.dispatchPolicy.singleton.update(row); + else tx.db.dispatchPolicy.insert(row); + }); + return true; + } +); + +// The host policy restricts recipients and applies caller and global quotas before +// delegating delivery through the private Resend configuration. +export const send_dispatch = spacetimedb.procedure( + { + to: t.string(), + subject: t.string(), + message: t.string(), + }, + dispatchSendResult, + (ctx, args) => { + const to = normalizeEmail(args.to); + const subject = clean(args.subject, '(no subject)', MAX_SUBJECT); + const message = (args.message ?? '').trim().slice(0, MAX_MESSAGE); + if (!message) fail('empty_message'); + + const policyJson = ctx.withTx( + tx => + tx.db.dispatchPolicy.singleton.find(true)?.allowedRecipientsJson ?? null + ); + if (policyJson == null) fail('policy_missing'); + const allowed = parseAllowedRecipients(policyJson); + if (!allowed.includes(to)) fail('recipient_not_allowed'); + + const authorization = ctx.withTx(tx => { + const caller = rateLimit.consumeRateLimit(tx.as.rateLimit, { + key: `dispatch:caller:${subjectFor(ctx)}`, + scope: 'dispatch.send.caller', + limit: CALLER_SEND_LIMIT, + windowSeconds: CALLER_WINDOW_SECONDS, + }); + if (!caller.allowed) return 'rate_limited' as const; + const global = rateLimit.consumeRateLimit(tx.as.rateLimit, { + key: 'dispatch:global', + scope: 'dispatch.send.global', + limit: GLOBAL_SEND_LIMIT, + windowSeconds: GLOBAL_WINDOW_SECONDS, + }); + return global.allowed ? ('allowed' as const) : ('rate_limited' as const); + }); + if (authorization !== 'allowed') fail(authorization); + + try { + const result = resend.sendEmail(ctx.as.resend, { + to: [to], + subject, + html: messageHtml(message), + text: message, + tagsJson: JSON.stringify([ + { name: 'source', value: 'dispatch' }, + { name: 'userId', value: subjectFor(ctx) }, + ]), + }); + return { + ok: true, + resendId: result.resendId, + message: `Dispatched to ${to}.`, + }; + } catch { + return { + ok: false, + resendId: undefined, + message: 'dispatch.delivery_failed', + }; + } + } +); + +const dispatchDeleteResult = t.object('DispatchDeleteResult', { + ok: t.bool(), + removed: t.u32(), +}); + +// Remove a single dispatch and any delivery events it collected. This is a demo +// convenience so the log can be pruned; it writes directly to the submodule tables. +export const delete_dispatch = spacetimedb.procedure( + { resendId: t.string() }, + dispatchDeleteResult, + (ctx, args) => { + return ctx.withTx(tx => { + let removed = 0; + const row = tx.db.resend.resendEmail.resendId.find(args.resendId); + if (row && row.userId === subjectFor(ctx)) { + tx.db.resend.resendEmail.delete(row); + removed += 1; + for (const event of tx.db.resend.resendDeliveryEvent.byResendId.filter( + args.resendId + )) { + tx.db.resend.resendDeliveryEvent.delete(event); + } + } + return { ok: true, removed }; + }); + } +); + +export const clear_dispatches = spacetimedb.procedure( + {}, + dispatchDeleteResult, + ctx => { + return ctx.withTx(tx => { + let removed = 0; + const owned = [ + ...tx.db.resend.resendEmail.byUserId.filter(subjectFor(ctx)), + ]; + for (const row of owned) { + for (const event of tx.db.resend.resendDeliveryEvent.byResendId.filter( + row.resendId + )) { + tx.db.resend.resendDeliveryEvent.delete(event); + } + tx.db.resend.resendEmail.delete(row); + removed += 1; + } + return { ok: true, removed }; + }); + } +); + +// Resend posts delivery webhooks straight to the database over a native STDB HTTP +// route. The submodule verifies the svix signature in-module (via crypto-ts) and +// ingests. No Node relay does any of this work. +const resendWebhookHandler = resend.makeResendWebhookHandler(); +export const resend_webhook = spacetimedb.httpHandler((ctx, req) => + resendWebhookHandler(ctx.as.resend, req) +); +export const router = spacetimedb.httpRouter( + new Router().post('/webhook/resend', resend_webhook) +); + +export const init = spacetimedb.init(ctx => { + resend.installResend(ctx.as.resend); + rateLimit.installRateLimit(ctx.as.rateLimit); +}); + +export default spacetimedb; diff --git a/spacetime-resend-ts/example/spacetimedb/src/message.ts b/spacetime-resend-ts/example/spacetimedb/src/message.ts new file mode 100644 index 00000000000..38656a3766c --- /dev/null +++ b/spacetime-resend-ts/example/spacetimedb/src/message.ts @@ -0,0 +1,20 @@ +function escapeHtml(value: string): string { + return value + .replace(/&/g, '&') + .replace(//g, '>'); +} + +export function messageHtml(message: string): string { + const paragraphs = message + .split(/\n{2,}/) + .map(block => escapeHtml(block).replace(/\n/g, '
')) + .map(block => `

${block}

`) + .join(''); + return [ + "
', + paragraphs, + '
', + ].join(''); +} diff --git a/spacetime-resend-ts/example/spacetimedb/tsconfig.json b/spacetime-resend-ts/example/spacetimedb/tsconfig.json new file mode 100644 index 00000000000..f004a6cbc79 --- /dev/null +++ b/spacetime-resend-ts/example/spacetimedb/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "strict": true, + "declaration": false, + "noEmit": true, + "skipLibCheck": true, + "moduleResolution": "Bundler", + "isolatedModules": true, + "allowImportingTsExtensions": true + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/spacetime-resend-ts/example/src/app.ts b/spacetime-resend-ts/example/src/app.ts new file mode 100644 index 00000000000..1193f9e73f4 --- /dev/null +++ b/spacetime-resend-ts/example/src/app.ts @@ -0,0 +1,673 @@ +import { + DbConnection, + tables, + type ErrorContext, + type EventContext, + type SubscriptionEventContext, +} from './module_bindings'; +import type { Timestamp } from 'spacetimedb'; +import { messageHtml } from '../spacetimedb/src/message'; + +type TableAccessor = { + iter(): Iterable; + onInsert(cb: (ctx: EventContext, row: T) => void): void; + onUpdate(cb: (ctx: EventContext, old: T, row: T) => void): void; + onDelete(cb: (ctx: EventContext, row: T) => void): void; +}; + +// Mirrors the caller-scoped my_dispatch_emails view. Options arrive as +// `T | undefined`; status is a tagged enum. +type ResendEmail = { + resendId: string; + fromAddress: string; + toAddressesJson: string; + subject?: string; + status: { tag: string } | string; + lastError?: string; + bouncedAt?: Timestamp; + failedAt?: Timestamp; + failureReason?: string; + complained: boolean; + complainedAt?: Timestamp; + opened: boolean; + openedAt?: Timestamp; + clicked: boolean; + clickedAt?: Timestamp; + deliveredAt?: Timestamp; + sentAt?: Timestamp; + createdAt: Timestamp; + updatedAt: Timestamp; +}; + +type SendResult = { + ok: boolean; + resendId?: string; + message: string; +}; + +type ServerConfig = { + spacetimeUri: string; + databaseName: string; + resendConfigured: boolean; + defaultFrom: string; + allowedRecipients: string[]; +}; + +const $ = (id: string) => + document.getElementById(id) as T; + +let conn: DbConnection | null = null; +let currentConfig: ServerConfig | null = null; +let sending = false; + +function escapeHtml(value: unknown): string { + return String(value ?? '') + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + +function tagOf(value: { tag: string } | string | undefined): string { + if (value && typeof value === 'object' && 'tag' in value) return value.tag; + return String(value ?? ''); +} + +function timestampMs(ts: Timestamp | undefined): number { + if (!ts) return 0; + return Number(ts.microsSinceUnixEpoch / 1000n); +} + +function timeLabel(ts: Timestamp | undefined): string { + const ms = timestampMs(ts); + if (!ms) return ''; + return new Date(ms).toLocaleTimeString([], { + hour: 'numeric', + minute: '2-digit', + second: '2-digit', + }); +} + +const AVATAR_COLORS = [ + '#4cf490', + '#02befa', + '#a880ff', + '#fbdc8e', + '#ff9e9e', + '#00ccb4', + '#ff80fb', +]; + +// The Resend test addresses always get the same, recognizable colour. +const KNOWN_AVATAR_COLORS: Record = { + 'delivered@resend.dev': '#4cf490', // green + 'bounced@resend.dev': '#ff9e9e', // orange + 'complained@resend.dev': '#a880ff', // purple +}; + +function avatarColor(seed: string): string { + const known = KNOWN_AVATAR_COLORS[seed.toLowerCase()]; + if (known) return known; + let h = 0; + for (let i = 0; i < seed.length; i++) h = (h * 31 + seed.charCodeAt(i)) >>> 0; + return AVATAR_COLORS[h % AVATAR_COLORS.length]; +} + +function recipient(row: ResendEmail): string { + try { + const parsed = JSON.parse(row.toAddressesJson); + if (Array.isArray(parsed) && parsed.length > 0) return String(parsed[0]); + } catch { + /* fall through */ + } + return row.toAddressesJson; +} + +function initials(email: string): string { + const local = email.split('@')[0] ?? email; + return (local.slice(0, 2) || '?').toUpperCase(); +} + +function showError(message: string) { + $('form-error').textContent = message; +} + +function clearError() { + $('form-error').textContent = ''; +} + +let flashTimer: ReturnType | undefined; + +function flashSent() { + const btn = $('send-btn') as HTMLButtonElement; + btn.classList.add('sent'); + btn.textContent = 'Sent'; + if (flashTimer) clearTimeout(flashTimer); + flashTimer = setTimeout(() => { + btn.classList.remove('sent'); + if (!sending) btn.textContent = 'Send'; + }, 1800); +} + +async function loadServerConfig(): Promise { + const res = await fetch('/api/config'); + if (!res.ok) throw new Error(`/api/config returned ${res.status}`); + return (await res.json()) as ServerConfig; +} + +async function connect(config: ServerConfig): Promise { + return new Promise((resolve, reject) => { + DbConnection.builder() + .withUri(config.spacetimeUri) + .withDatabaseName(config.databaseName) + .onConnect(c => resolve(c)) + .onDisconnect((_ctx, err) => { + showError(`Disconnected: ${err?.message ?? 'connection lost'}`); + }) + .onConnectError((_ctx: ErrorContext, err) => reject(err)) + .build(); + }); +} + +function getTableAccessor(name: string): TableAccessor | undefined { + const db = (conn?.db ?? {}) as Record | undefined>; + return db[name]; +} + +function emails(): ResendEmail[] { + return [ + ...(getTableAccessor('myDispatchEmails')?.iter() ?? []), + ].sort((a, b) => { + return timestampMs(b.createdAt) - timestampMs(a.createdAt); + }); +} + +type Node = { + key: string; + label: string; + at?: Timestamp; + state: 'done' | 'live' | 'pending' | 'bad' | 'warn'; +}; + +// Turn a stored email into an ordered set of lifecycle nodes. The EmailStatus enum +// tops out at Delivered; Opened/Clicked are booleans; negatives are terminal. +function timeline(row: ResendEmail): Node[] { + const status = tagOf(row.status); + const nodes: Node[] = [ + { key: 'queued', label: 'Queued', at: row.createdAt, state: 'done' }, + ]; + + const sentReached = + Boolean(row.sentAt) || + Boolean(row.deliveredAt) || + row.opened || + row.clicked || + status === 'Sent' || + status === 'Delivered'; + nodes.push({ + key: 'sent', + label: 'Sent', + at: row.sentAt, + state: sentReached ? 'done' : 'pending', + }); + + if (status === 'Bounced') { + nodes.push({ + key: 'bounced', + label: 'Bounced', + at: row.bouncedAt, + state: 'bad', + }); + return nodes; + } + if (status === 'Failed') { + nodes.push({ + key: 'failed', + label: 'Failed', + at: row.failedAt, + state: 'bad', + }); + return nodes; + } + if (status === 'Cancelled') { + nodes.push({ key: 'cancelled', label: 'Cancelled', state: 'warn' }); + return nodes; + } + + // A complaint (marked as spam) implies the mail was delivered, and it is terminal. + const deliveredReached = + Boolean(row.deliveredAt) || + row.opened || + row.clicked || + status === 'Delivered' || + row.complained; + const delayed = status === 'DeliveryDelayed' && !deliveredReached; + nodes.push({ + key: 'delivered', + label: delayed ? 'Delayed' : 'Delivered', + at: row.deliveredAt, + state: deliveredReached ? 'done' : delayed ? 'warn' : 'pending', + }); + + if (row.complained) { + // Terminal: show completed positive steps followed by Complaint. + // No trailing "pending" steps and no live pulse - the lifecycle is over. + if (row.opened) + nodes.push({ + key: 'opened', + label: 'Opened', + at: row.openedAt, + state: 'done', + }); + if (row.clicked) + nodes.push({ + key: 'clicked', + label: 'Clicked', + at: row.clickedAt, + state: 'done', + }); + nodes.push({ + key: 'complaint', + label: 'Complaint', + at: row.complainedAt, + state: 'bad', + }); + return nodes; + } + + nodes.push({ + key: 'opened', + label: 'Opened', + at: row.openedAt, + state: row.opened ? 'done' : 'pending', + }); + nodes.push({ + key: 'clicked', + label: 'Clicked', + at: row.clickedAt, + state: row.clicked ? 'done' : 'pending', + }); + + // The furthest reached positive node is the live frontier. A completed later + // node makes the final completed positive node the subtle accent. + const lastDone = nodes.map(n => n.state === 'done').lastIndexOf(true); + if ( + lastDone > 0 && + nodes[lastDone].state === 'done' && + nodes.some(n => n.state === 'pending') + ) { + nodes[lastDone].state = 'live'; + } + return nodes; +} + +function headPill(row: ResendEmail): { cls: string; text: string } { + const status = tagOf(row.status); + if (status === 'Bounced') return { cls: 'red', text: 'Bounced' }; + if (status === 'Failed') return { cls: 'red', text: 'Failed' }; + if (status === 'Cancelled') return { cls: 'muted', text: 'Cancelled' }; + if (row.complained) return { cls: 'red', text: 'Complaint' }; + if (row.clicked) return { cls: 'green', text: 'Clicked' }; + if (row.opened) return { cls: 'green', text: 'Opened' }; + if (row.deliveredAt || status === 'Delivered') + return { cls: 'green', text: 'Delivered' }; + if (status === 'DeliveryDelayed') return { cls: 'yellow', text: 'Delayed' }; + if (row.sentAt || status === 'Sent') return { cls: 'green', text: 'Sent' }; + return { cls: 'muted', text: 'Queued' }; +} + +// Per-email rendered-node keys ensure each changed transition animates once. +const lastReached = new Map>(); +const knownEmails = new Set(); +const lastCounts: Record = { + sent: -1, + delivered: -1, + opened: -1, + bounced: -1, +}; + +function prefersReducedMotion(): boolean { + return ( + typeof matchMedia === 'function' && + matchMedia('(prefers-reduced-motion: reduce)').matches + ); +} + +// Odometer roll: stack the current and incoming values in a clip window. An +// increase rolls up from below; a decrease rolls down. +function rollNumber(el: HTMLElement, from: number, to: number) { + const up = to > from; + const top = up ? from : to; + const bottom = up ? to : from; + el.innerHTML = + `` + + `${top}${bottom}` + + ``; + const odo = el.querySelector('.odo'); + if (!odo) { + el.textContent = String(to); + return; + } + // Collapse back to a plain number once the roll finishes (or is superseded). + const settle = () => { + if (el.contains(odo)) el.textContent = String(to); + }; + odo.addEventListener('animationend', settle, { once: true }); + setTimeout(settle, 600); +} + +function bumpMetric(id: string, key: string, value: number) { + const el = $(id); + const prev = lastCounts[key]; + lastCounts[key] = value; + if (prev < 0 || prev === value || prefersReducedMotion()) { + el.textContent = String(value); + return; + } + rollNumber(el, prev, value); +} + +function renderMetrics(list: ResendEmail[]) { + const delivered = list.filter( + r => + r.deliveredAt || r.opened || r.clicked || tagOf(r.status) === 'Delivered' + ).length; + const opened = list.filter(r => r.opened).length; + const bounced = list.filter(r => { + const s = tagOf(r.status); + return s === 'Bounced' || s === 'Failed' || r.complained; + }).length; + bumpMetric('count-sent', 'sent', list.length); + bumpMetric('count-delivered', 'delivered', delivered); + bumpMetric('count-opened', 'opened', opened); + bumpMetric('count-bounced', 'bounced', bounced); +} + +function nodeHtml(node: Node, lit: boolean): string { + const time = node.at ? timeLabel(node.at) : ''; + return ` +
+ + ${escapeHtml(node.label)} + ${escapeHtml(time)} +
`; +} + +function mailHtml( + row: ResendEmail, + nodes: Node[], + newlyLit: Set, + isNew: boolean +): string { + const to = recipient(row); + const pill = headPill(row); + const bad = pill.cls === 'red'; + const subject = + row.subject && row.subject.length ? row.subject : '(no subject)'; + const track = nodes.map(n => nodeHtml(n, newlyLit.has(n.key))).join(''); + const error = + bad && row.failureReason + ? `
${escapeHtml(row.failureReason)}
` + : bad && row.lastError + ? `
${escapeHtml(row.lastError)}
` + : ''; + return ` +
+
+ ${escapeHtml(initials(to))} + + ${escapeHtml(to)} + ${escapeHtml(subject)} + + ${escapeHtml(timeLabel(row.createdAt))} + +
+
${track}
+ ${error} +
`; +} + +function reachedKeys(nodes: Node[]): Set { + return new Set(nodes.filter(n => n.state !== 'pending').map(n => n.key)); +} + +function render() { + const list = emails(); + renderMetrics(list); + $('feed-count').textContent = `${list.length} sent`; + ($('clear-btn') as HTMLButtonElement).disabled = list.length === 0; + + if (list.length === 0) { + $('feed').innerHTML = + `
No dispatches yet.
Compose a message and hit Send to watch it move.
`; + lastReached.clear(); + knownEmails.clear(); + return; + } + + const entries = list.map(row => ({ row, nodes: timeline(row) })); + + $('feed').innerHTML = entries + .map(({ row, nodes }) => { + const reached = reachedKeys(nodes); + const prev = lastReached.get(row.resendId); + const isNew = !knownEmails.has(row.resendId); + const newlyLit = new Set(); + if (prev) for (const k of reached) if (!prev.has(k)) newlyLit.add(k); + return mailHtml(row, nodes, newlyLit, isNew); + }) + .join(''); + + // Record post-render state so the next render can diff against it. + const currentIds = new Set(); + for (const { row, nodes } of entries) { + lastReached.set(row.resendId, reachedKeys(nodes)); + knownEmails.add(row.resendId); + currentIds.add(row.resendId); + } + for (const id of [...knownEmails]) { + if (!currentIds.has(id)) { + knownEmails.delete(id); + lastReached.delete(id); + } + } +} + +// Coalesce the burst of table events from a single webhook (email row update + +// delivery-event insert land together) into one render, so the change diff sees the +// whole transition at once and animates the node that advanced. +let renderScheduled = false; +function scheduleRender() { + if (renderScheduled) return; + renderScheduled = true; + setTimeout(() => { + renderScheduled = false; + render(); + }, 0); +} + +function registerCallbacksForTable(name: string): void { + const accessor = getTableAccessor(name); + if (!accessor) throw new Error(`missing table accessor: ${name}`); + accessor.onInsert(() => scheduleRender()); + accessor.onUpdate(() => scheduleRender()); + accessor.onDelete(() => scheduleRender()); +} + +function registerRowCallbacks(): void { + for (const name of ['myDispatchEmails', 'myDispatchDeliveryEvents']) { + registerCallbacksForTable(name); + } +} + +function subscribeToTables(connection: DbConnection): void { + connection + .subscriptionBuilder() + .onApplied((_ctx: SubscriptionEventContext) => { + render(); + if (!currentConfig?.resendConfigured) { + showError( + 'RESEND_API_KEY is missing in this example server environment.' + ); + } + }) + .onError((ctx: ErrorContext) => { + console.error('subscription error:', ctx.event); + showError('Subscription failed. Check the server console.'); + }) + .subscribe([tables.myDispatchEmails, tables.myDispatchDeliveryEvents]); +} + +async function sendDispatch( + to: string, + subject: string, + message: string +): Promise { + if (!conn) throw new Error('not connected'); + return conn.procedures.sendDispatch({ to, subject, message }); +} + +async function deleteDispatch(resendId: string): Promise { + if (!conn) throw new Error('not connected'); + await conn.procedures.deleteDispatch({ resendId }); +} + +async function clearDispatches(): Promise { + if (!conn) throw new Error('not connected'); + await conn.procedures.clearDispatches({}); +} + +function setSending(next: boolean) { + sending = next; + const btn = $('send-btn') as HTMLButtonElement; + btn.disabled = next; + if (next) btn.textContent = 'Sending...'; + else if (!btn.classList.contains('sent')) btn.textContent = 'Send'; +} + +function renderPreview() { + const subject = + ($('subject-input') as HTMLInputElement).value.trim() || '(no subject)'; + const message = ($('message-input') as HTMLTextAreaElement).value.trim(); + const from = currentConfig?.defaultFrom || 'onboarding@resend.dev'; + const body = message + ? messageHtml(message) + : 'Nothing to preview yet.'; + $('message-preview').innerHTML = ` +
+
${escapeHtml(subject)}
+
From ${escapeHtml(from)}
+
+
${body}
`; +} + +function setMsgTab(tab: 'write' | 'preview') { + const isPreview = tab === 'preview'; + if (isPreview) renderPreview(); + ($('message-input') as HTMLTextAreaElement).hidden = isPreview; + $('message-preview').hidden = !isPreview; + document.querySelectorAll('.msg-tab').forEach(b => { + b.classList.toggle('active', b.dataset.tab === tab); + }); +} + +async function submitCompose() { + if (sending) return; + const to = ($('to-input') as HTMLInputElement).value.trim(); + const subject = ($('subject-input') as HTMLInputElement).value.trim(); + const message = ($('message-input') as HTMLTextAreaElement).value.trim(); + if (!to) { + showError('Add a recipient first.'); + return; + } + if (!message) { + showError('Write a message before sending.'); + return; + } + clearError(); + setSending(true); + try { + const result = await sendDispatch(to, subject, message); + setSending(false); + if (result.ok) { + ($('subject-input') as HTMLInputElement).value = ''; + ($('message-input') as HTMLTextAreaElement).value = ''; + setMsgTab('write'); + flashSent(); + } else { + showError(result.message); + } + } catch (err) { + setSending(false); + showError(err instanceof Error ? err.message : String(err)); + } +} + +function registerUiHandlers(): void { + $('compose-form').addEventListener('submit', event => { + event.preventDefault(); + submitCompose().catch(err => { + console.error(err); + showError(err instanceof Error ? err.message : String(err)); + }); + }); + + document.querySelectorAll('.msg-tab').forEach(button => { + button.addEventListener('click', () => { + setMsgTab(button.dataset.tab === 'preview' ? 'preview' : 'write'); + }); + }); + + document + .querySelectorAll('[data-fill]') + .forEach(button => { + button.addEventListener('click', () => { + const email = button.dataset.fill ?? 'delivered@resend.dev'; + ($('to-input') as HTMLInputElement).value = email; + const subject = $('subject-input') as HTMLInputElement; + const message = $('message-input') as HTMLTextAreaElement; + if (!subject.value.trim()) subject.value = 'Hello from Dispatch'; + if (!message.value.trim()) + message.value = 'Watching this one travel through the pipeline.'; + clearError(); + ($('to-input') as HTMLInputElement).focus(); + }); + }); + + $('feed').addEventListener('click', event => { + const btn = (event.target as HTMLElement).closest( + '[data-del]' + ); + if (!btn) return; + const resendId = btn.dataset.del; + if (!resendId) return; + btn.setAttribute('disabled', 'true'); + deleteDispatch(resendId).catch(err => { + console.error(err); + showError(err instanceof Error ? err.message : String(err)); + }); + }); + + $('clear-btn').addEventListener('click', () => { + clearDispatches().catch(err => { + console.error(err); + showError(err instanceof Error ? err.message : String(err)); + }); + }); +} + +async function main() { + registerUiHandlers(); + currentConfig = await loadServerConfig(); + const recipientInput = $('to-input') as HTMLInputElement; + recipientInput.value = currentConfig.allowedRecipients[0] ?? ''; + conn = await connect(currentConfig); + registerRowCallbacks(); + subscribeToTables(conn); +} + +main().catch(err => { + console.error(err); + showError(err instanceof Error ? err.message : String(err)); +}); diff --git a/spacetime-resend-ts/example/src/module_bindings/clear_dispatches_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/clear_dispatches_procedure.ts new file mode 100644 index 00000000000..6621a844b9c --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/clear_dispatches_procedure.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + DispatchDeleteResult, +} from "./types"; + +export const params = { +}; +export const returnType = DispatchDeleteResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/delete_dispatch_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/delete_dispatch_procedure.ts new file mode 100644 index 00000000000..9e246ad1174 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/delete_dispatch_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + DispatchDeleteResult, +} from "./types"; + +export const params = { + resendId: __t.string(), +}; +export const returnType = DispatchDeleteResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/index.ts b/spacetime-resend-ts/example/src/module_bindings/index.ts new file mode 100644 index 00000000000..b0aafebcd54 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/index.ts @@ -0,0 +1,239 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +// This was generated using spacetimedb cli version 2.8.3 (commit 8e410d2842147bd8e5a32a9589cc00c19f7478e2). + +/* eslint-disable */ +/* tslint:disable */ +import { + DbConnectionBuilder as __DbConnectionBuilder, + DbConnectionImpl as __DbConnectionImpl, + SubscriptionBuilderImpl as __SubscriptionBuilderImpl, + TypeBuilder as __TypeBuilder, + Uuid as __Uuid, + convertToAccessorMap as __convertToAccessorMap, + makeQueryBuilder as __makeQueryBuilder, + procedureSchema as __procedureSchema, + procedures as __procedures, + reducerSchema as __reducerSchema, + reducers as __reducers, + schema as __schema, + t as __t, + table as __table, + type AlgebraicTypeType as __AlgebraicTypeType, + type DbConnectionConfig as __DbConnectionConfig, + type ErrorContextInterface as __ErrorContextInterface, + type Event as __Event, + type EventContextInterface as __EventContextInterface, + type Infer as __Infer, + type QueryBuilder as __QueryBuilder, + type ReducerEventContextInterface as __ReducerEventContextInterface, + type RemoteModule as __RemoteModule, + type SubscriptionEventContextInterface as __SubscriptionEventContextInterface, + type SubscriptionHandleImpl as __SubscriptionHandleImpl, +} from "spacetimedb"; + +// Import all reducer arg schemas + +// Import all procedure arg schemas +import * as ClearDispatchesProcedure from "./clear_dispatches_procedure"; +import * as DeleteDispatchProcedure from "./delete_dispatch_procedure"; +import * as SendDispatchProcedure from "./send_dispatch_procedure"; +import * as SetDispatchPolicyProcedure from "./set_dispatch_policy_procedure"; + +// Import all table schema definitions +import MyDispatchDeliveryEventsRow from "./my_dispatch_delivery_events_table"; +import MyDispatchEmailsRow from "./my_dispatch_emails_table"; + +// Import namespace table schema definitions +import RateLimit_RateLimitConfigRow from "./rateLimit/rate_limit_config_table"; +import RateLimit_AdminRateLimitBucketsRow from "./rateLimit/admin_rate_limit_buckets_table"; + +// Import namespace reducer arg schemas +import Resend_IngestResendWebhookReducer from "./resend/ingest_resend_webhook_reducer"; +import Resend_ReplayWebhookEventReducer from "./resend/replay_webhook_event_reducer"; +import RateLimit_AddRateLimitAdminReducer from "./rateLimit/add_rate_limit_admin_reducer"; +import RateLimit_ResetBucketsReducer from "./rateLimit/reset_buckets_reducer"; +import RateLimit_UpdateConfigReducer from "./rateLimit/update_config_reducer"; + +// Import namespace procedure arg schemas +import * as Resend_AddAdminIdentityProcedure from "./resend/add_admin_identity_procedure"; +import * as Resend_CancelEmailProcedure from "./resend/cancel_email_procedure"; +import * as Resend_GetEmailProcedure from "./resend/get_email_procedure"; +import * as Resend_GetResendConfigStatusProcedure from "./resend/get_resend_config_status_procedure"; +import * as Resend_ListDeliveryEventsForEmailProcedure from "./resend/list_delivery_events_for_email_procedure"; +import * as Resend_ListEmailsByOrgIdProcedure from "./resend/list_emails_by_org_id_procedure"; +import * as Resend_ListEmailsByStatusProcedure from "./resend/list_emails_by_status_procedure"; +import * as Resend_ListEmailsByUserIdProcedure from "./resend/list_emails_by_user_id_procedure"; +import * as Resend_RemoveAdminIdentityProcedure from "./resend/remove_admin_identity_procedure"; +import * as Resend_ResendApiRequestProcedure from "./resend/resend_api_request_procedure"; +import * as Resend_SendEmailProcedure from "./resend/send_email_procedure"; +import * as Resend_SetResendConfigProcedure from "./resend/set_resend_config_procedure"; +import * as RateLimit_ConsumeProcedure from "./rateLimit/consume_procedure"; +import * as RateLimit_RunSweepProcedure from "./rateLimit/run_sweep_procedure"; + +/** Type-only namespace exports for generated type groups. */ + +/** The schema information for all tables in this module. This is defined the same was as the tables would have been defined in the server. */ +const tablesSchema = __schema({ + myDispatchDeliveryEvents: __table({ + name: 'my_dispatch_delivery_events', + indexes: [ + ], + constraints: [ + ], + }, MyDispatchDeliveryEventsRow), + myDispatchEmails: __table({ + name: 'my_dispatch_emails', + indexes: [ + ], + constraints: [ + ], + }, MyDispatchEmailsRow), + "rateLimit.rate_limit_config": __table({ + name: 'rateLimit.rate_limit_config', + indexes: [ + { accessor: 'singleton', name: 'rate_limit_config_singleton_idx_btree', algorithm: 'btree', columns: [ + 'singleton', + ] }, + ], + constraints: [ + { name: 'rate_limit_config_singleton_key', constraint: 'unique', columns: ['singleton'] }, + ], + }, RateLimit_RateLimitConfigRow), + "rateLimit.admin_rate_limit_buckets": __table({ + name: 'rateLimit.admin_rate_limit_buckets', + indexes: [ + ], + constraints: [ + ], + }, RateLimit_AdminRateLimitBucketsRow), +}); + +/** The schema information for all reducers in this module. This is defined the same way as the reducers would have been defined in the server, except the body of the reducer is omitted in code generation. */ +const reducersSchema = __reducers( + __reducerSchema("resend.ingest_resend_webhook", Resend_IngestResendWebhookReducer), + __reducerSchema("resend.replay_webhook_event", Resend_ReplayWebhookEventReducer), + __reducerSchema("rateLimit.add_rate_limit_admin", RateLimit_AddRateLimitAdminReducer), + __reducerSchema("rateLimit.reset_buckets", RateLimit_ResetBucketsReducer), + __reducerSchema("rateLimit.update_config", RateLimit_UpdateConfigReducer), +); + +/** The schema information for all procedures in this module. This is defined the same way as the procedures would have been defined in the server. */ +const proceduresSchema = __procedures( + __procedureSchema("clear_dispatches", ClearDispatchesProcedure.params, ClearDispatchesProcedure.returnType), + __procedureSchema("delete_dispatch", DeleteDispatchProcedure.params, DeleteDispatchProcedure.returnType), + __procedureSchema("send_dispatch", SendDispatchProcedure.params, SendDispatchProcedure.returnType), + __procedureSchema("set_dispatch_policy", SetDispatchPolicyProcedure.params, SetDispatchPolicyProcedure.returnType), + __procedureSchema("resend.add_admin_identity", Resend_AddAdminIdentityProcedure.params, Resend_AddAdminIdentityProcedure.returnType), + __procedureSchema("resend.cancel_email", Resend_CancelEmailProcedure.params, Resend_CancelEmailProcedure.returnType), + __procedureSchema("resend.get_email", Resend_GetEmailProcedure.params, Resend_GetEmailProcedure.returnType), + __procedureSchema("resend.get_resend_config_status", Resend_GetResendConfigStatusProcedure.params, Resend_GetResendConfigStatusProcedure.returnType), + __procedureSchema("resend.list_delivery_events_for_email", Resend_ListDeliveryEventsForEmailProcedure.params, Resend_ListDeliveryEventsForEmailProcedure.returnType), + __procedureSchema("resend.list_emails_by_org_id", Resend_ListEmailsByOrgIdProcedure.params, Resend_ListEmailsByOrgIdProcedure.returnType), + __procedureSchema("resend.list_emails_by_status", Resend_ListEmailsByStatusProcedure.params, Resend_ListEmailsByStatusProcedure.returnType), + __procedureSchema("resend.list_emails_by_user_id", Resend_ListEmailsByUserIdProcedure.params, Resend_ListEmailsByUserIdProcedure.returnType), + __procedureSchema("resend.remove_admin_identity", Resend_RemoveAdminIdentityProcedure.params, Resend_RemoveAdminIdentityProcedure.returnType), + __procedureSchema("resend.resend_api_request", Resend_ResendApiRequestProcedure.params, Resend_ResendApiRequestProcedure.returnType), + __procedureSchema("resend.send_email", Resend_SendEmailProcedure.params, Resend_SendEmailProcedure.returnType), + __procedureSchema("resend.set_resend_config", Resend_SetResendConfigProcedure.params, Resend_SetResendConfigProcedure.returnType), + __procedureSchema("rateLimit.consume", RateLimit_ConsumeProcedure.params, RateLimit_ConsumeProcedure.returnType), + __procedureSchema("rateLimit.run_sweep", RateLimit_RunSweepProcedure.params, RateLimit_RunSweepProcedure.returnType), +); + +/** The remote SpacetimeDB module schema, both runtime and type information. */ +const REMOTE_MODULE = { + versionInfo: { + cliVersion: "2.8.3" as const, + }, + tables: tablesSchema.schemaType.tables, + reducers: reducersSchema.reducersType.reducers, + ...proceduresSchema, +} satisfies __RemoteModule< + typeof tablesSchema.schemaType, + typeof reducersSchema.reducersType, + typeof proceduresSchema +>; + +/** The tables available in this remote SpacetimeDB module. Each table reference doubles as a query builder. */ +const __qb = __makeQueryBuilder(tablesSchema.schemaType); +export const tables = { + myDispatchDeliveryEvents: __qb.myDispatchDeliveryEvents, + myDispatchEmails: __qb.myDispatchEmails, + rateLimit: { + rateLimitConfig: __qb["rateLimit.rate_limit_config"], + adminRateLimitBuckets: __qb["rateLimit.admin_rate_limit_buckets"], + }, +} as const; + +/** The reducers available in this remote SpacetimeDB module. */ +const __reducerAccessors = __convertToAccessorMap(reducersSchema.reducersType.reducers); +export const reducers = { + rateLimit: { + addRateLimitAdmin: __reducerAccessors["rateLimit.addRateLimitAdmin"], + resetBuckets: __reducerAccessors["rateLimit.resetBuckets"], + updateConfig: __reducerAccessors["rateLimit.updateConfig"], + }, + resend: { + ingestResendWebhook: __reducerAccessors["resend.ingestResendWebhook"], + replayWebhookEvent: __reducerAccessors["resend.replayWebhookEvent"], + }, +} as const; + +/** The procedures available in this remote SpacetimeDB module. */ +const __procedureAccessors = __convertToAccessorMap(proceduresSchema.procedures); +export const procedures = { + clearDispatches: __procedureAccessors.clearDispatches, + deleteDispatch: __procedureAccessors.deleteDispatch, + sendDispatch: __procedureAccessors.sendDispatch, + setDispatchPolicy: __procedureAccessors.setDispatchPolicy, + rateLimit: { + consume: __procedureAccessors["rateLimit.consume"], + runSweep: __procedureAccessors["rateLimit.runSweep"], + }, + resend: { + addAdminIdentity: __procedureAccessors["resend.addAdminIdentity"], + cancelEmail: __procedureAccessors["resend.cancelEmail"], + getEmail: __procedureAccessors["resend.getEmail"], + getResendConfigStatus: __procedureAccessors["resend.getResendConfigStatus"], + listDeliveryEventsForEmail: __procedureAccessors["resend.listDeliveryEventsForEmail"], + listEmailsByOrgId: __procedureAccessors["resend.listEmailsByOrgId"], + listEmailsByStatus: __procedureAccessors["resend.listEmailsByStatus"], + listEmailsByUserId: __procedureAccessors["resend.listEmailsByUserId"], + removeAdminIdentity: __procedureAccessors["resend.removeAdminIdentity"], + resendApiRequest: __procedureAccessors["resend.resendApiRequest"], + sendEmail: __procedureAccessors["resend.sendEmail"], + setResendConfig: __procedureAccessors["resend.setResendConfig"], + }, +} as const; + +/** The context type returned in callbacks for all possible events. */ +export type EventContext = __EventContextInterface; +/** The context type returned in callbacks for reducer events. */ +export type ReducerEventContext = __ReducerEventContextInterface; +/** The context type returned in callbacks for subscription events. */ +export type SubscriptionEventContext = __SubscriptionEventContextInterface; +/** The context type returned in callbacks for error events. */ +export type ErrorContext = __ErrorContextInterface; +/** The subscription handle type to manage active subscriptions created from a {@link SubscriptionBuilder}. */ +export type SubscriptionHandle = __SubscriptionHandleImpl; + +/** Builder class to configure a new subscription to the remote SpacetimeDB instance. */ +export class SubscriptionBuilder extends __SubscriptionBuilderImpl {} + +/** Builder class to configure a new database connection to the remote SpacetimeDB instance. */ +export class DbConnectionBuilder extends __DbConnectionBuilder {} + +/** The typed database connection to manage connections to the remote SpacetimeDB instance. This class has type information specific to the generated module. */ +export class DbConnection extends __DbConnectionImpl { + /** Creates a new {@link DbConnectionBuilder} to configure and connect to the remote SpacetimeDB instance. */ + static builder = (): DbConnectionBuilder => { + return new DbConnectionBuilder(REMOTE_MODULE, (config: __DbConnectionConfig) => new DbConnection(config)); + }; + + /** Creates a new {@link SubscriptionBuilder} to configure a subscription to the remote SpacetimeDB instance. */ + override subscriptionBuilder = (): SubscriptionBuilder => { + return new SubscriptionBuilder(this); + }; +} + diff --git a/spacetime-resend-ts/example/src/module_bindings/my_dispatch_delivery_events_table.ts b/spacetime-resend-ts/example/src/module_bindings/my_dispatch_delivery_events_table.ts new file mode 100644 index 00000000000..bcd9654503f --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/my_dispatch_delivery_events_table.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default __t.row({ + eventId: __t.string().primaryKey().name("event_id"), + resendId: __t.string().name("resend_id"), + eventType: __t.string().name("event_type"), + createdAtIso: __t.string().name("created_at_iso"), + detailJson: __t.option(__t.string()).name("detail_json"), + insertedAt: __t.timestamp().name("inserted_at"), +}); diff --git a/spacetime-resend-ts/example/src/module_bindings/my_dispatch_emails_table.ts b/spacetime-resend-ts/example/src/module_bindings/my_dispatch_emails_table.ts new file mode 100644 index 00000000000..4e11a588405 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/my_dispatch_emails_table.ts @@ -0,0 +1,45 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; +import { + EmailStatus, +} from "./types"; + + +export default __t.row({ + resendId: __t.string().primaryKey().name("resend_id"), + fromAddress: __t.string().name("from_address"), + toAddressesJson: __t.string().name("to_addresses_json"), + subject: __t.option(__t.string()), + get status() { + return EmailStatus; + }, + lastError: __t.option(__t.string()).name("last_error"), + bouncedAt: __t.option(__t.timestamp()).name("bounced_at"), + bounceJson: __t.option(__t.string()).name("bounce_json"), + failedAt: __t.option(__t.timestamp()).name("failed_at"), + failureReason: __t.option(__t.string()).name("failure_reason"), + complained: __t.bool(), + complainedAt: __t.option(__t.timestamp()).name("complained_at"), + opened: __t.bool(), + openedAt: __t.option(__t.timestamp()).name("opened_at"), + clicked: __t.bool(), + clickedAt: __t.option(__t.timestamp()).name("clicked_at"), + deliveredAt: __t.option(__t.timestamp()).name("delivered_at"), + sentAt: __t.option(__t.timestamp()).name("sent_at"), + html: __t.option(__t.string()), + text: __t.option(__t.string()), + tagsJson: __t.option(__t.string()).name("tags_json"), + userId: __t.option(__t.string()).name("user_id"), + orgId: __t.option(__t.string()).name("org_id"), + createdAt: __t.timestamp().name("created_at"), + updatedAt: __t.timestamp().name("updated_at"), +}); diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/add_rate_limit_admin_reducer.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/add_rate_limit_admin_reducer.ts new file mode 100644 index 00000000000..e39846ca8d9 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/add_rate_limit_admin_reducer.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default { + identity: __t.identity(), +}; diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/admin_rate_limit_buckets_table.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/admin_rate_limit_buckets_table.ts new file mode 100644 index 00000000000..189f539a043 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/admin_rate_limit_buckets_table.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default __t.row({ + key: __t.string().primaryKey(), + scope: __t.string(), + windowStart: __t.timestamp().name("window_start"), + expiresAt: __t.timestamp().name("expires_at"), + count: __t.u32(), + updatedAt: __t.timestamp().name("updated_at"), +}); diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts new file mode 100644 index 00000000000..a98b8588aad --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts @@ -0,0 +1,24 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + RateLimitConsumeResult, +} from "./types"; + +export const params = { + scope: __t.string(), + actorKey: __t.string(), + limit: __t.u32(), + windowSeconds: __t.u32(), + cost: __t.option(__t.u32()), +}; +export const returnType = RateLimitConsumeResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/rate_limit_config_table.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/rate_limit_config_table.ts new file mode 100644 index 00000000000..66ffe86e399 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/rate_limit_config_table.ts @@ -0,0 +1,17 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default __t.row({ + singleton: __t.bool().primaryKey(), + sweepBatch: __t.u32().name("sweep_batch"), + updatedAt: __t.timestamp().name("updated_at"), +}); diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/reset_buckets_reducer.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/reset_buckets_reducer.ts new file mode 100644 index 00000000000..a7c5cc5274f --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/reset_buckets_reducer.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default { + maxRows: __t.option(__t.u32()), +}; diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/run_sweep_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/run_sweep_procedure.ts new file mode 100644 index 00000000000..9815c99eb38 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/run_sweep_procedure.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + maxRows: __t.option(__t.u32()), +}; +export const returnType = __t.u32() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts new file mode 100644 index 00000000000..151a90e827f --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts @@ -0,0 +1,56 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const AdminRateLimitBuckets = __t.object("AdminRateLimitBuckets", {}); +export type AdminRateLimitBuckets = __Infer; + +export const RateLimitAdminIdentity = __t.object("RateLimitAdminIdentity", { + identity: __t.identity(), + addedAtMicros: __t.i64(), +}); +export type RateLimitAdminIdentity = __Infer; + +export const RateLimitBucket = __t.object("RateLimitBucket", { + key: __t.string(), + scope: __t.string(), + windowStart: __t.timestamp(), + expiresAt: __t.timestamp(), + count: __t.u32(), + updatedAt: __t.timestamp(), +}); +export type RateLimitBucket = __Infer; + +export const RateLimitConfig = __t.object("RateLimitConfig", { + singleton: __t.bool(), + sweepBatch: __t.u32(), + updatedAt: __t.timestamp(), +}); +export type RateLimitConfig = __Infer; + +export const RateLimitConsumeResult = __t.object("RateLimitConsumeResult", { + allowed: __t.bool(), + scope: __t.string(), + key: __t.string(), + limit: __t.u32(), + used: __t.u32(), + remaining: __t.u32(), + retryAfterSeconds: __t.u32(), + resetAt: __t.timestamp(), +}); +export type RateLimitConsumeResult = __Infer; + +export const RateLimitSweepTick = __t.object("RateLimitSweepTick", { + scheduledId: __t.u64(), + scheduledAt: __t.scheduleAt(), +}); +export type RateLimitSweepTick = __Infer; + diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/update_config_reducer.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/update_config_reducer.ts new file mode 100644 index 00000000000..54fcf361af1 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/update_config_reducer.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default { + sweepBatch: __t.u32(), +}; diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/add_admin_identity_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/add_admin_identity_procedure.ts new file mode 100644 index 00000000000..bfd93108ec4 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/add_admin_identity_procedure.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + identity: __t.identity(), +}; +export const returnType = __t.unit() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/cancel_email_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/cancel_email_procedure.ts new file mode 100644 index 00000000000..ec778b46cd0 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/cancel_email_procedure.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + resendId: __t.string(), +}; +export const returnType = __t.unit() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/get_email_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/get_email_procedure.ts new file mode 100644 index 00000000000..0744ed77aab --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/get_email_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendEmail, +} from "./types"; + +export const params = { + resendId: __t.string(), +}; +export const returnType = __t.option(ResendEmail) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/get_resend_config_status_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/get_resend_config_status_procedure.ts new file mode 100644 index 00000000000..badd9ab9cc8 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/get_resend_config_status_procedure.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendConfigStatus, +} from "./types"; + +export const params = { +}; +export const returnType = ResendConfigStatus \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/ingest_resend_webhook_reducer.ts b/spacetime-resend-ts/example/src/module_bindings/resend/ingest_resend_webhook_reducer.ts new file mode 100644 index 00000000000..1e7afcc3703 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/ingest_resend_webhook_reducer.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default { + eventId: __t.string(), + eventType: __t.string(), + payloadJson: __t.string(), + signatureHeader: __t.option(__t.string()), + timestampHeader: __t.option(__t.string()), +}; diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/list_delivery_events_for_email_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/list_delivery_events_for_email_procedure.ts new file mode 100644 index 00000000000..27a3a808053 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/list_delivery_events_for_email_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendDeliveryEvent, +} from "./types"; + +export const params = { + resendId: __t.string(), +}; +export const returnType = __t.array(ResendDeliveryEvent) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_org_id_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_org_id_procedure.ts new file mode 100644 index 00000000000..cd1063291da --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_org_id_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendEmail, +} from "./types"; + +export const params = { + orgId: __t.string(), +}; +export const returnType = __t.array(ResendEmail) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_status_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_status_procedure.ts new file mode 100644 index 00000000000..c24fc71c444 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_status_procedure.ts @@ -0,0 +1,23 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendEmail, + EmailStatus, +} from "./types"; + +export const params = { + get status() { + return EmailStatus; + }, +}; +export const returnType = __t.array(ResendEmail) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_user_id_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_user_id_procedure.ts new file mode 100644 index 00000000000..f712b807b32 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/list_emails_by_user_id_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendEmail, +} from "./types"; + +export const params = { + userId: __t.string(), +}; +export const returnType = __t.array(ResendEmail) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/remove_admin_identity_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/remove_admin_identity_procedure.ts new file mode 100644 index 00000000000..bfd93108ec4 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/remove_admin_identity_procedure.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + identity: __t.identity(), +}; +export const returnType = __t.unit() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/replay_webhook_event_reducer.ts b/spacetime-resend-ts/example/src/module_bindings/resend/replay_webhook_event_reducer.ts new file mode 100644 index 00000000000..590a453de82 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/replay_webhook_event_reducer.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export default { + eventId: __t.string(), +}; diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/resend_api_request_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/resend_api_request_procedure.ts new file mode 100644 index 00000000000..aff60351ebf --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/resend_api_request_procedure.ts @@ -0,0 +1,23 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendApiRequestResult, +} from "./types"; + +export const params = { + method: __t.string(), + path: __t.string(), + jsonBody: __t.option(__t.string()), + idempotencyKey: __t.option(__t.string()), +}; +export const returnType = ResendApiRequestResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/send_email_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/send_email_procedure.ts new file mode 100644 index 00000000000..67babe6715f --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/send_email_procedure.ts @@ -0,0 +1,31 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + SendEmailResult, +} from "./types"; + +export const params = { + from: __t.option(__t.string()), + to: __t.array(__t.string()), + subject: __t.string(), + html: __t.option(__t.string()), + text: __t.option(__t.string()), + cc: __t.option(__t.array(__t.string())), + bcc: __t.option(__t.array(__t.string())), + replyTo: __t.option(__t.array(__t.string())), + tagsJson: __t.option(__t.string()), + headersJson: __t.option(__t.string()), + scheduledAt: __t.option(__t.string()), + idempotencyKey: __t.option(__t.string()), +}; +export const returnType = SendEmailResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/set_resend_config_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/set_resend_config_procedure.ts new file mode 100644 index 00000000000..645c952bd4d --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/set_resend_config_procedure.ts @@ -0,0 +1,18 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + apiKey: __t.string(), + webhookSigningSecret: __t.option(__t.string()), + defaultFrom: __t.option(__t.string()), +}; +export const returnType = __t.unit() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/types.ts b/spacetime-resend-ts/example/src/module_bindings/resend/types.ts new file mode 100644 index 00000000000..206a44b1f74 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/types.ts @@ -0,0 +1,123 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +// The tagged union or sum type for the algebraic type `EmailStatus`. +export const EmailStatus = __t.enum("EmailStatus", { + Queued: __t.unit(), + Sent: __t.unit(), + Delivered: __t.unit(), + DeliveryDelayed: __t.unit(), + Bounced: __t.unit(), + Failed: __t.unit(), + Cancelled: __t.unit(), +}); +export type EmailStatus = __Infer; + +export const ResendAdminIdentity = __t.object("ResendAdminIdentity", { + identity: __t.identity(), + addedAtMicros: __t.i64(), +}); +export type ResendAdminIdentity = __Infer; + +export const ResendApiRequestResult = __t.object("ResendApiRequestResult", { + status: __t.u16(), + body: __t.string(), +}); +export type ResendApiRequestResult = __Infer; + +export const ResendConfig = __t.object("ResendConfig", { + singleton: __t.bool(), + apiKey: __t.string(), + webhookSigningSecret: __t.option(__t.string()), + defaultFrom: __t.option(__t.string()), + updatedAt: __t.timestamp(), +}); +export type ResendConfig = __Infer; + +export const ResendConfigStatus = __t.object("ResendConfigStatus", { + isConfigured: __t.bool(), + hasWebhookSecret: __t.bool(), + defaultFrom: __t.option(__t.string()), + apiKeyLength: __t.u16(), +}); +export type ResendConfigStatus = __Infer; + +export const ResendDeliveryEvent = __t.object("ResendDeliveryEvent", { + eventId: __t.string(), + resendId: __t.string(), + eventType: __t.string(), + createdAtIso: __t.string(), + detailJson: __t.option(__t.string()), + insertedAt: __t.timestamp(), +}); +export type ResendDeliveryEvent = __Infer; + +export const ResendEmail = __t.object("ResendEmail", { + resendId: __t.string(), + fromAddress: __t.string(), + toAddressesJson: __t.string(), + subject: __t.option(__t.string()), + get status() { + return EmailStatus; + }, + lastError: __t.option(__t.string()), + bouncedAt: __t.option(__t.timestamp()), + bounceJson: __t.option(__t.string()), + failedAt: __t.option(__t.timestamp()), + failureReason: __t.option(__t.string()), + complained: __t.bool(), + complainedAt: __t.option(__t.timestamp()), + opened: __t.bool(), + openedAt: __t.option(__t.timestamp()), + clicked: __t.bool(), + clickedAt: __t.option(__t.timestamp()), + deliveredAt: __t.option(__t.timestamp()), + sentAt: __t.option(__t.timestamp()), + html: __t.option(__t.string()), + text: __t.option(__t.string()), + tagsJson: __t.option(__t.string()), + userId: __t.option(__t.string()), + orgId: __t.option(__t.string()), + createdAt: __t.timestamp(), + updatedAt: __t.timestamp(), +}); +export type ResendEmail = __Infer; + +export const ResendWebhookEvent = __t.object("ResendWebhookEvent", { + eventId: __t.string(), + eventType: __t.string(), + payloadJson: __t.string(), + signatureHeader: __t.option(__t.string()), + timestampHeader: __t.option(__t.string()), + get status() { + return WebhookEventStatus; + }, + errorMessage: __t.option(__t.string()), + receivedAt: __t.timestamp(), + processedAt: __t.option(__t.timestamp()), +}); +export type ResendWebhookEvent = __Infer; + +export const SendEmailResult = __t.object("SendEmailResult", { + resendId: __t.string(), +}); +export type SendEmailResult = __Infer; + +// The tagged union or sum type for the algebraic type `WebhookEventStatus`. +export const WebhookEventStatus = __t.enum("WebhookEventStatus", { + Received: __t.unit(), + Processed: __t.unit(), + Ignored: __t.unit(), + Failed: __t.unit(), +}); +export type WebhookEventStatus = __Infer; + diff --git a/spacetime-resend-ts/example/src/module_bindings/send_dispatch_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/send_dispatch_procedure.ts new file mode 100644 index 00000000000..5a200f044d9 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/send_dispatch_procedure.ts @@ -0,0 +1,22 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + DispatchSendResult, +} from "./types"; + +export const params = { + to: __t.string(), + subject: __t.string(), + message: __t.string(), +}; +export const returnType = DispatchSendResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/set_dispatch_policy_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/set_dispatch_policy_procedure.ts new file mode 100644 index 00000000000..d27751fdf5d --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/set_dispatch_policy_procedure.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const params = { + allowedRecipientsJson: __t.string(), +}; +export const returnType = __t.bool() \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/types.ts b/spacetime-resend-ts/example/src/module_bindings/types.ts new file mode 100644 index 00000000000..5275af6375d --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/types.ts @@ -0,0 +1,91 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +export const DispatchDeleteResult = __t.object("DispatchDeleteResult", { + ok: __t.bool(), + removed: __t.u32(), +}); +export type DispatchDeleteResult = __Infer; + +export const DispatchPolicy = __t.object("DispatchPolicy", { + singleton: __t.bool(), + allowedRecipientsJson: __t.string(), + updatedAt: __t.timestamp(), +}); +export type DispatchPolicy = __Infer; + +export const DispatchSendResult = __t.object("DispatchSendResult", { + ok: __t.bool(), + resendId: __t.option(__t.string()), + message: __t.string(), +}); +export type DispatchSendResult = __Infer; + +// The tagged union or sum type for the algebraic type `EmailStatus`. +export const EmailStatus = __t.enum("EmailStatus", { + Queued: __t.unit(), + Sent: __t.unit(), + Delivered: __t.unit(), + DeliveryDelayed: __t.unit(), + Bounced: __t.unit(), + Failed: __t.unit(), + Cancelled: __t.unit(), +}); +export type EmailStatus = __Infer; + +export const MyDispatchDeliveryEvents = __t.object("MyDispatchDeliveryEvents", {}); +export type MyDispatchDeliveryEvents = __Infer; + +export const MyDispatchEmails = __t.object("MyDispatchEmails", {}); +export type MyDispatchEmails = __Infer; + +export const ResendDeliveryEvent = __t.object("ResendDeliveryEvent", { + eventId: __t.string(), + resendId: __t.string(), + eventType: __t.string(), + createdAtIso: __t.string(), + detailJson: __t.option(__t.string()), + insertedAt: __t.timestamp(), +}); +export type ResendDeliveryEvent = __Infer; + +export const ResendEmail = __t.object("ResendEmail", { + resendId: __t.string(), + fromAddress: __t.string(), + toAddressesJson: __t.string(), + subject: __t.option(__t.string()), + get status() { + return EmailStatus; + }, + lastError: __t.option(__t.string()), + bouncedAt: __t.option(__t.timestamp()), + bounceJson: __t.option(__t.string()), + failedAt: __t.option(__t.timestamp()), + failureReason: __t.option(__t.string()), + complained: __t.bool(), + complainedAt: __t.option(__t.timestamp()), + opened: __t.bool(), + openedAt: __t.option(__t.timestamp()), + clicked: __t.bool(), + clickedAt: __t.option(__t.timestamp()), + deliveredAt: __t.option(__t.timestamp()), + sentAt: __t.option(__t.timestamp()), + html: __t.option(__t.string()), + text: __t.option(__t.string()), + tagsJson: __t.option(__t.string()), + userId: __t.option(__t.string()), + orgId: __t.option(__t.string()), + createdAt: __t.timestamp(), + updatedAt: __t.timestamp(), +}); +export type ResendEmail = __Infer; + diff --git a/spacetime-resend-ts/example/src/module_bindings/types/procedures.ts b/spacetime-resend-ts/example/src/module_bindings/types/procedures.ts new file mode 100644 index 00000000000..13622fae9a4 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/types/procedures.ts @@ -0,0 +1,22 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { type Infer as __Infer } from "spacetimedb"; + +// Import all procedure arg schemas +import * as ClearDispatchesProcedure from "../clear_dispatches_procedure"; +import * as DeleteDispatchProcedure from "../delete_dispatch_procedure"; +import * as SendDispatchProcedure from "../send_dispatch_procedure"; +import * as SetDispatchPolicyProcedure from "../set_dispatch_policy_procedure"; + +export type ClearDispatchesArgs = __Infer; +export type ClearDispatchesResult = __Infer; +export type DeleteDispatchArgs = __Infer; +export type DeleteDispatchResult = __Infer; +export type SendDispatchArgs = __Infer; +export type SendDispatchResult = __Infer; +export type SetDispatchPolicyArgs = __Infer; +export type SetDispatchPolicyResult = __Infer; + diff --git a/spacetime-resend-ts/example/src/module_bindings/types/reducers.ts b/spacetime-resend-ts/example/src/module_bindings/types/reducers.ts new file mode 100644 index 00000000000..c701027cbf1 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/types/reducers.ts @@ -0,0 +1,10 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { type Infer as __Infer } from "spacetimedb"; + +// Import all reducer arg schemas + + diff --git a/spacetime-resend-ts/example/tsconfig.json b/spacetime-resend-ts/example/tsconfig.json new file mode 100644 index 00000000000..8e5e2bbddf0 --- /dev/null +++ b/spacetime-resend-ts/example/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "moduleResolution": "Bundler", + "strict": true, + "noEmit": true, + "skipLibCheck": true, + "allowImportingTsExtensions": false, + "esModuleInterop": true, + "isolatedModules": true, + "useDefineForClassFields": true, + "lib": ["ES2022", "DOM", "DOM.Iterable"] + }, + "include": ["src/**/*.ts", "scripts/**/*.ts", "server.ts"], + "exclude": ["node_modules", "public", "dist"] +} diff --git a/spacetime-resend-ts/package.json b/spacetime-resend-ts/package.json new file mode 100644 index 00000000000..d28e548afad --- /dev/null +++ b/spacetime-resend-ts/package.json @@ -0,0 +1,71 @@ +{ + "name": "@spacetimedb/resend", + "description": "Resend email delivery, webhook verification, and event storage for SpacetimeDB TypeScript modules.", + "version": "0.1.0", + "license": "BUSL-1.1", + "type": "module", + "main": "./src/index.ts", + "types": "./src/index.ts", + "exports": { + ".": { + "types": "./src/index.ts", + "default": "./src/index.ts" + }, + "./submodule": { + "types": "./src/submodule.ts", + "default": "./src/submodule.ts" + } + }, + "files": [ + "src", + "LICENSE.txt", + "README.md" + ], + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/clockworklabs/SpacetimeDB.git", + "directory": "spacetime-resend-ts" + }, + "homepage": "https://github.com/clockworklabs/SpacetimeDB/tree/master/spacetime-resend-ts#readme", + "bugs": { + "url": "https://github.com/clockworklabs/SpacetimeDB/issues" + }, + "keywords": [ + "spacetimedb", + "resend", + "email", + "webhooks" + ], + "scripts": { + "build": "spacetime build", + "format": "prettier . --write --ignore-path ../.prettierignore", + "lint": "eslint . && prettier . --check --ignore-path ../.prettierignore", + "typecheck": "tsc --noEmit", + "test": "tsx scripts/test-unit.ts", + "spacetime:generate": "spacetime generate --lang typescript --out-dir ts-codegen", + "publish:module": "spacetime publish", + "publish:local": "spacetime publish --server local --yes spacetime-resend", + "publish:local:reset": "spacetime publish --server local --yes --delete-data=always spacetime-resend", + "test:smoke": "tsx scripts/test-resend-smoke.ts", + "test:gate:local": "tsx scripts/test-resend-smoke.ts" + }, + "dependencies": { + "@spacetimedb/crypto": "workspace:^", + "valibot": "^1.4.2" + }, + "peerDependencies": { + "spacetimedb": "workspace:^" + }, + "devDependencies": { + "eslint": "^9.17.0", + "prettier": "^3.3.3", + "@types/node": "^22.10.2", + "spacetimedb": "workspace:*", + "tsx": "^4.21.0", + "typescript": "^5.9.3" + } +} diff --git a/spacetime-resend-ts/scripts/test-resend-smoke.ts b/spacetime-resend-ts/scripts/test-resend-smoke.ts new file mode 100644 index 00000000000..8bd9ddeca06 --- /dev/null +++ b/spacetime-resend-ts/scripts/test-resend-smoke.ts @@ -0,0 +1,466 @@ +// Smoke test: build, publish, ingest synthetic events, verify state. No API key or real webhooks needed. Usage: pnpm run test:resend:smoke [-- --skip-build-publish] + +import { spawn } from 'node:child_process'; +import { createHmac } from 'node:crypto'; + +type Options = { + server: string; + database: string; + skipBuildPublish: boolean; +}; + +function parseArgs(argv: string[]): Options { + const opts: Options = { + server: 'http://127.0.0.1:3000', + // Dedicated DB so smoke test never overwrites the dev module's real config. + database: 'resend-ts-smoke-test', + skipBuildPublish: false, + }; + for (let i = 0; i < argv.length; i++) { + const raw = argv[i]!; + const flag = raw.replace(/^-+/, '').toLowerCase(); + if (flag === 'skip-build-publish') opts.skipBuildPublish = true; + if (flag === 'server') opts.server = argv[++i]!; + if (flag === 'database') opts.database = argv[++i]!; + } + return opts; +} + +function step(name: string) { + process.stdout.write(`\n==> ${name}\n`); +} + +function run( + cmd: string, + args: string[] +): Promise<{ code: number; stdout: string; stderr: string }> { + return new Promise(resolve => { + const child = spawn(cmd, args, { shell: false }); + let stdout = ''; + let stderr = ''; + child.stdout?.on('data', d => (stdout += String(d))); + child.stderr?.on('data', d => (stderr += String(d))); + child.on('close', code => resolve({ code: code ?? 1, stdout, stderr })); + child.on('error', err => + resolve({ code: 1, stdout, stderr: stderr + String(err) }) + ); + }); +} + +async function callReducer( + opts: Options, + name: string, + args: string[] +): Promise { + const result = await run('spacetime', [ + 'call', + '--server', + opts.server, + opts.database, + name, + ...args, + ]); + if (result.code !== 0) { + throw new Error( + `spacetime call ${name} failed: code=${result.code}\nstderr: ${result.stderr}\nstdout: ${result.stdout}` + ); + } + return result.stdout; +} + +// Expects the call to fail. Returns combined stderr/stdout for assertion. +async function expectCallFails( + opts: Options, + name: string, + args: string[], + anonymous = false +): Promise { + const result = await run('spacetime', [ + 'call', + ...(anonymous ? ['--anonymous'] : []), + '--server', + opts.server, + opts.database, + name, + ...args, + ]); + if (result.code === 0) { + throw new Error( + `expected ${name} to fail but it succeeded:\nstdout: ${result.stdout}` + ); + } + return result.stderr + result.stdout; +} + +function quote(s: string): string { + return JSON.stringify(s); +} + +function some(s: string): string { + return JSON.stringify({ some: s }); +} + +const RESEND_WEBHOOK_SECRET_RAW = 'resend_smoke_test_secret'; +const RESEND_WEBHOOK_SECRET = `whsec_${Buffer.from(RESEND_WEBHOOK_SECRET_RAW).toString('base64')}`; + +function svixSignature(args: { + eventId: string; + timestamp: string; + payloadJson: string; +}): string { + const digest = createHmac('sha256', RESEND_WEBHOOK_SECRET_RAW) + .update(`${args.eventId}.${args.timestamp}.${args.payloadJson}`) + .digest('base64'); + return `v1,${digest}`; +} + +async function ingestWebhook( + opts: Options, + eventId: string, + eventType: string, + payloadJson: string +) { + const timestamp = String(Math.floor(Date.now() / 1000)); + await callReducer(opts, 'ingest_resend_webhook', [ + quote(eventId), + quote(eventType), + quote(payloadJson), + some(svixSignature({ eventId, timestamp, payloadJson })), + some(timestamp), + ]); +} + +function eventPayload(args: { + type: string; + emailId: string; + from?: string; + to?: string[]; + subject?: string; + extra?: Record; +}): string { + const data: Record = { + email_id: args.emailId, + created_at: '2026-05-04T00:00:00Z', + from: args.from ?? 'onboarding@resend.dev', + to: args.to ?? ['delivered@resend.dev'], + subject: args.subject ?? 'smoke test', + ...(args.extra ?? {}), + }; + return JSON.stringify({ + type: args.type, + created_at: '2026-05-04T00:00:00Z', + data, + }); +} + +const EMAIL_STATUS = [ + 'Queued', + 'Sent', + 'Delivered', + 'DeliveryDelayed', + 'Bounced', + 'Failed', + 'Cancelled', +] as const; + +function emailStatus(rowText: string): string { + const parsed = JSON.parse(rowText); + const row = parsed?.[1]; + const variant = row?.[4]; + const tag = variant?.[0]; + if (typeof tag !== 'number' || tag < 0 || tag >= EMAIL_STATUS.length) { + throw new Error(`could not parse email status from row: ${rowText}`); + } + return EMAIL_STATUS[tag]!; +} + +async function main() { + const opts = parseArgs(process.argv.slice(2)); + // Unique run id so re-runs don't collide on idempotent webhook IDs. + const runId = Date.now().toString(36); + const evt = (suffix: string) => `evt_${runId}_${suffix}`; + const em = (suffix: string) => `em_${runId}_${suffix}`; + + if (!opts.skipBuildPublish) { + step('spacetime build'); + const build = await run('spacetime', ['build']); + if (build.code !== 0) { + process.stderr.write(build.stderr); + throw new Error('build failed'); + } + + step(`spacetime publish --server ${opts.server} ${opts.database}`); + const publish = await run('spacetime', [ + 'publish', + '--server', + opts.server, + '--yes', + '--delete-data', + opts.database, + ]); + if (publish.code !== 0) { + process.stderr.write(publish.stderr); + throw new Error('publish failed'); + } + } + + // Negative path: send_email refuses cleanly when config is not set. + step('negative: send_email before config, expect failure'); + const preBootstrap = await expectCallFails(opts, 'send_email', [ + 'null', + JSON.stringify(['delivered@resend.dev']), + quote('hello'), + some('

hello

'), + 'null', + 'null', + 'null', + 'null', + 'null', + 'null', + 'null', + 'null', + ]); + if (!preBootstrap.toLowerCase().includes('config')) { + throw new Error( + `expected error to mention config; got: ${preBootstrap.slice(0, 400)}` + ); + } + + step('set_resend_config'); + await callReducer(opts, 'set_resend_config', [ + quote('re_smoke_placeholder'), + some(RESEND_WEBHOOK_SECRET), + some('onboarding@resend.dev'), + ]); + + step('negative: anonymous callers cannot query email state'); + const unauthorized = await expectCallFails( + opts, + 'get_email', + [quote(em('a'))], + true + ); + if (!unauthorized.toLowerCase().includes('not_authorized')) { + throw new Error( + `expected get_email to reject a non-admin caller: ${unauthorized.slice(0, 400)}` + ); + } + + step('negative: signed webhook type must match supplied event type'); + const mismatchEventId = evt('metadata_mismatch'); + const mismatchPayload = eventPayload({ + type: 'email.delivered', + emailId: em('metadata_mismatch'), + }); + const mismatchTimestamp = String(Math.floor(Date.now() / 1000)); + const mismatch = await expectCallFails(opts, 'ingest_resend_webhook', [ + quote(mismatchEventId), + quote('email.bounced'), + quote(mismatchPayload), + some( + svixSignature({ + eventId: mismatchEventId, + timestamp: mismatchTimestamp, + payloadJson: mismatchPayload, + }) + ), + some(mismatchTimestamp), + ]); + if (!mismatch.toLowerCase().includes('metadata')) { + throw new Error( + `expected signed metadata mismatch failure: ${mismatch.slice(0, 400)}` + ); + } + + // Email A happy path: queued -> sent -> delivered, then flag overlays must not roll status back. + step(`ingest email.sent for ${em('a')}`); + await ingestWebhook( + opts, + evt('a'), + 'email.sent', + eventPayload({ type: 'email.sent', emailId: em('a') }) + ); + + step(`ingest email.delivered for ${em('a')}`); + await ingestWebhook( + opts, + evt('b'), + 'email.delivered', + eventPayload({ type: 'email.delivered', emailId: em('a') }) + ); + + step(`verify status=delivered for ${em('a')}`); + let row = await callReducer(opts, 'get_email', [quote(em('a'))]); + if (emailStatus(row) !== 'Delivered') { + throw new Error(`expected delivered for ${em('a')}, got: ${row}`); + } + + step(`ingest email.complained for ${em('a')} (flag, must NOT change status)`); + await ingestWebhook( + opts, + evt('e'), + 'email.complained', + eventPayload({ type: 'email.complained', emailId: em('a') }) + ); + row = await callReducer(opts, 'get_email', [quote(em('a'))]); + if (emailStatus(row) !== 'Delivered') { + throw new Error(`complained should not flip status: ${row}`); + } + + step(`ingest email.opened for ${em('a')} (flag)`); + await ingestWebhook( + opts, + evt('g'), + 'email.opened', + eventPayload({ type: 'email.opened', emailId: em('a') }) + ); + row = await callReducer(opts, 'get_email', [quote(em('a'))]); + if (emailStatus(row) !== 'Delivered') { + throw new Error(`opened should not flip status: ${row}`); + } + + step(`ingest email.clicked for ${em('a')} (flag + detail captured)`); + await ingestWebhook( + opts, + evt('h'), + 'email.clicked', + eventPayload({ + type: 'email.clicked', + emailId: em('a'), + extra: { + click: { + ipAddress: '203.0.113.42', + link: 'https://spacetimedb.com', + timestamp: '2026-05-04T00:00:00Z', + userAgent: 'Mozilla/5.0 (smoke-test)', + }, + }, + }) + ); + const clickEvents = await callReducer( + opts, + 'list_delivery_events_for_email', + [quote(em('a'))] + ); + const clickRows: unknown = JSON.parse(clickEvents); + const clickRow = Array.isArray(clickRows) + ? clickRows.find(row => Array.isArray(row) && row[2] === 'email.clicked') + : undefined; + if (!clickRow) { + throw new Error(`expected click event in delivery log: ${clickEvents}`); + } + const detailOption = Array.isArray(clickRow) ? clickRow[4] : undefined; + const detailJson = + Array.isArray(detailOption) && + detailOption[0] === 0 && + typeof detailOption[1] === 'string' + ? detailOption[1] + : undefined; + const clickDetail = detailJson ? JSON.parse(detailJson) : undefined; + if (clickDetail?.link !== 'https://spacetimedb.com') { + throw new Error(`expected click detail (link) preserved: ${clickEvents}`); + } + + // Email C bounce path with structured detail. + step(`ingest email.bounced for ${em('c')}`); + await ingestWebhook( + opts, + evt('c'), + 'email.bounced', + eventPayload({ + type: 'email.bounced', + emailId: em('c'), + to: ['bounced@resend.dev'], + extra: { + bounce: { + message: 'Mailbox does not exist', + subType: 'NoEmail', + type: 'Permanent', + }, + }, + }) + ); + const bouncedRow = await callReducer(opts, 'get_email', [quote(em('c'))]); + if (emailStatus(bouncedRow) !== 'Bounced') { + throw new Error(`expected bounced for ${em('c')}: ${bouncedRow}`); + } + if (!bouncedRow.includes('Mailbox does not exist')) { + throw new Error(`expected bounce reason in row: ${bouncedRow}`); + } + + // Email D delivery_delayed status path. + step(`ingest email.delivery_delayed for ${em('d')}`); + await ingestWebhook( + opts, + evt('d'), + 'email.delivery_delayed', + eventPayload({ type: 'email.delivery_delayed', emailId: em('d') }) + ); + const delayed = await callReducer(opts, 'get_email', [quote(em('d'))]); + if (emailStatus(delayed) !== 'DeliveryDelayed') { + throw new Error(`expected delivery_delayed for ${em('d')}: ${delayed}`); + } + + // Email F failed status with reason. + step(`ingest email.failed for ${em('f')}`); + await ingestWebhook( + opts, + evt('f'), + 'email.failed', + eventPayload({ + type: 'email.failed', + emailId: em('f'), + extra: { failed: { reason: 'rate limited by destination MTA' } }, + }) + ); + const failedRow = await callReducer(opts, 'get_email', [quote(em('f'))]); + if (emailStatus(failedRow) !== 'Failed') { + throw new Error(`expected failed for ${em('f')}: ${failedRow}`); + } + if (!failedRow.includes('rate limited')) { + throw new Error(`expected failure reason in row: ${failedRow}`); + } + + // Idempotency + replay. + step( + `verify idempotency: re-ingest ${evt('a')} (already processed, should no-op)` + ); + await ingestWebhook( + opts, + evt('a'), + 'email.sent', + eventPayload({ type: 'email.sent', emailId: em('a') }) + ); + row = await callReducer(opts, 'get_email', [quote(em('a'))]); + if (emailStatus(row) !== 'Delivered') { + throw new Error(`replay broke status: ${row}`); + } + + step(`replay_webhook_event re-applies ${evt('b')} (status stays delivered)`); + await callReducer(opts, 'replay_webhook_event', [quote(evt('b'))]); + row = await callReducer(opts, 'get_email', [quote(em('a'))]); + if (emailStatus(row) !== 'Delivered') { + throw new Error(`replay reducer altered state: ${row}`); + } + + step('negative: replay unknown event_id, expect failure'); + const replayMissing = await expectCallFails(opts, 'replay_webhook_event', [ + quote('evt_does_not_exist_xyz'), + ]); + if (!replayMissing.toLowerCase().includes('not_found')) { + throw new Error( + `expected not_found error; got: ${replayMissing.slice(0, 400)}` + ); + } + + step( + 'done: smoke test passed (8 event types + idempotency + replay + 2 negative)' + ); +} + +main().catch(err => { + process.stderr.write( + `\nSMOKE TEST FAILED: ${err instanceof Error ? err.message : String(err)}\n` + ); + process.exit(1); +}); diff --git a/spacetime-resend-ts/scripts/test-unit.ts b/spacetime-resend-ts/scripts/test-unit.ts new file mode 100644 index 00000000000..c4cffb97510 --- /dev/null +++ b/spacetime-resend-ts/scripts/test-unit.ts @@ -0,0 +1,98 @@ +import * as assert from 'node:assert/strict'; +import { buildResendHttpRequest } from '../src/submodule/request.ts'; +import { validateEmailInput } from '../src/submodule/email-input.ts'; +import { parseResendEventType } from '../src/submodule/webhook-metadata.ts'; + +assert.equal( + parseResendEventType('{"type":"email.delivered","data":{}}'), + 'email.delivered' +); +assert.equal(parseResendEventType('{"type":"","data":{}}'), undefined); +assert.equal(parseResendEventType('{"data":{}}'), undefined); +assert.equal(parseResendEventType('{bad json'), undefined); +assert.equal(parseResendEventType('[]'), undefined); + +const request = buildResendHttpRequest({ + method: 'post', + path: '/emails', + apiKey: 're_test_secret', + jsonBody: '{"to":["user@example.com"]}', + idempotencyKey: 'email-user-1', +}); +assert.equal(request.url, 'https://api.resend.com/emails'); +assert.equal(request.method, 'POST'); +assert.equal(request.headers.Authorization, 'Bearer re_test_secret'); +assert.throws( + () => + buildResendHttpRequest({ + method: 'GET', + path: 'https://attacker.example/collect', + apiKey: 're_test_secret', + jsonBody: undefined, + idempotencyKey: undefined, + }), + /resend\.request_path_invalid/ +); +assert.throws( + () => + buildResendHttpRequest({ + method: 'GET', + path: '/emails\u007fblocked', + apiKey: 're_test_secret', + jsonBody: undefined, + idempotencyKey: undefined, + }), + /resend\.request_path_invalid/ +); +assert.throws( + () => + buildResendHttpRequest({ + method: 'TRACE', + path: '/emails', + apiKey: 're_test_secret', + jsonBody: undefined, + idempotencyKey: undefined, + }), + /resend\.request_method_invalid/ +); + +assert.doesNotThrow(() => + validateEmailInput({ + to: ['user@example.com'], + subject: 'Welcome', + text: 'Hello', + }) +); +assert.throws( + () => validateEmailInput({ to: [], subject: 'Welcome', text: 'Hello' }), + /resend\.send_email_no_recipients/ +); +assert.throws( + () => + validateEmailInput({ + to: ['user@example.com\u007fblocked'], + subject: 'Welcome', + text: 'Hello', + }), + /resend\.to_invalid_address/ +); +assert.throws( + () => + validateEmailInput({ + to: ['user@example.com'], + subject: 'Welcome\r\nx-injected: yes', + text: 'Hello', + }), + /resend\.send_email_invalid_subject/ +); +assert.throws( + () => + validateEmailInput({ + to: Array.from({ length: 101 }, (_, index) => `user${index}@example.com`), + subject: 'Welcome', + text: 'Hello', + }), + /resend\.to_too_many/ +); + +console.log('resend unit tests passed'); diff --git a/spacetime-resend-ts/src/index.ts b/spacetime-resend-ts/src/index.ts new file mode 100644 index 00000000000..d940a097db9 --- /dev/null +++ b/spacetime-resend-ts/src/index.ts @@ -0,0 +1,21 @@ +export { default, init } from './submodule/schema'; +export { + ingest_resend_webhook, + replay_webhook_event, +} from './submodule/webhooks'; + +export { + set_resend_config, + get_resend_config_status, +} from './submodule/config'; +export { add_admin_identity, remove_admin_identity } from './submodule/auth'; +export { + cancel_email, + get_email, + list_delivery_events_for_email, + list_emails_by_org_id, + list_emails_by_status, + list_emails_by_user_id, + resend_api_request, + send_email, +} from './submodule/operations'; diff --git a/spacetime-resend-ts/src/submodule.ts b/spacetime-resend-ts/src/submodule.ts new file mode 100644 index 00000000000..6c7a5910234 --- /dev/null +++ b/spacetime-resend-ts/src/submodule.ts @@ -0,0 +1,15 @@ +export { default } from './submodule/schema'; +export { + resendDeliveryEventTable, + resendEmailTable, + t, +} from './submodule/schema'; +export { installResend } from './submodule/install'; +export * from './submodule/webhooks'; +export * from './submodule/operations'; + +export { + set_resend_config, + get_resend_config_status, +} from './submodule/config'; +export { add_admin_identity, remove_admin_identity } from './submodule/auth'; diff --git a/spacetime-resend-ts/src/submodule/auth.ts b/spacetime-resend-ts/src/submodule/auth.ts new file mode 100644 index 00000000000..44f2a577385 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/auth.ts @@ -0,0 +1,80 @@ +import { + spacetimedb, + t, + type ProcedureModuleCtx, + type WriteCtx, +} from './schema'; +import { throwSenderError } from './validation'; + +// Admin gate. Fresh publishes seed the owner via init. Public submodule calls +// never bootstrap admin state from "first caller wins". Procedure callers must +// pass the outer ctx.sender explicitly; transaction ctx may not carry sender. +type Sender = WriteCtx['sender']; +type ModuleTimestamp = WriteCtx['timestamp']; + +export type AdminVerdict = 'admin' | 'denied'; + +export function isAdmin(ctx: WriteCtx, sender: Sender): boolean { + return ctx.db.resendAdminIdentity.identity.find(sender) != null; +} + +export function adminVerdict(ctx: WriteCtx, sender: Sender): AdminVerdict { + return isAdmin(ctx, sender) ? 'admin' : 'denied'; +} + +export function denyIfNotAdmin(verdict: AdminVerdict): void { + if (verdict === 'denied') throwSenderError('resend.not_authorized'); +} + +export function requireAdmin(ctx: WriteCtx, sender: Sender): void { + if (!isAdmin(ctx, sender)) throwSenderError('resend.not_authorized'); +} + +// For owner-gated repair/setup code only. Do not call from a public bootstrap path. +export function seedAdmin( + ctx: WriteCtx, + sender: Sender, + timestamp: ModuleTimestamp +) { + if (ctx.db.resendAdminIdentity.identity.find(sender) != null) return; + ctx.db.resendAdminIdentity.insert({ + identity: sender, + addedAtMicros: timestamp.microsSinceUnixEpoch, + }); +} + +export const add_admin_identity = spacetimedb.procedure( + { identity: t.identity() }, + t.unit(), + (ctx: ProcedureModuleCtx, { identity }) => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + ctx.withTx(tx => { + if (tx.db.resendAdminIdentity.identity.find(identity) == null) { + tx.db.resendAdminIdentity.insert({ + identity, + addedAtMicros: ctx.timestamp.microsSinceUnixEpoch, + }); + } + }); + return {}; + } +); + +export const remove_admin_identity = spacetimedb.procedure( + { identity: t.identity() }, + t.unit(), + (ctx: ProcedureModuleCtx, { identity }) => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + ctx.withTx(tx => { + const existing = tx.db.resendAdminIdentity.identity.find(identity); + if (!existing) return; + if (tx.db.resendAdminIdentity.count() <= 1n) { + throwSenderError('resend.cannot_remove_last_admin'); + } + tx.db.resendAdminIdentity.delete(existing); + }); + return {}; + } +); diff --git a/spacetime-resend-ts/src/submodule/config.ts b/spacetime-resend-ts/src/submodule/config.ts new file mode 100644 index 00000000000..8f1334afee2 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/config.ts @@ -0,0 +1,112 @@ +import { + spacetimedb, + t, + type ProcedureModuleCtx, + type WriteCtx, +} from './schema'; +import { adminVerdict, denyIfNotAdmin } from './auth'; +import { throwSenderError } from './validation'; + +export type ResendConfig = { + apiKey: string; + webhookSigningSecret: string | undefined; + defaultFrom: string | undefined; +}; + +export function loadConfigOrThrow(ctx: WriteCtx): ResendConfig { + const row = ctx.db.resendConfig.singleton.find(true); + if (!row) { + throwSenderError( + 'resend.config_not_set: call set_resend_config(...) first' + ); + } + return { + apiKey: row.apiKey, + webhookSigningSecret: row.webhookSigningSecret, + defaultFrom: row.defaultFrom, + }; +} + +export function loadConfigOrThrowFromProcedure( + ctx: ProcedureModuleCtx +): ResendConfig { + return ctx.withTx(tx => loadConfigOrThrow(tx)); +} + +function upsertConfig( + ctx: WriteCtx, + args: { + apiKey: string; + webhookSigningSecret?: string | undefined; + defaultFrom?: string | undefined; + } +) { + const existing = ctx.db.resendConfig.singleton.find(true); + const row = { + singleton: true, + apiKey: args.apiKey, + webhookSigningSecret: + args.webhookSigningSecret ?? existing?.webhookSigningSecret, + defaultFrom: args.defaultFrom ?? existing?.defaultFrom, + updatedAt: ctx.timestamp, + }; + if (!existing) { + ctx.db.resendConfig.insert(row); + return; + } + if (ctx.db.resendConfig.singleton.update) { + ctx.db.resendConfig.singleton.update(row); + } else { + ctx.db.resendConfig.delete(existing); + ctx.db.resendConfig.insert(row); + } +} + +// Requires an admin seeded by the database owner; no public first-call bootstrap. +export const set_resend_config = spacetimedb.procedure( + { + apiKey: t.string(), + webhookSigningSecret: t.option(t.string()), + defaultFrom: t.option(t.string()), + }, + t.unit(), + (ctx, args) => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + ctx.withTx(tx => { + upsertConfig(tx, args); + }); + return {}; + } +); + +export const get_resend_config_status = spacetimedb.procedure( + {}, + t.object('ResendConfigStatus', { + isConfigured: t.bool(), + hasWebhookSecret: t.bool(), + defaultFrom: t.option(t.string()), + apiKeyLength: t.u16(), + }), + ctx => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + return ctx.withTx(tx => { + const row = tx.db.resendConfig.singleton.find(true); + if (!row) { + return { + isConfigured: false, + hasWebhookSecret: false, + defaultFrom: undefined, + apiKeyLength: 0, + }; + } + return { + isConfigured: true, + hasWebhookSecret: row.webhookSigningSecret !== undefined, + defaultFrom: row.defaultFrom, + apiKeyLength: row.apiKey.length, + }; + }); + } +); diff --git a/spacetime-resend-ts/src/submodule/email-input.ts b/spacetime-resend-ts/src/submodule/email-input.ts new file mode 100644 index 00000000000..c61608ff2a6 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/email-input.ts @@ -0,0 +1,90 @@ +import { hasControlCharacter } from './text-validation'; + +export type EmailInput = { + from?: string | undefined; + to: string[]; + subject: string; + html?: string | undefined; + text?: string | undefined; + cc?: string[] | undefined; + bcc?: string[] | undefined; + replyTo?: string[] | undefined; + tagsJson?: string | undefined; + headersJson?: string | undefined; + scheduledAt?: string | undefined; +}; + +function fail(code: string): never { + throw new Error(`resend.${code}`); +} + +function validateAddressList( + values: string[] | undefined, + field: string +): void { + if (values === undefined) return; + if (values.length > 100) fail(`${field}_too_many`); + for (const value of values) { + if ( + value.length === 0 || + value.length > 320 || + hasControlCharacter(value) + ) { + fail(`${field}_invalid_address`); + } + } +} + +function validateJson( + value: string | undefined, + field: string, + maxLength: number +): void { + if (value === undefined) return; + if (value.length > maxLength) fail(`${field}_too_large`); + let parsed: unknown; + try { + parsed = JSON.parse(value); + } catch { + fail(`${field}_invalid_json`); + } + if (parsed === null || typeof parsed !== 'object') + fail(`${field}_invalid_json`); +} + +export function validateEmailInput(args: EmailInput): void { + if (args.to.length === 0) fail('send_email_no_recipients'); + validateAddressList(args.to, 'to'); + validateAddressList(args.cc, 'cc'); + validateAddressList(args.bcc, 'bcc'); + validateAddressList(args.replyTo, 'reply_to'); + const recipientCount = + args.to.length + (args.cc?.length ?? 0) + (args.bcc?.length ?? 0); + if (recipientCount > 100) fail('send_email_too_many_recipients'); + if ( + args.from !== undefined && + (args.from.length === 0 || + args.from.length > 320 || + hasControlCharacter(args.from)) + ) + fail('send_email_invalid_from'); + if ( + args.subject.length === 0 || + args.subject.length > 998 || + hasControlCharacter(args.subject) + ) { + fail('send_email_invalid_subject'); + } + if (args.html === undefined && args.text === undefined) + fail('send_email_missing_content'); + if ((args.html?.length ?? 0) > 200_000) fail('send_email_html_too_large'); + if ((args.text?.length ?? 0) > 200_000) fail('send_email_text_too_large'); + validateJson(args.tagsJson, 'tags', 16_384); + validateJson(args.headersJson, 'headers', 16_384); + if ( + (args.scheduledAt?.length ?? 0) > 128 || + hasControlCharacter(args.scheduledAt ?? '') + ) { + fail('send_email_invalid_schedule'); + } +} diff --git a/spacetime-resend-ts/src/submodule/email_writes.ts b/spacetime-resend-ts/src/submodule/email_writes.ts new file mode 100644 index 00000000000..c7839b68880 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/email_writes.ts @@ -0,0 +1,77 @@ +import { + EmailStatus, + type EmailStatusValue, + type ModuleTimestamp, + type WriteCtx, +} from './schema'; + +// Any field passed as `undefined` preserves the existing row's value. Used by webhooks (sparse per-event fields). +export function upsertEmail( + ctx: WriteCtx, + now: ModuleTimestamp, + args: { + resendId: string; + fromAddress: string; + toAddressesJson: string; + subject: string | undefined; + html: string | undefined; + text: string | undefined; + status: EmailStatusValue | undefined; + lastError: string | undefined; + bouncedAt: ModuleTimestamp | undefined; + bounceJson: string | undefined; + failedAt: ModuleTimestamp | undefined; + failureReason: string | undefined; + complained: boolean; + complainedAt: ModuleTimestamp | undefined; + opened: boolean; + openedAt: ModuleTimestamp | undefined; + clicked: boolean; + clickedAt: ModuleTimestamp | undefined; + deliveredAt: ModuleTimestamp | undefined; + sentAt: ModuleTimestamp | undefined; + tagsJson: string | undefined; + userId: string | undefined; + orgId: string | undefined; + } +) { + const existing = ctx.db.resendEmail.resendId.find(args.resendId); + const row = { + resendId: args.resendId, + fromAddress: args.fromAddress, + toAddressesJson: args.toAddressesJson, + subject: args.subject ?? existing?.subject, + html: args.html ?? existing?.html, + text: args.text ?? existing?.text, + status: args.status ?? existing?.status ?? EmailStatus.Queued, + lastError: args.lastError ?? existing?.lastError, + bouncedAt: args.bouncedAt ?? existing?.bouncedAt, + bounceJson: args.bounceJson ?? existing?.bounceJson, + failedAt: args.failedAt ?? existing?.failedAt, + failureReason: args.failureReason ?? existing?.failureReason, + complained: args.complained || (existing?.complained ?? false), + complainedAt: args.complainedAt ?? existing?.complainedAt, + opened: args.opened || (existing?.opened ?? false), + openedAt: args.openedAt ?? existing?.openedAt, + clicked: args.clicked || (existing?.clicked ?? false), + clickedAt: args.clickedAt ?? existing?.clickedAt, + deliveredAt: args.deliveredAt ?? existing?.deliveredAt, + sentAt: args.sentAt ?? existing?.sentAt, + tagsJson: args.tagsJson ?? existing?.tagsJson, + userId: args.userId ?? existing?.userId, + orgId: args.orgId ?? existing?.orgId, + createdAt: existing?.createdAt ?? now, + updatedAt: now, + }; + + if (!existing) { + ctx.db.resendEmail.insert(row); + return; + } + if (ctx.db.resendEmail.resendId.update) { + ctx.db.resendEmail.resendId.update(row); + } else { + ctx.db.resendEmail.delete(existing); + ctx.db.resendEmail.insert(row); + } +} diff --git a/spacetime-resend-ts/src/submodule/http.ts b/spacetime-resend-ts/src/submodule/http.ts new file mode 100644 index 00000000000..9d60bcef084 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/http.ts @@ -0,0 +1,66 @@ +import { type ProcedureModuleCtx, vResendErrorBody } from './schema'; +import { parseWithSchema, safeJsonParse, throwSenderError } from './validation'; +import { buildResendHttpRequest } from './request'; + +export type ResendHttpResponse = { + status: number; + body: string; +}; + +export function callResend( + ctx: ProcedureModuleCtx, + args: { + method: string; + path: string; + apiKey: string; + jsonBody: string | undefined; + idempotencyKey: string | undefined; + } +): ResendHttpResponse { + let request; + try { + request = buildResendHttpRequest(args); + } catch (error) { + throwSenderError( + error instanceof Error ? error.message : 'resend.request_invalid' + ); + } + const response = ctx.http.fetch(request.url, { + method: request.method, + headers: request.headers, + body: request.body, + }); + return { + status: response.status, + body: response.text(), + }; +} +export function ensureOkOrThrow( + response: ResendHttpResponse, + errorPrefix: string +): void { + if (response.status >= 200 && response.status < 300) return; + throwSenderError( + `${errorPrefix}:${response.status}${resendErrorSuffix(response.body)}` + ); +} + +export function resendErrorSuffix(body: string): string { + const parsed = safeJsonParse(body); + if (parsed !== undefined) { + const result = parseWithSchema(vResendErrorBody, parsed); + if (result.kind === 'success') { + const parts: string[] = []; + if (result.data.name) parts.push(`name=${result.data.name}`); + if (result.data.message) { + parts.push( + `msg=${result.data.message.replace(/\s+/g, ' ').slice(0, 240)}` + ); + } + if (parts.length > 0) return `:${parts.join('|')}`; + } + } + const compact = body.replace(/\s+/g, ' ').trim(); + if (!compact) return ''; + return `:body=${compact.slice(0, 240)}`; +} diff --git a/spacetime-resend-ts/src/submodule/install.ts b/spacetime-resend-ts/src/submodule/install.ts new file mode 100644 index 00000000000..67858e196b6 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/install.ts @@ -0,0 +1,9 @@ +import type { ReducerModuleCtx } from './schema'; + +export function installResend(ctx: ReducerModuleCtx) { + if (ctx.db.resendAdminIdentity.identity.find(ctx.sender) != null) return; + ctx.db.resendAdminIdentity.insert({ + identity: ctx.sender, + addedAtMicros: ctx.timestamp.microsSinceUnixEpoch, + }); +} diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts new file mode 100644 index 00000000000..1e7f16a6f54 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -0,0 +1,376 @@ +import * as v from 'valibot'; +import { + EmailStatus, + emailStatus, + resendDeliveryEventTable, + resendEmailTable, + sendEmailResult, + spacetimedb, + t, + vSendEmailResponse, + type ProcedureModuleCtx, + type WriteCtx, +} from './schema'; +import { callResend, ensureOkOrThrow } from './http'; +import { + parseWithSchema, + safeJsonParse, + summarizeIssues, + throwSenderError, +} from './validation'; +import { upsertEmail } from './email_writes'; +import { loadConfigOrThrowFromProcedure } from './config'; +import { adminVerdict, denyIfNotAdmin } from './auth'; +import { validateEmailInput } from './email-input'; + +function requireProcedureAdmin(ctx: ProcedureModuleCtx): void { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); +} + +const MAX_QUERY_ROWS = 1000; + +function takeRows(rows: Iterable): T[] { + const out: T[] = []; + for (const row of rows) { + if (out.length >= MAX_QUERY_ROWS) break; + out.push(row); + } + return out; +} + +const vTagsForExtraction = v.union([ + v.record(v.string(), v.string()), + v.array(v.object({ name: v.string(), value: v.string() })), +]); + +function extractTagFieldsFromJson(tagsJson: string | undefined): { + userId: string | undefined; + orgId: string | undefined; +} { + if (!tagsJson) return { userId: undefined, orgId: undefined }; + const parsed = safeJsonParse(tagsJson); + if (parsed === undefined) return { userId: undefined, orgId: undefined }; + const result = parseWithSchema(vTagsForExtraction, parsed); + if (result.kind === 'error') { + return { userId: undefined, orgId: undefined }; + } + const tags = result.data; + if (Array.isArray(tags)) { + let userId: string | undefined; + let orgId: string | undefined; + for (const tag of tags) { + if (tag.name === 'userId') userId = tag.value; + if (tag.name === 'orgId') orgId = tag.value; + } + return { userId, orgId }; + } + return { userId: tags['userId'], orgId: tags['orgId'] }; +} + +// Resend expects snake_case fields in the POST /emails request body. +type ResendSendEmailBody = { + from: string; + to: string[]; + subject: string; + html?: string; + text?: string; + cc?: string[]; + bcc?: string[]; + reply_to?: string[]; + scheduled_at?: string; + tags?: unknown; + headers?: unknown; +}; + +function buildSendEmailBody(args: { + from: string; + to: string[]; + subject: string; + html: string | undefined; + text: string | undefined; + cc: string[] | undefined; + bcc: string[] | undefined; + replyTo: string[] | undefined; + tagsJson: string | undefined; + headersJson: string | undefined; + scheduledAt: string | undefined; +}): string { + const body: ResendSendEmailBody = { + from: args.from, + to: args.to, + subject: args.subject, + }; + if (args.html !== undefined) body.html = args.html; + if (args.text !== undefined) body.text = args.text; + if (args.cc !== undefined && args.cc.length > 0) body.cc = args.cc; + if (args.bcc !== undefined && args.bcc.length > 0) body.bcc = args.bcc; + if (args.replyTo !== undefined && args.replyTo.length > 0) { + body.reply_to = args.replyTo; + } + if (args.scheduledAt !== undefined) body.scheduled_at = args.scheduledAt; + if (args.tagsJson !== undefined) { + const parsed = safeJsonParse(args.tagsJson); + if (parsed !== undefined) body.tags = parsed; + } + if (args.headersJson !== undefined) { + const parsed = safeJsonParse(args.headersJson); + if (parsed !== undefined) body.headers = parsed; + } + return JSON.stringify(body); +} + +function recordQueuedEmail( + ctx: WriteCtx, + now: ProcedureModuleCtx['timestamp'], + args: { + resendId: string; + from: string; + to: string[]; + subject: string; + html: string | undefined; + text: string | undefined; + tagsJson: string | undefined; + } +) { + const tagFields = extractTagFieldsFromJson(args.tagsJson); + upsertEmail(ctx, now, { + resendId: args.resendId, + fromAddress: args.from, + toAddressesJson: JSON.stringify(args.to), + subject: args.subject, + html: args.html, + text: args.text, + status: EmailStatus.Queued, + lastError: undefined, + bouncedAt: undefined, + bounceJson: undefined, + failedAt: undefined, + failureReason: undefined, + complained: false, + complainedAt: undefined, + opened: false, + openedAt: undefined, + clicked: false, + clickedAt: undefined, + deliveredAt: undefined, + sentAt: undefined, + tagsJson: args.tagsJson, + userId: tagFields.userId, + orgId: tagFields.orgId, + }); +} + +export type SendEmailArgs = { + from?: string | undefined; + to: string[]; + subject: string; + html?: string | undefined; + text?: string | undefined; + cc?: string[] | undefined; + bcc?: string[] | undefined; + replyTo?: string[] | undefined; + tagsJson?: string | undefined; + headersJson?: string | undefined; + scheduledAt?: string | undefined; + idempotencyKey?: string | undefined; +}; + +export function sendEmail(ctx: ProcedureModuleCtx, args: SendEmailArgs) { + try { + validateEmailInput(args); + } catch (error) { + throwSenderError( + error instanceof Error ? error.message : 'resend.send_email_invalid_input' + ); + } + const cfg = loadConfigOrThrowFromProcedure(ctx); + const fromAddress = args.from ?? cfg.defaultFrom; + if (!fromAddress) throwSenderError('resend.send_email_missing_from'); + + const jsonBody = buildSendEmailBody({ + from: fromAddress, + to: args.to, + subject: args.subject, + html: args.html, + text: args.text, + cc: args.cc, + bcc: args.bcc, + replyTo: args.replyTo, + tagsJson: args.tagsJson, + headersJson: args.headersJson, + scheduledAt: args.scheduledAt, + }); + + const response = callResend(ctx, { + method: 'POST', + path: '/emails', + apiKey: cfg.apiKey, + jsonBody, + idempotencyKey: args.idempotencyKey, + }); + ensureOkOrThrow(response, 'resend.send_email_failed'); + + const parsed = safeJsonParse(response.body); + if (parsed === undefined) + throwSenderError('resend.send_email_invalid_response'); + const result = parseWithSchema(vSendEmailResponse, parsed); + if (result.kind === 'error') { + throwSenderError( + `resend.send_email_invalid_response:${summarizeIssues(result.issues)}` + ); + } + + const resendId = result.data.id; + ctx.withTx(tx => { + recordQueuedEmail(tx, ctx.timestamp, { + resendId, + from: fromAddress, + to: args.to, + subject: args.subject, + html: args.html, + text: args.text, + tagsJson: args.tagsJson, + }); + }); + return { resendId }; +} + +const sendEmailArgs = { + from: t.option(t.string()), + to: t.array(t.string()), + subject: t.string(), + html: t.option(t.string()), + text: t.option(t.string()), + cc: t.option(t.array(t.string())), + bcc: t.option(t.array(t.string())), + replyTo: t.option(t.array(t.string())), + tagsJson: t.option(t.string()), + headersJson: t.option(t.string()), + scheduledAt: t.option(t.string()), + idempotencyKey: t.option(t.string()), +}; + +export const send_email = spacetimedb.procedure( + sendEmailArgs, + sendEmailResult, + (ctx, args) => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + return sendEmail(ctx, args); + } +); + +export const cancel_email = spacetimedb.procedure( + { resendId: t.string() }, + t.unit(), + (ctx, args) => { + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + const cfg = loadConfigOrThrowFromProcedure(ctx); + const response = callResend(ctx, { + method: 'POST', + path: `/emails/${args.resendId}/cancel`, + apiKey: cfg.apiKey, + jsonBody: undefined, + idempotencyKey: undefined, + }); + ensureOkOrThrow(response, 'resend.cancel_email_failed'); + + ctx.withTx(tx => { + const existing = tx.db.resendEmail.resendId.find(args.resendId); + if (!existing) return; + const updated = { + ...existing, + status: EmailStatus.Cancelled, + updatedAt: ctx.timestamp, + }; + if (tx.db.resendEmail.resendId.update) { + tx.db.resendEmail.resendId.update(updated); + } else { + tx.db.resendEmail.delete(existing); + tx.db.resendEmail.insert(updated); + } + }); + return {}; + } +); + +export const get_email = spacetimedb.procedure( + { resendId: t.string() }, + t.option(resendEmailTable.rowType), + (ctx, { resendId }) => { + requireProcedureAdmin(ctx); + return ctx.withTx( + tx => tx.db.resendEmail.resendId.find(resendId) ?? undefined + ); + } +); + +export const list_emails_by_user_id = spacetimedb.procedure( + { userId: t.string() }, + t.array(resendEmailTable.rowType), + (ctx, { userId }) => { + requireProcedureAdmin(ctx); + return ctx.withTx(tx => + takeRows(tx.db.resendEmail.byUserId.filter(userId)) + ); + } +); + +export const list_emails_by_org_id = spacetimedb.procedure( + { orgId: t.string() }, + t.array(resendEmailTable.rowType), + (ctx, { orgId }) => { + requireProcedureAdmin(ctx); + return ctx.withTx(tx => takeRows(tx.db.resendEmail.byOrgId.filter(orgId))); + } +); + +export const list_emails_by_status = spacetimedb.procedure( + { status: emailStatus }, + t.array(resendEmailTable.rowType), + (ctx, { status }) => { + requireProcedureAdmin(ctx); + return ctx.withTx(tx => + takeRows(tx.db.resendEmail.byStatus.filter(status)) + ); + } +); + +export const list_delivery_events_for_email = spacetimedb.procedure( + { resendId: t.string() }, + t.array(resendDeliveryEventTable.rowType), + (ctx, { resendId }) => { + requireProcedureAdmin(ctx); + return ctx.withTx(tx => + takeRows(tx.db.resendDeliveryEvent.byResendId.filter(resendId)) + ); + } +); + +export const resend_api_request = spacetimedb.procedure( + { + method: t.string(), + path: t.string(), + jsonBody: t.option(t.string()), + idempotencyKey: t.option(t.string()), + }, + t.object('ResendApiRequestResult', { + status: t.u16(), + body: t.string(), + }), + (ctx, args) => { + // Administrators may make authenticated Resend calls with the stored key. + const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); + denyIfNotAdmin(verdict); + const cfg = loadConfigOrThrowFromProcedure(ctx); + return callResend(ctx, { + method: args.method, + path: args.path, + apiKey: cfg.apiKey, + jsonBody: args.jsonBody, + idempotencyKey: args.idempotencyKey, + }); + } +); diff --git a/spacetime-resend-ts/src/submodule/request.ts b/spacetime-resend-ts/src/submodule/request.ts new file mode 100644 index 00000000000..b7c067c77b6 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/request.ts @@ -0,0 +1,67 @@ +import { hasControlCharacter } from './text-validation'; + +const RESEND_API_BASE = 'https://api.resend.com'; +const ALLOWED_METHODS = new Set(['GET', 'POST', 'PATCH', 'DELETE']); +const MAX_PATH_LENGTH = 2048; +const MAX_JSON_BODY_LENGTH = 256 * 1024; +const MAX_IDEMPOTENCY_KEY_LENGTH = 256; + +export type ResendHttpRequest = { + method: string; + url: string; + headers: Record; + body: string | undefined; +}; + +function validatePath(path: string): string { + const normalized = path.trim(); + if (!normalized.startsWith('/') || normalized.startsWith('//')) { + throw new Error('resend.request_path_invalid'); + } + if (normalized.includes('\\') || normalized.includes('#')) { + throw new Error('resend.request_path_invalid'); + } + if (normalized.length > MAX_PATH_LENGTH || hasControlCharacter(normalized)) { + throw new Error('resend.request_path_invalid'); + } + return normalized; +} + +export function buildResendHttpRequest(args: { + method: string; + path: string; + apiKey: string; + jsonBody: string | undefined; + idempotencyKey: string | undefined; +}): ResendHttpRequest { + const method = args.method.trim().toUpperCase(); + if (!ALLOWED_METHODS.has(method)) { + throw new Error('resend.request_method_invalid'); + } + + const path = validatePath(args.path); + const body = args.jsonBody?.length ? args.jsonBody : undefined; + if (body !== undefined && body.length > MAX_JSON_BODY_LENGTH) { + throw new Error('resend.request_body_too_large'); + } + if ( + args.idempotencyKey && + args.idempotencyKey.length > MAX_IDEMPOTENCY_KEY_LENGTH + ) { + throw new Error('resend.idempotency_key_too_long'); + } + + const headers: Record = { + Authorization: `Bearer ${args.apiKey}`, + Accept: 'application/json', + }; + if (body !== undefined) headers['Content-Type'] = 'application/json'; + if (args.idempotencyKey) headers['Idempotency-Key'] = args.idempotencyKey; + + return { + method, + url: `${RESEND_API_BASE}${path}`, + headers, + body, + }; +} diff --git a/spacetime-resend-ts/src/submodule/schema.ts b/spacetime-resend-ts/src/submodule/schema.ts new file mode 100644 index 00000000000..47faa95f1a7 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/schema.ts @@ -0,0 +1,293 @@ +import { + schema, + table, + t, + Range, + SenderError, + type ProcedureCtx, + type ReducerCtx, + type TransactionCtx, +} from 'spacetimedb/server'; +import * as v from 'valibot'; +import { installResend } from './install'; + +// Webhook delivery state. Received = ingest accepted. Processed = applied. +// Ignored = duplicate/unknown event type. Failed = signature/format error. +export const webhookEventStatus = t.enum('WebhookEventStatus', [ + 'Received', + 'Processed', + 'Ignored', + 'Failed', +]); +export const WebhookEventStatus = { + Received: { tag: 'Received' as const }, + Processed: { tag: 'Processed' as const }, + Ignored: { tag: 'Ignored' as const }, + Failed: { tag: 'Failed' as const }, +}; + +// Lifecycle of a tracked outbound email. Tags match Resend's +// `email.` webhook events for direct ingestion mapping. +export const emailStatus = t.enum('EmailStatus', [ + 'Queued', + 'Sent', + 'Delivered', + 'DeliveryDelayed', + 'Bounced', + 'Failed', + 'Cancelled', +]); +export const EmailStatus = { + Queued: { tag: 'Queued' as const }, + Sent: { tag: 'Sent' as const }, + Delivered: { tag: 'Delivered' as const }, + DeliveryDelayed: { tag: 'DeliveryDelayed' as const }, + Bounced: { tag: 'Bounced' as const }, + Failed: { tag: 'Failed' as const }, + Cancelled: { tag: 'Cancelled' as const }, +}; +export type EmailStatusValue = (typeof EmailStatus)[keyof typeof EmailStatus]; +export type WebhookEventStatusValue = + (typeof WebhookEventStatus)[keyof typeof WebhookEventStatus]; + +export const resendEmailRow = { + resendId: t.string().primaryKey(), + fromAddress: t.string(), + toAddressesJson: t.string(), + subject: t.option(t.string()), + status: emailStatus, + lastError: t.option(t.string()), + bouncedAt: t.option(t.timestamp()), + bounceJson: t.option(t.string()), + failedAt: t.option(t.timestamp()), + failureReason: t.option(t.string()), + complained: t.bool(), + complainedAt: t.option(t.timestamp()), + opened: t.bool(), + openedAt: t.option(t.timestamp()), + clicked: t.bool(), + clickedAt: t.option(t.timestamp()), + deliveredAt: t.option(t.timestamp()), + sentAt: t.option(t.timestamp()), + html: t.option(t.string()), + text: t.option(t.string()), + tagsJson: t.option(t.string()), + userId: t.option(t.string()), + orgId: t.option(t.string()), + createdAt: t.timestamp(), + updatedAt: t.timestamp(), +}; + +export const resendDeliveryEventRow = { + eventId: t.string().primaryKey(), + resendId: t.string(), + eventType: t.string(), + createdAtIso: t.string(), + detailJson: t.option(t.string()), + insertedAt: t.timestamp(), +}; + +export const resendWebhookEventRow = { + eventId: t.string().primaryKey(), + eventType: t.string(), + payloadJson: t.string(), + signatureHeader: t.option(t.string()), + timestampHeader: t.option(t.string()), + status: webhookEventStatus, + errorMessage: t.option(t.string()), + receivedAt: t.timestamp(), + processedAt: t.option(t.timestamp()), +}; + +// Private singleton; secrets never leak via subscription. +export const resendConfigRow = { + singleton: t.bool().primaryKey(), + apiKey: t.string(), + webhookSigningSecret: t.option(t.string()), + defaultFrom: t.option(t.string()), + updatedAt: t.timestamp(), +}; + +// Fresh publishes seed the owner via init; public procedures never bootstrap admin state. +export const resendAdminIdentityRow = { + identity: t.identity().primaryKey(), + addedAtMicros: t.i64(), +}; + +export const resendEmailTable = table( + { + name: 'resend_email', + public: false, + indexes: [ + { accessor: 'byUserId', algorithm: 'btree', columns: ['userId'] }, + { accessor: 'byOrgId', algorithm: 'btree', columns: ['orgId'] }, + { accessor: 'byStatus', algorithm: 'btree', columns: ['status'] }, + { accessor: 'byUpdatedAt', algorithm: 'btree', columns: ['updatedAt'] }, + ], + }, + resendEmailRow +); + +export const resendDeliveryEventTable = table( + { + name: 'resend_delivery_event', + public: false, + indexes: [ + { accessor: 'byResendId', algorithm: 'btree', columns: ['resendId'] }, + { + accessor: 'byResendIdEventType', + algorithm: 'btree', + columns: ['resendId', 'eventType'], + }, + ], + }, + resendDeliveryEventRow +); + +// Private because the raw payload and signature headers are operational data. +// Host modules can expose an admin-only view when needed. +export const resendWebhookEventTable = table( + { + name: 'resend_webhook_event', + public: false, + indexes: [ + { accessor: 'byStatus', algorithm: 'btree', columns: ['status'] }, + ], + }, + resendWebhookEventRow +); + +export const resendConfigTable = table( + { name: 'resend_config', public: false, indexes: [] }, + resendConfigRow +); + +export const resendAdminIdentityTable = table( + { name: 'resend_admin_identity', public: false, indexes: [] }, + resendAdminIdentityRow +); + +export const spacetimedb = schema({ + resendEmail: resendEmailTable, + resendDeliveryEvent: resendDeliveryEventTable, + resendWebhookEvent: resendWebhookEventTable, + resendConfig: resendConfigTable, + resendAdminIdentity: resendAdminIdentityTable, +}); + +export const init = spacetimedb.init(ctx => { + installResend(ctx); +}); + +export default spacetimedb; + +export type ReducerModuleCtx = ReducerCtx; +export type ProcedureModuleCtx = ProcedureCtx; +export type TransactionModuleCtx = TransactionCtx< + typeof spacetimedb.schemaType +>; +export type WriteCtx = ReducerModuleCtx | TransactionModuleCtx; +export type ModuleTimestamp = ReducerModuleCtx['timestamp']; + +export const sendEmailResult = t.object('SendEmailResult', { + resendId: t.string(), +}); + +export const resendHttpResponse = t.object('ResendHttpResponse', { + status: t.u16(), + body: t.string(), +}); + +export { Range, SenderError, t }; + +// Mirror Resend SDK's BaseEmailEventData. +const vBaseEmailEventData = { + broadcast_id: v.optional(v.string()), + created_at: v.string(), + email_id: v.string(), + from: v.string(), + to: v.array(v.string()), + subject: v.string(), + template_id: v.optional(v.string()), + tags: v.optional(v.record(v.string(), v.string())), +}; + +const vBaseEvent = { + created_at: v.string(), +}; + +export const vEmailEvent = v.variant('type', [ + v.object({ + ...vBaseEvent, + type: v.literal('email.sent'), + data: v.object(vBaseEmailEventData), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.delivered'), + data: v.object(vBaseEmailEventData), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.delivery_delayed'), + data: v.object(vBaseEmailEventData), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.complained'), + data: v.object(vBaseEmailEventData), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.bounced'), + data: v.object({ + ...vBaseEmailEventData, + bounce: v.object({ + message: v.string(), + subType: v.string(), + type: v.string(), + }), + }), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.opened'), + data: v.object(vBaseEmailEventData), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.clicked'), + data: v.object({ + ...vBaseEmailEventData, + click: v.object({ + ipAddress: v.string(), + link: v.string(), + timestamp: v.string(), + userAgent: v.string(), + }), + }), + }), + v.object({ + ...vBaseEvent, + type: v.literal('email.failed'), + data: v.object({ + ...vBaseEmailEventData, + failed: v.object({ + reason: v.string(), + }), + }), + }), +]); + +export type EmailEvent = v.InferOutput; +export type EmailEventType = EmailEvent['type']; + +export const vResendErrorBody = v.object({ + name: v.optional(v.string()), + message: v.optional(v.string()), + statusCode: v.optional(v.union([v.number(), v.null()])), +}); + +export const vSendEmailResponse = v.object({ + id: v.string(), +}); diff --git a/spacetime-resend-ts/src/submodule/text-validation.ts b/spacetime-resend-ts/src/submodule/text-validation.ts new file mode 100644 index 00000000000..5409d6119b8 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/text-validation.ts @@ -0,0 +1,7 @@ +export function hasControlCharacter(value: string): boolean { + for (let index = 0; index < value.length; index++) { + const code = value.charCodeAt(index); + if (code <= 0x1f || code === 0x7f) return true; + } + return false; +} diff --git a/spacetime-resend-ts/src/submodule/validation.ts b/spacetime-resend-ts/src/submodule/validation.ts new file mode 100644 index 00000000000..e6947be4bc1 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/validation.ts @@ -0,0 +1,45 @@ +import * as v from 'valibot'; +import { SenderError } from 'spacetimedb/server'; + +export type ParseResult = + | { kind: 'success'; data: T } + | { kind: 'error'; issues: v.BaseIssue[] }; + +export function parseWithSchema( + schema: TSchema, + input: unknown +): ParseResult> { + const result = v.safeParse(schema, input); + if (result.success) return { kind: 'success', data: result.output }; + return { kind: 'error', issues: result.issues }; +} + +export function assertExhaustive(value: never): never { + throw new Error(`Unhandled discriminant: ${value as string}`); +} + +export function throwSenderError(message: string): never { + throw new SenderError(message); +} + +export function safeJsonParse(input: string): unknown { + try { + return JSON.parse(input); + } catch { + return undefined; + } +} + +export function summarizeIssues(issues: v.BaseIssue[]): string { + if (issues.length === 0) return 'no issues'; + const head = issues[0]!; + const path = (head.path ?? []) + .map(p => + typeof p.key === 'string' || typeof p.key === 'number' + ? String(p.key) + : '?' + ) + .join('.'); + const where = path ? ` at ${path}` : ''; + return `${head.message}${where}`; +} diff --git a/spacetime-resend-ts/src/submodule/webhook-metadata.ts b/spacetime-resend-ts/src/submodule/webhook-metadata.ts new file mode 100644 index 00000000000..62b77b6e133 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/webhook-metadata.ts @@ -0,0 +1,13 @@ +export function parseResendEventType(payloadJson: string): string | undefined { + let parsed: unknown; + try { + parsed = JSON.parse(payloadJson); + } catch { + return undefined; + } + if (typeof parsed !== 'object' || parsed === null) return undefined; + const eventType = (parsed as Record).type; + return typeof eventType === 'string' && eventType.length > 0 + ? eventType + : undefined; +} diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts new file mode 100644 index 00000000000..b43fcd61900 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -0,0 +1,396 @@ +import { + EmailStatus, + WebhookEventStatus, + spacetimedb, + t, + vEmailEvent, + type EmailEvent, + type EmailStatusValue, + type ModuleTimestamp, + type ReducerModuleCtx, + type WebhookEventStatusValue, + type WriteCtx, +} from './schema'; +import { upsertEmail } from './email_writes'; +import { requireAdmin } from './auth'; +import { verifySvixSignature } from '@spacetimedb/crypto'; +import { + SyncResponse, + type Request, + type HandlerContext, +} from 'spacetimedb/server'; +import { + assertExhaustive, + parseWithSchema, + safeJsonParse, + summarizeIssues, + throwSenderError, +} from './validation'; +import { parseResendEventType } from './webhook-metadata'; + +type ResendTags = EmailEvent['data']['tags']; + +function toAddressArray(value: string | string[]): string[] { + return Array.isArray(value) ? value : [value]; +} + +function tagsToJson(tags: ResendTags): string | undefined { + if (!tags) return undefined; + try { + return JSON.stringify(tags); + } catch { + return undefined; + } +} + +function extractTagFields(tags: ResendTags): { + userId: string | undefined; + orgId: string | undefined; +} { + if (!tags) return { userId: undefined, orgId: undefined }; + if (Array.isArray(tags)) { + let userId: string | undefined; + let orgId: string | undefined; + for (const tag of tags) { + if (tag.name === 'userId') userId = tag.value; + if (tag.name === 'orgId') orgId = tag.value; + } + return { userId, orgId }; + } + return { userId: tags['userId'], orgId: tags['orgId'] }; +} + +function recordDeliveryEvent( + ctx: WriteCtx, + now: ModuleTimestamp, + args: { + eventId: string; + resendId: string; + eventType: string; + createdAtIso: string; + detailJson: string | undefined; + } +) { + if (ctx.db.resendDeliveryEvent.eventId.find(args.eventId)) return; + ctx.db.resendDeliveryEvent.insert({ + eventId: args.eventId, + resendId: args.resendId, + eventType: args.eventType, + createdAtIso: args.createdAtIso, + detailJson: args.detailJson, + insertedAt: now, + }); +} + +function updateWebhookStatus( + ctx: ReducerModuleCtx, + eventId: string, + status: WebhookEventStatusValue, + errorMessage: string | undefined +) { + const existing = ctx.db.resendWebhookEvent.eventId.find(eventId); + if (!existing) return; + + const isTerminal = status.tag === 'Processed' || status.tag === 'Failed'; + const updated = { + ...existing, + status, + errorMessage, + processedAt: isTerminal ? ctx.timestamp : existing.processedAt, + }; + + if (ctx.db.resendWebhookEvent.eventId.update) { + ctx.db.resendWebhookEvent.eventId.update(updated); + } else { + ctx.db.resendWebhookEvent.delete(existing); + ctx.db.resendWebhookEvent.insert(updated); + } +} + +function makeEmailUpsertArgs( + event: EmailEvent, + now: ModuleTimestamp +): Parameters[2] { + const data = event.data; + const fromAddress = Array.isArray(data.from) ? data.from[0]! : data.from; + const toAddresses = toAddressArray(data.to); + const tagFields = extractTagFields(data.tags); + const tagsJson = tagsToJson(data.tags); + const subject = data.subject; + + const base = { + resendId: data.email_id, + fromAddress, + toAddressesJson: JSON.stringify(toAddresses), + subject, + // undefined preserves whatever send_email recorded. + html: undefined, + text: undefined, + lastError: undefined, + bouncedAt: undefined, + bounceJson: undefined, + failedAt: undefined, + failureReason: undefined, + complained: false, + complainedAt: undefined, + opened: false, + openedAt: undefined, + clicked: false, + clickedAt: undefined, + deliveredAt: undefined, + sentAt: undefined, + tagsJson, + userId: tagFields.userId, + orgId: tagFields.orgId, + // status undefined = preserve existing or default to queued; branches override. + status: undefined as EmailStatusValue | undefined, + } satisfies Parameters[2]; + + switch (event.type) { + case 'email.sent': + return { ...base, status: EmailStatus.Sent, sentAt: now }; + case 'email.delivered': + return { ...base, status: EmailStatus.Delivered, deliveredAt: now }; + case 'email.delivery_delayed': + return { ...base, status: EmailStatus.DeliveryDelayed }; + case 'email.bounced': + return { + ...base, + status: EmailStatus.Bounced, + bouncedAt: now, + bounceJson: JSON.stringify(event.data.bounce), + lastError: event.data.bounce.message, + }; + case 'email.failed': + return { + ...base, + status: EmailStatus.Failed, + failedAt: now, + failureReason: event.data.failed.reason, + lastError: event.data.failed.reason, + }; + case 'email.complained': + return { ...base, complained: true, complainedAt: now }; + case 'email.opened': + return { ...base, opened: true, openedAt: now }; + case 'email.clicked': + return { ...base, clicked: true, clickedAt: now }; + default: + return assertExhaustive(event); + } +} + +function detailJsonForEvent(event: EmailEvent): string | undefined { + switch (event.type) { + case 'email.bounced': + return JSON.stringify(event.data.bounce); + case 'email.failed': + return JSON.stringify(event.data.failed); + case 'email.clicked': + return JSON.stringify(event.data.click); + case 'email.sent': + case 'email.delivered': + case 'email.delivery_delayed': + case 'email.complained': + case 'email.opened': + return undefined; + default: + return assertExhaustive(event); + } +} + +function applyResendEvent( + ctx: ReducerModuleCtx, + eventId: string, + payloadJson: string +): { status: WebhookEventStatusValue; error: string | undefined } { + const parsed = safeJsonParse(payloadJson); + if (parsed === undefined) { + return { status: WebhookEventStatus.Failed, error: 'invalid JSON payload' }; + } + + const result = parseWithSchema(vEmailEvent, parsed); + if (result.kind === 'error') { + return { + status: WebhookEventStatus.Failed, + error: `payload validation failed: ${summarizeIssues(result.issues)}`, + }; + } + + const event = result.data; + const now = ctx.timestamp; + upsertEmail(ctx, now, makeEmailUpsertArgs(event, now)); + recordDeliveryEvent(ctx, now, { + eventId, + resendId: event.data.email_id, + eventType: event.type, + createdAtIso: event.created_at, + detailJson: detailJsonForEvent(event), + }); + return { status: WebhookEventStatus.Processed, error: undefined }; +} + +export interface ResendWebhookIngestArgs { + eventId: string; + eventType: string; + payloadJson: string; + signatureHeader?: string | undefined; + timestampHeader?: string | undefined; +} + +const MAX_WEBHOOK_BODY_LENGTH = 1024 * 1024; +const MAX_WEBHOOK_HEADER_LENGTH = 8192; +const MAX_WEBHOOK_METADATA_LENGTH = 255; + +// Verify the Svix signature in-module, then store and apply the event. The +// reducer and native HTTP route share this single ingest path and receive an +function ingestResendWebhook( + ctx: WriteCtx, + args: ResendWebhookIngestArgs +): { status: number; code: string } { + if ( + args.eventId.length === 0 || + args.eventId.length > MAX_WEBHOOK_METADATA_LENGTH || + args.eventType.length === 0 || + args.eventType.length > MAX_WEBHOOK_METADATA_LENGTH + ) { + return { status: 400, code: 'resend.webhook_metadata_invalid' }; + } + if (args.payloadJson.length > MAX_WEBHOOK_BODY_LENGTH) { + return { status: 413, code: 'resend.webhook_payload_too_large' }; + } + if ( + (args.signatureHeader?.length ?? 0) > MAX_WEBHOOK_HEADER_LENGTH || + (args.timestampHeader?.length ?? 0) > MAX_WEBHOOK_HEADER_LENGTH + ) { + return { status: 400, code: 'resend.webhook_header_too_large' }; + } + + const cfg = ctx.db.resendConfig.singleton.find(true); + if (!cfg?.webhookSigningSecret) { + return { status: 500, code: 'resend.webhook_secret_not_configured' }; + } + const nowSeconds = Number(ctx.timestamp.microsSinceUnixEpoch / 1_000_000n); + const sigOk = verifySvixSignature({ + svixId: args.eventId, + svixTimestamp: args.timestampHeader ?? '', + svixSignature: args.signatureHeader ?? '', + rawBody: args.payloadJson, + secret: cfg.webhookSigningSecret, + nowSeconds, + }); + if (!sigOk) return { status: 401, code: 'resend.webhook_signature_mismatch' }; + + const signedEventType = parseResendEventType(args.payloadJson); + if (!signedEventType || signedEventType !== args.eventType) { + return { status: 400, code: 'resend.webhook_metadata_mismatch' }; + } + + // Idempotent: svix redelivers, so a known event id is a success no-op. + if (ctx.db.resendWebhookEvent.eventId.find(args.eventId)) { + return { status: 200, code: 'ok' }; + } + + ctx.db.resendWebhookEvent.insert({ + eventId: args.eventId, + eventType: signedEventType, + payloadJson: args.payloadJson, + signatureHeader: args.signatureHeader, + timestampHeader: args.timestampHeader, + status: WebhookEventStatus.Received, + errorMessage: undefined, + receivedAt: ctx.timestamp, + processedAt: undefined, + }); + + const outcome = applyResendEvent( + ctx as ReducerModuleCtx, + args.eventId, + args.payloadJson + ); + updateWebhookStatus( + ctx as ReducerModuleCtx, + args.eventId, + outcome.status, + outcome.error + ); + return { status: 200, code: 'ok' }; +} + +export const ingest_resend_webhook = spacetimedb.reducer( + { + eventId: t.string(), + eventType: t.string(), + payloadJson: t.string(), + signatureHeader: t.option(t.string()), + timestampHeader: t.option(t.string()), + }, + (ctx, args) => { + const result = ingestResendWebhook(ctx, args); + if (result.status !== 200) throwSenderError(result.code); + } +); + +function webhookJson(body: unknown, status: number): SyncResponse { + return new SyncResponse(JSON.stringify(body), { + status, + headers: { 'content-type': 'application/json' }, + }); +} + +// Native SpacetimeDB HTTP route handler. Host modules register it on a router so +// Resend can post directly to the database. +export function makeResendWebhookHandler() { + // The host passes the submodule-scoped context, so this handler remains schema-agnostic. + return function resendWebhook( + ctx: HandlerContext, + req: Request + ): SyncResponse { + if (req.method.toUpperCase() !== 'POST') { + return webhookJson({ error: 'method_not_allowed' }, 405); + } + + const rawBody = req.text(); + const svixId = req.headers.get('svix-id') ?? ''; + const svixTimestamp = req.headers.get('svix-timestamp') ?? undefined; + const svixSignature = req.headers.get('svix-signature') ?? undefined; + if (!svixId) return webhookJson({ error: 'missing_svix_id' }, 400); + + let eventType: string | undefined; + const parsed = safeJsonParse(rawBody); + if ( + parsed && + typeof parsed === 'object' && + typeof (parsed as { type?: unknown }).type === 'string' + ) { + eventType = (parsed as { type: string }).type; + } + if (!eventType) return webhookJson({ error: 'missing_event_type' }, 400); + + const result = ctx.withTx(tx => + ingestResendWebhook(tx as WriteCtx, { + eventId: svixId, + eventType, + payloadJson: rawBody, + signatureHeader: svixSignature, + timestampHeader: svixTimestamp, + }) + ); + return webhookJson( + { ok: result.status === 200, code: result.code }, + result.status + ); + }; +} + +export const replay_webhook_event = spacetimedb.reducer( + { eventId: t.string() }, + (ctx, { eventId }) => { + // Administrators may run this operation over stored events. + requireAdmin(ctx, ctx.sender); + const event = ctx.db.resendWebhookEvent.eventId.find(eventId); + if (!event) throwSenderError(`resend.webhook_event_not_found:${eventId}`); + const outcome = applyResendEvent(ctx, eventId, event.payloadJson); + updateWebhookStatus(ctx, eventId, outcome.status, outcome.error); + } +); diff --git a/spacetime-resend-ts/tsconfig.json b/spacetime-resend-ts/tsconfig.json new file mode 100644 index 00000000000..c659d97428a --- /dev/null +++ b/spacetime-resend-ts/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "strict": true, + "declaration": false, + "emitDeclarationOnly": false, + "noEmit": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "allowImportingTsExtensions": true, + "noImplicitAny": true, + "moduleResolution": "Bundler", + "isolatedDeclarations": false, + "esModuleInterop": false, + "allowSyntheticDefaultImports": false, + "useDefineForClassFields": true, + "isolatedModules": true + }, + "include": ["src/**/*.ts", "scripts/**/*.ts"], + "exclude": ["node_modules", "dist/**/*"] +} From 74cddb0db07439146878a13011e83d0b9e303c68 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Wed, 9 Sep 2026 14:18:05 -0400 Subject: [PATCH 02/18] Use Valibot results directly in Resend --- spacetime-resend-ts/src/submodule/http.ts | 13 +++++++------ .../src/submodule/operations.ts | 19 +++++++------------ .../src/submodule/validation.ts | 13 ------------- spacetime-resend-ts/src/submodule/webhooks.ts | 8 ++++---- 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/spacetime-resend-ts/src/submodule/http.ts b/spacetime-resend-ts/src/submodule/http.ts index 9d60bcef084..aa10b491124 100644 --- a/spacetime-resend-ts/src/submodule/http.ts +++ b/spacetime-resend-ts/src/submodule/http.ts @@ -1,5 +1,6 @@ +import * as v from 'valibot'; import { type ProcedureModuleCtx, vResendErrorBody } from './schema'; -import { parseWithSchema, safeJsonParse, throwSenderError } from './validation'; +import { safeJsonParse, throwSenderError } from './validation'; import { buildResendHttpRequest } from './request'; export type ResendHttpResponse = { @@ -48,13 +49,13 @@ export function ensureOkOrThrow( export function resendErrorSuffix(body: string): string { const parsed = safeJsonParse(body); if (parsed !== undefined) { - const result = parseWithSchema(vResendErrorBody, parsed); - if (result.kind === 'success') { + const result = v.safeParse(vResendErrorBody, parsed); + if (result.success) { const parts: string[] = []; - if (result.data.name) parts.push(`name=${result.data.name}`); - if (result.data.message) { + if (result.output.name) parts.push(`name=${result.output.name}`); + if (result.output.message) { parts.push( - `msg=${result.data.message.replace(/\s+/g, ' ').slice(0, 240)}` + `msg=${result.output.message.replace(/\s+/g, ' ').slice(0, 240)}` ); } if (parts.length > 0) return `:${parts.join('|')}`; diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts index 1e7f16a6f54..e2c5a2969c7 100644 --- a/spacetime-resend-ts/src/submodule/operations.ts +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -12,12 +12,7 @@ import { type WriteCtx, } from './schema'; import { callResend, ensureOkOrThrow } from './http'; -import { - parseWithSchema, - safeJsonParse, - summarizeIssues, - throwSenderError, -} from './validation'; +import { safeJsonParse, summarizeIssues, throwSenderError } from './validation'; import { upsertEmail } from './email_writes'; import { loadConfigOrThrowFromProcedure } from './config'; import { adminVerdict, denyIfNotAdmin } from './auth'; @@ -51,11 +46,11 @@ function extractTagFieldsFromJson(tagsJson: string | undefined): { if (!tagsJson) return { userId: undefined, orgId: undefined }; const parsed = safeJsonParse(tagsJson); if (parsed === undefined) return { userId: undefined, orgId: undefined }; - const result = parseWithSchema(vTagsForExtraction, parsed); - if (result.kind === 'error') { + const result = v.safeParse(vTagsForExtraction, parsed); + if (!result.success) { return { userId: undefined, orgId: undefined }; } - const tags = result.data; + const tags = result.output; if (Array.isArray(tags)) { let userId: string | undefined; let orgId: string | undefined; @@ -214,14 +209,14 @@ export function sendEmail(ctx: ProcedureModuleCtx, args: SendEmailArgs) { const parsed = safeJsonParse(response.body); if (parsed === undefined) throwSenderError('resend.send_email_invalid_response'); - const result = parseWithSchema(vSendEmailResponse, parsed); - if (result.kind === 'error') { + const result = v.safeParse(vSendEmailResponse, parsed); + if (!result.success) { throwSenderError( `resend.send_email_invalid_response:${summarizeIssues(result.issues)}` ); } - const resendId = result.data.id; + const resendId = result.output.id; ctx.withTx(tx => { recordQueuedEmail(tx, ctx.timestamp, { resendId, diff --git a/spacetime-resend-ts/src/submodule/validation.ts b/spacetime-resend-ts/src/submodule/validation.ts index e6947be4bc1..181f0f5e9ad 100644 --- a/spacetime-resend-ts/src/submodule/validation.ts +++ b/spacetime-resend-ts/src/submodule/validation.ts @@ -1,19 +1,6 @@ import * as v from 'valibot'; import { SenderError } from 'spacetimedb/server'; -export type ParseResult = - | { kind: 'success'; data: T } - | { kind: 'error'; issues: v.BaseIssue[] }; - -export function parseWithSchema( - schema: TSchema, - input: unknown -): ParseResult> { - const result = v.safeParse(schema, input); - if (result.success) return { kind: 'success', data: result.output }; - return { kind: 'error', issues: result.issues }; -} - export function assertExhaustive(value: never): never { throw new Error(`Unhandled discriminant: ${value as string}`); } diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index b43fcd61900..f76ed45c114 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -1,3 +1,4 @@ +import * as v from 'valibot'; import { EmailStatus, WebhookEventStatus, @@ -21,7 +22,6 @@ import { } from 'spacetimedb/server'; import { assertExhaustive, - parseWithSchema, safeJsonParse, summarizeIssues, throwSenderError, @@ -209,15 +209,15 @@ function applyResendEvent( return { status: WebhookEventStatus.Failed, error: 'invalid JSON payload' }; } - const result = parseWithSchema(vEmailEvent, parsed); - if (result.kind === 'error') { + const result = v.safeParse(vEmailEvent, parsed); + if (!result.success) { return { status: WebhookEventStatus.Failed, error: `payload validation failed: ${summarizeIssues(result.issues)}`, }; } - const event = result.data; + const event = result.output; const now = ctx.timestamp; upsertEmail(ctx, now, makeEmailUpsertArgs(event, now)); recordDeliveryEvent(ctx, now, { From dea2d04af8ff3f399d5d80b339ba5ace8f571023 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 09:58:06 -0400 Subject: [PATCH 03/18] Use shared Resend license without changing the npm package license --- spacetime-resend-ts/LICENSE.txt | 202 +------------------------------ spacetime-resend-ts/package.json | 4 +- 2 files changed, 4 insertions(+), 202 deletions(-) mode change 100644 => 120000 spacetime-resend-ts/LICENSE.txt diff --git a/spacetime-resend-ts/LICENSE.txt b/spacetime-resend-ts/LICENSE.txt deleted file mode 100644 index 4e61a2122e7..00000000000 --- a/spacetime-resend-ts/LICENSE.txt +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2025 Clockwork Labs, Inc - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/spacetime-resend-ts/LICENSE.txt b/spacetime-resend-ts/LICENSE.txt new file mode 120000 index 00000000000..cc1ff9bdd72 --- /dev/null +++ b/spacetime-resend-ts/LICENSE.txt @@ -0,0 +1 @@ +../licenses/apache2.txt \ No newline at end of file diff --git a/spacetime-resend-ts/package.json b/spacetime-resend-ts/package.json index 8115e033977..de90701b640 100644 --- a/spacetime-resend-ts/package.json +++ b/spacetime-resend-ts/package.json @@ -18,7 +18,7 @@ }, "files": [ "src", - "LICENSE.txt", + "LICENSE", "README.md" ], "publishConfig": { @@ -41,6 +41,8 @@ "webhooks" ], "scripts": { + "prepack": "node -e \"require('node:fs').copyFileSync('../licenses/apache2.txt', 'LICENSE')\"", + "postpack": "node -e \"require('node:fs').unlinkSync('LICENSE')\"", "build": "spacetime build", "format": "prettier . --write --ignore-path ../.prettierignore", "lint": "eslint . && prettier . --check --ignore-path ../.prettierignore", From 70ae72c771a7d7ef3f360a6dc41b12f889c6f268 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:13:13 -0400 Subject: [PATCH 04/18] Export Resend error codes as constants Throw codes from a shared errors object instead of ad hoc resend.* strings. --- spacetime-resend-ts/src/submodule.ts | 1 + spacetime-resend-ts/src/submodule/auth.ts | 7 ++- spacetime-resend-ts/src/submodule/config.ts | 3 +- .../src/submodule/email-input.ts | 62 ++++++++++++------- spacetime-resend-ts/src/submodule/errors.ts | 44 +++++++++++++ spacetime-resend-ts/src/submodule/http.ts | 3 +- .../src/submodule/operations.ts | 14 ++--- spacetime-resend-ts/src/submodule/request.ts | 13 ++-- spacetime-resend-ts/src/submodule/webhooks.ts | 17 ++--- 9 files changed, 115 insertions(+), 49 deletions(-) create mode 100644 spacetime-resend-ts/src/submodule/errors.ts diff --git a/spacetime-resend-ts/src/submodule.ts b/spacetime-resend-ts/src/submodule.ts index 56285175084..05282dba0a6 100644 --- a/spacetime-resend-ts/src/submodule.ts +++ b/spacetime-resend-ts/src/submodule.ts @@ -5,6 +5,7 @@ export { t, } from './submodule/schema'; export { install } from './submodule/install'; +export { errors } from './submodule/errors'; export { ingestResendWebhook, replayWebhookEvent, diff --git a/spacetime-resend-ts/src/submodule/auth.ts b/spacetime-resend-ts/src/submodule/auth.ts index 8e1ae16e708..21ec303a4a1 100644 --- a/spacetime-resend-ts/src/submodule/auth.ts +++ b/spacetime-resend-ts/src/submodule/auth.ts @@ -5,6 +5,7 @@ import { type WriteCtx, } from './schema'; import { throwSenderError } from './validation'; +import { errors } from './errors'; // Admin gate. Fresh publishes seed the owner via init. Public submodule calls // never bootstrap admin state from "first caller wins". Procedure callers must @@ -23,11 +24,11 @@ export function adminVerdict(ctx: WriteCtx, sender: Sender): AdminVerdict { } export function denyIfNotAdmin(verdict: AdminVerdict): void { - if (verdict === 'denied') throwSenderError('resend.not_authorized'); + if (verdict === 'denied') throwSenderError(errors.notAuthorized); } export function requireAdmin(ctx: WriteCtx, sender: Sender): void { - if (!isAdmin(ctx, sender)) throwSenderError('resend.not_authorized'); + if (!isAdmin(ctx, sender)) throwSenderError(errors.notAuthorized); } // For owner-gated repair/setup code only. Do not call from a public bootstrap path. @@ -71,7 +72,7 @@ export const removeAdminIdentity = spacetimedb.procedure( const existing = tx.db.resendAdminIdentity.identity.find(identity); if (!existing) return; if (tx.db.resendAdminIdentity.count() <= 1n) { - throwSenderError('resend.cannot_remove_last_admin'); + throwSenderError(errors.cannotRemoveLastAdmin); } tx.db.resendAdminIdentity.delete(existing); }); diff --git a/spacetime-resend-ts/src/submodule/config.ts b/spacetime-resend-ts/src/submodule/config.ts index a868ef6e80f..8cbfd33e25f 100644 --- a/spacetime-resend-ts/src/submodule/config.ts +++ b/spacetime-resend-ts/src/submodule/config.ts @@ -6,6 +6,7 @@ import { } from './schema'; import { adminVerdict, denyIfNotAdmin } from './auth'; import { throwSenderError } from './validation'; +import { errors } from './errors'; export type ResendConfig = { apiKey: string; @@ -17,7 +18,7 @@ export function loadConfigOrThrow(ctx: WriteCtx): ResendConfig { const row = ctx.db.resendConfig.singleton.find(true); if (!row) { throwSenderError( - 'resend.config_not_set: call set_resend_config(...) first' + `${errors.configNotSet}: call set_resend_config(...) first` ); } return { diff --git a/spacetime-resend-ts/src/submodule/email-input.ts b/spacetime-resend-ts/src/submodule/email-input.ts index c61608ff2a6..5aeac640661 100644 --- a/spacetime-resend-ts/src/submodule/email-input.ts +++ b/spacetime-resend-ts/src/submodule/email-input.ts @@ -1,3 +1,4 @@ +import { errors } from './errors'; import { hasControlCharacter } from './text-validation'; export type EmailInput = { @@ -15,76 +16,91 @@ export type EmailInput = { }; function fail(code: string): never { - throw new Error(`resend.${code}`); + throw new Error(code); } function validateAddressList( values: string[] | undefined, - field: string + tooMany: string, + invalidAddress: string ): void { if (values === undefined) return; - if (values.length > 100) fail(`${field}_too_many`); + if (values.length > 100) fail(tooMany); for (const value of values) { if ( value.length === 0 || value.length > 320 || hasControlCharacter(value) ) { - fail(`${field}_invalid_address`); + fail(invalidAddress); } } } function validateJson( value: string | undefined, - field: string, + tooLarge: string, + invalidJson: string, maxLength: number ): void { if (value === undefined) return; - if (value.length > maxLength) fail(`${field}_too_large`); + if (value.length > maxLength) fail(tooLarge); let parsed: unknown; try { parsed = JSON.parse(value); } catch { - fail(`${field}_invalid_json`); + fail(invalidJson); } - if (parsed === null || typeof parsed !== 'object') - fail(`${field}_invalid_json`); + if (parsed === null || typeof parsed !== 'object') fail(invalidJson); } export function validateEmailInput(args: EmailInput): void { - if (args.to.length === 0) fail('send_email_no_recipients'); - validateAddressList(args.to, 'to'); - validateAddressList(args.cc, 'cc'); - validateAddressList(args.bcc, 'bcc'); - validateAddressList(args.replyTo, 'reply_to'); + if (args.to.length === 0) fail(errors.sendEmailNoRecipients); + validateAddressList(args.to, errors.toTooMany, errors.toInvalidAddress); + validateAddressList(args.cc, errors.ccTooMany, errors.ccInvalidAddress); + validateAddressList(args.bcc, errors.bccTooMany, errors.bccInvalidAddress); + validateAddressList( + args.replyTo, + errors.replyToTooMany, + errors.replyToInvalidAddress + ); const recipientCount = args.to.length + (args.cc?.length ?? 0) + (args.bcc?.length ?? 0); - if (recipientCount > 100) fail('send_email_too_many_recipients'); + if (recipientCount > 100) fail(errors.sendEmailTooManyRecipients); if ( args.from !== undefined && (args.from.length === 0 || args.from.length > 320 || hasControlCharacter(args.from)) ) - fail('send_email_invalid_from'); + fail(errors.sendEmailInvalidFrom); if ( args.subject.length === 0 || args.subject.length > 998 || hasControlCharacter(args.subject) ) { - fail('send_email_invalid_subject'); + fail(errors.sendEmailInvalidSubject); } if (args.html === undefined && args.text === undefined) - fail('send_email_missing_content'); - if ((args.html?.length ?? 0) > 200_000) fail('send_email_html_too_large'); - if ((args.text?.length ?? 0) > 200_000) fail('send_email_text_too_large'); - validateJson(args.tagsJson, 'tags', 16_384); - validateJson(args.headersJson, 'headers', 16_384); + fail(errors.sendEmailMissingContent); + if ((args.html?.length ?? 0) > 200_000) fail(errors.sendEmailHtmlTooLarge); + if ((args.text?.length ?? 0) > 200_000) fail(errors.sendEmailTextTooLarge); + validateJson( + args.tagsJson, + errors.tagsTooLarge, + errors.tagsInvalidJson, + 16_384 + ); + validateJson( + args.headersJson, + errors.headersTooLarge, + errors.headersInvalidJson, + 16_384 + ); if ( (args.scheduledAt?.length ?? 0) > 128 || hasControlCharacter(args.scheduledAt ?? '') ) { - fail('send_email_invalid_schedule'); + fail(errors.sendEmailInvalidSchedule); } } diff --git a/spacetime-resend-ts/src/submodule/errors.ts b/spacetime-resend-ts/src/submodule/errors.ts new file mode 100644 index 00000000000..20afc973521 --- /dev/null +++ b/spacetime-resend-ts/src/submodule/errors.ts @@ -0,0 +1,44 @@ +export const errors = { + notAuthorized: 'resend.not_authorized', + cannotRemoveLastAdmin: 'resend.cannot_remove_last_admin', + configNotSet: 'resend.config_not_set', + requestInvalid: 'resend.request_invalid', + requestPathInvalid: 'resend.request_path_invalid', + requestMethodInvalid: 'resend.request_method_invalid', + requestBodyTooLarge: 'resend.request_body_too_large', + idempotencyKeyTooLong: 'resend.idempotency_key_too_long', + sendEmailInvalidInput: 'resend.send_email_invalid_input', + sendEmailNoRecipients: 'resend.send_email_no_recipients', + sendEmailTooManyRecipients: 'resend.send_email_too_many_recipients', + sendEmailInvalidFrom: 'resend.send_email_invalid_from', + sendEmailInvalidSubject: 'resend.send_email_invalid_subject', + sendEmailMissingContent: 'resend.send_email_missing_content', + sendEmailHtmlTooLarge: 'resend.send_email_html_too_large', + sendEmailTextTooLarge: 'resend.send_email_text_too_large', + sendEmailInvalidSchedule: 'resend.send_email_invalid_schedule', + sendEmailMissingFrom: 'resend.send_email_missing_from', + sendEmailFailed: 'resend.send_email_failed', + sendEmailInvalidResponse: 'resend.send_email_invalid_response', + toTooMany: 'resend.to_too_many', + toInvalidAddress: 'resend.to_invalid_address', + ccTooMany: 'resend.cc_too_many', + ccInvalidAddress: 'resend.cc_invalid_address', + bccTooMany: 'resend.bcc_too_many', + bccInvalidAddress: 'resend.bcc_invalid_address', + replyToTooMany: 'resend.reply_to_too_many', + replyToInvalidAddress: 'resend.reply_to_invalid_address', + tagsTooLarge: 'resend.tags_too_large', + tagsInvalidJson: 'resend.tags_invalid_json', + headersTooLarge: 'resend.headers_too_large', + headersInvalidJson: 'resend.headers_invalid_json', + cancelEmailFailed: 'resend.cancel_email_failed', + webhookMethodNotAllowed: 'resend.webhook_method_not_allowed', + webhookMetadataInvalid: 'resend.webhook_metadata_invalid', + webhookMetadataMismatch: 'resend.webhook_metadata_mismatch', + webhookPayloadTooLarge: 'resend.webhook_payload_too_large', + webhookHeaderTooLarge: 'resend.webhook_header_too_large', + webhookSecretNotConfigured: 'resend.webhook_secret_not_configured', + webhookSignatureMismatch: 'resend.webhook_signature_mismatch', + webhookPayloadInvalid: 'resend.webhook_payload_invalid', + webhookEventNotFound: 'resend.webhook_event_not_found', +} as const; diff --git a/spacetime-resend-ts/src/submodule/http.ts b/spacetime-resend-ts/src/submodule/http.ts index aa10b491124..5dd1909cf40 100644 --- a/spacetime-resend-ts/src/submodule/http.ts +++ b/spacetime-resend-ts/src/submodule/http.ts @@ -2,6 +2,7 @@ import * as v from 'valibot'; import { type ProcedureModuleCtx, vResendErrorBody } from './schema'; import { safeJsonParse, throwSenderError } from './validation'; import { buildResendHttpRequest } from './request'; +import { errors } from './errors'; export type ResendHttpResponse = { status: number; @@ -23,7 +24,7 @@ export function callResend( request = buildResendHttpRequest(args); } catch (error) { throwSenderError( - error instanceof Error ? error.message : 'resend.request_invalid' + error instanceof Error ? error.message : errors.requestInvalid ); } const response = ctx.http.fetch(request.url, { diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts index 02d4e1e19f0..f6a3a52f03c 100644 --- a/spacetime-resend-ts/src/submodule/operations.ts +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -17,6 +17,7 @@ import { upsertEmail } from './email_writes'; import { loadConfigOrThrowFromProcedure } from './config'; import { adminVerdict, denyIfNotAdmin } from './auth'; import { validateEmailInput } from './email-input'; +import { errors } from './errors'; function requireProcedureAdmin(ctx: ProcedureModuleCtx): void { const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); @@ -176,12 +177,12 @@ export function sendEmailRequest(ctx: ProcedureModuleCtx, args: SendEmailArgs) { validateEmailInput(args); } catch (error) { throwSenderError( - error instanceof Error ? error.message : 'resend.send_email_invalid_input' + error instanceof Error ? error.message : errors.sendEmailInvalidInput ); } const cfg = loadConfigOrThrowFromProcedure(ctx); const fromAddress = args.from ?? cfg.defaultFrom; - if (!fromAddress) throwSenderError('resend.send_email_missing_from'); + if (!fromAddress) throwSenderError(errors.sendEmailMissingFrom); const jsonBody = buildSendEmailBody({ from: fromAddress, @@ -204,15 +205,14 @@ export function sendEmailRequest(ctx: ProcedureModuleCtx, args: SendEmailArgs) { jsonBody, idempotencyKey: args.idempotencyKey, }); - ensureOkOrThrow(response, 'resend.send_email_failed'); + ensureOkOrThrow(response, errors.sendEmailFailed); const parsed = safeJsonParse(response.body); - if (parsed === undefined) - throwSenderError('resend.send_email_invalid_response'); + if (parsed === undefined) throwSenderError(errors.sendEmailInvalidResponse); const result = v.safeParse(vSendEmailResponse, parsed); if (!result.success) { throwSenderError( - `resend.send_email_invalid_response:${summarizeIssues(result.issues)}` + `${errors.sendEmailInvalidResponse}:${summarizeIssues(result.issues)}` ); } @@ -270,7 +270,7 @@ export const cancelEmail = spacetimedb.procedure( jsonBody: undefined, idempotencyKey: undefined, }); - ensureOkOrThrow(response, 'resend.cancel_email_failed'); + ensureOkOrThrow(response, errors.cancelEmailFailed); ctx.withTx(tx => { const existing = tx.db.resendEmail.resendId.find(args.resendId); diff --git a/spacetime-resend-ts/src/submodule/request.ts b/spacetime-resend-ts/src/submodule/request.ts index b7c067c77b6..ea445336611 100644 --- a/spacetime-resend-ts/src/submodule/request.ts +++ b/spacetime-resend-ts/src/submodule/request.ts @@ -1,3 +1,4 @@ +import { errors } from './errors'; import { hasControlCharacter } from './text-validation'; const RESEND_API_BASE = 'https://api.resend.com'; @@ -16,13 +17,13 @@ export type ResendHttpRequest = { function validatePath(path: string): string { const normalized = path.trim(); if (!normalized.startsWith('/') || normalized.startsWith('//')) { - throw new Error('resend.request_path_invalid'); + throw new Error(errors.requestPathInvalid); } if (normalized.includes('\\') || normalized.includes('#')) { - throw new Error('resend.request_path_invalid'); + throw new Error(errors.requestPathInvalid); } if (normalized.length > MAX_PATH_LENGTH || hasControlCharacter(normalized)) { - throw new Error('resend.request_path_invalid'); + throw new Error(errors.requestPathInvalid); } return normalized; } @@ -36,19 +37,19 @@ export function buildResendHttpRequest(args: { }): ResendHttpRequest { const method = args.method.trim().toUpperCase(); if (!ALLOWED_METHODS.has(method)) { - throw new Error('resend.request_method_invalid'); + throw new Error(errors.requestMethodInvalid); } const path = validatePath(args.path); const body = args.jsonBody?.length ? args.jsonBody : undefined; if (body !== undefined && body.length > MAX_JSON_BODY_LENGTH) { - throw new Error('resend.request_body_too_large'); + throw new Error(errors.requestBodyTooLarge); } if ( args.idempotencyKey && args.idempotencyKey.length > MAX_IDEMPOTENCY_KEY_LENGTH ) { - throw new Error('resend.idempotency_key_too_long'); + throw new Error(errors.idempotencyKeyTooLong); } const headers: Record = { diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index 18666f5f958..0b7c354c236 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -28,6 +28,7 @@ import { throwSenderError, } from './validation'; import { parseResendEventType } from './webhook-metadata'; +import { errors } from './errors'; type ResendTags = EmailEvent['data']['tags']; @@ -260,21 +261,21 @@ function applyResendWebhook( args.eventType.length === 0 || args.eventType.length > MAX_WEBHOOK_METADATA_LENGTH ) { - return { status: 400, code: 'resend.webhook_metadata_invalid' }; + return { status: 400, code: errors.webhookMetadataInvalid }; } if (args.payloadJson.length > MAX_WEBHOOK_BODY_LENGTH) { - return { status: 413, code: 'resend.webhook_payload_too_large' }; + return { status: 413, code: errors.webhookPayloadTooLarge }; } if ( (args.signatureHeader?.length ?? 0) > MAX_WEBHOOK_HEADER_LENGTH || (args.timestampHeader?.length ?? 0) > MAX_WEBHOOK_HEADER_LENGTH ) { - return { status: 400, code: 'resend.webhook_header_too_large' }; + return { status: 400, code: errors.webhookHeaderTooLarge }; } const cfg = ctx.db.resendConfig.singleton.find(true); if (!cfg?.webhookSigningSecret) { - return { status: 500, code: 'resend.webhook_secret_not_configured' }; + return { status: 500, code: errors.webhookSecretNotConfigured }; } const nowSeconds = Number(ctx.timestamp.microsSinceUnixEpoch / 1_000_000n); const sigOk = verifySvixSignature({ @@ -285,11 +286,11 @@ function applyResendWebhook( secret: cfg.webhookSigningSecret, nowSeconds, }); - if (!sigOk) return { status: 401, code: 'resend.webhook_signature_mismatch' }; + if (!sigOk) return { status: 401, code: errors.webhookSignatureMismatch }; const signedEventType = parseResendEventType(args.payloadJson); if (!signedEventType || signedEventType !== args.eventType) { - return { status: 400, code: 'resend.webhook_metadata_mismatch' }; + return { status: 400, code: errors.webhookMetadataMismatch }; } const existing = ctx.db.resendWebhookEvent.eventId.find(args.eventId); @@ -323,7 +324,7 @@ function applyResendWebhook( ); return outcome.status.tag === 'Processed' ? { status: 200, code: 'ok' } - : { status: 400, code: 'resend.webhook_payload_invalid' }; + : { status: 400, code: errors.webhookPayloadInvalid }; } export const ingestResendWebhook = spacetimedb.reducer( @@ -398,7 +399,7 @@ export const replayWebhookEvent = spacetimedb.reducer( // Administrators may run this operation over stored events. requireAdmin(ctx, ctx.sender); const event = ctx.db.resendWebhookEvent.eventId.find(eventId); - if (!event) throwSenderError(`resend.webhook_event_not_found:${eventId}`); + if (!event) throwSenderError(`${errors.webhookEventNotFound}:${eventId}`); const outcome = applyResendEvent(ctx, eventId, event.payloadJson); updateWebhookStatus(ctx, eventId, outcome.status, outcome.error); } From 53456289f156c0bad029ba02bb85282e8dc389cb Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:13:40 -0400 Subject: [PATCH 05/18] Remove unused Resend code Drop seedAdmin, the ResendHttpResponse type, two unqueried indexes, and webhook branches for array tags and from/to shapes that the event schema never accepts. --- spacetime-resend-ts/src/submodule/auth.ts | 14 -------------- spacetime-resend-ts/src/submodule/schema.ts | 11 ----------- spacetime-resend-ts/src/submodule/webhooks.ts | 19 ++----------------- 3 files changed, 2 insertions(+), 42 deletions(-) diff --git a/spacetime-resend-ts/src/submodule/auth.ts b/spacetime-resend-ts/src/submodule/auth.ts index 21ec303a4a1..905a36ec432 100644 --- a/spacetime-resend-ts/src/submodule/auth.ts +++ b/spacetime-resend-ts/src/submodule/auth.ts @@ -11,7 +11,6 @@ import { errors } from './errors'; // never bootstrap admin state from "first caller wins". Procedure callers must // pass the outer ctx.sender explicitly; transaction ctx may not carry sender. type Sender = WriteCtx['sender']; -type ModuleTimestamp = WriteCtx['timestamp']; export type AdminVerdict = 'admin' | 'denied'; @@ -31,19 +30,6 @@ export function requireAdmin(ctx: WriteCtx, sender: Sender): void { if (!isAdmin(ctx, sender)) throwSenderError(errors.notAuthorized); } -// For owner-gated repair/setup code only. Do not call from a public bootstrap path. -export function seedAdmin( - ctx: WriteCtx, - sender: Sender, - timestamp: ModuleTimestamp -) { - if (ctx.db.resendAdminIdentity.identity.find(sender) != null) return; - ctx.db.resendAdminIdentity.insert({ - identity: sender, - addedAtMicros: timestamp.microsSinceUnixEpoch, - }); -} - export const addAdminIdentity = spacetimedb.procedure( { identity: t.identity() }, t.unit(), diff --git a/spacetime-resend-ts/src/submodule/schema.ts b/spacetime-resend-ts/src/submodule/schema.ts index 8fc517c61ca..9abecb45ef2 100644 --- a/spacetime-resend-ts/src/submodule/schema.ts +++ b/spacetime-resend-ts/src/submodule/schema.ts @@ -123,7 +123,6 @@ export const resendEmailTable = table( { accessor: 'byUserId', algorithm: 'btree', columns: ['userId'] }, { accessor: 'byOrgId', algorithm: 'btree', columns: ['orgId'] }, { accessor: 'byStatus', algorithm: 'btree', columns: ['status'] }, - { accessor: 'byUpdatedAt', algorithm: 'btree', columns: ['updatedAt'] }, ], }, resendEmailRow @@ -135,11 +134,6 @@ export const resendDeliveryEventTable = table( public: false, indexes: [ { accessor: 'byResendId', algorithm: 'btree', columns: ['resendId'] }, - { - accessor: 'byResendIdEventType', - algorithm: 'btree', - columns: ['resendId', 'eventType'], - }, ], }, resendDeliveryEventRow @@ -194,11 +188,6 @@ export const sendEmailResult = t.object('SendEmailResult', { resendId: t.string(), }); -export const resendHttpResponse = t.object('ResendHttpResponse', { - status: t.u16(), - body: t.string(), -}); - export { Range, SenderError, t }; // Mirror Resend SDK's BaseEmailEventData. diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index 0b7c354c236..b204bde4d1a 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -32,10 +32,6 @@ import { errors } from './errors'; type ResendTags = EmailEvent['data']['tags']; -function toAddressArray(value: string | string[]): string[] { - return Array.isArray(value) ? value : [value]; -} - function tagsToJson(tags: ResendTags): string | undefined { if (!tags) return undefined; try { @@ -50,15 +46,6 @@ function extractTagFields(tags: ResendTags): { orgId: string | undefined; } { if (!tags) return { userId: undefined, orgId: undefined }; - if (Array.isArray(tags)) { - let userId: string | undefined; - let orgId: string | undefined; - for (const tag of tags) { - if (tag.name === 'userId') userId = tag.value; - if (tag.name === 'orgId') orgId = tag.value; - } - return { userId, orgId }; - } return { userId: tags['userId'], orgId: tags['orgId'] }; } @@ -109,16 +96,14 @@ function makeEmailUpsertArgs( now: ModuleTimestamp ): Parameters[2] { const data = event.data; - const fromAddress = Array.isArray(data.from) ? data.from[0]! : data.from; - const toAddresses = toAddressArray(data.to); const tagFields = extractTagFields(data.tags); const tagsJson = tagsToJson(data.tags); const subject = data.subject; const base = { resendId: data.email_id, - fromAddress, - toAddressesJson: JSON.stringify(toAddresses), + fromAddress: data.from, + toAddressesJson: JSON.stringify(data.to), subject, // undefined preserves whatever send_email recorded. html: undefined, From 24e582d20401da2176a58b4384ec80302a4575ad Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:14:04 -0400 Subject: [PATCH 06/18] Acknowledge unhandled Resend webhook event types Validly signed events outside the eight tracked email.* types are stored as Ignored and return 200, so Resend stops retrying them. The HTTP route no longer parses the body to derive an event type that it then compares against the same body; only the reducer checks its eventType argument against the signed payload. --- .../scripts/test-resend-smoke.ts | 12 +++ spacetime-resend-ts/src/submodule/schema.ts | 2 +- spacetime-resend-ts/src/submodule/webhooks.ts | 80 ++++++++++--------- 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/spacetime-resend-ts/scripts/test-resend-smoke.ts b/spacetime-resend-ts/scripts/test-resend-smoke.ts index 1ec88272b61..476a908f92f 100644 --- a/spacetime-resend-ts/scripts/test-resend-smoke.ts +++ b/spacetime-resend-ts/scripts/test-resend-smoke.ts @@ -310,6 +310,18 @@ async function main() { ); } + step('signed event types the submodule does not handle are acknowledged'); + await ingestWebhook( + opts, + evt('contact'), + 'contact.created', + JSON.stringify({ + type: 'contact.created', + created_at: '2026-05-04T00:00:00Z', + data: { id: 'contact_smoke' }, + }) + ); + // Email A happy path: queued -> sent -> delivered, then flag overlays must not roll status back. step(`ingest email.sent for ${em('a')}`); await ingestWebhook( diff --git a/spacetime-resend-ts/src/submodule/schema.ts b/spacetime-resend-ts/src/submodule/schema.ts index 9abecb45ef2..5881f97f9d2 100644 --- a/spacetime-resend-ts/src/submodule/schema.ts +++ b/spacetime-resend-ts/src/submodule/schema.ts @@ -12,7 +12,7 @@ import * as v from 'valibot'; import { install } from './install'; // Webhook delivery state. Received = ingest accepted. Processed = applied. -// Ignored = duplicate/unknown event type. Failed = signature/format error. +// Ignored = event type this submodule does not handle. Failed = invalid payload. export const webhookEventStatus = t.enum('WebhookEventStatus', [ 'Received', 'Processed', diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index b204bde4d1a..d1e38ed1c0d 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -80,7 +80,7 @@ function updateWebhookStatus( const existing = ctx.db.resendWebhookEvent.eventId.find(eventId); if (!existing) return; - const isTerminal = status.tag === 'Processed' || status.tag === 'Failed'; + const isTerminal = status.tag !== 'Received'; const updated = { ...existing, status, @@ -181,6 +181,14 @@ function detailJsonForEvent(event: EmailEvent): string | undefined { } } +// Resend also sends event types this submodule does not track, such as +// `contact.*`, `domain.*`, and `email.received`. Those are acknowledged as Ignored. +const vHandledEventType = v.object({ + type: v.picklist( + vEmailEvent.options.map(option => option.entries.type.literal) + ), +}); + function applyResendEvent( ctx: ReducerModuleCtx, eventId: string, @@ -193,6 +201,9 @@ function applyResendEvent( const result = v.safeParse(vEmailEvent, parsed); if (!result.success) { + if (!v.is(vHandledEventType, parsed)) { + return { status: WebhookEventStatus.Ignored, error: undefined }; + } return { status: WebhookEventStatus.Failed, error: `payload validation failed: ${summarizeIssues(result.issues)}`, @@ -238,13 +249,11 @@ const MAX_WEBHOOK_METADATA_LENGTH = 255; // The reducer and HTTP route verify, store, and apply events in one transaction. function applyResendWebhook( ctx: WriteCtx, - args: ResendWebhookIngestArgs + args: Omit ): { status: number; code: string } { if ( args.eventId.length === 0 || - args.eventId.length > MAX_WEBHOOK_METADATA_LENGTH || - args.eventType.length === 0 || - args.eventType.length > MAX_WEBHOOK_METADATA_LENGTH + args.eventId.length > MAX_WEBHOOK_METADATA_LENGTH ) { return { status: 400, code: errors.webhookMetadataInvalid }; } @@ -274,12 +283,18 @@ function applyResendWebhook( if (!sigOk) return { status: 401, code: errors.webhookSignatureMismatch }; const signedEventType = parseResendEventType(args.payloadJson); - if (!signedEventType || signedEventType !== args.eventType) { - return { status: 400, code: errors.webhookMetadataMismatch }; + if ( + !signedEventType || + signedEventType.length > MAX_WEBHOOK_METADATA_LENGTH + ) { + return { status: 400, code: errors.webhookPayloadInvalid }; } const existing = ctx.db.resendWebhookEvent.eventId.find(args.eventId); - if (existing?.status.tag === 'Processed') { + if ( + existing?.status.tag === 'Processed' || + existing?.status.tag === 'Ignored' + ) { return { status: 200, code: 'ok' }; } @@ -307,9 +322,9 @@ function applyResendWebhook( outcome.status, outcome.error ); - return outcome.status.tag === 'Processed' - ? { status: 200, code: 'ok' } - : { status: 400, code: errors.webhookPayloadInvalid }; + return outcome.status.tag === 'Failed' + ? { status: 400, code: errors.webhookPayloadInvalid } + : { status: 200, code: 'ok' }; } export const ingestResendWebhook = spacetimedb.reducer( @@ -320,7 +335,10 @@ export const ingestResendWebhook = spacetimedb.reducer( signatureHeader: t.option(t.string()), timestampHeader: t.option(t.string()), }, - (ctx, args) => { + (ctx, { eventType, ...args }) => { + if (eventType !== parseResendEventType(args.payloadJson)) { + throwSenderError(errors.webhookMetadataMismatch); + } const result = applyResendWebhook(ctx, args); if (result.status !== 200) throwSenderError(result.code); } @@ -342,35 +360,19 @@ export function makeResendWebhookHandler() { req: Request ): SyncResponse { if (req.method.toUpperCase() !== 'POST') { - return webhookJson({ error: 'method_not_allowed' }, 405); - } - - const rawBody = req.text(); - const svixId = req.headers.get('svix-id') ?? ''; - const svixTimestamp = req.headers.get('svix-timestamp') ?? undefined; - const svixSignature = req.headers.get('svix-signature') ?? undefined; - if (!svixId) return webhookJson({ error: 'missing_svix_id' }, 400); - - let eventType: string | undefined; - const parsed = safeJsonParse(rawBody); - if ( - parsed && - typeof parsed === 'object' && - typeof (parsed as { type?: unknown }).type === 'string' - ) { - eventType = (parsed as { type: string }).type; + return webhookJson( + { ok: false, code: errors.webhookMethodNotAllowed }, + 405 + ); } - if (!eventType) return webhookJson({ error: 'missing_event_type' }, 400); - const result = ctx.withTx(tx => - applyResendWebhook(tx as WriteCtx, { - eventId: svixId, - eventType, - payloadJson: rawBody, - signatureHeader: svixSignature, - timestampHeader: svixTimestamp, - }) - ); + const args = { + eventId: req.headers.get('svix-id') ?? '', + payloadJson: req.text(), + signatureHeader: req.headers.get('svix-signature') ?? undefined, + timestampHeader: req.headers.get('svix-timestamp') ?? undefined, + }; + const result = ctx.withTx(tx => applyResendWebhook(tx as WriteCtx, args)); return webhookJson( { ok: result.status === 200, code: result.code }, result.status From 9d9ea65e3142241150b3a6f98e179c9f539b55ec Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:14:22 -0400 Subject: [PATCH 07/18] Keep Resend error details with the status that produced them A late email.failed after a bounce no longer writes its failure reason onto the Bounced row. --- .../scripts/test-resend-smoke.ts | 20 ++++++++++++++++++- .../src/submodule/email_writes.ts | 12 ++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/spacetime-resend-ts/scripts/test-resend-smoke.ts b/spacetime-resend-ts/scripts/test-resend-smoke.ts index 476a908f92f..ffb3b2f9ee8 100644 --- a/spacetime-resend-ts/scripts/test-resend-smoke.ts +++ b/spacetime-resend-ts/scripts/test-resend-smoke.ts @@ -438,7 +438,7 @@ async function main() { throw new Error(`expected bounce reason in row: ${bouncedRow}`); } - step('late sent and delayed events do not undo a bounce'); + step('late sent, delayed, and failed events do not undo a bounce'); for (const type of ['email.sent', 'email.delivery_delayed']) { await ingestWebhook( opts, @@ -454,6 +454,24 @@ async function main() { if (emailStatus(current) !== 'Bounced') throw new Error(`late ${type} changed bounce: ${current}`); } + await ingestWebhook( + opts, + evt('late_failed'), + 'email.failed', + eventPayload({ + type: 'email.failed', + emailId: em('c'), + extra: { failed: { reason: 'late failure' } }, + }) + ); + const afterLateFailure = await callReducer(opts, 'get_email', [ + quote(em('c')), + ]); + if ( + emailStatus(afterLateFailure) !== 'Bounced' || + afterLateFailure.includes('late failure') + ) + throw new Error(`late failure changed bounce: ${afterLateFailure}`); await callReducer(opts, 'replay_webhook_event', [ quote(evt('late_email.sent')), ]); diff --git a/spacetime-resend-ts/src/submodule/email_writes.ts b/spacetime-resend-ts/src/submodule/email_writes.ts index a6eff098072..c1f8fe100e2 100644 --- a/spacetime-resend-ts/src/submodule/email_writes.ts +++ b/spacetime-resend-ts/src/submodule/email_writes.ts @@ -57,6 +57,8 @@ export function upsertEmail( !existing.statusUpdatedAt || statusTime.microsSinceUnixEpoch >= existing.statusUpdatedAt.microsSinceUnixEpoch))); + // Error and bounce details describe the status that produced them. + const statusDetail = replaceStatus ? args : undefined; const row = { resendId: args.resendId, fromAddress: args.fromAddress, @@ -69,11 +71,11 @@ export function upsertEmail( ? args.status : (existing?.status ?? EmailStatus.Queued), statusUpdatedAt: replaceStatus ? statusTime : existing?.statusUpdatedAt, - lastError: args.lastError ?? existing?.lastError, - bouncedAt: args.bouncedAt ?? existing?.bouncedAt, - bounceJson: args.bounceJson ?? existing?.bounceJson, - failedAt: args.failedAt ?? existing?.failedAt, - failureReason: args.failureReason ?? existing?.failureReason, + lastError: statusDetail?.lastError ?? existing?.lastError, + bouncedAt: statusDetail?.bouncedAt ?? existing?.bouncedAt, + bounceJson: statusDetail?.bounceJson ?? existing?.bounceJson, + failedAt: statusDetail?.failedAt ?? existing?.failedAt, + failureReason: statusDetail?.failureReason ?? existing?.failureReason, complained: args.complained || (existing?.complained ?? false), complainedAt: args.complainedAt ?? existing?.complainedAt, opened: args.opened || (existing?.opened ?? false), From e3d0190f04eb74a171c4c1291e7e4550ff83764c Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:15:38 -0400 Subject: [PATCH 08/18] Fix Resend README tags, database name, and webhook docs --- spacetime-resend-ts/README.md | 49 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md index 34b7176dae0..965312d7f36 100644 --- a/spacetime-resend-ts/README.md +++ b/spacetime-resend-ts/README.md @@ -49,7 +49,7 @@ Resend credentials live in a private `resend_config` singleton. During `init`, a fresh database seeds the owner into the private `resend_admin_identity` table. ```bash -spacetime call --server http://127.0.0.1:3000 resend-ts set_resend_config \ +spacetime call --server http://127.0.0.1:3000 spacetime-resend set_resend_config \ '"re_..."' \ '{"some":"whsec_..."}' \ '{"some":"onboarding@resend.dev"}' @@ -62,7 +62,7 @@ For `t.option(...)` CLI arguments, use `null` for no value and Verify: ```bash -spacetime call --server http://127.0.0.1:3000 resend-ts get_resend_config_status '{}' +spacetime call --server http://127.0.0.1:3000 spacetime-resend get_resend_config_status '{}' ``` `send_email` reads the Resend API key from private module state. @@ -113,7 +113,10 @@ resend.sendEmailRequest(ctx.as.resend, { to: ['delivered@resend.dev'], subject: 'Welcome', html: '

Hello.

', - tagsJson: JSON.stringify({ userId: 'u_123', orgId: 'launch' }), + tagsJson: JSON.stringify([ + { name: 'userId', value: 'u_123' }, + { name: 'orgId', value: 'launch' }, + ]), }); ``` @@ -144,8 +147,9 @@ operators. - `makeResendWebhookHandler()` builds a direct HTTP webhook handler for a host router. -The HTTP handler returns 200 only after successful application or for an already -processed event. Invalid payloads return 400 and retain a failed event record; +The HTTP handler returns 200 after applying an event, for an already processed +event, and for signed event types the submodule does not handle, which are stored +as `Ignored`. Invalid payloads return 400 and retain a failed event record; redelivery retries failed records. Unexpected transaction failures propagate to the HTTP runtime. The reducer entrypoint throws on invalid payloads, rolling back its transaction. @@ -153,7 +157,8 @@ its transaction. Delivery timestamps use the provider's event time. Older events cannot replace newer status, and an earlier delivery stage cannot replace a later one. Queued send responses preserve any webhook state already recorded. Opens, clicks, and -complaints update their own fields without changing delivery status. +complaints update their own fields without changing delivery status. Error, +bounce, and failure details change only when their event sets the status. **Admin queries** @@ -163,6 +168,11 @@ complaints update their own fields without changing delivery status. List procedures return at most 1,000 rows. Host applications should expose caller-scoped, paginated views for product-facing history. +**Errors** + +`errors` from `@spacetimedb/resend/submodule` lists the `resend.*` codes the +submodule throws. + Package entrypoints: - `@spacetimedb/resend` can run as a standalone email database. @@ -178,20 +188,21 @@ email.complained email.failed email.opened email.clicked ``` +Other signed event types, such as `email.received`, `contact.*`, and `domain.*`, +are acknowledged and stored as `Ignored`. + `opened` / `clicked` / `complained` are recorded as **flags + timestamps** and leave `status` unchanged. Terminal states (`delivered`, `bounced`, `failed`, `cancelled`) and in-flight states (`queued`, `sent`, `delivery_delayed`) live in the `status` column. ## Webhook signature verification -The module's `ingest_resend_webhook` reducer verifies Standard Webhooks signatures (svix) using the configured `webhookSigningSecret`. `signatureHeader` and `timestampHeader` are stored on `resend_webhook_event` for forensic replay. +The `ingest_resend_webhook` reducer and the `makeResendWebhookHandler()` route verify Standard Webhooks signatures (svix) using the configured `webhookSigningSecret`. `signatureHeader` and `timestampHeader` are stored on `resend_webhook_event` for forensic replay. ## Tagging -`userId` / `orgId` are extracted from Resend `tags` and indexed for per-user / per-org listing. Both tag shapes are accepted: - -- Object form: `{"userId": "u_123", "orgId": "o_456"}` -- Array form (Resend's webhook output): `[{"name":"userId","value":"u_123"}]` +`userId` / `orgId` are extracted from Resend `tags` and indexed for per-user / per-org listing. -Pass `tagsJson` as a JSON string when calling `send_email`. +- `send_email` takes `tagsJson` as a JSON string in the array form Resend's send API expects: `[{"name":"userId","value":"u_123"}]`. +- Resend webhook payloads carry tags in object form: `{"userId": "u_123", "orgId": "o_456"}`. ## Integration testing @@ -206,27 +217,27 @@ For real Resend test-mode: ```bash # Bootstrap once with your real key: -spacetime call --server http://127.0.0.1:3000 resend-ts set_resend_config '"re_..."' null '{"some":"onboarding@resend.dev"}' +spacetime call --server http://127.0.0.1:3000 spacetime-resend set_resend_config '"re_..."' null '{"some":"onboarding@resend.dev"}' # Then send to one of Resend's test addresses (delivered@/bounced@/complained@): -spacetime call --server http://127.0.0.1:3000 resend-ts send_email \ +spacetime call --server http://127.0.0.1:3000 spacetime-resend send_email \ null \ '["delivered@resend.dev"]' \ '"Test from SpacetimeDB"' \ '{"some":"

Hello.

"}' \ null null null null \ - '{"some":"{\"userId\":\"u_123\"}"}' \ + '{"some":"[{\"name\":\"userId\",\"value\":\"u_123\"}]"}' \ null null null ``` -To exercise inbound webhooks end-to-end, expose your local relay via ngrok, register the URL in Resend's dashboard with the same `whsec_...` you passed to `set_resend_config`, and forward verified events to `ingest_resend_webhook`. +The standalone module has no HTTP route. To receive webhooks from Resend, mount the submodule in a host module that registers `makeResendWebhookHandler()` on a router, as the [example](./example/spacetimedb/) does at `/webhook/resend`. Expose the database through a public tunnel such as ngrok and register `https:///v1/database//route/webhook/resend` in Resend's dashboard with the same `whsec_...` you passed to `set_resend_config`. ## Architecture notes -- **valibot for runtime validation.** `vEmailEvent` is a `v.variant('type', [...])` over the 8 supported event types. The unit and smoke suites lock down the accepted wire shapes. +- **valibot for runtime validation.** `vEmailEvent` is a `v.variant('type', [...])` over the 8 supported event types; other event types are stored as `Ignored`. The unit and smoke suites lock down the accepted wire shapes. - **Synchronous HTTP.** Procedures are synchronous and `ctx.http.fetch` returns a `SyncResponse`, so `callResend` in `src/submodule/http.ts` implements the required API surface directly. - **Wire format.** The public input uses SDK-style camelCase (`replyTo`, `scheduledAt`), and `buildSendEmailBody` emits the provider's snake_case JSON fields. -- **Idempotency.** Each webhook event is keyed by `event.id`; re-ingest is a no-op. Status-changing events ratchet forward, so `email.complained` preserves a terminal `delivered` status. +- **Idempotency.** Each webhook event is keyed by its `svix-id` header (the reducer's `eventId` argument); re-ingesting a processed or ignored event is a no-op. Status-changing events ratchet forward, so `email.complained` preserves a terminal `delivered` status. ## Testing @@ -239,4 +250,4 @@ Credentialed smoke coverage is described in **Integration testing** above. ## License -[Apache-2.0](./LICENSE.txt). +Apache-2.0. From 2cc5c1fdb12df04ebd8ed2df7c2cd555f6c857d4 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 11:28:01 -0400 Subject: [PATCH 09/18] Keep removed dispatches from reappearing in the Resend example Deleting the email row let a later webhook recreate it with the same userId tag. Record removed ids in a private removed_dispatch table and filter them from the caller views instead. --- .../example/spacetimedb/src/index.ts | 50 ++++++++++--------- .../example/src/module_bindings/index.ts | 2 +- .../example/src/module_bindings/types.ts | 5 ++ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/spacetime-resend-ts/example/spacetimedb/src/index.ts b/spacetime-resend-ts/example/spacetimedb/src/index.ts index 2be5a08adae..fccdac9112a 100644 --- a/spacetime-resend-ts/example/spacetimedb/src/index.ts +++ b/spacetime-resend-ts/example/spacetimedb/src/index.ts @@ -17,10 +17,18 @@ const dispatchPolicy = table( } ); +// Removed dispatches stay in the Resend tables so later webhooks update the +// existing row instead of recreating it. The views skip these ids. +const removedDispatch = table( + { name: 'removed_dispatch', public: false }, + { resendId: t.string().primaryKey() } +); + const spacetimedb = schema({ resend, rateLimit, dispatchPolicy, + removedDispatch, }); function subjectFor(ctx: { sender: { toHexString(): string } }): string { @@ -30,7 +38,10 @@ function subjectFor(ctx: { sender: { toHexString(): string } }): string { export const myDispatchEmails = spacetimedb.view( { name: 'my_dispatch_emails', public: true }, resend.t.array(resend.resendEmailTable.rowType), - ctx => [...ctx.db.resend.resendEmail.byUserId.filter(subjectFor(ctx))] + ctx => + [...ctx.db.resend.resendEmail.byUserId.filter(subjectFor(ctx))].filter( + email => !ctx.db.removedDispatch.resendId.find(email.resendId) + ) ); export const myDispatchDeliveryEvents = spacetimedb.view( @@ -41,6 +52,7 @@ export const myDispatchDeliveryEvents = spacetimedb.view( for (const email of ctx.db.resend.resendEmail.byUserId.filter( subjectFor(ctx) )) { + if (ctx.db.removedDispatch.resendId.find(email.resendId)) continue; for (const event of ctx.db.resend.resendDeliveryEvent.byResendId.filter( email.resendId )) { @@ -204,25 +216,22 @@ const dispatchDeleteResult = t.object('DispatchDeleteResult', { removed: t.u32(), }); -// Remove a single dispatch and any delivery events it collected. This is a demo -// convenience so the log can be pruned; it writes directly to the submodule tables. +// Remove dispatches from the caller's log. This is a demo convenience so the +// log can be pruned. export const deleteDispatch = spacetimedb.procedure( { resendId: t.string() }, dispatchDeleteResult, (ctx, args) => { return ctx.withTx(tx => { - let removed = 0; const row = tx.db.resend.resendEmail.resendId.find(args.resendId); - if (row && row.userId === subjectFor(ctx)) { - tx.db.resend.resendEmail.delete(row); - removed += 1; - for (const event of tx.db.resend.resendDeliveryEvent.byResendId.filter( - args.resendId - )) { - tx.db.resend.resendDeliveryEvent.delete(event); - } + if ( + row?.userId !== subjectFor(ctx) || + tx.db.removedDispatch.resendId.find(args.resendId) + ) { + return { ok: true, removed: 0 }; } - return { ok: true, removed }; + tx.db.removedDispatch.insert({ resendId: args.resendId }); + return { ok: true, removed: 1 }; }); } ); @@ -233,16 +242,11 @@ export const clearDispatches = spacetimedb.procedure( ctx => { return ctx.withTx(tx => { let removed = 0; - const owned = [ - ...tx.db.resend.resendEmail.byUserId.filter(subjectFor(ctx)), - ]; - for (const row of owned) { - for (const event of tx.db.resend.resendDeliveryEvent.byResendId.filter( - row.resendId - )) { - tx.db.resend.resendDeliveryEvent.delete(event); - } - tx.db.resend.resendEmail.delete(row); + for (const row of tx.db.resend.resendEmail.byUserId.filter( + subjectFor(ctx) + )) { + if (tx.db.removedDispatch.resendId.find(row.resendId)) continue; + tx.db.removedDispatch.insert({ resendId: row.resendId }); removed += 1; } return { ok: true, removed }; diff --git a/spacetime-resend-ts/example/src/module_bindings/index.ts b/spacetime-resend-ts/example/src/module_bindings/index.ts index 48c1e04831e..e4ed9161542 100644 --- a/spacetime-resend-ts/example/src/module_bindings/index.ts +++ b/spacetime-resend-ts/example/src/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.10.1 (commit 5418e2f229c57f503d4306ae9e5fb6df29533f7d). +// This was generated using spacetimedb cli version 2.10.1 (commit e3d0190f04eb74a171c4c1291e7e4550ff83764c). /* eslint-disable */ /* tslint:disable */ diff --git a/spacetime-resend-ts/example/src/module_bindings/types.ts b/spacetime-resend-ts/example/src/module_bindings/types.ts index 30ca0349fc3..3a76237db76 100644 --- a/spacetime-resend-ts/example/src/module_bindings/types.ts +++ b/spacetime-resend-ts/example/src/module_bindings/types.ts @@ -48,6 +48,11 @@ export type MyDispatchDeliveryEvents = __Infer; export const MyDispatchEmails = __t.object("MyDispatchEmails", {}); export type MyDispatchEmails = __Infer; +export const RemovedDispatch = __t.object("RemovedDispatch", { + resendId: __t.string(), +}); +export type RemovedDispatch = __Infer; + export const ResendDeliveryEvent = __t.object("ResendDeliveryEvent", { eventId: __t.string(), resendId: __t.string(), From f7bba5daec7e6be7fe573fddd96a8cbfcd9cadc6 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 12:19:20 -0400 Subject: [PATCH 10/18] Adapt Resend to core crypto results and 2.11 bindings Report the crypto verification reason after resend.webhook_signature_mismatch. Extract send tags only from Resend's [{ name, value }] form. Regenerate the example bindings for snake_case submodule namespaces and the removed rate-limit consume procedure. --- spacetime-resend-ts/README.md | 2 +- .../example/src/module_bindings/index.ts | 57 +++++++++---------- .../rateLimit/consume_procedure.ts | 24 -------- .../src/module_bindings/rateLimit/types.ts | 12 ---- .../src/submodule/operations.ts | 29 +++------- spacetime-resend-ts/src/submodule/webhooks.ts | 9 ++- 6 files changed, 43 insertions(+), 90 deletions(-) delete mode 100644 spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md index 965312d7f36..f4ab99defb1 100644 --- a/spacetime-resend-ts/README.md +++ b/spacetime-resend-ts/README.md @@ -195,7 +195,7 @@ are acknowledged and stored as `Ignored`. ## Webhook signature verification -The `ingest_resend_webhook` reducer and the `makeResendWebhookHandler()` route verify Standard Webhooks signatures (svix) using the configured `webhookSigningSecret`. `signatureHeader` and `timestampHeader` are stored on `resend_webhook_event` for forensic replay. +The `ingest_resend_webhook` reducer and the `makeResendWebhookHandler()` route verify Standard Webhooks signatures (svix) using the configured `webhookSigningSecret`. A rejected signature returns 401 with `resend.webhook_signature_mismatch:`, where the reason is a `@spacetimedb/crypto` `errors` code such as `crypto.timestamp_outside_tolerance`. `signatureHeader` and `timestampHeader` are stored on `resend_webhook_event` for forensic replay. ## Tagging diff --git a/spacetime-resend-ts/example/src/module_bindings/index.ts b/spacetime-resend-ts/example/src/module_bindings/index.ts index e4ed9161542..fe5b4dee92d 100644 --- a/spacetime-resend-ts/example/src/module_bindings/index.ts +++ b/spacetime-resend-ts/example/src/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.10.1 (commit e3d0190f04eb74a171c4c1291e7e4550ff83764c). +// This was generated using spacetimedb cli version 2.11.0 (commit 094df0ed2107fe3a359f5e4ee7a4c344802fc96c). /* eslint-disable */ /* tslint:disable */ @@ -70,7 +70,6 @@ import * as Resend_RemoveAdminIdentityProcedure from "./resend/remove_admin_iden import * as Resend_ResendApiRequestProcedure from "./resend/resend_api_request_procedure"; import * as Resend_SendEmailProcedure from "./resend/send_email_procedure"; import * as Resend_SetResendConfigProcedure from "./resend/set_resend_config_procedure"; -import * as RateLimit_ConsumeProcedure from "./rateLimit/consume_procedure"; import * as RateLimit_RunSweepProcedure from "./rateLimit/run_sweep_procedure"; /** Type-only namespace exports for generated type groups. */ @@ -91,8 +90,8 @@ const tablesSchema = __schema({ constraints: [ ], }, MyDispatchEmailsRow), - "rateLimit.rate_limit_config": __table({ - name: 'rateLimit.rate_limit_config', + "rate_limit.rate_limit_config": __table({ + name: 'rate_limit.rate_limit_config', indexes: [ { accessor: 'singleton', name: 'rate_limit_config_singleton_idx_btree', algorithm: 'btree', columns: [ 'singleton', @@ -102,8 +101,8 @@ const tablesSchema = __schema({ { name: 'rate_limit_config_singleton_key', constraint: 'unique', columns: ['singleton'] }, ], }, RateLimit_RateLimitConfigRow), - "rateLimit.admin_rate_limit_buckets": __table({ - name: 'rateLimit.admin_rate_limit_buckets', + "rate_limit.admin_rate_limit_buckets": __table({ + name: 'rate_limit.admin_rate_limit_buckets', indexes: [ ], constraints: [ @@ -113,12 +112,12 @@ const tablesSchema = __schema({ /** The schema information for all reducers in this module. This is defined the same way as the reducers would have been defined in the server, except the body of the reducer is omitted in code generation. */ const reducersSchema = __reducers( - __reducerSchema("resend.ingest_resend_webhook", Resend_IngestResendWebhookReducer), - __reducerSchema("resend.replay_webhook_event", Resend_ReplayWebhookEventReducer), - __reducerSchema("rateLimit.add_rate_limit_admin", RateLimit_AddRateLimitAdminReducer), - __reducerSchema("rateLimit.remove_rate_limit_admin", RateLimit_RemoveRateLimitAdminReducer), - __reducerSchema("rateLimit.reset_buckets", RateLimit_ResetBucketsReducer), - __reducerSchema("rateLimit.update_config", RateLimit_UpdateConfigReducer), + __reducerSchema("resend.ingest_resend_webhook", Resend_IngestResendWebhookReducer, "resend.ingestResendWebhook"), + __reducerSchema("resend.replay_webhook_event", Resend_ReplayWebhookEventReducer, "resend.replayWebhookEvent"), + __reducerSchema("rate_limit.add_rate_limit_admin", RateLimit_AddRateLimitAdminReducer, "rateLimit.addRateLimitAdmin"), + __reducerSchema("rate_limit.remove_rate_limit_admin", RateLimit_RemoveRateLimitAdminReducer, "rateLimit.removeRateLimitAdmin"), + __reducerSchema("rate_limit.reset_buckets", RateLimit_ResetBucketsReducer, "rateLimit.resetBuckets"), + __reducerSchema("rate_limit.update_config", RateLimit_UpdateConfigReducer, "rateLimit.updateConfig"), ); /** The schema information for all procedures in this module. This is defined the same way as the procedures would have been defined in the server. */ @@ -127,26 +126,25 @@ const proceduresSchema = __procedures( __procedureSchema("delete_dispatch", DeleteDispatchProcedure.params, DeleteDispatchProcedure.returnType), __procedureSchema("send_dispatch", SendDispatchProcedure.params, SendDispatchProcedure.returnType), __procedureSchema("set_dispatch_policy", SetDispatchPolicyProcedure.params, SetDispatchPolicyProcedure.returnType), - __procedureSchema("resend.add_admin_identity", Resend_AddAdminIdentityProcedure.params, Resend_AddAdminIdentityProcedure.returnType), - __procedureSchema("resend.cancel_email", Resend_CancelEmailProcedure.params, Resend_CancelEmailProcedure.returnType), - __procedureSchema("resend.get_email", Resend_GetEmailProcedure.params, Resend_GetEmailProcedure.returnType), - __procedureSchema("resend.get_resend_config_status", Resend_GetResendConfigStatusProcedure.params, Resend_GetResendConfigStatusProcedure.returnType), - __procedureSchema("resend.list_delivery_events_for_email", Resend_ListDeliveryEventsForEmailProcedure.params, Resend_ListDeliveryEventsForEmailProcedure.returnType), - __procedureSchema("resend.list_emails_by_org_id", Resend_ListEmailsByOrgIdProcedure.params, Resend_ListEmailsByOrgIdProcedure.returnType), - __procedureSchema("resend.list_emails_by_status", Resend_ListEmailsByStatusProcedure.params, Resend_ListEmailsByStatusProcedure.returnType), - __procedureSchema("resend.list_emails_by_user_id", Resend_ListEmailsByUserIdProcedure.params, Resend_ListEmailsByUserIdProcedure.returnType), - __procedureSchema("resend.remove_admin_identity", Resend_RemoveAdminIdentityProcedure.params, Resend_RemoveAdminIdentityProcedure.returnType), - __procedureSchema("resend.resend_api_request", Resend_ResendApiRequestProcedure.params, Resend_ResendApiRequestProcedure.returnType), - __procedureSchema("resend.send_email", Resend_SendEmailProcedure.params, Resend_SendEmailProcedure.returnType), - __procedureSchema("resend.set_resend_config", Resend_SetResendConfigProcedure.params, Resend_SetResendConfigProcedure.returnType), - __procedureSchema("rateLimit.consume", RateLimit_ConsumeProcedure.params, RateLimit_ConsumeProcedure.returnType), - __procedureSchema("rateLimit.run_sweep", RateLimit_RunSweepProcedure.params, RateLimit_RunSweepProcedure.returnType), + __procedureSchema("resend.add_admin_identity", Resend_AddAdminIdentityProcedure.params, Resend_AddAdminIdentityProcedure.returnType, "resend.addAdminIdentity"), + __procedureSchema("resend.cancel_email", Resend_CancelEmailProcedure.params, Resend_CancelEmailProcedure.returnType, "resend.cancelEmail"), + __procedureSchema("resend.get_email", Resend_GetEmailProcedure.params, Resend_GetEmailProcedure.returnType, "resend.getEmail"), + __procedureSchema("resend.get_resend_config_status", Resend_GetResendConfigStatusProcedure.params, Resend_GetResendConfigStatusProcedure.returnType, "resend.getResendConfigStatus"), + __procedureSchema("resend.list_delivery_events_for_email", Resend_ListDeliveryEventsForEmailProcedure.params, Resend_ListDeliveryEventsForEmailProcedure.returnType, "resend.listDeliveryEventsForEmail"), + __procedureSchema("resend.list_emails_by_org_id", Resend_ListEmailsByOrgIdProcedure.params, Resend_ListEmailsByOrgIdProcedure.returnType, "resend.listEmailsByOrgId"), + __procedureSchema("resend.list_emails_by_status", Resend_ListEmailsByStatusProcedure.params, Resend_ListEmailsByStatusProcedure.returnType, "resend.listEmailsByStatus"), + __procedureSchema("resend.list_emails_by_user_id", Resend_ListEmailsByUserIdProcedure.params, Resend_ListEmailsByUserIdProcedure.returnType, "resend.listEmailsByUserId"), + __procedureSchema("resend.remove_admin_identity", Resend_RemoveAdminIdentityProcedure.params, Resend_RemoveAdminIdentityProcedure.returnType, "resend.removeAdminIdentity"), + __procedureSchema("resend.resend_api_request", Resend_ResendApiRequestProcedure.params, Resend_ResendApiRequestProcedure.returnType, "resend.resendApiRequest"), + __procedureSchema("resend.send_email", Resend_SendEmailProcedure.params, Resend_SendEmailProcedure.returnType, "resend.sendEmail"), + __procedureSchema("resend.set_resend_config", Resend_SetResendConfigProcedure.params, Resend_SetResendConfigProcedure.returnType, "resend.setResendConfig"), + __procedureSchema("rate_limit.run_sweep", RateLimit_RunSweepProcedure.params, RateLimit_RunSweepProcedure.returnType, "rateLimit.runSweep"), ); /** The remote SpacetimeDB module schema, both runtime and type information. */ const REMOTE_MODULE = { versionInfo: { - cliVersion: "2.10.1" as const, + cliVersion: "2.11.0" as const, }, tables: tablesSchema.schemaType.tables, reducers: reducersSchema.reducersType.reducers, @@ -163,8 +161,8 @@ export const tables = { myDispatchDeliveryEvents: __qb.myDispatchDeliveryEvents, myDispatchEmails: __qb.myDispatchEmails, rateLimit: { - rateLimitConfig: __qb["rateLimit.rate_limit_config"], - adminRateLimitBuckets: __qb["rateLimit.admin_rate_limit_buckets"], + rateLimitConfig: __qb["rate_limit.rate_limit_config"], + adminRateLimitBuckets: __qb["rate_limit.admin_rate_limit_buckets"], }, } as const; @@ -191,7 +189,6 @@ export const procedures = { sendDispatch: __procedureAccessors.sendDispatch, setDispatchPolicy: __procedureAccessors.setDispatchPolicy, rateLimit: { - consume: __procedureAccessors["rateLimit.consume"], runSweep: __procedureAccessors["rateLimit.runSweep"], }, resend: { diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts deleted file mode 100644 index a98b8588aad..00000000000 --- a/spacetime-resend-ts/example/src/module_bindings/rateLimit/consume_procedure.ts +++ /dev/null @@ -1,24 +0,0 @@ -// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE -// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. - -/* eslint-disable */ -/* tslint:disable */ -import { - TypeBuilder as __TypeBuilder, - t as __t, - type AlgebraicTypeType as __AlgebraicTypeType, - type Infer as __Infer, -} from "spacetimedb"; - -import { - RateLimitConsumeResult, -} from "./types"; - -export const params = { - scope: __t.string(), - actorKey: __t.string(), - limit: __t.u32(), - windowSeconds: __t.u32(), - cost: __t.option(__t.u32()), -}; -export const returnType = RateLimitConsumeResult \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts b/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts index dc6aa78cab2..d14931b1fb9 100644 --- a/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts +++ b/spacetime-resend-ts/example/src/module_bindings/rateLimit/types.ts @@ -36,18 +36,6 @@ export const RateLimitConfig = __t.object("RateLimitConfig", { }); export type RateLimitConfig = __Infer; -export const RateLimitConsumeResult = __t.object("RateLimitConsumeResult", { - allowed: __t.bool(), - scope: __t.string(), - key: __t.string(), - limit: __t.u32(), - used: __t.u32(), - remaining: __t.u32(), - retryAfterSeconds: __t.u32(), - resetAt: __t.timestamp(), -}); -export type RateLimitConsumeResult = __Infer; - export const RateLimitSweepTick = __t.object("RateLimitSweepTick", { scheduledId: __t.u64(), scheduledAt: __t.scheduleAt(), diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts index f6a3a52f03c..d73e96bd7dc 100644 --- a/spacetime-resend-ts/src/submodule/operations.ts +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -35,33 +35,20 @@ function takeRows(rows: Iterable): T[] { return out; } -const vTagsForExtraction = v.union([ - v.record(v.string(), v.string()), - v.array(v.object({ name: v.string(), value: v.string() })), -]); +// Resend's send API takes tags as `[{ name, value }]`. +const vSendTags = v.array(v.object({ name: v.string(), value: v.string() })); function extractTagFieldsFromJson(tagsJson: string | undefined): { userId: string | undefined; orgId: string | undefined; } { - if (!tagsJson) return { userId: undefined, orgId: undefined }; - const parsed = safeJsonParse(tagsJson); - if (parsed === undefined) return { userId: undefined, orgId: undefined }; - const result = v.safeParse(vTagsForExtraction, parsed); - if (!result.success) { - return { userId: undefined, orgId: undefined }; - } + const result = v.safeParse(vSendTags, tagsJson && safeJsonParse(tagsJson)); + if (!result.success) return { userId: undefined, orgId: undefined }; const tags = result.output; - if (Array.isArray(tags)) { - let userId: string | undefined; - let orgId: string | undefined; - for (const tag of tags) { - if (tag.name === 'userId') userId = tag.value; - if (tag.name === 'orgId') orgId = tag.value; - } - return { userId, orgId }; - } - return { userId: tags['userId'], orgId: tags['orgId'] }; + return { + userId: tags.find(tag => tag.name === 'userId')?.value, + orgId: tags.find(tag => tag.name === 'orgId')?.value, + }; } // Resend expects snake_case fields in the POST /emails request body. diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index d1e38ed1c0d..7be2b5b6200 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -272,7 +272,7 @@ function applyResendWebhook( return { status: 500, code: errors.webhookSecretNotConfigured }; } const nowSeconds = Number(ctx.timestamp.microsSinceUnixEpoch / 1_000_000n); - const sigOk = verifySvixSignature({ + const signature = verifySvixSignature({ svixId: args.eventId, svixTimestamp: args.timestampHeader ?? '', svixSignature: args.signatureHeader ?? '', @@ -280,7 +280,12 @@ function applyResendWebhook( secret: cfg.webhookSigningSecret, nowSeconds, }); - if (!sigOk) return { status: 401, code: errors.webhookSignatureMismatch }; + if (!signature.ok) { + return { + status: 401, + code: `${errors.webhookSignatureMismatch}:${signature.reason}`, + }; + } const signedEventType = parseResendEventType(args.payloadJson); if ( From 8fb6fe558f0c6042cb732ac226c3fd0f2dff88f7 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 12:22:20 -0400 Subject: [PATCH 11/18] Ship compiled dist from the Resend package Build src to dist with declarations on pack, publish dist entry points through publishConfig, and use .js relative imports. The standalone module build moves to build:module. --- spacetime-resend-ts/package.json | 21 +++++++++++++++---- spacetime-resend-ts/scripts/test-unit.ts | 6 +++--- spacetime-resend-ts/src/index.ts | 13 +++++++----- spacetime-resend-ts/src/submodule.ts | 16 +++++++------- spacetime-resend-ts/src/submodule/auth.ts | 6 +++--- spacetime-resend-ts/src/submodule/config.ts | 8 +++---- .../src/submodule/email-input.ts | 4 ++-- .../src/submodule/email_writes.ts | 2 +- spacetime-resend-ts/src/submodule/http.ts | 8 +++---- spacetime-resend-ts/src/submodule/install.ts | 2 +- .../src/submodule/operations.ts | 20 +++++++++++------- spacetime-resend-ts/src/submodule/request.ts | 4 ++-- spacetime-resend-ts/src/submodule/schema.ts | 2 +- spacetime-resend-ts/src/submodule/webhooks.ts | 12 +++++------ spacetime-resend-ts/tsconfig.build.json | 10 +++++++++ spacetime-resend-ts/tsconfig.json | 1 - 16 files changed, 82 insertions(+), 53 deletions(-) create mode 100644 spacetime-resend-ts/tsconfig.build.json diff --git a/spacetime-resend-ts/package.json b/spacetime-resend-ts/package.json index de90701b640..feb7da0de32 100644 --- a/spacetime-resend-ts/package.json +++ b/spacetime-resend-ts/package.json @@ -17,13 +17,25 @@ } }, "files": [ - "src", + "dist", "LICENSE", "README.md" ], "publishConfig": { "access": "public", - "registry": "https://registry.npmjs.org/" + "registry": "https://registry.npmjs.org/", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./submodule": { + "types": "./dist/submodule.d.ts", + "default": "./dist/submodule.js" + } + } }, "repository": { "type": "git", @@ -41,9 +53,10 @@ "webhooks" ], "scripts": { - "prepack": "node -e \"require('node:fs').copyFileSync('../licenses/apache2.txt', 'LICENSE')\"", + "prepack": "pnpm run build && node -e \"require('node:fs').copyFileSync('../licenses/apache2.txt', 'LICENSE')\"", "postpack": "node -e \"require('node:fs').unlinkSync('LICENSE')\"", - "build": "spacetime build", + "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json", + "build:module": "spacetime build", "format": "prettier . --write --ignore-path ../.prettierignore", "lint": "eslint . && prettier . --check --ignore-path ../.prettierignore", "typecheck": "tsc --noEmit", diff --git a/spacetime-resend-ts/scripts/test-unit.ts b/spacetime-resend-ts/scripts/test-unit.ts index c4cffb97510..01ac89d0cbf 100644 --- a/spacetime-resend-ts/scripts/test-unit.ts +++ b/spacetime-resend-ts/scripts/test-unit.ts @@ -1,7 +1,7 @@ import * as assert from 'node:assert/strict'; -import { buildResendHttpRequest } from '../src/submodule/request.ts'; -import { validateEmailInput } from '../src/submodule/email-input.ts'; -import { parseResendEventType } from '../src/submodule/webhook-metadata.ts'; +import { buildResendHttpRequest } from '../src/submodule/request'; +import { validateEmailInput } from '../src/submodule/email-input'; +import { parseResendEventType } from '../src/submodule/webhook-metadata'; assert.equal( parseResendEventType('{"type":"email.delivered","data":{}}'), diff --git a/spacetime-resend-ts/src/index.ts b/spacetime-resend-ts/src/index.ts index 4df9f3f54a5..c90912da678 100644 --- a/spacetime-resend-ts/src/index.ts +++ b/spacetime-resend-ts/src/index.ts @@ -1,8 +1,11 @@ -export { default, init } from './submodule/schema'; -export { ingestResendWebhook, replayWebhookEvent } from './submodule/webhooks'; +export { default, init } from './submodule/schema.js'; +export { + ingestResendWebhook, + replayWebhookEvent, +} from './submodule/webhooks.js'; -export { setResendConfig, getResendConfigStatus } from './submodule/config'; -export { addAdminIdentity, removeAdminIdentity } from './submodule/auth'; +export { setResendConfig, getResendConfigStatus } from './submodule/config.js'; +export { addAdminIdentity, removeAdminIdentity } from './submodule/auth.js'; export { cancelEmail, getEmail, @@ -12,4 +15,4 @@ export { listEmailsByUserId, resendApiRequest, sendEmail, -} from './submodule/operations'; +} from './submodule/operations.js'; diff --git a/spacetime-resend-ts/src/submodule.ts b/spacetime-resend-ts/src/submodule.ts index 05282dba0a6..1c376244850 100644 --- a/spacetime-resend-ts/src/submodule.ts +++ b/spacetime-resend-ts/src/submodule.ts @@ -1,17 +1,17 @@ -export { default } from './submodule/schema'; +export { default } from './submodule/schema.js'; export { resendDeliveryEventTable, resendEmailTable, t, -} from './submodule/schema'; -export { install } from './submodule/install'; -export { errors } from './submodule/errors'; +} from './submodule/schema.js'; +export { install } from './submodule/install.js'; +export { errors } from './submodule/errors.js'; export { ingestResendWebhook, replayWebhookEvent, makeResendWebhookHandler, type ResendWebhookIngestArgs, -} from './submodule/webhooks'; +} from './submodule/webhooks.js'; export { sendEmailRequest, sendEmail, @@ -23,7 +23,7 @@ export { listDeliveryEventsForEmail, resendApiRequest, type SendEmailArgs, -} from './submodule/operations'; +} from './submodule/operations.js'; -export { setResendConfig, getResendConfigStatus } from './submodule/config'; -export { addAdminIdentity, removeAdminIdentity } from './submodule/auth'; +export { setResendConfig, getResendConfigStatus } from './submodule/config.js'; +export { addAdminIdentity, removeAdminIdentity } from './submodule/auth.js'; diff --git a/spacetime-resend-ts/src/submodule/auth.ts b/spacetime-resend-ts/src/submodule/auth.ts index 905a36ec432..5ea77d51f9c 100644 --- a/spacetime-resend-ts/src/submodule/auth.ts +++ b/spacetime-resend-ts/src/submodule/auth.ts @@ -3,9 +3,9 @@ import { t, type ProcedureModuleCtx, type WriteCtx, -} from './schema'; -import { throwSenderError } from './validation'; -import { errors } from './errors'; +} from './schema.js'; +import { throwSenderError } from './validation.js'; +import { errors } from './errors.js'; // Admin gate. Fresh publishes seed the owner via init. Public submodule calls // never bootstrap admin state from "first caller wins". Procedure callers must diff --git a/spacetime-resend-ts/src/submodule/config.ts b/spacetime-resend-ts/src/submodule/config.ts index 8cbfd33e25f..0f1e546f60c 100644 --- a/spacetime-resend-ts/src/submodule/config.ts +++ b/spacetime-resend-ts/src/submodule/config.ts @@ -3,10 +3,10 @@ import { t, type ProcedureModuleCtx, type WriteCtx, -} from './schema'; -import { adminVerdict, denyIfNotAdmin } from './auth'; -import { throwSenderError } from './validation'; -import { errors } from './errors'; +} from './schema.js'; +import { adminVerdict, denyIfNotAdmin } from './auth.js'; +import { throwSenderError } from './validation.js'; +import { errors } from './errors.js'; export type ResendConfig = { apiKey: string; diff --git a/spacetime-resend-ts/src/submodule/email-input.ts b/spacetime-resend-ts/src/submodule/email-input.ts index 5aeac640661..1b52a077e12 100644 --- a/spacetime-resend-ts/src/submodule/email-input.ts +++ b/spacetime-resend-ts/src/submodule/email-input.ts @@ -1,5 +1,5 @@ -import { errors } from './errors'; -import { hasControlCharacter } from './text-validation'; +import { errors } from './errors.js'; +import { hasControlCharacter } from './text-validation.js'; export type EmailInput = { from?: string | undefined; diff --git a/spacetime-resend-ts/src/submodule/email_writes.ts b/spacetime-resend-ts/src/submodule/email_writes.ts index c1f8fe100e2..1519cb7b101 100644 --- a/spacetime-resend-ts/src/submodule/email_writes.ts +++ b/spacetime-resend-ts/src/submodule/email_writes.ts @@ -3,7 +3,7 @@ import { type EmailStatusValue, type ModuleTimestamp, type WriteCtx, -} from './schema'; +} from './schema.js'; const STATUS_ORDER = { Queued: 0, diff --git a/spacetime-resend-ts/src/submodule/http.ts b/spacetime-resend-ts/src/submodule/http.ts index 5dd1909cf40..fc924a1184d 100644 --- a/spacetime-resend-ts/src/submodule/http.ts +++ b/spacetime-resend-ts/src/submodule/http.ts @@ -1,8 +1,8 @@ import * as v from 'valibot'; -import { type ProcedureModuleCtx, vResendErrorBody } from './schema'; -import { safeJsonParse, throwSenderError } from './validation'; -import { buildResendHttpRequest } from './request'; -import { errors } from './errors'; +import { type ProcedureModuleCtx, vResendErrorBody } from './schema.js'; +import { safeJsonParse, throwSenderError } from './validation.js'; +import { buildResendHttpRequest } from './request.js'; +import { errors } from './errors.js'; export type ResendHttpResponse = { status: number; diff --git a/spacetime-resend-ts/src/submodule/install.ts b/spacetime-resend-ts/src/submodule/install.ts index f189253b08a..167bc680ec2 100644 --- a/spacetime-resend-ts/src/submodule/install.ts +++ b/spacetime-resend-ts/src/submodule/install.ts @@ -1,4 +1,4 @@ -import type { ReducerModuleCtx } from './schema'; +import type { ReducerModuleCtx } from './schema.js'; export function install(ctx: ReducerModuleCtx) { if (ctx.db.resendAdminIdentity.identity.find(ctx.sender) != null) return; diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts index d73e96bd7dc..5e3bd9291c7 100644 --- a/spacetime-resend-ts/src/submodule/operations.ts +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -10,14 +10,18 @@ import { vSendEmailResponse, type ProcedureModuleCtx, type WriteCtx, -} from './schema'; -import { callResend, ensureOkOrThrow } from './http'; -import { safeJsonParse, summarizeIssues, throwSenderError } from './validation'; -import { upsertEmail } from './email_writes'; -import { loadConfigOrThrowFromProcedure } from './config'; -import { adminVerdict, denyIfNotAdmin } from './auth'; -import { validateEmailInput } from './email-input'; -import { errors } from './errors'; +} from './schema.js'; +import { callResend, ensureOkOrThrow } from './http.js'; +import { + safeJsonParse, + summarizeIssues, + throwSenderError, +} from './validation.js'; +import { upsertEmail } from './email_writes.js'; +import { loadConfigOrThrowFromProcedure } from './config.js'; +import { adminVerdict, denyIfNotAdmin } from './auth.js'; +import { validateEmailInput } from './email-input.js'; +import { errors } from './errors.js'; function requireProcedureAdmin(ctx: ProcedureModuleCtx): void { const verdict = ctx.withTx(tx => adminVerdict(tx, ctx.sender)); diff --git a/spacetime-resend-ts/src/submodule/request.ts b/spacetime-resend-ts/src/submodule/request.ts index ea445336611..bbf13cec563 100644 --- a/spacetime-resend-ts/src/submodule/request.ts +++ b/spacetime-resend-ts/src/submodule/request.ts @@ -1,5 +1,5 @@ -import { errors } from './errors'; -import { hasControlCharacter } from './text-validation'; +import { errors } from './errors.js'; +import { hasControlCharacter } from './text-validation.js'; const RESEND_API_BASE = 'https://api.resend.com'; const ALLOWED_METHODS = new Set(['GET', 'POST', 'PATCH', 'DELETE']); diff --git a/spacetime-resend-ts/src/submodule/schema.ts b/spacetime-resend-ts/src/submodule/schema.ts index 5881f97f9d2..209f7af6855 100644 --- a/spacetime-resend-ts/src/submodule/schema.ts +++ b/spacetime-resend-ts/src/submodule/schema.ts @@ -9,7 +9,7 @@ import { type TransactionCtx, } from 'spacetimedb/server'; import * as v from 'valibot'; -import { install } from './install'; +import { install } from './install.js'; // Webhook delivery state. Received = ingest accepted. Processed = applied. // Ignored = event type this submodule does not handle. Failed = invalid payload. diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index 7be2b5b6200..e0697b4fc5a 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -12,9 +12,9 @@ import { type ReducerModuleCtx, type WebhookEventStatusValue, type WriteCtx, -} from './schema'; -import { upsertEmail } from './email_writes'; -import { requireAdmin } from './auth'; +} from './schema.js'; +import { upsertEmail } from './email_writes.js'; +import { requireAdmin } from './auth.js'; import { verifySvixSignature } from '@spacetimedb/crypto'; import { SyncResponse, @@ -26,9 +26,9 @@ import { safeJsonParse, summarizeIssues, throwSenderError, -} from './validation'; -import { parseResendEventType } from './webhook-metadata'; -import { errors } from './errors'; +} from './validation.js'; +import { parseResendEventType } from './webhook-metadata.js'; +import { errors } from './errors.js'; type ResendTags = EmailEvent['data']['tags']; diff --git a/spacetime-resend-ts/tsconfig.build.json b/spacetime-resend-ts/tsconfig.build.json new file mode 100644 index 00000000000..8635027968f --- /dev/null +++ b/spacetime-resend-ts/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + "declaration": true, + "outDir": "dist", + "rootDir": "src" + }, + "include": ["src"] +} diff --git a/spacetime-resend-ts/tsconfig.json b/spacetime-resend-ts/tsconfig.json index c659d97428a..d4766a1709a 100644 --- a/spacetime-resend-ts/tsconfig.json +++ b/spacetime-resend-ts/tsconfig.json @@ -8,7 +8,6 @@ "noEmit": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, - "allowImportingTsExtensions": true, "noImplicitAny": true, "moduleResolution": "Bundler", "isolatedDeclarations": false, From 63658ae7fe1c40cd934b9649ad926bb726dbbf35 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Tue, 29 Sep 2026 12:23:51 -0400 Subject: [PATCH 12/18] Test that Resend rejects bad webhook signatures Run the HTTP route and ingest reducer against a mock context: a wrong signature returns 401 or throws and stores nothing, and a valid one is accepted. --- spacetime-resend-ts/package.json | 2 +- .../scripts/sys-abi-test-register.mjs | 27 ++++++ spacetime-resend-ts/scripts/test-webhooks.ts | 91 +++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 spacetime-resend-ts/scripts/sys-abi-test-register.mjs create mode 100644 spacetime-resend-ts/scripts/test-webhooks.ts diff --git a/spacetime-resend-ts/package.json b/spacetime-resend-ts/package.json index feb7da0de32..ec58ab75835 100644 --- a/spacetime-resend-ts/package.json +++ b/spacetime-resend-ts/package.json @@ -60,7 +60,7 @@ "format": "prettier . --write --ignore-path ../.prettierignore", "lint": "eslint . && prettier . --check --ignore-path ../.prettierignore", "typecheck": "tsc --noEmit", - "test": "tsx scripts/test-unit.ts", + "test": "tsx scripts/test-unit.ts && node --js-explicit-resource-management --import tsx --import ./scripts/sys-abi-test-register.mjs scripts/test-webhooks.ts", "spacetime:generate": "spacetime generate --lang typescript --out-dir ts-codegen", "publish:module": "spacetime publish", "publish:local": "spacetime publish --server local --yes spacetime-resend", diff --git a/spacetime-resend-ts/scripts/sys-abi-test-register.mjs b/spacetime-resend-ts/scripts/sys-abi-test-register.mjs new file mode 100644 index 00000000000..30107456e71 --- /dev/null +++ b/spacetime-resend-ts/scripts/sys-abi-test-register.mjs @@ -0,0 +1,27 @@ +import { register } from 'node:module'; + +// Host syscall modules exist only inside SpacetimeDB. Stub the bindings that +// `spacetimedb/server` reads while loading. +const stubs = { + 'spacetime:sys@2.0': + 'export const moduleHooks = Symbol(); export function row_iter_bsatn_close() {}', + 'spacetime:sys@2.1': 'export {};', + 'spacetime:sys@2.2': 'export function env_get() {}', +}; + +const loaderSource = ` + const stubs = ${JSON.stringify(stubs)}; + export function resolve(specifier, context, nextResolve) { + return specifier in stubs + ? { + shortCircuit: true, + url: 'data:text/javascript,' + encodeURIComponent(stubs[specifier]), + } + : nextResolve(specifier, context); + } +`; + +register( + `data:text/javascript,${encodeURIComponent(loaderSource)}`, + import.meta.url +); diff --git a/spacetime-resend-ts/scripts/test-webhooks.ts b/spacetime-resend-ts/scripts/test-webhooks.ts new file mode 100644 index 00000000000..34fcaa6bdf7 --- /dev/null +++ b/spacetime-resend-ts/scripts/test-webhooks.ts @@ -0,0 +1,91 @@ +import * as assert from 'node:assert/strict'; +import { createHmac } from 'node:crypto'; +import { Timestamp } from 'spacetimedb'; +import { SenderError } from 'spacetimedb/server'; +import { + ingestResendWebhook, + makeResendWebhookHandler, +} from '../src/submodule/webhooks'; + +const secretRaw = 'resend_unit_test_secret'; +const secret = `whsec_${Buffer.from(secretRaw).toString('base64')}`; +const nowSeconds = 1_800_000_000; +const timestamp = String(nowSeconds); +const payloadJson = JSON.stringify({ + type: 'contact.created', + created_at: '2027-01-15T08:00:00Z', + data: { id: 'contact_1' }, +}); + +function sign(eventId: string): string { + const digest = createHmac('sha256', secretRaw) + .update(`${eventId}.${timestamp}.${payloadJson}`) + .digest('base64'); + return `v1,${digest}`; +} + +// The mock context implements only the tables these paths read and write. +const webhookEvents = new Map(); +const tx = { + timestamp: new Timestamp(BigInt(nowSeconds) * 1_000_000n), + db: { + resendConfig: { + singleton: { find: () => ({ webhookSigningSecret: secret }) }, + }, + resendWebhookEvent: { + eventId: { + find: (id: string) => webhookEvents.get(id), + update: (row: { eventId: string }) => + webhookEvents.set(row.eventId, row), + }, + insert: (row: { eventId: string }) => webhookEvents.set(row.eventId, row), + }, + }, +}; + +const handler = makeResendWebhookHandler(); +function post(eventId: string, signature: string) { + const headers = new Map([ + ['svix-id', eventId], + ['svix-timestamp', timestamp], + ['svix-signature', signature], + ]); + const req = { + method: 'POST', + headers: { get: (name: string) => headers.get(name) ?? null }, + text: () => payloadJson, + }; + return handler( + { withTx: (fn: (ctx: typeof tx) => unknown) => fn(tx) } as never, + req as never + ); +} + +const rejected = post('evt_bad_http', sign('evt_other')); +assert.equal(rejected.status, 401); +assert.match(rejected.text(), /resend\.webhook_signature_mismatch/); +assert.equal(webhookEvents.size, 0); + +assert.equal(post('evt_good_http', sign('evt_good_http')).status, 200); +assert.ok(webhookEvents.has('evt_good_http')); + +const ingest = (eventId: string, signature: string) => + ingestResendWebhook(tx as never, { + eventId, + eventType: 'contact.created', + payloadJson, + signatureHeader: signature, + timestampHeader: timestamp, + }); + +assert.throws( + () => ingest('evt_bad_reducer', sign('evt_other')), + (error: unknown) => + error instanceof SenderError && + /resend\.webhook_signature_mismatch/.test(error.message) +); +assert.ok(!webhookEvents.has('evt_bad_reducer')); +ingest('evt_good_reducer', sign('evt_good_reducer')); +assert.ok(webhookEvents.has('evt_good_reducer')); + +process.stdout.write('resend webhook tests passed\n'); From b60b241b758e8dd4e34b43fb16f8759c760b5e89 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Wed, 30 Sep 2026 12:42:45 -0400 Subject: [PATCH 13/18] Keep Failed Resend webhook rows on the reducer path The ingest reducer now throws only for requests rejected before any write. A stored event with an invalid payload commits as Failed, and admins read the outcome with the new get_webhook_event procedure. --- spacetime-resend-ts/README.md | 10 +++- .../scripts/test-resend-smoke.ts | 42 +++++++++++++++++ spacetime-resend-ts/scripts/test-webhooks.ts | 46 ++++++++++++++----- spacetime-resend-ts/src/index.ts | 1 + spacetime-resend-ts/src/submodule.ts | 1 + .../src/submodule/operations.ts | 12 +++++ spacetime-resend-ts/src/submodule/webhooks.ts | 24 ++++++---- 7 files changed, 113 insertions(+), 23 deletions(-) diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md index f4ab99defb1..c822b3170b3 100644 --- a/spacetime-resend-ts/README.md +++ b/spacetime-resend-ts/README.md @@ -151,8 +151,12 @@ The HTTP handler returns 200 after applying an event, for an already processed event, and for signed event types the submodule does not handle, which are stored as `Ignored`. Invalid payloads return 400 and retain a failed event record; redelivery retries failed records. Unexpected transaction failures propagate to -the HTTP runtime. The reducer entrypoint throws on invalid payloads, rolling back -its transaction. +the HTTP runtime. + +The reducer throws, writing nothing, when metadata, size, headers, secret, or +signature checks reject a request. Once a signed event is stored, the reducer +commits: an invalid payload keeps its `Failed` event record with the error +message. Callers read the outcome with `get_webhook_event`. Delivery timestamps use the provider's event time. Older events cannot replace newer status, and an earlier delivery stage cannot replace a later one. Queued @@ -164,6 +168,8 @@ bounce, and failure details change only when their event sets the status. - `get_email`, `list_emails_by_user_id`, `list_emails_by_org_id`, `list_emails_by_status` - `list_delivery_events_for_email` +- `get_webhook_event` - stored event with `status` (`Received`, `Processed`, + `Ignored`, or `Failed`) and `errorMessage` List procedures return at most 1,000 rows. Host applications should expose caller-scoped, paginated views for product-facing history. diff --git a/spacetime-resend-ts/scripts/test-resend-smoke.ts b/spacetime-resend-ts/scripts/test-resend-smoke.ts index ffb3b2f9ee8..0b62390a589 100644 --- a/spacetime-resend-ts/scripts/test-resend-smoke.ts +++ b/spacetime-resend-ts/scripts/test-resend-smoke.ts @@ -178,6 +178,30 @@ function emailStatus(rowText: string): string { return EMAIL_STATUS[tag]!; } +const WEBHOOK_EVENT_STATUS = [ + 'Received', + 'Processed', + 'Ignored', + 'Failed', +] as const; + +function webhookEventStatus(rowText: string): string { + const parsed = JSON.parse(rowText); + const row = parsed?.[1]; + const variant = row?.[5]; + const tag = variant?.[0]; + if ( + typeof tag !== 'number' || + tag < 0 || + tag >= WEBHOOK_EVENT_STATUS.length + ) { + throw new Error( + `could not parse webhook event status from row: ${rowText}` + ); + } + return WEBHOOK_EVENT_STATUS[tag]!; +} + async function main() { const opts = parseArgs(process.argv.slice(2)); // Unique run id so re-runs don't collide on idempotent webhook IDs. @@ -310,6 +334,24 @@ async function main() { ); } + step('reducer keeps a Failed row for an invalid signed payload'); + const invalidEventId = evt('invalid_reducer'); + await ingestWebhook( + opts, + invalidEventId, + 'email.sent', + JSON.stringify({ type: 'email.sent', data: {} }) + ); + const readInvalidEvent = () => + callReducer(opts, 'get_webhook_event', [quote(invalidEventId)]); + let stored = await readInvalidEvent(); + if (webhookEventStatus(stored) !== 'Failed') + throw new Error(`expected Failed webhook row after ingest: ${stored}`); + await callReducer(opts, 'replay_webhook_event', [quote(invalidEventId)]); + stored = await readInvalidEvent(); + if (webhookEventStatus(stored) !== 'Failed') + throw new Error(`expected Failed webhook row after replay: ${stored}`); + step('signed event types the submodule does not handle are acknowledged'); await ingestWebhook( opts, diff --git a/spacetime-resend-ts/scripts/test-webhooks.ts b/spacetime-resend-ts/scripts/test-webhooks.ts index 34fcaa6bdf7..85b3c54bfa0 100644 --- a/spacetime-resend-ts/scripts/test-webhooks.ts +++ b/spacetime-resend-ts/scripts/test-webhooks.ts @@ -17,15 +17,23 @@ const payloadJson = JSON.stringify({ data: { id: 'contact_1' }, }); -function sign(eventId: string): string { +// Signed and handled, but fails payload validation. +const invalidPayloadJson = JSON.stringify({ + type: 'email.sent', + created_at: '2027-01-15T08:00:00Z', + data: {}, +}); + +function sign(eventId: string, body = payloadJson): string { const digest = createHmac('sha256', secretRaw) - .update(`${eventId}.${timestamp}.${payloadJson}`) + .update(`${eventId}.${timestamp}.${body}`) .digest('base64'); return `v1,${digest}`; } // The mock context implements only the tables these paths read and write. -const webhookEvents = new Map(); +type EventRow = { eventId: string; status: { tag: string } }; +const webhookEvents = new Map(); const tx = { timestamp: new Timestamp(BigInt(nowSeconds) * 1_000_000n), db: { @@ -35,16 +43,15 @@ const tx = { resendWebhookEvent: { eventId: { find: (id: string) => webhookEvents.get(id), - update: (row: { eventId: string }) => - webhookEvents.set(row.eventId, row), + update: (row: EventRow) => webhookEvents.set(row.eventId, row), }, - insert: (row: { eventId: string }) => webhookEvents.set(row.eventId, row), + insert: (row: EventRow) => webhookEvents.set(row.eventId, row), }, }, }; const handler = makeResendWebhookHandler(); -function post(eventId: string, signature: string) { +function post(eventId: string, signature: string, body = payloadJson) { const headers = new Map([ ['svix-id', eventId], ['svix-timestamp', timestamp], @@ -53,7 +60,7 @@ function post(eventId: string, signature: string) { const req = { method: 'POST', headers: { get: (name: string) => headers.get(name) ?? null }, - text: () => payloadJson, + text: () => body, }; return handler( { withTx: (fn: (ctx: typeof tx) => unknown) => fn(tx) } as never, @@ -69,11 +76,20 @@ assert.equal(webhookEvents.size, 0); assert.equal(post('evt_good_http', sign('evt_good_http')).status, 200); assert.ok(webhookEvents.has('evt_good_http')); -const ingest = (eventId: string, signature: string) => +const invalidHttp = post( + 'evt_invalid_http', + sign('evt_invalid_http', invalidPayloadJson), + invalidPayloadJson +); +assert.equal(invalidHttp.status, 400); +assert.match(invalidHttp.text(), /resend\.webhook_payload_invalid/); +assert.equal(webhookEvents.get('evt_invalid_http')?.status.tag, 'Failed'); + +const ingest = (eventId: string, signature: string, body = payloadJson) => ingestResendWebhook(tx as never, { eventId, - eventType: 'contact.created', - payloadJson, + eventType: JSON.parse(body).type, + payloadJson: body, signatureHeader: signature, timestampHeader: timestamp, }); @@ -88,4 +104,12 @@ assert.ok(!webhookEvents.has('evt_bad_reducer')); ingest('evt_good_reducer', sign('evt_good_reducer')); assert.ok(webhookEvents.has('evt_good_reducer')); +// The reducer returns so the Failed row commits. +ingest( + 'evt_invalid_reducer', + sign('evt_invalid_reducer', invalidPayloadJson), + invalidPayloadJson +); +assert.equal(webhookEvents.get('evt_invalid_reducer')?.status.tag, 'Failed'); + process.stdout.write('resend webhook tests passed\n'); diff --git a/spacetime-resend-ts/src/index.ts b/spacetime-resend-ts/src/index.ts index c90912da678..72d8ebde3b1 100644 --- a/spacetime-resend-ts/src/index.ts +++ b/spacetime-resend-ts/src/index.ts @@ -9,6 +9,7 @@ export { addAdminIdentity, removeAdminIdentity } from './submodule/auth.js'; export { cancelEmail, getEmail, + getWebhookEvent, listDeliveryEventsForEmail, listEmailsByOrgId, listEmailsByStatus, diff --git a/spacetime-resend-ts/src/submodule.ts b/spacetime-resend-ts/src/submodule.ts index 1c376244850..6df49341a3f 100644 --- a/spacetime-resend-ts/src/submodule.ts +++ b/spacetime-resend-ts/src/submodule.ts @@ -17,6 +17,7 @@ export { sendEmail, cancelEmail, getEmail, + getWebhookEvent, listEmailsByUserId, listEmailsByOrgId, listEmailsByStatus, diff --git a/spacetime-resend-ts/src/submodule/operations.ts b/spacetime-resend-ts/src/submodule/operations.ts index 5e3bd9291c7..e5ba032dc11 100644 --- a/spacetime-resend-ts/src/submodule/operations.ts +++ b/spacetime-resend-ts/src/submodule/operations.ts @@ -4,6 +4,7 @@ import { emailStatus, resendDeliveryEventTable, resendEmailTable, + resendWebhookEventTable, sendEmailResult, spacetimedb, t, @@ -289,6 +290,17 @@ export const getEmail = spacetimedb.procedure( } ); +export const getWebhookEvent = spacetimedb.procedure( + { eventId: t.string() }, + t.option(resendWebhookEventTable.rowType), + (ctx, { eventId }) => { + requireProcedureAdmin(ctx); + return ctx.withTx( + tx => tx.db.resendWebhookEvent.eventId.find(eventId) ?? undefined + ); + } +); + export const listEmailsByUserId = spacetimedb.procedure( { userId: t.string() }, t.array(resendEmailTable.rowType), diff --git a/spacetime-resend-ts/src/submodule/webhooks.ts b/spacetime-resend-ts/src/submodule/webhooks.ts index e0697b4fc5a..b249d22d20d 100644 --- a/spacetime-resend-ts/src/submodule/webhooks.ts +++ b/spacetime-resend-ts/src/submodule/webhooks.ts @@ -246,11 +246,15 @@ const MAX_WEBHOOK_BODY_LENGTH = 1024 * 1024; const MAX_WEBHOOK_HEADER_LENGTH = 8192; const MAX_WEBHOOK_METADATA_LENGTH = 255; +type WebhookRejection = { status: number; code: string }; + // The reducer and HTTP route verify, store, and apply events in one transaction. +// Returns a rejection when the request is refused before anything is written, +// otherwise the stored event status. function applyResendWebhook( ctx: WriteCtx, args: Omit -): { status: number; code: string } { +): WebhookRejection | WebhookEventStatusValue { if ( args.eventId.length === 0 || args.eventId.length > MAX_WEBHOOK_METADATA_LENGTH @@ -300,7 +304,7 @@ function applyResendWebhook( existing?.status.tag === 'Processed' || existing?.status.tag === 'Ignored' ) { - return { status: 200, code: 'ok' }; + return existing.status; } if (!existing) @@ -327,9 +331,7 @@ function applyResendWebhook( outcome.status, outcome.error ); - return outcome.status.tag === 'Failed' - ? { status: 400, code: errors.webhookPayloadInvalid } - : { status: 200, code: 'ok' }; + return outcome.status; } export const ingestResendWebhook = spacetimedb.reducer( @@ -344,8 +346,9 @@ export const ingestResendWebhook = spacetimedb.reducer( if (eventType !== parseResendEventType(args.payloadJson)) { throwSenderError(errors.webhookMetadataMismatch); } + // A Failed event row commits so it can be read and replayed. const result = applyResendWebhook(ctx, args); - if (result.status !== 200) throwSenderError(result.code); + if ('code' in result) throwSenderError(result.code); } ); @@ -378,10 +381,11 @@ export function makeResendWebhookHandler() { timestampHeader: req.headers.get('svix-timestamp') ?? undefined, }; const result = ctx.withTx(tx => applyResendWebhook(tx as WriteCtx, args)); - return webhookJson( - { ok: result.status === 200, code: result.code }, - result.status - ); + if ('code' in result) + return webhookJson({ ok: false, code: result.code }, result.status); + return result.tag === 'Failed' + ? webhookJson({ ok: false, code: errors.webhookPayloadInvalid }, 400) + : webhookJson({ ok: true, code: 'ok' }, 200); }; } From 95cd82cf4b93ab08e0c59226b3ef897127c399a4 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Wed, 30 Sep 2026 12:42:46 -0400 Subject: [PATCH 14/18] Store Resend admin grant time as a timestamp resend_admin_identity.added_at_micros (i64) becomes added_at (timestamp). Existing databases need a fresh publish. --- spacetime-resend-ts/src/submodule/auth.ts | 2 +- spacetime-resend-ts/src/submodule/install.ts | 2 +- spacetime-resend-ts/src/submodule/schema.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spacetime-resend-ts/src/submodule/auth.ts b/spacetime-resend-ts/src/submodule/auth.ts index 5ea77d51f9c..83d435636b0 100644 --- a/spacetime-resend-ts/src/submodule/auth.ts +++ b/spacetime-resend-ts/src/submodule/auth.ts @@ -40,7 +40,7 @@ export const addAdminIdentity = spacetimedb.procedure( if (tx.db.resendAdminIdentity.identity.find(identity) == null) { tx.db.resendAdminIdentity.insert({ identity, - addedAtMicros: ctx.timestamp.microsSinceUnixEpoch, + addedAt: ctx.timestamp, }); } }); diff --git a/spacetime-resend-ts/src/submodule/install.ts b/spacetime-resend-ts/src/submodule/install.ts index 167bc680ec2..493ec8558cf 100644 --- a/spacetime-resend-ts/src/submodule/install.ts +++ b/spacetime-resend-ts/src/submodule/install.ts @@ -4,6 +4,6 @@ export function install(ctx: ReducerModuleCtx) { if (ctx.db.resendAdminIdentity.identity.find(ctx.sender) != null) return; ctx.db.resendAdminIdentity.insert({ identity: ctx.sender, - addedAtMicros: ctx.timestamp.microsSinceUnixEpoch, + addedAt: ctx.timestamp, }); } diff --git a/spacetime-resend-ts/src/submodule/schema.ts b/spacetime-resend-ts/src/submodule/schema.ts index 209f7af6855..44083445f4f 100644 --- a/spacetime-resend-ts/src/submodule/schema.ts +++ b/spacetime-resend-ts/src/submodule/schema.ts @@ -112,7 +112,7 @@ export const resendConfigRow = { // Fresh publishes seed the owner via init; public procedures never bootstrap admin state. export const resendAdminIdentityRow = { identity: t.identity().primaryKey(), - addedAtMicros: t.i64(), + addedAt: t.timestamp(), }; export const resendEmailTable = table( From 1567686014d2489539db8074d213e28ceed69d88 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Wed, 30 Sep 2026 12:43:33 -0400 Subject: [PATCH 15/18] Regenerate Resend example bindings --- .../example/src/module_bindings/index.ts | 5 ++++- .../resend/get_webhook_event_procedure.ts | 20 +++++++++++++++++++ .../src/module_bindings/resend/types.ts | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 spacetime-resend-ts/example/src/module_bindings/resend/get_webhook_event_procedure.ts diff --git a/spacetime-resend-ts/example/src/module_bindings/index.ts b/spacetime-resend-ts/example/src/module_bindings/index.ts index fe5b4dee92d..4d2fadb97d6 100644 --- a/spacetime-resend-ts/example/src/module_bindings/index.ts +++ b/spacetime-resend-ts/example/src/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.11.0 (commit 094df0ed2107fe3a359f5e4ee7a4c344802fc96c). +// This was generated using spacetimedb cli version 2.11.0 (commit dfdccc7e468cc3d378901eac5ee498b4243f4442). /* eslint-disable */ /* tslint:disable */ @@ -62,6 +62,7 @@ import * as Resend_AddAdminIdentityProcedure from "./resend/add_admin_identity_p import * as Resend_CancelEmailProcedure from "./resend/cancel_email_procedure"; import * as Resend_GetEmailProcedure from "./resend/get_email_procedure"; import * as Resend_GetResendConfigStatusProcedure from "./resend/get_resend_config_status_procedure"; +import * as Resend_GetWebhookEventProcedure from "./resend/get_webhook_event_procedure"; import * as Resend_ListDeliveryEventsForEmailProcedure from "./resend/list_delivery_events_for_email_procedure"; import * as Resend_ListEmailsByOrgIdProcedure from "./resend/list_emails_by_org_id_procedure"; import * as Resend_ListEmailsByStatusProcedure from "./resend/list_emails_by_status_procedure"; @@ -130,6 +131,7 @@ const proceduresSchema = __procedures( __procedureSchema("resend.cancel_email", Resend_CancelEmailProcedure.params, Resend_CancelEmailProcedure.returnType, "resend.cancelEmail"), __procedureSchema("resend.get_email", Resend_GetEmailProcedure.params, Resend_GetEmailProcedure.returnType, "resend.getEmail"), __procedureSchema("resend.get_resend_config_status", Resend_GetResendConfigStatusProcedure.params, Resend_GetResendConfigStatusProcedure.returnType, "resend.getResendConfigStatus"), + __procedureSchema("resend.get_webhook_event", Resend_GetWebhookEventProcedure.params, Resend_GetWebhookEventProcedure.returnType, "resend.getWebhookEvent"), __procedureSchema("resend.list_delivery_events_for_email", Resend_ListDeliveryEventsForEmailProcedure.params, Resend_ListDeliveryEventsForEmailProcedure.returnType, "resend.listDeliveryEventsForEmail"), __procedureSchema("resend.list_emails_by_org_id", Resend_ListEmailsByOrgIdProcedure.params, Resend_ListEmailsByOrgIdProcedure.returnType, "resend.listEmailsByOrgId"), __procedureSchema("resend.list_emails_by_status", Resend_ListEmailsByStatusProcedure.params, Resend_ListEmailsByStatusProcedure.returnType, "resend.listEmailsByStatus"), @@ -196,6 +198,7 @@ export const procedures = { cancelEmail: __procedureAccessors["resend.cancelEmail"], getEmail: __procedureAccessors["resend.getEmail"], getResendConfigStatus: __procedureAccessors["resend.getResendConfigStatus"], + getWebhookEvent: __procedureAccessors["resend.getWebhookEvent"], listDeliveryEventsForEmail: __procedureAccessors["resend.listDeliveryEventsForEmail"], listEmailsByOrgId: __procedureAccessors["resend.listEmailsByOrgId"], listEmailsByStatus: __procedureAccessors["resend.listEmailsByStatus"], diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/get_webhook_event_procedure.ts b/spacetime-resend-ts/example/src/module_bindings/resend/get_webhook_event_procedure.ts new file mode 100644 index 00000000000..8421d69c668 --- /dev/null +++ b/spacetime-resend-ts/example/src/module_bindings/resend/get_webhook_event_procedure.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from "spacetimedb"; + +import { + ResendWebhookEvent, +} from "./types"; + +export const params = { + eventId: __t.string(), +}; +export const returnType = __t.option(ResendWebhookEvent) \ No newline at end of file diff --git a/spacetime-resend-ts/example/src/module_bindings/resend/types.ts b/spacetime-resend-ts/example/src/module_bindings/resend/types.ts index 9dd3cba4631..505864052b1 100644 --- a/spacetime-resend-ts/example/src/module_bindings/resend/types.ts +++ b/spacetime-resend-ts/example/src/module_bindings/resend/types.ts @@ -24,7 +24,7 @@ export type EmailStatus = __Infer; export const ResendAdminIdentity = __t.object("ResendAdminIdentity", { identity: __t.identity(), - addedAtMicros: __t.i64(), + addedAt: __t.timestamp(), }); export type ResendAdminIdentity = __Infer; From cc045bb96404af4a4d17eb08ef562804b4d62c82 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Wed, 30 Sep 2026 12:56:36 -0400 Subject: [PATCH 16/18] Use the example-ui package in examples --- pnpm-lock.yaml | 4 ++-- spacetime-resend-ts/example/package.json | 2 +- spacetime-resend-ts/example/server.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 05ec5dfee8a..0bf8d497e0f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -585,9 +585,9 @@ importers: spacetime-resend-ts/example: dependencies: - '@spacetimedb/submodule-shared': + '@spacetimedb/example-ui': specifier: workspace:* - version: link:../../spacetime-submodule-shared-ts + version: link:../../spacetime-example-ui-ts dotenv: specifier: ^16.4.7 version: 16.6.1 diff --git a/spacetime-resend-ts/example/package.json b/spacetime-resend-ts/example/package.json index d7ced031655..2dc1f7f6aa2 100644 --- a/spacetime-resend-ts/example/package.json +++ b/spacetime-resend-ts/example/package.json @@ -14,7 +14,7 @@ "dev": "pnpm run build && tsx server.ts" }, "dependencies": { - "@spacetimedb/submodule-shared": "workspace:*", + "@spacetimedb/example-ui": "workspace:*", "dotenv": "^16.4.7", "express": "^4.21.2", "spacetimedb": "workspace:*" diff --git a/spacetime-resend-ts/example/server.ts b/spacetime-resend-ts/example/server.ts index 4d3412e9dde..5232d87182a 100644 --- a/spacetime-resend-ts/example/server.ts +++ b/spacetime-resend-ts/example/server.ts @@ -9,7 +9,7 @@ import { grantServerIdentity, loadServerToken, saveServerToken, -} from '@spacetimedb/submodule-shared/server'; +} from '@spacetimedb/example-ui/server'; import { DbConnection, type ErrorContext } from './src/module_bindings'; const __filename = fileURLToPath(import.meta.url); From 41d46acd34c3939d1b491fc530c6175cd81bdb55 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Thu, 1 Oct 2026 11:40:33 -0400 Subject: [PATCH 17/18] Make Resend README lead with sending and delivery tracking --- spacetime-resend-ts/README.md | 68 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md index c822b3170b3..8147b1c8ee9 100644 --- a/spacetime-resend-ts/README.md +++ b/spacetime-resend-ts/README.md @@ -1,10 +1,11 @@ # @spacetimedb/resend -A SpacetimeDB submodule for transactional email via [Resend](https://resend.com): -admin-gated outbound delivery, idempotent webhook ingest, private delivery -state, synchronous procedures, and valibot-validated webhook payloads. +Send email from your SpacetimeDB application through [Resend](https://resend.com). +Use it for welcome messages, receipts, and notifications, then track whether +each message was delivered, delayed, or bounced. ---- +The submodule stores delivery status in your database and updates it from +Resend webhooks. Your application decides who can send email and see its history. ## Install @@ -23,8 +24,7 @@ This submodule can be published directly as its own SpacetimeDB module from the ### Integrate into an application -Register Resend in the host schema, initialize its private state, and expose only -application-authorized send procedures and caller-scoped delivery views: +Add Resend to your module and initialize it: ```ts import { schema } from 'spacetimedb/server'; @@ -38,15 +38,36 @@ export const init = spacetimedb.init(ctx => { }); ``` -Provider configuration must run as the publishing owner or a registered Resend -administrator. See the -[Dispatch host module](./example/spacetimedb/) -for a narrow send procedure, scoped views, and signed webhook routing. +After publishing, configure your Resend API key, sender address, and webhook +signing secret as the database owner or a Resend administrator. When mounted +as `resend`, use `resend.set_resend_config`; the arguments are shown in +[Standalone configuration](#standalone-configuration). + +Call `resend.sendEmailRequest` from a procedure that checks who may send email +and which recipients they may contact. Apply rate limits before sending: + +```ts +resend.sendEmailRequest(ctx.as.resend, { + to: ['delivered@resend.dev'], + subject: 'Welcome', + html: '

Hello.

', + tagsJson: JSON.stringify([ + { name: 'userId', value: 'u_123' }, + { name: 'orgId', value: 'launch' }, + ]), +}); +``` + +For delivery updates, register `makeResendWebhookHandler()` on an HTTP route +and add that URL in Resend. See the [Dispatch example](./example/) for a +complete send procedure, webhook route, and views that show each user only +their own email history. ### Standalone configuration -Resend credentials live in a private `resend_config` singleton. During `init`, a -fresh database seeds the owner into the private `resend_admin_identity` table. +When running Resend as its own database, configure it with the unprefixed +operation below. Credentials are private, and the publishing owner is the +first administrator. ```bash spacetime call --server http://127.0.0.1:3000 spacetime-resend set_resend_config \ @@ -104,27 +125,8 @@ fields are capped at 320 characters, subjects at 998 characters, HTML and text at 200,000 characters each, and tag or header JSON at 16 KiB. Control characters in address, subject, and schedule fields are rejected before provider HTTP. -Host modules should prefer the submodule helper export: - -```ts -import * as resend from '@spacetimedb/resend/submodule'; - -resend.sendEmailRequest(ctx.as.resend, { - to: ['delivered@resend.dev'], - subject: 'Welcome', - html: '

Hello.

', - tagsJson: JSON.stringify([ - { name: 'userId', value: 'u_123' }, - { name: 'orgId', value: 'launch' }, - ]), -}); -``` - -That lets the host app own product-specific authorization and workflow while -the submodule owns config, delivery rows, and webhook ingest. - -Expose that helper through a product-facing procedure with recipient policy and -rate limits. The generated client then calls the wrapper: +The generated client calls your application's send procedure, such as the +`sendDispatch` procedure in the example: ```ts const result = await conn.procedures.sendDispatch({ From 4bbea3de81f14b2ad8105809b54b6e479067fc91 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Fri, 2 Oct 2026 12:50:21 -0400 Subject: [PATCH 18/18] docs: simplify email submodule READMEs --- spacetime-resend-ts/README.md | 22 +-- spacetime-resend-ts/example/README.md | 205 ++++++++------------------ 2 files changed, 62 insertions(+), 165 deletions(-) diff --git a/spacetime-resend-ts/README.md b/spacetime-resend-ts/README.md index 8147b1c8ee9..7ba61661644 100644 --- a/spacetime-resend-ts/README.md +++ b/spacetime-resend-ts/README.md @@ -15,14 +15,7 @@ npm install @spacetimedb/resend spacetimedb Requires SpacetimeDB 2.8.3 or later for submodule mounting. -For the install-to-publish workflow, see -[Getting started](https://spacetimedb.com/docs/). - -This submodule can be published directly as its own SpacetimeDB module from the root entry point. - -## Usage - -### Integrate into an application +## Integrate into an application Add Resend to your module and initialize it: @@ -86,8 +79,6 @@ Verify: spacetime call --server http://127.0.0.1:3000 spacetime-resend get_resend_config_status '{}' ``` -`send_email` reads the Resend API key from private module state. - ## Private tables | Table | Key | Notes | @@ -144,7 +135,7 @@ operators. **Webhook ingest / replay** -- `ingest_resend_webhook(eventId, eventType, payloadJson, signatureHeader, timestampHeader)` - idempotent +- `ingest_resend_webhook(eventId, eventType, payloadJson, signatureHeader, timestampHeader)` - deduplicates events by the `svix-id` header passed as `eventId` - `replay_webhook_event(eventId)` - re-applies a stored event - `makeResendWebhookHandler()` builds a direct HTTP webhook handler for a host router. @@ -240,13 +231,6 @@ spacetime call --server http://127.0.0.1:3000 spacetime-resend send_email \ The standalone module has no HTTP route. To receive webhooks from Resend, mount the submodule in a host module that registers `makeResendWebhookHandler()` on a router, as the [example](./example/spacetimedb/) does at `/webhook/resend`. Expose the database through a public tunnel such as ngrok and register `https:///v1/database//route/webhook/resend` in Resend's dashboard with the same `whsec_...` you passed to `set_resend_config`. -## Architecture notes - -- **valibot for runtime validation.** `vEmailEvent` is a `v.variant('type', [...])` over the 8 supported event types; other event types are stored as `Ignored`. The unit and smoke suites lock down the accepted wire shapes. -- **Synchronous HTTP.** Procedures are synchronous and `ctx.http.fetch` returns a `SyncResponse`, so `callResend` in `src/submodule/http.ts` implements the required API surface directly. -- **Wire format.** The public input uses SDK-style camelCase (`replyTo`, `scheduledAt`), and `buildSendEmailBody` emits the provider's snake_case JSON fields. -- **Idempotency.** Each webhook event is keyed by its `svix-id` header (the reducer's `eventId` argument); re-ingesting a processed or ignored event is a no-op. Status-changing events ratchet forward, so `email.complained` preserves a terminal `delivered` status. - ## Testing ```bash @@ -254,8 +238,6 @@ pnpm test pnpm run lint ``` -Credentialed smoke coverage is described in **Integration testing** above. - ## License Apache-2.0. diff --git a/spacetime-resend-ts/example/README.md b/spacetime-resend-ts/example/README.md index ec872ef1053..562dbb2de2f 100644 --- a/spacetime-resend-ts/example/README.md +++ b/spacetime-resend-ts/example/README.md @@ -1,179 +1,94 @@ -# Dispatch +# Dispatch email example -Dispatch demonstrates a host module that mounts -`@spacetimedb/resend/submodule`: compose an email, send it through Resend, and -watch verified delivery events stream back through SpacetimeDB. Provider credentials -and submodule administration remain outside the browser. +Send an email with Resend and watch its delivery status update in the app. -## What this demonstrates +## Run it locally -- Mounting the Resend submodule under the `resend` namespace. -- Calling a host `send_dispatch` procedure backed by a private API key. -- Showing caller-scoped email and delivery-event views in real time. -- Receiving Resend webhooks through a native SpacetimeDB HTTP route. -- Verifying Svix signatures inside the module with `@spacetimedb/crypto`. -- Persisting and explicitly authorizing a dedicated local server identity. +Requires Node.js 20+, pnpm 10, and the SpacetimeDB CLI and server built from +this checkout. -## Prerequisites +Start SpacetimeDB in a separate terminal: -- Node.js 20 or later and pnpm 10. -- A SpacetimeDB CLI and server built from this checkout, with the CLI available as `spacetime`. -- A local SpacetimeDB server reachable as `local`. -- A logged-in CLI identity that publishes the database. -- A Resend API key. -- A Resend webhook signing secret for delivery-status updates. - -This example uses the workspace SDK. Keep the matching local server running in -a separate terminal: - -```powershell +```bash spacetime start ``` -```powershell -spacetime server ping local -spacetime login show -``` +From `spacetime-resend-ts/example`, copy [.env.example](./.env.example) to `.env`. -## Quick start +You also need a Resend API key. In `.env`, replace `RESEND_API_KEY` with your +key. Leave `RESEND_WEBHOOK_SECRET` empty until you set up a webhook below. +Remove the sample `RESEND_ALLOWED_RECIPIENTS` address or replace it with an +address you control. -From `spacetime-resend-ts/example`: +Then publish the example and start its web server: -```powershell +```bash pnpm install -pnpm --dir spacetimedb install -node -e "require('node:fs').copyFileSync('.env.example', '.env')" -``` - -Set `RESEND_API_KEY` and, for webhooks, `RESEND_WEBHOOK_SECRET` in `.env`. - -```powershell -pnpm run build:module:fresh +pnpm run build:module pnpm run dev ``` -Open . The **Delivered**, **Bounced**, and **Complaint** -buttons fill Resend's test addresses. Sending creates a real Resend test request; -delivery-state transitions require a reachable webhook. - -`build:module:fresh` deletes and recreates only the local `spacetime-resend-example` -database. Use `pnpm run build:module` to preserve existing data. - -## Use in your project - -This workspace tests the submodule source in this repository. Consumer applications install published releases: +Open . -```bash -npm install @spacetimedb/resend @spacetimedb/rate-limit spacetimedb -``` - -Follow the package's -[integration guide](../README.md#integrate-into-an-application). Copy the -authorized send procedure, caller-scoped views, and signed webhook route; -replace the example's development identity bootstrap in production. +## Try it -## Configuration +1. Click **Delivered** to use Resend's test recipient. +2. Write a subject and message, then send the email. +3. The message appears in the list. To see it change to Delivered, complete + the webhook setup below. +4. Try the **Bounced** and **Complaint** test recipients to see other outcomes. -| Variable | Default | Purpose | -| --------------------------- | -------------------------- | ----------------------------------------------------------------------------------------------- | -| `RESEND_API_KEY` | empty | Required to send mail. | -| `RESEND_WEBHOOK_SECRET` | empty | Required to accept webhook requests. Missing or invalid signatures are rejected. | -| `RESEND_ALLOWED_RECIPIENTS` | empty | Optional comma-separated external recipients. Resend test addresses are included automatically. | -| `DEFAULT_FROM` | `onboarding@resend.dev` | Default sender; use a verified address outside Resend's test flow. | -| `STDB_URI` | `ws://127.0.0.1:3000` | Browser and server WebSocket endpoint. | -| `STDB_HTTP` | `http://127.0.0.1:3000` | CLI and native-route HTTP endpoint. Must match `STDB_URI`. | -| `SPACETIMEDB_DB_NAME` | `spacetime-resend-example` | Published database name. | -| `STDB_SERVER_TOKEN` | generated locally | Optional pre-provisioned server identity token. | -| `HOST` | `127.0.0.1` | Static-server bind address. | -| `PORT` | `8790` | Static-server port. | +To send to a real address, add it to +`RESEND_ALLOWED_RECIPIENTS` and restart the example server. Use a sender that +Resend permits for your account; `DEFAULT_FROM` starts as +`onboarding@resend.dev`. -When `STDB_SERVER_TOKEN` is unset, the server stores its identity token in the -ignored `.stdb-server-token` file. The logged-in publishing identity registers it -with `resend.add_admin_identity` before the server writes private configuration. -Configuration failure is fatal when an API key was supplied; the server will not -pretend to be ready with an unauthorized identity. +## Receive delivery updates -## Webhooks +1. Use a tunnel to expose the example's `/webhook/resend` route. +2. Add that public URL as a webhook endpoint in Resend and select the email + events you want to receive. +3. Copy the endpoint's signing secret into `RESEND_WEBHOOK_SECRET`. +4. Restart `pnpm run dev` and send another test email. -The native module endpoint is: +The local route is `http://127.0.0.1:8790/webhook/resend`. You can also forward +directly to the database route: ```text -POST http://127.0.0.1:3000/v1/database/spacetime-resend-example/route/webhook/resend +http://127.0.0.1:3000/v1/database/spacetime-resend-example/route/webhook/resend ``` -The Node server also exposes: +The app rejects webhook requests with a missing or invalid signature. +Without a reachable webhook, email status stays Queued. -```text -POST http://127.0.0.1:8790/webhook/resend -``` +## Before deploying -That route is a raw-body passthrough for stable tunnel URLs. Signature verification -and ingestion still occur inside the SpacetimeDB module. The module rejects requests -when no signing secret is configured or when the signature is invalid; there is no -unverified development fallback. +Anyone who can open this demo can send to the allowed recipients. It limits +each browser identity to five sends per ten minutes and all users to 25 per +hour. Add account-based access checks before making the app public. Expose +only the webhook route when testing through a tunnel. -Without a publicly reachable webhook, sending still creates a queued email row, but -Delivered, Opened, Clicked, Bounced, and Complaint transitions cannot arrive. +The server saves its admin credential in `.stdb-server-token`. Keep that file +and `.env` private. -## Architecture +## Troubleshooting -```text -Browser - -> send_dispatch host procedure - -> recipient allowlist and caller/global quotas - -> resend submodule namespace - -> Resend API - -Resend webhook - -> native SpacetimeDB HTTP route - -> signature verification and ingest - -> caller-scoped live views - -Authorized example server - -> private Resend configuration during startup - -> static UI and optional raw webhook passthrough -``` +- **Sending fails:** check the API key, allowed recipients, and sender address. +- **Status stays Queued:** check the public webhook URL and signing secret. +- **`resend.not_authorized`:** use the CLI account that published the database, + then restart the example server. -## Security and deployment boundaries - -- The browser never receives the Resend API key, signing secret, server token, or - submodule administrator role. -- `send_dispatch` accepts only server-configured recipients. It allows five sends - per caller every ten minutes and 25 sends globally per hour. -- Provider failures return a stable application error while detailed delivery - state remains in private submodule tables. -- `.env` and `.stdb-server-token` are ignored and must not be committed. -- The development server binds to loopback by default. -- A production deployment should provision its service identity and secret store - through deployment infrastructure. - -## Verification - -```powershell -pnpm --dir spacetimedb run build -pnpm run build -pnpm exec tsc -p tsconfig.json -``` +## Change the example -For a provider smoke test, send to `delivered@resend.dev` and confirm a queued row -appears. With a reachable signed webhook, confirm it advances to Delivered. Invalid -or unsigned webhook fixtures must return a non-success status. +- [spacetimedb/src/index.ts](./spacetimedb/src/index.ts): email sending and delivery updates. +- [server.ts](./server.ts): Resend setup and webhook forwarding. +- [src/app.ts](./src/app.ts): email form and message list. -## Troubleshooting +After changing server code, run `pnpm run build:module`. Restart +`pnpm run dev` after changing browser code or `.env`. + +To start over, run `pnpm run build:module:fresh`. **This deletes all data in +the local `spacetime-resend-example` database.** -- **`resend.not_authorized`:** publish with the logged-in CLI identity and restart - so it can authorize the persistent server identity. -- **Send remains Queued:** confirm the webhook is publicly reachable and its signing - secret matches `RESEND_WEBHOOK_SECRET`. -- **Connection targets disagree:** make `STDB_URI`, `STDB_HTTP`, and the publish - target refer to the same SpacetimeDB instance. -- **Replace the server identity:** stop the server, remove - `.stdb-server-token`, and restart while logged in as a database administrator. - -## Important files - -- `spacetimedb/src/index.ts`: host procedures, caller-scoped views, and native route. -- `server.ts`: safe startup configuration, server identity, and webhook passthrough. -- `src/app.ts`: browser connection and Dispatch UI behavior. -- `public/index.html`: Dispatch interface structure. -- `public/styles.css`: Dispatch presentation. +To use the submodule in your own app, see the +[package integration guide](../README.md#integrate-into-an-application).