@distilled.cloud/core 1.0.0-rc.17 → 1.0.0-rc.19
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/_bundle/base.js +2 -2
- package/lib/_bundle/base.js.map +1 -1
- package/lib/_bundle/graphql.js +2 -2
- package/lib/_bundle/graphql.js.map +1 -1
- package/lib/_bundle/grpc.js +2 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +1 -1
- package/lib/_bundle/rest.js.map +1 -1
- package/lib/_bundle/xml.js +1 -1
- package/lib/_bundle/xml.js.map +1 -1
- 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 +52 -1
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/config.d.ts +54 -13
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/errors.d.ts +3 -13
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +26 -5
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.types.d.ts.map +1 -1
- 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 +6 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -0
- package/lib/pagination.d.ts +55 -3
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -1
- 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 +38 -9
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -1
- package/lib/protocol-rest.d.ts +60 -42
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -1
- package/lib/response-validation.d.ts +5 -4
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/schema-lite-typed.js +1 -2
- 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 +7 -0
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/package.json +3 -5
- package/src/annotations.ts +53 -0
- package/src/api.ts +316 -43
- package/src/category.ts +21 -22
- package/src/config.ts +58 -29
- package/src/error-category.ts +12 -14
- package/src/errors.ts +11 -7
- package/src/graphql.ts +191 -63
- package/src/graphql.types.ts +5 -1
- 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 +6 -2
- package/src/pagination.ts +193 -8
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +87 -67
- package/src/protocol-rest.ts +183 -191
- package/src/response-validation.ts +8 -5
- 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 +10 -0
- package/src/xml.ts +10 -1
- package/lib/codegen/boolean-string-enums.d.ts +0 -36
- package/lib/codegen/boolean-string-enums.d.ts.map +0 -1
- package/lib/codegen/boolean-string-enums.js +0 -147
- package/lib/codegen/boolean-string-enums.js.map +0 -1
- package/lib/codegen/chunk.d.ts +0 -7
- package/lib/codegen/chunk.d.ts.map +0 -1
- package/lib/codegen/chunk.js +0 -139
- package/lib/codegen/chunk.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -80
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -166
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/datafy.d.ts +0 -14
- package/lib/codegen/datafy.d.ts.map +0 -1
- package/lib/codegen/datafy.js +0 -853
- package/lib/codegen/datafy.js.map +0 -1
- package/lib/codegen/dedupe.d.ts +0 -5
- package/lib/codegen/dedupe.d.ts.map +0 -1
- package/lib/codegen/dedupe.js +0 -153
- package/lib/codegen/dedupe.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -125
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -99
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -11
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -32
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generated-at.d.ts +0 -29
- package/lib/codegen/generated-at.d.ts.map +0 -1
- package/lib/codegen/generated-at.js +0 -78
- package/lib/codegen/generated-at.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -353
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -810
- 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/graphql-client.d.ts +0 -70
- package/lib/codegen/graphql-client.d.ts.map +0 -1
- package/lib/codegen/graphql-client.js +0 -439
- package/lib/codegen/graphql-client.js.map +0 -1
- package/lib/codegen/graphql.d.ts +0 -207
- package/lib/codegen/graphql.d.ts.map +0 -1
- package/lib/codegen/graphql.js +0 -786
- package/lib/codegen/graphql.js.map +0 -1
- package/lib/codegen/latin1.d.ts +0 -3
- package/lib/codegen/latin1.d.ts.map +0 -1
- package/lib/codegen/latin1.js +0 -126
- package/lib/codegen/latin1.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -32
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -62
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/model-diff.d.ts +0 -10
- package/lib/codegen/model-diff.d.ts.map +0 -1
- package/lib/codegen/model-diff.js +0 -67
- package/lib/codegen/model-diff.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 -52
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -84
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -209
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1548
- 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 -31
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/patch-audit.d.ts +0 -84
- package/lib/codegen/patch-audit.d.ts.map +0 -1
- package/lib/codegen/patch-audit.js +0 -366
- package/lib/codegen/patch-audit.js.map +0 -1
- package/lib/codegen/patches.d.ts +0 -129
- package/lib/codegen/patches.d.ts.map +0 -1
- package/lib/codegen/patches.js +0 -472
- package/lib/codegen/patches.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/codegen/privatize.d.ts +0 -21
- package/lib/codegen/privatize.d.ts.map +0 -1
- package/lib/codegen/privatize.js +0 -176
- package/lib/codegen/privatize.js.map +0 -1
- package/lib/codegen/proto.d.ts +0 -121
- package/lib/codegen/proto.d.ts.map +0 -1
- package/lib/codegen/proto.js +0 -960
- package/lib/codegen/proto.js.map +0 -1
- package/lib/codegen/pure.d.ts +0 -6
- package/lib/codegen/pure.d.ts.map +0 -1
- package/lib/codegen/pure.js +0 -91
- package/lib/codegen/pure.js.map +0 -1
- package/lib/codegen/rewrite-operation-ids.d.ts +0 -154
- package/lib/codegen/rewrite-operation-ids.d.ts.map +0 -1
- package/lib/codegen/rewrite-operation-ids.js +0 -1117
- package/lib/codegen/rewrite-operation-ids.js.map +0 -1
- package/lib/codegen/spec-path.d.ts +0 -16
- package/lib/codegen/spec-path.d.ts.map +0 -1
- package/lib/codegen/spec-path.js +0 -101
- package/lib/codegen/spec-path.js.map +0 -1
- package/lib/codegen/table.d.ts +0 -5
- package/lib/codegen/table.d.ts.map +0 -1
- package/lib/codegen/table.js +0 -249
- package/lib/codegen/table.js.map +0 -1
- package/lib/debug.d.ts +0 -2
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js +0 -1
- package/lib/json-patch.d.ts +0 -58
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -2
- package/lib/json-patch.js.map +0 -1
- package/lib/schema-lite-typed.js.map +0 -1
- package/lib/testing.d.ts +0 -40
- package/lib/testing.d.ts.map +0 -1
- package/lib/testing.js +0 -2
- package/lib/testing.js.map +0 -1
- package/src/codegen/boolean-string-enums.ts +0 -155
- package/src/codegen/chunk.ts +0 -147
- package/src/codegen/cli.ts +0 -293
- package/src/codegen/datafy.ts +0 -874
- package/src/codegen/dedupe.ts +0 -173
- package/src/codegen/emit.ts +0 -200
- package/src/codegen/format.ts +0 -49
- package/src/codegen/generated-at.ts +0 -88
- package/src/codegen/generator.ts +0 -1229
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/graphql-client.ts +0 -545
- package/src/codegen/graphql.ts +0 -1145
- package/src/codegen/latin1.ts +0 -131
- package/src/codegen/members.ts +0 -79
- package/src/codegen/model-diff.ts +0 -67
- package/src/codegen/naming.ts +0 -85
- package/src/codegen/openapi-cli.ts +0 -141
- package/src/codegen/openapi.ts +0 -1845
- package/src/codegen/operations.ts +0 -73
- package/src/codegen/pagination.ts +0 -64
- package/src/codegen/patch-audit.ts +0 -480
- package/src/codegen/patches.ts +0 -581
- package/src/codegen/prelude.ts +0 -70
- package/src/codegen/privatize.ts +0 -200
- package/src/codegen/proto.ts +0 -1092
- package/src/codegen/pure.ts +0 -94
- package/src/codegen/rewrite-operation-ids.ts +0 -1219
- package/src/codegen/spec-path.ts +0 -112
- package/src/codegen/table.ts +0 -254
- package/src/debug.ts +0 -8
- package/src/json-patch.ts +0 -273
- package/src/testing.ts +0 -74
package/src/codegen/spec-path.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Spec path resolution — the seam between a production spec mirror and a
|
|
3
|
-
* local working copy of one (dev-time only).
|
|
4
|
-
*
|
|
5
|
-
* Every SDK package reads its specs from `packages/<pkg>/specs/`. In
|
|
6
|
-
* production that directory holds a submodule of the package's mirror
|
|
7
|
-
* repository, `distilled-mirror/spec-mirror-<pkg>`, whose layout is fixed:
|
|
8
|
-
*
|
|
9
|
-
* packages/<pkg>/specs/spec-mirror-<pkg>/
|
|
10
|
-
* ├── .meta/ fetch-specs.ts + its package.json
|
|
11
|
-
* └── specs/ what the generator reads
|
|
12
|
-
*
|
|
13
|
-
* A contributor adding a provider cannot create that repository — it lives in
|
|
14
|
-
* an org they have no write access to, and it is only created once the
|
|
15
|
-
* `distilled-submodules` stack deploys on merge to main. So `pnpm specs:local
|
|
16
|
-
* <pkg>` runs the same `fetch-specs.ts` into a gitignored directory shaped
|
|
17
|
-
* exactly like the mirror:
|
|
18
|
-
*
|
|
19
|
-
* packages/<pkg>/specs/.local/
|
|
20
|
-
* ├── .meta/
|
|
21
|
-
* └── specs/
|
|
22
|
-
*
|
|
23
|
-
* Setting {@link LOCAL_ENV} then re-roots every spec read into it. Nothing in
|
|
24
|
-
* the package's source changes — the `specPath` a `convert.ts` declares stays
|
|
25
|
-
* the production one — so there is no `.local` reference that could be
|
|
26
|
-
* committed by accident, and the switch is impossible to leave on: it lives
|
|
27
|
-
* in the environment of one command, never in a file.
|
|
28
|
-
*
|
|
29
|
-
* The opt-in is deliberate rather than a fallback-when-missing. A silent
|
|
30
|
-
* fallback would regenerate an SDK from a stale `.local` copy the moment a
|
|
31
|
-
* submodule failed to check out, and the result — committed, formatted,
|
|
32
|
-
* plausible — would be indistinguishable from a real regeneration.
|
|
33
|
-
*/
|
|
34
|
-
import * as fs from "node:fs";
|
|
35
|
-
import * as path from "node:path";
|
|
36
|
-
|
|
37
|
-
/** Set to a non-empty value to read specs from `specs/.local` instead. */
|
|
38
|
-
export const LOCAL_ENV = "DISTILLED_SPECS_LOCAL";
|
|
39
|
-
|
|
40
|
-
/** The local working copy of a mirror, relative to a package root. */
|
|
41
|
-
export const LOCAL_DIR = path.join("specs", ".local");
|
|
42
|
-
|
|
43
|
-
/** Repository name of the mirror feeding `packages/<pkg>`. */
|
|
44
|
-
export const mirrorRepositoryName = (pkg: string) => `spec-mirror-${pkg}`;
|
|
45
|
-
|
|
46
|
-
/** Whether this process reads specs from `specs/.local`. */
|
|
47
|
-
export const isLocalSpecs = (): boolean => (process.env[LOCAL_ENV] ?? "").trim() !== "";
|
|
48
|
-
|
|
49
|
-
let announced = false;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The path within a mirror's `specs/` directory that `specPath` names.
|
|
53
|
-
*
|
|
54
|
-
* Spec paths take one of two shapes — `specs/<mirror>/specs/<tail>` for a
|
|
55
|
-
* submodule and `specs/<tail>` for a committed file — and in both the tail is
|
|
56
|
-
* everything after the LAST `specs` segment. That one rule covers the
|
|
57
|
-
* submodule form, the committed form, the nested forms
|
|
58
|
-
* (`specs/<mirror>/specs/models/<service>/...`) and the directory form
|
|
59
|
-
* (`specs/<mirror>/specs`, whose tail is empty — the mirror's `specs/`
|
|
60
|
-
* directory itself) without needing to know which it was handed.
|
|
61
|
-
*
|
|
62
|
-
* `undefined` means there is no `specs` segment at all, which local mode
|
|
63
|
-
* cannot interpret.
|
|
64
|
-
*/
|
|
65
|
-
const tailWithinSpecs = (specPath: string): string | undefined => {
|
|
66
|
-
const segments = specPath.split(/[/\\]/).filter((s) => s !== "" && s !== ".");
|
|
67
|
-
const last = segments.lastIndexOf("specs");
|
|
68
|
-
if (last === -1) return undefined;
|
|
69
|
-
return segments.slice(last + 1).join(path.sep);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Resolve a package-relative spec path to an absolute one.
|
|
74
|
-
*
|
|
75
|
-
* @param root absolute package root (`path.resolve(import.meta.dirname, "..")`)
|
|
76
|
-
* @param specPath the production path, relative to `root` (or absolute)
|
|
77
|
-
*/
|
|
78
|
-
export const resolveSpecPath = (root: string, specPath: string): string => {
|
|
79
|
-
const production = path.resolve(root, specPath);
|
|
80
|
-
if (!isLocalSpecs() || path.isAbsolute(specPath)) return production;
|
|
81
|
-
|
|
82
|
-
const tail = tailWithinSpecs(specPath);
|
|
83
|
-
if (tail === undefined) {
|
|
84
|
-
throw new Error(
|
|
85
|
-
`${LOCAL_ENV} is set, but "${specPath}" has no \`specs/\` segment to ` +
|
|
86
|
-
`re-root. Local mode only understands paths under the package's ` +
|
|
87
|
-
`specs/ directory.`,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (!announced) {
|
|
92
|
-
announced = true;
|
|
93
|
-
// stderr, not stdout: generate-all captures stdout per package and only
|
|
94
|
-
// prints it on failure, and this is exactly the line you need to see on
|
|
95
|
-
// a run that succeeded against the wrong specs.
|
|
96
|
-
console.error(`⚠ ${LOCAL_ENV} — reading specs from ${LOCAL_DIR}, not the mirror submodule.`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const local = path.join(root, LOCAL_DIR, "specs", tail);
|
|
100
|
-
if (!fs.existsSync(local)) {
|
|
101
|
-
// A missing local spec is a setup mistake, not a generator bug, and the
|
|
102
|
-
// ENOENT the caller would otherwise raise names a path nobody recognises.
|
|
103
|
-
throw new Error(
|
|
104
|
-
`${local} does not exist. Materialise this package's mirror first:\n` +
|
|
105
|
-
` pnpm specs:local ${path.basename(root)}\n` +
|
|
106
|
-
`If that succeeds and this path is still missing, the package's ` +
|
|
107
|
-
`declared spec path (${specPath}) is not mirror-shaped yet — it must ` +
|
|
108
|
-
`read \`specs/<mirror>/specs/<file>\` for local mode to find it.`,
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
return local;
|
|
112
|
-
};
|
package/src/codegen/table.ts
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Replace module-private schema consts with a lazily filled table
|
|
3
|
-
* (dev-time only; runs after `chunkSchemas`).
|
|
4
|
-
*
|
|
5
|
-
* const Customer = __L.at(__c12, 3) as any as S.Schema<Customer>;
|
|
6
|
-
* const Customer2 = Customer;
|
|
7
|
-
* const Status = S.String;
|
|
8
|
-
* ... S.optional(Customer2) ... S.optional(Status) ...
|
|
9
|
-
* becomes
|
|
10
|
-
* ... S.optional(__S(99, __c12, 3)) ... S.optional(S.String) ...
|
|
11
|
-
* const __s: any[] = [];
|
|
12
|
-
* function __S(i: number, f: (j: number) => unknown, j: number): any { return (__s[i] ??= __L.at(f, j)); }
|
|
13
|
-
*
|
|
14
|
-
* A top-level const costs memory at import even when its schema is never
|
|
15
|
-
* built: a context slot, its name in the module's scope info, bytecode in
|
|
16
|
-
* the module body and the lazy wrapper it holds. With the table, importing
|
|
17
|
-
* a module allocates nothing per schema; a schema's lazy wrapper is made the
|
|
18
|
-
* first time any code reaches it, and is the same object every time after.
|
|
19
|
-
*/
|
|
20
|
-
import { parseAst } from "rolldown/parseAst";
|
|
21
|
-
|
|
22
|
-
type Node = { readonly type: string; readonly start: number; readonly end: number } & Record<
|
|
23
|
-
string,
|
|
24
|
-
unknown
|
|
25
|
-
>;
|
|
26
|
-
|
|
27
|
-
const isNode = (v: unknown): v is Node =>
|
|
28
|
-
v !== null && typeof v === "object" && typeof (v as Node).type === "string";
|
|
29
|
-
|
|
30
|
-
const stripCasts = (n: Node): Node => {
|
|
31
|
-
while (
|
|
32
|
-
n.type === "TSAsExpression" ||
|
|
33
|
-
n.type === "TSSatisfiesExpression" ||
|
|
34
|
-
n.type === "ParenthesizedExpression"
|
|
35
|
-
)
|
|
36
|
-
n = n.expression as Node;
|
|
37
|
-
return n;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export const tableSchemas = (
|
|
41
|
-
code: string,
|
|
42
|
-
filename = "generated.ts",
|
|
43
|
-
): { code: string; tabled: number } => {
|
|
44
|
-
const program = parseAst(code, { sourceType: "module", lang: "ts" }, filename);
|
|
45
|
-
const body = program.body as unknown as Node[];
|
|
46
|
-
|
|
47
|
-
type Entry =
|
|
48
|
-
| { kind: "at"; chunk: number; j: number; cast?: string }
|
|
49
|
-
| { kind: "alias"; to: string; cast?: string }
|
|
50
|
-
| { kind: "leaf"; text: string; cast?: string };
|
|
51
|
-
const entries = new Map<string, Entry>();
|
|
52
|
-
const removals: Array<[number, number]> = [];
|
|
53
|
-
const declStart = new Map<string, number>();
|
|
54
|
-
let chunkSize = 0;
|
|
55
|
-
|
|
56
|
-
for (const st of body) {
|
|
57
|
-
if (st.type !== "VariableDeclaration" || (st.declarations as Node[]).length !== 1) continue;
|
|
58
|
-
const d = (st.declarations as Node[])[0]!;
|
|
59
|
-
const id = d.id as Node;
|
|
60
|
-
if (id.type !== "Identifier" || !isNode(d.init)) continue;
|
|
61
|
-
const name = id.name as string;
|
|
62
|
-
if (name.startsWith("__")) continue;
|
|
63
|
-
const init = stripCasts(d.init);
|
|
64
|
-
let entry: Entry | undefined;
|
|
65
|
-
if (
|
|
66
|
-
init.type === "CallExpression" &&
|
|
67
|
-
code.slice((init.callee as Node).start, (init.callee as Node).end) === "__L.at"
|
|
68
|
-
) {
|
|
69
|
-
const [c, j] = init.arguments as Node[];
|
|
70
|
-
const m = /^__c(\d+)$/.exec(code.slice(c!.start, c!.end));
|
|
71
|
-
if (m && j!.type === "Literal")
|
|
72
|
-
entry = { kind: "at", chunk: Number(m[1]), j: j!.value as number };
|
|
73
|
-
} else if (init.type === "Identifier") {
|
|
74
|
-
entry = { kind: "alias", to: init.name as string };
|
|
75
|
-
} else if (
|
|
76
|
-
init.type === "MemberExpression" &&
|
|
77
|
-
!init.computed &&
|
|
78
|
-
(init.object as Node).type === "Identifier" &&
|
|
79
|
-
(init.object as Node).name === "S"
|
|
80
|
-
) {
|
|
81
|
-
entry = { kind: "leaf", text: code.slice(init.start, init.end) };
|
|
82
|
-
}
|
|
83
|
-
if (!entry) continue;
|
|
84
|
-
// `as any as S.Schema<X>`: the type operation configs infer from.
|
|
85
|
-
const cast = /\bas (?:any as )?(S\.(?:Schema|Codec)<[\s\S]*>)\s*$/.exec(
|
|
86
|
-
code.slice(init.end, (d.init as Node).end),
|
|
87
|
-
)?.[1];
|
|
88
|
-
if (cast) entry.cast = cast;
|
|
89
|
-
entries.set(name, entry);
|
|
90
|
-
declStart.set(name, st.start);
|
|
91
|
-
removals.push([st.start, st.end]);
|
|
92
|
-
}
|
|
93
|
-
// Names also bound inside functions (params, locals, destructuring) stay consts: no scope analysis needed.
|
|
94
|
-
const nested = new Set<string>();
|
|
95
|
-
const collectBindings = (n: unknown): void => {
|
|
96
|
-
if (Array.isArray(n)) return n.forEach(collectBindings);
|
|
97
|
-
if (!isNode(n)) return;
|
|
98
|
-
if (n.type === "Identifier") nested.add(n.name as string);
|
|
99
|
-
else if (n.type === "ObjectPattern")
|
|
100
|
-
for (const p of n.properties as Node[])
|
|
101
|
-
collectBindings(p.type === "RestElement" ? p.argument : p.value);
|
|
102
|
-
else if (n.type === "ArrayPattern") for (const e of n.elements as Node[]) collectBindings(e);
|
|
103
|
-
else if (n.type === "AssignmentPattern") collectBindings(n.left);
|
|
104
|
-
else if (n.type === "RestElement") collectBindings(n.argument);
|
|
105
|
-
};
|
|
106
|
-
const scan = (n: unknown, inFunction: boolean): void => {
|
|
107
|
-
if (Array.isArray(n)) return n.forEach((c) => scan(c, inFunction));
|
|
108
|
-
if (!isNode(n)) return;
|
|
109
|
-
const fn =
|
|
110
|
-
n.type === "ArrowFunctionExpression" ||
|
|
111
|
-
n.type === "FunctionExpression" ||
|
|
112
|
-
n.type === "FunctionDeclaration";
|
|
113
|
-
if (fn) for (const p of n.params as Node[]) collectBindings(p);
|
|
114
|
-
if (inFunction && n.type === "VariableDeclarator") collectBindings(n.id);
|
|
115
|
-
if (
|
|
116
|
-
inFunction &&
|
|
117
|
-
(n.type === "FunctionDeclaration" || n.type === "ClassDeclaration") &&
|
|
118
|
-
isNode(n.id)
|
|
119
|
-
)
|
|
120
|
-
collectBindings(n.id);
|
|
121
|
-
if (n.type === "CatchClause" && n.param) collectBindings(n.param);
|
|
122
|
-
for (const [k, v] of Object.entries(n))
|
|
123
|
-
if (k !== "type" && v !== null && typeof v === "object") scan(v, inFunction || fn);
|
|
124
|
-
};
|
|
125
|
-
scan(body, false);
|
|
126
|
-
// Names exported by an `export { ... }` list must stay bindings.
|
|
127
|
-
for (const st of body)
|
|
128
|
-
if (st.type === "ExportNamedDeclaration" && !st.declaration)
|
|
129
|
-
for (const sp of st.specifiers as Node[]) nested.add((sp.local as Node).name as string);
|
|
130
|
-
for (const name of nested) entries.delete(name);
|
|
131
|
-
// Aliases must end at a table entry or a leaf; anything else stays a const.
|
|
132
|
-
const resolve = (name: string, seen = new Set<string>()): Entry | undefined => {
|
|
133
|
-
const e = entries.get(name);
|
|
134
|
-
if (!e || e.kind !== "alias") return e;
|
|
135
|
-
if (seen.has(name)) return undefined;
|
|
136
|
-
seen.add(name);
|
|
137
|
-
return resolve(e.to, seen);
|
|
138
|
-
};
|
|
139
|
-
// Repeat: deleting one entry can strand aliases that pointed at it.
|
|
140
|
-
for (let changed = true; changed;) {
|
|
141
|
-
changed = false;
|
|
142
|
-
for (const [name, e] of [...entries])
|
|
143
|
-
if (e.kind === "alias" && !resolve(name)) {
|
|
144
|
-
entries.delete(name);
|
|
145
|
-
changed = true;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
const removed = new Set(entries.keys());
|
|
149
|
-
removals.splice(
|
|
150
|
-
0,
|
|
151
|
-
removals.length,
|
|
152
|
-
...removals.filter(([start]) => [...removed].some((n) => declStart.get(n) === start)),
|
|
153
|
-
);
|
|
154
|
-
if (![...entries.values()].some((e) => e.kind === "at")) return { code, tabled: 0 };
|
|
155
|
-
for (const e of entries.values()) if (e.kind === "at") chunkSize = Math.max(chunkSize, e.j + 1);
|
|
156
|
-
const shift = Math.ceil(Math.log2(Math.max(2, chunkSize)));
|
|
157
|
-
|
|
158
|
-
const replacement = (name: string): string | undefined => {
|
|
159
|
-
const e = resolve(name);
|
|
160
|
-
if (!e) return undefined;
|
|
161
|
-
if (e.kind === "leaf") return e.text;
|
|
162
|
-
// The builder is named at the call site, so a bundler keeps only the builders a program reaches.
|
|
163
|
-
if (e.kind === "at") return `__S(${(e.chunk << shift) | e.j}, __c${e.chunk}, ${e.j})`;
|
|
164
|
-
return undefined;
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
// Value references (not keys, member names, type positions or the removed declarations).
|
|
168
|
-
const edits: Array<[number, number, string]> = [];
|
|
169
|
-
const removedIds = new Set<number>();
|
|
170
|
-
const visit = (node: unknown, parent?: Node, key?: string): void => {
|
|
171
|
-
if (Array.isArray(node)) {
|
|
172
|
-
for (const c of node) visit(c, parent, key);
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
if (!isNode(node)) return;
|
|
176
|
-
if (
|
|
177
|
-
node.type.startsWith("TS") &&
|
|
178
|
-
node.type !== "TSAsExpression" &&
|
|
179
|
-
node.type !== "TSSatisfiesExpression" &&
|
|
180
|
-
node.type !== "TSNonNullExpression"
|
|
181
|
-
)
|
|
182
|
-
return;
|
|
183
|
-
if (node.type === "Identifier") {
|
|
184
|
-
const isKey =
|
|
185
|
-
parent?.type === "Property" && key === "key" && !parent.computed && !parent.shorthand;
|
|
186
|
-
const isMember =
|
|
187
|
-
parent?.type === "MemberExpression" && key === "property" && !parent.computed;
|
|
188
|
-
const isDecl = parent?.type === "VariableDeclarator" && key === "id";
|
|
189
|
-
if (!isKey && !isMember && !isDecl && entries.has(node.name as string)) {
|
|
190
|
-
let r = replacement(node.name as string);
|
|
191
|
-
const cast = entries.get(node.name as string)?.cast ?? resolve(node.name as string)?.cast;
|
|
192
|
-
if (
|
|
193
|
-
r &&
|
|
194
|
-
cast &&
|
|
195
|
-
parent?.type === "Property" &&
|
|
196
|
-
key === "value" &&
|
|
197
|
-
isNode(parent.key) &&
|
|
198
|
-
["input", "output"].includes(parent.key.name as string)
|
|
199
|
-
)
|
|
200
|
-
r = `(${r} as any as ${cast})`;
|
|
201
|
-
if (r) {
|
|
202
|
-
const shorthand = parent?.type === "Property" && parent.shorthand;
|
|
203
|
-
edits.push([node.start, node.end, shorthand ? `${node.name}: ${r}` : r]);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
for (const [k, v] of Object.entries(node)) {
|
|
209
|
-
if (
|
|
210
|
-
k === "type" ||
|
|
211
|
-
k === "start" ||
|
|
212
|
-
k === "end" ||
|
|
213
|
-
k === "typeAnnotation" ||
|
|
214
|
-
k === "typeArguments" ||
|
|
215
|
-
k === "typeParameters" ||
|
|
216
|
-
k === "returnType"
|
|
217
|
-
)
|
|
218
|
-
continue;
|
|
219
|
-
if (v !== null && typeof v === "object") visit(v, node, k);
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
for (const st of body) {
|
|
223
|
-
// Import and export lists name bindings of other modules or re-export; not schema references.
|
|
224
|
-
if (st.type === "ImportDeclaration" || st.type === "ExportAllDeclaration") continue;
|
|
225
|
-
if (st.type === "ExportNamedDeclaration" && !st.declaration) continue;
|
|
226
|
-
const isRemoved = removals.some(([s]) => s === st.start);
|
|
227
|
-
if (isRemoved) {
|
|
228
|
-
removedIds.add(st.start);
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
visit(st);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const all: Array<[number, number, string]> = [
|
|
235
|
-
...edits,
|
|
236
|
-
...removals.map(([s, e]) => [s, e, ""] as [number, number, string]),
|
|
237
|
-
];
|
|
238
|
-
all.sort((a, b) => b[0] - a[0]);
|
|
239
|
-
let out = code;
|
|
240
|
-
for (const [s, e, t] of all) {
|
|
241
|
-
let end = e;
|
|
242
|
-
if (t === "") while (out[end] === "\n") end++;
|
|
243
|
-
out = out.slice(0, s) + t + out.slice(end);
|
|
244
|
-
}
|
|
245
|
-
// The table array goes right after the imports: error classes reach schemas while the module evaluates.
|
|
246
|
-
const lastImport = [...body].reverse().find((st) => st.type === "ImportDeclaration");
|
|
247
|
-
const importEnd = lastImport ? lastImport.end : 0;
|
|
248
|
-
// Edits only shift text after their position; the imports precede every edit.
|
|
249
|
-
out = `${out.slice(0, importEnd)}\n// Schema table, filled on first reach (see __S at the end).\nconst __s: any[] = [];${out.slice(importEnd)}`;
|
|
250
|
-
out +=
|
|
251
|
-
"\n// Schema table: entry i is schema j of builder f, made on first reach.\n" +
|
|
252
|
-
`function __S(i: number, f: (j: number) => unknown, j: number): any {\n return (__s[i] ??= __L.at(f, j));\n}\n`;
|
|
253
|
-
return { code: out, tabled: entries.size };
|
|
254
|
-
};
|
package/src/debug.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `DISTILLED_DEBUG_HTTP`, read once. Protocols check it on every request,
|
|
3
|
-
* and a `process.env` read is a C++ getter call: about 1 us each.
|
|
4
|
-
*/
|
|
5
|
-
let flag: boolean | undefined;
|
|
6
|
-
export const debugHttp = (): boolean =>
|
|
7
|
-
(flag ??= !!(globalThis as { process?: { env?: Record<string, string | undefined> } }).process
|
|
8
|
-
?.env?.DISTILLED_DEBUG_HTTP);
|
package/src/json-patch.ts
DELETED
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSON Patch (RFC 6902) Implementation
|
|
3
|
-
*
|
|
4
|
-
* The patch engine for every SDK. Patches are Smithy ops applied in
|
|
5
|
-
* `convert` (`finalizeConvert`), so `.generated-specs` is the patched Smithy
|
|
6
|
-
* model; generate does not patch. Operation names are convert policy
|
|
7
|
-
* (`operationNames`), never a patch.
|
|
8
|
-
*
|
|
9
|
-
* Pure functions only -- callers load patch files themselves (the generators
|
|
10
|
-
* use Effect's FileSystem) and hand the parsed operations to `applyPatch`.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
// ============================================================================
|
|
14
|
-
// Types
|
|
15
|
-
// ============================================================================
|
|
16
|
-
|
|
17
|
-
export interface JsonPatchOperation {
|
|
18
|
-
op: "add" | "remove" | "replace" | "move" | "copy" | "test";
|
|
19
|
-
path: string;
|
|
20
|
-
value?: unknown;
|
|
21
|
-
from?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Extension for `add` and `move`: when the op creates a new object key,
|
|
24
|
-
* insert it just before this sibling key instead of at the end, so the
|
|
25
|
-
* patched model keeps the order the converter would have written. A `move`
|
|
26
|
-
* whose `from` equals `path` reorders a key. Ignored when the key already
|
|
27
|
-
* exists, the parent is an array, or the sibling is absent.
|
|
28
|
-
*/
|
|
29
|
-
before?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type JsonPatch = JsonPatchOperation[];
|
|
33
|
-
|
|
34
|
-
export interface PatchFile {
|
|
35
|
-
description?: string;
|
|
36
|
-
patches: JsonPatch;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// ============================================================================
|
|
40
|
-
// JSON Pointer (RFC 6901)
|
|
41
|
-
// ============================================================================
|
|
42
|
-
|
|
43
|
-
/** Parse a JSON Pointer (RFC 6901) path into segments. */
|
|
44
|
-
export function parseJsonPointer(pointer: string): string[] {
|
|
45
|
-
if (pointer === "") return [];
|
|
46
|
-
if (!pointer.startsWith("/")) {
|
|
47
|
-
throw new Error(`Invalid JSON Pointer: ${pointer}`);
|
|
48
|
-
}
|
|
49
|
-
return pointer
|
|
50
|
-
.slice(1)
|
|
51
|
-
.split("/")
|
|
52
|
-
.map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Get a value at a JSON Pointer path. */
|
|
56
|
-
/**
|
|
57
|
-
* The target location of a patch operation is absent from the document --
|
|
58
|
-
* spec drift (renamed/removed upstream), not a malformed patch.
|
|
59
|
-
*/
|
|
60
|
-
export class StaleTargetError extends Error {
|
|
61
|
-
override readonly name = "StaleTargetError";
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function getValueAtPath(obj: unknown, pointer: string): unknown {
|
|
65
|
-
const segments = parseJsonPointer(pointer);
|
|
66
|
-
let current: unknown = obj;
|
|
67
|
-
|
|
68
|
-
for (const segment of segments) {
|
|
69
|
-
if (current === null || current === undefined || typeof current !== "object") {
|
|
70
|
-
throw new StaleTargetError(
|
|
71
|
-
`JSON pointer ${pointer} missing (at '${segment}'): not an object`,
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
if (Array.isArray(current)) {
|
|
75
|
-
const index = segment === "-" ? current.length : parseInt(segment, 10);
|
|
76
|
-
if (index < 0 || index >= current.length) {
|
|
77
|
-
throw new StaleTargetError(`JSON pointer ${pointer} missing index '${segment}'`);
|
|
78
|
-
}
|
|
79
|
-
current = current[index];
|
|
80
|
-
} else {
|
|
81
|
-
const record = current as Record<string, unknown>;
|
|
82
|
-
if (!Object.prototype.hasOwnProperty.call(record, segment)) {
|
|
83
|
-
throw new StaleTargetError(`JSON pointer ${pointer} missing key '${segment}'`);
|
|
84
|
-
}
|
|
85
|
-
current = record[segment];
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return current;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Set a value at a JSON Pointer path. */
|
|
93
|
-
export function setValueAtPath(
|
|
94
|
-
obj: unknown,
|
|
95
|
-
pointer: string,
|
|
96
|
-
value: unknown,
|
|
97
|
-
mode: "add" | "replace" = "add",
|
|
98
|
-
before?: string,
|
|
99
|
-
): void {
|
|
100
|
-
const segments = parseJsonPointer(pointer);
|
|
101
|
-
if (segments.length === 0) {
|
|
102
|
-
throw new Error("Cannot set value at root path");
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
let current: unknown = obj;
|
|
106
|
-
|
|
107
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
108
|
-
const segment = segments[i]!;
|
|
109
|
-
if (current === null || typeof current !== "object") {
|
|
110
|
-
throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
|
|
111
|
-
}
|
|
112
|
-
if (Array.isArray(current)) {
|
|
113
|
-
current = current[parseInt(segment, 10)];
|
|
114
|
-
} else {
|
|
115
|
-
current = (current as Record<string, unknown>)[segment];
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const lastSegment = segments[segments.length - 1]!;
|
|
120
|
-
if (current === null || typeof current !== "object") {
|
|
121
|
-
throw new StaleTargetError(`Cannot set value at path ${pointer}: parent is not an object`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (Array.isArray(current)) {
|
|
125
|
-
if (lastSegment === "-") {
|
|
126
|
-
current.push(value);
|
|
127
|
-
} else if (mode === "replace") {
|
|
128
|
-
current[parseInt(lastSegment, 10)] = value;
|
|
129
|
-
} else {
|
|
130
|
-
// RFC 6902 §4.1: `add` at an array index inserts before it.
|
|
131
|
-
const index = parseInt(lastSegment, 10);
|
|
132
|
-
if (Number.isNaN(index) || index < 0 || index > current.length) {
|
|
133
|
-
throw new StaleTargetError(
|
|
134
|
-
`JSON pointer ${pointer} index '${lastSegment}' is out of bounds`,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
current.splice(index, 0, value);
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
const record = current as Record<string, unknown>;
|
|
141
|
-
if (before === undefined || lastSegment in record || !(before in record)) {
|
|
142
|
-
record[lastSegment] = value;
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
// Rebuild the keys in place: the parent object stays the same object,
|
|
146
|
-
// since callers and later ops may hold a reference to it.
|
|
147
|
-
const entries = Object.entries(record);
|
|
148
|
-
for (const [key] of entries) delete record[key];
|
|
149
|
-
for (const [key, v] of entries) {
|
|
150
|
-
if (key === before) record[lastSegment] = value;
|
|
151
|
-
record[key] = v;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** Remove a value at a JSON Pointer path. */
|
|
157
|
-
export function removeValueAtPath(obj: unknown, pointer: string): void {
|
|
158
|
-
const segments = parseJsonPointer(pointer);
|
|
159
|
-
if (segments.length === 0) {
|
|
160
|
-
throw new Error("Cannot remove root");
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let current: unknown = obj;
|
|
164
|
-
|
|
165
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
166
|
-
const segment = segments[i]!;
|
|
167
|
-
if (current === null || typeof current !== "object") {
|
|
168
|
-
throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
|
|
169
|
-
}
|
|
170
|
-
if (Array.isArray(current)) {
|
|
171
|
-
current = current[parseInt(segment, 10)];
|
|
172
|
-
} else {
|
|
173
|
-
current = (current as Record<string, unknown>)[segment];
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const lastSegment = segments[segments.length - 1]!;
|
|
178
|
-
if (current === null || typeof current !== "object") {
|
|
179
|
-
throw new StaleTargetError(`Cannot remove at path ${pointer}: parent is not an object`);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// RFC 6902 §4.2: the target location MUST exist.
|
|
183
|
-
if (Array.isArray(current)) {
|
|
184
|
-
const index = parseInt(lastSegment, 10);
|
|
185
|
-
if (Number.isNaN(index) || index < 0 || index >= current.length) {
|
|
186
|
-
throw new StaleTargetError(`JSON pointer ${pointer} missing index '${lastSegment}'`);
|
|
187
|
-
}
|
|
188
|
-
current.splice(index, 1);
|
|
189
|
-
} else {
|
|
190
|
-
const record = current as Record<string, unknown>;
|
|
191
|
-
if (!Object.prototype.hasOwnProperty.call(record, lastSegment)) {
|
|
192
|
-
throw new StaleTargetError(`JSON pointer ${pointer} missing key '${lastSegment}'`);
|
|
193
|
-
}
|
|
194
|
-
delete record[lastSegment];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// ============================================================================
|
|
199
|
-
// Patch Operations
|
|
200
|
-
// ============================================================================
|
|
201
|
-
|
|
202
|
-
/** Apply a single JSON Patch operation (mutates `obj` in place). */
|
|
203
|
-
export function applyOperation(obj: unknown, operation: JsonPatchOperation): void {
|
|
204
|
-
switch (operation.op) {
|
|
205
|
-
case "add":
|
|
206
|
-
setValueAtPath(obj, operation.path, operation.value, "add", operation.before);
|
|
207
|
-
break;
|
|
208
|
-
case "remove":
|
|
209
|
-
removeValueAtPath(obj, operation.path);
|
|
210
|
-
break;
|
|
211
|
-
case "replace": {
|
|
212
|
-
const existing = getValueAtPath(obj, operation.path);
|
|
213
|
-
if (existing === undefined) {
|
|
214
|
-
throw new StaleTargetError(`JSON pointer ${operation.path} does not exist`);
|
|
215
|
-
}
|
|
216
|
-
setValueAtPath(obj, operation.path, operation.value, "replace");
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
case "move": {
|
|
220
|
-
if (!operation.from) throw new Error("move operation requires 'from'");
|
|
221
|
-
const moveValue = getValueAtPath(obj, operation.from);
|
|
222
|
-
if (moveValue === undefined) {
|
|
223
|
-
throw new StaleTargetError(`Cannot move from path ${operation.from}: not an object`);
|
|
224
|
-
}
|
|
225
|
-
removeValueAtPath(obj, operation.from);
|
|
226
|
-
setValueAtPath(obj, operation.path, moveValue, "add", operation.before);
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
case "copy": {
|
|
230
|
-
if (!operation.from) throw new Error("copy operation requires 'from'");
|
|
231
|
-
const copyValue = getValueAtPath(obj, operation.from);
|
|
232
|
-
setValueAtPath(obj, operation.path, JSON.parse(JSON.stringify(copyValue)));
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
case "test": {
|
|
236
|
-
const testValue = getValueAtPath(obj, operation.path);
|
|
237
|
-
if (JSON.stringify(testValue) !== JSON.stringify(operation.value)) {
|
|
238
|
-
throw new Error(
|
|
239
|
-
`Test operation failed at ${operation.path}: expected ${JSON.stringify(operation.value)}, got ${JSON.stringify(testValue)}`,
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
default:
|
|
245
|
-
throw new Error(`Unknown operation: ${(operation as { op: string }).op}`);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/** Apply a JSON Patch to an object (mutates in place). */
|
|
250
|
-
export function applyPatch(obj: unknown, patch: JsonPatch): void {
|
|
251
|
-
for (const operation of patch) {
|
|
252
|
-
applyOperation(obj, operation);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Whether a per-operation failure is a {@link StaleTargetError} (the
|
|
258
|
-
* operation/shape the patch targets was renamed or removed upstream) as
|
|
259
|
-
* opposed to a malformed patch. Accepts the thrown value or its message.
|
|
260
|
-
* Convert fails the run on these by default (`onStalePatch: "fail"`);
|
|
261
|
-
* `"warn"` restores skip-and-continue.
|
|
262
|
-
*/
|
|
263
|
-
export function isStaleTargetError(error: unknown): boolean {
|
|
264
|
-
if (error instanceof StaleTargetError) return true;
|
|
265
|
-
const message = typeof error === "string" ? error : error instanceof Error ? error.message : "";
|
|
266
|
-
return (
|
|
267
|
-
message.includes("not an object") ||
|
|
268
|
-
message.includes("parent is not an object") ||
|
|
269
|
-
message.includes("missing key") ||
|
|
270
|
-
message.includes("missing index") ||
|
|
271
|
-
message.includes("does not exist")
|
|
272
|
-
);
|
|
273
|
-
}
|