@loaders.gl/polyfills 4.0.0-beta.1 → 4.0.0-beta.2

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.
@@ -0,0 +1,39 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { FileSystem, RandomAccessReadFileSystem } from '@loaders.gl/loader-utils';
4
+ import fs from 'fs';
5
+ type Stat = {
6
+ size: number;
7
+ isDirectory: () => boolean;
8
+ info?: fs.Stats;
9
+ };
10
+ type ReadOptions = {
11
+ buffer?: Buffer;
12
+ offset?: number;
13
+ length?: number;
14
+ position?: number;
15
+ };
16
+ /**
17
+ * FileSystem pass-through for Node.js
18
+ * Compatible with BrowserFileSystem.
19
+ * @param options
20
+ */
21
+ export declare class NodeFileSystem implements FileSystem, RandomAccessReadFileSystem {
22
+ constructor(options: {
23
+ [key: string]: any;
24
+ });
25
+ readdir(dirname?: string, options?: {}): Promise<any[]>;
26
+ stat(path: string, options?: {}): Promise<Stat>;
27
+ fetch(path: string, options: {
28
+ [key: string]: any;
29
+ }): Promise<any>;
30
+ open(path: string, flags: string | number, mode?: any): Promise<number>;
31
+ close(fd: number): Promise<void>;
32
+ fstat(fd: number): Promise<Stat>;
33
+ read(fd: number, { buffer, offset, length, position }: ReadOptions): Promise<{
34
+ bytesRead: number;
35
+ buffer: Uint8Array;
36
+ }>;
37
+ }
38
+ export {};
39
+ //# sourceMappingURL=node-filesystem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-filesystem.d.ts","sourceRoot":"","sources":["../../src/filesystems/node-filesystem.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,UAAU,EAAE,0BAA0B,EAAC,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,MAAM,IAAI,CAAC;AAMpB,KAAK,IAAI,GAAG;IACV,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,OAAO,CAAC;IAC3B,IAAI,CAAC,EAAE,EAA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@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAUA,wBAAgB,oBAAoB,SAAK;AAGzC,eAAO,MAAM,cAAc,MAAO,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { FileReaderPolyfill } from './node/file/file-reader';
2
2
  export { FilePolyfill } from './node/file/file';
3
3
  export { installFilePolyfills } from './node/file/install-file-polyfills';
4
+ export { NodeFileSystem } from './filesystems/node-filesystem';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAC,kBAAkB,EAAC,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,oCAAoC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAC,kBAAkB,EAAC,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAC,oBAAoB,EAAC,MAAM,oCAAoC,CAAC;AAqCxE,OAAO,EAAC,cAAc,EAAC,MAAM,+BAA+B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/polyfills",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "Polyfills for TextEncoder/TextDecoder",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -26,70 +26,10 @@
26
26
  "README.md"
27
27
  ],
28
28
  "browser": {
29
- "./src/node/fetch/utils/decode-data-uri.node.js": false,
30
- "./src/node/fetch/utils/decode-data-uri.node.ts": false,
31
- "./dist/es5/node/fetch/utils/decode-data-uri.node.js": false,
32
- "./dist/esm/node/fetch/utils/decode-data-uri.node.js": false,
33
- "./src/node/fetch/utils/stream-utils.node.js": false,
34
- "./src/node/fetch/utils/stream-utils.node.ts": false,
35
- "./dist/es5/node/fetch/utils/stream-utils.node.js": false,
36
- "./dist/esm/node/fetch/utils/stream-utils.node.js": false,
37
- "./src/node/fetch/fetch.node.js": false,
38
- "./src/node/fetch/fetch.node.ts": false,
39
- "./dist/es5/node/fetch/fetch.node.js": false,
40
- "./dist/esm/node/fetch/fetch.node.js": false,
41
- "./src/node/fetch/headers.node.js": false,
42
- "./src/node/fetch/headers.node.ts": false,
43
- "./dist/es5/node/fetch/headers.node.js": false,
44
- "./dist/esm/node/fetch/headers.node.js": false,
45
- "./src/node/fetch/response.node.js": false,
46
- "./src/node/fetch/response.node.ts": false,
47
- "./dist/es5/node/fetch/response.node.js": false,
48
- "./dist/esm/node/fetch/response.node.js": false,
49
- "./src/node/images/encode-image.node.js": false,
50
- "./src/node/images/encode-image.node.ts": false,
51
- "./dist/es5/node/images/encode-image.node.js": false,
52
- "./dist/esm/node/images/encode-image.node.js": false,
53
- "./src/node/images/parse-image.node.js": false,
54
- "./src/node/images/parse-image.node.ts": false,
55
- "./dist/es5/node/images/parse-image.node.js": false,
56
- "./dist/esm/node/images/parse-image.node.js": false,
57
- "./src/node/buffer/to-array-buffer.node.js": false,
58
- "./src/node/buffer/to-array-buffer.node.ts": false,
59
- "./dist/es5/node/buffer/to-array-buffer.node.js": false,
60
- "./dist/esm/node/buffer/to-array-buffer.node.js": false,
61
- "./src/node/buffer/btoa.node.js": false,
62
- "./src/node/buffer/btoa.node.ts": false,
63
- "./dist/es5/node/buffer/btoa.node.js": false,
64
- "./dist/esm/node/buffer/btoa.node.js": false,
65
- "./src/node/file/blob.js": false,
66
- "./src/node/file/blob.ts": false,
67
- "./dist/es5/node/file/blob.js": false,
68
- "./dist/esm/node/file/blob.js": false,
69
- "./src/node/file/file.js": false,
70
- "./src/node/file/file.ts": false,
71
- "./dist/es5/node/file/file.js": false,
72
- "./dist/esm/node/file/file.js": false,
73
- "./src/node/file/readable-stream.js": false,
74
- "./src/node/file/readable-stream.ts": false,
75
- "./dist/es5/node/file/readable-stream.js": false,
76
- "./dist/esm/node/file/readable-stream.js": false,
77
- "./src/libs/encoding-indices.js": false,
78
- "./src/libs/encoding-indices.ts": false,
79
- "./dist/es5/libs/encoding-indices.js": false,
80
- "./dist/esm/libs/encoding-indices.js": false,
81
- "fs": false,
82
- "http": false,
83
- "https": false,
84
- "stream": false,
85
- "get-pixels": false,
86
- "ndarray": false,
87
- "save-pixels": false,
88
- "stream-to-async-iterator": false,
89
- "through": false,
90
- "util": false,
91
- "zlib": false,
92
- "web-streams-polyfill": false
29
+ "./src/index.ts": "./src/index.browser.ts",
30
+ "./dist/index.js": "./dist/index.browser.js",
31
+ "./dist/es5/index.js": "./dist/es5/index.browser.js",
32
+ "./dist/esm/index.js": "./dist/es5/index.browser.js"
93
33
  },
94
34
  "scripts": {
95
35
  "pre-build": "npm run build-bundle",
@@ -105,5 +45,5 @@
105
45
  "through": "^2.3.8",
106
46
  "web-streams-polyfill": "^3.0.0"
107
47
  },
108
- "gitHead": "35c625e67132b0784e597d9ddabae8aefea29ff2"
48
+ "gitHead": "79c2033f755e88e11bc30a04428e3666b177b8fc"
109
49
  }
@@ -0,0 +1,87 @@
1
+ import {FileSystem, RandomAccessReadFileSystem} from '@loaders.gl/loader-utils';
2
+ import fs from 'fs';
3
+ import fsPromise from 'fs/promises';
4
+
5
+ // import {fetchFile} from "../fetch/fetch-file"
6
+ // import {selectLoader} from "../api/select-loader";
7
+
8
+ type Stat = {
9
+ size: number;
10
+ isDirectory: () => boolean;
11
+ info?: fs.Stats;
12
+ };
13
+
14
+ type ReadOptions = {
15
+ buffer?: Buffer;
16
+ offset?: number;
17
+ length?: number;
18
+ position?: number;
19
+ };
20
+
21
+ /**
22
+ * FileSystem pass-through for Node.js
23
+ * Compatible with BrowserFileSystem.
24
+ * @param options
25
+ */
26
+ export class NodeFileSystem implements FileSystem, RandomAccessReadFileSystem {
27
+ // implements FileSystem
28
+ constructor(options: {[key: string]: any}) {
29
+ this.fetch = options._fetch;
30
+ }
31
+
32
+ async readdir(dirname = '.', options?: {}): Promise<any[]> {
33
+ return await fsPromise.readdir(dirname, options);
34
+ }
35
+
36
+ async stat(path: string, options?: {}): Promise<Stat> {
37
+ const info = await fsPromise.stat(path, options);
38
+ return {size: Number(info.size), isDirectory: () => false, info};
39
+ }
40
+
41
+ async fetch(path: string, options: {[key: string]: any}) {
42
+ // Falls back to handle https:/http:/data: etc fetches
43
+ // eslint-disable-next-line
44
+ const fallbackFetch = options.fetch || this.fetch;
45
+ return fallbackFetch(path, options);
46
+ }
47
+
48
+ // implements IRandomAccessFileSystem
49
+ async open(path: string, flags: string | number, mode?: any): Promise<number> {
50
+ return (await fsPromise.open(path, flags)) as unknown as number;
51
+ }
52
+
53
+ async close(fd: number): Promise<void> {
54
+ fs.close(fd);
55
+ }
56
+
57
+ async fstat(fd: number): Promise<Stat> {
58
+ return await new Promise((resolve, reject) =>
59
+ fs.fstat(fd, (err, info) => (err ? reject(err) : resolve(info)))
60
+ );
61
+ }
62
+
63
+ async read(
64
+ fd: number,
65
+ // @ts-ignore Possibly null
66
+ {buffer = null, offset = 0, length = buffer.byteLength, position = null}: ReadOptions
67
+ ): Promise<{bytesRead: number; buffer: Uint8Array}> {
68
+ let totalBytesRead = 0;
69
+ // Read in loop until we get required number of bytes
70
+ while (totalBytesRead < length) {
71
+ const {bytesRead} = await new Promise<{bytesRead: number; buffer: Buffer}>(
72
+ // eslint-disable-next-line no-loop-func
73
+ (resolve, reject) =>
74
+ fs.read(
75
+ fd,
76
+ buffer,
77
+ offset + totalBytesRead,
78
+ length - totalBytesRead,
79
+ position + totalBytesRead,
80
+ (err, bytesRead, buffer) => (err ? reject(err) : resolve({bytesRead, buffer}))
81
+ )
82
+ );
83
+ totalBytesRead += bytesRead;
84
+ }
85
+ return {bytesRead: totalBytesRead, buffer};
86
+ }
87
+ }
@@ -0,0 +1,14 @@
1
+ // loaders.gl, MIT License
2
+
3
+ import {allSettled} from './promise/all-settled';
4
+
5
+ if (!('allSettled' in Promise)) {
6
+ // @ts-ignore
7
+ Promise.allSettled = allSettled;
8
+ }
9
+
10
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
11
+ export function installFilePolyfills() {}
12
+
13
+ // Dummy export to avoid import errors in browser tests
14
+ export const NodeFileSystem = null;
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable dot-notation */
2
- import {isBrowser, global} from './utils/globals';
2
+ import {isBrowser} from './utils/globals';
3
3
 
4
4
  import {TextDecoder, TextEncoder} from './lib/encoding';
5
5
  import {allSettled} from './promise/all-settled';
@@ -7,10 +7,6 @@ import {allSettled} from './promise/all-settled';
7
7
  // Node specific
8
8
  import * as base64 from './node/buffer/btoa.node';
9
9
 
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';
13
-
14
10
  import {encodeImageNode} from './node/images/encode-image.node';
15
11
  import {parseImageNode, NODE_FORMAT_SUPPORT} from './node/images/parse-image.node';
16
12
 
@@ -20,63 +16,83 @@ export {FileReaderPolyfill} from './node/file/file-reader';
20
16
  export {FilePolyfill} from './node/file/file';
21
17
  export {installFilePolyfills} from './node/file/install-file-polyfills';
22
18
 
19
+ if (isBrowser) {
20
+ // eslint-disable-next-line no-console
21
+ console.error(
22
+ 'loaders.gl: The @loaders.gl/polyfills should only be used in Node.js environments'
23
+ );
24
+ }
25
+
23
26
  // POLYFILLS: TextEncoder, TextDecoder
24
27
  // - Recent Node versions have these classes but virtually no encodings unless special build.
25
28
  // - Browser: Edge, IE11 do not have these
26
29
 
27
- const installTextEncoder = !isBrowser || !('TextEncoder' in global);
28
- if (installTextEncoder) {
29
- global['TextEncoder'] = TextEncoder;
30
+ if (!globalThis.TextEncoder) {
31
+ // @ts-expect-error
32
+ globalThis.TextEncoder = TextEncoder;
30
33
  }
31
34
 
32
- const installTextDecoder = !isBrowser || !('TextDecoder' in global);
33
- if (installTextDecoder) {
34
- global['TextDecoder'] = TextDecoder;
35
+ if (!globalThis.TextDecoder) {
36
+ // @ts-expect-error
37
+ globalThis.TextDecoder = TextDecoder;
35
38
  }
36
39
 
37
40
  // POLYFILLS: btoa, atob
38
41
  // - Node: Yes
39
42
  // - Browser: No
40
43
 
41
- if (!isBrowser && !('atob' in global) && base64.atob) {
42
- global['atob'] = base64.atob;
44
+ if (!isBrowser && !('atob' in globalThis) && base64.atob) {
45
+ globalThis['atob'] = base64.atob;
43
46
  }
44
- if (!isBrowser && !('btoa' in global) && base64.btoa) {
45
- global['btoa'] = base64.btoa;
46
- }
47
-
48
- // DEPRECATED POLYFILL:
49
- // - Node v18+: No, not needed
50
- // - Node v16 and lower: Yes
51
- // - Browsers (evergreen): Not needed.
52
- // - IE11: No. This polyfill is node only, install external polyfill
53
-
54
- if (!isBrowser && !('Headers' in global) && HeadersNode) {
55
- global['Headers'] = HeadersNode;
47
+ if (!isBrowser && !('btoa' in globalThis) && base64.btoa) {
48
+ globalThis['btoa'] = base64.btoa;
56
49
  }
57
50
 
58
- if (!isBrowser && !('Response' in global) && ResponseNode) {
59
- global['Response'] = ResponseNode;
60
- }
51
+ globalThis.loaders = globalThis.loaders || {};
61
52
 
62
- if (!isBrowser && !('fetch' in global) && fetchNode) {
63
- global['fetch'] = fetchNode;
64
- }
53
+ // FILESYSTEM POLYFILLS:
54
+ export {NodeFileSystem} from './filesystems/node-filesystem';
55
+ import {NodeFileSystem} from './filesystems/node-filesystem';
56
+ globalThis.loaders.NodeFileSystem = NodeFileSystem;
65
57
 
66
58
  // NODE IMAGE FUNCTIONS:
67
59
  // These are not official polyfills but used by the @loaders.gl/images module if installed
68
60
  // TODO - is there an appropriate Image API we could polyfill using an adapter?
69
61
 
70
- if (!isBrowser && !('_encodeImageNode' in global) && encodeImageNode) {
71
- global['_encodeImageNode'] = encodeImageNode;
62
+ if (!isBrowser && !('_encodeImageNode' in globalThis) && encodeImageNode) {
63
+ globalThis['_encodeImageNode'] = encodeImageNode;
72
64
  }
73
65
 
74
- if (!isBrowser && !('_parseImageNode' in global) && parseImageNode) {
75
- global['_parseImageNode'] = parseImageNode;
76
- global['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;
66
+ if (!isBrowser && !('_parseImageNode' in globalThis) && parseImageNode) {
67
+ globalThis['_parseImageNode'] = parseImageNode;
68
+ globalThis['_imageFormatsNode'] = NODE_FORMAT_SUPPORT;
77
69
  }
78
70
 
79
71
  if (!('allSettled' in Promise)) {
80
72
  // @ts-ignore
81
73
  Promise.allSettled = allSettled;
82
74
  }
75
+
76
+ // DEPRECATED POLYFILL:
77
+ // - Node v18+: No, not needed
78
+ // - Node v16 and lower: Yes
79
+ // - Browsers (evergreen): Not needed.
80
+ // - IE11: No. This polyfill is node only, install external polyfill
81
+ import {Headers as HeadersNode} from './node/fetch/headers.node';
82
+ import {Response as ResponseNode} from './node/fetch/response.node';
83
+ import {fetchNode as fetchNode} from './node/fetch/fetch.node';
84
+
85
+ if (!isBrowser && !('Headers' in globalThis) && HeadersNode) {
86
+ // @ts-expect-error
87
+ globalThis.Headers = HeadersNode;
88
+ }
89
+
90
+ if (!isBrowser && !('Response' in globalThis) && ResponseNode) {
91
+ // @ts-expect-error
92
+ globalThis.Response = ResponseNode;
93
+ }
94
+
95
+ if (!isBrowser && !('fetch' in globalThis) && fetchNode) {
96
+ // @ts-expect-error
97
+ globalThis.fetch = fetchNode;
98
+ }