@zyro-sec/protocol 1.0.1 → 1.0.3
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/openapi.json +33222 -33222
- package/package.json +6 -2
- package/script/build.ts +9 -9
- package/script/generate-openapi.ts +25 -25
- package/script/openapi-parameters.ts +30 -30
- package/script/openapi-stabilize.ts +102 -102
- package/script/pack.ts +5 -5
- package/src/api.ts +264 -264
- package/src/client.ts +127 -127
- package/src/errors.ts +398 -398
- package/src/groups/agent.ts +39 -39
- package/src/groups/artifact.ts +103 -103
- package/src/groups/automation.ts +75 -75
- package/src/groups/blob.ts +20 -20
- package/src/groups/command.ts +27 -27
- package/src/groups/config.ts +46 -46
- package/src/groups/credential.ts +72 -72
- package/src/groups/debug.ts +68 -68
- package/src/groups/event.ts +86 -86
- package/src/groups/experimental.ts +19 -19
- package/src/groups/form.ts +26 -26
- package/src/groups/fs.ts +136 -136
- package/src/groups/generate.ts +120 -120
- package/src/groups/integration.ts +230 -230
- package/src/groups/job.ts +57 -57
- package/src/groups/location.ts +66 -66
- package/src/groups/machine.ts +120 -120
- package/src/groups/mcp.ts +167 -167
- package/src/groups/media.ts +23 -23
- package/src/groups/message.ts +124 -124
- package/src/groups/migration.ts +28 -28
- package/src/groups/model.ts +52 -52
- package/src/groups/permission.ts +164 -164
- package/src/groups/persistent-pty.ts +153 -153
- package/src/groups/plugin.ts +80 -80
- package/src/groups/project.ts +51 -51
- package/src/groups/provider.ts +45 -45
- package/src/groups/pty.ts +153 -153
- package/src/groups/push.ts +94 -94
- package/src/groups/reference.ts +42 -42
- package/src/groups/rpc.ts +30 -30
- package/src/groups/server.ts +80 -80
- package/src/groups/session.ts +918 -918
- package/src/groups/shell.ts +88 -88
- package/src/groups/skill.ts +46 -46
- package/src/groups/usage.ts +30 -30
- package/src/groups/vcs.ts +131 -131
- package/src/groups/voice.ts +96 -96
- package/src/groups/websearch.ts +46 -46
- package/src/groups/worktree.ts +78 -78
- package/src/middleware/authorization.ts +6 -6
- package/src/middleware/schema-error.ts +7 -7
- package/src/simulation.ts +691 -691
- package/tsconfig.build.json +11 -11
- package/tsconfig.json +14 -14
- package/test/event.test.ts +0 -34
- package/test/experimental-identifiers.test.ts +0 -24
- package/test/openapi-parameters.test.ts +0 -43
- package/test/openapi-patterns.test.ts +0 -28
- package/test/openapi-stabilize.test.ts +0 -161
- package/test/session-cursor.test.ts +0 -7
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@zyro-sec/protocol",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
|
+
"exports": {
|
|
16
|
+
"./simulation": "./src/simulation.ts",
|
|
17
|
+
"./*": "./src/*.ts"
|
|
18
|
+
},
|
|
15
19
|
"scripts": {
|
|
16
20
|
"build": "bun run script/build.ts",
|
|
17
21
|
"pack": "bun run script/pack.ts",
|
|
@@ -22,7 +26,7 @@
|
|
|
22
26
|
"typecheck:tsc": "tsc --noEmit"
|
|
23
27
|
},
|
|
24
28
|
"dependencies": {
|
|
25
|
-
"@zyro-sec/schema": "^1.0.
|
|
29
|
+
"@zyro-sec/schema": "^1.0.3",
|
|
26
30
|
"effect": "4.1.0"
|
|
27
31
|
},
|
|
28
32
|
"devDependencies": {
|
package/script/build.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
import { $ } from "bun"
|
|
4
|
-
import { fileURLToPath } from "node:url"
|
|
5
|
-
|
|
6
|
-
process.chdir(fileURLToPath(new URL("..", import.meta.url)))
|
|
7
|
-
|
|
8
|
-
await $`rm -rf dist`
|
|
9
|
-
await $`bun tsc -p tsconfig.build.json`
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { $ } from "bun"
|
|
4
|
+
import { fileURLToPath } from "node:url"
|
|
5
|
+
|
|
6
|
+
process.chdir(fileURLToPath(new URL("..", import.meta.url)))
|
|
7
|
+
|
|
8
|
+
await $`rm -rf dist`
|
|
9
|
+
await $`bun tsc -p tsconfig.build.json`
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { OpenApi } from "effect/http-api"
|
|
2
|
-
import { format } from "prettier"
|
|
3
|
-
import { fileURLToPath } from "url"
|
|
4
|
-
import { ClientApi } from "../src/client.js"
|
|
5
|
-
import { liftParameterDescriptions } from "./openapi-parameters.js"
|
|
6
|
-
import { stabilizeOpenApi } from "./openapi-stabilize.js"
|
|
7
|
-
|
|
8
|
-
const document = await format(
|
|
9
|
-
JSON.stringify(liftParameterDescriptions(stabilizeOpenApi(OpenApi.fromApi(ClientApi))), null, 2),
|
|
10
|
-
{
|
|
11
|
-
parser: "json",
|
|
12
|
-
printWidth: 120,
|
|
13
|
-
},
|
|
14
|
-
)
|
|
15
|
-
const target = fileURLToPath(new URL("../openapi.json", import.meta.url))
|
|
16
|
-
|
|
17
|
-
if (process.argv.includes("--check")) {
|
|
18
|
-
if ((await Bun.file(target).text()) !== document) {
|
|
19
|
-
console.error("Generated OpenAPI document is stale. Run `bun run generate` from packages/protocol.")
|
|
20
|
-
process.exit(1)
|
|
21
|
-
}
|
|
22
|
-
process.exit(0)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
await Bun.write(target, document)
|
|
1
|
+
import { OpenApi } from "effect/http-api"
|
|
2
|
+
import { format } from "prettier"
|
|
3
|
+
import { fileURLToPath } from "url"
|
|
4
|
+
import { ClientApi } from "../src/client.js"
|
|
5
|
+
import { liftParameterDescriptions } from "./openapi-parameters.js"
|
|
6
|
+
import { stabilizeOpenApi } from "./openapi-stabilize.js"
|
|
7
|
+
|
|
8
|
+
const document = await format(
|
|
9
|
+
JSON.stringify(liftParameterDescriptions(stabilizeOpenApi(OpenApi.fromApi(ClientApi))), null, 2),
|
|
10
|
+
{
|
|
11
|
+
parser: "json",
|
|
12
|
+
printWidth: 120,
|
|
13
|
+
},
|
|
14
|
+
)
|
|
15
|
+
const target = fileURLToPath(new URL("../openapi.json", import.meta.url))
|
|
16
|
+
|
|
17
|
+
if (process.argv.includes("--check")) {
|
|
18
|
+
if ((await Bun.file(target).text()) !== document) {
|
|
19
|
+
console.error("Generated OpenAPI document is stale. Run `bun run generate` from packages/protocol.")
|
|
20
|
+
process.exit(1)
|
|
21
|
+
}
|
|
22
|
+
process.exit(0)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
await Bun.write(target, document)
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
type Schema = { description?: string; allOf?: Array<Record<string, unknown>>; [key: string]: unknown }
|
|
2
|
-
type Parameter = { description?: string; schema?: Schema; [key: string]: unknown }
|
|
3
|
-
type Document = { paths?: Record<string, Record<string, { parameters?: Array<Parameter> }>> }
|
|
4
|
-
|
|
5
|
-
// Effect writes a parameter's description into its schema: `.annotate` sets `schema.description`, and `.annotateKey`
|
|
6
|
-
// (used where annotating would rename a shared component) adds a `{ description }` member to `schema.allOf`. OpenAPI
|
|
7
|
-
// documents parameters by `parameters[].description`, so move it there.
|
|
8
|
-
export function liftParameterDescriptions<T extends object>(document: T): T {
|
|
9
|
-
for (const operations of Object.values((document as Document).paths ?? {}))
|
|
10
|
-
for (const operation of Object.values(operations))
|
|
11
|
-
operation.parameters = operation.parameters?.map((parameter) => {
|
|
12
|
-
if (parameter.schema === undefined || parameter.description !== undefined) return parameter
|
|
13
|
-
const description = parameter.schema.allOf?.find(isDescription)?.description ?? parameter.schema.description
|
|
14
|
-
if (description === undefined) return parameter
|
|
15
|
-
return { ...parameter, description, schema: withoutDescription(parameter.schema) }
|
|
16
|
-
})
|
|
17
|
-
return document
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function isDescription(member: Record<string, unknown>): member is { description: string } {
|
|
21
|
-
return Object.keys(member).length === 1 && typeof member.description === "string"
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function withoutDescription(schema: Schema): Schema {
|
|
25
|
-
const rest = Object.fromEntries(Object.entries(schema).filter(([key]) => key !== "description" && key !== "allOf"))
|
|
26
|
-
const allOf = schema.allOf?.filter((member) => !isDescription(member)) ?? []
|
|
27
|
-
if (allOf.length === 0) return rest
|
|
28
|
-
if (allOf.length === 1 && Object.keys(rest).length === 0) return allOf[0]
|
|
29
|
-
return { ...rest, allOf }
|
|
30
|
-
}
|
|
1
|
+
type Schema = { description?: string; allOf?: Array<Record<string, unknown>>; [key: string]: unknown }
|
|
2
|
+
type Parameter = { description?: string; schema?: Schema; [key: string]: unknown }
|
|
3
|
+
type Document = { paths?: Record<string, Record<string, { parameters?: Array<Parameter> }>> }
|
|
4
|
+
|
|
5
|
+
// Effect writes a parameter's description into its schema: `.annotate` sets `schema.description`, and `.annotateKey`
|
|
6
|
+
// (used where annotating would rename a shared component) adds a `{ description }` member to `schema.allOf`. OpenAPI
|
|
7
|
+
// documents parameters by `parameters[].description`, so move it there.
|
|
8
|
+
export function liftParameterDescriptions<T extends object>(document: T): T {
|
|
9
|
+
for (const operations of Object.values((document as Document).paths ?? {}))
|
|
10
|
+
for (const operation of Object.values(operations))
|
|
11
|
+
operation.parameters = operation.parameters?.map((parameter) => {
|
|
12
|
+
if (parameter.schema === undefined || parameter.description !== undefined) return parameter
|
|
13
|
+
const description = parameter.schema.allOf?.find(isDescription)?.description ?? parameter.schema.description
|
|
14
|
+
if (description === undefined) return parameter
|
|
15
|
+
return { ...parameter, description, schema: withoutDescription(parameter.schema) }
|
|
16
|
+
})
|
|
17
|
+
return document
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isDescription(member: Record<string, unknown>): member is { description: string } {
|
|
21
|
+
return Object.keys(member).length === 1 && typeof member.description === "string"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function withoutDescription(schema: Schema): Schema {
|
|
25
|
+
const rest = Object.fromEntries(Object.entries(schema).filter(([key]) => key !== "description" && key !== "allOf"))
|
|
26
|
+
const allOf = schema.allOf?.filter((member) => !isDescription(member)) ?? []
|
|
27
|
+
if (allOf.length === 0) return rest
|
|
28
|
+
if (allOf.length === 1 && Object.keys(rest).length === 0) return allOf[0]
|
|
29
|
+
return { ...rest, allOf }
|
|
30
|
+
}
|
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
type Document = { components: { schemas: Record<string, unknown> } }
|
|
2
|
-
|
|
3
|
-
const prefix = "#/components/schemas/"
|
|
4
|
-
const byKey = ([left]: [string, unknown], [right]: [string, unknown]) => (left < right ? -1 : left > right ? 1 : 0)
|
|
5
|
-
const refName = (value: unknown) =>
|
|
6
|
-
typeof value === "string" && value.startsWith(prefix) ? value.slice(prefix.length) : undefined
|
|
7
|
-
|
|
8
|
-
const renameRefs = (node: unknown, rename: (name: string) => string): unknown => {
|
|
9
|
-
if (Array.isArray(node)) return node.map((item) => renameRefs(item, rename))
|
|
10
|
-
if (typeof node !== "object" || node === null) return node
|
|
11
|
-
return Object.fromEntries(
|
|
12
|
-
Object.entries(node).map(([key, value]) => {
|
|
13
|
-
const name = key === "$ref" ? refName(value) : undefined
|
|
14
|
-
return [key, name === undefined ? renameRefs(value, rename) : prefix + rename(name)]
|
|
15
|
-
}),
|
|
16
|
-
)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Sorted keys, with references to `inline` components replaced by their canonical content.
|
|
20
|
-
const canonical = (
|
|
21
|
-
node: unknown,
|
|
22
|
-
inline: ReadonlyMap<string, unknown> = new Map(),
|
|
23
|
-
seen = new Set<string>(),
|
|
24
|
-
): unknown => {
|
|
25
|
-
if (Array.isArray(node)) return node.map((item) => canonical(item, inline, seen))
|
|
26
|
-
if (typeof node !== "object" || node === null) return node
|
|
27
|
-
return Object.fromEntries(
|
|
28
|
-
Object.entries(node)
|
|
29
|
-
.sort(byKey)
|
|
30
|
-
.map(([key, value]) => {
|
|
31
|
-
const name = key === "$ref" ? refName(value) : undefined
|
|
32
|
-
if (name === undefined || !inline.has(name)) return [key, canonical(value, inline, seen)]
|
|
33
|
-
if (seen.has(name)) throw new Error(`Recursive anonymous OpenAPI component: ${name}`)
|
|
34
|
-
return [key, canonical(inline.get(name), inline, new Set([...seen, name]))]
|
|
35
|
-
}),
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
const shape = (node: unknown, inline?: ReadonlyMap<string, unknown>) => JSON.stringify(canonical(node, inline))
|
|
39
|
-
|
|
40
|
-
// Effect gives shared anonymous schemas encounter-order names (`Union_3`), and does the same for the encoded side of a
|
|
41
|
-
// named schema when it differs between uses (`Event.VersionEncoded_37`). Replace those names with a hash of their
|
|
42
|
-
// canonical shape and sort components so unrelated additions stay local.
|
|
43
|
-
export function stabilizeOpenApi(source: object) {
|
|
44
|
-
const schemas = (source as Document).components.schemas
|
|
45
|
-
const families = ["Union_", "Objects_", "Arrays_"].filter((name) => name in schemas)
|
|
46
|
-
const encoded = new Set(Object.keys(schemas).flatMap((name) => /^(.+Encoded)_\d+$/.exec(name)?.[1] ?? []))
|
|
47
|
-
const anonymous = new Map(
|
|
48
|
-
Object.entries(schemas).filter(
|
|
49
|
-
([name]) => families.includes(name.replace(/\d+$/, "")) || encoded.has(name.replace(/_\d+$/, "")),
|
|
50
|
-
),
|
|
51
|
-
)
|
|
52
|
-
const renames = new Map(
|
|
53
|
-
[...anonymous].map(([name, schema]) => {
|
|
54
|
-
const hash = new Bun.CryptoHasher("sha256").update(shape(schema, anonymous)).digest("hex").slice(0, 12)
|
|
55
|
-
return [name, `${name.replace(/_\d*$/, "")}_${hash}`] as const
|
|
56
|
-
}),
|
|
57
|
-
)
|
|
58
|
-
const result = renameRefs(source, (name) => renames.get(name) ?? name) as Document
|
|
59
|
-
const stable = new Map<string, unknown>()
|
|
60
|
-
for (const [name, schema] of Object.entries(result.components.schemas)) {
|
|
61
|
-
const target = renames.get(name) ?? name
|
|
62
|
-
const previous = stable.get(target)
|
|
63
|
-
if (previous !== undefined && shape(previous) !== shape(schema)) {
|
|
64
|
-
throw new Error(`Content-addressed OpenAPI component collision: ${target}`)
|
|
65
|
-
}
|
|
66
|
-
stable.set(target, schema)
|
|
67
|
-
}
|
|
68
|
-
result.components.schemas = Object.fromEntries([...stable].sort(byKey))
|
|
69
|
-
return collapseCopies(result)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Effect names a second AST copy of an identified schema `<Name>_<N>`; event streams reach their schemas without the
|
|
73
|
-
// JSON codec that REST bodies get. Fold a copy into `<Name>` only when the two are deeply equal, treating a reference
|
|
74
|
-
// to a copy as a reference to its original only while that copy still folds. Any real difference keeps its own name.
|
|
75
|
-
function collapseCopies(document: Document) {
|
|
76
|
-
const schemas = document.components.schemas
|
|
77
|
-
const copies = new Map<string, string>()
|
|
78
|
-
for (const name of Object.keys(schemas)) {
|
|
79
|
-
const original = /^(.+)_\d+$/.exec(name)?.[1]
|
|
80
|
-
if (original !== undefined && Object.hasOwn(schemas, original)) copies.set(name, original)
|
|
81
|
-
}
|
|
82
|
-
const resolve = (name: string): string => {
|
|
83
|
-
const original = copies.get(name)
|
|
84
|
-
return original === undefined ? name : resolve(original)
|
|
85
|
-
}
|
|
86
|
-
const folded = (name: string) => shape(renameRefs(schemas[name], resolve))
|
|
87
|
-
let changed = true
|
|
88
|
-
while (changed) {
|
|
89
|
-
changed = false
|
|
90
|
-
for (const [copy, original] of copies) {
|
|
91
|
-
if (folded(copy) === folded(original)) continue
|
|
92
|
-
copies.delete(copy)
|
|
93
|
-
changed = true
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (copies.size === 0) return document
|
|
97
|
-
const result = renameRefs(document, resolve) as Document
|
|
98
|
-
result.components.schemas = Object.fromEntries(
|
|
99
|
-
Object.entries(result.components.schemas).filter(([name]) => !copies.has(name)),
|
|
100
|
-
)
|
|
101
|
-
return result
|
|
102
|
-
}
|
|
1
|
+
type Document = { components: { schemas: Record<string, unknown> } }
|
|
2
|
+
|
|
3
|
+
const prefix = "#/components/schemas/"
|
|
4
|
+
const byKey = ([left]: [string, unknown], [right]: [string, unknown]) => (left < right ? -1 : left > right ? 1 : 0)
|
|
5
|
+
const refName = (value: unknown) =>
|
|
6
|
+
typeof value === "string" && value.startsWith(prefix) ? value.slice(prefix.length) : undefined
|
|
7
|
+
|
|
8
|
+
const renameRefs = (node: unknown, rename: (name: string) => string): unknown => {
|
|
9
|
+
if (Array.isArray(node)) return node.map((item) => renameRefs(item, rename))
|
|
10
|
+
if (typeof node !== "object" || node === null) return node
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(node).map(([key, value]) => {
|
|
13
|
+
const name = key === "$ref" ? refName(value) : undefined
|
|
14
|
+
return [key, name === undefined ? renameRefs(value, rename) : prefix + rename(name)]
|
|
15
|
+
}),
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Sorted keys, with references to `inline` components replaced by their canonical content.
|
|
20
|
+
const canonical = (
|
|
21
|
+
node: unknown,
|
|
22
|
+
inline: ReadonlyMap<string, unknown> = new Map(),
|
|
23
|
+
seen = new Set<string>(),
|
|
24
|
+
): unknown => {
|
|
25
|
+
if (Array.isArray(node)) return node.map((item) => canonical(item, inline, seen))
|
|
26
|
+
if (typeof node !== "object" || node === null) return node
|
|
27
|
+
return Object.fromEntries(
|
|
28
|
+
Object.entries(node)
|
|
29
|
+
.sort(byKey)
|
|
30
|
+
.map(([key, value]) => {
|
|
31
|
+
const name = key === "$ref" ? refName(value) : undefined
|
|
32
|
+
if (name === undefined || !inline.has(name)) return [key, canonical(value, inline, seen)]
|
|
33
|
+
if (seen.has(name)) throw new Error(`Recursive anonymous OpenAPI component: ${name}`)
|
|
34
|
+
return [key, canonical(inline.get(name), inline, new Set([...seen, name]))]
|
|
35
|
+
}),
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
const shape = (node: unknown, inline?: ReadonlyMap<string, unknown>) => JSON.stringify(canonical(node, inline))
|
|
39
|
+
|
|
40
|
+
// Effect gives shared anonymous schemas encounter-order names (`Union_3`), and does the same for the encoded side of a
|
|
41
|
+
// named schema when it differs between uses (`Event.VersionEncoded_37`). Replace those names with a hash of their
|
|
42
|
+
// canonical shape and sort components so unrelated additions stay local.
|
|
43
|
+
export function stabilizeOpenApi(source: object) {
|
|
44
|
+
const schemas = (source as Document).components.schemas
|
|
45
|
+
const families = ["Union_", "Objects_", "Arrays_"].filter((name) => name in schemas)
|
|
46
|
+
const encoded = new Set(Object.keys(schemas).flatMap((name) => /^(.+Encoded)_\d+$/.exec(name)?.[1] ?? []))
|
|
47
|
+
const anonymous = new Map(
|
|
48
|
+
Object.entries(schemas).filter(
|
|
49
|
+
([name]) => families.includes(name.replace(/\d+$/, "")) || encoded.has(name.replace(/_\d+$/, "")),
|
|
50
|
+
),
|
|
51
|
+
)
|
|
52
|
+
const renames = new Map(
|
|
53
|
+
[...anonymous].map(([name, schema]) => {
|
|
54
|
+
const hash = new Bun.CryptoHasher("sha256").update(shape(schema, anonymous)).digest("hex").slice(0, 12)
|
|
55
|
+
return [name, `${name.replace(/_\d*$/, "")}_${hash}`] as const
|
|
56
|
+
}),
|
|
57
|
+
)
|
|
58
|
+
const result = renameRefs(source, (name) => renames.get(name) ?? name) as Document
|
|
59
|
+
const stable = new Map<string, unknown>()
|
|
60
|
+
for (const [name, schema] of Object.entries(result.components.schemas)) {
|
|
61
|
+
const target = renames.get(name) ?? name
|
|
62
|
+
const previous = stable.get(target)
|
|
63
|
+
if (previous !== undefined && shape(previous) !== shape(schema)) {
|
|
64
|
+
throw new Error(`Content-addressed OpenAPI component collision: ${target}`)
|
|
65
|
+
}
|
|
66
|
+
stable.set(target, schema)
|
|
67
|
+
}
|
|
68
|
+
result.components.schemas = Object.fromEntries([...stable].sort(byKey))
|
|
69
|
+
return collapseCopies(result)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Effect names a second AST copy of an identified schema `<Name>_<N>`; event streams reach their schemas without the
|
|
73
|
+
// JSON codec that REST bodies get. Fold a copy into `<Name>` only when the two are deeply equal, treating a reference
|
|
74
|
+
// to a copy as a reference to its original only while that copy still folds. Any real difference keeps its own name.
|
|
75
|
+
function collapseCopies(document: Document) {
|
|
76
|
+
const schemas = document.components.schemas
|
|
77
|
+
const copies = new Map<string, string>()
|
|
78
|
+
for (const name of Object.keys(schemas)) {
|
|
79
|
+
const original = /^(.+)_\d+$/.exec(name)?.[1]
|
|
80
|
+
if (original !== undefined && Object.hasOwn(schemas, original)) copies.set(name, original)
|
|
81
|
+
}
|
|
82
|
+
const resolve = (name: string): string => {
|
|
83
|
+
const original = copies.get(name)
|
|
84
|
+
return original === undefined ? name : resolve(original)
|
|
85
|
+
}
|
|
86
|
+
const folded = (name: string) => shape(renameRefs(schemas[name], resolve))
|
|
87
|
+
let changed = true
|
|
88
|
+
while (changed) {
|
|
89
|
+
changed = false
|
|
90
|
+
for (const [copy, original] of copies) {
|
|
91
|
+
if (folded(copy) === folded(original)) continue
|
|
92
|
+
copies.delete(copy)
|
|
93
|
+
changed = true
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (copies.size === 0) return document
|
|
97
|
+
const result = renameRefs(document, resolve) as Document
|
|
98
|
+
result.components.schemas = Object.fromEntries(
|
|
99
|
+
Object.entries(result.components.schemas).filter(([name]) => !copies.has(name)),
|
|
100
|
+
)
|
|
101
|
+
return result
|
|
102
|
+
}
|
package/script/pack.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import path from "node:path"
|
|
3
|
-
import { pack } from "@zyro-sec/script/pack"
|
|
4
|
-
|
|
5
|
-
await pack(path.resolve(import.meta.dir, ".."))
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { pack } from "@zyro-sec/script/pack"
|
|
4
|
+
|
|
5
|
+
await pack(path.resolve(import.meta.dir, ".."))
|