@distilled.cloud/core 1.0.0-rc.13 → 1.0.0-rc.14
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/lib/api.d.ts +4 -4
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +3 -5
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +3 -5
- package/lib/category.js.map +1 -1
- package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
- package/lib/codegen/boolean-string-enums.js +2 -6
- package/lib/codegen/boolean-string-enums.js.map +1 -1
- package/lib/codegen/boolean-string-enums.test.js +3 -5
- package/lib/codegen/boolean-string-enums.test.js.map +1 -1
- package/lib/codegen/cli.d.ts +5 -4
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +12 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/defer-naming.test.d.ts +2 -0
- package/lib/codegen/defer-naming.test.d.ts.map +1 -0
- package/lib/codegen/defer-naming.test.js +105 -0
- package/lib/codegen/defer-naming.test.js.map +1 -0
- package/lib/codegen/emit.d.ts.map +1 -1
- package/lib/codegen/emit.js +1 -3
- package/lib/codegen/emit.js.map +1 -1
- package/lib/codegen/format.d.ts +0 -12
- package/lib/codegen/format.d.ts.map +1 -1
- package/lib/codegen/format.js +10 -6
- package/lib/codegen/format.js.map +1 -1
- package/lib/codegen/generated-at.d.ts +29 -0
- package/lib/codegen/generated-at.d.ts.map +1 -0
- package/lib/codegen/generated-at.js +78 -0
- package/lib/codegen/generated-at.js.map +1 -0
- package/lib/codegen/generated-at.test.d.ts +2 -0
- package/lib/codegen/generated-at.test.d.ts.map +1 -0
- package/lib/codegen/generated-at.test.js +81 -0
- package/lib/codegen/generated-at.test.js.map +1 -0
- package/lib/codegen/generator.d.ts +4 -2
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +37 -75
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/generator.test.js +4 -9
- package/lib/codegen/generator.test.js.map +1 -1
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +12 -33
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql-client.test.js +4 -12
- package/lib/codegen/graphql-client.test.js.map +1 -1
- package/lib/codegen/graphql.d.ts.map +1 -1
- package/lib/codegen/graphql.js +7 -20
- package/lib/codegen/graphql.js.map +1 -1
- package/lib/codegen/model-diff.d.ts +10 -0
- package/lib/codegen/model-diff.d.ts.map +1 -0
- package/lib/codegen/model-diff.js +67 -0
- package/lib/codegen/model-diff.js.map +1 -0
- package/lib/codegen/naming.d.ts.map +1 -1
- package/lib/codegen/naming.js.map +1 -1
- package/lib/codegen/openapi-binary.test.js +1 -1
- package/lib/codegen/openapi-binary.test.js.map +1 -1
- package/lib/codegen/openapi-cli.d.ts +11 -11
- package/lib/codegen/openapi-cli.d.ts.map +1 -1
- package/lib/codegen/openapi-cli.js +26 -51
- package/lib/codegen/openapi-cli.js.map +1 -1
- package/lib/codegen/openapi.d.ts +9 -1
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +48 -42
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.d.ts.map +1 -1
- package/lib/codegen/operations.js.map +1 -1
- package/lib/codegen/pagination.d.ts.map +1 -1
- package/lib/codegen/pagination.js +2 -4
- package/lib/codegen/pagination.js.map +1 -1
- package/lib/codegen/patch-audit.d.ts +84 -0
- package/lib/codegen/patch-audit.d.ts.map +1 -0
- package/lib/codegen/patch-audit.js +366 -0
- package/lib/codegen/patch-audit.js.map +1 -0
- package/lib/codegen/patch-audit.test.d.ts +2 -0
- package/lib/codegen/patch-audit.test.d.ts.map +1 -0
- package/lib/codegen/patch-audit.test.js +141 -0
- package/lib/codegen/patch-audit.test.js.map +1 -0
- package/lib/codegen/patches.d.ts +59 -10
- package/lib/codegen/patches.d.ts.map +1 -1
- package/lib/codegen/patches.js +233 -63
- package/lib/codegen/patches.js.map +1 -1
- package/lib/codegen/patches.test.js +8 -4
- package/lib/codegen/patches.test.js.map +1 -1
- package/lib/codegen/prelude.js +1 -1
- package/lib/codegen/prelude.js.map +1 -1
- package/lib/codegen/proto.d.ts.map +1 -1
- package/lib/codegen/proto.js +2 -4
- package/lib/codegen/proto.js.map +1 -1
- package/lib/codegen/protocol-imports.test.d.ts +2 -0
- package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
- package/lib/codegen/protocol-imports.test.js +36 -0
- package/lib/codegen/protocol-imports.test.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
- package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
- package/lib/codegen/rewrite-operation-ids.js +97 -59
- package/lib/codegen/rewrite-operation-ids.js.map +1 -1
- package/lib/codegen/rewrite-operation-ids.test.js +19 -101
- package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
- package/lib/codegen/spec-path.d.ts +1 -1
- package/lib/codegen/spec-path.d.ts.map +1 -1
- package/lib/codegen/spec-path.js +1 -1
- package/lib/codegen/spec-path.js.map +1 -1
- package/lib/codegen/untagged-unions.test.js +1 -1
- package/lib/codegen/untagged-unions.test.js.map +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +7 -5
- package/lib/errors.js.map +1 -1
- package/lib/graphql.d.ts +4 -8
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +20 -48
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.test.js +6 -6
- package/lib/graphql.test.js.map +1 -1
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/graphql.types.js.map +1 -1
- package/lib/json-patch.d.ts +13 -6
- package/lib/json-patch.d.ts.map +1 -1
- package/lib/json-patch.js +34 -19
- package/lib/json-patch.js.map +1 -1
- package/lib/json-patch.test.d.ts +2 -0
- package/lib/json-patch.test.d.ts.map +1 -0
- package/lib/json-patch.test.js +65 -0
- package/lib/json-patch.test.js.map +1 -0
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +6 -16
- package/lib/pagination.js.map +1 -1
- package/lib/pagination.test.js +3 -6
- package/lib/pagination.test.js.map +1 -1
- package/lib/protocol-http.d.ts +20 -1
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +41 -47
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-http.test.js +20 -10
- package/lib/protocol-http.test.js.map +1 -1
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +9 -18
- package/lib/protocol-rest.js.map +1 -1
- package/lib/protocol-rest.test.d.ts +2 -0
- package/lib/protocol-rest.test.d.ts.map +1 -0
- package/lib/protocol-rest.test.js +498 -0
- package/lib/protocol-rest.test.js.map +1 -0
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/response-validation.js.map +1 -1
- package/lib/response-validation.test.js +10 -5
- package/lib/response-validation.test.js.map +1 -1
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -1
- package/lib/retry-after.js.map +1 -1
- package/lib/retry-after.test.d.ts +2 -0
- package/lib/retry-after.test.d.ts.map +1 -0
- package/lib/retry-after.test.js +204 -0
- package/lib/retry-after.test.js.map +1 -0
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -2
- package/lib/retry.js.map +1 -1
- package/lib/retry.test.d.ts +2 -0
- package/lib/retry.test.d.ts.map +1 -0
- package/lib/retry.test.js +407 -0
- package/lib/retry.test.js.map +1 -0
- package/lib/sdks.test.d.ts +2 -0
- package/lib/sdks.test.d.ts.map +1 -0
- package/lib/sdks.test.js +76 -0
- package/lib/sdks.test.js.map +1 -0
- package/lib/testing.d.ts +2 -2
- package/lib/testing.d.ts.map +1 -1
- package/lib/testing.js +1 -1
- package/lib/testing.js.map +1 -1
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/lib/xml.js +3 -7
- package/lib/xml.js.map +1 -1
- package/lib/xml.test.js +11 -12
- package/lib/xml.test.js.map +1 -1
- package/package.json +7 -10
- package/src/api.ts +38 -55
- package/src/category.ts +21 -50
- package/src/codegen/boolean-string-enums.test.ts +12 -28
- package/src/codegen/boolean-string-enums.ts +2 -10
- package/src/codegen/cli.ts +29 -47
- package/src/codegen/defer-naming.test.ts +113 -0
- package/src/codegen/emit.ts +2 -5
- package/src/codegen/format.ts +12 -10
- package/src/codegen/generated-at.test.ts +89 -0
- package/src/codegen/generated-at.ts +88 -0
- package/src/codegen/generator.test.ts +8 -22
- package/src/codegen/generator.ts +72 -191
- package/src/codegen/graphql-client.test.ts +12 -42
- package/src/codegen/graphql-client.ts +30 -95
- package/src/codegen/graphql.ts +22 -94
- package/src/codegen/model-diff.ts +67 -0
- package/src/codegen/naming.ts +1 -2
- package/src/codegen/openapi-binary.test.ts +5 -12
- package/src/codegen/openapi-cli.ts +38 -79
- package/src/codegen/openapi.ts +79 -136
- package/src/codegen/operations.ts +2 -5
- package/src/codegen/pagination.ts +4 -11
- package/src/codegen/patch-audit.test.ts +160 -0
- package/src/codegen/patch-audit.ts +480 -0
- package/src/codegen/patches.test.ts +14 -21
- package/src/codegen/patches.ts +313 -103
- package/src/codegen/prelude.ts +1 -1
- package/src/codegen/proto.ts +18 -54
- package/src/codegen/protocol-imports.test.ts +39 -0
- package/src/codegen/rewrite-operation-ids.test.ts +23 -114
- package/src/codegen/rewrite-operation-ids.ts +111 -98
- package/src/codegen/spec-path.ts +3 -6
- package/src/codegen/untagged-unions.test.ts +3 -7
- package/src/error-category.ts +5 -17
- package/src/errors.ts +16 -31
- package/src/graphql.test.ts +6 -8
- package/src/graphql.ts +76 -219
- package/src/graphql.types.ts +10 -25
- package/src/json-patch.test.ts +77 -0
- package/src/json-patch.ts +54 -60
- package/src/pagination.test.ts +13 -35
- package/src/pagination.ts +44 -111
- package/src/protocol-http.test.ts +36 -30
- package/src/protocol-http.ts +73 -138
- package/src/protocol-rest.test.ts +703 -0
- package/src/protocol-rest.ts +24 -62
- package/src/response-validation.test.ts +12 -18
- package/src/response-validation.ts +3 -12
- package/src/retry-after.test.ts +267 -0
- package/src/retry-after.ts +4 -10
- package/src/retry.test.ts +566 -0
- package/src/retry.ts +6 -21
- package/src/sdks.test.ts +87 -0
- package/src/testing.ts +4 -10
- package/src/trait.ts +21 -67
- package/src/xml.test.ts +15 -33
- package/src/xml.ts +9 -25
package/src/sdks.test.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks that run against every SDK package in the repo. Generated code is
|
|
3
|
+
* tested once, here in core; these catch a package whose hand-written glue
|
|
4
|
+
* drifted from what the generator and protocols expect.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { describe, expect, test } from "vitest";
|
|
9
|
+
|
|
10
|
+
const PACKAGES = join(import.meta.dirname, "..", "..");
|
|
11
|
+
|
|
12
|
+
const sdks = readdirSync(PACKAGES)
|
|
13
|
+
.filter((name) => name !== "core" && existsSync(join(PACKAGES, name, "package.json")))
|
|
14
|
+
.sort();
|
|
15
|
+
|
|
16
|
+
/** Packages without a `<Sdk>OpError` union over a REST/RPC protocol, and why. */
|
|
17
|
+
const NO_OP_ERROR: Record<string, string> = {
|
|
18
|
+
aws: "hand-written protocols; ParseError is part of CommonErrors in src/errors.ts",
|
|
19
|
+
railway: "GraphQL Query SDK; errors are typed per root by core/graphql",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const read = (pkg: string, file: string): string | undefined => {
|
|
23
|
+
const p = join(PACKAGES, pkg, file);
|
|
24
|
+
return existsSync(p) ? readFileSync(p, "utf8") : undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Hand-written sources: `src/*.ts` without tests (generated code lives in `src/services/`). */
|
|
28
|
+
const handWritten = (pkg: string): string[] => {
|
|
29
|
+
const dir = join(PACKAGES, pkg, "src");
|
|
30
|
+
return readdirSync(dir)
|
|
31
|
+
.filter((f) => f.endsWith(".ts") && !f.endsWith(".test.ts"))
|
|
32
|
+
.map((f) => readFileSync(join(dir, f), "utf8"));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Every `export type <Name> = …` in the package's hand-written sources. */
|
|
36
|
+
const typeAliases = (pkg: string): Map<string, string> => {
|
|
37
|
+
const aliases = new Map<string, string>();
|
|
38
|
+
for (const source of handWritten(pkg)) {
|
|
39
|
+
for (const match of source.matchAll(/^export type (\w+)\s*=([\s\S]*?);[ \t]*$/gm)) {
|
|
40
|
+
aliases.set(match[1]!, match[2]!);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return aliases;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Names reachable from `root` through the package's own aliases (`DefaultErrors`, `ClientErrors`, …). */
|
|
47
|
+
const reachable = (aliases: Map<string, string>, root: string): Set<string> => {
|
|
48
|
+
const seen = new Set<string>();
|
|
49
|
+
const stack = [root];
|
|
50
|
+
while (stack.length > 0) {
|
|
51
|
+
const name = stack.pop()!;
|
|
52
|
+
if (seen.has(name)) continue;
|
|
53
|
+
seen.add(name);
|
|
54
|
+
for (const ref of aliases.get(name)?.match(/\b[A-Z]\w*\b/g) ?? []) stack.push(ref);
|
|
55
|
+
}
|
|
56
|
+
return seen;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
describe("every SDK", () => {
|
|
60
|
+
test("the exemption list names real packages", () => {
|
|
61
|
+
for (const pkg of Object.keys(NO_OP_ERROR)) expect(sdks).toContain(pkg);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe.each(sdks.filter((pkg) => !(pkg in NO_OP_ERROR)))("%s", (pkg) => {
|
|
65
|
+
test("declares its ParseError in the operation error union and constructs it in the protocol", () => {
|
|
66
|
+
const aliases = typeAliases(pkg);
|
|
67
|
+
const opError = [...aliases.keys()].find((name) => name.endsWith("OpError"));
|
|
68
|
+
expect(opError, `packages/${pkg}/src declares no \`export type <Sdk>OpError\``).toBeDefined();
|
|
69
|
+
const parseError = [...reachable(aliases, opError!)].find((name) =>
|
|
70
|
+
name.endsWith("ParseError"),
|
|
71
|
+
);
|
|
72
|
+
// Without it, `Effect.catchTag("<Sdk>ParseError")` on a strict call is a type error.
|
|
73
|
+
expect(parseError, `${opError} has no ParseError member`).toBeDefined();
|
|
74
|
+
expect(
|
|
75
|
+
handWritten(pkg).some((source) => source.includes(`new ${parseError}(`)),
|
|
76
|
+
`nothing outside src/services constructs ${parseError}`,
|
|
77
|
+
).toBe(true);
|
|
78
|
+
expect(read(pkg, "src/errors.ts")).toMatch(new RegExp(`export class ${parseError}\\b`));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("aws keeps ParseError in CommonErrors", () => {
|
|
83
|
+
const errors = read("aws", "src/errors.ts")!;
|
|
84
|
+
expect(errors).toMatch(/export class ParseError\b/);
|
|
85
|
+
expect(/export type CommonErrors =([^;]*);/.exec(errors)?.[1]).toMatch(/\bParseError\b/);
|
|
86
|
+
});
|
|
87
|
+
});
|
package/src/testing.ts
CHANGED
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
14
|
import * as Effect from "effect/Effect";
|
|
15
|
-
import * as Layer from "effect/Layer";
|
|
16
|
-
import type * as Result from "effect/Result";
|
|
17
15
|
import * as HttpClient from "effect/http/HttpClient";
|
|
18
16
|
import type * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
19
17
|
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
18
|
+
import * as Layer from "effect/Layer";
|
|
19
|
+
import type * as Result from "effect/Result";
|
|
20
20
|
import * as ResponseValidation from "./response-validation.ts";
|
|
21
21
|
|
|
22
22
|
export interface MockResponse {
|
|
@@ -32,9 +32,7 @@ export interface MockResponse {
|
|
|
32
32
|
* `response(request)` returns, for protocols that make more than one call).
|
|
33
33
|
*/
|
|
34
34
|
export const mockHttpClient = (
|
|
35
|
-
response:
|
|
36
|
-
| MockResponse
|
|
37
|
-
| ((request: HttpClientRequest.HttpClientRequest) => MockResponse),
|
|
35
|
+
response: MockResponse | ((request: HttpClientRequest.HttpClientRequest) => MockResponse),
|
|
38
36
|
): Layer.Layer<HttpClient.HttpClient> =>
|
|
39
37
|
Layer.succeed(
|
|
40
38
|
HttpClient.HttpClient,
|
|
@@ -64,11 +62,7 @@ export const runValidationModes = <A, E>(
|
|
|
64
62
|
readonly strict: Result.Result<A, E>;
|
|
65
63
|
}> => {
|
|
66
64
|
const run = (mode: Layer.Layer<never>) =>
|
|
67
|
-
effect.pipe(
|
|
68
|
-
Effect.provide(mockHttpClient(response)),
|
|
69
|
-
Effect.provide(mode),
|
|
70
|
-
Effect.result,
|
|
71
|
-
);
|
|
65
|
+
effect.pipe(Effect.provide(mockHttpClient(response)), Effect.provide(mode), Effect.result);
|
|
72
66
|
return Effect.runPromise(
|
|
73
67
|
Effect.all({
|
|
74
68
|
lenient: run(ResponseValidation.lenient),
|
package/src/trait.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export const annotationMetaSymbol = Symbol.for(
|
|
2
|
-
"@distilled.cloud/core/annotation-meta",
|
|
3
|
-
);
|
|
1
|
+
export const annotationMetaSymbol = Symbol.for("@distilled.cloud/core/annotation-meta");
|
|
4
2
|
|
|
5
3
|
export type Annotatable = {
|
|
6
4
|
annotate(annotations: any): Annotatable;
|
|
@@ -24,8 +22,7 @@ export interface Annotation {
|
|
|
24
22
|
* annotation dictionaries accept both.
|
|
25
23
|
*/
|
|
26
24
|
export function makeAnnotation<T>(sym: symbol | string, value: T): Annotation {
|
|
27
|
-
const fn = <A extends Annotatable>(schema: A): A =>
|
|
28
|
-
schema.annotate({ [sym]: value }) as A;
|
|
25
|
+
const fn = <A extends Annotatable>(schema: A): A => schema.annotate({ [sym]: value }) as A;
|
|
29
26
|
(fn as any)[annotationMetaSymbol] = [{ symbol: sym, value }];
|
|
30
27
|
(fn as any)[sym] = value;
|
|
31
28
|
return fn as Annotation;
|
|
@@ -59,14 +56,7 @@ export function all(...annotations: Annotation[]): Annotation {
|
|
|
59
56
|
//#region Generic Http traits
|
|
60
57
|
|
|
61
58
|
export interface HttpTrait {
|
|
62
|
-
readonly method:
|
|
63
|
-
| "GET"
|
|
64
|
-
| "POST"
|
|
65
|
-
| "PUT"
|
|
66
|
-
| "PATCH"
|
|
67
|
-
| "DELETE"
|
|
68
|
-
| "HEAD"
|
|
69
|
-
| "OPTIONS";
|
|
59
|
+
readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
|
|
70
60
|
/** URI template relative to the service base URL, e.g. `/accounts/{account_id}/foo`. */
|
|
71
61
|
readonly uri: string;
|
|
72
62
|
/** Default success status code (a `ResponseCode()` member can still read the actual code). */
|
|
@@ -106,41 +96,30 @@ export const Http = (trait: HttpTrait) => makeAnnotation(httpSymbol, trait);
|
|
|
106
96
|
|
|
107
97
|
export const labelSymbol = Symbol.for("@distilled.cloud/core/http/label");
|
|
108
98
|
/** Bind a member to a `{name}` placeholder in the operation's URI template. */
|
|
109
|
-
export const Label = (name?: string) =>
|
|
110
|
-
makeAnnotation(labelSymbol, name ?? true);
|
|
99
|
+
export const Label = (name?: string) => makeAnnotation(labelSymbol, name ?? true);
|
|
111
100
|
|
|
112
|
-
export const labelEncodingSymbol = Symbol.for(
|
|
113
|
-
"@distilled.cloud/core/http/label-encoding",
|
|
114
|
-
);
|
|
101
|
+
export const labelEncodingSymbol = Symbol.for("@distilled.cloud/core/http/label-encoding");
|
|
115
102
|
/** Preserve selected RFC 3986 pchar delimiters inside a single URI label. */
|
|
116
103
|
export const LabelEncoding = (options: { readonly preserve: string }) => {
|
|
117
104
|
if (!/^[!$&'()*+,;=:@]*$/.test(options.preserve)) {
|
|
118
|
-
throw new TypeError(
|
|
119
|
-
"LabelEncoding can preserve only URI path-segment delimiters",
|
|
120
|
-
);
|
|
105
|
+
throw new TypeError("LabelEncoding can preserve only URI path-segment delimiters");
|
|
121
106
|
}
|
|
122
107
|
return makeAnnotation(labelEncodingSymbol, options.preserve);
|
|
123
108
|
};
|
|
124
109
|
|
|
125
|
-
export const responseCodeSymbol = Symbol.for(
|
|
126
|
-
"@distilled.cloud/core/http/response-code",
|
|
127
|
-
);
|
|
110
|
+
export const responseCodeSymbol = Symbol.for("@distilled.cloud/core/http/response-code");
|
|
128
111
|
export const ResponseCode = () => makeAnnotation(responseCodeSymbol, true);
|
|
129
112
|
|
|
130
113
|
export const headerSymbol = Symbol.for("@distilled.cloud/core/http/header");
|
|
131
|
-
export const Header = (name?: string) =>
|
|
132
|
-
makeAnnotation(headerSymbol, name ?? true);
|
|
114
|
+
export const Header = (name?: string) => makeAnnotation(headerSymbol, name ?? true);
|
|
133
115
|
|
|
134
116
|
export const bodySymbol = Symbol.for("@distilled.cloud/core/http/body");
|
|
135
117
|
export const Body = (name?: string) => makeAnnotation(bodySymbol, name ?? true);
|
|
136
118
|
|
|
137
119
|
export const querySymbol = Symbol.for("@distilled.cloud/core/http/query");
|
|
138
|
-
export const Query = (name?: string) =>
|
|
139
|
-
makeAnnotation(querySymbol, name ?? true);
|
|
120
|
+
export const Query = (name?: string) => makeAnnotation(querySymbol, name ?? true);
|
|
140
121
|
|
|
141
|
-
export const deepQuerySymbol = Symbol.for(
|
|
142
|
-
"@distilled.cloud/core/http/deep-query",
|
|
143
|
-
);
|
|
122
|
+
export const deepQuerySymbol = Symbol.for("@distilled.cloud/core/http/deep-query");
|
|
144
123
|
/**
|
|
145
124
|
* Binds a struct-valued input member to a family of DOTTED query parameters:
|
|
146
125
|
* `{ account: { id, name } }` with `DeepQuery("account")` serializes as
|
|
@@ -149,8 +128,7 @@ export const deepQuerySymbol = Symbol.for(
|
|
|
149
128
|
* Cloudflare zones list `account.id` / `account.name`) while the TS surface
|
|
150
129
|
* keeps the nested-object shape (v0 parity).
|
|
151
130
|
*/
|
|
152
|
-
export const DeepQuery = (name?: string) =>
|
|
153
|
-
makeAnnotation(deepQuerySymbol, name ?? true);
|
|
131
|
+
export const DeepQuery = (name?: string) => makeAnnotation(deepQuerySymbol, name ?? true);
|
|
154
132
|
|
|
155
133
|
export const httpBodySymbol = Symbol.for("@distilled.cloud/core/http-body");
|
|
156
134
|
/**
|
|
@@ -160,9 +138,7 @@ export const httpBodySymbol = Symbol.for("@distilled.cloud/core/http-body");
|
|
|
160
138
|
*/
|
|
161
139
|
export const HttpBody = () => makeAnnotation(httpBodySymbol, true);
|
|
162
140
|
|
|
163
|
-
export const stringEncodedSymbol = Symbol.for(
|
|
164
|
-
"@distilled.cloud/core/http/string-encoded",
|
|
165
|
-
);
|
|
141
|
+
export const stringEncodedSymbol = Symbol.for("@distilled.cloud/core/http/string-encoded");
|
|
166
142
|
/**
|
|
167
143
|
* Marks a member the API only accepts as the STRING spelling of its value —
|
|
168
144
|
* `true` travels as `"true"`, `3` as `"3"` — while the TS surface keeps the
|
|
@@ -174,9 +150,7 @@ export const stringEncodedSymbol = Symbol.for(
|
|
|
174
150
|
*/
|
|
175
151
|
export const StringEncoded = () => makeAnnotation(stringEncodedSymbol, true);
|
|
176
152
|
|
|
177
|
-
export const formDataFileSymbol = Symbol.for(
|
|
178
|
-
"@distilled.cloud/core/form-data-file",
|
|
179
|
-
);
|
|
153
|
+
export const formDataFileSymbol = Symbol.for("@distilled.cloud/core/form-data-file");
|
|
180
154
|
/**
|
|
181
155
|
* Marks an input member holding `File`/`Blob` parts for a multipart upload
|
|
182
156
|
* (`Http({ contentType: "multipart" })`). Each file is appended to the form
|
|
@@ -187,9 +161,7 @@ export const FormDataFile = () => makeAnnotation(formDataFileSymbol, true);
|
|
|
187
161
|
|
|
188
162
|
//#region Generic JSON traits
|
|
189
163
|
|
|
190
|
-
export const keyDictionarySymbol = Symbol.for(
|
|
191
|
-
"@distilled.cloud/core/key-dictionary",
|
|
192
|
-
);
|
|
164
|
+
export const keyDictionarySymbol = Symbol.for("@distilled.cloud/core/key-dictionary");
|
|
193
165
|
/**
|
|
194
166
|
* Deep TS-name→wire-name key dictionary for members whose full structure is
|
|
195
167
|
* not modeled (opaque `Document` content). The protocol renames any matching
|
|
@@ -202,10 +174,7 @@ export const keyDictionarySymbol = Symbol.for(
|
|
|
202
174
|
* wire name used on encode; decode maps every listed spelling back to the
|
|
203
175
|
* TS name.
|
|
204
176
|
*/
|
|
205
|
-
export type KeyDictionaryEntries = Record<
|
|
206
|
-
string,
|
|
207
|
-
string | ReadonlyArray<string>
|
|
208
|
-
>;
|
|
177
|
+
export type KeyDictionaryEntries = Record<string, string | ReadonlyArray<string>>;
|
|
209
178
|
|
|
210
179
|
export const KeyDictionary = (dict: KeyDictionaryEntries) =>
|
|
211
180
|
makeAnnotation(keyDictionarySymbol, dict);
|
|
@@ -237,23 +206,15 @@ export interface UnionDiscriminator {
|
|
|
237
206
|
export const UnionCases = (
|
|
238
207
|
cases: ReadonlyArray<ReadonlyArray<string>>,
|
|
239
208
|
discriminator?: UnionDiscriminator,
|
|
240
|
-
) =>
|
|
241
|
-
makeAnnotation(
|
|
242
|
-
unionCasesSymbol,
|
|
243
|
-
discriminator ? { cases, discriminator } : { cases },
|
|
244
|
-
);
|
|
209
|
+
) => makeAnnotation(unionCasesSymbol, discriminator ? { cases, discriminator } : { cases });
|
|
245
210
|
//#endregion
|
|
246
211
|
|
|
247
212
|
//#region Error matcher traits
|
|
248
213
|
|
|
249
|
-
export const errorMatchersSymbol = Symbol.for(
|
|
250
|
-
"@distilled.cloud/core/error-matchers",
|
|
251
|
-
);
|
|
214
|
+
export const errorMatchersSymbol = Symbol.for("@distilled.cloud/core/error-matchers");
|
|
252
215
|
|
|
253
216
|
/** Exact text, or the conjunction of substring and regular-expression constraints. */
|
|
254
|
-
export type ErrorTextMatcher =
|
|
255
|
-
| string
|
|
256
|
-
| { readonly includes?: string; readonly matches?: string };
|
|
217
|
+
export type ErrorTextMatcher = string | { readonly includes?: string; readonly matches?: string };
|
|
257
218
|
|
|
258
219
|
/**
|
|
259
220
|
* One wire-matching rule. All supplied constraints must match; separate
|
|
@@ -277,9 +238,7 @@ export interface ErrorMatcher {
|
|
|
277
238
|
readonly code?: number;
|
|
278
239
|
readonly status?: number;
|
|
279
240
|
readonly message?: ErrorTextMatcher;
|
|
280
|
-
readonly body?: Readonly<
|
|
281
|
-
Record<string, ErrorTextMatcher | number | boolean | null>
|
|
282
|
-
>;
|
|
241
|
+
readonly body?: Readonly<Record<string, ErrorTextMatcher | number | boolean | null>>;
|
|
283
242
|
readonly headers?: Readonly<Record<string, ErrorTextMatcher>>;
|
|
284
243
|
}
|
|
285
244
|
|
|
@@ -289,17 +248,12 @@ export interface ErrorMatcher {
|
|
|
289
248
|
* response should surface as (most specific matcher wins; ties break by
|
|
290
249
|
* declaration order — see `matchTypedError` in `core/protocol-http`).
|
|
291
250
|
*/
|
|
292
|
-
export const applyErrorMatchers = <C>(
|
|
293
|
-
cls: C,
|
|
294
|
-
matchers: ReadonlyArray<ErrorMatcher>,
|
|
295
|
-
): C => {
|
|
251
|
+
export const applyErrorMatchers = <C>(cls: C, matchers: ReadonlyArray<ErrorMatcher>): C => {
|
|
296
252
|
(cls as any)[errorMatchersSymbol] = matchers;
|
|
297
253
|
return cls;
|
|
298
254
|
};
|
|
299
255
|
|
|
300
256
|
/** Read the matchers stamped on an error class, if any. */
|
|
301
|
-
export const getErrorMatchers = (
|
|
302
|
-
cls: unknown,
|
|
303
|
-
): ReadonlyArray<ErrorMatcher> | undefined =>
|
|
257
|
+
export const getErrorMatchers = (cls: unknown): ReadonlyArray<ErrorMatcher> | undefined =>
|
|
304
258
|
(cls as any)?.[errorMatchersSymbol];
|
|
305
259
|
//#endregion
|
package/src/xml.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { describe, it } from "node:test";
|
|
3
2
|
import * as Effect from "effect/Effect";
|
|
3
|
+
import { describe, it } from "vitest";
|
|
4
4
|
import fixtures from "./fixtures/xml/upstream-validator.json" with { type: "json" };
|
|
5
5
|
import { escapeXml, parseXml, parseXmlSync, XmlParseError } from "./xml.ts";
|
|
6
6
|
|
|
@@ -23,17 +23,14 @@ describe("upstream fast-xml-parser validation corpus", () => {
|
|
|
23
23
|
|
|
24
24
|
describe("XML object format", () => {
|
|
25
25
|
it("keeps primitives as strings and groups repeated children", () => {
|
|
26
|
-
assert.deepEqual(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
);
|
|
26
|
+
assert.deepEqual(parse("<R><x>001</x><x>false</x><x>1e3</x><empty/><empty></empty></R>"), {
|
|
27
|
+
R: { x: ["001", "false", "1e3"], empty: ["", ""] },
|
|
28
|
+
});
|
|
30
29
|
});
|
|
31
30
|
|
|
32
31
|
it("preserves namespace names, attributes, and text with attributes", () => {
|
|
33
32
|
assert.deepEqual(
|
|
34
|
-
parse(
|
|
35
|
-
'<ns:R xmlns:ns="urn:r"><ns:x ns:id="01">value</ns:x><empty a=""/></ns:R>',
|
|
36
|
-
),
|
|
33
|
+
parse('<ns:R xmlns:ns="urn:r"><ns:x ns:id="01">value</ns:x><empty a=""/></ns:R>'),
|
|
37
34
|
{
|
|
38
35
|
"ns:R": {
|
|
39
36
|
"@_xmlns:ns": "urn:r",
|
|
@@ -73,17 +70,15 @@ describe("XML object format", () => {
|
|
|
73
70
|
});
|
|
74
71
|
|
|
75
72
|
it("decodes predefined and numeric entities exactly once", () => {
|
|
76
|
-
assert.deepEqual(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
);
|
|
73
|
+
assert.deepEqual(parse('<R a=""'	">&<>A😀&lt;</R>'), {
|
|
74
|
+
R: { "@_a": "\"'\t", "#text": "&<>A😀<" },
|
|
75
|
+
});
|
|
80
76
|
});
|
|
81
77
|
|
|
82
78
|
it("normalizes literal line endings and attribute whitespace, not character references", () => {
|
|
83
|
-
assert.deepEqual(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
);
|
|
79
|
+
assert.deepEqual(parse('<R a="x\r\ny\tz\r 	">a\r\nb\rc </R>'), {
|
|
80
|
+
R: { "@_a": "x y z \r\n\t", "#text": "a\nb\nc\r" },
|
|
81
|
+
});
|
|
87
82
|
});
|
|
88
83
|
|
|
89
84
|
it("joins CDATA and text without decoding CDATA entities", () => {
|
|
@@ -109,8 +104,7 @@ describe("XML object format", () => {
|
|
|
109
104
|
});
|
|
110
105
|
|
|
111
106
|
it("accepts empty HTTP bodies", () => {
|
|
112
|
-
for (const xml of ["", " \r\n\t", "\uFEFF"])
|
|
113
|
-
assert.deepEqual(parse(xml), {});
|
|
107
|
+
for (const xml of ["", " \r\n\t", "\uFEFF"]) assert.deepEqual(parse(xml), {});
|
|
114
108
|
});
|
|
115
109
|
|
|
116
110
|
it("stores prototype-related names as own data properties", () => {
|
|
@@ -122,22 +116,13 @@ describe("XML object format", () => {
|
|
|
122
116
|
assert.equal(Object.getPrototypeOf(root), null);
|
|
123
117
|
assert.deepEqual(
|
|
124
118
|
JSON.parse(JSON.stringify(root)),
|
|
125
|
-
JSON.parse(
|
|
126
|
-
'{"constructor":["one","two"],"__proto__":"safe","toString":""}',
|
|
127
|
-
),
|
|
119
|
+
JSON.parse('{"constructor":["one","two"],"__proto__":"safe","toString":""}'),
|
|
128
120
|
);
|
|
129
121
|
assert.equal(Object.hasOwn(Object.prototype, "safe"), false);
|
|
130
122
|
});
|
|
131
123
|
|
|
132
124
|
it("round trips escaped strings", () => {
|
|
133
|
-
for (const text of [
|
|
134
|
-
' <tag a="x">&\'😀 ',
|
|
135
|
-
"",
|
|
136
|
-
"false",
|
|
137
|
-
"001",
|
|
138
|
-
" ",
|
|
139
|
-
"\t\n",
|
|
140
|
-
]) {
|
|
125
|
+
for (const text of [' <tag a="x">&\'😀 ', "", "false", "001", " ", "\t\n"]) {
|
|
141
126
|
assert.deepEqual(parse(`<R>${escapeXml(text)}</R>`), { R: text });
|
|
142
127
|
}
|
|
143
128
|
});
|
|
@@ -242,10 +227,7 @@ describe("malformed and unsupported XML", () => {
|
|
|
242
227
|
|
|
243
228
|
it("enforces depth and input length limits", () => {
|
|
244
229
|
assert.doesNotThrow(() => parseXmlSync("<a><b/></a>", { maxDepth: 2 }));
|
|
245
|
-
assert.throws(
|
|
246
|
-
() => parseXmlSync("<a><b/></a>", { maxDepth: 1 }),
|
|
247
|
-
XmlParseError,
|
|
248
|
-
);
|
|
230
|
+
assert.throws(() => parseXmlSync("<a><b/></a>", { maxDepth: 1 }), XmlParseError);
|
|
249
231
|
assert.doesNotThrow(() => parseXmlSync("<a/>", { maxLength: 4 }));
|
|
250
232
|
assert.throws(() => parseXmlSync("<a/>", { maxLength: 3 }), XmlParseError);
|
|
251
233
|
assert.throws(() => parseXmlSync("<a/>", { maxDepth: NaN }), XmlParseError);
|
package/src/xml.ts
CHANGED
|
@@ -52,10 +52,7 @@ interface Element {
|
|
|
52
52
|
* Throws XmlParseError for malformed input or exceeded limits.
|
|
53
53
|
* The iterative scanner allocates no Effects while traversing the document.
|
|
54
54
|
*/
|
|
55
|
-
export function parseXmlSync(
|
|
56
|
-
xml: string,
|
|
57
|
-
options: XmlParseOptions = {},
|
|
58
|
-
): XmlObject {
|
|
55
|
+
export function parseXmlSync(xml: string, options: XmlParseOptions = {}): XmlObject {
|
|
59
56
|
let pos = xml.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
60
57
|
const start = pos;
|
|
61
58
|
const maxDepth = options.maxDepth ?? 256;
|
|
@@ -78,8 +75,7 @@ export function parseXmlSync(
|
|
|
78
75
|
}
|
|
79
76
|
|
|
80
77
|
if (!Number.isSafeInteger(maxDepth) || maxDepth < 1) fail("Invalid maxDepth");
|
|
81
|
-
if (!Number.isSafeInteger(maxLength) || maxLength < 0)
|
|
82
|
-
fail("Invalid maxLength");
|
|
78
|
+
if (!Number.isSafeInteger(maxLength) || maxLength < 0) fail("Invalid maxLength");
|
|
83
79
|
if (xml.length > maxLength) fail("XML input length limit exceeded", 0);
|
|
84
80
|
const invalid = invalidCharacter.exec(xml);
|
|
85
81
|
if (invalid) fail("Invalid XML character", invalid.index);
|
|
@@ -111,9 +107,7 @@ export function parseXmlSync(
|
|
|
111
107
|
const end = amp < 0 ? raw.length : amp;
|
|
112
108
|
const literal = raw.slice(from, end);
|
|
113
109
|
parts.push(
|
|
114
|
-
attribute
|
|
115
|
-
? literal.replace(/\r\n|[\t\r\n]/g, " ")
|
|
116
|
-
: literal.replace(/\r\n?/g, "\n"),
|
|
110
|
+
attribute ? literal.replace(/\r\n|[\t\r\n]/g, " ") : literal.replace(/\r\n?/g, "\n"),
|
|
117
111
|
);
|
|
118
112
|
if (amp < 0) break;
|
|
119
113
|
const semi = raw.indexOf(";", amp + 1);
|
|
@@ -169,10 +163,7 @@ export function parseXmlSync(
|
|
|
169
163
|
if (!element.hasChildren && !element.hasAttributes) {
|
|
170
164
|
value = text;
|
|
171
165
|
} else {
|
|
172
|
-
if (
|
|
173
|
-
text &&
|
|
174
|
-
(!element.hasChildren || element.hasCdata || !xmlSpace.test(text))
|
|
175
|
-
) {
|
|
166
|
+
if (text && (!element.hasChildren || element.hasCdata || !xmlSpace.test(text))) {
|
|
176
167
|
element.object["#text"] = text;
|
|
177
168
|
}
|
|
178
169
|
value = element.object;
|
|
@@ -199,8 +190,7 @@ export function parseXmlSync(
|
|
|
199
190
|
if (!xmlSpace.test(raw)) fail("Text outside root element", offset);
|
|
200
191
|
} else {
|
|
201
192
|
const cdataEnd = raw.indexOf("]]>");
|
|
202
|
-
if (cdataEnd >= 0)
|
|
203
|
-
fail("Unexpected CDATA terminator", offset + cdataEnd);
|
|
193
|
+
if (cdataEnd >= 0) fail("Unexpected CDATA terminator", offset + cdataEnd);
|
|
204
194
|
element.text.push(decode(raw, offset));
|
|
205
195
|
}
|
|
206
196
|
continue;
|
|
@@ -210,8 +200,7 @@ export function parseXmlSync(
|
|
|
210
200
|
const end = xml.indexOf("-->", pos + 4);
|
|
211
201
|
if (end < 0) fail("Unterminated comment");
|
|
212
202
|
const comment = xml.slice(pos + 4, end);
|
|
213
|
-
if (comment.includes("--") || comment.endsWith("-"))
|
|
214
|
-
fail("Invalid comment");
|
|
203
|
+
if (comment.includes("--") || comment.endsWith("-")) fail("Invalid comment");
|
|
215
204
|
pos = end + 3;
|
|
216
205
|
} else if (xml.startsWith("<![CDATA[", pos)) {
|
|
217
206
|
const element = stack[stack.length - 1];
|
|
@@ -225,15 +214,11 @@ export function parseXmlSync(
|
|
|
225
214
|
const offset = pos;
|
|
226
215
|
pos += 2;
|
|
227
216
|
const target = readName();
|
|
228
|
-
if (!xml.startsWith("?>", pos) && !skipSpace())
|
|
229
|
-
fail("Expected PI whitespace");
|
|
217
|
+
if (!xml.startsWith("?>", pos) && !skipSpace()) fail("Expected PI whitespace");
|
|
230
218
|
const end = xml.indexOf("?>", pos);
|
|
231
219
|
if (end < 0) fail("Unterminated processing instruction", offset);
|
|
232
220
|
if (target.toLowerCase() === "xml") {
|
|
233
|
-
if (
|
|
234
|
-
offset !== start ||
|
|
235
|
-
!declarationPattern.test(xml.slice(offset + 2, end))
|
|
236
|
-
) {
|
|
221
|
+
if (offset !== start || !declarationPattern.test(xml.slice(offset + 2, end))) {
|
|
237
222
|
fail("Invalid or misplaced XML declaration", offset);
|
|
238
223
|
}
|
|
239
224
|
}
|
|
@@ -325,8 +310,7 @@ const xmlEscapes: Record<string, string> = {
|
|
|
325
310
|
/**
|
|
326
311
|
* Escape special XML characters in a string.
|
|
327
312
|
*/
|
|
328
|
-
export const escapeXml = (s: string): string =>
|
|
329
|
-
s.replace(/[&<>"']/g, (c) => xmlEscapes[c]);
|
|
313
|
+
export const escapeXml = (s: string): string => s.replace(/[&<>"']/g, (c) => xmlEscapes[c]);
|
|
330
314
|
|
|
331
315
|
// =============================================================================
|
|
332
316
|
// XML Tag Helpers
|