@loaders.gl/worker-utils 4.4.0-alpha.2 → 4.4.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.
Files changed (73) hide show
  1. package/dist/index.cjs +58 -138
  2. package/dist/index.cjs.map +4 -4
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -1
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/async-queue/async-queue.js +1 -0
  8. package/dist/lib/async-queue/async-queue.js.map +1 -0
  9. package/dist/lib/env-utils/assert.js +1 -0
  10. package/dist/lib/env-utils/assert.js.map +1 -0
  11. package/dist/lib/env-utils/globals.js +1 -0
  12. package/dist/lib/env-utils/globals.js.map +1 -0
  13. package/dist/lib/env-utils/version.d.ts.map +1 -1
  14. package/dist/lib/env-utils/version.js +5 -2
  15. package/dist/lib/env-utils/version.js.map +1 -0
  16. package/dist/lib/library-utils/library-utils.d.ts +19 -2
  17. package/dist/lib/library-utils/library-utils.d.ts.map +1 -1
  18. package/dist/lib/library-utils/library-utils.js +72 -30
  19. package/dist/lib/library-utils/library-utils.js.map +1 -0
  20. package/dist/lib/node/worker_threads-browser.js +1 -0
  21. package/dist/lib/node/worker_threads-browser.js.map +1 -0
  22. package/dist/lib/node/worker_threads.js +1 -0
  23. package/dist/lib/node/worker_threads.js.map +1 -0
  24. package/dist/lib/npm-tag.js +1 -0
  25. package/dist/lib/npm-tag.js.map +1 -0
  26. package/dist/lib/process-utils/child-process-proxy.browser.d.ts +8 -0
  27. package/dist/lib/process-utils/child-process-proxy.browser.d.ts.map +1 -0
  28. package/dist/lib/process-utils/child-process-proxy.browser.js +18 -0
  29. package/dist/lib/process-utils/child-process-proxy.browser.js.map +1 -0
  30. package/dist/lib/process-utils/child-process-proxy.d.ts +3 -2
  31. package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -1
  32. package/dist/lib/process-utils/child-process-proxy.js +12 -7
  33. package/dist/lib/process-utils/child-process-proxy.js.map +1 -0
  34. package/dist/lib/process-utils/process-utils.js +1 -0
  35. package/dist/lib/process-utils/process-utils.js.map +1 -0
  36. package/dist/lib/worker-api/create-worker.js +1 -0
  37. package/dist/lib/worker-api/create-worker.js.map +1 -0
  38. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
  39. package/dist/lib/worker-api/get-worker-url.js +3 -1
  40. package/dist/lib/worker-api/get-worker-url.js.map +1 -0
  41. package/dist/lib/worker-api/process-on-worker.js +1 -0
  42. package/dist/lib/worker-api/process-on-worker.js.map +1 -0
  43. package/dist/lib/worker-api/validate-worker-version.js +1 -0
  44. package/dist/lib/worker-api/validate-worker-version.js.map +1 -0
  45. package/dist/lib/worker-farm/worker-body.js +1 -0
  46. package/dist/lib/worker-farm/worker-body.js.map +1 -0
  47. package/dist/lib/worker-farm/worker-farm.js +1 -0
  48. package/dist/lib/worker-farm/worker-farm.js.map +1 -0
  49. package/dist/lib/worker-farm/worker-job.js +1 -0
  50. package/dist/lib/worker-farm/worker-job.js.map +1 -0
  51. package/dist/lib/worker-farm/worker-pool.js +1 -0
  52. package/dist/lib/worker-farm/worker-pool.js.map +1 -0
  53. package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -1
  54. package/dist/lib/worker-farm/worker-thread.js +4 -2
  55. package/dist/lib/worker-farm/worker-thread.js.map +1 -0
  56. package/dist/lib/worker-utils/get-loadable-worker-url.js +1 -0
  57. package/dist/lib/worker-utils/get-loadable-worker-url.js.map +1 -0
  58. package/dist/lib/worker-utils/get-transfer-list.js +1 -0
  59. package/dist/lib/worker-utils/get-transfer-list.js.map +1 -0
  60. package/dist/lib/worker-utils/remove-nontransferable-options.js +1 -0
  61. package/dist/lib/worker-utils/remove-nontransferable-options.js.map +1 -0
  62. package/dist/types.js +1 -0
  63. package/dist/types.js.map +1 -0
  64. package/dist/workers/null-worker.js +1 -0
  65. package/dist/workers/null-worker.js.map +1 -0
  66. package/package.json +10 -6
  67. package/src/index.ts +6 -1
  68. package/src/lib/env-utils/version.ts +4 -1
  69. package/src/lib/library-utils/library-utils.ts +95 -34
  70. package/src/lib/process-utils/child-process-proxy.browser.ts +23 -0
  71. package/src/lib/process-utils/child-process-proxy.ts +23 -9
  72. package/src/lib/worker-api/get-worker-url.ts +2 -1
  73. package/src/lib/worker-farm/worker-thread.ts +4 -3
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
7
  var __export = (target, all) => {
@@ -18,14 +16,6 @@ var __copyProps = (to, from, except, desc) => {
18
16
  }
19
17
  return to;
20
18
  };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
20
  var __publicField = (obj, key, value) => {
31
21
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -46,6 +36,7 @@ __export(dist_exports, {
46
36
  assert: () => assert,
47
37
  canProcessOnWorker: () => canProcessOnWorker,
48
38
  createWorker: () => createWorker,
39
+ extractLoadLibraryOptions: () => extractLoadLibraryOptions,
49
40
  getLibraryUrl: () => getLibraryUrl,
50
41
  getTransferList: () => getTransferList,
51
42
  getTransferListForWriter: () => getTransferListForWriter,
@@ -69,8 +60,9 @@ function getVersion() {
69
60
  if (false) {
70
61
  console.warn("loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.");
71
62
  globalThis._loadersgl_.version = NPM_TAG;
63
+ warningIssued = true;
72
64
  } else {
73
- globalThis._loadersgl_.version = "4.4.0-alpha.1";
65
+ globalThis._loadersgl_.version = "4.4.0";
74
66
  }
75
67
  }
76
68
  return globalThis._loadersgl_.version;
@@ -342,7 +334,8 @@ var WorkerThread = class {
342
334
  if (this.url) {
343
335
  const absolute = this.url.includes(":/") || this.url.startsWith("/");
344
336
  const url = absolute ? this.url : `./${this.url}`;
345
- worker = new NodeWorker(url, { eval: false });
337
+ const type = this.url.endsWith(".ts") || this.url.endsWith(".mjs") ? "module" : "commonjs";
338
+ worker = new NodeWorker(url, { eval: false, type });
346
339
  } else if (this.source) {
347
340
  worker = new NodeWorker(this.source, { eval: true });
348
341
  } else {
@@ -672,13 +665,15 @@ function getWorkerName(worker) {
672
665
  return `${worker.name}@${worker.version}${warning}`;
673
666
  }
674
667
  function getWorkerURL(worker, options2 = {}) {
668
+ var _a;
675
669
  const workerOptions = options2[worker.id] || {};
676
670
  const workerFile = isBrowser ? `${worker.id}-worker.js` : `${worker.id}-worker-node.js`;
677
671
  let url = workerOptions.workerUrl;
678
672
  if (!url && worker.id === "compression") {
679
673
  url = options2.workerUrl;
680
674
  }
681
- if (options2._workerType === "test") {
675
+ const workerType = options2._workerType || ((_a = options2 == null ? void 0 : options2.core) == null ? void 0 : _a._workerType);
676
+ if (workerType === "test") {
682
677
  if (isBrowser) {
683
678
  url = `modules/${worker.module}/dist/${workerFile}`;
684
679
  } else {
@@ -905,6 +900,17 @@ function validateWorkerVersion(worker, coreVersion = VERSION) {
905
900
 
906
901
  // dist/lib/library-utils/library-utils.js
907
902
  var loadLibraryPromises = {};
903
+ function extractLoadLibraryOptions(options2 = {}) {
904
+ var _a, _b;
905
+ const useLocalLibraries = options2.useLocalLibraries ?? ((_a = options2.core) == null ? void 0 : _a.useLocalLibraries);
906
+ const CDN = options2.CDN ?? ((_b = options2.core) == null ? void 0 : _b.CDN);
907
+ const modules = options2.modules;
908
+ return {
909
+ ...useLocalLibraries !== void 0 ? { useLocalLibraries } : {},
910
+ ...CDN !== void 0 ? { CDN } : {},
911
+ ...modules !== void 0 ? { modules } : {}
912
+ };
913
+ }
908
914
  async function loadLibrary(libraryUrl, moduleName = null, options2 = {}, libraryName = null) {
909
915
  if (moduleName) {
910
916
  libraryUrl = getLibraryUrl(libraryUrl, moduleName, options2, libraryName);
@@ -914,6 +920,9 @@ async function loadLibrary(libraryUrl, moduleName = null, options2 = {}, library
914
920
  return await loadLibraryPromises[libraryUrl];
915
921
  }
916
922
  function getLibraryUrl(library, moduleName, options2 = {}, libraryName = null) {
923
+ if (options2 == null ? void 0 : options2.core) {
924
+ throw new Error("loadLibrary: options.core must be pre-normalized");
925
+ }
917
926
  if (!options2.useLocalLibraries && library.startsWith("http")) {
918
927
  return library;
919
928
  }
@@ -939,10 +948,20 @@ async function loadLibraryFromFile(libraryUrl) {
939
948
  return await loadAsArrayBuffer(libraryUrl);
940
949
  }
941
950
  if (!isBrowser) {
951
+ const { requireFromFile } = globalThis.loaders || {};
942
952
  try {
943
- const { requireFromFile } = globalThis.loaders || {};
944
- return await (requireFromFile == null ? void 0 : requireFromFile(libraryUrl));
953
+ const result = await (requireFromFile == null ? void 0 : requireFromFile(libraryUrl));
954
+ if (result || !libraryUrl.includes("/dist/libs/")) {
955
+ return result;
956
+ }
957
+ return await (requireFromFile == null ? void 0 : requireFromFile(libraryUrl.replace("/dist/libs/", "/src/libs/")));
945
958
  } catch (error) {
959
+ if (libraryUrl.includes("/dist/libs/")) {
960
+ try {
961
+ return await (requireFromFile == null ? void 0 : requireFromFile(libraryUrl.replace("/dist/libs/", "/src/libs/")));
962
+ } catch {
963
+ }
964
+ }
946
965
  console.error(error);
947
966
  return null;
948
967
  }
@@ -978,7 +997,14 @@ async function loadAsArrayBuffer(url) {
978
997
  const response = await fetch(url);
979
998
  return await response.arrayBuffer();
980
999
  }
981
- return await readFileAsArrayBuffer(url);
1000
+ try {
1001
+ return await readFileAsArrayBuffer(url);
1002
+ } catch {
1003
+ if (url.includes("/dist/libs/")) {
1004
+ return await readFileAsArrayBuffer(url.replace("/dist/libs/", "/src/libs/"));
1005
+ }
1006
+ throw new Error(`Failed to load ArrayBuffer from ${url}`);
1007
+ }
982
1008
  }
983
1009
  async function loadAsText(url) {
984
1010
  const { readFileAsText } = globalThis.loaders || {};
@@ -986,135 +1012,29 @@ async function loadAsText(url) {
986
1012
  const response = await fetch(url);
987
1013
  return await response.text();
988
1014
  }
989
- return await readFileAsText(url);
990
- }
991
-
992
- // dist/lib/process-utils/child-process-proxy.js
993
- var ChildProcess2 = __toESM(require("child_process"), 1);
994
-
995
- // dist/lib/process-utils/process-utils.js
996
- var import_child_process = __toESM(require("child_process"), 1);
997
- function getAvailablePort(defaultPort = 3e3) {
998
- return new Promise((resolve) => {
999
- import_child_process.default.exec("lsof -i -P -n | grep LISTEN", (error, stdout) => {
1000
- if (error) {
1001
- resolve(defaultPort);
1002
- return;
1003
- }
1004
- const portsInUse = [];
1005
- const regex = /:(\d+) \(LISTEN\)/;
1006
- stdout.split("\n").forEach((line) => {
1007
- const match = regex.exec(line);
1008
- if (match) {
1009
- portsInUse.push(Number(match[1]));
1010
- }
1011
- });
1012
- let port = defaultPort;
1013
- while (portsInUse.includes(port)) {
1014
- port++;
1015
- }
1016
- resolve(port);
1017
- });
1018
- });
1015
+ try {
1016
+ return await readFileAsText(url);
1017
+ } catch {
1018
+ if (url.includes("/dist/libs/")) {
1019
+ return await readFileAsText(url.replace("/dist/libs/", "/src/libs/"));
1020
+ }
1021
+ throw new Error(`Failed to load text from ${url}`);
1022
+ }
1019
1023
  }
1020
1024
 
1021
- // dist/lib/process-utils/child-process-proxy.js
1022
- var DEFAULT_PROPS2 = {
1023
- command: "",
1024
- arguments: [],
1025
- port: 5e3,
1026
- autoPort: true,
1027
- wait: 2e3,
1028
- onSuccess: (processProxy) => {
1029
- console.log(`Started ${processProxy.props.command}`);
1030
- }
1031
- };
1025
+ // dist/lib/process-utils/child-process-proxy.browser.js
1032
1026
  var ChildProcessProxy = class {
1033
- id;
1034
- props = { ...DEFAULT_PROPS2 };
1035
- childProcess = null;
1036
- port = 0;
1037
- successTimer;
1038
- // NodeJS.Timeout;
1039
- // constructor(props?: {id?: string});
1040
- constructor({ id = "browser-driver" } = {}) {
1041
- this.id = id;
1042
- }
1043
- /** Starts a child process with the provided props */
1027
+ constructor() {
1028
+ throw new Error("ChildProcessProxy is not available in browser environments");
1029
+ }
1044
1030
  async start(props) {
1045
- props = { ...DEFAULT_PROPS2, ...props };
1046
- this.props = props;
1047
- const args = [...props.arguments];
1048
- this.port = Number(props.port);
1049
- if (props.portArg) {
1050
- if (props.autoPort) {
1051
- this.port = await getAvailablePort(props.port);
1052
- }
1053
- args.push(props.portArg, String(this.port));
1054
- }
1055
- return await new Promise((resolve, reject) => {
1056
- try {
1057
- this._setTimeout(() => {
1058
- if (props.onSuccess) {
1059
- props.onSuccess(this);
1060
- }
1061
- resolve({});
1062
- });
1063
- console.log(`Spawning ${props.command} ${props.arguments.join(" ")}`);
1064
- const childProcess = ChildProcess2.spawn(props.command, args, props.spawn);
1065
- this.childProcess = childProcess;
1066
- childProcess.stdout.on("data", (data) => {
1067
- console.log(data.toString());
1068
- });
1069
- childProcess.stderr.on("data", (data) => {
1070
- console.log(`Child process wrote to stderr: "${data}".`);
1071
- if (!props.ignoreStderr) {
1072
- this._clearTimeout();
1073
- reject(new Error(data));
1074
- }
1075
- });
1076
- childProcess.on("error", (error) => {
1077
- console.log(`Child process errored with ${error}`);
1078
- this._clearTimeout();
1079
- reject(error);
1080
- });
1081
- childProcess.on("close", (code) => {
1082
- console.log(`Child process exited with ${code}`);
1083
- this.childProcess = null;
1084
- this._clearTimeout();
1085
- resolve({});
1086
- });
1087
- } catch (error) {
1088
- reject(error);
1089
- }
1090
- });
1031
+ return await Promise.resolve({});
1091
1032
  }
1092
- /** Stops a running child process */
1093
1033
  async stop() {
1094
- if (this.childProcess) {
1095
- this.childProcess.kill();
1096
- this.childProcess = null;
1097
- }
1098
- }
1099
- /** Exits this process */
1100
- async exit(statusCode = 0) {
1101
- try {
1102
- await this.stop();
1103
- process.exit(statusCode);
1104
- } catch (error) {
1105
- console.error(error.message || error);
1106
- process.exit(1);
1107
- }
1034
+ return await Promise.resolve();
1108
1035
  }
1109
- _setTimeout(callback) {
1110
- if (Number(this.props.wait) > 0) {
1111
- this.successTimer = setTimeout(callback, this.props.wait);
1112
- }
1113
- }
1114
- _clearTimeout() {
1115
- if (this.successTimer) {
1116
- clearTimeout(this.successTimer);
1117
- }
1036
+ async exit() {
1037
+ return await Promise.resolve();
1118
1038
  }
1119
1039
  };
1120
1040