@apifuse/provider-sdk 2.1.0-beta.3 → 2.1.0-beta.4

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 (60) hide show
  1. package/AUTHORING.md +163 -8
  2. package/CHANGELOG.md +8 -1
  3. package/README.md +17 -16
  4. package/SUBMISSION.md +4 -4
  5. package/bin/apifuse-dev.ts +12 -5
  6. package/bin/apifuse-pack-check.ts +9 -2
  7. package/bin/apifuse-pack-smoke.ts +127 -6
  8. package/bin/apifuse-perf.ts +19 -15
  9. package/bin/apifuse-record.ts +41 -53
  10. package/bin/apifuse-submit-check.ts +179 -7
  11. package/bin/apifuse.ts +1 -1
  12. package/package.json +17 -8
  13. package/src/choice-token.ts +164 -0
  14. package/src/cli/commands.ts +1 -3
  15. package/src/cli/create.ts +159 -50
  16. package/src/cli/templates/provider/README.md.tpl +24 -7
  17. package/src/cli/templates/provider/dev.ts.tpl +1 -1
  18. package/src/cli/templates/provider/domain/README.md.tpl +3 -0
  19. package/src/cli/templates/provider/index.ts.tpl +5 -47
  20. package/src/cli/templates/provider/mappers/README.md.tpl +3 -0
  21. package/src/cli/templates/provider/meta.ts.tpl +7 -0
  22. package/src/cli/templates/provider/operations/index.ts.tpl +5 -0
  23. package/src/cli/templates/provider/operations/ping.ts.tpl +23 -0
  24. package/src/cli/templates/provider/schemas/ping.ts.tpl +16 -0
  25. package/src/cli/templates/provider/start.ts.tpl +1 -1
  26. package/src/cli/templates/provider/upstream/README.md.tpl +3 -0
  27. package/src/config/loader.ts +1206 -9
  28. package/src/define.ts +1618 -104
  29. package/src/errors.ts +12 -0
  30. package/src/i18n/catalog.ts +121 -0
  31. package/src/i18n/index.ts +2 -0
  32. package/src/i18n/keys.ts +64 -0
  33. package/src/index.ts +149 -8
  34. package/src/lint.ts +297 -42
  35. package/src/observability.ts +41 -0
  36. package/src/provider.ts +60 -3
  37. package/src/public-schema-field-lint.ts +237 -0
  38. package/src/runtime/auth-flow.ts +7 -0
  39. package/src/runtime/browser.ts +77 -21
  40. package/src/runtime/cache.ts +582 -0
  41. package/src/runtime/executor.ts +13 -1
  42. package/src/runtime/http.ts +939 -195
  43. package/src/runtime/insights.ts +11 -11
  44. package/src/runtime/instrumentation.ts +12 -4
  45. package/src/runtime/key-derivation.ts +1 -1
  46. package/src/runtime/keyring.ts +4 -3
  47. package/src/runtime/proxy-errors.ts +132 -0
  48. package/src/runtime/proxy-telemetry.ts +253 -0
  49. package/src/runtime/request-options.ts +66 -0
  50. package/src/runtime/state.ts +76 -0
  51. package/src/runtime/stealth.ts +1145 -0
  52. package/src/runtime/stt.ts +629 -0
  53. package/src/schema.ts +363 -1
  54. package/src/server/serve.ts +816 -58
  55. package/src/server/types.ts +35 -0
  56. package/src/stream.ts +210 -0
  57. package/src/testing/run.ts +17 -4
  58. package/src/types.ts +869 -50
  59. package/src/runtime/tls.ts +0 -434
  60. package/src/types/playwright-stealth.d.ts +0 -9
@@ -1,272 +1,1016 @@
1
- import { createRequire } from "node:module";
2
-
3
1
  import type { ProxyResolutionOptions } from "../config/loader";
4
- import { resolveProxyConfig } from "../config/loader";
5
- import { TransportError } from "../errors";
6
- import { generateLayer2Headers } from "../stealth/profiles";
2
+ import { resolveProxyConfigAsync } from "../config/loader";
3
+ import { ProviderError, TransportError } from "../errors";
4
+ import {
5
+ parseSseStream,
6
+ readableBytes,
7
+ readableLines,
8
+ readableTextChunks,
9
+ } from "../stream";
7
10
  import type {
8
11
  HttpClient,
12
+ HttpMethod,
9
13
  HttpResponse,
14
+ HttpRetryOptions,
15
+ HttpRetrySummary,
16
+ HttpStreamResponse,
10
17
  RequestOptions,
11
18
  RequestWithMethodOptions,
12
- StealthProfile,
19
+ SseMessage,
13
20
  } from "../types";
21
+ import {
22
+ HttpRetryAfterPolicy,
23
+ HttpRetryDelayStrategy,
24
+ HttpRetryJitter,
25
+ HttpRetryPreset,
26
+ HttpRetryUnsafeMethodPolicy,
27
+ } from "../types";
28
+ import { appendQueryParams, normalizeHttpRequestBody } from "./request-options";
14
29
 
15
- const require = createRequire(import.meta.url);
16
-
17
- const MISSING_PROXY_WARNING =
18
- "[provider-sdk] Provider requested proxy routing, but no proxy URL was configured. Continuing without proxy.";
19
-
20
- const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
30
+ const DEFAULT_HTTP_BASE_URL = "http://localhost";
21
31
 
22
- type FetchProxyInit = RequestInit & {
23
- dispatcher?: unknown;
24
- proxy?: string;
32
+ export type HttpClientOptions = ProxyResolutionOptions & {
33
+ warn?: (message: string) => void;
34
+ userAgent?: string;
35
+ onRetrySummary?: (summary: HttpRetrySummary) => void;
25
36
  };
26
37
 
27
- type UndiciModule = {
28
- ProxyAgent?: new (proxyUrl: string) => unknown;
38
+ type NormalizedRetryOptions = Required<
39
+ Pick<
40
+ HttpRetryOptions,
41
+ | "attempts"
42
+ | "delayStrategy"
43
+ | "baseDelayMs"
44
+ | "maxDelayMs"
45
+ | "jitter"
46
+ | "retryAfter"
47
+ | "unsafeMethodPolicy"
48
+ >
49
+ > & {
50
+ preset?: HttpRetryPreset;
51
+ methods: readonly string[];
52
+ statusCodes: readonly number[];
53
+ errorCodes: readonly string[];
29
54
  };
30
55
 
31
- export type HttpClientOptions = ProxyResolutionOptions & {
32
- warn?: (message: string) => void;
33
- stealthProfile?: StealthProfile;
34
- userAgent?: string;
56
+ type HttpStatusOutcome = {
57
+ kind: "http-status";
58
+ status: number;
59
+ headers: Record<string, string>;
60
+ retryable: boolean;
35
61
  };
36
62
 
37
- function buildUrl(url: string, params?: Record<string, string>): string {
38
- if (!params || Object.keys(params).length === 0) {
39
- return url;
40
- }
63
+ function isHttpStatusOutcome(
64
+ outcome: HttpResponse | HttpStatusOutcome,
65
+ ): outcome is HttpStatusOutcome {
66
+ return "kind" in outcome && outcome.kind === "http-status";
67
+ }
41
68
 
42
- const searchParams = new URLSearchParams(params);
43
- return `${url}?${searchParams.toString()}`;
69
+ const DEFAULT_RETRY_METHODS = ["GET", "HEAD", "OPTIONS"] as const;
70
+ const DEFAULT_RETRY_ERROR_CODES = [
71
+ "transport_network_error",
72
+ "transport_timeout",
73
+ ] as const;
74
+ const SAFE_RETRY_STATUS_CODES = [408, 429, 500, 502, 503, 504] as const;
75
+ const RATE_LIMIT_RETRY_STATUS_CODES = [429, 503] as const;
76
+ const KNOWN_RETRY_METHODS = new Set([
77
+ "GET",
78
+ "HEAD",
79
+ "POST",
80
+ "PUT",
81
+ "DELETE",
82
+ "OPTIONS",
83
+ "TRACE",
84
+ "PATCH",
85
+ ]);
86
+ const UNSAFE_RETRY_METHODS = new Set([
87
+ "POST",
88
+ "PUT",
89
+ "PATCH",
90
+ "DELETE",
91
+ "TRACE",
92
+ ]);
93
+ const MAX_RETRY_ATTEMPTS = 8;
94
+ const MAX_RETRY_DELAY_MS = 30_000;
95
+
96
+ function hasOwnValue<T extends string>(
97
+ values: Record<string, T>,
98
+ value: unknown,
99
+ ): value is T {
100
+ if (typeof value !== "string") return false;
101
+ return Object.values(values).some((candidate) => candidate === value);
44
102
  }
45
103
 
46
- function resolveUrl(baseUrl: string | undefined, url: string): string {
47
- if (!baseUrl) {
48
- return url;
49
- }
104
+ function createInvalidRetryPolicyError(message: string): ProviderError {
105
+ return new ProviderError(message, { code: "retry_invalid_policy" });
106
+ }
50
107
 
51
- return new URL(url, baseUrl).toString();
108
+ function createRetryOptions(preset: HttpRetryPreset): NormalizedRetryOptions {
109
+ switch (preset) {
110
+ case HttpRetryPreset.Off:
111
+ return {
112
+ preset,
113
+ attempts: 1,
114
+ methods: DEFAULT_RETRY_METHODS,
115
+ statusCodes: [],
116
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
117
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
118
+ baseDelayMs: 100,
119
+ maxDelayMs: 1_000,
120
+ jitter: HttpRetryJitter.Full,
121
+ retryAfter: HttpRetryAfterPolicy.Ignore,
122
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
123
+ };
124
+ case HttpRetryPreset.SafeRead:
125
+ return {
126
+ preset,
127
+ attempts: 3,
128
+ methods: DEFAULT_RETRY_METHODS,
129
+ statusCodes: SAFE_RETRY_STATUS_CODES,
130
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
131
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
132
+ baseDelayMs: 100,
133
+ maxDelayMs: 2_000,
134
+ jitter: HttpRetryJitter.Full,
135
+ retryAfter: HttpRetryAfterPolicy.Cap,
136
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
137
+ };
138
+ case HttpRetryPreset.AggressiveRead:
139
+ return {
140
+ preset,
141
+ attempts: 4,
142
+ methods: DEFAULT_RETRY_METHODS,
143
+ statusCodes: SAFE_RETRY_STATUS_CODES,
144
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
145
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
146
+ baseDelayMs: 150,
147
+ maxDelayMs: 5_000,
148
+ jitter: HttpRetryJitter.Full,
149
+ retryAfter: HttpRetryAfterPolicy.Cap,
150
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
151
+ };
152
+ case HttpRetryPreset.RateLimitAware:
153
+ return {
154
+ preset,
155
+ attempts: 3,
156
+ methods: DEFAULT_RETRY_METHODS,
157
+ statusCodes: RATE_LIMIT_RETRY_STATUS_CODES,
158
+ errorCodes: ["transport_timeout"],
159
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
160
+ baseDelayMs: 250,
161
+ maxDelayMs: 5_000,
162
+ jitter: HttpRetryJitter.Equal,
163
+ retryAfter: HttpRetryAfterPolicy.Respect,
164
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
165
+ };
166
+ case HttpRetryPreset.TransportTransient:
167
+ return {
168
+ preset,
169
+ attempts: 3,
170
+ methods: DEFAULT_RETRY_METHODS,
171
+ statusCodes: [],
172
+ errorCodes: DEFAULT_RETRY_ERROR_CODES,
173
+ delayStrategy: HttpRetryDelayStrategy.Exponential,
174
+ baseDelayMs: 100,
175
+ maxDelayMs: 1_000,
176
+ jitter: HttpRetryJitter.Full,
177
+ retryAfter: HttpRetryAfterPolicy.Ignore,
178
+ unsafeMethodPolicy: HttpRetryUnsafeMethodPolicy.Reject,
179
+ };
180
+ }
181
+ throw createInvalidRetryPolicyError(`Unknown HTTP retry preset: ${preset}`);
52
182
  }
53
183
 
54
- async function doRequest(
55
- baseUrl: string | undefined,
56
- url: string,
57
- method: string,
58
- proxy: string | undefined,
59
- options: RequestOptions & { body?: unknown } = {},
60
- ): Promise<HttpResponse> {
61
- const { headers, params, body } = options;
62
- const timeout = options.timeout ?? DEFAULT_REQUEST_TIMEOUT_MS;
184
+ function clampPositiveInteger(
185
+ value: number | undefined,
186
+ fallback: number,
187
+ max: number,
188
+ ): number {
189
+ if (value === undefined) return fallback;
190
+ if (!Number.isFinite(value) || value < 1) return fallback;
191
+ return Math.min(Math.floor(value), max);
192
+ }
63
193
 
64
- const controller = new AbortController();
65
- let timeoutId: ReturnType<typeof setTimeout> | undefined;
194
+ function clampDelay(value: number | undefined, fallback: number): number {
195
+ if (value === undefined) return fallback;
196
+ if (!Number.isFinite(value) || value < 0) return fallback;
197
+ return Math.min(Math.floor(value), MAX_RETRY_DELAY_MS);
198
+ }
66
199
 
67
- if (timeout) {
68
- timeoutId = setTimeout(() => controller.abort(), timeout);
200
+ function normalizeRetryOptions(
201
+ retry: RequestOptions["retry"],
202
+ ): NormalizedRetryOptions | undefined {
203
+ if (retry === undefined || retry === false) {
204
+ return undefined;
205
+ }
206
+ if (retry === true) {
207
+ return createRetryOptions(HttpRetryPreset.TransportTransient);
208
+ }
209
+ if (typeof retry === "string") {
210
+ if (!hasOwnValue(HttpRetryPreset, retry)) {
211
+ throw createInvalidRetryPolicyError(
212
+ `Unknown HTTP retry preset: ${retry}`,
213
+ );
214
+ }
215
+ return createRetryOptions(retry);
216
+ }
217
+ if (typeof retry !== "object" || retry === null) {
218
+ throw createInvalidRetryPolicyError("HTTP retry policy must be an object");
219
+ }
220
+ if (Array.isArray(retry)) {
221
+ throw createInvalidRetryPolicyError(
222
+ "HTTP retry policy must be a plain object",
223
+ );
69
224
  }
70
225
 
71
- try {
72
- const requestUrl = buildUrl(resolveUrl(baseUrl, url), params);
73
- const proxyInit = createProxyInit(proxy);
74
- const response = await fetch(requestUrl, {
75
- ...proxyInit,
76
- method,
77
- headers: {
78
- "Content-Type": "application/json",
79
- ...headers,
80
- },
81
- body: body !== undefined ? JSON.stringify(body) : undefined,
82
- signal: controller.signal,
83
- });
226
+ validateRetryOptionsShape(retry);
227
+ const base = createRetryOptions(
228
+ retry.preset ?? HttpRetryPreset.TransportTransient,
229
+ );
230
+ const maxDelayMs = clampDelay(retry.maxDelayMs, base.maxDelayMs);
231
+ const normalized: NormalizedRetryOptions = {
232
+ preset: retry.preset ?? base.preset,
233
+ attempts: clampPositiveInteger(
234
+ retry.attempts,
235
+ base.attempts,
236
+ MAX_RETRY_ATTEMPTS,
237
+ ),
238
+ methods:
239
+ retry.methods?.map((method) => method.toUpperCase()) ?? base.methods,
240
+ statusCodes:
241
+ retry.statusCodes?.filter((status) => Number.isInteger(status)) ??
242
+ base.statusCodes,
243
+ errorCodes: retry.errorCodes ?? base.errorCodes,
244
+ delayStrategy: retry.delayStrategy ?? base.delayStrategy,
245
+ baseDelayMs: clampDelay(retry.baseDelayMs, base.baseDelayMs),
246
+ maxDelayMs,
247
+ jitter: retry.jitter ?? base.jitter,
248
+ retryAfter: retry.retryAfter ?? base.retryAfter,
249
+ unsafeMethodPolicy: retry.unsafeMethodPolicy ?? base.unsafeMethodPolicy,
250
+ };
84
251
 
85
- if (!response.ok) {
86
- await drainFetchResponse(response);
87
- throw new TransportError(
88
- `Upstream request failed with status ${response.status}`,
89
- {
90
- code: "upstream_http_error",
91
- status: response.status,
92
- },
252
+ return normalized;
253
+ }
254
+
255
+ function validateRetryOptionsShape(retry: HttpRetryOptions): void {
256
+ if (
257
+ retry.preset !== undefined &&
258
+ !hasOwnValue(HttpRetryPreset, retry.preset)
259
+ ) {
260
+ throw createInvalidRetryPolicyError(
261
+ `Unknown HTTP retry preset: ${String(retry.preset)}`,
262
+ );
263
+ }
264
+ if (
265
+ retry.delayStrategy !== undefined &&
266
+ !hasOwnValue(HttpRetryDelayStrategy, retry.delayStrategy)
267
+ ) {
268
+ throw createInvalidRetryPolicyError(
269
+ `Unknown HTTP retry delay strategy: ${String(retry.delayStrategy)}`,
270
+ );
271
+ }
272
+ if (
273
+ retry.jitter !== undefined &&
274
+ !hasOwnValue(HttpRetryJitter, retry.jitter)
275
+ ) {
276
+ throw createInvalidRetryPolicyError(
277
+ `Unknown HTTP retry jitter policy: ${String(retry.jitter)}`,
278
+ );
279
+ }
280
+ if (
281
+ retry.retryAfter !== undefined &&
282
+ !hasOwnValue(HttpRetryAfterPolicy, retry.retryAfter)
283
+ ) {
284
+ throw createInvalidRetryPolicyError(
285
+ `Unknown HTTP retry-after policy: ${String(retry.retryAfter)}`,
286
+ );
287
+ }
288
+ if (
289
+ retry.unsafeMethodPolicy !== undefined &&
290
+ !hasOwnValue(HttpRetryUnsafeMethodPolicy, retry.unsafeMethodPolicy)
291
+ ) {
292
+ throw createInvalidRetryPolicyError(
293
+ `Unknown HTTP retry unsafe method policy: ${String(retry.unsafeMethodPolicy)}`,
294
+ );
295
+ }
296
+ if (retry.methods !== undefined) {
297
+ if (!Array.isArray(retry.methods)) {
298
+ throw createInvalidRetryPolicyError(
299
+ "HTTP retry methods must be an array",
300
+ );
301
+ }
302
+ const nonStringMethods = retry.methods.filter(
303
+ (method) => typeof method !== "string",
304
+ );
305
+ if (nonStringMethods.length > 0) {
306
+ throw createInvalidRetryPolicyError(
307
+ "HTTP retry methods must contain only strings",
308
+ );
309
+ }
310
+ const unknownMethods = retry.methods
311
+ .map((method) => method.toUpperCase())
312
+ .filter((method) => !KNOWN_RETRY_METHODS.has(method));
313
+ if (unknownMethods.length > 0) {
314
+ throw createInvalidRetryPolicyError(
315
+ `Unknown HTTP retry method(s): ${unknownMethods.join(", ")}`,
316
+ );
317
+ }
318
+ }
319
+ if (retry.statusCodes !== undefined) {
320
+ if (!Array.isArray(retry.statusCodes)) {
321
+ throw createInvalidRetryPolicyError(
322
+ "HTTP retry statusCodes must be an array",
93
323
  );
94
324
  }
325
+ const invalidStatusCodes = retry.statusCodes.filter(
326
+ (status) =>
327
+ !Number.isInteger(status) ||
328
+ Number(status) < 100 ||
329
+ Number(status) > 599,
330
+ );
331
+ if (invalidStatusCodes.length > 0) {
332
+ throw createInvalidRetryPolicyError(
333
+ "HTTP retry statusCodes must contain HTTP status integers in [100, 599]",
334
+ );
335
+ }
336
+ }
337
+ if (retry.errorCodes !== undefined) {
338
+ if (!Array.isArray(retry.errorCodes)) {
339
+ throw createInvalidRetryPolicyError(
340
+ "HTTP retry errorCodes must be an array",
341
+ );
342
+ }
343
+ const nonStringErrorCodes = retry.errorCodes.filter(
344
+ (errorCode) => typeof errorCode !== "string",
345
+ );
346
+ if (nonStringErrorCodes.length > 0) {
347
+ throw createInvalidRetryPolicyError(
348
+ "HTTP retry errorCodes must contain only strings",
349
+ );
350
+ }
351
+ }
352
+ if (
353
+ retry.preset === HttpRetryPreset.Off &&
354
+ ((retry.attempts !== undefined && retry.attempts > 1) ||
355
+ (retry.statusCodes !== undefined && retry.statusCodes.length > 0))
356
+ ) {
357
+ throw createInvalidRetryPolicyError(
358
+ "HTTP retry preset off cannot be combined with retry-enabling overrides",
359
+ );
360
+ }
361
+ }
362
+
363
+ function validateUnsafeRetryMethods(options: NormalizedRetryOptions): void {
364
+ if (
365
+ options.unsafeMethodPolicy ===
366
+ HttpRetryUnsafeMethodPolicy.AllowExplicitUnsafe
367
+ ) {
368
+ return;
369
+ }
370
+ const unsafeMethods = options.methods.filter((method) =>
371
+ UNSAFE_RETRY_METHODS.has(method.toUpperCase()),
372
+ );
373
+ if (unsafeMethods.length === 0) return;
374
+
375
+ throw new ProviderError(
376
+ `HTTP retry methods include unsafe method(s): ${unsafeMethods.join(", ")}`,
377
+ { code: "retry_unsafe_method" },
378
+ );
379
+ }
380
+
381
+ function isMethodRetryable(
382
+ method: HttpMethod,
383
+ options: NormalizedRetryOptions,
384
+ ): boolean {
385
+ return options.methods
386
+ .map((allowedMethod) => allowedMethod.toUpperCase())
387
+ .includes(method.toUpperCase());
388
+ }
389
+
390
+ function retryErrorCode(error: unknown): string | undefined {
391
+ if (error instanceof TransportError) {
392
+ return error.code;
393
+ }
394
+ if (error && typeof error === "object" && "code" in error) {
395
+ const code = Reflect.get(error, "code");
396
+ return typeof code === "string" ? code : undefined;
397
+ }
398
+ return undefined;
399
+ }
95
400
 
96
- const contentType = response.headers.get("content-type") ?? "";
97
- const rawText = await response.text();
98
- let data: unknown;
401
+ function retryErrorStatus(error: unknown): number | undefined {
402
+ if (error instanceof TransportError) {
403
+ return error.status ?? error.upstreamStatus;
404
+ }
405
+ return undefined;
406
+ }
99
407
 
100
- if (contentType.includes("application/json")) {
101
- data = rawText ? (JSON.parse(rawText) as unknown) : null;
102
- } else {
103
- data = rawText;
408
+ function shouldRetryTransportError(
409
+ error: unknown,
410
+ options: NormalizedRetryOptions,
411
+ ): boolean {
412
+ const code = retryErrorCode(error);
413
+ return Boolean(code && options.errorCodes.includes(code));
414
+ }
415
+
416
+ function retryAfterHeader(headers: Record<string, string>): string | undefined {
417
+ for (const [name, value] of Object.entries(headers)) {
418
+ if (name.toLowerCase() === "retry-after") return value;
419
+ }
420
+ return undefined;
421
+ }
422
+
423
+ function parseRetryAfterMs(
424
+ headers: Record<string, string>,
425
+ now: number = Date.now(),
426
+ ): number | undefined {
427
+ const value = retryAfterHeader(headers);
428
+ if (!value) return undefined;
429
+ const seconds = Number(value);
430
+ if (Number.isFinite(seconds)) {
431
+ return Math.max(0, Math.floor(seconds * 1_000));
432
+ }
433
+ const dateMs = Date.parse(value);
434
+ if (!Number.isNaN(dateMs)) {
435
+ return Math.max(0, dateMs - now);
436
+ }
437
+ return undefined;
438
+ }
439
+
440
+ function computeRetryDelayMs(
441
+ options: NormalizedRetryOptions,
442
+ attemptIndex: number,
443
+ headers?: Record<string, string>,
444
+ ): number {
445
+ const multiplier =
446
+ options.delayStrategy === HttpRetryDelayStrategy.Exponential
447
+ ? 2 ** Math.max(0, attemptIndex - 1)
448
+ : 1;
449
+ const configuredDelay = Math.min(
450
+ options.baseDelayMs * multiplier,
451
+ options.maxDelayMs,
452
+ );
453
+ const retryAfterMs =
454
+ options.retryAfter === HttpRetryAfterPolicy.Ignore
455
+ ? undefined
456
+ : headers
457
+ ? parseRetryAfterMs(headers)
458
+ : undefined;
459
+ if (retryAfterMs !== undefined) {
460
+ const boundedRetryAfterMs = Math.min(retryAfterMs, options.maxDelayMs);
461
+ if (options.retryAfter === HttpRetryAfterPolicy.Cap) {
462
+ return Math.min(boundedRetryAfterMs, configuredDelay);
104
463
  }
464
+ return boundedRetryAfterMs;
465
+ }
105
466
 
106
- return {
107
- data,
108
- headers: Object.fromEntries(response.headers.entries()),
109
- json: async <T = unknown>() =>
110
- contentType.includes("application/json")
111
- ? (data as T)
112
- : (JSON.parse(rawText) as T),
113
- ok: response.ok,
114
- status: response.status,
115
- text: async () => rawText,
116
- };
117
- } catch (error) {
118
- if (error instanceof TransportError) {
119
- throw error;
467
+ switch (options.jitter) {
468
+ case HttpRetryJitter.None:
469
+ return configuredDelay;
470
+ case HttpRetryJitter.Equal:
471
+ return Math.floor(
472
+ configuredDelay / 2 + Math.random() * (configuredDelay / 2),
473
+ );
474
+ case HttpRetryJitter.Full:
475
+ return Math.floor(Math.random() * configuredDelay);
476
+ }
477
+ }
478
+
479
+ async function sleep(ms: number): Promise<void> {
480
+ if (ms <= 0) return;
481
+ await new Promise((resolve) => setTimeout(resolve, ms));
482
+ }
483
+
484
+ function toUpstreamHttpError(status: number): TransportError {
485
+ return new TransportError(`Upstream request failed with status ${status}`, {
486
+ code: "upstream_http_error",
487
+ status,
488
+ upstreamStatus: status,
489
+ });
490
+ }
491
+
492
+ function hasHeader(headers: Record<string, string>, name: string): boolean {
493
+ const needle = name.toLowerCase();
494
+ return Object.keys(headers).some((key) => key.toLowerCase() === needle);
495
+ }
496
+
497
+ function withClientHeaders(
498
+ options: RequestOptions | undefined,
499
+ clientOptions: HttpClientOptions,
500
+ body: unknown,
501
+ ): RequestOptions {
502
+ const headers: Record<string, string> = {
503
+ ...(clientOptions.userAgent
504
+ ? { "User-Agent": clientOptions.userAgent }
505
+ : {}),
506
+ ...options?.headers,
507
+ };
508
+
509
+ if (body !== undefined && !hasHeader(headers, "Content-Type")) {
510
+ headers["Content-Type"] = "application/json";
511
+ }
512
+
513
+ return {
514
+ ...options,
515
+ headers,
516
+ };
517
+ }
518
+
519
+ function parseHttpData(body: string, headers: Record<string, string>): unknown {
520
+ const contentType =
521
+ headers["content-type"] ??
522
+ headers["Content-Type"] ??
523
+ headers["CONTENT-TYPE"];
524
+
525
+ if (contentType?.includes("application/json")) {
526
+ return body ? JSON.parse(body) : null;
527
+ }
528
+
529
+ return body;
530
+ }
531
+
532
+ function parseJson<T = unknown>(body: string): T {
533
+ return JSON.parse(body);
534
+ }
535
+
536
+ function isTimeoutMessage(message: string): boolean {
537
+ return /\b(timed out|timeout|deadline exceeded)\b/i.test(message);
538
+ }
539
+
540
+ function toHttpTransportError(error: unknown): TransportError {
541
+ if (error instanceof TransportError) {
542
+ if (error.code) {
543
+ return error;
120
544
  }
121
545
 
122
- if (error instanceof Error && error.name === "AbortError") {
123
- throw new TransportError("Request timed out", {
546
+ if (isTimeoutMessage(error.message)) {
547
+ return new TransportError("Request timed out", {
124
548
  code: "transport_timeout",
549
+ status: error.status ?? 0,
550
+ cause: error,
125
551
  });
126
552
  }
127
553
 
128
- throw new TransportError("Network error", {
129
- code: "transport_network_error",
130
- cause: error instanceof Error ? error : undefined,
131
- });
132
- } finally {
133
- if (timeoutId) {
134
- clearTimeout(timeoutId);
554
+ if ((error.status ?? 0) === 0) {
555
+ return new TransportError(error.message || "Network error", {
556
+ code: "transport_network_error",
557
+ status: 0,
558
+ cause: error,
559
+ });
135
560
  }
561
+
562
+ return error;
136
563
  }
137
- }
138
564
 
139
- async function drainFetchResponse(response: Response): Promise<void> {
140
- const reader = response.body?.getReader();
141
- if (!reader) {
142
- return;
565
+ if (error instanceof Error) {
566
+ const timeout =
567
+ error.name === "AbortError" ||
568
+ error.name === "TimeoutError" ||
569
+ isTimeoutMessage(error.message);
570
+ return new TransportError(timeout ? "Request timed out" : "Network error", {
571
+ code: timeout ? "transport_timeout" : "transport_network_error",
572
+ status: 0,
573
+ cause: error,
574
+ });
143
575
  }
144
576
 
577
+ return new TransportError("Network error", {
578
+ code: "transport_network_error",
579
+ status: 0,
580
+ });
581
+ }
582
+
583
+ async function toNativeHttpResponse(response: Response): Promise<HttpResponse> {
584
+ const headers = Object.fromEntries(response.headers.entries());
585
+ const rawText = await response.text();
586
+ const data = parseHttpData(rawText, headers);
587
+
588
+ return {
589
+ data,
590
+ headers,
591
+ json: async <T = unknown>() => {
592
+ const contentType =
593
+ headers["content-type"] ??
594
+ headers["Content-Type"] ??
595
+ headers["CONTENT-TYPE"];
596
+ return parseJson<T>(
597
+ contentType?.includes("application/json") && !rawText
598
+ ? "null"
599
+ : rawText,
600
+ );
601
+ },
602
+ ok: response.status >= 200 && response.status < 300,
603
+ status: response.status,
604
+ text: async () => rawText,
605
+ };
606
+ }
607
+
608
+ async function drainNativeResponseBody(response: Response): Promise<void> {
145
609
  try {
146
- while (true) {
147
- const { done } = await reader.read();
148
- if (done) {
149
- return;
150
- }
151
- }
610
+ await response.arrayBuffer();
152
611
  } catch {
153
- // Best-effort drain for transport reuse only; callers still receive the
154
- // sanitized upstream error below.
155
- } finally {
156
- reader.releaseLock();
612
+ await response.body?.cancel().catch(() => undefined);
613
+ }
614
+ }
615
+
616
+ function requireNativeResponseBody(
617
+ response: Response,
618
+ ): ReadableStream<Uint8Array> {
619
+ if (!response.body) {
620
+ throw new TransportError("Response body stream is unavailable", {
621
+ code: "transport_stream_unavailable",
622
+ status: response.status,
623
+ });
624
+ }
625
+ return response.body;
626
+ }
627
+
628
+ function toNativeHttpStreamResponse(response: Response): HttpStreamResponse {
629
+ const headers = Object.fromEntries(response.headers.entries());
630
+ const body = requireNativeResponseBody(response);
631
+ return {
632
+ body,
633
+ headers,
634
+ ok: response.status >= 200 && response.status < 300,
635
+ status: response.status,
636
+ bytes: () => readableBytes(body),
637
+ textChunks: () => readableTextChunks(body),
638
+ lines: () => readableLines(body),
639
+ };
640
+ }
641
+
642
+ function normalizeHttpMethod(method: string): HttpMethod {
643
+ switch (method.toUpperCase()) {
644
+ case "HEAD":
645
+ return "HEAD";
646
+ case "GET":
647
+ return "GET";
648
+ case "POST":
649
+ return "POST";
650
+ case "PUT":
651
+ return "PUT";
652
+ case "DELETE":
653
+ return "DELETE";
654
+ case "OPTIONS":
655
+ return "OPTIONS";
656
+ case "TRACE":
657
+ return "TRACE";
658
+ case "PATCH":
659
+ return "PATCH";
660
+ default:
661
+ throw new TransportError(`Unsupported HTTP method: ${method}`, {
662
+ code: "transport_invalid_method",
663
+ });
157
664
  }
158
665
  }
159
666
 
160
- function createProxyInit(
161
- proxy?: string,
162
- ): Pick<FetchProxyInit, "dispatcher" | "proxy"> {
163
- if (!proxy) {
164
- return {};
667
+ function isAbsoluteUrl(url: string): boolean {
668
+ return /^[a-z][a-z\d+\-.]*:/i.test(url);
669
+ }
670
+
671
+ function resolveHttpUrl(baseUrl: string | undefined, url: string): string {
672
+ return new URL(url, baseUrl ?? DEFAULT_HTTP_BASE_URL).toString();
673
+ }
674
+
675
+ type NativeFetchInit = RequestInit & { proxy?: string };
676
+
677
+ async function resolveNativeProxy(
678
+ options: RequestOptions,
679
+ clientOptions: HttpClientOptions,
680
+ warn: (message: string) => void,
681
+ ): Promise<string | undefined> {
682
+ const resolvedProxy = await resolveProxyConfigAsync({
683
+ proxy: options.proxy ?? clientOptions.proxy,
684
+ upstream: clientOptions.upstream,
685
+ apifuseConfig: clientOptions.apifuseConfig,
686
+ affinityKey: clientOptions.affinityKey,
687
+ telemetry: clientOptions.telemetry,
688
+ });
689
+ if (resolvedProxy.shouldWarn) {
690
+ warn(
691
+ "[provider-sdk] Provider requested proxy routing, but no proxy URL was configured. Continuing without proxy.",
692
+ );
165
693
  }
694
+ return resolvedProxy.url;
695
+ }
166
696
 
167
- const bunRuntime = Object.getOwnPropertyDescriptor(globalThis, "Bun")?.value;
697
+ function assertNoHttpTransportOverrides(options: RequestOptions): void {
698
+ if ("profile" in options || "stealth" in options) {
699
+ throw new ProviderError(
700
+ "ctx.http does not accept stealth transport options. Use ctx.stealth.fetch() for browser-like impersonation.",
701
+ { code: "http_transport_override_unsupported" },
702
+ );
703
+ }
704
+ }
168
705
 
169
- if (bunRuntime !== undefined) {
170
- return { proxy };
706
+ function normalizeNativeFetchBody(
707
+ body: unknown,
708
+ ): string | ArrayBuffer | undefined {
709
+ const normalized = normalizeHttpRequestBody(body);
710
+ if (!Buffer.isBuffer(normalized)) {
711
+ return normalized;
171
712
  }
713
+ const copied = new Uint8Array(normalized.byteLength);
714
+ copied.set(normalized);
715
+ return copied.buffer;
716
+ }
717
+
718
+ async function fetchNativeHttp(
719
+ baseUrl: string | undefined,
720
+ url: string,
721
+ method: HttpMethod,
722
+ options: RequestOptions & { body?: unknown },
723
+ clientOptions: HttpClientOptions,
724
+ warn: (message: string) => void,
725
+ statusRetryCodes?: readonly number[],
726
+ ): Promise<HttpResponse | HttpStatusOutcome> {
727
+ const requestUrl = appendQueryParams(
728
+ resolveHttpUrl(baseUrl, url),
729
+ options.params,
730
+ );
731
+ const controller = options.timeout ? new AbortController() : undefined;
732
+ const timeoutHandle = options.timeout
733
+ ? setTimeout(() => controller?.abort(), options.timeout)
734
+ : undefined;
172
735
 
173
736
  try {
174
- const undici = require("undici") as UndiciModule;
175
- if (typeof undici.ProxyAgent === "function") {
737
+ const proxy = await resolveNativeProxy(options, clientOptions, warn);
738
+ const requestInit: NativeFetchInit = {
739
+ headers: options.headers,
740
+ method,
741
+ ...(proxy ? { proxy } : {}),
742
+ signal: controller?.signal,
743
+ };
744
+ if (options.body !== undefined) {
745
+ requestInit.body = normalizeNativeFetchBody(options.body);
746
+ }
747
+ const response = await fetch(requestUrl, {
748
+ ...requestInit,
749
+ });
750
+ const headers = Object.fromEntries(response.headers.entries());
751
+
752
+ if (statusRetryCodes && response.status >= 400) {
753
+ await drainNativeResponseBody(response);
176
754
  return {
177
- dispatcher: new undici.ProxyAgent(
178
- proxy,
179
- ) as FetchProxyInit["dispatcher"],
755
+ kind: "http-status",
756
+ status: response.status,
757
+ headers,
758
+ retryable: statusRetryCodes.includes(response.status),
180
759
  };
181
760
  }
182
- } catch {
183
- // Fall through to a generic fetch init extension when undici is unavailable.
761
+
762
+ if (response.status >= 400 && options.throwOnHttpError !== false) {
763
+ await drainNativeResponseBody(response);
764
+ throw new TransportError(
765
+ `Upstream request failed with status ${response.status}`,
766
+ {
767
+ code: "upstream_http_error",
768
+ status: response.status,
769
+ },
770
+ );
771
+ }
772
+
773
+ return toNativeHttpResponse(response);
774
+ } catch (error) {
775
+ if (error instanceof SyntaxError) {
776
+ throw error;
777
+ }
778
+ throw toHttpTransportError(error);
779
+ } finally {
780
+ if (timeoutHandle) clearTimeout(timeoutHandle);
184
781
  }
782
+ }
783
+
784
+ async function fetchNativeHttpStream(
785
+ baseUrl: string | undefined,
786
+ url: string,
787
+ method: HttpMethod,
788
+ options: RequestOptions & { body?: unknown },
789
+ clientOptions: HttpClientOptions,
790
+ warn: (message: string) => void,
791
+ ): Promise<HttpStreamResponse> {
792
+ const requestUrl = appendQueryParams(
793
+ resolveHttpUrl(baseUrl, url),
794
+ options.params,
795
+ );
796
+ const controller = options.timeout ? new AbortController() : undefined;
797
+ const timeoutHandle = options.timeout
798
+ ? setTimeout(() => controller?.abort(), options.timeout)
799
+ : undefined;
800
+
801
+ try {
802
+ const proxy = await resolveNativeProxy(options, clientOptions, warn);
803
+ const requestInit: NativeFetchInit = {
804
+ headers: options.headers,
805
+ method,
806
+ ...(proxy ? { proxy } : {}),
807
+ signal: controller?.signal,
808
+ };
809
+ if (options.body !== undefined) {
810
+ requestInit.body = normalizeNativeFetchBody(options.body);
811
+ }
812
+ const response = await fetch(requestUrl, {
813
+ ...requestInit,
814
+ });
185
815
 
186
- return { proxy };
816
+ if (response.status >= 400 && options.throwOnHttpError !== false) {
817
+ await drainNativeResponseBody(response);
818
+ throw new TransportError(
819
+ `Upstream request failed with status ${response.status}`,
820
+ {
821
+ code: "upstream_http_error",
822
+ status: response.status,
823
+ },
824
+ );
825
+ }
826
+
827
+ return toNativeHttpStreamResponse(response);
828
+ } catch (error) {
829
+ if (error instanceof SyntaxError) {
830
+ throw error;
831
+ }
832
+ throw toHttpTransportError(error);
833
+ } finally {
834
+ if (timeoutHandle) clearTimeout(timeoutHandle);
835
+ }
187
836
  }
188
837
 
189
838
  export function createHttpClient(
190
839
  baseUrl?: string,
191
840
  clientOptions: HttpClientOptions = {},
192
841
  ): HttpClient {
193
- let hasWarnedMissingProxy = false;
842
+ const warnedMessages = new Set<string>();
194
843
  const warn = clientOptions.warn ?? console.warn;
195
- const stealthProfile = clientOptions.stealthProfile;
196
- const userAgent = clientOptions.userAgent ?? stealthProfile?.userAgent;
197
-
198
- function withClientHeaders(
199
- options?: RequestOptions,
200
- ): RequestOptions | undefined {
201
- const layer2Headers = stealthProfile
202
- ? generateLayer2Headers(stealthProfile)
203
- : undefined;
204
-
205
- if (!userAgent && !layer2Headers) {
206
- return options;
844
+ const warnOnce = (message: string) => {
845
+ if (warnedMessages.has(message)) {
846
+ return;
207
847
  }
848
+ warnedMessages.add(message);
849
+ warn(message);
850
+ };
208
851
 
209
- return {
210
- ...options,
211
- headers: {
212
- ...(userAgent ? { "User-Agent": userAgent } : {}),
213
- ...layer2Headers,
214
- ...options?.headers,
215
- },
216
- };
217
- }
852
+ async function request(
853
+ url: string,
854
+ method: string,
855
+ options: RequestOptions & { body?: unknown } = {},
856
+ ): Promise<HttpResponse> {
857
+ if (!baseUrl && !isAbsoluteUrl(url)) {
858
+ throw new TransportError(
859
+ "ctx.http requires an absolute URL when provider.upstream.baseUrl is not declared",
860
+ { code: "transport_invalid_url" },
861
+ );
862
+ }
863
+ assertNoHttpTransportOverrides(options);
864
+ const headersOptions = withClientHeaders(
865
+ options,
866
+ clientOptions,
867
+ options.body,
868
+ );
869
+ const methodName = normalizeHttpMethod(method);
870
+ const retryOptions = normalizeRetryOptions(headersOptions.retry);
871
+ if (retryOptions) validateUnsafeRetryMethods(retryOptions);
872
+ const retryEnabled = Boolean(
873
+ retryOptions &&
874
+ retryOptions.attempts > 1 &&
875
+ isMethodRetryable(methodName, retryOptions),
876
+ );
877
+ const statusRetryEnabled = Boolean(
878
+ retryEnabled &&
879
+ retryOptions &&
880
+ retryOptions.statusCodes.length > 0 &&
881
+ headersOptions.throwOnHttpError !== false,
882
+ );
883
+ const attemptOptions: RequestOptions & { body?: unknown } =
884
+ statusRetryEnabled
885
+ ? { ...headersOptions, throwOnHttpError: false }
886
+ : headersOptions;
887
+
888
+ const executeOnce = (): Promise<HttpResponse | HttpStatusOutcome> =>
889
+ fetchNativeHttp(
890
+ baseUrl,
891
+ url,
892
+ methodName,
893
+ attemptOptions,
894
+ clientOptions,
895
+ warnOnce,
896
+ statusRetryEnabled ? retryOptions?.statusCodes : undefined,
897
+ );
218
898
 
219
- function resolveRequestProxy(
220
- requestOptions?: RequestOptions,
221
- ): string | undefined {
222
- const resolvedProxy = resolveProxyConfig({
223
- proxy: requestOptions?.proxy ?? clientOptions.proxy,
224
- upstream: clientOptions.upstream,
225
- apifuseConfig: clientOptions.apifuseConfig,
226
- });
899
+ if (!retryEnabled || !retryOptions) {
900
+ const outcome = await executeOnce();
901
+ if (isHttpStatusOutcome(outcome)) {
902
+ throw toUpstreamHttpError(outcome.status);
903
+ }
904
+ return outcome;
905
+ }
227
906
 
228
- if (resolvedProxy.shouldWarn && !hasWarnedMissingProxy) {
229
- hasWarnedMissingProxy = true;
230
- warn(MISSING_PROXY_WARNING);
907
+ let lastErrorCode: string | undefined;
908
+ let lastStatus: number | undefined;
909
+ for (let attempt = 1; attempt <= retryOptions.attempts; attempt += 1) {
910
+ try {
911
+ const outcome = await executeOnce();
912
+ if (isHttpStatusOutcome(outcome)) {
913
+ lastStatus = outcome.status;
914
+ if (outcome.retryable && attempt < retryOptions.attempts) {
915
+ await sleep(
916
+ computeRetryDelayMs(retryOptions, attempt, outcome.headers),
917
+ );
918
+ continue;
919
+ }
920
+ throw toUpstreamHttpError(outcome.status);
921
+ }
922
+
923
+ const response = outcome;
924
+ if (
925
+ response.status >= 400 &&
926
+ headersOptions.throwOnHttpError !== false
927
+ ) {
928
+ throw toUpstreamHttpError(response.status);
929
+ }
930
+
931
+ if (attempt > 1) {
932
+ const summary: HttpRetrySummary = {
933
+ attempts: attempt,
934
+ retries: attempt - 1,
935
+ ...(retryOptions.preset ? { preset: retryOptions.preset } : {}),
936
+ transport: "native",
937
+ ...(lastErrorCode ? { lastErrorCode } : {}),
938
+ ...(lastStatus ? { lastStatus } : {}),
939
+ };
940
+ clientOptions.onRetrySummary?.(summary);
941
+ }
942
+ return response;
943
+ } catch (error) {
944
+ lastErrorCode = retryErrorCode(error);
945
+ lastStatus = retryErrorStatus(error);
946
+ if (
947
+ attempt < retryOptions.attempts &&
948
+ shouldRetryTransportError(error, retryOptions)
949
+ ) {
950
+ await sleep(computeRetryDelayMs(retryOptions, attempt));
951
+ continue;
952
+ }
953
+ throw error;
954
+ }
231
955
  }
232
956
 
233
- return resolvedProxy.url;
957
+ throw new TransportError("HTTP retry exhausted without a terminal result", {
958
+ code: "retry_exhausted",
959
+ });
960
+ }
961
+
962
+ async function streamRequest(
963
+ url: string,
964
+ method: string,
965
+ options: RequestOptions & { body?: unknown } = {},
966
+ ): Promise<HttpStreamResponse> {
967
+ if (!baseUrl && !isAbsoluteUrl(url)) {
968
+ throw new TransportError(
969
+ "ctx.http requires an absolute URL when provider.upstream.baseUrl is not declared",
970
+ { code: "transport_invalid_url" },
971
+ );
972
+ }
973
+ assertNoHttpTransportOverrides(options);
974
+ const headersOptions = withClientHeaders(
975
+ options,
976
+ clientOptions,
977
+ options.body,
978
+ );
979
+ const methodName = normalizeHttpMethod(method);
980
+ return fetchNativeHttpStream(
981
+ baseUrl,
982
+ url,
983
+ methodName,
984
+ headersOptions,
985
+ clientOptions,
986
+ warnOnce,
987
+ );
234
988
  }
235
989
 
236
990
  return {
237
- request: (url, options: RequestWithMethodOptions = {}) =>
238
- doRequest(
239
- baseUrl,
240
- url,
241
- options.method ?? "GET",
242
- resolveRequestProxy(options),
243
- withClientHeaders(options),
244
- ),
245
- get: (url, options) =>
246
- doRequest(
247
- baseUrl,
248
- url,
249
- "GET",
250
- resolveRequestProxy(options),
251
- withClientHeaders(options),
252
- ),
253
- post: (url, body, options) =>
254
- doRequest(baseUrl, url, "POST", resolveRequestProxy(options), {
255
- ...withClientHeaders(options),
256
- body,
257
- }),
258
- put: (url, body, options) =>
259
- doRequest(baseUrl, url, "PUT", resolveRequestProxy(options), {
260
- ...withClientHeaders(options),
261
- body,
262
- }),
263
- delete: (url, options) =>
264
- doRequest(
265
- baseUrl,
266
- url,
267
- "DELETE",
268
- resolveRequestProxy(options),
269
- withClientHeaders(options),
270
- ),
991
+ request: async (url, options: RequestWithMethodOptions = {}) =>
992
+ request(url, options.method ?? "GET", options),
993
+ get: async (url, options) => request(url, "GET", options),
994
+ post: async (url, body, options) =>
995
+ request(url, "POST", { ...options, body }),
996
+ put: async (url, body, options) =>
997
+ request(url, "PUT", { ...options, body }),
998
+ delete: async (url, options) => request(url, "DELETE", options),
999
+ stream: async (url, options: RequestWithMethodOptions = {}) =>
1000
+ streamRequest(url, options.method ?? "GET", options),
1001
+ sse: async (
1002
+ url,
1003
+ options: RequestWithMethodOptions = {},
1004
+ ): Promise<AsyncIterable<SseMessage>> => {
1005
+ const headers = {
1006
+ Accept: "text/event-stream",
1007
+ ...options.headers,
1008
+ };
1009
+ const response = await streamRequest(url, options.method ?? "GET", {
1010
+ ...options,
1011
+ headers,
1012
+ });
1013
+ return parseSseStream(response.body);
1014
+ },
271
1015
  };
272
1016
  }