@loaders.gl/core 3.1.0-alpha.5 → 3.1.0-beta.1

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 (155) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +3380 -0
  4. package/dist/core-addons/write-file-browser.d.ts +1 -0
  5. package/dist/core-addons/write-file-browser.d.ts.map +1 -0
  6. package/dist/core-addons/write-file-browser.js +60 -0
  7. package/dist/es5/lib/api/register-loaders.js.map +1 -1
  8. package/dist/es5/lib/init.js +1 -1
  9. package/dist/es5/lib/init.js.map +1 -1
  10. package/dist/es5/null-loader.js +1 -1
  11. package/dist/es5/null-loader.js.map +1 -1
  12. package/dist/esm/lib/api/register-loaders.js.map +1 -1
  13. package/dist/esm/lib/init.js +1 -1
  14. package/dist/esm/lib/init.js.map +1 -1
  15. package/dist/esm/null-loader.js +1 -1
  16. package/dist/esm/null-loader.js.map +1 -1
  17. package/dist/index.d.ts +31 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +106 -0
  20. package/dist/iterators/batch-iterators/timed-batch-iterator.d.ts +5 -0
  21. package/dist/iterators/batch-iterators/timed-batch-iterator.d.ts.map +1 -0
  22. package/dist/iterators/batch-iterators/timed-batch-iterator.js +22 -0
  23. package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts +9 -0
  24. package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts.map +1 -0
  25. package/dist/iterators/make-iterator/make-array-buffer-iterator.js +27 -0
  26. package/dist/iterators/make-iterator/make-blob-iterator.d.ts +9 -0
  27. package/dist/iterators/make-iterator/make-blob-iterator.d.ts.map +1 -0
  28. package/dist/iterators/make-iterator/make-blob-iterator.js +21 -0
  29. package/dist/iterators/make-iterator/make-iterator.d.ts +19 -0
  30. package/dist/iterators/make-iterator/make-iterator.d.ts.map +1 -0
  31. package/dist/iterators/make-iterator/make-iterator.js +37 -0
  32. package/dist/iterators/make-iterator/make-stream-iterator.d.ts +11 -0
  33. package/dist/iterators/make-iterator/make-stream-iterator.d.ts.map +1 -0
  34. package/dist/iterators/make-iterator/make-stream-iterator.js +96 -0
  35. package/dist/iterators/make-iterator/make-string-iterator.d.ts +9 -0
  36. package/dist/iterators/make-iterator/make-string-iterator.d.ts.map +1 -0
  37. package/dist/iterators/make-iterator/make-string-iterator.js +24 -0
  38. package/dist/iterators/make-stream/make-dom-stream.d.ts +9 -0
  39. package/dist/iterators/make-stream/make-dom-stream.d.ts.map +1 -0
  40. package/dist/iterators/make-stream/make-dom-stream.js +41 -0
  41. package/dist/iterators/make-stream/make-node-stream.d.ts +7 -0
  42. package/dist/iterators/make-stream/make-node-stream.d.ts.map +1 -0
  43. package/dist/iterators/make-stream/make-node-stream.js +58 -0
  44. package/dist/iterators/make-stream/make-stream.d.ts +9 -0
  45. package/dist/iterators/make-stream/make-stream.d.ts.map +1 -0
  46. package/dist/iterators/make-stream/make-stream.js +16 -0
  47. package/dist/javascript-utils/is-type.d.ts +22 -0
  48. package/dist/javascript-utils/is-type.d.ts.map +1 -0
  49. package/dist/javascript-utils/is-type.js +41 -0
  50. package/dist/lib/api/encode.d.ts +26 -0
  51. package/dist/lib/api/encode.d.ts.map +1 -0
  52. package/dist/lib/api/encode.js +111 -0
  53. package/dist/lib/api/load-in-batches.d.ts +13 -0
  54. package/dist/lib/api/load-in-batches.d.ts.map +1 -0
  55. package/dist/lib/api/load-in-batches.js +33 -0
  56. package/dist/lib/api/load.d.ts +12 -0
  57. package/dist/lib/api/load.d.ts.map +1 -0
  58. package/dist/lib/api/load.js +41 -0
  59. package/dist/lib/api/parse-in-batches.d.ts +10 -0
  60. package/dist/lib/api/parse-in-batches.d.ts.map +1 -0
  61. package/dist/lib/api/parse-in-batches.js +120 -0
  62. package/dist/lib/api/parse-sync.d.ts +10 -0
  63. package/dist/lib/api/parse-sync.d.ts.map +1 -0
  64. package/dist/lib/api/parse-sync.js +59 -0
  65. package/dist/lib/api/parse.d.ts +10 -0
  66. package/dist/lib/api/parse.d.ts.map +1 -0
  67. package/dist/lib/api/parse.js +74 -0
  68. package/dist/lib/api/register-loaders.d.ts +7 -0
  69. package/dist/lib/api/register-loaders.d.ts.map +1 -0
  70. package/dist/lib/api/register-loaders.js +35 -0
  71. package/dist/lib/api/save.d.ts +4 -0
  72. package/dist/lib/api/save.d.ts.map +1 -0
  73. package/dist/lib/api/save.js +15 -0
  74. package/dist/lib/api/select-loader.d.ts +22 -0
  75. package/dist/lib/api/select-loader.d.ts.map +1 -0
  76. package/dist/lib/api/select-loader.js +239 -0
  77. package/dist/lib/api/set-loader-options.d.ts +6 -0
  78. package/dist/lib/api/set-loader-options.d.ts.map +1 -0
  79. package/dist/lib/api/set-loader-options.js +12 -0
  80. package/dist/lib/common.d.ts +2 -0
  81. package/dist/lib/common.d.ts.map +1 -0
  82. package/dist/lib/common.js +2 -0
  83. package/dist/lib/fetch/fetch-error-message.d.ts +3 -0
  84. package/dist/lib/fetch/fetch-error-message.d.ts.map +1 -0
  85. package/dist/lib/fetch/fetch-error-message.js +25 -0
  86. package/dist/lib/fetch/fetch-file.d.ts +13 -0
  87. package/dist/lib/fetch/fetch-file.d.ts.map +1 -0
  88. package/dist/lib/fetch/fetch-file.js +27 -0
  89. package/dist/lib/fetch/read-array-buffer.d.ts +17 -0
  90. package/dist/lib/fetch/read-array-buffer.d.ts.map +1 -0
  91. package/dist/lib/fetch/read-array-buffer.js +41 -0
  92. package/dist/lib/fetch/read-file.d.ts +5 -0
  93. package/dist/lib/fetch/read-file.d.ts.map +1 -0
  94. package/dist/lib/fetch/read-file.js +29 -0
  95. package/dist/lib/fetch/write-file.d.ts +3 -0
  96. package/dist/lib/fetch/write-file.d.ts.map +1 -0
  97. package/dist/lib/fetch/write-file.js +22 -0
  98. package/dist/lib/filesystems/browser-filesystem.d.ts +58 -0
  99. package/dist/lib/filesystems/browser-filesystem.d.ts.map +1 -0
  100. package/dist/lib/filesystems/browser-filesystem.js +126 -0
  101. package/dist/lib/filesystems/filesystem.d.ts +46 -0
  102. package/dist/lib/filesystems/filesystem.d.ts.map +1 -0
  103. package/dist/lib/filesystems/filesystem.js +2 -0
  104. package/dist/lib/filesystems/read-array-buffer.d.ts +19 -0
  105. package/dist/lib/filesystems/read-array-buffer.d.ts.map +1 -0
  106. package/dist/lib/filesystems/read-array-buffer.js +29 -0
  107. package/dist/lib/init.d.ts +3 -0
  108. package/dist/lib/init.d.ts.map +1 -0
  109. package/dist/lib/init.js +10 -0
  110. package/dist/lib/loader-utils/check-errors.d.ts +3 -0
  111. package/dist/lib/loader-utils/check-errors.d.ts.map +1 -0
  112. package/dist/lib/loader-utils/check-errors.js +30 -0
  113. package/dist/lib/loader-utils/get-data.d.ts +6 -0
  114. package/dist/lib/loader-utils/get-data.d.ts.map +1 -0
  115. package/dist/lib/loader-utils/get-data.js +129 -0
  116. package/dist/lib/loader-utils/loader-context.d.ts +13 -0
  117. package/dist/lib/loader-utils/loader-context.d.ts.map +1 -0
  118. package/dist/lib/loader-utils/loader-context.js +49 -0
  119. package/dist/lib/loader-utils/loggers.d.ts +16 -0
  120. package/dist/lib/loader-utils/loggers.d.ts.map +1 -0
  121. package/dist/lib/loader-utils/loggers.js +41 -0
  122. package/dist/lib/loader-utils/normalize-loader.d.ts +4 -0
  123. package/dist/lib/loader-utils/normalize-loader.d.ts.map +1 -0
  124. package/dist/lib/loader-utils/normalize-loader.js +52 -0
  125. package/dist/lib/loader-utils/option-defaults.d.ts +20 -0
  126. package/dist/lib/loader-utils/option-defaults.d.ts.map +1 -0
  127. package/dist/lib/loader-utils/option-defaults.js +41 -0
  128. package/dist/lib/loader-utils/option-utils.d.ts +36 -0
  129. package/dist/lib/loader-utils/option-utils.d.ts.map +1 -0
  130. package/dist/lib/loader-utils/option-utils.js +177 -0
  131. package/dist/lib/progress/fetch-progress.d.ts +6 -0
  132. package/dist/lib/progress/fetch-progress.d.ts.map +1 -0
  133. package/dist/lib/progress/fetch-progress.js +59 -0
  134. package/dist/lib/utils/mime-type-utils.d.ts +20 -0
  135. package/dist/lib/utils/mime-type-utils.d.ts.map +1 -0
  136. package/dist/lib/utils/mime-type-utils.js +42 -0
  137. package/dist/lib/utils/resource-utils.d.ts +21 -0
  138. package/dist/lib/utils/resource-utils.d.ts.map +1 -0
  139. package/dist/lib/utils/resource-utils.js +78 -0
  140. package/dist/lib/utils/response-utils.d.ts +18 -0
  141. package/dist/lib/utils/response-utils.d.ts.map +1 -0
  142. package/dist/lib/utils/response-utils.js +114 -0
  143. package/dist/null-loader.d.ts +10 -0
  144. package/dist/null-loader.d.ts.map +1 -0
  145. package/dist/null-loader.js +44 -0
  146. package/dist/null-worker.js +178 -2
  147. package/dist/workers/null-worker.d.ts +2 -0
  148. package/dist/workers/null-worker.d.ts.map +1 -0
  149. package/dist/workers/null-worker.js +5 -0
  150. package/package.json +7 -7
  151. package/src/lib/api/register-loaders.ts +2 -1
  152. package/src/lib/{init.js → init.ts} +0 -0
  153. package/dist/dist.min.js +0 -2
  154. package/dist/dist.min.js.map +0 -1
  155. package/dist/null-worker.js.map +0 -1
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseInBatches = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ const normalize_loader_1 = require("../loader-utils/normalize-loader");
6
+ const option_utils_1 = require("../loader-utils/option-utils");
7
+ const loader_context_1 = require("../loader-utils/loader-context");
8
+ const get_data_1 = require("../loader-utils/get-data");
9
+ const resource_utils_1 = require("../utils/resource-utils");
10
+ const select_loader_1 = require("./select-loader");
11
+ // Ensure `parse` is available in context if loader falls back to `parse`
12
+ const parse_1 = require("./parse");
13
+ /**
14
+ * Parses `data` using a specified loader
15
+ * @param data
16
+ * @param loaders
17
+ * @param options
18
+ * @param context
19
+ */
20
+ async function parseInBatches(data, loaders, options, context) {
21
+ (0, loader_utils_1.assert)(!context || typeof context === 'object'); // parseInBatches no longer accepts final url
22
+ // Signature: parseInBatches(data, options, url) - Uses registered loaders
23
+ if (!Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
24
+ context = undefined; // context not supported in short signature
25
+ options = loaders;
26
+ loaders = undefined;
27
+ }
28
+ data = await data; // Resolve any promise
29
+ options = options || {};
30
+ // Extract a url for auto detection
31
+ const { url } = (0, resource_utils_1.getResourceUrlAndType)(data);
32
+ // Chooses a loader and normalizes it
33
+ // Note - only uses URL and contentType for streams and iterator inputs
34
+ const loader = await (0, select_loader_1.selectLoader)(data, loaders, options);
35
+ // Note: if options.nothrow was set, it is possible that no loader was found, if so just return null
36
+ if (!loader) {
37
+ // @ts-ignore
38
+ return null;
39
+ }
40
+ // Normalize options
41
+ // @ts-ignore
42
+ options = (0, option_utils_1.normalizeOptions)(options, loader, loaders, url);
43
+ // @ts-ignore
44
+ context = (0, loader_context_1.getLoaderContext)(
45
+ // @ts-ignore
46
+ { url, parseInBatches, parse: parse_1.parse, loaders: loaders }, options, context);
47
+ return await parseWithLoaderInBatches(loader, data, options, context);
48
+ }
49
+ exports.parseInBatches = parseInBatches;
50
+ /**
51
+ * Loader has been selected and context has been prepared, see if we need to emit a metadata batch
52
+ */
53
+ async function parseWithLoaderInBatches(loader, data, options, context) {
54
+ const outputIterator = await parseToOutputIterator(loader, data, options, context);
55
+ // Generate metadata batch if requested
56
+ if (!options.metadata) {
57
+ return outputIterator;
58
+ }
59
+ const metadataBatch = {
60
+ batchType: 'metadata',
61
+ metadata: {
62
+ _loader: loader,
63
+ _context: context
64
+ },
65
+ // Populate with some default fields to avoid crashing
66
+ data: [],
67
+ bytesUsed: 0
68
+ };
69
+ async function* makeMetadataBatchIterator(iterator) {
70
+ yield metadataBatch;
71
+ yield* iterator;
72
+ }
73
+ return makeMetadataBatchIterator(outputIterator);
74
+ }
75
+ /**
76
+ * Prep work is done, now it is time to start parsing into an output operator
77
+ * The approach depends on which parse function the loader exposes
78
+ * `parseInBatches` (preferred), `parse` (fallback)
79
+ */
80
+ async function parseToOutputIterator(loader, data, options, context) {
81
+ // Get an iterator from the input
82
+ const inputIterator = await (0, get_data_1.getAsyncIterableFromData)(data, options);
83
+ // Apply any iterator transforms (options.transforms)
84
+ const transformedIterator = await applyInputTransforms(inputIterator, options?.transforms || []);
85
+ // If loader supports parseInBatches, we are done
86
+ if (loader.parseInBatches) {
87
+ return loader.parseInBatches(transformedIterator, options, context);
88
+ }
89
+ // Fallback: load atomically using `parse` concatenating input iterator into single chunk
90
+ async function* parseChunkInBatches() {
91
+ const arrayBuffer = await (0, loader_utils_1.concatenateArrayBuffersAsync)(transformedIterator);
92
+ // Call `parse` instead of `loader.parse` to ensure we can call workers etc.
93
+ const parsedData = await (0, parse_1.parse)(arrayBuffer, loader,
94
+ // TODO - Hack: supply loaders MIME type to ensure we match it
95
+ { ...options, mimeType: loader.mimeTypes[0] }, context);
96
+ // yield a single batch, the output from loader.parse()
97
+ // TODO - run through batch builder to apply options etc...
98
+ const batch = {
99
+ mimeType: loader.mimeTypes[0],
100
+ shape: Array.isArray(parsedData) ? 'row-table' : 'unknown',
101
+ batchType: 'data',
102
+ data: parsedData,
103
+ length: Array.isArray(parsedData) ? parsedData.length : 1
104
+ };
105
+ yield batch;
106
+ }
107
+ return parseChunkInBatches();
108
+ }
109
+ /**
110
+ * Create an iterator chain with any transform iterators (crypto, decompression)
111
+ * @param inputIterator
112
+ * @param options
113
+ */
114
+ async function applyInputTransforms(inputIterator, transforms = []) {
115
+ let iteratorChain = inputIterator;
116
+ for await (const transformBatches of transforms) {
117
+ iteratorChain = transformBatches(iteratorChain);
118
+ }
119
+ return iteratorChain;
120
+ }
@@ -0,0 +1,10 @@
1
+ import type { SyncDataType, Loader, LoaderContext, LoaderOptions } from '@loaders.gl/loader-utils';
2
+ /**
3
+ * Parses `data` synchronously using a specified loader
4
+ * @param data
5
+ * @param loaders
6
+ * @param options
7
+ * @param context
8
+ */
9
+ export declare function parseSync(data: SyncDataType, loaders?: Loader | Loader[] | LoaderOptions, options?: LoaderOptions, context?: LoaderContext): any;
10
+ //# sourceMappingURL=parse-sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-sync.d.ts","sourceRoot":"","sources":["../../../src/lib/api/parse-sync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,MAAM,EAEN,aAAa,EACb,aAAa,EACd,MAAM,0BAA0B,CAAC;AASlC;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,aAAa,EAC3C,OAAO,CAAC,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE,aAAa,GACtB,GAAG,CAmCL"}
@@ -0,0 +1,59 @@
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.getResourceUrlAndType)(data);
41
+ const parse = () => {
42
+ throw new Error('parseSync called parse');
43
+ };
44
+ context = (0, loader_context_1.getLoaderContext)({ url, parseSync, parse, loaders: loaders }, options);
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
+ }
@@ -0,0 +1,10 @@
1
+ import type { DataType, Loader, LoaderContext, LoaderOptions } from '@loaders.gl/loader-utils';
2
+ /**
3
+ * Parses `data` using a specified loader
4
+ * @param data
5
+ * @param loaders
6
+ * @param options
7
+ * @param context
8
+ */
9
+ export declare function parse(data: DataType | Promise<DataType>, loaders?: Loader | Loader[] | LoaderOptions, options?: LoaderOptions, context?: LoaderContext): Promise<any>;
10
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/lib/api/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAU7F;;;;;;GAMG;AACH,wBAAsB,KAAK,CACzB,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,EAClC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,aAAa,EAC3C,OAAO,CAAC,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,GAAG,CAAC,CAmCd"}
@@ -0,0 +1,74 @@
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 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
+ const select_loader_1 = require("./select-loader");
12
+ /**
13
+ * Parses `data` using a specified loader
14
+ * @param data
15
+ * @param loaders
16
+ * @param options
17
+ * @param context
18
+ */
19
+ async function parse(data, loaders, options, context) {
20
+ (0, worker_utils_1.assert)(!context || typeof context === 'object'); // parse no longer accepts final url
21
+ // Signature: parse(data, options, context | url)
22
+ // Uses registered loaders
23
+ if (loaders && !Array.isArray(loaders) && !(0, normalize_loader_1.isLoaderObject)(loaders)) {
24
+ context = undefined; // context not supported in short signature
25
+ options = loaders;
26
+ loaders = undefined;
27
+ }
28
+ data = await data; // Resolve any promise
29
+ options = options || {};
30
+ // Extract a url for auto detection
31
+ const { url } = (0, resource_utils_1.getResourceUrlAndType)(data);
32
+ // Chooses a loader (and normalizes it)
33
+ // Also use any loaders in the context, new loaders take priority
34
+ const typedLoaders = loaders;
35
+ const candidateLoaders = (0, loader_context_1.getLoadersFromContext)(typedLoaders, context);
36
+ // todo hacky type cast
37
+ const loader = await (0, select_loader_1.selectLoader)(data, candidateLoaders, options);
38
+ // Note: if no loader was found, if so just return null
39
+ if (!loader) {
40
+ return null;
41
+ }
42
+ // Normalize options
43
+ options = (0, option_utils_1.normalizeOptions)(options, loader, candidateLoaders, url);
44
+ // Get a context (if already present, will be unchanged)
45
+ context = (0, loader_context_1.getLoaderContext)({ url, parse, loaders: candidateLoaders }, options, context);
46
+ return await parseWithLoader(loader, data, options, context);
47
+ }
48
+ exports.parse = parse;
49
+ // TODO: support progress and abort
50
+ // TODO - should accept loader.parseAsyncIterator and concatenate.
51
+ async function parseWithLoader(loader, data, options, context) {
52
+ (0, worker_utils_1.validateWorkerVersion)(loader);
53
+ data = await (0, get_data_1.getArrayBufferOrStringFromData)(data, loader, options);
54
+ // First check for synchronous text parser, wrap results in promises
55
+ if (loader.parseTextSync && typeof data === 'string') {
56
+ options.dataType = 'text';
57
+ return loader.parseTextSync(data, options, context, loader);
58
+ }
59
+ // If we have a workerUrl and the loader can parse the given options efficiently in a worker
60
+ if ((0, loader_utils_1.canParseWithWorker)(loader, options)) {
61
+ return await (0, loader_utils_1.parseWithWorker)(loader, data, options, context, parse);
62
+ }
63
+ // Check for asynchronous parser
64
+ if (loader.parseText && typeof data === 'string') {
65
+ return await loader.parseText(data, options, context, loader);
66
+ }
67
+ if (loader.parse) {
68
+ return await loader.parse(data, options, context, loader);
69
+ }
70
+ // This should not happen, all sync loaders should also offer `parse` function
71
+ (0, worker_utils_1.assert)(!loader.parseSync);
72
+ // TBD - If asynchronous parser not available, return null
73
+ throw new Error(`${loader.id} loader - no parser found and worker is disabled`);
74
+ }
@@ -0,0 +1,7 @@
1
+ import { Loader } from '@loaders.gl/loader-utils';
2
+ /** Register a list of global loaders */
3
+ export declare function registerLoaders(loaders: Loader[]): void;
4
+ export declare function getRegisteredLoaders(): Loader[];
5
+ /** @deprecated For testing only */
6
+ export declare function _unregisterLoaders(): void;
7
+ //# sourceMappingURL=register-loaders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register-loaders.d.ts","sourceRoot":"","sources":["../../../src/lib/api/register-loaders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,0BAA0B,CAAC;AAYhD,wCAAwC;AACxC,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAYhD;AAED,wBAAgB,oBAAoB,IAAI,MAAM,EAAE,CAE/C;AAED,oCAAoC;AACpC,wBAAgB,kBAAkB,SAGjC"}
@@ -0,0 +1,35 @@
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;
@@ -0,0 +1,4 @@
1
+ import type { Writer, WriterOptions } from '@loaders.gl/loader-utils';
2
+ export declare function save(data: any, url: any, writer: Writer, options: WriterOptions): Promise<void>;
3
+ export declare function saveSync(data: any, url: any, writer: any, options: any): void;
4
+ //# sourceMappingURL=save.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"save.d.ts","sourceRoot":"","sources":["../../../src/lib/api/save.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAIpE,wBAAsB,IAAI,CAAC,IAAI,KAAA,EAAE,GAAG,KAAA,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,iBAG3E;AAED,wBAAgB,QAAQ,CAAC,IAAI,KAAA,EAAE,GAAG,KAAA,EAAE,MAAM,KAAA,EAAE,OAAO,KAAA,QAGlD"}
@@ -0,0 +1,15 @@
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;
@@ -0,0 +1,22 @@
1
+ import type { LoaderContext, LoaderOptions, Loader } from '@loaders.gl/loader-utils';
2
+ /**
3
+ * Find a loader that matches file extension and/or initial file content
4
+ * Search the loaders array argument for a loader that matches url extension or initial data
5
+ * Returns: a normalized loader
6
+ * @param data data to assist
7
+ * @param loaders
8
+ * @param options
9
+ * @param context used internally, applications should not provide this parameter
10
+ */
11
+ export declare function selectLoader(data: Response | Blob | ArrayBuffer | string, loaders?: Loader[] | Loader, options?: LoaderOptions, context?: LoaderContext): Promise<Loader | null>;
12
+ /**
13
+ * Find a loader that matches file extension and/or initial file content
14
+ * Search the loaders array argument for a loader that matches url extension or initial data
15
+ * Returns: a normalized loader
16
+ * @param data data to assist
17
+ * @param loaders
18
+ * @param options
19
+ * @param context used internally, applications should not provide this parameter
20
+ */
21
+ export declare function selectLoaderSync(data: Response | Blob | ArrayBuffer | string, loaders?: Loader[] | Loader, options?: LoaderOptions, context?: LoaderContext): Loader | null;
22
+ //# sourceMappingURL=select-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"select-loader.d.ts","sourceRoot":"","sources":["../../../src/lib/api/select-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAE,aAAa,EAAE,MAAM,EAAC,MAAM,0BAA0B,CAAC;AAanF;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,IAAI,EAAE,QAAQ,GAAG,IAAI,GAAG,WAAW,GAAG,MAAM,EAC5C,OAAO,GAAE,MAAM,EAAE,GAAG,MAAW,EAC/B,OAAO,CAAC,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAwBxB;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,QAAQ,GAAG,IAAI,GAAG,WAAW,GAAG,MAAM,EAC5C,OAAO,GAAE,MAAM,EAAE,GAAG,MAAW,EAC/B,OAAO,CAAC,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE,aAAa,GACtB,MAAM,GAAG,IAAI,CAmCf"}
@@ -0,0 +1,239 @@
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 resource_utils_1 = require("../utils/resource-utils");
7
+ const register_loaders_1 = require("./register-loaders");
8
+ const is_type_1 = require("../../javascript-utils/is-type");
9
+ const EXT_PATTERN = /\.([^.]+)$/;
10
+ // TODO - Need a variant that peeks at streams for parseInBatches
11
+ // TODO - Detect multiple matching loaders? Use heuristics to grade matches?
12
+ // TODO - Allow apps to pass context to disambiguate between multiple matches (e.g. multiple .json formats)?
13
+ /**
14
+ * Find a loader that matches file extension and/or initial file content
15
+ * Search the loaders array argument for a loader that matches url extension or initial data
16
+ * Returns: a normalized loader
17
+ * @param data data to assist
18
+ * @param loaders
19
+ * @param options
20
+ * @param context used internally, applications should not provide this parameter
21
+ */
22
+ async function selectLoader(data, loaders = [], options, context) {
23
+ if (!validHTTPResponse(data)) {
24
+ return null;
25
+ }
26
+ // First make a sync attempt, disabling exceptions
27
+ let loader = selectLoaderSync(data, loaders, { ...options, nothrow: true }, context);
28
+ if (loader) {
29
+ return loader;
30
+ }
31
+ // For Blobs and Files, try to asynchronously read a small initial slice and test again with that
32
+ // to see if we can detect by initial content
33
+ if ((0, is_type_1.isBlob)(data)) {
34
+ data = await data.slice(0, 10).arrayBuffer();
35
+ loader = selectLoaderSync(data, loaders, options, context);
36
+ }
37
+ // no loader available
38
+ if (!loader && !options?.nothrow) {
39
+ throw new Error(getNoValidLoaderMessage(data));
40
+ }
41
+ return loader;
42
+ }
43
+ exports.selectLoader = selectLoader;
44
+ /**
45
+ * Find a loader that matches file extension and/or initial file content
46
+ * Search the loaders array argument for a loader that matches url extension or initial data
47
+ * Returns: a normalized loader
48
+ * @param data data to assist
49
+ * @param loaders
50
+ * @param options
51
+ * @param context used internally, applications should not provide this parameter
52
+ */
53
+ function selectLoaderSync(data, loaders = [], options, context) {
54
+ if (!validHTTPResponse(data)) {
55
+ return null;
56
+ }
57
+ // eslint-disable-next-line complexity
58
+ // if only a single loader was provided (not as array), force its use
59
+ // TODO - Should this behavior be kept and documented?
60
+ if (loaders && !Array.isArray(loaders)) {
61
+ // TODO - remove support for legacy loaders
62
+ return (0, normalize_loader_1.normalizeLoader)(loaders);
63
+ }
64
+ // Build list of candidate loaders that will be searched in order for a match
65
+ let candidateLoaders = [];
66
+ // First search supplied loaders
67
+ if (loaders) {
68
+ candidateLoaders = candidateLoaders.concat(loaders);
69
+ }
70
+ // Then fall back to registered loaders
71
+ if (!options?.ignoreRegisteredLoaders) {
72
+ candidateLoaders.push(...(0, register_loaders_1.getRegisteredLoaders)());
73
+ }
74
+ // TODO - remove support for legacy loaders
75
+ normalizeLoaders(candidateLoaders);
76
+ const loader = selectLoaderInternal(data, candidateLoaders, options, context);
77
+ // no loader available
78
+ if (!loader && !options?.nothrow) {
79
+ throw new Error(getNoValidLoaderMessage(data));
80
+ }
81
+ return loader;
82
+ }
83
+ exports.selectLoaderSync = selectLoaderSync;
84
+ /** Implements loaders selection logic */
85
+ function selectLoaderInternal(data, loaders, options, context) {
86
+ const { url, type } = (0, resource_utils_1.getResourceUrlAndType)(data);
87
+ const testUrl = url || context?.url;
88
+ let loader = null;
89
+ // if options.mimeType is supplied, it takes precedence
90
+ if (options?.mimeType) {
91
+ loader = findLoaderByMIMEType(loaders, options?.mimeType);
92
+ }
93
+ // Look up loader by url
94
+ loader = loader || findLoaderByUrl(loaders, testUrl);
95
+ // Look up loader by mime type
96
+ loader = loader || findLoaderByMIMEType(loaders, type);
97
+ // Look for loader via initial bytes (Note: not always accessible (e.g. Response, stream, async iterator)
98
+ loader = loader || findLoaderByInitialBytes(loaders, data);
99
+ // Look up loader by fallback mime type
100
+ loader = loader || findLoaderByMIMEType(loaders, options?.fallbackMimeType);
101
+ return loader;
102
+ }
103
+ /** Check HTTP Response */
104
+ function validHTTPResponse(data) {
105
+ // HANDLE HTTP status
106
+ if (data instanceof Response) {
107
+ // 204 - NO CONTENT. This handles cases where e.g. a tile server responds with 204 for a missing tile
108
+ if (data.status === 204) {
109
+ return false;
110
+ }
111
+ }
112
+ return true;
113
+ }
114
+ /** Generate a helpful message to help explain why loader selection failed. */
115
+ function getNoValidLoaderMessage(data) {
116
+ const { url, type } = (0, resource_utils_1.getResourceUrlAndType)(data);
117
+ let message = 'No valid loader found (';
118
+ message += url ? `${loader_utils_1.path.filename(url)}, ` : 'no url provided, ';
119
+ message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
120
+ // First characters are only accessible when called on data (string or arrayBuffer).
121
+ const firstCharacters = data ? getFirstCharacters(data) : '';
122
+ message += firstCharacters ? ` first bytes: "${firstCharacters}"` : 'first bytes: not available';
123
+ message += ')';
124
+ return message;
125
+ }
126
+ function normalizeLoaders(loaders) {
127
+ for (const loader of loaders) {
128
+ (0, normalize_loader_1.normalizeLoader)(loader);
129
+ }
130
+ }
131
+ // TODO - Would be nice to support http://example.com/file.glb?parameter=1
132
+ // E.g: x = new URL('http://example.com/file.glb?load=1'; x.pathname
133
+ function findLoaderByUrl(loaders, url) {
134
+ // Get extension
135
+ const match = url && EXT_PATTERN.exec(url);
136
+ const extension = match && match[1];
137
+ return extension ? findLoaderByExtension(loaders, extension) : null;
138
+ }
139
+ function findLoaderByExtension(loaders, extension) {
140
+ extension = extension.toLowerCase();
141
+ for (const loader of loaders) {
142
+ for (const loaderExtension of loader.extensions) {
143
+ if (loaderExtension.toLowerCase() === extension) {
144
+ return loader;
145
+ }
146
+ }
147
+ }
148
+ return null;
149
+ }
150
+ function findLoaderByMIMEType(loaders, mimeType) {
151
+ for (const loader of loaders) {
152
+ if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
153
+ return loader;
154
+ }
155
+ // Support referring to loaders using the "unregistered tree"
156
+ // https://en.wikipedia.org/wiki/Media_type#Unregistered_tree
157
+ if (mimeType === `application/x.${loader.id}`) {
158
+ return loader;
159
+ }
160
+ }
161
+ return null;
162
+ }
163
+ function findLoaderByInitialBytes(loaders, data) {
164
+ if (!data) {
165
+ return null;
166
+ }
167
+ for (const loader of loaders) {
168
+ if (typeof data === 'string') {
169
+ if (testDataAgainstText(data, loader)) {
170
+ return loader;
171
+ }
172
+ }
173
+ else if (ArrayBuffer.isView(data)) {
174
+ // Typed Arrays can have offsets into underlying buffer
175
+ if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) {
176
+ return loader;
177
+ }
178
+ }
179
+ else if (data instanceof ArrayBuffer) {
180
+ const byteOffset = 0;
181
+ if (testDataAgainstBinary(data, byteOffset, loader)) {
182
+ return loader;
183
+ }
184
+ }
185
+ // TODO Handle streaming case (requires creating a new AsyncIterator)
186
+ }
187
+ return null;
188
+ }
189
+ function testDataAgainstText(data, loader) {
190
+ if (loader.testText) {
191
+ return loader.testText(data);
192
+ }
193
+ const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
194
+ return tests.some((test) => data.startsWith(test));
195
+ }
196
+ function testDataAgainstBinary(data, byteOffset, loader) {
197
+ const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
198
+ return tests.some((test) => testBinary(data, byteOffset, loader, test));
199
+ }
200
+ function testBinary(data, byteOffset, loader, test) {
201
+ if (test instanceof ArrayBuffer) {
202
+ return (0, loader_utils_1.compareArrayBuffers)(test, data, test.byteLength);
203
+ }
204
+ switch (typeof test) {
205
+ case 'function':
206
+ return test(data, loader);
207
+ case 'string':
208
+ // Magic bytes check: If `test` is a string, check if binary data starts with that strings
209
+ const magic = getMagicString(data, byteOffset, test.length);
210
+ return test === magic;
211
+ default:
212
+ return false;
213
+ }
214
+ }
215
+ function getFirstCharacters(data, length = 5) {
216
+ if (typeof data === 'string') {
217
+ return data.slice(0, length);
218
+ }
219
+ else if (ArrayBuffer.isView(data)) {
220
+ // Typed Arrays can have offsets into underlying buffer
221
+ return getMagicString(data.buffer, data.byteOffset, length);
222
+ }
223
+ else if (data instanceof ArrayBuffer) {
224
+ const byteOffset = 0;
225
+ return getMagicString(data, byteOffset, length);
226
+ }
227
+ return '';
228
+ }
229
+ function getMagicString(arrayBuffer, byteOffset, length) {
230
+ if (arrayBuffer.byteLength < byteOffset + length) {
231
+ return '';
232
+ }
233
+ const dataView = new DataView(arrayBuffer);
234
+ let magic = '';
235
+ for (let i = 0; i < length; i++) {
236
+ magic += String.fromCharCode(dataView.getUint8(byteOffset + i));
237
+ }
238
+ return magic;
239
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Set global loader options
3
+ * @param options
4
+ */
5
+ export declare function setLoaderOptions(options: object): void;
6
+ //# sourceMappingURL=set-loader-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-loader-options.d.ts","sourceRoot":"","sources":["../../../src/lib/api/set-loader-options.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAEtD"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setLoaderOptions = void 0;
4
+ const option_utils_1 = require("../loader-utils/option-utils");
5
+ /**
6
+ * Set global loader options
7
+ * @param options
8
+ */
9
+ function setLoaderOptions(options) {
10
+ (0, option_utils_1.setGlobalOptions)(options);
11
+ }
12
+ exports.setLoaderOptions = setLoaderOptions;
@@ -0,0 +1,2 @@
1
+ export type { LoaderWithParser } from '@loaders.gl/loader-utils';
2
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/lib/common.ts"],"names":[],"mappings":"AAAA,YAAY,EAAC,gBAAgB,EAAC,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });