@loaders.gl/core 5.0.0-alpha.7 → 5.0.0-alpha.8

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.
Files changed (47) hide show
  1. package/dist/dist.dev.js +294 -43
  2. package/dist/dist.min.js +3 -3
  3. package/dist/index.cjs +279 -51
  4. package/dist/index.cjs.map +3 -3
  5. package/dist/lib/api/create-data-source.d.ts +1 -1
  6. package/dist/lib/api/create-data-source.d.ts.map +1 -1
  7. package/dist/lib/api/create-data-source.js +23 -2
  8. package/dist/lib/api/create-data-source.js.map +1 -1
  9. package/dist/lib/api/load-in-batches.d.ts.map +1 -1
  10. package/dist/lib/api/load-in-batches.js +17 -6
  11. package/dist/lib/api/load-in-batches.js.map +1 -1
  12. package/dist/lib/api/load.d.ts +3 -3
  13. package/dist/lib/api/load.d.ts.map +1 -1
  14. package/dist/lib/api/load.js +14 -5
  15. package/dist/lib/api/load.js.map +1 -1
  16. package/dist/lib/api/parse-file.d.ts.map +1 -1
  17. package/dist/lib/api/parse-file.js +6 -2
  18. package/dist/lib/api/parse-file.js.map +1 -1
  19. package/dist/lib/api/parse-in-batches.d.ts.map +1 -1
  20. package/dist/lib/api/parse-in-batches.js +6 -1
  21. package/dist/lib/api/parse-in-batches.js.map +1 -1
  22. package/dist/lib/api/parse.d.ts.map +1 -1
  23. package/dist/lib/api/parse.js +6 -1
  24. package/dist/lib/api/parse.js.map +1 -1
  25. package/dist/lib/init.js +1 -1
  26. package/dist/lib/loader-utils/get-fetch-function.js +4 -13
  27. package/dist/lib/loader-utils/get-fetch-function.js.map +1 -1
  28. package/dist/lib/loader-utils/option-defaults.d.ts.map +1 -1
  29. package/dist/lib/loader-utils/option-defaults.js +1 -0
  30. package/dist/lib/loader-utils/option-defaults.js.map +1 -1
  31. package/dist/lib/loader-utils/option-utils.d.ts +7 -0
  32. package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
  33. package/dist/lib/loader-utils/option-utils.js +29 -0
  34. package/dist/lib/loader-utils/option-utils.js.map +1 -1
  35. package/dist/null-loader.js +1 -1
  36. package/dist/null-worker-node.js +1 -1
  37. package/dist/null-worker.js +1 -1
  38. package/package.json +6 -6
  39. package/src/lib/api/create-data-source.ts +31 -2
  40. package/src/lib/api/load-in-batches.ts +18 -11
  41. package/src/lib/api/load.ts +44 -6
  42. package/src/lib/api/parse-file.ts +15 -2
  43. package/src/lib/api/parse-in-batches.ts +11 -1
  44. package/src/lib/api/parse.ts +11 -1
  45. package/src/lib/loader-utils/get-fetch-function.ts +4 -15
  46. package/src/lib/loader-utils/option-defaults.ts +1 -0
  47. package/src/lib/loader-utils/option-utils.ts +34 -0
package/dist/dist.dev.js CHANGED
@@ -1700,6 +1700,10 @@ var __exports__ = (() => {
1700
1700
 
1701
1701
  // ../loader-utils/src/lib/worker-loader-utils/parse-with-worker.ts
1702
1702
  function canParseWithWorker(loader, options) {
1703
+ if (options?.core?.credentials?.some(
1704
+ (credential) => credential.type === "request" || "token" in credential && typeof credential.token === "function"
1705
+ ))
1706
+ return false;
1703
1707
  const workerOptions = getWorkerOptions(options);
1704
1708
  const nodeWorkers = workerOptions._nodeWorkers;
1705
1709
  if (!isBrowser3 && !nodeWorkers) {
@@ -1794,7 +1798,12 @@ var __exports__ = (() => {
1794
1798
  return parseOnMainThread(input, void 0, options, context);
1795
1799
  }
1796
1800
  function getWorkerOptions(options = {}, loader) {
1797
- const serializedOptions = JSON.parse(JSON.stringify(options));
1801
+ const serializedOptions = JSON.parse(
1802
+ JSON.stringify({
1803
+ ...options,
1804
+ ...options.core ? { core: { ...options.core, authentications: void 0 } } : {}
1805
+ })
1806
+ );
1798
1807
  const workerOptions = {
1799
1808
  ...serializedOptions.core,
1800
1809
  ...serializedOptions
@@ -2193,6 +2202,7 @@ var __exports__ = (() => {
2193
2202
  /** Tracks the number of active requests and prioritizes/cancels queued requests. */
2194
2203
  requestQueue = [];
2195
2204
  requestMap = /* @__PURE__ */ new Map();
2205
+ /** Pending arrival debounce or next-tick refill; only new arrivals replace this timer. */
2196
2206
  updateTimer = null;
2197
2207
  constructor(props = {}) {
2198
2208
  this.props = { ...DEFAULT_PROPS2, ...props };
@@ -2260,20 +2270,27 @@ var __exports__ = (() => {
2260
2270
  isDone = true;
2261
2271
  this.requestMap.delete(handle);
2262
2272
  this.activeRequestCount--;
2263
- this._issueNewRequests();
2273
+ this.scheduleRefill();
2264
2274
  }
2265
2275
  };
2266
2276
  this.activeRequestCount++;
2267
2277
  return resolve2 ? resolve2({ done }) : Promise.resolve({ done });
2268
2278
  }
2269
- /** We check requests asynchronously, to prevent multiple updates */
2279
+ /** Refills freed slots on the next tick while preserving any pending arrival debounce. */
2280
+ scheduleRefill() {
2281
+ if (this.updateTimer !== null) {
2282
+ return;
2283
+ }
2284
+ this.updateTimer = setTimeout(() => this._issueNewRequestsAsync(), 0);
2285
+ }
2286
+ /** Restarts the quiet period when a new request enters the queue. */
2270
2287
  _issueNewRequests() {
2271
2288
  if (this.updateTimer !== null) {
2272
2289
  clearTimeout(this.updateTimer);
2273
2290
  }
2274
2291
  this.updateTimer = setTimeout(() => this._issueNewRequestsAsync(), this.props.debounceTime);
2275
2292
  }
2276
- /** Refresh all requests */
2293
+ /** Clears timer ownership before updating priorities and dispatching queued requests. */
2277
2294
  _issueNewRequestsAsync() {
2278
2295
  if (this.updateTimer !== null) {
2279
2296
  clearTimeout(this.updateTimer);
@@ -2315,23 +2332,103 @@ var __exports__ = (() => {
2315
2332
  }
2316
2333
  };
2317
2334
 
2335
+ // ../loader-utils/src/lib/request-utils/authentication.ts
2336
+ function resolveCredentials(credentials, authentications = []) {
2337
+ if (credentials.every(isAuthentication)) return credentials;
2338
+ return credentials.map((credential, index) => {
2339
+ if (isAuthentication(credential)) return credential;
2340
+ const AuthenticationClass = authentications.find(
2341
+ (authentication2) => authentication2.type === credential?.type
2342
+ );
2343
+ if (!AuthenticationClass) {
2344
+ throw new Error(`No authentication class registered for credential at index ${index}.`);
2345
+ }
2346
+ let authentication;
2347
+ try {
2348
+ authentication = new AuthenticationClass(credential);
2349
+ } catch {
2350
+ throw new Error(`Invalid authentication configuration at index ${index}.`);
2351
+ }
2352
+ if (!isAuthentication(authentication)) {
2353
+ throw new Error(`Authentication class returned an invalid credential at index ${index}.`);
2354
+ }
2355
+ return authentication;
2356
+ });
2357
+ }
2358
+ function resolveAuthenticationOptions(options) {
2359
+ if (!options.core?.credentials?.length) return options;
2360
+ const credentials = resolveCredentials(options.core.credentials, options.core.authentications);
2361
+ if (credentials === options.core.credentials) return options;
2362
+ return { ...options, core: { ...options.core, credentials } };
2363
+ }
2364
+ async function resolveLoaderAuthenticationOptions(loader, url, options) {
2365
+ if (!options.core?.credentials?.length || options.core.credentials.every(isAuthentication)) {
2366
+ return options;
2367
+ }
2368
+ const contributions = await loader?.getAuthentications?.(url, options);
2369
+ if (!contributions?.length) return resolveAuthenticationOptions(options);
2370
+ return resolveAuthenticationOptions({
2371
+ ...options,
2372
+ core: {
2373
+ ...options.core,
2374
+ authentications: [...options.core?.authentications || [], ...contributions]
2375
+ }
2376
+ });
2377
+ }
2378
+ function isAuthentication(credential) {
2379
+ if (!credential || typeof credential !== "object") return false;
2380
+ if (typeof credential.id !== "string" || !Array.isArray(credential.origins)) return false;
2381
+ if (credential.type === "request") return typeof credential.authenticate === "function";
2382
+ return (credential.type === "header" || credential.type === "query-parameter") && typeof credential.name === "string" && (typeof credential.token === "string" || typeof credential.token === "function") && Array.isArray(credential.refreshStatusCodes);
2383
+ }
2384
+
2318
2385
  // ../loader-utils/src/lib/request-utils/request-credentials.ts
2319
2386
  var AUTHENTICATED_FETCH = /* @__PURE__ */ Symbol("loaders.gl.authenticated-fetch");
2320
2387
  var refreshPromises = /* @__PURE__ */ new WeakMap();
2321
2388
  function createAuthenticatedFetch(options) {
2322
- const baseFetch = options.fetch || ((url, requestOptions) => fetch(url, requestOptions));
2323
- if (!options.credentials.length) return baseFetch;
2324
- const existingMetadata = baseFetch[AUTHENTICATED_FETCH];
2325
- if (existingMetadata === options.credentials) return baseFetch;
2389
+ const suppliedFetch = options.fetch || ((url, requestOptions) => fetch(url, requestOptions));
2390
+ const existingMetadata = suppliedFetch[AUTHENTICATED_FETCH];
2391
+ const credentialResolutions = new Map(existingMetadata?.credentialResolutions);
2392
+ const resolvedCredentials = resolveCredentials(
2393
+ options.credentials.map((credential) => credentialResolutions.get(credential) || credential),
2394
+ options.authentications
2395
+ );
2396
+ options.credentials.forEach((credential, index) => {
2397
+ credentialResolutions.set(credential, resolvedCredentials[index]);
2398
+ });
2399
+ if (!options.fetchOptions && (!resolvedCredentials.length || existingMetadata?.credentials.length === resolvedCredentials.length && resolvedCredentials.every(
2400
+ (credential, index) => credential === existingMetadata.credentials[index]
2401
+ )))
2402
+ return suppliedFetch;
2403
+ const baseFetch = existingMetadata?.fetch || suppliedFetch;
2404
+ const credentials = existingMetadata ? [.../* @__PURE__ */ new Set([...resolvedCredentials, ...existingMetadata.credentials])] : resolvedCredentials;
2405
+ const fetchOptions = mergeFetchOptions(
2406
+ existingMetadata?.fetchOptions || {},
2407
+ options.fetchOptions
2408
+ );
2409
+ const tokenCredentials = credentials.filter(
2410
+ (credential) => credential.type !== "request"
2411
+ );
2412
+ const requestCredentials = credentials.filter(
2413
+ (credential) => credential.type === "request"
2414
+ );
2415
+ for (const credential of requestCredentials) normalizeRequestOrigins(credential.origins);
2326
2416
  const authenticatedFetch = async (url, requestOptions = {}) => {
2417
+ requestOptions = mergeFetchOptions(fetchOptions, requestOptions);
2418
+ if (requestOptions.signal?.aborted) throw createAbortError3();
2419
+ if (!credentials.length) return baseFetch(url, requestOptions);
2327
2420
  const resolvedURL = parseHTTPURL(url);
2328
- if (!resolvedURL) return baseFetch(url, requestOptions);
2329
- const authorization = await authorizeRequest(resolvedURL, requestOptions, options.credentials);
2330
- const response = await baseFetch(authorization.url, authorization.options);
2421
+ const authorization = resolvedURL ? await authorizeRequest(resolvedURL, requestOptions, tokenCredentials) : { url, options: requestOptions, credentials: [] };
2422
+ const signedRequest = await authenticateRequest(authorization, requestCredentials, "request");
2423
+ const response = await baseFetch(signedRequest.url, signedRequest.options);
2331
2424
  const refreshableCredentials = authorization.credentials.filter(
2332
2425
  ({ credential }) => typeof credential.token === "function" && credential.refreshStatusCodes.includes(response.status)
2333
2426
  );
2334
- if (!refreshableCredentials.length || !isReplayableRequest(requestOptions)) return response;
2427
+ const shouldRetrySignature = signedRequest.credentials.some(
2428
+ (credential) => credential.refreshStatusCodes?.includes(response.status)
2429
+ );
2430
+ if (!refreshableCredentials.length && !shouldRetrySignature || !isReplayableRequest(requestOptions) || !isReplayableRequest(signedRequest.options))
2431
+ return response;
2335
2432
  if (requestOptions.signal?.aborted) throw createAbortError3();
2336
2433
  const refreshedTokens = /* @__PURE__ */ new Map();
2337
2434
  await Promise.all(
@@ -2340,24 +2437,95 @@ var __exports__ = (() => {
2340
2437
  if (token) refreshedTokens.set(credential, token);
2341
2438
  })
2342
2439
  );
2343
- if (!refreshedTokens.size) return response;
2440
+ if (!refreshedTokens.size && !shouldRetrySignature) return response;
2344
2441
  await response.body?.cancel().catch(() => {
2345
2442
  });
2346
2443
  if (requestOptions.signal?.aborted) throw createAbortError3();
2347
- const replayAuthorization = await authorizeRequest(
2444
+ const replayAuthorization = resolvedURL ? await authorizeRequest(
2348
2445
  resolvedURL,
2349
2446
  requestOptions,
2350
- options.credentials,
2447
+ tokenCredentials,
2351
2448
  refreshedTokens,
2352
2449
  authorization.credentials
2450
+ ) : { url, options: requestOptions };
2451
+ const replayRequest = await authenticateRequest(
2452
+ replayAuthorization,
2453
+ requestCredentials,
2454
+ "retry",
2455
+ response
2353
2456
  );
2354
- return baseFetch(replayAuthorization.url, replayAuthorization.options);
2457
+ return baseFetch(replayRequest.url, replayRequest.options);
2355
2458
  };
2356
2459
  Object.defineProperty(authenticatedFetch, AUTHENTICATED_FETCH, {
2357
- value: options.credentials
2460
+ value: {
2461
+ fetch: baseFetch,
2462
+ credentials,
2463
+ credentialResolutions,
2464
+ fetchOptions
2465
+ }
2358
2466
  });
2359
2467
  return authenticatedFetch;
2360
2468
  }
2469
+ function mergeFetchOptions(defaultOptions, requestOptions) {
2470
+ const mergedOptions = { ...defaultOptions, ...requestOptions };
2471
+ if (defaultOptions.headers || requestOptions?.headers) {
2472
+ const headers = new Headers(defaultOptions.headers);
2473
+ new Headers(requestOptions?.headers).forEach((value, key) => headers.set(key, value));
2474
+ mergedOptions.headers = headers;
2475
+ }
2476
+ return mergedOptions;
2477
+ }
2478
+ async function authenticateRequest(request, credentials, reason, response) {
2479
+ const appliedCredentials = [];
2480
+ const signal = request.options.signal;
2481
+ for (const credential of credentials) {
2482
+ const origin = getRequestOrigin(request.url);
2483
+ if (!origin || !normalizeRequestOrigins(credential.origins).includes(origin)) continue;
2484
+ if (signal?.aborted) throw createAbortError3();
2485
+ const authenticatedRequest = await credential.authenticate({
2486
+ url: request.url,
2487
+ options: { ...request.options, headers: new Headers(request.options.headers) },
2488
+ reason,
2489
+ response: response ? { status: response.status, headers: new Headers(response.headers) } : void 0
2490
+ });
2491
+ if (!authenticatedRequest || typeof authenticatedRequest.url !== "string" || !authenticatedRequest.options || typeof authenticatedRequest.options !== "object") {
2492
+ throw new Error("Authentication callback returned an invalid request.");
2493
+ }
2494
+ request = {
2495
+ url: authenticatedRequest.url,
2496
+ options: { ...authenticatedRequest.options, ...signal ? { signal } : {} }
2497
+ };
2498
+ appliedCredentials.push(credential);
2499
+ }
2500
+ if (signal?.aborted) throw createAbortError3();
2501
+ return { ...request, credentials: appliedCredentials };
2502
+ }
2503
+ function getRequestOrigin(url) {
2504
+ try {
2505
+ const parsedURL = new URL(url);
2506
+ if (!parsedURL.host || parsedURL.username || parsedURL.password) return null;
2507
+ return parsedURL.origin !== "null" ? parsedURL.origin : `${parsedURL.protocol}//${parsedURL.host}`;
2508
+ } catch {
2509
+ return null;
2510
+ }
2511
+ }
2512
+ function normalizeRequestOrigins(origins) {
2513
+ if (!origins.length)
2514
+ throw new Error("Request authentication requires at least one exact origin.");
2515
+ return origins.map((origin) => {
2516
+ const resolvedOrigin = getRequestOrigin(origin);
2517
+ let url;
2518
+ try {
2519
+ url = new URL(origin);
2520
+ } catch {
2521
+ throw new Error("Invalid request authentication origin.");
2522
+ }
2523
+ if (!resolvedOrigin || url.pathname && url.pathname !== "/" || url.search || url.hash) {
2524
+ throw new Error("Invalid request authentication origin.");
2525
+ }
2526
+ return resolvedOrigin;
2527
+ });
2528
+ }
2361
2529
  async function authorizeRequest(sourceURL, requestOptions, credentials, tokenOverrides = /* @__PURE__ */ new Map(), previousCredentials = []) {
2362
2530
  const url = new URL(sourceURL);
2363
2531
  const headers = new Headers(requestOptions.headers);
@@ -3044,6 +3212,7 @@ var __exports__ = (() => {
3044
3212
 
3045
3213
  // src/lib/loader-utils/option-defaults.ts
3046
3214
  var DEFAULT_LOADER_OPTIONS = {
3215
+ searchParams: void 0,
3047
3216
  core: {
3048
3217
  baseUrl: void 0,
3049
3218
  // baseUrl
@@ -3150,6 +3319,27 @@ var __exports__ = (() => {
3150
3319
  "metadata",
3151
3320
  "transforms"
3152
3321
  ];
3322
+ function applySearchParamsToUrl(url, options) {
3323
+ const searchParams = options?.searchParams;
3324
+ if (!searchParams || Object.keys(searchParams).length === 0 || /^(?:data|blob):/i.test(url)) {
3325
+ return url;
3326
+ }
3327
+ const hashIndex = url.indexOf("#");
3328
+ const hash = hashIndex >= 0 ? url.slice(hashIndex) : "";
3329
+ const urlWithoutHash = hashIndex >= 0 ? url.slice(0, hashIndex) : url;
3330
+ const queryIndex = urlWithoutHash.indexOf("?");
3331
+ const path = queryIndex >= 0 ? urlWithoutHash.slice(0, queryIndex) : urlWithoutHash;
3332
+ const query = queryIndex >= 0 ? urlWithoutHash.slice(queryIndex + 1) : "";
3333
+ const existingSearchParams = new URLSearchParams(query);
3334
+ const appendedSearchParams = new URLSearchParams();
3335
+ for (const [key, value] of Object.entries(searchParams)) {
3336
+ if (!existingSearchParams.has(key)) {
3337
+ appendedSearchParams.set(key, String(value));
3338
+ }
3339
+ }
3340
+ const serializedAppendedSearchParams = appendedSearchParams.toString();
3341
+ return `${path}${queryIndex >= 0 ? `?${query}` : ""}${serializedAppendedSearchParams ? `${queryIndex >= 0 ? "&" : "?"}${serializedAppendedSearchParams}` : ""}${hash}`;
3342
+ }
3153
3343
  function getGlobalLoaderState() {
3154
3344
  globalThis.loaders = globalThis.loaders || {};
3155
3345
  const { loaders } = globalThis;
@@ -3922,8 +4112,7 @@ var __exports__ = (() => {
3922
4112
  if (typeof fetchOption === "function") {
3923
4113
  fetchFunction = fetchOption;
3924
4114
  } else if (isObject(fetchOption)) {
3925
- const baseFetch = context?.fetch || fetchFile;
3926
- fetchFunction = (url, requestOptions) => baseFetch(url, mergeFetchOptions(fetchOption, requestOptions));
4115
+ fetchFunction = context?.fetch || fetchFile;
3927
4116
  } else if (context?.fetch) {
3928
4117
  fetchFunction = context.fetch;
3929
4118
  } else {
@@ -3931,18 +4120,11 @@ var __exports__ = (() => {
3931
4120
  }
3932
4121
  return createAuthenticatedFetch({
3933
4122
  fetch: fetchFunction,
3934
- credentials: loaderOptions.core?.credentials || []
4123
+ credentials: loaderOptions.core?.credentials || [],
4124
+ authentications: loaderOptions.core?.authentications,
4125
+ fetchOptions: isObject(fetchOption) ? fetchOption : void 0
3935
4126
  });
3936
4127
  }
3937
- function mergeFetchOptions(defaultOptions, requestOptions) {
3938
- const options = { ...defaultOptions, ...requestOptions };
3939
- if (defaultOptions.headers || requestOptions?.headers) {
3940
- const headers = new Headers(defaultOptions.headers);
3941
- new Headers(requestOptions?.headers).forEach((value, key) => headers.set(key, value));
3942
- options.headers = headers;
3943
- }
3944
- return options;
3945
- }
3946
4128
 
3947
4129
  // src/lib/api/load-loader.ts
3948
4130
  var loaderImplementationPromises = /* @__PURE__ */ new Map();
@@ -4063,7 +4245,13 @@ var __exports__ = (() => {
4063
4245
  `${loader.id} is a SourceLoader. Use load() to create a runtime source object instead of parseFile().`
4064
4246
  );
4065
4247
  }
4066
- const strictOptions = normalizeOptions(options, loader, candidateLoaders, url);
4248
+ const normalizedOptions = normalizeOptions(options, loader, candidateLoaders, url);
4249
+ const authenticationLoader = !loader.getAuthentications && normalizedOptions.core?.credentials?.length ? await getLoaderImplementation(loader, normalizedOptions, url || context?.url) : loader;
4250
+ const strictOptions = await resolveLoaderAuthenticationOptions(
4251
+ authenticationLoader,
4252
+ url,
4253
+ normalizedOptions
4254
+ );
4067
4255
  context = getLoaderContext(
4068
4256
  // @ts-expect-error candidateLoaders may be a single forced loader
4069
4257
  { url, _parse: parse, loaders: candidateLoaders },
@@ -4203,7 +4391,13 @@ var __exports__ = (() => {
4203
4391
  if (!loader) {
4204
4392
  return [];
4205
4393
  }
4206
- const strictOptions = normalizeOptions(options, loader, loaderArray, url);
4394
+ const normalizedOptions = normalizeOptions(options, loader, loaderArray, url);
4395
+ const authenticationLoader = !loader.getAuthentications && normalizedOptions.core?.credentials?.length ? await getLoaderImplementation(loader, normalizedOptions, url || context?.url) : loader;
4396
+ const strictOptions = await resolveLoaderAuthenticationOptions(
4397
+ authenticationLoader,
4398
+ url || "",
4399
+ normalizedOptions
4400
+ );
4207
4401
  context = getLoaderContext(
4208
4402
  { url, _parseInBatches: parseInBatches, _parse: parse, loaders: loaderArray },
4209
4403
  strictOptions,
@@ -4288,19 +4482,23 @@ var __exports__ = (() => {
4288
4482
  } else {
4289
4483
  loadersArray = loaders;
4290
4484
  }
4291
- const fetch2 = getFetchFunction(options || {});
4292
4485
  if (!Array.isArray(files)) {
4293
- return loadOneFileInBatches(files, loadersArray, options || {}, fetch2);
4486
+ return loadOneFileInBatches(files, loadersArray, options || {});
4294
4487
  }
4295
- const promises = files.map(
4296
- (file) => loadOneFileInBatches(file, loadersArray, options || {}, fetch2)
4297
- );
4488
+ const promises = files.map((file) => loadOneFileInBatches(file, loadersArray, options || {}));
4298
4489
  return promises;
4299
4490
  }
4300
- async function loadOneFileInBatches(file, loaders, options, fetch2) {
4491
+ async function loadOneFileInBatches(file, loaders, options) {
4301
4492
  if (typeof file === "string") {
4302
4493
  const url = file;
4303
- const response = await fetch2(url);
4494
+ const loader = await selectLoader(url, loaders, {
4495
+ ...options,
4496
+ core: { ...options.core, nothrow: true }
4497
+ });
4498
+ const authenticationLoader = loader && !loader.getAuthentications && options.core?.credentials?.length ? await getLoaderImplementation(loader, options, url) : loader;
4499
+ options = await resolveLoaderAuthenticationOptions(authenticationLoader, url, options);
4500
+ const fetch2 = getFetchFunction(options);
4501
+ const response = await fetch2(applySearchParamsToUrl(url, options));
4304
4502
  return Array.isArray(loaders) ? await parseInBatches(response, loaders, options) : await parseInBatches(response, loaders, options);
4305
4503
  }
4306
4504
  return Array.isArray(loaders) ? await parseInBatches(file, loaders, options) : await parseInBatches(file, loaders, options);
@@ -4318,8 +4516,14 @@ var __exports__ = (() => {
4318
4516
  resolvedLoaders = loaders;
4319
4517
  resolvedOptions = options;
4320
4518
  }
4519
+ resolvedOptions ||= getGlobalLoaderOptions();
4321
4520
  if (!Array.isArray(resolvedLoaders) && isSourceLoader(resolvedLoaders)) {
4322
4521
  const sourceLoader = await resolveSourceLoader(resolvedLoaders, url, resolvedOptions);
4522
+ resolvedOptions = await resolveLoaderAuthenticationOptions(
4523
+ resolvedLoaders.getAuthentications ? resolvedLoaders : sourceLoader,
4524
+ typeof url === "string" ? url : "",
4525
+ resolvedOptions || {}
4526
+ );
4323
4527
  const runtimeCoreApi = {
4324
4528
  fetchFile,
4325
4529
  parse,
@@ -4337,6 +4541,11 @@ var __exports__ = (() => {
4337
4541
  }
4338
4542
  if (Array.isArray(resolvedLoaders) && resolvedLoaders.length === 1 && isSourceLoader(resolvedLoaders[0])) {
4339
4543
  const sourceLoader = await resolveSourceLoader(resolvedLoaders[0], url, resolvedOptions);
4544
+ resolvedOptions = await resolveLoaderAuthenticationOptions(
4545
+ resolvedLoaders[0].getAuthentications ? resolvedLoaders[0] : sourceLoader,
4546
+ typeof url === "string" ? url : "",
4547
+ resolvedOptions || {}
4548
+ );
4340
4549
  const runtimeCoreApi = {
4341
4550
  fetchFile,
4342
4551
  parse,
@@ -4359,6 +4568,11 @@ var __exports__ = (() => {
4359
4568
  });
4360
4569
  if (selectedLoader && isSourceLoader(selectedLoader)) {
4361
4570
  const sourceLoader = await resolveSourceLoader(selectedLoader, url, resolvedOptions);
4571
+ resolvedOptions = await resolveLoaderAuthenticationOptions(
4572
+ selectedLoader.getAuthentications ? selectedLoader : sourceLoader,
4573
+ typeof url === "string" ? url : "",
4574
+ resolvedOptions || {}
4575
+ );
4362
4576
  return sourceLoader.createDataSource(
4363
4577
  url,
4364
4578
  resolvedOptions || {},
@@ -4379,14 +4593,23 @@ var __exports__ = (() => {
4379
4593
  resolvedOptions,
4380
4594
  url
4381
4595
  );
4596
+ resolvedOptions = await resolveLoaderAuthenticationOptions(
4597
+ selectedLoader.getAuthentications ? selectedLoader : loaderImplementation,
4598
+ url,
4599
+ resolvedOptions || {}
4600
+ );
4382
4601
  const parseUrl = loaderImplementation.parseUrl;
4383
- if (parseUrl) return await parseUrl(url, resolvedOptions, context);
4602
+ if (parseUrl) {
4603
+ const requestUrl = applySearchParamsToUrl(url, resolvedOptions);
4604
+ return await parseUrl(requestUrl, resolvedOptions, context);
4605
+ }
4384
4606
  }
4385
4607
  }
4608
+ if (resolvedOptions) resolvedOptions = resolveAuthenticationOptions(resolvedOptions);
4386
4609
  const fetch2 = getFetchFunction(resolvedOptions);
4387
4610
  let data = url;
4388
4611
  if (typeof url === "string") {
4389
- data = await fetch2(url);
4612
+ data = await fetch2(applySearchParamsToUrl(url, resolvedOptions));
4390
4613
  }
4391
4614
  if (isBlob(url)) {
4392
4615
  data = await fetch2(url);
@@ -4488,7 +4711,13 @@ var __exports__ = (() => {
4488
4711
  `${loader.id} is a SourceLoader. Use load() to create a runtime source object instead of parse().`
4489
4712
  );
4490
4713
  }
4491
- const strictOptions = normalizeOptions(options, loader, candidateLoaders, url);
4714
+ const normalizedOptions = normalizeOptions(options, loader, candidateLoaders, url);
4715
+ const authenticationLoader = !loader.getAuthentications && normalizedOptions.core?.credentials?.length ? await getLoaderImplementation(loader, normalizedOptions, url || context?.url) : loader;
4716
+ const strictOptions = await resolveLoaderAuthenticationOptions(
4717
+ authenticationLoader,
4718
+ url || "",
4719
+ normalizedOptions
4720
+ );
4492
4721
  context = getLoaderContext(
4493
4722
  // @ts-expect-error
4494
4723
  { url, _parse: parse, loaders: candidateLoaders },
@@ -4664,7 +4893,7 @@ var __exports__ = (() => {
4664
4893
  }
4665
4894
 
4666
4895
  // src/lib/api/create-data-source.ts
4667
- function createDataSource(data, sources, options) {
4896
+ function createDataSource(data, sources, options = {}) {
4668
4897
  const resolvedOptions = options || {};
4669
4898
  const type = resolvedOptions?.core?.type || resolvedOptions.type || "auto";
4670
4899
  const source = sources.length === 1 ? sources[0] : type === "auto" ? selectLoaderSync(data, sources, {
@@ -4676,7 +4905,29 @@ var __exports__ = (() => {
4676
4905
  if (!source) {
4677
4906
  throw new Error("Not a valid source type");
4678
4907
  }
4679
- return source.createDataSource(data, resolvedOptions, coreApi);
4908
+ const contributions = resolvedOptions.core?.credentials?.length ? source.getAuthentications?.(typeof data === "string" ? data : "", resolvedOptions) : void 0;
4909
+ if (contributions && "then" in contributions) {
4910
+ void contributions.catch(() => {
4911
+ });
4912
+ throw new Error("Use load() with an asynchronous getAuthentications() hook.");
4913
+ }
4914
+ const authenticatedOptions = resolveAuthenticationOptions(
4915
+ contributions?.length ? {
4916
+ ...resolvedOptions,
4917
+ core: {
4918
+ ...resolvedOptions.core,
4919
+ authentications: [
4920
+ ...resolvedOptions.core?.authentications || [],
4921
+ ...contributions || []
4922
+ ]
4923
+ }
4924
+ } : resolvedOptions
4925
+ );
4926
+ return source.createDataSource(
4927
+ data,
4928
+ authenticatedOptions,
4929
+ coreApi
4930
+ );
4680
4931
  }
4681
4932
  function getSourceOfType(type, sources) {
4682
4933
  for (const service of sources) {