@ai-sdk/provider-utils 4.0.56 → 4.0.57

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
@@ -248,6 +248,15 @@ function isBrowserRuntime(globalThisAny = globalThis) {
248
248
  return globalThisAny.window != null;
249
249
  }
250
250
 
251
+ // src/is-same-origin.ts
252
+ function isSameOrigin(url, baseUrl) {
253
+ try {
254
+ return new URL(url).origin === new URL(baseUrl).origin;
255
+ } catch (e) {
256
+ return false;
257
+ }
258
+ }
259
+
251
260
  // src/validate-download-url.ts
252
261
  function validateDownloadUrl(url) {
253
262
  let parsed;
@@ -433,34 +442,24 @@ function createSafeLookup(lookup) {
433
442
  });
434
443
  }
435
444
  var safeNodeFetchPromise;
436
- var initialGlobalFetch = globalThis.fetch;
437
- var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
438
445
  function isNodeRuntime() {
439
- var _a2, _b2;
446
+ var _a2, _b2, _c;
440
447
  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;
448
+ 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;
442
449
  }
443
450
  async function getDefaultDownloadFetch() {
444
- if (!isNodeRuntime() || !initialGlobalFetchIsNodeDefault || globalThis.fetch !== initialGlobalFetch) {
451
+ if (!isNodeRuntime()) {
445
452
  return globalThis.fetch;
446
453
  }
447
454
  return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
448
455
  }
449
- function isNodeDefaultFetch(fetch) {
450
- if (typeof fetch !== "function") {
451
- return false;
452
- }
453
- const source = Function.prototype.toString.call(fetch);
454
- return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
455
- }
456
456
  async function createSafeNodeFetch() {
457
- const [{ createRequire }, { lookup }] = await Promise.all([
457
+ const [module, { lookup }] = await Promise.all([
458
458
  loadNodeModule("node:module"),
459
459
  loadNodeModule("node:dns")
460
460
  ]);
461
- const { Agent, fetch } = createRequire(getCurrentModulePath())(
462
- "undici"
463
- );
461
+ const nodeRequire = module.createRequire(getCurrentModulePath());
462
+ const { Agent, fetch } = nodeRequire("undici");
464
463
  const dispatcher = new Agent({
465
464
  connect: {
466
465
  lookup: createSafeLookup(lookup)
@@ -502,20 +501,47 @@ function getCurrentModulePath() {
502
501
 
503
502
  // src/fetch-with-validated-redirects.ts
504
503
  var MAX_DOWNLOAD_REDIRECTS = 10;
504
+ async function getValidatedFetch(customFetch) {
505
+ return customFetch == null || customFetch === globalThis.fetch ? await getDefaultDownloadFetch() : customFetch;
506
+ }
507
+ async function fetchWithValidatedEndpoint({
508
+ url,
509
+ init,
510
+ fetch: customFetch,
511
+ trustedOrigin,
512
+ redirect = "error"
513
+ }) {
514
+ const urlText = url.toString();
515
+ const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
516
+ if (!isTrusted) {
517
+ validateDownloadUrl(urlText);
518
+ }
519
+ const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
520
+ return await fetch(url, {
521
+ ...init,
522
+ redirect
523
+ });
524
+ }
505
525
  async function fetchWithValidatedRedirects({
506
526
  url,
507
527
  headers,
508
528
  abortSignal,
509
- maxRedirects = MAX_DOWNLOAD_REDIRECTS
529
+ maxRedirects = MAX_DOWNLOAD_REDIRECTS,
530
+ fetch: customFetch,
531
+ trustedOrigin
510
532
  }) {
533
+ var _a2;
511
534
  const baseInit = { signal: abortSignal };
512
535
  if (headers !== void 0) {
513
536
  baseInit.headers = headers;
514
537
  }
515
538
  let currentUrl = url;
516
539
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
517
- validateDownloadUrl(currentUrl);
518
- const fetch = await getDefaultDownloadFetch();
540
+ const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
541
+ if (!isTrustedHop) {
542
+ validateDownloadUrl(currentUrl);
543
+ }
544
+ const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
519
545
  const response = await fetch(currentUrl, {
520
546
  ...baseInit,
521
547
  redirect: "manual"
@@ -529,7 +555,7 @@ async function fetchWithValidatedRedirects({
529
555
  }
530
556
  return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
531
557
  }
532
- const location = response.headers.get("location");
558
+ const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
533
559
  if (response.status >= 300 && response.status < 400 && location) {
534
560
  await cancelResponseBody(response);
535
561
  currentUrl = new URL(location, currentUrl).toString();
@@ -822,7 +848,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
822
848
  }
823
849
 
824
850
  // src/version.ts
825
- var VERSION = true ? "4.0.56" : "0.0.0-test";
851
+ var VERSION = true ? "4.0.57" : "0.0.0-test";
826
852
 
827
853
  // src/get-from-api.ts
828
854
  var getOriginalFetch = () => globalThis.fetch;
@@ -938,15 +964,6 @@ function isNonNullable(value) {
938
964
  return value != null;
939
965
  }
940
966
 
941
- // src/is-same-origin.ts
942
- function isSameOrigin(url, baseUrl) {
943
- try {
944
- return new URL(url).origin === new URL(baseUrl).origin;
945
- } catch (e) {
946
- return false;
947
- }
948
- }
949
-
950
967
  // src/is-url-supported.ts
951
968
  function isUrlSupported({
952
969
  mediaType,
@@ -3510,6 +3527,7 @@ export {
3510
3527
  dynamicTool,
3511
3528
  executeTool,
3512
3529
  extractResponseHeaders,
3530
+ fetchWithValidatedEndpoint,
3513
3531
  fetchWithValidatedRedirects,
3514
3532
  generateId,
3515
3533
  getErrorMessage,