diff --git a/packages/core/test/sqlite-interrupt.test.ts b/packages/core/test/sqlite-interrupt.test.ts index 9242b08c252..30a7521c3fa 100644 --- a/packages/core/test/sqlite-interrupt.test.ts +++ b/packages/core/test/sqlite-interrupt.test.ts @@ -17,12 +17,17 @@ const sweep = ( Effect.runPromise( Effect.gen(function* () { const leaked: Array = [] - for (let delay = 0; delay < 31; delay++) { + for (let delay = 0; delay < 45; delay++) { yield* Effect.scoped( Effect.gen(function* () { const sql = yield* SqlClient.SqlClient const holder = yield* Effect.forkChild( - Effect.scoped(Effect.andThen(sql.reserve, Effect.andThen(Effect.yieldNow, Effect.yieldNow))), + Effect.scoped( + Effect.andThen( + sql.reserve, + Effect.forEach(Array.from({ length: 22 }), () => Effect.yieldNow, { discard: true }), + ), + ), ) const queued = yield* queue(sql).pipe( Effect.provideService(References.MaxOpsBeforeYield, 3), diff --git a/packages/protocol/script/openapi-stabilize.ts b/packages/protocol/script/openapi-stabilize.ts index c44044275ad..02873841bb9 100644 --- a/packages/protocol/script/openapi-stabilize.ts +++ b/packages/protocol/script/openapi-stabilize.ts @@ -1,140 +1,100 @@ +type Document = { components: { schemas: Record } } + +const prefix = "#/components/schemas/" +const byKey = ([left]: [string, unknown], [right]: [string, unknown]) => (left < right ? -1 : left > right ? 1 : 0) +const refName = (value: unknown) => + typeof value === "string" && value.startsWith(prefix) ? value.slice(prefix.length) : undefined + +const renameRefs = (node: unknown, rename: (name: string) => string): unknown => { + if (Array.isArray(node)) return node.map((item) => renameRefs(item, rename)) + if (typeof node !== "object" || node === null) return node + return Object.fromEntries( + Object.entries(node).map(([key, value]) => { + const name = key === "$ref" ? refName(value) : undefined + return [key, name === undefined ? renameRefs(value, rename) : prefix + rename(name)] + }), + ) +} + +// Sorted keys, with references to `inline` components replaced by their canonical content. +const canonical = ( + node: unknown, + inline: ReadonlyMap = new Map(), + seen = new Set(), +): unknown => { + if (Array.isArray(node)) return node.map((item) => canonical(item, inline, seen)) + if (typeof node !== "object" || node === null) return node + return Object.fromEntries( + Object.entries(node) + .sort(byKey) + .map(([key, value]) => { + const name = key === "$ref" ? refName(value) : undefined + if (name === undefined || !inline.has(name)) return [key, canonical(value, inline, seen)] + if (seen.has(name)) throw new Error(`Recursive anonymous OpenAPI component: ${name}`) + return [key, canonical(inline.get(name), inline, new Set([...seen, name]))] + }), + ) +} +const shape = (node: unknown, inline?: ReadonlyMap) => JSON.stringify(canonical(node, inline)) + // Effect gives shared anonymous schemas encounter-order names (`Union_3`), and does the same for the encoded side of a // named schema when it differs between uses (`Event.VersionEncoded_37`). Replace those names with a hash of their // canonical shape and sort components so unrelated additions stay local. export function stabilizeOpenApi(source: object) { - const document = source as { - components: { schemas: Record } - } - const schemas = document.components.schemas - const reference = (name: string) => `#/components/schemas/${name}` - const families = ["Union", "Objects", "Arrays"].filter((name) => `${name}_` in schemas) + const schemas = (source as Document).components.schemas + const families = ["Union_", "Objects_", "Arrays_"].filter((name) => name in schemas) + const encoded = new Set(Object.keys(schemas).flatMap((name) => /^(.+Encoded)_\d+$/.exec(name)?.[1] ?? [])) const anonymous = new Map( - Object.keys(schemas) - .filter( - (name) => families.some((family) => new RegExp(`^${family}_\\d*$`).test(name)) || /^.+Encoded_\d+$/.test(name), - ) - .map((name) => [name, schemas[name]]), + Object.entries(schemas).filter( + ([name]) => families.includes(name.replace(/\d+$/, "")) || encoded.has(name.replace(/_\d+$/, "")), + ), ) - - const canonical = (node: unknown, seen = new Set()): unknown => { - if (Array.isArray(node)) return node.map((item) => canonical(item, seen)) - if (typeof node !== "object" || node === null) return node - const $ref = "$ref" in node && typeof node.$ref === "string" ? node.$ref : undefined - const name = $ref?.startsWith(reference("")) ? $ref.slice(reference("").length) : undefined - if (name !== undefined && anonymous.has(name)) { - if (seen.has(name)) throw new Error(`Recursive anonymous OpenAPI component: ${name}`) - return Object.fromEntries([ - ["$ref", canonical(anonymous.get(name), new Set([...seen, name]))], - ...Object.entries(node) - .filter(([key]) => key !== "$ref") - .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)) - .map(([key, value]) => [key, canonical(value, seen)]), - ]) - } - return Object.fromEntries( - Object.entries(node) - .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)) - .map(([key, value]) => [key, canonical(value, seen)]), - ) - } const renames = new Map( [...anonymous].map(([name, schema]) => { - const hash = new Bun.CryptoHasher("sha256") - .update(JSON.stringify(canonical(schema))) - .digest("hex") - .slice(0, 12) + const hash = new Bun.CryptoHasher("sha256").update(shape(schema, anonymous)).digest("hex").slice(0, 12) return [name, `${name.replace(/_\d*$/, "")}_${hash}`] as const }), ) - const rewrite = (node: unknown): unknown => { - if (Array.isArray(node)) return node.map(rewrite) - if (typeof node !== "object" || node === null) return node - return Object.fromEntries( - Object.entries(node).map(([key, value]) => { - if (key !== "$ref" || typeof value !== "string" || !value.startsWith(reference(""))) { - return [key, rewrite(value)] - } - const name = value.slice(reference("").length) - return [key, reference(renames.get(name) ?? name)] - }), - ) - } - const result = rewrite(document) as typeof document + const result = renameRefs(source, (name) => renames.get(name) ?? name) as Document const stable = new Map() for (const [name, schema] of Object.entries(result.components.schemas)) { const target = renames.get(name) ?? name const previous = stable.get(target) - if (previous !== undefined && JSON.stringify(canonical(previous)) !== JSON.stringify(canonical(schema))) { + if (previous !== undefined && shape(previous) !== shape(schema)) { throw new Error(`Content-addressed OpenAPI component collision: ${target}`) } stable.set(target, schema) } - result.components.schemas = Object.fromEntries( - [...stable].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)), - ) + result.components.schemas = Object.fromEntries([...stable].sort(byKey)) return collapseCopies(result) } // Effect names a second AST copy of an identified schema `_`; event streams reach their schemas without the // JSON codec that REST bodies get. Fold a copy into `` only when the two are deeply equal, treating a reference -// to another copy as equal to its original only when that copy folds too. Any real difference keeps its own name. -function collapseCopies } }>(document: Document) { +// to a copy as a reference to its original only while that copy still folds. Any real difference keeps its own name. +function collapseCopies(document: Document) { const schemas = document.components.schemas - const prefix = "#/components/schemas/" const copies = new Map() for (const name of Object.keys(schemas)) { const original = /^(.+)_\d+$/.exec(name)?.[1] if (original !== undefined && Object.hasOwn(schemas, original)) copies.set(name, original) } - const sameReference = (left: string, right: string) => { - if (left === right) return true - if (!left.startsWith(prefix) || !right.startsWith(prefix)) return false - const a = left.slice(prefix.length) - const b = right.slice(prefix.length) - return copies.get(a) === b || copies.get(b) === a - } - const equal = (left: unknown, right: unknown): boolean => { - if (Array.isArray(left) || Array.isArray(right)) { - return ( - Array.isArray(left) && - Array.isArray(right) && - left.length === right.length && - left.every((item, index) => equal(item, right[index])) - ) - } - if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) return left === right - const entries = new Map(Object.entries(left)) - const others = new Map(Object.entries(right)) - if (entries.size !== others.size) return false - return [...entries].every(([key, a]) => { - if (!others.has(key)) return false - const b = others.get(key) - if (key === "$ref" && typeof a === "string" && typeof b === "string") return sameReference(a, b) - return equal(a, b) - }) + const resolve = (name: string): string => { + const original = copies.get(name) + return original === undefined ? name : resolve(original) } + const folded = (name: string) => shape(renameRefs(schemas[name], resolve)) let changed = true while (changed) { changed = false for (const [copy, original] of copies) { - if (equal(schemas[copy], schemas[original])) continue + if (folded(copy) === folded(original)) continue copies.delete(copy) changed = true } } if (copies.size === 0) return document - const rewrite = (node: unknown): unknown => { - if (Array.isArray(node)) return node.map(rewrite) - if (typeof node !== "object" || node === null) return node - return Object.fromEntries( - Object.entries(node).map(([key, value]) => { - if (key !== "$ref" || typeof value !== "string" || !value.startsWith(prefix)) return [key, rewrite(value)] - const name = value.slice(prefix.length) - return [key, `${prefix}${copies.get(name) ?? name}`] - }), - ) - } - const result = rewrite(document) as Document + const result = renameRefs(document, resolve) as Document result.components.schemas = Object.fromEntries( Object.entries(result.components.schemas).filter(([name]) => !copies.has(name)), ) diff --git a/packages/protocol/test/openapi-stabilize.test.ts b/packages/protocol/test/openapi-stabilize.test.ts index 5fcb26c5a7a..759904f1fee 100644 --- a/packages/protocol/test/openapi-stabilize.test.ts +++ b/packages/protocol/test/openapi-stabilize.test.ts @@ -128,3 +128,34 @@ test("preserves authored synthetic-looking names without an anonymous family roo expect(result.components.schemas.Union_2).toEqual({ type: "string" }) }) + +test("folds a chain of copies onto the original without leaving a dangling reference", () => { + const result = stabilizeOpenApi({ + components: { + schemas: { + OAuth: { type: "string" }, + OAuth_2: { type: "string" }, + OAuth_2_1: { type: "string" }, + Holder: { type: "array", items: [{ $ref: "#/components/schemas/OAuth_2_1" }] }, + }, + }, + }) as { components: { schemas: Record }> } } + + expect(Object.keys(result.components.schemas).sort()).toEqual(["Holder", "OAuth"]) + expect(result.components.schemas.Holder?.items?.[0]?.$ref).toBe("#/components/schemas/OAuth") +}) + +test("names an unsuffixed encoded variant by content when suffixed siblings exist", () => { + const generate = (versions: Array) => { + const schemas: Record = {} + versions.forEach((version, index) => { + schemas[index === 0 ? "Event.VersionEncoded" : `Event.VersionEncoded_${index}`] = { enum: [version] } + }) + return stabilizeOpenApi({ components: { schemas } }) as { components: { schemas: Record } } + } + + const forward = generate([2, 1]).components.schemas + const reversed = generate([1, 2]).components.schemas + expect(Object.keys(forward)).not.toContain("Event.VersionEncoded") + expect(forward).toEqual(reversed) +})