• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / loaders.gl / 20352515932

18 Dec 2025 09:56PM UTC coverage: 35.115% (-28.4%) from 63.485%
20352515932

push

github

web-flow
feat(loader-utils): Export is-type helpers (#3258)

1188 of 1998 branches covered (59.46%)

Branch coverage included in aggregate %.

147 of 211 new or added lines in 13 files covered. (69.67%)

30011 existing lines in 424 files now uncovered.

37457 of 108056 relevant lines covered (34.66%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
/modules/loader-utils/src/loader-types.ts
1
// loaders.gl
1✔
2
// SPDX-License-Identifier: MIT
1✔
3
// Copyright (c) vis.gl contributors
1✔
4

1✔
5
import type {Format} from './format-types';
1✔
6
import {FetchLike, TransformBatches} from './types';
1✔
7
import {ReadableFile} from './lib/files/file';
1✔
8

1✔
9
// LOADERS
1✔
10

1✔
11
/**
1✔
12
 * Core Loader Options
1✔
13
 */
1✔
14
export type StrictLoaderOptions = {
1✔
15
  core?: {
1✔
16
    /** Base URI for resolving relative paths */
1✔
17
    baseUri?: string;
1✔
18
    /** fetch options or a custom fetch function */
1✔
19
    fetch?: typeof fetch | FetchLike | RequestInit | null;
1✔
20
    /** Do not throw on errors */
1✔
21
    nothrow?: boolean;
1✔
22

1✔
23
    /** loader selection, search first for supplied mimeType */
1✔
24
    mimeType?: string;
1✔
25
    /** loader selection, provide fallback mimeType is server does not provide */
1✔
26
    fallbackMimeType?: string;
1✔
27
    /** loader selection, avoid searching registered loaders */
1✔
28
    ignoreRegisteredLoaders?: boolean;
1✔
29

1✔
30
    // general
1✔
31
    /** Experimental: Supply a logger to the parser */
1✔
32
    log?: any;
1✔
33

1✔
34
    // batched parsing
1✔
35

1✔
36
    /** Size of each batch. `auto` matches batches to size of incoming chunks */
1✔
37
    batchSize?: number | 'auto';
1✔
38
    /** Minimal amount of time between batches */
1✔
39
    batchDebounceMs?: number;
1✔
40
    /** Stop loading after a given number of rows (compare SQL limit clause) */
1✔
41
    limit?: 0;
1✔
42
    /** Experimental: Stop loading after reaching */
1✔
43
    _limitMB?: 0;
1✔
44
    /** Generate metadata batches */
1✔
45
    metadata?: boolean;
1✔
46
    /** Transforms to run on incoming batches */
1✔
47
    transforms?: TransformBatches[];
1✔
48

1✔
49
    // module loading
1✔
50

1✔
51
    /** Force to load WASM libraries from local file system in NodeJS or from loaders.gl CDN in a web browser */
1✔
52
    useLocalLibraries?: boolean;
1✔
53

1✔
54
    // workers
1✔
55

1✔
56
    /** CDN load workers from */
1✔
57
    CDN?: string | null;
1✔
58
    /** Set to `false` to disable workers */
1✔
59
    worker?: boolean;
1✔
60
    /** Number of concurrent workers (per loader) on desktop browser */
1✔
61
    maxConcurrency?: number;
1✔
62
    /** Number of concurrent workers (per loader) on mobile browsers */
1✔
63
    maxMobileConcurrency?: number;
1✔
64
    /** Set to `false` to prevent reuse workers */
1✔
65
    reuseWorkers?: boolean;
1✔
66
    /** Whether to use workers under Node.js (experimental) */
1✔
67
    _nodeWorkers?: boolean;
1✔
68
    /** set to 'test' to run local worker */
1✔
69
    _workerType?: string;
1✔
70
  };
1✔
71

1✔
72
  /** Any additional JS libraries */
1✔
73
  modules?: Record<string, unknown>;
1✔
74

1✔
75
  // Accept other keys (loader options objects, e.g. `options.csv`, `options.json` ...)
1✔
76
  [loaderId: string]: Record<string, unknown> | undefined;
1✔
77
};
1✔
78

1✔
79
/**
1✔
80
 * Core Loader Options
1✔
81
 */
1✔
82
export type LoaderOptions = {
1✔
83
  core?: StrictLoaderOptions['core'];
1✔
84
  modules?: StrictLoaderOptions['modules'];
1✔
85

1✔
86
  // Deprecated top-level aliases for core options
1✔
87
  /** @deprecated Use options.core.baseUri */
1✔
88
  baseUri?: string;
1✔
89
  /** @deprecated Use options.core.fetch */
1✔
90
  fetch?: typeof fetch | FetchLike | RequestInit | null;
1✔
91
  /** @deprecated Use options.core.mimeType */
1✔
92
  mimeType?: string;
1✔
93
  /** @deprecated Use options.core.fallbackMimeType */
1✔
94
  fallbackMimeType?: string;
1✔
95
  /** @deprecated Use options.core.ignoreRegisteredLoaders */
1✔
96
  ignoreRegisteredLoaders?: boolean;
1✔
97
  /** @deprecated Use options.core.nothrow */
1✔
98
  nothrow?: boolean;
1✔
99
  /** @deprecated Use options.core.log */
1✔
100
  log?: any;
1✔
101
  /** @deprecated Use options.core.useLocalLibraries */
1✔
102
  useLocalLibraries?: boolean;
1✔
103
  /** @deprecated Use options.core.CDN */
1✔
104
  CDN?: string | null;
1✔
105
  /** @deprecated Use options.core.worker */
1✔
106
  worker?: boolean;
1✔
107
  /** @deprecated Use options.core.maxConcurrency */
1✔
108
  maxConcurrency?: number;
1✔
109
  /** @deprecated Use options.core.maxMobileConcurrency */
1✔
110
  maxMobileConcurrency?: number;
1✔
111
  /** @deprecated Use options.core.reuseWorkers */
1✔
112
  reuseWorkers?: boolean;
1✔
113
  /** @deprecated Use options.core._nodeWorkers */
1✔
114
  _nodeWorkers?: boolean;
1✔
115
  /** @deprecated Use options.core._workerType */
1✔
116
  _workerType?: string;
1✔
117
  /** @deprecated Use options.core._workerType */
1✔
118
  _worker?: string;
1✔
119
  /** @deprecated Use options.core.limit */
1✔
120
  limit?: 0;
1✔
121
  /** @deprecated Use options.core._limitMB */
1✔
122
  _limitMB?: 0;
1✔
123
  /** @deprecated Use options.core.batchSize */
1✔
124
  batchSize?: number | 'auto';
1✔
125
  /** @deprecated Use options.core.batchDebounceMs */
1✔
126
  batchDebounceMs?: number;
1✔
127
  /** @deprecated Use options.core.metadata */
1✔
128
  metadata?: boolean;
1✔
129
  /** @deprecated Use options.core.transforms */
1✔
130
  transforms?: TransformBatches[];
1✔
131

1✔
132
  // Accept other keys (loader options objects, e.g. `options.csv`, `options.json` ...)
1✔
133
  [loaderId: string]: unknown;
1✔
134
};
1✔
135

1✔
136
type PreloadOptions = {
1✔
137
  [key: string]: unknown;
1✔
138
};
1✔
139

1✔
140
/**
1✔
141
 * A worker loader definition that can be used with `@loaders.gl/core` functions
1✔
142
 */
1✔
143
export type Loader<DataT = any, BatchT = any, LoaderOptionsT = StrictLoaderOptions> = Format & {
1✔
144
  /** The result type of this loader  */
1✔
145
  dataType?: DataT;
1✔
146
  /** The batched result type of this loader  */
1✔
147
  batchType?: BatchT;
1✔
148

1✔
149
  /** Default Options */
1✔
150
  options: LoaderOptionsT;
1✔
151
  /** Deprecated Options */
1✔
152
  deprecatedOptions?: Record<string, string | Record<string, string>>;
1✔
153
  /** Version should be injected by build tools */
1✔
154
  version: string;
1✔
155
  /** A boolean, or a URL */
1✔
156
  worker?: string | boolean;
1✔
157
  // end Worker
1✔
158

1✔
159
  /** Human readable name */
1✔
160
  name: string;
1✔
161
  /** id should be the same as the field used in LoaderOptions */
1✔
162
  id: string;
1✔
163
  /** module is used to generate worker threads, need to be the module directory name */
1✔
164
  module: string;
1✔
165
  /** Which category does this loader belong to */
1✔
166
  category?: string;
1✔
167
  /** File extensions that are potential matches with this loader. */
1✔
168
  extensions: string[];
1✔
169
  /** MIMETypes that indicate a match with this loader. @note Some MIMETypes are generic and supported by many loaders */
1✔
170
  mimeTypes: string[];
1✔
171
  /** Is the input of this loader binary */
1✔
172
  binary?: boolean;
1✔
173
  /** Is the input of this loader text */
1✔
174
  text?: boolean;
1✔
175

1✔
176
  /** Test some initial bytes of content to see if this loader might be a match */
1✔
177
  tests?: (((ArrayBuffer: ArrayBuffer) => boolean) | ArrayBuffer | string)[];
1✔
178

1✔
179
  /** @deprecated */
1✔
180
  supported?: boolean;
1✔
181
  /** @deprecated */
1✔
182
  testText?: (string: string) => boolean;
1✔
183
};
1✔
184

1✔
185
/**
1✔
186
 * A "bundled" loader definition that can be used with `@loaders.gl/core` functions
1✔
187
 * If a worker loader is supported it will also be supported.
1✔
188
 */
1✔
189
export type LoaderWithParser<
1✔
190
  DataT = any,
1✔
191
  BatchT = any,
1✔
192
  LoaderOptionsT = StrictLoaderOptions
1✔
193
> = Loader<DataT, BatchT, LoaderOptionsT> & {
1✔
194
  /** Perform actions before load. @deprecated Not officially supported. */
1✔
195
  preload?: Preload;
1✔
196
  /** Parse asynchronously and atomically from an arraybuffer */
1✔
197
  parse: (
1✔
198
    arrayBuffer: ArrayBuffer,
1✔
199
    options?: LoaderOptionsT,
1✔
200
    context?: LoaderContext
1✔
201
  ) => Promise<DataT>;
1✔
202
  /** Parse asynchronously and atomically from a random access "file like" input */
1✔
203
  parseFile?: (
1✔
204
    file: ReadableFile,
1✔
205
    options?: LoaderOptionsT,
1✔
206
    context?: LoaderContext
1✔
207
  ) => Promise<DataT>;
1✔
208
  /** Parse synchronously and atomically from an arraybuffer */
1✔
209
  parseSync?: (
1✔
210
    arrayBuffer: ArrayBuffer,
1✔
211
    options?: LoaderOptionsT,
1✔
212
    context?: LoaderContext
1✔
213
  ) => DataT;
1✔
214
  /** Parse batches of data from an iterator (e.g. fetch stream), return an iterator that yield parsed batches. */
1✔
215
  parseInBatches?: (
1✔
216
    iterator:
1✔
217
      | AsyncIterable<ArrayBufferLike | ArrayBufferView>
1✔
218
      | Iterable<ArrayBufferLike | ArrayBufferView>,
1✔
219
    options?: LoaderOptionsT,
1✔
220
    context?: LoaderContext
1✔
221
  ) => AsyncIterable<BatchT>;
1✔
222
  /** For random access, file like sources, source that don't integrate with fetch. */
1✔
223
  parseFileInBatches?: (
1✔
224
    file: ReadableFile,
1✔
225
    options?: LoaderOptionsT,
1✔
226
    context?: LoaderContext
1✔
227
  ) => AsyncIterable<BatchT>;
1✔
228

1✔
229
  /** Parse atomically from a string asynchronously */
1✔
230
  parseText?: (text: string, options?: LoaderOptionsT, context?: LoaderContext) => Promise<DataT>;
1✔
231
  /** Parse atomically from a string synchronously */
1✔
232
  parseTextSync?: (text: string, options?: LoaderOptionsT, context?: LoaderContext) => DataT;
1✔
233
};
1✔
234

1✔
235
/**
1✔
236
 * A Loader context is provided as a third parameters to a loader object's
1✔
237
 * parse functions when that loader is called by other loaders rather then
1✔
238
 * directly by the application.
1✔
239
 *
1✔
240
 * - The context object allows the subloaders to be aware of the parameters and
1✔
241
 *   options that the application provided in the top level call.
1✔
242
 * - The context also providedsaccess to parse functions so that the subloader
1✔
243
 *   does not need to include the core module.
1✔
244
 * - In addition, the context's parse functions may also redirect loads from worker
1✔
245
 *   threads back to main thread.
1✔
246
 */
1✔
247
export type LoaderContext = {
1✔
248
  /** Loader list provided to top-level loader call plus any sub loaders */
1✔
249
  loaders?: Loader[] | null;
1✔
250
  /** If URL is available.  */
1✔
251
  url?: string;
1✔
252
  /** the file name component of the URL (leading path and query string removed) */
1✔
253
  filename?: string;
1✔
254
  /** the directory name component of the URL (leading path excluding file name and query string) */
1✔
255
  baseUrl?: string;
1✔
256
  /** Query string (characters after `?`) */
1✔
257
  queryString?: string;
1✔
258

1✔
259
  /** Provides access to any application overrides of fetch() */
1✔
260
  fetch: typeof fetch | FetchLike;
1✔
261

1✔
262
  /** TBD */
1✔
263
  response?: Response;
1✔
264

1✔
265
  /**
1✔
266
   * Parse function for subloaders. Avoids importing `core`. In workers, may redirect to main thread
1✔
267
   */
1✔
268
  _parse: (
1✔
269
    arrayBuffer: ArrayBuffer,
1✔
270
    loaders?: Loader | Loader[] | LoaderOptions,
1✔
271
    options?: LoaderOptions,
1✔
272
    context?: LoaderContext
1✔
273
  ) => Promise<any>;
1✔
274

1✔
275
  /**
1✔
276
   * ParseSync function. Avoids importing `core`. In workers, may redirect to main thread
1✔
277
   * @deprecated Do not call directly, use `parseSyncFromContext` instead
1✔
278
   */
1✔
279
  _parseSync?: (
1✔
280
    arrayBuffer: ArrayBuffer,
1✔
281
    loaders?: Loader | Loader[] | LoaderOptions,
1✔
282
    options?: LoaderOptions,
1✔
283
    context?: LoaderContext
1✔
284
  ) => any;
1✔
285

1✔
286
  /**
1✔
287
   * ParseInBatches function. Avoids importing `core`.
1✔
288
   * @deprecated Do not call directly, use `parseInBatchesFromContext` instead
1✔
289
   */
1✔
290
  _parseInBatches?: (
1✔
291
    iterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer> | Response,
1✔
292
    loaders?: Loader | Loader[] | LoaderOptions,
1✔
293
    options?: LoaderOptions,
1✔
294
    context?: LoaderContext
1✔
295
  ) => AsyncIterable<any> | Promise<AsyncIterable<any>>;
1✔
296
};
1✔
297

1✔
298
// type Parse = (
1✔
299
//   arrayBuffer: ArrayBuffer,
1✔
300
//   options?: LoaderOptions,
1✔
301
//   context?: LoaderContext
1✔
302
// ) => Promise<any>;
1✔
303
// type ParseSync = (
1✔
304
//   arrayBuffer: ArrayBuffer,
1✔
305
//   options?: LoaderOptions,
1✔
306
//   context?: LoaderContext
1✔
307
// ) => any;
1✔
308
// type ParseText = (text: string, options?: LoaderOptions) => Promise<any>;
1✔
309
// type ParseTextSync = (text: string, options?: LoaderOptions) => any;
1✔
310
// type ParseInBatches = (
1✔
311
//   iterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
1✔
312
//   options?: LoaderOptions,
1✔
313
//   context?: LoaderContext
1✔
314
// ) => AsyncIterable<any>;
1✔
315
// type ParseFileInBatches = (
1✔
316
//   file: Blob,
1✔
317
//   options?: LoaderOptions,
1✔
318
//   context?: LoaderContext
1✔
319
// ) => AsyncIterable<any>;
1✔
320

1✔
321
type Preload = (url: string, options?: PreloadOptions) => any;
1✔
322

1✔
323
/** Typescript helper to extract options type from a loader type */
1✔
324
export type LoaderOptionsType<T = Loader> =
1✔
325
  T extends Loader<unknown, unknown, infer Options> ? Options : never;
1✔
326
/** Typescript helper to extract data type from a loader type */
1✔
327
export type LoaderReturnType<T = Loader> =
1✔
328
  T extends Loader<infer Return, unknown, unknown> ? Return : never;
1✔
329
/** Typescript helper to extract batch type from a loader type */
1✔
330
export type LoaderBatchType<T = Loader> =
1✔
331
  T extends Loader<unknown, infer Batch, unknown> ? Batch : never;
1✔
332

1✔
333
/** Typescript helper to extract options type from an array of loader types */
1✔
334
export type LoaderArrayOptionsType<LoadersT extends Loader[] = Loader[]> =
1✔
335
  LoadersT[number]['options'];
1✔
336
/** Typescript helper to extract data type from a loader type */
1✔
337
export type LoaderArrayReturnType<LoadersT extends Loader[] = Loader[]> =
1✔
338
  LoadersT[number]['dataType'];
1✔
339
/** Typescript helper to extract batch type from a loader type */
1✔
340
export type LoaderArrayBatchType<LoadersT extends Loader[] = Loader[]> =
1✔
341
  LoadersT[number]['batchType'];
1✔
342

1✔
343
/**
1✔
344
 * Parses `data` asynchronously using the supplied loader, parse function provided via the loader context
1✔
345
 */
1✔
346
export async function parseFromContext<
1✔
347
  LoaderT extends Loader,
1✔
348
  OptionsT extends StrictLoaderOptions = LoaderOptionsType<LoaderT>
1✔
349
>(
1✔
350
  data: ArrayBuffer,
1✔
351
  loader: LoaderT,
1✔
352
  options: OptionsT | undefined,
1✔
353
  context: LoaderContext
1✔
354
): Promise<LoaderReturnType<LoaderT>>;
1✔
355

1✔
356
/**
1✔
357
 * Parses `data` asynchronously by matching one of the supplied loader
1✔
358
 */
1✔
359
export async function parseFromContext(
1✔
360
  data: ArrayBuffer,
1✔
361
  loaders: Loader[],
1✔
362
  options: LoaderOptions | undefined,
1✔
363
  context: LoaderContext
1✔
364
): Promise<unknown>;
1✔
365

1✔
366
/**
1✔
367
 * Parses `data` using a specified loader
1✔
368
 * @param data
1✔
369
 * @param loaders
1✔
370
 * @param options
1✔
371
 * @param context
1✔
372
 */
1✔
373
// implementation signature
1✔
UNCOV
374
export async function parseFromContext(
✔
UNCOV
375
  data: ArrayBuffer,
×
UNCOV
376
  loaders: Loader | Loader[],
×
UNCOV
377
  options: LoaderOptions | undefined,
×
UNCOV
378
  context: LoaderContext
×
UNCOV
379
): Promise<unknown> {
×
UNCOV
380
  return context._parse(data, loaders, options, context);
×
UNCOV
381
}
×
382

1✔
383
/**
1✔
384
 * Parses `data` synchronously using the specified loader, parse function provided via the loader context
1✔
385
 */
1✔
386
export function parseSyncFromContext<
1✔
387
  LoaderT extends Loader,
×
388
  OptionsT extends StrictLoaderOptions = LoaderOptionsType<LoaderT>
×
389
>(
×
390
  data: ArrayBuffer,
×
391
  loader: LoaderT,
×
392
  options: OptionsT | undefined,
×
393
  context: LoaderContext
×
394
): LoaderReturnType<LoaderT> {
×
395
  if (!context._parseSync) {
×
396
    throw new Error('parseSync');
×
397
  }
×
398
  return context._parseSync(data, loader, options, context);
×
399
}
×
400

1✔
401
/**
1✔
402
 * Parses `data` synchronously using a specified loader, parse function provided via the loader context
1✔
403
 */
1✔
404
export async function parseInBatchesFromContext<
1✔
405
  LoaderT extends Loader,
1✔
406
  OptionsT extends StrictLoaderOptions = LoaderOptionsType<LoaderT>
1✔
407
>(
1✔
408
  data: Iterable<ArrayBuffer> | AsyncIterable<ArrayBuffer> | Response,
1✔
409
  loader: LoaderT,
1✔
410
  options: OptionsT | undefined,
1✔
411
  context: LoaderContext
1✔
412
): Promise<AsyncIterable<LoaderBatchType<LoaderT>>> {
1✔
413
  if (!context._parseInBatches) {
1!
414
    throw new Error('parseInBatches');
×
415
  }
×
416
  return context._parseInBatches(data, loader, options, context);
1✔
417
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc