@loaders.gl/core 3.4.0-alpha.4 → 3.4.0-alpha.5

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 (65) hide show
  1. package/dist/dist.min.js +76 -45
  2. package/dist/es5/lib/api/parse-in-batches.js +15 -14
  3. package/dist/es5/lib/api/parse-in-batches.js.map +1 -1
  4. package/dist/es5/lib/api/parse-sync.js +3 -4
  5. package/dist/es5/lib/api/parse-sync.js.map +1 -1
  6. package/dist/es5/lib/api/parse.js +3 -3
  7. package/dist/es5/lib/api/parse.js.map +1 -1
  8. package/dist/es5/lib/api/select-loader.js +6 -7
  9. package/dist/es5/lib/api/select-loader.js.map +1 -1
  10. package/dist/es5/lib/init.js +1 -1
  11. package/dist/es5/lib/loader-utils/loader-context.js +16 -8
  12. package/dist/es5/lib/loader-utils/loader-context.js.map +1 -1
  13. package/dist/es5/lib/utils/resource-utils.js +31 -27
  14. package/dist/es5/lib/utils/resource-utils.js.map +1 -1
  15. package/dist/es5/lib/utils/response-utils.js +6 -5
  16. package/dist/es5/lib/utils/response-utils.js.map +1 -1
  17. package/dist/es5/lib/utils/url-utils.js +16 -0
  18. package/dist/es5/lib/utils/url-utils.js.map +1 -0
  19. package/dist/es5/null-loader.js +1 -1
  20. package/dist/esm/lib/api/parse-in-batches.js +6 -7
  21. package/dist/esm/lib/api/parse-in-batches.js.map +1 -1
  22. package/dist/esm/lib/api/parse-sync.js +4 -6
  23. package/dist/esm/lib/api/parse-sync.js.map +1 -1
  24. package/dist/esm/lib/api/parse.js +3 -5
  25. package/dist/esm/lib/api/parse.js.map +1 -1
  26. package/dist/esm/lib/api/select-loader.js +7 -10
  27. package/dist/esm/lib/api/select-loader.js.map +1 -1
  28. package/dist/esm/lib/init.js +1 -1
  29. package/dist/esm/lib/loader-utils/loader-context.js +16 -8
  30. package/dist/esm/lib/loader-utils/loader-context.js.map +1 -1
  31. package/dist/esm/lib/utils/resource-utils.js +29 -26
  32. package/dist/esm/lib/utils/resource-utils.js.map +1 -1
  33. package/dist/esm/lib/utils/response-utils.js +3 -5
  34. package/dist/esm/lib/utils/response-utils.js.map +1 -1
  35. package/dist/esm/lib/utils/url-utils.js +9 -0
  36. package/dist/esm/lib/utils/url-utils.js.map +1 -0
  37. package/dist/esm/null-loader.js +1 -1
  38. package/dist/lib/api/parse-in-batches.d.ts.map +1 -1
  39. package/dist/lib/api/parse-in-batches.js +4 -7
  40. package/dist/lib/api/parse-sync.d.ts.map +1 -1
  41. package/dist/lib/api/parse-sync.js +3 -3
  42. package/dist/lib/api/parse.js +2 -2
  43. package/dist/lib/api/select-loader.d.ts.map +1 -1
  44. package/dist/lib/api/select-loader.js +6 -3
  45. package/dist/lib/loader-utils/loader-context.d.ts +1 -1
  46. package/dist/lib/loader-utils/loader-context.d.ts.map +1 -1
  47. package/dist/lib/loader-utils/loader-context.js +17 -7
  48. package/dist/lib/utils/resource-utils.d.ts +17 -8
  49. package/dist/lib/utils/resource-utils.d.ts.map +1 -1
  50. package/dist/lib/utils/resource-utils.js +46 -34
  51. package/dist/lib/utils/response-utils.d.ts.map +1 -1
  52. package/dist/lib/utils/response-utils.js +2 -1
  53. package/dist/lib/utils/url-utils.d.ts +3 -0
  54. package/dist/lib/utils/url-utils.d.ts.map +1 -0
  55. package/dist/lib/utils/url-utils.js +14 -0
  56. package/dist/null-worker.js +1 -1
  57. package/package.json +4 -4
  58. package/src/lib/api/parse-in-batches.ts +8 -9
  59. package/src/lib/api/parse-sync.ts +8 -4
  60. package/src/lib/api/parse.ts +3 -3
  61. package/src/lib/api/select-loader.ts +7 -4
  62. package/src/lib/loader-utils/loader-context.ts +19 -8
  63. package/src/lib/utils/resource-utils.ts +54 -34
  64. package/src/lib/utils/response-utils.ts +3 -2
  65. package/src/lib/utils/url-utils.ts +12 -0
@@ -7,6 +7,7 @@ const log_1 = require("../utils/log");
7
7
  const resource_utils_1 = require("../utils/resource-utils");
8
8
  const register_loaders_1 = require("./register-loaders");
9
9
  const is_type_1 = require("../../javascript-utils/is-type");
10
+ const url_utils_1 = require("../utils/url-utils");
10
11
  const EXT_PATTERN = /\.([^.]+)$/;
11
12
  // TODO - Need a variant that peeks at streams for parseInBatches
12
13
  // TODO - Detect multiple matching loaders? Use heuristics to grade matches?
@@ -85,8 +86,9 @@ exports.selectLoaderSync = selectLoaderSync;
85
86
  /** Implements loaders selection logic */
86
87
  // eslint-disable-next-line complexity
87
88
  function selectLoaderInternal(data, loaders, options, context) {
88
- const { url, type } = (0, resource_utils_1.getResourceUrlAndType)(data);
89
- const testUrl = url || context?.url;
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;
90
92
  let loader = null;
91
93
  let reason = '';
92
94
  // if options.mimeType is supplied, it takes precedence
@@ -124,7 +126,8 @@ function validHTTPResponse(data) {
124
126
  }
125
127
  /** Generate a helpful message to help explain why loader selection failed. */
126
128
  function getNoValidLoaderMessage(data) {
127
- const { url, type } = (0, resource_utils_1.getResourceUrlAndType)(data);
129
+ const url = (0, resource_utils_1.getResourceUrl)(data);
130
+ const type = (0, resource_utils_1.getResourceMIMEType)(data);
128
131
  let message = 'No valid loader found (';
129
132
  message += url ? `${loader_utils_1.path.filename(url)}, ` : 'no url provided, ';
130
133
  message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
@@ -8,6 +8,6 @@ import type { Loader, LoaderOptions, LoaderContext } from '@loaders.gl/loader-ut
8
8
  * @param options
9
9
  * @param previousContext
10
10
  */
11
- export declare function getLoaderContext(context: Omit<LoaderContext, 'fetch'> & Partial<Pick<LoaderContext, 'fetch'>>, options?: LoaderOptions, previousContext?: LoaderContext | null): LoaderContext;
11
+ export declare function getLoaderContext(context: Omit<LoaderContext, 'fetch'> & Partial<Pick<LoaderContext, 'fetch'>>, options: LoaderOptions, parentContext: LoaderContext | null): LoaderContext;
12
12
  export declare function getLoadersFromContext(loaders: Loader[] | Loader | undefined, context?: LoaderContext): any;
13
13
  //# sourceMappingURL=loader-context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"loader-context.d.ts","sourceRoot":"","sources":["../../../src/lib/loader-utils/loader-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAGnF;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC,EAC7E,OAAO,CAAC,EAAE,aAAa,EACvB,eAAe,GAAE,aAAa,GAAG,IAAW,GAC3C,aAAa,CAkBf;AAGD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,EACtC,OAAO,CAAC,EAAE,aAAa,OAkBxB"}
1
+ {"version":3,"file":"loader-context.d.ts","sourceRoot":"","sources":["../../../src/lib/loader-utils/loader-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAKnF;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC,EAC7E,OAAO,EAAE,aAAa,EACtB,aAAa,EAAE,aAAa,GAAG,IAAI,GAClC,aAAa,CA2Bf;AAGD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,EACtC,OAAO,CAAC,EAAE,aAAa,OAkBxB"}
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLoadersFromContext = exports.getLoaderContext = void 0;
4
4
  const get_fetch_function_1 = require("./get-fetch-function");
5
+ const url_utils_1 = require("../utils/url-utils");
6
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
7
  /**
6
8
  * "sub" loaders invoked by other loaders get a "context" injected on `this`
7
9
  * The context will inject core methods like `parse` and contain information
@@ -11,21 +13,29 @@ const get_fetch_function_1 = require("./get-fetch-function");
11
13
  * @param options
12
14
  * @param previousContext
13
15
  */
14
- function getLoaderContext(context, options, previousContext = null) {
16
+ function getLoaderContext(context, options, parentContext) {
15
17
  // For recursive calls, we already have a context
16
18
  // TODO - add any additional loaders to context?
17
- if (previousContext) {
18
- return previousContext;
19
+ if (parentContext) {
20
+ return parentContext;
19
21
  }
20
- const resolvedContext = {
22
+ const newContext = {
21
23
  fetch: (0, get_fetch_function_1.getFetchFunction)(options, context),
22
24
  ...context
23
25
  };
26
+ // Parse URLs so that subloaders can easily generate correct strings
27
+ if (newContext.url) {
28
+ const baseUrl = (0, url_utils_1.stripQueryString)(newContext.url);
29
+ newContext.baseUrl = baseUrl;
30
+ newContext.queryString = (0, url_utils_1.extractQueryString)(newContext.url);
31
+ newContext.filename = loader_utils_1.path.filename(baseUrl);
32
+ newContext.baseUrl = loader_utils_1.path.dirname(baseUrl);
33
+ }
24
34
  // Recursive loading does not use single loader
25
- if (!Array.isArray(resolvedContext.loaders)) {
26
- resolvedContext.loaders = null;
35
+ if (!Array.isArray(newContext.loaders)) {
36
+ newContext.loaders = null;
27
37
  }
28
- return resolvedContext;
38
+ return newContext;
29
39
  }
30
40
  exports.getLoaderContext = getLoaderContext;
31
41
  // eslint-disable-next-line complexity
@@ -1,15 +1,24 @@
1
1
  /**
2
- * Returns an object with `url` and (MIME) `type` fields
3
- * If it cannot determine url or type, the corresponding value will be an empty string
2
+ * A loadable resource. Includes:
3
+ * `Response`, `Blob` (`File` is a subclass), string URLs and data URLs
4
+ */
5
+ export type Resource = Response | Blob | string;
6
+ /**
7
+ * Returns the URL associated with this resource.
8
+ * The returned value may include a query string and need further processing.
9
+ * If it cannot determine url, the corresponding value will be an empty string
4
10
  *
5
- * @param resource Any type, but only Responses, string URLs and data URLs are processed
11
+ * @todo string parameters are assumed to be URLs
12
+ */
13
+ export declare function getResourceUrl(resource: unknown): string;
14
+ /**
15
+ * Returns the URL associated with this resource.
16
+ * The returned value may include a query string and need further processing.
17
+ * If it cannot determine url, the corresponding value will be an empty string
6
18
  *
7
19
  * @todo string parameters are assumed to be URLs
8
20
  */
9
- export declare function getResourceUrlAndType(resource: any): {
10
- url: string;
11
- type: string;
12
- };
21
+ export declare function getResourceMIMEType(resource: unknown): string;
13
22
  /**
14
23
  * Returns (approximate) content length for a resource if it can be determined.
15
24
  * Returns -1 if content length cannot be determined.
@@ -17,5 +26,5 @@ export declare function getResourceUrlAndType(resource: any): {
17
26
 
18
27
  * @note string parameters are NOT assumed to be URLs
19
28
  */
20
- export declare function getResourceContentLength(resource: any): number;
29
+ export declare function getResourceContentLength(resource: unknown): number;
21
30
  //# sourceMappingURL=resource-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resource-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/resource-utils.ts"],"names":[],"mappings":"AAKA;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,GAAG,GAAG;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAC,CAmChF;AAED;;;;;;IAMI;AACJ,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,GAAG,GAAG,MAAM,CAkB9D"}
1
+ {"version":3,"file":"resource-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/resource-utils.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,IAAI,GAAG,MAAM,CAAC;AAEhD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAqBxD;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAqB7D;AAED;;;;;;IAMI;AACJ,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAoBlE"}
@@ -1,51 +1,64 @@
1
1
  "use strict";
2
+ // loaders.gl, MIT license
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getResourceContentLength = exports.getResourceUrlAndType = void 0;
4
+ exports.getResourceContentLength = exports.getResourceMIMEType = exports.getResourceUrl = void 0;
4
5
  const is_type_1 = require("../../javascript-utils/is-type");
5
6
  const mime_type_utils_1 = require("./mime-type-utils");
6
- const QUERY_STRING_PATTERN = /\?.*/;
7
+ const url_utils_1 = require("./url-utils");
7
8
  /**
8
- * Returns an object with `url` and (MIME) `type` fields
9
- * If it cannot determine url or type, the corresponding value will be an empty string
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
10
12
  *
11
- * @param resource Any type, but only Responses, string URLs and data URLs are processed
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
12
39
  *
13
40
  * @todo string parameters are assumed to be URLs
14
41
  */
15
- function getResourceUrlAndType(resource) {
42
+ function getResourceMIMEType(resource) {
16
43
  // If resource is a response, it contains the information directly
17
44
  if ((0, is_type_1.isResponse)(resource)) {
18
- const url = stripQueryString(resource.url || '');
19
- const contentTypeHeader = resource.headers.get('content-type') || '';
20
- return {
21
- url,
22
- type: (0, mime_type_utils_1.parseMIMEType)(contentTypeHeader) || (0, mime_type_utils_1.parseMIMETypeFromURL)(url)
23
- };
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);
24
49
  }
25
50
  // If the resource is a Blob or a File (subclass of Blob)
26
51
  if ((0, is_type_1.isBlob)(resource)) {
27
- return {
28
- // File objects have a "name" property. Blob objects don't have any
29
- // url (name) information
30
- url: stripQueryString(resource.name || ''),
31
- type: resource.type || ''
32
- };
52
+ const blob = resource;
53
+ return blob.type || '';
33
54
  }
34
55
  if (typeof resource === 'string') {
35
- return {
36
- // TODO this could mess up data URL but it doesn't matter as it is just used for inference
37
- url: stripQueryString(resource),
38
- // If a data url
39
- type: (0, mime_type_utils_1.parseMIMETypeFromURL)(resource)
40
- };
56
+ return (0, mime_type_utils_1.parseMIMETypeFromURL)(resource);
41
57
  }
42
58
  // Unknown
43
- return {
44
- url: '',
45
- type: ''
46
- };
59
+ return '';
47
60
  }
48
- exports.getResourceUrlAndType = getResourceUrlAndType;
61
+ exports.getResourceMIMEType = getResourceMIMEType;
49
62
  /**
50
63
  * Returns (approximate) content length for a resource if it can be determined.
51
64
  * Returns -1 if content length cannot be determined.
@@ -55,10 +68,12 @@ exports.getResourceUrlAndType = getResourceUrlAndType;
55
68
  */
56
69
  function getResourceContentLength(resource) {
57
70
  if ((0, is_type_1.isResponse)(resource)) {
58
- return resource.headers['content-length'] || -1;
71
+ const response = resource;
72
+ return response.headers['content-length'] || -1;
59
73
  }
60
74
  if ((0, is_type_1.isBlob)(resource)) {
61
- return resource.size;
75
+ const blob = resource;
76
+ return blob.size;
62
77
  }
63
78
  if (typeof resource === 'string') {
64
79
  // TODO - handle data URL?
@@ -73,6 +88,3 @@ function getResourceContentLength(resource) {
73
88
  return -1;
74
89
  }
75
90
  exports.getResourceContentLength = getResourceContentLength;
76
- function stripQueryString(url) {
77
- return url.replace(QUERY_STRING_PATTERN, '');
78
- }
@@ -1 +1 @@
1
- {"version":3,"file":"response-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/response-utils.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,QAAQ,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsCnE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAKrE;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAM1D"}
1
+ {"version":3,"file":"response-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/response-utils.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,QAAQ,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAuCnE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAKrE;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAM1D"}
@@ -21,7 +21,8 @@ async function makeResponse(resource) {
21
21
  }
22
22
  // `new Response(File)` does not preserve content-type and URL
23
23
  // so we add them here
24
- const { url, type } = (0, resource_utils_1.getResourceUrlAndType)(resource);
24
+ const url = (0, resource_utils_1.getResourceUrl)(resource);
25
+ const type = (0, resource_utils_1.getResourceMIMEType)(resource);
25
26
  if (type) {
26
27
  headers['content-type'] = type;
27
28
  }
@@ -0,0 +1,3 @@
1
+ export declare function extractQueryString(url: any): string;
2
+ export declare function stripQueryString(url: any): string;
3
+ //# sourceMappingURL=url-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/url-utils.ts"],"names":[],"mappings":"AAIA,wBAAgB,kBAAkB,CAAC,GAAG,KAAA,GAAG,MAAM,CAG9C;AAED,wBAAgB,gBAAgB,CAAC,GAAG,KAAA,GAAG,MAAM,CAE5C"}
@@ -0,0 +1,14 @@
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;
@@ -184,7 +184,7 @@
184
184
  }
185
185
 
186
186
  // src/null-loader.ts
187
- var VERSION = true ? "3.4.0-alpha.4" : "latest";
187
+ var VERSION = true ? "3.4.0-alpha.5" : "latest";
188
188
  function parseSync(arrayBuffer, options, context) {
189
189
  if (!options.null.echoParameters)
190
190
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/core",
3
- "version": "3.4.0-alpha.4",
3
+ "version": "3.4.0-alpha.5",
4
4
  "description": "The core API for working with loaders.gl loaders and writers",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -42,9 +42,9 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@babel/runtime": "^7.3.1",
45
- "@loaders.gl/loader-utils": "3.4.0-alpha.4",
46
- "@loaders.gl/worker-utils": "3.4.0-alpha.4",
45
+ "@loaders.gl/loader-utils": "3.4.0-alpha.5",
46
+ "@loaders.gl/worker-utils": "3.4.0-alpha.5",
47
47
  "@probe.gl/log": "^4.0.1"
48
48
  },
49
- "gitHead": "f039a523df8b908a91f26c5ba8c4cfc1924b6140"
49
+ "gitHead": "686929f85b3420c15df051eab769942b260dd242"
50
50
  }
@@ -11,7 +11,7 @@ import {isLoaderObject} from '../loader-utils/normalize-loader';
11
11
  import {normalizeOptions} from '../loader-utils/option-utils';
12
12
  import {getLoaderContext} from '../loader-utils/loader-context';
13
13
  import {getAsyncIterableFromData} from '../loader-utils/get-data';
14
- import {getResourceUrlAndType} from '../utils/resource-utils';
14
+ import {getResourceUrl} from '../utils/resource-utils';
15
15
  import {selectLoader} from './select-loader';
16
16
 
17
17
  // Ensure `parse` is available in context if loader falls back to `parse`
@@ -32,6 +32,8 @@ export async function parseInBatches(
32
32
  ): Promise<AsyncIterable<any>> {
33
33
  assert(!context || typeof context === 'object'); // parseInBatches no longer accepts final url
34
34
 
35
+ const loaderArray = Array.isArray(loaders) ? loaders : undefined;
36
+
35
37
  // Signature: parseInBatches(data, options, url) - Uses registered loaders
36
38
  if (!Array.isArray(loaders) && !isLoaderObject(loaders)) {
37
39
  context = undefined; // context not supported in short signature
@@ -43,11 +45,11 @@ export async function parseInBatches(
43
45
  options = options || {};
44
46
 
45
47
  // Extract a url for auto detection
46
- const {url} = getResourceUrlAndType(data);
48
+ const url = getResourceUrl(data);
47
49
 
48
50
  // Chooses a loader and normalizes it
49
51
  // Note - only uses URL and contentType for streams and iterator inputs
50
- const loader = await selectLoader(data as ArrayBuffer, loaders as Loader[], options);
52
+ const loader = await selectLoader(data as ArrayBuffer, loaders as Loader | Loader[], options);
51
53
  // Note: if options.nothrow was set, it is possible that no loader was found, if so just return null
52
54
  if (!loader) {
53
55
  // @ts-ignore
@@ -55,14 +57,11 @@ export async function parseInBatches(
55
57
  }
56
58
 
57
59
  // Normalize options
58
- // @ts-ignore
59
- options = normalizeOptions(options, loader, loaders, url);
60
- // @ts-ignore
60
+ options = normalizeOptions(options, loader, loaderArray, url);
61
61
  context = getLoaderContext(
62
- // @ts-ignore
63
- {url, parseInBatches, parse, loaders: loaders as Loader[]},
62
+ {url, parseInBatches, parse, loaders: loaderArray},
64
63
  options,
65
- context
64
+ context || null
66
65
  );
67
66
 
68
67
  return await parseWithLoaderInBatches(loader as LoaderWithParser, data, options, context);
@@ -11,7 +11,7 @@ import {isLoaderObject} from '../loader-utils/normalize-loader';
11
11
  import {normalizeOptions} from '../loader-utils/option-utils';
12
12
  import {getArrayBufferOrStringFromDataSync} from '../loader-utils/get-data';
13
13
  import {getLoaderContext, getLoadersFromContext} from '../loader-utils/loader-context';
14
- import {getResourceUrlAndType} from '../utils/resource-utils';
14
+ import {getResourceUrl} from '../utils/resource-utils';
15
15
 
16
16
  /**
17
17
  * Parses `data` synchronously using a specified loader
@@ -52,12 +52,16 @@ export function parseSync(
52
52
  options = normalizeOptions(options, loader, candidateLoaders);
53
53
 
54
54
  // Extract a url for auto detection
55
- const {url} = getResourceUrlAndType(data);
55
+ const url = getResourceUrl(data);
56
56
 
57
57
  const parse = () => {
58
- throw new Error('parseSync called parse');
58
+ throw new Error('parseSync called parse (which is async');
59
59
  };
60
- context = getLoaderContext({url, parseSync, parse, loaders: loaders as Loader[]}, options);
60
+ context = getLoaderContext(
61
+ {url, parseSync, parse, loaders: loaders as Loader[]},
62
+ options,
63
+ context || null
64
+ );
61
65
 
62
66
  return parseWithLoaderSync(loader as LoaderWithParser, data, options, context);
63
67
  }
@@ -6,7 +6,7 @@ import {isResponse} from '../../javascript-utils/is-type';
6
6
  import {normalizeOptions} from '../loader-utils/option-utils';
7
7
  import {getArrayBufferOrStringFromData} from '../loader-utils/get-data';
8
8
  import {getLoaderContext, getLoadersFromContext} from '../loader-utils/loader-context';
9
- import {getResourceUrlAndType} from '../utils/resource-utils';
9
+ import {getResourceUrl} from '../utils/resource-utils';
10
10
  import {selectLoader} from './select-loader';
11
11
 
12
12
  /**
@@ -36,7 +36,7 @@ export async function parse(
36
36
  options = options || {};
37
37
 
38
38
  // Extract a url for auto detection
39
- const {url} = getResourceUrlAndType(data);
39
+ const url = getResourceUrl(data);
40
40
 
41
41
  // Chooses a loader (and normalizes it)
42
42
  // Also use any loaders in the context, new loaders take priority
@@ -53,7 +53,7 @@ export async function parse(
53
53
  options = normalizeOptions(options, loader, candidateLoaders, url);
54
54
 
55
55
  // Get a context (if already present, will be unchanged)
56
- context = getLoaderContext({url, parse, loaders: candidateLoaders}, options, context);
56
+ context = getLoaderContext({url, parse, loaders: candidateLoaders}, options, context || null);
57
57
 
58
58
  return await parseWithLoader(loader, data, options, context);
59
59
  }
@@ -2,9 +2,10 @@ import type {LoaderContext, LoaderOptions, Loader} from '@loaders.gl/loader-util
2
2
  import {compareArrayBuffers, path} from '@loaders.gl/loader-utils';
3
3
  import {normalizeLoader} from '../loader-utils/normalize-loader';
4
4
  import {log} from '../utils/log';
5
- import {getResourceUrlAndType} from '../utils/resource-utils';
5
+ import {getResourceUrl, getResourceMIMEType} from '../utils/resource-utils';
6
6
  import {getRegisteredLoaders} from './register-loaders';
7
7
  import {isBlob} from '../../javascript-utils/is-type';
8
+ import {stripQueryString} from '../utils/url-utils';
8
9
 
9
10
  const EXT_PATTERN = /\.([^.]+)$/;
10
11
 
@@ -111,9 +112,10 @@ function selectLoaderInternal(
111
112
  options?: LoaderOptions,
112
113
  context?: LoaderContext
113
114
  ) {
114
- const {url, type} = getResourceUrlAndType(data);
115
+ const url = getResourceUrl(data);
116
+ const type = getResourceMIMEType(data);
115
117
 
116
- const testUrl = url || context?.url;
118
+ const testUrl = stripQueryString(url) || context?.url;
117
119
 
118
120
  let loader: Loader | null = null;
119
121
  let reason: string = '';
@@ -161,7 +163,8 @@ function validHTTPResponse(data: any): boolean {
161
163
 
162
164
  /** Generate a helpful message to help explain why loader selection failed. */
163
165
  function getNoValidLoaderMessage(data): string {
164
- const {url, type} = getResourceUrlAndType(data);
166
+ const url = getResourceUrl(data);
167
+ const type = getResourceMIMEType(data);
165
168
 
166
169
  let message = 'No valid loader found (';
167
170
  message += url ? `${path.filename(url)}, ` : 'no url provided, ';
@@ -1,5 +1,7 @@
1
1
  import type {Loader, LoaderOptions, LoaderContext} from '@loaders.gl/loader-utils';
2
2
  import {getFetchFunction} from './get-fetch-function';
3
+ import {extractQueryString, stripQueryString} from '../utils/url-utils';
4
+ import {path} from '@loaders.gl/loader-utils';
3
5
 
4
6
  /**
5
7
  * "sub" loaders invoked by other loaders get a "context" injected on `this`
@@ -12,26 +14,35 @@ import {getFetchFunction} from './get-fetch-function';
12
14
  */
13
15
  export function getLoaderContext(
14
16
  context: Omit<LoaderContext, 'fetch'> & Partial<Pick<LoaderContext, 'fetch'>>,
15
- options?: LoaderOptions,
16
- previousContext: LoaderContext | null = null
17
+ options: LoaderOptions,
18
+ parentContext: LoaderContext | null
17
19
  ): LoaderContext {
18
20
  // For recursive calls, we already have a context
19
21
  // TODO - add any additional loaders to context?
20
- if (previousContext) {
21
- return previousContext;
22
+ if (parentContext) {
23
+ return parentContext;
22
24
  }
23
25
 
24
- const resolvedContext: LoaderContext = {
26
+ const newContext: LoaderContext = {
25
27
  fetch: getFetchFunction(options, context),
26
28
  ...context
27
29
  };
28
30
 
31
+ // Parse URLs so that subloaders can easily generate correct strings
32
+ if (newContext.url) {
33
+ const baseUrl = stripQueryString(newContext.url);
34
+ newContext.baseUrl = baseUrl;
35
+ newContext.queryString = extractQueryString(newContext.url);
36
+ newContext.filename = path.filename(baseUrl);
37
+ newContext.baseUrl = path.dirname(baseUrl);
38
+ }
39
+
29
40
  // Recursive loading does not use single loader
30
- if (!Array.isArray(resolvedContext.loaders)) {
31
- resolvedContext.loaders = null;
41
+ if (!Array.isArray(newContext.loaders)) {
42
+ newContext.loaders = null;
32
43
  }
33
44
 
34
- return resolvedContext;
45
+ return newContext;
35
46
  }
36
47
 
37
48
  // eslint-disable-next-line complexity
@@ -1,51 +1,73 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import {isResponse, isBlob} from '../../javascript-utils/is-type';
2
4
  import {parseMIMEType, parseMIMETypeFromURL} from './mime-type-utils';
5
+ import {stripQueryString} from './url-utils';
3
6
 
4
- const QUERY_STRING_PATTERN = /\?.*/;
7
+ /**
8
+ * A loadable resource. Includes:
9
+ * `Response`, `Blob` (`File` is a subclass), string URLs and data URLs
10
+ */
11
+ export type Resource = Response | Blob | string;
5
12
 
6
13
  /**
7
- * Returns an object with `url` and (MIME) `type` fields
8
- * If it cannot determine url or type, the corresponding value will be an empty string
14
+ * Returns the URL associated with this resource.
15
+ * The returned value may include a query string and need further processing.
16
+ * If it cannot determine url, the corresponding value will be an empty string
9
17
  *
10
- * @param resource Any type, but only Responses, string URLs and data URLs are processed
18
+ * @todo string parameters are assumed to be URLs
19
+ */
20
+ export function getResourceUrl(resource: unknown): string {
21
+ // If resource is a `Response`, it contains the information directly as a field
22
+ if (isResponse(resource)) {
23
+ const response = resource as Response;
24
+ return response.url;
25
+ }
26
+
27
+ // If the resource is a Blob or a File (subclass of Blob)
28
+ if (isBlob(resource)) {
29
+ const blob = resource as Blob;
30
+ // File objects have a "name" property. Blob objects don't have any
31
+ // url (name) information
32
+ return blob.name || '';
33
+ }
34
+
35
+ if (typeof resource === 'string') {
36
+ return resource;
37
+ }
38
+
39
+ // Unknown
40
+ return '';
41
+ }
42
+
43
+ /**
44
+ * Returns the URL associated with this resource.
45
+ * The returned value may include a query string and need further processing.
46
+ * If it cannot determine url, the corresponding value will be an empty string
11
47
  *
12
48
  * @todo string parameters are assumed to be URLs
13
49
  */
14
- export function getResourceUrlAndType(resource: any): {url: string; type: string} {
50
+ export function getResourceMIMEType(resource: unknown): string {
15
51
  // If resource is a response, it contains the information directly
16
52
  if (isResponse(resource)) {
17
- const url = stripQueryString(resource.url || '');
18
- const contentTypeHeader = resource.headers.get('content-type') || '';
19
- return {
20
- url,
21
- type: parseMIMEType(contentTypeHeader) || parseMIMETypeFromURL(url)
22
- };
53
+ const response = resource as Response;
54
+ const contentTypeHeader = response.headers.get('content-type') || '';
55
+ const noQueryUrl = stripQueryString(response.url);
56
+ return parseMIMEType(contentTypeHeader) || parseMIMETypeFromURL(noQueryUrl);
23
57
  }
24
58
 
25
59
  // If the resource is a Blob or a File (subclass of Blob)
26
60
  if (isBlob(resource)) {
27
- return {
28
- // File objects have a "name" property. Blob objects don't have any
29
- // url (name) information
30
- url: stripQueryString(resource.name || ''),
31
- type: resource.type || ''
32
- };
61
+ const blob = resource as Blob;
62
+ return blob.type || '';
33
63
  }
34
64
 
35
65
  if (typeof resource === 'string') {
36
- return {
37
- // TODO this could mess up data URL but it doesn't matter as it is just used for inference
38
- url: stripQueryString(resource),
39
- // If a data url
40
- type: parseMIMETypeFromURL(resource)
41
- };
66
+ return parseMIMETypeFromURL(resource);
42
67
  }
43
68
 
44
69
  // Unknown
45
- return {
46
- url: '',
47
- type: ''
48
- };
70
+ return '';
49
71
  }
50
72
 
51
73
  /**
@@ -55,12 +77,14 @@ export function getResourceUrlAndType(resource: any): {url: string; type: string
55
77
 
56
78
  * @note string parameters are NOT assumed to be URLs
57
79
  */
58
- export function getResourceContentLength(resource: any): number {
80
+ export function getResourceContentLength(resource: unknown): number {
59
81
  if (isResponse(resource)) {
60
- return resource.headers['content-length'] || -1;
82
+ const response = resource as Response;
83
+ return response.headers['content-length'] || -1;
61
84
  }
62
85
  if (isBlob(resource)) {
63
- return resource.size;
86
+ const blob = resource as Blob;
87
+ return blob.size;
64
88
  }
65
89
  if (typeof resource === 'string') {
66
90
  // TODO - handle data URL?
@@ -74,7 +98,3 @@ export function getResourceContentLength(resource: any): number {
74
98
  }
75
99
  return -1;
76
100
  }
77
-
78
- function stripQueryString(url) {
79
- return url.replace(QUERY_STRING_PATTERN, '');
80
- }