@loaders.gl/core 3.4.11 → 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,160 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// loaders.gl, MIT license
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.normalizeOptions = exports.setGlobalOptions = exports.getGlobalLoaderOptions = exports.getGlobalLoaderState = void 0;
|
|
5
|
-
const is_type_1 = require("../../javascript-utils/is-type");
|
|
6
|
-
const loggers_1 = require("./loggers");
|
|
7
|
-
const option_defaults_1 = require("./option-defaults");
|
|
8
|
-
/**
|
|
9
|
-
* Helper for safely accessing global loaders.gl variables
|
|
10
|
-
* Wraps initialization of global variable in function to defeat overly aggressive tree-shakers
|
|
11
|
-
*/
|
|
12
|
-
function getGlobalLoaderState() {
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
globalThis.loaders = globalThis.loaders || {};
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
const { loaders } = globalThis;
|
|
17
|
-
// Add _state object to keep separate from modules added to globalThis.loaders
|
|
18
|
-
loaders._state = loaders._state || {};
|
|
19
|
-
return loaders._state;
|
|
20
|
-
}
|
|
21
|
-
exports.getGlobalLoaderState = getGlobalLoaderState;
|
|
22
|
-
/**
|
|
23
|
-
* Store global loader options on the global object to increase chances of cross loaders-version interoperability
|
|
24
|
-
* NOTE: This use case is not reliable but can help when testing new versions of loaders.gl with existing frameworks
|
|
25
|
-
* @returns global loader options merged with default loader options
|
|
26
|
-
*/
|
|
27
|
-
const getGlobalLoaderOptions = () => {
|
|
28
|
-
const state = getGlobalLoaderState();
|
|
29
|
-
// Ensure all default loader options from this library are mentioned
|
|
30
|
-
state.globalOptions = state.globalOptions || { ...option_defaults_1.DEFAULT_LOADER_OPTIONS };
|
|
31
|
-
return state.globalOptions;
|
|
32
|
-
};
|
|
33
|
-
exports.getGlobalLoaderOptions = getGlobalLoaderOptions;
|
|
34
|
-
/**
|
|
35
|
-
* Set global loader options
|
|
36
|
-
* @param options
|
|
37
|
-
*/
|
|
38
|
-
function setGlobalOptions(options) {
|
|
39
|
-
const state = getGlobalLoaderState();
|
|
40
|
-
const globalOptions = (0, exports.getGlobalLoaderOptions)();
|
|
41
|
-
state.globalOptions = normalizeOptionsInternal(globalOptions, options);
|
|
42
|
-
}
|
|
43
|
-
exports.setGlobalOptions = setGlobalOptions;
|
|
44
|
-
/**
|
|
45
|
-
* Merges options with global opts and loader defaults, also injects baseUri
|
|
46
|
-
* @param options
|
|
47
|
-
* @param loader
|
|
48
|
-
* @param loaders
|
|
49
|
-
* @param url
|
|
50
|
-
*/
|
|
51
|
-
function normalizeOptions(options, loader, loaders, url) {
|
|
52
|
-
loaders = loaders || [];
|
|
53
|
-
loaders = Array.isArray(loaders) ? loaders : [loaders];
|
|
54
|
-
validateOptions(options, loaders);
|
|
55
|
-
return normalizeOptionsInternal(loader, options, url);
|
|
56
|
-
}
|
|
57
|
-
exports.normalizeOptions = normalizeOptions;
|
|
58
|
-
// VALIDATE OPTIONS
|
|
59
|
-
/**
|
|
60
|
-
* Warn for unsupported options
|
|
61
|
-
* @param options
|
|
62
|
-
* @param loaders
|
|
63
|
-
*/
|
|
64
|
-
function validateOptions(options, loaders) {
|
|
65
|
-
// Check top level options
|
|
66
|
-
validateOptionsObject(options, null, option_defaults_1.DEFAULT_LOADER_OPTIONS, option_defaults_1.REMOVED_LOADER_OPTIONS, loaders);
|
|
67
|
-
for (const loader of loaders) {
|
|
68
|
-
// Get the scoped, loader specific options from the user supplied options
|
|
69
|
-
const idOptions = (options && options[loader.id]) || {};
|
|
70
|
-
// Get scoped, loader specific default and deprecated options from the selected loader
|
|
71
|
-
const loaderOptions = (loader.options && loader.options[loader.id]) || {};
|
|
72
|
-
const deprecatedOptions = (loader.deprecatedOptions && loader.deprecatedOptions[loader.id]) || {};
|
|
73
|
-
// Validate loader specific options
|
|
74
|
-
validateOptionsObject(idOptions, loader.id, loaderOptions, deprecatedOptions, loaders);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// eslint-disable-next-line max-params, complexity
|
|
78
|
-
function validateOptionsObject(options, id, defaultOptions, deprecatedOptions, loaders) {
|
|
79
|
-
const loaderName = id || 'Top level';
|
|
80
|
-
const prefix = id ? `${id}.` : '';
|
|
81
|
-
for (const key in options) {
|
|
82
|
-
// If top level option value is an object it could options for a loader, so ignore
|
|
83
|
-
const isSubOptions = !id && (0, is_type_1.isObject)(options[key]);
|
|
84
|
-
const isBaseUriOption = key === 'baseUri' && !id;
|
|
85
|
-
const isWorkerUrlOption = key === 'workerUrl' && id;
|
|
86
|
-
// <loader>.workerUrl requires special handling as it is now auto-generated and no longer specified as a default option.
|
|
87
|
-
if (!(key in defaultOptions) && !isBaseUriOption && !isWorkerUrlOption) {
|
|
88
|
-
// Issue deprecation warnings
|
|
89
|
-
if (key in deprecatedOptions) {
|
|
90
|
-
loggers_1.probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' no longer supported, use \'${deprecatedOptions[key]}\'`)();
|
|
91
|
-
}
|
|
92
|
-
else if (!isSubOptions) {
|
|
93
|
-
const suggestion = findSimilarOption(key, loaders);
|
|
94
|
-
loggers_1.probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' not recognized. ${suggestion}`)();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function findSimilarOption(optionKey, loaders) {
|
|
100
|
-
const lowerCaseOptionKey = optionKey.toLowerCase();
|
|
101
|
-
let bestSuggestion = '';
|
|
102
|
-
for (const loader of loaders) {
|
|
103
|
-
for (const key in loader.options) {
|
|
104
|
-
if (optionKey === key) {
|
|
105
|
-
return `Did you mean \'${loader.id}.${key}\'?`;
|
|
106
|
-
}
|
|
107
|
-
const lowerCaseKey = key.toLowerCase();
|
|
108
|
-
const isPartialMatch = lowerCaseOptionKey.startsWith(lowerCaseKey) || lowerCaseKey.startsWith(lowerCaseOptionKey);
|
|
109
|
-
if (isPartialMatch) {
|
|
110
|
-
bestSuggestion = bestSuggestion || `Did you mean \'${loader.id}.${key}\'?`;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return bestSuggestion;
|
|
115
|
-
}
|
|
116
|
-
function normalizeOptionsInternal(loader, options, url) {
|
|
117
|
-
const loaderDefaultOptions = loader.options || {};
|
|
118
|
-
const mergedOptions = { ...loaderDefaultOptions };
|
|
119
|
-
addUrlOptions(mergedOptions, url);
|
|
120
|
-
// LOGGING: options.log can be set to `null` to defeat logging
|
|
121
|
-
if (mergedOptions.log === null) {
|
|
122
|
-
mergedOptions.log = new loggers_1.NullLog();
|
|
123
|
-
}
|
|
124
|
-
mergeNestedFields(mergedOptions, (0, exports.getGlobalLoaderOptions)());
|
|
125
|
-
mergeNestedFields(mergedOptions, options);
|
|
126
|
-
return mergedOptions;
|
|
127
|
-
}
|
|
128
|
-
// Merge nested options objects
|
|
129
|
-
function mergeNestedFields(mergedOptions, options) {
|
|
130
|
-
for (const key in options) {
|
|
131
|
-
// Check for nested options
|
|
132
|
-
// object in options => either no key in defaultOptions or object in defaultOptions
|
|
133
|
-
if (key in options) {
|
|
134
|
-
const value = options[key];
|
|
135
|
-
if ((0, is_type_1.isPureObject)(value) && (0, is_type_1.isPureObject)(mergedOptions[key])) {
|
|
136
|
-
mergedOptions[key] = {
|
|
137
|
-
...mergedOptions[key],
|
|
138
|
-
...options[key]
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
mergedOptions[key] = options[key];
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// else: No need to merge nested opts, and the initial merge already copied over the nested options
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Harvest information from the url
|
|
150
|
-
* @deprecated This is mainly there to support a hack in the GLTFLoader
|
|
151
|
-
* TODO - baseUri should be a directory, i.e. remove file component from baseUri
|
|
152
|
-
* TODO - extract extension?
|
|
153
|
-
* TODO - extract query parameters?
|
|
154
|
-
* TODO - should these be injected on context instead of options?
|
|
155
|
-
*/
|
|
156
|
-
function addUrlOptions(options, url) {
|
|
157
|
-
if (url && !('baseUri' in options)) {
|
|
158
|
-
options.baseUri = url;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Forked from github AnthumChris/fetch-progress-indicators under MIT license
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
/**
|
|
5
|
-
* Intercepts the Response stream and creates a new Response
|
|
6
|
-
*/
|
|
7
|
-
async function fetchProgress(response, onProgress, // TODO better callback types
|
|
8
|
-
onDone = () => { }, onError = () => { }) {
|
|
9
|
-
response = await response;
|
|
10
|
-
if (!response.ok) {
|
|
11
|
-
// ERROR checking needs to be done separately
|
|
12
|
-
return response;
|
|
13
|
-
}
|
|
14
|
-
const body = response.body;
|
|
15
|
-
if (!body) {
|
|
16
|
-
// 'ReadableStream not yet supported in this browser.
|
|
17
|
-
return response;
|
|
18
|
-
}
|
|
19
|
-
const contentLength = response.headers.get('content-length') || 0;
|
|
20
|
-
const totalBytes = contentLength ? parseInt(contentLength) : 0;
|
|
21
|
-
if (!(totalBytes > 0)) {
|
|
22
|
-
return response;
|
|
23
|
-
}
|
|
24
|
-
// Currently override only implemented in browser
|
|
25
|
-
if (typeof ReadableStream === 'undefined' || !body.getReader) {
|
|
26
|
-
return response;
|
|
27
|
-
}
|
|
28
|
-
// Create a new stream that invisbly wraps original stream
|
|
29
|
-
const progressStream = new ReadableStream({
|
|
30
|
-
async start(controller) {
|
|
31
|
-
const reader = body.getReader();
|
|
32
|
-
await read(controller, reader, 0, totalBytes, onProgress, onDone, onError);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
return new Response(progressStream);
|
|
36
|
-
}
|
|
37
|
-
exports.default = fetchProgress;
|
|
38
|
-
// Forward to original streams controller
|
|
39
|
-
// TODO - this causes a crazy deep "async stack"... rewrite as async iterator?
|
|
40
|
-
// eslint-disable-next-line max-params
|
|
41
|
-
async function read(controller, reader, loadedBytes, totalBytes, onProgress, onDone, onError) {
|
|
42
|
-
try {
|
|
43
|
-
const { done, value } = await reader.read();
|
|
44
|
-
if (done) {
|
|
45
|
-
onDone();
|
|
46
|
-
controller.close();
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
loadedBytes += value.byteLength;
|
|
50
|
-
const percent = Math.round((loadedBytes / totalBytes) * 100);
|
|
51
|
-
onProgress(percent, { loadedBytes, totalBytes });
|
|
52
|
-
controller.enqueue(value);
|
|
53
|
-
await read(controller, reader, loadedBytes, totalBytes, onProgress, onDone, onError);
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
controller.error(error);
|
|
57
|
-
onError(error);
|
|
58
|
-
}
|
|
59
|
-
}
|
package/dist/lib/utils/log.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// TODO - build/integrate proper MIME type parsing
|
|
3
|
-
// https://mimesniff.spec.whatwg.org/
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.parseMIMETypeFromURL = exports.parseMIMEType = void 0;
|
|
6
|
-
const DATA_URL_PATTERN = /^data:([-\w.]+\/[-\w.+]+)(;|,)/;
|
|
7
|
-
const MIME_TYPE_PATTERN = /^([-\w.]+\/[-\w.+]+)/;
|
|
8
|
-
/**
|
|
9
|
-
* Remove extra data like `charset` from MIME types
|
|
10
|
-
* @param mimeString
|
|
11
|
-
* @returns A clean MIME type, or an empty string
|
|
12
|
-
*
|
|
13
|
-
* @todo - handle more advanced MIMETYpes, multiple types
|
|
14
|
-
* @todo - extract charset etc
|
|
15
|
-
*/
|
|
16
|
-
function parseMIMEType(mimeString) {
|
|
17
|
-
// If resource is a data url, extract any embedded mime type
|
|
18
|
-
const matches = MIME_TYPE_PATTERN.exec(mimeString);
|
|
19
|
-
if (matches) {
|
|
20
|
-
return matches[1];
|
|
21
|
-
}
|
|
22
|
-
return mimeString;
|
|
23
|
-
}
|
|
24
|
-
exports.parseMIMEType = parseMIMEType;
|
|
25
|
-
/**
|
|
26
|
-
* Extract MIME type from data URL
|
|
27
|
-
*
|
|
28
|
-
* @param mimeString
|
|
29
|
-
* @returns A clean MIME type, or an empty string
|
|
30
|
-
*
|
|
31
|
-
* @todo - handle more advanced MIMETYpes, multiple types
|
|
32
|
-
* @todo - extract charset etc
|
|
33
|
-
*/
|
|
34
|
-
function parseMIMETypeFromURL(url) {
|
|
35
|
-
// If resource is a data URL, extract any embedded mime type
|
|
36
|
-
const matches = DATA_URL_PATTERN.exec(url);
|
|
37
|
-
if (matches) {
|
|
38
|
-
return matches[1];
|
|
39
|
-
}
|
|
40
|
-
return '';
|
|
41
|
-
}
|
|
42
|
-
exports.parseMIMETypeFromURL = parseMIMETypeFromURL;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// loaders.gl, MIT license
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.getResourceContentLength = exports.getResourceMIMEType = exports.getResourceUrl = void 0;
|
|
5
|
-
const is_type_1 = require("../../javascript-utils/is-type");
|
|
6
|
-
const mime_type_utils_1 = require("./mime-type-utils");
|
|
7
|
-
const url_utils_1 = require("./url-utils");
|
|
8
|
-
/**
|
|
9
|
-
* Returns the URL associated with this resource.
|
|
10
|
-
* The returned value may include a query string and need further processing.
|
|
11
|
-
* If it cannot determine url, the corresponding value will be an empty string
|
|
12
|
-
*
|
|
13
|
-
* @todo string parameters are assumed to be URLs
|
|
14
|
-
*/
|
|
15
|
-
function getResourceUrl(resource) {
|
|
16
|
-
// If resource is a `Response`, it contains the information directly as a field
|
|
17
|
-
if ((0, is_type_1.isResponse)(resource)) {
|
|
18
|
-
const response = resource;
|
|
19
|
-
return response.url;
|
|
20
|
-
}
|
|
21
|
-
// If the resource is a Blob or a File (subclass of Blob)
|
|
22
|
-
if ((0, is_type_1.isBlob)(resource)) {
|
|
23
|
-
const blob = resource;
|
|
24
|
-
// File objects have a "name" property. Blob objects don't have any
|
|
25
|
-
// url (name) information
|
|
26
|
-
return blob.name || '';
|
|
27
|
-
}
|
|
28
|
-
if (typeof resource === 'string') {
|
|
29
|
-
return resource;
|
|
30
|
-
}
|
|
31
|
-
// Unknown
|
|
32
|
-
return '';
|
|
33
|
-
}
|
|
34
|
-
exports.getResourceUrl = getResourceUrl;
|
|
35
|
-
/**
|
|
36
|
-
* Returns the URL associated with this resource.
|
|
37
|
-
* The returned value may include a query string and need further processing.
|
|
38
|
-
* If it cannot determine url, the corresponding value will be an empty string
|
|
39
|
-
*
|
|
40
|
-
* @todo string parameters are assumed to be URLs
|
|
41
|
-
*/
|
|
42
|
-
function getResourceMIMEType(resource) {
|
|
43
|
-
// If resource is a response, it contains the information directly
|
|
44
|
-
if ((0, is_type_1.isResponse)(resource)) {
|
|
45
|
-
const response = resource;
|
|
46
|
-
const contentTypeHeader = response.headers.get('content-type') || '';
|
|
47
|
-
const noQueryUrl = (0, url_utils_1.stripQueryString)(response.url);
|
|
48
|
-
return (0, mime_type_utils_1.parseMIMEType)(contentTypeHeader) || (0, mime_type_utils_1.parseMIMETypeFromURL)(noQueryUrl);
|
|
49
|
-
}
|
|
50
|
-
// If the resource is a Blob or a File (subclass of Blob)
|
|
51
|
-
if ((0, is_type_1.isBlob)(resource)) {
|
|
52
|
-
const blob = resource;
|
|
53
|
-
return blob.type || '';
|
|
54
|
-
}
|
|
55
|
-
if (typeof resource === 'string') {
|
|
56
|
-
return (0, mime_type_utils_1.parseMIMETypeFromURL)(resource);
|
|
57
|
-
}
|
|
58
|
-
// Unknown
|
|
59
|
-
return '';
|
|
60
|
-
}
|
|
61
|
-
exports.getResourceMIMEType = getResourceMIMEType;
|
|
62
|
-
/**
|
|
63
|
-
* Returns (approximate) content length for a resource if it can be determined.
|
|
64
|
-
* Returns -1 if content length cannot be determined.
|
|
65
|
-
* @param resource
|
|
66
|
-
|
|
67
|
-
* @note string parameters are NOT assumed to be URLs
|
|
68
|
-
*/
|
|
69
|
-
function getResourceContentLength(resource) {
|
|
70
|
-
if ((0, is_type_1.isResponse)(resource)) {
|
|
71
|
-
const response = resource;
|
|
72
|
-
return response.headers['content-length'] || -1;
|
|
73
|
-
}
|
|
74
|
-
if ((0, is_type_1.isBlob)(resource)) {
|
|
75
|
-
const blob = resource;
|
|
76
|
-
return blob.size;
|
|
77
|
-
}
|
|
78
|
-
if (typeof resource === 'string') {
|
|
79
|
-
// TODO - handle data URL?
|
|
80
|
-
return resource.length;
|
|
81
|
-
}
|
|
82
|
-
if (resource instanceof ArrayBuffer) {
|
|
83
|
-
return resource.byteLength;
|
|
84
|
-
}
|
|
85
|
-
if (ArrayBuffer.isView(resource)) {
|
|
86
|
-
return resource.byteLength;
|
|
87
|
-
}
|
|
88
|
-
return -1;
|
|
89
|
-
}
|
|
90
|
-
exports.getResourceContentLength = getResourceContentLength;
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkResponseSync = exports.checkResponse = exports.makeResponse = void 0;
|
|
4
|
-
const is_type_1 = require("../../javascript-utils/is-type");
|
|
5
|
-
const resource_utils_1 = require("./resource-utils");
|
|
6
|
-
/**
|
|
7
|
-
* Returns a Response object
|
|
8
|
-
* Adds content-length header when possible
|
|
9
|
-
*
|
|
10
|
-
* @param resource
|
|
11
|
-
*/
|
|
12
|
-
async function makeResponse(resource) {
|
|
13
|
-
if ((0, is_type_1.isResponse)(resource)) {
|
|
14
|
-
return resource;
|
|
15
|
-
}
|
|
16
|
-
// Add content-length header if possible
|
|
17
|
-
const headers = {};
|
|
18
|
-
const contentLength = (0, resource_utils_1.getResourceContentLength)(resource);
|
|
19
|
-
if (contentLength >= 0) {
|
|
20
|
-
headers['content-length'] = String(contentLength);
|
|
21
|
-
}
|
|
22
|
-
// `new Response(File)` does not preserve content-type and URL
|
|
23
|
-
// so we add them here
|
|
24
|
-
const url = (0, resource_utils_1.getResourceUrl)(resource);
|
|
25
|
-
const type = (0, resource_utils_1.getResourceMIMEType)(resource);
|
|
26
|
-
if (type) {
|
|
27
|
-
headers['content-type'] = type;
|
|
28
|
-
}
|
|
29
|
-
// Add a custom header with initial bytes if available
|
|
30
|
-
const initialDataUrl = await getInitialDataUrl(resource);
|
|
31
|
-
if (initialDataUrl) {
|
|
32
|
-
headers['x-first-bytes'] = initialDataUrl;
|
|
33
|
-
}
|
|
34
|
-
// TODO - is this the best way of handling strings?
|
|
35
|
-
// Maybe package as data URL instead?
|
|
36
|
-
if (typeof resource === 'string') {
|
|
37
|
-
// Convert to ArrayBuffer to avoid Response treating it as a URL
|
|
38
|
-
resource = new TextEncoder().encode(resource);
|
|
39
|
-
}
|
|
40
|
-
// Attempt to create a Response from the resource, adding headers and setting url
|
|
41
|
-
const response = new Response(resource, { headers });
|
|
42
|
-
// We can't control `Response.url` via constructor, use a property override to record URL.
|
|
43
|
-
Object.defineProperty(response, 'url', { value: url });
|
|
44
|
-
return response;
|
|
45
|
-
}
|
|
46
|
-
exports.makeResponse = makeResponse;
|
|
47
|
-
/**
|
|
48
|
-
* Checks response status (async) and throws a helpful error message if status is not OK.
|
|
49
|
-
* @param response
|
|
50
|
-
*/
|
|
51
|
-
async function checkResponse(response) {
|
|
52
|
-
if (!response.ok) {
|
|
53
|
-
const message = await getResponseError(response);
|
|
54
|
-
throw new Error(message);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.checkResponse = checkResponse;
|
|
58
|
-
/**
|
|
59
|
-
* Checks response status (sync) and throws a helpful error message if status is not OK.
|
|
60
|
-
* @param response
|
|
61
|
-
*/
|
|
62
|
-
function checkResponseSync(response) {
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
let message = `${response.status} ${response.statusText}`;
|
|
65
|
-
message = message.length > 60 ? `${message.slice(0, 60)}...` : message;
|
|
66
|
-
throw new Error(message);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
exports.checkResponseSync = checkResponseSync;
|
|
70
|
-
// HELPERS
|
|
71
|
-
async function getResponseError(response) {
|
|
72
|
-
let message = `Failed to fetch resource ${response.url} (${response.status}): `;
|
|
73
|
-
try {
|
|
74
|
-
const contentType = response.headers.get('Content-Type');
|
|
75
|
-
let text = response.statusText;
|
|
76
|
-
if (contentType.includes('application/json')) {
|
|
77
|
-
text += ` ${await response.text()}`;
|
|
78
|
-
}
|
|
79
|
-
message += text;
|
|
80
|
-
message = message.length > 60 ? `${message.slice(0, 60)}...` : message;
|
|
81
|
-
}
|
|
82
|
-
catch (error) {
|
|
83
|
-
// eslint forbids return in a finally statement, so we just catch here
|
|
84
|
-
}
|
|
85
|
-
return message;
|
|
86
|
-
}
|
|
87
|
-
async function getInitialDataUrl(resource) {
|
|
88
|
-
const INITIAL_DATA_LENGTH = 5;
|
|
89
|
-
if (typeof resource === 'string') {
|
|
90
|
-
return `data:,${resource.slice(0, INITIAL_DATA_LENGTH)}`;
|
|
91
|
-
}
|
|
92
|
-
if (resource instanceof Blob) {
|
|
93
|
-
const blobSlice = resource.slice(0, 5);
|
|
94
|
-
return await new Promise((resolve) => {
|
|
95
|
-
const reader = new FileReader();
|
|
96
|
-
reader.onload = (event) => resolve(event?.target?.result);
|
|
97
|
-
reader.readAsDataURL(blobSlice);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
if (resource instanceof ArrayBuffer) {
|
|
101
|
-
const slice = resource.slice(0, INITIAL_DATA_LENGTH);
|
|
102
|
-
const base64 = arrayBufferToBase64(slice);
|
|
103
|
-
return `data:base64,${base64}`;
|
|
104
|
-
}
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
// https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string
|
|
108
|
-
function arrayBufferToBase64(buffer) {
|
|
109
|
-
let binary = '';
|
|
110
|
-
const bytes = new Uint8Array(buffer);
|
|
111
|
-
for (let i = 0; i < bytes.byteLength; i++) {
|
|
112
|
-
binary += String.fromCharCode(bytes[i]);
|
|
113
|
-
}
|
|
114
|
-
return btoa(binary);
|
|
115
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// loaders.gl, MIT license
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.stripQueryString = exports.extractQueryString = void 0;
|
|
5
|
-
const QUERY_STRING_PATTERN = /\?.*/;
|
|
6
|
-
function extractQueryString(url) {
|
|
7
|
-
const matches = url.match(QUERY_STRING_PATTERN);
|
|
8
|
-
return matches && matches[0];
|
|
9
|
-
}
|
|
10
|
-
exports.extractQueryString = extractQueryString;
|
|
11
|
-
function stripQueryString(url) {
|
|
12
|
-
return url.replace(QUERY_STRING_PATTERN, '');
|
|
13
|
-
}
|
|
14
|
-
exports.stripQueryString = stripQueryString;
|
package/dist/null-loader.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NullLoader = exports.NullWorkerLoader = void 0;
|
|
4
|
-
// __VERSION__ is injected by babel-plugin-version-inline
|
|
5
|
-
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
6
|
-
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
7
|
-
/**
|
|
8
|
-
* Loads any data and returns null (or optionally passes through data unparsed)
|
|
9
|
-
*/
|
|
10
|
-
exports.NullWorkerLoader = {
|
|
11
|
-
name: 'Null loader',
|
|
12
|
-
id: 'null',
|
|
13
|
-
module: 'core',
|
|
14
|
-
version: VERSION,
|
|
15
|
-
worker: true,
|
|
16
|
-
mimeTypes: ['application/x.empty'],
|
|
17
|
-
extensions: ['null'],
|
|
18
|
-
tests: [() => false],
|
|
19
|
-
options: {
|
|
20
|
-
null: {}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Returns arguments passed to the parse API in a format that can be transfered to a
|
|
25
|
-
* web worker. The `context` parameter is stripped using JSON.stringify & parse.
|
|
26
|
-
*/
|
|
27
|
-
function parseSync(arrayBuffer, options, context) {
|
|
28
|
-
if (!options.null.echoParameters)
|
|
29
|
-
return null;
|
|
30
|
-
context = context && JSON.parse(JSON.stringify(context));
|
|
31
|
-
return { arrayBuffer, options, context };
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Loads any data and returns null (or optionally passes through data unparsed)
|
|
35
|
-
*/
|
|
36
|
-
exports.NullLoader = {
|
|
37
|
-
name: 'Null loader',
|
|
38
|
-
id: 'null',
|
|
39
|
-
module: 'core',
|
|
40
|
-
version: VERSION,
|
|
41
|
-
mimeTypes: ['application/x.empty'],
|
|
42
|
-
extensions: ['null'],
|
|
43
|
-
parse: async (arrayBuffer, options, context) => parseSync(arrayBuffer, options, context),
|
|
44
|
-
parseSync,
|
|
45
|
-
parseInBatches: async function* generator(asyncIterator, options, context) {
|
|
46
|
-
for await (const batch of asyncIterator) {
|
|
47
|
-
yield parseSync(batch, options, context);
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
tests: [() => false],
|
|
51
|
-
options: {
|
|
52
|
-
null: {
|
|
53
|
-
echoParameters: false
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|