@lostgradient/backchannel 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/access-policy-metadata.d.ts +92 -0
- package/dist/access-policy-metadata.js +87 -0
- package/dist/asyncapi-channels.d.ts +46 -0
- package/dist/asyncapi-channels.js +399 -0
- package/dist/asyncapi.d.ts +40 -0
- package/dist/asyncapi.js +165 -0
- package/dist/authentication/api-key.d.ts +4 -0
- package/dist/authentication/api-key.js +114 -0
- package/dist/authentication/audit.d.ts +115 -0
- package/dist/authentication/audit.js +75 -0
- package/dist/authentication/credential-verifiers.d.ts +60 -0
- package/dist/authentication/credential-verifiers.js +102 -0
- package/dist/authentication/crypto.d.ts +7 -0
- package/dist/authentication/crypto.js +140 -0
- package/dist/authentication/index.d.ts +80 -0
- package/dist/authentication/index.js +212 -0
- package/dist/authentication/rate-limiter.d.ts +133 -0
- package/dist/authentication/rate-limiter.js +124 -0
- package/dist/authentication/redaction.d.ts +72 -0
- package/dist/authentication/redaction.js +118 -0
- package/dist/authentication/rotating-api-key-store.d.ts +127 -0
- package/dist/authentication/rotating-api-key-store.js +126 -0
- package/dist/authentication/types.d.ts +337 -0
- package/dist/authentication/types.js +26 -0
- package/dist/authentication.d.ts +1 -0
- package/dist/authentication.js +1 -0
- package/dist/authorization-scope.d.ts +109 -0
- package/dist/authorization-scope.js +144 -0
- package/dist/authorization.d.ts +55 -0
- package/dist/authorization.js +134 -0
- package/dist/bind-feed-lifetime.d.ts +45 -0
- package/dist/bind-feed-lifetime.js +55 -0
- package/dist/byte-arrays.d.ts +5 -0
- package/dist/byte-arrays.js +9 -0
- package/dist/client/catalog-client.d.ts +285 -0
- package/dist/client/catalog-client.js +74 -0
- package/dist/client/in-process-transport.d.ts +19 -0
- package/dist/client/in-process-transport.js +136 -0
- package/dist/client/index.d.ts +14 -0
- package/dist/client/index.js +14 -0
- package/dist/client/json-rpc-http-transport.d.ts +118 -0
- package/dist/client/json-rpc-http-transport.js +127 -0
- package/dist/client/server-sent-event-parser.d.ts +55 -0
- package/dist/client/server-sent-event-parser.js +78 -0
- package/dist/client/sse-subscription-transport.d.ts +72 -0
- package/dist/client/sse-subscription-transport.js +224 -0
- package/dist/client/subscription-client.d.ts +122 -0
- package/dist/client/subscription-client.js +28 -0
- package/dist/connection-lease.d.ts +10 -0
- package/dist/connection-lease.js +8 -0
- package/dist/discovery-filter.d.ts +13 -0
- package/dist/discovery-filter.js +16 -0
- package/dist/discovery-info.d.ts +94 -0
- package/dist/discovery-info.js +38 -0
- package/dist/event-selector.d.ts +7 -0
- package/dist/event-selector.js +7 -0
- package/dist/fault-code.d.ts +84 -0
- package/dist/fault-code.js +114 -0
- package/dist/fault-remediation.d.ts +30 -0
- package/dist/fault-remediation.js +52 -0
- package/dist/fault-to-http.d.ts +11 -0
- package/dist/fault-to-http.js +13 -0
- package/dist/fault-to-json-rpc.d.ts +53 -0
- package/dist/fault-to-json-rpc.js +143 -0
- package/dist/fault-wire-safety.d.ts +45 -0
- package/dist/fault-wire-safety.js +72 -0
- package/dist/generator.d.ts +9 -0
- package/dist/generator.js +9 -0
- package/dist/handler/auth-context-principal.d.ts +37 -0
- package/dist/handler/auth-context-principal.js +67 -0
- package/dist/handler/authentication-responses.d.ts +23 -0
- package/dist/handler/authentication-responses.js +34 -0
- package/dist/handler/json-responses.d.ts +15 -0
- package/dist/handler/json-responses.js +32 -0
- package/dist/handler/operation-handler-authentication.d.ts +64 -0
- package/dist/handler/operation-handler-authentication.js +58 -0
- package/dist/handler/operation-handler-discovery.d.ts +29 -0
- package/dist/handler/operation-handler-discovery.js +55 -0
- package/dist/handler/operation-handler-mounts.d.ts +100 -0
- package/dist/handler/operation-handler-mounts.js +134 -0
- package/dist/handler/operation-handler-subscription.d.ts +34 -0
- package/dist/handler/operation-handler-subscription.js +112 -0
- package/dist/handler/operation-handler.d.ts +162 -0
- package/dist/handler/operation-handler.js +236 -0
- package/dist/handler/workspace-release.d.ts +6 -0
- package/dist/handler/workspace-release.js +75 -0
- package/dist/in-memory-replay-live-backend.d.ts +47 -0
- package/dist/in-memory-replay-live-backend.js +129 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +30 -0
- package/dist/internal/cryptography/encryption.d.ts +108 -0
- package/dist/internal/cryptography/encryption.js +241 -0
- package/dist/internal/cryptography/hash.d.ts +72 -0
- package/dist/internal/cryptography/hash.js +233 -0
- package/dist/internal/cryptography/index.d.ts +4 -0
- package/dist/internal/cryptography/index.js +2 -0
- package/dist/internals.d.ts +58 -0
- package/dist/internals.js +54 -0
- package/dist/json-rpc-dispatch.d.ts +88 -0
- package/dist/json-rpc-dispatch.js +154 -0
- package/dist/json-rpc-framing.d.ts +42 -0
- package/dist/json-rpc-framing.js +50 -0
- package/dist/json-rpc-http.d.ts +36 -0
- package/dist/json-rpc-http.js +191 -0
- package/dist/json-rpc-parse.d.ts +69 -0
- package/dist/json-rpc-parse.js +206 -0
- package/dist/json-rpc-protocol.d.ts +76 -0
- package/dist/json-rpc-protocol.js +54 -0
- package/dist/json-rpc-session-runtime.d.ts +55 -0
- package/dist/json-rpc-session-runtime.js +111 -0
- package/dist/json-rpc-websocket-subscription-pump.d.ts +9 -0
- package/dist/json-rpc-websocket-subscription-pump.js +46 -0
- package/dist/json-rpc-websocket-subscriptions.d.ts +10 -0
- package/dist/json-rpc-websocket-subscriptions.js +33 -0
- package/dist/json-rpc-websocket-types.d.ts +34 -0
- package/dist/json-rpc-websocket-types.js +1 -0
- package/dist/json-rpc-websocket-validation.d.ts +45 -0
- package/dist/json-rpc-websocket-validation.js +154 -0
- package/dist/json-rpc-websocket.d.ts +20 -0
- package/dist/json-rpc-websocket.js +363 -0
- package/dist/json-schema-utilities.d.ts +5 -0
- package/dist/json-schema-utilities.js +17 -0
- package/dist/mcp-discovery-names.d.ts +11 -0
- package/dist/mcp-discovery-names.js +11 -0
- package/dist/openapi-canonical-json.d.ts +10 -0
- package/dist/openapi-canonical-json.js +23 -0
- package/dist/openapi-error-responses.d.ts +15 -0
- package/dist/openapi-error-responses.js +104 -0
- package/dist/openapi-schemas.d.ts +17 -0
- package/dist/openapi-schemas.js +94 -0
- package/dist/openapi.d.ts +60 -0
- package/dist/openapi.js +351 -0
- package/dist/openrpc-document-schema.d.ts +255 -0
- package/dist/openrpc-document-schema.js +84 -0
- package/dist/openrpc-errors.d.ts +93 -0
- package/dist/openrpc-errors.js +134 -0
- package/dist/openrpc.d.ts +66 -0
- package/dist/openrpc.js +260 -0
- package/dist/operation-catalog/behavior-types.d.ts +34 -0
- package/dist/operation-catalog/behavior-types.js +1 -0
- package/dist/operation-catalog/behavior-validation.d.ts +3 -0
- package/dist/operation-catalog/behavior-validation.js +298 -0
- package/dist/operation-catalog/dispatch-allowlist.d.ts +4 -0
- package/dist/operation-catalog/dispatch-allowlist.js +7 -0
- package/dist/operation-catalog/dispatch-preparation.d.ts +24 -0
- package/dist/operation-catalog/dispatch-preparation.js +49 -0
- package/dist/operation-catalog/index.d.ts +19 -0
- package/dist/operation-catalog/index.js +17 -0
- package/dist/operation-catalog/operation-dispatch.d.ts +14 -0
- package/dist/operation-catalog/operation-dispatch.js +115 -0
- package/dist/operation-catalog/operation-interceptors.d.ts +15 -0
- package/dist/operation-catalog/operation-interceptors.js +1 -0
- package/dist/operation-catalog/operation-metadata.d.ts +5 -0
- package/dist/operation-catalog/operation-metadata.js +104 -0
- package/dist/operation-catalog/pipeline-helpers.d.ts +70 -0
- package/dist/operation-catalog/pipeline-helpers.js +242 -0
- package/dist/operation-catalog/pipeline-stages.d.ts +23 -0
- package/dist/operation-catalog/pipeline-stages.js +226 -0
- package/dist/operation-catalog/pipeline.d.ts +7 -0
- package/dist/operation-catalog/pipeline.js +33 -0
- package/dist/operation-catalog/registry.d.ts +17 -0
- package/dist/operation-catalog/registry.js +132 -0
- package/dist/operation-catalog/stream-pipeline.d.ts +37 -0
- package/dist/operation-catalog/stream-pipeline.js +129 -0
- package/dist/operation-catalog/subscription-lifecycle.d.ts +6 -0
- package/dist/operation-catalog/subscription-lifecycle.js +27 -0
- package/dist/operation-catalog/types.d.ts +392 -0
- package/dist/operation-catalog/types.js +40 -0
- package/dist/operation-catalog/universal-faults.d.ts +11 -0
- package/dist/operation-catalog/universal-faults.js +15 -0
- package/dist/operation-catalog-snapshot.d.ts +215 -0
- package/dist/operation-catalog-snapshot.js +128 -0
- package/dist/operation-catalog.d.ts +1 -0
- package/dist/operation-catalog.js +1 -0
- package/dist/operation-description-audit.d.ts +44 -0
- package/dist/operation-description-audit.js +90 -0
- package/dist/operation-fault-helpers.d.ts +21 -0
- package/dist/operation-fault-helpers.js +32 -0
- package/dist/operation-fault.d.ts +245 -0
- package/dist/operation-fault.js +235 -0
- package/dist/operation-registry.d.ts +46 -0
- package/dist/operation-registry.js +46 -0
- package/dist/operations/closable-iterable.d.ts +23 -0
- package/dist/operations/closable-iterable.js +51 -0
- package/dist/operations/get-principal.d.ts +106 -0
- package/dist/operations/get-principal.js +107 -0
- package/dist/operations/sse-stream.d.ts +67 -0
- package/dist/operations/sse-stream.js +264 -0
- package/dist/principal.d.ts +253 -0
- package/dist/principal.js +254 -0
- package/dist/replay-live-feed-internals.d.ts +47 -0
- package/dist/replay-live-feed-internals.js +215 -0
- package/dist/replay-live-feed-types.d.ts +66 -0
- package/dist/replay-live-feed-types.js +7 -0
- package/dist/replay-live-feed.d.ts +49 -0
- package/dist/replay-live-feed.js +126 -0
- package/dist/rest-binding.d.ts +215 -0
- package/dist/rest-binding.js +95 -0
- package/dist/rest-body.d.ts +17 -0
- package/dist/rest-body.js +95 -0
- package/dist/route-model.d.ts +305 -0
- package/dist/route-model.js +320 -0
- package/dist/runtime/cors.d.ts +161 -0
- package/dist/runtime/cors.js +265 -0
- package/dist/scripts/generate-operation-client.d.ts +186 -0
- package/dist/scripts/generate-operation-client.js +561 -0
- package/dist/scripts/operation-behaviors.d.ts +3 -0
- package/dist/scripts/operation-behaviors.js +16 -0
- package/dist/scripts/operation-client-aliases.d.ts +32 -0
- package/dist/scripts/operation-client-aliases.js +255 -0
- package/dist/scripts/operation-client-domains.d.ts +21 -0
- package/dist/scripts/operation-client-domains.js +74 -0
- package/dist/scripts/operation-client-schema.d.ts +46 -0
- package/dist/scripts/operation-client-schema.js +310 -0
- package/dist/scripts/operation-client-svelte.d.ts +3 -0
- package/dist/scripts/operation-client-svelte.js +61 -0
- package/dist/sequence-cursor.d.ts +46 -0
- package/dist/sequence-cursor.js +62 -0
- package/dist/sse-error-payload-schema.d.ts +12 -0
- package/dist/sse-error-payload-schema.js +33 -0
- package/dist/stdio-session.d.ts +71 -0
- package/dist/stdio-session.js +347 -0
- package/dist/zod-json-schema.d.ts +22 -0
- package/dist/zod-json-schema.js +49 -0
- package/package.json +38 -3
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { compareStrings } from "../internals.js";
|
|
2
|
+
import { canonicalKey, renderNode } from "./operation-client-schema.js";
|
|
3
|
+
/**
|
|
4
|
+
* Hoist a repeated object shape into a named alias only when it appears at least
|
|
5
|
+
* twice. Below this it is unique and aliasing would only add indirection.
|
|
6
|
+
*/
|
|
7
|
+
const MINIMUM_OCCURRENCES = 2;
|
|
8
|
+
/**
|
|
9
|
+
* Object shapes with at least this many fields are always worth a named alias
|
|
10
|
+
* once they repeat. Smaller shapes (e.g. a single-field `{ key: string }`) are
|
|
11
|
+
* left inline unless they repeat unusually often (see {@link FREQUENT_OCCURRENCES}).
|
|
12
|
+
*/
|
|
13
|
+
const LARGE_FIELD_COUNT = 3;
|
|
14
|
+
/**
|
|
15
|
+
* The minimum field count and occurrence count under which a small (2-field)
|
|
16
|
+
* shape still earns an alias. Keeps trivial 1-field objects inline while
|
|
17
|
+
* de-duplicating genuinely repeated small records.
|
|
18
|
+
*/
|
|
19
|
+
const SMALL_FIELD_COUNT = 2;
|
|
20
|
+
const FREQUENT_OCCURRENCES = 3;
|
|
21
|
+
/** Number of PascalCased field-name fragments folded into a readable alias hint. */
|
|
22
|
+
const HINT_FIELD_LIMIT = 3;
|
|
23
|
+
/** Maximum length of the readable hint portion of an alias name. */
|
|
24
|
+
const HINT_MAX_LENGTH = 24;
|
|
25
|
+
/** Names referenced from operation roots after alias substitution. */
|
|
26
|
+
export function referencedRootAliases(roots, aliases) {
|
|
27
|
+
const referenced = new Set();
|
|
28
|
+
const visit = (node) => {
|
|
29
|
+
const alias = aliases.get(canonicalKey(node));
|
|
30
|
+
if (alias !== undefined) {
|
|
31
|
+
referenced.add(alias);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (node.kind === 'array')
|
|
35
|
+
visit(node.element);
|
|
36
|
+
else if (node.kind === 'record' && node.value !== undefined)
|
|
37
|
+
visit(node.value);
|
|
38
|
+
else if (node.kind === 'union')
|
|
39
|
+
for (const member of node.members)
|
|
40
|
+
visit(member);
|
|
41
|
+
else if (node.kind === 'object')
|
|
42
|
+
for (const field of node.fields)
|
|
43
|
+
visit(field.value);
|
|
44
|
+
};
|
|
45
|
+
for (const root of roots)
|
|
46
|
+
visit(root);
|
|
47
|
+
return referenced;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Select which structural shapes to hoist into aliases. Prunes to a fixed point
|
|
51
|
+
* so that every surviving alias is referenced at least twice across the emitted
|
|
52
|
+
* alias bodies and operation entries. Returns both the name map and the original
|
|
53
|
+
* nodes so declarations render without round-tripping through the string key.
|
|
54
|
+
*/
|
|
55
|
+
export function selectAliases(roots) {
|
|
56
|
+
const counts = new Map();
|
|
57
|
+
for (const root of roots)
|
|
58
|
+
collectCounts(root, counts);
|
|
59
|
+
let candidates = new Map();
|
|
60
|
+
for (const [key, { count, node }] of counts) {
|
|
61
|
+
if (isHoistWorthy(node, count))
|
|
62
|
+
candidates.set(key, node);
|
|
63
|
+
}
|
|
64
|
+
// Prune aliases referenced fewer than twice once substitution is applied.
|
|
65
|
+
// Dropping a parent can lower a child's reference count, so iterate to a fixed
|
|
66
|
+
// point. References are counted structurally over the node graph (not by text
|
|
67
|
+
// matching), so the count is exact. The loop strictly shrinks `candidates`
|
|
68
|
+
// each iteration until it stabilizes, so it terminates in at most
|
|
69
|
+
// `candidates.size` rounds.
|
|
70
|
+
for (;;) {
|
|
71
|
+
const aliasKeys = new Set(candidates.keys());
|
|
72
|
+
const references = countAliasReferences(aliasKeys, roots);
|
|
73
|
+
const survivors = new Map();
|
|
74
|
+
for (const [key, node] of candidates) {
|
|
75
|
+
if ((references.get(key) ?? 0) >= MINIMUM_OCCURRENCES)
|
|
76
|
+
survivors.set(key, node);
|
|
77
|
+
}
|
|
78
|
+
if (survivors.size === candidates.size)
|
|
79
|
+
break;
|
|
80
|
+
candidates = survivors;
|
|
81
|
+
}
|
|
82
|
+
return { aliasNameByKey: assignAliasNames(candidates), nodeByKey: candidates };
|
|
83
|
+
}
|
|
84
|
+
/** Increment the occurrence count for `node` and recurse into its children. */
|
|
85
|
+
function collectCounts(node, counts) {
|
|
86
|
+
const key = canonicalKey(node);
|
|
87
|
+
const existing = counts.get(key);
|
|
88
|
+
if (existing)
|
|
89
|
+
existing.count += 1;
|
|
90
|
+
else
|
|
91
|
+
counts.set(key, { count: 1, node });
|
|
92
|
+
if (node.kind === 'array')
|
|
93
|
+
collectCounts(node.element, counts);
|
|
94
|
+
else if (node.kind === 'record' && node.value !== undefined)
|
|
95
|
+
collectCounts(node.value, counts);
|
|
96
|
+
else if (node.kind === 'union')
|
|
97
|
+
for (const member of node.members)
|
|
98
|
+
collectCounts(member, counts);
|
|
99
|
+
else if (node.kind === 'object')
|
|
100
|
+
for (const field of node.fields)
|
|
101
|
+
collectCounts(field.value, counts);
|
|
102
|
+
}
|
|
103
|
+
export function isHoistWorthy(node, count) {
|
|
104
|
+
if (node.kind !== 'object')
|
|
105
|
+
return false;
|
|
106
|
+
if (count < MINIMUM_OCCURRENCES)
|
|
107
|
+
return false;
|
|
108
|
+
const fieldCount = node.fields.length;
|
|
109
|
+
if (fieldCount >= LARGE_FIELD_COUNT)
|
|
110
|
+
return true;
|
|
111
|
+
return fieldCount >= SMALL_FIELD_COUNT && count >= FREQUENT_OCCURRENCES;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Assign a stable, readable, content-derived name to each candidate shape.
|
|
115
|
+
* `hashFn` is injectable so the collision guard can be exercised in tests;
|
|
116
|
+
* it defaults to the real FNV-1a hash.
|
|
117
|
+
*/
|
|
118
|
+
export function assignAliasNames(candidates, hashFn = fnv1a) {
|
|
119
|
+
const aliasNameByKey = new Map();
|
|
120
|
+
const keyByName = new Map();
|
|
121
|
+
// Deterministic assignment order so names never depend on Map insertion order.
|
|
122
|
+
const sortedEntries = [...candidates.entries()].toSorted(([left], [right]) => compareStrings(left, right));
|
|
123
|
+
for (const [key, node] of sortedEntries) {
|
|
124
|
+
const name = aliasNameFor(node, hashFn);
|
|
125
|
+
const existing = keyByName.get(name);
|
|
126
|
+
if (existing !== undefined && existing !== key) {
|
|
127
|
+
throw new Error(`alias name collision: ${name} for ${existing} and ${key}`);
|
|
128
|
+
}
|
|
129
|
+
keyByName.set(name, key);
|
|
130
|
+
aliasNameByKey.set(key, name);
|
|
131
|
+
}
|
|
132
|
+
return aliasNameByKey;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Build the alias name `Shared<hint>_<hash>` for an object node: a readable
|
|
136
|
+
* hint drawn from the first sorted field names plus a stable content hash.
|
|
137
|
+
*/
|
|
138
|
+
export function aliasNameFor(node, hashFn = fnv1a) {
|
|
139
|
+
const fields = node.kind === 'object' ? node.fields : [];
|
|
140
|
+
const hint = fields
|
|
141
|
+
.slice(0, HINT_FIELD_LIMIT)
|
|
142
|
+
.map((field) => toPascalCase(field.name))
|
|
143
|
+
.join('')
|
|
144
|
+
.slice(0, HINT_MAX_LENGTH);
|
|
145
|
+
return `Shared${hint}_${hashFn(canonicalKey(node))}`;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Count, per alias key, how many substitution sites would reference it once
|
|
149
|
+
* `aliasKeys` are hoisted. Counts are structural — derived by walking the node
|
|
150
|
+
* graph, never by matching rendered text. A reference is a position where a
|
|
151
|
+
* hoisted key appears: a root that is itself an alias, or a child (array
|
|
152
|
+
* element, object field, union member) whose key is hoisted. An alias node's
|
|
153
|
+
* own body does not count as a self-reference (the body inlines its top level).
|
|
154
|
+
*/
|
|
155
|
+
function countAliasReferences(aliasKeys, roots) {
|
|
156
|
+
const references = new Map();
|
|
157
|
+
for (const key of aliasKeys)
|
|
158
|
+
references.set(key, 0);
|
|
159
|
+
const bump = (key) => references.set(key, (references.get(key) ?? 0) + 1);
|
|
160
|
+
/** Count alias references among the children of `node` (not `node` itself). */
|
|
161
|
+
const countChildren = (node) => {
|
|
162
|
+
if (node.kind === 'array')
|
|
163
|
+
visitChild(node.element);
|
|
164
|
+
else if (node.kind === 'record' && node.value !== undefined)
|
|
165
|
+
visitChild(node.value);
|
|
166
|
+
else if (node.kind === 'union')
|
|
167
|
+
for (const member of node.members)
|
|
168
|
+
visitChild(member);
|
|
169
|
+
else if (node.kind === 'object')
|
|
170
|
+
for (const field of node.fields)
|
|
171
|
+
visitChild(field.value);
|
|
172
|
+
};
|
|
173
|
+
const visitChild = (child) => {
|
|
174
|
+
const key = canonicalKey(child);
|
|
175
|
+
if (aliasKeys.has(key)) {
|
|
176
|
+
// The child collapses to an alias reference; do not descend past it.
|
|
177
|
+
bump(key);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
countChildren(child);
|
|
181
|
+
};
|
|
182
|
+
// Operation entries: a root that is itself an alias is one reference.
|
|
183
|
+
for (const root of roots) {
|
|
184
|
+
const key = canonicalKey(root);
|
|
185
|
+
if (aliasKeys.has(key))
|
|
186
|
+
bump(key);
|
|
187
|
+
else
|
|
188
|
+
countChildren(root);
|
|
189
|
+
}
|
|
190
|
+
// Alias bodies: each hoisted shape's body contributes references to the
|
|
191
|
+
// nested aliases it substitutes (its own top level is inlined, not a self-ref).
|
|
192
|
+
// The two steps below do DIFFERENT work and must both run for an alias node:
|
|
193
|
+
// `countChildren` records this body's direct alias references (stopping at
|
|
194
|
+
// alias boundaries), while the tail recursion keeps descending to discover and
|
|
195
|
+
// count the bodies of aliases nested deeper. Returning early after
|
|
196
|
+
// `countChildren` would miss grandchild aliases and is a bug, not an
|
|
197
|
+
// optimization. `seen` makes the per-body counting idempotent across roots that
|
|
198
|
+
// share a subtree.
|
|
199
|
+
const seen = new Set();
|
|
200
|
+
const collectBodies = (node) => {
|
|
201
|
+
const key = canonicalKey(node);
|
|
202
|
+
if (aliasKeys.has(key) && !seen.has(key)) {
|
|
203
|
+
seen.add(key);
|
|
204
|
+
countChildren(node);
|
|
205
|
+
}
|
|
206
|
+
if (node.kind === 'array')
|
|
207
|
+
collectBodies(node.element);
|
|
208
|
+
else if (node.kind === 'record' && node.value !== undefined)
|
|
209
|
+
collectBodies(node.value);
|
|
210
|
+
else if (node.kind === 'union')
|
|
211
|
+
for (const member of node.members)
|
|
212
|
+
collectBodies(member);
|
|
213
|
+
else if (node.kind === 'object')
|
|
214
|
+
for (const field of node.fields)
|
|
215
|
+
collectBodies(field.value);
|
|
216
|
+
};
|
|
217
|
+
for (const root of roots)
|
|
218
|
+
collectBodies(root);
|
|
219
|
+
return references;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Render the `type Shared… = …;` declarations, sorted by alias name. Each body
|
|
223
|
+
* is rendered from its original {@link TypeNode} with its own key suppressed, so
|
|
224
|
+
* nested aliases substitute but the alias never references itself.
|
|
225
|
+
*/
|
|
226
|
+
export function renderAliasDeclarations(aliasNameByKey, nodeByKey) {
|
|
227
|
+
const entries = [...aliasNameByKey.entries()].toSorted(([, left], [, right]) => compareStrings(left, right));
|
|
228
|
+
return entries.map(([key, name]) => {
|
|
229
|
+
const node = nodeByKey.get(key);
|
|
230
|
+
if (node === undefined)
|
|
231
|
+
throw new Error(`missing node for alias ${name} (${key})`);
|
|
232
|
+
return `type ${name} = ${renderNode(node, aliasNameByKey, key)};`;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function toPascalCase(value) {
|
|
236
|
+
return value
|
|
237
|
+
.split(/[^A-Za-z0-9]+/)
|
|
238
|
+
.filter((segment) => segment.length > 0)
|
|
239
|
+
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
|
|
240
|
+
.join('');
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* 32-bit FNV-1a hash, formatted as 8 lowercase hex characters. Uses `Math.imul`
|
|
244
|
+
* and `>>> 0` normalization to stay within unsigned 32-bit semantics regardless
|
|
245
|
+
* of JavaScript's signed-int behavior.
|
|
246
|
+
*/
|
|
247
|
+
function fnv1a(value) {
|
|
248
|
+
let hash = 0x811c9dc5;
|
|
249
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
250
|
+
hash ^= value.charCodeAt(index);
|
|
251
|
+
hash = Math.imul(hash, 0x01000193);
|
|
252
|
+
hash >>>= 0;
|
|
253
|
+
}
|
|
254
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
255
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CatalogOperationSnapshot } from "../index.js";
|
|
2
|
+
export declare const GENERATED_HEADER = "/* generated by scripts/generate-operation-client.ts; do not edit by hand */";
|
|
3
|
+
export type OperationDomain = {
|
|
4
|
+
name: string;
|
|
5
|
+
fileName: string;
|
|
6
|
+
typeName: string;
|
|
7
|
+
operations: CatalogOperationSnapshot[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Names the domain (a group of operations that share a generated file) an operation belongs to.
|
|
11
|
+
* The default is the operation's first catalog tag, or `Other` when it has none; a catalog with
|
|
12
|
+
* finer groups than its tags supplies its own.
|
|
13
|
+
*/
|
|
14
|
+
export type OperationDomainName = (operation: CatalogOperationSnapshot) => string;
|
|
15
|
+
export declare function operationDomains(operations: readonly CatalogOperationSnapshot[], domainName?: OperationDomainName): OperationDomain[];
|
|
16
|
+
export declare function operationDomainSource(domain: OperationDomain, aliases: ReadonlyMap<string, string>): string;
|
|
17
|
+
/**
|
|
18
|
+
* The file of types for the subscription operations: each name maps to the input the call takes
|
|
19
|
+
* and the event each message of its stream carries, from the operation's `eventSchema`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function subscriptionTypesSource(operations: readonly CatalogOperationSnapshot[], aliases: ReadonlyMap<string, string>): string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { compareStrings } from "../internals.js";
|
|
2
|
+
import { renderRoot, schemaToNode } from "./operation-client-schema.js";
|
|
3
|
+
export const GENERATED_HEADER = '/* generated by scripts/generate-operation-client.ts; do not edit by hand */';
|
|
4
|
+
const tagDomainName = (operation) => operation.tags[0] ?? 'Other';
|
|
5
|
+
export function operationDomains(operations, domainName = tagDomainName) {
|
|
6
|
+
const groups = new Map();
|
|
7
|
+
for (const operation of operations) {
|
|
8
|
+
const name = domainName(operation);
|
|
9
|
+
let group = groups.get(name);
|
|
10
|
+
if (!group) {
|
|
11
|
+
const words = name.split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
12
|
+
if (!words.length)
|
|
13
|
+
throw new Error(`Operation domain requires an alphanumeric name: ${name}`);
|
|
14
|
+
const slug = words.map((word) => word.toLowerCase()).join('-');
|
|
15
|
+
const fileName = `${slug}-operations.generated.ts`;
|
|
16
|
+
const typeName = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join('') +
|
|
17
|
+
'OperationTypes';
|
|
18
|
+
if (!/^[A-Za-z]/.test(typeName)) {
|
|
19
|
+
throw new Error(`Operation domain must start with a letter: ${name}`);
|
|
20
|
+
}
|
|
21
|
+
group = {
|
|
22
|
+
name,
|
|
23
|
+
fileName,
|
|
24
|
+
typeName,
|
|
25
|
+
operations: [],
|
|
26
|
+
};
|
|
27
|
+
if ([...groups.values()].some((existing) => existing.fileName === fileName)) {
|
|
28
|
+
throw new Error(`Operation domains collide at ${fileName}`);
|
|
29
|
+
}
|
|
30
|
+
if ([...groups.values()].some((existing) => existing.typeName === typeName)) {
|
|
31
|
+
throw new Error(`Operation domain types collide at ${typeName}`);
|
|
32
|
+
}
|
|
33
|
+
groups.set(name, group);
|
|
34
|
+
}
|
|
35
|
+
group.operations.push(operation);
|
|
36
|
+
}
|
|
37
|
+
return [...groups.values()].toSorted((left, right) => compareStrings(left.name, right.name));
|
|
38
|
+
}
|
|
39
|
+
export function operationDomainSource(domain, aliases) {
|
|
40
|
+
const entries = domain.operations.map((operation) => operationToTypeEntry(operation, aliases));
|
|
41
|
+
const body = entries.join('\n');
|
|
42
|
+
const usedAliases = [...aliases.values()].filter((name) => new RegExp(`\\b${name}\\b`).test(body));
|
|
43
|
+
const imports = usedAliases.length
|
|
44
|
+
? `import type { ${usedAliases.join(', ')} } from './shared-operation-types.generated.ts';`
|
|
45
|
+
: '';
|
|
46
|
+
return `${GENERATED_HEADER}\n${imports}\nexport type ${domain.typeName} = {\n${body}\n};\n`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The file of types for the subscription operations: each name maps to the input the call takes
|
|
50
|
+
* and the event each message of its stream carries, from the operation's `eventSchema`.
|
|
51
|
+
*/
|
|
52
|
+
export function subscriptionTypesSource(operations, aliases) {
|
|
53
|
+
const body = operations
|
|
54
|
+
.map((operation) => ` '${operation.name}': {
|
|
55
|
+
readonly input: ${renderRoot(schemaToNode(operation.inputSchema), aliases)};
|
|
56
|
+
readonly event: ${renderRoot(schemaToNode(operation.eventSchema ?? {}), aliases)};
|
|
57
|
+
};`)
|
|
58
|
+
.join('\n');
|
|
59
|
+
const used = [...aliases.values()].filter((name) => new RegExp(`\\b${name}\\b`).test(body));
|
|
60
|
+
const imports = used.length
|
|
61
|
+
? `import type { ${used.join(', ')} } from './shared-operation-types.generated.ts';`
|
|
62
|
+
: '';
|
|
63
|
+
return `${GENERATED_HEADER}\n${imports}\nexport type SubscriptionOperationTypes = {\n${body}\n};\n`;
|
|
64
|
+
}
|
|
65
|
+
function operationToTypeEntry(operation, aliases) {
|
|
66
|
+
const faults = operation.producibleFaults.length
|
|
67
|
+
? operation.producibleFaults.map((fault) => `'${fault}'`).join(' | ')
|
|
68
|
+
: 'never';
|
|
69
|
+
return ` '${operation.name}': {
|
|
70
|
+
readonly input: ${renderRoot(schemaToNode(operation.inputSchema), aliases)};
|
|
71
|
+
readonly output: ${renderRoot(schemaToNode(operation.outputSchema), aliases)};
|
|
72
|
+
readonly faults: ${faults};
|
|
73
|
+
};`;
|
|
74
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type TypeNode = {
|
|
2
|
+
readonly kind: 'primitive';
|
|
3
|
+
readonly text: string;
|
|
4
|
+
} | {
|
|
5
|
+
readonly kind: 'reference';
|
|
6
|
+
readonly name: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: 'array';
|
|
9
|
+
readonly element: TypeNode;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: 'union';
|
|
12
|
+
readonly members: ReadonlyArray<TypeNode>;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: 'record';
|
|
15
|
+
readonly value?: TypeNode;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: 'object';
|
|
18
|
+
readonly fields: ReadonlyArray<ObjectField>;
|
|
19
|
+
};
|
|
20
|
+
export type ObjectField = {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly optional: boolean;
|
|
23
|
+
readonly value: TypeNode;
|
|
24
|
+
};
|
|
25
|
+
/** Parse one schema root and its reachable, root-scoped local definitions. */
|
|
26
|
+
export declare function parseSchema(schema: Record<string, unknown>): {
|
|
27
|
+
readonly root: TypeNode;
|
|
28
|
+
readonly definitions: ReadonlyMap<string, TypeNode>;
|
|
29
|
+
};
|
|
30
|
+
export declare function schemaToNode(schema: Record<string, unknown>): TypeNode;
|
|
31
|
+
/**
|
|
32
|
+
* Render a {@link TypeNode} to TypeScript. When a child node's
|
|
33
|
+
* {@link canonicalKey} is present in `aliasNameByKey`, the alias name is
|
|
34
|
+
* substituted in place of inlining the shape. `suppressKey` prevents an alias
|
|
35
|
+
* body from referencing itself (which would emit `type X = X`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function renderNode(node: TypeNode, aliasNameByKey: ReadonlyMap<string, string>, suppressKey?: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Render a top-level node (an operation input/output position). Unlike
|
|
40
|
+
* {@link renderNode}, this substitutes the alias name when the node *itself* is
|
|
41
|
+
* hoisted — so two operations with an identical whole-object input both collapse
|
|
42
|
+
* to the same alias reference.
|
|
43
|
+
*/
|
|
44
|
+
export declare function renderRoot(node: TypeNode, aliasNameByKey: ReadonlyMap<string, string>): string;
|
|
45
|
+
/** Deterministic structural key for a node; equal iff the emitted text is equal. */
|
|
46
|
+
export declare function canonicalKey(node: TypeNode): string;
|