@distilled.cloud/core 1.0.0-rc.14 → 1.0.0-rc.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.d.ts +118 -13
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +755 -147
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +146 -146
- package/lib/category.js.map +1 -1
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +11 -0
- package/lib/chunks.js.map +1 -0
- package/lib/codegen/boolean-string-enums.js +36 -36
- package/lib/codegen/chunk.d.ts +7 -0
- package/lib/codegen/chunk.d.ts.map +1 -0
- package/lib/codegen/chunk.js +139 -0
- package/lib/codegen/chunk.js.map +1 -0
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +28 -14
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/datafy.d.ts +14 -0
- package/lib/codegen/datafy.d.ts.map +1 -0
- package/lib/codegen/datafy.js +853 -0
- package/lib/codegen/datafy.js.map +1 -0
- package/lib/codegen/dedupe.d.ts +5 -0
- package/lib/codegen/dedupe.d.ts.map +1 -0
- package/lib/codegen/dedupe.js +153 -0
- package/lib/codegen/dedupe.js.map +1 -0
- package/lib/codegen/emit.js +56 -56
- package/lib/codegen/format.js +18 -18
- package/lib/codegen/generated-at.js +29 -29
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +92 -84
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/graph.js +36 -36
- package/lib/codegen/graphql-client.d.ts +2 -0
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +55 -26
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql.js +113 -113
- package/lib/codegen/latin1.d.ts +3 -0
- package/lib/codegen/latin1.d.ts.map +1 -0
- package/lib/codegen/latin1.js +126 -0
- package/lib/codegen/latin1.js.map +1 -0
- package/lib/codegen/members.d.ts +7 -0
- package/lib/codegen/members.d.ts.map +1 -1
- package/lib/codegen/members.js +26 -19
- package/lib/codegen/members.js.map +1 -1
- package/lib/codegen/model-diff.js +6 -6
- package/lib/codegen/naming.js +19 -19
- package/lib/codegen/openapi-cli.js +15 -15
- package/lib/codegen/openapi.d.ts +17 -0
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +323 -169
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.js +14 -14
- package/lib/codegen/pagination.js +11 -11
- package/lib/codegen/patch-audit.js +57 -57
- package/lib/codegen/patches.js +79 -79
- package/lib/codegen/prelude.js +19 -19
- package/lib/codegen/privatize.d.ts +21 -0
- package/lib/codegen/privatize.d.ts.map +1 -0
- package/lib/codegen/privatize.js +176 -0
- package/lib/codegen/privatize.js.map +1 -0
- package/lib/codegen/proto.js +25 -25
- package/lib/codegen/pure.d.ts +6 -0
- package/lib/codegen/pure.d.ts.map +1 -0
- package/lib/codegen/pure.js +91 -0
- package/lib/codegen/pure.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.js +108 -108
- package/lib/codegen/spec-path.js +57 -57
- package/lib/codegen/table.d.ts +5 -0
- package/lib/codegen/table.d.ts.map +1 -0
- package/lib/codegen/table.js +249 -0
- package/lib/codegen/table.js.map +1 -0
- package/lib/config.d.ts +27 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +161 -0
- package/lib/config.js.map +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +130 -0
- package/lib/crypto.js.map +1 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.d.ts.map +1 -0
- package/lib/debug.js +8 -0
- package/lib/debug.js.map +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +7 -6
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts +62 -62
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +117 -114
- package/lib/errors.js.map +1 -1
- package/lib/graphql.d.ts +31 -20
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +150 -119
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.types.js +4 -3
- package/lib/graphql.types.js.map +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +15 -14
- package/lib/index.js.map +1 -1
- package/lib/json-patch.d.ts +2 -2
- package/lib/json-patch.js +28 -28
- package/lib/pagination.d.ts +9 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +95 -90
- package/lib/pagination.js.map +1 -1
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +40 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protocol-http.d.ts +50 -14
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +351 -178
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-rest.d.ts +38 -25
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +238 -130
- package/lib/protocol-rest.js.map +1 -1
- package/lib/query.js +16 -16
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +284 -0
- package/lib/response-validation-strict.js.map +1 -0
- package/lib/response-validation.d.ts +43 -25
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/response-validation.js +112 -70
- package/lib/response-validation.js.map +1 -1
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.js +49 -49
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +142 -154
- package/lib/retry.js.map +1 -1
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +341 -0
- package/lib/schema-lite-impl.js.map +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +27 -0
- package/lib/schema-lite-typed.js.map +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +31 -0
- package/lib/schema-lite.js.map +1 -0
- package/lib/schema.d.ts +20 -6
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +202 -30
- package/lib/schema.js.map +1 -1
- package/lib/testing.d.ts +3 -2
- package/lib/testing.d.ts.map +1 -1
- package/lib/testing.js +22 -21
- package/lib/testing.js.map +1 -1
- package/lib/trait.d.ts +13 -13
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +80 -80
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/lib/xml.js +38 -26
- package/lib/xml.js.map +1 -1
- package/package.json +9 -7
- package/src/api.ts +1079 -180
- package/src/category.ts +33 -33
- package/src/chunks.ts +30 -0
- package/src/codegen/chunk.ts +147 -0
- package/src/codegen/cli.ts +27 -1
- package/src/codegen/datafy.ts +874 -0
- package/src/codegen/dedupe.ts +173 -0
- package/src/codegen/generator.ts +10 -3
- package/src/codegen/graphql-client.ts +49 -15
- package/src/codegen/latin1.ts +131 -0
- package/src/codegen/members.ts +8 -0
- package/src/codegen/openapi.ts +194 -3
- package/src/codegen/privatize.ts +200 -0
- package/src/codegen/pure.ts +94 -0
- package/src/codegen/table.ts +254 -0
- package/src/config.ts +216 -0
- package/src/crypto.ts +202 -0
- package/src/debug.ts +8 -0
- package/src/error-category.ts +2 -1
- package/src/errors.ts +65 -56
- package/src/graphql.ts +109 -62
- package/src/graphql.types.ts +9 -7
- package/src/index.ts +3 -2
- package/src/json-patch.ts +2 -2
- package/src/pagination.ts +37 -18
- package/src/plain-values.ts +41 -0
- package/src/protocol-http.ts +378 -145
- package/src/protocol-rest.ts +337 -169
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +101 -50
- package/src/retry-after.ts +3 -3
- package/src/retry.ts +94 -93
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +258 -14
- package/src/testing.ts +4 -2
- package/src/trait.ts +36 -28
- package/src/xml.ts +31 -17
- package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
- package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
- package/lib/codegen/boolean-string-enums.test.js +0 -275
- package/lib/codegen/boolean-string-enums.test.js.map +0 -1
- package/lib/codegen/defer-naming.test.d.ts +0 -2
- package/lib/codegen/defer-naming.test.d.ts.map +0 -1
- package/lib/codegen/defer-naming.test.js +0 -105
- package/lib/codegen/defer-naming.test.js.map +0 -1
- package/lib/codegen/generated-at.test.d.ts +0 -2
- package/lib/codegen/generated-at.test.d.ts.map +0 -1
- package/lib/codegen/generated-at.test.js +0 -81
- package/lib/codegen/generated-at.test.js.map +0 -1
- package/lib/codegen/generator.test.d.ts +0 -2
- package/lib/codegen/generator.test.d.ts.map +0 -1
- package/lib/codegen/generator.test.js +0 -121
- package/lib/codegen/generator.test.js.map +0 -1
- package/lib/codegen/graphql-client.test.d.ts +0 -2
- package/lib/codegen/graphql-client.test.d.ts.map +0 -1
- package/lib/codegen/graphql-client.test.js +0 -171
- package/lib/codegen/graphql-client.test.js.map +0 -1
- package/lib/codegen/openapi-binary.test.d.ts +0 -2
- package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
- package/lib/codegen/openapi-binary.test.js +0 -67
- package/lib/codegen/openapi-binary.test.js.map +0 -1
- package/lib/codegen/patch-audit.test.d.ts +0 -2
- package/lib/codegen/patch-audit.test.d.ts.map +0 -1
- package/lib/codegen/patch-audit.test.js +0 -141
- package/lib/codegen/patch-audit.test.js.map +0 -1
- package/lib/codegen/patches.test.d.ts +0 -2
- package/lib/codegen/patches.test.d.ts.map +0 -1
- package/lib/codegen/patches.test.js +0 -155
- package/lib/codegen/patches.test.js.map +0 -1
- package/lib/codegen/protocol-imports.test.d.ts +0 -2
- package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
- package/lib/codegen/protocol-imports.test.js +0 -36
- package/lib/codegen/protocol-imports.test.js.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
- package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.js +0 -451
- package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
- package/lib/codegen/untagged-unions.test.d.ts +0 -2
- package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
- package/lib/codegen/untagged-unions.test.js +0 -80
- package/lib/codegen/untagged-unions.test.js.map +0 -1
- package/lib/fixtures/xml/upstream-validator.json +0 -397
- package/lib/graphql.test.d.ts +0 -2
- package/lib/graphql.test.d.ts.map +0 -1
- package/lib/graphql.test.js +0 -151
- package/lib/graphql.test.js.map +0 -1
- package/lib/json-patch.test.d.ts +0 -2
- package/lib/json-patch.test.d.ts.map +0 -1
- package/lib/json-patch.test.js +0 -65
- package/lib/json-patch.test.js.map +0 -1
- package/lib/pagination.test.d.ts +0 -2
- package/lib/pagination.test.d.ts.map +0 -1
- package/lib/pagination.test.js +0 -131
- package/lib/pagination.test.js.map +0 -1
- package/lib/protocol-http.test.d.ts +0 -2
- package/lib/protocol-http.test.d.ts.map +0 -1
- package/lib/protocol-http.test.js +0 -228
- package/lib/protocol-http.test.js.map +0 -1
- package/lib/protocol-rest.test.d.ts +0 -2
- package/lib/protocol-rest.test.d.ts.map +0 -1
- package/lib/protocol-rest.test.js +0 -498
- package/lib/protocol-rest.test.js.map +0 -1
- package/lib/response-validation.test.d.ts +0 -2
- package/lib/response-validation.test.d.ts.map +0 -1
- package/lib/response-validation.test.js +0 -72
- package/lib/response-validation.test.js.map +0 -1
- package/lib/retry-after.test.d.ts +0 -2
- package/lib/retry-after.test.d.ts.map +0 -1
- package/lib/retry-after.test.js +0 -204
- package/lib/retry-after.test.js.map +0 -1
- package/lib/retry.test.d.ts +0 -2
- package/lib/retry.test.d.ts.map +0 -1
- package/lib/retry.test.js +0 -407
- package/lib/retry.test.js.map +0 -1
- package/lib/sdks.test.d.ts +0 -2
- package/lib/sdks.test.d.ts.map +0 -1
- package/lib/sdks.test.js +0 -76
- package/lib/sdks.test.js.map +0 -1
- package/lib/xml.test.d.ts +0 -2
- package/lib/xml.test.d.ts.map +0 -1
- package/lib/xml.test.js +0 -210
- package/lib/xml.test.js.map +0 -1
- package/src/codegen/boolean-string-enums.test.ts +0 -299
- package/src/codegen/defer-naming.test.ts +0 -113
- package/src/codegen/generated-at.test.ts +0 -89
- package/src/codegen/generator.test.ts +0 -137
- package/src/codegen/graphql-client.test.ts +0 -189
- package/src/codegen/openapi-binary.test.ts +0 -91
- package/src/codegen/patch-audit.test.ts +0 -160
- package/src/codegen/patches.test.ts +0 -188
- package/src/codegen/protocol-imports.test.ts +0 -39
- package/src/codegen/rewrite-operation-ids.test.ts +0 -472
- package/src/codegen/untagged-unions.test.ts +0 -85
- package/src/fixtures/xml/upstream-validator.json +0 -397
- package/src/graphql.test.ts +0 -198
- package/src/json-patch.test.ts +0 -77
- package/src/pagination.test.ts +0 -169
- package/src/protocol-http.test.ts +0 -276
- package/src/protocol-rest.test.ts +0 -703
- package/src/response-validation.test.ts +0 -89
- package/src/retry-after.test.ts +0 -267
- package/src/retry.test.ts +0 -566
- package/src/sdks.test.ts +0 -87
- package/src/xml.test.ts +0 -251
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retry-after.test.d.ts","sourceRoot":"","sources":["../src/retry-after.test.ts"],"names":[],"mappings":""}
|
package/lib/retry-after.test.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import * as Clock from "effect/Clock";
|
|
2
|
-
import * as Duration from "effect/Duration";
|
|
3
|
-
import * as Effect from "effect/Effect";
|
|
4
|
-
import * as Fiber from "effect/Fiber";
|
|
5
|
-
import * as HttpClient from "effect/http/HttpClient";
|
|
6
|
-
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
7
|
-
import * as Layer from "effect/Layer";
|
|
8
|
-
import * as Schema from "effect/Schema";
|
|
9
|
-
import * as TestClock from "effect/testing/TestClock";
|
|
10
|
-
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
11
|
-
import * as API from "./api.js";
|
|
12
|
-
import { RETRYABLE_HTTP_STATUSES } from "./errors.js";
|
|
13
|
-
import { makeRestProtocol } from "./protocol-rest.js";
|
|
14
|
-
import { parseRatelimit, parseRetryAfter, parseRetryAfterForStatus, parseServerRetryHint, } from "./retry-after.js";
|
|
15
|
-
import * as Retry from "./retry.js";
|
|
16
|
-
import * as S from "./schema.js";
|
|
17
|
-
import * as T from "./trait.js";
|
|
18
|
-
const NOW = Date.parse("2026-10-05T12:00:00Z");
|
|
19
|
-
const millis = (d) => d === undefined ? undefined : Duration.toMillis(d);
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
vi.spyOn(Date, "now").mockReturnValue(NOW);
|
|
22
|
-
// Pin retry jitter to zero so end-to-end delays are exact.
|
|
23
|
-
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
24
|
-
});
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
vi.restoreAllMocks();
|
|
27
|
-
});
|
|
28
|
-
describe("parseRetryAfter delay-seconds", () => {
|
|
29
|
-
test("parses a non-negative integer number of seconds", () => {
|
|
30
|
-
expect(millis(parseRetryAfter({ "retry-after": "120" }))).toBe(120_000);
|
|
31
|
-
expect(millis(parseRetryAfter({ "retry-after": "1" }))).toBe(1_000);
|
|
32
|
-
});
|
|
33
|
-
test("zero seconds is a zero duration, not absent", () => {
|
|
34
|
-
expect(millis(parseRetryAfter({ "retry-after": "0" }))).toBe(0);
|
|
35
|
-
});
|
|
36
|
-
test("surrounding whitespace is ignored", () => {
|
|
37
|
-
expect(millis(parseRetryAfter({ "retry-after": " 5\t" }))).toBe(5_000);
|
|
38
|
-
});
|
|
39
|
-
test("leading zeros are accepted", () => {
|
|
40
|
-
expect(millis(parseRetryAfter({ "retry-after": "007" }))).toBe(7_000);
|
|
41
|
-
});
|
|
42
|
-
test("signed or fractional numbers are rejected", () => {
|
|
43
|
-
for (const raw of ["-5", "+5", "1.5", "-0", "0.0"]) {
|
|
44
|
-
expect(parseRetryAfter({ "retry-after": raw })).toBeUndefined();
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
describe("parseRetryAfter HTTP-date", () => {
|
|
49
|
-
test("parses an IMF-fixdate relative to now", () => {
|
|
50
|
-
expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 12:00:30 GMT" }))).toBe(30_000);
|
|
51
|
-
});
|
|
52
|
-
test("parses the obsolete RFC 850 and asctime forms", () => {
|
|
53
|
-
expect(millis(parseRetryAfter({ "retry-after": "Monday, 05-Oct-26 12:01:00 GMT" }))).toBe(60_000);
|
|
54
|
-
expect(millis(parseRetryAfter({ "retry-after": "Mon Oct 5 12:00:10 2026 GMT" }))).toBe(10_000);
|
|
55
|
-
});
|
|
56
|
-
test("a date in the past is a zero duration", () => {
|
|
57
|
-
expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 11:00:00 GMT" }))).toBe(0);
|
|
58
|
-
});
|
|
59
|
-
test("a date equal to now is a zero duration", () => {
|
|
60
|
-
expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 12:00:00 GMT" }))).toBe(0);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
describe("parseRetryAfter absent or invalid headers", () => {
|
|
64
|
-
test("no header bag, or no retry-after header, is absent", () => {
|
|
65
|
-
expect(parseRetryAfter(undefined)).toBeUndefined();
|
|
66
|
-
expect(parseRetryAfter({})).toBeUndefined();
|
|
67
|
-
expect(parseRetryAfter({ "retry-after": undefined })).toBeUndefined();
|
|
68
|
-
});
|
|
69
|
-
test("empty and whitespace-only values are absent", () => {
|
|
70
|
-
expect(parseRetryAfter({ "retry-after": "" })).toBeUndefined();
|
|
71
|
-
expect(parseRetryAfter({ "retry-after": " " })).toBeUndefined();
|
|
72
|
-
});
|
|
73
|
-
test("unparseable values are absent", () => {
|
|
74
|
-
for (const raw of ["soon", "5s", "PT5S", "tomorrow at noon"]) {
|
|
75
|
-
expect(parseRetryAfter({ "retry-after": raw })).toBeUndefined();
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
test("only the lowercase header key is read", () => {
|
|
79
|
-
expect(parseRetryAfter({ "Retry-After": "5" })).toBeUndefined();
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
describe("parseRatelimit", () => {
|
|
83
|
-
test("returns the reset time when the remaining quota is zero", () => {
|
|
84
|
-
expect(millis(parseRatelimit({ ratelimit: "r=0, t=30" }))).toBe(30_000);
|
|
85
|
-
expect(millis(parseRatelimit({ ratelimit: "limit=100, remaining=0, reset=30" }))).toBe(30_000);
|
|
86
|
-
expect(millis(parseRatelimit({ ratelimit: '"default";r=0;t=30' }))).toBe(30_000);
|
|
87
|
-
});
|
|
88
|
-
test("tolerates whitespace and key case", () => {
|
|
89
|
-
expect(millis(parseRatelimit({ ratelimit: " R = 0 ; T = 12 " }))).toBe(12_000);
|
|
90
|
-
});
|
|
91
|
-
test("is absent while quota remains", () => {
|
|
92
|
-
expect(parseRatelimit({ ratelimit: "r=5, t=30" })).toBeUndefined();
|
|
93
|
-
expect(parseRatelimit({ ratelimit: "limit=100, remaining=1, reset=30" })).toBeUndefined();
|
|
94
|
-
expect(parseRatelimit({ ratelimit: '"default";r=2;t=30' })).toBeUndefined();
|
|
95
|
-
});
|
|
96
|
-
test("is absent unless remaining is present and exactly zero", () => {
|
|
97
|
-
expect(parseRatelimit({ ratelimit: "t=30" })).toBeUndefined();
|
|
98
|
-
expect(parseRatelimit({ ratelimit: "limit=100, reset=30" })).toBeUndefined();
|
|
99
|
-
expect(parseRatelimit({ ratelimit: '"default";t=30' })).toBeUndefined();
|
|
100
|
-
expect(parseRatelimit({ ratelimit: "r=-1, t=30" })).toBeUndefined();
|
|
101
|
-
expect(parseRatelimit({ ratelimit: "remaining=-1, reset=30" })).toBeUndefined();
|
|
102
|
-
expect(parseRatelimit({ ratelimit: "r=0.5, t=30" })).toBeUndefined();
|
|
103
|
-
expect(parseRatelimit({ ratelimit: "r=none, t=30" })).toBeUndefined();
|
|
104
|
-
});
|
|
105
|
-
test("is absent without a usable reset", () => {
|
|
106
|
-
expect(parseRatelimit({ ratelimit: "r=0" })).toBeUndefined();
|
|
107
|
-
expect(parseRatelimit({ ratelimit: "r=0, t=-1" })).toBeUndefined();
|
|
108
|
-
expect(parseRatelimit({ ratelimit: "r=0, t=later" })).toBeUndefined();
|
|
109
|
-
});
|
|
110
|
-
test("is absent when the header is missing or empty", () => {
|
|
111
|
-
expect(parseRatelimit(undefined)).toBeUndefined();
|
|
112
|
-
expect(parseRatelimit({})).toBeUndefined();
|
|
113
|
-
expect(parseRatelimit({ ratelimit: "" })).toBeUndefined();
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
describe("parseServerRetryHint", () => {
|
|
117
|
-
test("prefers Retry-After over RateLimit", () => {
|
|
118
|
-
expect(millis(parseServerRetryHint({ "retry-after": "5", ratelimit: "r=0, t=30" }))).toBe(5_000);
|
|
119
|
-
});
|
|
120
|
-
test("falls back to RateLimit when Retry-After is absent or invalid", () => {
|
|
121
|
-
expect(millis(parseServerRetryHint({ ratelimit: "r=0, t=30" }))).toBe(30_000);
|
|
122
|
-
expect(millis(parseServerRetryHint({ "retry-after": "soon", ratelimit: "r=0, t=30" }))).toBe(30_000);
|
|
123
|
-
});
|
|
124
|
-
test("is absent when neither header yields a value", () => {
|
|
125
|
-
expect(parseServerRetryHint({ ratelimit: "r=3, t=30" })).toBeUndefined();
|
|
126
|
-
expect(parseServerRetryHint({ ratelimit: "t=30" })).toBeUndefined();
|
|
127
|
-
expect(parseServerRetryHint(undefined)).toBeUndefined();
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
describe("parseRetryAfterForStatus", () => {
|
|
131
|
-
test("returns the hint for every retryable status", () => {
|
|
132
|
-
for (const status of RETRYABLE_HTTP_STATUSES) {
|
|
133
|
-
expect(millis(parseRetryAfterForStatus(status, { "retry-after": "4" }))).toBe(4_000);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
test("ignores hints on non-retryable statuses", () => {
|
|
137
|
-
for (const status of [200, 400, 401, 403, 404, 409, 422, 501, 521]) {
|
|
138
|
-
expect(parseRetryAfterForStatus(status, { "retry-after": "4" })).toBeUndefined();
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
// =============================================================================
|
|
143
|
-
// End to end: header → error.retryAfter → retry delay
|
|
144
|
-
// =============================================================================
|
|
145
|
-
class TestRetry extends Retry.makeRetryService("RetryAfterTest/Retry") {
|
|
146
|
-
}
|
|
147
|
-
class UnknownTestError extends Schema.TaggedError()("UnknownTestError", {
|
|
148
|
-
message: Schema.String,
|
|
149
|
-
}) {
|
|
150
|
-
}
|
|
151
|
-
const TestProtocol = makeRestProtocol({
|
|
152
|
-
credentials: Effect.succeed({}),
|
|
153
|
-
baseUrl: () => "https://api.test",
|
|
154
|
-
headers: () => ({}),
|
|
155
|
-
unknownError: ({ message }) => new UnknownTestError({ message }),
|
|
156
|
-
parseError: ({ body }) => new UnknownTestError({ message: String(body) }),
|
|
157
|
-
});
|
|
158
|
-
const getThing = API.make(() => ({
|
|
159
|
-
input: S.Struct({}).pipe(T.Http({ method: "GET", uri: "/thing" })),
|
|
160
|
-
output: S.Struct({ ok: S.optional(S.Boolean) }),
|
|
161
|
-
protocol: TestProtocol,
|
|
162
|
-
retry: TestRetry,
|
|
163
|
-
}));
|
|
164
|
-
/** First response fails with `status`/`headers`, the second succeeds; returns attempt times. */
|
|
165
|
-
const retryDelays = async (status, headers) => {
|
|
166
|
-
const times = [];
|
|
167
|
-
const client = Layer.succeed(HttpClient.HttpClient, HttpClient.make((request) => Effect.gen(function* () {
|
|
168
|
-
times.push(yield* Clock.currentTimeMillis);
|
|
169
|
-
const response = times.length === 1
|
|
170
|
-
? new Response(JSON.stringify({ message: "nope" }), { status, headers })
|
|
171
|
-
: new Response(JSON.stringify({ ok: true }), { status: 200 });
|
|
172
|
-
return HttpClientResponse.fromWeb(request, response);
|
|
173
|
-
})));
|
|
174
|
-
await Effect.runPromise(Effect.gen(function* () {
|
|
175
|
-
const fiber = yield* Effect.forkChild(Effect.result(getThing({})));
|
|
176
|
-
yield* TestClock.adjust(Duration.hours(1));
|
|
177
|
-
return yield* Fiber.join(fiber);
|
|
178
|
-
}).pipe(Effect.provide(Layer.mergeAll(client, TestClock.layer()))));
|
|
179
|
-
return times.slice(1).map((t, i) => t - times[i]);
|
|
180
|
-
};
|
|
181
|
-
describe("Retry-After feeding the default retry delay", () => {
|
|
182
|
-
test("delay-seconds replaces the default backoff", async () => {
|
|
183
|
-
expect(await retryDelays(503, { "retry-after": "3" })).toEqual([3_000]);
|
|
184
|
-
});
|
|
185
|
-
test("an HTTP-date waits until that date", async () => {
|
|
186
|
-
expect(await retryDelays(429, { "retry-after": "Mon, 05 Oct 2026 12:00:04 GMT" })).toEqual([
|
|
187
|
-
4_000,
|
|
188
|
-
]);
|
|
189
|
-
});
|
|
190
|
-
test("a RateLimit reset is used when Retry-After is absent", async () => {
|
|
191
|
-
expect(await retryDelays(429, { ratelimit: "r=0, t=2" })).toEqual([2_000]);
|
|
192
|
-
});
|
|
193
|
-
test("an invalid header falls back to the default backoff", async () => {
|
|
194
|
-
expect(await retryDelays(503, { "retry-after": "soon" })).toEqual([250]);
|
|
195
|
-
expect(await retryDelays(429, { "retry-after": "soon" })).toEqual([500]);
|
|
196
|
-
});
|
|
197
|
-
test("a RateLimit header with quota left falls back to the default backoff", async () => {
|
|
198
|
-
expect(await retryDelays(503, { ratelimit: "r=4, t=30" })).toEqual([250]);
|
|
199
|
-
});
|
|
200
|
-
test("a hint on a non-retryable status neither stamps nor triggers a retry", async () => {
|
|
201
|
-
expect(await retryDelays(404, { "retry-after": "3" })).toEqual([]);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
//# sourceMappingURL=retry-after.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retry-after.test.js","sourceRoot":"","sources":["../src/retry-after.test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,kBAAkB,MAAM,gCAAgC,CAAC;AACrE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,SAAS,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC3E,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EACL,cAAc,EACd,eAAe,EACf,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,OAAO,KAAK,CAAC,MAAM,aAAa,CAAC;AACjC,OAAO,KAAK,CAAC,MAAM,YAAY,CAAC;AAEhC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;AAE/C,MAAM,MAAM,GAAG,CAAC,CAAgC,EA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,CAAC;IAEH,IAAI,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;QACtE,MAAM,CAAC,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,qDAAqD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,CAAC,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACzE,MAAM,CAAC,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sEAAsE,EAAE,KAAK,IAAI,EAAE;QACtF,MAAM,CAAC,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,sEAAsE,EAAE,KAAK,IAAI,EAAE;QACtF,MAAM,CAAC,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACrE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib/retry.test.d.ts
DELETED
package/lib/retry.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retry.test.d.ts","sourceRoot":"","sources":["../src/retry.test.ts"],"names":[],"mappings":""}
|
package/lib/retry.test.js
DELETED
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
import * as Clock from "effect/Clock";
|
|
2
|
-
import * as ConfigProvider from "effect/ConfigProvider";
|
|
3
|
-
import * as Duration from "effect/Duration";
|
|
4
|
-
import * as Effect from "effect/Effect";
|
|
5
|
-
import * as Fiber from "effect/Fiber";
|
|
6
|
-
import * as HttpClient from "effect/http/HttpClient";
|
|
7
|
-
import * as HttpClientError from "effect/http/HttpClientError";
|
|
8
|
-
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
9
|
-
import * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
10
|
-
import * as Layer from "effect/Layer";
|
|
11
|
-
import * as Ref from "effect/Ref";
|
|
12
|
-
import * as Schedule from "effect/Schedule";
|
|
13
|
-
import * as Schema from "effect/Schema";
|
|
14
|
-
import * as TestClock from "effect/testing/TestClock";
|
|
15
|
-
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
16
|
-
import * as API from "./api.js";
|
|
17
|
-
import * as Category from "./category.js";
|
|
18
|
-
import { BadGateway, BadRequest, Conflict, Forbidden, GatewayTimeout, InternalServerError, Locked, NotFound, ServiceUnavailable, TooManyRequests, Unauthorized, } from "./errors.js";
|
|
19
|
-
import { makeRestProtocol } from "./protocol-rest.js";
|
|
20
|
-
import * as Retry from "./retry.js";
|
|
21
|
-
import * as S from "./schema.js";
|
|
22
|
-
import * as T from "./trait.js";
|
|
23
|
-
// =============================================================================
|
|
24
|
-
// Fixtures
|
|
25
|
-
// =============================================================================
|
|
26
|
-
class TestRetry extends Retry.makeRetryService("RetryTest/Retry") {
|
|
27
|
-
}
|
|
28
|
-
class OtherRetry extends Retry.makeRetryService("RetryTest/OtherRetry") {
|
|
29
|
-
}
|
|
30
|
-
class UnknownTestError extends Schema.TaggedError()("UnknownTestError", {
|
|
31
|
-
message: Schema.String,
|
|
32
|
-
}) {
|
|
33
|
-
}
|
|
34
|
-
const TestProtocol = makeRestProtocol({
|
|
35
|
-
credentials: Effect.succeed({}),
|
|
36
|
-
baseUrl: () => "https://api.test",
|
|
37
|
-
headers: () => ({}),
|
|
38
|
-
unknownError: ({ message }) => new UnknownTestError({ message }),
|
|
39
|
-
parseError: ({ body }) => new UnknownTestError({ message: String(body) }),
|
|
40
|
-
});
|
|
41
|
-
const Output = S.Struct({ ok: S.optional(S.Boolean) });
|
|
42
|
-
const operation = (method, retry) => API.make(() => ({
|
|
43
|
-
input: S.Struct({}).pipe(T.Http({ method, uri: "/thing" })),
|
|
44
|
-
output: Output,
|
|
45
|
-
protocol: TestProtocol,
|
|
46
|
-
...(retry ? { retry } : {}),
|
|
47
|
-
}));
|
|
48
|
-
const getThing = operation("GET", TestRetry);
|
|
49
|
-
const createThing = operation("POST", TestRetry);
|
|
50
|
-
const getThingWithoutRetry = operation("GET");
|
|
51
|
-
/**
|
|
52
|
-
* Fake HttpClient replying from `script` by attempt number (0-based) and
|
|
53
|
-
* recording the TestClock time of every attempt.
|
|
54
|
-
*/
|
|
55
|
-
const scripted = (script) => {
|
|
56
|
-
const attempts = [];
|
|
57
|
-
const layer = Layer.succeed(HttpClient.HttpClient, HttpClient.make((request) => Effect.gen(function* () {
|
|
58
|
-
const at = yield* Clock.currentTimeMillis;
|
|
59
|
-
const reply = script(attempts.length);
|
|
60
|
-
attempts.push({ at, method: request.method });
|
|
61
|
-
if (reply === "transport") {
|
|
62
|
-
return yield* Effect.fail(new HttpClientError.HttpClientError({
|
|
63
|
-
reason: new HttpClientError.TransportError({ request, description: "reset" }),
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
return HttpClientResponse.fromWeb(request, reply);
|
|
67
|
-
})));
|
|
68
|
-
return { attempts, layer, times: () => attempts.map((a) => a.at) };
|
|
69
|
-
};
|
|
70
|
-
const status = (code, headers = {}) => new Response(JSON.stringify({ message: `status ${code}` }), { status: code, headers });
|
|
71
|
-
const ok = () => new Response(JSON.stringify({ ok: true }), { status: 200 });
|
|
72
|
-
/** Fail `n` times with `reply`, then succeed. */
|
|
73
|
-
const failTimes = (n, reply) => (attempt) => attempt < n ? reply() : ok();
|
|
74
|
-
/**
|
|
75
|
-
* Run `effect` under a TestClock, advancing virtual time far enough for any
|
|
76
|
-
* finite retry sequence in these tests to complete.
|
|
77
|
-
*/
|
|
78
|
-
const runVirtual = (effect, layer) => Effect.runPromise(Effect.gen(function* () {
|
|
79
|
-
const fiber = yield* Effect.forkChild(Effect.result(effect));
|
|
80
|
-
yield* TestClock.adjust(Duration.hours(1));
|
|
81
|
-
return yield* Fiber.join(fiber);
|
|
82
|
-
}).pipe(Effect.provide(Layer.mergeAll(layer, TestClock.layer()))));
|
|
83
|
-
const delays = (times) => times.slice(1).map((t, i) => t - times[i]);
|
|
84
|
-
const withPolicy = (policy) => (effect) => effect.pipe(Effect.provideService(TestRetry, policy));
|
|
85
|
-
const none = { while: () => false };
|
|
86
|
-
beforeEach(() => {
|
|
87
|
-
// Pin jitter (Math.random * 50ms) to zero so delays are exact.
|
|
88
|
-
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
89
|
-
});
|
|
90
|
-
afterEach(() => {
|
|
91
|
-
vi.restoreAllMocks();
|
|
92
|
-
});
|
|
93
|
-
// The default ConfigProvider snapshots process.env on first use, so env
|
|
94
|
-
// overrides are supplied through an explicit provider.
|
|
95
|
-
const env = (vars) => ConfigProvider.layer(ConfigProvider.fromEnv({ env: vars }));
|
|
96
|
-
// =============================================================================
|
|
97
|
-
// Error categories
|
|
98
|
-
// =============================================================================
|
|
99
|
-
const request = HttpClientRequest.get("https://api.test/thing");
|
|
100
|
-
const transportError = new HttpClientError.HttpClientError({
|
|
101
|
-
reason: new HttpClientError.TransportError({ request }),
|
|
102
|
-
});
|
|
103
|
-
const decodeError = new HttpClientError.HttpClientError({
|
|
104
|
-
reason: new HttpClientError.DecodeError({
|
|
105
|
-
request,
|
|
106
|
-
response: HttpClientResponse.fromWeb(request, new Response("x")),
|
|
107
|
-
}),
|
|
108
|
-
});
|
|
109
|
-
class CategorizedNetworkError extends Schema.TaggedError()("CategorizedNetworkError", { message: Schema.String }).pipe(Category.withCategory(Category.NetworkError)) {
|
|
110
|
-
}
|
|
111
|
-
class CategorizedTimeoutError extends Schema.TaggedError()("CategorizedTimeoutError", { message: Schema.String }).pipe(Category.withCategory(Category.TimeoutError)) {
|
|
112
|
-
}
|
|
113
|
-
class RetryableMarked extends Schema.TaggedError()("RetryableMarked", {
|
|
114
|
-
message: Schema.String,
|
|
115
|
-
}).pipe(Category.withRetryable()) {
|
|
116
|
-
}
|
|
117
|
-
const transient = [
|
|
118
|
-
new TooManyRequests({ message: "" }),
|
|
119
|
-
new Locked({ message: "" }),
|
|
120
|
-
new InternalServerError({ message: "" }),
|
|
121
|
-
new BadGateway({ message: "" }),
|
|
122
|
-
new ServiceUnavailable({ message: "" }),
|
|
123
|
-
new GatewayTimeout({ message: "" }),
|
|
124
|
-
new CategorizedNetworkError({ message: "" }),
|
|
125
|
-
new CategorizedTimeoutError({ message: "" }),
|
|
126
|
-
new RetryableMarked({ message: "" }),
|
|
127
|
-
transportError,
|
|
128
|
-
];
|
|
129
|
-
const permanent = [
|
|
130
|
-
new BadRequest({ message: "" }),
|
|
131
|
-
new Unauthorized({ message: "" }),
|
|
132
|
-
new Forbidden({ message: "" }),
|
|
133
|
-
new NotFound({ message: "" }),
|
|
134
|
-
new Conflict({ message: "" }),
|
|
135
|
-
new UnknownTestError({ message: "" }),
|
|
136
|
-
decodeError,
|
|
137
|
-
new Error("plain"),
|
|
138
|
-
undefined,
|
|
139
|
-
];
|
|
140
|
-
const nameOf = (e) => e?._tag === "HttpClientError"
|
|
141
|
-
? `HttpClientError(${e.reason._tag})`
|
|
142
|
-
: (e?._tag ?? String(e));
|
|
143
|
-
describe("retry predicates by error category", () => {
|
|
144
|
-
const lastError = Effect.runSync(Ref.make(undefined));
|
|
145
|
-
const policies = {
|
|
146
|
-
makeDefault: Retry.makeDefault(lastError),
|
|
147
|
-
transientFactory: Retry.transientFactory(lastError),
|
|
148
|
-
transientOptions: Retry.transientOptions,
|
|
149
|
-
throttlingFactory: Retry.throttlingFactory(lastError),
|
|
150
|
-
throttlingOptions: Retry.throttlingOptions,
|
|
151
|
-
};
|
|
152
|
-
for (const error of transient) {
|
|
153
|
-
test(`default and transient policies retry ${nameOf(error)}`, () => {
|
|
154
|
-
expect(policies.makeDefault.while(error)).toBe(true);
|
|
155
|
-
expect(policies.transientFactory.while(error)).toBe(true);
|
|
156
|
-
expect(policies.transientOptions.while(error)).toBe(true);
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
for (const error of permanent) {
|
|
160
|
-
test(`no policy retries ${nameOf(error)}`, () => {
|
|
161
|
-
for (const policy of Object.values(policies)) {
|
|
162
|
-
expect(policy.while(error)).toBe(false);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
test("throttling policies retry only throttling errors", () => {
|
|
167
|
-
for (const policy of [policies.throttlingFactory, policies.throttlingOptions]) {
|
|
168
|
-
expect(policy.while(new TooManyRequests({ message: "" }))).toBe(true);
|
|
169
|
-
for (const error of transient.filter((e) => !(e instanceof TooManyRequests))) {
|
|
170
|
-
expect(policy.while(error)).toBe(false);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
// =============================================================================
|
|
176
|
-
// Default policy
|
|
177
|
-
// =============================================================================
|
|
178
|
-
describe("makeDefault schedule", () => {
|
|
179
|
-
test("backs off exponentially from 250ms, capped at 5s, and gives up after 8 retries", async () => {
|
|
180
|
-
const http = scripted(() => status(503));
|
|
181
|
-
const result = await runVirtual(getThing({}), http.layer);
|
|
182
|
-
expect(result._tag).toBe("Failure");
|
|
183
|
-
expect(result.failure).toBeInstanceOf(ServiceUnavailable);
|
|
184
|
-
expect(http.attempts).toHaveLength(9);
|
|
185
|
-
expect(delays(http.times())).toEqual([250, 500, 1000, 2000, 4000, 5000, 5000, 5000]);
|
|
186
|
-
});
|
|
187
|
-
test("succeeds as soon as a retry succeeds", async () => {
|
|
188
|
-
const http = scripted(failTimes(2, () => status(500)));
|
|
189
|
-
const result = await runVirtual(getThing({}), http.layer);
|
|
190
|
-
expect(result).toMatchObject({ _tag: "Success", success: { ok: true } });
|
|
191
|
-
expect(http.attempts).toHaveLength(3);
|
|
192
|
-
});
|
|
193
|
-
test("throttling errors wait at least 500ms", async () => {
|
|
194
|
-
const http = scripted(failTimes(3, () => status(429)));
|
|
195
|
-
await runVirtual(getThing({}), http.layer);
|
|
196
|
-
expect(delays(http.times())).toEqual([500, 500, 1000]);
|
|
197
|
-
});
|
|
198
|
-
test("retries wire-level transport failures", async () => {
|
|
199
|
-
const http = scripted(failTimes(2, () => "transport"));
|
|
200
|
-
const result = await runVirtual(getThing({}), http.layer);
|
|
201
|
-
expect(result._tag).toBe("Success");
|
|
202
|
-
expect(delays(http.times())).toEqual([250, 500]);
|
|
203
|
-
});
|
|
204
|
-
test("does not retry non-transient failures", async () => {
|
|
205
|
-
for (const code of [400, 401, 403, 404, 409, 422, 402]) {
|
|
206
|
-
const http = scripted(() => status(code));
|
|
207
|
-
const result = await runVirtual(getThing({}), http.layer);
|
|
208
|
-
expect(result._tag).toBe("Failure");
|
|
209
|
-
expect(http.attempts).toHaveLength(1);
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
test("does not distinguish mutations from reads", async () => {
|
|
213
|
-
const reads = scripted(failTimes(2, () => status(503)));
|
|
214
|
-
const writes = scripted(failTimes(2, () => status(503)));
|
|
215
|
-
await runVirtual(getThing({}), reads.layer);
|
|
216
|
-
await runVirtual(createThing({}), writes.layer);
|
|
217
|
-
expect(writes.attempts.map((a) => a.method)).toEqual(["POST", "POST", "POST"]);
|
|
218
|
-
expect(writes.times()).toEqual(reads.times());
|
|
219
|
-
});
|
|
220
|
-
test("jitter adds up to 50ms to each delay", async () => {
|
|
221
|
-
vi.spyOn(Math, "random").mockReturnValue(0.5);
|
|
222
|
-
const http = scripted(failTimes(2, () => status(503)));
|
|
223
|
-
await runVirtual(getThing({}), http.layer);
|
|
224
|
-
expect(delays(http.times())).toEqual([275, 525]);
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
describe("server retry hints", () => {
|
|
228
|
-
test("the default policy honors retryAfter over its own backoff", async () => {
|
|
229
|
-
const http = scripted(failTimes(2, () => status(503, { "retry-after": "2" })));
|
|
230
|
-
await runVirtual(getThing({}), http.layer);
|
|
231
|
-
expect(delays(http.times())).toEqual([2000, 2000]);
|
|
232
|
-
});
|
|
233
|
-
test("a hint longer than the 5s backoff cap is honored in full", async () => {
|
|
234
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "30" })));
|
|
235
|
-
await runVirtual(getThing({}), http.layer);
|
|
236
|
-
expect(delays(http.times())).toEqual([30_000]);
|
|
237
|
-
});
|
|
238
|
-
test("a hint shorter than the throttling floor still wins", async () => {
|
|
239
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "0" })));
|
|
240
|
-
await runVirtual(getThing({}), http.layer);
|
|
241
|
-
expect(delays(http.times())).toEqual([0]);
|
|
242
|
-
});
|
|
243
|
-
test("hints are capped at DEFAULT_SERVER_RETRY_HINT_CAP_MS", async () => {
|
|
244
|
-
expect(Retry.DEFAULT_SERVER_RETRY_HINT_CAP_MS).toBe(60_000);
|
|
245
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
246
|
-
await runVirtual(getThing({}), http.layer);
|
|
247
|
-
expect(delays(http.times())).toEqual([60_000]);
|
|
248
|
-
});
|
|
249
|
-
test("serverRetryHintCapLayer overrides the cap", async () => {
|
|
250
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
251
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, Retry.serverRetryHintCapLayer(1_500)));
|
|
252
|
-
expect(delays(http.times())).toEqual([1_500]);
|
|
253
|
-
});
|
|
254
|
-
test("DISTILLED_SERVER_RETRY_HINT_CAP_MS overrides the cap", async () => {
|
|
255
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
256
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" })));
|
|
257
|
-
expect(delays(http.times())).toEqual([2_500]);
|
|
258
|
-
});
|
|
259
|
-
test("an invalid DISTILLED_SERVER_RETRY_HINT_CAP_MS falls back to the default cap", async () => {
|
|
260
|
-
for (const raw of ["-1", "soon", "Infinity", ""]) {
|
|
261
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
262
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: raw })));
|
|
263
|
-
expect(delays(http.times())).toEqual([60_000]);
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
test("DISTILLED_SERVER_RETRY_HINT_CAP_MS is truncated to whole milliseconds", async () => {
|
|
267
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
268
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "1234.9" })));
|
|
269
|
-
expect(delays(http.times())).toEqual([1_234]);
|
|
270
|
-
});
|
|
271
|
-
test("the layer cap wins over the environment cap", async () => {
|
|
272
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
273
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, Retry.serverRetryHintCapLayer(1_000), env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" })));
|
|
274
|
-
expect(delays(http.times())).toEqual([1_000]);
|
|
275
|
-
});
|
|
276
|
-
test("an invalid layer cap falls back to the environment cap", async () => {
|
|
277
|
-
const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
|
|
278
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, Retry.serverRetryHintCapLayer(-1), env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" })));
|
|
279
|
-
expect(delays(http.times())).toEqual([2_500]);
|
|
280
|
-
});
|
|
281
|
-
test("factory policies honor hints; static Options policies cannot", async () => {
|
|
282
|
-
const factory = scripted(failTimes(1, () => status(429, { "retry-after": "3" })));
|
|
283
|
-
await runVirtual(getThing({}).pipe(withPolicy(Retry.throttlingFactory)), factory.layer);
|
|
284
|
-
expect(delays(factory.times())).toEqual([3_000]);
|
|
285
|
-
const options = scripted(failTimes(1, () => status(429, { "retry-after": "3" })));
|
|
286
|
-
await runVirtual(getThing({}).pipe(withPolicy(Retry.throttlingOptions)), options.layer);
|
|
287
|
-
expect(delays(options.times())).toEqual([1_000]);
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
describe("readServerRetryHintCapMsFromEnv", () => {
|
|
291
|
-
test("returns the default cap when the variable is unset", () => {
|
|
292
|
-
expect(process.env.DISTILLED_SERVER_RETRY_HINT_CAP_MS).toBeUndefined();
|
|
293
|
-
expect(Retry.readServerRetryHintCapMsFromEnv()).toBe(Retry.DEFAULT_SERVER_RETRY_HINT_CAP_MS);
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
// =============================================================================
|
|
297
|
-
// Other built-in policies
|
|
298
|
-
// =============================================================================
|
|
299
|
-
describe("throttling and transient policies", () => {
|
|
300
|
-
test("transient policies back off from 1s, cap at 5s, and keep retrying past 8 attempts", async () => {
|
|
301
|
-
for (const policy of [Retry.transientFactory, Retry.transientOptions]) {
|
|
302
|
-
const http = scripted(failTimes(10, () => status(502)));
|
|
303
|
-
const result = await runVirtual(getThing({}).pipe(withPolicy(policy)), http.layer);
|
|
304
|
-
expect(result._tag).toBe("Success");
|
|
305
|
-
expect(delays(http.times())).toEqual([
|
|
306
|
-
1000, 2000, 4000, 5000, 5000, 5000, 5000, 5000, 5000, 5000,
|
|
307
|
-
]);
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
test("throttling policies do not retry server errors", async () => {
|
|
311
|
-
const http = scripted(() => status(503));
|
|
312
|
-
const result = await runVirtual(getThing({}).pipe(withPolicy(Retry.throttlingFactory)), http.layer);
|
|
313
|
-
expect(result.failure).toBeInstanceOf(ServiceUnavailable);
|
|
314
|
-
expect(http.attempts).toHaveLength(1);
|
|
315
|
-
});
|
|
316
|
-
});
|
|
317
|
-
describe("schedule helpers", () => {
|
|
318
|
-
const delaysOf = async (schedule, failures) => {
|
|
319
|
-
const http = scripted(failTimes(failures, () => status(503)));
|
|
320
|
-
await runVirtual(getThing({}).pipe(withPolicy({ while: () => true, schedule })), http.layer);
|
|
321
|
-
return delays(http.times());
|
|
322
|
-
};
|
|
323
|
-
test("capped clamps each delay to the maximum", async () => {
|
|
324
|
-
expect(await delaysOf(Schedule.exponential(1000, 3).pipe(Retry.capped(Duration.seconds(2))), 3)).toEqual([1000, 2000, 2000]);
|
|
325
|
-
});
|
|
326
|
-
test("jittered adds Math.random() * 50ms", async () => {
|
|
327
|
-
vi.spyOn(Math, "random").mockReturnValue(0.2);
|
|
328
|
-
expect(await delaysOf(Schedule.spaced(100).pipe(Retry.jittered), 2)).toEqual([110, 110]);
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
// =============================================================================
|
|
332
|
-
// Policy resolution from context
|
|
333
|
-
// =============================================================================
|
|
334
|
-
describe("retry policy resolution", () => {
|
|
335
|
-
test("operations without a retry tag never retry", async () => {
|
|
336
|
-
const http = scripted(() => status(503));
|
|
337
|
-
await runVirtual(getThingWithoutRetry({}).pipe(withPolicy(Retry.transientOptions)), http.layer);
|
|
338
|
-
expect(http.attempts).toHaveLength(1);
|
|
339
|
-
});
|
|
340
|
-
test("with no policy in context the default policy applies", async () => {
|
|
341
|
-
const http = scripted(failTimes(1, () => status(503)));
|
|
342
|
-
const result = await runVirtual(getThing({}), http.layer);
|
|
343
|
-
expect(result._tag).toBe("Success");
|
|
344
|
-
expect(http.attempts).toHaveLength(2);
|
|
345
|
-
});
|
|
346
|
-
test("a policy that never retries disables the default", async () => {
|
|
347
|
-
const http = scripted(() => status(503));
|
|
348
|
-
await runVirtual(getThing({}).pipe(withPolicy(none)), http.layer);
|
|
349
|
-
expect(http.attempts).toHaveLength(1);
|
|
350
|
-
});
|
|
351
|
-
test("a policy without `while` disables retrying", async () => {
|
|
352
|
-
const http = scripted(() => status(503));
|
|
353
|
-
await runVirtual(getThing({}).pipe(withPolicy({ schedule: Schedule.recurs(5) })), http.layer);
|
|
354
|
-
expect(http.attempts).toHaveLength(1);
|
|
355
|
-
});
|
|
356
|
-
test("a policy without a schedule retries immediately while its predicate holds", async () => {
|
|
357
|
-
const http = scripted(failTimes(3, () => status(400)));
|
|
358
|
-
const result = await runVirtual(getThing({}).pipe(withPolicy({ while: (e) => e instanceof BadRequest })), http.layer);
|
|
359
|
-
expect(result._tag).toBe("Success");
|
|
360
|
-
expect(http.times()).toEqual([0, 0, 0, 0]);
|
|
361
|
-
});
|
|
362
|
-
test("a custom schedule bounds the attempts", async () => {
|
|
363
|
-
const http = scripted(() => status(503));
|
|
364
|
-
await runVirtual(getThing({}).pipe(withPolicy({ while: () => true, schedule: Schedule.recurs(2) })), http.layer);
|
|
365
|
-
expect(http.attempts).toHaveLength(3);
|
|
366
|
-
});
|
|
367
|
-
test("factory policies get a ref holding the latest failure", async () => {
|
|
368
|
-
const seen = [];
|
|
369
|
-
const factory = (lastError) => ({
|
|
370
|
-
while: () => true,
|
|
371
|
-
schedule: Schedule.recurs(2).pipe(Schedule.tap(() => Effect.flatMap(Ref.get(lastError), (e) => Effect.sync(() => seen.push(e))))),
|
|
372
|
-
});
|
|
373
|
-
let n = 0;
|
|
374
|
-
const http = scripted(() => status([500, 502, 503][n++]));
|
|
375
|
-
const result = await runVirtual(getThing({}).pipe(withPolicy(factory)), http.layer);
|
|
376
|
-
// One schedule step per retry decision, each seeing the failure it retries.
|
|
377
|
-
expect(seen.map((e) => e._tag)).toEqual([
|
|
378
|
-
"InternalServerError",
|
|
379
|
-
"BadGateway",
|
|
380
|
-
]);
|
|
381
|
-
expect(result.failure).toBeInstanceOf(ServiceUnavailable);
|
|
382
|
-
});
|
|
383
|
-
test("an outer policy applies to every call beneath it", async () => {
|
|
384
|
-
const http = scripted(() => status(503));
|
|
385
|
-
await runVirtual(Effect.all([Effect.result(getThing({})), Effect.result(createThing({}))]).pipe(withPolicy(none)), http.layer);
|
|
386
|
-
expect(http.attempts.map((a) => a.method)).toEqual(["GET", "POST"]);
|
|
387
|
-
});
|
|
388
|
-
test("an inner policy overrides the outer one for its own subtree only", async () => {
|
|
389
|
-
const http = scripted(() => status(503));
|
|
390
|
-
const inner = getThing({}).pipe(withPolicy(none));
|
|
391
|
-
const sibling = createThing({});
|
|
392
|
-
await runVirtual(Effect.all([Effect.result(inner), Effect.result(sibling)]).pipe(withPolicy({ while: () => true, schedule: Schedule.recurs(2) })), http.layer);
|
|
393
|
-
expect(http.attempts.map((a) => a.method)).toEqual(["GET", "POST", "POST", "POST"]);
|
|
394
|
-
});
|
|
395
|
-
test("a policy under another SDK's tag does not apply", async () => {
|
|
396
|
-
const http = scripted(failTimes(1, () => status(503)));
|
|
397
|
-
const result = await runVirtual(getThing({}).pipe(Effect.provideService(OtherRetry, none)), http.layer);
|
|
398
|
-
expect(result._tag).toBe("Success");
|
|
399
|
-
expect(http.attempts).toHaveLength(2);
|
|
400
|
-
});
|
|
401
|
-
test("a policy installed as a layer applies like provideService", async () => {
|
|
402
|
-
const http = scripted(() => status(503));
|
|
403
|
-
await runVirtual(getThing({}), Layer.mergeAll(http.layer, Layer.succeed(TestRetry, none)));
|
|
404
|
-
expect(http.attempts).toHaveLength(1);
|
|
405
|
-
});
|
|
406
|
-
});
|
|
407
|
-
//# sourceMappingURL=retry.test.js.map
|