@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.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -2
  3. package/dist/access-policy-metadata.d.ts +92 -0
  4. package/dist/access-policy-metadata.js +87 -0
  5. package/dist/asyncapi-channels.d.ts +46 -0
  6. package/dist/asyncapi-channels.js +399 -0
  7. package/dist/asyncapi.d.ts +40 -0
  8. package/dist/asyncapi.js +165 -0
  9. package/dist/authentication/api-key.d.ts +4 -0
  10. package/dist/authentication/api-key.js +114 -0
  11. package/dist/authentication/audit.d.ts +115 -0
  12. package/dist/authentication/audit.js +75 -0
  13. package/dist/authentication/credential-verifiers.d.ts +60 -0
  14. package/dist/authentication/credential-verifiers.js +102 -0
  15. package/dist/authentication/crypto.d.ts +7 -0
  16. package/dist/authentication/crypto.js +140 -0
  17. package/dist/authentication/index.d.ts +80 -0
  18. package/dist/authentication/index.js +212 -0
  19. package/dist/authentication/rate-limiter.d.ts +133 -0
  20. package/dist/authentication/rate-limiter.js +124 -0
  21. package/dist/authentication/redaction.d.ts +72 -0
  22. package/dist/authentication/redaction.js +118 -0
  23. package/dist/authentication/rotating-api-key-store.d.ts +127 -0
  24. package/dist/authentication/rotating-api-key-store.js +126 -0
  25. package/dist/authentication/types.d.ts +337 -0
  26. package/dist/authentication/types.js +26 -0
  27. package/dist/authentication.d.ts +1 -0
  28. package/dist/authentication.js +1 -0
  29. package/dist/authorization-scope.d.ts +109 -0
  30. package/dist/authorization-scope.js +144 -0
  31. package/dist/authorization.d.ts +55 -0
  32. package/dist/authorization.js +134 -0
  33. package/dist/bind-feed-lifetime.d.ts +45 -0
  34. package/dist/bind-feed-lifetime.js +55 -0
  35. package/dist/byte-arrays.d.ts +5 -0
  36. package/dist/byte-arrays.js +9 -0
  37. package/dist/client/catalog-client.d.ts +285 -0
  38. package/dist/client/catalog-client.js +74 -0
  39. package/dist/client/in-process-transport.d.ts +19 -0
  40. package/dist/client/in-process-transport.js +136 -0
  41. package/dist/client/index.d.ts +14 -0
  42. package/dist/client/index.js +14 -0
  43. package/dist/client/json-rpc-http-transport.d.ts +118 -0
  44. package/dist/client/json-rpc-http-transport.js +127 -0
  45. package/dist/client/server-sent-event-parser.d.ts +55 -0
  46. package/dist/client/server-sent-event-parser.js +78 -0
  47. package/dist/client/sse-subscription-transport.d.ts +72 -0
  48. package/dist/client/sse-subscription-transport.js +224 -0
  49. package/dist/client/subscription-client.d.ts +122 -0
  50. package/dist/client/subscription-client.js +28 -0
  51. package/dist/connection-lease.d.ts +10 -0
  52. package/dist/connection-lease.js +8 -0
  53. package/dist/discovery-filter.d.ts +13 -0
  54. package/dist/discovery-filter.js +16 -0
  55. package/dist/discovery-info.d.ts +94 -0
  56. package/dist/discovery-info.js +38 -0
  57. package/dist/event-selector.d.ts +7 -0
  58. package/dist/event-selector.js +7 -0
  59. package/dist/fault-code.d.ts +84 -0
  60. package/dist/fault-code.js +114 -0
  61. package/dist/fault-remediation.d.ts +30 -0
  62. package/dist/fault-remediation.js +52 -0
  63. package/dist/fault-to-http.d.ts +11 -0
  64. package/dist/fault-to-http.js +13 -0
  65. package/dist/fault-to-json-rpc.d.ts +53 -0
  66. package/dist/fault-to-json-rpc.js +143 -0
  67. package/dist/fault-wire-safety.d.ts +45 -0
  68. package/dist/fault-wire-safety.js +72 -0
  69. package/dist/generator.d.ts +9 -0
  70. package/dist/generator.js +9 -0
  71. package/dist/handler/auth-context-principal.d.ts +37 -0
  72. package/dist/handler/auth-context-principal.js +67 -0
  73. package/dist/handler/authentication-responses.d.ts +23 -0
  74. package/dist/handler/authentication-responses.js +34 -0
  75. package/dist/handler/json-responses.d.ts +15 -0
  76. package/dist/handler/json-responses.js +32 -0
  77. package/dist/handler/operation-handler-authentication.d.ts +64 -0
  78. package/dist/handler/operation-handler-authentication.js +58 -0
  79. package/dist/handler/operation-handler-discovery.d.ts +29 -0
  80. package/dist/handler/operation-handler-discovery.js +55 -0
  81. package/dist/handler/operation-handler-mounts.d.ts +100 -0
  82. package/dist/handler/operation-handler-mounts.js +134 -0
  83. package/dist/handler/operation-handler-subscription.d.ts +34 -0
  84. package/dist/handler/operation-handler-subscription.js +112 -0
  85. package/dist/handler/operation-handler.d.ts +162 -0
  86. package/dist/handler/operation-handler.js +236 -0
  87. package/dist/handler/workspace-release.d.ts +6 -0
  88. package/dist/handler/workspace-release.js +75 -0
  89. package/dist/in-memory-replay-live-backend.d.ts +47 -0
  90. package/dist/in-memory-replay-live-backend.js +129 -0
  91. package/dist/index.d.ts +37 -0
  92. package/dist/index.js +30 -0
  93. package/dist/internal/cryptography/encryption.d.ts +108 -0
  94. package/dist/internal/cryptography/encryption.js +241 -0
  95. package/dist/internal/cryptography/hash.d.ts +72 -0
  96. package/dist/internal/cryptography/hash.js +233 -0
  97. package/dist/internal/cryptography/index.d.ts +4 -0
  98. package/dist/internal/cryptography/index.js +2 -0
  99. package/dist/internals.d.ts +58 -0
  100. package/dist/internals.js +54 -0
  101. package/dist/json-rpc-dispatch.d.ts +88 -0
  102. package/dist/json-rpc-dispatch.js +154 -0
  103. package/dist/json-rpc-framing.d.ts +42 -0
  104. package/dist/json-rpc-framing.js +50 -0
  105. package/dist/json-rpc-http.d.ts +36 -0
  106. package/dist/json-rpc-http.js +191 -0
  107. package/dist/json-rpc-parse.d.ts +69 -0
  108. package/dist/json-rpc-parse.js +206 -0
  109. package/dist/json-rpc-protocol.d.ts +76 -0
  110. package/dist/json-rpc-protocol.js +54 -0
  111. package/dist/json-rpc-session-runtime.d.ts +55 -0
  112. package/dist/json-rpc-session-runtime.js +111 -0
  113. package/dist/json-rpc-websocket-subscription-pump.d.ts +9 -0
  114. package/dist/json-rpc-websocket-subscription-pump.js +46 -0
  115. package/dist/json-rpc-websocket-subscriptions.d.ts +10 -0
  116. package/dist/json-rpc-websocket-subscriptions.js +33 -0
  117. package/dist/json-rpc-websocket-types.d.ts +34 -0
  118. package/dist/json-rpc-websocket-types.js +1 -0
  119. package/dist/json-rpc-websocket-validation.d.ts +45 -0
  120. package/dist/json-rpc-websocket-validation.js +154 -0
  121. package/dist/json-rpc-websocket.d.ts +20 -0
  122. package/dist/json-rpc-websocket.js +363 -0
  123. package/dist/json-schema-utilities.d.ts +5 -0
  124. package/dist/json-schema-utilities.js +17 -0
  125. package/dist/mcp-discovery-names.d.ts +11 -0
  126. package/dist/mcp-discovery-names.js +11 -0
  127. package/dist/openapi-canonical-json.d.ts +10 -0
  128. package/dist/openapi-canonical-json.js +23 -0
  129. package/dist/openapi-error-responses.d.ts +15 -0
  130. package/dist/openapi-error-responses.js +104 -0
  131. package/dist/openapi-schemas.d.ts +17 -0
  132. package/dist/openapi-schemas.js +94 -0
  133. package/dist/openapi.d.ts +60 -0
  134. package/dist/openapi.js +351 -0
  135. package/dist/openrpc-document-schema.d.ts +255 -0
  136. package/dist/openrpc-document-schema.js +84 -0
  137. package/dist/openrpc-errors.d.ts +93 -0
  138. package/dist/openrpc-errors.js +134 -0
  139. package/dist/openrpc.d.ts +66 -0
  140. package/dist/openrpc.js +260 -0
  141. package/dist/operation-catalog/behavior-types.d.ts +34 -0
  142. package/dist/operation-catalog/behavior-types.js +1 -0
  143. package/dist/operation-catalog/behavior-validation.d.ts +3 -0
  144. package/dist/operation-catalog/behavior-validation.js +298 -0
  145. package/dist/operation-catalog/dispatch-allowlist.d.ts +4 -0
  146. package/dist/operation-catalog/dispatch-allowlist.js +7 -0
  147. package/dist/operation-catalog/dispatch-preparation.d.ts +24 -0
  148. package/dist/operation-catalog/dispatch-preparation.js +49 -0
  149. package/dist/operation-catalog/index.d.ts +19 -0
  150. package/dist/operation-catalog/index.js +17 -0
  151. package/dist/operation-catalog/operation-dispatch.d.ts +14 -0
  152. package/dist/operation-catalog/operation-dispatch.js +115 -0
  153. package/dist/operation-catalog/operation-interceptors.d.ts +15 -0
  154. package/dist/operation-catalog/operation-interceptors.js +1 -0
  155. package/dist/operation-catalog/operation-metadata.d.ts +5 -0
  156. package/dist/operation-catalog/operation-metadata.js +104 -0
  157. package/dist/operation-catalog/pipeline-helpers.d.ts +70 -0
  158. package/dist/operation-catalog/pipeline-helpers.js +242 -0
  159. package/dist/operation-catalog/pipeline-stages.d.ts +23 -0
  160. package/dist/operation-catalog/pipeline-stages.js +226 -0
  161. package/dist/operation-catalog/pipeline.d.ts +7 -0
  162. package/dist/operation-catalog/pipeline.js +33 -0
  163. package/dist/operation-catalog/registry.d.ts +17 -0
  164. package/dist/operation-catalog/registry.js +132 -0
  165. package/dist/operation-catalog/stream-pipeline.d.ts +37 -0
  166. package/dist/operation-catalog/stream-pipeline.js +129 -0
  167. package/dist/operation-catalog/subscription-lifecycle.d.ts +6 -0
  168. package/dist/operation-catalog/subscription-lifecycle.js +27 -0
  169. package/dist/operation-catalog/types.d.ts +392 -0
  170. package/dist/operation-catalog/types.js +40 -0
  171. package/dist/operation-catalog/universal-faults.d.ts +11 -0
  172. package/dist/operation-catalog/universal-faults.js +15 -0
  173. package/dist/operation-catalog-snapshot.d.ts +215 -0
  174. package/dist/operation-catalog-snapshot.js +128 -0
  175. package/dist/operation-catalog.d.ts +1 -0
  176. package/dist/operation-catalog.js +1 -0
  177. package/dist/operation-description-audit.d.ts +44 -0
  178. package/dist/operation-description-audit.js +90 -0
  179. package/dist/operation-fault-helpers.d.ts +21 -0
  180. package/dist/operation-fault-helpers.js +32 -0
  181. package/dist/operation-fault.d.ts +245 -0
  182. package/dist/operation-fault.js +235 -0
  183. package/dist/operation-registry.d.ts +46 -0
  184. package/dist/operation-registry.js +46 -0
  185. package/dist/operations/closable-iterable.d.ts +23 -0
  186. package/dist/operations/closable-iterable.js +51 -0
  187. package/dist/operations/get-principal.d.ts +106 -0
  188. package/dist/operations/get-principal.js +107 -0
  189. package/dist/operations/sse-stream.d.ts +67 -0
  190. package/dist/operations/sse-stream.js +264 -0
  191. package/dist/principal.d.ts +253 -0
  192. package/dist/principal.js +254 -0
  193. package/dist/replay-live-feed-internals.d.ts +47 -0
  194. package/dist/replay-live-feed-internals.js +215 -0
  195. package/dist/replay-live-feed-types.d.ts +66 -0
  196. package/dist/replay-live-feed-types.js +7 -0
  197. package/dist/replay-live-feed.d.ts +49 -0
  198. package/dist/replay-live-feed.js +126 -0
  199. package/dist/rest-binding.d.ts +215 -0
  200. package/dist/rest-binding.js +95 -0
  201. package/dist/rest-body.d.ts +17 -0
  202. package/dist/rest-body.js +95 -0
  203. package/dist/route-model.d.ts +305 -0
  204. package/dist/route-model.js +320 -0
  205. package/dist/runtime/cors.d.ts +161 -0
  206. package/dist/runtime/cors.js +265 -0
  207. package/dist/scripts/generate-operation-client.d.ts +186 -0
  208. package/dist/scripts/generate-operation-client.js +561 -0
  209. package/dist/scripts/operation-behaviors.d.ts +3 -0
  210. package/dist/scripts/operation-behaviors.js +16 -0
  211. package/dist/scripts/operation-client-aliases.d.ts +32 -0
  212. package/dist/scripts/operation-client-aliases.js +255 -0
  213. package/dist/scripts/operation-client-domains.d.ts +21 -0
  214. package/dist/scripts/operation-client-domains.js +74 -0
  215. package/dist/scripts/operation-client-schema.d.ts +46 -0
  216. package/dist/scripts/operation-client-schema.js +310 -0
  217. package/dist/scripts/operation-client-svelte.d.ts +3 -0
  218. package/dist/scripts/operation-client-svelte.js +61 -0
  219. package/dist/sequence-cursor.d.ts +46 -0
  220. package/dist/sequence-cursor.js +62 -0
  221. package/dist/sse-error-payload-schema.d.ts +12 -0
  222. package/dist/sse-error-payload-schema.js +33 -0
  223. package/dist/stdio-session.d.ts +71 -0
  224. package/dist/stdio-session.js +347 -0
  225. package/dist/zod-json-schema.d.ts +22 -0
  226. package/dist/zod-json-schema.js +49 -0
  227. package/package.json +38 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024–2026 Steve Kinney
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,63 @@
1
- # Temporary Holding Version
1
+ # Backchannel
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This package runs on Cloudflare Workers with `nodejs_compat`. Supply a WebSocketPair-backed socket for JSON-RPC WebSocket operations.
4
+
5
+ `@lostgradient/backchannel` is the transport-neutral operation layer that Weft, Bureau and Interborough share. An operation is defined once, with its input and output schemas, its access policy and the faults it can raise. The layer then serves it over REST, JSON-RPC (HTTP, WebSocket and stdio) and Server-Sent Events, and generates its OpenAPI, OpenRPC and AsyncAPI documents from the catalog. It imports no Bun or Node API, so any host that can hand it a `Request`, a socket or a pair of streams can serve a catalog.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @lostgradient/backchannel
11
+ ```
12
+
13
+ ## Entries
14
+
15
+ - `@lostgradient/backchannel` is the public surface: `defineOperation`, `createOperationHandler`, the fault model, principals and authorization scopes, the authentication helpers, the replay-and-live feed and the document generators.
16
+ - `@lostgradient/backchannel/client` is the browser-safe client: `createCatalogClient`, the `CatalogTransport` seam and `CatalogClientError`, the JSON-RPC HTTP transport (`httpJsonRpcTransport`), the subscription client (`createSubscriptionClient`) with its Server-Sent Events transport (`sseSubscriptionTransport`), and the chunk parser. Every call takes an optional `{ signal, fetch }` second argument. A subscription method returns an `AsyncIterable` of `{ type: 'event', id, data }` and one `{ type: 'replay-complete' }`, and takes `lastEventId` to resume.
17
+ - `@lostgradient/backchannel/generator` generates a typed client for any operation catalog: `createOperationClientSources(snapshot, options)` returns the files as a map from file name to source, and the caller passes its own formatter. It may import `node:` modules; the other entries may not.
18
+ - `@lostgradient/backchannel/internals` is the plumbing a host needs: the dispatch pipeline, the transport sessions and the route model. It is unstable. A name there may change or move without notice.
19
+ - `@lostgradient/backchannel/testing` is test support shared with the packages that build on the layer.
20
+
21
+ ## How It Works
22
+
23
+ `defineOperation` builds an operation from a Zod input and output schema. `createOperationHandler` takes a list of operations and the `host` they read, and returns a fetch handler with `upgrade` and `websocket` properties for a host that can accept a WebSocket. The handler owns authentication, access policy, fault mapping, cursor replay, CORS and the discovery documents. The host supplies what the layer does not know: a `FaultPolicy` for its own error classes and its strict-fault setting, the title and version of its documents, and an encoder if it serves MessagePack.
24
+
25
+ ## Example
26
+
27
+ This defines one operation and serves it with `createOperationHandler`. The handler is a function from `Request` to `Response`, so the example calls it directly over JSON-RPC at `/jsonrpc`; a host would pass the same function to its server.
28
+
29
+ ```ts
30
+ import { createOperationHandler, defineOperation } from '@lostgradient/backchannel';
31
+ import { z } from 'zod';
32
+
33
+ const echo = defineOperation({
34
+ name: 'probe.echo',
35
+ mcpExposable: false,
36
+ summary: 'Echo a message',
37
+ destructive: false,
38
+ inputSchema: z.object({ message: z.string() }),
39
+ outputSchema: z.object({ message: z.string() }),
40
+ access: { kind: 'public' },
41
+ transports: { http: false, jsonRpcHttp: true, jsonRpcWebSocket: false, jsonRpcStdio: false },
42
+ unknownKeyPolicy: { http: 'reject', jsonRpc: 'reject' },
43
+ invoke: async ({ input }) => ({ message: input.message }),
44
+ });
45
+
46
+ const handler = createOperationHandler({ operations: [echo], host: {} });
47
+
48
+ const response = await handler(
49
+ new Request('http://localhost/jsonrpc', {
50
+ method: 'POST',
51
+ headers: { 'content-type': 'application/json' },
52
+ body: JSON.stringify({
53
+ jsonrpc: '2.0',
54
+ id: 1,
55
+ method: 'probe.echo',
56
+ params: { message: 'hello' },
57
+ }),
58
+ }),
59
+ );
60
+ console.log(await response.json()); // { jsonrpc: '2.0', result: { message: 'hello' }, id: 1 }
61
+ ```
62
+
63
+ Weft hosts its engine's operations on the layer, and Bureau serves its own through `createBureauHandler`.
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Canonical discovery-document metadata for operation access policies.
3
+ *
4
+ * @module server/access-policy-metadata
5
+ */
6
+ import { z } from 'zod';
7
+ import type { AccessPolicy } from "./authorization.js";
8
+ import type { ParameterizedAccessHint } from "./operation-catalog/types.js";
9
+ export declare const ScopeRequirementMetadataSchema: z.ZodObject<{
10
+ kind: z.ZodEnum<{
11
+ anyOf: "anyOf";
12
+ allOf: "allOf";
13
+ }>;
14
+ scopes: z.ZodArray<z.ZodString>;
15
+ }, z.core.$strict>;
16
+ export declare const AccessPolicyMetadataSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
17
+ kind: z.ZodLiteral<"public">;
18
+ }, z.core.$strict>, z.ZodObject<{
19
+ kind: z.ZodLiteral<"authenticated">;
20
+ }, z.core.$strict>, z.ZodObject<{
21
+ kind: z.ZodLiteral<"scoped">;
22
+ scopes: z.ZodObject<{
23
+ kind: z.ZodEnum<{
24
+ anyOf: "anyOf";
25
+ allOf: "allOf";
26
+ }>;
27
+ scopes: z.ZodArray<z.ZodString>;
28
+ }, z.core.$strict>;
29
+ }, z.core.$strict>, z.ZodObject<{
30
+ kind: z.ZodLiteral<"optionalAuth">;
31
+ authenticatedScopes: z.ZodObject<{
32
+ kind: z.ZodEnum<{
33
+ anyOf: "anyOf";
34
+ allOf: "allOf";
35
+ }>;
36
+ scopes: z.ZodArray<z.ZodString>;
37
+ }, z.core.$strict>;
38
+ }, z.core.$strict>, z.ZodObject<{
39
+ kind: z.ZodLiteral<"scopedAlternatives">;
40
+ alternatives: z.ZodArray<z.ZodObject<{
41
+ kind: z.ZodEnum<{
42
+ anyOf: "anyOf";
43
+ allOf: "allOf";
44
+ }>;
45
+ scopes: z.ZodArray<z.ZodString>;
46
+ }, z.core.$strict>>;
47
+ }, z.core.$strict>], "kind">;
48
+ export declare const ParameterizedAccessMetadataSchema: z.ZodObject<{
49
+ discriminator: z.ZodString;
50
+ defaultValue: z.ZodOptional<z.ZodString>;
51
+ variants: z.ZodArray<z.ZodObject<{
52
+ value: z.ZodString;
53
+ access: z.ZodDiscriminatedUnion<[z.ZodObject<{
54
+ kind: z.ZodLiteral<"public">;
55
+ }, z.core.$strict>, z.ZodObject<{
56
+ kind: z.ZodLiteral<"authenticated">;
57
+ }, z.core.$strict>, z.ZodObject<{
58
+ kind: z.ZodLiteral<"scoped">;
59
+ scopes: z.ZodObject<{
60
+ kind: z.ZodEnum<{
61
+ anyOf: "anyOf";
62
+ allOf: "allOf";
63
+ }>;
64
+ scopes: z.ZodArray<z.ZodString>;
65
+ }, z.core.$strict>;
66
+ }, z.core.$strict>, z.ZodObject<{
67
+ kind: z.ZodLiteral<"optionalAuth">;
68
+ authenticatedScopes: z.ZodObject<{
69
+ kind: z.ZodEnum<{
70
+ anyOf: "anyOf";
71
+ allOf: "allOf";
72
+ }>;
73
+ scopes: z.ZodArray<z.ZodString>;
74
+ }, z.core.$strict>;
75
+ }, z.core.$strict>, z.ZodObject<{
76
+ kind: z.ZodLiteral<"scopedAlternatives">;
77
+ alternatives: z.ZodArray<z.ZodObject<{
78
+ kind: z.ZodEnum<{
79
+ anyOf: "anyOf";
80
+ allOf: "allOf";
81
+ }>;
82
+ scopes: z.ZodArray<z.ZodString>;
83
+ }, z.core.$strict>>;
84
+ }, z.core.$strict>], "kind">;
85
+ }, z.core.$strict>>;
86
+ }, z.core.$strict>;
87
+ export type AccessPolicyMetadata = z.infer<typeof AccessPolicyMetadataSchema>;
88
+ export type ParameterizedAccessMetadata = z.infer<typeof ParameterizedAccessMetadataSchema>;
89
+ /** Serialize an operation's static access policy for discovery documents. */
90
+ export declare function serializeAccessPolicy(policy: AccessPolicy): AccessPolicyMetadata;
91
+ /** Serialize discriminator-specific access overrides for discovery documents. */
92
+ export declare function serializeParameterizedAccess(hint: ParameterizedAccessHint): ParameterizedAccessMetadata;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Canonical discovery-document metadata for operation access policies.
3
+ *
4
+ * @module server/access-policy-metadata
5
+ */
6
+ import { z } from 'zod';
7
+ import { compareStrings } from "./json-schema-utilities.js";
8
+ export const ScopeRequirementMetadataSchema = z.strictObject({
9
+ kind: z.enum(['anyOf', 'allOf']),
10
+ scopes: z.array(z.string()).min(1),
11
+ });
12
+ export const AccessPolicyMetadataSchema = z.discriminatedUnion('kind', [
13
+ z.strictObject({ kind: z.literal('public') }),
14
+ z.strictObject({ kind: z.literal('authenticated') }),
15
+ z.strictObject({
16
+ kind: z.literal('scoped'),
17
+ scopes: ScopeRequirementMetadataSchema,
18
+ }),
19
+ z.strictObject({
20
+ kind: z.literal('optionalAuth'),
21
+ authenticatedScopes: ScopeRequirementMetadataSchema,
22
+ }),
23
+ z.strictObject({
24
+ kind: z.literal('scopedAlternatives'),
25
+ alternatives: z.array(ScopeRequirementMetadataSchema).min(1),
26
+ }),
27
+ ]);
28
+ export const ParameterizedAccessMetadataSchema = z.strictObject({
29
+ discriminator: z.string(),
30
+ defaultValue: z.string().optional(),
31
+ variants: z.array(z.strictObject({
32
+ value: z.string(),
33
+ access: AccessPolicyMetadataSchema,
34
+ })),
35
+ });
36
+ /** Serialize an operation's static access policy for discovery documents. */
37
+ export function serializeAccessPolicy(policy) {
38
+ switch (policy.kind) {
39
+ case 'public':
40
+ return { kind: 'public' };
41
+ case 'authenticated':
42
+ return { kind: 'authenticated' };
43
+ case 'scoped':
44
+ return { kind: 'scoped', scopes: serializeScopeRequirement(policy.scopes) };
45
+ case 'optionalAuth':
46
+ return {
47
+ kind: 'optionalAuth',
48
+ authenticatedScopes: serializeScopeRequirement(policy.authenticatedScopes),
49
+ };
50
+ case 'scopedAlternatives':
51
+ return {
52
+ kind: 'scopedAlternatives',
53
+ alternatives: policy.alternatives.map(serializeScopeRequirement),
54
+ };
55
+ default:
56
+ return unsupportedAccessPolicy(policy);
57
+ }
58
+ }
59
+ /** Serialize discriminator-specific access overrides for discovery documents. */
60
+ export function serializeParameterizedAccess(hint) {
61
+ return {
62
+ discriminator: hint.discriminator,
63
+ ...(hint.defaultValue === undefined ? {} : { defaultValue: hint.defaultValue }),
64
+ variants: hint.variants.map((variant) => ({
65
+ value: variant.value,
66
+ access: serializeAccessPolicy(variant.access),
67
+ })),
68
+ };
69
+ }
70
+ function serializeScopeRequirement(requirement) {
71
+ switch (requirement.kind) {
72
+ case 'anyOf':
73
+ case 'allOf':
74
+ return {
75
+ kind: requirement.kind,
76
+ scopes: [...requirement.scopes].toSorted(compareStrings),
77
+ };
78
+ default:
79
+ return unsupportedScopeRequirement(requirement);
80
+ }
81
+ }
82
+ function unsupportedAccessPolicy(policy) {
83
+ throw new Error(`cannot serialize unsupported access policy: ${String(policy)}`);
84
+ }
85
+ function unsupportedScopeRequirement(requirement) {
86
+ throw new Error(`cannot serialize unsupported scope requirement: ${String(requirement)}`);
87
+ }
@@ -0,0 +1,46 @@
1
+ import type { z } from 'zod';
2
+ import type { ErasedOperation } from "./operation-catalog.js";
3
+ import type { ZodJsonSchemaDirection } from "./zod-json-schema.js";
4
+ /**
5
+ * Build the AsyncAPI channel object for a WebSocket subscription operation.
6
+ */
7
+ export declare function buildWebSocketChannel(operation: ErasedOperation): Record<string, unknown>;
8
+ /**
9
+ * Build the AsyncAPI channel object for an SSE stream operation.
10
+ *
11
+ * `restBindingPath` MUST be the operation's REST binding path translated to
12
+ * OpenAPI path-parameter syntax (e.g. `/v1/workflows/{id}/sse`). Without it
13
+ * the channel address falls back to a synthetic dotted-name-to-path
14
+ * conversion that does not match any wire endpoint — kept only as a
15
+ * non-failing default so a misconfigured registry produces a recognizable
16
+ * synthetic address rather than throwing during document generation.
17
+ */
18
+ export declare function buildSseChannel(operation: ErasedOperation, restBindingPath: string | undefined,
19
+ /**
20
+ * Maps the binding path to its wire address. Defaults to the `/api`
21
+ * external prefix `serve()` mounts; the engine-independent handler has no
22
+ * front door and passes the path through unchanged.
23
+ */
24
+ toWireAddress?: (path: string) => string): Record<string, unknown>;
25
+ /**
26
+ * Build named component messages for a WebSocket subscription operation.
27
+ */
28
+ export declare function buildWebSocketMessages(operation: ErasedOperation, definitionSchemaToJsonSchema: (schema: z.ZodType, direction?: ZodJsonSchemaDirection) => Record<string, unknown>): Record<string, Record<string, unknown>>;
29
+ /**
30
+ * Build named component messages for an SSE stream operation.
31
+ *
32
+ * The token message's `data:` line carries the operation's wire-encoded
33
+ * token text — for the only SSE operation today (`weft.workflows.streams.sse`)
34
+ * that is the raw `token` string from `mapTokenChunkToText`, NOT a JSON
35
+ * encoding of the operation's `eventSchema`. The `eventSchema` describes
36
+ * the logical per-element type for non-SSE callers (JSON-RPC envelopes
37
+ * carry the full object); SSE is a separate, plain-text wire.
38
+ *
39
+ * The schema below reflects the actual SSE wire. The `x-weft-event-schema`
40
+ * extension surfaces the logical schema for clients that need it.
41
+ */
42
+ export declare function buildSseMessages(operation: ErasedOperation, definitionSchemaToJsonSchema: (schema: z.ZodType, direction?: ZodJsonSchemaDirection) => Record<string, unknown>): Record<string, Record<string, unknown>>;
43
+ /**
44
+ * Build an AsyncAPI operation entry for a stream or subscription operation.
45
+ */
46
+ export declare function buildOperationEntry(operation: ErasedOperation, channelName: string, kind: 'subscription' | 'stream'): Record<string, unknown>;