@ai-sdk/provider-utils 3.0.44 → 3.0.46

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
@@ -434,12 +434,20 @@ async function fetchWithValidatedEndpoint({
434
434
  init,
435
435
  fetch: customFetch,
436
436
  trustedOrigin,
437
- redirect = "error"
437
+ redirect = "error",
438
+ requireDnsValidation = false
438
439
  }) {
439
440
  const urlText = url.toString();
440
441
  const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
441
442
  if (!isTrusted) {
442
443
  validateDownloadUrl(urlText);
444
+ if (requireDnsValidation && (customFetch == null || customFetch === globalThis.fetch) && !isNodeRuntime() && // A window alias alone does not distinguish older Deno from a browser.
445
+ !(isBrowserRuntime() && typeof document !== "undefined")) {
446
+ throw new DownloadError({
447
+ url: urlText,
448
+ message: "This server runtime requires a custom fetch with connect-time DNS validation for untrusted URLs"
449
+ });
450
+ }
443
451
  }
444
452
  const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
445
453
  return await fetch(url, {
@@ -456,10 +464,12 @@ async function fetchWithValidatedRedirects({
456
464
  trustedOrigin
457
465
  }) {
458
466
  var _a2;
459
- const baseInit = { signal: abortSignal };
460
- if (headers !== void 0) {
461
- baseInit.headers = headers;
462
- }
467
+ let currentHeaders = headers === void 0 ? void 0 : new Headers(headers);
468
+ const perHopInit = (redirect) => ({
469
+ signal: abortSignal,
470
+ redirect,
471
+ ...currentHeaders === void 0 ? {} : { headers: new Headers(currentHeaders) }
472
+ });
463
473
  let currentUrl = url;
464
474
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
465
475
  const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
@@ -467,10 +477,7 @@ async function fetchWithValidatedRedirects({
467
477
  validateDownloadUrl(currentUrl);
468
478
  }
469
479
  const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
470
- const response = await fetch(currentUrl, {
471
- ...baseInit,
472
- redirect: "manual"
473
- });
480
+ const response = await fetch(currentUrl, perHopInit("manual"));
474
481
  if (response.type === "opaqueredirect") {
475
482
  if (!isBrowserRuntime()) {
476
483
  throw new DownloadError({
@@ -478,12 +485,25 @@ async function fetchWithValidatedRedirects({
478
485
  message: `Redirect from ${currentUrl} could not be validated and was blocked`
479
486
  });
480
487
  }
481
- return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
488
+ return await fetch(currentUrl, perHopInit("follow"));
482
489
  }
483
490
  const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
484
491
  if (response.status >= 300 && response.status < 400 && location) {
485
- await cancelResponseBody(response);
486
- currentUrl = new URL(location, currentUrl).toString();
492
+ void cancelResponseBody(response);
493
+ const nextUrl = new URL(location, currentUrl).toString();
494
+ if (new URL(currentUrl).protocol === "https:" && new URL(nextUrl).protocol === "http:") {
495
+ throw new DownloadError({
496
+ url,
497
+ message: "Redirect from HTTPS to HTTP was blocked"
498
+ });
499
+ }
500
+ if (currentHeaders !== void 0 && !isSameOrigin(nextUrl, currentUrl)) {
501
+ const userAgent = currentHeaders.get("user-agent");
502
+ currentHeaders = new Headers(
503
+ userAgent == null ? void 0 : { "user-agent": userAgent }
504
+ );
505
+ }
506
+ currentUrl = nextUrl;
487
507
  continue;
488
508
  }
489
509
  return response;
@@ -740,7 +760,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
740
760
  }
741
761
 
742
762
  // src/version.ts
743
- var VERSION = true ? "3.0.44" : "0.0.0-test";
763
+ var VERSION = true ? "3.0.46" : "0.0.0-test";
744
764
 
745
765
  // src/get-from-api.ts
746
766
  var getOriginalFetch = () => globalThis.fetch;
@@ -750,16 +770,28 @@ var getFromApi = async ({
750
770
  successfulResponseHandler,
751
771
  failedResponseHandler,
752
772
  abortSignal,
753
- fetch = getOriginalFetch()
773
+ fetch,
774
+ validateUrl,
775
+ credentialedOrigin,
776
+ trustedOrigin
754
777
  }) => {
755
778
  try {
756
- const response = await fetch(url, {
779
+ const requestFetch = fetch != null ? fetch : getOriginalFetch();
780
+ const outgoingHeaders = credentialedOrigin !== void 0 && !isSameOrigin(url, credentialedOrigin) ? {} : headers;
781
+ const requestHeaders = withUserAgentSuffix(
782
+ outgoingHeaders,
783
+ `ai-sdk/provider-utils/${VERSION}`,
784
+ getRuntimeEnvironmentUserAgent()
785
+ );
786
+ const response = validateUrl ? await fetchWithValidatedRedirects({
787
+ url,
788
+ headers: requestHeaders,
789
+ abortSignal,
790
+ fetch,
791
+ trustedOrigin
792
+ }) : await requestFetch(url, {
757
793
  method: "GET",
758
- headers: withUserAgentSuffix(
759
- headers,
760
- `ai-sdk/provider-utils/${VERSION}`,
761
- getRuntimeEnvironmentUserAgent()
762
- ),
794
+ headers: requestHeaders,
763
795
  signal: abortSignal
764
796
  });
765
797
  const responseHeaders = extractResponseHeaders(response);