@ai-sdk/provider-utils 4.0.40 → 4.0.42

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
@@ -300,6 +300,19 @@ function validateDownloadUrl(url) {
300
300
  return;
301
301
  }
302
302
  }
303
+ function validateDownloadAddress({
304
+ address,
305
+ family,
306
+ hostname
307
+ }) {
308
+ const isUnsafe = family === 4 ? !isIPv4(address) || isPrivateIPv4(address) : family === 6 ? isPrivateIPv6(address) : true;
309
+ if (isUnsafe) {
310
+ throw new DownloadError({
311
+ url: hostname,
312
+ message: `Hostname ${hostname} resolved to disallowed IP address ${address}`
313
+ });
314
+ }
315
+ }
303
316
  function isIPv4(hostname) {
304
317
  const parts = hostname.split(".");
305
318
  if (parts.length !== 4) return false;
@@ -386,6 +399,106 @@ function isPrivateIPv6(ip) {
386
399
  return false;
387
400
  }
388
401
 
402
+ // src/safe-node-fetch.ts
403
+ function createSafeLookup(lookup) {
404
+ return ((hostname, options, callback) => {
405
+ lookup(hostname, { ...options, all: true }, (error, addresses) => {
406
+ if (error) {
407
+ callback(error);
408
+ return;
409
+ }
410
+ try {
411
+ const [firstAddress] = addresses;
412
+ if (firstAddress == null) {
413
+ throw new Error(`Hostname ${hostname} did not resolve to an address`);
414
+ }
415
+ for (const { address, family } of addresses) {
416
+ validateDownloadAddress({ address, family, hostname });
417
+ }
418
+ if (options.all === true) {
419
+ callback(null, addresses);
420
+ } else {
421
+ callback(
422
+ null,
423
+ firstAddress.address,
424
+ firstAddress.family
425
+ );
426
+ }
427
+ } catch (error2) {
428
+ callback(
429
+ error2 instanceof Error ? error2 : new Error(String(error2))
430
+ );
431
+ }
432
+ });
433
+ });
434
+ }
435
+ var safeNodeFetchPromise;
436
+ var initialGlobalFetch = globalThis.fetch;
437
+ var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
438
+ function isNodeRuntime() {
439
+ var _a2, _b2;
440
+ const runtimeProcess = globalThis.process;
441
+ return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null;
442
+ }
443
+ async function getDefaultDownloadFetch() {
444
+ if (!isNodeRuntime() || !initialGlobalFetchIsNodeDefault || globalThis.fetch !== initialGlobalFetch) {
445
+ return globalThis.fetch;
446
+ }
447
+ return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
448
+ }
449
+ function isNodeDefaultFetch(fetch) {
450
+ const source = Function.prototype.toString.call(fetch);
451
+ return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
452
+ }
453
+ async function createSafeNodeFetch() {
454
+ const [{ createRequire }, { lookup }] = await Promise.all([
455
+ loadNodeModule("node:module"),
456
+ loadNodeModule("node:dns")
457
+ ]);
458
+ const { Agent, fetch } = createRequire(getCurrentModulePath())(
459
+ "undici"
460
+ );
461
+ const dispatcher = new Agent({
462
+ connect: {
463
+ lookup: createSafeLookup(lookup)
464
+ }
465
+ });
466
+ return ((input, init) => fetch(
467
+ input,
468
+ {
469
+ ...init,
470
+ dispatcher
471
+ }
472
+ ));
473
+ }
474
+ async function loadNodeModule(id) {
475
+ var _a2;
476
+ const processWithBuiltins = globalThis.process;
477
+ const builtinModule = (_a2 = processWithBuiltins == null ? void 0 : processWithBuiltins.getBuiltinModule) == null ? void 0 : _a2.call(processWithBuiltins, id);
478
+ return builtinModule == null ? await importNodeModule(id) : builtinModule;
479
+ }
480
+ var dynamicImport;
481
+ function importNodeModule(id) {
482
+ dynamicImport != null ? dynamicImport : dynamicImport = Function("specifier", "return import(specifier)");
483
+ return dynamicImport(id);
484
+ }
485
+ function getCurrentModulePath() {
486
+ const originalPrepareStackTrace = Error.prepareStackTrace;
487
+ try {
488
+ Error.prepareStackTrace = (_error, callSites) => callSites;
489
+ const error = new Error("Capture current module path");
490
+ Error.captureStackTrace(error, getCurrentModulePath);
491
+ const [caller] = error.stack;
492
+ const fileName = caller == null ? void 0 : caller.getFileName();
493
+ if (fileName == null) {
494
+ throw new Error("Unable to determine the current module path");
495
+ }
496
+ return fileName;
497
+ } finally {
498
+ Error.prepareStackTrace = originalPrepareStackTrace;
499
+ }
500
+ }
501
+
389
502
  // src/fetch-with-validated-redirects.ts
390
503
  var MAX_DOWNLOAD_REDIRECTS = 10;
391
504
  async function fetchWithValidatedRedirects({
@@ -401,6 +514,7 @@ async function fetchWithValidatedRedirects({
401
514
  let currentUrl = url;
402
515
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
403
516
  validateDownloadUrl(currentUrl);
517
+ const fetch = await getDefaultDownloadFetch();
404
518
  const response = await fetch(currentUrl, {
405
519
  ...baseInit,
406
520
  redirect: "manual"
@@ -677,7 +791,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
677
791
  }
678
792
 
679
793
  // src/version.ts
680
- var VERSION = true ? "4.0.40" : "0.0.0-test";
794
+ var VERSION = true ? "4.0.42" : "0.0.0-test";
681
795
 
682
796
  // src/get-from-api.ts
683
797
  var getOriginalFetch = () => globalThis.fetch;
@@ -687,10 +801,10 @@ var getFromApi = async ({
687
801
  successfulResponseHandler,
688
802
  failedResponseHandler,
689
803
  abortSignal,
690
- fetch: fetch2 = getOriginalFetch()
804
+ fetch = getOriginalFetch()
691
805
  }) => {
692
806
  try {
693
- const response = await fetch2(url, {
807
+ const response = await fetch(url, {
694
808
  method: "GET",
695
809
  headers: withUserAgentSuffix(
696
810
  headers,
@@ -2451,7 +2565,7 @@ var postJsonToApi = async ({
2451
2565
  failedResponseHandler,
2452
2566
  successfulResponseHandler,
2453
2567
  abortSignal,
2454
- fetch: fetch2
2568
+ fetch
2455
2569
  }) => postToApi({
2456
2570
  url,
2457
2571
  headers: {
@@ -2465,7 +2579,7 @@ var postJsonToApi = async ({
2465
2579
  failedResponseHandler,
2466
2580
  successfulResponseHandler,
2467
2581
  abortSignal,
2468
- fetch: fetch2
2582
+ fetch
2469
2583
  });
2470
2584
  var postFormDataToApi = async ({
2471
2585
  url,
@@ -2474,7 +2588,7 @@ var postFormDataToApi = async ({
2474
2588
  failedResponseHandler,
2475
2589
  successfulResponseHandler,
2476
2590
  abortSignal,
2477
- fetch: fetch2
2591
+ fetch
2478
2592
  }) => postToApi({
2479
2593
  url,
2480
2594
  headers,
@@ -2485,7 +2599,7 @@ var postFormDataToApi = async ({
2485
2599
  failedResponseHandler,
2486
2600
  successfulResponseHandler,
2487
2601
  abortSignal,
2488
- fetch: fetch2
2602
+ fetch
2489
2603
  });
2490
2604
  var postToApi = async ({
2491
2605
  url,
@@ -2494,10 +2608,10 @@ var postToApi = async ({
2494
2608
  successfulResponseHandler,
2495
2609
  failedResponseHandler,
2496
2610
  abortSignal,
2497
- fetch: fetch2 = getOriginalFetch2()
2611
+ fetch = getOriginalFetch2()
2498
2612
  }) => {
2499
2613
  try {
2500
- const response = await fetch2(url, {
2614
+ const response = await fetch(url, {
2501
2615
  method: "POST",
2502
2616
  headers: withUserAgentSuffix(
2503
2617
  headers,