@abelxiaoxing/cadence 1.8.3 → 2.0.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/README.md +324 -111
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/agents/design-explorer.md +14 -26
- package/agents/diagnosis-worker.md +14 -25
- package/agents/implementation-worker.md +16 -35
- package/config/.env.example +2 -5
- package/config/workflow-task.example.json +7 -0
- package/docs/remote-access.md +60 -0
- package/docs/reuse-and-distribution.md +44 -0
- package/docs/service-management.md +108 -0
- package/licenses/bundled-web.txt +6245 -0
- package/licenses/pi-web-ui-MIT.txt +21 -0
- package/licenses/pi-web-ui-reuse.md +22 -0
- package/package.json +94 -21
- package/src/index.ts +22 -1807
- package/src/native-build.ts +6 -0
- package/src/native-client.ts +103 -0
- package/src/native-export.ts +58 -0
- package/src/native-history.ts +359 -0
- package/src/operator-cli.mjs +453 -1937
- package/src/plan-proposal.ts +87 -0
- package/src/sdk-worker.mjs +549 -0
- package/src/service/contracts/active.ts +82 -0
- package/src/service/contracts/budget.ts +148 -0
- package/src/service/contracts/build.ts +67 -0
- package/src/service/contracts/capture.ts +48 -0
- package/src/service/contracts/core.ts +33 -0
- package/src/service/contracts/current.ts +261 -0
- package/src/service/contracts/domain.ts +68 -0
- package/src/service/contracts/history.ts +2245 -0
- package/src/service/contracts/models.ts +139 -0
- package/src/service/coordination.ts +862 -0
- package/src/service/private-files.ts +204 -0
- package/src/service-cli.mjs +5592 -0
- package/src/service-client.ts +747 -0
- package/src/service-contract.ts +245 -0
- package/src/service-host.mjs +22382 -0
- package/src/service-http-client.ts +425 -0
- package/src/service-wire.ts +372 -0
- package/src/task-shell.ts +1045 -0
- package/src/task-store.ts +479 -0
- package/src/task-types.ts +184 -0
- package/src/verification-diagnostics.ts +29 -72
- package/src/worker-capture.mjs +1197 -0
- package/src/worker-public.mjs +1753 -0
- package/src/worker-redaction.mjs +608 -0
- package/src/worker-tools.mjs +202 -0
- package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/web/dist/assets/cadence.css +2 -0
- package/web/dist/assets/cadence.js +7 -0
- package/web/dist/assets/markdown.js +296 -0
- package/web/dist/assets/react.js +9 -0
- package/web/dist/assets/rolldown-runtime.js +1 -0
- package/web/dist/assets/timeline-client.js +41 -0
- package/web/dist/assets/vis-timeline.css +1274 -0
- package/web/dist/assets/vis-timeline.js +32 -0
- package/web/dist/index.html +18 -0
- package/web/dist/manifest.json +505 -0
- package/config/plan-draft.example.json +0 -125
- package/config/plan-draft.multiple-tasks.example.json +0 -375
- package/config/plan-draft.quick.example.json +0 -78
- package/config/routes.example.json +0 -39
- package/prompts/abel-design.md +0 -382
- package/prompts/abel-diagnose.md +0 -71
- package/prompts/abel-implement.md +0 -284
- package/prompts/abel-init.md +0 -52
- package/src/activation.ts +0 -44
- package/src/activity-contracts.ts +0 -82
- package/src/agent-registry.ts +0 -53
- package/src/apply-transaction.ts +0 -1437
- package/src/artifact-store.ts +0 -213
- package/src/build-windows-job.mjs +0 -95
- package/src/candidate-artifact.ts +0 -143
- package/src/candidate-patch.ts +0 -271
- package/src/candidate-workspace.ts +0 -80
- package/src/canonical.ts +0 -16
- package/src/change-contract-schema.ts +0 -191
- package/src/change-contract.ts +0 -324
- package/src/change-verification.ts +0 -1617
- package/src/child-budget.ts +0 -58
- package/src/child-model.ts +0 -142
- package/src/child-session.ts +0 -640
- package/src/contracts.ts +0 -2476
- package/src/control-contracts.ts +0 -382
- package/src/delivery-compiler.ts +0 -1805
- package/src/delivery-revision.ts +0 -42
- package/src/design-control.ts +0 -1299
- package/src/design-diagnostics.ts +0 -181
- package/src/design-journal.ts +0 -1773
- package/src/directory-sync.ts +0 -36
- package/src/durable-contracts.ts +0 -231
- package/src/durable-workflow.ts +0 -978
- package/src/evidence-draft.ts +0 -122
- package/src/execution-profile.ts +0 -64
- package/src/execution-retention.ts +0 -77
- package/src/file-snapshot.ts +0 -163
- package/src/implement-continuation.ts +0 -559
- package/src/implement-graph.ts +0 -690
- package/src/implement-plan.ts +0 -72
- package/src/install-config.d.mts +0 -6
- package/src/isolation-backend.ts +0 -353
- package/src/model-source.ts +0 -22
- package/src/openspec-cli.ts +0 -494
- package/src/operator-cli-entry.mjs +0 -24
- package/src/operator-tools.ts +0 -309
- package/src/package-candidate.ts +0 -227
- package/src/package-delivery.ts +0 -486
- package/src/package-state.ts +0 -320
- package/src/package-verification.ts +0 -635
- package/src/package-workflow.ts +0 -342
- package/src/packet-runtime.ts +0 -570
- package/src/parent-provider.ts +0 -375
- package/src/phase-execution.ts +0 -2259
- package/src/pi-adapter.ts +0 -19
- package/src/plan-draft-summary.ts +0 -527
- package/src/plan-draft.ts +0 -502
- package/src/remove-path.ts +0 -32
- package/src/route-policy.ts +0 -545
- package/src/run-state.ts +0 -420
- package/src/run-store.ts +0 -1179
- package/src/safe-path.ts +0 -77
- package/src/scoped-grep-worker.mjs +0 -23
- package/src/scoped-tools.ts +0 -775
- package/src/single-task-draft.ts +0 -142
- package/src/sqlite-schema.ts +0 -171
- package/src/state-root.ts +0 -215
- package/src/storage-schema.ts +0 -360
- package/src/subagent-activity.ts +0 -1417
- package/src/submit-tool.ts +0 -950
- package/src/task-ledger.ts +0 -1338
- package/src/transport-budget.ts +0 -52
- package/src/verification-capability.ts +0 -1191
- package/src/verification-environment-io.ts +0 -389
- package/src/verification-environment.ts +0 -162
- package/src/verification-identity.ts +0 -77
- package/src/verification-scan.ts +0 -64
- package/src/windows-job-backend.ts +0 -419
- package/src/windows-job.c +0 -149
- package/src/worker-broker.ts +0 -806
- package/src/workflow-engine.ts +0 -28
- package/src/workflow-policy.ts +0 -1679
- package/src/workflow-recovery-policy.ts +0 -97
- package/src/workflow-scheduling.ts +0 -105
- package/src/workflow-state-machine.ts +0 -4355
- package/src/workflow-status.ts +0 -576
- package/src/workspace-io-entry.mjs +0 -76
- package/src/workspace-io-worker.mjs +0 -1312
- package/src/workspace-io.ts +0 -89
- package/src/workspace-store.ts +0 -634
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { closed, oneOf } from "./service/contracts/core.ts";
|
|
2
|
+
/** Portable finite HTTP/WS route owner. Common DTO semantics stay in service-contract. */
|
|
3
|
+
import {
|
|
4
|
+
ContractError,
|
|
5
|
+
canonicalJson,
|
|
6
|
+
decodeActiveRuns,
|
|
7
|
+
decodeCancel,
|
|
8
|
+
decodeCheckObligations,
|
|
9
|
+
decodeChooseModelRequest,
|
|
10
|
+
decodeCommand,
|
|
11
|
+
decodeCurrentChanges,
|
|
12
|
+
decodeCurrentChangesRequest,
|
|
13
|
+
decodeCurrentRequest,
|
|
14
|
+
decodeCurrentView,
|
|
15
|
+
decodeCursor,
|
|
16
|
+
decodeDetailReadResult,
|
|
17
|
+
decodeEntityReadRequest,
|
|
18
|
+
decodeEntityReadResult,
|
|
19
|
+
decodeExportRequest,
|
|
20
|
+
decodeGraphReadRequest,
|
|
21
|
+
decodeGraphReadResult,
|
|
22
|
+
decodeHistoryReadResult,
|
|
23
|
+
decodeId,
|
|
24
|
+
decodeListRequest,
|
|
25
|
+
decodeLiveEvent,
|
|
26
|
+
decodeModelCatalog,
|
|
27
|
+
decodeModelChoiceState,
|
|
28
|
+
decodePreflight,
|
|
29
|
+
decodePreflightRequest,
|
|
30
|
+
decodeReceipt,
|
|
31
|
+
decodeReceiptLookup,
|
|
32
|
+
decodeReply,
|
|
33
|
+
decodeScope,
|
|
34
|
+
decodeSearchPage,
|
|
35
|
+
decodeSessionBootstrap,
|
|
36
|
+
decodeSnapshot,
|
|
37
|
+
decodeWorkspaceBootstrap,
|
|
38
|
+
LIMITS,
|
|
39
|
+
type LiveEvent,
|
|
40
|
+
type Scope,
|
|
41
|
+
type Snapshot,
|
|
42
|
+
utf8Bytes,
|
|
43
|
+
} from "./service-contract.ts";
|
|
44
|
+
|
|
45
|
+
export const HTTP_ROUTES = {
|
|
46
|
+
session: "/api/v1/session",
|
|
47
|
+
current: "/api/v1/current",
|
|
48
|
+
currentChanges: "/api/v1/current-changes",
|
|
49
|
+
activeRuns: "/api/v1/active-runs",
|
|
50
|
+
models: "/api/v1/models",
|
|
51
|
+
chooseModel: "/api/v1/model-choice",
|
|
52
|
+
workspace: "/api/v1/workspace",
|
|
53
|
+
tasks: "/api/v1/tasks",
|
|
54
|
+
runs: "/api/v1/runs",
|
|
55
|
+
messages: "/api/v1/messages",
|
|
56
|
+
tools: "/api/v1/tools",
|
|
57
|
+
contents: "/api/v1/contents",
|
|
58
|
+
events: "/api/v1/history-events",
|
|
59
|
+
checks: "/api/v1/checks",
|
|
60
|
+
changes: "/api/v1/changes",
|
|
61
|
+
reviews: "/api/v1/reviews",
|
|
62
|
+
checkObligations: "/api/v1/check-obligations",
|
|
63
|
+
entity: "/api/v1/entity",
|
|
64
|
+
graph: "/api/v1/graph",
|
|
65
|
+
snapshot: "/api/v1/snapshot",
|
|
66
|
+
detail: "/api/v1/details",
|
|
67
|
+
search: "/api/v1/search",
|
|
68
|
+
export: "/api/v1/export",
|
|
69
|
+
download: "/api/v1/download",
|
|
70
|
+
preflight: "/api/v1/preflight",
|
|
71
|
+
command: "/api/v1/commands",
|
|
72
|
+
cancel: "/api/v1/cancel",
|
|
73
|
+
receipt: "/api/v1/receipt",
|
|
74
|
+
detach: "/api/v1/detach",
|
|
75
|
+
} as const;
|
|
76
|
+
export const WS_PATH = "/api/v1/events";
|
|
77
|
+
export type HttpOperation = keyof typeof HTTP_ROUTES;
|
|
78
|
+
const pageReaders = [
|
|
79
|
+
"tasks",
|
|
80
|
+
"runs",
|
|
81
|
+
"messages",
|
|
82
|
+
"tools",
|
|
83
|
+
"contents",
|
|
84
|
+
"events",
|
|
85
|
+
"checks",
|
|
86
|
+
"changes",
|
|
87
|
+
"reviews",
|
|
88
|
+
] as const;
|
|
89
|
+
/** Encoded HTTP Reply category, including wire whitespace/escaping. Graph data
|
|
90
|
+
* is a control document; only its proof-only branch shares the page ceiling. */
|
|
91
|
+
export function httpOutputLimit(
|
|
92
|
+
operation: HttpOperation,
|
|
93
|
+
reply?: unknown,
|
|
94
|
+
): number {
|
|
95
|
+
if (
|
|
96
|
+
(pageReaders as readonly string[]).includes(operation) ||
|
|
97
|
+
operation === "detail" ||
|
|
98
|
+
operation === "search" ||
|
|
99
|
+
operation === "entity" ||
|
|
100
|
+
operation === "current" ||
|
|
101
|
+
operation === "currentChanges" ||
|
|
102
|
+
operation === "activeRuns" ||
|
|
103
|
+
operation === "workspace" ||
|
|
104
|
+
operation === "models" ||
|
|
105
|
+
operation === "chooseModel"
|
|
106
|
+
)
|
|
107
|
+
return LIMITS.pageBytes;
|
|
108
|
+
if (operation === "graph") {
|
|
109
|
+
const kind = Object.getOwnPropertyDescriptor(reply ?? {}, "kind")?.value,
|
|
110
|
+
value = Object.getOwnPropertyDescriptor(reply ?? {}, "value")?.value;
|
|
111
|
+
if (
|
|
112
|
+
kind === "ok" &&
|
|
113
|
+
Object.getOwnPropertyDescriptor(value ?? {}, "kind")?.value === "progress"
|
|
114
|
+
)
|
|
115
|
+
return LIMITS.pageBytes;
|
|
116
|
+
}
|
|
117
|
+
return LIMITS.controlBytes;
|
|
118
|
+
}
|
|
119
|
+
export function mutation(operation: HttpOperation): boolean {
|
|
120
|
+
return ["preflight", "command", "cancel", "detach", "chooseModel"].includes(
|
|
121
|
+
operation,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
export function decodeHttpInput(operation: HttpOperation, value: unknown) {
|
|
125
|
+
switch (operation) {
|
|
126
|
+
case "session":
|
|
127
|
+
case "models":
|
|
128
|
+
case "activeRuns":
|
|
129
|
+
case "workspace":
|
|
130
|
+
case "detach":
|
|
131
|
+
if (value !== null) throw new ContractError("invalid");
|
|
132
|
+
return null;
|
|
133
|
+
case "tasks":
|
|
134
|
+
case "runs":
|
|
135
|
+
case "messages":
|
|
136
|
+
case "tools":
|
|
137
|
+
case "contents":
|
|
138
|
+
case "events":
|
|
139
|
+
case "checks":
|
|
140
|
+
case "changes":
|
|
141
|
+
case "reviews": {
|
|
142
|
+
const request = decodeListRequest(value);
|
|
143
|
+
if (request.reader !== operation) throw new ContractError("invalid");
|
|
144
|
+
return request;
|
|
145
|
+
}
|
|
146
|
+
case "current":
|
|
147
|
+
return decodeCurrentRequest(value);
|
|
148
|
+
case "currentChanges":
|
|
149
|
+
return decodeCurrentChangesRequest(value);
|
|
150
|
+
case "checkObligations":
|
|
151
|
+
case "receipt":
|
|
152
|
+
return decodeId(value);
|
|
153
|
+
case "entity":
|
|
154
|
+
return decodeEntityReadRequest(value);
|
|
155
|
+
case "graph":
|
|
156
|
+
return decodeGraphReadRequest(value);
|
|
157
|
+
case "snapshot":
|
|
158
|
+
return decodeScope(value);
|
|
159
|
+
case "detail":
|
|
160
|
+
case "search":
|
|
161
|
+
return decodeCursor(value);
|
|
162
|
+
case "export":
|
|
163
|
+
case "download":
|
|
164
|
+
return decodeExportRequest(value);
|
|
165
|
+
case "chooseModel":
|
|
166
|
+
return decodeChooseModelRequest(value);
|
|
167
|
+
case "preflight":
|
|
168
|
+
return decodePreflightRequest(value);
|
|
169
|
+
case "command":
|
|
170
|
+
return decodeCommand(value);
|
|
171
|
+
case "cancel":
|
|
172
|
+
return decodeCancel(value);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export function decodeHttpOutput(
|
|
176
|
+
operation: HttpOperation,
|
|
177
|
+
value: unknown,
|
|
178
|
+
): unknown {
|
|
179
|
+
if ((pageReaders as readonly string[]).includes(operation))
|
|
180
|
+
return decodeReply(value, decodeHistoryReadResult);
|
|
181
|
+
switch (operation) {
|
|
182
|
+
case "current":
|
|
183
|
+
return decodeReply(value, decodeCurrentView);
|
|
184
|
+
case "currentChanges":
|
|
185
|
+
return decodeReply(value, decodeCurrentChanges);
|
|
186
|
+
case "activeRuns":
|
|
187
|
+
return decodeReply(value, decodeActiveRuns);
|
|
188
|
+
case "models":
|
|
189
|
+
return decodeReply(value, decodeModelCatalog);
|
|
190
|
+
case "chooseModel":
|
|
191
|
+
return decodeReply(value, decodeModelChoiceState);
|
|
192
|
+
case "session":
|
|
193
|
+
return decodeReply(value, decodeSessionBootstrap);
|
|
194
|
+
case "workspace":
|
|
195
|
+
return decodeReply(value, decodeWorkspaceBootstrap);
|
|
196
|
+
case "checkObligations":
|
|
197
|
+
return decodeReply(value, decodeCheckObligations);
|
|
198
|
+
case "entity":
|
|
199
|
+
return decodeReply(value, decodeEntityReadResult);
|
|
200
|
+
case "graph":
|
|
201
|
+
return decodeReply(value, decodeGraphReadResult);
|
|
202
|
+
case "snapshot":
|
|
203
|
+
return decodeReply(value, decodeSnapshot);
|
|
204
|
+
case "detail":
|
|
205
|
+
return decodeReply(value, decodeDetailReadResult);
|
|
206
|
+
case "search":
|
|
207
|
+
return decodeReply(value, decodeSearchPage);
|
|
208
|
+
case "preflight":
|
|
209
|
+
return decodeReply(value, decodePreflight);
|
|
210
|
+
case "command":
|
|
211
|
+
case "cancel":
|
|
212
|
+
return decodeReply(value, decodeReceipt);
|
|
213
|
+
case "receipt":
|
|
214
|
+
return decodeReceiptLookup(value);
|
|
215
|
+
case "detach":
|
|
216
|
+
return decodeReply(value, (v) => {
|
|
217
|
+
if (v !== null) throw new ContractError("invalid");
|
|
218
|
+
return null;
|
|
219
|
+
});
|
|
220
|
+
default:
|
|
221
|
+
throw new ContractError("invalid");
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** Body transport and POST security are independent of execution semantics. */
|
|
225
|
+
export function readonlyBody(operation: HttpOperation): boolean {
|
|
226
|
+
const operations: readonly HttpOperation[] = [
|
|
227
|
+
...pageReaders,
|
|
228
|
+
"current",
|
|
229
|
+
"currentChanges",
|
|
230
|
+
"checkObligations",
|
|
231
|
+
"entity",
|
|
232
|
+
"graph",
|
|
233
|
+
"snapshot",
|
|
234
|
+
"detail",
|
|
235
|
+
"search",
|
|
236
|
+
"export",
|
|
237
|
+
"download",
|
|
238
|
+
"receipt",
|
|
239
|
+
];
|
|
240
|
+
return operations.includes(operation);
|
|
241
|
+
}
|
|
242
|
+
function queryUrl(operation: HttpOperation, input: unknown): string {
|
|
243
|
+
const path = HTTP_ROUTES[operation];
|
|
244
|
+
return input === null || mutation(operation)
|
|
245
|
+
? path
|
|
246
|
+
: `${path}?request=${encodeURIComponent(canonicalJson(input))}`;
|
|
247
|
+
}
|
|
248
|
+
export function wireUrl(operation: HttpOperation, value: unknown): string {
|
|
249
|
+
const result = queryUrl(operation, decodeHttpInput(operation, value));
|
|
250
|
+
if (utf8Bytes(result) > LIMITS.headerBytes)
|
|
251
|
+
throw new ContractError("oversize");
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
/** Select once BEFORE sending. Reserve header headroom; never retry a failed GET. */
|
|
255
|
+
export function wireRequest(
|
|
256
|
+
operation: HttpOperation,
|
|
257
|
+
value: unknown,
|
|
258
|
+
): {
|
|
259
|
+
path: string;
|
|
260
|
+
method: "GET" | "POST";
|
|
261
|
+
body?: string;
|
|
262
|
+
} {
|
|
263
|
+
const input = decodeHttpInput(operation, value);
|
|
264
|
+
const hasNavigation =
|
|
265
|
+
input !== null &&
|
|
266
|
+
typeof input === "object" &&
|
|
267
|
+
Object.hasOwn(input, "navigation");
|
|
268
|
+
const path = hasNavigation
|
|
269
|
+
? HTTP_ROUTES[operation]
|
|
270
|
+
: queryUrl(operation, input);
|
|
271
|
+
if (
|
|
272
|
+
mutation(operation) ||
|
|
273
|
+
(readonlyBody(operation) &&
|
|
274
|
+
(hasNavigation || utf8Bytes(path) > LIMITS.headerBytes / 4))
|
|
275
|
+
)
|
|
276
|
+
return {
|
|
277
|
+
path: HTTP_ROUTES[operation],
|
|
278
|
+
method: "POST",
|
|
279
|
+
body: canonicalJson(input),
|
|
280
|
+
};
|
|
281
|
+
return { path, method: "GET" };
|
|
282
|
+
}
|
|
283
|
+
export function assertRequestTarget(target: string): void {
|
|
284
|
+
// Raw request-target is ASCII with strict UTF-8 percent encoding, never a URL
|
|
285
|
+
// parser's normalized authority/path or replacement-character credential.
|
|
286
|
+
if (
|
|
287
|
+
!target.startsWith("/") ||
|
|
288
|
+
target.startsWith("//") ||
|
|
289
|
+
/[\\#\s\u007f-\uffff]/u.test(target) ||
|
|
290
|
+
utf8Bytes(target) > LIMITS.headerBytes
|
|
291
|
+
)
|
|
292
|
+
throw new ContractError("invalid");
|
|
293
|
+
try {
|
|
294
|
+
decodeURIComponent(target);
|
|
295
|
+
} catch {
|
|
296
|
+
throw new ContractError("invalid");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
export function readRoute(
|
|
300
|
+
target: string,
|
|
301
|
+
method: string,
|
|
302
|
+
):
|
|
303
|
+
| { operation: HttpOperation; input: string | null; source: "query" | "body" }
|
|
304
|
+
| undefined {
|
|
305
|
+
assertRequestTarget(target);
|
|
306
|
+
const [path, query] = target.split("?");
|
|
307
|
+
if (target.split("?").length > 2) throw new ContractError("invalid");
|
|
308
|
+
const operation = (Object.keys(HTTP_ROUTES) as HttpOperation[]).find(
|
|
309
|
+
(key) => HTTP_ROUTES[key] === path,
|
|
310
|
+
);
|
|
311
|
+
if (!operation) return undefined;
|
|
312
|
+
const body =
|
|
313
|
+
method === "POST" && (mutation(operation) || readonlyBody(operation));
|
|
314
|
+
if (!body && (method !== "GET" || mutation(operation)))
|
|
315
|
+
throw new ContractError("invalid");
|
|
316
|
+
if (body && query !== undefined) throw new ContractError("invalid");
|
|
317
|
+
const params = new URLSearchParams(query);
|
|
318
|
+
if (
|
|
319
|
+
[...params.keys()].some((key) => key !== "request") ||
|
|
320
|
+
params.getAll("request").length > 1
|
|
321
|
+
)
|
|
322
|
+
throw new ContractError("invalid");
|
|
323
|
+
if (mutation(operation) && query !== undefined)
|
|
324
|
+
throw new ContractError("invalid");
|
|
325
|
+
// Keep encoded JSON opaque until HTTP authentication succeeds.
|
|
326
|
+
return {
|
|
327
|
+
operation,
|
|
328
|
+
input: params.get("request"),
|
|
329
|
+
source: body ? "body" : "query",
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
export type SocketInput =
|
|
333
|
+
| { kind: "subscribe" | "resync"; scope: Scope }
|
|
334
|
+
| { kind: "ping" };
|
|
335
|
+
export type SocketOutput =
|
|
336
|
+
| { kind: "snapshot"; snapshot: Snapshot }
|
|
337
|
+
| { kind: "event"; event: LiveEvent }
|
|
338
|
+
| { kind: "resync" };
|
|
339
|
+
export function decodeSocketInput(value: unknown): SocketInput {
|
|
340
|
+
canonicalJson(value, LIMITS.wsClientFrameBytes);
|
|
341
|
+
const kind = oneOf(Object.getOwnPropertyDescriptor(value, "kind")?.value, [
|
|
342
|
+
"subscribe",
|
|
343
|
+
"resync",
|
|
344
|
+
"ping",
|
|
345
|
+
]);
|
|
346
|
+
if (kind === "ping") {
|
|
347
|
+
closed(value, ["kind"]);
|
|
348
|
+
return { kind };
|
|
349
|
+
}
|
|
350
|
+
const o = closed(value, ["kind", "scope"]);
|
|
351
|
+
return { kind, scope: decodeScope(o.scope) };
|
|
352
|
+
}
|
|
353
|
+
export function decodeSocketOutput(value: unknown): SocketOutput {
|
|
354
|
+
canonicalJson(value, LIMITS.viewerBytes);
|
|
355
|
+
const kind = oneOf(Object.getOwnPropertyDescriptor(value, "kind")?.value, [
|
|
356
|
+
"snapshot",
|
|
357
|
+
"event",
|
|
358
|
+
"resync",
|
|
359
|
+
]);
|
|
360
|
+
if (kind === "snapshot")
|
|
361
|
+
return {
|
|
362
|
+
kind,
|
|
363
|
+
snapshot: decodeSnapshot(closed(value, ["kind", "snapshot"]).snapshot),
|
|
364
|
+
};
|
|
365
|
+
if (kind === "event")
|
|
366
|
+
return {
|
|
367
|
+
kind,
|
|
368
|
+
event: decodeLiveEvent(closed(value, ["kind", "event"]).event),
|
|
369
|
+
};
|
|
370
|
+
closed(value, ["kind"]);
|
|
371
|
+
return { kind };
|
|
372
|
+
}
|