@loaders.gl/loader-utils 3.3.0 → 3.4.0-alpha.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 (119) hide show
  1. package/dist/es5/index.js +22 -36
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/json-loader.js +1 -1
  4. package/dist/es5/json-loader.js.map +1 -1
  5. package/dist/es5/lib/binary-utils/array-buffer-utils.js +30 -0
  6. package/dist/es5/lib/binary-utils/array-buffer-utils.js.map +1 -1
  7. package/dist/es5/lib/binary-utils/{dataview-copy-utils.js → binary-copy-utils.js} +1 -33
  8. package/dist/es5/lib/binary-utils/binary-copy-utils.js.map +1 -0
  9. package/dist/es5/lib/binary-utils/{memory-conversion-utils.js → buffer-utils.js} +7 -26
  10. package/dist/es5/lib/binary-utils/buffer-utils.js.map +1 -0
  11. package/dist/es5/lib/binary-utils/encode-utils.js +36 -0
  12. package/dist/es5/lib/binary-utils/encode-utils.js.map +1 -0
  13. package/dist/es5/lib/binary-utils/get-first-characters.js +0 -2
  14. package/dist/es5/lib/binary-utils/get-first-characters.js.map +1 -1
  15. package/dist/es5/lib/node/buffer.js +8 -5
  16. package/dist/es5/lib/node/buffer.js.map +1 -1
  17. package/dist/es5/lib/node/fs.js +9 -18
  18. package/dist/es5/lib/node/fs.js.map +1 -1
  19. package/dist/es5/lib/node/util.js +7 -31
  20. package/dist/es5/lib/node/util.js.map +1 -1
  21. package/dist/esm/index.js +9 -11
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/json-loader.js +1 -1
  24. package/dist/esm/json-loader.js.map +1 -1
  25. package/dist/esm/lib/binary-utils/array-buffer-utils.js +27 -0
  26. package/dist/esm/lib/binary-utils/array-buffer-utils.js.map +1 -1
  27. package/dist/esm/lib/binary-utils/{dataview-copy-utils.js → binary-copy-utils.js} +1 -31
  28. package/dist/esm/lib/binary-utils/binary-copy-utils.js.map +1 -0
  29. package/dist/esm/lib/binary-utils/buffer-utils.js +18 -0
  30. package/dist/esm/lib/binary-utils/buffer-utils.js.map +1 -0
  31. package/dist/esm/lib/binary-utils/encode-utils.js +29 -0
  32. package/dist/esm/lib/binary-utils/encode-utils.js.map +1 -0
  33. package/dist/esm/lib/binary-utils/get-first-characters.js +0 -3
  34. package/dist/esm/lib/binary-utils/get-first-characters.js.map +1 -1
  35. package/dist/esm/lib/node/buffer.js +9 -5
  36. package/dist/esm/lib/node/buffer.js.map +1 -1
  37. package/dist/esm/lib/node/fs.js +9 -11
  38. package/dist/esm/lib/node/fs.js.map +1 -1
  39. package/dist/esm/lib/node/util.js +2 -10
  40. package/dist/esm/lib/node/util.js.map +1 -1
  41. package/dist/index.d.ts +7 -10
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +23 -25
  44. package/dist/lib/binary-utils/array-buffer-utils.d.ts +8 -4
  45. package/dist/lib/binary-utils/array-buffer-utils.d.ts.map +1 -1
  46. package/dist/lib/binary-utils/array-buffer-utils.js +36 -5
  47. package/dist/lib/binary-utils/binary-copy-utils.d.ts +24 -0
  48. package/dist/lib/binary-utils/binary-copy-utils.d.ts.map +1 -0
  49. package/dist/lib/binary-utils/{dataview-copy-utils.js → binary-copy-utils.js} +5 -51
  50. package/dist/lib/binary-utils/{memory-conversion-utils.d.ts → buffer-utils.d.ts} +4 -3
  51. package/dist/lib/binary-utils/buffer-utils.d.ts.map +1 -0
  52. package/dist/lib/binary-utils/{memory-conversion-utils.js → buffer-utils.js} +9 -28
  53. package/dist/lib/binary-utils/encode-utils.d.ts +4 -0
  54. package/dist/lib/binary-utils/encode-utils.d.ts.map +1 -0
  55. package/dist/lib/binary-utils/encode-utils.js +35 -0
  56. package/dist/lib/binary-utils/get-first-characters.d.ts +1 -15
  57. package/dist/lib/binary-utils/get-first-characters.d.ts.map +1 -1
  58. package/dist/lib/binary-utils/get-first-characters.js +0 -15
  59. package/dist/lib/node/buffer.d.ts +1 -3
  60. package/dist/lib/node/buffer.d.ts.map +1 -1
  61. package/dist/lib/node/buffer.js +8 -7
  62. package/dist/lib/node/fs.d.ts +2 -4
  63. package/dist/lib/node/fs.d.ts.map +1 -1
  64. package/dist/lib/node/fs.js +9 -10
  65. package/dist/lib/node/util.d.ts +3 -11
  66. package/dist/lib/node/util.d.ts.map +1 -1
  67. package/dist/lib/node/util.js +27 -20
  68. package/dist/lib/worker-loader-utils/create-loader-worker.js +1 -1
  69. package/package.json +4 -5
  70. package/src/index.ts +18 -22
  71. package/src/lib/binary-utils/array-buffer-utils.ts +40 -4
  72. package/src/lib/binary-utils/{dataview-copy-utils.ts → binary-copy-utils.ts} +5 -57
  73. package/src/lib/binary-utils/buffer-utils.ts +28 -0
  74. package/src/lib/binary-utils/encode-utils.ts +32 -0
  75. package/src/lib/binary-utils/get-first-characters.ts +1 -17
  76. package/src/lib/node/buffer.ts +10 -9
  77. package/src/lib/node/fs.ts +11 -16
  78. package/src/lib/node/util.ts +2 -68
  79. package/dist/es5/lib/binary-utils/dataview-copy-utils.js.map +0 -1
  80. package/dist/es5/lib/binary-utils/memory-conversion-utils.js.map +0 -1
  81. package/dist/es5/lib/filesystems/readable-file.js +0 -100
  82. package/dist/es5/lib/filesystems/readable-file.js.map +0 -1
  83. package/dist/es5/lib/filesystems/writable-file.js +0 -89
  84. package/dist/es5/lib/filesystems/writable-file.js.map +0 -1
  85. package/dist/es5/lib/loader-utils/buffer-utils.js +0 -19
  86. package/dist/es5/lib/loader-utils/buffer-utils.js.map +0 -1
  87. package/dist/es5/lib/node/stream.js +0 -20
  88. package/dist/es5/lib/node/stream.js.map +0 -1
  89. package/dist/esm/lib/binary-utils/dataview-copy-utils.js.map +0 -1
  90. package/dist/esm/lib/binary-utils/memory-conversion-utils.js +0 -38
  91. package/dist/esm/lib/binary-utils/memory-conversion-utils.js.map +0 -1
  92. package/dist/esm/lib/filesystems/readable-file.js +0 -22
  93. package/dist/esm/lib/filesystems/readable-file.js.map +0 -1
  94. package/dist/esm/lib/filesystems/writable-file.js +0 -21
  95. package/dist/esm/lib/filesystems/writable-file.js.map +0 -1
  96. package/dist/esm/lib/loader-utils/buffer-utils.js +0 -13
  97. package/dist/esm/lib/loader-utils/buffer-utils.js.map +0 -1
  98. package/dist/esm/lib/node/stream.js +0 -11
  99. package/dist/esm/lib/node/stream.js.map +0 -1
  100. package/dist/lib/binary-utils/dataview-copy-utils.d.ts +0 -43
  101. package/dist/lib/binary-utils/dataview-copy-utils.d.ts.map +0 -1
  102. package/dist/lib/binary-utils/memory-conversion-utils.d.ts.map +0 -1
  103. package/dist/lib/filesystems/readable-file.d.ts +0 -10
  104. package/dist/lib/filesystems/readable-file.d.ts.map +0 -1
  105. package/dist/lib/filesystems/readable-file.js +0 -25
  106. package/dist/lib/filesystems/writable-file.d.ts +0 -18
  107. package/dist/lib/filesystems/writable-file.d.ts.map +0 -1
  108. package/dist/lib/filesystems/writable-file.js +0 -48
  109. package/dist/lib/loader-utils/buffer-utils.d.ts +0 -10
  110. package/dist/lib/loader-utils/buffer-utils.d.ts.map +0 -1
  111. package/dist/lib/loader-utils/buffer-utils.js +0 -22
  112. package/dist/lib/node/stream.d.ts +0 -4
  113. package/dist/lib/node/stream.d.ts.map +0 -1
  114. package/dist/lib/node/stream.js +0 -17
  115. package/src/lib/binary-utils/memory-conversion-utils.ts +0 -53
  116. package/src/lib/filesystems/readable-file.ts +0 -30
  117. package/src/lib/filesystems/writable-file.ts +0 -44
  118. package/src/lib/loader-utils/buffer-utils.ts +0 -18
  119. package/src/lib/node/stream.ts +0 -18
@@ -1,22 +1,29 @@
1
1
  "use strict";
2
- // @loaders.gl, MIT license
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
3
25
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.promisify3 = exports.promisify2 = exports.promisify1 = void 0;
5
- /**
6
- * Typesafe promisify implementation
7
- * @link https://dev.to/_gdelgado/implement-a-type-safe-version-of-node-s-promisify-in-7-lines-of-code-in-typescript-2j34
8
- * @param fn
9
- * @returns
10
- */
11
- function promisify1(fn) {
12
- return (args) => new Promise((resolve, reject) => fn(args, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
13
- }
14
- exports.promisify1 = promisify1;
15
- function promisify2(fn) {
16
- return (arg1, arg2) => new Promise((resolve, reject) => fn(arg1, arg2, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
17
- }
18
- exports.promisify2 = promisify2;
19
- function promisify3(fn) {
20
- return (arg1, arg2, arg3) => new Promise((resolve, reject) => fn(arg1, arg2, arg3, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
21
- }
22
- exports.promisify3 = promisify3;
26
+ exports.promisify = void 0;
27
+ const util = __importStar(require("util"));
28
+ /** Wrapper for Node.js promisify */
29
+ exports.promisify = util.promisify;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createLoaderWorker = void 0;
4
4
  const worker_utils_1 = require("@loaders.gl/worker-utils");
5
- // import {validateLoaderVersion} from './validate-loader-version.js';
5
+ // import {validateLoaderVersion} from './validate-loader-version';
6
6
  let requestId = 0;
7
7
  /**
8
8
  * Set up a WebWorkerGlobalScope to talk with the main thread
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/loader-utils",
3
- "version": "3.3.0",
3
+ "version": "3.4.0-alpha.1",
4
4
  "description": "Framework-independent loaders for 3D graphics formats",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -40,14 +40,13 @@
40
40
  "./dist/es5/lib/node/util.js": false,
41
41
  "./dist/esm/lib/node/util.js": false,
42
42
  "fs": false,
43
- "util": false,
44
- "stream": false
43
+ "util": false
45
44
  },
46
45
  "scripts": {},
47
46
  "dependencies": {
48
47
  "@babel/runtime": "^7.3.1",
49
- "@loaders.gl/worker-utils": "3.3.0",
48
+ "@loaders.gl/worker-utils": "3.4.0-alpha.1",
50
49
  "@probe.gl/stats": "^3.5.0"
51
50
  },
52
- "gitHead": "566d194f175a03e042d3e588cdd514048f428cc1"
51
+ "gitHead": "4085b0323050e4361614471319a1fb4729547bbf"
53
52
  }
package/src/index.ts CHANGED
@@ -35,6 +35,7 @@ export {parseJSON} from './lib/parser-utils/parse-json';
35
35
 
36
36
  // MEMORY COPY UTILS
37
37
  export {
38
+ toArrayBuffer,
38
39
  sliceArrayBuffer,
39
40
  concatenateArrayBuffers,
40
41
  concatenateTypedArrays,
@@ -42,12 +43,14 @@ export {
42
43
  } from './lib/binary-utils/array-buffer-utils';
43
44
  export {padToNBytes, copyToArray, copyArrayBuffer} from './lib/binary-utils/memory-copy-utils';
44
45
  export {
45
- padStringToByteAlignment,
46
- copyStringToDataView,
47
- copyBinaryToDataView,
48
46
  copyPaddedArrayBufferToDataView,
49
47
  copyPaddedStringToDataView
50
- } from './lib/binary-utils/dataview-copy-utils';
48
+ } from './lib/binary-utils/binary-copy-utils';
49
+ export {
50
+ padStringToByteAlignment,
51
+ copyStringToDataView,
52
+ copyBinaryToDataView
53
+ } from './lib/binary-utils/encode-utils';
51
54
  export {getFirstCharacters, getMagicString} from './lib/binary-utils/get-first-characters';
52
55
 
53
56
  // ITERATOR UTILS
@@ -73,31 +76,24 @@ export {JSONLoader} from './json-loader';
73
76
 
74
77
  // Node.js emulation (can be used in browser)
75
78
 
79
+ // `path` replacement (avoids bundling big path polyfill)
80
+ import * as path from './lib/path-utils/path';
81
+ export {path};
82
+
76
83
  // Avoid direct use of `Buffer` which pulls in 50KB polyfill
77
- export {isBuffer, toBuffer, toArrayBuffer} from './lib/binary-utils/memory-conversion-utils';
84
+ export {isBuffer, toBuffer, bufferToArrayBuffer} from './lib/binary-utils/buffer-utils';
78
85
 
79
86
  // Note.js wrappers (can be safely imported, but not used in browser)
80
87
 
81
- // Use instead of importing 'util' to avoid node dependencies
82
- export {promisify1, promisify2} from './lib/node/util';
83
-
84
- // `path` replacement (avoids bundling big path polyfill)
85
- import * as path from './lib/path-utils/path';
86
- export {path};
88
+ // Use instead of importing 'util'
89
+ import * as util from './lib/node/util';
90
+ export {util};
91
+ // TODO - remove
92
+ export {promisify} from './lib/node/util';
87
93
 
88
- // Use instead of importing 'fs' to avoid node dependencies`
94
+ // Use instead of importing 'fs';`
89
95
  import * as fs from './lib/node/fs';
90
96
  export {fs};
91
97
 
92
- // Use instead of importing 'stream' to avoid node dependencies`
93
- import * as stream from './lib/node/stream';
94
- export {stream};
95
-
96
98
  // EXPERIMENTAL
97
- export type {ReadableFile} from './lib/filesystems/readable-file';
98
- export {makeReadableFile} from './lib/filesystems/readable-file';
99
-
100
- export type {WritableFile} from './lib/filesystems/writable-file';
101
- export {makeWritableFile} from './lib/filesystems/writable-file';
102
-
103
99
  export {default as _NodeFileSystem} from './lib/filesystems/node-filesystem';
@@ -1,10 +1,46 @@
1
1
  import {TypedArray} from '../../types';
2
+ import {isBuffer, bufferToArrayBuffer} from './buffer-utils';
3
+
4
+ /**
5
+ * Convert an object to an array buffer
6
+ */
7
+ export function toArrayBuffer(data: any): ArrayBuffer {
8
+ // Note: Should be called first, Buffers can trigger other detections below
9
+ if (isBuffer(data)) {
10
+ return bufferToArrayBuffer(data);
11
+ }
12
+
13
+ if (data instanceof ArrayBuffer) {
14
+ return data;
15
+ }
16
+
17
+ // Careful - Node Buffers look like Uint8Arrays (keep after isBuffer)
18
+ if (ArrayBuffer.isView(data)) {
19
+ if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
20
+ return data.buffer;
21
+ }
22
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
23
+ }
24
+
25
+ if (typeof data === 'string') {
26
+ const text = data;
27
+ const uint8Array = new TextEncoder().encode(text);
28
+ return uint8Array.buffer;
29
+ }
30
+
31
+ // HACK to support Blob polyfill
32
+ if (data && typeof data === 'object' && data._toArrayBuffer) {
33
+ return data._toArrayBuffer();
34
+ }
35
+
36
+ throw new Error('toArrayBuffer');
37
+ }
2
38
 
3
39
  /**
4
40
  * compare two binary arrays for equality
5
- * @param a
6
- * @param b
7
- * @param byteLength
41
+ * @param {ArrayBuffer} a
42
+ * @param {ArrayBuffer} b
43
+ * @param {number} byteLength
8
44
  */
9
45
  export function compareArrayBuffers(
10
46
  arrayBuffer1: ArrayBuffer,
@@ -55,7 +91,7 @@ export function concatenateArrayBuffers(...sources: (ArrayBuffer | Uint8Array)[]
55
91
  /**
56
92
  * Concatenate arbitrary count of typed arrays
57
93
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
58
- * @param - list of arrays. All arrays should be the same type
94
+ * @param {...*} arrays - list of arrays. All arrays should be the same type
59
95
  * @return A concatenated TypedArray
60
96
  */
61
97
  export function concatenateTypedArrays<T>(...typedArrays: T[]): T {
@@ -1,65 +1,13 @@
1
- // loaders./gl, MIT license
2
-
3
1
  import {TypedArray} from '../../types';
4
2
  import {padToNBytes} from './memory-copy-utils';
5
3
 
6
- /**
7
- * Helper function that pads a string with spaces to fit a certain byte alignment
8
- * @param string
9
- * @param byteAlignment
10
- * @returns
11
- *
12
- * @todo PERFORMANCE IDEA: No need to copy string twice...
13
- */
14
- export function padStringToByteAlignment(string: string, byteAlignment: number): string {
15
- const length = string.length;
16
- const paddedLength = Math.ceil(length / byteAlignment) * byteAlignment; // Round up to the required alignment
17
- const padding = paddedLength - length;
18
- let whitespace = '';
19
- for (let i = 0; i < padding; ++i) {
20
- whitespace += ' ';
21
- }
22
- return string + whitespace;
23
- }
24
-
25
- /**
26
- *
27
- * @param dataView
28
- * @param byteOffset
29
- * @param string
30
- * @param byteLength
31
- * @returns
32
- */
33
- export function copyStringToDataView(
34
- dataView: DataView,
35
- byteOffset: number,
36
- string: string,
37
- byteLength: number
38
- ): number {
39
- if (dataView) {
40
- for (let i = 0; i < byteLength; i++) {
41
- dataView.setUint8(byteOffset + i, string.charCodeAt(i));
42
- }
43
- }
44
- return byteOffset + byteLength;
45
- }
46
-
47
- export function copyBinaryToDataView(dataView, byteOffset, binary, byteLength) {
48
- if (dataView) {
49
- for (let i = 0; i < byteLength; i++) {
50
- dataView.setUint8(byteOffset + i, binary[i]);
51
- }
52
- }
53
- return byteOffset + byteLength;
54
- }
55
-
56
4
  /**
57
5
  * Copy sourceBuffer to dataView with some padding
58
6
  *
59
- * @param dataView - destination data container. If null - only new offset is calculated
60
- * @param byteOffset - destination byte offset to copy to
61
- * @param sourceBuffer - source data buffer
62
- * @param padding - pad the resulting array to multiple of "padding" bytes. Additional bytes are filled with 0x20 (ASCII space)
7
+ * @param {DataView | null} dataView - destination data container. If null - only new offset is calculated
8
+ * @param {number} byteOffset - destination byte offset to copy to
9
+ * @param {Array | TypedArray} sourceBuffer - source data buffer
10
+ * @param {number} padding - pad the resulting array to multiple of "padding" bytes. Additional bytes are filled with 0x20 (ASCII space)
63
11
  *
64
12
  * @return new byteOffset of resulting dataView
65
13
  */
@@ -68,7 +16,7 @@ export function copyPaddedArrayBufferToDataView(
68
16
  byteOffset: number,
69
17
  sourceBuffer: TypedArray,
70
18
  padding: number
71
- ): number {
19
+ ) {
72
20
  const paddedLength = padToNBytes(sourceBuffer.byteLength, padding);
73
21
  const padLength = paddedLength - sourceBuffer.byteLength;
74
22
 
@@ -0,0 +1,28 @@
1
+ import * as node from '../node/buffer';
2
+
3
+ /**
4
+ * Check for Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)
5
+ */
6
+ export function isBuffer(value: any): boolean {
7
+ return value && typeof value === 'object' && value.isBuffer;
8
+ }
9
+
10
+ /**
11
+ * Converts to Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)
12
+ * @todo better data type
13
+ */
14
+ export function toBuffer(data: any): Buffer {
15
+ return node.toBuffer ? node.toBuffer(data) : data;
16
+ }
17
+
18
+ /**
19
+ * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
20
+ * @todo better data type
21
+ */
22
+ export function bufferToArrayBuffer(buffer: any): ArrayBuffer {
23
+ if (isBuffer(buffer)) {
24
+ const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
25
+ return typedArray.slice().buffer;
26
+ }
27
+ return buffer;
28
+ }
@@ -0,0 +1,32 @@
1
+ // Note: These were broken out from gltf loader...
2
+ // eslint-disable-next-line complexity
3
+
4
+ // PERFORMANCE IDEA: No need to copy string twice...
5
+ export function padStringToByteAlignment(string, byteAlignment) {
6
+ const length = string.length;
7
+ const paddedLength = Math.ceil(length / byteAlignment) * byteAlignment; // Round up to the required alignment
8
+ const padding = paddedLength - length;
9
+ let whitespace = '';
10
+ for (let i = 0; i < padding; ++i) {
11
+ whitespace += ' ';
12
+ }
13
+ return string + whitespace;
14
+ }
15
+
16
+ export function copyStringToDataView(dataView, byteOffset, string, byteLength) {
17
+ if (dataView) {
18
+ for (let i = 0; i < byteLength; i++) {
19
+ dataView.setUint8(byteOffset + i, string.charCodeAt(i));
20
+ }
21
+ }
22
+ return byteOffset + byteLength;
23
+ }
24
+
25
+ export function copyBinaryToDataView(dataView, byteOffset, binary, byteLength) {
26
+ if (dataView) {
27
+ for (let i = 0; i < byteLength; i++) {
28
+ dataView.setUint8(byteOffset + i, binary[i]);
29
+ }
30
+ }
31
+ return byteOffset + byteLength;
32
+ }
@@ -1,12 +1,4 @@
1
- // loaders.gl, MIT license
2
-
3
- /**
4
- * Get the first characters from a binary file (interpret the first bytes as an ASCII string)
5
- * @param data
6
- * @param length
7
- * @returns
8
- */
9
- export function getFirstCharacters(data: string | ArrayBuffer, length: number = 5): string {
1
+ export function getFirstCharacters(data, length = 5): string {
10
2
  if (typeof data === 'string') {
11
3
  return data.slice(0, length);
12
4
  } else if (ArrayBuffer.isView(data)) {
@@ -19,14 +11,6 @@ export function getFirstCharacters(data: string | ArrayBuffer, length: number =
19
11
  return '';
20
12
  }
21
13
 
22
- /**
23
- * Gets a magic string from a "file"
24
- * Typically used to check or detect file format
25
- * @param arrayBuffer
26
- * @param byteOffset
27
- * @param length
28
- * @returns
29
- */
30
14
  export function getMagicString(
31
15
  arrayBuffer: ArrayBuffer,
32
16
  byteOffset: number,
@@ -1,10 +1,10 @@
1
1
  // Isolates Buffer references to ensure they are only bundled under Node.js (avoids big webpack polyfill)
2
2
  // this file is not visible to webpack (it is excluded in the package.json "browser" field).
3
3
 
4
+ import {assert} from '../env-utils/assert';
5
+
4
6
  /**
5
7
  * Convert Buffer to ArrayBuffer
6
- * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
7
- * @todo better data type
8
8
  */
9
9
  export function toArrayBuffer(buffer) {
10
10
  // TODO - per docs we should just be able to call buffer.buffer, but there are issues
@@ -18,18 +18,19 @@ export function toArrayBuffer(buffer) {
18
18
  /**
19
19
  * Convert (copy) ArrayBuffer to Buffer
20
20
  */
21
- export function toBuffer(binaryData: ArrayBuffer | ArrayBuffer | Buffer): Buffer {
22
- if (Buffer.isBuffer(binaryData)) {
23
- return binaryData;
24
- }
25
-
21
+ export function toBuffer(binaryData) {
26
22
  if (ArrayBuffer.isView(binaryData)) {
27
23
  binaryData = binaryData.buffer;
28
24
  }
29
25
 
30
26
  if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
31
- return Buffer.from(binaryData);
27
+ const buffer = new Buffer(binaryData.byteLength);
28
+ const view = new Uint8Array(binaryData);
29
+ for (let i = 0; i < buffer.length; ++i) {
30
+ buffer[i] = view[i];
31
+ }
32
+ return buffer;
32
33
  }
33
34
 
34
- throw new Error('toBuffer');
35
+ return assert(false);
35
36
  }
@@ -1,9 +1,9 @@
1
1
  // fs wrapper (promisified fs + avoids bundling fs in browsers)
2
2
  import fs from 'fs';
3
3
  import {toArrayBuffer} from './buffer';
4
- import {promisify2, promisify3} from './util';
4
+ import {promisify} from './util';
5
5
 
6
- export type {Stats, WriteStream} from 'fs';
6
+ export type {Stats} from 'fs';
7
7
 
8
8
  export let readdir;
9
9
  /** Wrapper for Node.js fs method */
@@ -23,46 +23,41 @@ export let writeFileSync;
23
23
  /** Wrapper for Node.js fs method */
24
24
  export let open;
25
25
  /** Wrapper for Node.js fs method */
26
- export let close: (fd: number) => Promise<void>;
26
+ export let close;
27
27
  /** Wrapper for Node.js fs method */
28
28
  export let read;
29
29
  /** Wrapper for Node.js fs method */
30
30
  export let fstat;
31
31
 
32
- export let createWriteStream: typeof fs.createWriteStream;
33
-
34
32
  export let isSupported = Boolean(fs);
35
33
 
36
34
  // paths
37
35
 
38
36
  try {
39
37
  /** Wrapper for Node.js fs method */
40
- readdir = promisify2(fs.readdir);
38
+ readdir = promisify(fs.readdir);
41
39
  /** Wrapper for Node.js fs method */
42
- stat = promisify2(fs.stat);
40
+ stat = promisify(fs.stat);
43
41
 
44
42
  /** Wrapper for Node.js fs method */
45
- readFile = fs.readFile;
43
+ readFile = promisify(fs.readFile);
46
44
  /** Wrapper for Node.js fs method */
47
45
  readFileSync = fs.readFileSync;
48
46
  /** Wrapper for Node.js fs method */
49
- writeFile = promisify3(fs.writeFile);
47
+ writeFile = promisify(fs.writeFile);
50
48
  /** Wrapper for Node.js fs method */
51
49
  writeFileSync = fs.writeFileSync;
52
50
 
53
51
  // file descriptors
54
52
 
55
53
  /** Wrapper for Node.js fs method */
56
- open = fs.open;
54
+ open = promisify(fs.open);
57
55
  /** Wrapper for Node.js fs method */
58
- close = (fd: number) =>
59
- new Promise((resolve, reject) => fs.close(fd, (err) => (err ? reject(err) : resolve())));
56
+ close = promisify(fs.close);
60
57
  /** Wrapper for Node.js fs method */
61
- read = fs.read;
58
+ read = promisify(fs.read);
62
59
  /** Wrapper for Node.js fs method */
63
- fstat = fs.fstat;
64
-
65
- createWriteStream = fs.createWriteStream;
60
+ fstat = promisify(fs.fstat);
66
61
 
67
62
  isSupported = Boolean(fs);
68
63
  } catch {
@@ -1,70 +1,4 @@
1
- // @loaders.gl, MIT license
2
-
3
- // type Parameter1<T extends (arg1: any, ...args: unknown[]) => unknown> = T extends (
4
- // arg1: infer P,
5
- // ...args: unknown[]
6
- // ) => unknown
7
- // ? P
8
- // : never;
9
-
10
- // type Parameter2<T extends (arg1: unknown, arg2: any, ...args: unknown[]) => unknown> = T extends (
11
- // arg1: unknown,
12
- // arg2: infer P,
13
- // ...args: unknown[]
14
- // ) => unknown
15
- // ? P
16
- // : never;
17
-
18
- // type CallbackParameter2<
19
- // T extends (arg: unknown, cb: (error: any, value: any) => void) => unknown
20
- // > = T extends (arg: unknown, cb: (error: any, value: infer P) => void) => unknown ? P : never;
21
-
22
- // type CallbackParameter3<
23
- // T extends (arg: unknown, arg2: unknown, cb: (error: any, value: any) => void) => unknown
24
- // > = T extends (arg: unknown, arg2: unknown, cb: (error: any, value: infer P) => void) => unknown
25
- // ? P
26
- // : never;
27
-
28
- // /** Extract the parameters of a function type in a tuple */
29
- // export type Promisified<F extends (arg1, cb: (error: unknown, value: any) => void) => any> = (
30
- // arg1: Parameter1<F>
31
- // ) => Promise<CallbackParameter2<F>>;
32
- // /** Extract the parameters of a function type in a tuple */
33
- // export type Promisified2<F extends (arg1, arg2, cb: (error: unknown, value: any) => void) => any> = (
34
- // arg1: Parameter1<F>,
35
- // arg2: Parameter2<F>
36
- // ) => Promise<CallbackParameter3<F>>;
1
+ import * as util from 'util';
37
2
 
38
3
  /** Wrapper for Node.js promisify */
39
- type Callback<A> = (error: unknown, args: A) => void;
40
-
41
- /**
42
- * Typesafe promisify implementation
43
- * @link https://dev.to/_gdelgado/implement-a-type-safe-version-of-node-s-promisify-in-7-lines-of-code-in-typescript-2j34
44
- * @param fn
45
- * @returns
46
- */
47
- export function promisify1<T, A>(fn: (args: T, cb: Callback<A>) => void): (args: T) => Promise<A> {
48
- return (args: T) =>
49
- new Promise((resolve, reject) =>
50
- fn(args, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs)))
51
- );
52
- }
53
-
54
- export function promisify2<T1, T2, A>(
55
- fn: (arg1: T1, arg2: T2, cb: Callback<A>) => void
56
- ): (arg1: T1, arg2: T2) => Promise<A> {
57
- return (arg1: T1, arg2: T2) =>
58
- new Promise((resolve, reject) =>
59
- fn(arg1, arg2, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs)))
60
- );
61
- }
62
-
63
- export function promisify3<T1, T2, T3, A>(
64
- fn: (arg1: T1, arg2: T2, arg3: T3, cb: Callback<A>) => void
65
- ): (arg1: T1, arg2: T2, arg3: T3) => Promise<A> {
66
- return (arg1: T1, arg2: T2, arg3: T3) =>
67
- new Promise((resolve, reject) =>
68
- fn(arg1, arg2, arg3, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs)))
69
- );
70
- }
4
+ export const promisify = util.promisify;
@@ -1 +0,0 @@
1
- {"version":3,"file":"dataview-copy-utils.js","names":["padStringToByteAlignment","string","byteAlignment","length","paddedLength","Math","ceil","padding","whitespace","i","copyStringToDataView","dataView","byteOffset","byteLength","setUint8","charCodeAt","copyBinaryToDataView","binary","copyPaddedArrayBufferToDataView","sourceBuffer","padToNBytes","padLength","targetArray","Uint8Array","buffer","sourceArray","set","copyPaddedStringToDataView","textEncoder","TextEncoder","stringBuffer","encode"],"sources":["../../../../src/lib/binary-utils/dataview-copy-utils.ts"],"sourcesContent":["// loaders./gl, MIT license\n\nimport {TypedArray} from '../../types';\nimport {padToNBytes} from './memory-copy-utils';\n\n/**\n * Helper function that pads a string with spaces to fit a certain byte alignment\n * @param string\n * @param byteAlignment\n * @returns\n *\n * @todo PERFORMANCE IDEA: No need to copy string twice...\n */\nexport function padStringToByteAlignment(string: string, byteAlignment: number): string {\n const length = string.length;\n const paddedLength = Math.ceil(length / byteAlignment) * byteAlignment; // Round up to the required alignment\n const padding = paddedLength - length;\n let whitespace = '';\n for (let i = 0; i < padding; ++i) {\n whitespace += ' ';\n }\n return string + whitespace;\n}\n\n/**\n *\n * @param dataView\n * @param byteOffset\n * @param string\n * @param byteLength\n * @returns\n */\nexport function copyStringToDataView(\n dataView: DataView,\n byteOffset: number,\n string: string,\n byteLength: number\n): number {\n if (dataView) {\n for (let i = 0; i < byteLength; i++) {\n dataView.setUint8(byteOffset + i, string.charCodeAt(i));\n }\n }\n return byteOffset + byteLength;\n}\n\nexport function copyBinaryToDataView(dataView, byteOffset, binary, byteLength) {\n if (dataView) {\n for (let i = 0; i < byteLength; i++) {\n dataView.setUint8(byteOffset + i, binary[i]);\n }\n }\n return byteOffset + byteLength;\n}\n\n/**\n * Copy sourceBuffer to dataView with some padding\n *\n * @param dataView - destination data container. If null - only new offset is calculated\n * @param byteOffset - destination byte offset to copy to\n * @param sourceBuffer - source data buffer\n * @param padding - pad the resulting array to multiple of \"padding\" bytes. Additional bytes are filled with 0x20 (ASCII space)\n *\n * @return new byteOffset of resulting dataView\n */\nexport function copyPaddedArrayBufferToDataView(\n dataView: DataView | null,\n byteOffset: number,\n sourceBuffer: TypedArray,\n padding: number\n): number {\n const paddedLength = padToNBytes(sourceBuffer.byteLength, padding);\n const padLength = paddedLength - sourceBuffer.byteLength;\n\n if (dataView) {\n // Copy array\n const targetArray = new Uint8Array(\n dataView.buffer,\n dataView.byteOffset + byteOffset,\n sourceBuffer.byteLength\n );\n const sourceArray = new Uint8Array(sourceBuffer);\n targetArray.set(sourceArray);\n\n // Add PADDING\n for (let i = 0; i < padLength; ++i) {\n // json chunk is padded with spaces (ASCII 0x20)\n dataView.setUint8(byteOffset + sourceBuffer.byteLength + i, 0x20);\n }\n }\n byteOffset += paddedLength;\n return byteOffset;\n}\n\n/**\n * Copy string to dataView with some padding\n *\n * @param {DataView | null} dataView - destination data container. If null - only new offset is calculated\n * @param {number} byteOffset - destination byte offset to copy to\n * @param {string} string - source string\n * @param {number} padding - pad the resulting array to multiple of \"padding\" bytes. Additional bytes are filled with 0x20 (ASCII space)\n *\n * @return new byteOffset of resulting dataView\n */\nexport function copyPaddedStringToDataView(\n dataView: DataView | null,\n byteOffset: number,\n string: string,\n padding: number\n): number {\n const textEncoder = new TextEncoder();\n // PERFORMANCE IDEA: We encode twice, once to get size and once to store\n // PERFORMANCE IDEA: Use TextEncoder.encodeInto() to avoid temporary copy\n const stringBuffer = textEncoder.encode(string);\n\n byteOffset = copyPaddedArrayBufferToDataView(dataView, byteOffset, stringBuffer, padding);\n\n return byteOffset;\n}\n"],"mappings":";;;;;;;;;;AAGA;;AAUO,SAASA,wBAAwB,CAACC,MAAc,EAAEC,aAAqB,EAAU;EACtF,IAAMC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC5B,IAAMC,YAAY,GAAGC,IAAI,CAACC,IAAI,CAACH,MAAM,GAAGD,aAAa,CAAC,GAAGA,aAAa;EACtE,IAAMK,OAAO,GAAGH,YAAY,GAAGD,MAAM;EACrC,IAAIK,UAAU,GAAG,EAAE;EACnB,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGF,OAAO,EAAE,EAAEE,CAAC,EAAE;IAChCD,UAAU,IAAI,GAAG;EACnB;EACA,OAAOP,MAAM,GAAGO,UAAU;AAC5B;;AAUO,SAASE,oBAAoB,CAClCC,QAAkB,EAClBC,UAAkB,EAClBX,MAAc,EACdY,UAAkB,EACV;EACR,IAAIF,QAAQ,EAAE;IACZ,KAAK,IAAIF,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGI,UAAU,EAAEJ,CAAC,EAAE,EAAE;MACnCE,QAAQ,CAACG,QAAQ,CAACF,UAAU,GAAGH,CAAC,EAAER,MAAM,CAACc,UAAU,CAACN,CAAC,CAAC,CAAC;IACzD;EACF;EACA,OAAOG,UAAU,GAAGC,UAAU;AAChC;AAEO,SAASG,oBAAoB,CAACL,QAAQ,EAAEC,UAAU,EAAEK,MAAM,EAAEJ,UAAU,EAAE;EAC7E,IAAIF,QAAQ,EAAE;IACZ,KAAK,IAAIF,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGI,UAAU,EAAEJ,CAAC,EAAE,EAAE;MACnCE,QAAQ,CAACG,QAAQ,CAACF,UAAU,GAAGH,CAAC,EAAEQ,MAAM,CAACR,CAAC,CAAC,CAAC;IAC9C;EACF;EACA,OAAOG,UAAU,GAAGC,UAAU;AAChC;;AAYO,SAASK,+BAA+B,CAC7CP,QAAyB,EACzBC,UAAkB,EAClBO,YAAwB,EACxBZ,OAAe,EACP;EACR,IAAMH,YAAY,GAAG,IAAAgB,4BAAW,EAACD,YAAY,CAACN,UAAU,EAAEN,OAAO,CAAC;EAClE,IAAMc,SAAS,GAAGjB,YAAY,GAAGe,YAAY,CAACN,UAAU;EAExD,IAAIF,QAAQ,EAAE;IAEZ,IAAMW,WAAW,GAAG,IAAIC,UAAU,CAChCZ,QAAQ,CAACa,MAAM,EACfb,QAAQ,CAACC,UAAU,GAAGA,UAAU,EAChCO,YAAY,CAACN,UAAU,CACxB;IACD,IAAMY,WAAW,GAAG,IAAIF,UAAU,CAACJ,YAAY,CAAC;IAChDG,WAAW,CAACI,GAAG,CAACD,WAAW,CAAC;;IAG5B,KAAK,IAAIhB,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGY,SAAS,EAAE,EAAEZ,CAAC,EAAE;MAElCE,QAAQ,CAACG,QAAQ,CAACF,UAAU,GAAGO,YAAY,CAACN,UAAU,GAAGJ,CAAC,EAAE,IAAI,CAAC;IACnE;EACF;EACAG,UAAU,IAAIR,YAAY;EAC1B,OAAOQ,UAAU;AACnB;;AAYO,SAASe,0BAA0B,CACxChB,QAAyB,EACzBC,UAAkB,EAClBX,MAAc,EACdM,OAAe,EACP;EACR,IAAMqB,WAAW,GAAG,IAAIC,WAAW,EAAE;EAGrC,IAAMC,YAAY,GAAGF,WAAW,CAACG,MAAM,CAAC9B,MAAM,CAAC;EAE/CW,UAAU,GAAGM,+BAA+B,CAACP,QAAQ,EAAEC,UAAU,EAAEkB,YAAY,EAAEvB,OAAO,CAAC;EAEzF,OAAOK,UAAU;AACnB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"memory-conversion-utils.js","names":["isBuffer","value","toBuffer","data","node","toArrayBuffer","ArrayBuffer","isView","byteOffset","byteLength","buffer","slice","text","uint8Array","TextEncoder","encode","_toArrayBuffer","Error"],"sources":["../../../../src/lib/binary-utils/memory-conversion-utils.ts"],"sourcesContent":["// loaders.gl, MIT license\n\nimport * as node from '../node/buffer';\n\n/**\n * Check for Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)\n */\nexport function isBuffer(value: any): boolean {\n return value && typeof value === 'object' && value.isBuffer;\n}\n\n/**\n * Converts to Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)\n * @todo better data type\n */\nexport function toBuffer(data: any): Buffer {\n return node.toBuffer ? node.toBuffer(data) : data;\n}\n\n/**\n * Convert an object to an array buffer\n */\nexport function toArrayBuffer(data: unknown): ArrayBuffer {\n // Note: Should be called first, Buffers can trigger other detections below\n if (isBuffer(data)) {\n return node.toArrayBuffer(data);\n }\n\n if (data instanceof ArrayBuffer) {\n return data;\n }\n\n // Careful - Node Buffers look like Uint8Arrays (keep after isBuffer)\n if (ArrayBuffer.isView(data)) {\n if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {\n return data.buffer;\n }\n return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);\n }\n\n if (typeof data === 'string') {\n const text = data;\n const uint8Array = new TextEncoder().encode(text);\n return uint8Array.buffer;\n }\n\n // HACK to support Blob polyfill\n if (data && typeof data === 'object' && (data as any)._toArrayBuffer) {\n return (data as any)._toArrayBuffer();\n }\n\n throw new Error('toArrayBuffer');\n}\n"],"mappings":";;;;;;;;;;;AAEA;AAAuC;AAAA;;AAKhC,SAASA,QAAQ,CAACC,KAAU,EAAW;EAC5C,OAAOA,KAAK,IAAI,sBAAOA,KAAK,MAAK,QAAQ,IAAIA,KAAK,CAACD,QAAQ;AAC7D;;AAMO,SAASE,QAAQ,CAACC,IAAS,EAAU;EAC1C,OAAOC,IAAI,CAACF,QAAQ,GAAGE,IAAI,CAACF,QAAQ,CAACC,IAAI,CAAC,GAAGA,IAAI;AACnD;;AAKO,SAASE,aAAa,CAACF,IAAa,EAAe;EAExD,IAAIH,QAAQ,CAACG,IAAI,CAAC,EAAE;IAClB,OAAOC,IAAI,CAACC,aAAa,CAACF,IAAI,CAAC;EACjC;EAEA,IAAIA,IAAI,YAAYG,WAAW,EAAE;IAC/B,OAAOH,IAAI;EACb;;EAGA,IAAIG,WAAW,CAACC,MAAM,CAACJ,IAAI,CAAC,EAAE;IAC5B,IAAIA,IAAI,CAACK,UAAU,KAAK,CAAC,IAAIL,IAAI,CAACM,UAAU,KAAKN,IAAI,CAACO,MAAM,CAACD,UAAU,EAAE;MACvE,OAAON,IAAI,CAACO,MAAM;IACpB;IACA,OAAOP,IAAI,CAACO,MAAM,CAACC,KAAK,CAACR,IAAI,CAACK,UAAU,EAAEL,IAAI,CAACK,UAAU,GAAGL,IAAI,CAACM,UAAU,CAAC;EAC9E;EAEA,IAAI,OAAON,IAAI,KAAK,QAAQ,EAAE;IAC5B,IAAMS,IAAI,GAAGT,IAAI;IACjB,IAAMU,UAAU,GAAG,IAAIC,WAAW,EAAE,CAACC,MAAM,CAACH,IAAI,CAAC;IACjD,OAAOC,UAAU,CAACH,MAAM;EAC1B;;EAGA,IAAIP,IAAI,IAAI,sBAAOA,IAAI,MAAK,QAAQ,IAAKA,IAAI,CAASa,cAAc,EAAE;IACpE,OAAQb,IAAI,CAASa,cAAc,EAAE;EACvC;EAEA,MAAM,IAAIC,KAAK,CAAC,eAAe,CAAC;AAClC"}