@projected/http 0.0.0-stage → 0.1.1
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/dist/.tsbuildinfo +1 -0
- package/dist/client.d.ts +89 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +176 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +20 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +64 -0
- package/dist/node.js.map +1 -0
- package/dist/openapi.d.ts +43 -0
- package/dist/openapi.d.ts.map +1 -0
- package/dist/openapi.js +296 -0
- package/dist/openapi.js.map +1 -0
- package/dist/protocol.d.ts +68 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +98 -0
- package/dist/protocol.js.map +1 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +200 -0
- package/dist/server.js.map +1 -0
- package/package.json +41 -3
- package/src/client.ts +293 -0
- package/src/index.ts +32 -0
- package/src/node.ts +81 -0
- package/src/openapi.ts +344 -0
- package/src/protocol.ts +118 -0
- package/src/server.ts +263 -0
- package/README.md +0 -3
package/dist/node.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
/** Run a {@link FetchHandler} on Node's built-in HTTP server. */
|
|
3
|
+
export function serve(handler, options = {}) {
|
|
4
|
+
const maxBodyBytes = options.maxBodyBytes ?? 1024 * 1024;
|
|
5
|
+
const server = createServer((req, res) => {
|
|
6
|
+
void respond(handler, req, res, maxBodyBytes).catch((error) => {
|
|
7
|
+
options.onError?.(error);
|
|
8
|
+
if (!res.headersSent)
|
|
9
|
+
res.writeHead(500, { "content-type": "application/problem+json" });
|
|
10
|
+
res.end(JSON.stringify({ type: "urn:projected:problem:internal-error", title: "Internal error", status: 500 }));
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
server.once("error", reject);
|
|
15
|
+
server.listen(options.port ?? 3000, options.hostname ?? "0.0.0.0", () => {
|
|
16
|
+
const { port } = server.address();
|
|
17
|
+
const host = options.hostname === undefined || options.hostname === "0.0.0.0" ? "localhost" : options.hostname;
|
|
18
|
+
resolve({
|
|
19
|
+
url: `http://${host}:${port}`,
|
|
20
|
+
close: () => new Promise((done, fail) => server.close((error) => (error ? fail(error) : done()))),
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async function respond(handler, req, res, maxBodyBytes) {
|
|
26
|
+
const chunks = [];
|
|
27
|
+
let size = 0;
|
|
28
|
+
for await (const chunk of req) {
|
|
29
|
+
size += chunk.length;
|
|
30
|
+
if (size > maxBodyBytes) {
|
|
31
|
+
res.writeHead(413, { "content-type": "application/problem+json", connection: "close" });
|
|
32
|
+
res.end(JSON.stringify({ type: "urn:projected:problem:payload-too-large", title: "Payload too large", status: 413 }));
|
|
33
|
+
req.destroy();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
chunks.push(chunk);
|
|
37
|
+
}
|
|
38
|
+
const headers = new Headers();
|
|
39
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
40
|
+
if (Array.isArray(value))
|
|
41
|
+
value.forEach((v) => headers.append(name, v));
|
|
42
|
+
else if (value !== undefined)
|
|
43
|
+
headers.set(name, value);
|
|
44
|
+
}
|
|
45
|
+
const method = req.method ?? "GET";
|
|
46
|
+
const request = new Request(`http://${req.headers.host ?? "localhost"}${req.url ?? "/"}`, {
|
|
47
|
+
method,
|
|
48
|
+
headers,
|
|
49
|
+
...(method === "GET" || method === "HEAD" ? {} : { body: Buffer.concat(chunks) }),
|
|
50
|
+
});
|
|
51
|
+
const response = await handler(request);
|
|
52
|
+
// One entry per name would keep only the last cookie: Set-Cookie is the header that repeats.
|
|
53
|
+
const outgoing = {};
|
|
54
|
+
response.headers.forEach((value, name) => {
|
|
55
|
+
if (name !== "set-cookie")
|
|
56
|
+
outgoing[name] = value;
|
|
57
|
+
});
|
|
58
|
+
const cookies = response.headers.getSetCookie();
|
|
59
|
+
if (cookies.length > 0)
|
|
60
|
+
outgoing["set-cookie"] = cookies;
|
|
61
|
+
res.writeHead(response.status, outgoing);
|
|
62
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA6C,MAAM,WAAW,CAAC;AAuBpF,iEAAiE;AACjE,MAAM,UAAU,KAAK,CAAC,OAAqB,EAAE,UAAwB,EAAE;IACrE,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,IAAI,GAAG,IAAI,CAAC;IACzD,MAAM,MAAM,GAAG,YAAY,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACvC,KAAK,OAAO,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;YACrE,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;YACzB,IAAI,CAAC,GAAG,CAAC,WAAW;gBAAE,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,0BAA0B,EAAE,CAAC,CAAC;YACzF,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,sCAAsC,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;QAClH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,OAAO,IAA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,GAAsC,EAAE,CAAC;IACvD,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACvC,IAAI,IAAI,KAAK,YAAY;YAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IACpD,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC;IAChD,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;QAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC;IACzD,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACzC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { AnyContracts } from "@projected/application";
|
|
2
|
+
export interface OpenApiOptions {
|
|
3
|
+
/** `info.version` — the version of the service, not of any operation. Defaults to `0.0.0`. */
|
|
4
|
+
readonly version?: string;
|
|
5
|
+
/** `info.title`. Defaults to the contracts' name. */
|
|
6
|
+
readonly title?: string;
|
|
7
|
+
readonly servers?: ReadonlyArray<{
|
|
8
|
+
readonly url: string;
|
|
9
|
+
readonly description?: string;
|
|
10
|
+
}>;
|
|
11
|
+
/** Prefix for every path, matching the handler's `basePath`. */
|
|
12
|
+
readonly basePath?: string;
|
|
13
|
+
/**
|
|
14
|
+
* How callers authenticate, as OpenAPI security schemes (e.g. `betterAuthSecuritySchemes`).
|
|
15
|
+
* When given, every non-public operation requires one of them.
|
|
16
|
+
*/
|
|
17
|
+
readonly securitySchemes?: Readonly<Record<string, unknown>>;
|
|
18
|
+
}
|
|
19
|
+
/** An OpenAPI 3.1 document. */
|
|
20
|
+
export interface OpenApiDocument {
|
|
21
|
+
readonly openapi: "3.1.0";
|
|
22
|
+
readonly info: {
|
|
23
|
+
readonly title: string;
|
|
24
|
+
readonly version: string;
|
|
25
|
+
readonly description?: string;
|
|
26
|
+
};
|
|
27
|
+
readonly servers?: ReadonlyArray<{
|
|
28
|
+
readonly url: string;
|
|
29
|
+
readonly description?: string;
|
|
30
|
+
}>;
|
|
31
|
+
readonly tags: ReadonlyArray<{
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly description: string;
|
|
34
|
+
}>;
|
|
35
|
+
readonly paths: Readonly<Record<string, unknown>>;
|
|
36
|
+
readonly components: Readonly<Record<string, Record<string, unknown>>>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Derive the OpenAPI document for a set of contracts. Deterministic: the same contracts always
|
|
40
|
+
* produce the same document, so it can be committed and checked in CI (`projected openapi --check`).
|
|
41
|
+
*/
|
|
42
|
+
export declare function openApiDocument(contracts: AnyContracts, options?: OpenApiOptions): OpenApiDocument;
|
|
43
|
+
//# sourceMappingURL=openapi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiC,YAAY,EAAwB,MAAM,wBAAwB,CAAC;AAOhH,MAAM,WAAW,cAAc;IAC7B,8FAA8F;IAC9F,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,qDAAqD;IACrD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1F,gEAAgE;IAChE,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC9D;AAED,+BAA+B;AAC/B,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACnG,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1F,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACtF,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAClD,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;CACxE;AAKD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,YAAY,EAAE,OAAO,GAAE,cAAmB,GAAG,eAAe,CAgPtG"}
|
package/dist/openapi.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { panic } from "better-result";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { HEADERS, PLURAL, operationId, operationPath } from "./protocol.js";
|
|
4
|
+
const pascal = (kebab) => kebab.replace(/(^|-)([a-z0-9])/g, (_, __, c) => c.toUpperCase());
|
|
5
|
+
const ref = (section, name) => ({ $ref: `#/components/${section}/${name}` });
|
|
6
|
+
/**
|
|
7
|
+
* Derive the OpenAPI document for a set of contracts. Deterministic: the same contracts always
|
|
8
|
+
* produce the same document, so it can be committed and checked in CI (`projected openapi --check`).
|
|
9
|
+
*/
|
|
10
|
+
export function openApiDocument(contracts, options = {}) {
|
|
11
|
+
const basePath = (options.basePath ?? "").replace(/\/+$/, "");
|
|
12
|
+
const securitySchemes = options.securitySchemes;
|
|
13
|
+
/** Public operations need nothing; the rest need any one of the schemes. */
|
|
14
|
+
const security = (access) => {
|
|
15
|
+
if (securitySchemes === undefined)
|
|
16
|
+
return {};
|
|
17
|
+
if (access === "public")
|
|
18
|
+
return { security: [] };
|
|
19
|
+
return { security: Object.keys(securitySchemes).map((scheme) => ({ [scheme]: [] })) };
|
|
20
|
+
};
|
|
21
|
+
const schemas = { ...PROTOCOL_SCHEMAS };
|
|
22
|
+
const errorSources = new Map();
|
|
23
|
+
const paths = {};
|
|
24
|
+
/** Convert a zod schema, hoisting any `$defs` into components so references stay resolvable. */
|
|
25
|
+
const component = (name, schema, io) => {
|
|
26
|
+
const converted = z.toJSONSchema(schema, { target: "draft-2020-12", io, unrepresentable: "throw" });
|
|
27
|
+
const { $schema: _ignored, $defs, ...rest } = converted;
|
|
28
|
+
const rename = (definition) => `${name}_${definition}`;
|
|
29
|
+
const rewrite = (value) => {
|
|
30
|
+
if (Array.isArray(value))
|
|
31
|
+
return value.map(rewrite);
|
|
32
|
+
if (value === null || typeof value !== "object")
|
|
33
|
+
return value;
|
|
34
|
+
return Object.fromEntries(Object.entries(value).map(([key, inner]) => key === "$ref" && typeof inner === "string" && inner.startsWith("#/$defs/")
|
|
35
|
+
? [key, `#/components/schemas/${rename(inner.slice("#/$defs/".length))}`]
|
|
36
|
+
: key === "$ref" && inner === "#"
|
|
37
|
+
? [key, `#/components/schemas/${name}`]
|
|
38
|
+
: [key, rewrite(inner)]));
|
|
39
|
+
};
|
|
40
|
+
for (const [definition, value] of Object.entries(($defs ?? {}))) {
|
|
41
|
+
schemas[rename(definition)] = rewrite(value);
|
|
42
|
+
}
|
|
43
|
+
schemas[name] = rewrite(rest);
|
|
44
|
+
return ref("schemas", name);
|
|
45
|
+
};
|
|
46
|
+
const errorComponent = (error) => {
|
|
47
|
+
const data = z.toJSONSchema(error.schema, { target: "draft-2020-12", io: "output", unrepresentable: "throw" });
|
|
48
|
+
const fingerprint = JSON.stringify(data);
|
|
49
|
+
const seen = errorSources.get(error.tag);
|
|
50
|
+
if (seen !== undefined && seen !== fingerprint) {
|
|
51
|
+
panic(`openApiDocument: two different errors are tagged ${error.tag}`);
|
|
52
|
+
}
|
|
53
|
+
if (seen === undefined) {
|
|
54
|
+
errorSources.set(error.tag, fingerprint);
|
|
55
|
+
const dataRef = component(`${error.tag}Data`, error.schema, "output");
|
|
56
|
+
schemas[error.tag] = {
|
|
57
|
+
type: "object",
|
|
58
|
+
...(error.description === undefined ? {} : { description: error.description }),
|
|
59
|
+
required: ["type", "message", "data"],
|
|
60
|
+
additionalProperties: false,
|
|
61
|
+
properties: { type: { const: error.tag }, message: { type: "string" }, data: dataRef },
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return ref("schemas", error.tag);
|
|
65
|
+
};
|
|
66
|
+
const resultSchema = (contract, outputRef) => {
|
|
67
|
+
const ok = {
|
|
68
|
+
type: "object",
|
|
69
|
+
required: ["status", "value"],
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
properties: { status: { const: "ok" }, value: outputRef },
|
|
72
|
+
};
|
|
73
|
+
if (contract.errors.length === 0)
|
|
74
|
+
return ok;
|
|
75
|
+
const errors = contract.errors.map(errorComponent);
|
|
76
|
+
const error = {
|
|
77
|
+
type: "object",
|
|
78
|
+
required: ["status", "error"],
|
|
79
|
+
additionalProperties: false,
|
|
80
|
+
properties: { status: { const: "error" }, error: errors.length === 1 ? errors[0] : { oneOf: errors } },
|
|
81
|
+
};
|
|
82
|
+
return { oneOf: [ok, error] };
|
|
83
|
+
};
|
|
84
|
+
const batchItems = {
|
|
85
|
+
command: { request: [], result: [] },
|
|
86
|
+
query: { request: [], result: [] },
|
|
87
|
+
};
|
|
88
|
+
for (const contract of [...contracts.commands, ...contracts.queries]) {
|
|
89
|
+
const name = `${pascal(contract.type)}V${contract.version}`;
|
|
90
|
+
const inputRef = component(`${name}Input`, contract.input, "input");
|
|
91
|
+
const outputRef = component(`${name}Output`, contract.output, "output");
|
|
92
|
+
const result = resultSchema(contract, outputRef);
|
|
93
|
+
const isCommand = contract.kind === "command";
|
|
94
|
+
const access = contracts.accessOf(contract);
|
|
95
|
+
const isPublic = access === "public";
|
|
96
|
+
paths[`${basePath}${operationPath(contract.kind, contract.type, contract.version)}`] = {
|
|
97
|
+
post: {
|
|
98
|
+
operationId: operationId(contract.type, contract.version),
|
|
99
|
+
tags: [PLURAL[contract.kind]],
|
|
100
|
+
...(contract.summary === undefined ? {} : { summary: contract.summary }),
|
|
101
|
+
...(contract.description === undefined ? {} : { description: contract.description }),
|
|
102
|
+
...(contract.deprecated ? { deprecated: true } : {}),
|
|
103
|
+
"x-projected-access": access,
|
|
104
|
+
...security(access),
|
|
105
|
+
parameters: [
|
|
106
|
+
ref("parameters", "MessageId"),
|
|
107
|
+
ref("parameters", "CorrelationId"),
|
|
108
|
+
ref("parameters", "CausationId"),
|
|
109
|
+
...(isCommand ? [ref("parameters", "IdempotencyKey")] : []),
|
|
110
|
+
],
|
|
111
|
+
requestBody: { required: true, content: { "application/json": { schema: inputRef } } },
|
|
112
|
+
responses: {
|
|
113
|
+
"200": {
|
|
114
|
+
description: "The operation ran. `status` is `ok` with the output, or `error` with one of the business errors this operation declares.",
|
|
115
|
+
headers: { [HEADERS.messageId]: ref("headers", "MessageId"), [HEADERS.correlationId]: ref("headers", "CorrelationId") },
|
|
116
|
+
content: { "application/json": { schema: result } },
|
|
117
|
+
},
|
|
118
|
+
"400": ref("responses", "BadRequest"),
|
|
119
|
+
...(isPublic ? {} : { "401": ref("responses", "Unauthenticated"), "403": ref("responses", "Forbidden") }),
|
|
120
|
+
...(isCommand ? { "409": ref("responses", "Conflict"), "422": ref("responses", "IdempotencyKeyReused") } : {}),
|
|
121
|
+
"415": ref("responses", "UnsupportedMediaType"),
|
|
122
|
+
"500": ref("responses", "InternalError"),
|
|
123
|
+
"502": ref("responses", "BadGateway"),
|
|
124
|
+
"503": ref("responses", "ServiceUnavailable"),
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
batchItems[contract.kind].request.push({
|
|
129
|
+
type: "object",
|
|
130
|
+
required: ["type", "version", "data"],
|
|
131
|
+
additionalProperties: false,
|
|
132
|
+
properties: { type: { const: contract.type }, version: { const: contract.version }, data: inputRef },
|
|
133
|
+
});
|
|
134
|
+
batchItems[contract.kind].result.push(result);
|
|
135
|
+
}
|
|
136
|
+
for (const kind of ["command", "query"]) {
|
|
137
|
+
const { request, result } = batchItems[kind];
|
|
138
|
+
if (request.length === 0)
|
|
139
|
+
continue;
|
|
140
|
+
const plural = PLURAL[kind];
|
|
141
|
+
paths[`${basePath}/${plural}`] = {
|
|
142
|
+
post: {
|
|
143
|
+
operationId: `${kind}Batch`,
|
|
144
|
+
tags: [plural],
|
|
145
|
+
summary: `Run several ${plural} in one request`,
|
|
146
|
+
description: `Items run in order and independently: each has its own outcome, and a failed item does not stop the rest. Not atomic.`,
|
|
147
|
+
parameters: [ref("parameters", "CorrelationId")],
|
|
148
|
+
requestBody: {
|
|
149
|
+
required: true,
|
|
150
|
+
content: {
|
|
151
|
+
"application/json": {
|
|
152
|
+
schema: {
|
|
153
|
+
type: "object",
|
|
154
|
+
required: [plural],
|
|
155
|
+
additionalProperties: false,
|
|
156
|
+
properties: { [plural]: { type: "array", items: request.length === 1 ? request[0] : { oneOf: request } } },
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
responses: {
|
|
162
|
+
"200": {
|
|
163
|
+
description: "One result per item, in order.",
|
|
164
|
+
content: {
|
|
165
|
+
"application/json": {
|
|
166
|
+
schema: {
|
|
167
|
+
type: "object",
|
|
168
|
+
required: ["results"],
|
|
169
|
+
properties: { results: { type: "array", items: { anyOf: [...result, ref("schemas", "FailedBatchItem")] } } },
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
"400": ref("responses", "BadRequest"),
|
|
175
|
+
"413": ref("responses", "PayloadTooLarge"),
|
|
176
|
+
"415": ref("responses", "UnsupportedMediaType"),
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const live = {
|
|
182
|
+
"200": {
|
|
183
|
+
description: "The service is up.",
|
|
184
|
+
content: { "application/json": { schema: { type: "object", properties: { status: { const: "ok" } } } } },
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
paths[`${basePath}/health`] = { get: { operationId: "health", tags: ["system"], summary: "Liveness", responses: live } };
|
|
188
|
+
paths[`${basePath}/health/live`] = { get: { operationId: "healthLive", tags: ["system"], summary: "Liveness", responses: live } };
|
|
189
|
+
const report = {
|
|
190
|
+
type: "object",
|
|
191
|
+
required: ["status", "checks"],
|
|
192
|
+
properties: {
|
|
193
|
+
status: { enum: ["ready", "unready", "draining"] },
|
|
194
|
+
checks: {
|
|
195
|
+
type: "array",
|
|
196
|
+
items: {
|
|
197
|
+
type: "object",
|
|
198
|
+
required: ["name", "status"],
|
|
199
|
+
properties: { name: { type: "string" }, status: { enum: ["up", "down"] }, message: { type: "string" } },
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
paths[`${basePath}/health/ready`] = {
|
|
205
|
+
get: {
|
|
206
|
+
operationId: "healthReady",
|
|
207
|
+
tags: ["system"],
|
|
208
|
+
summary: "Readiness",
|
|
209
|
+
description: "Whether the service should be sent traffic: every readiness check passes, and it is not shutting down.",
|
|
210
|
+
responses: {
|
|
211
|
+
"200": { description: "Ready.", content: { "application/json": { schema: report } } },
|
|
212
|
+
"503": { description: "A dependency is down, or the service is draining.", content: { "application/json": { schema: report } } },
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
return {
|
|
217
|
+
openapi: "3.1.0",
|
|
218
|
+
info: {
|
|
219
|
+
title: options.title ?? contracts.name,
|
|
220
|
+
version: options.version ?? "0.0.0",
|
|
221
|
+
...(contracts.description === undefined ? {} : { description: contracts.description }),
|
|
222
|
+
},
|
|
223
|
+
...(options.servers === undefined ? {} : { servers: options.servers }),
|
|
224
|
+
tags: [
|
|
225
|
+
{ name: "commands", description: "Business operations that change the system." },
|
|
226
|
+
{ name: "queries", description: "Business questions about the system's state." },
|
|
227
|
+
{ name: "system", description: "Operational endpoints." },
|
|
228
|
+
],
|
|
229
|
+
paths,
|
|
230
|
+
components: {
|
|
231
|
+
schemas,
|
|
232
|
+
...(securitySchemes === undefined ? {} : { securitySchemes }),
|
|
233
|
+
responses: PROTOCOL_RESPONSES,
|
|
234
|
+
parameters: PROTOCOL_PARAMETERS,
|
|
235
|
+
headers: {
|
|
236
|
+
MessageId: { description: "Id of this request.", schema: { type: "string" } },
|
|
237
|
+
CorrelationId: { description: "Id shared by every message in this business flow.", schema: { type: "string" } },
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const PROTOCOL_SCHEMAS = {
|
|
243
|
+
ValidationIssue: {
|
|
244
|
+
type: "object",
|
|
245
|
+
required: ["path", "message"],
|
|
246
|
+
properties: {
|
|
247
|
+
path: { type: "array", items: { type: ["string", "integer"] } },
|
|
248
|
+
message: { type: "string" },
|
|
249
|
+
code: { type: "string" },
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
ProblemDetails: {
|
|
253
|
+
type: "object",
|
|
254
|
+
description: "RFC 9457 problem details. Used only for protocol-level failures, never for business outcomes.",
|
|
255
|
+
required: ["type", "title", "status"],
|
|
256
|
+
properties: {
|
|
257
|
+
type: { type: "string", format: "uri" },
|
|
258
|
+
title: { type: "string" },
|
|
259
|
+
status: { type: "integer" },
|
|
260
|
+
detail: { type: "string" },
|
|
261
|
+
issues: { type: "array", items: ref("schemas", "ValidationIssue") },
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
FailedBatchItem: {
|
|
265
|
+
type: "object",
|
|
266
|
+
required: ["status", "problem"],
|
|
267
|
+
properties: { status: { const: "failed" }, problem: ref("schemas", "ProblemDetails") },
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
const problemResponse = (description, headers) => ({
|
|
271
|
+
description,
|
|
272
|
+
...(headers === undefined ? {} : { headers }),
|
|
273
|
+
content: { "application/problem+json": { schema: ref("schemas", "ProblemDetails") } },
|
|
274
|
+
});
|
|
275
|
+
const PROTOCOL_RESPONSES = {
|
|
276
|
+
BadRequest: problemResponse("The body is not valid JSON or does not match the operation's input schema."),
|
|
277
|
+
UnsupportedMediaType: problemResponse("The body is not `application/json`."),
|
|
278
|
+
PayloadTooLarge: problemResponse("The batch has more items than the service accepts."),
|
|
279
|
+
Unauthenticated: problemResponse("The operation needs an authenticated caller and the request has no valid credentials."),
|
|
280
|
+
Forbidden: problemResponse("The caller lacks a permission the operation requires (see `x-projected-access`)."),
|
|
281
|
+
Conflict: problemResponse("Retry later: the command raced with another change to the same data (`concurrency-conflict`), or a request with the same idempotency key is still running (`request-in-progress`)."),
|
|
282
|
+
IdempotencyKeyReused: problemResponse("The idempotency key was already used for a different request. Use a new key."),
|
|
283
|
+
InternalError: problemResponse("A defect in the service. Details are logged, not returned."),
|
|
284
|
+
BadGateway: problemResponse("A dependency of the service answered in a way it cannot handle."),
|
|
285
|
+
ServiceUnavailable: problemResponse("A dependency of the service is unreachable. Retry later.", {
|
|
286
|
+
"retry-after": { description: "Seconds to wait before retrying.", schema: { type: "integer" } },
|
|
287
|
+
}),
|
|
288
|
+
};
|
|
289
|
+
const header = (name, description) => ({ name, in: "header", required: false, description, schema: { type: "string" } });
|
|
290
|
+
const PROTOCOL_PARAMETERS = {
|
|
291
|
+
MessageId: header(HEADERS.messageId, "Unique id of this request. Generated when absent."),
|
|
292
|
+
CorrelationId: header(HEADERS.correlationId, "Id shared by every message in one business flow. Defaults to the message id."),
|
|
293
|
+
CausationId: header(HEADERS.causationId, "Id of the message that caused this request."),
|
|
294
|
+
IdempotencyKey: header(HEADERS.idempotencyKey, "Caller-chosen key identifying this command across retries. Passed to the handler as `metadata.idempotencyKey`."),
|
|
295
|
+
};
|
|
296
|
+
//# sourceMappingURL=openapi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.js","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AA6B5E,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;AAC3G,MAAM,GAAG,GAAG,CAAC,OAAe,EAAE,IAAY,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,gBAAgB,OAAO,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;AAE7F;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,SAAuB,EAAE,UAA0B,EAAE;IACnF,MAAM,QAAQ,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC9D,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAChD,4EAA4E;IAC5E,MAAM,QAAQ,GAAG,CAAC,MAAc,EAA4B,EAAE;QAC5D,IAAI,eAAe,K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type { DispatchError, OperateError, OperationKind, ValidationIssue } from "@projected/application";
|
|
2
|
+
/**
|
|
3
|
+
* The HTTP binding of the projected protocol (see `spec/protocol.md`).
|
|
4
|
+
*
|
|
5
|
+
* Business outcomes — success or a declared business error — are always `200 OK` with an
|
|
6
|
+
* `OperationResult` body. HTTP status codes are reserved for the protocol itself: the request
|
|
7
|
+
* could not be understood, the operation does not exist, or the machinery failed. Those
|
|
8
|
+
* responses use RFC 9457 problem details.
|
|
9
|
+
*/
|
|
10
|
+
export declare const HEADERS: {
|
|
11
|
+
readonly messageId: "x-message-id";
|
|
12
|
+
readonly correlationId: "x-correlation-id";
|
|
13
|
+
readonly causationId: "x-causation-id";
|
|
14
|
+
readonly idempotencyKey: "idempotency-key";
|
|
15
|
+
readonly traceparent: "traceparent";
|
|
16
|
+
readonly tracestate: "tracestate";
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The headers a browser on another origin needs to be allowed: those a caller sends, and those it
|
|
20
|
+
* reads back. For the CORS layer in front of the handler — Hono's `cors()` in a generated service.
|
|
21
|
+
*/
|
|
22
|
+
export declare const CORS_HEADERS: {
|
|
23
|
+
readonly allow: readonly ["content-type", "authorization", "x-message-id", "x-correlation-id", "x-causation-id", "idempotency-key", "traceparent", "tracestate"];
|
|
24
|
+
readonly expose: readonly ["x-message-id", "x-correlation-id", "retry-after", "www-authenticate"];
|
|
25
|
+
};
|
|
26
|
+
export declare const PLURAL: Record<OperationKind, "commands" | "queries">;
|
|
27
|
+
/** `/commands/enlist-student/version/1` */
|
|
28
|
+
export declare function operationPath(kind: OperationKind, type: string, version: number): string;
|
|
29
|
+
/** `enlist-student`, 1 → `enlistStudentV1` */
|
|
30
|
+
export declare function operationId(type: string, version: number): string;
|
|
31
|
+
/** RFC 9457 problem details. */
|
|
32
|
+
export interface Problem {
|
|
33
|
+
readonly type: string;
|
|
34
|
+
readonly title: string;
|
|
35
|
+
readonly status: number;
|
|
36
|
+
readonly detail?: string;
|
|
37
|
+
readonly issues?: readonly ValidationIssue[];
|
|
38
|
+
}
|
|
39
|
+
/** Problem types are URNs so they are stable identifiers, not links that may rot. */
|
|
40
|
+
export declare const PROBLEM_TYPES: {
|
|
41
|
+
readonly notFound: "urn:projected:problem:not-found";
|
|
42
|
+
readonly methodNotAllowed: "urn:projected:problem:method-not-allowed";
|
|
43
|
+
readonly unsupportedMediaType: "urn:projected:problem:unsupported-media-type";
|
|
44
|
+
readonly malformedRequest: "urn:projected:problem:malformed-request";
|
|
45
|
+
readonly payloadTooLarge: "urn:projected:problem:payload-too-large";
|
|
46
|
+
readonly unknownOperation: "urn:projected:problem:unknown-operation";
|
|
47
|
+
readonly unknownTask: "urn:projected:problem:unknown-task";
|
|
48
|
+
readonly unauthenticated: "urn:projected:problem:unauthenticated";
|
|
49
|
+
readonly forbidden: "urn:projected:problem:forbidden";
|
|
50
|
+
readonly requestInProgress: "urn:projected:problem:request-in-progress";
|
|
51
|
+
readonly idempotencyKeyReused: "urn:projected:problem:idempotency-key-reused";
|
|
52
|
+
readonly invalidInput: "urn:projected:problem:invalid-input";
|
|
53
|
+
readonly concurrencyConflict: "urn:projected:problem:concurrency-conflict";
|
|
54
|
+
readonly dependencyUnavailable: "urn:projected:problem:dependency-unavailable";
|
|
55
|
+
readonly dependencyFailure: "urn:projected:problem:dependency-failure";
|
|
56
|
+
readonly internalError: "urn:projected:problem:internal-error";
|
|
57
|
+
};
|
|
58
|
+
export declare function problem(type: string, title: string, status: number, detail?: string): Problem;
|
|
59
|
+
/** The protocol-level response for a request that did not reach a business outcome. */
|
|
60
|
+
export declare function problemForDispatchError(error: DispatchError): Problem;
|
|
61
|
+
/** The protocol-level response for an operator task that did not run to completion. */
|
|
62
|
+
export declare function problemForOperateError(error: OperateError): Problem;
|
|
63
|
+
/**
|
|
64
|
+
* The protocol's answer to a body over the size limit, for a body-size layer in front of the
|
|
65
|
+
* handler — Hono's `bodyLimit()` in a generated service.
|
|
66
|
+
*/
|
|
67
|
+
export declare function payloadTooL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,aAAa,GAAG,OAAO,CAwBrE;AAED,uFAAuF;AACvF,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAInE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,CAK1D"}
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP binding of the projected protocol (see `spec/protocol.md`).
|
|
3
|
+
*
|
|
4
|
+
* Business outcomes — success or a declared business error — are always `200 OK` with an
|
|
5
|
+
* `OperationResult` body. HTTP status codes are reserved for the protocol itself: the request
|
|
6
|
+
* could not be understood, the operation does not exist, or the machinery failed. Those
|
|
7
|
+
* responses use RFC 9457 problem details.
|
|
8
|
+
*/
|
|
9
|
+
export const HEADERS = {
|
|
10
|
+
messageId: "x-message-id",
|
|
11
|
+
correlationId: "x-correlation-id",
|
|
12
|
+
causationId: "x-causation-id",
|
|
13
|
+
idempotencyKey: "idempotency-key",
|
|
14
|
+
traceparent: "traceparent",
|
|
15
|
+
tracestate: "tracestate",
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The headers a browser on another origin needs to be allowed: those a caller sends, and those it
|
|
19
|
+
* reads back. For the CORS layer in front of the handler — Hono's `cors()` in a generated service.
|
|
20
|
+
*/
|
|
21
|
+
export const CORS_HEADERS = {
|
|
22
|
+
allow: ["content-type", "authorization", HEADERS.messageId, HEADERS.correlationId, HEADERS.causationId, HEADERS.idempotencyKey, HEADERS.traceparent, HEADERS.tracestate],
|
|
23
|
+
expose: [HEADERS.messageId, HEADERS.correlationId, "retry-after", "www-authenticate"],
|
|
24
|
+
};
|
|
25
|
+
export const PLURAL = { command: "commands", query: "queries" };
|
|
26
|
+
/** `/commands/enlist-student/version/1` */
|
|
27
|
+
export function operationPath(kind, type, version) {
|
|
28
|
+
return `/${PLURAL[kind]}/${type}/version/${version}`;
|
|
29
|
+
}
|
|
30
|
+
/** `enlist-student`, 1 → `enlistStudentV1` */
|
|
31
|
+
export function operationId(type, version) {
|
|
32
|
+
return `${type.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase())}V${version}`;
|
|
33
|
+
}
|
|
34
|
+
/** Problem types are URNs so they are stable identifiers, not links that may rot. */
|
|
35
|
+
export const PROBLEM_TYPES = {
|
|
36
|
+
notFound: "urn:projected:problem:not-found",
|
|
37
|
+
methodNotAllowed: "urn:projected:problem:method-not-allowed",
|
|
38
|
+
unsupportedMediaType: "urn:projected:problem:unsupported-media-type",
|
|
39
|
+
malformedRequest: "urn:projected:problem:malformed-request",
|
|
40
|
+
payloadTooLarge: "urn:projected:problem:payload-too-large",
|
|
41
|
+
unknownOperation: "urn:projected:problem:unknown-operation",
|
|
42
|
+
unknownTask: "urn:projected:problem:unknown-task",
|
|
43
|
+
unauthenticated: "urn:projected:problem:unauthenticated",
|
|
44
|
+
forbidden: "urn:projected:problem:forbidden",
|
|
45
|
+
requestInProgress: "urn:projected:problem:request-in-progress",
|
|
46
|
+
idempotencyKeyReused: "urn:projected:problem:idempotency-key-reused",
|
|
47
|
+
invalidInput: "urn:projected:problem:invalid-input",
|
|
48
|
+
concurrencyConflict: "urn:projected:problem:concurrency-conflict",
|
|
49
|
+
dependencyUnavailable: "urn:projected:problem:dependency-unavailable",
|
|
50
|
+
dependencyFailure: "urn:projected:problem:dependency-failure",
|
|
51
|
+
internalError: "urn:projected:problem:internal-error",
|
|
52
|
+
};
|
|
53
|
+
export function problem(type, title, status, detail) {
|
|
54
|
+
return detail === undefined ? { type, title, status } : { type, title, status, detail };
|
|
55
|
+
}
|
|
56
|
+
/** The protocol-level response for a request that did not reach a business outcome. */
|
|
57
|
+
export function problemForDispatchError(error) {
|
|
58
|
+
switch (error._tag) {
|
|
59
|
+
case "UnknownOperation":
|
|
60
|
+
return problem(PROBLEM_TYPES.unknownOperation, "Unknown operation", 404, error.message);
|
|
61
|
+
case "InvalidInput":
|
|
62
|
+
return { ...problem(PROBLEM_TYPES.invalidInput, "Invalid input", 400, error.message), issues: error.issues };
|
|
63
|
+
case "Unauthenticated":
|
|
64
|
+
return problem(PROBLEM_TYPES.unauthenticated, "Authentication required", 401, error.message);
|
|
65
|
+
case "Forbidden":
|
|
66
|
+
return problem(PROBLEM_TYPES.forbidden, "Forbidden", 403, error.message);
|
|
67
|
+
case "RequestInProgress":
|
|
68
|
+
return problem(PROBLEM_TYPES.requestInProgress, "A request with this idempotency key is in progress", 409);
|
|
69
|
+
case "IdempotencyKeyReused":
|
|
70
|
+
return problem(PROBLEM_TYPES.idempotencyKeyReused, "Idempotency key reused for a different request", 422);
|
|
71
|
+
case "ConcurrencyConflict":
|
|
72
|
+
return problem(PROBLEM_TYPES.concurrencyConflict, "Concurrent modification, retry the request", 409);
|
|
73
|
+
case "DependencyUnavailable":
|
|
74
|
+
return problem(PROBLEM_TYPES.dependencyUnavailable, "Service temporarily unavailable", 503);
|
|
75
|
+
case "DependencyFailure":
|
|
76
|
+
return problem(PROBLEM_TYPES.dependencyFailure, "A dependency failed", 502);
|
|
77
|
+
case "OperationDefect":
|
|
78
|
+
// Defects are bugs: say nothing about internals.
|
|
79
|
+
return problem(PROBLEM_TYPES.internalError, "Internal error", 500);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** The protocol-level response for an operator task that did not run to completion. */
|
|
83
|
+
export function problemForOperateError(error) {
|
|
84
|
+
return error._tag === "UnknownTask"
|
|
85
|
+
? problem(PROBLEM_TYPES.unknownTask, "Unknown operator task", 404, error.message)
|
|
86
|
+
: problemForDispatchError(error);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The protocol's answer to a body over the size limit, for a body-size layer in front of the
|
|
90
|
+
* handler — Hono's `bodyLimit()` in a generated service.
|
|
91
|
+
*/
|
|
92
|
+
export function payloadTooLarge(maxBytes) {
|
|
93
|
+
return new Response(JSON.stringify(problem(PROBLEM_TYPES.payloadTooLarge, "Payload too large", 413, `At most ${maxBytes} bytes per request`)), {
|
|
94
|
+
status: 413,
|
|
95
|
+
headers: { "content-type": "application/problem+json" },
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,SAAS,EAAE,cAAc;IACzB,aAAa,EAAE,kBAAkB;IACjC,WAAW,EAAE,gBAAgB;IAC7B,cAAc,EAAE,iBAAiB;IACjC,WAAW,EAAE,aAAa;IAC1B,UAAU,EAAE,YAAY;CAChB,CAAC;AAEX;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,KAAK,EAAE,CAAC,cAAc,EAAE,eAAe,EAAE,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,UAAU,CAAC;IACxK,MAAM,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,aAAa,EAAE,aAAa,EAAE,kBAAkB,CAAC;CAC7E,CAAC;AAEX,MAAM,CAAC,MAAM,MAAM,GAAkD,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAE/G,2CAA2C;AAC3C,MAAM,UAAU,aAAa,CAAC,IAAmB,EAAE,IAAY,EAAE,OAAe;IAC9E,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,IAAI,YAAY,OAAO,EAAE,CAAC;AACvD,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,OAAe;IACvD,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC,EAAE,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,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