@ai-sdk/provider-utils 3.0.29 → 3.0.31

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"
@@ -539,7 +651,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
539
651
  }
540
652
 
541
653
  // src/version.ts
542
- var VERSION = true ? "3.0.29" : "0.0.0-test";
654
+ var VERSION = true ? "3.0.31" : "0.0.0-test";
543
655
 
544
656
  // src/get-from-api.ts
545
657
  var getOriginalFetch = () => globalThis.fetch;
@@ -549,10 +661,10 @@ var getFromApi = async ({
549
661
  successfulResponseHandler,
550
662
  failedResponseHandler,
551
663
  abortSignal,
552
- fetch: fetch2 = getOriginalFetch()
664
+ fetch = getOriginalFetch()
553
665
  }) => {
554
666
  try {
555
- const response = await fetch2(url, {
667
+ const response = await fetch(url, {
556
668
  method: "GET",
557
669
  headers: withUserAgentSuffix(
558
670
  headers,
@@ -1003,7 +1115,7 @@ var postJsonToApi = async ({
1003
1115
  failedResponseHandler,
1004
1116
  successfulResponseHandler,
1005
1117
  abortSignal,
1006
- fetch: fetch2
1118
+ fetch
1007
1119
  }) => postToApi({
1008
1120
  url,
1009
1121
  headers: {
@@ -1017,7 +1129,7 @@ var postJsonToApi = async ({
1017
1129
  failedResponseHandler,
1018
1130
  successfulResponseHandler,
1019
1131
  abortSignal,
1020
- fetch: fetch2
1132
+ fetch
1021
1133
  });
1022
1134
  var postFormDataToApi = async ({
1023
1135
  url,
@@ -1026,7 +1138,7 @@ var postFormDataToApi = async ({
1026
1138
  failedResponseHandler,
1027
1139
  successfulResponseHandler,
1028
1140
  abortSignal,
1029
- fetch: fetch2
1141
+ fetch
1030
1142
  }) => postToApi({
1031
1143
  url,
1032
1144
  headers,
@@ -1037,7 +1149,7 @@ var postFormDataToApi = async ({
1037
1149
  failedResponseHandler,
1038
1150
  successfulResponseHandler,
1039
1151
  abortSignal,
1040
- fetch: fetch2
1152
+ fetch
1041
1153
  });
1042
1154
  var postToApi = async ({
1043
1155
  url,
@@ -1046,10 +1158,10 @@ var postToApi = async ({
1046
1158
  successfulResponseHandler,
1047
1159
  failedResponseHandler,
1048
1160
  abortSignal,
1049
- fetch: fetch2 = getOriginalFetch2()
1161
+ fetch = getOriginalFetch2()
1050
1162
  }) => {
1051
1163
  try {
1052
- const response = await fetch2(url, {
1164
+ const response = await fetch(url, {
1053
1165
  method: "POST",
1054
1166
  headers: withUserAgentSuffix(
1055
1167
  headers,