@loaders.gl/loader-utils 3.3.0-alpha.8 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es5/index.js +36 -22
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/json-loader.js +1 -1
- package/dist/es5/json-loader.js.map +1 -1
- package/dist/es5/lib/binary-utils/array-buffer-utils.js +0 -30
- package/dist/es5/lib/binary-utils/array-buffer-utils.js.map +1 -1
- package/dist/es5/lib/binary-utils/{binary-copy-utils.js → dataview-copy-utils.js} +33 -1
- package/dist/es5/lib/binary-utils/dataview-copy-utils.js.map +1 -0
- package/dist/es5/lib/binary-utils/get-first-characters.js +2 -0
- package/dist/es5/lib/binary-utils/get-first-characters.js.map +1 -1
- package/dist/es5/lib/binary-utils/{buffer-utils.js → memory-conversion-utils.js} +26 -7
- package/dist/es5/lib/binary-utils/memory-conversion-utils.js.map +1 -0
- package/dist/es5/lib/filesystems/readable-file.js +100 -0
- package/dist/es5/lib/filesystems/readable-file.js.map +1 -0
- package/dist/es5/lib/filesystems/writable-file.js +89 -0
- package/dist/es5/lib/filesystems/writable-file.js.map +1 -0
- package/dist/es5/lib/loader-utils/buffer-utils.js +19 -0
- package/dist/es5/lib/loader-utils/buffer-utils.js.map +1 -0
- package/dist/es5/lib/node/buffer.js +5 -8
- package/dist/es5/lib/node/buffer.js.map +1 -1
- package/dist/es5/lib/node/fs.js +18 -9
- package/dist/es5/lib/node/fs.js.map +1 -1
- package/dist/es5/lib/node/stream.js +20 -0
- package/dist/es5/lib/node/stream.js.map +1 -0
- package/dist/es5/lib/node/util.js +31 -7
- package/dist/es5/lib/node/util.js.map +1 -1
- package/dist/esm/index.js +11 -9
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/json-loader.js +1 -1
- package/dist/esm/json-loader.js.map +1 -1
- package/dist/esm/lib/binary-utils/array-buffer-utils.js +0 -27
- package/dist/esm/lib/binary-utils/array-buffer-utils.js.map +1 -1
- package/dist/esm/lib/binary-utils/{binary-copy-utils.js → dataview-copy-utils.js} +31 -1
- package/dist/esm/lib/binary-utils/dataview-copy-utils.js.map +1 -0
- package/dist/esm/lib/binary-utils/get-first-characters.js +3 -0
- package/dist/esm/lib/binary-utils/get-first-characters.js.map +1 -1
- package/dist/esm/lib/binary-utils/memory-conversion-utils.js +38 -0
- package/dist/esm/lib/binary-utils/memory-conversion-utils.js.map +1 -0
- package/dist/esm/lib/filesystems/readable-file.js +22 -0
- package/dist/esm/lib/filesystems/readable-file.js.map +1 -0
- package/dist/esm/lib/filesystems/writable-file.js +21 -0
- package/dist/esm/lib/filesystems/writable-file.js.map +1 -0
- package/dist/esm/lib/loader-utils/buffer-utils.js +13 -0
- package/dist/esm/lib/loader-utils/buffer-utils.js.map +1 -0
- package/dist/esm/lib/node/buffer.js +5 -9
- package/dist/esm/lib/node/buffer.js.map +1 -1
- package/dist/esm/lib/node/fs.js +11 -9
- package/dist/esm/lib/node/fs.js.map +1 -1
- package/dist/esm/lib/node/stream.js +11 -0
- package/dist/esm/lib/node/stream.js.map +1 -0
- package/dist/esm/lib/node/util.js +10 -2
- package/dist/esm/lib/node/util.js.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -23
- package/dist/lib/binary-utils/array-buffer-utils.d.ts +4 -8
- package/dist/lib/binary-utils/array-buffer-utils.d.ts.map +1 -1
- package/dist/lib/binary-utils/array-buffer-utils.js +5 -36
- package/dist/lib/binary-utils/dataview-copy-utils.d.ts +43 -0
- package/dist/lib/binary-utils/dataview-copy-utils.d.ts.map +1 -0
- package/dist/lib/binary-utils/{binary-copy-utils.js → dataview-copy-utils.js} +51 -5
- package/dist/lib/binary-utils/get-first-characters.d.ts +15 -1
- package/dist/lib/binary-utils/get-first-characters.d.ts.map +1 -1
- package/dist/lib/binary-utils/get-first-characters.js +15 -0
- package/dist/lib/binary-utils/{buffer-utils.d.ts → memory-conversion-utils.d.ts} +3 -4
- package/dist/lib/binary-utils/memory-conversion-utils.d.ts.map +1 -0
- package/dist/lib/binary-utils/{buffer-utils.js → memory-conversion-utils.js} +28 -9
- package/dist/lib/filesystems/readable-file.d.ts +10 -0
- package/dist/lib/filesystems/readable-file.d.ts.map +1 -0
- package/dist/lib/filesystems/readable-file.js +25 -0
- package/dist/lib/filesystems/writable-file.d.ts +18 -0
- package/dist/lib/filesystems/writable-file.d.ts.map +1 -0
- package/dist/lib/filesystems/writable-file.js +48 -0
- package/dist/lib/loader-utils/buffer-utils.d.ts +10 -0
- package/dist/lib/loader-utils/buffer-utils.d.ts.map +1 -0
- package/dist/lib/loader-utils/buffer-utils.js +22 -0
- package/dist/lib/node/buffer.d.ts +3 -1
- package/dist/lib/node/buffer.d.ts.map +1 -1
- package/dist/lib/node/buffer.js +7 -8
- package/dist/lib/node/fs.d.ts +4 -2
- package/dist/lib/node/fs.d.ts.map +1 -1
- package/dist/lib/node/fs.js +10 -9
- package/dist/lib/node/stream.d.ts +4 -0
- package/dist/lib/node/stream.d.ts.map +1 -0
- package/dist/lib/node/stream.js +17 -0
- package/dist/lib/node/util.d.ts +11 -3
- package/dist/lib/node/util.d.ts.map +1 -1
- package/dist/lib/node/util.js +20 -27
- package/dist/lib/worker-loader-utils/create-loader-worker.js +1 -1
- package/package.json +5 -4
- package/src/index.ts +22 -18
- package/src/lib/binary-utils/array-buffer-utils.ts +4 -40
- package/src/lib/binary-utils/{binary-copy-utils.ts → dataview-copy-utils.ts} +57 -5
- package/src/lib/binary-utils/get-first-characters.ts +17 -1
- package/src/lib/binary-utils/memory-conversion-utils.ts +53 -0
- package/src/lib/filesystems/readable-file.ts +30 -0
- package/src/lib/filesystems/writable-file.ts +44 -0
- package/src/lib/loader-utils/buffer-utils.ts +18 -0
- package/src/lib/node/buffer.ts +9 -10
- package/src/lib/node/fs.ts +16 -11
- package/src/lib/node/stream.ts +18 -0
- package/src/lib/node/util.ts +68 -2
- package/dist/es5/lib/binary-utils/binary-copy-utils.js.map +0 -1
- package/dist/es5/lib/binary-utils/buffer-utils.js.map +0 -1
- package/dist/es5/lib/binary-utils/encode-utils.js +0 -36
- package/dist/es5/lib/binary-utils/encode-utils.js.map +0 -1
- package/dist/esm/lib/binary-utils/binary-copy-utils.js.map +0 -1
- package/dist/esm/lib/binary-utils/buffer-utils.js +0 -18
- package/dist/esm/lib/binary-utils/buffer-utils.js.map +0 -1
- package/dist/esm/lib/binary-utils/encode-utils.js +0 -29
- package/dist/esm/lib/binary-utils/encode-utils.js.map +0 -1
- package/dist/lib/binary-utils/binary-copy-utils.d.ts +0 -24
- package/dist/lib/binary-utils/binary-copy-utils.d.ts.map +0 -1
- package/dist/lib/binary-utils/buffer-utils.d.ts.map +0 -1
- package/dist/lib/binary-utils/encode-utils.d.ts +0 -4
- package/dist/lib/binary-utils/encode-utils.d.ts.map +0 -1
- package/dist/lib/binary-utils/encode-utils.js +0 -35
- package/src/lib/binary-utils/buffer-utils.ts +0 -28
- package/src/lib/binary-utils/encode-utils.ts +0 -32
package/src/index.ts
CHANGED
|
@@ -35,22 +35,19 @@ export {parseJSON} from './lib/parser-utils/parse-json';
|
|
|
35
35
|
|
|
36
36
|
// MEMORY COPY UTILS
|
|
37
37
|
export {
|
|
38
|
-
toArrayBuffer,
|
|
39
38
|
sliceArrayBuffer,
|
|
40
39
|
concatenateArrayBuffers,
|
|
41
40
|
concatenateTypedArrays,
|
|
42
41
|
compareArrayBuffers
|
|
43
42
|
} from './lib/binary-utils/array-buffer-utils';
|
|
44
43
|
export {padToNBytes, copyToArray, copyArrayBuffer} from './lib/binary-utils/memory-copy-utils';
|
|
45
|
-
export {
|
|
46
|
-
copyPaddedArrayBufferToDataView,
|
|
47
|
-
copyPaddedStringToDataView
|
|
48
|
-
} from './lib/binary-utils/binary-copy-utils';
|
|
49
44
|
export {
|
|
50
45
|
padStringToByteAlignment,
|
|
51
46
|
copyStringToDataView,
|
|
52
|
-
copyBinaryToDataView
|
|
53
|
-
|
|
47
|
+
copyBinaryToDataView,
|
|
48
|
+
copyPaddedArrayBufferToDataView,
|
|
49
|
+
copyPaddedStringToDataView
|
|
50
|
+
} from './lib/binary-utils/dataview-copy-utils';
|
|
54
51
|
export {getFirstCharacters, getMagicString} from './lib/binary-utils/get-first-characters';
|
|
55
52
|
|
|
56
53
|
// ITERATOR UTILS
|
|
@@ -76,24 +73,31 @@ export {JSONLoader} from './json-loader';
|
|
|
76
73
|
|
|
77
74
|
// Node.js emulation (can be used in browser)
|
|
78
75
|
|
|
79
|
-
// `path` replacement (avoids bundling big path polyfill)
|
|
80
|
-
import * as path from './lib/path-utils/path';
|
|
81
|
-
export {path};
|
|
82
|
-
|
|
83
76
|
// Avoid direct use of `Buffer` which pulls in 50KB polyfill
|
|
84
|
-
export {isBuffer, toBuffer,
|
|
77
|
+
export {isBuffer, toBuffer, toArrayBuffer} from './lib/binary-utils/memory-conversion-utils';
|
|
85
78
|
|
|
86
79
|
// Note.js wrappers (can be safely imported, but not used in browser)
|
|
87
80
|
|
|
88
|
-
// Use instead of importing 'util'
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
|
|
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};
|
|
93
87
|
|
|
94
|
-
// Use instead of importing 'fs'
|
|
88
|
+
// Use instead of importing 'fs' to avoid node dependencies`
|
|
95
89
|
import * as fs from './lib/node/fs';
|
|
96
90
|
export {fs};
|
|
97
91
|
|
|
92
|
+
// Use instead of importing 'stream' to avoid node dependencies`
|
|
93
|
+
import * as stream from './lib/node/stream';
|
|
94
|
+
export {stream};
|
|
95
|
+
|
|
98
96
|
// 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
|
+
|
|
99
103
|
export {default as _NodeFileSystem} from './lib/filesystems/node-filesystem';
|
|
@@ -1,46 +1,10 @@
|
|
|
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
|
-
}
|
|
38
2
|
|
|
39
3
|
/**
|
|
40
4
|
* compare two binary arrays for equality
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
* @param
|
|
5
|
+
* @param a
|
|
6
|
+
* @param b
|
|
7
|
+
* @param byteLength
|
|
44
8
|
*/
|
|
45
9
|
export function compareArrayBuffers(
|
|
46
10
|
arrayBuffer1: ArrayBuffer,
|
|
@@ -91,7 +55,7 @@ export function concatenateArrayBuffers(...sources: (ArrayBuffer | Uint8Array)[]
|
|
|
91
55
|
/**
|
|
92
56
|
* Concatenate arbitrary count of typed arrays
|
|
93
57
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
|
|
94
|
-
* @param
|
|
58
|
+
* @param - list of arrays. All arrays should be the same type
|
|
95
59
|
* @return A concatenated TypedArray
|
|
96
60
|
*/
|
|
97
61
|
export function concatenateTypedArrays<T>(...typedArrays: T[]): T {
|
|
@@ -1,13 +1,65 @@
|
|
|
1
|
+
// loaders./gl, MIT license
|
|
2
|
+
|
|
1
3
|
import {TypedArray} from '../../types';
|
|
2
4
|
import {padToNBytes} from './memory-copy-utils';
|
|
3
5
|
|
|
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
|
+
|
|
4
56
|
/**
|
|
5
57
|
* Copy sourceBuffer to dataView with some padding
|
|
6
58
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
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)
|
|
11
63
|
*
|
|
12
64
|
* @return new byteOffset of resulting dataView
|
|
13
65
|
*/
|
|
@@ -16,7 +68,7 @@ export function copyPaddedArrayBufferToDataView(
|
|
|
16
68
|
byteOffset: number,
|
|
17
69
|
sourceBuffer: TypedArray,
|
|
18
70
|
padding: number
|
|
19
|
-
) {
|
|
71
|
+
): number {
|
|
20
72
|
const paddedLength = padToNBytes(sourceBuffer.byteLength, padding);
|
|
21
73
|
const padLength = paddedLength - sourceBuffer.byteLength;
|
|
22
74
|
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
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 {
|
|
2
10
|
if (typeof data === 'string') {
|
|
3
11
|
return data.slice(0, length);
|
|
4
12
|
} else if (ArrayBuffer.isView(data)) {
|
|
@@ -11,6 +19,14 @@ export function getFirstCharacters(data, length = 5): string {
|
|
|
11
19
|
return '';
|
|
12
20
|
}
|
|
13
21
|
|
|
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
|
+
*/
|
|
14
30
|
export function getMagicString(
|
|
15
31
|
arrayBuffer: ArrayBuffer,
|
|
16
32
|
byteOffset: number,
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
import * as node from '../node/buffer';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Check for Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)
|
|
7
|
+
*/
|
|
8
|
+
export function isBuffer(value: any): boolean {
|
|
9
|
+
return value && typeof value === 'object' && value.isBuffer;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Converts to Node.js `Buffer` (without triggering bundler to include Buffer polyfill on browser)
|
|
14
|
+
* @todo better data type
|
|
15
|
+
*/
|
|
16
|
+
export function toBuffer(data: any): Buffer {
|
|
17
|
+
return node.toBuffer ? node.toBuffer(data) : data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Convert an object to an array buffer
|
|
22
|
+
*/
|
|
23
|
+
export function toArrayBuffer(data: unknown): ArrayBuffer {
|
|
24
|
+
// Note: Should be called first, Buffers can trigger other detections below
|
|
25
|
+
if (isBuffer(data)) {
|
|
26
|
+
return node.toArrayBuffer(data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (data instanceof ArrayBuffer) {
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Careful - Node Buffers look like Uint8Arrays (keep after isBuffer)
|
|
34
|
+
if (ArrayBuffer.isView(data)) {
|
|
35
|
+
if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
|
|
36
|
+
return data.buffer;
|
|
37
|
+
}
|
|
38
|
+
return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (typeof data === 'string') {
|
|
42
|
+
const text = data;
|
|
43
|
+
const uint8Array = new TextEncoder().encode(text);
|
|
44
|
+
return uint8Array.buffer;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// HACK to support Blob polyfill
|
|
48
|
+
if (data && typeof data === 'object' && (data as any)._toArrayBuffer) {
|
|
49
|
+
return (data as any)._toArrayBuffer();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new Error('toArrayBuffer');
|
|
53
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
export type ReadableFile = {
|
|
4
|
+
read: (position: number, length: number) => Promise<Buffer>;
|
|
5
|
+
close: () => Promise<void>;
|
|
6
|
+
/** Length of file in bytes */
|
|
7
|
+
size: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** Helper function to create an envelope reader for a binary memory input */
|
|
11
|
+
export function makeReadableFile(data: Blob | ArrayBuffer): ReadableFile {
|
|
12
|
+
if (data instanceof ArrayBuffer) {
|
|
13
|
+
const arrayBuffer: ArrayBuffer = data;
|
|
14
|
+
return {
|
|
15
|
+
read: async (start: number, length: number) => Buffer.from(data, start, length),
|
|
16
|
+
close: async () => {},
|
|
17
|
+
size: arrayBuffer.byteLength
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const blob: Blob = data;
|
|
22
|
+
return {
|
|
23
|
+
read: async (start: number, length: number) => {
|
|
24
|
+
const arrayBuffer = await blob.slice(start, start + length).arrayBuffer();
|
|
25
|
+
return Buffer.from(arrayBuffer);
|
|
26
|
+
},
|
|
27
|
+
close: async () => {},
|
|
28
|
+
size: blob.size
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Forked from https://github.com/kbajalc/parquets under MIT license (Copyright (c) 2017 ironSource Ltd.)
|
|
2
|
+
import {isBrowser} from '@loaders.gl/loader-utils';
|
|
3
|
+
import * as fs from '../node/fs';
|
|
4
|
+
import type {Writable} from 'stream';
|
|
5
|
+
|
|
6
|
+
export type WritableFile = {
|
|
7
|
+
write: (buf: Buffer) => Promise<void>;
|
|
8
|
+
close: () => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface WriteStreamOptions {
|
|
12
|
+
flags?: string;
|
|
13
|
+
encoding?: 'utf8';
|
|
14
|
+
fd?: number;
|
|
15
|
+
mode?: number;
|
|
16
|
+
autoClose?: boolean;
|
|
17
|
+
start?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Helper function to create an envelope reader for a binary memory input */
|
|
21
|
+
export function makeWritableFile(
|
|
22
|
+
pathOrStream: string | Writable,
|
|
23
|
+
options?: WriteStreamOptions
|
|
24
|
+
): WritableFile {
|
|
25
|
+
if (isBrowser) {
|
|
26
|
+
return {
|
|
27
|
+
write: async () => {},
|
|
28
|
+
close: async () => {}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const outputStream: Writable =
|
|
33
|
+
typeof pathOrStream === 'string' ? fs.createWriteStream(pathOrStream, options) : pathOrStream;
|
|
34
|
+
return {
|
|
35
|
+
write: async (buffer: Buffer) =>
|
|
36
|
+
new Promise((resolve, reject) => {
|
|
37
|
+
outputStream.write(buffer, (err) => (err ? reject(err) : resolve()));
|
|
38
|
+
}),
|
|
39
|
+
close: () =>
|
|
40
|
+
new Promise((resolve, reject) => {
|
|
41
|
+
(outputStream as any).close((err) => (err ? reject(err) : resolve()));
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert Buffer to ArrayBuffer
|
|
3
|
+
*/
|
|
4
|
+
export function toArrayBuffer(buffer: Buffer): ArrayBuffer {
|
|
5
|
+
// TODO - per docs we should just be able to call buffer.buffer, but there are issues
|
|
6
|
+
if (Buffer.isBuffer(buffer)) {
|
|
7
|
+
const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
|
|
8
|
+
return typedArray.slice().buffer;
|
|
9
|
+
}
|
|
10
|
+
return buffer;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Convert (copy) ArrayBuffer to Buffer
|
|
15
|
+
*/
|
|
16
|
+
export function toBuffer(arrayBuffer: ArrayBuffer): Buffer {
|
|
17
|
+
return Buffer.from(arrayBuffer);
|
|
18
|
+
}
|
package/src/lib/node/buffer.ts
CHANGED
|
@@ -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
|
-
|
|
6
4
|
/**
|
|
7
5
|
* 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,19 +18,18 @@ export function toArrayBuffer(buffer) {
|
|
|
18
18
|
/**
|
|
19
19
|
* Convert (copy) ArrayBuffer to Buffer
|
|
20
20
|
*/
|
|
21
|
-
export function toBuffer(binaryData) {
|
|
21
|
+
export function toBuffer(binaryData: ArrayBuffer | ArrayBuffer | Buffer): Buffer {
|
|
22
|
+
if (Buffer.isBuffer(binaryData)) {
|
|
23
|
+
return binaryData;
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
if (ArrayBuffer.isView(binaryData)) {
|
|
23
27
|
binaryData = binaryData.buffer;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
|
|
27
|
-
|
|
28
|
-
const view = new Uint8Array(binaryData);
|
|
29
|
-
for (let i = 0; i < buffer.length; ++i) {
|
|
30
|
-
buffer[i] = view[i];
|
|
31
|
-
}
|
|
32
|
-
return buffer;
|
|
31
|
+
return Buffer.from(binaryData);
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
throw new Error('toBuffer');
|
|
36
35
|
}
|
package/src/lib/node/fs.ts
CHANGED
|
@@ -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 {
|
|
4
|
+
import {promisify2, promisify3} from './util';
|
|
5
5
|
|
|
6
|
-
export type {Stats} from 'fs';
|
|
6
|
+
export type {Stats, WriteStream} from 'fs';
|
|
7
7
|
|
|
8
8
|
export let readdir;
|
|
9
9
|
/** Wrapper for Node.js fs method */
|
|
@@ -23,41 +23,46 @@ 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
|
|
26
|
+
export let close: (fd: number) => Promise<void>;
|
|
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
|
+
|
|
32
34
|
export let isSupported = Boolean(fs);
|
|
33
35
|
|
|
34
36
|
// paths
|
|
35
37
|
|
|
36
38
|
try {
|
|
37
39
|
/** Wrapper for Node.js fs method */
|
|
38
|
-
readdir =
|
|
40
|
+
readdir = promisify2(fs.readdir);
|
|
39
41
|
/** Wrapper for Node.js fs method */
|
|
40
|
-
stat =
|
|
42
|
+
stat = promisify2(fs.stat);
|
|
41
43
|
|
|
42
44
|
/** Wrapper for Node.js fs method */
|
|
43
|
-
readFile =
|
|
45
|
+
readFile = fs.readFile;
|
|
44
46
|
/** Wrapper for Node.js fs method */
|
|
45
47
|
readFileSync = fs.readFileSync;
|
|
46
48
|
/** Wrapper for Node.js fs method */
|
|
47
|
-
writeFile =
|
|
49
|
+
writeFile = promisify3(fs.writeFile);
|
|
48
50
|
/** Wrapper for Node.js fs method */
|
|
49
51
|
writeFileSync = fs.writeFileSync;
|
|
50
52
|
|
|
51
53
|
// file descriptors
|
|
52
54
|
|
|
53
55
|
/** Wrapper for Node.js fs method */
|
|
54
|
-
open =
|
|
56
|
+
open = fs.open;
|
|
55
57
|
/** Wrapper for Node.js fs method */
|
|
56
|
-
close =
|
|
58
|
+
close = (fd: number) =>
|
|
59
|
+
new Promise((resolve, reject) => fs.close(fd, (err) => (err ? reject(err) : resolve())));
|
|
57
60
|
/** Wrapper for Node.js fs method */
|
|
58
|
-
read =
|
|
61
|
+
read = fs.read;
|
|
59
62
|
/** Wrapper for Node.js fs method */
|
|
60
|
-
fstat =
|
|
63
|
+
fstat = fs.fstat;
|
|
64
|
+
|
|
65
|
+
createWriteStream = fs.createWriteStream;
|
|
61
66
|
|
|
62
67
|
isSupported = Boolean(fs);
|
|
63
68
|
} catch {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
import stream from 'stream';
|
|
4
|
+
|
|
5
|
+
export type {Writable} from 'stream';
|
|
6
|
+
|
|
7
|
+
export let Transform;
|
|
8
|
+
|
|
9
|
+
export const isSupported = Boolean(stream);
|
|
10
|
+
|
|
11
|
+
// paths
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
/** Wrapper for Node.js fs method */
|
|
15
|
+
Transform = stream.Transform;
|
|
16
|
+
} catch {
|
|
17
|
+
// ignore
|
|
18
|
+
}
|
package/src/lib/node/util.ts
CHANGED
|
@@ -1,4 +1,70 @@
|
|
|
1
|
-
|
|
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>>;
|
|
2
37
|
|
|
3
38
|
/** Wrapper for Node.js promisify */
|
|
4
|
-
|
|
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
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"binary-copy-utils.js","names":["copyPaddedArrayBufferToDataView","dataView","byteOffset","sourceBuffer","padding","paddedLength","padToNBytes","byteLength","padLength","targetArray","Uint8Array","buffer","sourceArray","set","i","setUint8","copyPaddedStringToDataView","string","textEncoder","TextEncoder","stringBuffer","encode"],"sources":["../../../../src/lib/binary-utils/binary-copy-utils.ts"],"sourcesContent":["import {TypedArray} from '../../types';\nimport {padToNBytes} from './memory-copy-utils';\n\n/**\n * Copy sourceBuffer 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 {Array | TypedArray} sourceBuffer - source data buffer\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 copyPaddedArrayBufferToDataView(\n dataView: DataView | null,\n byteOffset: number,\n sourceBuffer: TypedArray,\n padding: number\n) {\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":";;;;;;;AACA;AAYO,SAASA,+BAA+B,CAC7CC,QAAyB,EACzBC,UAAkB,EAClBC,YAAwB,EACxBC,OAAe,EACf;EACA,IAAMC,YAAY,GAAG,IAAAC,4BAAW,EAACH,YAAY,CAACI,UAAU,EAAEH,OAAO,CAAC;EAClE,IAAMI,SAAS,GAAGH,YAAY,GAAGF,YAAY,CAACI,UAAU;EAExD,IAAIN,QAAQ,EAAE;IAEZ,IAAMQ,WAAW,GAAG,IAAIC,UAAU,CAChCT,QAAQ,CAACU,MAAM,EACfV,QAAQ,CAACC,UAAU,GAAGA,UAAU,EAChCC,YAAY,CAACI,UAAU,CACxB;IACD,IAAMK,WAAW,GAAG,IAAIF,UAAU,CAACP,YAAY,CAAC;IAChDM,WAAW,CAACI,GAAG,CAACD,WAAW,CAAC;;IAG5B,KAAK,IAAIE,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGN,SAAS,EAAE,EAAEM,CAAC,EAAE;MAElCb,QAAQ,CAACc,QAAQ,CAACb,UAAU,GAAGC,YAAY,CAACI,UAAU,GAAGO,CAAC,EAAE,IAAI,CAAC;IACnE;EACF;EACAZ,UAAU,IAAIG,YAAY;EAC1B,OAAOH,UAAU;AACnB;;AAYO,SAASc,0BAA0B,CACxCf,QAAyB,EACzBC,UAAkB,EAClBe,MAAc,EACdb,OAAe,EACP;EACR,IAAMc,WAAW,GAAG,IAAIC,WAAW,EAAE;EAGrC,IAAMC,YAAY,GAAGF,WAAW,CAACG,MAAM,CAACJ,MAAM,CAAC;EAE/Cf,UAAU,GAAGF,+BAA+B,CAACC,QAAQ,EAAEC,UAAU,EAAEkB,YAAY,EAAEhB,OAAO,CAAC;EAEzF,OAAOF,UAAU;AACnB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-utils.js","names":["isBuffer","value","toBuffer","data","node","bufferToArrayBuffer","buffer","typedArray","Uint8Array","byteOffset","length","slice"],"sources":["../../../../src/lib/binary-utils/buffer-utils.ts"],"sourcesContent":["import * 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 * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)\n * @todo better data type\n */\nexport function bufferToArrayBuffer(buffer: any): ArrayBuffer {\n if (isBuffer(buffer)) {\n const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);\n return typedArray.slice().buffer;\n }\n return buffer;\n}\n"],"mappings":";;;;;;;;;;;AAAA;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;;AAMO,SAASE,mBAAmB,CAACC,MAAW,EAAe;EAC5D,IAAIN,QAAQ,CAACM,MAAM,CAAC,EAAE;IACpB,IAAMC,UAAU,GAAG,IAAIC,UAAU,CAACF,MAAM,CAACA,MAAM,EAAEA,MAAM,CAACG,UAAU,EAAEH,MAAM,CAACI,MAAM,CAAC;IAClF,OAAOH,UAAU,CAACI,KAAK,EAAE,CAACL,MAAM;EAClC;EACA,OAAOA,MAAM;AACf"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.copyBinaryToDataView = copyBinaryToDataView;
|
|
7
|
-
exports.copyStringToDataView = copyStringToDataView;
|
|
8
|
-
exports.padStringToByteAlignment = padStringToByteAlignment;
|
|
9
|
-
|
|
10
|
-
function padStringToByteAlignment(string, byteAlignment) {
|
|
11
|
-
var length = string.length;
|
|
12
|
-
var paddedLength = Math.ceil(length / byteAlignment) * byteAlignment;
|
|
13
|
-
var padding = paddedLength - length;
|
|
14
|
-
var whitespace = '';
|
|
15
|
-
for (var i = 0; i < padding; ++i) {
|
|
16
|
-
whitespace += ' ';
|
|
17
|
-
}
|
|
18
|
-
return string + whitespace;
|
|
19
|
-
}
|
|
20
|
-
function copyStringToDataView(dataView, byteOffset, string, byteLength) {
|
|
21
|
-
if (dataView) {
|
|
22
|
-
for (var i = 0; i < byteLength; i++) {
|
|
23
|
-
dataView.setUint8(byteOffset + i, string.charCodeAt(i));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return byteOffset + byteLength;
|
|
27
|
-
}
|
|
28
|
-
function copyBinaryToDataView(dataView, byteOffset, binary, byteLength) {
|
|
29
|
-
if (dataView) {
|
|
30
|
-
for (var i = 0; i < byteLength; i++) {
|
|
31
|
-
dataView.setUint8(byteOffset + i, binary[i]);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return byteOffset + byteLength;
|
|
35
|
-
}
|
|
36
|
-
//# sourceMappingURL=encode-utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encode-utils.js","names":["padStringToByteAlignment","string","byteAlignment","length","paddedLength","Math","ceil","padding","whitespace","i","copyStringToDataView","dataView","byteOffset","byteLength","setUint8","charCodeAt","copyBinaryToDataView","binary"],"sources":["../../../../src/lib/binary-utils/encode-utils.ts"],"sourcesContent":["// Note: These were broken out from gltf loader...\n// eslint-disable-next-line complexity\n\n// PERFORMANCE IDEA: No need to copy string twice...\nexport function padStringToByteAlignment(string, byteAlignment) {\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\nexport function copyStringToDataView(dataView, byteOffset, string, byteLength) {\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"],"mappings":";;;;;;;;;AAIO,SAASA,wBAAwB,CAACC,MAAM,EAAEC,aAAa,EAAE;EAC9D,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;AAEO,SAASE,oBAAoB,CAACC,QAAQ,EAAEC,UAAU,EAAEX,MAAM,EAAEY,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,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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"binary-copy-utils.js","names":["padToNBytes","copyPaddedArrayBufferToDataView","dataView","byteOffset","sourceBuffer","padding","paddedLength","byteLength","padLength","targetArray","Uint8Array","buffer","sourceArray","set","i","setUint8","copyPaddedStringToDataView","string","textEncoder","TextEncoder","stringBuffer","encode"],"sources":["../../../../src/lib/binary-utils/binary-copy-utils.ts"],"sourcesContent":["import {TypedArray} from '../../types';\nimport {padToNBytes} from './memory-copy-utils';\n\n/**\n * Copy sourceBuffer 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 {Array | TypedArray} sourceBuffer - source data buffer\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 copyPaddedArrayBufferToDataView(\n dataView: DataView | null,\n byteOffset: number,\n sourceBuffer: TypedArray,\n padding: number\n) {\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":"AACA,SAAQA,WAAW,QAAO,qBAAqB;;AAY/C,OAAO,SAASC,+BAA+B,CAC7CC,QAAyB,EACzBC,UAAkB,EAClBC,YAAwB,EACxBC,OAAe,EACf;EACA,MAAMC,YAAY,GAAGN,WAAW,CAACI,YAAY,CAACG,UAAU,EAAEF,OAAO,CAAC;EAClE,MAAMG,SAAS,GAAGF,YAAY,GAAGF,YAAY,CAACG,UAAU;EAExD,IAAIL,QAAQ,EAAE;IAEZ,MAAMO,WAAW,GAAG,IAAIC,UAAU,CAChCR,QAAQ,CAACS,MAAM,EACfT,QAAQ,CAACC,UAAU,GAAGA,UAAU,EAChCC,YAAY,CAACG,UAAU,CACxB;IACD,MAAMK,WAAW,GAAG,IAAIF,UAAU,CAACN,YAAY,CAAC;IAChDK,WAAW,CAACI,GAAG,CAACD,WAAW,CAAC;;IAG5B,KAAK,IAAIE,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGN,SAAS,EAAE,EAAEM,CAAC,EAAE;MAElCZ,QAAQ,CAACa,QAAQ,CAACZ,UAAU,GAAGC,YAAY,CAACG,UAAU,GAAGO,CAAC,EAAE,IAAI,CAAC;IACnE;EACF;EACAX,UAAU,IAAIG,YAAY;EAC1B,OAAOH,UAAU;AACnB;;AAYA,OAAO,SAASa,0BAA0B,CACxCd,QAAyB,EACzBC,UAAkB,EAClBc,MAAc,EACdZ,OAAe,EACP;EACR,MAAMa,WAAW,GAAG,IAAIC,WAAW,EAAE;EAGrC,MAAMC,YAAY,GAAGF,WAAW,CAACG,MAAM,CAACJ,MAAM,CAAC;EAE/Cd,UAAU,GAAGF,+BAA+B,CAACC,QAAQ,EAAEC,UAAU,EAAEiB,YAAY,EAAEf,OAAO,CAAC;EAEzF,OAAOF,UAAU;AACnB"}
|