@ai-sdk/provider-utils 4.0.39 → 4.0.41

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,104 @@ 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
+ function importNodeModule(id) {
481
+ return import(id);
482
+ }
483
+ function getCurrentModulePath() {
484
+ const originalPrepareStackTrace = Error.prepareStackTrace;
485
+ try {
486
+ Error.prepareStackTrace = (_error, callSites) => callSites;
487
+ const error = new Error("Capture current module path");
488
+ Error.captureStackTrace(error, getCurrentModulePath);
489
+ const [caller] = error.stack;
490
+ const fileName = caller == null ? void 0 : caller.getFileName();
491
+ if (fileName == null) {
492
+ throw new Error("Unable to determine the current module path");
493
+ }
494
+ return fileName;
495
+ } finally {
496
+ Error.prepareStackTrace = originalPrepareStackTrace;
497
+ }
498
+ }
499
+
389
500
  // src/fetch-with-validated-redirects.ts
390
501
  var MAX_DOWNLOAD_REDIRECTS = 10;
391
502
  async function fetchWithValidatedRedirects({
@@ -401,6 +512,7 @@ async function fetchWithValidatedRedirects({
401
512
  let currentUrl = url;
402
513
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
403
514
  validateDownloadUrl(currentUrl);
515
+ const fetch = await getDefaultDownloadFetch();
404
516
  const response = await fetch(currentUrl, {
405
517
  ...baseInit,
406
518
  redirect: "manual"
@@ -677,7 +789,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
677
789
  }
678
790
 
679
791
  // src/version.ts
680
- var VERSION = true ? "4.0.39" : "0.0.0-test";
792
+ var VERSION = true ? "4.0.41" : "0.0.0-test";
681
793
 
682
794
  // src/get-from-api.ts
683
795
  var getOriginalFetch = () => globalThis.fetch;
@@ -687,10 +799,10 @@ var getFromApi = async ({
687
799
  successfulResponseHandler,
688
800
  failedResponseHandler,
689
801
  abortSignal,
690
- fetch: fetch2 = getOriginalFetch()
802
+ fetch = getOriginalFetch()
691
803
  }) => {
692
804
  try {
693
- const response = await fetch2(url, {
805
+ const response = await fetch(url, {
694
806
  method: "GET",
695
807
  headers: withUserAgentSuffix(
696
808
  headers,
@@ -2451,7 +2563,7 @@ var postJsonToApi = async ({
2451
2563
  failedResponseHandler,
2452
2564
  successfulResponseHandler,
2453
2565
  abortSignal,
2454
- fetch: fetch2
2566
+ fetch
2455
2567
  }) => postToApi({
2456
2568
  url,
2457
2569
  headers: {
@@ -2465,7 +2577,7 @@ var postJsonToApi = async ({
2465
2577
  failedResponseHandler,
2466
2578
  successfulResponseHandler,
2467
2579
  abortSignal,
2468
- fetch: fetch2
2580
+ fetch
2469
2581
  });
2470
2582
  var postFormDataToApi = async ({
2471
2583
  url,
@@ -2474,7 +2586,7 @@ var postFormDataToApi = async ({
2474
2586
  failedResponseHandler,
2475
2587
  successfulResponseHandler,
2476
2588
  abortSignal,
2477
- fetch: fetch2
2589
+ fetch
2478
2590
  }) => postToApi({
2479
2591
  url,
2480
2592
  headers,
@@ -2485,7 +2597,7 @@ var postFormDataToApi = async ({
2485
2597
  failedResponseHandler,
2486
2598
  successfulResponseHandler,
2487
2599
  abortSignal,
2488
- fetch: fetch2
2600
+ fetch
2489
2601
  });
2490
2602
  var postToApi = async ({
2491
2603
  url,
@@ -2494,10 +2606,10 @@ var postToApi = async ({
2494
2606
  successfulResponseHandler,
2495
2607
  failedResponseHandler,
2496
2608
  abortSignal,
2497
- fetch: fetch2 = getOriginalFetch2()
2609
+ fetch = getOriginalFetch2()
2498
2610
  }) => {
2499
2611
  try {
2500
- const response = await fetch2(url, {
2612
+ const response = await fetch(url, {
2501
2613
  method: "POST",
2502
2614
  headers: withUserAgentSuffix(
2503
2615
  headers,