@drfed/models 0.0.0-stage → 0.1.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +230 -2
- package/dist/activity-delivery.d.mts +111 -0
- package/dist/activity-delivery.d.mts.map +1 -0
- package/dist/activity-delivery.mjs +240 -0
- package/dist/activity-delivery.mjs.map +1 -0
- package/dist/db-DtcLuISw.d.mts +3098 -0
- package/dist/db-DtcLuISw.d.mts.map +1 -0
- package/dist/db.d.mts +2 -0
- package/dist/db.mjs +3 -0
- package/dist/email.d.mts +39 -0
- package/dist/email.d.mts.map +1 -0
- package/dist/email.mjs +12 -0
- package/dist/email.mjs.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.mjs +18 -0
- package/dist/instance.d.mts +11 -0
- package/dist/instance.d.mts.map +1 -0
- package/dist/instance.mjs +21 -0
- package/dist/instance.mjs.map +1 -0
- package/dist/key.d.mts +2 -0
- package/dist/key.mjs +86 -0
- package/dist/key.mjs.map +1 -0
- package/dist/login.d.mts +49 -0
- package/dist/login.d.mts.map +1 -0
- package/dist/login.mjs +71 -0
- package/dist/login.mjs.map +1 -0
- package/dist/migrate.d.mts +46 -0
- package/dist/migrate.d.mts.map +1 -0
- package/dist/migrate.mjs +58 -0
- package/dist/migrate.mjs.map +1 -0
- package/dist/migration-manifest.d.mts +39 -0
- package/dist/migration-manifest.d.mts.map +1 -0
- package/dist/migration-manifest.mjs +77 -0
- package/dist/migration-manifest.mjs.map +1 -0
- package/dist/migration-session-B4xHeU_a.d.mts +56 -0
- package/dist/migration-session-B4xHeU_a.d.mts.map +1 -0
- package/dist/migration-session.d.mts +2 -0
- package/dist/migration-session.mjs +204 -0
- package/dist/migration-session.mjs.map +1 -0
- package/dist/migration-snapshot-OM5jQN1-.mjs +69 -0
- package/dist/migration-snapshot-OM5jQN1-.mjs.map +1 -0
- package/dist/migration-state.d.mts +41 -0
- package/dist/migration-state.d.mts.map +1 -0
- package/dist/migration-state.mjs +155 -0
- package/dist/migration-state.mjs.map +1 -0
- package/dist/migrations-manifest.json +109 -0
- package/dist/pglite-preparation.d.mts +23 -0
- package/dist/pglite-preparation.d.mts.map +1 -0
- package/dist/pglite-preparation.mjs +77 -0
- package/dist/pglite-preparation.mjs.map +1 -0
- package/dist/pglite-store.d.mts +29 -0
- package/dist/pglite-store.d.mts.map +1 -0
- package/dist/pglite-store.mjs +117 -0
- package/dist/pglite-store.mjs.map +1 -0
- package/dist/relations.d.mts +2 -0
- package/dist/relations.mjs +307 -0
- package/dist/relations.mjs.map +1 -0
- package/dist/resource.d.mts +50 -0
- package/dist/resource.d.mts.map +1 -0
- package/dist/resource.mjs +123 -0
- package/dist/resource.mjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/dist/schema.d.mts +2 -0
- package/dist/schema.mjs +495 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/slug.d.mts +28 -0
- package/dist/slug.d.mts.map +1 -0
- package/dist/slug.mjs +55 -0
- package/dist/slug.mjs.map +1 -0
- package/dist/text.d.mts +10 -0
- package/dist/text.d.mts.map +1 -0
- package/dist/text.mjs +11 -0
- package/dist/text.mjs.map +1 -0
- package/dist/uuid-CKMzlIND.d.mts +26 -0
- package/dist/uuid-CKMzlIND.d.mts.map +1 -0
- package/dist/uuid.d.mts +2 -0
- package/dist/uuid.mjs +28 -0
- package/dist/uuid.mjs.map +1 -0
- package/drizzle/20260622021748_init/migration.sql +25 -0
- package/drizzle/20260622021748_init/snapshot.json +246 -0
- package/drizzle/20260623162238_dear_felicia_hardy/migration.sql +5 -0
- package/drizzle/20260623162238_dear_felicia_hardy/snapshot.json +321 -0
- package/drizzle/20260624094817_lethal_sinister_six/migration.sql +2 -0
- package/drizzle/20260624094817_lethal_sinister_six/snapshot.json +347 -0
- package/drizzle/20260716000333_email_auth/migration.sql +20 -0
- package/drizzle/20260716000333_email_auth/snapshot.json +575 -0
- package/drizzle/20260723065002_chief_colleen_wing/migration.sql +2 -0
- package/drizzle/20260723065002_chief_colleen_wing/snapshot.json +595 -0
- package/drizzle/20260802205417_separate-instance-local-remote/migration.sql +26 -0
- package/drizzle/20260802205417_separate-instance-local-remote/snapshot.json +768 -0
- package/drizzle/20260804061119_instances-local-remote-constraint/migration.sql +7 -0
- package/drizzle/20260804061119_instances-local-remote-constraint/snapshot.json +843 -0
- package/drizzle/20260818080259_dark_night_thrasher/migration.sql +15 -0
- package/drizzle/20260818080259_dark_night_thrasher/snapshot.json +722 -0
- package/drizzle/20260821072150_merge_remote_actors/migration.sql +54 -0
- package/drizzle/20260821072150_merge_remote_actors/snapshot.json +1312 -0
- package/drizzle/20260906052949_rename_followees_to_following/migration.sql +2 -0
- package/drizzle/20260906052949_rename_followees_to_following/snapshot.json +1315 -0
- package/drizzle/20260908062446_rename_login_tokens_to_login_challenges/migration.sql +1 -0
- package/drizzle/20260908062446_rename_login_tokens_to_login_challenges/snapshot.json +1312 -0
- package/drizzle/20260908065945_simplify_login_challenges/migration.sql +7 -0
- package/drizzle/20260908065945_simplify_login_challenges/snapshot.json +1290 -0
- package/drizzle/20260915061356_tighten_slug_constraint/migration.sql +12 -0
- package/drizzle/20260915061356_tighten_slug_constraint/snapshot.json +1290 -0
- package/drizzle/20260915063339_widen_instance_host/migration.sql +1 -0
- package/drizzle/20260915063339_widen_instance_host/snapshot.json +1290 -0
- package/drizzle/20260916085902_add_objects_activities_and_resources/migration.sql +110 -0
- package/drizzle/20260916085902_add_objects_activities_and_resources/snapshot.json +2263 -0
- package/drizzle/20260917163517_order_local_outbox_items/migration.sql +22 -0
- package/drizzle/20260917163517_order_local_outbox_items/snapshot.json +2263 -0
- package/drizzle/20260918071839_add_activity_object_index/migration.sql +1 -0
- package/drizzle/20260918071839_add_activity_object_index/snapshot.json +2298 -0
- package/drizzle/20261006061745_add_activity_deliveries_and_keys/migration.sql +106 -0
- package/drizzle/20261006061745_add_activity_deliveries_and_keys/snapshot.json +3538 -0
- package/drizzle/20261006085045_snake_case_columns/migration.sql +42 -0
- package/drizzle/20261006085045_snake_case_columns/snapshot.json +3570 -0
- package/drizzle/20261006122653_local_actor_keys/migration.sql +11 -0
- package/drizzle/20261006122653_local_actor_keys/snapshot.json +3627 -0
- package/drizzle/20261010044954_link_activity_deliveries_to_activities/migration.sql +8 -0
- package/drizzle/20261010044954_link_activity_deliveries_to_activities/snapshot.json +3714 -0
- package/drizzle/20261010073134_check_inbound_activity_link/migration.sql +1 -0
- package/drizzle/20261010073134_check_inbound_activity_link/snapshot.json +3721 -0
- package/drizzle/20261010104304_record_object_activity/migration.sql +3 -0
- package/drizzle/20261010104304_record_object_activity/snapshot.json +3756 -0
- package/drizzle/20261010111648_record_delivery_snapshot/migration.sql +29 -0
- package/drizzle/20261010111648_record_delivery_snapshot/snapshot.json +3769 -0
- package/drizzle/20261011144453_record_object_snapshot/migration.sql +10 -0
- package/drizzle/20261011144453_record_object_snapshot/snapshot.json +3808 -0
- package/drizzle/20261011192718_record_actor_followers/migration.sql +1 -0
- package/drizzle/20261011192718_record_actor_followers/snapshot.json +3821 -0
- package/package.json +181 -3
package/README.md
CHANGED
|
@@ -1,3 +1,231 @@
|
|
|
1
|
-
|
|
1
|
+
@drfed/models
|
|
2
|
+
=============
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
Database schema, Drizzle ORM types, and migration runner for [DrFed]. Supports
|
|
5
|
+
both PGlite (embedded) and PostgreSQL.
|
|
6
|
+
|
|
7
|
+
[DrFed]: https://drfed.org/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Schema
|
|
11
|
+
------
|
|
12
|
+
|
|
13
|
+
| Table | Key columns |
|
|
14
|
+
| ------------------ | ---------------------------------------------------- |
|
|
15
|
+
| `accounts` | `id` (UUID), `email`, `created` |
|
|
16
|
+
| `instances` | `id` (UUID), `slug`, `expires`, `created` |
|
|
17
|
+
| `instance_members` | `instanceId` → `instances`, `accountId` → `accounts` |
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
Migrations
|
|
21
|
+
----------
|
|
22
|
+
|
|
23
|
+
Generate a migration after changing *src/schema.ts*:
|
|
24
|
+
|
|
25
|
+
~~~~ sh
|
|
26
|
+
mise run generate:migrate --name your_migration_name
|
|
27
|
+
~~~~
|
|
28
|
+
|
|
29
|
+
After reviewing newly generated SQL, register its identity:
|
|
30
|
+
|
|
31
|
+
~~~~ sh
|
|
32
|
+
mise run manifest:migrations --append
|
|
33
|
+
~~~~
|
|
34
|
+
|
|
35
|
+
This appends SHA-256 hashes of raw SQL bytes to *migrations-manifest.json* and
|
|
36
|
+
refuses to rewrite or remove existing entries. Builds verify the SQL against
|
|
37
|
+
this file and copy it to *dist/migrations-manifest.json*. `mise run check`
|
|
38
|
+
checks the same identities, and pull request CI compares the manifest with the
|
|
39
|
+
base commit to catch simultaneous changes to historical SQL and its hash.
|
|
40
|
+
Migration SQL is checked out with LF endings on every platform, so Git's
|
|
41
|
+
`core.autocrlf` setting does not change these raw-byte identities.
|
|
42
|
+
A base commit from before the manifest's introduction starts the baseline.
|
|
43
|
+
|
|
44
|
+
`@drfed/models/migration-manifest` exposes `verifyMigrationManifest()` for
|
|
45
|
+
installed package checks without connecting to a database. These checks do
|
|
46
|
+
not change the migration runner's database behavior.
|
|
47
|
+
|
|
48
|
+
`@drfed/models/migration-state` exposes `inspectMigrationState(client)`.
|
|
49
|
+
It uses only `SELECT` statements, verifies the installed manifest, and never
|
|
50
|
+
owns or closes the supplied query client. Exact named history is `ready`;
|
|
51
|
+
a strict prefix is `pending`. Changed hashes, diverged history, newer
|
|
52
|
+
migrations, unsupported table formats, and DrFed tables without tracked
|
|
53
|
+
history are reported separately. Legacy history must match both generation
|
|
54
|
+
seconds and hashes, and reports `upgrade-required` without altering metadata.
|
|
55
|
+
Named rows from Drizzle's legacy upgrade retain generation milliseconds and
|
|
56
|
+
have SQL `NULL` in `applied_at`; these also match by generation second, name,
|
|
57
|
+
and hash. Other named rows require the exact manifest timestamp.
|
|
58
|
+
Database query failures propagate instead of being treated as a fresh database.
|
|
59
|
+
Connections subject to row-level security on migration history are rejected,
|
|
60
|
+
since filtered rows cannot prove that the history is complete.
|
|
61
|
+
|
|
62
|
+
`@drfed/models/migration-session` exposes `preparePostgresDatabase()` for
|
|
63
|
+
PostgreSQL. It verifies bundled SQL, connects a fresh owned [`pg.Client`], takes
|
|
64
|
+
a session advisory lock, inspects history before any metadata DDL, applies
|
|
65
|
+
verified pending SQL or legacy upgrades, and checks exact history afterward.
|
|
66
|
+
The optional client factory must return an unconnected, exclusive client;
|
|
67
|
+
the runner owns `connect()` and `end()` and never reconnects after failure.
|
|
68
|
+
The runner hashes the exact SQL bytes again after acquiring the lock and gives
|
|
69
|
+
Drizzle a private temporary snapshot, so a concurrent installation change cannot
|
|
70
|
+
supply unverified SQL between validation and application.
|
|
71
|
+
|
|
72
|
+
`lockTimeoutMs` bounds connection and lock acquisition; SQL application is
|
|
73
|
+
not limited by that deadline. `signal` aborts waiting or closes an active
|
|
74
|
+
connection. PostgreSQL can retain an interrupted statement and its lock until
|
|
75
|
+
it observes the disconnect, so another runner continues to wait rather than
|
|
76
|
+
stealing the lock. Migration errors have fixed codes and omit raw DB errors
|
|
77
|
+
and credentials; the `drfed.models.migration` debug category records the
|
|
78
|
+
backend PID after locking. Use a direct database connection or a proxy that
|
|
79
|
+
preserves sessions; transaction pooling is unsupported.
|
|
80
|
+
|
|
81
|
+
`@drfed/models/pglite-preparation` exposes `preparePgliteDatabase({ store })`
|
|
82
|
+
for a store returned by `openPersistentPglite()`. It verifies SQL and history,
|
|
83
|
+
refuses incompatible or untracked schemas before metadata DDL, applies exact
|
|
84
|
+
verified snapshots, then requires ready history. Verified legacy milliseconds
|
|
85
|
+
are preserved during metadata upgrade. Preparers sharing one client are
|
|
86
|
+
serialized, so a later request inspects the first request's committed history.
|
|
87
|
+
|
|
88
|
+
The caller owns the store throughout preparation and must refrain from other
|
|
89
|
+
queries until it settles. Preparation never closes the database or releases
|
|
90
|
+
its native lock, even after failure. `signal` is checked at safe boundaries;
|
|
91
|
+
active WASM SQL settles before an aborted operation completes. Drain preparation
|
|
92
|
+
before `store.close()`.
|
|
93
|
+
|
|
94
|
+
The existing `migrate()` remains a low-level API whose caller must ensure
|
|
95
|
+
exclusive access. The CLI uses the safe preparation APIs described above.
|
|
96
|
+
|
|
97
|
+
For contributor tests, point `DRFED_TEST_POSTGRES_URL` at an isolated
|
|
98
|
+
PostgreSQL cluster with `CREATEDB` and `CREATEROLE` permissions, then run:
|
|
99
|
+
|
|
100
|
+
~~~~ sh
|
|
101
|
+
mise run test:postgres
|
|
102
|
+
~~~~
|
|
103
|
+
|
|
104
|
+
This task creates and removes unique test databases and roles. Ordinary
|
|
105
|
+
`mise run test` skips the service-dependent cases when that variable is absent.
|
|
106
|
+
|
|
107
|
+
The *drizzle/* directory is included in the published npm package so that
|
|
108
|
+
installed users can run migrations without access to the repository source.
|
|
109
|
+
|
|
110
|
+
[`pg.Client`]: https://node-postgres.com/apis/client
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
Persistent PGlite ownership
|
|
114
|
+
---------------------------
|
|
115
|
+
|
|
116
|
+
`@drfed/models/pglite-store` exposes `openPersistentPglite(dataPath)`. It
|
|
117
|
+
creates the directory, resolves its real absolute path, acquires an exclusive
|
|
118
|
+
native file lock, then constructs PGlite. Register the returned `close()`
|
|
119
|
+
immediately and await `ready` before querying. This promise maps asynchronous
|
|
120
|
+
initialization failures to the fixed `PgliteStoreError` code `open-failed`;
|
|
121
|
+
`close()` remains available even after an initialization failure. The shared
|
|
122
|
+
close promise shuts PGlite down before releasing its lock. If database close
|
|
123
|
+
fails, the lock stays held until that owner process exits.
|
|
124
|
+
|
|
125
|
+
[fs-native-extensions] supplies prebuilt Node addons for macOS, Windows and
|
|
126
|
+
Linux on x64/ARM64. Its file-description locks prevent another owner in the
|
|
127
|
+
same or a different process; suspension does not make a lock stale, and process
|
|
128
|
+
exit releases it. Loading an unavailable addon fails closed; there is no
|
|
129
|
+
compiler, heartbeat or PID-based fallback. Use a local filesystem. Network
|
|
130
|
+
filesystems and shared directories across container/PID namespaces are
|
|
131
|
+
unsupported.
|
|
132
|
+
|
|
133
|
+
The stable *.drfed.lock* file stays in the data directory after shutdown. Never
|
|
134
|
+
remove or replace it while an owner may be alive: two different inodes would
|
|
135
|
+
allow separate owners. A leftover file is harmless once the OS has released
|
|
136
|
+
its lock. After a failed close, stop that owner before reopening. Other code
|
|
137
|
+
that directly constructs PGlite bypasses this ownership API; coordinate all
|
|
138
|
+
access through it. The CLI uses this store for persistent PGlite databases.
|
|
139
|
+
|
|
140
|
+
The ordinary test task covers duplicate owners, a simultaneous process race,
|
|
141
|
+
symlink aliases, close ordering and failed-close ownership. POSIX-only tests
|
|
142
|
+
also suspend and forcibly terminate an owned child process before reopening.
|
|
143
|
+
|
|
144
|
+
[fs-native-extensions]: https://github.com/holepunchto/fs-native-extensions
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
Activity delivery storage
|
|
148
|
+
-------------------------
|
|
149
|
+
|
|
150
|
+
`activity_deliveries` stores delivery observations separately from ActivityPub
|
|
151
|
+
`activities`. `@drfed/models/activity-delivery` exposes `recordInbound`,
|
|
152
|
+
`receiveInbound`, `recordOutbound`, and `settleOutbound`. `receiveInbound`
|
|
153
|
+
marks an `acknowledged` inbound delivery `received` once a queued inbox
|
|
154
|
+
listener ran. An inbound delivery keeps the received
|
|
155
|
+
octets in `body` and the request headers in `headers`; `payload` is the body
|
|
156
|
+
parsed as JSON, and is SQL `NULL` when it does not parse or holds U+0000 or an
|
|
157
|
+
unpaired surrogate, which jsonb cannot store. `error` and `response_body`
|
|
158
|
+
are stored with U+FFFD for each U+0000, which text cannot store, so that a
|
|
159
|
+
remote response never keeps a delivery from being written. `created` is when
|
|
160
|
+
a request arrived or a delivery started, and `completed` when DrFed answered
|
|
161
|
+
the request or the delivery last changed status.
|
|
162
|
+
|
|
163
|
+
`settleOutbound` settles the most recent pending (`queued` or `failed`)
|
|
164
|
+
delivery to an inbox and never changes a `sent`, `permanently_failed`, or
|
|
165
|
+
`abandoned` one. Each attempt that ends is kept in
|
|
166
|
+
`activity_delivery_attempts`, so a retry never rewrites the outcome of an
|
|
167
|
+
earlier attempt.
|
|
168
|
+
|
|
169
|
+
`activity_id` links a delivery to the stored activity it carried.
|
|
170
|
+
`linkInboundActivity` links an inbound delivery only when it was verified and
|
|
171
|
+
answered with 2xx, by the activity IRI it recorded; `recordOutbound` takes the
|
|
172
|
+
`activityId` of the activity it sends. `recordInbound` does not take one, and
|
|
173
|
+
a CHECK constraint refuses a linked inbound delivery that is not verified,
|
|
174
|
+
`received` or `acknowledged`. A delivery outlives its activity.
|
|
175
|
+
`carried_snapshot` marks an inbound delivery whose payload is the stored
|
|
176
|
+
activity's document, and `carried_object_snapshot` one that carried the object
|
|
177
|
+
stored with that activity as well: the object was taken from the activity, or
|
|
178
|
+
`objects.fetched` says it was fetched and the delivery's `fetched_object`, what
|
|
179
|
+
its inbox listener fetched, is the stored document. Neither record function
|
|
180
|
+
takes a mark; `linkInboundActivity` decides both anew on every link, and
|
|
181
|
+
`recordFetchedObject` again for a delivery linked before its listener ran, so
|
|
182
|
+
that a mark left from a deleted activity never carries over to another stored
|
|
183
|
+
under the same IRI.
|
|
184
|
+
|
|
185
|
+
`activity_delivery_actors` relates a delivery to each local actor it concerns,
|
|
186
|
+
as the owner of the inbox, as an addressed recipient, or as the sender, with
|
|
187
|
+
one row per delivery and actor. An addressed actor keeps every way it was
|
|
188
|
+
addressed: `addressed_directly` when the activity named the actor itself, and
|
|
189
|
+
a row in `activity_delivery_actor_collections` for each addressed collection
|
|
190
|
+
it was a member of. Each row copies the delivery's `created`, by which an
|
|
191
|
+
actor's deliveries are ordered. Both record functions insert these rows in
|
|
192
|
+
the same transaction as the delivery. They record nothing and throw when the
|
|
193
|
+
inbox owner, the sender, or an addressed actor is not a local actor of the
|
|
194
|
+
delivery's instance, since neither foreign key can tell.
|
|
195
|
+
|
|
196
|
+
`@drfed/models/key` exposes public-JWK validation, RFC 7638/RFC 8037 SHA-256
|
|
197
|
+
thumbprints, and `observeKeyVersion`. `keys` identifies each exact key IRI;
|
|
198
|
+
`key_versions` retains immutable public material per fingerprint. Returning to
|
|
199
|
+
an earlier key reuses its version. Observation timestamps do not describe
|
|
200
|
+
remote rotation times or continuous use. Deliveries retain referenced versions
|
|
201
|
+
with `ON DELETE RESTRICT`, independently of Fedify cache expiry.
|
|
202
|
+
|
|
203
|
+
The `drfed-server` facade now uses these safe preparation APIs for automatic
|
|
204
|
+
migration and `inspectMigrationState()` for `--no-migrate`. Its persistent
|
|
205
|
+
PGlite owner remains held through preparation, serving and database cleanup.
|
|
206
|
+
The older `migrate()` function remains a low-level compatibility API; it does
|
|
207
|
+
not provide this facade ownership and history validation contract.
|
|
208
|
+
|
|
209
|
+
`preparePostgresDatabase` also accepts explicit node-postgres connection
|
|
210
|
+
settings as `credentials.connection`, without a `connectionString`. The facade
|
|
211
|
+
uses this form so that decoded host names, database names, credentials and TLS
|
|
212
|
+
policy are the same during migration and runtime. Both connections explicitly
|
|
213
|
+
verify the configured host identity for TLS, including IP addresses.
|
|
214
|
+
|
|
215
|
+
Installed users can run `drfed-migrate` from `@drfed/drfed` to prepare the
|
|
216
|
+
database; `drfed-migrate --check` only inspects history and exits 0 for ready
|
|
217
|
+
or 1 for every other status/error. Both commands accept the facade's shared
|
|
218
|
+
TOML and database environment/CLI settings. Stop a PGlite server before either
|
|
219
|
+
command because its directory remains exclusively locked. For PostgreSQL
|
|
220
|
+
inspection with a separate runtime role, grant `USAGE` on the migration schema
|
|
221
|
+
and `SELECT` on its history table, for example:
|
|
222
|
+
|
|
223
|
+
~~~~ sql
|
|
224
|
+
GRANT USAGE ON SCHEMA drizzle TO drfed_runtime;
|
|
225
|
+
GRANT SELECT ON TABLE drizzle.__drizzle_migrations TO drfed_runtime;
|
|
226
|
+
~~~~
|
|
227
|
+
|
|
228
|
+
Back up and stop DrFed before an update, install the updated package, run
|
|
229
|
+
`drfed-migrate`, verify `drfed-migrate --check`, then start the server. Keep the
|
|
230
|
+
backup until the updated application has been verified. Installed users do not
|
|
231
|
+
need `drizzle-kit`; it is a developer tool for generating migration SQL.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { M as NewActivityDelivery, N as NewActivityDeliveryActor, o as ActivityDelivery, r as Transaction, t as Database } from "./db-DtcLuISw.mjs";
|
|
2
|
+
import { t as Uuid } from "./uuid-CKMzlIND.mjs";
|
|
3
|
+
//#region src/activity-delivery.d.ts
|
|
4
|
+
/** The marks of what a delivery carried are the model's to decide. */
|
|
5
|
+
type DeliveryEntry = Omit<NewActivityDelivery, "id" | "direction" | "status" | "carriedSnapshot" | "carriedObjectSnapshot">;
|
|
6
|
+
interface AddressedActor {
|
|
7
|
+
readonly actorId: Uuid;
|
|
8
|
+
readonly viaCollectionIri?: string | null;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* `payload` is `undefined` for an unparsable body and `null` for JSON `null`;
|
|
12
|
+
* it is stored as SQL `NULL` when jsonb cannot hold it, and `body` keeps it.
|
|
13
|
+
* `fetchedObject` is stored as SQL `NULL` when jsonb cannot hold it too.
|
|
14
|
+
* An inbound delivery is linked to its activity only by
|
|
15
|
+
* `linkInboundActivity()`.
|
|
16
|
+
*/
|
|
17
|
+
type InboundDeliveryEntry = Omit<DeliveryEntry, "body" | "verificationResult" | "recipientIris" | "activityId"> & {
|
|
18
|
+
/**
|
|
19
|
+
* Chosen in advance when something must name the delivery before it exists.
|
|
20
|
+
*/
|
|
21
|
+
readonly id?: Uuid;
|
|
22
|
+
readonly status: "received" | "acknowledged" | "unverified" | "rejected";
|
|
23
|
+
readonly verificationResult: NonNullable<DeliveryEntry["verificationResult"]>;
|
|
24
|
+
readonly body: Uint8Array;
|
|
25
|
+
readonly addressed?: readonly AddressedActor[];
|
|
26
|
+
/** When the request arrived, before verification and handling. */
|
|
27
|
+
readonly created: Temporal.Instant;
|
|
28
|
+
/** When DrFed answered the request. */
|
|
29
|
+
readonly completed: Temporal.Instant;
|
|
30
|
+
};
|
|
31
|
+
/** `payload` is the document before signing, without `bto` and `bcc`. */
|
|
32
|
+
type OutboundDeliveryEntry = Omit<DeliveryEntry, "verificationKeyId" | "verificationMechanism" | "verificationResult" | "body" | "headers" | "requestUrl" | "completed" | "fetchedObject"> & {
|
|
33
|
+
readonly activityIri: string;
|
|
34
|
+
};
|
|
35
|
+
type ActorRow = Omit<NewActivityDeliveryActor, "deliveryId" | "created"> & {
|
|
36
|
+
/** The addressed collections that reached the actor, sorted. */
|
|
37
|
+
readonly collectionIris?: readonly string[];
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Merge the inbox owner and addressed actors into one row per actor, keeping
|
|
41
|
+
* every way each was addressed, whatever order they come in.
|
|
42
|
+
* @returns The actor rows of an inbound delivery.
|
|
43
|
+
*/
|
|
44
|
+
declare function inboundActorRows(entry: {
|
|
45
|
+
readonly actorId?: Uuid | null | undefined;
|
|
46
|
+
readonly addressed?: readonly AddressedActor[] | undefined;
|
|
47
|
+
}): ActorRow[];
|
|
48
|
+
/**
|
|
49
|
+
* Persist one inbound observation with its actor rows in one transaction.
|
|
50
|
+
* The inbox owner and every addressed actor must be local actors of
|
|
51
|
+
* `instanceId`; otherwise nothing is recorded.
|
|
52
|
+
* @returns The inserted inbound delivery.
|
|
53
|
+
* @throws {Error} When an actor is not a local actor of the instance.
|
|
54
|
+
*/
|
|
55
|
+
declare function recordInbound(db: Database, { addressed, body, id, ...entry }: InboundDeliveryEntry): Promise<ActivityDelivery>;
|
|
56
|
+
/**
|
|
57
|
+
* Record that an inbox listener ran for an inbound delivery answered before it
|
|
58
|
+
* did, as a queued one is.
|
|
59
|
+
* @returns Whether an acknowledged inbound delivery was found.
|
|
60
|
+
*/
|
|
61
|
+
declare function receiveInbound(db: Database, id: Uuid): Promise<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Link an inbound delivery to the stored activity its activity IRI names.
|
|
64
|
+
* Only a delivery Fedify verified and answered with 2xx is linked: Fedify
|
|
65
|
+
* authenticates an activity before accepting it, so its actor has the origin
|
|
66
|
+
* of the activity's ID, whether an inbox listener ran or it was a duplicate.
|
|
67
|
+
* A delivery that is already linked is left as is.
|
|
68
|
+
*
|
|
69
|
+
* The link names the activity whatever the delivery carried; what it carried
|
|
70
|
+
* is decided as the link is made, as `recordFetchedObject()` decides it again.
|
|
71
|
+
* @returns Whether the delivery was linked.
|
|
72
|
+
*/
|
|
73
|
+
declare function linkInboundActivity(db: Database | Transaction, deliveryId: Uuid): Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Record the object an inbound delivery's inbox listener fetched from its IRI,
|
|
76
|
+
* and decide again what the delivery carried if it is linked already, as a
|
|
77
|
+
* duplicate queued for a worker is before its listener runs.
|
|
78
|
+
* @returns Whether the inbound delivery was found.
|
|
79
|
+
*/
|
|
80
|
+
declare function recordFetchedObject(db: Database, deliveryId: Uuid, document: unknown): Promise<boolean>;
|
|
81
|
+
/**
|
|
82
|
+
* Start a delivery observation. The payload is the document before signing.
|
|
83
|
+
* The sender must be a local actor of `instanceId`; otherwise nothing is
|
|
84
|
+
* recorded.
|
|
85
|
+
* @returns The inserted queued outbound delivery.
|
|
86
|
+
* @throws {Error} When the sender is not a local actor of the instance.
|
|
87
|
+
*/
|
|
88
|
+
declare function recordOutbound(db: Database, entry: OutboundDeliveryEntry): Promise<ActivityDelivery>;
|
|
89
|
+
/** What a settled delivery shows; null fields for a successful attempt. */
|
|
90
|
+
interface OutboundSettlement {
|
|
91
|
+
readonly activityIri: string;
|
|
92
|
+
readonly inboxUrl: string;
|
|
93
|
+
readonly status: "sent" | "failed" | "permanently_failed" | "abandoned";
|
|
94
|
+
readonly statusCode?: number | null;
|
|
95
|
+
readonly error?: string | null;
|
|
96
|
+
readonly responseBody?: string | null;
|
|
97
|
+
/** Whether an attempt ended with this result, rather than none being made. */
|
|
98
|
+
readonly attempted: boolean;
|
|
99
|
+
/** Restrict a synchronous delivery to the row started by that invocation. */
|
|
100
|
+
readonly id?: Uuid;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Settle the most recent pending (`queued` or `failed`) delivery to an inbox,
|
|
104
|
+
* keeping each ended attempt. `sent`, `permanently_failed` and `abandoned`
|
|
105
|
+
* deliveries are never changed again.
|
|
106
|
+
* @returns Whether a pending delivery was found.
|
|
107
|
+
*/
|
|
108
|
+
declare function settleOutbound(db: Database, entry: OutboundSettlement): Promise<boolean>;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { AddressedActor, InboundDeliveryEntry, OutboundDeliveryEntry, OutboundSettlement, inboundActorRows, linkInboundActivity, receiveInbound, recordFetchedObject, recordInbound, recordOutbound, settleOutbound };
|
|
111
|
+
//# sourceMappingURL=activity-delivery.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activity-delivery.d.mts","names":[],"sources":["../src/activity-delivery.ts"],"mappings":";;;;KAsCK,gBAAgB,KACnB;UAIe;WACN,SAAS;WACT;;;;;;;;;KAUC,uBAAuB,KACjC;;;;WAMS,KAAK;WACL;WACA,oBAAoB,YAAY;WAChC,MAAM;WACN,qBAAqB;;WAErB,SAAS,SAAS;;WAElB,WAAW,SAAS;;;KAInB,wBAAwB,KAClC;WAUS;;KAGN,WAAW,KAAK;;WAEV;;;;;;;iBAQK,iBAAiB;WACtB,UAAU;WACV,qBAAqB;IAC5B;;;;;;;;iBAgJkB,cACpB,IAAI,YACF,WAAW,MAAM,OAAkB,SAAS,uBAC7C,QAAQ;;;;;;iBAuBW,eAAe,IAAI,UAAU,IAAI,OAAO;;;;;;;;;;;;iBA0ExC,oBACpB,IAAI,WAAW,aACf,YAAY,OACX;;;;;;;iBA+BmB,oBACpB,IAAI,UACJ,YAAY,MACZ,oBACC;;;;;;;;iBAyBmB,eACpB,IAAI,UACJ,OAAO,wBACN,QAAQ;;UAoBM;WACN;WACA;WACA;WACA;WACA;WACA;;WAEA;;WAEA,KAAK;;;;;;;;iBASM,eACpB,IAAI,UACJ,OAAO,qBACN"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { activities, activityDeliveries, activityDeliveryActorCollections, activityDeliveryActors, activityDeliveryAttempts, actors, objects, resources } from "./schema.mjs";
|
|
2
|
+
import { uuidV7 } from "./uuid.mjs";
|
|
3
|
+
import { storableText } from "./text.mjs";
|
|
4
|
+
import { and, desc, eq, inArray, isNotNull, isNull, sql } from "drizzle-orm";
|
|
5
|
+
import { isDeepStrictEqual } from "node:util";
|
|
6
|
+
//#region src/activity-delivery.ts
|
|
7
|
+
/**
|
|
8
|
+
* Merge the inbox owner and addressed actors into one row per actor, keeping
|
|
9
|
+
* every way each was addressed, whatever order they come in.
|
|
10
|
+
* @returns The actor rows of an inbound delivery.
|
|
11
|
+
*/
|
|
12
|
+
function inboundActorRows(entry) {
|
|
13
|
+
const rows = /* @__PURE__ */ new Map();
|
|
14
|
+
if (entry.actorId != null) rows.set(entry.actorId, {
|
|
15
|
+
actorId: entry.actorId,
|
|
16
|
+
inboxOwner: true
|
|
17
|
+
});
|
|
18
|
+
for (const { actorId, viaCollectionIri } of entry.addressed ?? []) {
|
|
19
|
+
const row = rows.get(actorId);
|
|
20
|
+
const collectionIris = row?.collectionIris ?? [];
|
|
21
|
+
rows.set(actorId, {
|
|
22
|
+
...row,
|
|
23
|
+
actorId,
|
|
24
|
+
addressed: true,
|
|
25
|
+
addressedDirectly: row?.addressedDirectly === true || viaCollectionIri == null,
|
|
26
|
+
collectionIris: viaCollectionIri == null ? collectionIris : [.../* @__PURE__ */ new Set([...collectionIris, viaCollectionIri])].toSorted()
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return [...rows.values()];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* PostgreSQL's jsonb holds neither U+0000 nor an unpaired surrogate, in keys
|
|
33
|
+
* or in values.
|
|
34
|
+
* @returns Whether the JSON value can be stored as jsonb.
|
|
35
|
+
*/
|
|
36
|
+
function storableJson(value) {
|
|
37
|
+
if (typeof value === "string") return storableText(value);
|
|
38
|
+
return typeof value !== "object" || value == null || Object.entries(value).every(([key, item]) => storableJson(key) && storableJson(item));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Keep a payload jsonb cannot hold out of the delivery, rather than the
|
|
42
|
+
* delivery.
|
|
43
|
+
* @returns The value to store, SQL `NULL` for such a payload.
|
|
44
|
+
*/
|
|
45
|
+
const jsonb = (payload) => payload === null ? sql`'null'::jsonb` : storableJson(payload) ? payload : null;
|
|
46
|
+
/**
|
|
47
|
+
* A fetched object is a JSON object, so nothing fetched is SQL `NULL`.
|
|
48
|
+
* @returns The value to store.
|
|
49
|
+
*/
|
|
50
|
+
const storedObject = (document) => document == null ? null : jsonb(document);
|
|
51
|
+
/**
|
|
52
|
+
* PostgreSQL's text holds no U+0000, which a remote response or an error
|
|
53
|
+
* quoting one may carry; keep the rest of it rather than lose the delivery.
|
|
54
|
+
* @returns The text with each U+0000 replaced by U+FFFD.
|
|
55
|
+
*/
|
|
56
|
+
const text = (value) => value?.replaceAll("\0", "�") ?? null;
|
|
57
|
+
/**
|
|
58
|
+
* Refuse to relate a delivery to anything but local actors of its instance,
|
|
59
|
+
* which neither foreign key can tell.
|
|
60
|
+
* @throws {Error} When an actor is remote, or of another instance.
|
|
61
|
+
*/
|
|
62
|
+
async function checkLocalActors(tx, instanceId, actorIds) {
|
|
63
|
+
const ids = [...new Set(actorIds.filter((id) => id != null))];
|
|
64
|
+
if (ids.length === 0) return;
|
|
65
|
+
if (await tx.$count(actors, and(inArray(actors.id, ids), eq(actors.instanceId, instanceId), isNotNull(actors.localId))) !== ids.length) throw new Error("An activity delivery relates only to local actors of its instance.");
|
|
66
|
+
}
|
|
67
|
+
async function insertDelivery(db, delivery, links) {
|
|
68
|
+
return await db.transaction(async (tx) => {
|
|
69
|
+
await checkLocalActors(tx, delivery.instanceId, [delivery.actorId, ...links.map(({ actorId }) => actorId)]);
|
|
70
|
+
const [row] = await tx.insert(activityDeliveries).values({
|
|
71
|
+
...delivery,
|
|
72
|
+
error: text(delivery.error),
|
|
73
|
+
responseBody: text(delivery.responseBody)
|
|
74
|
+
}).returning();
|
|
75
|
+
if (row == null) throw new Error("Missing activity delivery after insertion.");
|
|
76
|
+
if (links.length > 0) await tx.insert(activityDeliveryActors).values(links.map(({ collectionIris: _, ...link }) => ({
|
|
77
|
+
...link,
|
|
78
|
+
deliveryId: row.id,
|
|
79
|
+
created: row.created
|
|
80
|
+
})));
|
|
81
|
+
const collections = links.flatMap(({ actorId, collectionIris = [] }) => collectionIris.map((collectionIri) => ({
|
|
82
|
+
deliveryId: row.id,
|
|
83
|
+
actorId,
|
|
84
|
+
collectionIri
|
|
85
|
+
})));
|
|
86
|
+
if (collections.length > 0) await tx.insert(activityDeliveryActorCollections).values(collections);
|
|
87
|
+
return row;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Persist one inbound observation with its actor rows in one transaction.
|
|
92
|
+
* The inbox owner and every addressed actor must be local actors of
|
|
93
|
+
* `instanceId`; otherwise nothing is recorded.
|
|
94
|
+
* @returns The inserted inbound delivery.
|
|
95
|
+
* @throws {Error} When an actor is not a local actor of the instance.
|
|
96
|
+
*/
|
|
97
|
+
async function recordInbound(db, { addressed, body, id = uuidV7(), ...entry }) {
|
|
98
|
+
return await insertDelivery(db, {
|
|
99
|
+
...entry,
|
|
100
|
+
body: Buffer.from(body),
|
|
101
|
+
payload: jsonb(entry.payload),
|
|
102
|
+
fetchedObject: storedObject(entry.fetchedObject),
|
|
103
|
+
id,
|
|
104
|
+
direction: "inbound",
|
|
105
|
+
carriedSnapshot: false,
|
|
106
|
+
carriedObjectSnapshot: false
|
|
107
|
+
}, inboundActorRows({
|
|
108
|
+
actorId: entry.actorId,
|
|
109
|
+
addressed
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Record that an inbox listener ran for an inbound delivery answered before it
|
|
114
|
+
* did, as a queued one is.
|
|
115
|
+
* @returns Whether an acknowledged inbound delivery was found.
|
|
116
|
+
*/
|
|
117
|
+
async function receiveInbound(db, id) {
|
|
118
|
+
return (await db.update(activityDeliveries).set({ status: "received" }).where(and(eq(activityDeliveries.id, id), eq(activityDeliveries.direction, "inbound"), eq(activityDeliveries.status, "acknowledged"))).returning({ id: activityDeliveries.id })).length > 0;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Decide what a linked inbound delivery carried, anew every time, so that a
|
|
122
|
+
* mark left from an activity deleted since never carries over to another
|
|
123
|
+
* stored under the same IRI:
|
|
124
|
+
*
|
|
125
|
+
* - The stored activity, when its payload is the stored document, since the
|
|
126
|
+
* same IRI may arrive again with other contents.
|
|
127
|
+
* - The object stored with that activity, when the delivery carried the
|
|
128
|
+
* activity and the object was taken from it, or when its inbox listener
|
|
129
|
+
* fetched the object as stored. Matching the activity proves nothing of
|
|
130
|
+
* an object fetched apart from it, which may differ from fetch to fetch.
|
|
131
|
+
*
|
|
132
|
+
* A payload or a fetched object jsonb could not hold is stored as SQL `NULL`
|
|
133
|
+
* and carries nothing. An unlinked delivery is left as is.
|
|
134
|
+
*/
|
|
135
|
+
async function decideCarried(tx, deliveryId) {
|
|
136
|
+
const [row] = await tx.select({
|
|
137
|
+
payload: activityDeliveries.payload,
|
|
138
|
+
fetchedObject: activityDeliveries.fetchedObject,
|
|
139
|
+
activity: activities.document,
|
|
140
|
+
object: objects.document,
|
|
141
|
+
fetched: objects.fetched
|
|
142
|
+
}).from(activityDeliveries).innerJoin(activities, eq(activities.id, activityDeliveries.activityId)).leftJoin(objects, eq(objects.activityId, activityDeliveries.activityId)).where(and(eq(activityDeliveries.id, deliveryId), eq(activityDeliveries.direction, "inbound")));
|
|
143
|
+
if (row == null) return;
|
|
144
|
+
const carriedSnapshot = row.payload != null && isDeepStrictEqual(row.payload, row.activity);
|
|
145
|
+
const carriedObjectSnapshot = carriedSnapshot && row.fetched != null && (!row.fetched || row.fetchedObject != null && isDeepStrictEqual(row.fetchedObject, row.object));
|
|
146
|
+
await tx.update(activityDeliveries).set({
|
|
147
|
+
carriedSnapshot,
|
|
148
|
+
carriedObjectSnapshot
|
|
149
|
+
}).where(eq(activityDeliveries.id, deliveryId));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Link an inbound delivery to the stored activity its activity IRI names.
|
|
153
|
+
* Only a delivery Fedify verified and answered with 2xx is linked: Fedify
|
|
154
|
+
* authenticates an activity before accepting it, so its actor has the origin
|
|
155
|
+
* of the activity's ID, whether an inbox listener ran or it was a duplicate.
|
|
156
|
+
* A delivery that is already linked is left as is.
|
|
157
|
+
*
|
|
158
|
+
* The link names the activity whatever the delivery carried; what it carried
|
|
159
|
+
* is decided as the link is made, as `recordFetchedObject()` decides it again.
|
|
160
|
+
* @returns Whether the delivery was linked.
|
|
161
|
+
*/
|
|
162
|
+
async function linkInboundActivity(db, deliveryId) {
|
|
163
|
+
return await db.transaction(async (tx) => {
|
|
164
|
+
const [row] = await tx.update(activityDeliveries).set({ activityId: resources.id }).from(resources).innerJoin(activities, eq(activities.id, resources.id)).where(and(eq(activityDeliveries.id, deliveryId), eq(activityDeliveries.direction, "inbound"), inArray(activityDeliveries.status, ["received", "acknowledged"]), eq(activityDeliveries.verificationResult, "verified"), isNull(activityDeliveries.activityId), eq(resources.iri, activityDeliveries.activityIri), eq(resources.kind, "activity"))).returning({ id: activityDeliveries.id });
|
|
165
|
+
if (row == null) return false;
|
|
166
|
+
await decideCarried(tx, deliveryId);
|
|
167
|
+
return true;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Record the object an inbound delivery's inbox listener fetched from its IRI,
|
|
172
|
+
* and decide again what the delivery carried if it is linked already, as a
|
|
173
|
+
* duplicate queued for a worker is before its listener runs.
|
|
174
|
+
* @returns Whether the inbound delivery was found.
|
|
175
|
+
*/
|
|
176
|
+
async function recordFetchedObject(db, deliveryId, document) {
|
|
177
|
+
return await db.transaction(async (tx) => {
|
|
178
|
+
if ((await tx.update(activityDeliveries).set({ fetchedObject: storedObject(document) }).where(and(eq(activityDeliveries.id, deliveryId), eq(activityDeliveries.direction, "inbound"))).returning({ id: activityDeliveries.id })).length < 1) return false;
|
|
179
|
+
await decideCarried(tx, deliveryId);
|
|
180
|
+
return true;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Start a delivery observation. The payload is the document before signing.
|
|
185
|
+
* The sender must be a local actor of `instanceId`; otherwise nothing is
|
|
186
|
+
* recorded.
|
|
187
|
+
* @returns The inserted queued outbound delivery.
|
|
188
|
+
* @throws {Error} When the sender is not a local actor of the instance.
|
|
189
|
+
*/
|
|
190
|
+
async function recordOutbound(db, entry) {
|
|
191
|
+
return await insertDelivery(db, {
|
|
192
|
+
...entry,
|
|
193
|
+
created: entry.created ?? Temporal.Now.instant(),
|
|
194
|
+
payload: jsonb(entry.payload),
|
|
195
|
+
id: uuidV7(),
|
|
196
|
+
direction: "outbound",
|
|
197
|
+
status: "queued",
|
|
198
|
+
fetchedObject: null,
|
|
199
|
+
carriedSnapshot: false,
|
|
200
|
+
carriedObjectSnapshot: false
|
|
201
|
+
}, entry.actorId == null ? [] : [{
|
|
202
|
+
actorId: entry.actorId,
|
|
203
|
+
sender: true
|
|
204
|
+
}]);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Settle the most recent pending (`queued` or `failed`) delivery to an inbox,
|
|
208
|
+
* keeping each ended attempt. `sent`, `permanently_failed` and `abandoned`
|
|
209
|
+
* deliveries are never changed again.
|
|
210
|
+
* @returns Whether a pending delivery was found.
|
|
211
|
+
*/
|
|
212
|
+
async function settleOutbound(db, entry) {
|
|
213
|
+
const summary = {
|
|
214
|
+
statusCode: entry.statusCode ?? null,
|
|
215
|
+
error: text(entry.error),
|
|
216
|
+
responseBody: text(entry.responseBody)
|
|
217
|
+
};
|
|
218
|
+
return await db.transaction(async (tx) => {
|
|
219
|
+
const [delivery] = await tx.select({ id: activityDeliveries.id }).from(activityDeliveries).where(and(eq(activityDeliveries.direction, "outbound"), eq(activityDeliveries.activityIri, entry.activityIri), eq(activityDeliveries.inboxUrl, entry.inboxUrl), inArray(activityDeliveries.status, ["queued", "failed"]), entry.id == null ? void 0 : eq(activityDeliveries.id, entry.id))).orderBy(desc(activityDeliveries.created), desc(activityDeliveries.id)).limit(1).for("update");
|
|
220
|
+
if (delivery == null) return false;
|
|
221
|
+
const completed = Temporal.Now.instant();
|
|
222
|
+
if (entry.attempted) await tx.insert(activityDeliveryAttempts).values({
|
|
223
|
+
...summary,
|
|
224
|
+
id: uuidV7(),
|
|
225
|
+
deliveryId: delivery.id,
|
|
226
|
+
succeeded: entry.status === "sent",
|
|
227
|
+
created: completed
|
|
228
|
+
});
|
|
229
|
+
await tx.update(activityDeliveries).set({
|
|
230
|
+
...summary,
|
|
231
|
+
status: entry.status,
|
|
232
|
+
completed
|
|
233
|
+
}).where(eq(activityDeliveries.id, delivery.id));
|
|
234
|
+
return true;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
//#endregion
|
|
238
|
+
export { inboundActorRows, linkInboundActivity, receiveInbound, recordFetchedObject, recordInbound, recordOutbound, settleOutbound };
|
|
239
|
+
|
|
240
|
+
//# sourceMappingURL=activity-delivery.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activity-delivery.mjs","names":[],"sources":["../src/activity-delivery.ts"],"sourcesContent":["// DrFed: A web-based platform for developing and debugging ActivityPub apps\n// Copyright (C) 2026 DrFed team\n//\n// This program is free software: you can redistribute it and/or modify\n// it under the terms of the GNU Affero General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// This program is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU Affero General Public License for more details.\n//\n// You should have received a copy of the GNU Affero General Public License\n// along with this program. If not, see <https://www.gnu.org/licenses/>.\n\nimport { isDeepStrictEqual } from \"node:util\";\n\nimport { and, desc, eq, inArray, isNotNull, isNull, sql } from \"drizzle-orm\";\n\nimport type { Database, Transaction } from \"./db.ts\";\nimport {\n type ActivityDelivery,\n type NewActivityDelivery,\n type NewActivityDeliveryActor,\n activities,\n activityDeliveries,\n activityDeliveryActorCollections,\n activityDeliveryActors,\n activityDeliveryAttempts,\n actors,\n objects,\n resources,\n} from \"./schema.ts\";\nimport { storableText } from \"./text.ts\";\nimport { type Uuid, uuidV7 } from \"./uuid.ts\";\n\n/** The marks of what a delivery carried are the model's to decide. */\ntype DeliveryEntry = Omit<\n NewActivityDelivery,\n \"id\" | \"direction\" | \"status\" | \"carriedSnapshot\" | \"carriedObjectSnapshot\"\n>;\n\nexport interface AddressedActor {\n readonly actorId: Uuid;\n readonly viaCollectionIri?: string | null;\n}\n\n/**\n * `payload` is `undefined` for an unparsable body and `null` for JSON `null`;\n * it is stored as SQL `NULL` when jsonb cannot hold it, and `body` keeps it.\n * `fetchedObject` is stored as SQL `NULL` when jsonb cannot hold it too.\n * An inbound delivery is linked to its activity only by\n * `linkInboundActivity()`.\n */\nexport type InboundDeliveryEntry = Omit<\n DeliveryEntry,\n \"body\" | \"verificationResult\" | \"recipientIris\" | \"activityId\"\n> & {\n /**\n * Chosen in advance when something must name the delivery before it exists.\n */\n readonly id?: Uuid;\n readonly status: \"received\" | \"acknowledged\" | \"unverified\" | \"rejected\";\n readonly verificationResult: NonNullable<DeliveryEntry[\"verificationResult\"]>;\n readonly body: Uint8Array;\n readonly addressed?: readonly AddressedActor[];\n /** When the request arrived, before verification and handling. */\n readonly created: Temporal.Instant;\n /** When DrFed answered the request. */\n readonly completed: Temporal.Instant;\n};\n\n/** `payload` is the document before signing, without `bto` and `bcc`. */\nexport type OutboundDeliveryEntry = Omit<\n DeliveryEntry,\n | \"verificationKeyId\"\n | \"verificationMechanism\"\n | \"verificationResult\"\n | \"body\"\n | \"headers\"\n | \"requestUrl\"\n | \"completed\"\n | \"fetchedObject\"\n> & {\n readonly activityIri: string;\n};\n\ntype ActorRow = Omit<NewActivityDeliveryActor, \"deliveryId\" | \"created\"> & {\n /** The addressed collections that reached the actor, sorted. */\n readonly collectionIris?: readonly string[];\n};\n\n/**\n * Merge the inbox owner and addressed actors into one row per actor, keeping\n * every way each was addressed, whatever order they come in.\n * @returns The actor rows of an inbound delivery.\n */\nexport function inboundActorRows(entry: {\n readonly actorId?: Uuid | null | undefined;\n readonly addressed?: readonly AddressedActor[] | undefined;\n}): ActorRow[] {\n const rows = new Map<Uuid, ActorRow>();\n if (entry.actorId != null) {\n rows.set(entry.actorId, { actorId: entry.actorId, inboxOwner: true });\n }\n for (const { actorId, viaCollectionIri } of entry.addressed ?? []) {\n const row = rows.get(actorId);\n const collectionIris = row?.collectionIris ?? [];\n rows.set(actorId, {\n ...row,\n actorId,\n addressed: true,\n addressedDirectly:\n row?.addressedDirectly === true || viaCollectionIri == null,\n collectionIris:\n viaCollectionIri == null\n ? collectionIris\n : [...new Set([...collectionIris, viaCollectionIri])].toSorted(),\n });\n }\n return [...rows.values()];\n}\n\n/**\n * PostgreSQL's jsonb holds neither U+0000 nor an unpaired surrogate, in keys\n * or in values.\n * @returns Whether the JSON value can be stored as jsonb.\n */\nfunction storableJson(value: unknown): boolean {\n if (typeof value === \"string\") return storableText(value);\n return (\n typeof value !== \"object\" ||\n value == null ||\n Object.entries(value).every(\n ([key, item]) => storableJson(key) && storableJson(item),\n )\n );\n}\n\n/**\n * Keep a payload jsonb cannot hold out of the delivery, rather than the\n * delivery.\n * @returns The value to store, SQL `NULL` for such a payload.\n */\nconst jsonb = (payload: unknown) =>\n payload === null\n ? sql`'null'::jsonb`\n : storableJson(payload)\n ? payload\n : null;\n\n/**\n * A fetched object is a JSON object, so nothing fetched is SQL `NULL`.\n * @returns The value to store.\n */\nconst storedObject = (document: unknown) =>\n document == null ? null : jsonb(document);\n\n/**\n * PostgreSQL's text holds no U+0000, which a remote response or an error\n * quoting one may carry; keep the rest of it rather than lose the delivery.\n * @returns The text with each U+0000 replaced by U+FFFD.\n */\nconst text = (value: string | null | undefined): string | null =>\n value?.replaceAll(\"\\0\", \"\\ufffd\") ?? null;\n\n/**\n * Refuse to relate a delivery to anything but local actors of its instance,\n * which neither foreign key can tell.\n * @throws {Error} When an actor is remote, or of another instance.\n */\nasync function checkLocalActors(\n tx: Transaction,\n instanceId: Uuid,\n actorIds: readonly (Uuid | null | undefined)[],\n): Promise<void> {\n const ids = [...new Set(actorIds.filter((id) => id != null))];\n if (ids.length === 0) return;\n const local = await tx.$count(\n actors,\n and(\n inArray(actors.id, ids),\n eq(actors.instanceId, instanceId),\n isNotNull(actors.localId),\n ),\n );\n if (local !== ids.length) {\n throw new Error(\n \"An activity delivery relates only to local actors of its instance.\",\n );\n }\n}\n\nasync function insertDelivery(\n db: Database,\n delivery: NewActivityDelivery,\n links: readonly ActorRow[],\n): Promise<ActivityDelivery> {\n return await db.transaction(async (tx) => {\n await checkLocalActors(tx, delivery.instanceId, [\n delivery.actorId,\n ...links.map(({ actorId }) => actorId),\n ]);\n const [row] = await tx\n .insert(activityDeliveries)\n .values({\n ...delivery,\n error: text(delivery.error),\n responseBody: text(delivery.responseBody),\n })\n .returning();\n if (row == null) {\n throw new Error(\"Missing activity delivery after insertion.\");\n }\n if (links.length > 0) {\n await tx.insert(activityDeliveryActors).values(\n links.map(({ collectionIris: _, ...link }) => ({\n ...link,\n deliveryId: row.id,\n created: row.created,\n })),\n );\n }\n const collections = links.flatMap(({ actorId, collectionIris = [] }) =>\n collectionIris.map((collectionIri) => ({\n deliveryId: row.id,\n actorId,\n collectionIri,\n })),\n );\n if (collections.length > 0) {\n await tx.insert(activityDeliveryActorCollections).values(collections);\n }\n return row;\n });\n}\n\n/**\n * Persist one inbound observation with its actor rows in one transaction.\n * The inbox owner and every addressed actor must be local actors of\n * `instanceId`; otherwise nothing is recorded.\n * @returns The inserted inbound delivery.\n * @throws {Error} When an actor is not a local actor of the instance.\n */\nexport async function recordInbound(\n db: Database,\n { addressed, body, id = uuidV7(), ...entry }: InboundDeliveryEntry,\n): Promise<ActivityDelivery> {\n return await insertDelivery(\n db,\n {\n ...entry,\n body: Buffer.from(body),\n payload: jsonb(entry.payload),\n fetchedObject: storedObject(entry.fetchedObject),\n id,\n direction: \"inbound\",\n // Whatever a caller outside TypeScript passes.\n carriedSnapshot: false,\n carriedObjectSnapshot: false,\n },\n inboundActorRows({ actorId: entry.actorId, addressed }),\n );\n}\n\n/**\n * Record that an inbox listener ran for an inbound delivery answered before it\n * did, as a queued one is.\n * @returns Whether an acknowledged inbound delivery was found.\n */\nexport async function receiveInbound(db: Database, id: Uuid): Promise<boolean> {\n const rows = await db\n .update(activityDeliveries)\n .set({ status: \"received\" })\n .where(\n and(\n eq(activityDeliveries.id, id),\n eq(activityDeliveries.direction, \"inbound\"),\n eq(activityDeliveries.status, \"acknowledged\"),\n ),\n )\n .returning({ id: activityDeliveries.id });\n return rows.length > 0;\n}\n\n/**\n * Decide what a linked inbound delivery carried, anew every time, so that a\n * mark left from an activity deleted since never carries over to another\n * stored under the same IRI:\n *\n * - The stored activity, when its payload is the stored document, since the\n * same IRI may arrive again with other contents.\n * - The object stored with that activity, when the delivery carried the\n * activity and the object was taken from it, or when its inbox listener\n * fetched the object as stored. Matching the activity proves nothing of\n * an object fetched apart from it, which may differ from fetch to fetch.\n *\n * A payload or a fetched object jsonb could not hold is stored as SQL `NULL`\n * and carries nothing. An unlinked delivery is left as is.\n */\nasync function decideCarried(tx: Transaction, deliveryId: Uuid): Promise<void> {\n const [row] = await tx\n .select({\n payload: activityDeliveries.payload,\n fetchedObject: activityDeliveries.fetchedObject,\n activity: activities.document,\n object: objects.document,\n fetched: objects.fetched,\n })\n .from(activityDeliveries)\n .innerJoin(activities, eq(activities.id, activityDeliveries.activityId))\n .leftJoin(objects, eq(objects.activityId, activityDeliveries.activityId))\n .where(\n and(\n eq(activityDeliveries.id, deliveryId),\n eq(activityDeliveries.direction, \"inbound\"),\n ),\n );\n if (row == null) return;\n const carriedSnapshot =\n row.payload != null && isDeepStrictEqual(row.payload, row.activity);\n const carriedObjectSnapshot =\n carriedSnapshot &&\n row.fetched != null &&\n (!row.fetched ||\n (row.fetchedObject != null &&\n isDeepStrictEqual(row.fetchedObject, row.object)));\n await tx\n .update(activityDeliveries)\n .set({ carriedSnapshot, carriedObjectSnapshot })\n .where(eq(activityDeliveries.id, deliveryId));\n}\n\n/**\n * Link an inbound delivery to the stored activity its activity IRI names.\n * Only a delivery Fedify verified and answered with 2xx is linked: Fedify\n * authenticates an activity before accepting it, so its actor has the origin\n * of the activity's ID, whether an inbox listener ran or it was a duplicate.\n * A delivery that is already linked is left as is.\n *\n * The link names the activity whatever the delivery carried; what it carried\n * is decided as the link is made, as `recordFetchedObject()` decides it again.\n * @returns Whether the delivery was linked.\n */\nexport async function linkInboundActivity(\n db: Database | Transaction,\n deliveryId: Uuid,\n): Promise<boolean> {\n return await db.transaction(async (tx) => {\n const [row] = await tx\n .update(activityDeliveries)\n .set({ activityId: resources.id })\n .from(resources)\n .innerJoin(activities, eq(activities.id, resources.id))\n .where(\n and(\n eq(activityDeliveries.id, deliveryId),\n eq(activityDeliveries.direction, \"inbound\"),\n inArray(activityDeliveries.status, [\"received\", \"acknowledged\"]),\n eq(activityDeliveries.verificationResult, \"verified\"),\n isNull(activityDeliveries.activityId),\n eq(resources.iri, activityDeliveries.activityIri),\n eq(resources.kind, \"activity\"),\n ),\n )\n .returning({ id: activityDeliveries.id });\n if (row == null) return false;\n await decideCarried(tx, deliveryId);\n return true;\n });\n}\n\n/**\n * Record the object an inbound delivery's inbox listener fetched from its IRI,\n * and decide again what the delivery carried if it is linked already, as a\n * duplicate queued for a worker is before its listener runs.\n * @returns Whether the inbound delivery was found.\n */\nexport async function recordFetchedObject(\n db: Database,\n deliveryId: Uuid,\n document: unknown,\n): Promise<boolean> {\n return await db.transaction(async (tx) => {\n const rows = await tx\n .update(activityDeliveries)\n .set({ fetchedObject: storedObject(document) })\n .where(\n and(\n eq(activityDeliveries.id, deliveryId),\n eq(activityDeliveries.direction, \"inbound\"),\n ),\n )\n .returning({ id: activityDeliveries.id });\n if (rows.length < 1) return false;\n await decideCarried(tx, deliveryId);\n return true;\n });\n}\n\n/**\n * Start a delivery observation. The payload is the document before signing.\n * The sender must be a local actor of `instanceId`; otherwise nothing is\n * recorded.\n * @returns The inserted queued outbound delivery.\n * @throws {Error} When the sender is not a local actor of the instance.\n */\nexport async function recordOutbound(\n db: Database,\n entry: OutboundDeliveryEntry,\n): Promise<ActivityDelivery> {\n return await insertDelivery(\n db,\n {\n ...entry,\n // The application clock, as inbound deliveries use, so both directions sort together.\n created: entry.created ?? Temporal.Now.instant(),\n payload: jsonb(entry.payload),\n id: uuidV7(),\n direction: \"outbound\",\n status: \"queued\",\n fetchedObject: null,\n carriedSnapshot: false,\n carriedObjectSnapshot: false,\n },\n entry.actorId == null ? [] : [{ actorId: entry.actorId, sender: true }],\n );\n}\n\n/** What a settled delivery shows; null fields for a successful attempt. */\nexport interface OutboundSettlement {\n readonly activityIri: string;\n readonly inboxUrl: string;\n readonly status: \"sent\" | \"failed\" | \"permanently_failed\" | \"abandoned\";\n readonly statusCode?: number | null;\n readonly error?: string | null;\n readonly responseBody?: string | null;\n /** Whether an attempt ended with this result, rather than none being made. */\n readonly attempted: boolean;\n /** Restrict a synchronous delivery to the row started by that invocation. */\n readonly id?: Uuid;\n}\n\n/**\n * Settle the most recent pending (`queued` or `failed`) delivery to an inbox,\n * keeping each ended attempt. `sent`, `permanently_failed` and `abandoned`\n * deliveries are never changed again.\n * @returns Whether a pending delivery was found.\n */\nexport async function settleOutbound(\n db: Database,\n entry: OutboundSettlement,\n): Promise<boolean> {\n const summary = {\n statusCode: entry.statusCode ?? null,\n error: text(entry.error),\n responseBody: text(entry.responseBody),\n };\n return await db.transaction(async (tx) => {\n const [delivery] = await tx\n .select({ id: activityDeliveries.id })\n .from(activityDeliveries)\n .where(\n and(\n eq(activityDeliveries.direction, \"outbound\"),\n eq(activityDeliveries.activityIri, entry.activityIri),\n eq(activityDeliveries.inboxUrl, entry.inboxUrl),\n inArray(activityDeliveries.status, [\"queued\", \"failed\"]),\n entry.id == null ? undefined : eq(activityDeliveries.id, entry.id),\n ),\n )\n .orderBy(desc(activityDeliveries.created), desc(activityDeliveries.id))\n .limit(1)\n .for(\"update\");\n if (delivery == null) return false;\n const completed = Temporal.Now.instant();\n if (entry.attempted) {\n await tx.insert(activityDeliveryAttempts).values({\n ...summary,\n id: uuidV7(),\n deliveryId: delivery.id,\n succeeded: entry.status === \"sent\",\n created: completed,\n });\n }\n await tx\n .update(activityDeliveries)\n .set({ ...summary, status: entry.status, completed })\n .where(eq(activityDeliveries.id, delivery.id));\n return true;\n });\n}\n"],"mappings":";;;;;;;;;;;AAkGA,SAAgB,iBAAiB,OAGlB;CACb,MAAM,uBAAO,IAAI,IAAoB;CACrC,IAAI,MAAM,WAAW,MACnB,KAAK,IAAI,MAAM,SAAS;EAAE,SAAS,MAAM;EAAS,YAAY;CAAK,CAAC;CAEtE,KAAK,MAAM,EAAE,SAAS,sBAAsB,MAAM,aAAa,CAAC,GAAG;EACjE,MAAM,MAAM,KAAK,IAAI,OAAO;EAC5B,MAAM,iBAAiB,KAAK,kBAAkB,CAAC;EAC/C,KAAK,IAAI,SAAS;GAChB,GAAG;GACH;GACA,WAAW;GACX,mBACE,KAAK,sBAAsB,QAAQ,oBAAoB;GACzD,gBACE,oBAAoB,OAChB,iBACA,CAAC,mBAAG,IAAI,IAAI,CAAC,GAAG,gBAAgB,gBAAgB,CAAC,CAAC,CAAC,CAAC,SAAS;EACrE,CAAC;CACH;CACA,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC;AAC1B;;;;;;AAOA,SAAS,aAAa,OAAyB;CAC7C,IAAI,OAAO,UAAU,UAAU,OAAO,aAAa,KAAK;CACxD,OACE,OAAO,UAAU,YACjB,SAAS,QACT,OAAO,QAAQ,KAA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