@ai-sdk/provider-utils 3.0.40 → 3.0.42

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;
@@ -200,7 +209,7 @@ function validateDownloadUrl(url) {
200
209
  message: `URL must have a hostname`
201
210
  });
202
211
  }
203
- if (hostname === "localhost" || hostname.endsWith(".local") || hostname.endsWith(".localhost")) {
212
+ if (hostname === "localhost" || hostname.endsWith(".local") || hostname.endsWith(".localhost") || hostname.endsWith(".internal")) {
204
213
  throw new DownloadError({
205
214
  url,
206
215
  message: `URL with hostname ${hostname} is not allowed`
@@ -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();
@@ -554,6 +580,12 @@ var createIdGenerator = ({
554
580
  };
555
581
  var generateId = createIdGenerator();
556
582
 
583
+ // src/encode-path-segment.ts
584
+ function encodePathSegment(value) {
585
+ const encodedValue = encodeURIComponent(value);
586
+ return encodedValue === "." ? "%252E" : encodedValue === ".." ? "%252E%252E" : encodedValue;
587
+ }
588
+
557
589
  // src/get-error-message.ts
558
590
  function getErrorMessage(error) {
559
591
  if (error == null) {
@@ -708,7 +740,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
708
740
  }
709
741
 
710
742
  // src/version.ts
711
- var VERSION = true ? "3.0.40" : "0.0.0-test";
743
+ var VERSION = true ? "3.0.42" : "0.0.0-test";
712
744
 
713
745
  // src/get-from-api.ts
714
746
  var getOriginalFetch = () => globalThis.fetch;
@@ -819,15 +851,6 @@ function injectJsonInstructionIntoMessages({
819
851
  ];
820
852
  }
821
853
 
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
854
  // src/is-url-supported.ts
832
855
  function isUrlSupported({
833
856
  mediaType,
@@ -3340,8 +3363,10 @@ export {
3340
3363
  createStatusCodeErrorResponseHandler,
3341
3364
  delay,
3342
3365
  dynamicTool,
3366
+ encodePathSegment,
3343
3367
  executeTool,
3344
3368
  extractResponseHeaders,
3369
+ fetchWithValidatedEndpoint,
3345
3370
  fetchWithValidatedRedirects,
3346
3371
  generateId,
3347
3372
  getErrorMessage,