@apifuse/provider-sdk 2.2.0-beta.60 → 2.2.0-beta.61

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.
Files changed (73) hide show
  1. package/AUTHORING.md +17 -16
  2. package/CHANGELOG.md +4 -0
  3. package/bin/apifuse-dev.ts +5 -7
  4. package/bin/apifuse-pack-types.ts +22 -0
  5. package/bin/apifuse-perf.ts +4 -6
  6. package/bin/apifuse-record.ts +4 -7
  7. package/bin/apifuse-submit-check.ts +2 -2
  8. package/dist/define.d.ts +3 -2
  9. package/dist/define.js +1 -1
  10. package/dist/dev.js +3 -1
  11. package/dist/engine.d.ts +7 -6
  12. package/dist/engine.js +3 -4
  13. package/dist/error-resolution.js +21 -4
  14. package/dist/errors.d.ts +3 -0
  15. package/dist/errors.js +9 -0
  16. package/dist/handle-meta.d.ts +39 -0
  17. package/dist/handle-meta.js +29 -0
  18. package/dist/handle.d.ts +187 -0
  19. package/dist/handle.js +254 -0
  20. package/dist/i18n/catalog.js +21 -1
  21. package/dist/index.d.ts +4 -3
  22. package/dist/index.js +3 -2
  23. package/dist/lint.d.ts +3 -0
  24. package/dist/lint.js +352 -47
  25. package/dist/provider.d.ts +3 -3
  26. package/dist/provider.js +2 -2
  27. package/dist/runtime/handle-wordlist.d.ts +43 -0
  28. package/dist/runtime/{choice-wordlist.js → handle-wordlist.js} +127 -9
  29. package/dist/runtime/handle.d.ts +71 -0
  30. package/dist/runtime/handle.js +895 -0
  31. package/dist/runtime/native-network.d.ts +10 -1
  32. package/dist/runtime/native-network.js +230 -29
  33. package/dist/runtime/native-telemetry.d.ts +142 -0
  34. package/dist/runtime/native-telemetry.js +231 -0
  35. package/dist/runtime/state.d.ts +4 -1
  36. package/dist/runtime/state.js +32 -17
  37. package/dist/server/index.d.ts +1 -0
  38. package/dist/server/index.js +1 -0
  39. package/dist/server/sensitive-values.d.ts +1 -1
  40. package/dist/server/sensitive-values.js +0 -3
  41. package/dist/server/serve-implementation.d.ts +4 -11
  42. package/dist/server/serve-implementation.js +86 -18
  43. package/dist/testing/run.js +10 -7
  44. package/dist/types.d.ts +4 -107
  45. package/package.json +1 -1
  46. package/src/define.ts +4 -3
  47. package/src/dev.ts +3 -1
  48. package/src/engine.ts +5 -6
  49. package/src/error-resolution.ts +21 -4
  50. package/src/errors.ts +13 -0
  51. package/src/handle-meta.ts +58 -0
  52. package/src/handle.ts +529 -0
  53. package/src/i18n/catalog.ts +23 -1
  54. package/src/index.ts +26 -13
  55. package/src/lint.ts +414 -55
  56. package/src/provider.ts +9 -20
  57. package/src/runtime/{choice-wordlist.ts → handle-wordlist.ts} +130 -9
  58. package/src/runtime/handle.ts +1162 -0
  59. package/src/runtime/native-network.ts +281 -33
  60. package/src/runtime/native-telemetry.ts +366 -0
  61. package/src/runtime/state.ts +44 -16
  62. package/src/server/index.ts +17 -0
  63. package/src/server/sensitive-values.ts +0 -3
  64. package/src/server/serve-implementation.ts +87 -32
  65. package/src/testing/run.ts +11 -7
  66. package/src/types.ts +4 -137
  67. package/dist/choice-token.d.ts +0 -24
  68. package/dist/choice-token.js +0 -74
  69. package/dist/runtime/choice-wordlist.d.ts +0 -9
  70. package/dist/runtime/choice.d.ts +0 -27
  71. package/dist/runtime/choice.js +0 -801
  72. package/src/choice-token.ts +0 -165
  73. package/src/runtime/choice.ts +0 -1197
@@ -1,165 +0,0 @@
1
- import {
2
- createCipheriv,
3
- createDecipheriv,
4
- createHash,
5
- createHmac,
6
- randomBytes,
7
- timingSafeEqual,
8
- } from "node:crypto";
9
-
10
- export type ProviderChoiceTokenPayload = Record<string, unknown>;
11
-
12
- export type ProviderChoiceTokenErrorReason =
13
- | "invalid_shape"
14
- | "invalid_signature"
15
- | "invalid_payload"
16
- | "invalid_binding"
17
- | "stale";
18
-
19
- export class ProviderChoiceTokenError extends Error {
20
- readonly reason: ProviderChoiceTokenErrorReason;
21
-
22
- constructor(reason: ProviderChoiceTokenErrorReason, message: string) {
23
- super(message);
24
- this.name = "ProviderChoiceTokenError";
25
- this.reason = reason;
26
- }
27
- }
28
-
29
- export interface CreateProviderChoiceTokenOptions<
30
- TPayload extends ProviderChoiceTokenPayload,
31
- > {
32
- prefix: string;
33
- payload: TPayload;
34
- secret: string;
35
- }
36
-
37
- export interface ParseProviderChoiceTokenOptions {
38
- token: string;
39
- prefix: string;
40
- secret: string;
41
- }
42
-
43
- export interface FreshProviderChoiceIssuedAtOptions {
44
- ttlMs: number;
45
- nowMs?: number;
46
- futureToleranceMs?: number;
47
- }
48
-
49
- export function createProviderChoiceToken<
50
- TPayload extends ProviderChoiceTokenPayload,
51
- >(options: CreateProviderChoiceTokenOptions<TPayload>): string {
52
- const iv = randomBytes(12);
53
- const cipher = createCipheriv(
54
- "aes-256-gcm",
55
- choiceEncryptionKey(options.secret),
56
- iv,
57
- );
58
- const encryptedPayload = Buffer.concat([
59
- cipher.update(JSON.stringify(options.payload), "utf8"),
60
- cipher.final(),
61
- ]).toString("base64url");
62
- const authTag = cipher.getAuthTag().toString("base64url");
63
- const encodedIv = iv.toString("base64url");
64
- const signature = signProviderChoiceTokenBody(
65
- `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}`,
66
- options.secret,
67
- );
68
- return `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}.${signature}`;
69
- }
70
-
71
- export function parseProviderChoiceToken(
72
- options: ParseProviderChoiceTokenOptions,
73
- ): ProviderChoiceTokenPayload {
74
- const [
75
- actualPrefix,
76
- encodedIv,
77
- encryptedPayload,
78
- authTag,
79
- signature,
80
- ...extra
81
- ] = options.token.split(".");
82
- if (
83
- actualPrefix !== options.prefix ||
84
- !encodedIv ||
85
- !encryptedPayload ||
86
- !authTag ||
87
- !signature ||
88
- extra.length > 0
89
- ) {
90
- throw new ProviderChoiceTokenError(
91
- "invalid_shape",
92
- "Provider choice token shape is invalid.",
93
- );
94
- }
95
-
96
- const signedBody = `${options.prefix}.${encodedIv}.${encryptedPayload}.${authTag}`;
97
- const expectedSignature = signProviderChoiceTokenBody(
98
- signedBody,
99
- options.secret,
100
- );
101
- const actualBuffer = Buffer.from(signature);
102
- const expectedBuffer = Buffer.from(expectedSignature);
103
- if (
104
- actualBuffer.length !== expectedBuffer.length ||
105
- !timingSafeEqual(actualBuffer, expectedBuffer)
106
- ) {
107
- throw new ProviderChoiceTokenError(
108
- "invalid_signature",
109
- "Provider choice token signature is invalid.",
110
- );
111
- }
112
-
113
- try {
114
- const decipher = createDecipheriv(
115
- "aes-256-gcm",
116
- choiceEncryptionKey(options.secret),
117
- Buffer.from(encodedIv, "base64url"),
118
- );
119
- decipher.setAuthTag(Buffer.from(authTag, "base64url"));
120
- const decrypted = Buffer.concat([
121
- decipher.update(Buffer.from(encryptedPayload, "base64url")),
122
- decipher.final(),
123
- ]).toString("utf8");
124
- const parsed = JSON.parse(decrypted);
125
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
126
- throw new Error("payload is not an object");
127
- }
128
- return Object.fromEntries(Object.entries(parsed));
129
- } catch {
130
- throw new ProviderChoiceTokenError(
131
- "invalid_payload",
132
- "Provider choice token payload is invalid.",
133
- );
134
- }
135
- }
136
-
137
- export function assertFreshProviderChoiceIssuedAt(
138
- issuedAtMs: unknown,
139
- options: FreshProviderChoiceIssuedAtOptions,
140
- ): number {
141
- const parsed =
142
- typeof issuedAtMs === "number" ? issuedAtMs : Number(issuedAtMs);
143
- const nowMs = options.nowMs ?? Date.now();
144
- const futureToleranceMs = options.futureToleranceMs ?? 30_000;
145
- if (
146
- !Number.isFinite(parsed) ||
147
- parsed <= 0 ||
148
- nowMs - parsed > options.ttlMs ||
149
- parsed - nowMs > futureToleranceMs
150
- ) {
151
- throw new ProviderChoiceTokenError(
152
- "stale",
153
- "Provider choice token is stale.",
154
- );
155
- }
156
- return parsed;
157
- }
158
-
159
- function choiceEncryptionKey(secret: string): Buffer {
160
- return createHash("sha256").update(secret).digest();
161
- }
162
-
163
- function signProviderChoiceTokenBody(body: string, secret: string): string {
164
- return createHmac("sha256", secret).update(body).digest("base64url");
165
- }