@ai-sdk/provider-utils 3.0.30 → 3.0.32

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/dist/index.mjs CHANGED
@@ -221,6 +221,19 @@ function validateDownloadUrl(url) {
221
221
  return;
222
222
  }
223
223
  }
224
+ function validateDownloadAddress({
225
+ address,
226
+ family,
227
+ hostname
228
+ }) {
229
+ const isUnsafe = family === 4 ? !isIPv4(address) || isPrivateIPv4(address) : family === 6 ? isPrivateIPv6(address) : true;
230
+ if (isUnsafe) {
231
+ throw new DownloadError({
232
+ url: hostname,
233
+ message: `Hostname ${hostname} resolved to disallowed IP address ${address}`
234
+ });
235
+ }
236
+ }
224
237
  function isIPv4(hostname) {
225
238
  const parts = hostname.split(".");
226
239
  if (parts.length !== 4) return false;
@@ -307,6 +320,104 @@ function isPrivateIPv6(ip) {
307
320
  return false;
308
321
  }
309
322
 
323
+ // src/safe-node-fetch.ts
324
+ function createSafeLookup(lookup) {
325
+ return ((hostname, options, callback) => {
326
+ lookup(hostname, { ...options, all: true }, (error, addresses) => {
327
+ if (error) {
328
+ callback(error);
329
+ return;
330
+ }
331
+ try {
332
+ const [firstAddress] = addresses;
333
+ if (firstAddress == null) {
334
+ throw new Error(`Hostname ${hostname} did not resolve to an address`);
335
+ }
336
+ for (const { address, family } of addresses) {
337
+ validateDownloadAddress({ address, family, hostname });
338
+ }
339
+ if (options.all === true) {
340
+ callback(null, addresses);
341
+ } else {
342
+ callback(
343
+ null,
344
+ firstAddress.address,
345
+ firstAddress.family
346
+ );
347
+ }
348
+ } catch (error2) {
349
+ callback(
350
+ error2 instanceof Error ? error2 : new Error(String(error2))
351
+ );
352
+ }
353
+ });
354
+ });
355
+ }
356
+ var safeNodeFetchPromise;
357
+ var initialGlobalFetch = globalThis.fetch;
358
+ var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
359
+ function isNodeRuntime() {
360
+ var _a2, _b2;
361
+ const runtimeProcess = globalThis.process;
362
+ return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null;
363
+ }
364
+ async function getDefaultDownloadFetch() {
365
+ if (!isNodeRuntime() || !initialGlobalFetchIsNodeDefault || globalThis.fetch !== initialGlobalFetch) {
366
+ return globalThis.fetch;
367
+ }
368
+ return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
369
+ }
370
+ function isNodeDefaultFetch(fetch) {
371
+ const source = Function.prototype.toString.call(fetch);
372
+ return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
373
+ }
374
+ async function createSafeNodeFetch() {
375
+ const [{ createRequire }, { lookup }] = await Promise.all([
376
+ loadNodeModule("node:module"),
377
+ loadNodeModule("node:dns")
378
+ ]);
379
+ const { Agent, fetch } = createRequire(getCurrentModulePath())(
380
+ "undici"
381
+ );
382
+ const dispatcher = new Agent({
383
+ connect: {
384
+ lookup: createSafeLookup(lookup)
385
+ }
386
+ });
387
+ return ((input, init) => fetch(
388
+ input,
389
+ {
390
+ ...init,
391
+ dispatcher
392
+ }
393
+ ));
394
+ }
395
+ async function loadNodeModule(id) {
396
+ var _a2;
397
+ const processWithBuiltins = globalThis.process;
398
+ const builtinModule = (_a2 = processWithBuiltins == null ? void 0 : processWithBuiltins.getBuiltinModule) == null ? void 0 : _a2.call(processWithBuiltins, id);
399
+ return builtinModule == null ? await importNodeModule(id) : builtinModule;
400
+ }
401
+ function importNodeModule(id) {
402
+ return import(id);
403
+ }
404
+ function getCurrentModulePath() {
405
+ const originalPrepareStackTrace = Error.prepareStackTrace;
406
+ try {
407
+ Error.prepareStackTrace = (_error, callSites) => callSites;
408
+ const error = new Error("Capture current module path");
409
+ Error.captureStackTrace(error, getCurrentModulePath);
410
+ const [caller] = error.stack;
411
+ const fileName = caller == null ? void 0 : caller.getFileName();
412
+ if (fileName == null) {
413
+ throw new Error("Unable to determine the current module path");
414
+ }
415
+ return fileName;
416
+ } finally {
417
+ Error.prepareStackTrace = originalPrepareStackTrace;
418
+ }
419
+ }
420
+
310
421
  // src/fetch-with-validated-redirects.ts
311
422
  var MAX_DOWNLOAD_REDIRECTS = 10;
312
423
  async function fetchWithValidatedRedirects({
@@ -322,6 +433,7 @@ async function fetchWithValidatedRedirects({
322
433
  let currentUrl = url;
323
434
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
324
435
  validateDownloadUrl(currentUrl);
436
+ const fetch = await getDefaultDownloadFetch();
325
437
  const response = await fetch(currentUrl, {
326
438
  ...baseInit,
327
439
  redirect: "manual"
@@ -392,6 +504,7 @@ async function readResponseWithSizeLimit({
392
504
  } finally {
393
505
  try {
394
506
  await reader.cancel();
507
+ } catch (e) {
395
508
  } finally {
396
509
  reader.releaseLock();
397
510
  }
@@ -539,7 +652,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
539
652
  }
540
653
 
541
654
  // src/version.ts
542
- var VERSION = true ? "3.0.30" : "0.0.0-test";
655
+ var VERSION = true ? "3.0.32" : "0.0.0-test";
543
656
 
544
657
  // src/get-from-api.ts
545
658
  var getOriginalFetch = () => globalThis.fetch;
@@ -549,10 +662,10 @@ var getFromApi = async ({
549
662
  successfulResponseHandler,
550
663
  failedResponseHandler,
551
664
  abortSignal,
552
- fetch: fetch2 = getOriginalFetch()
665
+ fetch = getOriginalFetch()
553
666
  }) => {
554
667
  try {
555
- const response = await fetch2(url, {
668
+ const response = await fetch(url, {
556
669
  method: "GET",
557
670
  headers: withUserAgentSuffix(
558
671
  headers,
@@ -1003,7 +1116,7 @@ var postJsonToApi = async ({
1003
1116
  failedResponseHandler,
1004
1117
  successfulResponseHandler,
1005
1118
  abortSignal,
1006
- fetch: fetch2
1119
+ fetch
1007
1120
  }) => postToApi({
1008
1121
  url,
1009
1122
  headers: {
@@ -1017,7 +1130,7 @@ var postJsonToApi = async ({
1017
1130
  failedResponseHandler,
1018
1131
  successfulResponseHandler,
1019
1132
  abortSignal,
1020
- fetch: fetch2
1133
+ fetch
1021
1134
  });
1022
1135
  var postFormDataToApi = async ({
1023
1136
  url,
@@ -1026,7 +1139,7 @@ var postFormDataToApi = async ({
1026
1139
  failedResponseHandler,
1027
1140
  successfulResponseHandler,
1028
1141
  abortSignal,
1029
- fetch: fetch2
1142
+ fetch
1030
1143
  }) => postToApi({
1031
1144
  url,
1032
1145
  headers,
@@ -1037,7 +1150,7 @@ var postFormDataToApi = async ({
1037
1150
  failedResponseHandler,
1038
1151
  successfulResponseHandler,
1039
1152
  abortSignal,
1040
- fetch: fetch2
1153
+ fetch
1041
1154
  });
1042
1155
  var postToApi = async ({
1043
1156
  url,
@@ -1046,10 +1159,10 @@ var postToApi = async ({
1046
1159
  successfulResponseHandler,
1047
1160
  failedResponseHandler,
1048
1161
  abortSignal,
1049
- fetch: fetch2 = getOriginalFetch2()
1162
+ fetch = getOriginalFetch2()
1050
1163
  }) => {
1051
1164
  try {
1052
- const response = await fetch2(url, {
1165
+ const response = await fetch(url, {
1053
1166
  method: "POST",
1054
1167
  headers: withUserAgentSuffix(
1055
1168
  headers,