sdkloom 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// sdkloom TypeScript kit: reading a stream of server-sent events.
|
|
2
|
+
|
|
3
|
+
import { DecodeError, encodeJson, type Decoder } from "./models.ts";
|
|
4
|
+
|
|
5
|
+
// @block sse.parse
|
|
6
|
+
// @summary Reads a text/event-stream response as it arrives: splits lines on CRLF, LF or CR (also across chunks), collects event, data and id fields, dispatches on a blank line, drops an unfinished event at the end, and decodes each event's data (JSON for a typed stream, text for a string stream).
|
|
7
|
+
// @versions >=18
|
|
8
|
+
// @source [1] https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
9
|
+
// @quote [1] "Event streams in this format must always be encoded as UTF-8."
|
|
10
|
+
// @quote [1] "Once the end of the file is reached, any pending data must be discarded."
|
|
11
|
+
// @quote [1] "If value starts with a U+0020 SPACE"
|
|
12
|
+
// @retrieved 2026-12-06
|
|
13
|
+
// @tests tests/sse.test.ts
|
|
14
|
+
// @requirement SDKL-SSE-001
|
|
15
|
+
|
|
16
|
+
/** One server-sent event: its type ("message" unless the server named one), the last event ID and its data. */
|
|
17
|
+
export interface ServerSentEvent<T> {
|
|
18
|
+
readonly event: string;
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly data: T;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Parses the WHATWG event-stream format; yields each event as soon as its blank line arrives. */
|
|
24
|
+
export async function* parseEvents(chunks: AsyncIterable<Uint8Array>): AsyncGenerator<ServerSentEvent<string>, void, undefined> {
|
|
25
|
+
// TextDecoder drops one leading byte order mark and keeps multi-byte characters split across chunks whole.
|
|
26
|
+
const decoder = new TextDecoder();
|
|
27
|
+
let buffer = "";
|
|
28
|
+
let type = "";
|
|
29
|
+
let data = "";
|
|
30
|
+
let lastId = "";
|
|
31
|
+
const line = (text: string): ServerSentEvent<string> | undefined => {
|
|
32
|
+
if (text === "") {
|
|
33
|
+
if (data === "") {
|
|
34
|
+
type = "";
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const event = { event: type === "" ? "message" : type, id: lastId, data: data.endsWith("\n") ? data.slice(0, -1) : data };
|
|
38
|
+
type = "";
|
|
39
|
+
data = "";
|
|
40
|
+
return event;
|
|
41
|
+
}
|
|
42
|
+
if (text.startsWith(":")) return undefined;
|
|
43
|
+
const colon = text.indexOf(":");
|
|
44
|
+
const field = colon === -1 ? text : text.slice(0, colon);
|
|
45
|
+
const raw = colon === -1 ? "" : text.slice(colon + 1);
|
|
46
|
+
const value = raw.startsWith(" ") ? raw.slice(1) : raw;
|
|
47
|
+
if (field === "event") type = value;
|
|
48
|
+
else if (field === "data") data += `${value}\n`;
|
|
49
|
+
else if (field === "id" && !value.includes("\0")) lastId = value;
|
|
50
|
+
return undefined;
|
|
51
|
+
};
|
|
52
|
+
const drain = function* (final: boolean): Generator<ServerSentEvent<string>> {
|
|
53
|
+
for (;;) {
|
|
54
|
+
const end = buffer.search(/[\r\n]/);
|
|
55
|
+
if (end === -1) return;
|
|
56
|
+
// A CR at the end of what has arrived may be the first half of a CRLF.
|
|
57
|
+
if (buffer[end] === "\r" && end === buffer.length - 1 && !final) return;
|
|
58
|
+
const next = buffer[end] === "\r" && buffer[end + 1] === "\n" ? end + 2 : end + 1;
|
|
59
|
+
const event = line(buffer.slice(0, end));
|
|
60
|
+
buffer = buffer.slice(next);
|
|
61
|
+
if (event !== undefined) yield event;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
for await (const chunk of chunks) {
|
|
65
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
66
|
+
yield* drain(false);
|
|
67
|
+
}
|
|
68
|
+
buffer += decoder.decode();
|
|
69
|
+
yield* drain(true);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Events whose data is read with the decoder after JSON.parse, or kept as text when there is no decoder. */
|
|
73
|
+
export async function* decodeEvents<T>(events: AsyncIterable<ServerSentEvent<string>>, decode: Decoder<T> | undefined): AsyncGenerator<ServerSentEvent<T>, void, undefined> {
|
|
74
|
+
for await (const event of events) {
|
|
75
|
+
if (decode === undefined) {
|
|
76
|
+
yield event as ServerSentEvent<unknown> as ServerSentEvent<T>;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
let value: unknown;
|
|
80
|
+
try {
|
|
81
|
+
value = JSON.parse(event.data) as unknown;
|
|
82
|
+
} catch (error: unknown) {
|
|
83
|
+
throw new DecodeError("$", `the event data is not JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
84
|
+
}
|
|
85
|
+
yield { event: event.event, id: event.id, data: decode(value, "$") };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Writes events in the event-stream format (mocks and tests); data that is not a string is written as JSON. */
|
|
89
|
+
export function encodeEvents(events: readonly ServerSentEvent<unknown>[]): string {
|
|
90
|
+
return events
|
|
91
|
+
.map((event) => {
|
|
92
|
+
const data = typeof event.data === "string" ? event.data : new TextDecoder().decode(encodeJson(event.data));
|
|
93
|
+
const lines = [
|
|
94
|
+
...(event.event === "message" ? [] : [`event: ${event.event}`]),
|
|
95
|
+
...(event.id === "" ? [] : [`id: ${event.id}`]),
|
|
96
|
+
...data.split(/\r\n|\r|\n/).map((line) => `data: ${line}`),
|
|
97
|
+
];
|
|
98
|
+
return `${lines.join("\n")}\n\n`;
|
|
99
|
+
})
|
|
100
|
+
.join("");
|
|
101
|
+
}
|
|
102
|
+
// @end
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// sdkloom TypeScript kit: test helpers shipped inside every generated SDK.
|
|
2
|
+
|
|
3
|
+
import type { Clock } from "./context.ts";
|
|
4
|
+
import { HttpResponse, type HeaderList, type HttpRequest } from "./http.ts";
|
|
5
|
+
import { encodeJson, type Decoder } from "./models.ts";
|
|
6
|
+
import { encodeEvents, type ServerSentEvent } from "./sse.ts";
|
|
7
|
+
import type { SendOptions } from "./transport.ts";
|
|
8
|
+
|
|
9
|
+
// @block testing.mock_transport
|
|
10
|
+
// @summary A transport for tests: answers requests from routes registered by method and path template, records every call and fails requests no route answers.
|
|
11
|
+
// @versions >=18
|
|
12
|
+
// @source [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
|
|
13
|
+
// @quote [1] "The async function declaration creates a binding of a new async function to a given name."
|
|
14
|
+
// @retrieved 2026-12-05
|
|
15
|
+
// @tests tests/testing.test.ts
|
|
16
|
+
// @requirement SDKL-MOCK-001
|
|
17
|
+
export interface MockReply {
|
|
18
|
+
readonly status: number;
|
|
19
|
+
readonly json?: unknown;
|
|
20
|
+
readonly body?: Uint8Array;
|
|
21
|
+
readonly headers?: HeaderList;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MockAnswer = MockReply | Error;
|
|
25
|
+
export type RequestPredicate = (request: HttpRequest) => boolean;
|
|
26
|
+
|
|
27
|
+
function pathOf(url: string): string {
|
|
28
|
+
return url.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i, "").split("?")[0] ?? "";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function escapeForRegExp(text: string): string {
|
|
32
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class MockRoute {
|
|
36
|
+
readonly method: string;
|
|
37
|
+
readonly template: string;
|
|
38
|
+
readonly #transport: MockTransport;
|
|
39
|
+
readonly #pattern: RegExp;
|
|
40
|
+
readonly #predicate: RequestPredicate;
|
|
41
|
+
readonly #answers: MockAnswer[] = [];
|
|
42
|
+
readonly #calls: HttpRequest[] = [];
|
|
43
|
+
|
|
44
|
+
constructor(transport: MockTransport, method: string, template: string, predicate: RequestPredicate = () => true) {
|
|
45
|
+
this.#transport = transport;
|
|
46
|
+
this.method = method;
|
|
47
|
+
this.template = template;
|
|
48
|
+
this.#predicate = predicate;
|
|
49
|
+
// {name} matches one segment, {+name} one or more.
|
|
50
|
+
const parts = template.split(/(\{[^{}]+\})/);
|
|
51
|
+
this.#pattern = new RegExp(`^${parts.map((part, index) => (index % 2 === 0 ? escapeForRegExp(part) : part.startsWith("{+") ? ".+" : "[^/]+")).join("")}$`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
matches(request: HttpRequest): boolean {
|
|
55
|
+
return request.method === this.method && this.#pattern.test(pathOf(request.url)) && this.#predicate(request);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Adds an answer. Answers are used in order; the last one repeats. */
|
|
59
|
+
reply(answer: MockAnswer): this {
|
|
60
|
+
this.#answers.push(answer);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A new route for the same operation that only answers requests matching `predicate`. */
|
|
65
|
+
narrow(predicate: RequestPredicate): MockRoute {
|
|
66
|
+
return this.#transport.route(this.method, this.template, predicate);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get calls(): readonly HttpRequest[] {
|
|
70
|
+
return [...this.#calls];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether the route has an answer yet. */
|
|
74
|
+
get answered(): boolean {
|
|
75
|
+
return this.#answers.length > 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
answer(request: HttpRequest): MockAnswer {
|
|
79
|
+
this.#calls.push(request);
|
|
80
|
+
const answer = this.#answers[Math.min(this.#calls.length, this.#answers.length) - 1];
|
|
81
|
+
if (answer === undefined) throw new Error(`mock for ${this.method} ${this.template} has no answer`);
|
|
82
|
+
return answer;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class MockTransport {
|
|
87
|
+
readonly #routes: MockRoute[] = [];
|
|
88
|
+
readonly #calls: HttpRequest[] = [];
|
|
89
|
+
|
|
90
|
+
route(method: string, template: string, predicate?: RequestPredicate): MockRoute {
|
|
91
|
+
const route = new MockRoute(this, method, template, predicate);
|
|
92
|
+
this.#routes.push(route);
|
|
93
|
+
return route;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get calls(): readonly HttpRequest[] {
|
|
97
|
+
return [...this.#calls];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The Transport function; routes added later take priority, and a route with answers beats one without, so a mock for
|
|
102
|
+
* /users/me still answers when /users/{id} (registered later, with no answer yet) matches too.
|
|
103
|
+
*/
|
|
104
|
+
readonly send = async (request: HttpRequest, _options: SendOptions): Promise<HttpResponse> => {
|
|
105
|
+
this.#calls.push(request);
|
|
106
|
+
const matching = [...this.#routes].reverse().filter((candidate) => candidate.matches(request));
|
|
107
|
+
const route = matching.find((candidate) => candidate.answered) ?? matching[0];
|
|
108
|
+
if (route === undefined) throw new Error(`no mock for ${request.method} ${pathOf(request.url)}`);
|
|
109
|
+
const answer = route.answer(request);
|
|
110
|
+
if (answer instanceof Error) throw answer;
|
|
111
|
+
const body = answer.body ?? (answer.json === undefined ? new Uint8Array() : encodeJson(answer.json));
|
|
112
|
+
const headers: HeaderList = [...(answer.json === undefined ? [] : [["content-type", "application/json"] as const]), ...(answer.headers ?? [])];
|
|
113
|
+
return new HttpResponse(answer.status, headers, body);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// @end
|
|
117
|
+
|
|
118
|
+
// @block testing.operation_mock
|
|
119
|
+
// @summary A typed mock for one operation: returns() checks values against the response type, fails() and throws() simulate errors, calls records requests.
|
|
120
|
+
// @versions >=18
|
|
121
|
+
// @source [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
|
|
122
|
+
// @quote [1] "The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified."
|
|
123
|
+
// @retrieved 2026-12-05
|
|
124
|
+
// @tests tests/testing.test.ts
|
|
125
|
+
// @requirement SDKL-MOCK-002
|
|
126
|
+
export class OperationMock<R> {
|
|
127
|
+
readonly #name: string;
|
|
128
|
+
readonly #route: MockRoute;
|
|
129
|
+
readonly #decode: Decoder<unknown> | undefined;
|
|
130
|
+
readonly #events: boolean;
|
|
131
|
+
|
|
132
|
+
/** For an event stream, R is the list of events to send and decode checks each event's data. */
|
|
133
|
+
constructor(name: string, route: MockRoute, decode?: Decoder<unknown>, kind?: "events") {
|
|
134
|
+
this.#name = name;
|
|
135
|
+
this.#route = route;
|
|
136
|
+
this.#decode = decode;
|
|
137
|
+
this.#events = kind === "events";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Answers with a success; the value is checked against the operation's response type first. */
|
|
141
|
+
returns(value: R): this {
|
|
142
|
+
if (this.#events) {
|
|
143
|
+
const events = value as readonly ServerSentEvent<unknown>[];
|
|
144
|
+
const decode = this.#decode;
|
|
145
|
+
if (decode !== undefined) events.forEach((event) => decode(JSON.parse(new TextDecoder().decode(encodeJson(event.data)) || "null") as unknown, "$"));
|
|
146
|
+
this.#route.reply({ status: 200, body: new TextEncoder().encode(encodeEvents(events)), headers: [["content-type", "text/event-stream"]] });
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
if (value instanceof Uint8Array) {
|
|
150
|
+
this.#route.reply({ status: 200, body: value });
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
if (this.#decode !== undefined) this.#decode(JSON.parse(new TextDecoder().decode(encodeJson(value)) || "null") as unknown, "$");
|
|
154
|
+
this.#route.reply({ status: 200, json: value });
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Answers with an HTTP error status and an optional JSON body. */
|
|
159
|
+
fails(status: number, body?: unknown): this {
|
|
160
|
+
this.#route.reply({ status, ...(body === undefined ? {} : { json: body }) });
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Makes the call fail without a response, e.g. a network error or a timeout. */
|
|
165
|
+
throws(error: Error): this {
|
|
166
|
+
this.#route.reply(error);
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Reads naturally between answers: mock.op.fails(503).then.returns(value). */
|
|
171
|
+
get then(): this {
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A mock for the same operation that only answers requests matching `predicate`. */
|
|
176
|
+
when(predicate: RequestPredicate): OperationMock<R> {
|
|
177
|
+
return new OperationMock(this.#name, this.#route.narrow(predicate), this.#decode, this.#events ? "events" : undefined);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
get calls(): readonly HttpRequest[] {
|
|
181
|
+
return this.#route.calls;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
get name(): string {
|
|
185
|
+
return this.#name;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// @end
|
|
189
|
+
|
|
190
|
+
// @block testing.fake_clock
|
|
191
|
+
// @summary A clock for tests that moves only when slept or advanced, so retries and expiries run instantly.
|
|
192
|
+
// @versions >=18
|
|
193
|
+
// @source [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
|
|
194
|
+
// @quote [1] "The Date.now() static method returns the number of milliseconds elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC."
|
|
195
|
+
// @retrieved 2026-12-05
|
|
196
|
+
// @tests tests/testing.test.ts
|
|
197
|
+
// @requirement SDKL-MOCK-002
|
|
198
|
+
export class FakeClock implements Clock {
|
|
199
|
+
#now: number;
|
|
200
|
+
|
|
201
|
+
constructor(start = 0) {
|
|
202
|
+
this.#now = start;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
now(): number {
|
|
206
|
+
return this.#now;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async sleep(ms: number): Promise<void> {
|
|
210
|
+
this.#now += ms;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
advance(ms: number): void {
|
|
214
|
+
this.#now += ms;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// @end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// sdkloom TypeScript kit: the transport that sends requests.
|
|
2
|
+
|
|
3
|
+
import { TransportError } from "./errors.ts";
|
|
4
|
+
import { collectBytes, HttpResponse, type HttpRequest } from "./http.ts";
|
|
5
|
+
|
|
6
|
+
// @block transport.interface
|
|
7
|
+
// @summary The pluggable transport: one request in, one response out. Pass another one to use a different HTTP client.
|
|
8
|
+
// @versions >=18
|
|
9
|
+
// @source [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
|
|
10
|
+
// @quote [1] "The async function declaration creates a binding of a new async function to a given name."
|
|
11
|
+
// @retrieved 2026-12-05
|
|
12
|
+
// @tests tests/transport.test.ts
|
|
13
|
+
// @requirement SDKL-HTTP-004
|
|
14
|
+
export interface SendOptions {
|
|
15
|
+
/** For a streamed response, the time until the headers arrive; the body may then take as long as it needs. */
|
|
16
|
+
readonly timeoutMs?: number;
|
|
17
|
+
readonly signal?: AbortSignal;
|
|
18
|
+
/** Hand a successful response's body over as it arrives instead of reading it first (event streams). */
|
|
19
|
+
readonly stream?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Transport = (request: HttpRequest, options: SendOptions) => Promise<HttpResponse>;
|
|
23
|
+
// @end
|
|
24
|
+
|
|
25
|
+
// @block transport.fetch
|
|
26
|
+
// @summary The default transport, built on the global fetch() with timeouts and cancellation. Bodies are buffered in memory, except a successful response's body when the caller asks for a stream.
|
|
27
|
+
// @versions >=18
|
|
28
|
+
// @source [1] https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
|
|
29
|
+
// @quote [1] "The fetch() method of the Window interface starts the process of fetching a resource from the network, returning a promise that is fulfilled once the response is available."
|
|
30
|
+
// @source [2] https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
31
|
+
// @quote [2] "The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired."
|
|
32
|
+
// @source [3] https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout
|
|
33
|
+
// @quote [3] "The setTimeout() method of the Window interface sets a timer which executes a function or specified piece of code once the timer expires."
|
|
34
|
+
// @source [4] https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer
|
|
35
|
+
// @quote [4] "The arrayBuffer() method of the Response interface takes a Response stream and reads it to completion."
|
|
36
|
+
// @retrieved 2026-12-05
|
|
37
|
+
// @tests tests/transport.test.ts
|
|
38
|
+
// @requirement SDKL-HTTP-004
|
|
39
|
+
export function fetchTransport(): Transport {
|
|
40
|
+
return async (request, options) => {
|
|
41
|
+
// Read the signal each time: it can be aborted while the request is in flight.
|
|
42
|
+
const aborted = (): boolean => options.signal?.aborted === true;
|
|
43
|
+
if (aborted()) throw new TransportError("aborted");
|
|
44
|
+
const controller = new AbortController();
|
|
45
|
+
let timedOut = false;
|
|
46
|
+
const onAbort = (): void => controller.abort();
|
|
47
|
+
options.signal?.addEventListener("abort", onAbort);
|
|
48
|
+
const timer =
|
|
49
|
+
options.timeoutMs === undefined
|
|
50
|
+
? undefined
|
|
51
|
+
: setTimeout(() => {
|
|
52
|
+
timedOut = true;
|
|
53
|
+
controller.abort();
|
|
54
|
+
}, options.timeoutMs);
|
|
55
|
+
const cleanUp = (): void => {
|
|
56
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
57
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
58
|
+
};
|
|
59
|
+
const failure = (error: unknown): TransportError => new TransportError(timedOut ? "timeout" : aborted() ? "aborted" : "network", error);
|
|
60
|
+
|
|
61
|
+
let streaming = false;
|
|
62
|
+
try {
|
|
63
|
+
const body = request.body === undefined ? undefined : await collectBytes(request.body);
|
|
64
|
+
const response = await fetch(request.url, {
|
|
65
|
+
method: request.method,
|
|
66
|
+
headers: request.headers.map(([name, value]): [string, string] => [name, value]),
|
|
67
|
+
...(body === undefined ? {} : { body }),
|
|
68
|
+
signal: controller.signal,
|
|
69
|
+
});
|
|
70
|
+
const reader = options.stream === true && response.ok ? response.body?.getReader() : undefined;
|
|
71
|
+
if (reader !== undefined) {
|
|
72
|
+
// The timeout covers the wait for the headers; the caller's signal still cancels the stream.
|
|
73
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
74
|
+
streaming = true;
|
|
75
|
+
const chunks = async function* (): AsyncGenerator<Uint8Array> {
|
|
76
|
+
try {
|
|
77
|
+
for (;;) {
|
|
78
|
+
const { done, value } = await reader.read().catch((error: unknown) => {
|
|
79
|
+
throw failure(error);
|
|
80
|
+
});
|
|
81
|
+
if (done) return;
|
|
82
|
+
yield value;
|
|
83
|
+
}
|
|
84
|
+
} finally {
|
|
85
|
+
cleanUp();
|
|
86
|
+
reader.releaseLock();
|
|
87
|
+
await response.body?.cancel().catch(() => undefined);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
return new HttpResponse(response.status, [...response.headers], chunks());
|
|
91
|
+
}
|
|
92
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
93
|
+
return new HttpResponse(response.status, [...response.headers], bytes);
|
|
94
|
+
} catch (error: unknown) {
|
|
95
|
+
throw failure(error);
|
|
96
|
+
} finally {
|
|
97
|
+
if (!streaming) cleanUp();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
// @end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// sdkloom TypeScript kit: waiting for a long-running operation.
|
|
2
|
+
|
|
3
|
+
import type { Clock } from "./context.ts";
|
|
4
|
+
|
|
5
|
+
// @block lro.wait
|
|
6
|
+
// @summary Waits for a long-running operation: polls until the result says the work is done, sleeping on the client's clock between polls, and fails with WaitTimeoutError when the next poll would come after the deadline.
|
|
7
|
+
// @versions >=18
|
|
8
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-15.3.3
|
|
9
|
+
// @quote [1] "The 202 (Accepted) status code indicates that the request has been accepted for processing, but the processing has not been completed."
|
|
10
|
+
// @retrieved 2026-12-06
|
|
11
|
+
// @tests tests/wait.test.ts
|
|
12
|
+
// @requirement SDKL-WAIT-001
|
|
13
|
+
|
|
14
|
+
/** The work did not finish within the wait's timeout; the last result read is kept. */
|
|
15
|
+
export class WaitTimeoutError extends Error {
|
|
16
|
+
readonly last: unknown;
|
|
17
|
+
|
|
18
|
+
constructor(message: string, last: unknown) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "WaitTimeoutError";
|
|
21
|
+
this.last = last;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Polls until done(result); the first result comes from starting the work. */
|
|
26
|
+
export async function waitUntil<R>(first: R, poll: (current: R) => Promise<R>, done: (current: R) => boolean, clock: Clock, intervalMs: number, timeoutMs: number): Promise<R> {
|
|
27
|
+
const deadline = clock.now() + timeoutMs;
|
|
28
|
+
let current = first;
|
|
29
|
+
while (!done(current)) {
|
|
30
|
+
if (clock.now() + intervalMs > deadline) throw new WaitTimeoutError(`the work did not finish within ${timeoutMs} ms`, current);
|
|
31
|
+
await clock.sleep(intervalMs);
|
|
32
|
+
current = await poll(current);
|
|
33
|
+
}
|
|
34
|
+
return current;
|
|
35
|
+
}
|
|
36
|
+
// @end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// sdkloom TypeScript kit: verifying the signature of a webhook the API sent, and the HMAC-SHA256 it needs. Written here
|
|
2
|
+
// rather than on WebCrypto so it is synchronous and runs the same on Node.js 18, browsers and edge workers.
|
|
3
|
+
|
|
4
|
+
// @block crypto.hmac_sha256
|
|
5
|
+
// @summary SHA-256 and HMAC-SHA256 over bytes, checked against the RFC 4231 test vectors.
|
|
6
|
+
// @versions >=18
|
|
7
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc2104
|
|
8
|
+
// @quote [1] "HMAC can be used with any iterative cryptographic hash function, e.g., MD5, SHA-1, in combination with a secret shared key."
|
|
9
|
+
// @source [2] https://www.rfc-editor.org/rfc/rfc4231
|
|
10
|
+
// @quote [2] "This document provides test vectors for the HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512 message authentication schemes."
|
|
11
|
+
// @retrieved 2026-12-05
|
|
12
|
+
// @tests tests/webhooks.test.ts
|
|
13
|
+
// @requirement SDKL-HOOK-001
|
|
14
|
+
|
|
15
|
+
const K = new Uint32Array([
|
|
16
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
17
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
18
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
19
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
20
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
21
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const rotate = (value: number, bits: number): number => (value >>> bits) | (value << (32 - bits));
|
|
25
|
+
|
|
26
|
+
/** SHA-256 of the bytes. */
|
|
27
|
+
export function sha256(data: Uint8Array): Uint8Array {
|
|
28
|
+
const length = data.length;
|
|
29
|
+
const padded = new Uint8Array(Math.ceil((length + 9) / 64) * 64);
|
|
30
|
+
padded.set(data);
|
|
31
|
+
padded[length] = 0x80;
|
|
32
|
+
const view = new DataView(padded.buffer);
|
|
33
|
+
view.setUint32(padded.length - 8, Math.floor((length * 8) / 2 ** 32));
|
|
34
|
+
view.setUint32(padded.length - 4, (length * 8) >>> 0);
|
|
35
|
+
const hash = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
36
|
+
const w = new Uint32Array(64);
|
|
37
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
38
|
+
for (let i = 0; i < 16; i += 1) w[i] = view.getUint32(offset + i * 4);
|
|
39
|
+
for (let i = 16; i < 64; i += 1) {
|
|
40
|
+
const a = w[i - 15] ?? 0;
|
|
41
|
+
const b = w[i - 2] ?? 0;
|
|
42
|
+
w[i] = ((w[i - 16] ?? 0) + (rotate(a, 7) ^ rotate(a, 18) ^ (a >>> 3)) + (w[i - 7] ?? 0) + (rotate(b, 17) ^ rotate(b, 19) ^ (b >>> 10))) >>> 0;
|
|
43
|
+
}
|
|
44
|
+
let [a, b, c, d, e, f, g, h] = [hash[0] ?? 0, hash[1] ?? 0, hash[2] ?? 0, hash[3] ?? 0, hash[4] ?? 0, hash[5] ?? 0, hash[6] ?? 0, hash[7] ?? 0];
|
|
45
|
+
for (let i = 0; i < 64; i += 1) {
|
|
46
|
+
const t1 = (h + (rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25)) + ((e & f) ^ (~e & g)) + (K[i] ?? 0) + (w[i] ?? 0)) >>> 0;
|
|
47
|
+
const t2 = ((rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
48
|
+
[h, g, f, e, d, c, b, a] = [g, f, e, (d + t1) >>> 0, c, b, a, (t1 + t2) >>> 0];
|
|
49
|
+
}
|
|
50
|
+
[a, b, c, d, e, f, g, h].forEach((value, i) => {
|
|
51
|
+
hash[i] = ((hash[i] ?? 0) + value) >>> 0;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const out = new Uint8Array(32);
|
|
55
|
+
const outView = new DataView(out.buffer);
|
|
56
|
+
hash.forEach((value, i) => outView.setUint32(i * 4, value));
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const BLOCK = 64;
|
|
61
|
+
|
|
62
|
+
/** HMAC-SHA256 of the message with the key (RFC 2104). */
|
|
63
|
+
export function hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array {
|
|
64
|
+
const shortKey = key.length > BLOCK ? sha256(key) : key;
|
|
65
|
+
const inner = new Uint8Array(BLOCK + message.length);
|
|
66
|
+
const outer = new Uint8Array(BLOCK + 32);
|
|
67
|
+
for (let i = 0; i < BLOCK; i += 1) {
|
|
68
|
+
inner[i] = (shortKey[i] ?? 0) ^ 0x36;
|
|
69
|
+
outer[i] = (shortKey[i] ?? 0) ^ 0x5c;
|
|
70
|
+
}
|
|
71
|
+
inner.set(message, BLOCK);
|
|
72
|
+
outer.set(sha256(inner), BLOCK);
|
|
73
|
+
return sha256(outer);
|
|
74
|
+
}
|
|
75
|
+
// @end
|
|
76
|
+
|
|
77
|
+
// @block webhooks.verify
|
|
78
|
+
// @summary Verifies a webhook's HMAC-SHA256 signature as the definition describes it (signed text from the body and headers, several signatures in one header, an optional timestamp tolerance against replays), comparing in constant time, and fails with WebhookSignatureError.
|
|
79
|
+
// @versions >=18
|
|
80
|
+
// @source [1] https://github.com/standard-webhooks/standard-webhooks/blob/main/spec/standard-webhooks.md
|
|
81
|
+
// @quote [1] "When verifying symmetric signatures, use a constant time comparison function to compare the calculated with the expected signature."
|
|
82
|
+
// @quote [1] "The signature header is a space delimited list of signatures associated with this webhook."
|
|
83
|
+
// @quote [1] "has a timestamp that is within some allowable tolerance of the current timestamp to prevent replay attacks."
|
|
84
|
+
// @retrieved 2026-12-05
|
|
85
|
+
// @tests tests/webhooks.test.ts
|
|
86
|
+
// @requirement SDKL-HOOK-001
|
|
87
|
+
|
|
88
|
+
/** How a webhook is signed; the same shape as the definition's hmacSignature(). */
|
|
89
|
+
export interface WebhookSignature {
|
|
90
|
+
readonly header: string;
|
|
91
|
+
readonly encoding: "hex" | "base64";
|
|
92
|
+
readonly prefix?: string;
|
|
93
|
+
readonly separator?: string;
|
|
94
|
+
readonly signed: string;
|
|
95
|
+
readonly timestamp?: { readonly header: string; readonly toleranceSeconds: number };
|
|
96
|
+
readonly secret: "text" | "base64";
|
|
97
|
+
readonly secretPrefix?: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Request headers as a plain object (Node.js, Express) or a fetch Headers. Names are matched case-insensitively. */
|
|
101
|
+
export type WebhookHeaders = Readonly<Record<string, string | readonly string[] | undefined>> | { get(name: string): string | null };
|
|
102
|
+
|
|
103
|
+
/** The webhook was not signed with the secret, or is too old or too new. */
|
|
104
|
+
export class WebhookSignatureError extends Error {
|
|
105
|
+
constructor(message: string) {
|
|
106
|
+
super(message);
|
|
107
|
+
this.name = "WebhookSignatureError";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function headerOf(headers: WebhookHeaders, name: string): string | undefined {
|
|
112
|
+
if (typeof (headers as { get?: unknown }).get === "function") return (headers as { get(name: string): string | null }).get(name) ?? undefined;
|
|
113
|
+
const entry = Object.entries(headers as Record<string, string | readonly string[] | undefined>).find(([key]) => key.toLowerCase() === name);
|
|
114
|
+
const value = entry?.[1];
|
|
115
|
+
return Array.isArray(value) ? value.join(", ") : (value as string | undefined);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function decodeBase64(text: string): Uint8Array {
|
|
119
|
+
const binary = atob(text);
|
|
120
|
+
return Uint8Array.from(binary, (char: string) => char.charCodeAt(0));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function encode(bytes: Uint8Array, encoding: "hex" | "base64"): string {
|
|
124
|
+
if (encoding === "hex") return [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
125
|
+
return btoa(String.fromCharCode(...bytes));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Equal strings, in time that does not depend on where they differ. */
|
|
129
|
+
function sameText(a: string, b: string): boolean {
|
|
130
|
+
let difference = a.length ^ b.length;
|
|
131
|
+
for (let i = 0; i < Math.max(a.length, b.length); i += 1) difference |= (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0);
|
|
132
|
+
return difference === 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Throws WebhookSignatureError unless one of the header's signatures is the HMAC of the signed text, made in time. */
|
|
136
|
+
export function verifyWebhook(signature: WebhookSignature, body: Uint8Array | string, headers: WebhookHeaders, secret: string, nowSeconds: number): void {
|
|
137
|
+
const raw = typeof body === "string" ? new TextEncoder().encode(body) : body;
|
|
138
|
+
if (signature.timestamp !== undefined) {
|
|
139
|
+
const stamp = headerOf(headers, signature.timestamp.header) ?? "";
|
|
140
|
+
if (!/^[0-9]{1,15}$/.test(stamp.trim())) throw new WebhookSignatureError(`missing or invalid ${signature.timestamp.header} header`);
|
|
141
|
+
const sent = Number(stamp.trim());
|
|
142
|
+
if (Math.abs(nowSeconds - sent) > signature.timestamp.toleranceSeconds) throw new WebhookSignatureError(`the webhook was signed at ${sent}, more than ${signature.timestamp.toleranceSeconds} s from now`);
|
|
143
|
+
}
|
|
144
|
+
const given = headerOf(headers, signature.header);
|
|
145
|
+
if (given === undefined) throw new WebhookSignatureError(`missing ${signature.header} header`);
|
|
146
|
+
const parts: Uint8Array[] = [];
|
|
147
|
+
for (const piece of signature.signed.split(/(\{[^{}]*\})/)) {
|
|
148
|
+
if (piece === "{body}") parts.push(raw);
|
|
149
|
+
else if (piece.startsWith("{") && piece.endsWith("}")) {
|
|
150
|
+
const value = headerOf(headers, piece.slice(1, -1));
|
|
151
|
+
if (value === undefined) throw new WebhookSignatureError(`missing ${piece.slice(1, -1)} header`);
|
|
152
|
+
parts.push(new TextEncoder().encode(value));
|
|
153
|
+
} else if (piece !== "") parts.push(new TextEncoder().encode(piece));
|
|
154
|
+
}
|
|
155
|
+
const message = new Uint8Array(parts.reduce((size, part) => size + part.length, 0));
|
|
156
|
+
parts.reduce((offset, part) => {
|
|
157
|
+
message.set(part, offset);
|
|
158
|
+
return offset + part.length;
|
|
159
|
+
}, 0);
|
|
160
|
+
const keyText = signature.secretPrefix !== undefined && secret.startsWith(signature.secretPrefix) ? secret.slice(signature.secretPrefix.length) : secret;
|
|
161
|
+
const key = signature.secret === "base64" ? decodeBase64(keyText) : new TextEncoder().encode(keyText);
|
|
162
|
+
const expected = encode(hmacSha256(key, message), signature.encoding);
|
|
163
|
+
const prefix = signature.prefix ?? "";
|
|
164
|
+
const candidates = signature.separator === undefined ? [given] : given.split(signature.separator);
|
|
165
|
+
const matches = candidates.filter((candidate) => candidate.startsWith(prefix)).map((candidate) => sameText(candidate.slice(prefix.length), expected));
|
|
166
|
+
if (!matches.includes(true)) throw new WebhookSignatureError("no signature matches: the webhook was not signed with this secret, or was changed after signing");
|
|
167
|
+
}
|
|
168
|
+
// @end
|