@classytic/arc 2.48.0 → 2.49.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/dist/{EventTransport-BWwg5YAU.mjs → EventTransport-DbHZK230.mjs} +2 -2
- package/dist/{HookSystem-B9x-Y_k7.mjs → HookSystem-DMoeJ6Do.mjs} +2 -2
- package/dist/{QueryCache-DdvrGG8Q.d.mts → QueryCache-BE47tmlO.d.mts} +36 -1
- package/dist/{ResourceRegistry-BcvEJoZd.mjs → ResourceRegistry-CP-iTcKy.mjs} +2 -2
- package/dist/{acquire-D2yj6Zzl.mjs → acquire-CB0va87C.mjs} +1 -1
- package/dist/audit/index.d.mts +1 -1
- package/dist/audit/index.mjs +4 -4
- package/dist/auth/audit.mjs +1 -1
- package/dist/auth/index.d.mts +2 -2
- package/dist/auth/index.mjs +53 -15
- package/dist/auth/redis-session.mjs +1 -1
- package/dist/{authorizationDecision-BZSJcPNE.mjs → authorizationDecision-DHO94aV2.mjs} +4 -4
- package/dist/{boundedMap-o2KZDCWy.mjs → boundedMap-C51EUUOm.mjs} +1 -1
- package/dist/{buildHandler-G3k59KY2.mjs → buildHandler-9DuwEKvr.mjs} +23 -16
- package/dist/cache/index.d.mts +2 -2
- package/dist/cache/index.mjs +4 -4
- package/dist/{caching-CJIDPZOv.mjs → caching-COTX3sDa.mjs} +3 -6
- package/dist/{claimed-D0A43wjT.mjs → claimed-C7T1nHlo.mjs} +1 -1
- package/dist/cleanup/index.d.mts +2 -2
- package/dist/cleanup/index.mjs +4 -4
- package/dist/cli/commands/describe.d.mts +1 -1
- package/dist/cli/commands/describe.mjs +3 -3
- package/dist/cli/commands/docs.mjs +3 -3
- package/dist/cli/commands/doctor.mjs +1 -1
- package/dist/cli/commands/generate.mjs +2 -2
- package/dist/cli/commands/init.mjs +2 -2
- package/dist/cli/commands/introspect.mjs +2 -2
- package/dist/cli/index.mjs +3 -3
- package/dist/{constants-D-faL5SX.mjs → constants-DOnfMd7i.mjs} +1 -1
- package/dist/context/index.mjs +5 -5
- package/dist/core/index.d.mts +3 -3
- package/dist/core/index.mjs +7 -7
- package/dist/{core-NSu1peyq.mjs → core-BoEdFDDw.mjs} +3 -3
- package/dist/{createActionRouter-DTSVv1VK.mjs → createActionRouter-CpTn15dm.mjs} +54 -18
- package/dist/{createAggregationRouter-zEasm-ax.mjs → createAggregationRouter-CD_Hydp0.mjs} +13 -11
- package/dist/{createApp-CjqhkX1x.mjs → createApp-Be6XTGD9.mjs} +332 -40
- package/dist/{createMcpServer-hSFeqRUb.d.mts → createMcpServer-CS2KOVP6.d.mts} +1 -1
- package/dist/{defineAggregation-DH2L6_v9.mjs → defineAggregation-BI5pRb8c.mjs} +1 -1
- package/dist/{defineResource-zYCrhNaN.mjs → defineResource-BAzNu5Um.mjs} +8 -8
- package/dist/discovery/index.mjs +1 -1
- package/dist/docs/index.d.mts +1 -1
- package/dist/docs/index.mjs +3 -3
- package/dist/{elevation-z--rFcZ5.mjs → elevation-DupGuM41.mjs} +6 -9
- package/dist/encryption/index.mjs +3 -3
- package/dist/{errorHandler-3_3oN-Uc.mjs → errorHandler-DA03vRqp.mjs} +4 -7
- package/dist/{errors-DyY48oRi.mjs → errors-BpYV859Y.mjs} +1 -1
- package/dist/{eventPlugin-Co9sABCg.mjs → eventPlugin-D7LYtYzL.mjs} +9 -13
- package/dist/{eventTypes-HmAFyJmJ.mjs → eventTypes-BqA-N-Uz.mjs} +2 -2
- package/dist/events/index.d.mts +18 -5
- package/dist/events/index.mjs +8 -8
- package/dist/events/transports/redis-stream-entry.mjs +4 -4
- package/dist/events/transports/redis.d.mts +35 -0
- package/dist/events/transports/redis.mjs +87 -11
- package/dist/factory/index.d.mts +4 -4
- package/dist/factory/index.mjs +6 -6
- package/dist/{fastify-augmentation-bzkWPt1f.d.mts → fastify-augmentation-baQc3MaO.d.mts} +2 -2
- package/dist/fieldTypes-EFWxCkZ5.mjs +80 -0
- package/dist/{fields-rfsl26Ov.mjs → fields-CT1Erg7o.mjs} +1 -1
- package/dist/{filter-merge-8glLr7Dk.mjs → filter-merge-B1eJ1pMx.mjs} +1 -1
- package/dist/{generate-BOfBueRt.mjs → generate-Cr0ULavs.mjs} +2 -2
- package/dist/{headers-CHSOqpcY.mjs → headers-D4bncIOf.mjs} +2 -2
- package/dist/{health-ezwTHQh2.mjs → health-BoFxrkJE.mjs} +1 -1
- package/dist/{health-DHvAIVmM.mjs → health-DN_pUVMX.mjs} +1 -1
- package/dist/hooks/index.d.mts +1 -1
- package/dist/hooks/index.mjs +2 -2
- package/dist/http-cache/index.d.mts +2 -2
- package/dist/http-cache/index.mjs +4 -4
- package/dist/http-cache/purgers/cloudflare.mjs +2 -2
- package/dist/http-cache/purgers/fastly.mjs +2 -2
- package/dist/http-cache/testing/index.mjs +1 -1
- package/dist/idempotency/index.d.mts +1 -1
- package/dist/idempotency/index.mjs +10 -8
- package/dist/idempotency/redis.mjs +2 -2
- package/dist/{index-B5i9-vuP.d.mts → index-CMNpMBtW.d.mts} +1 -1
- package/dist/{index-BxZS-8So.d.mts → index-DKM2p5w5.d.mts} +48 -23
- package/dist/{index-Cy2-QffW.d.mts → index-DLCcLUQT.d.mts} +69 -6
- package/dist/{index-DFCCQ9AE.d.mts → index-DexdFs1a.d.mts} +1 -1
- package/dist/{index-D1X_C_qz.d.mts → index-Dhrks-hM.d.mts} +60 -10
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +11 -11
- package/dist/{init-MoE3eZNR.mjs → init-C4a24wD2.mjs} +1 -1
- package/dist/integrations/event-gateway.mjs +2 -2
- package/dist/integrations/index.d.mts +2 -2
- package/dist/integrations/index.mjs +1 -1
- package/dist/integrations/jobs/index.mjs +3 -3
- package/dist/integrations/mcp/index.d.mts +5 -5
- package/dist/integrations/mcp/index.mjs +2 -2
- package/dist/integrations/mcp/testing.d.mts +2 -2
- package/dist/integrations/mcp/testing.mjs +2 -2
- package/dist/integrations/streamline.mjs +3 -3
- package/dist/integrations/webhooks.d.mts +1 -1
- package/dist/integrations/webhooks.mjs +4 -4
- package/dist/integrations/websocket-pushref-redis.mjs +1 -1
- package/dist/integrations/websocket-redis.mjs +1 -1
- package/dist/integrations/websocket.mjs +2 -2
- package/dist/{jsonBody-DZGiCawf.mjs → jsonBody-DoTt5BS6.mjs} +2 -2
- package/dist/{keys-CrMOZuk6.mjs → keys--l9n7aOC.mjs} +1 -1
- package/dist/{loadResources-CVQvYUhN.mjs → loadResources-rygByIQA.mjs} +3 -3
- package/dist/{loadResourcesFromEntry-D0naccV2.mjs → loadResourcesFromEntry-ItNBIQ5w.mjs} +2 -2
- package/dist/logger/index.mjs +2 -2
- package/dist/{logger-VyMcSmVG.mjs → logger-UbknHqHy.mjs} +1 -1
- package/dist/{mcpPlugin-DQACaqVu.mjs → mcpPlugin-CTkk4RP8.mjs} +29 -26
- package/dist/{memory-Lpfu8Hga.mjs → memory-C47xVsjX.mjs} +3 -3
- package/dist/{metrics-DjVj1sg5.mjs → metrics-TJyAb1Xk.mjs} +4 -7
- package/dist/middleware/index.d.mts +1 -1
- package/dist/middleware/index.mjs +3 -3
- package/dist/migrations/index.mjs +2 -2
- package/dist/{module-BqPJpZ_L.mjs → module-CWiqsXnh.mjs} +1 -1
- package/dist/{multiTenant-KZj8j1uJ.d.mts → multiTenant-QtDzxx_K.d.mts} +1 -1
- package/dist/{multipartBody-BNgNv7r0.mjs → multipartBody-BdG1qbn1.mjs} +1 -1
- package/dist/{observeSend-BKJHibJz.mjs → observeSend-DrA3ADFN.mjs} +1 -1
- package/dist/{openapi-T90KrPR6.mjs → openapi-D6MAbYpv.mjs} +7 -7
- package/dist/outbox-admin/index.d.mts +1 -1
- package/dist/outbox-admin/index.mjs +5 -5
- package/dist/{outbox-module-C874VgBr.mjs → outbox-module-D5QABFQW.mjs} +197 -28
- package/dist/{outbox-module-5Mnwcycy.d.mts → outbox-module-DzW6zwWJ.d.mts} +103 -10
- package/dist/permissions/index.d.mts +2 -2
- package/dist/permissions/index.mjs +7 -7
- package/dist/{permissions-R9T2G1PH.mjs → permissions-CFsmdYUt.mjs} +9 -9
- package/dist/{pipe-DzqbsZy-.mjs → pipe-B_05AgGt.mjs} +2 -2
- package/dist/pipeline/index.d.mts +1 -1
- package/dist/pipeline/index.mjs +2 -2
- package/dist/{plan-C0gkeABY.mjs → plan-BnlNJhrj.mjs} +1 -1
- package/dist/{plugin-BJcVOX5r.mjs → plugin-CrMPYbdj.mjs} +94 -50
- package/dist/plugins/index.d.mts +6 -5
- package/dist/plugins/index.mjs +19 -18
- package/dist/plugins/response-cache.mjs +4 -4
- package/dist/plugins/tracing-entry.mjs +2 -2
- package/dist/{pluralize-CrCpyoZs.mjs → pluralize-oVIxJ4CY.mjs} +1 -1
- package/dist/presets/filesUpload.d.mts +1 -1
- package/dist/presets/filesUpload.mjs +5 -5
- package/dist/presets/index.d.mts +2 -2
- package/dist/presets/index.mjs +2 -2
- package/dist/presets/multiTenant.d.mts +1 -1
- package/dist/presets/multiTenant.mjs +5 -5
- package/dist/presets/search.d.mts +1 -1
- package/dist/presets/search.mjs +2 -2
- package/dist/{presets-CNDvNYsz.mjs → presets-CUd3L7v6.mjs} +4 -4
- package/dist/{projection-CIUe8b59.mjs → projection-DowJlIto.mjs} +2 -2
- package/dist/{purger.types-f83fTzGy.mjs → purger.types-BjKy8869.mjs} +1 -1
- package/dist/{queryCachePlugin-B2jpwFcv.mjs → queryCachePlugin-BvB3k_Fh.mjs} +114 -14
- package/dist/{queryCachePlugin-DYQUyeJ9.d.mts → queryCachePlugin-DrQNnGws.d.mts} +1 -1
- package/dist/{rateLimitKey-BWx-qep0.mjs → rateLimitKey-56nH1cWN.mjs} +1 -1
- package/dist/{realtime-D2tqi3d2.mjs → realtime-DXGBPtYK.mjs} +19 -22
- package/dist/registry/index.d.mts +2 -2
- package/dist/registry/index.mjs +3 -3
- package/dist/{registry-Dl0k6UXy.mjs → registry-Biz-6sEZ.mjs} +2 -2
- package/dist/{relay-ZYvS4xyW.mjs → relay-DEIoMntZ.mjs} +200 -99
- package/dist/{renewingLease-DT4yLb29.mjs → renewingLease-BCJpW9-u.mjs} +1 -1
- package/dist/{replyHelpers-BJFN5AW7.mjs → replyHelpers-DliJNb0-.mjs} +2 -2
- package/dist/{requestContext-Ct5la-SA.mjs → requestContext-tUgLt9Ky.mjs} +1 -1
- package/dist/{requirement-CxDxjB6V.mjs → requirement-0l2DApiQ.mjs} +1 -1
- package/dist/{resourceRateLimit-DCWglD7V.d.mts → resourceRateLimit-BPiESPtG.d.mts} +4 -0
- package/dist/{resourceRateLimit-D1oH2obv.mjs → resourceRateLimit-iSXF8rLH.mjs} +19 -4
- package/dist/{rolldown-runtime-BdTN42uL.mjs → rolldown-runtime-gKJR3o-6.mjs} +1 -1
- package/dist/{routeValidator-DclDl5WJ.mjs → routeValidator-xNYA4rr1.mjs} +47 -5
- package/dist/{runtimeCapabilities-ClWr60El.mjs → runtimeCapabilities-BQK-GrYe.mjs} +1 -1
- package/dist/{schedules-gVkRcs-9.mjs → schedules-D9iMVFhF.mjs} +6 -9
- package/dist/{schemaConverter-CIwB5wPO.mjs → schemaConverter-cIh4oX9a.mjs} +2 -2
- package/dist/{actionPermissions-Dha8sAz2.mjs → schemaIR-BsY4y9VP.mjs} +184 -16
- package/dist/schemas/index.mjs +1 -1
- package/dist/scim/index.mjs +2 -2
- package/dist/scope/index.d.mts +1 -1
- package/dist/scope/index.mjs +5 -5
- package/dist/{scope-VMZhdkbT.mjs → scope-QyhM9w8z.mjs} +4 -4
- package/dist/{shutdown-Bi19vH0z.mjs → shutdown-BM7Q0h4B.mjs} +1 -1
- package/dist/{sse-BqM359f1.mjs → sse-ofvlcTQQ.mjs} +9 -35
- package/dist/sseStats-DQqNN385.d.mts +112 -0
- package/dist/sseStream-BxQW7hPq.mjs +166 -0
- package/dist/{store-helpers-rx-F2ZGR.mjs → store-helpers-_FRhHDyv.mjs} +1 -1
- package/dist/{simpleEqualityMatcher-CaCS09fh.mjs → streamFrames-B3ZJfI02.mjs} +19 -2
- package/dist/{streaming-CPChx3A7.mjs → streaming-HZr63UiM.mjs} +1 -1
- package/dist/{subscriberIdentity-CYNYw8p_.mjs → subscriberIdentity-DSgnoT_D.mjs} +1 -1
- package/dist/{tenancy-BoWImS3K.mjs → tenancy-_ukT0oBt.mjs} +2 -2
- package/dist/{tenantPreparation-oqKCZQZM.mjs → tenantPreparation-DSlQI53c.mjs} +4 -4
- package/dist/testing/cleanupStoreContract.mjs +1 -1
- package/dist/testing/index.d.mts +4 -4
- package/dist/testing/index.mjs +324 -17
- package/dist/testing/outboxStoreContract.mjs +2 -2
- package/dist/testing/requiredPorts.mjs +2 -2
- package/dist/testing/storageContract.mjs +1 -1
- package/dist/{timing-Dsteq59c.mjs → timing-Z8p-Zlw1.mjs} +1 -1
- package/dist/{transactionContext-C1OgH-hW.mjs → transactionContext-DBbSTfY3.mjs} +1 -1
- package/dist/{typeGuards-DNlpASzd.mjs → typeGuards-_6nNQqgq.mjs} +1 -1
- package/dist/types/index.d.mts +1 -1
- package/dist/types/index.mjs +1 -1
- package/dist/types/storage.mjs +1 -1
- package/dist/{types-DduBiYaI.d.mts → types-B4E0sc6D.d.mts} +2 -2
- package/dist/{types-HFFSwF_K.mjs → types-CrOlZ4rY.mjs} +4 -4
- package/dist/usage/index.d.mts +1 -1
- package/dist/usage/index.mjs +7 -7
- package/dist/{userHelpers-N-k-8dNF.mjs → userHelpers-CBKx3XJN.mjs} +1 -1
- package/dist/utils/index.d.mts +239 -34
- package/dist/utils/index.mjs +13 -13
- package/dist/{utils-C7w_RVlx.mjs → utils--HE8R8sH.mjs} +761 -8
- package/dist/{validate-Brr4b_fH.mjs → validate-BUeE2MLR.mjs} +3 -3
- package/dist/{versioning-S18y2-0C.d.mts → versioning-B07RgD27.d.mts} +2 -13
- package/dist/{versioning-CE5kdWOY.mjs → versioning-qYgqwOhL.mjs} +4 -7
- package/dist/{workScope-A3XBpYTa.mjs → workScope-C-1QHNmV.mjs} +2 -2
- package/llms.txt +2 -2
- package/package.json +34 -34
- package/skills/arc/references/api-reference.md +2 -2
- package/skills/arc/references/auth-better-auth.md +1 -0
- package/skills/arc/references/events-outbox.md +7 -4
- package/skills/arc/references/events.md +2 -2
- package/skills/arc/references/production-api.md +4 -0
- package/skills/arc/references/production-boot.md +2 -0
- package/skills/arc/references/production-idempotency.md +1 -1
- package/skills/arc/references/production-realtime.md +28 -2
- package/skills/arc/references/validation.md +4 -0
- package/skills/arc/references/writes-hooks-errors.md +1 -0
- package/dist/schemaIR-A0IoD05P.mjs +0 -108
- package/dist/sseStream-qSa-F2Qm.mjs +0 -57
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
/*! @classytic/arc v2.
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
import {
|
|
5
|
-
import { t as
|
|
1
|
+
/*! @classytic/arc v2.49.0 | MIT | https://classytic.com */
|
|
2
|
+
import { t as ArcError, u as ValidationError } from "./errors-BpYV859Y.mjs";
|
|
3
|
+
import { t as runObservingSend } from "./observeSend-DrA3ADFN.mjs";
|
|
4
|
+
import { h as RESERVED_QUERY_PARAMS } from "./constants-DOnfMd7i.mjs";
|
|
5
|
+
import { t as arcLog } from "./logger-UbknHqHy.mjs";
|
|
6
|
+
import { a as registerSseStats } from "./sseStream-BxQW7hPq.mjs";
|
|
7
|
+
import { t as STREAM_FRAMES } from "./streamFrames-B3ZJfI02.mjs";
|
|
6
8
|
import { errorContractSchema as errorContractSchema$1 } from "@classytic/repo-core/errors";
|
|
7
9
|
import { readEnv } from "@classytic/primitives/environment";
|
|
8
10
|
import { OPERATOR_DESCRIPTIONS, URL_OPERATORS, escapeRegex, isQueryGrammarError, readFilterClauses, readPageRequest, readSearch, readSelect, readSort } from "@classytic/repo-core/query-parser";
|
|
11
|
+
import { createCipheriv, createDecipheriv, hkdfSync, randomBytes } from "node:crypto";
|
|
9
12
|
// src/utils/filter-dialect.ts
|
|
10
13
|
/**
|
|
11
14
|
* Filter dialects — what `ArcQueryParser` EMITS. Reading a URL (keys, operators, coercion,
|
|
@@ -138,7 +141,7 @@ const NEUTRAL_DIALECT = {
|
|
|
138
141
|
* emits filters through a {@link FilterDialect}. Swap in any `QueryParserInterface` via
|
|
139
142
|
* `defineResource({ queryParser })`.
|
|
140
143
|
*/
|
|
141
|
-
const log = arcLog("queryParser");
|
|
144
|
+
const log$1 = arcLog("queryParser");
|
|
142
145
|
const FIELD_RE = /^[A-Za-z_][A-Za-z0-9_-]*(?:\.[A-Za-z0-9_-]+)*$/;
|
|
143
146
|
/**
|
|
144
147
|
* Arc's default query parser:
|
|
@@ -229,7 +232,7 @@ var ArcQueryParser = class {
|
|
|
229
232
|
}
|
|
230
233
|
}
|
|
231
234
|
drop(error) {
|
|
232
|
-
log.warn(`query input dropped (ARC_STRICT_QUERY_PARAMS=false): ${error.message}`);
|
|
235
|
+
log$1.warn(`query input dropped (ARC_STRICT_QUERY_PARAMS=false): ${error.message}`);
|
|
233
236
|
}
|
|
234
237
|
/**
|
|
235
238
|
* Simple `?populate=author,category`, or bracket `?populate[author][select]=name,email` and
|
|
@@ -1322,6 +1325,756 @@ function handleRaw(handler, statusCode = 200) {
|
|
|
1322
1325
|
}
|
|
1323
1326
|
};
|
|
1324
1327
|
}
|
|
1328
|
+
// src/utils/replayStream.ts
|
|
1329
|
+
/**
|
|
1330
|
+
* Resumable SSE over a change log — the replay port for custom `openSseStream` routes.
|
|
1331
|
+
*
|
|
1332
|
+
* A live feed fed straight from the event bus loses whatever was published while
|
|
1333
|
+
* a client was reconnecting, and has no answer for "I was away for an hour". This
|
|
1334
|
+
* hub feeds frames from a repo-core `ChangeLogStore` instead: every frame's SSE
|
|
1335
|
+
* `id:` is the log cursor, a reconnect sends it back as `Last-Event-ID`, and the
|
|
1336
|
+
* hub replays the gap from the log or says, in one frame, that it cannot.
|
|
1337
|
+
*
|
|
1338
|
+
* ## Wire contract (shared with `@classytic/arc-next/sse`)
|
|
1339
|
+
*
|
|
1340
|
+
* Control frames carry no `id:` and never reach a client's data handler:
|
|
1341
|
+
*
|
|
1342
|
+
* | frame | when |
|
|
1343
|
+
* |---|---|
|
|
1344
|
+
* | `ready {cursor, resumed}` | first frame on every connection (see the `revoked` exception) |
|
|
1345
|
+
* | `resync {cursor, reason}` | the gap could not be replayed — reload the snapshot, continue from `cursor` |
|
|
1346
|
+
* | `revoked {reason}` | access ended; the stream closes right after. If access ends while the connection is still attaching, this is the only frame |
|
|
1347
|
+
* | `retry {reason}` | not final: the server closes right after; the client reconnects at once (with its cursor) and is authorized afresh |
|
|
1348
|
+
*
|
|
1349
|
+
* Server order on one connection: attach (buffering live entries) → `ready` →
|
|
1350
|
+
* the replayed frames OR exactly one `resync` → the buffer → live. A tailed
|
|
1351
|
+
* entry the replay already sent is never sent again.
|
|
1352
|
+
*
|
|
1353
|
+
* | client sent | server sends |
|
|
1354
|
+
* |---|---|
|
|
1355
|
+
* | no `Last-Event-ID` | `ready{cursor: head, resumed: false}`, no `resync` |
|
|
1356
|
+
* | a cursor it can replay | `ready{cursor: head, resumed: true}`, then the replay |
|
|
1357
|
+
* | a cursor it cannot replay (expired, more than `maxReplay` behind, invalid) | `ready{cursor: head, resumed: false}`, then `resync{cursor: head, reason}` |
|
|
1358
|
+
*
|
|
1359
|
+
* An id-only frame (`id: <cursor>`, no data) follows a batch of entries this
|
|
1360
|
+
* subscriber was not shown (`toFrame` → `null`), so its reconnect cursor keeps up
|
|
1361
|
+
* with the log and a short blip does not turn into `gap_too_large`.
|
|
1362
|
+
*
|
|
1363
|
+
* ## Mechanics
|
|
1364
|
+
*
|
|
1365
|
+
* - Subscribers are grouped into PARTITIONS by `readScope(ctx)`; each partition
|
|
1366
|
+
* tails the log once (`since(cursor, scope)`), however many subscribers it has.
|
|
1367
|
+
* - The tail wakes on {@link ReplayHub.notify} (call it from a bus subscriber) and
|
|
1368
|
+
* every `pollMs`. The bus only wakes it — frames always come from the log, so a
|
|
1369
|
+
* duplicated bus event is harmless.
|
|
1370
|
+
* - Backpressure: a replay or a live burst never outruns the socket. When the
|
|
1371
|
+
* stream reports a full buffer (`needsDrain`, from `openSseStream`), the hub
|
|
1372
|
+
* queues what follows for that subscriber and writes it on `drain`, in order,
|
|
1373
|
+
* so a resume of megabytes stays inside the stream's byte budget. A client
|
|
1374
|
+
* that stops reading is cut by the stream's drain timeout; its queue is capped.
|
|
1375
|
+
* - Cursors stay OPAQUE by default: a replayed entry is recognised later by its
|
|
1376
|
+
* cursor (a set kept per subscriber), never by comparing cursors. With
|
|
1377
|
+
* `compareCursors` the hub skips every tailed entry at or below the last cursor
|
|
1378
|
+
* the client holds, and a cursor ahead of head becomes `cursor_invalid`.
|
|
1379
|
+
* Without it, a client resuming from a cursor AHEAD of this replica's head (it
|
|
1380
|
+
* read a newer entry through another replica a moment ago) may see the entries
|
|
1381
|
+
* in between again — pass `compareCursors` when cursors are ordered.
|
|
1382
|
+
*
|
|
1383
|
+
* Fixed-width cursors (mongokit, `MemoryChangeLogStore`: 16-digit zero-padded):
|
|
1384
|
+
* `isValidCursor: (c) => /^\d{16}$/.test(c)`, `compareCursors: (a, b) => (a < b ? -1 : a > b ? 1 : 0)`.
|
|
1385
|
+
*/
|
|
1386
|
+
const log = arcLog("replay-stream");
|
|
1387
|
+
/** C0 controls and DEL — never valid in an SSE id, and a header-injection shape. */
|
|
1388
|
+
function hasControlChar(value) {
|
|
1389
|
+
for (let i = 0; i < value.length; i++) {
|
|
1390
|
+
const code = value.charCodeAt(i);
|
|
1391
|
+
if (code < 32 || code === 127) return true;
|
|
1392
|
+
}
|
|
1393
|
+
return false;
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* The resume cursor a client sent: the `last-event-id` header first, then the
|
|
1397
|
+
* `queryParam` query parameter (default `'lastEventId'`; native `EventSource`
|
|
1398
|
+
* cannot set headers on a fresh instance). `null` when absent, empty, longer than
|
|
1399
|
+
* 256 chars, or containing control characters. Pass `queryParam: null` to read
|
|
1400
|
+
* the header only.
|
|
1401
|
+
*/
|
|
1402
|
+
function lastEventIdOf(request, opts = {}) {
|
|
1403
|
+
const queryParam = opts.queryParam === void 0 ? "lastEventId" : opts.queryParam;
|
|
1404
|
+
const first = (v) => Array.isArray(v) ? v[0] : v;
|
|
1405
|
+
let value = first(request.headers["last-event-id"]);
|
|
1406
|
+
if ((value === void 0 || value === "") && queryParam) value = first(request.query?.[queryParam]);
|
|
1407
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 256) return null;
|
|
1408
|
+
return hasControlChar(value) ? null : value;
|
|
1409
|
+
}
|
|
1410
|
+
const PAGE_LIMIT = 500;
|
|
1411
|
+
const DEFAULT_MAX_REPLAY = 500;
|
|
1412
|
+
const DEFAULT_POLL_MS = 1e3;
|
|
1413
|
+
const DEFAULT_REVOKE_TYPE = "arc.realtime.revoke";
|
|
1414
|
+
const MIN_REVOKE_SECRET_LENGTH = 16;
|
|
1415
|
+
/** `CursorExpiredError` by NAME — a second copy of repo-core must still match. */
|
|
1416
|
+
function isCursorExpired(err) {
|
|
1417
|
+
return typeof err === "object" && err !== null && err.name === "CursorExpiredError";
|
|
1418
|
+
}
|
|
1419
|
+
/** A {@link ReplayCloseVerdict}: exactly `{ close: 'retry' | 'revoke', reason?: string }`. */
|
|
1420
|
+
function isCloseVerdict(value) {
|
|
1421
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1422
|
+
const v = value;
|
|
1423
|
+
if (v.close !== "retry" && v.close !== "revoke") return false;
|
|
1424
|
+
if (v.reason !== void 0 && typeof v.reason !== "string") return false;
|
|
1425
|
+
return Object.keys(v).every((k) => k === "close" || k === "reason");
|
|
1426
|
+
}
|
|
1427
|
+
/** AES-256-GCM over the revoke message; `iv(12) | tag(16) | ciphertext`, base64. */
|
|
1428
|
+
function revokeSealer(secret) {
|
|
1429
|
+
const key = Buffer.from(hkdfSync("sha256", secret, "", "arc.realtime.revoke/v1", 32));
|
|
1430
|
+
return {
|
|
1431
|
+
seal(msg) {
|
|
1432
|
+
const iv = randomBytes(12);
|
|
1433
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
1434
|
+
const body = Buffer.concat([cipher.update(JSON.stringify(msg), "utf8"), cipher.final()]);
|
|
1435
|
+
return {
|
|
1436
|
+
v: 1,
|
|
1437
|
+
sealed: Buffer.concat([
|
|
1438
|
+
iv,
|
|
1439
|
+
cipher.getAuthTag(),
|
|
1440
|
+
body
|
|
1441
|
+
]).toString("base64")
|
|
1442
|
+
};
|
|
1443
|
+
},
|
|
1444
|
+
/** `undefined` for anything not sealed under this secret. */
|
|
1445
|
+
open(payload) {
|
|
1446
|
+
const sealed = payload?.sealed;
|
|
1447
|
+
if (typeof sealed !== "string") return void 0;
|
|
1448
|
+
try {
|
|
1449
|
+
const raw = Buffer.from(sealed, "base64");
|
|
1450
|
+
if (raw.length < 29) return void 0;
|
|
1451
|
+
const decipher = createDecipheriv("aes-256-gcm", key, raw.subarray(0, 12));
|
|
1452
|
+
decipher.setAuthTag(raw.subarray(12, 28));
|
|
1453
|
+
const text = Buffer.concat([decipher.update(raw.subarray(28)), decipher.final()]);
|
|
1454
|
+
const msg = JSON.parse(text.toString("utf8"));
|
|
1455
|
+
if (typeof msg.origin !== "string" || typeof msg.keys !== "object" || msg.keys === null || Array.isArray(msg.keys)) return;
|
|
1456
|
+
return {
|
|
1457
|
+
origin: msg.origin,
|
|
1458
|
+
reason: typeof msg.reason === "string" ? msg.reason : "revoked",
|
|
1459
|
+
keys: msg.keys
|
|
1460
|
+
};
|
|
1461
|
+
} catch {
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Create a replay hub. Call at boot, before `ready`: it registers the
|
|
1469
|
+
* `fastify.sseStats()` provider, the cross-replica revoke subscription (with
|
|
1470
|
+
* `revokeVia`) and an `onClose` hook.
|
|
1471
|
+
*
|
|
1472
|
+
* ```ts
|
|
1473
|
+
* const hub = createReplayHub(app, {
|
|
1474
|
+
* store: alertLog, // a ChangeLogStore
|
|
1475
|
+
* readScope: (ctx) => ({ tenantId: ctx.orgId, scopes: ['alerts'] }),
|
|
1476
|
+
* toFrame: (entry, ctx) => (sees(ctx, entry) ? { event: 'alert', data: forViewer(ctx, entry) } : null),
|
|
1477
|
+
* isValidCursor: (c) => /^\d{16}$/.test(c),
|
|
1478
|
+
* compareCursors: (a, b) => (a < b ? -1 : a > b ? 1 : 0),
|
|
1479
|
+
* revalidate: { everyMs: 15_000, check: (ctx, { memo }) => stillAllowed(ctx, memo) },
|
|
1480
|
+
* identify: (ctx) => ({ userId: ctx.userId, sessionId: ctx.sessionId }),
|
|
1481
|
+
* revokeVia: { secret: process.env.REALTIME_REVOKE_SECRET! },
|
|
1482
|
+
* });
|
|
1483
|
+
* await app.events.subscribe('alert.*', () => hub.notify());
|
|
1484
|
+
*
|
|
1485
|
+
* app.get('/alerts/stream', async (req, reply) => {
|
|
1486
|
+
* const ctx = await authorize(req); // every gate BEFORE the hijack
|
|
1487
|
+
* const stream = openSseStream(req, reply);
|
|
1488
|
+
* await hub.attach(stream, ctx, lastEventIdOf(req));
|
|
1489
|
+
* });
|
|
1490
|
+
* ```
|
|
1491
|
+
*/
|
|
1492
|
+
function createReplayHub(fastify, options) {
|
|
1493
|
+
const { store, toFrame, compareCursors } = options;
|
|
1494
|
+
const maxReplay = options.maxReplay ?? DEFAULT_MAX_REPLAY;
|
|
1495
|
+
const pollMs = options.pollMs ?? DEFAULT_POLL_MS;
|
|
1496
|
+
if (!Number.isInteger(maxReplay) || maxReplay < 0) throw new Error(`createReplayHub: maxReplay must be an integer >= 0, got ${String(maxReplay)}.`);
|
|
1497
|
+
if (!Number.isFinite(pollMs) || pollMs < 0) throw new Error(`createReplayHub: pollMs must be a non-negative number, got ${String(pollMs)}.`);
|
|
1498
|
+
if (options.revalidate && (!Number.isFinite(options.revalidate.everyMs) || options.revalidate.everyMs <= 0)) throw new Error(`createReplayHub: revalidate.everyMs must be a positive number, got ${String(options.revalidate.everyMs)}.`);
|
|
1499
|
+
const maxFailures = options.revalidate?.maxConsecutiveFailures;
|
|
1500
|
+
if (maxFailures !== void 0 && (!Number.isInteger(maxFailures) || maxFailures < 1)) throw new Error(`createReplayHub: revalidate.maxConsecutiveFailures must be an integer >= 1, got ${String(maxFailures)}.`);
|
|
1501
|
+
if (options.revokeVia && (typeof options.revokeVia.secret !== "string" || options.revokeVia.secret.length < MIN_REVOKE_SECRET_LENGTH)) throw new Error(`createReplayHub: revokeVia.secret must be a string of at least ${MIN_REVOKE_SECRET_LENGTH} characters, shared by every replica.`);
|
|
1502
|
+
const timers = options.timers ?? {
|
|
1503
|
+
setInterval: (fn, ms) => setInterval(fn, ms),
|
|
1504
|
+
clearInterval: (h) => clearInterval(h)
|
|
1505
|
+
};
|
|
1506
|
+
const readScope = (ctx) => options.readScope?.(ctx) ?? {};
|
|
1507
|
+
const origin = randomBytes(8).toString("hex");
|
|
1508
|
+
const revokeType = options.revokeVia ? options.revokeVia.type ?? DEFAULT_REVOKE_TYPE : void 0;
|
|
1509
|
+
const sealer = options.revokeVia ? revokeSealer(options.revokeVia.secret) : void 0;
|
|
1510
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
1511
|
+
const partitions = /* @__PURE__ */ new Map();
|
|
1512
|
+
const counters = {
|
|
1513
|
+
replayed: 0,
|
|
1514
|
+
resyncs: 0,
|
|
1515
|
+
revoked: 0,
|
|
1516
|
+
revalidateFailures: 0,
|
|
1517
|
+
retried: 0
|
|
1518
|
+
};
|
|
1519
|
+
let hubClosed = false;
|
|
1520
|
+
let pollTimer;
|
|
1521
|
+
let revalidateTimer;
|
|
1522
|
+
let notifyQueued = false;
|
|
1523
|
+
let revokeUnsub;
|
|
1524
|
+
let warnedNonStringIdentity = false;
|
|
1525
|
+
const bufferCap = Math.max(1e3, maxReplay * 2);
|
|
1526
|
+
const unref = (handle) => {
|
|
1527
|
+
handle?.unref?.();
|
|
1528
|
+
};
|
|
1529
|
+
const writeControl = (sub, event, frame) => {
|
|
1530
|
+
if (!sub.closed) sub.stream.write(event, JSON.stringify(frame));
|
|
1531
|
+
};
|
|
1532
|
+
/** Write one entry to a subscriber, or remember its cursor when it is hidden. */
|
|
1533
|
+
const writeEntry = (sub, entry) => {
|
|
1534
|
+
if (sub.closed) return;
|
|
1535
|
+
let frame;
|
|
1536
|
+
try {
|
|
1537
|
+
frame = toFrame(entry, sub.ctx);
|
|
1538
|
+
} catch (error) {
|
|
1539
|
+
log.error(`toFrame threw for cursor '${entry.cursor}'; entry hidden from this subscriber`, error);
|
|
1540
|
+
frame = null;
|
|
1541
|
+
}
|
|
1542
|
+
if (!frame) {
|
|
1543
|
+
sub.unsent = entry.cursor;
|
|
1544
|
+
return;
|
|
1545
|
+
}
|
|
1546
|
+
sub.unsent = null;
|
|
1547
|
+
if (!sub.stream.write(frame.event, JSON.stringify(frame.data), entry.cursor)) detach(sub);
|
|
1548
|
+
};
|
|
1549
|
+
/** Move the client's `lastEventId` past entries it was not shown (id-only frame). */
|
|
1550
|
+
const checkpoint = (sub) => {
|
|
1551
|
+
if (sub.closed || sub.buffer || sub.unsent === null) return;
|
|
1552
|
+
const cursor = sub.unsent;
|
|
1553
|
+
sub.unsent = null;
|
|
1554
|
+
if (sub.stream.writeId && !sub.stream.writeId(cursor)) detach(sub);
|
|
1555
|
+
};
|
|
1556
|
+
/** `true` when the replay already sent this tailed entry. */
|
|
1557
|
+
const alreadySent = (sub, entry) => {
|
|
1558
|
+
if (sub.floor !== null && compareCursors) {
|
|
1559
|
+
if (compareCursors(entry.cursor, sub.floor) <= 0) return true;
|
|
1560
|
+
sub.floor = null;
|
|
1561
|
+
return false;
|
|
1562
|
+
}
|
|
1563
|
+
if (sub.skip?.delete(entry.cursor)) {
|
|
1564
|
+
if (sub.skip.size === 0) sub.skip = null;
|
|
1565
|
+
return true;
|
|
1566
|
+
}
|
|
1567
|
+
return false;
|
|
1568
|
+
};
|
|
1569
|
+
/** The stream's socket buffer is full (only streams that report it, e.g. `openSseStream`). */
|
|
1570
|
+
const backpressured = (sub) => sub.stream.needsDrain === true && typeof sub.stream.drain === "function";
|
|
1571
|
+
/** Wait for the stream's `drain`; `false` when the subscriber closed first. */
|
|
1572
|
+
const drained = async (sub) => {
|
|
1573
|
+
return await sub.stream.drain?.() !== false && !sub.closed;
|
|
1574
|
+
};
|
|
1575
|
+
/** Write a tailed entry unless the replay already sent it. */
|
|
1576
|
+
const sendLive = (sub, entry) => {
|
|
1577
|
+
if (sub.closed || alreadySent(sub, entry)) return;
|
|
1578
|
+
writeEntry(sub, entry);
|
|
1579
|
+
};
|
|
1580
|
+
/**
|
|
1581
|
+
* Write `sub.buffer` in order, waiting for `drain` whenever the socket is full,
|
|
1582
|
+
* then go live (`buffer = null`) and checkpoint. Entries delivered meanwhile join
|
|
1583
|
+
* the buffer, so order holds. Never rejects.
|
|
1584
|
+
*/
|
|
1585
|
+
const flush = async (sub) => {
|
|
1586
|
+
for (;;) {
|
|
1587
|
+
if (sub.closed) return;
|
|
1588
|
+
if (backpressured(sub) && !await drained(sub)) return;
|
|
1589
|
+
const buffer = sub.buffer;
|
|
1590
|
+
if (buffer === null) return;
|
|
1591
|
+
const entry = buffer.shift();
|
|
1592
|
+
if (entry === void 0) {
|
|
1593
|
+
sub.buffer = null;
|
|
1594
|
+
checkpoint(sub);
|
|
1595
|
+
return;
|
|
1596
|
+
}
|
|
1597
|
+
sendLive(sub, entry);
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
/** Hand a tailed entry to a subscriber that is past its replay. */
|
|
1601
|
+
const deliverLive = (sub, entry) => {
|
|
1602
|
+
sendLive(sub, entry);
|
|
1603
|
+
if (!sub.closed && sub.buffer === null && backpressured(sub)) {
|
|
1604
|
+
sub.buffer = [];
|
|
1605
|
+
flush(sub);
|
|
1606
|
+
}
|
|
1607
|
+
};
|
|
1608
|
+
/** Route a tailed entry: buffered while the subscriber is still replaying. */
|
|
1609
|
+
const deliver = (sub, entry) => {
|
|
1610
|
+
if (sub.closed) return;
|
|
1611
|
+
if (sub.buffer) {
|
|
1612
|
+
sub.buffer.push(entry);
|
|
1613
|
+
if (sub.buffer.length > bufferCap) sub.stream.close();
|
|
1614
|
+
return;
|
|
1615
|
+
}
|
|
1616
|
+
deliverLive(sub, entry);
|
|
1617
|
+
};
|
|
1618
|
+
const stopTimersIfIdle = () => {
|
|
1619
|
+
if (subscribers.size > 0) return;
|
|
1620
|
+
if (pollTimer !== void 0) timers.clearInterval(pollTimer);
|
|
1621
|
+
if (revalidateTimer !== void 0) timers.clearInterval(revalidateTimer);
|
|
1622
|
+
pollTimer = void 0;
|
|
1623
|
+
revalidateTimer = void 0;
|
|
1624
|
+
};
|
|
1625
|
+
/** Stop tailing a partition nobody reads and nobody is joining. */
|
|
1626
|
+
const dropIfIdle = (p) => {
|
|
1627
|
+
if (p.subs.size === 0 && p.pending === 0 && partitions.get(p.key) === p) partitions.delete(p.key);
|
|
1628
|
+
};
|
|
1629
|
+
const detach = (sub) => {
|
|
1630
|
+
if (sub.closed) return;
|
|
1631
|
+
sub.closed = true;
|
|
1632
|
+
sub.buffer = null;
|
|
1633
|
+
sub.skip = null;
|
|
1634
|
+
subscribers.delete(sub);
|
|
1635
|
+
const p = sub.partition;
|
|
1636
|
+
if (p) {
|
|
1637
|
+
p.subs.delete(sub);
|
|
1638
|
+
dropIfIdle(p);
|
|
1639
|
+
}
|
|
1640
|
+
stopTimersIfIdle();
|
|
1641
|
+
};
|
|
1642
|
+
const tailError = (p, error) => {
|
|
1643
|
+
log.warn(`change-log tail failed for partition ${p.key}; retrying on the next wake`, error);
|
|
1644
|
+
};
|
|
1645
|
+
/**
|
|
1646
|
+
* The tail cursor was compacted away under us: every subscriber of the partition
|
|
1647
|
+
* has a gap. Jump to head and tell live subscribers to resync; close the ones
|
|
1648
|
+
* still replaying or waiting for `drain` (they reconnect).
|
|
1649
|
+
*/
|
|
1650
|
+
const recoverExpiredTail = async (p) => {
|
|
1651
|
+
const head = await store.latestCursor({
|
|
1652
|
+
tenantId: p.scope.tenantId,
|
|
1653
|
+
scopes: p.scope.scopes
|
|
1654
|
+
});
|
|
1655
|
+
p.cursor = head;
|
|
1656
|
+
for (const sub of [...p.subs]) {
|
|
1657
|
+
if (sub.buffer) {
|
|
1658
|
+
sub.stream.close();
|
|
1659
|
+
continue;
|
|
1660
|
+
}
|
|
1661
|
+
sub.skip = null;
|
|
1662
|
+
sub.floor = null;
|
|
1663
|
+
sub.unsent = null;
|
|
1664
|
+
counters.resyncs++;
|
|
1665
|
+
writeControl(sub, STREAM_FRAMES.RESYNC, {
|
|
1666
|
+
cursor: head,
|
|
1667
|
+
reason: "cursor_expired"
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
};
|
|
1671
|
+
/** Admit the joiners whose required page has been delivered (`seq` = that page). */
|
|
1672
|
+
const admitJoiners = (p, seq) => {
|
|
1673
|
+
if (p.joiners.length === 0) return;
|
|
1674
|
+
const waiting = [];
|
|
1675
|
+
for (const j of p.joiners) {
|
|
1676
|
+
if (j.minPage > seq) {
|
|
1677
|
+
waiting.push(j);
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
if (!j.sub.closed) {
|
|
1681
|
+
j.sub.partition = p;
|
|
1682
|
+
p.subs.add(j.sub);
|
|
1683
|
+
}
|
|
1684
|
+
j.resolve(p.cursor);
|
|
1685
|
+
}
|
|
1686
|
+
p.joiners = waiting;
|
|
1687
|
+
};
|
|
1688
|
+
const rejectJoiners = (p, error) => {
|
|
1689
|
+
const joiners = p.joiners;
|
|
1690
|
+
p.joiners = [];
|
|
1691
|
+
for (const j of joiners) j.reject(error);
|
|
1692
|
+
};
|
|
1693
|
+
/**
|
|
1694
|
+
* Single-flight tail pull. A call during a pull marks the partition dirty, so the
|
|
1695
|
+
* running loop goes round once more — no wake is lost, no pull overlaps.
|
|
1696
|
+
*
|
|
1697
|
+
* INVARIANT: delivery, `p.cursor` and admission of joiners advance in one
|
|
1698
|
+
* synchronous step, so a subscriber joined at head `h` receives every entry after
|
|
1699
|
+
* `h`. A joiner waits for at most the page in flight plus one fresh page, never
|
|
1700
|
+
* for the dirty loop to go quiet (a hot log would otherwise starve new connections).
|
|
1701
|
+
*/
|
|
1702
|
+
const pull = (p) => {
|
|
1703
|
+
if (p.pulling !== null) {
|
|
1704
|
+
p.dirty = true;
|
|
1705
|
+
return p.pulling;
|
|
1706
|
+
}
|
|
1707
|
+
p.pulling = (async () => {
|
|
1708
|
+
try {
|
|
1709
|
+
do {
|
|
1710
|
+
p.dirty = false;
|
|
1711
|
+
for (;;) {
|
|
1712
|
+
const seq = ++p.pagesStarted;
|
|
1713
|
+
let page;
|
|
1714
|
+
try {
|
|
1715
|
+
page = await store.since(p.cursor, {
|
|
1716
|
+
...p.scope,
|
|
1717
|
+
limit: PAGE_LIMIT
|
|
1718
|
+
});
|
|
1719
|
+
} catch (error) {
|
|
1720
|
+
if (!isCursorExpired(error)) throw error;
|
|
1721
|
+
await recoverExpiredTail(p);
|
|
1722
|
+
admitJoiners(p, Number.POSITIVE_INFINITY);
|
|
1723
|
+
break;
|
|
1724
|
+
}
|
|
1725
|
+
for (const entry of page.changes) for (const sub of [...p.subs]) deliver(sub, entry);
|
|
1726
|
+
p.cursor = page.cursor;
|
|
1727
|
+
for (const sub of [...p.subs]) checkpoint(sub);
|
|
1728
|
+
admitJoiners(p, seq);
|
|
1729
|
+
if (!page.hasMore || page.changes.length === 0) break;
|
|
1730
|
+
}
|
|
1731
|
+
} while (p.dirty && !hubClosed);
|
|
1732
|
+
} catch (error) {
|
|
1733
|
+
rejectJoiners(p, error);
|
|
1734
|
+
throw error;
|
|
1735
|
+
} finally {
|
|
1736
|
+
p.pulling = null;
|
|
1737
|
+
admitJoiners(p, Number.POSITIVE_INFINITY);
|
|
1738
|
+
}
|
|
1739
|
+
})();
|
|
1740
|
+
return p.pulling;
|
|
1741
|
+
};
|
|
1742
|
+
const pullAll = () => {
|
|
1743
|
+
for (const p of partitions.values()) {
|
|
1744
|
+
if (!p.initialized) continue;
|
|
1745
|
+
pull(p).catch((error) => tailError(p, error));
|
|
1746
|
+
}
|
|
1747
|
+
};
|
|
1748
|
+
/** Join `p` at the next page boundary; resolves with the head the subscriber joined at. */
|
|
1749
|
+
const join = (p, sub) => new Promise((resolve, reject) => {
|
|
1750
|
+
p.joiners.push({
|
|
1751
|
+
sub,
|
|
1752
|
+
minPage: p.pagesStarted + 1,
|
|
1753
|
+
resolve,
|
|
1754
|
+
reject
|
|
1755
|
+
});
|
|
1756
|
+
pull(p).catch(() => {});
|
|
1757
|
+
});
|
|
1758
|
+
/** Get or create the partition and count one pending attach on it (synchronous). */
|
|
1759
|
+
const partitionFor = (scope, key) => {
|
|
1760
|
+
let p = partitions.get(key);
|
|
1761
|
+
if (!p) {
|
|
1762
|
+
const created = {
|
|
1763
|
+
key,
|
|
1764
|
+
scope,
|
|
1765
|
+
cursor: "",
|
|
1766
|
+
initialized: false,
|
|
1767
|
+
subs: /* @__PURE__ */ new Set(),
|
|
1768
|
+
pulling: null,
|
|
1769
|
+
dirty: false,
|
|
1770
|
+
ready: Promise.resolve(),
|
|
1771
|
+
pending: 0,
|
|
1772
|
+
pagesStarted: 0,
|
|
1773
|
+
joiners: []
|
|
1774
|
+
};
|
|
1775
|
+
created.ready = store.latestCursor({
|
|
1776
|
+
tenantId: scope.tenantId,
|
|
1777
|
+
scopes: scope.scopes
|
|
1778
|
+
}).then((head) => {
|
|
1779
|
+
created.cursor = head;
|
|
1780
|
+
created.initialized = true;
|
|
1781
|
+
}).catch((error) => {
|
|
1782
|
+
if (partitions.get(key) === created) partitions.delete(key);
|
|
1783
|
+
throw error;
|
|
1784
|
+
});
|
|
1785
|
+
partitions.set(key, created);
|
|
1786
|
+
p = created;
|
|
1787
|
+
}
|
|
1788
|
+
p.pending++;
|
|
1789
|
+
return p;
|
|
1790
|
+
};
|
|
1791
|
+
const ensureTimers = () => {
|
|
1792
|
+
if (pollMs > 0 && pollTimer === void 0) {
|
|
1793
|
+
pollTimer = timers.setInterval(pullAll, pollMs);
|
|
1794
|
+
unref(pollTimer);
|
|
1795
|
+
}
|
|
1796
|
+
if (options.revalidate && revalidateTimer === void 0) {
|
|
1797
|
+
revalidateTimer = timers.setInterval(revalidateTick, options.revalidate.everyMs);
|
|
1798
|
+
unref(revalidateTimer);
|
|
1799
|
+
}
|
|
1800
|
+
};
|
|
1801
|
+
const revokeSub = (sub, reason) => {
|
|
1802
|
+
if (sub.closed) return;
|
|
1803
|
+
counters.revoked++;
|
|
1804
|
+
writeControl(sub, STREAM_FRAMES.REVOKED, { reason });
|
|
1805
|
+
sub.stream.close();
|
|
1806
|
+
detach(sub);
|
|
1807
|
+
};
|
|
1808
|
+
/** Close with a non-final `retry {reason}`: the client reconnects at once. */
|
|
1809
|
+
const retrySub = (sub, reason) => {
|
|
1810
|
+
if (sub.closed) return;
|
|
1811
|
+
counters.retried++;
|
|
1812
|
+
writeControl(sub, STREAM_FRAMES.RETRY, { reason });
|
|
1813
|
+
sub.stream.close();
|
|
1814
|
+
detach(sub);
|
|
1815
|
+
};
|
|
1816
|
+
/** One subscriber's re-check. Never rejects. */
|
|
1817
|
+
const revalidateOne = async (sub, memo) => {
|
|
1818
|
+
const revalidate = options.revalidate;
|
|
1819
|
+
if (!revalidate) return;
|
|
1820
|
+
let next;
|
|
1821
|
+
try {
|
|
1822
|
+
next = await revalidate.check(sub.ctx, { memo });
|
|
1823
|
+
} catch (error) {
|
|
1824
|
+
counters.revalidateFailures++;
|
|
1825
|
+
sub.failures++;
|
|
1826
|
+
const limit = revalidate.maxConsecutiveFailures;
|
|
1827
|
+
if (limit !== void 0 && sub.failures >= limit) {
|
|
1828
|
+
log.warn(`revalidate check threw ${sub.failures} times in a row; closing with retry`, error);
|
|
1829
|
+
retrySub(sub, "revalidate_failed");
|
|
1830
|
+
return;
|
|
1831
|
+
}
|
|
1832
|
+
log.warn("revalidate check threw; stream kept open", error);
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
sub.failures = 0;
|
|
1836
|
+
if (sub.closed) return;
|
|
1837
|
+
if (next === null || next === void 0 || next === false) {
|
|
1838
|
+
revokeSub(sub, "access_ended");
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
if (isCloseVerdict(next)) {
|
|
1842
|
+
if (next.close === "retry") retrySub(sub, next.reason ?? "retry");
|
|
1843
|
+
else revokeSub(sub, next.reason ?? "access_ended");
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
let key;
|
|
1847
|
+
try {
|
|
1848
|
+
key = JSON.stringify(readScope(next));
|
|
1849
|
+
} catch (error) {
|
|
1850
|
+
log.error("readScope threw for a revalidated ctx; closing the stream", error);
|
|
1851
|
+
sub.stream.close();
|
|
1852
|
+
return;
|
|
1853
|
+
}
|
|
1854
|
+
sub.ctx = next;
|
|
1855
|
+
if (key !== sub.scopeKey) sub.stream.close();
|
|
1856
|
+
};
|
|
1857
|
+
function revalidateTick() {
|
|
1858
|
+
if (!options.revalidate) return;
|
|
1859
|
+
const memo = /* @__PURE__ */ new Map();
|
|
1860
|
+
for (const sub of [...subscribers]) {
|
|
1861
|
+
if (sub.closed || sub.revalidating) continue;
|
|
1862
|
+
sub.revalidating = true;
|
|
1863
|
+
revalidateOne(sub, memo).finally(() => {
|
|
1864
|
+
sub.revalidating = false;
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
const revokeLocal = (matches, reason) => {
|
|
1869
|
+
let n = 0;
|
|
1870
|
+
for (const sub of [...subscribers]) {
|
|
1871
|
+
if (sub.closed || !matches(sub)) continue;
|
|
1872
|
+
revokeSub(sub, reason);
|
|
1873
|
+
n++;
|
|
1874
|
+
}
|
|
1875
|
+
return n;
|
|
1876
|
+
};
|
|
1877
|
+
/** `identify(ctx)` with every value as a string (ObjectIds and numbers included). */
|
|
1878
|
+
const identityOf = (sub) => {
|
|
1879
|
+
let raw;
|
|
1880
|
+
try {
|
|
1881
|
+
raw = options.identify?.(sub.ctx);
|
|
1882
|
+
} catch (error) {
|
|
1883
|
+
log.error("identify threw; subscriber not matched for revoke", error);
|
|
1884
|
+
return;
|
|
1885
|
+
}
|
|
1886
|
+
if (!raw) return void 0;
|
|
1887
|
+
const out = {};
|
|
1888
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
1889
|
+
if (v === void 0 || v === null) continue;
|
|
1890
|
+
if (typeof v !== "string" && !warnedNonStringIdentity) {
|
|
1891
|
+
warnedNonStringIdentity = true;
|
|
1892
|
+
log.warn(`identify returned a ${typeof v} for '${k}'; revoke compares String(value)`);
|
|
1893
|
+
}
|
|
1894
|
+
out[k] = String(v);
|
|
1895
|
+
}
|
|
1896
|
+
return out;
|
|
1897
|
+
};
|
|
1898
|
+
const matchesTarget = (target) => {
|
|
1899
|
+
const entries = Object.entries(target).filter(([, v]) => v !== void 0 && v !== null).map(([k, v]) => [k, String(v)]);
|
|
1900
|
+
return (sub) => {
|
|
1901
|
+
if (entries.length === 0) return false;
|
|
1902
|
+
const id = identityOf(sub);
|
|
1903
|
+
if (!id) return false;
|
|
1904
|
+
return entries.every(([k, v]) => id[k] === v);
|
|
1905
|
+
};
|
|
1906
|
+
};
|
|
1907
|
+
const events = () => fastify.events;
|
|
1908
|
+
if (revokeType && sealer) fastify.addHook("onReady", async () => {
|
|
1909
|
+
const bus = events();
|
|
1910
|
+
if (!bus) throw new Error("createReplayHub: `revokeVia` broadcasts over `fastify.events`, but arc's events plugin is not registered.");
|
|
1911
|
+
revokeUnsub = await bus.subscribe(revokeType, (event) => {
|
|
1912
|
+
const msg = sealer.open(event.payload);
|
|
1913
|
+
if (!msg || msg.origin === origin) return;
|
|
1914
|
+
revokeLocal(matchesTarget(msg.keys), msg.reason);
|
|
1915
|
+
});
|
|
1916
|
+
});
|
|
1917
|
+
registerSseStats(fastify, () => ({
|
|
1918
|
+
connections: subscribers.size,
|
|
1919
|
+
keys: 0
|
|
1920
|
+
}));
|
|
1921
|
+
const close = () => {
|
|
1922
|
+
if (hubClosed) return;
|
|
1923
|
+
hubClosed = true;
|
|
1924
|
+
for (const sub of [...subscribers]) {
|
|
1925
|
+
sub.stream.close();
|
|
1926
|
+
detach(sub);
|
|
1927
|
+
}
|
|
1928
|
+
partitions.clear();
|
|
1929
|
+
if (pollTimer !== void 0) timers.clearInterval(pollTimer);
|
|
1930
|
+
if (revalidateTimer !== void 0) timers.clearInterval(revalidateTimer);
|
|
1931
|
+
pollTimer = void 0;
|
|
1932
|
+
revalidateTimer = void 0;
|
|
1933
|
+
revokeUnsub?.();
|
|
1934
|
+
revokeUnsub = void 0;
|
|
1935
|
+
};
|
|
1936
|
+
fastify.addHook("onClose", async () => close());
|
|
1937
|
+
const handleOf = (sub) => ({
|
|
1938
|
+
get ctx() {
|
|
1939
|
+
return sub.ctx;
|
|
1940
|
+
},
|
|
1941
|
+
get closed() {
|
|
1942
|
+
return sub.closed;
|
|
1943
|
+
},
|
|
1944
|
+
close() {
|
|
1945
|
+
if (sub.closed) return;
|
|
1946
|
+
sub.stream.close();
|
|
1947
|
+
detach(sub);
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
/** Read the gap after `from`; `undefined` = more than `maxReplay` entries. */
|
|
1951
|
+
const readGap = async (from, scope) => {
|
|
1952
|
+
const out = [];
|
|
1953
|
+
let cursor = from;
|
|
1954
|
+
for (;;) {
|
|
1955
|
+
const limit = Math.min(PAGE_LIMIT, maxReplay + 1 - out.length);
|
|
1956
|
+
const page = await store.since(cursor, {
|
|
1957
|
+
...scope,
|
|
1958
|
+
limit
|
|
1959
|
+
});
|
|
1960
|
+
out.push(...page.changes);
|
|
1961
|
+
if (out.length > maxReplay) return void 0;
|
|
1962
|
+
if (!page.hasMore || page.changes.length === 0) return out;
|
|
1963
|
+
cursor = page.cursor;
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
const attach = async (stream, ctx, lastEventId) => {
|
|
1967
|
+
const sub = {
|
|
1968
|
+
stream,
|
|
1969
|
+
ctx,
|
|
1970
|
+
scopeKey: void 0,
|
|
1971
|
+
partition: void 0,
|
|
1972
|
+
buffer: [],
|
|
1973
|
+
skip: null,
|
|
1974
|
+
floor: null,
|
|
1975
|
+
unsent: null,
|
|
1976
|
+
closed: false,
|
|
1977
|
+
revalidating: false,
|
|
1978
|
+
failures: 0
|
|
1979
|
+
};
|
|
1980
|
+
const handle = handleOf(sub);
|
|
1981
|
+
if (hubClosed) {
|
|
1982
|
+
sub.closed = true;
|
|
1983
|
+
stream.close();
|
|
1984
|
+
return handle;
|
|
1985
|
+
}
|
|
1986
|
+
stream.onCleanup(() => detach(sub));
|
|
1987
|
+
subscribers.add(sub);
|
|
1988
|
+
ensureTimers();
|
|
1989
|
+
try {
|
|
1990
|
+
const scope = readScope(ctx);
|
|
1991
|
+
sub.scopeKey = JSON.stringify(scope);
|
|
1992
|
+
const p = partitionFor(scope, sub.scopeKey);
|
|
1993
|
+
let head;
|
|
1994
|
+
try {
|
|
1995
|
+
await p.ready;
|
|
1996
|
+
if (!sub.closed) head = await join(p, sub);
|
|
1997
|
+
} finally {
|
|
1998
|
+
p.pending--;
|
|
1999
|
+
if (sub.closed || head === void 0) dropIfIdle(p);
|
|
2000
|
+
}
|
|
2001
|
+
if (sub.closed || head === void 0) return handle;
|
|
2002
|
+
let reason;
|
|
2003
|
+
let replay = [];
|
|
2004
|
+
if (lastEventId !== null) {
|
|
2005
|
+
if (options.isValidCursor && !options.isValidCursor(lastEventId)) reason = "cursor_invalid";
|
|
2006
|
+
else if (compareCursors && compareCursors(lastEventId, head) > 0) reason = "cursor_invalid";
|
|
2007
|
+
else try {
|
|
2008
|
+
const gap = await readGap(lastEventId, scope);
|
|
2009
|
+
if (gap === void 0) reason = "gap_too_large";
|
|
2010
|
+
else replay = gap;
|
|
2011
|
+
} catch (error) {
|
|
2012
|
+
if (!isCursorExpired(error)) throw error;
|
|
2013
|
+
reason = "cursor_expired";
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
if (sub.closed) return handle;
|
|
2017
|
+
const resumed = lastEventId !== null && reason === void 0;
|
|
2018
|
+
writeControl(sub, STREAM_FRAMES.READY, {
|
|
2019
|
+
cursor: head,
|
|
2020
|
+
resumed
|
|
2021
|
+
});
|
|
2022
|
+
if (reason) {
|
|
2023
|
+
counters.resyncs++;
|
|
2024
|
+
writeControl(sub, STREAM_FRAMES.RESYNC, {
|
|
2025
|
+
cursor: head,
|
|
2026
|
+
reason
|
|
2027
|
+
});
|
|
2028
|
+
} else if (lastEventId !== null) {
|
|
2029
|
+
for (const entry of replay) {
|
|
2030
|
+
if (backpressured(sub) && !await drained(sub)) return handle;
|
|
2031
|
+
writeEntry(sub, entry);
|
|
2032
|
+
if (sub.closed) return handle;
|
|
2033
|
+
}
|
|
2034
|
+
counters.replayed += replay.length;
|
|
2035
|
+
if (compareCursors) sub.floor = replay.at(-1)?.cursor ?? lastEventId;
|
|
2036
|
+
else if (replay.length > 0) sub.skip = new Set(replay.map((e) => e.cursor));
|
|
2037
|
+
}
|
|
2038
|
+
await flush(sub);
|
|
2039
|
+
} catch (error) {
|
|
2040
|
+
log.warn("change-log read failed while attaching a stream; closing it for the client to retry", error);
|
|
2041
|
+
stream.close();
|
|
2042
|
+
detach(sub);
|
|
2043
|
+
}
|
|
2044
|
+
return handle;
|
|
2045
|
+
};
|
|
2046
|
+
return {
|
|
2047
|
+
attach,
|
|
2048
|
+
notify() {
|
|
2049
|
+
if (notifyQueued || hubClosed) return;
|
|
2050
|
+
notifyQueued = true;
|
|
2051
|
+
queueMicrotask(() => {
|
|
2052
|
+
notifyQueued = false;
|
|
2053
|
+
pullAll();
|
|
2054
|
+
});
|
|
2055
|
+
},
|
|
2056
|
+
async revoke(target, reason = "revoked") {
|
|
2057
|
+
const n = revokeLocal(matchesTarget(target), reason);
|
|
2058
|
+
const bus = revokeType && sealer ? events() : void 0;
|
|
2059
|
+
if (bus && revokeType && sealer) {
|
|
2060
|
+
const keys = {};
|
|
2061
|
+
for (const [k, v] of Object.entries(target)) if (v !== void 0 && v !== null) keys[k] = String(v);
|
|
2062
|
+
await bus.publish(revokeType, sealer.seal({
|
|
2063
|
+
origin,
|
|
2064
|
+
reason,
|
|
2065
|
+
keys
|
|
2066
|
+
}));
|
|
2067
|
+
}
|
|
2068
|
+
return n;
|
|
2069
|
+
},
|
|
2070
|
+
stats: () => ({
|
|
2071
|
+
subscribers: subscribers.size,
|
|
2072
|
+
partitions: partitions.size,
|
|
2073
|
+
...counters
|
|
2074
|
+
}),
|
|
2075
|
+
close
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
1325
2078
|
// src/utils/stateMachine.ts
|
|
1326
2079
|
/**
|
|
1327
2080
|
* Create a state machine for validating transitions
|
|
@@ -1422,4 +2175,4 @@ function createStateMachine(name, transitions = {}, options = {}) {
|
|
|
1422
2175
|
getAvailableActions
|
|
1423
2176
|
};
|
|
1424
2177
|
}
|
|
1425
|
-
export {
|
|
2178
|
+
export { MONGO_DIALECT as A, listResponse as C, responses as D, queryParams as E, ArcQueryParser as O, keysetListResponse as S, paginationSchema as T, bareListResponse as _, defineGuard as a, getDefaultCrudSchemas as b, withCompensation as c, CircuitBreakerError as d, CircuitBreakerRegistry as f, aggregateListResponse as g, createCircuitBreakerRegistry as h, handleRaw as i, NEUTRAL_DIALECT as j, createQueryParser as k, RedisCircuitBreakerState as l, createCircuitBreaker as m, createReplayHub as n, defineErrorMapper as o, CircuitState as p, lastEventIdOf as r, defineCompensation as s, createStateMachine as t, CircuitBreaker as u, deleteResponse as v, offsetListResponse as w, getListQueryParams as x, errorContractSchema$1 as y };
|