@loaders.gl/core 3.4.10 → 3.4.12
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/dist.min.js +40 -112
- package/dist/es5/lib/init.js +1 -1
- package/dist/es5/null-loader.js +1 -1
- package/dist/esm/lib/init.js +1 -1
- package/dist/esm/null-loader.js +1 -1
- package/dist/lib/fetch/read-file.d.ts +1 -1
- package/dist/lib/fetch/read-file.d.ts.map +1 -1
- package/dist/null-worker.js +1 -1
- package/package.json +4 -6
- package/dist/bundle.js +0 -5
- package/dist/core-addons/write-file-browser.js +0 -60
- package/dist/index.js +0 -104
- package/dist/iterators/batch-iterators/timed-batch-iterator.js +0 -22
- package/dist/iterators/make-iterator/make-array-buffer-iterator.js +0 -27
- package/dist/iterators/make-iterator/make-blob-iterator.js +0 -21
- package/dist/iterators/make-iterator/make-iterator.js +0 -37
- package/dist/iterators/make-iterator/make-stream-iterator.js +0 -96
- package/dist/iterators/make-iterator/make-string-iterator.js +0 -24
- package/dist/iterators/make-stream/make-dom-stream.js +0 -46
- package/dist/iterators/make-stream/make-node-stream.js +0 -82
- package/dist/javascript-utils/is-type.js +0 -41
- package/dist/lib/api/encode.js +0 -120
- package/dist/lib/api/load-in-batches.js +0 -33
- package/dist/lib/api/load.js +0 -42
- package/dist/lib/api/loader-options.js +0 -7
- package/dist/lib/api/parse-in-batches.js +0 -117
- package/dist/lib/api/parse-sync.js +0 -59
- package/dist/lib/api/parse.js +0 -82
- package/dist/lib/api/register-loaders.js +0 -35
- package/dist/lib/api/save.js +0 -15
- package/dist/lib/api/select-loader.js +0 -253
- package/dist/lib/common.js +0 -2
- package/dist/lib/fetch/fetch-error-message.js +0 -25
- package/dist/lib/fetch/fetch-file.js +0 -27
- package/dist/lib/fetch/read-array-buffer.js +0 -41
- package/dist/lib/fetch/read-file.js +0 -29
- package/dist/lib/fetch/write-file.js +0 -22
- package/dist/lib/filesystems/browser-filesystem.js +0 -126
- package/dist/lib/filesystems/filesystem.js +0 -2
- package/dist/lib/filesystems/read-array-buffer.js +0 -29
- package/dist/lib/init.js +0 -16
- package/dist/lib/loader-utils/check-errors.js +0 -30
- package/dist/lib/loader-utils/get-data.js +0 -129
- package/dist/lib/loader-utils/get-fetch-function.js +0 -31
- package/dist/lib/loader-utils/loader-context.js +0 -59
- package/dist/lib/loader-utils/loggers.js +0 -41
- package/dist/lib/loader-utils/normalize-loader.js +0 -52
- package/dist/lib/loader-utils/option-defaults.js +0 -43
- package/dist/lib/loader-utils/option-utils.js +0 -160
- package/dist/lib/progress/fetch-progress.js +0 -59
- package/dist/lib/utils/log.js +0 -6
- package/dist/lib/utils/mime-type-utils.js +0 -42
- package/dist/lib/utils/resource-utils.js +0 -90
- package/dist/lib/utils/response-utils.js +0 -115
- package/dist/lib/utils/url-utils.js +0 -14
- package/dist/null-loader.js +0 -56
- package/dist/workers/null-worker.js +0 -5
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseSync = void 0;
|
|
4
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
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
|
-
* @param data
|
|
14
|
-
* @param loaders
|
|
15
|
-
* @param options
|
|
16
|
-
* @param context
|
|
17
|
-
*/
|
|
18
|
-
function parseSync(data, loaders, options, context) {
|
|
19
|
-
(0, loader_utils_1.assert)(!context || typeof context === 'object'); // parseSync no longer accepts final url
|
|
20
|
-
// Signature: parseSync(data, options)
|
|
21
|
-
// Uses registered loaders
|
|
22
|
-
if (!Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
|
|
23
|
-
context = undefined; // context not supported in short signature
|
|
24
|
-
options = loaders;
|
|
25
|
-
loaders = undefined;
|
|
26
|
-
}
|
|
27
|
-
options = options || {};
|
|
28
|
-
// Chooses a loader (and normalizes it)
|
|
29
|
-
// Also use any loaders in the context, new loaders take priority
|
|
30
|
-
const typedLoaders = loaders;
|
|
31
|
-
const candidateLoaders = (0, loader_context_1.getLoadersFromContext)(typedLoaders, context);
|
|
32
|
-
const loader = (0, select_loader_1.selectLoaderSync)(data, candidateLoaders, options);
|
|
33
|
-
// Note: if nothrow option was set, it is possible that no loader was found, if so just return null
|
|
34
|
-
if (!loader) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
// Normalize options
|
|
38
|
-
options = (0, option_utils_1.normalizeOptions)(options, loader, candidateLoaders);
|
|
39
|
-
// Extract a url for auto detection
|
|
40
|
-
const url = (0, resource_utils_1.getResourceUrl)(data);
|
|
41
|
-
const parse = () => {
|
|
42
|
-
throw new Error('parseSync called parse (which is async');
|
|
43
|
-
};
|
|
44
|
-
context = (0, loader_context_1.getLoaderContext)({ url, parseSync, parse, loaders: loaders }, options, context || null);
|
|
45
|
-
return parseWithLoaderSync(loader, data, options, context);
|
|
46
|
-
}
|
|
47
|
-
exports.parseSync = parseSync;
|
|
48
|
-
// TODO - should accept loader.parseSync/parse and generate 1 chunk asyncIterator
|
|
49
|
-
function parseWithLoaderSync(loader, data, options, context) {
|
|
50
|
-
data = (0, get_data_1.getArrayBufferOrStringFromDataSync)(data, loader, options);
|
|
51
|
-
if (loader.parseTextSync && typeof data === 'string') {
|
|
52
|
-
return loader.parseTextSync(data, options); // , context, loader);
|
|
53
|
-
}
|
|
54
|
-
if (loader.parseSync && data instanceof ArrayBuffer) {
|
|
55
|
-
return loader.parseSync(data, options, context); // , loader);
|
|
56
|
-
}
|
|
57
|
-
// TBD - If synchronous parser not available, return null
|
|
58
|
-
throw new Error(`${loader.name} loader: 'parseSync' not supported by this loader, use 'parse' instead. ${context.url || ''}`);
|
|
59
|
-
}
|
package/dist/lib/api/parse.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parse = void 0;
|
|
4
|
-
const worker_utils_1 = require("@loaders.gl/worker-utils");
|
|
5
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
6
|
-
const normalize_loader_1 = require("../loader-utils/normalize-loader");
|
|
7
|
-
const is_type_1 = require("../../javascript-utils/is-type");
|
|
8
|
-
const option_utils_1 = require("../loader-utils/option-utils");
|
|
9
|
-
const get_data_1 = require("../loader-utils/get-data");
|
|
10
|
-
const loader_context_1 = require("../loader-utils/loader-context");
|
|
11
|
-
const resource_utils_1 = require("../utils/resource-utils");
|
|
12
|
-
const select_loader_1 = require("./select-loader");
|
|
13
|
-
/**
|
|
14
|
-
* Parses `data` using a specified loader
|
|
15
|
-
* @param data
|
|
16
|
-
* @param loaders
|
|
17
|
-
* @param options
|
|
18
|
-
* @param context
|
|
19
|
-
*/
|
|
20
|
-
async function parse(data, loaders, options, context) {
|
|
21
|
-
(0, worker_utils_1.assert)(!context || typeof context === 'object'); // parse no longer accepts final url
|
|
22
|
-
// Signature: parse(data, options, context | url)
|
|
23
|
-
// Uses registered loaders
|
|
24
|
-
if (loaders && !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
|
-
// Also use any loaders in the context, new loaders take priority
|
|
35
|
-
const typedLoaders = loaders;
|
|
36
|
-
const candidateLoaders = (0, loader_context_1.getLoadersFromContext)(typedLoaders, context);
|
|
37
|
-
// todo hacky type cast
|
|
38
|
-
const loader = await (0, select_loader_1.selectLoader)(data, candidateLoaders, options);
|
|
39
|
-
// Note: if no loader was found, if so just return null
|
|
40
|
-
if (!loader) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
// Normalize options
|
|
44
|
-
options = (0, option_utils_1.normalizeOptions)(options, loader, candidateLoaders, url);
|
|
45
|
-
// Get a context (if already present, will be unchanged)
|
|
46
|
-
context = (0, loader_context_1.getLoaderContext)({ url, parse, loaders: candidateLoaders }, options, context || null);
|
|
47
|
-
return await parseWithLoader(loader, data, options, context);
|
|
48
|
-
}
|
|
49
|
-
exports.parse = parse;
|
|
50
|
-
// TODO: support progress and abort
|
|
51
|
-
// TODO - should accept loader.parseAsyncIterator and concatenate.
|
|
52
|
-
async function parseWithLoader(loader, data, options, context) {
|
|
53
|
-
(0, worker_utils_1.validateWorkerVersion)(loader);
|
|
54
|
-
if ((0, is_type_1.isResponse)(data)) {
|
|
55
|
-
// Serialize to support passing the response to web worker
|
|
56
|
-
const response = data;
|
|
57
|
-
const { ok, redirected, status, statusText, type, url } = response;
|
|
58
|
-
const headers = Object.fromEntries(response.headers.entries());
|
|
59
|
-
context.response = { headers, ok, redirected, status, statusText, type, url };
|
|
60
|
-
}
|
|
61
|
-
data = await (0, get_data_1.getArrayBufferOrStringFromData)(data, loader, options);
|
|
62
|
-
// First check for synchronous text parser, wrap results in promises
|
|
63
|
-
if (loader.parseTextSync && typeof data === 'string') {
|
|
64
|
-
options.dataType = 'text';
|
|
65
|
-
return loader.parseTextSync(data, options, context, loader);
|
|
66
|
-
}
|
|
67
|
-
// If we have a workerUrl and the loader can parse the given options efficiently in a worker
|
|
68
|
-
if ((0, loader_utils_1.canParseWithWorker)(loader, options)) {
|
|
69
|
-
return await (0, loader_utils_1.parseWithWorker)(loader, data, options, context, parse);
|
|
70
|
-
}
|
|
71
|
-
// Check for asynchronous parser
|
|
72
|
-
if (loader.parseText && typeof data === 'string') {
|
|
73
|
-
return await loader.parseText(data, options, context, loader);
|
|
74
|
-
}
|
|
75
|
-
if (loader.parse) {
|
|
76
|
-
return await loader.parse(data, options, context, loader);
|
|
77
|
-
}
|
|
78
|
-
// This should not happen, all sync loaders should also offer `parse` function
|
|
79
|
-
(0, worker_utils_1.assert)(!loader.parseSync);
|
|
80
|
-
// TBD - If asynchronous parser not available, return null
|
|
81
|
-
throw new Error(`${loader.id} loader - no parser found and worker is disabled`);
|
|
82
|
-
}
|
|
@@ -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;
|
package/dist/lib/api/save.js
DELETED
|
@@ -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,253 +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
|
-
loader = loader || findLoaderByInitialBytes(loaders, data);
|
|
107
|
-
reason = reason || (loader ? `matched initial data ${getFirstCharacters(data)}` : '');
|
|
108
|
-
// Look up loader by fallback mime type
|
|
109
|
-
loader = loader || findLoaderByMIMEType(loaders, options?.fallbackMimeType);
|
|
110
|
-
reason = reason || (loader ? `matched fallback MIME type ${type}` : '');
|
|
111
|
-
if (reason) {
|
|
112
|
-
log_1.log.log(1, `selectLoader selected ${loader?.name}: ${reason}.`);
|
|
113
|
-
}
|
|
114
|
-
return loader;
|
|
115
|
-
}
|
|
116
|
-
/** Check HTTP Response */
|
|
117
|
-
function validHTTPResponse(data) {
|
|
118
|
-
// HANDLE HTTP status
|
|
119
|
-
if (data instanceof Response) {
|
|
120
|
-
// 204 - NO CONTENT. This handles cases where e.g. a tile server responds with 204 for a missing tile
|
|
121
|
-
if (data.status === 204) {
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
/** Generate a helpful message to help explain why loader selection failed. */
|
|
128
|
-
function getNoValidLoaderMessage(data) {
|
|
129
|
-
const url = (0, resource_utils_1.getResourceUrl)(data);
|
|
130
|
-
const type = (0, resource_utils_1.getResourceMIMEType)(data);
|
|
131
|
-
let message = 'No valid loader found (';
|
|
132
|
-
message += url ? `${loader_utils_1.path.filename(url)}, ` : 'no url provided, ';
|
|
133
|
-
message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
|
|
134
|
-
// First characters are only accessible when called on data (string or arrayBuffer).
|
|
135
|
-
const firstCharacters = data ? getFirstCharacters(data) : '';
|
|
136
|
-
message += firstCharacters ? ` first bytes: "${firstCharacters}"` : 'first bytes: not available';
|
|
137
|
-
message += ')';
|
|
138
|
-
return message;
|
|
139
|
-
}
|
|
140
|
-
function normalizeLoaders(loaders) {
|
|
141
|
-
for (const loader of loaders) {
|
|
142
|
-
(0, normalize_loader_1.normalizeLoader)(loader);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// TODO - Would be nice to support http://example.com/file.glb?parameter=1
|
|
146
|
-
// E.g: x = new URL('http://example.com/file.glb?load=1'; x.pathname
|
|
147
|
-
function findLoaderByUrl(loaders, url) {
|
|
148
|
-
// Get extension
|
|
149
|
-
const match = url && EXT_PATTERN.exec(url);
|
|
150
|
-
const extension = match && match[1];
|
|
151
|
-
return extension ? findLoaderByExtension(loaders, extension) : null;
|
|
152
|
-
}
|
|
153
|
-
function findLoaderByExtension(loaders, extension) {
|
|
154
|
-
extension = extension.toLowerCase();
|
|
155
|
-
for (const loader of loaders) {
|
|
156
|
-
for (const loaderExtension of loader.extensions) {
|
|
157
|
-
if (loaderExtension.toLowerCase() === extension) {
|
|
158
|
-
return loader;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
function findLoaderByMIMEType(loaders, mimeType) {
|
|
165
|
-
for (const loader of loaders) {
|
|
166
|
-
if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
|
|
167
|
-
return loader;
|
|
168
|
-
}
|
|
169
|
-
// Support referring to loaders using the "unregistered tree"
|
|
170
|
-
// https://en.wikipedia.org/wiki/Media_type#Unregistered_tree
|
|
171
|
-
if (mimeType === `application/x.${loader.id}`) {
|
|
172
|
-
return loader;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
function findLoaderByInitialBytes(loaders, data) {
|
|
178
|
-
if (!data) {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
for (const loader of loaders) {
|
|
182
|
-
if (typeof data === 'string') {
|
|
183
|
-
if (testDataAgainstText(data, loader)) {
|
|
184
|
-
return loader;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
else if (ArrayBuffer.isView(data)) {
|
|
188
|
-
// Typed Arrays can have offsets into underlying buffer
|
|
189
|
-
if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) {
|
|
190
|
-
return loader;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
else if (data instanceof ArrayBuffer) {
|
|
194
|
-
const byteOffset = 0;
|
|
195
|
-
if (testDataAgainstBinary(data, byteOffset, loader)) {
|
|
196
|
-
return loader;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
// TODO Handle streaming case (requires creating a new AsyncIterator)
|
|
200
|
-
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
function testDataAgainstText(data, loader) {
|
|
204
|
-
if (loader.testText) {
|
|
205
|
-
return loader.testText(data);
|
|
206
|
-
}
|
|
207
|
-
const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
|
|
208
|
-
return tests.some((test) => data.startsWith(test));
|
|
209
|
-
}
|
|
210
|
-
function testDataAgainstBinary(data, byteOffset, loader) {
|
|
211
|
-
const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
|
|
212
|
-
return tests.some((test) => testBinary(data, byteOffset, loader, test));
|
|
213
|
-
}
|
|
214
|
-
function testBinary(data, byteOffset, loader, test) {
|
|
215
|
-
if (test instanceof ArrayBuffer) {
|
|
216
|
-
return (0, loader_utils_1.compareArrayBuffers)(test, data, test.byteLength);
|
|
217
|
-
}
|
|
218
|
-
switch (typeof test) {
|
|
219
|
-
case 'function':
|
|
220
|
-
return test(data, loader);
|
|
221
|
-
case 'string':
|
|
222
|
-
// Magic bytes check: If `test` is a string, check if binary data starts with that strings
|
|
223
|
-
const magic = getMagicString(data, byteOffset, test.length);
|
|
224
|
-
return test === magic;
|
|
225
|
-
default:
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
function getFirstCharacters(data, length = 5) {
|
|
230
|
-
if (typeof data === 'string') {
|
|
231
|
-
return data.slice(0, length);
|
|
232
|
-
}
|
|
233
|
-
else if (ArrayBuffer.isView(data)) {
|
|
234
|
-
// Typed Arrays can have offsets into underlying buffer
|
|
235
|
-
return getMagicString(data.buffer, data.byteOffset, length);
|
|
236
|
-
}
|
|
237
|
-
else if (data instanceof ArrayBuffer) {
|
|
238
|
-
const byteOffset = 0;
|
|
239
|
-
return getMagicString(data, byteOffset, length);
|
|
240
|
-
}
|
|
241
|
-
return '';
|
|
242
|
-
}
|
|
243
|
-
function getMagicString(arrayBuffer, byteOffset, length) {
|
|
244
|
-
if (arrayBuffer.byteLength < byteOffset + length) {
|
|
245
|
-
return '';
|
|
246
|
-
}
|
|
247
|
-
const dataView = new DataView(arrayBuffer);
|
|
248
|
-
let magic = '';
|
|
249
|
-
for (let i = 0; i < length; i++) {
|
|
250
|
-
magic += String.fromCharCode(dataView.getUint8(byteOffset + i));
|
|
251
|
-
}
|
|
252
|
-
return magic;
|
|
253
|
-
}
|
package/dist/lib/common.js
DELETED
|
@@ -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,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchFile = void 0;
|
|
4
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
5
|
-
const response_utils_1 = require("../utils/response-utils");
|
|
6
|
-
// import {getErrorMessageFromResponse} from './fetch-error-message';
|
|
7
|
-
/**
|
|
8
|
-
* fetch compatible function
|
|
9
|
-
* Reads file data from:
|
|
10
|
-
* - http/http urls
|
|
11
|
-
* - data urls
|
|
12
|
-
* - File/Blob objects
|
|
13
|
-
* Leverages `@loaders.gl/polyfills` for Node.js support
|
|
14
|
-
* Respects pathPrefix and file aliases
|
|
15
|
-
*/
|
|
16
|
-
async function fetchFile(url, options) {
|
|
17
|
-
if (typeof url === 'string') {
|
|
18
|
-
url = (0, loader_utils_1.resolvePath)(url);
|
|
19
|
-
let fetchOptions = options;
|
|
20
|
-
if (options?.fetch && typeof options?.fetch !== 'function') {
|
|
21
|
-
fetchOptions = options.fetch;
|
|
22
|
-
}
|
|
23
|
-
return await fetch(url, fetchOptions);
|
|
24
|
-
}
|
|
25
|
-
return await (0, response_utils_1.makeResponse)(url);
|
|
26
|
-
}
|
|
27
|
-
exports.fetchFile = fetchFile;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readBlob = exports.readArrayBuffer = void 0;
|
|
4
|
-
//
|
|
5
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
6
|
-
/**
|
|
7
|
-
* Reads a chunk from a random access file
|
|
8
|
-
* @param file
|
|
9
|
-
* @param start
|
|
10
|
-
* @param length
|
|
11
|
-
* @returns
|
|
12
|
-
*/
|
|
13
|
-
async function readArrayBuffer(file, start, length) {
|
|
14
|
-
if (typeof file === 'number') {
|
|
15
|
-
return await loader_utils_1.fs._readToArrayBuffer(file, start, length);
|
|
16
|
-
}
|
|
17
|
-
// TODO - we can do better for ArrayBuffer and string
|
|
18
|
-
if (!(file instanceof Blob)) {
|
|
19
|
-
file = new Blob([file]);
|
|
20
|
-
}
|
|
21
|
-
const slice = file.slice(start, start + length);
|
|
22
|
-
return await readBlob(slice);
|
|
23
|
-
}
|
|
24
|
-
exports.readArrayBuffer = readArrayBuffer;
|
|
25
|
-
/**
|
|
26
|
-
* Read a slice of a Blob or File, without loading the entire file into memory
|
|
27
|
-
* The trick when reading File objects is to read successive "slices" of the File
|
|
28
|
-
* Per spec https://w3c.github.io/FileAPI/, slicing a File only updates the start and end fields
|
|
29
|
-
* Actually reading from file happens in `readAsArrayBuffer`
|
|
30
|
-
* @param blob to read
|
|
31
|
-
*/
|
|
32
|
-
async function readBlob(blob) {
|
|
33
|
-
return await new Promise((resolve, reject) => {
|
|
34
|
-
const fileReader = new FileReader();
|
|
35
|
-
fileReader.onload = (event) => resolve(event?.target?.result);
|
|
36
|
-
// TODO - reject with a proper Error
|
|
37
|
-
fileReader.onerror = (error) => reject(error);
|
|
38
|
-
fileReader.readAsArrayBuffer(blob);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
exports.readBlob = readBlob;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readFileSync = void 0;
|
|
4
|
-
// File read
|
|
5
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
6
|
-
const loader_utils_2 = require("@loaders.gl/loader-utils");
|
|
7
|
-
// TODO - this is not tested
|
|
8
|
-
// const isDataURL = (url) => url.startsWith('data:');
|
|
9
|
-
/**
|
|
10
|
-
* In a few cases (data URIs, node.js) "files" can be read synchronously
|
|
11
|
-
*/
|
|
12
|
-
function readFileSync(url, options = {}) {
|
|
13
|
-
url = (0, loader_utils_1.resolvePath)(url);
|
|
14
|
-
// Only support this if we can also support sync data URL decoding in browser
|
|
15
|
-
// if (isDataURL(url)) {
|
|
16
|
-
// return decodeDataUri(url);
|
|
17
|
-
// }
|
|
18
|
-
if (!loader_utils_1.isBrowser) {
|
|
19
|
-
const buffer = loader_utils_1.fs.readFileSync(url, options);
|
|
20
|
-
return typeof buffer !== 'string' ? (0, loader_utils_1.toArrayBuffer)(buffer) : buffer;
|
|
21
|
-
}
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
if (!options.nothrow) {
|
|
24
|
-
// throw new Error('Cant load URI synchronously');
|
|
25
|
-
(0, loader_utils_2.assert)(false);
|
|
26
|
-
}
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
exports.readFileSync = readFileSync;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.writeFileSync = exports.writeFile = void 0;
|
|
4
|
-
// file write
|
|
5
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
6
|
-
const loader_utils_2 = require("@loaders.gl/loader-utils");
|
|
7
|
-
async function writeFile(filePath, arrayBufferOrString, options) {
|
|
8
|
-
filePath = (0, loader_utils_1.resolvePath)(filePath);
|
|
9
|
-
if (!loader_utils_1.isBrowser) {
|
|
10
|
-
await loader_utils_2.fs.writeFile(filePath, (0, loader_utils_2.toBuffer)(arrayBufferOrString), { flag: 'w' });
|
|
11
|
-
}
|
|
12
|
-
(0, loader_utils_1.assert)(false);
|
|
13
|
-
}
|
|
14
|
-
exports.writeFile = writeFile;
|
|
15
|
-
function writeFileSync(filePath, arrayBufferOrString, options) {
|
|
16
|
-
filePath = (0, loader_utils_1.resolvePath)(filePath);
|
|
17
|
-
if (!loader_utils_1.isBrowser) {
|
|
18
|
-
loader_utils_2.fs.writeFileSync(filePath, (0, loader_utils_2.toBuffer)(arrayBufferOrString), { flag: 'w' });
|
|
19
|
-
}
|
|
20
|
-
(0, loader_utils_1.assert)(false);
|
|
21
|
-
}
|
|
22
|
-
exports.writeFileSync = writeFileSync;
|