@distilled.cloud/acme 1.0.0-rc.13 → 1.0.0-rc.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/lib/credentials.d.ts.map +1 -1
- package/lib/credentials.js.map +1 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/errors.js.map +1 -1
- package/lib/index.d.ts +4 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/jose.d.ts.map +1 -1
- package/lib/jose.js +4 -11
- package/lib/jose.js.map +1 -1
- package/lib/protocol.d.ts +4 -4
- package/lib/protocol.d.ts.map +1 -1
- package/lib/protocol.js +12 -18
- package/lib/protocol.js.map +1 -1
- package/lib/protocol.test.d.ts +2 -0
- package/lib/protocol.test.d.ts.map +1 -0
- package/lib/protocol.test.js +533 -0
- package/lib/protocol.test.js.map +1 -0
- package/lib/retry.d.ts +1 -1
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -1
- package/lib/retry.js.map +1 -1
- package/lib/services/acme.d.ts +83 -20
- package/lib/services/acme.d.ts.map +1 -1
- package/lib/services/acme.js +72 -130
- package/lib/services/acme.js.map +1 -1
- package/package.json +15 -10
- package/src/credentials.ts +13 -19
- package/src/errors.ts +15 -25
- package/src/index.ts +4 -8
- package/src/jose.ts +14 -41
- package/src/protocol.test.ts +806 -0
- package/src/protocol.ts +32 -81
- package/src/retry.ts +2 -8
- package/src/services/acme.ts +178 -222
- package/lib/response-validation.test.d.ts +0 -4
- package/lib/response-validation.test.d.ts.map +0 -1
- package/lib/response-validation.test.js +0 -51
- package/lib/response-validation.test.js.map +0 -1
- package/src/response-validation.test.ts +0 -65
package/src/protocol.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import * as API from "@distilled.cloud/core/api";
|
|
2
|
+
import {
|
|
3
|
+
HTTP_STATUS_MAP,
|
|
4
|
+
InternalServerError,
|
|
5
|
+
type ConfigError,
|
|
6
|
+
} from "@distilled.cloud/core/errors";
|
|
7
|
+
import { getAnn, getProps, hasPropAnn, mapKeys } from "@distilled.cloud/core/protocol-http";
|
|
8
|
+
import { unwrapRedactedDeep } from "@distilled.cloud/core/protocol-rest";
|
|
1
9
|
/**
|
|
2
10
|
* AcmeProtocol — hand-written.
|
|
3
11
|
*
|
|
@@ -24,42 +32,19 @@
|
|
|
24
32
|
* RFC 7807 problem documents matched on their `type` URN against the
|
|
25
33
|
* operation's typed error classes; an unmatched URN is `UnknownAcmeError`.
|
|
26
34
|
*/
|
|
27
|
-
import {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} from "@distilled.cloud/core/response-validation";
|
|
35
|
+
import { isStrict, validateResponse } from "@distilled.cloud/core/response-validation";
|
|
36
|
+
import { parseRetryAfter, parseRetryAfterForStatus } from "@distilled.cloud/core/retry-after";
|
|
37
|
+
import { getErrorMatchers, httpSymbol, labelSymbol } from "@distilled.cloud/core/trait";
|
|
31
38
|
import * as Effect from "effect/Effect";
|
|
32
|
-
import * as Layer from "effect/Layer";
|
|
33
|
-
import * as Redacted from "effect/Redacted";
|
|
34
|
-
import * as Schema from "effect/Schema";
|
|
35
39
|
import type * as HttpBody from "effect/http/HttpBody";
|
|
36
|
-
import type * as AST from "effect/SchemaAST";
|
|
37
40
|
import * as HttpClient from "effect/http/HttpClient";
|
|
38
41
|
import type * as HttpClientError from "effect/http/HttpClientError";
|
|
39
42
|
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
40
43
|
import type * as HttpClientResponse from "effect/http/HttpClientResponse";
|
|
41
|
-
import * as
|
|
42
|
-
import
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
hasPropAnn,
|
|
46
|
-
mapKeys,
|
|
47
|
-
} from "@distilled.cloud/core/protocol-http";
|
|
48
|
-
import { unwrapRedactedDeep } from "@distilled.cloud/core/protocol-rest";
|
|
49
|
-
import {
|
|
50
|
-
getErrorMatchers,
|
|
51
|
-
httpSymbol,
|
|
52
|
-
labelSymbol,
|
|
53
|
-
} from "@distilled.cloud/core/trait";
|
|
54
|
-
import {
|
|
55
|
-
HTTP_STATUS_MAP,
|
|
56
|
-
InternalServerError,
|
|
57
|
-
type ConfigError,
|
|
58
|
-
} from "@distilled.cloud/core/errors";
|
|
59
|
-
import {
|
|
60
|
-
parseRetryAfter,
|
|
61
|
-
parseRetryAfterForStatus,
|
|
62
|
-
} from "@distilled.cloud/core/retry-after";
|
|
44
|
+
import * as Layer from "effect/Layer";
|
|
45
|
+
import * as Redacted from "effect/Redacted";
|
|
46
|
+
import * as Schema from "effect/Schema";
|
|
47
|
+
import type * as AST from "effect/SchemaAST";
|
|
63
48
|
import { Credentials, type Config } from "./credentials.ts";
|
|
64
49
|
import {
|
|
65
50
|
AcmeParseError,
|
|
@@ -68,12 +53,7 @@ import {
|
|
|
68
53
|
UnknownAcmeError,
|
|
69
54
|
type DefaultErrors,
|
|
70
55
|
} from "./errors.ts";
|
|
71
|
-
import {
|
|
72
|
-
parseJwk,
|
|
73
|
-
signExternalAccountBinding,
|
|
74
|
-
signRequest,
|
|
75
|
-
type SignOptions,
|
|
76
|
-
} from "./jose.ts";
|
|
56
|
+
import { parseJwk, signExternalAccountBinding, signRequest, type SignOptions } from "./jose.ts";
|
|
77
57
|
|
|
78
58
|
/**
|
|
79
59
|
* Error channel shared by every generated ACME operation. Generated service
|
|
@@ -81,10 +61,7 @@ import {
|
|
|
81
61
|
* AcmeOpContext>` explicitly so the compiler never infers these back out of
|
|
82
62
|
* the schema generics.
|
|
83
63
|
*/
|
|
84
|
-
export type AcmeOpError =
|
|
85
|
-
| DefaultErrors
|
|
86
|
-
| ConfigError
|
|
87
|
-
| HttpClientError.HttpClientError;
|
|
64
|
+
export type AcmeOpError = DefaultErrors | ConfigError | HttpClientError.HttpClientError;
|
|
88
65
|
|
|
89
66
|
/** Context (requirements) shared by every generated ACME operation. */
|
|
90
67
|
export type AcmeOpContext = Credentials | HttpClient.HttpClient;
|
|
@@ -138,9 +115,7 @@ const fetchDirectory = (directoryUrl: string) =>
|
|
|
138
115
|
}
|
|
139
116
|
const text = yield* response.text;
|
|
140
117
|
const json = yield* parseJson(text);
|
|
141
|
-
const directory = yield* Schema.decodeUnknownEffect(DirectorySchema)(
|
|
142
|
-
json,
|
|
143
|
-
).pipe(
|
|
118
|
+
const directory = yield* Schema.decodeUnknownEffect(DirectorySchema)(json).pipe(
|
|
144
119
|
Effect.mapError(() => parseError("Invalid ACME directory response")),
|
|
145
120
|
);
|
|
146
121
|
directories.set(directoryUrl, directory);
|
|
@@ -166,9 +141,7 @@ const takeNonce = (directoryUrl: string, directory: Directory) =>
|
|
|
166
141
|
return cached;
|
|
167
142
|
}
|
|
168
143
|
const client = yield* HttpClient.HttpClient;
|
|
169
|
-
const response = yield* client.execute(
|
|
170
|
-
HttpClientRequest.head(directory.newNonce),
|
|
171
|
-
);
|
|
144
|
+
const response = yield* client.execute(HttpClientRequest.head(directory.newNonce));
|
|
172
145
|
const nonce = response.headers["replay-nonce"];
|
|
173
146
|
if (typeof nonce !== "string" || nonce.length === 0) {
|
|
174
147
|
return yield* fail(
|
|
@@ -220,11 +193,7 @@ const DIRECTORY_OPERATIONS: Record<string, keyof Directory> = {
|
|
|
220
193
|
};
|
|
221
194
|
|
|
222
195
|
/** Operations whose payload is the empty POST-as-GET. */
|
|
223
|
-
const POST_AS_GET = new Set([
|
|
224
|
-
"GetOrder",
|
|
225
|
-
"GetAuthorization",
|
|
226
|
-
"DownloadCertificate",
|
|
227
|
-
]);
|
|
196
|
+
const POST_AS_GET = new Set(["GetOrder", "GetAuthorization", "DownloadCertificate"]);
|
|
228
197
|
|
|
229
198
|
const JOSE_ACCEPT = "application/json, application/pem-certificate-chain";
|
|
230
199
|
|
|
@@ -261,10 +230,7 @@ const encode = ({
|
|
|
261
230
|
|
|
262
231
|
// Resolve the request URL: from the directory for the well-known
|
|
263
232
|
// resources, else the absolute `url` label the CA handed back.
|
|
264
|
-
const inputObj = (unwrapRedactedDeep(input) ?? {}) as Record<
|
|
265
|
-
string,
|
|
266
|
-
unknown
|
|
267
|
-
>;
|
|
233
|
+
const inputObj = (unwrapRedactedDeep(input) ?? {}) as Record<string, unknown>;
|
|
268
234
|
const directoryKey = DIRECTORY_OPERATIONS[operation];
|
|
269
235
|
let url: string;
|
|
270
236
|
if (directoryKey !== undefined) {
|
|
@@ -308,8 +274,7 @@ const encode = ({
|
|
|
308
274
|
// `newAccount` proves the key by embedding it; a CA that requires an
|
|
309
275
|
// External Account Binding gets it from the credentials unless the
|
|
310
276
|
// caller built one.
|
|
311
|
-
const embedKey =
|
|
312
|
-
operation === "NewAccount" || creds.accountUrl === undefined;
|
|
277
|
+
const embedKey = operation === "NewAccount" || creds.accountUrl === undefined;
|
|
313
278
|
if (
|
|
314
279
|
operation === "NewAccount" &&
|
|
315
280
|
creds.externalAccountBinding !== undefined &&
|
|
@@ -383,12 +348,10 @@ const matchProblem = (
|
|
|
383
348
|
let hit = false;
|
|
384
349
|
if (rule === undefined) hit = true;
|
|
385
350
|
else if (typeof rule === "string") hit = rule === urn;
|
|
386
|
-
else if (rule.matches !== undefined)
|
|
387
|
-
hit = new RegExp(rule.matches).test(urn);
|
|
351
|
+
else if (rule.matches !== undefined) hit = new RegExp(rule.matches).test(urn);
|
|
388
352
|
else if (rule.includes !== undefined) hit = urn.includes(rule.includes);
|
|
389
353
|
if (!hit) continue;
|
|
390
|
-
const specificity =
|
|
391
|
-
(m.status !== undefined ? 1 : 0) + (rule !== undefined ? 2 : 0);
|
|
354
|
+
const specificity = (m.status !== undefined ? 1 : 0) + (rule !== undefined ? 2 : 0);
|
|
392
355
|
if (!best || specificity > best.specificity) best = { cls, specificity };
|
|
393
356
|
}
|
|
394
357
|
}
|
|
@@ -428,16 +391,13 @@ const decode = ({
|
|
|
428
391
|
Effect.gen(function* () {
|
|
429
392
|
const signing = signedRequests.get(response.request.body);
|
|
430
393
|
signedRequests.delete(response.request.body);
|
|
431
|
-
const directoryUrl =
|
|
432
|
-
signing?.directoryUrl ?? (yield* resolveCredentials).directoryUrl;
|
|
394
|
+
const directoryUrl = signing?.directoryUrl ?? (yield* resolveCredentials).directoryUrl;
|
|
433
395
|
|
|
434
396
|
for (let retries = 0; ; retries++) {
|
|
435
397
|
const headers = response.headers as Record<string, string | undefined>;
|
|
436
398
|
const status = response.status;
|
|
437
399
|
const isNewNonce = response.request.method === "HEAD";
|
|
438
|
-
const isCertificate = (headers["content-type"] ?? "").includes(
|
|
439
|
-
"pem-certificate-chain",
|
|
440
|
-
);
|
|
400
|
+
const isCertificate = (headers["content-type"] ?? "").includes("pem-certificate-chain");
|
|
441
401
|
const text = isNewNonce ? "" : yield* response.text;
|
|
442
402
|
const json =
|
|
443
403
|
isNewNonce || (isCertificate && status < 400)
|
|
@@ -449,9 +409,7 @@ const decode = ({
|
|
|
449
409
|
status >= 400
|
|
450
410
|
? Effect.succeed(undefined)
|
|
451
411
|
: Effect.flatMap(isStrict, (strict) =>
|
|
452
|
-
strict
|
|
453
|
-
? Effect.fail(error)
|
|
454
|
-
: Effect.succeed<unknown>(text),
|
|
412
|
+
strict ? Effect.fail(error) : Effect.succeed<unknown>(text),
|
|
455
413
|
),
|
|
456
414
|
),
|
|
457
415
|
);
|
|
@@ -462,9 +420,7 @@ const decode = ({
|
|
|
462
420
|
// Rejection nonces belong only to this request, never to the shared cache.
|
|
463
421
|
if (signing && nonce && retries < 2) {
|
|
464
422
|
const client = yield* HttpClient.HttpClient;
|
|
465
|
-
response = yield* client.execute(
|
|
466
|
-
yield* signedRequest(signing, nonce),
|
|
467
|
-
);
|
|
423
|
+
response = yield* client.execute(yield* signedRequest(signing, nonce));
|
|
468
424
|
continue;
|
|
469
425
|
}
|
|
470
426
|
} else {
|
|
@@ -487,9 +443,7 @@ const decode = ({
|
|
|
487
443
|
);
|
|
488
444
|
}
|
|
489
445
|
const message = `HTTP ${status}`;
|
|
490
|
-
const StatusClass = (HTTP_STATUS_MAP as Record<number, unknown>)[
|
|
491
|
-
status
|
|
492
|
-
] as
|
|
446
|
+
const StatusClass = (HTTP_STATUS_MAP as Record<number, unknown>)[status] as
|
|
493
447
|
| (new (args: {
|
|
494
448
|
message: string;
|
|
495
449
|
retryAfter?: ReturnType<typeof parseRetryAfterForStatus>;
|
|
@@ -538,10 +492,8 @@ const decode = ({
|
|
|
538
492
|
return yield* fail(parseError("Expected a JSON object"));
|
|
539
493
|
}
|
|
540
494
|
// Strict mode (core/response-validation) checks the output schema.
|
|
541
|
-
return yield* validateResponse(
|
|
542
|
-
|
|
543
|
-
mapKeys(outputAst, body, "decode"),
|
|
544
|
-
() => parseError("Response does not match the output schema"),
|
|
495
|
+
return yield* validateResponse(outputAst, mapKeys(outputAst, body, "decode"), () =>
|
|
496
|
+
parseError("Response does not match the output schema"),
|
|
545
497
|
);
|
|
546
498
|
}
|
|
547
499
|
});
|
|
@@ -566,8 +518,7 @@ const fail = <E>(error: E) => Effect.fail(error) as Effect.Effect<never, E>;
|
|
|
566
518
|
export const AcmeProtocol: Layer.Layer<API.Protocol> = Layer.succeed(
|
|
567
519
|
API.Protocol,
|
|
568
520
|
API.Protocol.of({
|
|
569
|
-
encode: (args) =>
|
|
570
|
-
encode(args) as Effect.Effect<HttpClientRequest.HttpClientRequest>,
|
|
521
|
+
encode: (args) => encode(args) as Effect.Effect<HttpClientRequest.HttpClientRequest>,
|
|
571
522
|
decode: (args) => decode(args) as Effect.Effect<unknown>,
|
|
572
523
|
}),
|
|
573
524
|
);
|
package/src/retry.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Policy, throttlingFactory, transientFactory } from "@distilled.cloud/core/retry";
|
|
1
2
|
/**
|
|
2
3
|
* ACME retry configuration.
|
|
3
4
|
*
|
|
@@ -17,11 +18,6 @@
|
|
|
17
18
|
import * as Context from "effect/Context";
|
|
18
19
|
import * as Effect from "effect/Effect";
|
|
19
20
|
import * as Layer from "effect/Layer";
|
|
20
|
-
import {
|
|
21
|
-
type Policy,
|
|
22
|
-
throttlingFactory,
|
|
23
|
-
transientFactory,
|
|
24
|
-
} from "@distilled.cloud/core/retry";
|
|
25
21
|
|
|
26
22
|
export {
|
|
27
23
|
type Options,
|
|
@@ -44,9 +40,7 @@ export const policy = (optionsOrFactory: Policy) =>
|
|
|
44
40
|
Effect.provide(Layer.succeed(Retry, optionsOrFactory));
|
|
45
41
|
|
|
46
42
|
/** Disables all automatic retries. */
|
|
47
|
-
export const none = Effect.provide(
|
|
48
|
-
Layer.succeed(Retry, { while: () => false }),
|
|
49
|
-
);
|
|
43
|
+
export const none = Effect.provide(Layer.succeed(Retry, { while: () => false }));
|
|
50
44
|
|
|
51
45
|
/** Apply the throttling retry policy (retries throttling errors indefinitely). */
|
|
52
46
|
export const throttling = policy(throttlingFactory);
|