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

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 (150) hide show
  1. package/dist/es5/index.js +19 -19
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/lib/async-queue/async-queue.js +62 -76
  4. package/dist/es5/lib/async-queue/async-queue.js.map +1 -1
  5. package/dist/es5/lib/env-utils/globals.js +10 -15
  6. package/dist/es5/lib/env-utils/globals.js.map +1 -1
  7. package/dist/es5/lib/env-utils/version.js +3 -3
  8. package/dist/es5/lib/env-utils/version.js.map +1 -1
  9. package/dist/es5/lib/library-utils/library-utils.js +29 -127
  10. package/dist/es5/lib/library-utils/library-utils.js.map +1 -1
  11. package/dist/es5/lib/node/require-utils.node.js +18 -67
  12. package/dist/es5/lib/node/require-utils.node.js.map +1 -1
  13. package/dist/es5/lib/process-utils/child-process-proxy.js +90 -199
  14. package/dist/es5/lib/process-utils/child-process-proxy.js.map +1 -1
  15. package/dist/es5/lib/process-utils/process-utils.js +8 -9
  16. package/dist/es5/lib/process-utils/process-utils.js.map +1 -1
  17. package/dist/es5/lib/worker-api/create-worker.js +52 -162
  18. package/dist/es5/lib/worker-api/create-worker.js.map +1 -1
  19. package/dist/es5/lib/worker-api/get-worker-url.js +9 -10
  20. package/dist/es5/lib/worker-api/get-worker-url.js.map +1 -1
  21. package/dist/es5/lib/worker-api/process-on-worker.js +59 -130
  22. package/dist/es5/lib/worker-api/process-on-worker.js.map +1 -1
  23. package/dist/es5/lib/worker-api/validate-worker-version.js +3 -4
  24. package/dist/es5/lib/worker-api/validate-worker-version.js.map +1 -1
  25. package/dist/es5/lib/worker-farm/worker-body.js +46 -58
  26. package/dist/es5/lib/worker-farm/worker-body.js.map +1 -1
  27. package/dist/es5/lib/worker-farm/worker-farm.js +60 -99
  28. package/dist/es5/lib/worker-farm/worker-farm.js.map +1 -1
  29. package/dist/es5/lib/worker-farm/worker-job.js +28 -40
  30. package/dist/es5/lib/worker-farm/worker-job.js.map +1 -1
  31. package/dist/es5/lib/worker-farm/worker-pool.js +108 -204
  32. package/dist/es5/lib/worker-farm/worker-pool.js.map +1 -1
  33. package/dist/es5/lib/worker-farm/worker-thread.js +67 -85
  34. package/dist/es5/lib/worker-farm/worker-thread.js.map +1 -1
  35. package/dist/es5/lib/worker-utils/get-loadable-worker-url.js +4 -4
  36. package/dist/es5/lib/worker-utils/get-loadable-worker-url.js.map +1 -1
  37. package/dist/es5/lib/worker-utils/get-transfer-list.js +4 -10
  38. package/dist/es5/lib/worker-utils/get-transfer-list.js.map +1 -1
  39. package/dist/es5/lib/worker-utils/remove-nontransferable-options.js +3 -7
  40. package/dist/es5/lib/worker-utils/remove-nontransferable-options.js.map +1 -1
  41. package/dist/es5/workers/null-worker.js +3 -26
  42. package/dist/es5/workers/null-worker.js.map +1 -1
  43. package/dist/esm/lib/env-utils/version.js +2 -2
  44. package/dist/esm/lib/env-utils/version.js.map +1 -1
  45. package/dist/esm/lib/library-utils/library-utils.js +5 -5
  46. package/dist/esm/lib/library-utils/library-utils.js.map +1 -1
  47. package/dist/esm/lib/node/require-utils.node.js +6 -10
  48. package/dist/esm/lib/node/require-utils.node.js.map +1 -1
  49. package/dist/esm/lib/process-utils/child-process-proxy.js +5 -5
  50. package/dist/esm/lib/process-utils/child-process-proxy.js.map +1 -1
  51. package/dist/esm/lib/worker-api/create-worker.js +1 -1
  52. package/dist/esm/lib/worker-api/create-worker.js.map +1 -1
  53. package/dist/esm/lib/worker-api/get-worker-url.js +7 -7
  54. package/dist/esm/lib/worker-api/get-worker-url.js.map +1 -1
  55. package/dist/esm/lib/worker-api/process-on-worker.js +2 -2
  56. package/dist/esm/lib/worker-api/process-on-worker.js.map +1 -1
  57. package/dist/esm/lib/worker-farm/worker-body.js.map +1 -1
  58. package/dist/esm/lib/worker-farm/worker-job.js.map +1 -1
  59. package/dist/esm/lib/worker-farm/worker-pool.js +1 -1
  60. package/dist/esm/lib/worker-farm/worker-pool.js.map +1 -1
  61. package/dist/esm/lib/worker-farm/worker-thread.js +3 -3
  62. package/dist/esm/lib/worker-farm/worker-thread.js.map +1 -1
  63. package/dist/esm/lib/worker-utils/get-loadable-worker-url.js +1 -7
  64. package/dist/esm/lib/worker-utils/get-loadable-worker-url.js.map +1 -1
  65. package/dist/esm/lib/worker-utils/get-transfer-list.js.map +1 -1
  66. package/dist/esm/lib/worker-utils/remove-nontransferable-options.js.map +1 -1
  67. package/dist/index.d.ts +20 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +58 -0
  70. package/dist/lib/async-queue/async-queue.d.ts +32 -0
  71. package/dist/lib/async-queue/async-queue.d.ts.map +1 -0
  72. package/dist/lib/async-queue/async-queue.js +87 -0
  73. package/dist/lib/env-utils/assert.d.ts +3 -0
  74. package/dist/lib/env-utils/assert.d.ts.map +1 -0
  75. package/dist/lib/env-utils/assert.js +13 -0
  76. package/dist/lib/env-utils/globals.d.ts +22 -0
  77. package/dist/lib/env-utils/globals.d.ts.map +1 -0
  78. package/dist/lib/env-utils/globals.js +32 -0
  79. package/dist/lib/env-utils/version.d.ts +2 -0
  80. package/dist/lib/env-utils/version.d.ts.map +1 -0
  81. package/dist/lib/env-utils/version.js +12 -0
  82. package/dist/lib/library-utils/library-utils.d.ts +18 -0
  83. package/dist/lib/library-utils/library-utils.d.ts.map +1 -0
  84. package/dist/lib/library-utils/library-utils.js +158 -0
  85. package/dist/lib/node/require-utils.node.d.ts +6 -0
  86. package/dist/lib/node/require-utils.node.d.ts.map +1 -0
  87. package/dist/lib/node/require-utils.node.js +60 -0
  88. package/dist/lib/process-utils/child-process-proxy.d.ts +43 -0
  89. package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -0
  90. package/dist/lib/process-utils/child-process-proxy.js +131 -0
  91. package/dist/lib/process-utils/process-utils.d.ts +2 -0
  92. package/dist/lib/process-utils/process-utils.d.ts.map +1 -0
  93. package/dist/lib/process-utils/process-utils.js +35 -0
  94. package/dist/lib/worker-api/create-worker.d.ts +9 -0
  95. package/dist/lib/worker-api/create-worker.d.ts.map +1 -0
  96. package/dist/lib/worker-api/create-worker.js +92 -0
  97. package/dist/lib/worker-api/get-worker-url.d.ts +14 -0
  98. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -0
  99. package/dist/lib/worker-api/get-worker-url.js +48 -0
  100. package/dist/lib/worker-api/process-on-worker.d.ts +20 -0
  101. package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -0
  102. package/dist/lib/worker-api/process-on-worker.js +79 -0
  103. package/dist/lib/worker-api/validate-worker-version.d.ts +9 -0
  104. package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -0
  105. package/dist/lib/worker-api/validate-worker-version.js +35 -0
  106. package/dist/lib/worker-farm/worker-body.d.ts +16 -0
  107. package/dist/lib/worker-farm/worker-body.d.ts.map +1 -0
  108. package/dist/lib/worker-farm/worker-body.js +67 -0
  109. package/dist/lib/worker-farm/worker-farm.d.ts +55 -0
  110. package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -0
  111. package/dist/lib/worker-farm/worker-farm.js +88 -0
  112. package/dist/lib/worker-farm/worker-job.d.ts +29 -0
  113. package/dist/lib/worker-farm/worker-job.d.ts.map +1 -0
  114. package/dist/lib/worker-farm/worker-job.js +47 -0
  115. package/dist/lib/worker-farm/worker-pool.d.ts +75 -0
  116. package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -0
  117. package/dist/lib/worker-farm/worker-pool.js +151 -0
  118. package/dist/lib/worker-farm/worker-thread.d.ts +42 -0
  119. package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -0
  120. package/dist/lib/worker-farm/worker-thread.js +95 -0
  121. package/dist/lib/worker-utils/get-loadable-worker-url.d.ts +14 -0
  122. package/dist/lib/worker-utils/get-loadable-worker-url.d.ts.map +1 -0
  123. package/dist/lib/worker-utils/get-loadable-worker-url.js +74 -0
  124. package/dist/lib/worker-utils/get-transfer-list.d.ts +10 -0
  125. package/dist/lib/worker-utils/get-transfer-list.d.ts.map +1 -0
  126. package/dist/lib/worker-utils/get-transfer-list.js +61 -0
  127. package/dist/lib/worker-utils/remove-nontransferable-options.d.ts +6 -0
  128. package/dist/lib/worker-utils/remove-nontransferable-options.d.ts.map +1 -0
  129. package/dist/lib/worker-utils/remove-nontransferable-options.js +35 -0
  130. package/dist/null-worker.js +189 -550
  131. package/dist/null-worker.js.map +7 -1
  132. package/dist/types.d.ts +56 -0
  133. package/dist/types.d.ts.map +1 -0
  134. package/dist/types.js +2 -0
  135. package/dist/workers/null-worker.d.ts +2 -0
  136. package/dist/workers/null-worker.d.ts.map +1 -0
  137. package/dist/workers/null-worker.js +7 -0
  138. package/package.json +8 -6
  139. package/src/lib/env-utils/version.ts +1 -1
  140. package/src/lib/library-utils/library-utils.ts +6 -4
  141. package/src/lib/node/{require-utils.node.js → require-utils.node.ts} +13 -9
  142. package/src/lib/process-utils/child-process-proxy.ts +1 -1
  143. package/src/lib/worker-api/create-worker.ts +11 -7
  144. package/src/lib/worker-api/process-on-worker.ts +1 -1
  145. package/src/lib/worker-farm/worker-body.ts +2 -2
  146. package/src/lib/worker-farm/worker-job.ts +2 -2
  147. package/src/lib/worker-farm/worker-thread.ts +2 -2
  148. package/src/lib/worker-utils/get-transfer-list.ts +1 -1
  149. package/src/lib/worker-utils/remove-nontransferable-options.ts +1 -1
  150. package/src/types.ts +1 -1
@@ -0,0 +1,151 @@
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
+ const globals_1 = require("../env-utils/globals");
7
+ const worker_thread_1 = __importDefault(require("./worker-thread"));
8
+ const worker_job_1 = __importDefault(require("./worker-job"));
9
+ /**
10
+ * Process multiple data messages with small pool of identical workers
11
+ */
12
+ class WorkerPool {
13
+ /**
14
+ * @param processor - worker function
15
+ * @param maxConcurrency - max count of workers
16
+ */
17
+ constructor(props) {
18
+ this.name = 'unnamed';
19
+ this.maxConcurrency = 1;
20
+ this.maxMobileConcurrency = 1;
21
+ this.onDebug = () => { };
22
+ this.reuseWorkers = true;
23
+ this.props = {};
24
+ this.jobQueue = [];
25
+ this.idleQueue = [];
26
+ this.count = 0;
27
+ this.isDestroyed = false;
28
+ this.source = props.source;
29
+ this.url = props.url;
30
+ this.setProps(props);
31
+ }
32
+ /**
33
+ * Terminates all workers in the pool
34
+ * @note Can free up significant memory
35
+ */
36
+ destroy() {
37
+ // Destroy idle workers, active Workers will be destroyed on completion
38
+ this.idleQueue.forEach((worker) => worker.destroy());
39
+ this.isDestroyed = true;
40
+ }
41
+ setProps(props) {
42
+ this.props = { ...this.props, ...props };
43
+ if (props.name !== undefined) {
44
+ this.name = props.name;
45
+ }
46
+ if (props.maxConcurrency !== undefined) {
47
+ this.maxConcurrency = props.maxConcurrency;
48
+ }
49
+ if (props.maxMobileConcurrency !== undefined) {
50
+ this.maxMobileConcurrency = props.maxMobileConcurrency;
51
+ }
52
+ if (props.reuseWorkers !== undefined) {
53
+ this.reuseWorkers = props.reuseWorkers;
54
+ }
55
+ if (props.onDebug !== undefined) {
56
+ this.onDebug = props.onDebug;
57
+ }
58
+ }
59
+ async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
60
+ // Promise resolves when thread starts working on this job
61
+ const startPromise = new Promise((onStart) => {
62
+ // Promise resolves when thread completes or fails working on this job
63
+ this.jobQueue.push({ name, onMessage, onError, onStart });
64
+ return this;
65
+ });
66
+ this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
67
+ return await startPromise;
68
+ }
69
+ // PRIVATE
70
+ /**
71
+ * Starts first queued job if worker is available or can be created
72
+ * Called when job is started and whenever a worker returns to the idleQueue
73
+ */
74
+ async _startQueuedJob() {
75
+ if (!this.jobQueue.length) {
76
+ return;
77
+ }
78
+ const workerThread = this._getAvailableWorker();
79
+ if (!workerThread) {
80
+ return;
81
+ }
82
+ // We have a worker, dequeue and start the job
83
+ const queuedJob = this.jobQueue.shift();
84
+ if (queuedJob) {
85
+ // Emit a debug event
86
+ // @ts-ignore
87
+ this.onDebug({
88
+ message: 'Starting job',
89
+ name: queuedJob.name,
90
+ workerThread,
91
+ backlog: this.jobQueue.length
92
+ });
93
+ // Create a worker job to let the app access thread and manage job completion
94
+ const job = new worker_job_1.default(queuedJob.name, workerThread);
95
+ // Set the worker thread's message handlers
96
+ workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);
97
+ workerThread.onError = (error) => queuedJob.onError(job, error);
98
+ // Resolve the start promise so that the app can start sending messages to worker
99
+ queuedJob.onStart(job);
100
+ // Wait for the app to signal that the job is complete, then return worker to queue
101
+ try {
102
+ await job.result;
103
+ }
104
+ finally {
105
+ this.returnWorkerToQueue(workerThread);
106
+ }
107
+ }
108
+ }
109
+ /**
110
+ * Returns a worker to the idle queue
111
+ * Destroys the worker if
112
+ * - pool is destroyed
113
+ * - if this pool doesn't reuse workers
114
+ * - if maxConcurrency has been lowered
115
+ * @param worker
116
+ */
117
+ returnWorkerToQueue(worker) {
118
+ const shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency();
119
+ if (shouldDestroyWorker) {
120
+ worker.destroy();
121
+ this.count--;
122
+ }
123
+ else {
124
+ this.idleQueue.push(worker);
125
+ }
126
+ if (!this.isDestroyed) {
127
+ this._startQueuedJob(); // eslint-disable-line @typescript-eslint/no-floating-promises
128
+ }
129
+ }
130
+ /**
131
+ * Returns idle worker or creates new worker if maxConcurrency has not been reached
132
+ */
133
+ _getAvailableWorker() {
134
+ // If a worker has completed and returned to the queue, it can be used
135
+ if (this.idleQueue.length > 0) {
136
+ return this.idleQueue.shift() || null;
137
+ }
138
+ // Create fresh worker if we haven't yet created the max amount of worker threads for this worker source
139
+ if (this.count < this._getMaxConcurrency()) {
140
+ this.count++;
141
+ const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
142
+ return new worker_thread_1.default({ name, source: this.source, url: this.url });
143
+ }
144
+ // No worker available, have to wait
145
+ return null;
146
+ }
147
+ _getMaxConcurrency() {
148
+ return globals_1.isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
149
+ }
150
+ }
151
+ exports.default = WorkerPool;
@@ -0,0 +1,42 @@
1
+ export declare type WorkerThreadProps = {
2
+ name: string;
3
+ source?: string;
4
+ url?: string;
5
+ };
6
+ /**
7
+ * Represents one worker thread
8
+ */
9
+ export default class WorkerThread {
10
+ readonly name: string;
11
+ readonly source: string | undefined;
12
+ readonly url: string | undefined;
13
+ terminated: boolean;
14
+ worker: Worker;
15
+ onMessage: (message: any) => void;
16
+ onError: (error: Error) => void;
17
+ private _loadableURL;
18
+ static isSupported(): boolean;
19
+ constructor(props: WorkerThreadProps);
20
+ /**
21
+ * Terminate this worker thread
22
+ * @note Can free up significant memory
23
+ */
24
+ destroy(): void;
25
+ get isRunning(): boolean;
26
+ /**
27
+ * Send a message to this worker thread
28
+ * @param data any data structure, ideally consisting mostly of transferrable objects
29
+ * @param transferList If not supplied, calculated automatically by traversing data
30
+ */
31
+ postMessage(data: any, transferList?: any[]): void;
32
+ /**
33
+ * Generate a standard Error from an ErrorEvent
34
+ * @param {ErrorEvent} event
35
+ */
36
+ _getErrorFromErrorEvent(event: ErrorEvent): Error;
37
+ /**
38
+ * Creates a worker thread on the browser
39
+ */
40
+ _createBrowserWorker(): Worker;
41
+ }
42
+ //# sourceMappingURL=worker-thread.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-thread.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-farm/worker-thread.ts"],"names":[],"mappings":"AAMA,oBAAY,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,UAAU,EAAE,OAAO,CAAS;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAEhC,OAAO,CAAC,YAAY,CAAc;IAElC,MAAM,CAAC,WAAW,IAAI,OAAO;gBAIjB,KAAK,EAAE,iBAAiB;IAYpC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAQf,IAAI,SAAS,YAEZ;IAED;;;;OAIG;IACH,WAAW,CAAC,IAAI,EAAE,GAAG,EAAE,YAAY,CAAC,EAAE,GAAG,EAAE,GAAG,IAAI;IAQlD;;;OAGG;IACH,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK;IAiBjD;;OAEG;IACH,oBAAoB;CAqBrB"}
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const assert_1 = require("../env-utils/assert");
4
+ const get_loadable_worker_url_1 = require("../worker-utils/get-loadable-worker-url");
5
+ const get_transfer_list_1 = require("../worker-utils/get-transfer-list");
6
+ const NOOP = () => { };
7
+ /**
8
+ * Represents one worker thread
9
+ */
10
+ class WorkerThread {
11
+ constructor(props) {
12
+ this.terminated = false;
13
+ this._loadableURL = '';
14
+ const { name, source, url } = props;
15
+ (0, assert_1.assert)(source || url); // Either source or url must be defined
16
+ this.name = name;
17
+ this.source = source;
18
+ this.url = url;
19
+ this.onMessage = NOOP;
20
+ this.onError = (error) => console.log(error); // eslint-disable-line
21
+ this.worker = this._createBrowserWorker();
22
+ }
23
+ static isSupported() {
24
+ return typeof Worker !== 'undefined';
25
+ }
26
+ /**
27
+ * Terminate this worker thread
28
+ * @note Can free up significant memory
29
+ */
30
+ destroy() {
31
+ this.onMessage = NOOP;
32
+ this.onError = NOOP;
33
+ // @ts-ignore
34
+ this.worker.terminate();
35
+ this.terminated = true;
36
+ }
37
+ get isRunning() {
38
+ return Boolean(this.onMessage);
39
+ }
40
+ /**
41
+ * Send a message to this worker thread
42
+ * @param data any data structure, ideally consisting mostly of transferrable objects
43
+ * @param transferList If not supplied, calculated automatically by traversing data
44
+ */
45
+ postMessage(data, transferList) {
46
+ transferList = transferList || (0, get_transfer_list_1.getTransferList)(data);
47
+ // @ts-ignore
48
+ this.worker.postMessage(data, transferList);
49
+ }
50
+ // PRIVATE
51
+ /**
52
+ * Generate a standard Error from an ErrorEvent
53
+ * @param {ErrorEvent} event
54
+ */
55
+ _getErrorFromErrorEvent(event) {
56
+ // Note Error object does not have the expected fields if loading failed completely
57
+ // https://developer.mozilla.org/en-US/docs/Web/API/Worker#Event_handlers
58
+ // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
59
+ let message = 'Failed to load ';
60
+ message += `worker ${this.name} from ${this.url}. `;
61
+ if (event.message) {
62
+ message += `${event.message} in `;
63
+ }
64
+ // const hasFilename = event.filename && !event.filename.startsWith('blob:');
65
+ // message += hasFilename ? event.filename : this.source.slice(0, 100);
66
+ if (event.lineno) {
67
+ message += `:${event.lineno}:${event.colno}`;
68
+ }
69
+ return new Error(message);
70
+ }
71
+ /**
72
+ * Creates a worker thread on the browser
73
+ */
74
+ _createBrowserWorker() {
75
+ this._loadableURL = (0, get_loadable_worker_url_1.getLoadableWorkerURL)({ source: this.source, url: this.url });
76
+ const worker = new Worker(this._loadableURL, { name: this.name });
77
+ worker.onmessage = (event) => {
78
+ if (!event.data) {
79
+ this.onError(new Error('No data received'));
80
+ }
81
+ else {
82
+ this.onMessage(event.data);
83
+ }
84
+ };
85
+ // This callback represents an uncaught exception in the worker thread
86
+ worker.onerror = (error) => {
87
+ this.onError(this._getErrorFromErrorEvent(error));
88
+ this.terminated = true;
89
+ };
90
+ // TODO - not clear when this would be called, for now just log in case it happens
91
+ worker.onmessageerror = (event) => console.error(event); // eslint-disable-line
92
+ return worker;
93
+ }
94
+ }
95
+ exports.default = WorkerThread;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Creates a loadable URL from worker source or URL
3
+ * that can be used to create `Worker` instances.
4
+ * Due to CORS issues it may be necessary to wrap a URL in a small importScripts
5
+ * @param props
6
+ * @param props.source Worker source
7
+ * @param props.url Worker URL
8
+ * @returns loadable url
9
+ */
10
+ export declare function getLoadableWorkerURL(props: {
11
+ source?: string;
12
+ url?: string;
13
+ }): any;
14
+ //# sourceMappingURL=get-loadable-worker-url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-loadable-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-utils/get-loadable-worker-url.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAC,OAmB1E"}
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLoadableWorkerURL = void 0;
4
+ const assert_1 = require("../env-utils/assert");
5
+ const workerURLCache = new Map();
6
+ /**
7
+ * Creates a loadable URL from worker source or URL
8
+ * that can be used to create `Worker` instances.
9
+ * Due to CORS issues it may be necessary to wrap a URL in a small importScripts
10
+ * @param props
11
+ * @param props.source Worker source
12
+ * @param props.url Worker URL
13
+ * @returns loadable url
14
+ */
15
+ function getLoadableWorkerURL(props) {
16
+ (0, assert_1.assert)((props.source && !props.url) || (!props.source && props.url)); // Either source or url must be defined
17
+ let workerURL = workerURLCache.get(props.source || props.url);
18
+ if (!workerURL) {
19
+ // Differentiate worker urls from worker source code
20
+ if (props.url) {
21
+ workerURL = getLoadableWorkerURLFromURL(props.url);
22
+ workerURLCache.set(props.url, workerURL);
23
+ }
24
+ if (props.source) {
25
+ workerURL = getLoadableWorkerURLFromSource(props.source);
26
+ workerURLCache.set(props.source, workerURL);
27
+ }
28
+ }
29
+ (0, assert_1.assert)(workerURL);
30
+ return workerURL;
31
+ }
32
+ exports.getLoadableWorkerURL = getLoadableWorkerURL;
33
+ /**
34
+ * Build a loadable worker URL from worker URL
35
+ * @param url
36
+ * @returns loadable URL
37
+ */
38
+ function getLoadableWorkerURLFromURL(url) {
39
+ // A local script url, we can use it to initialize a Worker directly
40
+ if (!url.startsWith('http')) {
41
+ return url;
42
+ }
43
+ // A remote script, we need to use `importScripts` to load from different origin
44
+ const workerSource = buildScriptSource(url);
45
+ return getLoadableWorkerURLFromSource(workerSource);
46
+ }
47
+ /**
48
+ * Build a loadable worker URL from worker source
49
+ * @param workerSource
50
+ * @returns loadable url
51
+ */
52
+ function getLoadableWorkerURLFromSource(workerSource) {
53
+ // NOTE: webworkify was previously used
54
+ // const blob = webworkify(workerSource, {bare: true});
55
+ const blob = new Blob([workerSource], { type: 'application/javascript' });
56
+ return URL.createObjectURL(blob);
57
+ }
58
+ /**
59
+ * Per spec, worker cannot be initialized with a script from a different origin
60
+ * However a local worker script can still import scripts from other origins,
61
+ * so we simply build a wrapper script.
62
+ *
63
+ * @param workerUrl
64
+ * @returns source
65
+ */
66
+ function buildScriptSource(workerUrl) {
67
+ return `\
68
+ try {
69
+ importScripts('${workerUrl}');
70
+ } catch (error) {
71
+ console.error(error);
72
+ throw error;
73
+ }`;
74
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns an array of Transferrable objects that can be used with postMessage
3
+ * https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
4
+ * @param object data to be sent via postMessage
5
+ * @param recursive - not for application use
6
+ * @param transfers - not for application use
7
+ * @returns a transfer list that can be passed to postMessage
8
+ */
9
+ export declare function getTransferList(object: any, recursive?: boolean, transfers?: Set<any>): Transferable[];
10
+ //# sourceMappingURL=get-transfer-list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-transfer-list.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-utils/get-transfer-list.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,GAAG,EACX,SAAS,GAAE,OAAc,EACzB,SAAS,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GACnB,YAAY,EAAE,CAwBhB"}
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ // NOTE - there is a copy of this function is both in core and loader-utils
3
+ // core does not need all the utils in loader-utils, just this one.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.getTransferList = void 0;
6
+ /**
7
+ * Returns an array of Transferrable objects that can be used with postMessage
8
+ * https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
9
+ * @param object data to be sent via postMessage
10
+ * @param recursive - not for application use
11
+ * @param transfers - not for application use
12
+ * @returns a transfer list that can be passed to postMessage
13
+ */
14
+ function getTransferList(object, recursive = true, transfers) {
15
+ // Make sure that items in the transfer list is unique
16
+ const transfersSet = transfers || new Set();
17
+ if (!object) {
18
+ // ignore
19
+ }
20
+ else if (isTransferable(object)) {
21
+ transfersSet.add(object);
22
+ }
23
+ else if (isTransferable(object.buffer)) {
24
+ // Typed array
25
+ transfersSet.add(object.buffer);
26
+ }
27
+ else if (ArrayBuffer.isView(object)) {
28
+ // object is a TypeArray viewing into a SharedArrayBuffer (not transferable)
29
+ // Do not iterate through the content in this case
30
+ }
31
+ else if (recursive && typeof object === 'object') {
32
+ for (const key in object) {
33
+ // Avoid perf hit - only go one level deep
34
+ getTransferList(object[key], recursive, transfersSet);
35
+ }
36
+ }
37
+ // If transfers is defined, is internal recursive call
38
+ // Otherwise it's called by the user
39
+ return transfers === undefined ? Array.from(transfersSet) : [];
40
+ }
41
+ exports.getTransferList = getTransferList;
42
+ // https://developer.mozilla.org/en-US/docs/Web/API/Transferable
43
+ function isTransferable(object) {
44
+ if (!object) {
45
+ return false;
46
+ }
47
+ if (object instanceof ArrayBuffer) {
48
+ return true;
49
+ }
50
+ if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) {
51
+ return true;
52
+ }
53
+ if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) {
54
+ return true;
55
+ }
56
+ // @ts-ignore
57
+ if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) {
58
+ return true;
59
+ }
60
+ return false;
61
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Safely stringify JSON (drop non serializable values like functions and regexps)
3
+ * @param value
4
+ */
5
+ export declare function removeNontransferableOptions(object: object): object;
6
+ //# sourceMappingURL=remove-nontransferable-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove-nontransferable-options.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-utils/remove-nontransferable-options.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAKnE"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeNontransferableOptions = void 0;
4
+ /**
5
+ * Safely stringify JSON (drop non serializable values like functions and regexps)
6
+ * @param value
7
+ */
8
+ function removeNontransferableOptions(object) {
9
+ // options.log object contains functions which cannot be transferred
10
+ // TODO - decide how to handle logging on workers
11
+ // TODO - warn if options stringification is long
12
+ return JSON.parse(stringifyJSON(object));
13
+ }
14
+ exports.removeNontransferableOptions = removeNontransferableOptions;
15
+ function stringifyJSON(v) {
16
+ const cache = new Set();
17
+ return JSON.stringify(v, (key, value) => {
18
+ if (typeof value === 'object' && value !== null) {
19
+ if (cache.has(value)) {
20
+ // Circular reference found
21
+ try {
22
+ // If this value does not reference a parent it can be deduped
23
+ return JSON.parse(JSON.stringify(value));
24
+ }
25
+ catch (err) {
26
+ // discard key if value cannot be deduped
27
+ return undefined;
28
+ }
29
+ }
30
+ // Store value in our set
31
+ cache.add(value);
32
+ }
33
+ return value;
34
+ });
35
+ }