@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.20
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 +201 -0
- package/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/grpc.js +2 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +174 -18
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +4 -4
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +68 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +64 -72
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +403 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +12 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +76 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/pagination.d.ts +101 -24
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protobuf.d.ts +92 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +86 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +102 -23
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +120 -50
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -112
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -210
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +27 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +288 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/trait.d.ts +71 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1370 -200
- package/src/category.ts +76 -106
- package/src/chunks.ts +30 -0
- package/src/config.ts +245 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +75 -75
- package/src/graphql.ts +1778 -0
- package/src/graphql.types.ts +105 -0
- package/src/grpc-transport-cloudflare-http2.ts +494 -0
- package/src/grpc-transport-cloudflare-sockets.ts +299 -0
- package/src/grpc-transport-node-http2.ts +234 -0
- package/src/grpc-transport.ts +118 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/pagination.ts +368 -94
- package/src/plain-values.ts +41 -0
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +622 -322
- package/src/protocol-rest.ts +440 -227
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +7 -13
- package/src/retry.ts +96 -110
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +259 -15
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +832 -0
- package/src/trait.ts +106 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
package/src/trait.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export const annotationMetaSymbol = Symbol.for(
|
|
1
|
+
export const annotationMetaSymbol = /*@__PURE__*/ Symbol.for(
|
|
2
2
|
"@distilled.cloud/core/annotation-meta",
|
|
3
3
|
);
|
|
4
4
|
|
|
5
|
-
type Annotatable = {
|
|
5
|
+
export type Annotatable = {
|
|
6
6
|
annotate(annotations: any): Annotatable;
|
|
7
7
|
};
|
|
8
8
|
|
|
@@ -20,19 +20,18 @@ export interface Annotation {
|
|
|
20
20
|
* Build a pipeable schema annotation carrying `value` under `sym`. Exported
|
|
21
21
|
* so SDK packages can define their own protocol traits (e.g. cloudflare's
|
|
22
22
|
* envelope traits, aws's smithy traits) with the same mechanics as the
|
|
23
|
-
* generic ones here. Keys may be symbols or plain strings
|
|
23
|
+
* generic ones here. Keys may be symbols or plain strings -- effect Schema
|
|
24
24
|
* annotation dictionaries accept both.
|
|
25
25
|
*/
|
|
26
26
|
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;
|
|
27
|
+
const fn = <A extends Annotatable>(schema: A): A => schema.annotate({ [sym]: value }) as A;
|
|
29
28
|
(fn as any)[annotationMetaSymbol] = [{ symbol: sym, value }];
|
|
30
29
|
(fn as any)[sym] = value;
|
|
31
30
|
return fn as Annotation;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
|
-
* Combine several annotations into one
|
|
34
|
+
* Combine several annotations into one -- needed where only a single
|
|
36
35
|
* annotations object can be supplied (e.g. the second argument of
|
|
37
36
|
* `S.Class`).
|
|
38
37
|
*/
|
|
@@ -59,21 +58,14 @@ export function all(...annotations: Annotation[]): Annotation {
|
|
|
59
58
|
//#region Generic Http traits
|
|
60
59
|
|
|
61
60
|
export interface HttpTrait {
|
|
62
|
-
readonly method:
|
|
63
|
-
| "GET"
|
|
64
|
-
| "POST"
|
|
65
|
-
| "PUT"
|
|
66
|
-
| "PATCH"
|
|
67
|
-
| "DELETE"
|
|
68
|
-
| "HEAD"
|
|
69
|
-
| "OPTIONS";
|
|
61
|
+
readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
|
|
70
62
|
/** URI template relative to the service base URL, e.g. `/accounts/{account_id}/foo`. */
|
|
71
63
|
readonly uri: string;
|
|
72
64
|
/** Default success status code (a `ResponseCode()` member can still read the actual code). */
|
|
73
65
|
readonly code?: number;
|
|
74
66
|
/**
|
|
75
67
|
* Request body encoding. Default is JSON; `"multipart"` sends
|
|
76
|
-
* multipart/form-data
|
|
68
|
+
* multipart/form-data -- body members become form parts (objects
|
|
77
69
|
* JSON-encoded) and members marked `FormDataFile()` append their files.
|
|
78
70
|
* `"form-urlencoded"` sends application/x-www-form-urlencoded with
|
|
79
71
|
* Stripe-style bracket notation for nested objects and arrays (see
|
|
@@ -90,13 +82,13 @@ export interface HttpTrait {
|
|
|
90
82
|
/**
|
|
91
83
|
* Explicit `Accept` for the operation, when the API serves more than one
|
|
92
84
|
* response media type. Atlas pins an API version this way (e.g.
|
|
93
|
-
* `application/vnd.atlas.2024-05-30+json`
|
|
85
|
+
* `application/vnd.atlas.2024-05-30+json` -- `application/json` alone is a
|
|
94
86
|
* 406). Protocols that don't read it fall back to their own default.
|
|
95
87
|
*/
|
|
96
88
|
readonly accept?: string;
|
|
97
89
|
}
|
|
98
90
|
|
|
99
|
-
export const httpSymbol = Symbol.for("@distilled.cloud/core/http");
|
|
91
|
+
export const httpSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http");
|
|
100
92
|
/**
|
|
101
93
|
* Operation-level HTTP binding: the request method + URI template. Stamped on
|
|
102
94
|
* the input schema so the protocol can build the request line. URI `{labels}`
|
|
@@ -104,52 +96,79 @@ export const httpSymbol = Symbol.for("@distilled.cloud/core/http");
|
|
|
104
96
|
*/
|
|
105
97
|
export const Http = (trait: HttpTrait) => makeAnnotation(httpSymbol, trait);
|
|
106
98
|
|
|
107
|
-
export const labelSymbol = Symbol.for("@distilled.cloud/core/http/label");
|
|
99
|
+
export const labelSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/label");
|
|
108
100
|
/** Bind a member to a `{name}` placeholder in the operation's URI template. */
|
|
109
|
-
export const Label = (name?: string) =>
|
|
110
|
-
makeAnnotation(labelSymbol, name ?? true);
|
|
101
|
+
export const Label = (name?: string) => makeAnnotation(labelSymbol, name ?? true);
|
|
111
102
|
|
|
112
|
-
export const
|
|
103
|
+
export const labelEncodingSymbol = /*@__PURE__*/ Symbol.for(
|
|
104
|
+
"@distilled.cloud/core/http/label-encoding",
|
|
105
|
+
);
|
|
106
|
+
/** Preserve selected RFC 3986 pchar delimiters inside a single URI label. */
|
|
107
|
+
export const LabelEncoding = (options: { readonly preserve: string }) => {
|
|
108
|
+
if (!/^[!$&'()*+,;=:@]*$/.test(options.preserve)) {
|
|
109
|
+
throw new TypeError("LabelEncoding can preserve only URI path-segment delimiters");
|
|
110
|
+
}
|
|
111
|
+
return makeAnnotation(labelEncodingSymbol, options.preserve);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const responseCodeSymbol = /*@__PURE__*/ Symbol.for(
|
|
113
115
|
"@distilled.cloud/core/http/response-code",
|
|
114
116
|
);
|
|
115
117
|
export const ResponseCode = () => makeAnnotation(responseCodeSymbol, true);
|
|
116
118
|
|
|
117
|
-
export const headerSymbol = Symbol.for("@distilled.cloud/core/http/header");
|
|
118
|
-
export const Header = (name?: string) =>
|
|
119
|
-
makeAnnotation(headerSymbol, name ?? true);
|
|
119
|
+
export const headerSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/header");
|
|
120
|
+
export const Header = (name?: string) => makeAnnotation(headerSymbol, name ?? true);
|
|
120
121
|
|
|
121
|
-
export const bodySymbol = Symbol.for("@distilled.cloud/core/http/body");
|
|
122
|
+
export const bodySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/body");
|
|
122
123
|
export const Body = (name?: string) => makeAnnotation(bodySymbol, name ?? true);
|
|
123
124
|
|
|
124
|
-
export const querySymbol = Symbol.for("@distilled.cloud/core/http/query");
|
|
125
|
-
export const Query = (name?: string) =>
|
|
126
|
-
makeAnnotation(querySymbol, name ?? true);
|
|
125
|
+
export const querySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/query");
|
|
126
|
+
export const Query = (name?: string) => makeAnnotation(querySymbol, name ?? true);
|
|
127
127
|
|
|
128
|
-
export const
|
|
129
|
-
"@distilled.cloud/core/http/
|
|
128
|
+
export const commaDelimitedSymbol = /*@__PURE__*/ Symbol.for(
|
|
129
|
+
"@distilled.cloud/core/http/comma-delimited",
|
|
130
130
|
);
|
|
131
|
+
/**
|
|
132
|
+
* Sends a list-valued `Query` member as ONE comma-separated parameter
|
|
133
|
+
* (`?state=running,paused`) instead of one parameter per item -- OpenAPI's
|
|
134
|
+
* `style: form, explode: false`. Takes the trait value the codegen inlines.
|
|
135
|
+
*/
|
|
136
|
+
export const CommaDelimited = (_trait?: unknown) => makeAnnotation(commaDelimitedSymbol, true);
|
|
137
|
+
|
|
138
|
+
export const deepQuerySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/deep-query");
|
|
131
139
|
/**
|
|
132
140
|
* Binds a struct-valued input member to a family of DOTTED query parameters:
|
|
133
141
|
* `{ account: { id, name } }` with `DeepQuery("account")` serializes as
|
|
134
|
-
* `?account.id
|
|
142
|
+
* `?account.id=...&account.name=...` (null/undefined entries skipped). This is
|
|
135
143
|
* how several list endpoints model their filters on the wire (e.g.
|
|
136
144
|
* Cloudflare zones list `account.id` / `account.name`) while the TS surface
|
|
137
145
|
* keeps the nested-object shape (v0 parity).
|
|
138
146
|
*/
|
|
139
|
-
export const DeepQuery = (name?: string) =>
|
|
140
|
-
makeAnnotation(deepQuerySymbol, name ?? true);
|
|
147
|
+
export const DeepQuery = (name?: string) => makeAnnotation(deepQuerySymbol, name ?? true);
|
|
141
148
|
|
|
142
|
-
export const httpBodySymbol = Symbol.for("@distilled.cloud/core/http-body");
|
|
149
|
+
export const httpBodySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http-body");
|
|
143
150
|
/**
|
|
144
151
|
* Marks the input member whose value IS the entire request body (raw arrays/
|
|
145
|
-
* scalars
|
|
152
|
+
* scalars -- e.g. endpoints that POST a bare JSON array). Mirrors
|
|
146
153
|
* `smithy.api#httpPayload`.
|
|
147
154
|
*/
|
|
148
155
|
export const HttpBody = () => makeAnnotation(httpBodySymbol, true);
|
|
149
156
|
|
|
150
|
-
export const
|
|
151
|
-
"@distilled.cloud/core/
|
|
157
|
+
export const stringEncodedSymbol = /*@__PURE__*/ Symbol.for(
|
|
158
|
+
"@distilled.cloud/core/http/string-encoded",
|
|
152
159
|
);
|
|
160
|
+
/**
|
|
161
|
+
* Marks a member the API only accepts as the STRING spelling of its value --
|
|
162
|
+
* `true` travels as `"true"`, `3` as `"3"` -- while the TS surface keeps the
|
|
163
|
+
* natural type (e.g. Cloudflare's multipart `validation_enabled`, which the
|
|
164
|
+
* docs model as the enum `"true" | "false"`). The protocol stringifies body
|
|
165
|
+
* members on encode, element-wise for lists; `null` is left alone. Query and
|
|
166
|
+
* header bindings already serialize their values as strings, so the trait is
|
|
167
|
+
* documentation there.
|
|
168
|
+
*/
|
|
169
|
+
export const StringEncoded = () => makeAnnotation(stringEncodedSymbol, true);
|
|
170
|
+
|
|
171
|
+
export const formDataFileSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/form-data-file");
|
|
153
172
|
/**
|
|
154
173
|
* Marks an input member holding `File`/`Blob` parts for a multipart upload
|
|
155
174
|
* (`Http({ contentType: "multipart" })`). Each file is appended to the form
|
|
@@ -160,11 +179,9 @@ export const FormDataFile = () => makeAnnotation(formDataFileSymbol, true);
|
|
|
160
179
|
|
|
161
180
|
//#region Generic JSON traits
|
|
162
181
|
|
|
163
|
-
export const keyDictionarySymbol = Symbol.for(
|
|
164
|
-
"@distilled.cloud/core/key-dictionary",
|
|
165
|
-
);
|
|
182
|
+
export const keyDictionarySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/key-dictionary");
|
|
166
183
|
/**
|
|
167
|
-
* Deep TS-name
|
|
184
|
+
* Deep TS-name->wire-name key dictionary for members whose full structure is
|
|
168
185
|
* not modeled (opaque `Document` content). The protocol renames any matching
|
|
169
186
|
* key at any depth on encode (and the reverse on decode); keys not in the
|
|
170
187
|
* dictionary pass through verbatim.
|
|
@@ -175,64 +192,86 @@ export const keyDictionarySymbol = Symbol.for(
|
|
|
175
192
|
* wire name used on encode; decode maps every listed spelling back to the
|
|
176
193
|
* TS name.
|
|
177
194
|
*/
|
|
178
|
-
export type KeyDictionaryEntries = Record<
|
|
179
|
-
string,
|
|
180
|
-
string | ReadonlyArray<string>
|
|
181
|
-
>;
|
|
195
|
+
export type KeyDictionaryEntries = Record<string, string | ReadonlyArray<string>>;
|
|
182
196
|
|
|
183
197
|
export const KeyDictionary = (dict: KeyDictionaryEntries) =>
|
|
184
198
|
makeAnnotation(keyDictionarySymbol, dict);
|
|
185
199
|
|
|
186
|
-
export const unionCasesSymbol = Symbol.for("@distilled.cloud/core/union-cases");
|
|
200
|
+
export const unionCasesSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/union-cases");
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The literal member every case of a union fixes to its own value -- what the
|
|
204
|
+
* docs call the resource `type` or the action `id`. `values[i]` is the value
|
|
205
|
+
* case `i` carries.
|
|
206
|
+
*/
|
|
207
|
+
export interface UnionDiscriminator {
|
|
208
|
+
readonly key: string;
|
|
209
|
+
readonly values: ReadonlyArray<string>;
|
|
210
|
+
}
|
|
211
|
+
|
|
187
212
|
/**
|
|
188
213
|
* Marks an opaque schema standing in for a discriminated union of object
|
|
189
214
|
* cases, carrying each case's TS-facing key set. For APIs that return every
|
|
190
215
|
* case's keys with `null` for the inactive ones, the protocol uses these key
|
|
191
216
|
* sets to pick the active case and drop the others, so consumers' `"key" in
|
|
192
217
|
* value` discrimination works.
|
|
218
|
+
*
|
|
219
|
+
* Key sets alone cannot tell apart cases that share them -- the 41 resource
|
|
220
|
+
* kinds of `resource_tagging`, the 34 page-rule actions -- so when the union
|
|
221
|
+
* has a discriminator the protocol reads the case off it and only falls back
|
|
222
|
+
* to key-set scoring for a value whose tag is missing or unknown.
|
|
193
223
|
*/
|
|
194
|
-
export const UnionCases = (
|
|
195
|
-
|
|
224
|
+
export const UnionCases = (
|
|
225
|
+
cases: ReadonlyArray<ReadonlyArray<string>>,
|
|
226
|
+
discriminator?: UnionDiscriminator,
|
|
227
|
+
) => makeAnnotation(unionCasesSymbol, discriminator ? { cases, discriminator } : { cases });
|
|
196
228
|
//#endregion
|
|
197
229
|
|
|
198
230
|
//#region Error matcher traits
|
|
199
231
|
|
|
200
|
-
export const errorMatchersSymbol = Symbol.for(
|
|
201
|
-
|
|
202
|
-
|
|
232
|
+
export const errorMatchersSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/error-matchers");
|
|
233
|
+
|
|
234
|
+
/** Exact text, or the conjunction of substring and regular-expression constraints. */
|
|
235
|
+
export type ErrorTextMatcher = string | { readonly includes?: string; readonly matches?: string };
|
|
203
236
|
|
|
204
237
|
/**
|
|
205
|
-
* One wire-matching rule
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* `
|
|
238
|
+
* One wire-matching rule. All supplied constraints must match; separate
|
|
239
|
+
* matchers on a class are alternatives. Empty matchers match nothing.
|
|
240
|
+
*
|
|
241
|
+
* `body` maps RFC 6901 JSON Pointers to scalar constraints. For example,
|
|
242
|
+
* `{ "/success": false, "/result/status": "error" }`. Strings also accept
|
|
243
|
+
* `includes` / `matches`. Missing fields never match, including against null.
|
|
244
|
+
* The empty pointer addresses the entire body; array indices are supported.
|
|
245
|
+
* Header names are case-insensitive; their value constraints are case-sensitive.
|
|
246
|
+
* Each body path and header adds one specificity point, like code/status/message.
|
|
247
|
+
* Protocols evaluate these rules only after identifying a failed response.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* { status: 200, body: { "/success": false, "/result/status": "error" } }
|
|
252
|
+
* { status: 409, headers: { "x-error-type": { includes: "Conflict" } } }
|
|
253
|
+
* ```
|
|
210
254
|
*/
|
|
211
255
|
export interface ErrorMatcher {
|
|
212
256
|
readonly code?: number;
|
|
213
257
|
readonly status?: number;
|
|
214
|
-
readonly message?:
|
|
215
|
-
|
|
216
|
-
|
|
258
|
+
readonly message?: ErrorTextMatcher;
|
|
259
|
+
readonly body?: Readonly<Record<string, ErrorTextMatcher | number | boolean | null>>;
|
|
260
|
+
readonly headers?: Readonly<Record<string, ErrorTextMatcher>>;
|
|
217
261
|
}
|
|
218
262
|
|
|
219
263
|
/**
|
|
220
264
|
* Stamp wire-matching rules onto a generated error class. Protocols consult
|
|
221
265
|
* these to decide which of an operation's declared error classes a failed
|
|
222
266
|
* response should surface as (most specific matcher wins; ties break by
|
|
223
|
-
* declaration order
|
|
267
|
+
* declaration order -- see `matchTypedError` in `core/protocol-http`).
|
|
224
268
|
*/
|
|
225
|
-
export const applyErrorMatchers = <C>(
|
|
226
|
-
cls: C,
|
|
227
|
-
matchers: ReadonlyArray<ErrorMatcher>,
|
|
228
|
-
): C => {
|
|
269
|
+
export const applyErrorMatchers = <C>(cls: C, matchers: ReadonlyArray<ErrorMatcher>): C => {
|
|
229
270
|
(cls as any)[errorMatchersSymbol] = matchers;
|
|
230
271
|
return cls;
|
|
231
272
|
};
|
|
232
273
|
|
|
233
274
|
/** Read the matchers stamped on an error class, if any. */
|
|
234
|
-
export const getErrorMatchers = (
|
|
235
|
-
cls: unknown,
|
|
236
|
-
): ReadonlyArray<ErrorMatcher> | undefined =>
|
|
275
|
+
export const getErrorMatchers = (cls: unknown): ReadonlyArray<ErrorMatcher> | undefined =>
|
|
237
276
|
(cls as any)?.[errorMatchersSymbol];
|
|
238
277
|
//#endregion
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fields every SDK's `Unknown<Sdk>Error` carries: what the response said
|
|
3
|
+
* about a failure that no typed error matched. An SDK spreads
|
|
4
|
+
* {@link UnknownErrorFields} into its own class and adds the fields its
|
|
5
|
+
* provider sends (Stripe's `type` and `param`, AWS's `service` and
|
|
6
|
+
* `operation`, ...):
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* export class UnknownNeonError
|
|
10
|
+
* extends Schema.TaggedError<UnknownNeonError>()("UnknownNeonError", UnknownErrorFields) {}
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* The class carries no error category: which statuses reach it, and whether
|
|
14
|
+
* they are worth a retry, is the protocol's call.
|
|
15
|
+
*
|
|
16
|
+
* Defined on its own, in core's `base` bundle group, so AWS uses it without
|
|
17
|
+
* loading the REST modules.
|
|
18
|
+
*/
|
|
19
|
+
import * as Schema from "./schema-lite-typed.ts";
|
|
20
|
+
|
|
21
|
+
export const UnknownErrorFields = {
|
|
22
|
+
/** HTTP status of the response that carried the failure. */
|
|
23
|
+
status: Schema.Number,
|
|
24
|
+
/** The provider's machine-readable error code, when the response has one. */
|
|
25
|
+
code: /*@__PURE__*/ Schema.optional(Schema.String),
|
|
26
|
+
/** The provider's error message, when the response has one. */
|
|
27
|
+
message: /*@__PURE__*/ Schema.optional(Schema.String),
|
|
28
|
+
/** Parsed JSON body, or the raw text when the body wasn't JSON. */
|
|
29
|
+
body: Schema.Unknown,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The values of {@link UnknownErrorFields}. */
|
|
33
|
+
export interface UnknownErrorProps {
|
|
34
|
+
readonly status: number;
|
|
35
|
+
readonly code?: string | undefined;
|
|
36
|
+
readonly message?: string | undefined;
|
|
37
|
+
readonly body: unknown;
|
|
38
|
+
}
|
package/src/xml.ts
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import * as Data from "effect/Data";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
|
|
4
|
+
export type XmlValue = string | XmlObject | XmlValue[];
|
|
5
|
+
export interface XmlObject {
|
|
6
|
+
[name: string]: XmlValue;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class XmlParseError
|
|
10
|
+
extends /*@__PURE__*/ Data.TaggedError("XmlParseError")<{
|
|
11
|
+
readonly message: string;
|
|
12
|
+
/** UTF-16 offset in the original input; line and column are one-based. */
|
|
13
|
+
readonly offset: number;
|
|
14
|
+
readonly line: number;
|
|
15
|
+
readonly column: number;
|
|
16
|
+
}> {}
|
|
17
|
+
|
|
18
|
+
export interface XmlParseOptions {
|
|
19
|
+
/** Maximum open elements, including the root. Defaults to 256. */
|
|
20
|
+
readonly maxDepth?: number;
|
|
21
|
+
/** Maximum input length in UTF-16 code units. Defaults to 64 Mi. */
|
|
22
|
+
readonly maxLength?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// XML 1.0 (Fifth Edition) NameStartChar / NameChar, including astral names.
|
|
26
|
+
const namePattern =
|
|
27
|
+
// XML NameChar explicitly includes combining marks as individual code points.
|
|
28
|
+
// eslint-disable-next-line no-misleading-character-class
|
|
29
|
+
/[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}][:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}.\-0-9\u00B7\u0300-\u036F\u203F-\u2040]*/uy;
|
|
30
|
+
const invalidCharacter =
|
|
31
|
+
// XML 1.0 forbids these literal controls and unpaired surrogates.
|
|
32
|
+
// eslint-disable-next-line no-control-regex
|
|
33
|
+
/[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/u;
|
|
34
|
+
const xmlSpace = /^[\t\n\r ]*$/;
|
|
35
|
+
const declarationPattern =
|
|
36
|
+
/^xml[\t\n\r ]+version[\t\n\r ]*=[\t\n\r ]*(?:"1\.0"|'1\.0')(?:[\t\n\r ]+encoding[\t\n\r ]*=[\t\n\r ]*(?:"[A-Za-z][A-Za-z0-9._-]*"|'[A-Za-z][A-Za-z0-9._-]*'))?(?:[\t\n\r ]+standalone[\t\n\r ]*=[\t\n\r ]*(?:"(?:yes|no)"|'(?:yes|no)'))?[\t\n\r ]*$/;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The prototype of every object the parser returns. It has no prototype
|
|
40
|
+
* itself, so element names such as `__proto__` or `constructor` are plain
|
|
41
|
+
* own keys, as with a null prototype -- but V8 keeps objects with a
|
|
42
|
+
* non-null prototype in fast mode, which makes parsing several times faster.
|
|
43
|
+
*/
|
|
44
|
+
const nodeProto: object = /*@__PURE__*/ Object.create(null);
|
|
45
|
+
|
|
46
|
+
/** An ASCII XML name not followed by a non-ASCII (possibly name) character. */
|
|
47
|
+
const asciiName = /[A-Za-z_:][\w:.-]*(?![^\0-\x7f])/y;
|
|
48
|
+
|
|
49
|
+
interface Element {
|
|
50
|
+
name: string;
|
|
51
|
+
object: XmlObject;
|
|
52
|
+
text: string;
|
|
53
|
+
hasChildren: boolean;
|
|
54
|
+
hasAttributes: boolean;
|
|
55
|
+
hasCdata: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Parse DTD-free XML 1.0. Values remain strings;
|
|
60
|
+
* repeated children become arrays, attributes use @_ and mixed text uses #text.
|
|
61
|
+
* Names are preserved verbatim (no namespace resolution). Comments and PIs are
|
|
62
|
+
* ignored. Text whitespace is preserved, except layout-only text in containers.
|
|
63
|
+
* Empty HTTP bodies produce {}. This is not a DOM or a validating DTD processor.
|
|
64
|
+
* Throws XmlParseError for malformed input or exceeded limits.
|
|
65
|
+
* The iterative scanner allocates no Effects while traversing the document.
|
|
66
|
+
*/
|
|
67
|
+
export function parseXmlSync(xml: string, options: XmlParseOptions = {}): XmlObject {
|
|
68
|
+
let pos = xml.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
69
|
+
const start = pos;
|
|
70
|
+
const maxDepth = options.maxDepth ?? 256;
|
|
71
|
+
const maxLength = options.maxLength ?? 64 * 1024 * 1024;
|
|
72
|
+
const stack: Element[] = [];
|
|
73
|
+
const document: XmlObject = Object.create(nodeProto);
|
|
74
|
+
let hasRoot = false;
|
|
75
|
+
|
|
76
|
+
function fail(message: string, offset = pos): never {
|
|
77
|
+
offset = Math.min(offset, xml.length);
|
|
78
|
+
const lines = xml.slice(0, offset).split(/\r\n|[\r\n]/);
|
|
79
|
+
const line = lines.length;
|
|
80
|
+
const column = lines[line - 1].length + 1;
|
|
81
|
+
throw new XmlParseError({
|
|
82
|
+
message: `${message} at ${line}:${column}`,
|
|
83
|
+
offset,
|
|
84
|
+
line,
|
|
85
|
+
column,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!Number.isSafeInteger(maxDepth) || maxDepth < 1) fail("Invalid maxDepth");
|
|
90
|
+
if (!Number.isSafeInteger(maxLength) || maxLength < 0) fail("Invalid maxLength");
|
|
91
|
+
if (xml.length > maxLength) fail("XML input length limit exceeded", 0);
|
|
92
|
+
const invalid = invalidCharacter.exec(xml);
|
|
93
|
+
if (invalid) fail("Invalid XML character", invalid.index);
|
|
94
|
+
if (xmlSpace.test(xml.slice(start))) return document;
|
|
95
|
+
|
|
96
|
+
function skipSpace(): boolean {
|
|
97
|
+
const before = pos;
|
|
98
|
+
while (pos < xml.length) {
|
|
99
|
+
const c = xml.charCodeAt(pos);
|
|
100
|
+
if (c !== 32 && c !== 9 && c !== 10 && c !== 13) break;
|
|
101
|
+
pos++;
|
|
102
|
+
}
|
|
103
|
+
return pos !== before;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function readName(): string {
|
|
107
|
+
// ASCII names (most documents) match a cheaper, non-Unicode pattern first.
|
|
108
|
+
asciiName.lastIndex = namePattern.lastIndex = pos;
|
|
109
|
+
const match = asciiName.exec(xml) ?? namePattern.exec(xml);
|
|
110
|
+
if (!match) fail("Expected XML name");
|
|
111
|
+
pos += match[0].length;
|
|
112
|
+
return match[0];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function decode(raw: string, offset: number, attribute = false): string {
|
|
116
|
+
if (raw.indexOf("&") < 0 && !(attribute ? /[\t\r\n]/ : /\r/).test(raw)) return raw;
|
|
117
|
+
const parts: string[] = [];
|
|
118
|
+
let from = 0;
|
|
119
|
+
while (from < raw.length) {
|
|
120
|
+
const amp = raw.indexOf("&", from);
|
|
121
|
+
const end = amp < 0 ? raw.length : amp;
|
|
122
|
+
const literal = raw.slice(from, end);
|
|
123
|
+
parts.push(
|
|
124
|
+
attribute ? literal.replace(/\r\n|[\t\r\n]/g, " ") : literal.replace(/\r\n?/g, "\n"),
|
|
125
|
+
);
|
|
126
|
+
if (amp < 0) break;
|
|
127
|
+
const semi = raw.indexOf(";", amp + 1);
|
|
128
|
+
if (semi < 0) fail("Unterminated entity reference", offset + amp);
|
|
129
|
+
const entity = raw.slice(amp + 1, semi);
|
|
130
|
+
switch (entity) {
|
|
131
|
+
case "amp":
|
|
132
|
+
parts.push("&");
|
|
133
|
+
break;
|
|
134
|
+
case "lt":
|
|
135
|
+
parts.push("<");
|
|
136
|
+
break;
|
|
137
|
+
case "gt":
|
|
138
|
+
parts.push(">");
|
|
139
|
+
break;
|
|
140
|
+
case "quot":
|
|
141
|
+
parts.push('"');
|
|
142
|
+
break;
|
|
143
|
+
case "apos":
|
|
144
|
+
parts.push("'");
|
|
145
|
+
break;
|
|
146
|
+
default: {
|
|
147
|
+
if (!/^#(?:[0-9]+|x[0-9a-fA-F]+)$/.test(entity)) {
|
|
148
|
+
fail("Unknown or invalid entity reference", offset + amp);
|
|
149
|
+
}
|
|
150
|
+
const code =
|
|
151
|
+
entity[1] === "x"
|
|
152
|
+
? Number.parseInt(entity.slice(2), 16)
|
|
153
|
+
: Number.parseInt(entity.slice(1), 10);
|
|
154
|
+
if (
|
|
155
|
+
!(
|
|
156
|
+
code === 9 ||
|
|
157
|
+
code === 10 ||
|
|
158
|
+
code === 13 ||
|
|
159
|
+
(code >= 0x20 && code <= 0xd7ff) ||
|
|
160
|
+
(code >= 0xe000 && code <= 0xfffd) ||
|
|
161
|
+
(code >= 0x10000 && code <= 0x10ffff)
|
|
162
|
+
)
|
|
163
|
+
) {
|
|
164
|
+
fail("Invalid character reference", offset + amp);
|
|
165
|
+
}
|
|
166
|
+
parts.push(String.fromCodePoint(code));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
from = semi + 1;
|
|
170
|
+
}
|
|
171
|
+
return parts.join("");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function finish(element: Element): void {
|
|
175
|
+
const text = element.text;
|
|
176
|
+
let value: XmlValue;
|
|
177
|
+
if (!element.hasChildren && !element.hasAttributes) {
|
|
178
|
+
value = text;
|
|
179
|
+
} else {
|
|
180
|
+
if (text && (!element.hasChildren || element.hasCdata || !xmlSpace.test(text))) {
|
|
181
|
+
element.object["#text"] = text;
|
|
182
|
+
}
|
|
183
|
+
value = element.object;
|
|
184
|
+
}
|
|
185
|
+
const parent = stack[stack.length - 1];
|
|
186
|
+
const target = parent ? parent.object : document;
|
|
187
|
+
if (parent) parent.hasChildren = true;
|
|
188
|
+
if (!Object.hasOwn(target, element.name)) target[element.name] = value;
|
|
189
|
+
else {
|
|
190
|
+
const previous = target[element.name];
|
|
191
|
+
if (Array.isArray(previous)) previous.push(value);
|
|
192
|
+
else target[element.name] = [previous, value];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
while (pos < xml.length) {
|
|
197
|
+
if (xml[pos] !== "<") {
|
|
198
|
+
const offset = pos;
|
|
199
|
+
const next = xml.indexOf("<", pos);
|
|
200
|
+
pos = next < 0 ? xml.length : next;
|
|
201
|
+
const raw = xml.slice(offset, pos);
|
|
202
|
+
const element = stack[stack.length - 1];
|
|
203
|
+
if (!element) {
|
|
204
|
+
if (!xmlSpace.test(raw)) fail("Text outside root element", offset);
|
|
205
|
+
} else {
|
|
206
|
+
const cdataEnd = raw.indexOf("]]>");
|
|
207
|
+
if (cdataEnd >= 0) fail("Unexpected CDATA terminator", offset + cdataEnd);
|
|
208
|
+
element.text += decode(raw, offset);
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (xml.startsWith("<!--", pos)) {
|
|
214
|
+
const end = xml.indexOf("-->", pos + 4);
|
|
215
|
+
if (end < 0) fail("Unterminated comment");
|
|
216
|
+
const comment = xml.slice(pos + 4, end);
|
|
217
|
+
if (comment.includes("--") || comment.endsWith("-")) fail("Invalid comment");
|
|
218
|
+
pos = end + 3;
|
|
219
|
+
} else if (xml.startsWith("<![CDATA[", pos)) {
|
|
220
|
+
const element = stack[stack.length - 1];
|
|
221
|
+
if (!element) fail("CDATA outside root element");
|
|
222
|
+
const end = xml.indexOf("]]>", pos + 9);
|
|
223
|
+
if (end < 0) fail("Unterminated CDATA");
|
|
224
|
+
element.text += xml.slice(pos + 9, end).replace(/\r\n?/g, "\n");
|
|
225
|
+
element.hasCdata = true;
|
|
226
|
+
pos = end + 3;
|
|
227
|
+
} else if (xml.startsWith("<?", pos)) {
|
|
228
|
+
const offset = pos;
|
|
229
|
+
pos += 2;
|
|
230
|
+
const target = readName();
|
|
231
|
+
if (!xml.startsWith("?>", pos) && !skipSpace()) fail("Expected PI whitespace");
|
|
232
|
+
const end = xml.indexOf("?>", pos);
|
|
233
|
+
if (end < 0) fail("Unterminated processing instruction", offset);
|
|
234
|
+
if (target.toLowerCase() === "xml") {
|
|
235
|
+
if (offset !== start || !declarationPattern.test(xml.slice(offset + 2, end))) {
|
|
236
|
+
fail("Invalid or misplaced XML declaration", offset);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
pos = end + 2;
|
|
240
|
+
} else if (xml.startsWith("<!", pos)) {
|
|
241
|
+
fail("DTDs and markup declarations are not supported");
|
|
242
|
+
} else if (xml.startsWith("</", pos)) {
|
|
243
|
+
pos += 2;
|
|
244
|
+
const name = readName();
|
|
245
|
+
skipSpace();
|
|
246
|
+
if (xml[pos] !== ">") fail("Expected closing tag terminator");
|
|
247
|
+
const element = stack.pop();
|
|
248
|
+
if (!element || element.name !== name) fail("Mismatched closing tag");
|
|
249
|
+
pos++;
|
|
250
|
+
finish(element);
|
|
251
|
+
} else {
|
|
252
|
+
if (stack.length >= maxDepth) fail("XML depth limit exceeded");
|
|
253
|
+
if (stack.length === 0) {
|
|
254
|
+
if (hasRoot) fail("Multiple root elements");
|
|
255
|
+
hasRoot = true;
|
|
256
|
+
}
|
|
257
|
+
pos++;
|
|
258
|
+
const element: Element = {
|
|
259
|
+
name: readName(),
|
|
260
|
+
object: Object.create(nodeProto),
|
|
261
|
+
text: "",
|
|
262
|
+
hasChildren: false,
|
|
263
|
+
hasAttributes: false,
|
|
264
|
+
hasCdata: false,
|
|
265
|
+
};
|
|
266
|
+
while (true) {
|
|
267
|
+
const spaced = skipSpace();
|
|
268
|
+
if (xml.startsWith("/>", pos)) {
|
|
269
|
+
pos += 2;
|
|
270
|
+
finish(element);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
if (xml[pos] === ">") {
|
|
274
|
+
pos++;
|
|
275
|
+
stack.push(element);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
if (!spaced) fail("Expected whitespace or tag terminator");
|
|
279
|
+
const name = readName();
|
|
280
|
+
const key = `@_${name}`;
|
|
281
|
+
if (Object.hasOwn(element.object, key)) fail("Duplicate attribute");
|
|
282
|
+
skipSpace();
|
|
283
|
+
if (xml[pos++] !== "=") fail("Expected attribute equals sign");
|
|
284
|
+
skipSpace();
|
|
285
|
+
const quote = xml[pos++];
|
|
286
|
+
if (quote !== '"' && quote !== "'") fail("Expected quoted attribute");
|
|
287
|
+
const end = xml.indexOf(quote, pos);
|
|
288
|
+
if (end < 0) fail("Unterminated attribute");
|
|
289
|
+
const raw = xml.slice(pos, end);
|
|
290
|
+
if (raw.includes("<")) fail("Unescaped '<' in attribute");
|
|
291
|
+
element.object[key] = decode(raw, pos, true);
|
|
292
|
+
element.hasAttributes = true;
|
|
293
|
+
pos = end + 1;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (stack.length) fail("Unclosed element");
|
|
298
|
+
if (!hasRoot) fail("Missing root element");
|
|
299
|
+
return document;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Parse XML lazily, reporting malformed documents in the typed error channel. */
|
|
303
|
+
export const parseXml = (
|
|
304
|
+
xml: string,
|
|
305
|
+
options?: XmlParseOptions,
|
|
306
|
+
): Effect.Effect<XmlObject, XmlParseError> =>
|
|
307
|
+
Effect.suspend(() => {
|
|
308
|
+
try {
|
|
309
|
+
return Effect.succeed(parseXmlSync(xml, options));
|
|
310
|
+
} catch (error) {
|
|
311
|
+
if (error instanceof XmlParseError) return Effect.fail(error);
|
|
312
|
+
// Anything else the parser throws (a stack overflow on deep nesting,
|
|
313
|
+
// ...) still means this document could not be parsed.
|
|
314
|
+
return Effect.fail(
|
|
315
|
+
new XmlParseError({
|
|
316
|
+
message: `XML could not be parsed: ${error instanceof Error ? error.message : String(error)}`,
|
|
317
|
+
offset: 0,
|
|
318
|
+
line: 1,
|
|
319
|
+
column: 1,
|
|
320
|
+
}),
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const xmlEscapes: Record<string, string> = {
|
|
326
|
+
"&": "&",
|
|
327
|
+
"<": "<",
|
|
328
|
+
">": ">",
|
|
329
|
+
'"': """,
|
|
330
|
+
"'": "'",
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Escape special XML characters in a string.
|
|
335
|
+
*/
|
|
336
|
+
export const escapeXml = (s: string): string => s.replace(/[&<>"']/g, (c) => xmlEscapes[c]);
|
|
337
|
+
|
|
338
|
+
// =============================================================================
|
|
339
|
+
// XML Tag Helpers
|
|
340
|
+
// =============================================================================
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Wrap content in an XML tag with optional xmlns attribute.
|
|
344
|
+
*/
|
|
345
|
+
export const wrapTag = (tag: string, content: string, xmlns?: string): string =>
|
|
346
|
+
`<${tag}${xmlns ? ` xmlns="${escapeXml(xmlns)}"` : ""}>${content}</${tag}>`;
|