@ai-sdk/provider-utils 3.0.40 → 3.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
@@ -173,6 +173,15 @@ function isBrowserRuntime(globalThisAny = globalThis) {
173
173
  return globalThisAny.window != null;
174
174
  }
175
175
 
176
+ // src/is-same-origin.ts
177
+ function isSameOrigin(url, baseUrl) {
178
+ try {
179
+ return new URL(url).origin === new URL(baseUrl).origin;
180
+ } catch (e) {
181
+ return false;
182
+ }
183
+ }
184
+
176
185
  // src/validate-download-url.ts
177
186
  function validateDownloadUrl(url) {
178
187
  let parsed;
@@ -358,34 +367,24 @@ function createSafeLookup(lookup) {
358
367
  });
359
368
  }
360
369
  var safeNodeFetchPromise;
361
- var initialGlobalFetch = globalThis.fetch;
362
- var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
363
370
  function isNodeRuntime() {
364
- var _a2, _b2;
371
+ var _a2, _b2, _c;
365
372
  const runtimeProcess = globalThis.process;
366
- return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null;
373
+ return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null && ((_c = runtimeProcess.versions) == null ? void 0 : _c.deno) == null && runtimeProcess.title !== "workerd" && globalThis.EdgeRuntime == null;
367
374
  }
368
375
  async function getDefaultDownloadFetch() {
369
- if (!isNodeRuntime() || !initialGlobalFetchIsNodeDefault || globalThis.fetch !== initialGlobalFetch) {
376
+ if (!isNodeRuntime()) {
370
377
  return globalThis.fetch;
371
378
  }
372
379
  return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
373
380
  }
374
- function isNodeDefaultFetch(fetch) {
375
- if (typeof fetch !== "function") {
376
- return false;
377
- }
378
- const source = Function.prototype.toString.call(fetch);
379
- return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
380
- }
381
381
  async function createSafeNodeFetch() {
382
- const [{ createRequire }, { lookup }] = await Promise.all([
382
+ const [module, { lookup }] = await Promise.all([
383
383
  loadNodeModule("node:module"),
384
384
  loadNodeModule("node:dns")
385
385
  ]);
386
- const { Agent, fetch } = createRequire(getCurrentModulePath())(
387
- "undici"
388
- );
386
+ const nodeRequire = module.createRequire(getCurrentModulePath());
387
+ const { Agent, fetch } = nodeRequire("undici");
389
388
  const dispatcher = new Agent({
390
389
  connect: {
391
390
  lookup: createSafeLookup(lookup)
@@ -427,20 +426,47 @@ function getCurrentModulePath() {
427
426
 
428
427
  // src/fetch-with-validated-redirects.ts
429
428
  var MAX_DOWNLOAD_REDIRECTS = 10;
429
+ async function getValidatedFetch(customFetch) {
430
+ return customFetch == null || customFetch === globalThis.fetch ? await getDefaultDownloadFetch() : customFetch;
431
+ }
432
+ async function fetchWithValidatedEndpoint({
433
+ url,
434
+ init,
435
+ fetch: customFetch,
436
+ trustedOrigin,
437
+ redirect = "error"
438
+ }) {
439
+ const urlText = url.toString();
440
+ const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
441
+ if (!isTrusted) {
442
+ validateDownloadUrl(urlText);
443
+ }
444
+ const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
445
+ return await fetch(url, {
446
+ ...init,
447
+ redirect
448
+ });
449
+ }
430
450
  async function fetchWithValidatedRedirects({
431
451
  url,
432
452
  headers,
433
453
  abortSignal,
434
- maxRedirects = MAX_DOWNLOAD_REDIRECTS
454
+ maxRedirects = MAX_DOWNLOAD_REDIRECTS,
455
+ fetch: customFetch,
456
+ trustedOrigin
435
457
  }) {
458
+ var _a2;
436
459
  const baseInit = { signal: abortSignal };
437
460
  if (headers !== void 0) {
438
461
  baseInit.headers = headers;
439
462
  }
440
463
  let currentUrl = url;
441
464
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
442
- validateDownloadUrl(currentUrl);
443
- const fetch = await getDefaultDownloadFetch();
465
+ const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
466
+ if (!isTrustedHop) {
467
+ validateDownloadUrl(currentUrl);
468
+ }
469
+ const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
444
470
  const response = await fetch(currentUrl, {
445
471
  ...baseInit,
446
472
  redirect: "manual"
@@ -454,7 +480,7 @@ async function fetchWithValidatedRedirects({
454
480
  }
455
481
  return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
456
482
  }
457
- const location = response.headers.get("location");
483
+ const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
458
484
  if (response.status >= 300 && response.status < 400 && location) {
459
485
  await cancelResponseBody(response);
460
486
  currentUrl = new URL(location, currentUrl).toString();
@@ -708,7 +734,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
708
734
  }
709
735
 
710
736
  // src/version.ts
711
- var VERSION = true ? "3.0.40" : "0.0.0-test";
737
+ var VERSION = true ? "3.0.41" : "0.0.0-test";
712
738
 
713
739
  // src/get-from-api.ts
714
740
  var getOriginalFetch = () => globalThis.fetch;
@@ -819,15 +845,6 @@ function injectJsonInstructionIntoMessages({
819
845
  ];
820
846
  }
821
847
 
822
- // src/is-same-origin.ts
823
- function isSameOrigin(url, baseUrl) {
824
- try {
825
- return new URL(url).origin === new URL(baseUrl).origin;
826
- } catch (e) {
827
- return false;
828
- }
829
- }
830
-
831
848
  // src/is-url-supported.ts
832
849
  function isUrlSupported({
833
850
  mediaType,
@@ -3342,6 +3359,7 @@ export {
3342
3359
  dynamicTool,
3343
3360
  executeTool,
3344
3361
  extractResponseHeaders,
3362
+ fetchWithValidatedEndpoint,
3345
3363
  fetchWithValidatedRedirects,
3346
3364
  generateId,
3347
3365
  getErrorMessage,