cursedbelt-server 4.47.1 → 4.48.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/activity/store.d.ts +1 -1
- package/dist/server/analytics/injectedScripts.d.ts +1 -1
- package/dist/server/analytics/injectedScripts.js +1 -1
- package/dist/server/analytics/webAnalytics.d.ts +1 -1
- package/dist/server/analytics/webAnalytics.js +1 -1
- package/dist/server/auth/loginThrottleDurable.d.ts +3 -3
- package/dist/server/auth/loginThrottleDurable.js +3 -3
- package/dist/server/auth/passwordCost.js +1 -1
- package/dist/server/ctgrChunkDurable.d.ts +1 -1
- package/dist/server/ctgrChunkDurable.js +1 -1
- package/dist/server/d1/backup.d.ts +1 -1
- package/dist/server/d1/backup.js +1 -1
- package/dist/server/d1/http.d.ts +2 -2
- package/dist/server/d1/http.js +2 -2
- package/dist/server/d1/invocation.js +1 -1
- package/dist/server/d1/limits.d.ts +1 -1
- package/dist/server/d1/limits.js +1 -1
- package/dist/server/d1/scheduling.d.ts +1 -1
- package/dist/server/d1/scheduling.js +1 -1
- package/dist/server/d1/types.d.ts +1 -1
- package/dist/server/d1/types.js +1 -1
- package/dist/server/d1/values.d.ts +1 -1
- package/dist/server/d1/values.js +1 -1
- package/dist/server/dropzone/scan.d.ts +1 -1
- package/dist/server/engagement/actions.d.ts +20 -0
- package/dist/server/engagement/actions.js +62 -0
- package/dist/server/engagement/api.d.ts +13 -2
- package/dist/server/engagement/api.js +30 -2
- package/dist/server/engagement/d1.d.ts +13 -2
- package/dist/server/engagement/d1.js +30 -2
- package/dist/server/engagement/index.d.ts +5 -4
- package/dist/server/engagement/index.js +3 -2
- package/dist/server/engagement/manifestPlaces.d.ts +7 -1
- package/dist/server/engagement/manifestPlaces.js +21 -0
- package/dist/server/engagement/places.d.ts +3 -0
- package/dist/server/engagement/places.js +9 -2
- package/dist/server/engagement/policy.d.ts +21 -0
- package/dist/server/engagement/policy.js +27 -0
- package/dist/server/engagement/store.d.ts +12 -3
- package/dist/server/engagement/store.js +30 -2
- package/dist/server/engagement/summary.d.ts +15 -1
- package/dist/server/engagement/summary.js +15 -0
- package/dist/server/engagement/types.d.ts +9 -0
- package/dist/server/guard/argon2Binding.js +1 -1
- package/dist/server/guard/kdfWorkerEntry.js +2 -2
- package/dist/server/incidents/index.d.ts +1 -1
- package/dist/server/incidents/index.js +1 -1
- package/dist/server/incidents/store.d.ts +1 -1
- package/dist/server/incidents/store.js +1 -1
- package/dist/server/jobs/timeout.d.ts +1 -1
- package/dist/server/jobs/timeout.js +1 -1
- package/dist/server/master-lock/accountsPage.d.ts +1 -1
- package/dist/server/master-lock/accountsPage.js +2 -2
- package/dist/server/master-lock/guard.js +1 -1
- package/dist/server/master-lock/lockPage.d.ts +1 -1
- package/dist/server/master-lock/lockPage.js +2 -2
- package/dist/server/master-lock/masterLock.d.ts +2 -2
- package/dist/server/master-lock/masterLock.js +2 -2
- package/dist/server/metrics/normalizeRoute.js +1 -1
- package/dist/server/metrics/telemetrySink.d.ts +2 -2
- package/dist/server/metrics/telemetrySink.js +1 -1
- package/dist/server/middleware/zValidator.js +2 -2
- package/dist/server/notifications/hub.js +1 -1
- package/dist/server/requestLog/requestMetrics.d.ts +2 -2
- package/dist/server/requestLog/requestMetrics.js +2 -2
- package/dist/server/retention/archive.d.ts +1 -1
- package/dist/server/retention/archive.js +2 -2
- package/dist/server/satellite/config.js +2 -2
- package/dist/server/satellite/door.d.ts +1 -1
- package/dist/server/sharing/contract.d.ts +1 -1
- package/dist/server/storage/binaryStore.d.ts +1 -1
- package/dist/server/storage/binaryStore.js +3 -3
- package/dist/server/storage/derivatives.d.ts +2 -2
- package/dist/server/storage/derivatives.js +2 -2
- package/dist/server/storage/files/adapter.d.ts +2 -2
- package/dist/server/storage/files/adapter.js +10 -10
- package/dist/server/storage/files/catalogue.d.ts +1 -1
- package/dist/server/storage/files/catalogue.js +4 -4
- package/dist/server/storage/files/chunkedUpload.d.ts +2 -2
- package/dist/server/storage/files/chunkedUpload.js +1 -1
- package/dist/server/storage/files/fileRoutes.d.ts +1 -1
- package/dist/server/storage/files/fileRoutes.js +4 -4
- package/dist/server/storage/files/mimeByExt.js +1 -1
- package/dist/server/storage/files/stack.d.ts +2 -2
- package/dist/server/storage/files/types.d.ts +4 -4
- package/dist/server/storage/files/types.js +1 -1
- package/dist/server/storage/index.js +1 -1
- package/dist/server/storage/objectStoreMediaStore.js +1 -1
- package/dist/server/storage/transcodeJobs.js +5 -5
- package/dist/server/storage/types.d.ts +1 -1
- package/dist/server/storage/types.js +1 -1
- package/dist/server/storage/videoDerivativeKeys.d.ts +1 -1
- package/dist/server/storage/videoDerivativeKeys.js +1 -1
- package/dist/server/storage/videoProcessor.js +1 -1
- package/dist/server/sync/status.d.ts +1 -1
- package/dist/server/zipStream.d.ts +1 -1
- package/dist/server/zipStream.js +1 -1
- package/docs/activity.md +2 -2
- package/docs/engagement.md +10 -1
- package/docs/retention.md +2 -2
- package/package.json +1 -1
- package/src/leafSubpathsImportNothing.spec.ts +1 -1
- package/src/namedSubpathsResolve.spec.ts +1 -1
- package/src/noTimerDialsAPeer.spec.ts +1 -1
- package/src/nodeBuiltinsAreProbedByCalling.spec.ts +1 -1
- package/src/publishShape.spec.ts +1 -1
- package/src/server/activity/store.ts +1 -1
- package/src/server/analytics/injectedScripts.spec.ts +1 -1
- package/src/server/analytics/injectedScripts.ts +1 -1
- package/src/server/analytics/webAnalytics.ts +1 -1
- package/src/server/auth/loginThrottleDurable.ts +3 -3
- package/src/server/auth/passwordCost.ts +1 -1
- package/src/server/bench/cpuBudget.spec.ts +1 -1
- package/src/server/bench/tail.spec.ts +1 -1
- package/src/server/ctgrChunkDurable.ts +1 -1
- package/src/server/d1/backup.spec.ts +1 -1
- package/src/server/d1/backup.ts +1 -1
- package/src/server/d1/http.ts +2 -2
- package/src/server/d1/invocation.spec.ts +1 -1
- package/src/server/d1/invocation.ts +1 -1
- package/src/server/d1/limits.ts +1 -1
- package/src/server/d1/pullD1.spec.ts +1 -1
- package/src/server/d1/scheduling.ts +1 -1
- package/src/server/d1/types.ts +1 -1
- package/src/server/d1/values.ts +1 -1
- package/src/server/dropzone/scan.spec.ts +2 -2
- package/src/server/dropzone/scan.ts +1 -1
- package/src/server/engagement/actions.ts +71 -0
- package/src/server/engagement/api.ts +36 -1
- package/src/server/engagement/d1.spec.ts +49 -3
- package/src/server/engagement/d1.ts +35 -2
- package/src/server/engagement/engagement.spec.ts +103 -1
- package/src/server/engagement/index.ts +5 -2
- package/src/server/engagement/manifestPlaces.ts +21 -1
- package/src/server/engagement/places.ts +12 -2
- package/src/server/engagement/policy.ts +34 -0
- package/src/server/engagement/store.ts +33 -4
- package/src/server/engagement/summary.ts +33 -0
- package/src/server/engagement/types.ts +10 -0
- package/src/server/google-oauth/googleOAuth.spec.ts +1 -1
- package/src/server/guard/argon2Binding.ts +1 -1
- package/src/server/guard/kdfWorkerEntry.ts +2 -2
- package/src/server/incidents/index.ts +1 -1
- package/src/server/incidents/store.ts +1 -1
- package/src/server/jobs/timeout.ts +1 -1
- package/src/server/master-lock/accountsPage.spec.ts +2 -2
- package/src/server/master-lock/accountsPage.ts +2 -2
- package/src/server/master-lock/beaconNeverReachesTheWall.spec.ts +2 -2
- package/src/server/master-lock/guard.spec.ts +2 -2
- package/src/server/master-lock/guard.ts +1 -1
- package/src/server/master-lock/lockPage.spec.ts +1 -1
- package/src/server/master-lock/lockPage.ts +2 -2
- package/src/server/master-lock/masterLock.spec.ts +1 -1
- package/src/server/master-lock/masterLock.ts +2 -2
- package/src/server/master-lock/sessions.spec.ts +1 -1
- package/src/server/metrics/normalizeRoute.ts +1 -1
- package/src/server/metrics/telemetrySink.ts +3 -3
- package/src/server/middleware/bodyLimit.spec.ts +1 -1
- package/src/server/middleware/zValidator.spec.ts +3 -3
- package/src/server/middleware/zValidator.ts +3 -3
- package/src/server/notifications/hub.ts +1 -1
- package/src/server/requestLog/requestMetrics.spec.ts +2 -2
- package/src/server/requestLog/requestMetrics.ts +3 -3
- package/src/server/retention/archive.spec.ts +2 -2
- package/src/server/retention/archive.ts +2 -2
- package/src/server/satellite/config.ts +2 -2
- package/src/server/satellite/door.ts +1 -1
- package/src/server/satellite/satellite.spec.ts +1 -1
- package/src/server/sharing/contract.ts +1 -1
- package/src/server/storage/binaryServerProcessing.spec.ts +1 -1
- package/src/server/storage/binaryServerProcessing.ts +4 -4
- package/src/server/storage/binaryStore.spec.ts +6 -6
- package/src/server/storage/binaryStore.ts +4 -4
- package/src/server/storage/chunked.integration.spec.ts +2 -2
- package/src/server/storage/derivatives.ts +2 -2
- package/src/server/storage/fileUploadClient.integration.spec.ts +1 -1
- package/src/server/storage/files/adapter.photo.spec.ts +2 -2
- package/src/server/storage/files/adapter.spec.ts +3 -3
- package/src/server/storage/files/adapter.ts +10 -10
- package/src/server/storage/files/catalogue.spec.ts +1 -1
- package/src/server/storage/files/catalogue.ts +4 -4
- package/src/server/storage/files/chunkedUpload.spec.ts +5 -5
- package/src/server/storage/files/chunkedUpload.ts +3 -3
- package/src/server/storage/files/fileRoutes.spec.ts +3 -3
- package/src/server/storage/files/fileRoutes.ts +5 -5
- package/src/server/storage/files/mimeByExt.ts +1 -1
- package/src/server/storage/files/stack.ts +3 -3
- package/src/server/storage/files/types.ts +6 -6
- package/src/server/storage/index.ts +1 -1
- package/src/server/storage/objectStoreMediaStore.ts +1 -1
- package/src/server/storage/sources.ts +1 -1
- package/src/server/storage/transcodeJobs.ts +5 -5
- package/src/server/storage/types.ts +2 -2
- package/src/server/storage/uploadSession.spec.ts +1 -1
- package/src/server/storage/videoDerivativeKeys.spec.ts +3 -3
- package/src/server/storage/videoDerivativeKeys.ts +1 -1
- package/src/server/storage/videoProcessor.ts +1 -1
- package/src/server/sync/status.ts +1 -1
- package/src/server/sync/timer.spec.ts +1 -1
- package/src/server/worker-argon2/index.spec.ts +1 -1
- package/src/server/zipStream.spec.ts +2 -2
- package/src/server/zipStream.ts +1 -1
- package/src/shippedFilesAreTracked.spec.ts +2 -2
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* ── 🔴 Four properties this file exists to guarantee ────────────────────────
|
|
22
|
-
* 1. **Append only.** No update verb, no delete verb, no `TRUNCATE`.
|
|
22
|
+
* 1. **Append only.** No update verb, no delete verb, no `TRUNCATE`. Aging
|
|
23
23
|
* rows out is the fleet retention standard's job, through a declaration the
|
|
24
24
|
* owner can see (`satellite-kit/retention`) — never a silent cutoff here.
|
|
25
25
|
* 2. **A source is READ, never copied.** `share_events` stays the canonical
|
|
@@ -39,7 +39,7 @@ export interface InjectedScript {
|
|
|
39
39
|
* Every `<script src=…>` in `html` whose origin is NOT `pageUrl`'s.
|
|
40
40
|
*
|
|
41
41
|
* Inline scripts are ignored: they are the page's own (most shells here ship one, a pre-paint
|
|
42
|
-
*
|
|
42
|
+
* color-scheme script) and a CSP that refuses them is a different finding from this one.
|
|
43
43
|
* `data:` and `blob:` are ignored for the same reason — neither is a host anything could have
|
|
44
44
|
* injected.
|
|
45
45
|
*/
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
* Every `<script src=…>` in `html` whose origin is NOT `pageUrl`'s.
|
|
33
33
|
*
|
|
34
34
|
* Inline scripts are ignored: they are the page's own (most shells here ship one, a pre-paint
|
|
35
|
-
*
|
|
35
|
+
* color-scheme script) and a CSP that refuses them is a different finding from this one.
|
|
36
36
|
* `data:` and `blob:` are ignored for the same reason — neither is a host anything could have
|
|
37
37
|
* injected.
|
|
38
38
|
*/
|
|
@@ -120,7 +120,7 @@ export declare const WEB_ANALYTICS_PUBLIC_HOST = "cursedalchemy.com";
|
|
|
120
120
|
*
|
|
121
121
|
* A device in GET-only mode (`cursedbelt-core/ctgr-client`) sits behind something that drops
|
|
122
122
|
* every non-GET, and the beacon reports with a POST to the page's `/cdn-cgi/rum`. It cannot be
|
|
123
|
-
*
|
|
123
|
+
* tunneled the way our own API is — the script is Cloudflare's — so there it only ever failed,
|
|
124
124
|
* a red line per page view, and recorded nothing. Not loading it loses no data (2026-10-08).
|
|
125
125
|
*
|
|
126
126
|
* The guard reads the URL as well as `localStorage.ctgr`: this script runs before the app's
|
|
@@ -127,7 +127,7 @@ export const WEB_ANALYTICS_PUBLIC_HOST = "cursedalchemy.com";
|
|
|
127
127
|
*
|
|
128
128
|
* A device in GET-only mode (`cursedbelt-core/ctgr-client`) sits behind something that drops
|
|
129
129
|
* every non-GET, and the beacon reports with a POST to the page's `/cdn-cgi/rum`. It cannot be
|
|
130
|
-
*
|
|
130
|
+
* tunneled the way our own API is — the script is Cloudflare's — so there it only ever failed,
|
|
131
131
|
* a red line per page view, and recorded nothing. Not loading it loses no data (2026-10-08).
|
|
132
132
|
*
|
|
133
133
|
* The guard reads the URL as well as `localStorage.ctgr`: this script runs before the app's
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* spread across N isolates gets N × the global allowance the header calls "the actual defense".
|
|
12
12
|
*
|
|
13
13
|
* A Durable Object is the exact shape of the fix: one instance addressed by name, requests to it
|
|
14
|
-
*
|
|
14
|
+
* serialized, so every isolate in every colo meets the same counts. It runs the SAME
|
|
15
15
|
* `createLoginThrottle` algorithm — nothing here re-decides a threshold or a delay — and persists
|
|
16
16
|
* the ledger to the object's storage after every change, so the object's own eviction (minutes
|
|
17
17
|
* idle, measured on patterns' live object) forgets nothing either.
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
* The throttle is checked BEFORE argon2, on every attempt, and the point of checking first is
|
|
21
21
|
* that a refused attempt is cheap. A D1 read-modify-write per attempt is a query against the
|
|
22
22
|
* invocation's 1,000-query budget and, worse, not atomic across concurrent invocations: two
|
|
23
|
-
* guesses that snapshot the same count both write count+1. The object
|
|
23
|
+
* guesses that snapshot the same count both write count+1. The object serializes for free.
|
|
24
24
|
*
|
|
25
25
|
* ── `attempt` is one operation, not two ─────────────────────────────────────
|
|
26
|
-
* `/throttle/attempt` checks and charges in one
|
|
26
|
+
* `/throttle/attempt` checks and charges in one serialized step, so a burst of concurrent guesses
|
|
27
27
|
* is admitted only up to the threshold rather than all at once against one stale count. See
|
|
28
28
|
* `loginThrottle.ts`'s `attempt` section.
|
|
29
29
|
*
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* spread across N isolates gets N × the global allowance the header calls "the actual defense".
|
|
12
12
|
*
|
|
13
13
|
* A Durable Object is the exact shape of the fix: one instance addressed by name, requests to it
|
|
14
|
-
*
|
|
14
|
+
* serialized, so every isolate in every colo meets the same counts. It runs the SAME
|
|
15
15
|
* `createLoginThrottle` algorithm — nothing here re-decides a threshold or a delay — and persists
|
|
16
16
|
* the ledger to the object's storage after every change, so the object's own eviction (minutes
|
|
17
17
|
* idle, measured on patterns' live object) forgets nothing either.
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
* The throttle is checked BEFORE argon2, on every attempt, and the point of checking first is
|
|
21
21
|
* that a refused attempt is cheap. A D1 read-modify-write per attempt is a query against the
|
|
22
22
|
* invocation's 1,000-query budget and, worse, not atomic across concurrent invocations: two
|
|
23
|
-
* guesses that snapshot the same count both write count+1. The object
|
|
23
|
+
* guesses that snapshot the same count both write count+1. The object serializes for free.
|
|
24
24
|
*
|
|
25
25
|
* ── `attempt` is one operation, not two ─────────────────────────────────────
|
|
26
|
-
* `/throttle/attempt` checks and charges in one
|
|
26
|
+
* `/throttle/attempt` checks and charges in one serialized step, so a burst of concurrent guesses
|
|
27
27
|
* is admitted only up to the threshold rather than all at once against one stale count. See
|
|
28
28
|
* `loginThrottle.ts`'s `attempt` section.
|
|
29
29
|
*
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* production cost 52.85 s
|
|
15
15
|
* test cost 7.32 s
|
|
16
16
|
*
|
|
17
|
-
* Eighty-six
|
|
17
|
+
* Eighty-six percent of what was, that morning, the largest single workspace in
|
|
18
18
|
* `verify:scoped` — paid by every agent on every branch touching auth or any shared
|
|
19
19
|
* package, and again on every union gate.
|
|
20
20
|
*
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*
|
|
19
19
|
* ── One object per payload ───────────────────────────────────────────────────
|
|
20
20
|
* Addressed by `ctgr:<_cid>` (a `crypto.randomUUID()` the client mints per request), so the
|
|
21
|
-
* chunks of one request are
|
|
21
|
+
* chunks of one request are serialized through one object and different requests never
|
|
22
22
|
* contend. The partial lives in the object's memory: chunks arrive milliseconds apart, the idle
|
|
23
23
|
* TTL is 30 s, and an object is only evicted after minutes idle — a partial it loses is one a
|
|
24
24
|
* deploy would have lost too. An object holding nothing costs nothing.
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*
|
|
19
19
|
* ── One object per payload ───────────────────────────────────────────────────
|
|
20
20
|
* Addressed by `ctgr:<_cid>` (a `crypto.randomUUID()` the client mints per request), so the
|
|
21
|
-
* chunks of one request are
|
|
21
|
+
* chunks of one request are serialized through one object and different requests never
|
|
22
22
|
* contend. The partial lives in the object's memory: chunks arrive milliseconds apart, the idle
|
|
23
23
|
* TTL is 30 s, and an object is only evicted after minutes idle — a partial it loses is one a
|
|
24
24
|
* deploy would have lost too. An object holding nothing costs nothing.
|
|
@@ -63,7 +63,7 @@ export interface TimeTravelOpts {
|
|
|
63
63
|
/**
|
|
64
64
|
* The D1 implementation: record the coordinate, because retention is automatic.
|
|
65
65
|
*
|
|
66
|
-
* 🔴 It reports `needsPeriodicCapture: false`, and a scheduler must
|
|
66
|
+
* 🔴 It reports `needsPeriodicCapture: false`, and a scheduler must honor that rather
|
|
67
67
|
* than calling `capture()` on a timer — a nightly no-op job that logs "backup complete"
|
|
68
68
|
* is worse than no job, because it reads as evidence.
|
|
69
69
|
*/
|
package/dist/server/d1/backup.js
CHANGED
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
/**
|
|
37
37
|
* The D1 implementation: record the coordinate, because retention is automatic.
|
|
38
38
|
*
|
|
39
|
-
* 🔴 It reports `needsPeriodicCapture: false`, and a scheduler must
|
|
39
|
+
* 🔴 It reports `needsPeriodicCapture: false`, and a scheduler must honor that rather
|
|
40
40
|
* than calling `capture()` on a timer — a nightly no-op job that logs "backup complete"
|
|
41
41
|
* is worse than no job, because it reads as evidence.
|
|
42
42
|
*/
|
package/dist/server/d1/http.d.ts
CHANGED
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
* the only other door into D1 was a per-app bearer route on the Worker for each one.
|
|
11
11
|
*
|
|
12
12
|
* So the Mac gets the same {@link D1LikeDatabase} a Worker holds, pointed at the same
|
|
13
|
-
* database, and the app's own
|
|
13
|
+
* database, and the app's own catalog code runs unchanged against production — ONE writer
|
|
14
14
|
* of the rows, one schema, one set of statements.
|
|
15
15
|
*
|
|
16
16
|
* ── How ──────────────────────────────────────────────────────────────────────────────────
|
|
17
17
|
* It is a {@link D1BindingLike} over `POST /accounts/:account/d1/database/:db/query` (and
|
|
18
|
-
* `/raw`), handed to {@link createRemoteD1} — so every value
|
|
18
|
+
* `/raw`), handed to {@link createRemoteD1} — so every value normalization, limit check and
|
|
19
19
|
* `success: false` guard is the binding driver's own, not a second copy that could drift.
|
|
20
20
|
* `batch()` sends `{ batch: [...] }`, which D1 runs as one unit exactly as a binding's batch
|
|
21
21
|
* does: measured 2026-09-23 on a throwaway database, a batch whose second statement violated
|
package/dist/server/d1/http.js
CHANGED
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
* the only other door into D1 was a per-app bearer route on the Worker for each one.
|
|
11
11
|
*
|
|
12
12
|
* So the Mac gets the same {@link D1LikeDatabase} a Worker holds, pointed at the same
|
|
13
|
-
* database, and the app's own
|
|
13
|
+
* database, and the app's own catalog code runs unchanged against production — ONE writer
|
|
14
14
|
* of the rows, one schema, one set of statements.
|
|
15
15
|
*
|
|
16
16
|
* ── How ──────────────────────────────────────────────────────────────────────────────────
|
|
17
17
|
* It is a {@link D1BindingLike} over `POST /accounts/:account/d1/database/:db/query` (and
|
|
18
|
-
* `/raw`), handed to {@link createRemoteD1} — so every value
|
|
18
|
+
* `/raw`), handed to {@link createRemoteD1} — so every value normalization, limit check and
|
|
19
19
|
* `success: false` guard is the binding driver's own, not a second copy that could drift.
|
|
20
20
|
* `batch()` sends `{ batch: [...] }`, which D1 runs as one unit exactly as a binding's batch
|
|
21
21
|
* does: measured 2026-09-23 on a throwaway database, a batch whose second statement violated
|
|
@@ -135,7 +135,7 @@ export function perInvocation(db, opts = {}) {
|
|
|
135
135
|
throw new RangeError(`max must be a positive integer, got ${max}`);
|
|
136
136
|
}
|
|
137
137
|
if (max > LIMITS.queriesPerInvocation) {
|
|
138
|
-
throw new D1LimitError('queries per Worker invocation', max, LIMITS.queriesPerInvocation, 'A budget above D1\'s own cap cannot be
|
|
138
|
+
throw new D1LimitError('queries per Worker invocation', max, LIMITS.queriesPerInvocation, 'A budget above D1\'s own cap cannot be honored — the service refuses first. Lower it, or split the work across invocations.');
|
|
139
139
|
}
|
|
140
140
|
const budget = new Budget(max);
|
|
141
141
|
return {
|
|
@@ -93,7 +93,7 @@ export declare function chunkForBind<T>(items: readonly T[], paramsPerItem: numb
|
|
|
93
93
|
* `WHERE … ORDER BY … LIMIT ? OFFSET ?` page and the `COUNT(*)` beside it), and on a write
|
|
94
94
|
* it trades one atomic statement for a batch that is atomic only per chunk. `json_each`
|
|
95
95
|
* over a JSON array is one parameter however long the list is, keeps the statement whole,
|
|
96
|
-
* and SQLite still drives the column's index through the
|
|
96
|
+
* and SQLite still drives the column's index through the materialized set.
|
|
97
97
|
*
|
|
98
98
|
* The array travels as one TEXT value, so the ceiling moves from 100 ids to
|
|
99
99
|
* `LIMITS.valueBytes` (2 MB) of JSON — about 70,000 of this fleet's 25-character ids — and
|
package/dist/server/d1/limits.js
CHANGED
|
@@ -203,7 +203,7 @@ export function chunkForBind(items, paramsPerItem) {
|
|
|
203
203
|
* `WHERE … ORDER BY … LIMIT ? OFFSET ?` page and the `COUNT(*)` beside it), and on a write
|
|
204
204
|
* it trades one atomic statement for a batch that is atomic only per chunk. `json_each`
|
|
205
205
|
* over a JSON array is one parameter however long the list is, keeps the statement whole,
|
|
206
|
-
* and SQLite still drives the column's index through the
|
|
206
|
+
* and SQLite still drives the column's index through the materialized set.
|
|
207
207
|
*
|
|
208
208
|
* The array travels as one TEXT value, so the ceiling moves from 100 ids to
|
|
209
209
|
* `LIMITS.valueBytes` (2 MB) of JSON — about 70,000 of this fleet's 25-character ids — and
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
* a 6-field expression with a leading seconds column, and this repo already contains three
|
|
32
32
|
* jobs declared `'* * * * * *'` — once per second. Those cannot become Cron Triggers at
|
|
33
33
|
* all, and {@link classifySchedule} says so by name instead of silently rounding them up to
|
|
34
|
-
* a minute, which would change
|
|
34
|
+
* a minute, which would change behavior in production and nowhere else.
|
|
35
35
|
*/
|
|
36
36
|
import type { D1LikeDatabase } from './types.js';
|
|
37
37
|
/** The Workers primitive a piece of scheduled work maps onto. */
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
* a 6-field expression with a leading seconds column, and this repo already contains three
|
|
32
32
|
* jobs declared `'* * * * * *'` — once per second. Those cannot become Cron Triggers at
|
|
33
33
|
* all, and {@link classifySchedule} says so by name instead of silently rounding them up to
|
|
34
|
-
* a minute, which would change
|
|
34
|
+
* a minute, which would change behavior in production and nowhere else.
|
|
35
35
|
*/
|
|
36
36
|
/** Cloudflare Queues: $0.40 per million operations, first million free each month. */
|
|
37
37
|
const QUEUE_USD_PER_MILLION = 0.4;
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
*
|
|
41
41
|
* D1 has no interactive transaction — there is no `BEGIN`/`COMMIT` over HTTP, only
|
|
42
42
|
* {@link D1LikeDatabase.batch}, which is atomic and runs as one round trip. A local
|
|
43
|
-
* driver that happily
|
|
43
|
+
* driver that happily honored `BEGIN` would be the sync-vs-async trap a third time, so
|
|
44
44
|
* {@link D1UnsupportedError} is thrown by both. Wrap the statements in `batch()` instead;
|
|
45
45
|
* it is atomic on D1 and wrapped in a real `bun:sqlite` transaction locally, so the
|
|
46
46
|
* guarantee is the same.
|
package/dist/server/d1/types.js
CHANGED
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
*
|
|
41
41
|
* D1 has no interactive transaction — there is no `BEGIN`/`COMMIT` over HTTP, only
|
|
42
42
|
* {@link D1LikeDatabase.batch}, which is atomic and runs as one round trip. A local
|
|
43
|
-
* driver that happily
|
|
43
|
+
* driver that happily honored `BEGIN` would be the sync-vs-async trap a third time, so
|
|
44
44
|
* {@link D1UnsupportedError} is thrown by both. Wrap the statements in `batch()` instead;
|
|
45
45
|
* it is atomic on D1 and wrapped in a real `bun:sqlite` transaction locally, so the
|
|
46
46
|
* guarantee is the same.
|
|
@@ -36,7 +36,7 @@ export declare const normalizeBinds: (values: readonly unknown[]) => D1LikeBinda
|
|
|
36
36
|
* Only a leading `SELECT` is claimed, because that is the one keyword that is
|
|
37
37
|
* unambiguous: `WITH … INSERT … RETURNING` and `INSERT … RETURNING` both return rows AND
|
|
38
38
|
* write. Anything this returns `false` for is measured exactly instead (see
|
|
39
|
-
* `local.ts`), so a statement shape this does not
|
|
39
|
+
* `local.ts`), so a statement shape this does not recognize costs two extra queries —
|
|
40
40
|
* never a wrong number.
|
|
41
41
|
*/
|
|
42
42
|
export declare const isPureReadSql: (sql: string) => boolean;
|
package/dist/server/d1/values.js
CHANGED
|
@@ -84,7 +84,7 @@ const LEADING_NOISE = /^(?:\s|--[^\n]*\n?|\/\*[\s\S]*?\*\/)+/;
|
|
|
84
84
|
* Only a leading `SELECT` is claimed, because that is the one keyword that is
|
|
85
85
|
* unambiguous: `WITH … INSERT … RETURNING` and `INSERT … RETURNING` both return rows AND
|
|
86
86
|
* write. Anything this returns `false` for is measured exactly instead (see
|
|
87
|
-
* `local.ts`), so a statement shape this does not
|
|
87
|
+
* `local.ts`), so a statement shape this does not recognize costs two extra queries —
|
|
88
88
|
* never a wrong number.
|
|
89
89
|
*/
|
|
90
90
|
export const isPureReadSql = (sql) => /^select\b/i.test(sql.replace(LEADING_NOISE, ''));
|
|
@@ -99,7 +99,7 @@ export interface ScanOptions {
|
|
|
99
99
|
*
|
|
100
100
|
* 🔴 Load-bearing for sidecar art, and it cannot be done later. A folder's `cover.jpg` is
|
|
101
101
|
* only reachable while the directory still exists, and filing the audio out of it removes
|
|
102
|
-
* the
|
|
102
|
+
* the neighbors — by the time anything else could look, they are gone. Capturing on the
|
|
103
103
|
* walk also costs one `readdir` per directory instead of one per file, which on a 6,000
|
|
104
104
|
* track library is the difference between seconds and minutes.
|
|
105
105
|
*/
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const ENGAGEMENT_ACTION_DDL: readonly string[];
|
|
2
|
+
/** One resolved action, ready to count. `place` and `action` have been through the allowlists. */
|
|
3
|
+
export interface ActionUse {
|
|
4
|
+
userKey: string;
|
|
5
|
+
place: string;
|
|
6
|
+
action: string;
|
|
7
|
+
ts: number;
|
|
8
|
+
}
|
|
9
|
+
/** The two upserts one action is — run them together (a transaction, or one D1 batch). */
|
|
10
|
+
export declare function actionStatements(use: ActionUse): Array<{
|
|
11
|
+
sql: string;
|
|
12
|
+
params: (string | number)[];
|
|
13
|
+
}>;
|
|
14
|
+
/** Read the claimed `{ place, action }` out of a request body — a string each, or a reason why not. */
|
|
15
|
+
export declare function claimedAction(body: unknown): {
|
|
16
|
+
place: string;
|
|
17
|
+
action: string;
|
|
18
|
+
} | {
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature ACTIONS — what somebody DID on a page, beside where they were (task 3470).
|
|
3
|
+
*
|
|
4
|
+
* A place-view answers "which pages do they open". The owner's question goes one step further:
|
|
5
|
+
* *"what features they use and ignore … and know which users to ask first when I am planning to
|
|
6
|
+
* add or delete a feature"*. A page is not a feature — music's library page holds play, search,
|
|
7
|
+
* rate and download — so this records a declared ACTION NAME per (account, place).
|
|
8
|
+
*
|
|
9
|
+
* ── The privacy contract is the places' contract, unchanged ─────────────────
|
|
10
|
+
* The action is a NAME from the app's own `routes.manifest.json` `actions` list
|
|
11
|
+
* (`policy.ts:resolveAction`); the place is resolved against the same manifest as a view. There is
|
|
12
|
+
* no field for a value — `search`, never the query; `play`, never the track — so nothing a person
|
|
13
|
+
* typed or chose can reach a row. The account is the session's subject, never the body's.
|
|
14
|
+
*
|
|
15
|
+
* ── Counts and clocks, two grains ───────────────────────────────────────────
|
|
16
|
+
* engagement_actions (user, place, action) → uses, first, last. LIFETIME, never trimmed:
|
|
17
|
+
* it is the row that answers "who uses this most, and when did they last",
|
|
18
|
+
* and trimming it would make a feature's old regulars vanish.
|
|
19
|
+
* engagement_action_days (user, place, action, day) → uses. Trimmed with the view days, for
|
|
20
|
+
* "used this month" against "used once, long ago".
|
|
21
|
+
* No event log, no sequence: an action does not touch the sessionization clock or dwell.
|
|
22
|
+
*
|
|
23
|
+
* 🔴 Its own DDL, not appended to `ENGAGEMENT_DDL`: an app whose schema is generated from that list
|
|
24
|
+
* (music, family, desk, arch) would otherwise go red on the version bump alone. An app opts in by
|
|
25
|
+
* declaring `actions` and applying {@link ENGAGEMENT_ACTION_DDL}; the Bun store always creates both.
|
|
26
|
+
*
|
|
27
|
+
* Pure and Worker-safe: the Bun store and the D1 recorder both write through
|
|
28
|
+
* {@link actionStatements}, so there is one write, not two that agree.
|
|
29
|
+
*/
|
|
30
|
+
import { dayKey } from "./policy.js";
|
|
31
|
+
export const ENGAGEMENT_ACTION_DDL = [
|
|
32
|
+
"CREATE TABLE IF NOT EXISTS engagement_actions ( user_key TEXT NOT NULL, place TEXT NOT NULL, action TEXT NOT NULL, uses INTEGER NOT NULL DEFAULT 0, first_ts INTEGER NOT NULL, last_ts INTEGER NOT NULL, PRIMARY KEY (user_key, place, action) )",
|
|
33
|
+
"CREATE TABLE IF NOT EXISTS engagement_action_days ( user_key TEXT NOT NULL, place TEXT NOT NULL, action TEXT NOT NULL, day TEXT NOT NULL, uses INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (user_key, place, action, day) )",
|
|
34
|
+
"CREATE INDEX IF NOT EXISTS idx_engagement_action_days_day ON engagement_action_days (day)",
|
|
35
|
+
];
|
|
36
|
+
/** The two upserts one action is — run them together (a transaction, or one D1 batch). */
|
|
37
|
+
export function actionStatements(use) {
|
|
38
|
+
const { userKey, place, action, ts } = use;
|
|
39
|
+
return [
|
|
40
|
+
{
|
|
41
|
+
sql: `INSERT INTO engagement_actions (user_key, place, action, uses, first_ts, last_ts)
|
|
42
|
+
VALUES (?, ?, ?, 1, ?, ?)
|
|
43
|
+
ON CONFLICT(user_key, place, action) DO UPDATE SET uses = uses + 1, last_ts = excluded.last_ts`,
|
|
44
|
+
params: [userKey, place, action, ts, ts],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
sql: `INSERT INTO engagement_action_days (user_key, place, action, day, uses)
|
|
48
|
+
VALUES (?, ?, ?, ?, 1)
|
|
49
|
+
ON CONFLICT(user_key, place, action, day) DO UPDATE SET uses = uses + 1`,
|
|
50
|
+
params: [userKey, place, action, dayKey(ts)],
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
/** Read the claimed `{ place, action }` out of a request body — a string each, or a reason why not. */
|
|
55
|
+
export function claimedAction(body) {
|
|
56
|
+
const b = body;
|
|
57
|
+
if (typeof b?.action !== "string")
|
|
58
|
+
return { error: "action must be a string" };
|
|
59
|
+
if (typeof b.place !== "string")
|
|
60
|
+
return { error: "place must be a string" };
|
|
61
|
+
return { place: b.place, action: b.action };
|
|
62
|
+
}
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* POST /api/engagement/view → the beacon. SESSION-gated; the user id comes
|
|
5
5
|
* from the session, never from the body.
|
|
6
|
+
* POST /api/engagement/action → a feature used on a page — `{ place, action }`, both
|
|
7
|
+
* NAMES resolved against the manifest (`actions.ts`).
|
|
8
|
+
* Mounted only for an app that passes `actions`.
|
|
6
9
|
*
|
|
7
10
|
* 🔴 **There is no HTTP read surface, on purpose** (task `113`/`268`, 2026-09-22).
|
|
8
11
|
* `GET /api/admin/engagement` used to sit here behind `SATELLITE_METRICS_TOKEN` —
|
|
@@ -31,7 +34,7 @@
|
|
|
31
34
|
*/
|
|
32
35
|
import { Hono } from "hono";
|
|
33
36
|
import type { Context } from "hono";
|
|
34
|
-
import { type PlaceRef } from "./policy.js";
|
|
37
|
+
import { type ActionRef, type PlaceRef } from "./policy.js";
|
|
35
38
|
import type { EngagementStore } from "./store.js";
|
|
36
39
|
import { type AppEngagement } from "./summary.js";
|
|
37
40
|
export interface EngagementApiOptions {
|
|
@@ -44,6 +47,12 @@ export interface EngagementApiOptions {
|
|
|
44
47
|
* rule 1 for why this is the whole privacy story.
|
|
45
48
|
*/
|
|
46
49
|
places: readonly PlaceRef[];
|
|
50
|
+
/**
|
|
51
|
+
* The allowlist of ACTION names — the manifest's `actions` (`engagementActionsFor`). Present,
|
|
52
|
+
* the recorder grows `POST /action`; an undeclared name records as `other`, as a place does.
|
|
53
|
+
* Absent, there is no action door at all (task 3470).
|
|
54
|
+
*/
|
|
55
|
+
actions?: readonly ActionRef[];
|
|
47
56
|
/** Resolve the CURRENT request's account. Null → 401, nothing recorded. */
|
|
48
57
|
resolveUser: (c: Context) => string | null;
|
|
49
58
|
/** Injectable clock, so the tests are not timing-dependent. */
|
|
@@ -56,6 +65,8 @@ export interface AppEngagementSnapshot extends AppEngagement {
|
|
|
56
65
|
/** The manifest ids this app CAN report — so a reader can show a place
|
|
57
66
|
* with zero views, which is the half of the question that is easy to lose. */
|
|
58
67
|
knownPlaces: string[];
|
|
68
|
+
/** The action names this app declares — a declared action nobody used is the "ignored" half. */
|
|
69
|
+
knownActions: string[];
|
|
59
70
|
returnCurve: Array<{
|
|
60
71
|
day: string;
|
|
61
72
|
joined: number;
|
|
@@ -68,4 +79,4 @@ export interface AppEngagementSnapshot extends AppEngagement {
|
|
|
68
79
|
oldestDay: string | null;
|
|
69
80
|
};
|
|
70
81
|
}
|
|
71
|
-
export declare function readAppEngagement(store: EngagementStore, app: string, now: number, knownPlaces?: readonly PlaceRef[]): AppEngagementSnapshot;
|
|
82
|
+
export declare function readAppEngagement(store: EngagementStore, app: string, now: number, knownPlaces?: readonly PlaceRef[], knownActions?: readonly ActionRef[]): AppEngagementSnapshot;
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* POST /api/engagement/view → the beacon. SESSION-gated; the user id comes
|
|
5
5
|
* from the session, never from the body.
|
|
6
|
+
* POST /api/engagement/action → a feature used on a page — `{ place, action }`, both
|
|
7
|
+
* NAMES resolved against the manifest (`actions.ts`).
|
|
8
|
+
* Mounted only for an app that passes `actions`.
|
|
6
9
|
*
|
|
7
10
|
* 🔴 **There is no HTTP read surface, on purpose** (task `113`/`268`, 2026-09-22).
|
|
8
11
|
* `GET /api/admin/engagement` used to sit here behind `SATELLITE_METRICS_TOKEN` —
|
|
@@ -30,7 +33,8 @@
|
|
|
30
33
|
* rule and its reason: `docs/engagement.md` §2 in this package.
|
|
31
34
|
*/
|
|
32
35
|
import { Hono } from "hono";
|
|
33
|
-
import {
|
|
36
|
+
import { claimedAction } from "./actions.js";
|
|
37
|
+
import { OTHER_PLACE, resolveAction, resolvePlace } from "./policy.js";
|
|
34
38
|
import { returnCurve, summarize } from "./summary.js";
|
|
35
39
|
/** The recorder half — session-gated, mounted at `/api/engagement`. */
|
|
36
40
|
export function createEngagementRecorder(options) {
|
|
@@ -61,9 +65,31 @@ export function createEngagementRecorder(options) {
|
|
|
61
65
|
// unpopular feature.
|
|
62
66
|
return c.json({ ok: true, place, bucketed: place === OTHER_PLACE, ...fold });
|
|
63
67
|
});
|
|
68
|
+
const actions = options.actions;
|
|
69
|
+
if (actions) {
|
|
70
|
+
app.post("/action", async (c) => {
|
|
71
|
+
const userKey = options.resolveUser(c);
|
|
72
|
+
if (!userKey)
|
|
73
|
+
return c.json({ error: "unauthenticated" }, 401);
|
|
74
|
+
let body;
|
|
75
|
+
try {
|
|
76
|
+
body = await c.req.json();
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return c.json({ error: "expected a json body" }, 400);
|
|
80
|
+
}
|
|
81
|
+
const claimed = claimedAction(body);
|
|
82
|
+
if ("error" in claimed)
|
|
83
|
+
return c.json({ error: claimed.error }, 400);
|
|
84
|
+
const place = resolvePlace(claimed.place, options.places);
|
|
85
|
+
const action = resolveAction(claimed.action, actions);
|
|
86
|
+
options.store.recordAction({ userKey, place, action, ts: now() });
|
|
87
|
+
return c.json({ ok: true, place, action, bucketed: place === OTHER_PLACE || action === OTHER_PLACE });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
64
90
|
return app;
|
|
65
91
|
}
|
|
66
|
-
export function readAppEngagement(store, app, now, knownPlaces = []) {
|
|
92
|
+
export function readAppEngagement(store, app, now, knownPlaces = [], knownActions = []) {
|
|
67
93
|
const users = store.users();
|
|
68
94
|
const summary = summarize({
|
|
69
95
|
app,
|
|
@@ -74,10 +100,12 @@ export function readAppEngagement(store, app, now, knownPlaces = []) {
|
|
|
74
100
|
// silently return less, which reads as a quiet period rather than as a
|
|
75
101
|
// window boundary.
|
|
76
102
|
days: store.days(now - 120 * 86_400_000),
|
|
103
|
+
actions: store.actions(),
|
|
77
104
|
});
|
|
78
105
|
return {
|
|
79
106
|
...summary,
|
|
80
107
|
knownPlaces: knownPlaces.map((p) => p.id),
|
|
108
|
+
knownActions: knownActions.map((a) => a.id),
|
|
81
109
|
returnCurve: returnCurve(users, now),
|
|
82
110
|
retention: store.stats(),
|
|
83
111
|
};
|
|
@@ -31,13 +31,16 @@
|
|
|
31
31
|
*
|
|
32
32
|
* gated.route("/api/engagement", createD1EngagementRecorder({
|
|
33
33
|
* db, places: placesFromManifest(routesManifest), resolveUser: (c) => sso.userOf(c)?.id ?? null,
|
|
34
|
+
* // Optional — feature actions (task 3470); the D1 then needs ENGAGEMENT_ACTION_DDL too.
|
|
35
|
+
* actions: actionsFromManifest(routesManifest),
|
|
34
36
|
* }));
|
|
35
37
|
* ```
|
|
36
38
|
*/
|
|
37
39
|
import type { D1LikeDatabase } from "../d1/types.js";
|
|
38
40
|
import type { Context } from "hono";
|
|
39
41
|
import { Hono } from "hono";
|
|
40
|
-
import { type
|
|
42
|
+
import { type ActionUse } from "./actions.js";
|
|
43
|
+
import { type ActionRef, type PlaceRef } from "./policy.js";
|
|
41
44
|
/** The kit store's own tables and indexes, as one-line DDL — see the header. */
|
|
42
45
|
export declare const ENGAGEMENT_DDL: readonly string[];
|
|
43
46
|
/**
|
|
@@ -53,6 +56,8 @@ export declare function recordViewD1(db: D1LikeDatabase, input: {
|
|
|
53
56
|
newSession: boolean;
|
|
54
57
|
activeMs: number;
|
|
55
58
|
}>;
|
|
59
|
+
/** Count one resolved action — the Bun store's `recordAction`, as ONE D1 batch (`./actions.ts`). */
|
|
60
|
+
export declare function recordActionD1(db: D1LikeDatabase, use: ActionUse): Promise<void>;
|
|
56
61
|
/**
|
|
57
62
|
* The recorder as the kit's `createEngagementRecorder` mounts it — `POST /view` under
|
|
58
63
|
* `ENGAGEMENT_BASE_PATH` — over D1. Recording only: there is no read surface here, as there is
|
|
@@ -61,7 +66,13 @@ export declare function recordViewD1(db: D1LikeDatabase, input: {
|
|
|
61
66
|
export declare function createD1EngagementRecorder(options: {
|
|
62
67
|
db: D1LikeDatabase;
|
|
63
68
|
places: readonly PlaceRef[];
|
|
69
|
+
/**
|
|
70
|
+
* The manifest's declared actions (`actionsFromManifest`). Present, `POST /action` is mounted
|
|
71
|
+
* and the D1 needs {@link ENGAGEMENT_ACTION_DDL} applied; absent, there is no action door.
|
|
72
|
+
*/
|
|
73
|
+
actions?: readonly ActionRef[];
|
|
64
74
|
resolveUser: (c: Context) => string | null;
|
|
65
75
|
now?: () => number;
|
|
66
76
|
}): Hono;
|
|
67
|
-
export {
|
|
77
|
+
export { ENGAGEMENT_ACTION_DDL } from "./actions.js";
|
|
78
|
+
export { actionsFromManifest, placesFromManifest } from "./manifestPlaces.js";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
|
-
import {
|
|
2
|
+
import { actionStatements, claimedAction } from "./actions.js";
|
|
3
|
+
import { dayKey, foldView, OTHER_PLACE, resolveAction, resolvePlace } from "./policy.js";
|
|
3
4
|
/** The kit store's own tables and indexes, as one-line DDL — see the header. */
|
|
4
5
|
export const ENGAGEMENT_DDL = [
|
|
5
6
|
"CREATE TABLE IF NOT EXISTS engagement_days ( user_key TEXT NOT NULL, place TEXT NOT NULL, day TEXT NOT NULL, views INTEGER NOT NULL DEFAULT 0, active_ms INTEGER NOT NULL DEFAULT 0, sessions INTEGER NOT NULL DEFAULT 0, first_ts INTEGER NOT NULL, last_ts INTEGER NOT NULL, PRIMARY KEY (user_key, place, day) )",
|
|
@@ -50,6 +51,10 @@ export async function recordViewD1(db, input) {
|
|
|
50
51
|
await db.batch(statements);
|
|
51
52
|
return fold;
|
|
52
53
|
}
|
|
54
|
+
/** Count one resolved action — the Bun store's `recordAction`, as ONE D1 batch (`./actions.ts`). */
|
|
55
|
+
export async function recordActionD1(db, use) {
|
|
56
|
+
await db.batch(actionStatements(use).map(({ sql, params }) => db.prepare(sql).bind(...params)));
|
|
57
|
+
}
|
|
53
58
|
/**
|
|
54
59
|
* The recorder as the kit's `createEngagementRecorder` mounts it — `POST /view` under
|
|
55
60
|
* `ENGAGEMENT_BASE_PATH` — over D1. Recording only: there is no read surface here, as there is
|
|
@@ -76,6 +81,29 @@ export function createD1EngagementRecorder(options) {
|
|
|
76
81
|
const fold = await recordViewD1(options.db, { userKey, place, ts: now() });
|
|
77
82
|
return c.json({ ok: true, place, bucketed: place === OTHER_PLACE, ...fold });
|
|
78
83
|
});
|
|
84
|
+
const actions = options.actions;
|
|
85
|
+
if (actions) {
|
|
86
|
+
app.post("/action", async (c) => {
|
|
87
|
+
const userKey = options.resolveUser(c);
|
|
88
|
+
if (!userKey)
|
|
89
|
+
return c.json({ error: "unauthenticated" }, 401);
|
|
90
|
+
let body;
|
|
91
|
+
try {
|
|
92
|
+
body = await c.req.json();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return c.json({ error: "expected a json body" }, 400);
|
|
96
|
+
}
|
|
97
|
+
const claimed = claimedAction(body);
|
|
98
|
+
if ("error" in claimed)
|
|
99
|
+
return c.json({ error: claimed.error }, 400);
|
|
100
|
+
const place = resolvePlace(claimed.place, options.places);
|
|
101
|
+
const action = resolveAction(claimed.action, actions);
|
|
102
|
+
await recordActionD1(options.db, { userKey, place, action, ts: now() });
|
|
103
|
+
return c.json({ ok: true, place, action, bucketed: place === OTHER_PLACE || action === OTHER_PLACE });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
79
106
|
return app;
|
|
80
107
|
}
|
|
81
|
-
export {
|
|
108
|
+
export { ENGAGEMENT_ACTION_DDL } from "./actions.js";
|
|
109
|
+
export { actionsFromManifest, placesFromManifest } from "./manifestPlaces.js";
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* `policy.ts` holds the three privacy rules that make it safe to run USER-KEYED analytics
|
|
8
8
|
* inside private household apps, and four copies of a privacy promise is four promises, with
|
|
9
9
|
* nothing keeping them the same — three of the twelve files had already forked (in `music`,
|
|
10
|
-
* doc comments only; the
|
|
10
|
+
* doc comments only; the behavior was identical in all four, so the plurality's text is what
|
|
11
11
|
* came across). The browser half — the beacon — is React and lives in the design system's tier;
|
|
12
12
|
* it depends on nothing here but the wire shape `POST <mount>/view {place}`.
|
|
13
13
|
*
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
*/
|
|
50
50
|
export { type AppEngagementSnapshot, createEngagementRecorder, type EngagementApiOptions, readAppEngagement, } from "./api.js";
|
|
51
51
|
export { type EngagementEnv, type EngagementEnvOptions, readEngagementEnv } from "./env.js";
|
|
52
|
-
export {
|
|
53
|
-
export {
|
|
52
|
+
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
|
+
export { type EngagementPlace, engagementActionsFor, engagementPlacesFor } from "./places.js";
|
|
54
|
+
export { type ActionRef, classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, engagementAllowedForApp, foldView, INTEREST_ACTIVE_DAYS, INTEREST_FADING_DAYS, type InterestVerdict, MAX_DWELL_SLICE_MS, normalizePlaceId, OTHER_PLACE, type PlaceRef, resolvePlace, SESSION_GAP_MS, type ViewFold, } from "./policy.js";
|
|
54
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, type EngagementDay, EngagementStore, type EngagementStoreOptions, type EngagementUser, type EngagementUserPlace, } from "./store.js";
|
|
55
|
-
export { type AppEngagement, type DailyEngagement, type PlaceEngagement, returnCurve, summarize, type SummarizeInput, type UserEngagement, } from "./summary.js";
|
|
56
|
+
export { type ActionEngagement, type AppEngagement, type DailyEngagement, type PlaceEngagement, returnCurve, summarize, type SummarizeInput, type UserEngagement, } from "./summary.js";
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* `policy.ts` holds the three privacy rules that make it safe to run USER-KEYED analytics
|
|
8
8
|
* inside private household apps, and four copies of a privacy promise is four promises, with
|
|
9
9
|
* nothing keeping them the same — three of the twelve files had already forked (in `music`,
|
|
10
|
-
* doc comments only; the
|
|
10
|
+
* doc comments only; the behavior was identical in all four, so the plurality's text is what
|
|
11
11
|
* came across). The browser half — the beacon — is React and lives in the design system's tier;
|
|
12
12
|
* it depends on nothing here but the wire shape `POST <mount>/view {place}`.
|
|
13
13
|
*
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
*/
|
|
50
50
|
export { createEngagementRecorder, readAppEngagement, } from "./api.js";
|
|
51
51
|
export { readEngagementEnv } from "./env.js";
|
|
52
|
-
export {
|
|
52
|
+
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
|
+
export { engagementActionsFor, engagementPlacesFor } from "./places.js";
|
|
53
54
|
export { classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, engagementAllowedForApp, foldView, INTEREST_ACTIVE_DAYS, INTEREST_FADING_DAYS, MAX_DWELL_SLICE_MS, normalizePlaceId, OTHER_PLACE, resolvePlace, SESSION_GAP_MS, } from "./policy.js";
|
|
54
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, EngagementStore, } from "./store.js";
|
|
55
56
|
export { returnCurve, summarize, } from "./summary.js";
|