@loaders.gl/polyfills 4.0.0-alpha.5 → 4.0.0-alpha.7

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 (170) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dist.min.js +65 -2188
  3. package/dist/es5/bundle.js +6 -0
  4. package/dist/es5/bundle.js.map +1 -0
  5. package/dist/es5/index.js +99 -0
  6. package/dist/es5/index.js.map +1 -0
  7. package/dist/es5/lib/encoding-indexes.js +37 -0
  8. package/dist/es5/lib/encoding-indexes.js.map +1 -0
  9. package/dist/es5/lib/encoding.js +1214 -0
  10. package/dist/es5/lib/encoding.js.map +1 -0
  11. package/dist/es5/libs/encoding-indexes-asian.js +13 -0
  12. package/dist/es5/node/buffer/btoa.node.js +14 -0
  13. package/dist/es5/node/buffer/btoa.node.js.map +1 -0
  14. package/dist/es5/node/buffer/to-array-buffer.node.js +14 -0
  15. package/dist/es5/node/buffer/to-array-buffer.node.js.map +1 -0
  16. package/dist/es5/node/fetch/fetch-file.node.js +83 -0
  17. package/dist/es5/node/fetch/fetch-file.node.js.map +1 -0
  18. package/dist/es5/node/fetch/fetch.node.js +194 -0
  19. package/dist/es5/node/fetch/fetch.node.js.map +1 -0
  20. package/dist/es5/node/fetch/headers.node.js +151 -0
  21. package/dist/es5/node/fetch/headers.node.js.map +1 -0
  22. package/dist/es5/node/fetch/response.node.js +182 -0
  23. package/dist/es5/node/fetch/response.node.js.map +1 -0
  24. package/dist/es5/node/fetch/utils/decode-data-uri.node.js +58 -0
  25. package/dist/es5/node/fetch/utils/decode-data-uri.node.js.map +1 -0
  26. package/dist/es5/node/fetch/utils/stream-utils.node.js +92 -0
  27. package/dist/es5/node/fetch/utils/stream-utils.node.js.map +1 -0
  28. package/dist/es5/node/file/blob-stream-controller.js +90 -0
  29. package/dist/es5/node/file/blob-stream-controller.js.map +1 -0
  30. package/dist/es5/node/file/blob-stream.js +64 -0
  31. package/dist/es5/node/file/blob-stream.js.map +1 -0
  32. package/dist/es5/node/file/blob.js +212 -0
  33. package/dist/es5/node/file/blob.js.map +1 -0
  34. package/dist/es5/node/file/file-reader.js +153 -0
  35. package/dist/es5/node/file/file-reader.js.map +1 -0
  36. package/dist/es5/node/file/file.js +44 -0
  37. package/dist/es5/node/file/file.js.map +1 -0
  38. package/dist/es5/node/file/install-file-polyfills.js +25 -0
  39. package/dist/es5/node/file/install-file-polyfills.js.map +1 -0
  40. package/dist/es5/node/file/readable-stream.js +27 -0
  41. package/dist/es5/node/file/readable-stream.js.map +1 -0
  42. package/dist/es5/node/images/encode-image.node.js +30 -0
  43. package/dist/es5/node/images/encode-image.node.js.map +1 -0
  44. package/dist/es5/node/images/parse-image.node.js +64 -0
  45. package/dist/es5/node/images/parse-image.node.js.map +1 -0
  46. package/dist/es5/promise/all-settled.js +28 -0
  47. package/dist/es5/promise/all-settled.js.map +1 -0
  48. package/dist/es5/utils/assert.js +12 -0
  49. package/dist/es5/utils/assert.js.map +1 -0
  50. package/dist/es5/utils/globals.js +18 -0
  51. package/dist/es5/utils/globals.js.map +1 -0
  52. package/dist/esm/bundle.js +4 -0
  53. package/dist/esm/bundle.js.map +1 -0
  54. package/dist/esm/index.js +50 -0
  55. package/dist/esm/index.js.map +1 -0
  56. package/dist/esm/lib/encoding-indexes.js +30 -0
  57. package/dist/esm/lib/encoding-indexes.js.map +1 -0
  58. package/dist/esm/lib/encoding.js +1206 -0
  59. package/dist/esm/lib/encoding.js.map +1 -0
  60. package/dist/esm/libs/encoding-indexes-asian.js +13 -0
  61. package/dist/esm/node/buffer/btoa.node.js +7 -0
  62. package/dist/esm/node/buffer/btoa.node.js.map +1 -0
  63. package/dist/esm/node/buffer/to-array-buffer.node.js +8 -0
  64. package/dist/esm/node/buffer/to-array-buffer.node.js.map +1 -0
  65. package/dist/esm/node/fetch/fetch-file.node.js +50 -0
  66. package/dist/esm/node/fetch/fetch-file.node.js.map +1 -0
  67. package/dist/esm/node/fetch/fetch.node.js +126 -0
  68. package/dist/esm/node/fetch/fetch.node.js.map +1 -0
  69. package/dist/esm/node/fetch/headers.node.js +102 -0
  70. package/dist/esm/node/fetch/headers.node.js.map +1 -0
  71. package/dist/esm/node/fetch/response.node.js +67 -0
  72. package/dist/esm/node/fetch/response.node.js.map +1 -0
  73. package/dist/esm/node/fetch/utils/decode-data-uri.node.js +45 -0
  74. package/dist/esm/node/fetch/utils/decode-data-uri.node.js.map +1 -0
  75. package/dist/esm/node/fetch/utils/stream-utils.node.js +43 -0
  76. package/dist/esm/node/fetch/utils/stream-utils.node.js.map +1 -0
  77. package/dist/esm/node/file/blob-stream-controller.js +44 -0
  78. package/dist/esm/node/file/blob-stream-controller.js.map +1 -0
  79. package/dist/esm/node/file/blob-stream.js +20 -0
  80. package/dist/esm/node/file/blob-stream.js.map +1 -0
  81. package/dist/esm/node/file/blob.js +120 -0
  82. package/dist/esm/node/file/blob.js.map +1 -0
  83. package/dist/esm/node/file/file-reader.js +60 -0
  84. package/dist/esm/node/file/file-reader.js.map +1 -0
  85. package/dist/esm/node/file/file.js +19 -0
  86. package/dist/esm/node/file/file.js.map +1 -0
  87. package/dist/esm/node/file/install-file-polyfills.js +19 -0
  88. package/dist/esm/node/file/install-file-polyfills.js.map +1 -0
  89. package/dist/esm/node/file/readable-stream.js +4 -0
  90. package/dist/esm/node/file/readable-stream.js.map +1 -0
  91. package/dist/esm/node/images/encode-image.node.js +20 -0
  92. package/dist/esm/node/images/encode-image.node.js.map +1 -0
  93. package/dist/esm/node/images/parse-image.node.js +29 -0
  94. package/dist/esm/node/images/parse-image.node.js.map +1 -0
  95. package/dist/esm/promise/all-settled.js +19 -0
  96. package/dist/esm/promise/all-settled.js.map +1 -0
  97. package/dist/esm/utils/assert.js +6 -0
  98. package/dist/esm/utils/assert.js.map +1 -0
  99. package/dist/esm/utils/globals.js +9 -0
  100. package/dist/esm/utils/globals.js.map +1 -0
  101. package/dist/index.d.ts +2 -0
  102. package/dist/index.d.ts.map +1 -1
  103. package/dist/index.js +84 -50
  104. package/dist/lib/encoding-indexes.js +34 -29
  105. package/dist/lib/encoding.js +2610 -1281
  106. package/dist/libs/encoding-indexes-asian.d.ts +1 -1
  107. package/dist/libs/encoding-indexes-asian.js +9 -8
  108. package/dist/node/buffer/btoa.node.js +12 -5
  109. package/dist/node/buffer/to-array-buffer.node.js +11 -8
  110. package/dist/node/fetch/fetch-file.node.d.ts +4 -0
  111. package/dist/node/fetch/fetch-file.node.d.ts.map +1 -0
  112. package/dist/node/fetch/fetch-file.node.js +51 -0
  113. package/dist/node/fetch/fetch.node.d.ts +6 -1
  114. package/dist/node/fetch/fetch.node.d.ts.map +1 -1
  115. package/dist/node/fetch/fetch.node.js +128 -111
  116. package/dist/node/fetch/headers.node.d.ts +1 -1
  117. package/dist/node/fetch/headers.node.d.ts.map +1 -1
  118. package/dist/node/fetch/headers.node.js +95 -114
  119. package/dist/node/fetch/response.node.d.ts +2 -2
  120. package/dist/node/fetch/response.node.d.ts.map +1 -1
  121. package/dist/node/fetch/response.node.js +72 -84
  122. package/dist/node/fetch/utils/decode-data-uri.node.js +63 -53
  123. package/dist/node/fetch/utils/stream-utils.node.d.ts +8 -1
  124. package/dist/node/fetch/utils/stream-utils.node.d.ts.map +1 -1
  125. package/dist/node/fetch/utils/stream-utils.node.js +68 -93
  126. package/dist/node/file/blob-stream-controller.js +59 -52
  127. package/dist/node/file/blob-stream.js +36 -25
  128. package/dist/node/file/blob.js +151 -131
  129. package/dist/node/file/file-reader.js +28 -77
  130. package/dist/node/file/file.js +36 -25
  131. package/dist/node/file/install-file-polyfills.js +26 -21
  132. package/dist/node/file/readable-stream.js +10 -3
  133. package/dist/node/images/encode-image.node.js +38 -17
  134. package/dist/node/images/parse-image.node.d.ts +3 -1
  135. package/dist/node/images/parse-image.node.d.ts.map +1 -1
  136. package/dist/node/images/parse-image.node.js +40 -19
  137. package/dist/promise/all-settled.js +22 -17
  138. package/dist/utils/assert.js +8 -5
  139. package/dist/utils/globals.js +34 -7
  140. package/package.json +5 -5
  141. package/src/index.ts +7 -13
  142. package/src/node/fetch/fetch-file.node.ts +51 -0
  143. package/src/node/fetch/fetch.node.ts +64 -30
  144. package/src/node/fetch/headers.node.ts +1 -1
  145. package/src/node/fetch/response.node.ts +4 -2
  146. package/src/node/fetch/utils/stream-utils.node.ts +10 -58
  147. package/src/node/images/parse-image.node.ts +35 -20
  148. package/dist/bundle.js.map +0 -1
  149. package/dist/index.js.map +0 -1
  150. package/dist/lib/encoding-indexes.js.map +0 -1
  151. package/dist/lib/encoding.js.map +0 -1
  152. package/dist/node/buffer/btoa.node.js.map +0 -1
  153. package/dist/node/buffer/to-array-buffer.node.js.map +0 -1
  154. package/dist/node/fetch/fetch.node.js.map +0 -1
  155. package/dist/node/fetch/headers.node.js.map +0 -1
  156. package/dist/node/fetch/response.node.js.map +0 -1
  157. package/dist/node/fetch/utils/decode-data-uri.node.js.map +0 -1
  158. package/dist/node/fetch/utils/stream-utils.node.js.map +0 -1
  159. package/dist/node/file/blob-stream-controller.js.map +0 -1
  160. package/dist/node/file/blob-stream.js.map +0 -1
  161. package/dist/node/file/blob.js.map +0 -1
  162. package/dist/node/file/file-reader.js.map +0 -1
  163. package/dist/node/file/file.js.map +0 -1
  164. package/dist/node/file/install-file-polyfills.js.map +0 -1
  165. package/dist/node/file/readable-stream.js.map +0 -1
  166. package/dist/node/images/encode-image.node.js.map +0 -1
  167. package/dist/node/images/parse-image.node.js.map +0 -1
  168. package/dist/promise/all-settled.js.map +0 -1
  169. package/dist/utils/assert.js.map +0 -1
  170. package/dist/utils/globals.js.map +0 -1
@@ -1,20 +1,41 @@
1
- import getPixels from 'get-pixels';
2
- import { assert } from '../../utils/assert';
3
- import util from 'util';
4
- export async function parseImageNode(arrayBuffer, mimeType) {
5
- assert(mimeType, 'MIMEType is required to parse image under Node.js');
6
- const getPixelsAsync = util.promisify(getPixels);
7
- const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
8
- const ndarray = await getPixelsAsync(buffer, mimeType);
9
- const shape = [...ndarray.shape];
10
- const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
11
- return {
12
- shape,
13
- data: ndarray.data,
14
- width: ndarray.shape[0],
15
- height: ndarray.shape[1],
16
- components: ndarray.shape[2],
17
- layers
18
- };
1
+ "use strict";
2
+ // loaders.gl, MIT license
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.parseImageNode = exports.NODE_FORMAT_SUPPORT = void 0;
8
+ const get_pixels_1 = __importDefault(require("get-pixels"));
9
+ /** Declares which image format mime types this loader polyfill supports */
10
+ exports.NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
11
+ async function parseImageNode(arrayBuffer, mimeType) {
12
+ if (!mimeType) {
13
+ throw new Error('MIMEType is required to parse image under Node.js');
14
+ }
15
+ const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
16
+ const ndarray = await getPixelsAsync(buffer, mimeType);
17
+ return ndarray;
18
+ }
19
+ exports.parseImageNode = parseImageNode;
20
+ // TODO - check if getPixels callback is asynchronous if provided with buffer input
21
+ // if not, parseImage can be a sync function
22
+ function getPixelsAsync(buffer, mimeType) {
23
+ return new Promise((resolve) => (0, get_pixels_1.default)(buffer, mimeType, (err, ndarray) => {
24
+ if (err) {
25
+ throw err;
26
+ }
27
+ const shape = [...ndarray.shape];
28
+ const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
29
+ const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
30
+ // extract width/height etc
31
+ resolve({
32
+ shape,
33
+ data,
34
+ width: ndarray.shape[0],
35
+ height: ndarray.shape[1],
36
+ components: ndarray.shape[2],
37
+ // TODO - error
38
+ layers: layers ? [layers] : []
39
+ });
40
+ }));
19
41
  }
20
- //# sourceMappingURL=parse-image.node.js.map
@@ -1,19 +1,24 @@
1
- export const REJECTED_STATUS = 'rejected';
2
- export const FULFILLED_STATUS = 'fulfilled';
3
- export function allSettled(promises) {
4
- const mappedPromises = promises.map(promise => {
5
- return promise.then(value => {
6
- return {
7
- status: FULFILLED_STATUS,
8
- value
9
- };
10
- }).catch(reason => {
11
- return {
12
- status: REJECTED_STATUS,
13
- reason
14
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.allSettled = exports.FULFILLED_STATUS = exports.REJECTED_STATUS = void 0;
4
+ exports.REJECTED_STATUS = 'rejected';
5
+ exports.FULFILLED_STATUS = 'fulfilled';
6
+ /**
7
+ * Handle list of promises and return all values regardless of results.
8
+ * Polyfill for Promise.allSettled() method.
9
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
10
+ * @param promises
11
+ */
12
+ function allSettled(promises) {
13
+ const mappedPromises = promises.map((promise) => {
14
+ return promise
15
+ .then((value) => {
16
+ return { status: exports.FULFILLED_STATUS, value };
17
+ })
18
+ .catch((reason) => {
19
+ return { status: exports.REJECTED_STATUS, reason };
20
+ });
15
21
  });
16
- });
17
- return Promise.all(mappedPromises);
22
+ return Promise.all(mappedPromises);
18
23
  }
19
- //# sourceMappingURL=all-settled.js.map
24
+ exports.allSettled = allSettled;
@@ -1,6 +1,9 @@
1
- export function assert(condition, message) {
2
- if (!condition) {
3
- throw new Error("@loaders.gl/polyfills assertion ".concat(message));
4
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assert = void 0;
4
+ function assert(condition, message) {
5
+ if (!condition) {
6
+ throw new Error(`@loaders.gl/polyfills assertion ${message}`);
7
+ }
5
8
  }
6
- //# sourceMappingURL=assert.js.map
9
+ exports.assert = assert;
@@ -1,9 +1,36 @@
1
- const isBrowser = typeof process !== 'object' || String(process) !== '[object process]' || process.browser;
1
+ "use strict";
2
+ // Copyright (c) 2015 - 2017 Uber Technologies, Inc.
3
+ //
4
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ // of this software and associated documentation files (the "Software"), to deal
6
+ // in the Software without restriction, including without limitation the rights
7
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ // copies of the Software, and to permit persons to whom the Software is
9
+ // furnished to do so, subject to the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included in
12
+ // all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ // THE SOFTWARE.
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.global = exports.isBrowser = void 0;
23
+ // Purpose: include this in your module to avoids adding dependencies on
24
+ // micro modules like 'global' and 'is-browser';
25
+ /* eslint-disable no-restricted-globals */
26
+ const isBrowser =
27
+ // @ts-ignore process.browser
28
+ typeof process !== 'object' || String(process) !== '[object process]' || process.browser;
29
+ exports.isBrowser = isBrowser;
2
30
  const globals = {
3
- self: typeof self !== 'undefined' && self,
4
- window: typeof window !== 'undefined' && window,
5
- global: typeof global !== 'undefined' && global
31
+ self: typeof self !== 'undefined' && self,
32
+ window: typeof window !== 'undefined' && window,
33
+ global: typeof global !== 'undefined' && global
6
34
  };
7
- const global_ = globals.global || globals.self || globals.window;
8
- export { isBrowser, global_ as global };
9
- //# sourceMappingURL=globals.js.map
35
+ const global_ = (globals.global || globals.self || globals.window);
36
+ exports.global = global_;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/polyfills",
3
- "version": "4.0.0-alpha.5",
3
+ "version": "4.0.0-alpha.7",
4
4
  "description": "Polyfills for TextEncoder/TextDecoder",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -18,8 +18,8 @@
18
18
  "TextDecoder"
19
19
  ],
20
20
  "types": "dist/index.d.ts",
21
- "main": "dist/index.js",
22
- "module": "dist/index.js",
21
+ "main": "dist/es5/index.js",
22
+ "module": "dist/esm/index.js",
23
23
  "files": [
24
24
  "src",
25
25
  "dist",
@@ -97,7 +97,7 @@
97
97
  },
98
98
  "dependencies": {
99
99
  "@babel/runtime": "^7.3.1",
100
- "@xmldom/xmldom": "^0.7.5",
100
+ "buffer": "^6.0.3",
101
101
  "get-pixels": "^3.3.2",
102
102
  "ndarray": "^1.0.18",
103
103
  "save-pixels": "^2.3.2",
@@ -105,5 +105,5 @@
105
105
  "through": "^2.3.8",
106
106
  "web-streams-polyfill": "^3.0.0"
107
107
  },
108
- "gitHead": "7a71a54bdf1ddf985cc3af3db90b82e7fa97d025"
108
+ "gitHead": "afb59c4d8e5d8ebb9c28f111cb0c96c5527d0ffd"
109
109
  }
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /* eslint-disable dot-notation */
2
- import {DOMParser} from '@xmldom/xmldom';
3
2
  import {isBrowser, global} from './utils/globals';
4
3
 
5
4
  import {TextDecoder, TextEncoder} from './lib/encoding';
@@ -8,18 +7,20 @@ import {allSettled} from './promise/all-settled';
8
7
  // Node specific
9
8
  import * as base64 from './node/buffer/btoa.node';
10
9
 
11
- import HeadersNode from './node/fetch/headers.node';
12
- import ResponseNode from './node/fetch/response.node';
13
- import fetchNode from './node/fetch/fetch.node';
10
+ import {Headers as HeadersNode} from './node/fetch/headers.node';
11
+ import {Response as ResponseNode} from './node/fetch/response.node';
12
+ import {fetchNode as fetchNode} from './node/fetch/fetch.node';
14
13
 
15
14
  import {encodeImageNode} from './node/images/encode-image.node';
16
- import {parseImageNode} from './node/images/parse-image.node';
15
+ import {parseImageNode, NODE_FORMAT_SUPPORT} from './node/images/parse-image.node';
17
16
 
18
17
  export {ReadableStreamPolyfill} from './node/file/readable-stream';
19
18
  export {BlobPolyfill} from './node/file/blob';
20
19
  export {FileReaderPolyfill} from './node/file/file-reader';
21
20
  export {FilePolyfill} from './node/file/file';
22
21
  export {installFilePolyfills} from './node/file/install-file-polyfills';
22
+ export {fetchNode as _fetchNode} from './node/fetch/fetch.node';
23
+ export {fetchFileNode as _fetchFileNode} from './node/fetch/fetch-file.node';
23
24
 
24
25
  // POLYFILLS: TextEncoder, TextDecoder
25
26
  // - Recent Node versions have these classes but virtually no encodings unless special build.
@@ -62,14 +63,6 @@ if (!isBrowser && !('fetch' in global) && fetchNode) {
62
63
  global['fetch'] = fetchNode;
63
64
  }
64
65
 
65
- // POLYFILL: DOMParser
66
- // - Node: Yes
67
- // - Browser: No
68
-
69
- if (!isBrowser && !('DOMParser' in global) && DOMParser) {
70
- global['DOMParser'] = DOMParser;
71
- }
72
-
73
66
  // NODE IMAGE FUNCTIONS:
74
67
  // These are not official polyfills but used by the @loaders.gl/images module if installed
75
68
  // TODO - is there an appropriate Image API we could polyfill using an adapter?
@@ -80,6 +73,7 @@ if (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {
80
73
 
81
74
  if (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {
82
75
  global['_parseImageNode'] = parseImageNode;
76
+ global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;
83
77
  }
84
78
 
85
79
  if (!('allSettled' in Promise)) {
@@ -0,0 +1,51 @@
1
+ // loaders.gl, MIT license
2
+
3
+ import fs from 'fs'; // `fs` will be empty object in browsers (see package.json "browser" field).
4
+ import {Response} from './response.node';
5
+ import {Headers} from './headers.node';
6
+
7
+ export function isRequestURL(url: string): boolean {
8
+ return url.startsWith('http:') || url.startsWith('https:');
9
+ }
10
+
11
+ export async function fetchFileNode(url: string, options): Promise<Response> {
12
+ const noqueryUrl = url.split('?')[0];
13
+
14
+ try {
15
+ // Now open the stream
16
+ const body = await new Promise((resolve, reject) => {
17
+ // @ts-ignore
18
+ const stream = fs.createReadStream(noqueryUrl, {encoding: null});
19
+ stream.once('readable', () => resolve(stream));
20
+ stream.on('error', (error) => reject(error));
21
+ });
22
+
23
+ const status = 200;
24
+ const statusText = 'OK';
25
+ const headers = getHeadersForFile(noqueryUrl);
26
+ return new Response(body, {headers, status, statusText, url});
27
+ } catch (error) {
28
+ const status = 400;
29
+ const statusText = (error as Error).message;
30
+ const headers = {};
31
+ return new Response((error as Error).message, {headers, status, statusText, url});
32
+ }
33
+ }
34
+
35
+ function getHeadersForFile(noqueryUrl: string): Headers {
36
+ const headers = {};
37
+
38
+ // Fix up content length if we can for best progress experience
39
+ if (!headers['content-length']) {
40
+ const stats = fs.statSync(noqueryUrl);
41
+ headers['content-length'] = stats.size;
42
+ }
43
+
44
+ // Automatically decompress gzipped files with .gz extension
45
+ if (noqueryUrl.endsWith('.gz')) {
46
+ noqueryUrl = noqueryUrl.slice(0, -3);
47
+ headers['content-encoding'] = 'gzip';
48
+ }
49
+
50
+ return new Headers(headers);
51
+ }
@@ -1,20 +1,29 @@
1
- import fs from 'fs'; // `fs` will be empty object in browsers (see package.json "browser" field).
2
- import Response from './response.node';
3
- import Headers from './headers.node';
1
+ // loaders.gl, MIT license
4
2
 
3
+ import http from 'http';
4
+ import https from 'https';
5
+ import {Response} from './response.node';
6
+ import {Headers} from './headers.node';
5
7
  import {decodeDataUri} from './utils/decode-data-uri.node';
6
- import {createReadStream} from './utils/stream-utils.node';
7
8
 
8
- const isDataURL = (url) => url.startsWith('data:');
9
- const isRequestURL = (url) => url.startsWith('http:') || url.startsWith('https:');
9
+ import {fetchFileNode} from './fetch-file.node';
10
+
11
+ const isDataURL = (url: string): boolean => url.startsWith('data:');
12
+ const isRequestURL = (url: string): boolean => url.startsWith('http:') || url.startsWith('https:');
10
13
 
11
14
  /**
12
15
  * Emulation of Browser fetch for Node.js
13
16
  * @param url
14
17
  * @param options
15
18
  */
16
- export default async function fetchNode(url, options) {
19
+ // eslint-disable-next-line complexity
20
+ export async function fetchNode(url: string, options): Promise<Response> {
17
21
  try {
22
+ // Handle file streams in node
23
+ if (!isRequestURL(url) && !isDataURL(url)) {
24
+ return await fetchFileNode(url, options);
25
+ }
26
+
18
27
  // Handle data urls in node, to match `fetch``
19
28
  // Note - this loses the MIME type, data URIs are handled directly in fetch
20
29
  if (isDataURL(url)) {
@@ -35,7 +44,7 @@ export default async function fetchNode(url, options) {
35
44
  }
36
45
 
37
46
  // Need to create the stream in advance since Response constructor needs to be sync
38
- const body = await createReadStream(originalUrl, options);
47
+ const body = await createHTTPRequestReadStream(originalUrl, options);
39
48
  const headers = getHeaders(url, body, syntheticResponseHeaders);
40
49
  const {status, statusText} = getStatus(body);
41
50
 
@@ -55,6 +64,23 @@ export default async function fetchNode(url, options) {
55
64
  }
56
65
  }
57
66
 
67
+ /** Returns a promise that resolves to a readable stream */
68
+ export async function createHTTPRequestReadStream(
69
+ url: string,
70
+ options
71
+ ): Promise<http.IncomingMessage> {
72
+ // HANDLE HTTP/HTTPS REQUESTS IN NODE
73
+ // TODO: THIS IS BAD SINCE WE RETURN A PROMISE INSTEAD OF A STREAM
74
+ return await new Promise((resolve, reject) => {
75
+ const requestOptions = getRequestOptions(url, options);
76
+ const req = url.startsWith('https:')
77
+ ? https.request(requestOptions, (res) => resolve(res))
78
+ : http.request(requestOptions, (res) => resolve(res));
79
+ req.on('error', (error) => reject(error));
80
+ req.end();
81
+ });
82
+ }
83
+
58
84
  /**
59
85
  * Generate redirect url from location without origin and protocol.
60
86
  * @param originalUrl
@@ -72,9 +98,33 @@ function generateRedirectUrl(originalUrl: string, location: string): string {
72
98
  }
73
99
 
74
100
  // HELPER FUNCTIONS
75
- // PRIVATE
76
101
 
77
- function getStatus(httpResponse) {
102
+ function getRequestOptions(url: string, options?: {fetch?: typeof fetch; headers?}) {
103
+ // Ensure header keys are lower case so that we can merge without duplicates
104
+ const originalHeaders = options?.headers || {};
105
+ const headers = {};
106
+ for (const key of Object.keys(originalHeaders)) {
107
+ headers[key.toLowerCase()] = originalHeaders[key];
108
+ }
109
+
110
+ // Add default accept-encoding to headers
111
+ headers['accept-encoding'] = headers['accept-encoding'] || 'gzip,br,deflate';
112
+
113
+ const urlObject = new URL(url);
114
+ return {
115
+ hostname: urlObject.hostname,
116
+ path: urlObject.pathname,
117
+ method: 'GET',
118
+ // Add options and user provided 'options.fetch' overrides if available
119
+ ...options,
120
+ ...options?.fetch,
121
+ // Override with updated headers with accepted encodings:
122
+ headers,
123
+ port: urlObject.port
124
+ };
125
+ }
126
+
127
+ function getStatus(httpResponse: http.IncomingMessage): {status: number; statusText: string} {
78
128
  if (httpResponse.statusCode) {
79
129
  return {status: httpResponse.statusCode, statusText: httpResponse.statusMessage || 'NA'};
80
130
  }
@@ -105,24 +155,8 @@ function getHeaders(url, httpResponse, additionalHeaders = {}) {
105
155
  return new Headers(headers);
106
156
  }
107
157
 
108
- function getContentLength(url) {
109
- if (isRequestURL(url)) {
110
- // Needs to be read from actual headers
111
- return null;
112
- } else if (isDataURL(url)) {
113
- // TODO - remove media type etc
114
- return url.length - 'data:'.length;
115
- }
116
- // File URL
117
- // TODO - how to handle non-existing file, this presumably just throws
118
- try {
119
- // strip query params from URL
120
- const noqueryUrl = url.split('?')[0];
121
- const stats = fs.statSync(noqueryUrl);
122
- return stats.size;
123
- } catch (error) {
124
- // ignore for now
125
- }
126
-
127
- return null;
158
+ /** Needs to be read from actual headers */
159
+ function getContentLength(url: string): number | null {
160
+ // TODO - remove media type etc
161
+ return isDataURL(url) ? url.length - 'data:'.length : null;
128
162
  }
@@ -2,7 +2,7 @@
2
2
  * Polyfill for Browser Headers
3
3
  * Based on https://github.com/github/fetch under MIT license
4
4
  */
5
- export default class Headers {
5
+ export class Headers {
6
6
  map: {};
7
7
 
8
8
  constructor(headers) {
@@ -1,6 +1,8 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import {assert} from '../../utils/assert';
2
4
  import {decompressReadStream, concatenateReadStream} from './utils/stream-utils.node';
3
- import Headers from './headers.node';
5
+ import {Headers} from './headers.node';
4
6
 
5
7
  const isBoolean = (x) => typeof x === 'boolean';
6
8
  const isFunction = (x) => typeof x === 'function';
@@ -20,7 +22,7 @@ const isReadableNodeStream = (x) =>
20
22
  */
21
23
  import {Readable} from 'stream';
22
24
 
23
- export default class Response {
25
+ export class Response {
24
26
  readonly ok: boolean;
25
27
  readonly status: number;
26
28
  readonly statusText: string;
@@ -1,39 +1,12 @@
1
- import fs from 'fs'; // `fs` will be empty object in browsers (see package.json "browser" field).
2
- import http from 'http';
3
- import https from 'https';
1
+ // loaders.gl, MIT license
2
+
4
3
  import zlib from 'zlib';
5
4
 
6
5
  import {toArrayBuffer} from './decode-data-uri.node';
7
6
 
8
- function isRequestURL(url: string): boolean {
9
- return url.startsWith('http:') || url.startsWith('https:');
10
- }
11
-
12
- // Returns a promise that resolves to a readable stream
13
- export async function createReadStream(url, options): Promise<any> {
14
- // Handle file streams in node
15
- if (!isRequestURL(url)) {
16
- const noqueryUrl = url.split('?')[0];
17
- // Now open the stream
18
- return await new Promise((resolve, reject) => {
19
- // @ts-ignore
20
- const stream = fs.createReadStream(noqueryUrl, {encoding: null});
21
- stream.once('readable', () => resolve(stream));
22
- stream.on('error', (error) => reject(error));
23
- });
24
- }
25
-
26
- // HANDLE HTTP/HTTPS REQUESTS IN NODE
27
- // TODO: THIS IS BAD SINCE WE RETURN A PROMISE INSTEAD OF A STREAM
28
- return await new Promise((resolve, reject) => {
29
- const requestFunction = url.startsWith('https:') ? https.request : http.request;
30
- const requestOptions = getRequestOptions(url, options);
31
- const req = requestFunction(requestOptions, (res) => resolve(res));
32
- req.on('error', (error) => reject(error));
33
- req.end();
34
- });
35
- }
36
-
7
+ /**
8
+ *
9
+ */
37
10
  export function decompressReadStream(readStream, headers) {
38
11
  switch (headers.get('content-encoding')) {
39
12
  case 'br':
@@ -48,6 +21,11 @@ export function decompressReadStream(readStream, headers) {
48
21
  }
49
22
  }
50
23
 
24
+ /**
25
+ *
26
+ * @param readStream
27
+ * @returns
28
+ */
51
29
  export async function concatenateReadStream(readStream): Promise<ArrayBuffer> {
52
30
  const arrayBufferChunks: ArrayBuffer[] = [];
53
31
 
@@ -72,32 +50,6 @@ export async function concatenateReadStream(readStream): Promise<ArrayBuffer> {
72
50
  });
73
51
  }
74
52
 
75
- // HELPERS
76
-
77
- function getRequestOptions(url: string, options?: {fetch?: typeof fetch; headers?}) {
78
- // Ensure header keys are lower case so that we can merge without duplicates
79
- const originalHeaders = options?.headers || {};
80
- const headers = {};
81
- for (const key of Object.keys(originalHeaders)) {
82
- headers[key.toLowerCase()] = originalHeaders[key];
83
- }
84
-
85
- // Add default accept-encoding to headers
86
- headers['accept-encoding'] = headers['accept-encoding'] || 'gzip,br,deflate';
87
-
88
- const urlObject = new URL(url);
89
- return {
90
- hostname: urlObject.hostname,
91
- path: urlObject.pathname,
92
- method: 'GET',
93
- // Add options and user provided 'options.fetch' overrides if available
94
- ...options,
95
- ...options?.fetch,
96
- // Override with updated headers with accepted encodings:
97
- headers
98
- };
99
- }
100
-
101
53
  /**
102
54
  * Concatenate a sequence of ArrayBuffers
103
55
  * @return A concatenated ArrayBuffer
@@ -1,6 +1,9 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import getPixels from 'get-pixels';
2
- import {assert} from '../../utils/assert';
3
- import util from 'util';
4
+
5
+ /** Declares which image format mime types this loader polyfill supports */
6
+ export const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
4
7
 
5
8
  // Note: These types are also defined in @loaders.gl/images and need to be kept in sync
6
9
  type NDArray = {
@@ -13,26 +16,38 @@ type NDArray = {
13
16
  };
14
17
 
15
18
  export async function parseImageNode(arrayBuffer: ArrayBuffer, mimeType: string): Promise<NDArray> {
16
- assert(mimeType, 'MIMEType is required to parse image under Node.js');
17
-
18
- // TODO - check if getPixels callback is asynchronous if provided with buffer input
19
- // if not, parseImage can be a sync function
20
- const getPixelsAsync = util.promisify(getPixels);
19
+ if (!mimeType) {
20
+ throw new Error('MIMEType is required to parse image under Node.js');
21
+ }
21
22
 
22
23
  const buffer = arrayBuffer instanceof Buffer ? arrayBuffer : Buffer.from(arrayBuffer);
23
-
24
24
  const ndarray = await getPixelsAsync(buffer, mimeType);
25
+ return ndarray;
26
+ }
27
+
28
+ // TODO - check if getPixels callback is asynchronous if provided with buffer input
29
+ // if not, parseImage can be a sync function
30
+ function getPixelsAsync(buffer: Buffer, mimeType: string): Promise<NDArray> {
31
+ return new Promise<NDArray>((resolve) =>
32
+ getPixels(buffer, mimeType, (err, ndarray) => {
33
+ if (err) {
34
+ throw err;
35
+ }
36
+
37
+ const shape = [...ndarray.shape];
38
+ const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
39
+ const data = ndarray.data instanceof Buffer ? new Uint8Array(ndarray.data) : ndarray.data;
25
40
 
26
- const shape = [...ndarray.shape];
27
- const layers = ndarray.shape.length === 4 ? ndarray.shape.shift() : 1;
28
-
29
- // extract width/height etc
30
- return {
31
- shape,
32
- data: ndarray.data,
33
- width: ndarray.shape[0],
34
- height: ndarray.shape[1],
35
- components: ndarray.shape[2],
36
- layers
37
- };
41
+ // extract width/height etc
42
+ resolve({
43
+ shape,
44
+ data,
45
+ width: ndarray.shape[0],
46
+ height: ndarray.shape[1],
47
+ components: ndarray.shape[2],
48
+ // TODO - error
49
+ layers: layers ? [layers] : []
50
+ });
51
+ })
52
+ );
38
53
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/bundle.ts"],"names":["moduleExports","require","globalThis","loaders","module","exports","Object","assign"],"mappings":"AACA,MAAMA,aAAa,GAAGC,OAAO,CAAC,SAAD,CAA7B;;AACAC,UAAU,CAACC,OAAX,GAAqBD,UAAU,CAACC,OAAX,IAAsB,EAA3C;AACAC,MAAM,CAACC,OAAP,GAAiBC,MAAM,CAACC,MAAP,CAAcL,UAAU,CAACC,OAAzB,EAAkCH,aAAlC,CAAjB","sourcesContent":["// @ts-nocheck\nconst moduleExports = require('./index');\nglobalThis.loaders = globalThis.loaders || {};\nmodule.exports = Object.assign(globalThis.loaders, moduleExports);\n"],"file":"bundle.js"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["DOMParser","isBrowser","global","TextDecoder","TextEncoder","allSettled","base64","HeadersNode","ResponseNode","fetchNode","encodeImageNode","parseImageNode","ReadableStreamPolyfill","BlobPolyfill","FileReaderPolyfill","FilePolyfill","installFilePolyfills","installTextEncoder","installTextDecoder","atob","btoa","Promise"],"mappings":"AACA,SAAQA,SAAR,QAAwB,gBAAxB;AACA,SAAQC,SAAR,EAAmBC,MAAnB,QAAgC,iBAAhC;AAEA,SAAQC,WAAR,EAAqBC,WAArB,QAAuC,gBAAvC;AACA,SAAQC,UAAR,QAAyB,uBAAzB;AAGA,OAAO,KAAKC,MAAZ,MAAwB,yBAAxB;AAEA,OAAOC,WAAP,MAAwB,2BAAxB;AACA,OAAOC,YAAP,MAAyB,4BAAzB;AACA,OAAOC,SAAP,MAAsB,yBAAtB;AAEA,SAAQC,eAAR,QAA8B,iCAA9B;AACA,SAAQC,cAAR,QAA6B,gCAA7B;AAEA,SAAQC,sBAAR,QAAqC,6BAArC;AACA,SAAQC,YAAR,QAA2B,kBAA3B;AACA,SAAQC,kBAAR,QAAiC,yBAAjC;AACA,SAAQC,YAAR,QAA2B,kBAA3B;AACA,SAAQC,oBAAR,QAAmC,oCAAnC;AAMA,MAAMC,kBAAkB,GAAG,CAAChB,SAAD,IAAc,EAAE,iBAAiBC,MAAnB,CAAzC;;AACA,IAAIe,kBAAJ,EAAwB;AACtBf,EAAAA,MAAM,CAAC,aAAD,CAAN,GAAwBE,WAAxB;AACD;;AAED,MAAMc,kBAAkB,GAAG,CAACjB,SAAD,IAAc,EAAE,iBAAiBC,MAAnB,CAAzC;;AACA,IAAIgB,kBAAJ,EAAwB;AACtBhB,EAAAA,MAAM,CAAC,aAAD,CAAN,GAAwBC,WAAxB;AACD;;AAMD,IAAI,CAACF,SAAD,IAAc,EAAE,UAAUC,MAAZ,CAAd,IAAqCI,MAAM,CAACa,IAAhD,EAAsD;AACpDjB,EAAAA,MAAM,CAAC,MAAD,CAAN,GAAiBI,MAAM,CAACa,IAAxB;AACD;;AACD,IAAI,CAAClB,SAAD,IAAc,EAAE,UAAUC,MAAZ,CAAd,IAAqCI,MAAM,CAACc,IAAhD,EAAsD;AACpDlB,EAAAA,MAAM,CAAC,MAAD,CAAN,GAAiBI,MAAM,CAACc,IAAxB;AACD;;AAMD,IAAI,CAACnB,SAAD,IAAc,EAAE,aAAaC,MAAf,CAAd,IAAwCK,WAA5C,EAAyD;AACvDL,EAAAA,MAAM,CAAC,SAAD,CAAN,GAAoBK,WAApB;AACD;;AAED,IAAI,CAACN,SAAD,IAAc,EAAE,cAAcC,MAAhB,CAAd,IAAyCM,YAA7C,EAA2D;AACzDN,EAAAA,MAAM,CAAC,UAAD,CAAN,GAAqBM,YAArB;AACD;;AAED,IAAI,CAACP,SAAD,IAAc,EAAE,WAAWC,MAAb,CAAd,IAAsCO,SAA1C,EAAqD;AACnDP,EAAAA,MAAM,CAAC,OAAD,CAAN,GAAkBO,SAAlB;AACD;;AAMD,IAAI,CAACR,SAAD,IAAc,EAAE,eAAeC,MAAjB,CAAd,IAA0CF,SAA9C,EAAyD;AACvDE,EAAAA,MAAM,CAAC,WAAD,CAAN,GAAsBF,SAAtB;AACD;;AAMD,IAAI,CAACC,SAAD,IAAc,EAAE,sBAAsBC,MAAxB,CAAd,IAAiDQ,eAArD,EAAsE;AACpER,EAAAA,MAAM,CAAC,kBAAD,CAAN,GAA6BQ,eAA7B;AACD;;AAED,IAAI,CAACT,SAAD,IAAc,EAAE,qBAAqBC,MAAvB,CAAd,IAAgDS,cAApD,EAAoE;AAClET,EAAAA,MAAM,CAAC,iBAAD,CAAN,GAA4BS,cAA5B;AACD;;AAED,IAAI,EAAE,gBAAgBU,OAAlB,CAAJ,EAAgC;AAE9BA,EAAAA,OAAO,CAAChB,UAAR,GAAqBA,UAArB;AACD","sourcesContent":["/* eslint-disable dot-notation */\nimport {DOMParser} from '@xmldom/xmldom';\nimport {isBrowser, global} from './utils/globals';\n\nimport {TextDecoder, TextEncoder} from './lib/encoding';\nimport {allSettled} from './promise/all-settled';\n\n// Node specific\nimport * as base64 from './node/buffer/btoa.node';\n\nimport HeadersNode from './node/fetch/headers.node';\nimport ResponseNode from './node/fetch/response.node';\nimport fetchNode from './node/fetch/fetch.node';\n\nimport {encodeImageNode} from './node/images/encode-image.node';\nimport {parseImageNode} from './node/images/parse-image.node';\n\nexport {ReadableStreamPolyfill} from './node/file/readable-stream';\nexport {BlobPolyfill} from './node/file/blob';\nexport {FileReaderPolyfill} from './node/file/file-reader';\nexport {FilePolyfill} from './node/file/file';\nexport {installFilePolyfills} from './node/file/install-file-polyfills';\n\n// POLYFILLS: TextEncoder, TextDecoder\n// - Recent Node versions have these classes but virtually no encodings unless special build.\n// - Browser: Edge, IE11 do not have these\n\nconst installTextEncoder = !isBrowser || !('TextEncoder' in global);\nif (installTextEncoder) {\n global['TextEncoder'] = TextEncoder;\n}\n\nconst installTextDecoder = !isBrowser || !('TextDecoder' in global);\nif (installTextDecoder) {\n global['TextDecoder'] = TextDecoder;\n}\n\n// POLYFILLS: btoa, atob\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('atob' in global) && base64.atob) {\n global['atob'] = base64.atob;\n}\nif (!isBrowser && !('btoa' in global) && base64.btoa) {\n global['btoa'] = base64.btoa;\n}\n\n// POLYFILL: fetch\n// - Node: Yes\n// - Browser: No. For This polyfill is node only, IE11 etc, install external polyfill\n\nif (!isBrowser && !('Headers' in global) && HeadersNode) {\n global['Headers'] = HeadersNode;\n}\n\nif (!isBrowser && !('Response' in global) && ResponseNode) {\n global['Response'] = ResponseNode;\n}\n\nif (!isBrowser && !('fetch' in global) && fetchNode) {\n global['fetch'] = fetchNode;\n}\n\n// POLYFILL: DOMParser\n// - Node: Yes\n// - Browser: No\n\nif (!isBrowser && !('DOMParser' in global) && DOMParser) {\n global['DOMParser'] = DOMParser;\n}\n\n// NODE IMAGE FUNCTIONS:\n// These are not official polyfills but used by the @loaders.gl/images module if installed\n// TODO - is there an appropriate Image API we could polyfill using an adapter?\n\nif (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {\n global['_encodeImageNode'] = encodeImageNode;\n}\n\nif (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {\n global['_parseImageNode'] = parseImageNode;\n}\n\nif (!('allSettled' in Promise)) {\n // @ts-ignore\n Promise.allSettled = allSettled;\n}\n"],"file":"index.js"}