@loaders.gl/worker-utils 3.1.0-alpha.5 → 3.1.0-beta.4

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 (120) hide show
  1. package/dist/es5/lib/env-utils/version.js +2 -2
  2. package/dist/es5/lib/env-utils/version.js.map +1 -1
  3. package/dist/es5/lib/library-utils/library-utils.js +1 -1
  4. package/dist/es5/lib/library-utils/library-utils.js.map +1 -1
  5. package/dist/es5/lib/node/require-utils.node.js +4 -8
  6. package/dist/es5/lib/node/require-utils.node.js.map +1 -1
  7. package/dist/es5/lib/process-utils/child-process-proxy.js.map +1 -1
  8. package/dist/es5/lib/worker-api/create-worker.js +1 -1
  9. package/dist/es5/lib/worker-api/create-worker.js.map +1 -1
  10. package/dist/es5/lib/worker-api/get-worker-url.js +1 -1
  11. package/dist/es5/lib/worker-api/get-worker-url.js.map +1 -1
  12. package/dist/es5/lib/worker-api/process-on-worker.js +1 -1
  13. package/dist/es5/lib/worker-api/process-on-worker.js.map +1 -1
  14. package/dist/es5/lib/worker-farm/worker-body.js.map +1 -1
  15. package/dist/es5/lib/worker-farm/worker-job.js.map +1 -1
  16. package/dist/es5/lib/worker-farm/worker-thread.js.map +1 -1
  17. package/dist/es5/lib/worker-utils/get-transfer-list.js.map +1 -1
  18. package/dist/es5/lib/worker-utils/remove-nontransferable-options.js.map +1 -1
  19. package/dist/esm/lib/env-utils/version.js +2 -2
  20. package/dist/esm/lib/env-utils/version.js.map +1 -1
  21. package/dist/esm/lib/library-utils/library-utils.js +1 -1
  22. package/dist/esm/lib/library-utils/library-utils.js.map +1 -1
  23. package/dist/esm/lib/node/require-utils.node.js +4 -8
  24. package/dist/esm/lib/node/require-utils.node.js.map +1 -1
  25. package/dist/esm/lib/process-utils/child-process-proxy.js.map +1 -1
  26. package/dist/esm/lib/worker-api/create-worker.js +1 -1
  27. package/dist/esm/lib/worker-api/create-worker.js.map +1 -1
  28. package/dist/esm/lib/worker-api/get-worker-url.js +1 -1
  29. package/dist/esm/lib/worker-api/get-worker-url.js.map +1 -1
  30. package/dist/esm/lib/worker-api/process-on-worker.js +1 -1
  31. package/dist/esm/lib/worker-api/process-on-worker.js.map +1 -1
  32. package/dist/esm/lib/worker-farm/worker-body.js.map +1 -1
  33. package/dist/esm/lib/worker-farm/worker-job.js.map +1 -1
  34. package/dist/esm/lib/worker-farm/worker-thread.js.map +1 -1
  35. package/dist/esm/lib/worker-utils/get-transfer-list.js.map +1 -1
  36. package/dist/esm/lib/worker-utils/remove-nontransferable-options.js.map +1 -1
  37. package/dist/index.d.ts +1 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +58 -0
  40. package/dist/lib/async-queue/async-queue.d.ts +1 -0
  41. package/dist/lib/async-queue/async-queue.d.ts.map +1 -0
  42. package/dist/lib/async-queue/async-queue.js +87 -0
  43. package/dist/lib/env-utils/assert.d.ts +1 -0
  44. package/dist/lib/env-utils/assert.d.ts.map +1 -0
  45. package/dist/lib/env-utils/assert.js +13 -0
  46. package/dist/lib/env-utils/globals.d.ts +1 -0
  47. package/dist/lib/env-utils/globals.d.ts.map +1 -0
  48. package/dist/lib/env-utils/globals.js +32 -0
  49. package/dist/lib/env-utils/version.d.ts +2 -1
  50. package/dist/lib/env-utils/version.d.ts.map +1 -0
  51. package/dist/lib/env-utils/version.js +12 -0
  52. package/dist/lib/library-utils/library-utils.d.ts +2 -1
  53. package/dist/lib/library-utils/library-utils.d.ts.map +1 -0
  54. package/dist/lib/library-utils/library-utils.js +158 -0
  55. package/dist/lib/node/require-utils.node.d.ts +6 -2
  56. package/dist/lib/node/require-utils.node.d.ts.map +1 -0
  57. package/dist/lib/node/require-utils.node.js +60 -0
  58. package/dist/lib/process-utils/child-process-proxy.d.ts +1 -0
  59. package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -0
  60. package/dist/lib/process-utils/child-process-proxy.js +131 -0
  61. package/dist/lib/process-utils/process-utils.d.ts +1 -0
  62. package/dist/lib/process-utils/process-utils.d.ts.map +1 -0
  63. package/dist/lib/process-utils/process-utils.js +35 -0
  64. package/dist/lib/worker-api/create-worker.d.ts +3 -2
  65. package/dist/lib/worker-api/create-worker.d.ts.map +1 -0
  66. package/dist/lib/worker-api/create-worker.js +92 -0
  67. package/dist/lib/worker-api/get-worker-url.d.ts +1 -0
  68. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -0
  69. package/dist/lib/worker-api/get-worker-url.js +48 -0
  70. package/dist/lib/worker-api/process-on-worker.d.ts +1 -0
  71. package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -0
  72. package/dist/lib/worker-api/process-on-worker.js +79 -0
  73. package/dist/lib/worker-api/validate-worker-version.d.ts +1 -0
  74. package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -0
  75. package/dist/lib/worker-api/validate-worker-version.js +35 -0
  76. package/dist/lib/worker-farm/worker-body.d.ts +1 -0
  77. package/dist/lib/worker-farm/worker-body.d.ts.map +1 -0
  78. package/dist/lib/worker-farm/worker-body.js +67 -0
  79. package/dist/lib/worker-farm/worker-farm.d.ts +1 -0
  80. package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -0
  81. package/dist/lib/worker-farm/worker-farm.js +88 -0
  82. package/dist/lib/worker-farm/worker-job.d.ts +2 -1
  83. package/dist/lib/worker-farm/worker-job.d.ts.map +1 -0
  84. package/dist/lib/worker-farm/worker-job.js +47 -0
  85. package/dist/lib/worker-farm/worker-pool.d.ts +1 -0
  86. package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -0
  87. package/dist/lib/worker-farm/worker-pool.js +151 -0
  88. package/dist/lib/worker-farm/worker-thread.d.ts +2 -1
  89. package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -0
  90. package/dist/lib/worker-farm/worker-thread.js +95 -0
  91. package/dist/lib/worker-utils/get-loadable-worker-url.d.ts +1 -0
  92. package/dist/lib/worker-utils/get-loadable-worker-url.d.ts.map +1 -0
  93. package/dist/lib/worker-utils/get-loadable-worker-url.js +74 -0
  94. package/dist/lib/worker-utils/get-transfer-list.d.ts +1 -0
  95. package/dist/lib/worker-utils/get-transfer-list.d.ts.map +1 -0
  96. package/dist/lib/worker-utils/get-transfer-list.js +61 -0
  97. package/dist/lib/worker-utils/remove-nontransferable-options.d.ts +1 -0
  98. package/dist/lib/worker-utils/remove-nontransferable-options.d.ts.map +1 -0
  99. package/dist/lib/worker-utils/remove-nontransferable-options.js +35 -0
  100. package/dist/null-worker.js +189 -550
  101. package/dist/null-worker.js.map +7 -1
  102. package/dist/types.d.ts +2 -1
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +2 -0
  105. package/dist/workers/null-worker.d.ts +1 -0
  106. package/dist/workers/null-worker.d.ts.map +1 -0
  107. package/dist/workers/null-worker.js +7 -0
  108. package/package.json +7 -6
  109. package/src/lib/env-utils/version.ts +1 -1
  110. package/src/lib/library-utils/library-utils.ts +6 -4
  111. package/src/lib/node/{require-utils.node.js → require-utils.node.ts} +13 -9
  112. package/src/lib/process-utils/child-process-proxy.ts +1 -1
  113. package/src/lib/worker-api/create-worker.ts +11 -7
  114. package/src/lib/worker-api/process-on-worker.ts +1 -1
  115. package/src/lib/worker-farm/worker-body.ts +2 -2
  116. package/src/lib/worker-farm/worker-job.ts +2 -2
  117. package/src/lib/worker-farm/worker-thread.ts +2 -2
  118. package/src/lib/worker-utils/get-transfer-list.ts +1 -1
  119. package/src/lib/worker-utils/remove-nontransferable-options.ts +1 -1
  120. package/src/types.ts +1 -1
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.getLibraryUrl = exports.loadLibrary = void 0;
23
+ /* global importScripts */
24
+ const globals_1 = require("../env-utils/globals");
25
+ const node = __importStar(require("../node/require-utils.node"));
26
+ const assert_1 = require("../env-utils/assert");
27
+ const version_1 = require("../env-utils/version");
28
+ // TODO - unpkg.com doesn't seem to have a `latest` specifier for alpha releases...
29
+ const LATEST = 'beta';
30
+ const VERSION = typeof version_1.VERSION !== 'undefined' ? version_1.VERSION : LATEST;
31
+ const loadLibraryPromises = {}; // promises
32
+ /**
33
+ * Dynamically loads a library ("module")
34
+ *
35
+ * - wasm library: Array buffer is returned
36
+ * - js library: Parse JS is returned
37
+ *
38
+ * Method depends on environment
39
+ * - browser - script element is created and installed on document
40
+ * - worker - eval is called on global context
41
+ * - node - file is required
42
+ *
43
+ * @param libraryUrl
44
+ * @param moduleName
45
+ * @param options
46
+ */
47
+ async function loadLibrary(libraryUrl, moduleName = null, options = {}) {
48
+ if (moduleName) {
49
+ libraryUrl = getLibraryUrl(libraryUrl, moduleName, options);
50
+ }
51
+ // Ensure libraries are only loaded once
52
+ loadLibraryPromises[libraryUrl] =
53
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
54
+ loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
55
+ return await loadLibraryPromises[libraryUrl];
56
+ }
57
+ exports.loadLibrary = loadLibrary;
58
+ // TODO - sort out how to resolve paths for main/worker and dev/prod
59
+ function getLibraryUrl(library, moduleName, options) {
60
+ // Check if already a URL
61
+ if (library.startsWith('http')) {
62
+ return library;
63
+ }
64
+ // Allow application to import and supply libraries through `options.modules`
65
+ const modules = options.modules || {};
66
+ if (modules[library]) {
67
+ return modules[library];
68
+ }
69
+ // Load from local files, not from CDN scripts in Node.js
70
+ // TODO - needs to locate the modules directory when installed!
71
+ if (!globals_1.isBrowser) {
72
+ return `modules/${moduleName}/dist/libs/${library}`;
73
+ }
74
+ // In browser, load from external scripts
75
+ if (options.CDN) {
76
+ (0, assert_1.assert)(options.CDN.startsWith('http'));
77
+ return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${library}`;
78
+ }
79
+ // TODO - loading inside workers requires paths relative to worker script location...
80
+ if (globals_1.isWorker) {
81
+ return `../src/libs/${library}`;
82
+ }
83
+ return `modules/${moduleName}/src/libs/${library}`;
84
+ }
85
+ exports.getLibraryUrl = getLibraryUrl;
86
+ async function loadLibraryFromFile(libraryUrl) {
87
+ if (libraryUrl.endsWith('wasm')) {
88
+ const response = await fetch(libraryUrl);
89
+ return await response.arrayBuffer();
90
+ }
91
+ if (!globals_1.isBrowser) {
92
+ return node.requireFromFile && (await node.requireFromFile(libraryUrl));
93
+ }
94
+ if (globals_1.isWorker) {
95
+ return importScripts(libraryUrl);
96
+ }
97
+ // TODO - fix - should be more secure than string parsing since observes CORS
98
+ // if (isBrowser) {
99
+ // return await loadScriptFromFile(libraryUrl);
100
+ // }
101
+ const response = await fetch(libraryUrl);
102
+ const scriptSource = await response.text();
103
+ return loadLibraryFromString(scriptSource, libraryUrl);
104
+ }
105
+ /*
106
+ async function loadScriptFromFile(libraryUrl) {
107
+ const script = document.createElement('script');
108
+ script.src = libraryUrl;
109
+ return await new Promise((resolve, reject) => {
110
+ script.onload = data => {
111
+ resolve(data);
112
+ };
113
+ script.onerror = reject;
114
+ });
115
+ }
116
+ */
117
+ // TODO - Needs security audit...
118
+ // - Raw eval call
119
+ // - Potentially bypasses CORS
120
+ // Upside is that this separates fetching and parsing
121
+ // we could create a`LibraryLoader` or`ModuleLoader`
122
+ function loadLibraryFromString(scriptSource, id) {
123
+ if (!globals_1.isBrowser) {
124
+ return node.requireFromString && node.requireFromString(scriptSource, id);
125
+ }
126
+ if (globals_1.isWorker) {
127
+ // Use lvalue trick to make eval run in global scope
128
+ eval.call(globals_1.global, scriptSource); // eslint-disable-line no-eval
129
+ // https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript
130
+ // http://perfectionkills.com/global-eval-what-are-the-options/
131
+ return null;
132
+ }
133
+ const script = document.createElement('script');
134
+ script.id = id;
135
+ // most browsers like a separate text node but some throw an error. The second method covers those.
136
+ try {
137
+ script.appendChild(document.createTextNode(scriptSource));
138
+ }
139
+ catch (e) {
140
+ script.text = scriptSource;
141
+ }
142
+ document.body.appendChild(script);
143
+ return null;
144
+ }
145
+ // TODO - technique for module injection into worker, from THREE.DracoLoader...
146
+ /*
147
+ function combineWorkerWithLibrary(worker, jsContent) {
148
+ var fn = wWorker.toString();
149
+ var body = [
150
+ '// injected',
151
+ jsContent,
152
+ '',
153
+ '// worker',
154
+ fn.substring(fn.indexOf('{') + 1, fn.lastIndexOf('}'))
155
+ ].join('\n');
156
+ this.workerSourceURL = URL.createObjectURL(new Blob([body]));
157
+ }
158
+ */
@@ -1,2 +1,6 @@
1
- export function requireFromFile(filename: any): Promise<any>;
2
- export function requireFromString(code: any, filename?: string, options?: {}): any;
1
+ export declare function requireFromFile(filename: string): Promise<any>;
2
+ export declare function requireFromString(code: string, filename?: string, options?: {
3
+ prependPaths?: string[];
4
+ appendPaths?: string[];
5
+ }): any;
6
+ //# sourceMappingURL=require-utils.node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"require-utils.node.d.ts","sourceRoot":"","sources":["../../../src/lib/node/require-utils.node.ts"],"names":[],"mappings":"AAaA,wBAAsB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAWpE;AAQD,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,QAAQ,SAAK,EACb,OAAO,CAAC,EAAE;IACR,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB,GACA,GAAG,CA6BL"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ // Fork of https://github.com/floatdrop/require-from-string/blob/master/index.js
3
+ // Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
4
+ // MIT license
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.requireFromString = exports.requireFromFile = void 0;
10
+ // this file is not visible to webpack (it is excluded in the package.json "browser" field).
11
+ const module_1 = __importDefault(require("module"));
12
+ const path_1 = __importDefault(require("path"));
13
+ // Node.js Dynamically require from file
14
+ // Relative names are resolved relative to cwd
15
+ // This indirect function is provided because webpack will try to bundle `module.require`.
16
+ // this file is not visible to webpack (it is excluded in the package.json "browser" field).
17
+ async function requireFromFile(filename) {
18
+ if (filename.startsWith('http')) {
19
+ const response = await fetch(filename);
20
+ const code = await response.text();
21
+ return requireFromString(code);
22
+ }
23
+ if (!filename.startsWith('/')) {
24
+ filename = `${process.cwd()}/${filename}`;
25
+ }
26
+ return require(filename);
27
+ }
28
+ exports.requireFromFile = requireFromFile;
29
+ // Dynamically require from string
30
+ // - `code` - Required - Type: string - Module code.
31
+ // - `filename` - Type: string - Default: '' - Optional filename.
32
+ // - `options.appendPaths` Type: Array List of paths, that will be appended to module paths.
33
+ // Useful, when you want to be able require modules from these paths.
34
+ // - `options.prependPaths` Type: Array Same as appendPaths, but paths will be prepended.
35
+ function requireFromString(code, filename = '', options) {
36
+ if (typeof filename === 'object') {
37
+ options = filename;
38
+ filename = '';
39
+ }
40
+ if (typeof code !== 'string') {
41
+ throw new Error(`code must be a string, not ${typeof code}`);
42
+ }
43
+ // @ts-ignore
44
+ const paths = module_1.default._nodeModulePaths(path_1.default.dirname(filename));
45
+ const parent = module.parent;
46
+ // @ts-ignore
47
+ const newModule = new module_1.default(filename, parent);
48
+ newModule.filename = filename;
49
+ newModule.paths = []
50
+ .concat(options?.prependPaths || [])
51
+ .concat(paths)
52
+ .concat(options?.appendPaths || []);
53
+ // @ts-ignore
54
+ newModule._compile(code, filename);
55
+ if (parent && parent.children) {
56
+ parent.children.splice(parent.children.indexOf(newModule), 1);
57
+ }
58
+ return newModule.exports;
59
+ }
60
+ exports.requireFromString = requireFromString;
@@ -40,3 +40,4 @@ export default class ChildProcessProxy {
40
40
  _setTimeout(callback: (...args: any[]) => void): void;
41
41
  _clearTimeout(): void;
42
42
  }
43
+ //# sourceMappingURL=child-process-proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"child-process-proxy.d.ts","sourceRoot":"","sources":["../../../src/lib/process-utils/child-process-proxy.ts"],"names":[],"mappings":";AAIA,OAAO,KAAK,YAAY,MAAM,eAAe,CAAC;AAG9C,oBAAY,sBAAsB,GAAG;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uBAAuB;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8DAA8D;IAC9D,yBAAyB;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,KAAK,CAAC,EAAE,YAAY,CAAC,wBAAwB,CAAC;IAC9C,yBAAyB;IACzB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IAC7C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAChD,CAAC;AAaF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,sBAAsB,CAAsB;IACnD,OAAO,CAAC,YAAY,CAA0C;IAC9D,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,YAAY,CAAC,CAAM;gBAGf,EAAC,EAAqB,EAAC;;KAAK;IAIxC,qDAAqD;IAC/C,KAAK,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,MAAM,CAAC;IAsD3D,oCAAoC;IAC9B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAO3B,yBAAyB;IACnB,IAAI,CAAC,UAAU,GAAE,MAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjD,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI;IAM9C,aAAa;CAKd"}
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ /* eslint-disable no-console */
23
+ // Avoid using named imports for Node builtins to help with "empty" resolution
24
+ // for bundlers targeting browser environments. Access imports & types
25
+ // through the `ChildProcess` object (e.g. `ChildProcess.spawn`, `ChildProcess.ChildProcess`).
26
+ const ChildProcess = __importStar(require("child_process"));
27
+ const process_utils_1 = require("./process-utils");
28
+ const DEFAULT_PROPS = {
29
+ command: '',
30
+ arguments: [],
31
+ port: 5000,
32
+ autoPort: true,
33
+ wait: 2000,
34
+ onSuccess: (processProxy) => {
35
+ console.log(`Started ${processProxy.props.command}`);
36
+ }
37
+ };
38
+ /**
39
+ * Manager for a Node.js child process
40
+ * Prepares arguments, starts, stops and tracks output
41
+ */
42
+ class ChildProcessProxy {
43
+ // constructor(props?: {id?: string});
44
+ constructor({ id = 'browser-driver' } = {}) {
45
+ this.props = { ...DEFAULT_PROPS };
46
+ this.childProcess = null;
47
+ this.port = 0;
48
+ this.id = id;
49
+ }
50
+ /** Starts a child process with the provided props */
51
+ async start(props) {
52
+ props = { ...DEFAULT_PROPS, ...props };
53
+ this.props = props;
54
+ const args = [...props.arguments];
55
+ // If portArg is set, we can look up an available port
56
+ this.port = Number(props.port);
57
+ if (props.portArg) {
58
+ if (props.autoPort) {
59
+ this.port = await (0, process_utils_1.getAvailablePort)(props.port);
60
+ }
61
+ args.push(props.portArg, String(this.port));
62
+ }
63
+ return await new Promise((resolve, reject) => {
64
+ try {
65
+ this._setTimeout(() => {
66
+ if (props.onSuccess) {
67
+ props.onSuccess(this);
68
+ }
69
+ resolve({});
70
+ });
71
+ console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`);
72
+ const childProcess = ChildProcess.spawn(props.command, args, props.spawn);
73
+ this.childProcess = childProcess;
74
+ childProcess.stdout.on('data', (data) => {
75
+ console.log(data.toString());
76
+ });
77
+ // TODO - add option regarding whether stderr should be treated as data
78
+ childProcess.stderr.on('data', (data) => {
79
+ console.log(`Child process wrote to stderr: "${data}".`);
80
+ this._clearTimeout();
81
+ reject(new Error(data));
82
+ });
83
+ childProcess.on('error', (error) => {
84
+ console.log(`Child process errored with ${error}`);
85
+ this._clearTimeout();
86
+ reject(error);
87
+ });
88
+ childProcess.on('close', (code) => {
89
+ console.log(`Child process exited with ${code}`);
90
+ this.childProcess = null;
91
+ this._clearTimeout();
92
+ resolve({});
93
+ });
94
+ }
95
+ catch (error) {
96
+ reject(error);
97
+ }
98
+ });
99
+ }
100
+ /** Stops a running child process */
101
+ async stop() {
102
+ if (this.childProcess) {
103
+ this.childProcess.kill();
104
+ this.childProcess = null;
105
+ }
106
+ }
107
+ /** Exits this process */
108
+ async exit(statusCode = 0) {
109
+ try {
110
+ await this.stop();
111
+ // eslint-disable-next-line no-process-exit
112
+ process.exit(statusCode);
113
+ }
114
+ catch (error) {
115
+ console.error(error.message || error);
116
+ // eslint-disable-next-line no-process-exit
117
+ process.exit(1);
118
+ }
119
+ }
120
+ _setTimeout(callback) {
121
+ if (Number(this.props.wait) > 0) {
122
+ this.successTimer = setTimeout(callback, this.props.wait);
123
+ }
124
+ }
125
+ _clearTimeout() {
126
+ if (this.successTimer) {
127
+ clearTimeout(this.successTimer);
128
+ }
129
+ }
130
+ }
131
+ exports.default = ChildProcessProxy;
@@ -1 +1,2 @@
1
1
  export declare function getAvailablePort(defaultPort?: number): Promise<number>;
2
+ //# sourceMappingURL=process-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/process-utils/process-utils.ts"],"names":[],"mappings":"AAIA,wBAAgB,gBAAgB,CAAC,WAAW,GAAE,MAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAyB5E"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAvailablePort = void 0;
7
+ const child_process_1 = __importDefault(require("child_process"));
8
+ // Get an available port
9
+ // Works on Unix systems
10
+ function getAvailablePort(defaultPort = 3000) {
11
+ return new Promise((resolve) => {
12
+ // Get a list of all ports in use
13
+ child_process_1.default.exec('lsof -i -P -n | grep LISTEN', (error, stdout) => {
14
+ if (error) {
15
+ // likely no permission, e.g. CI
16
+ resolve(defaultPort);
17
+ return;
18
+ }
19
+ const portsInUse = [];
20
+ const regex = /:(\d+) \(LISTEN\)/;
21
+ stdout.split('\n').forEach((line) => {
22
+ const match = regex.exec(line);
23
+ if (match) {
24
+ portsInUse.push(Number(match[1]));
25
+ }
26
+ });
27
+ let port = defaultPort;
28
+ while (portsInUse.includes(port)) {
29
+ port++;
30
+ }
31
+ resolve(port);
32
+ });
33
+ });
34
+ }
35
+ exports.getAvailablePort = getAvailablePort;
@@ -1,8 +1,9 @@
1
- import type { Process, ProcessInBatches } from '../../types';
1
+ import type { WorkerContext, Process, ProcessInBatches } from '../../types';
2
2
  export declare type ProcessOnMainThread = (data: any, options?: {
3
3
  [key: string]: any;
4
- }, context?: any) => any;
4
+ }, context?: WorkerContext) => any;
5
5
  /**
6
6
  * Set up a WebWorkerGlobalScope to talk with the main thread
7
7
  */
8
8
  export declare function createWorker(process: Process, processInBatches?: ProcessInBatches): void;
9
+ //# sourceMappingURL=create-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-worker.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/create-worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,aAAa,EACb,OAAO,EACP,gBAAgB,EACjB,MAAM,aAAa,CAAC;AAUrB,oBAAY,mBAAmB,GAAG,CAChC,IAAI,EAAE,GAAG,EACT,OAAO,CAAC,EAAE;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,EAC9B,OAAO,CAAC,EAAE,aAAa,KACpB,GAAG,CAAC;AAET;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,gBAAgB,CAAC,EAAE,gBAAgB,GAAG,IAAI,CAkDxF"}
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createWorker = void 0;
7
+ const async_queue_1 = __importDefault(require("../async-queue/async-queue"));
8
+ const worker_body_1 = __importDefault(require("../worker-farm/worker-body"));
9
+ // import {validateWorkerVersion} from './validate-worker-version';
10
+ /** Counter for jobs */
11
+ let requestId = 0;
12
+ let inputBatches;
13
+ let options;
14
+ /**
15
+ * Set up a WebWorkerGlobalScope to talk with the main thread
16
+ */
17
+ function createWorker(process, processInBatches) {
18
+ // Check that we are actually in a worker thread
19
+ if (typeof self === 'undefined') {
20
+ return;
21
+ }
22
+ const context = {
23
+ process: processOnMainThread
24
+ };
25
+ // eslint-disable-next-line complexity
26
+ worker_body_1.default.onmessage = async (type, payload) => {
27
+ try {
28
+ switch (type) {
29
+ case 'process':
30
+ if (!process) {
31
+ throw new Error('Worker does not support atomic processing');
32
+ }
33
+ const result = await process(payload.input, payload.options || {}, context);
34
+ worker_body_1.default.postMessage('done', { result });
35
+ break;
36
+ case 'process-in-batches':
37
+ if (!processInBatches) {
38
+ throw new Error('Worker does not support batched processing');
39
+ }
40
+ inputBatches = new async_queue_1.default();
41
+ options = payload.options || {};
42
+ const resultIterator = processInBatches(inputBatches, options, context);
43
+ for await (const batch of resultIterator) {
44
+ worker_body_1.default.postMessage('output-batch', { result: batch });
45
+ }
46
+ worker_body_1.default.postMessage('done', {});
47
+ break;
48
+ case 'input-batch':
49
+ inputBatches.push(payload.input);
50
+ break;
51
+ case 'input-done':
52
+ inputBatches.close();
53
+ break;
54
+ default:
55
+ }
56
+ }
57
+ catch (error) {
58
+ const message = error instanceof Error ? error.message : '';
59
+ worker_body_1.default.postMessage('error', { error: message });
60
+ }
61
+ };
62
+ }
63
+ exports.createWorker = createWorker;
64
+ function processOnMainThread(arrayBuffer, options = {}) {
65
+ return new Promise((resolve, reject) => {
66
+ const id = requestId++;
67
+ /**
68
+ */
69
+ const onMessage = (type, payload) => {
70
+ if (payload.id !== id) {
71
+ // not ours
72
+ return;
73
+ }
74
+ switch (type) {
75
+ case 'done':
76
+ worker_body_1.default.removeEventListener(onMessage);
77
+ resolve(payload.result);
78
+ break;
79
+ case 'error':
80
+ worker_body_1.default.removeEventListener(onMessage);
81
+ reject(payload.error);
82
+ break;
83
+ default:
84
+ // ignore
85
+ }
86
+ };
87
+ worker_body_1.default.addEventListener(onMessage);
88
+ // Ask the main thread to decode data
89
+ const payload = { id, input: arrayBuffer, options };
90
+ worker_body_1.default.postMessage('process', payload);
91
+ });
92
+ }
@@ -11,3 +11,4 @@ export declare function getWorkerName(worker: WorkerObject): string;
11
11
  * - a URL provided by the user in options
12
12
  */
13
13
  export declare function getWorkerURL(worker: WorkerObject, options?: WorkerOptions): string;
14
+ //# sourceMappingURL=get-worker-url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/get-worker-url.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAO7D;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA8BtF"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getWorkerURL = exports.getWorkerName = void 0;
4
+ const assert_1 = require("../env-utils/assert");
5
+ const version_1 = require("../env-utils/version");
6
+ const NPM_TAG = 'beta'; // Change to 'latest' on release-branch
7
+ const VERSION = typeof version_1.VERSION !== 'undefined' ? version_1.VERSION : NPM_TAG;
8
+ /**
9
+ * Gets worker object's name (for debugging in Chrome thread inspector window)
10
+ */
11
+ function getWorkerName(worker) {
12
+ const warning = worker.version !== VERSION ? ` (worker-utils@${VERSION})` : '';
13
+ return `${worker.name}@${worker.version}${warning}`;
14
+ }
15
+ exports.getWorkerName = getWorkerName;
16
+ /**
17
+ * Generate a worker URL based on worker object and options
18
+ * @returns A URL to one of the following:
19
+ * - a published worker on unpkg CDN
20
+ * - a local test worker
21
+ * - a URL provided by the user in options
22
+ */
23
+ function getWorkerURL(worker, options = {}) {
24
+ const workerOptions = options[worker.id] || {};
25
+ const workerFile = `${worker.id}-worker.js`;
26
+ let url = workerOptions.workerUrl;
27
+ // If URL is test, generate local loaders.gl url
28
+ // @ts-ignore _workerType
29
+ if (options._workerType === 'test') {
30
+ url = `modules/${worker.module}/dist/${workerFile}`;
31
+ }
32
+ // If url override is not provided, generate a URL to published version on npm CDN unpkg.com
33
+ if (!url) {
34
+ // GENERATE
35
+ let version = worker.version;
36
+ // On master we need to load npm alpha releases published with the `beta` tag
37
+ if (version === 'latest') {
38
+ // throw new Error('latest worker version specified');
39
+ version = NPM_TAG;
40
+ }
41
+ const versionTag = version ? `@${version}` : '';
42
+ url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;
43
+ }
44
+ (0, assert_1.assert)(url);
45
+ // Allow user to override location
46
+ return url;
47
+ }
48
+ exports.getWorkerURL = getWorkerURL;
@@ -17,3 +17,4 @@ export declare function canProcessOnWorker(worker: WorkerObject, options?: Worke
17
17
  */
18
18
  export declare function processOnWorker(worker: WorkerObject, data: any, options?: ProcessOnWorkerOptions, context?: WorkerContext): Promise<any>;
19
19
  export {};
20
+ //# sourceMappingURL=process-on-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-on-worker.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/process-on-worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EAGd,MAAM,aAAa,CAAC;AAMrB,aAAK,sBAAsB,GAAG,aAAa,GAAG;IAC5C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,aAAa,4BAM/E;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,YAAY,EACpB,IAAI,EAAE,GAAG,EACT,OAAO,GAAE,sBAA2B,EACpC,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,GAAG,CAAC,CAoBd"}