@ai-sdk/provider-utils 3.0.44 → 3.0.45

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
@@ -456,10 +456,12 @@ async function fetchWithValidatedRedirects({
456
456
  trustedOrigin
457
457
  }) {
458
458
  var _a2;
459
- const baseInit = { signal: abortSignal };
460
- if (headers !== void 0) {
461
- baseInit.headers = headers;
462
- }
459
+ let currentHeaders = headers === void 0 ? void 0 : new Headers(headers);
460
+ const perHopInit = (redirect) => ({
461
+ signal: abortSignal,
462
+ redirect,
463
+ ...currentHeaders === void 0 ? {} : { headers: new Headers(currentHeaders) }
464
+ });
463
465
  let currentUrl = url;
464
466
  for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
465
467
  const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
@@ -467,10 +469,7 @@ async function fetchWithValidatedRedirects({
467
469
  validateDownloadUrl(currentUrl);
468
470
  }
469
471
  const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
470
- const response = await fetch(currentUrl, {
471
- ...baseInit,
472
- redirect: "manual"
473
- });
472
+ const response = await fetch(currentUrl, perHopInit("manual"));
474
473
  if (response.type === "opaqueredirect") {
475
474
  if (!isBrowserRuntime()) {
476
475
  throw new DownloadError({
@@ -478,12 +477,25 @@ async function fetchWithValidatedRedirects({
478
477
  message: `Redirect from ${currentUrl} could not be validated and was blocked`
479
478
  });
480
479
  }
481
- return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
480
+ return await fetch(currentUrl, perHopInit("follow"));
482
481
  }
483
482
  const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
484
483
  if (response.status >= 300 && response.status < 400 && location) {
485
- await cancelResponseBody(response);
486
- currentUrl = new URL(location, currentUrl).toString();
484
+ void cancelResponseBody(response);
485
+ const nextUrl = new URL(location, currentUrl).toString();
486
+ if (new URL(currentUrl).protocol === "https:" && new URL(nextUrl).protocol === "http:") {
487
+ throw new DownloadError({
488
+ url,
489
+ message: "Redirect from HTTPS to HTTP was blocked"
490
+ });
491
+ }
492
+ if (currentHeaders !== void 0 && !isSameOrigin(nextUrl, currentUrl)) {
493
+ const userAgent = currentHeaders.get("user-agent");
494
+ currentHeaders = new Headers(
495
+ userAgent == null ? void 0 : { "user-agent": userAgent }
496
+ );
497
+ }
498
+ currentUrl = nextUrl;
487
499
  continue;
488
500
  }
489
501
  return response;
@@ -740,7 +752,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
740
752
  }
741
753
 
742
754
  // src/version.ts
743
- var VERSION = true ? "3.0.44" : "0.0.0-test";
755
+ var VERSION = true ? "3.0.45" : "0.0.0-test";
744
756
 
745
757
  // src/get-from-api.ts
746
758
  var getOriginalFetch = () => globalThis.fetch;
@@ -750,16 +762,28 @@ var getFromApi = async ({
750
762
  successfulResponseHandler,
751
763
  failedResponseHandler,
752
764
  abortSignal,
753
- fetch = getOriginalFetch()
765
+ fetch,
766
+ validateUrl,
767
+ credentialedOrigin,
768
+ trustedOrigin
754
769
  }) => {
755
770
  try {
756
- const response = await fetch(url, {
771
+ const requestFetch = fetch != null ? fetch : getOriginalFetch();
772
+ const outgoingHeaders = credentialedOrigin !== void 0 && !isSameOrigin(url, credentialedOrigin) ? {} : headers;
773
+ const requestHeaders = withUserAgentSuffix(
774
+ outgoingHeaders,
775
+ `ai-sdk/provider-utils/${VERSION}`,
776
+ getRuntimeEnvironmentUserAgent()
777
+ );
778
+ const response = validateUrl ? await fetchWithValidatedRedirects({
779
+ url,
780
+ headers: requestHeaders,
781
+ abortSignal,
782
+ fetch,
783
+ trustedOrigin
784
+ }) : await requestFetch(url, {
757
785
  method: "GET",
758
- headers: withUserAgentSuffix(
759
- headers,
760
- `ai-sdk/provider-utils/${VERSION}`,
761
- getRuntimeEnvironmentUserAgent()
762
- ),
786
+ headers: requestHeaders,
763
787
  signal: abortSignal
764
788
  });
765
789
  const responseHeaders = extractResponseHeaders(response);