@loaders.gl/core 4.0.0-alpha.23 → 4.0.0-alpha.24

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 (57) hide show
  1. package/dist/dist.min.js +40 -100
  2. package/dist/es5/lib/init.js +1 -1
  3. package/dist/es5/null-loader.js +1 -1
  4. package/dist/esm/lib/init.js +1 -1
  5. package/dist/esm/null-loader.js +1 -1
  6. package/dist/null-worker-node.js +1 -1
  7. package/dist/null-worker.js +1 -1
  8. package/package.json +4 -7
  9. package/dist/bundle.js +0 -5
  10. package/dist/core-addons/write-file-browser.js +0 -60
  11. package/dist/index.js +0 -105
  12. package/dist/iterators/batch-iterators/timed-batch-iterator.js +0 -22
  13. package/dist/iterators/make-iterator/make-array-buffer-iterator.js +0 -27
  14. package/dist/iterators/make-iterator/make-blob-iterator.js +0 -21
  15. package/dist/iterators/make-iterator/make-iterator.js +0 -37
  16. package/dist/iterators/make-iterator/make-stream-iterator.js +0 -96
  17. package/dist/iterators/make-iterator/make-string-iterator.js +0 -24
  18. package/dist/iterators/make-stream/make-dom-stream.js +0 -47
  19. package/dist/iterators/make-stream/make-node-stream.js +0 -85
  20. package/dist/javascript-utils/is-type.js +0 -41
  21. package/dist/lib/api/encode-table.js +0 -54
  22. package/dist/lib/api/encode.js +0 -121
  23. package/dist/lib/api/load-in-batches.js +0 -40
  24. package/dist/lib/api/load.js +0 -43
  25. package/dist/lib/api/loader-options.js +0 -7
  26. package/dist/lib/api/parse-in-batches.js +0 -117
  27. package/dist/lib/api/parse-sync.js +0 -54
  28. package/dist/lib/api/parse.js +0 -87
  29. package/dist/lib/api/register-loaders.js +0 -35
  30. package/dist/lib/api/save.js +0 -15
  31. package/dist/lib/api/select-loader.js +0 -258
  32. package/dist/lib/common.js +0 -2
  33. package/dist/lib/fetch/fetch-error-message.js +0 -25
  34. package/dist/lib/fetch/fetch-file.js +0 -61
  35. package/dist/lib/fetch/fetch-file.node.js +0 -57
  36. package/dist/lib/fetch/read-array-buffer.js +0 -41
  37. package/dist/lib/fetch/read-file.js +0 -29
  38. package/dist/lib/fetch/write-file.js +0 -22
  39. package/dist/lib/filesystems/browser-filesystem.js +0 -127
  40. package/dist/lib/filesystems/read-array-buffer.js +0 -29
  41. package/dist/lib/init.js +0 -17
  42. package/dist/lib/loader-utils/check-errors.js +0 -46
  43. package/dist/lib/loader-utils/get-data.js +0 -130
  44. package/dist/lib/loader-utils/get-fetch-function.js +0 -31
  45. package/dist/lib/loader-utils/loader-context.js +0 -59
  46. package/dist/lib/loader-utils/loggers.js +0 -41
  47. package/dist/lib/loader-utils/normalize-loader.js +0 -52
  48. package/dist/lib/loader-utils/option-defaults.js +0 -44
  49. package/dist/lib/loader-utils/option-utils.js +0 -162
  50. package/dist/lib/progress/fetch-progress.js +0 -60
  51. package/dist/lib/utils/log.js +0 -6
  52. package/dist/lib/utils/mime-type-utils.js +0 -42
  53. package/dist/lib/utils/resource-utils.js +0 -90
  54. package/dist/lib/utils/response-utils.js +0 -115
  55. package/dist/lib/utils/url-utils.js +0 -14
  56. package/dist/null-loader.js +0 -51
  57. package/dist/workers/null-worker.js +0 -5
@@ -1,117 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parseInBatches = void 0;
5
- const loader_utils_1 = require("@loaders.gl/loader-utils");
6
- const normalize_loader_1 = require("../loader-utils/normalize-loader");
7
- const option_utils_1 = require("../loader-utils/option-utils");
8
- const loader_context_1 = require("../loader-utils/loader-context");
9
- const get_data_1 = require("../loader-utils/get-data");
10
- const resource_utils_1 = require("../utils/resource-utils");
11
- const select_loader_1 = require("./select-loader");
12
- // Ensure `parse` is available in context if loader falls back to `parse`
13
- const parse_1 = require("./parse");
14
- /**
15
- * Parses `data` using a specified loader
16
- * @param data
17
- * @param loaders
18
- * @param options
19
- * @param context
20
- */
21
- async function parseInBatches(data, loaders, options, context) {
22
- const loaderArray = Array.isArray(loaders) ? loaders : undefined;
23
- // Signature: parseInBatches(data, options, url) - Uses registered loaders
24
- if (!Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
25
- context = undefined; // context not supported in short signature
26
- options = loaders;
27
- loaders = undefined;
28
- }
29
- data = await data; // Resolve any promise
30
- options = options || {};
31
- // Extract a url for auto detection
32
- const url = (0, resource_utils_1.getResourceUrl)(data);
33
- // Chooses a loader and normalizes it
34
- // Note - only uses URL and contentType for streams and iterator inputs
35
- const loader = await (0, select_loader_1.selectLoader)(data, loaders, options);
36
- // Note: if options.nothrow was set, it is possible that no loader was found, if so just return null
37
- if (!loader) {
38
- // @ts-ignore
39
- return null;
40
- }
41
- // Normalize options
42
- options = (0, option_utils_1.normalizeOptions)(options, loader, loaderArray, url);
43
- context = (0, loader_context_1.getLoaderContext)({ url, _parseInBatches: parseInBatches, _parse: parse_1.parse, loaders: loaderArray }, options, context || null);
44
- return await parseWithLoaderInBatches(loader, data, options, context);
45
- }
46
- exports.parseInBatches = parseInBatches;
47
- /**
48
- * Loader has been selected and context has been prepared, see if we need to emit a metadata batch
49
- */
50
- async function parseWithLoaderInBatches(loader, data, options, context) {
51
- const outputIterator = await parseToOutputIterator(loader, data, options, context);
52
- // Generate metadata batch if requested
53
- if (!options.metadata) {
54
- return outputIterator;
55
- }
56
- const metadataBatch = {
57
- batchType: 'metadata',
58
- metadata: {
59
- _loader: loader,
60
- _context: context
61
- },
62
- // Populate with some default fields to avoid crashing
63
- data: [],
64
- bytesUsed: 0
65
- };
66
- async function* makeMetadataBatchIterator(iterator) {
67
- yield metadataBatch;
68
- yield* iterator;
69
- }
70
- return makeMetadataBatchIterator(outputIterator);
71
- }
72
- /**
73
- * Prep work is done, now it is time to start parsing into an output operator
74
- * The approach depends on which parse function the loader exposes
75
- * `parseInBatches` (preferred), `parse` (fallback)
76
- */
77
- async function parseToOutputIterator(loader, data, options, context) {
78
- // Get an iterator from the input
79
- const inputIterator = await (0, get_data_1.getAsyncIterableFromData)(data, options);
80
- // Apply any iterator transforms (options.transforms)
81
- const transformedIterator = await applyInputTransforms(inputIterator, options?.transforms || []);
82
- // If loader supports parseInBatches, we are done
83
- if (loader.parseInBatches) {
84
- return loader.parseInBatches(transformedIterator, options, context);
85
- }
86
- // Fallback: load atomically using `parse` concatenating input iterator into single chunk
87
- async function* parseChunkInBatches() {
88
- const arrayBuffer = await (0, loader_utils_1.concatenateArrayBuffersAsync)(transformedIterator);
89
- // Call `parse` instead of `loader.parse` to ensure we can call workers etc.
90
- const parsedData = await (0, parse_1.parse)(arrayBuffer, loader,
91
- // TODO - Hack: supply loaders MIME type to ensure we match it
92
- { ...options, mimeType: loader.mimeTypes[0] }, context);
93
- // yield a single batch, the output from loader.parse()
94
- // TODO - run through batch builder to apply options etc...
95
- const batch = {
96
- mimeType: loader.mimeTypes[0],
97
- shape: Array.isArray(parsedData) ? 'row-table' : 'unknown',
98
- batchType: 'data',
99
- data: parsedData,
100
- length: Array.isArray(parsedData) ? parsedData.length : 1
101
- };
102
- yield batch;
103
- }
104
- return parseChunkInBatches();
105
- }
106
- /**
107
- * Create an iterator chain with any transform iterators (crypto, decompression)
108
- * @param inputIterator
109
- * @param options
110
- */
111
- async function applyInputTransforms(inputIterator, transforms = []) {
112
- let iteratorChain = inputIterator;
113
- for await (const transformBatches of transforms) {
114
- iteratorChain = transformBatches(iteratorChain);
115
- }
116
- return iteratorChain;
117
- }
@@ -1,54 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parseSync = void 0;
5
- const select_loader_1 = require("./select-loader");
6
- const normalize_loader_1 = require("../loader-utils/normalize-loader");
7
- const option_utils_1 = require("../loader-utils/option-utils");
8
- const get_data_1 = require("../loader-utils/get-data");
9
- const loader_context_1 = require("../loader-utils/loader-context");
10
- const resource_utils_1 = require("../utils/resource-utils");
11
- /**
12
- * Parses `data` synchronously using a specified loader
13
- */
14
- function parseSync(data, loaders, options, context) {
15
- // Signature: parseSync(data, options)
16
- // Uses registered loaders
17
- if (!Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
18
- context = undefined; // context not supported in short signature
19
- options = loaders;
20
- loaders = undefined;
21
- }
22
- options = options || {};
23
- // Chooses a loader (and normalizes it)
24
- // Also use any loaders in the context, new loaders take priority
25
- const typedLoaders = loaders;
26
- const candidateLoaders = (0, loader_context_1.getLoadersFromContext)(typedLoaders, context);
27
- const loader = (0, select_loader_1.selectLoaderSync)(data, candidateLoaders, options);
28
- // Note: if nothrow option was set, it is possible that no loader was found, if so just return null
29
- if (!loader) {
30
- return null;
31
- }
32
- // Normalize options
33
- options = (0, option_utils_1.normalizeOptions)(options, loader, candidateLoaders);
34
- // Extract a url for auto detection
35
- const url = (0, resource_utils_1.getResourceUrl)(data);
36
- const parse = () => {
37
- throw new Error('parseSync called parse (which is async');
38
- };
39
- context = (0, loader_context_1.getLoaderContext)({ url, _parseSync: parse, _parse: parse, loaders: loaders }, options, context || null);
40
- return parseWithLoaderSync(loader, data, options, context);
41
- }
42
- exports.parseSync = parseSync;
43
- // TODO - should accept loader.parseSync/parse and generate 1 chunk asyncIterator
44
- function parseWithLoaderSync(loader, data, options, context) {
45
- data = (0, get_data_1.getArrayBufferOrStringFromDataSync)(data, loader, options);
46
- if (loader.parseTextSync && typeof data === 'string') {
47
- return loader.parseTextSync(data, options); // , context, loader);
48
- }
49
- if (loader.parseSync && data instanceof ArrayBuffer) {
50
- return loader.parseSync(data, options, context); // , loader);
51
- }
52
- // TBD - If synchronous parser not available, return null
53
- throw new Error(`${loader.name} loader: 'parseSync' not supported by this loader, use 'parse' instead. ${context.url || ''}`);
54
- }
@@ -1,87 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parse = void 0;
5
- const loader_utils_1 = require("@loaders.gl/loader-utils");
6
- const worker_utils_1 = require("@loaders.gl/worker-utils");
7
- const normalize_loader_1 = require("../loader-utils/normalize-loader");
8
- const is_type_1 = require("../../javascript-utils/is-type");
9
- const option_utils_1 = require("../loader-utils/option-utils");
10
- const get_data_1 = require("../loader-utils/get-data");
11
- const loader_context_1 = require("../loader-utils/loader-context");
12
- const resource_utils_1 = require("../utils/resource-utils");
13
- const select_loader_1 = require("./select-loader");
14
- /**
15
- * Parses `data` using a specified loader
16
- * @param data
17
- * @param loaders
18
- * @param options
19
- * @param context
20
- */
21
- // implementation signature
22
- async function parse(data, loaders, options, context) {
23
- // Signature: parse(data, options, context | url)
24
- // Uses registered loaders
25
- if (loaders && !Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
26
- context = undefined; // context not supported in short signature
27
- options = loaders;
28
- loaders = undefined;
29
- }
30
- data = await data; // Resolve any promise
31
- options = options || {}; // Could be invalid...
32
- // Extract a url for auto detection
33
- const url = (0, resource_utils_1.getResourceUrl)(data);
34
- // Chooses a loader (and normalizes it)
35
- // Also use any loaders in the context, new loaders take priority
36
- const typedLoaders = loaders;
37
- const candidateLoaders = (0, loader_context_1.getLoadersFromContext)(typedLoaders, context);
38
- // todo hacky type cast
39
- const loader = await (0, select_loader_1.selectLoader)(data, candidateLoaders, options);
40
- // Note: if no loader was found, if so just return null
41
- if (!loader) {
42
- return null;
43
- }
44
- // Normalize options
45
- // @ts-expect-error
46
- options = (0, option_utils_1.normalizeOptions)(options, loader, candidateLoaders, url); // Could be invalid...
47
- // Get a context (if already present, will be unchanged)
48
- context = (0, loader_context_1.getLoaderContext)(
49
- // @ts-expect-error
50
- { url, _parse: parse, loaders: candidateLoaders }, options, context || null);
51
- return await parseWithLoader(loader, data, options, context);
52
- }
53
- exports.parse = parse;
54
- // TODO: support progress and abort
55
- // TODO - should accept loader.parseAsyncIterator and concatenate.
56
- async function parseWithLoader(loader, data, options, context) {
57
- (0, worker_utils_1.validateWorkerVersion)(loader);
58
- if ((0, is_type_1.isResponse)(data)) {
59
- // Serialize to support passing the response to web worker
60
- const response = data;
61
- const { ok, redirected, status, statusText, type, url } = response;
62
- const headers = Object.fromEntries(response.headers.entries());
63
- // @ts-expect-error TODO - fix this
64
- context.response = { headers, ok, redirected, status, statusText, type, url };
65
- }
66
- data = await (0, get_data_1.getArrayBufferOrStringFromData)(data, loader, options);
67
- const loaderWithParser = loader;
68
- // First check for synchronous text parser, wrap results in promises
69
- if (loaderWithParser.parseTextSync && typeof data === 'string') {
70
- return loaderWithParser.parseTextSync(data, options, context);
71
- }
72
- // If we have a workerUrl and the loader can parse the given options efficiently in a worker
73
- if ((0, loader_utils_1.canParseWithWorker)(loader, options)) {
74
- return await (0, loader_utils_1.parseWithWorker)(loader, data, options, context, parse);
75
- }
76
- // Check for asynchronous parser
77
- if (loaderWithParser.parseText && typeof data === 'string') {
78
- return await loaderWithParser.parseText(data, options, context);
79
- }
80
- if (loaderWithParser.parse) {
81
- return await loaderWithParser.parse(data, options, context);
82
- }
83
- // This should not happen, all sync loaders should also offer `parse` function
84
- (0, worker_utils_1.assert)(!loaderWithParser.parseSync);
85
- // TBD - If asynchronous parser not available, return null
86
- throw new Error(`${loader.id} loader - no parser found and worker is disabled`);
87
- }
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._unregisterLoaders = exports.getRegisteredLoaders = exports.registerLoaders = void 0;
4
- const normalize_loader_1 = require("../loader-utils/normalize-loader");
5
- const option_utils_1 = require("../loader-utils/option-utils");
6
- // Store global registered loaders on the global object to increase chances of cross loaders-version interoperability
7
- // This use case is not reliable but can help when testing new versions of loaders.gl with existing frameworks
8
- const getGlobalLoaderRegistry = () => {
9
- const state = (0, option_utils_1.getGlobalLoaderState)();
10
- state.loaderRegistry = state.loaderRegistry || [];
11
- return state.loaderRegistry;
12
- };
13
- /** Register a list of global loaders */
14
- function registerLoaders(loaders) {
15
- const loaderRegistry = getGlobalLoaderRegistry();
16
- loaders = Array.isArray(loaders) ? loaders : [loaders];
17
- for (const loader of loaders) {
18
- const normalizedLoader = (0, normalize_loader_1.normalizeLoader)(loader);
19
- if (!loaderRegistry.find((registeredLoader) => normalizedLoader === registeredLoader)) {
20
- // add to the beginning of the loaderRegistry, so the last registeredLoader get picked
21
- loaderRegistry.unshift(normalizedLoader);
22
- }
23
- }
24
- }
25
- exports.registerLoaders = registerLoaders;
26
- function getRegisteredLoaders() {
27
- return getGlobalLoaderRegistry();
28
- }
29
- exports.getRegisteredLoaders = getRegisteredLoaders;
30
- /** @deprecated For testing only */
31
- function _unregisterLoaders() {
32
- const state = (0, option_utils_1.getGlobalLoaderState)();
33
- state.loaderRegistry = [];
34
- }
35
- exports._unregisterLoaders = _unregisterLoaders;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.saveSync = exports.save = void 0;
4
- const encode_1 = require("./encode");
5
- const write_file_1 = require("../fetch/write-file");
6
- async function save(data, url, writer, options) {
7
- const encodedData = await (0, encode_1.encode)(data, writer, options);
8
- return await (0, write_file_1.writeFile)(url, encodedData);
9
- }
10
- exports.save = save;
11
- function saveSync(data, url, writer, options) {
12
- const encodedData = (0, encode_1.encodeSync)(data, writer, options);
13
- return (0, write_file_1.writeFileSync)(url, encodedData);
14
- }
15
- exports.saveSync = saveSync;
@@ -1,258 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.selectLoaderSync = exports.selectLoader = void 0;
4
- const loader_utils_1 = require("@loaders.gl/loader-utils");
5
- const normalize_loader_1 = require("../loader-utils/normalize-loader");
6
- const log_1 = require("../utils/log");
7
- const resource_utils_1 = require("../utils/resource-utils");
8
- const register_loaders_1 = require("./register-loaders");
9
- const is_type_1 = require("../../javascript-utils/is-type");
10
- const url_utils_1 = require("../utils/url-utils");
11
- const EXT_PATTERN = /\.([^.]+)$/;
12
- // TODO - Need a variant that peeks at streams for parseInBatches
13
- // TODO - Detect multiple matching loaders? Use heuristics to grade matches?
14
- // TODO - Allow apps to pass context to disambiguate between multiple matches (e.g. multiple .json formats)?
15
- /**
16
- * Find a loader that matches file extension and/or initial file content
17
- * Search the loaders array argument for a loader that matches url extension or initial data
18
- * Returns: a normalized loader
19
- * @param data data to assist
20
- * @param loaders
21
- * @param options
22
- * @param context used internally, applications should not provide this parameter
23
- */
24
- async function selectLoader(data, loaders = [], options, context) {
25
- if (!validHTTPResponse(data)) {
26
- return null;
27
- }
28
- // First make a sync attempt, disabling exceptions
29
- let loader = selectLoaderSync(data, loaders, { ...options, nothrow: true }, context);
30
- if (loader) {
31
- return loader;
32
- }
33
- // For Blobs and Files, try to asynchronously read a small initial slice and test again with that
34
- // to see if we can detect by initial content
35
- if ((0, is_type_1.isBlob)(data)) {
36
- data = await data.slice(0, 10).arrayBuffer();
37
- loader = selectLoaderSync(data, loaders, options, context);
38
- }
39
- // no loader available
40
- if (!loader && !options?.nothrow) {
41
- throw new Error(getNoValidLoaderMessage(data));
42
- }
43
- return loader;
44
- }
45
- exports.selectLoader = selectLoader;
46
- /**
47
- * Find a loader that matches file extension and/or initial file content
48
- * Search the loaders array argument for a loader that matches url extension or initial data
49
- * Returns: a normalized loader
50
- * @param data data to assist
51
- * @param loaders
52
- * @param options
53
- * @param context used internally, applications should not provide this parameter
54
- */
55
- function selectLoaderSync(data, loaders = [], options, context) {
56
- if (!validHTTPResponse(data)) {
57
- return null;
58
- }
59
- // eslint-disable-next-line complexity
60
- // if only a single loader was provided (not as array), force its use
61
- // TODO - Should this behavior be kept and documented?
62
- if (loaders && !Array.isArray(loaders)) {
63
- // TODO - remove support for legacy loaders
64
- return (0, normalize_loader_1.normalizeLoader)(loaders);
65
- }
66
- // Build list of candidate loaders that will be searched in order for a match
67
- let candidateLoaders = [];
68
- // First search supplied loaders
69
- if (loaders) {
70
- candidateLoaders = candidateLoaders.concat(loaders);
71
- }
72
- // Then fall back to registered loaders
73
- if (!options?.ignoreRegisteredLoaders) {
74
- candidateLoaders.push(...(0, register_loaders_1.getRegisteredLoaders)());
75
- }
76
- // TODO - remove support for legacy loaders
77
- normalizeLoaders(candidateLoaders);
78
- const loader = selectLoaderInternal(data, candidateLoaders, options, context);
79
- // no loader available
80
- if (!loader && !options?.nothrow) {
81
- throw new Error(getNoValidLoaderMessage(data));
82
- }
83
- return loader;
84
- }
85
- exports.selectLoaderSync = selectLoaderSync;
86
- /** Implements loaders selection logic */
87
- // eslint-disable-next-line complexity
88
- function selectLoaderInternal(data, loaders, options, context) {
89
- const url = (0, resource_utils_1.getResourceUrl)(data);
90
- const type = (0, resource_utils_1.getResourceMIMEType)(data);
91
- const testUrl = (0, url_utils_1.stripQueryString)(url) || context?.url;
92
- let loader = null;
93
- let reason = '';
94
- // if options.mimeType is supplied, it takes precedence
95
- if (options?.mimeType) {
96
- loader = findLoaderByMIMEType(loaders, options?.mimeType);
97
- reason = `match forced by supplied MIME type ${options?.mimeType}`;
98
- }
99
- // Look up loader by url
100
- loader = loader || findLoaderByUrl(loaders, testUrl);
101
- reason = reason || (loader ? `matched url ${testUrl}` : '');
102
- // Look up loader by mime type
103
- loader = loader || findLoaderByMIMEType(loaders, type);
104
- reason = reason || (loader ? `matched MIME type ${type}` : '');
105
- // Look for loader via initial bytes (Note: not always accessible (e.g. Response, stream, async iterator)
106
- // @ts-ignore Blob | Response
107
- loader = loader || findLoaderByInitialBytes(loaders, data);
108
- // @ts-ignore Blob | Response
109
- reason = reason || (loader ? `matched initial data ${getFirstCharacters(data)}` : '');
110
- // Look up loader by fallback mime type
111
- if (options?.fallbackMimeType) {
112
- loader = loader || findLoaderByMIMEType(loaders, options?.fallbackMimeType);
113
- reason = reason || (loader ? `matched fallback MIME type ${type}` : '');
114
- }
115
- if (reason) {
116
- log_1.log.log(1, `selectLoader selected ${loader?.name}: ${reason}.`);
117
- }
118
- return loader;
119
- }
120
- /** Check HTTP Response */
121
- function validHTTPResponse(data) {
122
- // HANDLE HTTP status
123
- if (data instanceof Response) {
124
- // 204 - NO CONTENT. This handles cases where e.g. a tile server responds with 204 for a missing tile
125
- if (data.status === 204) {
126
- return false;
127
- }
128
- }
129
- return true;
130
- }
131
- /** Generate a helpful message to help explain why loader selection failed. */
132
- function getNoValidLoaderMessage(data) {
133
- const url = (0, resource_utils_1.getResourceUrl)(data);
134
- const type = (0, resource_utils_1.getResourceMIMEType)(data);
135
- let message = 'No valid loader found (';
136
- message += url ? `${loader_utils_1.path.filename(url)}, ` : 'no url provided, ';
137
- message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
138
- // First characters are only accessible when called on data (string or arrayBuffer).
139
- // @ts-ignore Blob | Response
140
- const firstCharacters = data ? getFirstCharacters(data) : '';
141
- message += firstCharacters ? ` first bytes: "${firstCharacters}"` : 'first bytes: not available';
142
- message += ')';
143
- return message;
144
- }
145
- function normalizeLoaders(loaders) {
146
- for (const loader of loaders) {
147
- (0, normalize_loader_1.normalizeLoader)(loader);
148
- }
149
- }
150
- // TODO - Would be nice to support http://example.com/file.glb?parameter=1
151
- // E.g: x = new URL('http://example.com/file.glb?load=1'; x.pathname
152
- function findLoaderByUrl(loaders, url) {
153
- // Get extension
154
- const match = url && EXT_PATTERN.exec(url);
155
- const extension = match && match[1];
156
- return extension ? findLoaderByExtension(loaders, extension) : null;
157
- }
158
- function findLoaderByExtension(loaders, extension) {
159
- extension = extension.toLowerCase();
160
- for (const loader of loaders) {
161
- for (const loaderExtension of loader.extensions) {
162
- if (loaderExtension.toLowerCase() === extension) {
163
- return loader;
164
- }
165
- }
166
- }
167
- return null;
168
- }
169
- function findLoaderByMIMEType(loaders, mimeType) {
170
- for (const loader of loaders) {
171
- if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
172
- return loader;
173
- }
174
- // Support referring to loaders using the "unregistered tree"
175
- // https://en.wikipedia.org/wiki/Media_type#Unregistered_tree
176
- if (mimeType === `application/x.${loader.id}`) {
177
- return loader;
178
- }
179
- }
180
- return null;
181
- }
182
- function findLoaderByInitialBytes(loaders, data) {
183
- if (!data) {
184
- return null;
185
- }
186
- for (const loader of loaders) {
187
- if (typeof data === 'string') {
188
- if (testDataAgainstText(data, loader)) {
189
- return loader;
190
- }
191
- }
192
- else if (ArrayBuffer.isView(data)) {
193
- // Typed Arrays can have offsets into underlying buffer
194
- if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) {
195
- return loader;
196
- }
197
- }
198
- else if (data instanceof ArrayBuffer) {
199
- const byteOffset = 0;
200
- if (testDataAgainstBinary(data, byteOffset, loader)) {
201
- return loader;
202
- }
203
- }
204
- // TODO Handle streaming case (requires creating a new AsyncIterator)
205
- }
206
- return null;
207
- }
208
- function testDataAgainstText(data, loader) {
209
- if (loader.testText) {
210
- return loader.testText(data);
211
- }
212
- const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
213
- return tests.some((test) => data.startsWith(test));
214
- }
215
- function testDataAgainstBinary(data, byteOffset, loader) {
216
- const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
217
- return tests.some((test) => testBinary(data, byteOffset, loader, test));
218
- }
219
- function testBinary(data, byteOffset, loader, test) {
220
- if (test instanceof ArrayBuffer) {
221
- return (0, loader_utils_1.compareArrayBuffers)(test, data, test.byteLength);
222
- }
223
- switch (typeof test) {
224
- case 'function':
225
- return test(data);
226
- case 'string':
227
- // Magic bytes check: If `test` is a string, check if binary data starts with that strings
228
- const magic = getMagicString(data, byteOffset, test.length);
229
- return test === magic;
230
- default:
231
- return false;
232
- }
233
- }
234
- function getFirstCharacters(data, length = 5) {
235
- if (typeof data === 'string') {
236
- return data.slice(0, length);
237
- }
238
- else if (ArrayBuffer.isView(data)) {
239
- // Typed Arrays can have offsets into underlying buffer
240
- return getMagicString(data.buffer, data.byteOffset, length);
241
- }
242
- else if (data instanceof ArrayBuffer) {
243
- const byteOffset = 0;
244
- return getMagicString(data, byteOffset, length);
245
- }
246
- return '';
247
- }
248
- function getMagicString(arrayBuffer, byteOffset, length) {
249
- if (arrayBuffer.byteLength < byteOffset + length) {
250
- return '';
251
- }
252
- const dataView = new DataView(arrayBuffer);
253
- let magic = '';
254
- for (let i = 0; i < length; i++) {
255
- magic += String.fromCharCode(dataView.getUint8(byteOffset + i));
256
- }
257
- return magic;
258
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getErrorMessageFromResponse = exports.getErrorMessageFromResponseSync = void 0;
4
- function getErrorMessageFromResponseSync(response) {
5
- return `Failed to fetch resource ${response.url}(${response.status}): ${response.statusText} `;
6
- }
7
- exports.getErrorMessageFromResponseSync = getErrorMessageFromResponseSync;
8
- async function getErrorMessageFromResponse(response) {
9
- let message = `Failed to fetch resource ${response.url} (${response.status}): `;
10
- try {
11
- const contentType = response.headers.get('Content-Type') || '';
12
- if (contentType.includes('application/json')) {
13
- message += await response.text();
14
- }
15
- else {
16
- message += response.statusText;
17
- }
18
- }
19
- catch (error) {
20
- // eslint forbids return in finally statement
21
- return message;
22
- }
23
- return message;
24
- }
25
- exports.getErrorMessageFromResponse = getErrorMessageFromResponse;
@@ -1,61 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.fetchFile = exports.isDataURL = exports.isRequestURL = exports.isNodePath = void 0;
28
- const loader_utils_1 = require("@loaders.gl/loader-utils");
29
- const response_utils_1 = require("../utils/response-utils");
30
- const node = __importStar(require("./fetch-file.node"));
31
- function isNodePath(url) {
32
- return !isRequestURL(url) && !isDataURL(url);
33
- }
34
- exports.isNodePath = isNodePath;
35
- function isRequestURL(url) {
36
- return url.startsWith('http:') || url.startsWith('https:');
37
- }
38
- exports.isRequestURL = isRequestURL;
39
- function isDataURL(url) {
40
- return url.startsWith('data:');
41
- }
42
- exports.isDataURL = isDataURL;
43
- /**
44
- * fetch API compatible function
45
- * - Supports fetching from Node.js local file system paths
46
- * - Respects pathPrefix and file aliases
47
- */
48
- async function fetchFile(urlOrData, fetchOptions) {
49
- if (typeof urlOrData === 'string') {
50
- const url = (0, loader_utils_1.resolvePath)(urlOrData);
51
- // Support fetching from local file system
52
- if (isNodePath(url) && node?.fetchFileNode) {
53
- return node.fetchFileNode(url, fetchOptions);
54
- }
55
- // Call global fetch
56
- return await fetch(url, fetchOptions);
57
- }
58
- // TODO - should we still call fetch on non-URL inputs?
59
- return await (0, response_utils_1.makeResponse)(urlOrData);
60
- }
61
- exports.fetchFile = fetchFile;