@blamejs/core 0.9.23 → 0.9.24
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/CHANGELOG.md +1 -0
- package/index.js +4 -1
- package/lib/agent-stream.js +243 -0
- package/lib/guard-stream-args.js +166 -0
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,7 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.9.x
|
|
10
10
|
|
|
11
|
+
- v0.9.24 (2026-05-14) — **`b.agent.stream` — async-iterable variants for agent methods that yield N rows.** Fourth slice of the v0.9.21–v0.9.30 substrate playbook (numbers shifted +1 by v0.9.23 CodeQL insertion). JMAP `Email/queryChanges` against million-message mailboxes returns an array today — OOM on client + agent before the response ships. RFC 8620 §5.5 allows `position`-paginated responses; real mailboxes need cursor-backed delivery with built-in backpressure. (1) **`b.agent.stream.create({ openCursor, cursorOpts, batchSize, orchestrator, actor, kind, audit })`** — returns an object implementing `[Symbol.asyncIterator]` so operators write `for await (var row of stream) { ... }`. Operator supplies an `openCursor(cursorOpts)` factory returning a cursor with `fetchBatch(batchSize) → { rows, nextCursor, done }` + optional `close()` + optional `lastSeenCursor()`. (2) **Backpressure built-in** — async-generator semantics; each `next()` call yields one row from an in-memory batch buffer; the cursor only refetches when the buffer drains. Pulling slowly applies backpressure to the store side; a slow client can't OOM the server. (3) **Auto cursor close on every exit path** — consumer `break` calls iterator `.return()`, consumer `throw` calls `.throw()`, natural exhaustion calls `.next()` until done — all three close the cursor via `try`/`finally`-style `_closeOnce` and emit `agent.stream.closed` audit with reason (`exhausted` / `consumer-break` / `consumer-throw` / `drain` / `error`). (4) **Drain marker** — when orchestrator drain fires mid-stream, the next `next()` call returns ONE final `{ _drainMarker: true, lastSeenCursor: <opaque>, reason: "drain" }` row + closes the cursor. Clients reconnecting via JMAP-WebSocket / IMAP NOTIFY pass `lastSeenCursor` back to resume from the same position against the new agent post-deploy. Composes v0.9.21 `orchestrator.registerStream` / `unregisterStream` / `isDraining` hooks. (5) **`b.guardStreamArgs`** — validates `b.agent.stream.create` opts. Refuses non-integer `batchSize` (silent shard-style routing drift class — same shape Codex caught on v0.9.21), batchSize out of `[1, 1024]` strict-profile range, empty `kind` string, function / regex / Buffer / `__proto__` keys inside `cursorOpts` (structured-clone-unsafe — same shape `b.guardMailQuery` refuses for filter specs). Ships `strict` / `balanced` / `permissive` profiles + hipaa / pci-dss / gdpr / soc2 postures. (6) Audit lifecycle: `agent.stream.opened` on iterator creation, `agent.stream.closed` on every exit path, `agent.stream.drain_marker_emitted` when orch drain fires. Fuzz harness ships in `fuzz/guard-stream-args.fuzz.js`. Per the substrate playbook in `memory/specs/blamejs-agent-stream-spec.md`.
|
|
11
12
|
- v0.9.23 (2026-05-14) — **CodeQL alert sweep — 30 alerts closed across 6 rule classes + wiki @primitive validator extracted into static gates.** Standalone substrate slice inserted into the v0.9.21–v0.9.30 playbook; downstream substrate slices shift +1 (stream now v0.9.24, eventBus v0.9.25, tenant v0.9.26, saga v0.9.27, postureChain v0.9.28, trace v0.9.29, snapshot v0.9.30). The 30 alerts were regressions of v0.9.18's earlier batch fix — v0.9.15's framework-wide `require()`-binding rename sweep (184 renames across 108 files) shifted line numbers and the suppression comments stopped tracking. (1) **`js/file-system-race` (10 sites)** — `lib/atomic-file.js:_readSyncCore` migrated from `statSync`+`readFileSync` to the canonical TOCTOU-safe-read scaffold (open fd → `fstatSync` → `readSync` loop → `closeSync` in finally; ENOENT now surfaces from `openSync`); `lib/restore-rollback.js` marker write switched to `openSync(..., "wx", 0o600)` + `writeSync` + `fsyncSync` + EEXIST tolerance; `lib/network-tls.js` new `_readPathFile` fd-based reader; `lib/backup/bundle.js` fd-narrowed read with short-read detection; `lib/static.js` content-safety gate converted to `fsp.open` filehandle pattern; `lib/vault/seal-pem-file.js`, `lib/atomic-file.js:fsyncDir`, `test/30-chain.js`, `examples/wiki/test/validate-{env,cli}-snapshot.js`, `examples/wiki/lib/source-doc-parser.js` got suppression refresh OR fd-narrowed refactor per shape. (2) **`js/insecure-temporary-file` (6 sites)** — `lib/mtls-ca.js` new `_writeExclusive` helper using `openSync(..., "wx", mode)` + `writeSync` + `fsyncSync`; `lib/vault/rotate.js` + `lib/http-client.js` got suppression refresh pointing at the operator-supplied stagingDir / 64-bit CSPRNG suffix defenses. (3) **`js/path-injection` (2 sites in `lib/static.js`)** — suppression refresh pointing at the upstream `_resolveSafe` sandbox check at line 181 (lexical resolve + `startsWith(rootResolved + sep)` + realpath escape guard + guardFilename gate). (4) **`js/remote-property-injection` (7 sites)** — `lib/middleware/csrf-protect.js` cookie-parse output + `lib/websocket.js` `_parseExtensionHeader` params switched to `Object.create(null)` so attacker-controlled keys have no prototype chain to pollute; `lib/middleware/body-parser.js` multipart fields got suppression refresh pointing at the POISONED_KEYS gate at line 867; `test/40-consumers.js` + `test/00-primitives.js` test fixtures suppressed with `test/` scope justification. (5) **`PinnedDependenciesID` (2 workflow files)** — every `uses:` line was already SHA-pinned per v0.9.18; the remaining alerts pointed at the `npm install --no-audit --no-fund` step in `.github/workflows/npm-publish.yml` + `ci.yml`; added explicit `name@version` specifiers (esbuild + postject, mirroring `package.json`'s exact pins) so CodeQL recognizes the install as pinned without committing a lockfile (which CLAUDE.md hard rule §1 forbids — vendored stack, zero npm runtime deps). (6) **`js/regex/missing-regexp-anchor` (1 site)** — `scripts/build-vendored-sbom.js:42` host-extractor regex anchored to `^https?://github.com/` so an attacker-controlled `entry.source` containing `github.com` as a path or query substring can't misdirect purl dispatch into the github branch. (7) **Wiki @primitive validator extracted into static gates** — the `@related namespace-not-primitive` nit fired on PRs #50 (v0.9.20), #51 (v0.9.21), AND #52 (v0.9.22) — same class, three rediscoveries because the validator only ran in CI's wiki-e2e gate (~90s round-trip), never in local pre-push. Extraction: `examples/wiki/lib/source-comment-block-validator.js` (shared engine, pure module, no side effects) + `scripts/validate-source-comment-blocks.js` (framework-level standalone wrapper, runs in 419–466ms, no `@blamejs/core` / vault / DB / network dependencies). Existing wiki-e2e gate refactored to delegate to the shared engine; CI invocation unchanged. CLAUDE.md release workflow step 4 (static gates) now lists `node scripts/validate-source-comment-blocks.js` after `codebase-patterns.test.js`. Each suppression comment references the active defense by file:line so future CodeQL re-runs OR future readers know which suppression applies. Multi-agent fan-out — 4 parallel subagents in isolated git worktrees per rule class.
|
|
12
13
|
- v0.9.22 (2026-05-14) — **`b.agent.idempotency` — cross-dispatch idempotency keys honored at every agent consumer boundary; JMAP retry-safe semantics from day one.** Second slice of the v0.9.21–v0.9.29 substrate playbook. (1) **`b.agent.idempotency.create({ store, audit, ttlMs, maxResultBytes, fingerprintArgs })`** — pluggable backing store via `{ get, put, delete, gc }`; in-memory default ships for single-process deployments, operator wires durable backends (sqlite-backed adapter or external). Surface: `instance.get(method, actorId, key)` returns cached envelope `{ result, firstAt, lastReplayedAt, replayCount, requestFingerprint }` or `null`; `instance.put(method, actorId, key, result, { args, requestFingerprint })` serializes via `b.safeJson.stringify` + persists with TTL + refuses key-reuse-different-args via the request-fingerprint (sha3-512 of args sans key) check; `instance.invalidate(method, actorId, key)` is the saga-compensation escape hatch; `instance.gc({ olderThanMs })` for periodic cleanup. (2) **Keys hashed at the boundary** — operator-supplied keys never reach disk in raw form; namespace-hashed via `b.crypto.namespaceHash("agent.idempotency", method + "\\0" + actorId + "\\0" + key)`. Cross-actor + cross-method isolation by construction (an attacker can't replay another actor's mutation by guessing the key). (3) **Replay tracking** — every cache hit increments `replayCount` + bumps `lastReplayedAt`; audit emits `agent.idempotency.replay` with the truncated actor-id hash + first/replay timestamps so operator pipelines surface retry storms. (4) **`b.guardIdempotencyKey`** — operator-supplied key shape validator. Refuses oversized (default 256 bytes), control chars (C0/NUL/DEL — defends audit-log injection), slash + backslash (defends operators routing keys through filesystem paths), path-traversal (`..`), non-ASCII under strict (operator-greppable in audit logs across stack boundaries; permissive opts down for legacy Unicode tenant IDs). (5) **JSON round-trip test helper** — new `test/helpers/json-round-trip.js` exporting `assertJsonRoundTrip(shape, label)`. Catches the bug class Codex flagged on PR #51 (v0.9.21 `register()` stored agent function ref on backend row, lost in DB/JSON serialization). Refuses on function fields, Buffer fields, Date objects, Symbol keys, BigInt values, non-finite numbers, cycles. Applied retroactively to v0.9.21 agent-orchestrator backend row in regression test. (6) **Result size cap** (default 1 MiB per cached entry) — refuses with `agent-idempotency/result-too-big` so operators discover OOM-prone result shapes locally instead of at runtime. Fuzz harness ships in `fuzz/guard-idempotency-key.fuzz.js`; ClusterFuzzLite matrix entries added. Per the substrate playbook in `memory/specs/blamejs-agent-idempotency-spec.md`.
|
|
13
14
|
- v0.9.21 (2026-05-14) — **`b.agent.orchestrator` — framework-level supervisor for every agent blamejs ships.** First slice of the v0.9.21–v0.9.29 substrate playbook that builds before mail-stack resumes. (1) **`b.agent.orchestrator.create(opts)`** — facade with `register(name, agent, opts)` / `lookup(name)` / `unregister(name)` / `list({ kind, tenantId })` registry, `spawnConsumers({ agent, queue, shards, taskTopic, maxConcurrency })` for sharded-topic dispatch (FNV-1a consistent-hash via `b.agent.orchestrator.shardFor(key, shards)`; per-shard topic suffix `<base>.<shard>`), `elect({ resource })` composing `b.cluster` DB-row leader election (returns `{ isLeader, fencingToken, leaderId }` — single-process deployments get a trivial-leader; cluster deployments delegate), `drain({ timeoutMs })` stopping every spawned consumer + audit-emitting elapsed/count, and `health()` aggregating per-agent + per-consumer + per-election state into one shape ready for `b.middleware.healthcheck`. (2) **Pluggable backend** — `{ get, set, delete, list }` interface; in-memory default ships for single-process deployments, operator wires `b.config.loadDbBacked`-shaped or external for restart-survival. (3) **`b.guardAgentRegistry`** — registry-op shape validator. Refuses non-ASCII agent names (NFC + ASCII-only — operator-greppable in audit logs), path-traversal shapes (`..` / `/` / `\` / NUL / C0 / DEL), oversized (default 64 bytes), reserved `FRAMEWORK.*` / `ROOT.*` prefix, duplicate-on-register, register without `agentKind`. Ships `strict` / `balanced` / `permissive` profiles and `hipaa` / `pci-dss` / `gdpr` / `soc2` postures (all pin `strict`). (4) **Drain phase auto-wires into `b.appShutdown`** when operator supplies an `appShutdown` instance; `SIGTERM` delivers a clean drain of all spawned consumers + stream-registry signal before the process exits. (5) **Stream-registry hook** (`registerStream` / `unregisterStream` / `isDraining`) — substrate for v0.9.23 `b.agent.stream` so async-iterable method variants can check the drain flag and emit drain-markers. Substrate is NOT a process supervisor — process spawn / restart-on-crash / pod scheduling delegate to pm2 / systemd / k8s / Nomad; framework doesn't compete. Fuzz harness ships in `fuzz/guard-agent-registry.fuzz.js`. Per the substrate playbook in `memory/specs/blamejs-agent-orchestrator-spec.md`; v0.9.22 idempotency wires on top next.
|
package/index.js
CHANGED
|
@@ -167,8 +167,10 @@ var guardMailMove = require("./lib/guard-mail-move");
|
|
|
167
167
|
var guardMailSieve = require("./lib/guard-mail-sieve");
|
|
168
168
|
var guardAgentRegistry = require("./lib/guard-agent-registry");
|
|
169
169
|
var guardIdempotencyKey = require("./lib/guard-idempotency-key");
|
|
170
|
+
var guardStreamArgs = require("./lib/guard-stream-args");
|
|
170
171
|
var agentOrchestrator = require("./lib/agent-orchestrator");
|
|
171
172
|
var agentIdempotency = require("./lib/agent-idempotency");
|
|
173
|
+
var agentStream = require("./lib/agent-stream");
|
|
172
174
|
var guardArchive = require("./lib/guard-archive");
|
|
173
175
|
var guardJson = require("./lib/guard-json");
|
|
174
176
|
var guardYaml = require("./lib/guard-yaml");
|
|
@@ -414,7 +416,8 @@ module.exports = {
|
|
|
414
416
|
guardMailSieve: guardMailSieve,
|
|
415
417
|
guardAgentRegistry: guardAgentRegistry,
|
|
416
418
|
guardIdempotencyKey: guardIdempotencyKey,
|
|
417
|
-
|
|
419
|
+
guardStreamArgs: guardStreamArgs,
|
|
420
|
+
agent: { orchestrator: agentOrchestrator, idempotency: agentIdempotency, stream: agentStream },
|
|
418
421
|
guardArchive: guardArchive,
|
|
419
422
|
guardJson: guardJson,
|
|
420
423
|
guardYaml: guardYaml,
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module b.agent.stream
|
|
4
|
+
* @nav Agent
|
|
5
|
+
* @title Agent Stream
|
|
6
|
+
* @order 60
|
|
7
|
+
*
|
|
8
|
+
* @intro
|
|
9
|
+
* Async-iterable variants for agent methods that yield N rows.
|
|
10
|
+
* Operator wraps a cursor-shaped fetcher with `b.agent.stream.create`;
|
|
11
|
+
* the resulting object is `AsyncIterable<row>` — built-in
|
|
12
|
+
* backpressure (each `yield` blocks until the consumer pulls),
|
|
13
|
+
* automatic cursor close via `try`/`finally` on any exit path
|
|
14
|
+
* (consumer break, throw, network drop), and drain-marker emit on
|
|
15
|
+
* orchestrator drain so clients can resume from `lastSeenCursor`
|
|
16
|
+
* against the new agent post-drain.
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* var stream = b.agent.stream.create({
|
|
20
|
+
* orchestrator: orch, // optional — for drain reg
|
|
21
|
+
* actor: { id: "u1" },
|
|
22
|
+
* kind: "search",
|
|
23
|
+
* batchSize: 256,
|
|
24
|
+
* openCursor: function (cursorOpts) {
|
|
25
|
+
* return store.openSearchCursor(cursorOpts); // operator
|
|
26
|
+
* },
|
|
27
|
+
* cursorOpts: { folder: "INBOX", sinceModseq: 0 },
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* for await (var row of stream) {
|
|
31
|
+
* // row delivered as soon as the cursor yields it.
|
|
32
|
+
* // Pulling slowly applies backpressure to the store.
|
|
33
|
+
* if (someCondition) break; // cursor.close() fires automatically
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ## Drain-marker semantic
|
|
38
|
+
*
|
|
39
|
+
* When orchestrator drain fires, in-flight streams emit ONE final
|
|
40
|
+
* `{ _drainMarker: true, lastSeenCursor: <opaque>, reason: "drain" }`
|
|
41
|
+
* row and exit cleanly. Clients reconnecting via JMAP-WebSocket /
|
|
42
|
+
* IMAP NOTIFY pass `lastSeenCursor` back to resume.
|
|
43
|
+
*
|
|
44
|
+
* ## Cursor contract
|
|
45
|
+
*
|
|
46
|
+
* Operator-supplied cursor:
|
|
47
|
+
* `cursor.fetchBatch(batchSize) → { rows, nextCursor, done }`
|
|
48
|
+
* `cursor.close() → void | Promise<void>`
|
|
49
|
+
*
|
|
50
|
+
* The framework's `b.mailStore` will gain `openSearchCursor` /
|
|
51
|
+
* `openFolderCursor` / `openExportCursor` etc. at later mail-stack
|
|
52
|
+
* slices that compose this primitive.
|
|
53
|
+
*
|
|
54
|
+
* @card
|
|
55
|
+
* Async-iterable variants for agent methods that yield N rows.
|
|
56
|
+
* Cursor-backed backpressure; auto-close on exit; drain-marker
|
|
57
|
+
* emit so clients resume cleanly post-deploy.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
var lazyRequire = require("./lazy-require");
|
|
61
|
+
var { defineClass } = require("./framework-error");
|
|
62
|
+
var guardStreamArgs = require("./guard-stream-args");
|
|
63
|
+
|
|
64
|
+
var audit = lazyRequire(function () { return require("./audit"); });
|
|
65
|
+
|
|
66
|
+
var AgentStreamError = defineClass("AgentStreamError", { alwaysPermanent: true });
|
|
67
|
+
|
|
68
|
+
var DEFAULT_BATCH_SIZE = 256; // allow:raw-byte-literal — cursor batch row count, not bytes
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @primitive b.agent.stream.create
|
|
72
|
+
* @signature b.agent.stream.create(opts)
|
|
73
|
+
* @since 0.9.24
|
|
74
|
+
* @status stable
|
|
75
|
+
* @related b.agent.orchestrator.create
|
|
76
|
+
*
|
|
77
|
+
* Create an async-iterable backed by an operator-supplied cursor.
|
|
78
|
+
* Returns an object that implements `[Symbol.asyncIterator]` — usable
|
|
79
|
+
* with `for await (var row of stream)`. Cursor close + audit emit +
|
|
80
|
+
* orchestrator stream-registry hook are owned by the framework;
|
|
81
|
+
* operator only supplies the `openCursor` factory + `cursorOpts`.
|
|
82
|
+
*
|
|
83
|
+
* @opts
|
|
84
|
+
* openCursor: function(cursorOpts) → cursor, // required
|
|
85
|
+
* cursorOpts: object, // operator-passed
|
|
86
|
+
* batchSize: integer, // default 256
|
|
87
|
+
* orchestrator: b.agent.orchestrator, // optional — for drain reg
|
|
88
|
+
* actor: { id, ... }, // optional — audit attribution
|
|
89
|
+
* kind: string, // "search" / "export" / ...
|
|
90
|
+
* audit: b.audit, // optional
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* var stream = b.agent.stream.create({
|
|
94
|
+
* openCursor: function (o) { return store.openSearchCursor(o); },
|
|
95
|
+
* cursorOpts: { folder: "INBOX" },
|
|
96
|
+
* });
|
|
97
|
+
* for await (var row of stream) { process(row); }
|
|
98
|
+
*/
|
|
99
|
+
function create(opts) {
|
|
100
|
+
if (!opts || typeof opts !== "object") {
|
|
101
|
+
throw new AgentStreamError("agent-stream/bad-opts", "create: opts required");
|
|
102
|
+
}
|
|
103
|
+
if (typeof opts.openCursor !== "function") {
|
|
104
|
+
throw new AgentStreamError("agent-stream/bad-open-cursor",
|
|
105
|
+
"create: opts.openCursor must be a function");
|
|
106
|
+
}
|
|
107
|
+
guardStreamArgs.validate({
|
|
108
|
+
batchSize: opts.batchSize,
|
|
109
|
+
kind: opts.kind,
|
|
110
|
+
cursorOpts: opts.cursorOpts,
|
|
111
|
+
});
|
|
112
|
+
var batchSize = typeof opts.batchSize === "number" ? opts.batchSize : DEFAULT_BATCH_SIZE;
|
|
113
|
+
var orch = opts.orchestrator || null;
|
|
114
|
+
var auditImpl = opts.audit || audit();
|
|
115
|
+
var actor = opts.actor || null;
|
|
116
|
+
var kind = opts.kind || "stream";
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
[Symbol.asyncIterator]: function () {
|
|
120
|
+
return _makeIterator({
|
|
121
|
+
openCursor: opts.openCursor,
|
|
122
|
+
cursorOpts: opts.cursorOpts,
|
|
123
|
+
batchSize: batchSize,
|
|
124
|
+
orchestrator: orch,
|
|
125
|
+
audit: auditImpl,
|
|
126
|
+
actor: actor,
|
|
127
|
+
kind: kind,
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function _makeIterator(ctx) {
|
|
134
|
+
var streamId = ctx.orchestrator ? ctx.orchestrator.registerStream({ kind: ctx.kind, actor: ctx.actor }) : null;
|
|
135
|
+
var cursor = null;
|
|
136
|
+
var buffer = [];
|
|
137
|
+
var done = false;
|
|
138
|
+
var closed = false;
|
|
139
|
+
var drained = false;
|
|
140
|
+
_safeAudit(ctx.audit, "agent.stream.opened", ctx.actor, { kind: ctx.kind, streamId: streamId });
|
|
141
|
+
|
|
142
|
+
async function _closeOnce(reason) {
|
|
143
|
+
if (closed) return;
|
|
144
|
+
closed = true;
|
|
145
|
+
if (cursor && typeof cursor.close === "function") {
|
|
146
|
+
try { await cursor.close(); } catch (_e) { /* best-effort */ }
|
|
147
|
+
}
|
|
148
|
+
if (streamId && ctx.orchestrator) {
|
|
149
|
+
try { ctx.orchestrator.unregisterStream(streamId); } catch (_e) { /* best-effort */ }
|
|
150
|
+
}
|
|
151
|
+
_safeAudit(ctx.audit, "agent.stream.closed", ctx.actor, {
|
|
152
|
+
kind: ctx.kind, streamId: streamId, reason: reason || "exhausted",
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
next: async function () {
|
|
158
|
+
try {
|
|
159
|
+
if (buffer.length > 0) {
|
|
160
|
+
var row = buffer.shift();
|
|
161
|
+
return { value: row, done: false };
|
|
162
|
+
}
|
|
163
|
+
if (done) {
|
|
164
|
+
if (!closed) await _closeOnce("exhausted");
|
|
165
|
+
return { value: undefined, done: true };
|
|
166
|
+
}
|
|
167
|
+
// Check orchestrator drain BEFORE fetching the next batch.
|
|
168
|
+
if (ctx.orchestrator && ctx.orchestrator.isDraining && ctx.orchestrator.isDraining()) {
|
|
169
|
+
if (!drained) {
|
|
170
|
+
drained = true;
|
|
171
|
+
var marker = {
|
|
172
|
+
_drainMarker: true,
|
|
173
|
+
lastSeenCursor: cursor && typeof cursor.lastSeenCursor === "function"
|
|
174
|
+
? cursor.lastSeenCursor() : null,
|
|
175
|
+
reason: "drain",
|
|
176
|
+
};
|
|
177
|
+
_safeAudit(ctx.audit, "agent.stream.drain_marker_emitted", ctx.actor, {
|
|
178
|
+
kind: ctx.kind, streamId: streamId,
|
|
179
|
+
});
|
|
180
|
+
done = true;
|
|
181
|
+
return { value: marker, done: false };
|
|
182
|
+
}
|
|
183
|
+
await _closeOnce("drain");
|
|
184
|
+
return { value: undefined, done: true };
|
|
185
|
+
}
|
|
186
|
+
if (!cursor) {
|
|
187
|
+
cursor = await ctx.openCursor(ctx.cursorOpts);
|
|
188
|
+
if (!cursor || typeof cursor.fetchBatch !== "function") {
|
|
189
|
+
throw new AgentStreamError("agent-stream/bad-cursor",
|
|
190
|
+
"openCursor returned non-cursor (missing fetchBatch)");
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
var batch = await cursor.fetchBatch(ctx.batchSize);
|
|
194
|
+
if (!batch || typeof batch !== "object") {
|
|
195
|
+
throw new AgentStreamError("agent-stream/bad-batch",
|
|
196
|
+
"cursor.fetchBatch returned non-object");
|
|
197
|
+
}
|
|
198
|
+
var rows = batch.rows || [];
|
|
199
|
+
if (batch.done) done = true;
|
|
200
|
+
if (rows.length === 0) {
|
|
201
|
+
if (!closed) await _closeOnce("exhausted");
|
|
202
|
+
return { value: undefined, done: true };
|
|
203
|
+
}
|
|
204
|
+
// Push all but the first into the buffer; return the first.
|
|
205
|
+
for (var i = 1; i < rows.length; i += 1) buffer.push(rows[i]);
|
|
206
|
+
return { value: rows[0], done: false };
|
|
207
|
+
} catch (e) {
|
|
208
|
+
// Any error closes the cursor + emits an audit. Re-throw to
|
|
209
|
+
// surface upward.
|
|
210
|
+
await _closeOnce("error");
|
|
211
|
+
throw e;
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
return: async function () {
|
|
215
|
+
// Consumer's `break` calls this — close the cursor cleanly.
|
|
216
|
+
await _closeOnce("consumer-break");
|
|
217
|
+
return { value: undefined, done: true };
|
|
218
|
+
},
|
|
219
|
+
throw: async function (err) {
|
|
220
|
+
await _closeOnce("consumer-throw");
|
|
221
|
+
throw err;
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function _safeAudit(auditImpl, action, actor, metadata) {
|
|
227
|
+
try {
|
|
228
|
+
auditImpl.safeEmit({
|
|
229
|
+
action: action,
|
|
230
|
+
actor: actor ? { id: actor.id, roles: actor.roles || [] } : { id: "<system>" },
|
|
231
|
+
outcome: "success",
|
|
232
|
+
metadata: metadata || {},
|
|
233
|
+
});
|
|
234
|
+
} catch (_e) { /* drop-silent */ }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
module.exports = {
|
|
238
|
+
create: create,
|
|
239
|
+
AgentStreamError: AgentStreamError,
|
|
240
|
+
guards: {
|
|
241
|
+
args: guardStreamArgs,
|
|
242
|
+
},
|
|
243
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module b.guardStreamArgs
|
|
4
|
+
* @nav Guards
|
|
5
|
+
* @title Guard Stream Args
|
|
6
|
+
* @order 437
|
|
7
|
+
*
|
|
8
|
+
* @intro
|
|
9
|
+
* Validates `b.agent.stream.create` opts (and the operator-supplied
|
|
10
|
+
* stream args). Refuses non-positive batch sizes, non-integer batch
|
|
11
|
+
* sizes (silent shard-style routing drift class — same shape Codex
|
|
12
|
+
* caught on v0.9.21), oversized batch sizes (back-pressure becomes
|
|
13
|
+
* meaningless), and structured-clone-unsafe filter shapes (functions
|
|
14
|
+
* / regex / Buffer in the cursor opts — same shape `b.guardMailQuery`
|
|
15
|
+
* refuses).
|
|
16
|
+
*
|
|
17
|
+
* @card
|
|
18
|
+
* Validates `b.agent.stream.create` opts + cursor args. Integer-only
|
|
19
|
+
* batchSize, structured-clone-safe filter shapes, sensible caps.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
var { defineClass } = require("./framework-error");
|
|
23
|
+
|
|
24
|
+
var GuardStreamArgsError = defineClass("GuardStreamArgsError", { alwaysPermanent: true });
|
|
25
|
+
|
|
26
|
+
var DEFAULT_PROFILE = "strict";
|
|
27
|
+
|
|
28
|
+
var PROFILES = Object.freeze({
|
|
29
|
+
strict: { maxBatchSize: 1024, minBatchSize: 1, maxOpenStreams: 4 }, // allow:raw-byte-literal
|
|
30
|
+
balanced: { maxBatchSize: 4096, minBatchSize: 1, maxOpenStreams: 16 }, // allow:raw-byte-literal
|
|
31
|
+
permissive: { maxBatchSize: 16384, minBatchSize: 1, maxOpenStreams: 64 }, // allow:raw-byte-literal
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
var COMPLIANCE_POSTURES = Object.freeze({
|
|
35
|
+
hipaa: "strict",
|
|
36
|
+
"pci-dss": "strict",
|
|
37
|
+
gdpr: "strict",
|
|
38
|
+
soc2: "strict",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @primitive b.guardStreamArgs.validate
|
|
43
|
+
* @signature b.guardStreamArgs.validate(args, opts?)
|
|
44
|
+
* @since 0.9.24
|
|
45
|
+
* @status stable
|
|
46
|
+
* @related b.agent.stream.create
|
|
47
|
+
*
|
|
48
|
+
* Validate `b.agent.stream.create` opts shape. Returns the input on
|
|
49
|
+
* success; throws `GuardStreamArgsError` on refusal.
|
|
50
|
+
*
|
|
51
|
+
* @opts
|
|
52
|
+
* profile: "strict" | "balanced" | "permissive",
|
|
53
|
+
* posture: "hipaa" | "pci-dss" | "gdpr" | "soc2",
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* b.guardStreamArgs.validate({
|
|
57
|
+
* batchSize: 256,
|
|
58
|
+
* kind: "search",
|
|
59
|
+
* });
|
|
60
|
+
*/
|
|
61
|
+
function validate(args, opts) {
|
|
62
|
+
opts = opts || {};
|
|
63
|
+
var profile = PROFILES[_resolveProfile(opts)];
|
|
64
|
+
if (!args || typeof args !== "object") {
|
|
65
|
+
throw new GuardStreamArgsError("stream-args/bad-input",
|
|
66
|
+
"guardStreamArgs.validate: args required");
|
|
67
|
+
}
|
|
68
|
+
if (typeof args.batchSize !== "undefined") {
|
|
69
|
+
if (!Number.isInteger(args.batchSize)) {
|
|
70
|
+
throw new GuardStreamArgsError("stream-args/bad-batch-size",
|
|
71
|
+
"guardStreamArgs.validate: batchSize must be an integer");
|
|
72
|
+
}
|
|
73
|
+
if (args.batchSize < profile.minBatchSize || args.batchSize > profile.maxBatchSize) {
|
|
74
|
+
throw new GuardStreamArgsError("stream-args/batch-size-out-of-range",
|
|
75
|
+
"guardStreamArgs.validate: batchSize " + args.batchSize +
|
|
76
|
+
" not in [" + profile.minBatchSize + ", " + profile.maxBatchSize + "]");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (typeof args.kind !== "undefined") {
|
|
80
|
+
if (typeof args.kind !== "string" || args.kind.length === 0) {
|
|
81
|
+
throw new GuardStreamArgsError("stream-args/bad-kind",
|
|
82
|
+
"guardStreamArgs.validate: kind must be a non-empty string");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Cursor opts can't carry function / regex / Buffer — they must
|
|
86
|
+
// cross the structured-clone boundary into a worker thread.
|
|
87
|
+
if (typeof args.cursorOpts !== "undefined") {
|
|
88
|
+
_checkCursorOpts(args.cursorOpts);
|
|
89
|
+
}
|
|
90
|
+
return args;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @primitive b.guardStreamArgs.compliancePosture
|
|
95
|
+
* @signature b.guardStreamArgs.compliancePosture(posture)
|
|
96
|
+
* @since 0.9.24
|
|
97
|
+
* @status stable
|
|
98
|
+
*
|
|
99
|
+
* Return the effective profile for a given compliance posture name.
|
|
100
|
+
* Returns `null` for unknown posture names so operator typos surface
|
|
101
|
+
* here instead of silently falling through to the default profile.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* b.guardStreamArgs.compliancePosture("hipaa"); // → "strict"
|
|
105
|
+
*/
|
|
106
|
+
function compliancePosture(posture) {
|
|
107
|
+
return COMPLIANCE_POSTURES[posture] || null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function _checkCursorOpts(cursorOpts, depth) {
|
|
111
|
+
depth = depth || 0;
|
|
112
|
+
if (depth > 8) { // allow:raw-byte-literal — recursion depth cap
|
|
113
|
+
throw new GuardStreamArgsError("stream-args/cursor-opts-too-deep",
|
|
114
|
+
"guardStreamArgs.validate: cursorOpts nesting depth exceeds 8");
|
|
115
|
+
}
|
|
116
|
+
// Function check FIRST — `typeof function === "function"` not
|
|
117
|
+
// "object", so a function value would silently skip the non-object
|
|
118
|
+
// early-return below.
|
|
119
|
+
if (typeof cursorOpts === "function") {
|
|
120
|
+
throw new GuardStreamArgsError("stream-args/function-not-allowed",
|
|
121
|
+
"guardStreamArgs.validate: functions refused in cursorOpts (structured-clone-unsafe)");
|
|
122
|
+
}
|
|
123
|
+
if (cursorOpts === null || typeof cursorOpts !== "object") return;
|
|
124
|
+
if (cursorOpts instanceof RegExp) {
|
|
125
|
+
throw new GuardStreamArgsError("stream-args/regex-not-allowed",
|
|
126
|
+
"guardStreamArgs.validate: RegExp refused in cursorOpts");
|
|
127
|
+
}
|
|
128
|
+
if (Buffer.isBuffer(cursorOpts)) {
|
|
129
|
+
throw new GuardStreamArgsError("stream-args/buffer-not-allowed",
|
|
130
|
+
"guardStreamArgs.validate: Buffer refused in cursorOpts");
|
|
131
|
+
}
|
|
132
|
+
if (Array.isArray(cursorOpts)) {
|
|
133
|
+
for (var i = 0; i < cursorOpts.length; i += 1) _checkCursorOpts(cursorOpts[i], depth + 1);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
var keys = Object.keys(cursorOpts);
|
|
137
|
+
for (var k = 0; k < keys.length; k += 1) {
|
|
138
|
+
if (keys[k] === "__proto__" || keys[k] === "constructor" || keys[k] === "prototype") {
|
|
139
|
+
throw new GuardStreamArgsError("stream-args/proto-key",
|
|
140
|
+
"guardStreamArgs.validate: forbidden key '" + keys[k] + "' in cursorOpts");
|
|
141
|
+
}
|
|
142
|
+
_checkCursorOpts(cursorOpts[keys[k]], depth + 1);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function _resolveProfile(opts) {
|
|
147
|
+
if (opts.posture && COMPLIANCE_POSTURES[opts.posture]) {
|
|
148
|
+
return COMPLIANCE_POSTURES[opts.posture];
|
|
149
|
+
}
|
|
150
|
+
var p = opts.profile || DEFAULT_PROFILE;
|
|
151
|
+
if (!PROFILES[p]) {
|
|
152
|
+
throw new GuardStreamArgsError("stream-args/bad-profile",
|
|
153
|
+
"guardStreamArgs: unknown profile '" + p + "'");
|
|
154
|
+
}
|
|
155
|
+
return p;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = {
|
|
159
|
+
validate: validate,
|
|
160
|
+
compliancePosture: compliancePosture,
|
|
161
|
+
PROFILES: PROFILES,
|
|
162
|
+
COMPLIANCE_POSTURES: COMPLIANCE_POSTURES,
|
|
163
|
+
GuardStreamArgsError: GuardStreamArgsError,
|
|
164
|
+
NAME: "streamArgs",
|
|
165
|
+
KIND: "stream-args",
|
|
166
|
+
};
|
package/package.json
CHANGED
package/sbom.cdx.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
|
|
3
3
|
"bomFormat": "CycloneDX",
|
|
4
4
|
"specVersion": "1.6",
|
|
5
|
-
"serialNumber": "urn:uuid:
|
|
5
|
+
"serialNumber": "urn:uuid:49322f04-cea2-480b-ac77-16cc39333700",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2026-05-
|
|
8
|
+
"timestamp": "2026-05-14T23:15:40.993Z",
|
|
9
9
|
"lifecycles": [
|
|
10
10
|
{
|
|
11
11
|
"phase": "build"
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
21
|
"component": {
|
|
22
|
-
"bom-ref": "@blamejs/core@0.9.
|
|
22
|
+
"bom-ref": "@blamejs/core@0.9.24",
|
|
23
23
|
"type": "library",
|
|
24
24
|
"name": "blamejs",
|
|
25
|
-
"version": "0.9.
|
|
25
|
+
"version": "0.9.24",
|
|
26
26
|
"scope": "required",
|
|
27
27
|
"author": "blamejs contributors",
|
|
28
28
|
"description": "The Node framework that owns its stack.",
|
|
29
|
-
"purl": "pkg:npm/%40blamejs/core@0.9.
|
|
29
|
+
"purl": "pkg:npm/%40blamejs/core@0.9.24",
|
|
30
30
|
"properties": [],
|
|
31
31
|
"externalReferences": [
|
|
32
32
|
{
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"components": [],
|
|
55
55
|
"dependencies": [
|
|
56
56
|
{
|
|
57
|
-
"ref": "@blamejs/core@0.9.
|
|
57
|
+
"ref": "@blamejs/core@0.9.24",
|
|
58
58
|
"dependsOn": []
|
|
59
59
|
}
|
|
60
60
|
]
|