@apifuse/provider-sdk 2.1.0-beta.1 → 2.1.0-beta.10

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 (212) hide show
  1. package/AUTHORING.md +208 -2
  2. package/CHANGELOG.md +47 -0
  3. package/README.md +114 -10
  4. package/SUBMISSION.md +87 -0
  5. package/bin/apifuse-check.ts +86 -4
  6. package/bin/apifuse-dev.ts +87 -13
  7. package/bin/apifuse-pack-check.ts +80 -0
  8. package/bin/apifuse-pack-smoke.ts +303 -2
  9. package/bin/apifuse-perf.ts +142 -49
  10. package/bin/apifuse-record.ts +182 -104
  11. package/bin/apifuse-submit-check.ts +2538 -0
  12. package/bin/apifuse.ts +1 -1
  13. package/dist/ceremonies/index.d.ts +41 -0
  14. package/dist/ceremonies/index.js +490 -0
  15. package/dist/choice-token.d.ts +24 -0
  16. package/dist/choice-token.js +74 -0
  17. package/dist/cli/commands.d.ts +10 -0
  18. package/dist/cli/commands.js +80 -0
  19. package/dist/cli/create.d.ts +47 -0
  20. package/dist/cli/create.js +762 -0
  21. package/dist/cli/templates/provider/.dockerignore.tpl +22 -0
  22. package/dist/cli/templates/provider/.gitignore.tpl +22 -0
  23. package/dist/cli/templates/provider/Dockerfile.tpl +7 -0
  24. package/dist/cli/templates/provider/README.md.tpl +160 -0
  25. package/dist/cli/templates/provider/dev.ts.tpl +5 -0
  26. package/dist/cli/templates/provider/domain/README.md.tpl +3 -0
  27. package/dist/cli/templates/provider/index.test.ts.tpl +13 -0
  28. package/dist/cli/templates/provider/index.ts.tpl +15 -0
  29. package/dist/cli/templates/provider/mappers/README.md.tpl +3 -0
  30. package/dist/cli/templates/provider/meta.ts.tpl +7 -0
  31. package/dist/cli/templates/provider/operations/index.ts.tpl +5 -0
  32. package/dist/cli/templates/provider/operations/ping.ts.tpl +24 -0
  33. package/dist/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  34. package/dist/cli/templates/provider/start.ts.tpl +5 -0
  35. package/dist/cli/templates/provider/upstream/README.md.tpl +3 -0
  36. package/dist/config/loader.d.ts +107 -0
  37. package/dist/config/loader.js +935 -0
  38. package/dist/contract-json.d.ts +9 -0
  39. package/dist/contract-json.js +51 -0
  40. package/dist/contract-serialization.d.ts +4 -0
  41. package/dist/contract-serialization.js +78 -0
  42. package/dist/contract-types.d.ts +49 -0
  43. package/dist/contract-types.js +1 -0
  44. package/dist/contract.d.ts +6 -0
  45. package/dist/contract.js +155 -0
  46. package/dist/define.d.ts +97 -0
  47. package/dist/define.js +1320 -0
  48. package/dist/dev.d.ts +9 -0
  49. package/dist/dev.js +15 -0
  50. package/dist/errors.d.ts +59 -0
  51. package/dist/errors.js +97 -0
  52. package/dist/i18n/catalog.d.ts +29 -0
  53. package/dist/i18n/catalog.js +159 -0
  54. package/dist/i18n/index.d.ts +2 -0
  55. package/dist/i18n/index.js +2 -0
  56. package/dist/i18n/keys.d.ts +10 -0
  57. package/dist/i18n/keys.js +34 -0
  58. package/dist/index.d.ts +41 -0
  59. package/dist/index.js +37 -0
  60. package/dist/lint.d.ts +73 -0
  61. package/dist/lint.js +702 -0
  62. package/dist/observability.d.ts +5 -0
  63. package/dist/observability.js +39 -0
  64. package/dist/provider.d.ts +9 -0
  65. package/dist/provider.js +8 -0
  66. package/dist/public-schema-field-lint.d.ts +2 -0
  67. package/dist/public-schema-field-lint.js +158 -0
  68. package/dist/recipes/gov-api.d.ts +19 -0
  69. package/dist/recipes/gov-api.js +72 -0
  70. package/dist/recipes/rest-api.d.ts +21 -0
  71. package/dist/recipes/rest-api.js +115 -0
  72. package/dist/runtime/auth-flow.d.ts +14 -0
  73. package/dist/runtime/auth-flow.js +44 -0
  74. package/dist/runtime/browser.d.ts +25 -0
  75. package/dist/runtime/browser.js +1034 -0
  76. package/dist/runtime/cache.d.ts +10 -0
  77. package/dist/runtime/cache.js +372 -0
  78. package/dist/runtime/choice.d.ts +15 -0
  79. package/dist/runtime/choice.js +435 -0
  80. package/dist/runtime/credential.d.ts +8 -0
  81. package/dist/runtime/credential.js +61 -0
  82. package/dist/runtime/env.d.ts +2 -0
  83. package/dist/runtime/env.js +10 -0
  84. package/dist/runtime/executor.d.ts +16 -0
  85. package/dist/runtime/executor.js +51 -0
  86. package/dist/runtime/http.d.ts +8 -0
  87. package/dist/runtime/http.js +706 -0
  88. package/dist/runtime/insights.d.ts +9 -0
  89. package/dist/runtime/insights.js +324 -0
  90. package/dist/runtime/instrumentation.d.ts +8 -0
  91. package/dist/runtime/instrumentation.js +269 -0
  92. package/dist/runtime/key-derivation.d.ts +24 -0
  93. package/dist/runtime/key-derivation.js +73 -0
  94. package/dist/runtime/keyring.d.ts +25 -0
  95. package/dist/runtime/keyring.js +93 -0
  96. package/dist/runtime/namespace.d.ts +9 -0
  97. package/dist/runtime/namespace.js +19 -0
  98. package/dist/runtime/otlp.d.ts +39 -0
  99. package/dist/runtime/otlp.js +103 -0
  100. package/dist/runtime/perf.d.ts +12 -0
  101. package/dist/runtime/perf.js +52 -0
  102. package/dist/runtime/prevalidate.d.ts +12 -0
  103. package/dist/runtime/prevalidate.js +173 -0
  104. package/dist/runtime/provider.d.ts +2 -0
  105. package/dist/runtime/provider.js +11 -0
  106. package/dist/runtime/proxy-errors.d.ts +21 -0
  107. package/dist/runtime/proxy-errors.js +83 -0
  108. package/dist/runtime/proxy-telemetry.d.ts +8 -0
  109. package/dist/runtime/proxy-telemetry.js +174 -0
  110. package/dist/runtime/redis.d.ts +17 -0
  111. package/dist/runtime/redis.js +82 -0
  112. package/dist/runtime/request-options.d.ts +3 -0
  113. package/dist/runtime/request-options.js +42 -0
  114. package/dist/runtime/state.d.ts +17 -0
  115. package/dist/runtime/state.js +344 -0
  116. package/dist/runtime/stealth.d.ts +18 -0
  117. package/dist/runtime/stealth.js +834 -0
  118. package/dist/runtime/stt.d.ts +22 -0
  119. package/dist/runtime/stt.js +480 -0
  120. package/dist/runtime/trace.d.ts +26 -0
  121. package/dist/runtime/trace.js +142 -0
  122. package/dist/runtime/waterfall.d.ts +12 -0
  123. package/dist/runtime/waterfall.js +147 -0
  124. package/dist/schema.d.ts +74 -0
  125. package/dist/schema.js +243 -0
  126. package/dist/serve.d.ts +1 -0
  127. package/dist/serve.js +1 -0
  128. package/dist/server/index.d.ts +3 -0
  129. package/dist/server/index.js +2 -0
  130. package/dist/server/serve.d.ts +64 -0
  131. package/dist/server/serve.js +1110 -0
  132. package/dist/server/types.d.ts +136 -0
  133. package/dist/server/types.js +86 -0
  134. package/dist/stealth/profiles.d.ts +4 -0
  135. package/dist/stealth/profiles.js +259 -0
  136. package/dist/stream.d.ts +44 -0
  137. package/dist/stream.js +151 -0
  138. package/dist/testing/helpers.d.ts +23 -0
  139. package/dist/testing/helpers.js +95 -0
  140. package/dist/testing/index.d.ts +2 -0
  141. package/dist/testing/index.js +2 -0
  142. package/dist/testing/run.d.ts +34 -0
  143. package/dist/testing/run.js +303 -0
  144. package/dist/types.d.ts +1326 -0
  145. package/dist/types.js +61 -0
  146. package/dist/utils/date.d.ts +6 -0
  147. package/dist/utils/date.js +101 -0
  148. package/dist/utils/parse.d.ts +16 -0
  149. package/dist/utils/parse.js +51 -0
  150. package/dist/utils/text.d.ts +4 -0
  151. package/dist/utils/text.js +14 -0
  152. package/dist/utils/transform.d.ts +8 -0
  153. package/dist/utils/transform.js +48 -0
  154. package/package.json +57 -30
  155. package/src/ceremonies/index.ts +8 -2
  156. package/src/choice-token.ts +165 -0
  157. package/src/cli/commands.ts +34 -11
  158. package/src/cli/create.ts +214 -52
  159. package/src/cli/templates/provider/.dockerignore.tpl +22 -0
  160. package/src/cli/templates/provider/.gitignore.tpl +22 -0
  161. package/src/cli/templates/provider/README.md.tpl +120 -1
  162. package/src/cli/templates/provider/dev.ts.tpl +1 -1
  163. package/src/cli/templates/provider/domain/README.md.tpl +3 -0
  164. package/src/cli/templates/provider/index.ts.tpl +5 -48
  165. package/src/cli/templates/provider/mappers/README.md.tpl +3 -0
  166. package/src/cli/templates/provider/meta.ts.tpl +7 -0
  167. package/src/cli/templates/provider/operations/index.ts.tpl +5 -0
  168. package/src/cli/templates/provider/operations/ping.ts.tpl +24 -0
  169. package/src/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  170. package/src/cli/templates/provider/start.ts.tpl +1 -1
  171. package/src/cli/templates/provider/upstream/README.md.tpl +3 -0
  172. package/src/config/loader.ts +1224 -9
  173. package/src/contract-json.ts +75 -0
  174. package/src/contract-serialization.ts +89 -0
  175. package/src/contract-types.ts +52 -0
  176. package/src/contract.ts +215 -0
  177. package/src/define.ts +1688 -48
  178. package/src/errors.ts +27 -0
  179. package/src/i18n/catalog.ts +277 -0
  180. package/src/i18n/index.ts +2 -0
  181. package/src/i18n/keys.ts +64 -0
  182. package/src/index.ts +174 -9
  183. package/src/lint.ts +547 -73
  184. package/src/observability.ts +41 -0
  185. package/src/provider.ts +104 -4
  186. package/src/public-schema-field-lint.ts +237 -0
  187. package/src/runtime/auth-flow.ts +7 -0
  188. package/src/runtime/browser.ts +762 -51
  189. package/src/runtime/cache.ts +528 -0
  190. package/src/runtime/choice.ts +760 -0
  191. package/src/runtime/executor.ts +32 -3
  192. package/src/runtime/http.ts +939 -195
  193. package/src/runtime/insights.ts +11 -11
  194. package/src/runtime/instrumentation.ts +12 -4
  195. package/src/runtime/key-derivation.ts +1 -1
  196. package/src/runtime/keyring.ts +4 -3
  197. package/src/runtime/proxy-errors.ts +132 -0
  198. package/src/runtime/proxy-telemetry.ts +253 -0
  199. package/src/runtime/redis.ts +116 -0
  200. package/src/runtime/request-options.ts +66 -0
  201. package/src/runtime/state.ts +563 -0
  202. package/src/runtime/stealth.ts +1159 -0
  203. package/src/runtime/stt.ts +629 -0
  204. package/src/runtime/trace.ts +1 -1
  205. package/src/schema.ts +363 -1
  206. package/src/server/serve.ts +1157 -75
  207. package/src/server/types.ts +37 -0
  208. package/src/stream.ts +210 -0
  209. package/src/testing/run.ts +31 -5
  210. package/src/types.ts +1107 -59
  211. package/src/runtime/tls.ts +0 -434
  212. package/src/types/playwright-stealth.d.ts +0 -9
@@ -0,0 +1,706 @@
1
+ import { resolveProxyConfigAsync } from "../config/loader";
2
+ import { ProviderError, TransportError } from "../errors";
3
+ import { parseSseStream, readableBytes, readableLines, readableTextChunks, } from "../stream";
4
+ import { HttpRetryAfterPolicy, HttpRetryDelayStrategy, HttpRetryJitter, HttpRetryPreset, HttpRetryUnsafeMethodPolicy, } from "../types";
5
+ import { appendQueryParams, normalizeHttpRequestBody } from "./request-options";
6
+ const DEFAULT_HTTP_BASE_URL = "http://localhost";
7
+ function isHttpStatusOutcome(outcome) {
8
+ return "kind" in outcome && outcome.kind === "http-status";
9
+ }
10
+ const DEFAULT_RETRY_METHODS = ["GET", "HEAD", "OPTIONS"];
11
+ const DEFAULT_RETRY_ERROR_CODES = [
12
+ "transport_network_error",
13
+ "transport_timeout",
14
+ ];
15
+ const SAFE_RETRY_STATUS_CODES = [408, 429, 500, 502, 503, 504];
16
+ const RATE_LIMIT_RETRY_STATUS_CODES = [429, 503];
17
+ const KNOWN_RETRY_METHODS = new Set([
18
+ "GET",
19
+ "HEAD",
20
+ "POST",
21
+ "PUT",
22
+ "DELETE",
23
+ "OPTIONS",
24
+ "TRACE",
25
+ "PATCH",
26
+ ]);
27
+ const UNSAFE_RETRY_METHODS = new Set([
28
+ "POST",
29
+ "PUT",
30
+ "PATCH",
31
+ "DELETE",
32
+ "TRACE",
33
+ ]);
34
+ const MAX_RETRY_ATTEMPTS = 8;
35
+ const MAX_RETRY_DELAY_MS = 30_000;
36
+ function hasOwnValue(values, value) {
37
+ if (typeof value !== "string")
38
+ return false;
39
+ return Object.values(values).some((candidate) => candidate === value);
40
+ }
41
+ function createInvalidRetryPolicyError(message) {
42
+ return new ProviderError(message, { code: "retry_invalid_policy" });
43
+ }
44
+ function createRetryOptions(preset) {
45
+ switch (preset) {
46
+ case HttpRetryPreset.Off:
47
+ return {
48
+ preset,
49
+ attempts: 1,
50
+ methods: DEFAULT_RETRY_METHODS,
51
+ statusCodes: [],
52
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
53
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
54
+ baseDelayMs: 100,
55
+ maxDelayMs: 1_000,
56
+ jitter: HttpRetryJitter.Full,
57
+ retryAfter: HttpRetryAfterPolicy.Ignore,
58
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
59
+ };
60
+ case HttpRetryPreset.SafeRead:
61
+ return {
62
+ preset,
63
+ attempts: 3,
64
+ methods: DEFAULT_RETRY_METHODS,
65
+ statusCodes: SAFE_RETRY_STATUS_CODES,
66
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
67
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
68
+ baseDelayMs: 100,
69
+ maxDelayMs: 2_000,
70
+ jitter: HttpRetryJitter.Full,
71
+ retryAfter: HttpRetryAfterPolicy.Cap,
72
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
73
+ };
74
+ case HttpRetryPreset.AggressiveRead:
75
+ return {
76
+ preset,
77
+ attempts: 4,
78
+ methods: DEFAULT_RETRY_METHODS,
79
+ statusCodes: SAFE_RETRY_STATUS_CODES,
80
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
81
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
82
+ baseDelayMs: 150,
83
+ maxDelayMs: 5_000,
84
+ jitter: HttpRetryJitter.Full,
85
+ retryAfter: HttpRetryAfterPolicy.Cap,
86
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
87
+ };
88
+ case HttpRetryPreset.RateLimitAware:
89
+ return {
90
+ preset,
91
+ attempts: 3,
92
+ methods: DEFAULT_RETRY_METHODS,
93
+ statusCodes: RATE_LIMIT_RETRY_STATUS_CODES,
94
+ errorCodes: ["transport_timeout"],
95
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
96
+ baseDelayMs: 250,
97
+ maxDelayMs: 5_000,
98
+ jitter: HttpRetryJitter.Equal,
99
+ retryAfter: HttpRetryAfterPolicy.Respect,
100
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
101
+ };
102
+ case HttpRetryPreset.TransportTransient:
103
+ return {
104
+ preset,
105
+ attempts: 3,
106
+ methods: DEFAULT_RETRY_METHODS,
107
+ statusCodes: [],
108
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
109
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
110
+ baseDelayMs: 100,
111
+ maxDelayMs: 1_000,
112
+ jitter: HttpRetryJitter.Full,
113
+ retryAfter: HttpRetryAfterPolicy.Ignore,
114
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
115
+ };
116
+ }
117
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry preset: ${preset}`);
118
+ }
119
+ function clampPositiveInteger(value, fallback, max) {
120
+ if (value === undefined)
121
+ return fallback;
122
+ if (!Number.isFinite(value) || value < 1)
123
+ return fallback;
124
+ return Math.min(Math.floor(value), max);
125
+ }
126
+ function clampDelay(value, fallback) {
127
+ if (value === undefined)
128
+ return fallback;
129
+ if (!Number.isFinite(value) || value < 0)
130
+ return fallback;
131
+ return Math.min(Math.floor(value), MAX_RETRY_DELAY_MS);
132
+ }
133
+ function normalizeRetryOptions(retry) {
134
+ if (retry === undefined || retry === false) {
135
+ return undefined;
136
+ }
137
+ if (retry === true) {
138
+ return createRetryOptions(HttpRetryPreset.TransportTransient);
139
+ }
140
+ if (typeof retry === "string") {
141
+ if (!hasOwnValue(HttpRetryPreset, retry)) {
142
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry preset: ${retry}`);
143
+ }
144
+ return createRetryOptions(retry);
145
+ }
146
+ if (typeof retry !== "object" || retry === null) {
147
+ throw createInvalidRetryPolicyError("HTTP retry policy must be an object");
148
+ }
149
+ if (Array.isArray(retry)) {
150
+ throw createInvalidRetryPolicyError("HTTP retry policy must be a plain object");
151
+ }
152
+ validateRetryOptionsShape(retry);
153
+ const base = createRetryOptions(retry.preset ?? HttpRetryPreset.TransportTransient);
154
+ const maxDelayMs = clampDelay(retry.maxDelayMs, base.maxDelayMs);
155
+ const normalized = {
156
+ preset: retry.preset ?? base.preset,
157
+ attempts: clampPositiveInteger(retry.attempts, base.attempts, MAX_RETRY_ATTEMPTS),
158
+ methods: retry.methods?.map((method) => method.toUpperCase()) ?? base.methods,
159
+ statusCodes: retry.statusCodes?.filter((status) => Number.isInteger(status)) ??
160
+ base.statusCodes,
161
+ errorCodes: retry.errorCodes ?? base.errorCodes,
162
+ delayStrategy: retry.delayStrategy ?? base.delayStrategy,
163
+ baseDelayMs: clampDelay(retry.baseDelayMs, base.baseDelayMs),
164
+ maxDelayMs,
165
+ jitter: retry.jitter ?? base.jitter,
166
+ retryAfter: retry.retryAfter ?? base.retryAfter,
167
+ unsafeMethodPolicy: retry.unsafeMethodPolicy ?? base.unsafeMethodPolicy,
168
+ };
169
+ return normalized;
170
+ }
171
+ function validateRetryOptionsShape(retry) {
172
+ if (retry.preset !== undefined &&
173
+ !hasOwnValue(HttpRetryPreset, retry.preset)) {
174
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry preset: ${String(retry.preset)}`);
175
+ }
176
+ if (retry.delayStrategy !== undefined &&
177
+ !hasOwnValue(HttpRetryDelayStrategy, retry.delayStrategy)) {
178
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry delay strategy: ${String(retry.delayStrategy)}`);
179
+ }
180
+ if (retry.jitter !== undefined &&
181
+ !hasOwnValue(HttpRetryJitter, retry.jitter)) {
182
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry jitter policy: ${String(retry.jitter)}`);
183
+ }
184
+ if (retry.retryAfter !== undefined &&
185
+ !hasOwnValue(HttpRetryAfterPolicy, retry.retryAfter)) {
186
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry-after policy: ${String(retry.retryAfter)}`);
187
+ }
188
+ if (retry.unsafeMethodPolicy !== undefined &&
189
+ !hasOwnValue(HttpRetryUnsafeMethodPolicy, retry.unsafeMethodPolicy)) {
190
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry unsafe method policy: ${String(retry.unsafeMethodPolicy)}`);
191
+ }
192
+ if (retry.methods !== undefined) {
193
+ if (!Array.isArray(retry.methods)) {
194
+ throw createInvalidRetryPolicyError("HTTP retry methods must be an array");
195
+ }
196
+ const nonStringMethods = retry.methods.filter((method) => typeof method !== "string");
197
+ if (nonStringMethods.length > 0) {
198
+ throw createInvalidRetryPolicyError("HTTP retry methods must contain only strings");
199
+ }
200
+ const unknownMethods = retry.methods
201
+ .map((method) => method.toUpperCase())
202
+ .filter((method) => !KNOWN_RETRY_METHODS.has(method));
203
+ if (unknownMethods.length > 0) {
204
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry method(s): ${unknownMethods.join(", ")}`);
205
+ }
206
+ }
207
+ if (retry.statusCodes !== undefined) {
208
+ if (!Array.isArray(retry.statusCodes)) {
209
+ throw createInvalidRetryPolicyError("HTTP retry statusCodes must be an array");
210
+ }
211
+ const invalidStatusCodes = retry.statusCodes.filter((status) => !Number.isInteger(status) ||
212
+ Number(status) < 100 ||
213
+ Number(status) > 599);
214
+ if (invalidStatusCodes.length > 0) {
215
+ throw createInvalidRetryPolicyError("HTTP retry statusCodes must contain HTTP status integers in [100, 599]");
216
+ }
217
+ }
218
+ if (retry.errorCodes !== undefined) {
219
+ if (!Array.isArray(retry.errorCodes)) {
220
+ throw createInvalidRetryPolicyError("HTTP retry errorCodes must be an array");
221
+ }
222
+ const nonStringErrorCodes = retry.errorCodes.filter((errorCode) => typeof errorCode !== "string");
223
+ if (nonStringErrorCodes.length > 0) {
224
+ throw createInvalidRetryPolicyError("HTTP retry errorCodes must contain only strings");
225
+ }
226
+ }
227
+ if (retry.preset === HttpRetryPreset.Off &&
228
+ ((retry.attempts !== undefined && retry.attempts > 1) ||
229
+ (retry.statusCodes !== undefined && retry.statusCodes.length > 0))) {
230
+ throw createInvalidRetryPolicyError("HTTP retry preset off cannot be combined with retry-enabling overrides");
231
+ }
232
+ }
233
+ function validateUnsafeRetryMethods(options) {
234
+ if (options.unsafeMethodPolicy ===
235
+ HttpRetryUnsafeMethodPolicy.AllowExplicitUnsafe) {
236
+ return;
237
+ }
238
+ const unsafeMethods = options.methods.filter((method) => UNSAFE_RETRY_METHODS.has(method.toUpperCase()));
239
+ if (unsafeMethods.length === 0)
240
+ return;
241
+ throw new ProviderError(`HTTP retry methods include unsafe method(s): ${unsafeMethods.join(", ")}`, { code: "retry_unsafe_method" });
242
+ }
243
+ function isMethodRetryable(method, options) {
244
+ return options.methods
245
+ .map((allowedMethod) => allowedMethod.toUpperCase())
246
+ .includes(method.toUpperCase());
247
+ }
248
+ function retryErrorCode(error) {
249
+ if (error instanceof TransportError) {
250
+ return error.code;
251
+ }
252
+ if (error && typeof error === "object" && "code" in error) {
253
+ const code = Reflect.get(error, "code");
254
+ return typeof code === "string" ? code : undefined;
255
+ }
256
+ return undefined;
257
+ }
258
+ function retryErrorStatus(error) {
259
+ if (error instanceof TransportError) {
260
+ return error.status ?? error.upstreamStatus;
261
+ }
262
+ return undefined;
263
+ }
264
+ function shouldRetryTransportError(error, options) {
265
+ const code = retryErrorCode(error);
266
+ return Boolean(code && options.errorCodes.includes(code));
267
+ }
268
+ function retryAfterHeader(headers) {
269
+ for (const [name, value] of Object.entries(headers)) {
270
+ if (name.toLowerCase() === "retry-after")
271
+ return value;
272
+ }
273
+ return undefined;
274
+ }
275
+ function parseRetryAfterMs(headers, now = Date.now()) {
276
+ const value = retryAfterHeader(headers);
277
+ if (!value)
278
+ return undefined;
279
+ const seconds = Number(value);
280
+ if (Number.isFinite(seconds)) {
281
+ return Math.max(0, Math.floor(seconds * 1_000));
282
+ }
283
+ const dateMs = Date.parse(value);
284
+ if (!Number.isNaN(dateMs)) {
285
+ return Math.max(0, dateMs - now);
286
+ }
287
+ return undefined;
288
+ }
289
+ function computeRetryDelayMs(options, attemptIndex, headers) {
290
+ const multiplier = options.delayStrategy === HttpRetryDelayStrategy.Exponential
291
+ ? 2 ** Math.max(0, attemptIndex - 1)
292
+ : 1;
293
+ const configuredDelay = Math.min(options.baseDelayMs * multiplier, options.maxDelayMs);
294
+ const retryAfterMs = options.retryAfter === HttpRetryAfterPolicy.Ignore
295
+ ? undefined
296
+ : headers
297
+ ? parseRetryAfterMs(headers)
298
+ : undefined;
299
+ if (retryAfterMs !== undefined) {
300
+ const boundedRetryAfterMs = Math.min(retryAfterMs, options.maxDelayMs);
301
+ if (options.retryAfter === HttpRetryAfterPolicy.Cap) {
302
+ return Math.min(boundedRetryAfterMs, configuredDelay);
303
+ }
304
+ return boundedRetryAfterMs;
305
+ }
306
+ switch (options.jitter) {
307
+ case HttpRetryJitter.None:
308
+ return configuredDelay;
309
+ case HttpRetryJitter.Equal:
310
+ return Math.floor(configuredDelay / 2 + Math.random() * (configuredDelay / 2));
311
+ case HttpRetryJitter.Full:
312
+ return Math.floor(Math.random() * configuredDelay);
313
+ }
314
+ }
315
+ async function sleep(ms) {
316
+ if (ms <= 0)
317
+ return;
318
+ await new Promise((resolve) => setTimeout(resolve, ms));
319
+ }
320
+ function toUpstreamHttpError(status) {
321
+ return new TransportError(`Upstream request failed with status ${status}`, {
322
+ code: "upstream_http_error",
323
+ status,
324
+ upstreamStatus: status,
325
+ });
326
+ }
327
+ function hasHeader(headers, name) {
328
+ const needle = name.toLowerCase();
329
+ return Object.keys(headers).some((key) => key.toLowerCase() === needle);
330
+ }
331
+ function withClientHeaders(options, clientOptions, body) {
332
+ const headers = {
333
+ ...(clientOptions.userAgent
334
+ ? { "User-Agent": clientOptions.userAgent }
335
+ : {}),
336
+ ...options?.headers,
337
+ };
338
+ if (body !== undefined && !hasHeader(headers, "Content-Type")) {
339
+ headers["Content-Type"] = "application/json";
340
+ }
341
+ return {
342
+ ...options,
343
+ headers,
344
+ };
345
+ }
346
+ function parseHttpData(body, headers) {
347
+ const contentType = headers["content-type"] ??
348
+ headers["Content-Type"] ??
349
+ headers["CONTENT-TYPE"];
350
+ if (contentType?.includes("application/json")) {
351
+ return body ? JSON.parse(body) : null;
352
+ }
353
+ return body;
354
+ }
355
+ function parseJson(body) {
356
+ return JSON.parse(body);
357
+ }
358
+ function isTimeoutMessage(message) {
359
+ return /\b(timed out|timeout|deadline exceeded)\b/i.test(message);
360
+ }
361
+ function toHttpTransportError(error) {
362
+ if (error instanceof TransportError) {
363
+ if (error.code) {
364
+ return error;
365
+ }
366
+ if (isTimeoutMessage(error.message)) {
367
+ return new TransportError("Request timed out", {
368
+ code: "transport_timeout",
369
+ status: error.status ?? 0,
370
+ cause: error,
371
+ });
372
+ }
373
+ if ((error.status ?? 0) === 0) {
374
+ return new TransportError(error.message || "Network error", {
375
+ code: "transport_network_error",
376
+ status: 0,
377
+ cause: error,
378
+ });
379
+ }
380
+ return error;
381
+ }
382
+ if (error instanceof Error) {
383
+ const timeout = error.name === "AbortError" ||
384
+ error.name === "TimeoutError" ||
385
+ isTimeoutMessage(error.message);
386
+ return new TransportError(timeout ? "Request timed out" : "Network error", {
387
+ code: timeout ? "transport_timeout" : "transport_network_error",
388
+ status: 0,
389
+ cause: error,
390
+ });
391
+ }
392
+ return new TransportError("Network error", {
393
+ code: "transport_network_error",
394
+ status: 0,
395
+ });
396
+ }
397
+ async function toNativeHttpResponse(response) {
398
+ const headers = Object.fromEntries(response.headers.entries());
399
+ const rawText = await response.text();
400
+ const data = parseHttpData(rawText, headers);
401
+ return {
402
+ data,
403
+ headers,
404
+ json: async () => {
405
+ const contentType = headers["content-type"] ??
406
+ headers["Content-Type"] ??
407
+ headers["CONTENT-TYPE"];
408
+ return parseJson(contentType?.includes("application/json") && !rawText
409
+ ? "null"
410
+ : rawText);
411
+ },
412
+ ok: response.status >= 200 && response.status < 300,
413
+ status: response.status,
414
+ text: async () => rawText,
415
+ };
416
+ }
417
+ async function drainNativeResponseBody(response) {
418
+ try {
419
+ await response.arrayBuffer();
420
+ }
421
+ catch {
422
+ await response.body?.cancel().catch(() => undefined);
423
+ }
424
+ }
425
+ function requireNativeResponseBody(response) {
426
+ if (!response.body) {
427
+ throw new TransportError("Response body stream is unavailable", {
428
+ code: "transport_stream_unavailable",
429
+ status: response.status,
430
+ });
431
+ }
432
+ return response.body;
433
+ }
434
+ function toNativeHttpStreamResponse(response) {
435
+ const headers = Object.fromEntries(response.headers.entries());
436
+ const body = requireNativeResponseBody(response);
437
+ return {
438
+ body,
439
+ headers,
440
+ ok: response.status >= 200 && response.status < 300,
441
+ status: response.status,
442
+ bytes: () => readableBytes(body),
443
+ textChunks: () => readableTextChunks(body),
444
+ lines: () => readableLines(body),
445
+ };
446
+ }
447
+ function normalizeHttpMethod(method) {
448
+ switch (method.toUpperCase()) {
449
+ case "HEAD":
450
+ return "HEAD";
451
+ case "GET":
452
+ return "GET";
453
+ case "POST":
454
+ return "POST";
455
+ case "PUT":
456
+ return "PUT";
457
+ case "DELETE":
458
+ return "DELETE";
459
+ case "OPTIONS":
460
+ return "OPTIONS";
461
+ case "TRACE":
462
+ return "TRACE";
463
+ case "PATCH":
464
+ return "PATCH";
465
+ default:
466
+ throw new TransportError(`Unsupported HTTP method: ${method}`, {
467
+ code: "transport_invalid_method",
468
+ });
469
+ }
470
+ }
471
+ function isAbsoluteUrl(url) {
472
+ return /^[a-z][a-z\d+\-.]*:/i.test(url);
473
+ }
474
+ function resolveHttpUrl(baseUrl, url) {
475
+ return new URL(url, baseUrl ?? DEFAULT_HTTP_BASE_URL).toString();
476
+ }
477
+ async function resolveNativeProxy(options, clientOptions, warn) {
478
+ const resolvedProxy = await resolveProxyConfigAsync({
479
+ proxy: options.proxy ?? clientOptions.proxy,
480
+ upstream: clientOptions.upstream,
481
+ apifuseConfig: clientOptions.apifuseConfig,
482
+ affinityKey: clientOptions.affinityKey,
483
+ telemetry: clientOptions.telemetry,
484
+ });
485
+ if (resolvedProxy.shouldWarn) {
486
+ warn("[provider-sdk] Provider requested proxy routing, but no proxy URL was configured. Continuing without proxy.");
487
+ }
488
+ return resolvedProxy.url;
489
+ }
490
+ function assertNoHttpTransportOverrides(options) {
491
+ if ("profile" in options || "stealth" in options) {
492
+ throw new ProviderError("ctx.http does not accept stealth transport options. Use ctx.stealth.fetch() for browser-like impersonation.", { code: "http_transport_override_unsupported" });
493
+ }
494
+ }
495
+ function normalizeNativeFetchBody(body) {
496
+ const normalized = normalizeHttpRequestBody(body);
497
+ if (!Buffer.isBuffer(normalized)) {
498
+ return normalized;
499
+ }
500
+ const copied = new Uint8Array(normalized.byteLength);
501
+ copied.set(normalized);
502
+ return copied.buffer;
503
+ }
504
+ async function fetchNativeHttp(baseUrl, url, method, options, clientOptions, warn, statusRetryCodes) {
505
+ const requestUrl = appendQueryParams(resolveHttpUrl(baseUrl, url), options.params);
506
+ const controller = options.timeout ? new AbortController() : undefined;
507
+ const timeoutHandle = options.timeout
508
+ ? setTimeout(() => controller?.abort(), options.timeout)
509
+ : undefined;
510
+ try {
511
+ const proxy = await resolveNativeProxy(options, clientOptions, warn);
512
+ const requestInit = {
513
+ headers: options.headers,
514
+ method,
515
+ ...(proxy ? { proxy } : {}),
516
+ signal: controller?.signal,
517
+ };
518
+ if (options.body !== undefined) {
519
+ requestInit.body = normalizeNativeFetchBody(options.body);
520
+ }
521
+ const response = await fetch(requestUrl, {
522
+ ...requestInit,
523
+ });
524
+ const headers = Object.fromEntries(response.headers.entries());
525
+ if (statusRetryCodes && response.status >= 400) {
526
+ await drainNativeResponseBody(response);
527
+ return {
528
+ kind: "http-status",
529
+ status: response.status,
530
+ headers,
531
+ retryable: statusRetryCodes.includes(response.status),
532
+ };
533
+ }
534
+ if (response.status >= 400 && options.throwOnHttpError !== false) {
535
+ await drainNativeResponseBody(response);
536
+ throw new TransportError(`Upstream request failed with status ${response.status}`, {
537
+ code: "upstream_http_error",
538
+ status: response.status,
539
+ });
540
+ }
541
+ return toNativeHttpResponse(response);
542
+ }
543
+ catch (error) {
544
+ if (error instanceof SyntaxError) {
545
+ throw error;
546
+ }
547
+ throw toHttpTransportError(error);
548
+ }
549
+ finally {
550
+ if (timeoutHandle)
551
+ clearTimeout(timeoutHandle);
552
+ }
553
+ }
554
+ async function fetchNativeHttpStream(baseUrl, url, method, options, clientOptions, warn) {
555
+ const requestUrl = appendQueryParams(resolveHttpUrl(baseUrl, url), options.params);
556
+ const controller = options.timeout ? new AbortController() : undefined;
557
+ const timeoutHandle = options.timeout
558
+ ? setTimeout(() => controller?.abort(), options.timeout)
559
+ : undefined;
560
+ try {
561
+ const proxy = await resolveNativeProxy(options, clientOptions, warn);
562
+ const requestInit = {
563
+ headers: options.headers,
564
+ method,
565
+ ...(proxy ? { proxy } : {}),
566
+ signal: controller?.signal,
567
+ };
568
+ if (options.body !== undefined) {
569
+ requestInit.body = normalizeNativeFetchBody(options.body);
570
+ }
571
+ const response = await fetch(requestUrl, {
572
+ ...requestInit,
573
+ });
574
+ if (response.status >= 400 && options.throwOnHttpError !== false) {
575
+ await drainNativeResponseBody(response);
576
+ throw new TransportError(`Upstream request failed with status ${response.status}`, {
577
+ code: "upstream_http_error",
578
+ status: response.status,
579
+ });
580
+ }
581
+ return toNativeHttpStreamResponse(response);
582
+ }
583
+ catch (error) {
584
+ if (error instanceof SyntaxError) {
585
+ throw error;
586
+ }
587
+ throw toHttpTransportError(error);
588
+ }
589
+ finally {
590
+ if (timeoutHandle)
591
+ clearTimeout(timeoutHandle);
592
+ }
593
+ }
594
+ export function createHttpClient(baseUrl, clientOptions = {}) {
595
+ const warnedMessages = new Set();
596
+ const warn = clientOptions.warn ?? console.warn;
597
+ const warnOnce = (message) => {
598
+ if (warnedMessages.has(message)) {
599
+ return;
600
+ }
601
+ warnedMessages.add(message);
602
+ warn(message);
603
+ };
604
+ async function request(url, method, options = {}) {
605
+ if (!baseUrl && !isAbsoluteUrl(url)) {
606
+ throw new TransportError("ctx.http requires an absolute URL when provider.upstream.baseUrl is not declared", { code: "transport_invalid_url" });
607
+ }
608
+ assertNoHttpTransportOverrides(options);
609
+ const headersOptions = withClientHeaders(options, clientOptions, options.body);
610
+ const methodName = normalizeHttpMethod(method);
611
+ const retryOptions = normalizeRetryOptions(headersOptions.retry);
612
+ if (retryOptions)
613
+ validateUnsafeRetryMethods(retryOptions);
614
+ const retryEnabled = Boolean(retryOptions &&
615
+ retryOptions.attempts > 1 &&
616
+ isMethodRetryable(methodName, retryOptions));
617
+ const statusRetryEnabled = Boolean(retryEnabled &&
618
+ retryOptions &&
619
+ retryOptions.statusCodes.length > 0 &&
620
+ headersOptions.throwOnHttpError !== false);
621
+ const attemptOptions = statusRetryEnabled
622
+ ? { ...headersOptions, throwOnHttpError: false }
623
+ : headersOptions;
624
+ const executeOnce = () => fetchNativeHttp(baseUrl, url, methodName, attemptOptions, clientOptions, warnOnce, statusRetryEnabled ? retryOptions?.statusCodes : undefined);
625
+ if (!retryEnabled || !retryOptions) {
626
+ const outcome = await executeOnce();
627
+ if (isHttpStatusOutcome(outcome)) {
628
+ throw toUpstreamHttpError(outcome.status);
629
+ }
630
+ return outcome;
631
+ }
632
+ let lastErrorCode;
633
+ let lastStatus;
634
+ for (let attempt = 1; attempt <= retryOptions.attempts; attempt += 1) {
635
+ try {
636
+ const outcome = await executeOnce();
637
+ if (isHttpStatusOutcome(outcome)) {
638
+ lastStatus = outcome.status;
639
+ if (outcome.retryable && attempt < retryOptions.attempts) {
640
+ await sleep(computeRetryDelayMs(retryOptions, attempt, outcome.headers));
641
+ continue;
642
+ }
643
+ throw toUpstreamHttpError(outcome.status);
644
+ }
645
+ const response = outcome;
646
+ if (response.status >= 400 &&
647
+ headersOptions.throwOnHttpError !== false) {
648
+ throw toUpstreamHttpError(response.status);
649
+ }
650
+ if (attempt > 1) {
651
+ const summary = {
652
+ attempts: attempt,
653
+ retries: attempt - 1,
654
+ ...(retryOptions.preset ? { preset: retryOptions.preset } : {}),
655
+ transport: "native",
656
+ ...(lastErrorCode ? { lastErrorCode } : {}),
657
+ ...(lastStatus ? { lastStatus } : {}),
658
+ };
659
+ clientOptions.onRetrySummary?.(summary);
660
+ }
661
+ return response;
662
+ }
663
+ catch (error) {
664
+ lastErrorCode = retryErrorCode(error);
665
+ lastStatus = retryErrorStatus(error);
666
+ if (attempt < retryOptions.attempts &&
667
+ shouldRetryTransportError(error, retryOptions)) {
668
+ await sleep(computeRetryDelayMs(retryOptions, attempt));
669
+ continue;
670
+ }
671
+ throw error;
672
+ }
673
+ }
674
+ throw new TransportError("HTTP retry exhausted without a terminal result", {
675
+ code: "retry_exhausted",
676
+ });
677
+ }
678
+ async function streamRequest(url, method, options = {}) {
679
+ if (!baseUrl && !isAbsoluteUrl(url)) {
680
+ throw new TransportError("ctx.http requires an absolute URL when provider.upstream.baseUrl is not declared", { code: "transport_invalid_url" });
681
+ }
682
+ assertNoHttpTransportOverrides(options);
683
+ const headersOptions = withClientHeaders(options, clientOptions, options.body);
684
+ const methodName = normalizeHttpMethod(method);
685
+ return fetchNativeHttpStream(baseUrl, url, methodName, headersOptions, clientOptions, warnOnce);
686
+ }
687
+ return {
688
+ request: async (url, options = {}) => request(url, options.method ?? "GET", options),
689
+ get: async (url, options) => request(url, "GET", options),
690
+ post: async (url, body, options) => request(url, "POST", { ...options, body }),
691
+ put: async (url, body, options) => request(url, "PUT", { ...options, body }),
692
+ delete: async (url, options) => request(url, "DELETE", options),
693
+ stream: async (url, options = {}) => streamRequest(url, options.method ?? "GET", options),
694
+ sse: async (url, options = {}) => {
695
+ const headers = {
696
+ Accept: "text/event-stream",
697
+ ...options.headers,
698
+ };
699
+ const response = await streamRequest(url, options.method ?? "GET", {
700
+ ...options,
701
+ headers,
702
+ });
703
+ return parseSseStream(response.body);
704
+ },
705
+ };
706
+ }