@workerstack/clients 1.0.0-rc.28 → 1.0.0-rc.30
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/README.md +403 -21
- package/assets/installers/examples/github.config.ts +17 -19
- package/assets/installers/examples/goreleaser.config.ts +16 -18
- package/assets/installers/examples/raw.config.ts +13 -15
- package/bin/clients.js +87 -66
- package/bin/installgen.js +19 -10
- package/dist/cli.d.ts +4 -4
- package/dist/cli.js +18 -12
- package/dist/emit/cli/channels.d.ts +2 -2
- package/dist/emit/cli/channels.js +18 -13
- package/dist/emit/cli/commands.d.ts +1 -1
- package/dist/emit/cli/commands.js +39 -13
- package/dist/emit/cli/discovery.d.ts +0 -6
- package/dist/emit/cli/discovery.js +9 -7
- package/dist/emit/cli/flags.d.ts +1 -3
- package/dist/emit/cli/flags.js +18 -8
- package/dist/emit/cli/install.d.ts +0 -8
- package/dist/emit/cli/install.js +3 -3
- package/dist/emit/cli/layout.d.ts +1 -1
- package/dist/emit/cli/layout.js +13 -10
- package/dist/emit/cli/module.d.ts +6 -6
- package/dist/emit/cli/module.js +10 -10
- package/dist/emit/cli/release.d.ts +0 -11
- package/dist/emit/cli/release.js +4 -4
- package/dist/emit/cli/skills.js +8 -4
- package/dist/emit/csharp/errors.js +4 -4
- package/dist/emit/csharp/json.js +8 -8
- package/dist/emit/csharp/realtime.d.ts +1 -8
- package/dist/emit/csharp/realtime.js +26 -26
- package/dist/emit/csharp/sdk-answer.d.ts +10 -2
- package/dist/emit/csharp/sdk-answer.js +4 -4
- package/dist/emit/csharp/sdk-body.js +8 -6
- package/dist/emit/csharp/sdk-client.d.ts +3 -3
- package/dist/emit/csharp/sdk-client.js +2 -2
- package/dist/emit/csharp/sdk-csproj.d.ts +7 -9
- package/dist/emit/csharp/sdk-csproj.js +10 -10
- package/dist/emit/csharp/sdk-doc.js +1 -1
- package/dist/emit/csharp/sdk-method.d.ts +0 -1
- package/dist/emit/csharp/sdk-method.js +19 -8
- package/dist/emit/csharp/sdk-plan.js +0 -1
- package/dist/emit/csharp/sdk.js +3 -3
- package/dist/emit/csharp/sse.d.ts +0 -13
- package/dist/emit/csharp/sse.js +92 -11
- package/dist/emit/csharp/transport.js +9 -9
- package/dist/emit/csharp/types-enum.js +3 -1
- package/dist/emit/csharp/types-messages.d.ts +47 -0
- package/dist/emit/csharp/types-messages.js +172 -0
- package/dist/emit/csharp/types-names.js +13 -14
- package/dist/emit/csharp/types-record.js +3 -3
- package/dist/emit/csharp/types-union.d.ts +1 -1
- package/dist/emit/csharp/types.d.ts +0 -1
- package/dist/emit/csharp/types.js +3 -3
- package/dist/emit/csharp/ws.d.ts +0 -15
- package/dist/emit/csharp/ws.js +26 -5
- package/dist/emit/go/errors.js +1 -1
- package/dist/emit/go/http.js +2 -2
- package/dist/emit/go/module-path.js +2 -2
- package/dist/emit/go/sdk-answer.d.ts +24 -1
- package/dist/emit/go/sdk-answer.js +1 -1
- package/dist/emit/go/sdk-body.js +11 -7
- package/dist/emit/go/sdk-client.js +1 -1
- package/dist/emit/go/sdk-method.d.ts +3 -0
- package/dist/emit/go/sdk-method.js +18 -12
- package/dist/emit/go/sdk-mirror.js +2 -2
- package/dist/emit/go/sdk-opts.d.ts +0 -2
- package/dist/emit/go/sdk-opts.js +3 -3
- package/dist/emit/go/sdk.d.ts +1 -1
- package/dist/emit/go/sdk.js +9 -9
- package/dist/emit/go/sse.d.ts +2 -10
- package/dist/emit/go/sse.js +88 -13
- package/dist/emit/go/types-names.js +5 -0
- package/dist/emit/go/types-struct.d.ts +2 -1
- package/dist/emit/go/types-struct.js +1 -1
- package/dist/emit/go/types-union.d.ts +3 -2
- package/dist/emit/go/types-union.js +2 -0
- package/dist/emit/go/types.d.ts +6 -0
- package/dist/emit/go/types.js +20 -9
- package/dist/emit/go/ws.d.ts +4 -11
- package/dist/emit/go/ws.js +215 -59
- package/dist/emit/java/gradle.d.ts +7 -9
- package/dist/emit/java/gradle.js +8 -8
- package/dist/emit/java/json.d.ts +3 -0
- package/dist/emit/java/json.js +1 -1
- package/dist/emit/java/layout-ast.d.ts +4 -1
- package/dist/emit/java/layout-ast.js +1 -1
- package/dist/emit/java/layout-decl.js +5 -4
- package/dist/emit/java/layout-doc.js +13 -7
- package/dist/emit/java/layout-dot.js +1 -1
- package/dist/emit/java/layout-strings.js +4 -5
- package/dist/emit/java/layout-types.js +7 -5
- package/dist/emit/java/layout-visitor.js +4 -3
- package/dist/emit/java/layout.js +5 -4
- package/dist/emit/java/models-names.d.ts +2 -0
- package/dist/emit/java/models-names.js +6 -4
- package/dist/emit/java/models-record.d.ts +0 -1
- package/dist/emit/java/models-record.js +3 -3
- package/dist/emit/java/models-union.d.ts +13 -2
- package/dist/emit/java/models-union.js +12 -6
- package/dist/emit/java/models.d.ts +5 -1
- package/dist/emit/java/models.js +44 -20
- package/dist/emit/java/realtime.d.ts +34 -26
- package/dist/emit/java/realtime.js +45 -10
- package/dist/emit/java/sdk-client.d.ts +6 -6
- package/dist/emit/java/sdk-client.js +1 -1
- package/dist/emit/java/sdk-decl.js +2 -2
- package/dist/emit/java/sdk-errors.js +1 -1
- package/dist/emit/java/sdk-headers.js +2 -2
- package/dist/emit/java/sdk-mirror.js +7 -6
- package/dist/emit/java/sdk-models.d.ts +1 -1
- package/dist/emit/java/sdk-models.js +7 -7
- package/dist/emit/java/sdk-op.d.ts +1 -10
- package/dist/emit/java/sdk-op.js +9 -9
- package/dist/emit/java/sdk-params.d.ts +5 -5
- package/dist/emit/java/sdk-params.js +3 -2
- package/dist/emit/java/sdk-plan.js +0 -1
- package/dist/emit/java/sdk.d.ts +1 -1
- package/dist/emit/java/sdk.js +14 -13
- package/dist/emit/java/sse.d.ts +12 -13
- package/dist/emit/java/sse.js +105 -6
- package/dist/emit/java/ws.d.ts +48 -9
- package/dist/emit/java/ws.js +88 -4
- package/dist/emit/mcp/annotations.js +8 -4
- package/dist/emit/mcp/files.js +9 -9
- package/dist/emit/mcp/mounted.js +2 -2
- package/dist/emit/mcp/output-schema.d.ts +4 -2
- package/dist/emit/mcp/output-schema.js +58 -14
- package/dist/emit/mcp/registry.d.ts +0 -2
- package/dist/emit/mcp/registry.js +5 -4
- package/dist/emit/mcp/runtime.js +2 -2
- package/dist/emit/mcp/schema.js +22 -11
- package/dist/emit/mcp/server.d.ts +7 -7
- package/dist/emit/mcp/server.js +7 -6
- package/dist/emit/mcp/tools.js +7 -6
- package/dist/emit/python/errors.js +3 -3
- package/dist/emit/python/realtime.d.ts +9 -6
- package/dist/emit/python/realtime.js +22 -20
- package/dist/emit/python/sdk-answer.d.ts +8 -8
- package/dist/emit/python/sdk-answer.js +2 -2
- package/dist/emit/python/sdk-body.js +4 -5
- package/dist/emit/python/sdk-client.js +5 -5
- package/dist/emit/python/sdk-doc.js +2 -2
- package/dist/emit/python/sdk-method.d.ts +1 -1
- package/dist/emit/python/sdk-method.js +38 -11
- package/dist/emit/python/sdk-plan.js +0 -1
- package/dist/emit/python/sdk-pyproject.d.ts +0 -2
- package/dist/emit/python/sdk-pyproject.js +8 -8
- package/dist/emit/python/sdk.d.ts +1 -1
- package/dist/emit/python/sdk.js +28 -8
- package/dist/emit/python/sse.d.ts +0 -7
- package/dist/emit/python/sse.js +36 -6
- package/dist/emit/python/transport.js +4 -4
- package/dist/emit/python/types-dict.d.ts +2 -2
- package/dist/emit/python/types-dict.js +1 -1
- package/dist/emit/python/types-names.js +2 -2
- package/dist/emit/python/types-union.js +1 -1
- package/dist/emit/python/types.d.ts +7 -0
- package/dist/emit/python/types.js +23 -6
- package/dist/emit/python/ws.d.ts +21 -10
- package/dist/emit/python/ws.js +69 -3
- package/dist/emit/rust/errors.js +3 -3
- package/dist/emit/rust/http.js +4 -4
- package/dist/emit/rust/realtime.d.ts +7 -5
- package/dist/emit/rust/realtime.js +15 -18
- package/dist/emit/rust/sdk-body.js +8 -5
- package/dist/emit/rust/sdk-cargo.d.ts +16 -18
- package/dist/emit/rust/sdk-cargo.js +12 -12
- package/dist/emit/rust/sdk-client.d.ts +1 -1
- package/dist/emit/rust/sdk-client.js +3 -3
- package/dist/emit/rust/sdk-method.js +3 -3
- package/dist/emit/rust/sdk-opts.d.ts +2 -1
- package/dist/emit/rust/sdk-opts.js +2 -2
- package/dist/emit/rust/sdk-plan.js +0 -1
- package/dist/emit/rust/sdk.d.ts +1 -1
- package/dist/emit/rust/sdk.js +5 -5
- package/dist/emit/rust/sse.d.ts +11 -12
- package/dist/emit/rust/sse.js +54 -8
- package/dist/emit/rust/types-names.js +3 -3
- package/dist/emit/rust/types-struct.js +1 -1
- package/dist/emit/rust/types-union.d.ts +10 -2
- package/dist/emit/rust/types-union.js +4 -0
- package/dist/emit/rust/types.d.ts +9 -0
- package/dist/emit/rust/types.js +20 -12
- package/dist/emit/rust/ws.d.ts +22 -15
- package/dist/emit/rust/ws.js +60 -10
- package/dist/emit/shared/escape.js +17 -17
- package/dist/emit/shared/format.js +1 -1
- package/dist/emit/shared/gofmt.js +1 -1
- package/dist/emit/shared/lang.js +10 -10
- package/dist/emit/shared/literals.d.ts +5 -0
- package/dist/emit/shared/literals.js +8 -0
- package/dist/emit/shared/metadata.d.ts +0 -6
- package/dist/emit/shared/metadata.js +8 -7
- package/dist/emit/shared/ruff.js +1 -1
- package/dist/emit/shared/rustfmt.js +1 -1
- package/dist/emit/shared/type-emitter.js +37 -20
- package/dist/emit/shared/unknown-type.constants.d.ts +2 -0
- package/dist/emit/shared/unknown-type.constants.js +2 -0
- package/dist/emit/ts/http.d.ts +0 -2
- package/dist/emit/ts/http.js +4 -4
- package/dist/emit/ts/package.d.ts +14 -12
- package/dist/emit/ts/package.js +9 -8
- package/dist/emit/ts/route-tree.js +2 -2
- package/dist/emit/ts/sdk-types-preamble.d.ts +1 -1
- package/dist/emit/ts/sdk-types-preamble.js +6 -2
- package/dist/emit/ts/sdk-types.js +59 -20
- package/dist/emit/ts/sdk.js +6 -6
- package/dist/emit/ts/service-map.d.ts +10 -3
- package/dist/emit/ts/service-map.js +30 -2
- package/dist/emit/ts/tanstack-query.js +6 -6
- package/dist/emit/ts/type-gen.js +36 -21
- package/dist/emit/ts/webhooks.d.ts +0 -2
- package/dist/emit/ts/webhooks.js +3 -3
- package/dist/extract/chain.js +5 -4
- package/dist/extract/extract.js +5 -5
- package/dist/extract/serialize.d.ts +0 -2
- package/dist/extract/serialize.js +14 -15
- package/dist/extract/walk.js +15 -12
- package/dist/gen/args.js +9 -5
- package/dist/gen/formatters.js +5 -3
- package/dist/gen/shipped.js +2 -2
- package/dist/gen/targets.d.ts +2 -4
- package/dist/gen/targets.js +8 -4
- package/dist/gen/write.js +4 -2
- package/dist/index.js +2 -2
- package/dist/installers/cli.d.ts +4 -4
- package/dist/installers/cli.js +24 -24
- package/dist/installers/commands/build.d.ts +4 -4
- package/dist/installers/commands/build.js +2 -3
- package/dist/installers/commands/check.d.ts +2 -2
- package/dist/installers/commands/check.js +11 -10
- package/dist/installers/commands/init.d.ts +2 -2
- package/dist/installers/commands/init.js +1 -2
- package/dist/installers/config/define-config.d.ts +16 -16
- package/dist/installers/config/define-config.js +1 -1
- package/dist/installers/config/hooks/bun.d.ts +6 -6
- package/dist/installers/config/hooks/node.js +1 -1
- package/dist/installers/config/load.d.ts +6 -8
- package/dist/installers/config/load.js +7 -8
- package/dist/installers/config/resolve.d.ts +1 -2
- package/dist/installers/config/resolve.js +28 -33
- package/dist/installers/config/types.d.ts +6 -6
- package/dist/installers/config/types.js +0 -1
- package/dist/installers/render/quote.js +3 -5
- package/dist/installers/render/render.d.ts +6 -6
- package/dist/installers/render/render.js +26 -26
- package/dist/installers/ui.d.ts +4 -4
- package/dist/installers/ui.js +3 -3
- package/dist/ir/build.d.ts +3 -3
- package/dist/ir/build.js +12 -5
- package/dist/ir/load.js +9 -10
- package/dist/ir/model.d.ts +16 -1
- package/dist/ir/model.js +0 -1
- package/dist/ir/resolve-refs.js +2 -2
- package/dist/ir/resource-tree.js +2 -1
- package/dist/ir/route-tree-intern.js +1 -1
- package/dist/ir/schema.js +3 -3
- package/dist/ir/sdk-model.d.ts +25 -1
- package/dist/ir/sdk-model.js +38 -8
- package/dist/ir/slots.d.ts +1 -1
- package/dist/ir/slots.js +22 -11
- package/dist/ir/streams.d.ts +16 -0
- package/dist/ir/streams.js +40 -0
- package/dist/release/bootstrap.d.ts +3 -3
- package/dist/release/bootstrap.js +7 -7
- package/dist/release/bump-args.js +4 -4
- package/dist/release/bump-command.d.ts +1 -1
- package/dist/release/bump-command.js +9 -10
- package/dist/release/bump.d.ts +5 -4
- package/dist/release/bump.js +2 -2
- package/dist/release/cli.d.ts +4 -4
- package/dist/release/cli.js +4 -4
- package/dist/release/codegen.js +1 -1
- package/dist/release/digest.js +5 -7
- package/dist/release/git.js +5 -8
- package/dist/release/load.js +3 -3
- package/dist/release/manifest.js +9 -10
- package/dist/release/oasdiff.d.ts +10 -15
- package/dist/release/oasdiff.js +5 -6
- package/dist/release/registries.d.ts +0 -11
- package/dist/release/registries.js +17 -17
- package/dist/release/release-state.d.ts +16 -13
- package/dist/release/release-workflow.js +5 -7
- package/dist/release/repo-settings.d.ts +2 -1
- package/dist/release/spec-gate.d.ts +0 -2
- package/dist/release/spec-gate.js +6 -10
- package/dist/release/sync-command.d.ts +2 -2
- package/dist/release/sync-command.js +1 -1
- package/dist/release/sync-tree.js +9 -13
- package/dist/release/sync.d.ts +6 -6
- package/dist/release/sync.js +12 -13
- package/dist/release/types.d.ts +22 -22
- package/dist/release/types.js +0 -1
- package/dist/release/version-files.d.ts +3 -4
- package/dist/release/version-files.js +4 -5
- package/dist/runtime/.oxfmtrc.jsonc +8 -0
- package/dist/runtime/cli/socket.go +107 -13
- package/dist/runtime/cli/stream.go +18 -11
- package/dist/runtime/csharp/Adapters.cs +12 -11
- package/dist/runtime/csharp/Events.cs +22 -17
- package/dist/runtime/csharp/Realtime.cs +50 -23
- package/dist/runtime/csharp/RealtimeLoop.cs +1 -1
- package/dist/runtime/csharp/TransportSse.cs +3 -3
- package/dist/runtime/csharp/Ws.cs +1 -1
- package/dist/runtime/csharp/WsDial.cs +19 -1
- package/dist/runtime/csharp/WsTyped.cs +109 -0
- package/dist/runtime/go/realtime.go +125 -41
- package/dist/runtime/go/realtime_loop.go +21 -18
- package/dist/runtime/go/result.go +2 -1
- package/dist/runtime/go/sse.go +11 -0
- package/dist/runtime/go/stream.go +29 -13
- package/dist/runtime/go/transport.go +15 -21
- package/dist/runtime/go/transport_longpoll.go +3 -4
- package/dist/runtime/go/transport_sse.go +6 -6
- package/dist/runtime/go/ws.go +157 -61
- package/dist/runtime/java/EventStream.java +47 -29
- package/dist/runtime/java/Exchange.java +5 -5
- package/dist/runtime/java/Flight.java +3 -3
- package/dist/runtime/java/Realtime.java +45 -11
- package/dist/runtime/java/ResumableConnection.java +77 -32
- package/dist/runtime/java/Socket.java +8 -4
- package/dist/runtime/java/SseAdapter.java +1 -1
- package/dist/runtime/java/TypedWebSocket.java +81 -24
- package/dist/runtime/java/WsAdapter.java +4 -2
- package/dist/runtime/java/WsDial.java +12 -5
- package/dist/runtime/mcp/files.ts +1 -1
- package/dist/runtime/mcp/http.ts +6 -2
- package/dist/runtime/mcp/listen.ts +1 -1
- package/dist/runtime/mcp/mount.ts +2 -2
- package/dist/runtime/mcp/protocol.ts +1 -1
- package/dist/runtime/python/events.py +25 -11
- package/dist/runtime/python/operation.py +6 -1
- package/dist/runtime/python/realtime.py +47 -33
- package/dist/runtime/python/send_async.py +1 -0
- package/dist/runtime/python/transport_sse.py +2 -2
- package/dist/runtime/python/transport_ws.py +1 -1
- package/dist/runtime/python/ws.py +100 -39
- package/dist/runtime/rust/events.rs +64 -41
- package/dist/runtime/rust/realtime.rs +80 -34
- package/dist/runtime/rust/realtime_loop.rs +37 -12
- package/dist/runtime/rust/sse.rs +3 -2
- package/dist/runtime/rust/transport_sse.rs +1 -1
- package/dist/runtime/rust/transport_ws.rs +3 -2
- package/dist/runtime/rust/ws.rs +122 -50
- package/dist/runtime/ts/realtime-connection.ts +22 -0
- package/dist/runtime/ts/realtime-transports.ts +13 -4
- package/dist/runtime/ts/realtime.ts +27 -3
- package/dist/runtime/ts/sdk-realtime.ts +1 -1
- package/dist/runtime/ts/sdk-sse.ts +31 -14
- package/dist/runtime/ts/sdk-ws.ts +2 -1
- package/dist/runtime/ts/sdk.ts +9 -2
- package/dist/runtime/ts/sse.ts +8 -0
- package/dist/runtime/ts/ws.ts +88 -50
- package/dist/runtime/ts-tanstack-query/query.ts +0 -2
- package/package.json +12 -40
- package/dist/emit/cli/index.d.ts +0 -1
- package/dist/emit/cli/index.js +0 -1
- package/dist/emit/csharp/index.d.ts +0 -1
- package/dist/emit/csharp/index.js +0 -1
- package/dist/emit/go/index.d.ts +0 -1
- package/dist/emit/go/index.js +0 -1
- package/dist/emit/java/index.d.ts +0 -2
- package/dist/emit/java/index.js +0 -2
- package/dist/emit/mcp/index.d.ts +0 -3
- package/dist/emit/mcp/index.js +0 -3
- package/dist/emit/python/index.d.ts +0 -1
- package/dist/emit/python/index.js +0 -1
- package/dist/emit/rust/index.d.ts +0 -1
- package/dist/emit/rust/index.js +0 -1
|
@@ -2,22 +2,19 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { MANIFEST } from "../gen/write.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* spec with the committed one before they let changed generated output out.
|
|
8
|
-
*/
|
|
5
|
+
// Generated code must only describe operations prod serves. Sync, bump and release compare the deployed public
|
|
6
|
+
// spec with the committed one before they let changed generated output out.
|
|
9
7
|
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
10
8
|
const FETCH_TIMEOUT_MS = 30_000;
|
|
11
9
|
/** The value with every object's keys sorted, so equal specs serialize equally. */
|
|
12
|
-
|
|
10
|
+
function canonicalize(value) {
|
|
13
11
|
if (value === null || typeof value !== "object")
|
|
14
12
|
return value;
|
|
15
13
|
if (Array.isArray(value))
|
|
16
14
|
return value.map(canonicalize);
|
|
17
15
|
const sorted = {};
|
|
18
|
-
for (const key of Object.keys(value).sort())
|
|
16
|
+
for (const key of Object.keys(value).sort())
|
|
19
17
|
sorted[key] = canonicalize(value[key]);
|
|
20
|
-
}
|
|
21
18
|
return sorted;
|
|
22
19
|
}
|
|
23
20
|
/** sha256 of the spec's canonical JSON. */
|
|
@@ -29,11 +26,10 @@ export function canonicalSha(spec) {
|
|
|
29
26
|
function operations(spec) {
|
|
30
27
|
const paths = spec.paths ?? {};
|
|
31
28
|
const out = new Set();
|
|
32
|
-
for (const [path, item] of Object.entries(paths))
|
|
29
|
+
for (const [path, item] of Object.entries(paths))
|
|
33
30
|
for (const method of METHODS)
|
|
34
31
|
if (item[method] !== undefined)
|
|
35
32
|
out.add(`${method.toUpperCase()} ${path}`);
|
|
36
|
-
}
|
|
37
33
|
return out;
|
|
38
34
|
}
|
|
39
35
|
const minus = (a, b) => [...a].filter((x) => !b.has(x)).sort();
|
|
@@ -65,7 +61,7 @@ export async function checkDeployedSpec(source, fetchImpl = fetch) {
|
|
|
65
61
|
status: "drift",
|
|
66
62
|
};
|
|
67
63
|
}
|
|
68
|
-
|
|
64
|
+
// each operation on a line of its own, or a line saying there is none
|
|
69
65
|
const list = (ops) => (ops.length === 0 ? " (none)" : ops.map((o) => ` ${o}`).join("\n"));
|
|
70
66
|
/** Why `packageName` was refused, and what unblocks it. */
|
|
71
67
|
export function describeDrift(packageName, gate) {
|
|
@@ -3,7 +3,7 @@ import { type PackageResult } from "./sync.ts";
|
|
|
3
3
|
/** What `release sync --help` prints. */
|
|
4
4
|
export declare const HELP = "Usage: clients release sync --config <path> [options]\n\nOptions:\n --config <path> The release config (required)\n --only <names> Comma-separated package filter. Accepts short \"name\" (if unique) or \"project/name\"\n --dry-run Log push commands without executing them\n --validate-only Validate the release config and exit 0/1\n --help Show this message\n\nEnv:\n GH_TOKEN GitHub token (required unless --validate-only or --dry-run)\n";
|
|
5
5
|
/** What `release sync` was asked for. */
|
|
6
|
-
export
|
|
6
|
+
export type CliOptions = {
|
|
7
7
|
/** The release config's path; required unless `--help`. */
|
|
8
8
|
config: string | undefined;
|
|
9
9
|
/** Report what would be pushed and push nothing. */
|
|
@@ -14,7 +14,7 @@ export interface CliOptions {
|
|
|
14
14
|
only: string[] | undefined;
|
|
15
15
|
/** Only load and check the release config. */
|
|
16
16
|
validateOnly: boolean;
|
|
17
|
-
}
|
|
17
|
+
};
|
|
18
18
|
/** A parse that succeeded with `options`, or failed with an `error` to show the caller. */
|
|
19
19
|
export type ParsedArgs = {
|
|
20
20
|
error: string;
|
|
@@ -17,26 +17,23 @@ export async function prepareWorktree(worktree, httpsUrl, { branch, committer },
|
|
|
17
17
|
await gitRun(["-C", worktree, "fetch", "--depth=1", "origin", branch], worktree);
|
|
18
18
|
await gitRun(["-C", worktree, "reset", "--hard", "FETCH_HEAD"], worktree);
|
|
19
19
|
const tracked = await gitOutput(["-C", worktree, "ls-files"], worktree);
|
|
20
|
-
if (tracked)
|
|
20
|
+
if (tracked)
|
|
21
21
|
await gitRun(["-C", worktree, "rm", "-rf", "--quiet", "."], worktree);
|
|
22
|
-
}
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
export async function replaceTreeWithSource(args) {
|
|
26
25
|
const sourceFull = join(args.repoRoot, args.sourcePath);
|
|
27
26
|
await runRsync(`${sourceFull}/`, `${args.worktree}/`, args.excludes);
|
|
28
|
-
|
|
27
|
+
// the sidecar message file is the source's note to sync; it never lands in the public repo
|
|
29
28
|
const sidecarPath = join(args.worktree, SIDECAR_FILE);
|
|
30
|
-
if (existsSync(sidecarPath))
|
|
29
|
+
if (existsSync(sidecarPath))
|
|
31
30
|
unlinkSync(sidecarPath);
|
|
32
|
-
}
|
|
33
31
|
}
|
|
34
32
|
async function runRsync(src, dst, excludes) {
|
|
35
|
-
|
|
33
|
+
// clients' manifest is the source repo's record of what it generated, not part of the package
|
|
36
34
|
const args = ["-a", "--delete", "--exclude=.git", `--exclude=${MANIFEST}`];
|
|
37
|
-
for (const glob of excludes)
|
|
35
|
+
for (const glob of excludes)
|
|
38
36
|
args.push(`--exclude=${glob}`);
|
|
39
|
-
}
|
|
40
37
|
args.push(src, dst);
|
|
41
38
|
await new Promise((resolve, reject) => {
|
|
42
39
|
const child = spawn("rsync", args, { stdio: ["ignore", "ignore", "pipe"] });
|
|
@@ -71,14 +68,14 @@ export async function pendingTag(entry, repoRoot, worktree, httpsUrl) {
|
|
|
71
68
|
const existing = await gitOutput(["ls-remote", "--tags", httpsUrl, `refs/tags/${tag}`], worktree);
|
|
72
69
|
return existing === "" ? tag : undefined;
|
|
73
70
|
}
|
|
74
|
-
|
|
71
|
+
// No force: a tag someone pushed in between makes this fail rather than move theirs.
|
|
75
72
|
export async function pushTag(worktree, httpsUrl, tag) {
|
|
76
73
|
await gitRun(["-C", worktree, "tag", tag, "HEAD"], worktree);
|
|
77
74
|
await gitRun(["-C", worktree, "push", httpsUrl, `refs/tags/${tag}`], worktree);
|
|
78
75
|
}
|
|
79
76
|
export async function isTreeUnchanged(worktree) {
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
// -Af forces past the package's own .gitignore: a generated SDK may ship one that keeps its generated source out
|
|
78
|
+
// of the source repo's git, and that filter must not apply here, where those files are what is published
|
|
82
79
|
await gitRun(["-C", worktree, "add", "-Af"], worktree);
|
|
83
80
|
const diffCode = await gitExitCode(["-C", worktree, "diff", "--cached", "--quiet"], worktree);
|
|
84
81
|
return diffCode === 0;
|
|
@@ -92,9 +89,8 @@ export function readSidecar(repoRoot, sourcePath) {
|
|
|
92
89
|
}
|
|
93
90
|
export function buildCommitMessage(args) {
|
|
94
91
|
const stamp = formatUtcStamp(new Date());
|
|
95
|
-
if (args.sidecarMessage)
|
|
92
|
+
if (args.sidecarMessage)
|
|
96
93
|
return `${args.sidecarMessage} [${stamp}]`;
|
|
97
|
-
}
|
|
98
94
|
if (args.isInitial) {
|
|
99
95
|
const initial = args.initialMessage ?? "chore: initial release";
|
|
100
96
|
return `${initial} [${stamp}]`;
|
package/dist/release/sync.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { PublicPackage, SpecSource } from "./types.ts";
|
|
2
1
|
import type { RepoSettingsSync } from "./repo-settings.ts";
|
|
3
2
|
import { type SpecGate } from "./spec-gate.ts";
|
|
3
|
+
import type { PublicPackage, SpecSource } from "./types.ts";
|
|
4
4
|
/** How one sync run behaves: the repo it reads, what it may push, and the steps it is handed. */
|
|
5
|
-
export
|
|
5
|
+
export type SyncOptions = {
|
|
6
6
|
/** When true, use entry.bootstrapExclude globs as rsync excludes. Caller opts-in per run. */
|
|
7
7
|
applyBootstrapExclude?: boolean;
|
|
8
8
|
/** Report what would be pushed and push nothing. */
|
|
@@ -10,7 +10,7 @@ export interface SyncOptions {
|
|
|
10
10
|
/** The message of the first commit to an empty public repo; `chore: initial release` by default. */
|
|
11
11
|
initialMessage?: string;
|
|
12
12
|
/** Package names (`name` or `project/name`) to sync; every package when absent or empty. */
|
|
13
|
-
only?: string[];
|
|
13
|
+
only?: string[] | undefined;
|
|
14
14
|
/** Push over a tip the sync bot did not commit. Bootstrap only: it has already judged the repo. */
|
|
15
15
|
overwriteForeignTip?: boolean;
|
|
16
16
|
/** The git toplevel every package path is relative to. */
|
|
@@ -21,9 +21,9 @@ export interface SyncOptions {
|
|
|
21
21
|
specGate: (spec: SpecSource) => Promise<SpecGate>;
|
|
22
22
|
/** The GitHub token every push and API call authenticates with. */
|
|
23
23
|
token: string;
|
|
24
|
-
}
|
|
24
|
+
};
|
|
25
25
|
/** What syncing one package did: pushed, skipped as unchanged, or failed, and why. */
|
|
26
|
-
export
|
|
26
|
+
export type PackageResult = {
|
|
27
27
|
mirrorSha?: string;
|
|
28
28
|
name: string;
|
|
29
29
|
reason?: string;
|
|
@@ -33,7 +33,7 @@ export interface PackageResult {
|
|
|
33
33
|
status: "failed" | "skipped" | "synced";
|
|
34
34
|
/** The release tag pushed (or, on a dry run, that would be) at the public commit equal to the source. */
|
|
35
35
|
tag?: string;
|
|
36
|
-
}
|
|
36
|
+
};
|
|
37
37
|
/**
|
|
38
38
|
* Mirrors one package into its public repo: a snapshot commit when its tree changed (generated output only when prod
|
|
39
39
|
* serves the committed spec), the release tag its state records, and its repo settings. Refuses a public tip another
|
package/dist/release/sync.js
CHANGED
|
@@ -17,8 +17,8 @@ function describeSettings(result, dryRun) {
|
|
|
17
17
|
return "settings in step";
|
|
18
18
|
return `settings ${dryRun ? "would change" : "changed"}: ${result.changed.join(", ")}`;
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
// Applies the repo settings after the tree step, whatever it did: a failure there fails the package but keeps
|
|
21
|
+
// what already landed (the pushed commit and tag) in the result.
|
|
22
22
|
async function withSettings(entry, options, result) {
|
|
23
23
|
try {
|
|
24
24
|
return {
|
|
@@ -36,8 +36,8 @@ async function withSettings(entry, options, result) {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
const tagged = (tag) => (tag === undefined ? {} : { tag });
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
// Only the bot commits to a public repo. Any other committer is a hand edit there that a snapshot would silently
|
|
40
|
+
// erase: its reason, or undefined when the tip is the bot's, there is none, or bootstrap judged it already.
|
|
41
41
|
async function foreignTip({ entry, options, remoteTip, worktree }) {
|
|
42
42
|
if (!remoteTip || options.overwriteForeignTip)
|
|
43
43
|
return undefined;
|
|
@@ -46,8 +46,8 @@ async function foreignTip({ entry, options, remoteTip, worktree }) {
|
|
|
46
46
|
return undefined;
|
|
47
47
|
return `external push detected on mirror; reconcile manually (tip=${remoteTip}, committer=${committer})`;
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
// The source's tree over the public tip, with the release workflow its registries render (a package that ships its
|
|
50
|
+
// own lists none): why it cannot be, or undefined.
|
|
51
51
|
async function stageTree({ entry, options, worktree }) {
|
|
52
52
|
await replaceTreeWithSource({
|
|
53
53
|
excludes: options.applyBootstrapExclude && entry.bootstrapExclude ? [...entry.bootstrapExclude] : [],
|
|
@@ -58,14 +58,13 @@ async function stageTree({ entry, options, worktree }) {
|
|
|
58
58
|
if (entry.registries.length === 0)
|
|
59
59
|
return undefined;
|
|
60
60
|
const workflow = join(worktree, RELEASE_WORKFLOW);
|
|
61
|
-
if (existsSync(workflow))
|
|
61
|
+
if (existsSync(workflow))
|
|
62
62
|
return `${join(entry.path, RELEASE_WORKFLOW)} exists and the package lists registries: drop one, the other writes it`;
|
|
63
|
-
}
|
|
64
63
|
mkdirSync(dirname(workflow), { recursive: true });
|
|
65
64
|
writeFileSync(workflow, releaseWorkflow(entry.registries));
|
|
66
65
|
return undefined;
|
|
67
66
|
}
|
|
68
|
-
|
|
67
|
+
// Generated output prod does not serve must not ship; hand files go out regardless: the refusal, or undefined.
|
|
69
68
|
async function drift({ entry, key, options, worktree }) {
|
|
70
69
|
const generated = generatedPaths(join(options.repoRoot, entry.path));
|
|
71
70
|
const changed = (await gitOutput(["-C", worktree, "diff", "--cached", "--name-only"], worktree)).split("\n");
|
|
@@ -74,7 +73,7 @@ async function drift({ entry, key, options, worktree }) {
|
|
|
74
73
|
const gate = await options.specGate(entry.spec);
|
|
75
74
|
return gate.status === "drift" ? describeDrift(key, gate) : undefined;
|
|
76
75
|
}
|
|
77
|
-
|
|
76
|
+
// the tip is the bot's and equals the source, so a tag a stopped run never pushed goes on it
|
|
78
77
|
async function skipUnchanged(run) {
|
|
79
78
|
const { entry, httpsUrl, key, options, worktree } = run;
|
|
80
79
|
const tag = await pendingTag(entry, options.repoRoot, worktree, httpsUrl);
|
|
@@ -87,7 +86,7 @@ async function skipUnchanged(run) {
|
|
|
87
86
|
...tagged(tag),
|
|
88
87
|
});
|
|
89
88
|
}
|
|
90
|
-
|
|
89
|
+
// the snapshot committed and pushed, then its tag; on a dry run, what would be
|
|
91
90
|
async function push(run) {
|
|
92
91
|
const { entry, headSha, httpsUrl, key, message, options, remoteTip, worktree } = run;
|
|
93
92
|
const tag = await pendingTag(entry, options.repoRoot, worktree, httpsUrl);
|
|
@@ -106,7 +105,7 @@ async function push(run) {
|
|
|
106
105
|
await pushTag(worktree, httpsUrl, tag);
|
|
107
106
|
return withSettings(entry, options, { mirrorSha, name: key, sourceSha: headSha, status: "synced", ...tagged(tag) });
|
|
108
107
|
}
|
|
109
|
-
|
|
108
|
+
// each step in turn; the first that refuses fails the package
|
|
110
109
|
async function syncIn(run) {
|
|
111
110
|
await prepareWorktree(run.worktree, run.httpsUrl, run.entry, run.remoteTip);
|
|
112
111
|
const foreign = await foreignTip(run);
|
|
@@ -138,7 +137,7 @@ export async function syncPackage(entry, options) {
|
|
|
138
137
|
sidecarMessage: readSidecar(options.repoRoot, entry.path),
|
|
139
138
|
templateMessage: entry.commitMessage,
|
|
140
139
|
});
|
|
141
|
-
const worktree = mkdtempSync(join(tmpdir(), `public-sync-${key.
|
|
140
|
+
const worktree = mkdtempSync(join(tmpdir(), `public-sync-${key.replaceAll("/", "-")}-`));
|
|
142
141
|
try {
|
|
143
142
|
return await syncIn({ entry, headSha, httpsUrl, key, message, options, remoteTip, worktree });
|
|
144
143
|
}
|
package/dist/release/types.d.ts
CHANGED
|
@@ -6,34 +6,34 @@ import type { Target } from "../gen/targets.ts";
|
|
|
6
6
|
/** The package metadata clients writes into each generated package's manifest; every field is optional. */
|
|
7
7
|
export type PackageMetadata = {
|
|
8
8
|
/** Authors, as `Name <email>`. */
|
|
9
|
-
readonly authors?: readonly string[];
|
|
9
|
+
readonly authors?: readonly string[] | undefined;
|
|
10
10
|
/** The changelog's URL. */
|
|
11
|
-
readonly changelog?: string;
|
|
11
|
+
readonly changelog?: string | undefined;
|
|
12
12
|
/** The copyright line. */
|
|
13
|
-
readonly copyright?: string;
|
|
13
|
+
readonly copyright?: string | undefined;
|
|
14
14
|
/** A one-line description. */
|
|
15
|
-
readonly description?: string;
|
|
15
|
+
readonly description?: string | undefined;
|
|
16
16
|
/** The documentation's URL. */
|
|
17
|
-
readonly documentation?: string;
|
|
17
|
+
readonly documentation?: string | undefined;
|
|
18
18
|
/** The project's site. */
|
|
19
|
-
readonly homepage?: string;
|
|
19
|
+
readonly homepage?: string | undefined;
|
|
20
20
|
/** The package icon's path, relative to the repo root. */
|
|
21
|
-
readonly icon?: string;
|
|
21
|
+
readonly icon?: string | undefined;
|
|
22
22
|
/** Registry search keywords. */
|
|
23
|
-
readonly keywords?: readonly string[];
|
|
23
|
+
readonly keywords?: readonly string[] | undefined;
|
|
24
24
|
/** The SPDX license expression. */
|
|
25
|
-
readonly license?: string;
|
|
25
|
+
readonly license?: string | undefined;
|
|
26
26
|
/** The license file's path, relative to the repo root. */
|
|
27
|
-
readonly licenseFile?: string;
|
|
27
|
+
readonly licenseFile?: string | undefined;
|
|
28
28
|
/** The README's path, relative to the repo root. */
|
|
29
|
-
readonly readme?: string;
|
|
29
|
+
readonly readme?: string | undefined;
|
|
30
30
|
/** The source repository's URL. */
|
|
31
|
-
readonly repository?: string;
|
|
31
|
+
readonly repository?: string | undefined;
|
|
32
32
|
};
|
|
33
33
|
/** How clients generates a package: its target, and that target's options by name. */
|
|
34
34
|
export type ReleaseRun = {
|
|
35
35
|
/** The target's options, as `--<target>-<option>` takes them; none by default. */
|
|
36
|
-
readonly options?: Readonly<Record<string, string
|
|
36
|
+
readonly options?: Readonly<Record<string, string>> | undefined;
|
|
37
37
|
/** The clients target the package is generated with. */
|
|
38
38
|
readonly target: Target;
|
|
39
39
|
};
|
|
@@ -50,21 +50,21 @@ export type Registry = {
|
|
|
50
50
|
/** One public package a config lists. */
|
|
51
51
|
export type ManifestPackageInput = {
|
|
52
52
|
/** Globs bootstrap leaves out of the first push, so the next sync adds them as their own commit. */
|
|
53
|
-
readonly bootstrapExclude?: readonly string[];
|
|
53
|
+
readonly bootstrapExclude?: readonly string[] | undefined;
|
|
54
54
|
/** The public repo's branch sync pushes to; `main` by default. */
|
|
55
|
-
readonly branch?: string;
|
|
55
|
+
readonly branch?: string | undefined;
|
|
56
56
|
/** The message of each snapshot commit, unless the package's `.publicsync-message` names one. */
|
|
57
57
|
readonly commitMessage: string;
|
|
58
58
|
/** The package's one description: its public repo's, every registry's, and its first CHANGELOG entry. */
|
|
59
59
|
readonly description: string;
|
|
60
60
|
/** How clients generates it; absent for a package backend does not generate (the spec itself). */
|
|
61
|
-
readonly clients?: ReleaseRun;
|
|
61
|
+
readonly clients?: ReleaseRun | undefined;
|
|
62
62
|
/** This package's metadata over the product's; its keywords add to the product's. */
|
|
63
|
-
readonly metadata?: PackageMetadata;
|
|
63
|
+
readonly metadata?: PackageMetadata | undefined;
|
|
64
64
|
/** The package's slug: its dir beside the config, and the key bump's `RELEASE_VERSIONS` uses. */
|
|
65
65
|
readonly name: string;
|
|
66
66
|
/** The registries its release workflow publishes to; none for a package that ships its own workflow. */
|
|
67
|
-
readonly registries?: readonly Registry[];
|
|
67
|
+
readonly registries?: readonly Registry[] | undefined;
|
|
68
68
|
/** The public repo, `owner/name` on github.com. */
|
|
69
69
|
readonly repo: string;
|
|
70
70
|
/** The public repo's GitHub topics, 1 to 20 lowercase slugs. */
|
|
@@ -89,11 +89,11 @@ export type ReleaseManifestInput = {
|
|
|
89
89
|
/** The command that regenerates every package of the product, run from the repo root; it honors `RELEASE_VERSIONS`. */
|
|
90
90
|
readonly codegen: readonly string[];
|
|
91
91
|
/** The identity sync commits as; `public-sync[bot]` by default, so another org's GitHub App can sync. */
|
|
92
|
-
readonly committer?: Committer;
|
|
92
|
+
readonly committer?: Committer | undefined;
|
|
93
93
|
/** The product's site: every public repo's website and every registry's homepage. */
|
|
94
94
|
readonly homepage: string;
|
|
95
95
|
/** The metadata every generated package of the product carries. */
|
|
96
|
-
readonly metadata?: PackageMetadata;
|
|
96
|
+
readonly metadata?: PackageMetadata | undefined;
|
|
97
97
|
/** The product's public packages, at least one; each name and repo once. */
|
|
98
98
|
readonly packages: readonly ManifestPackageInput[];
|
|
99
99
|
/** The product's slug: the `<project>/<name>` key of each package, and what its codegen filters on. */
|
|
@@ -104,14 +104,14 @@ export type ReleaseManifestInput = {
|
|
|
104
104
|
* The tools codegen needs, each a dep name in the repo root's `system.config.ts`; the release workflows install
|
|
105
105
|
* them in this order before codegen runs. None by default.
|
|
106
106
|
*/
|
|
107
|
-
readonly toolchains?: readonly string[];
|
|
107
|
+
readonly toolchains?: readonly string[] | undefined;
|
|
108
108
|
};
|
|
109
109
|
/** A listed package with its defaults applied. */
|
|
110
110
|
export type ManifestPackage = ManifestPackageInput & {
|
|
111
111
|
/** The public repo's branch. */
|
|
112
112
|
readonly branch: string;
|
|
113
113
|
/** The package's metadata: the product's, then its own over it. */
|
|
114
|
-
readonly metadata?: PackageMetadata;
|
|
114
|
+
readonly metadata?: PackageMetadata | undefined;
|
|
115
115
|
/** The registries its release workflow publishes to, possibly none. */
|
|
116
116
|
readonly registries: readonly Registry[];
|
|
117
117
|
};
|
package/dist/release/types.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -2,13 +2,12 @@ type VersionFile = {
|
|
|
2
2
|
read(text: string): string;
|
|
3
3
|
write(text: string, version: string): string;
|
|
4
4
|
};
|
|
5
|
+
/** The name of a version file bump keeps in step. */
|
|
6
|
+
export type VersionFileName = "jsr.json" | "package.json" | "pyproject.toml";
|
|
5
7
|
/** The version files bump keeps in step with `.release-state.json`, by file name. */
|
|
6
8
|
export declare const VERSION_FILES: {
|
|
7
|
-
readonly
|
|
8
|
-
readonly "package.json": VersionFile;
|
|
9
|
-
readonly "pyproject.toml": VersionFile;
|
|
9
|
+
readonly [N in VersionFileName]: VersionFile;
|
|
10
10
|
};
|
|
11
|
-
export type VersionFileName = keyof typeof VERSION_FILES;
|
|
12
11
|
/** Each version file present in `dir` with the version it holds; throws naming the file whose version line is missing or doubled. */
|
|
13
12
|
export declare function readVersions(dir: string): Partial<Record<VersionFileName, string>>;
|
|
14
13
|
/** Writes `version` into every version file present in `dir`; returns the paths written. */
|
|
@@ -3,21 +3,20 @@ import { join } from "node:path";
|
|
|
3
3
|
/** The one match of `pattern` (whose group 1 always participates) in `text`, or a throw naming `what`. */
|
|
4
4
|
function only(text, pattern, what) {
|
|
5
5
|
const matches = [...text.matchAll(new RegExp(pattern.source, `${pattern.flags.replace("g", "")}g`))];
|
|
6
|
-
if (matches.length !== 1)
|
|
6
|
+
if (matches.length !== 1)
|
|
7
7
|
throw new Error(`expected one ${what}, found ${matches.length}`);
|
|
8
|
-
}
|
|
9
8
|
return matches[0];
|
|
10
9
|
}
|
|
11
10
|
function replaceAt(text, match, replacement) {
|
|
12
11
|
return text.slice(0, match.index) + replacement + text.slice(match.index + match[0].length);
|
|
13
12
|
}
|
|
14
|
-
|
|
13
|
+
// The top-level `"version"` of a tab-indented JSON manifest.
|
|
15
14
|
const JSON_VERSION = /^\t"version": "([^"]+)"/m;
|
|
16
15
|
const json = {
|
|
17
16
|
read: (text) => only(text, JSON_VERSION, 'top-level "version"')[1],
|
|
18
17
|
write: (text, version) => replaceAt(text, only(text, JSON_VERSION, 'top-level "version"'), `\t"version": "${version}"`),
|
|
19
18
|
};
|
|
20
|
-
|
|
19
|
+
// The `version = "…"` line of the `[project]` table, which ends at the next table header.
|
|
21
20
|
const PROJECT_TABLE = /^\[project\]\n((?:(?!\[).*\n?)*)/m;
|
|
22
21
|
const TOML_VERSION = /^version = "([^"]+)"$/m;
|
|
23
22
|
const pyproject = {
|
|
@@ -51,7 +50,7 @@ export function readVersions(dir) {
|
|
|
51
50
|
versions[name] = VERSION_FILES[name].read(readFileSync(file, "utf8"));
|
|
52
51
|
}
|
|
53
52
|
catch (error) {
|
|
54
|
-
|
|
53
|
+
// each read throws only `only`'s Error
|
|
55
54
|
throw new Error(`${file}: ${error.message}`, {
|
|
56
55
|
cause: error,
|
|
57
56
|
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The runtime is read as text and vendored into every generated TS and MCP SDK, so it keeps the style the
|
|
2
|
+
// emitters format SDKs in (EMIT_STYLE in src/emit/shared/format.ts), not the repo's: tabs, no semicolons, imports
|
|
3
|
+
// as written. Its byte locks in scripts/sdk-budget*.test.ts measure the shipped text.
|
|
4
|
+
{
|
|
5
|
+
"printWidth": 120,
|
|
6
|
+
"semi": false,
|
|
7
|
+
"useTabs": true,
|
|
8
|
+
}
|
|
@@ -5,42 +5,136 @@ package cli
|
|
|
5
5
|
import (
|
|
6
6
|
"bufio"
|
|
7
7
|
"context"
|
|
8
|
+
"encoding/json"
|
|
8
9
|
"errors"
|
|
10
|
+
"fmt"
|
|
11
|
+
"sort"
|
|
12
|
+
"strconv"
|
|
13
|
+
"strings"
|
|
14
|
+
"sync/atomic"
|
|
9
15
|
|
|
10
16
|
"github.com/coder/websocket"
|
|
11
17
|
)
|
|
12
18
|
|
|
13
|
-
// Socket is a WebSocket connection as the SDK opens it.
|
|
19
|
+
// Socket is a WebSocket connection as the SDK opens it, whatever its messages' types.
|
|
14
20
|
type Socket interface {
|
|
15
|
-
|
|
16
|
-
Read(ctx context.Context) ([]byte, error)
|
|
21
|
+
SendText(ctx context.Context, text string) error
|
|
17
22
|
Close(code websocket.StatusCode, reason string) error
|
|
18
23
|
}
|
|
19
24
|
|
|
25
|
+
// LineCheck refuses an input line that is no message the server receives.
|
|
26
|
+
type LineCheck func(line string) error
|
|
27
|
+
|
|
28
|
+
// JSONLine checks that a line decodes as T and holds each field of required.
|
|
29
|
+
func JSONLine[T any](required ...string) LineCheck {
|
|
30
|
+
return func(line string) error {
|
|
31
|
+
var msg T
|
|
32
|
+
if err := json.Unmarshal([]byte(line), &msg); err != nil {
|
|
33
|
+
var syntax *json.SyntaxError
|
|
34
|
+
if errors.As(err, &syntax) {
|
|
35
|
+
return fmt.Errorf("not valid JSON: %w", err)
|
|
36
|
+
}
|
|
37
|
+
return err
|
|
38
|
+
}
|
|
39
|
+
var fields map[string]json.RawMessage
|
|
40
|
+
_ = json.Unmarshal([]byte(line), &fields) // T decoded: a missing field is all left to find
|
|
41
|
+
for _, name := range required {
|
|
42
|
+
if _, ok := fields[name]; !ok {
|
|
43
|
+
return fmt.Errorf("missing %q", name)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return nil
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Keyed checks a line by the member its key names, one of members.
|
|
51
|
+
func Keyed(key string, members map[string]LineCheck) LineCheck {
|
|
52
|
+
values := make([]string, 0, len(members))
|
|
53
|
+
for value := range members {
|
|
54
|
+
values = append(values, strconv.Quote(value))
|
|
55
|
+
}
|
|
56
|
+
sort.Strings(values)
|
|
57
|
+
want := fmt.Errorf("want an object whose %q is one of %s", key, strings.Join(values, ", "))
|
|
58
|
+
return func(line string) error {
|
|
59
|
+
var fields map[string]json.RawMessage
|
|
60
|
+
if err := json.Unmarshal([]byte(line), &fields); err != nil {
|
|
61
|
+
var syntax *json.SyntaxError
|
|
62
|
+
if errors.As(err, &syntax) {
|
|
63
|
+
return fmt.Errorf("not valid JSON: %w", err)
|
|
64
|
+
}
|
|
65
|
+
return want
|
|
66
|
+
}
|
|
67
|
+
var value string
|
|
68
|
+
_ = json.Unmarshal(fields[key], &value) // no string there: no member's value
|
|
69
|
+
if check, ok := members[value]; ok {
|
|
70
|
+
return check(line)
|
|
71
|
+
}
|
|
72
|
+
return want
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// checked is check's verdict on line; none without a check.
|
|
77
|
+
func checked(check LineCheck, line string) error {
|
|
78
|
+
if check == nil {
|
|
79
|
+
return nil
|
|
80
|
+
}
|
|
81
|
+
return check(line)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Undecoded reports whether err is an E, the SDK's error for a message that does not decode.
|
|
85
|
+
func Undecoded[E error](err error) bool {
|
|
86
|
+
var undecoded E
|
|
87
|
+
return errors.As(err, &undecoded)
|
|
88
|
+
}
|
|
89
|
+
|
|
20
90
|
// Converse sends each standard-input line as a text frame, reading on after its end, and prints
|
|
21
|
-
// each message as one line until the server closes, --take
|
|
22
|
-
// is interrupted (a close frame 1000 "interrupted").
|
|
23
|
-
|
|
91
|
+
// each message read returns as one line (a typed one as its JSON) until the server closes, --take
|
|
92
|
+
// is met (a close frame 1000) or the run is interrupted (a close frame 1000 "interrupted"). With a
|
|
93
|
+
// check, a blank line is skipped and a line it refuses is not sent: the refusal goes to stderr as
|
|
94
|
+
// an error does, the run reads on, and its end is a usage error counting the lines not sent. A
|
|
95
|
+
// message that does not decode, as undecoded tells, ends the run with its error after a close
|
|
96
|
+
// frame 1007.
|
|
97
|
+
func Converse[T any](s *Stream, conn Socket, read func(context.Context) (T, error), check LineCheck, undecoded func(error) bool) error {
|
|
24
98
|
/* reads outlive Ctrl-C, so the close frame goes out and is answered */
|
|
25
99
|
ctx := context.WithoutCancel(s.c.Context())
|
|
26
100
|
stop := context.AfterFunc(s.c.Context(), func() { conn.Close(websocket.StatusNormalClosure, "interrupted") })
|
|
27
101
|
defer stop()
|
|
102
|
+
var refused atomic.Int64
|
|
28
103
|
go func() {
|
|
29
104
|
lines := bufio.NewScanner(s.c.InOrStdin())
|
|
30
|
-
for lines.Scan() {
|
|
31
|
-
|
|
105
|
+
for n := 1; lines.Scan(); n++ {
|
|
106
|
+
line := lines.Text()
|
|
107
|
+
if check != nil && strings.TrimSpace(line) == "" {
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
if err := checked(check, line); err != nil {
|
|
111
|
+
refused.Add(1)
|
|
112
|
+
report(s.c, Usage("input line %d not sent: %v", n, err), false)
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
if conn.SendText(ctx, line) != nil {
|
|
32
116
|
return
|
|
33
117
|
}
|
|
34
118
|
}
|
|
35
119
|
}()
|
|
120
|
+
ended := func(err error) error {
|
|
121
|
+
if n := refused.Load(); err == nil && n > 0 && s.c.Context().Err() == nil {
|
|
122
|
+
return Usage("input lines not sent: %d", n)
|
|
123
|
+
}
|
|
124
|
+
return err
|
|
125
|
+
}
|
|
36
126
|
for {
|
|
37
|
-
message, err :=
|
|
38
|
-
|
|
39
|
-
|
|
127
|
+
message, err := read(ctx)
|
|
128
|
+
switch {
|
|
129
|
+
case err != nil && undecoded(err):
|
|
130
|
+
conn.Close(websocket.StatusInvalidFramePayloadData, "a message did not decode")
|
|
131
|
+
return err
|
|
132
|
+
case err != nil:
|
|
133
|
+
return ended(closedOf(err, s.c.Context().Err() != nil))
|
|
40
134
|
}
|
|
41
|
-
if !s.Write(
|
|
135
|
+
if !s.Write(printed(message)) {
|
|
42
136
|
conn.Close(websocket.StatusNormalClosure, "")
|
|
43
|
-
return s.err
|
|
137
|
+
return ended(s.err)
|
|
44
138
|
}
|
|
45
139
|
}
|
|
46
140
|
}
|
|
@@ -149,38 +149,45 @@ func (e *Closed) Error() string {
|
|
|
149
149
|
return fmt.Sprintf("the server closed the connection: %d %s", e.Code, e.Reason)
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
// Feed is a realtime connection as the SDK opens it.
|
|
152
|
+
// Feed is a realtime connection as the SDK opens it, whatever its messages' types.
|
|
153
153
|
type Feed interface {
|
|
154
|
-
Events() <-chan any
|
|
155
154
|
Errors() <-chan error
|
|
156
155
|
Cursor() (lastEventID, reconnectToken string)
|
|
157
156
|
Close() error
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
// Resume prints each
|
|
159
|
+
// Resume prints each message of events, the feed's, until it ends, --take is met or the run is
|
|
161
160
|
// interrupted; then its cursor, to resume from.
|
|
162
|
-
func (s *Stream
|
|
163
|
-
err :=
|
|
161
|
+
func Resume[T any](s *Stream, feed Feed, events <-chan T) error {
|
|
162
|
+
err := follow(s, feed, events)
|
|
164
163
|
feed.Close()
|
|
165
164
|
s.Cursor(feed.Cursor())
|
|
166
165
|
return err
|
|
167
166
|
}
|
|
168
167
|
|
|
169
|
-
|
|
168
|
+
// printed is a message as the CLI prints it: bytes or a string as text, any other value as JSON.
|
|
169
|
+
func printed(message any) any {
|
|
170
|
+
switch v := message.(type) {
|
|
171
|
+
case []byte:
|
|
172
|
+
return textOf(v)
|
|
173
|
+
case string:
|
|
174
|
+
return textOf([]byte(v))
|
|
175
|
+
}
|
|
176
|
+
return message
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
func follow[T any](s *Stream, feed Feed, events <-chan T) error {
|
|
170
180
|
for {
|
|
171
181
|
select {
|
|
172
182
|
case <-s.c.Context().Done():
|
|
173
183
|
return nil
|
|
174
184
|
case err := <-feed.Errors():
|
|
175
185
|
return err
|
|
176
|
-
case v, ok := <-
|
|
186
|
+
case v, ok := <-events:
|
|
177
187
|
if !ok {
|
|
178
188
|
return nil
|
|
179
189
|
}
|
|
180
|
-
if
|
|
181
|
-
v = textOf([]byte(text))
|
|
182
|
-
}
|
|
183
|
-
if !s.Write(v) {
|
|
190
|
+
if !s.Write(printed(v)) {
|
|
184
191
|
return s.err
|
|
185
192
|
}
|
|
186
193
|
}
|