@atlaspack/workers 2.14.21 → 2.14.22-typescript-5ad950d33.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 (51) hide show
  1. package/LICENSE +201 -0
  2. package/index.d.ts +94 -2
  3. package/lib/Handle.d.ts +19 -0
  4. package/lib/Handle.js +0 -3
  5. package/lib/Worker.d.ts +40 -0
  6. package/lib/Worker.js +7 -1
  7. package/lib/WorkerFarm.d.ts +93 -0
  8. package/lib/WorkerFarm.js +17 -7
  9. package/lib/backend.d.ts +4 -0
  10. package/lib/backend.js +5 -1
  11. package/lib/bus.d.ts +6 -0
  12. package/lib/bus.js +1 -1
  13. package/lib/child.d.ts +43 -0
  14. package/lib/child.js +11 -4
  15. package/lib/childState.d.ts +3 -0
  16. package/lib/cpuCount.d.ts +2 -0
  17. package/lib/cpuCount.js +6 -2
  18. package/lib/index.d.ts +6 -0
  19. package/lib/process/ProcessChild.d.ts +9 -0
  20. package/lib/process/ProcessChild.js +6 -1
  21. package/lib/process/ProcessWorker.d.ts +16 -0
  22. package/lib/process/ProcessWorker.js +9 -2
  23. package/lib/threads/ThreadsChild.d.ts +8 -0
  24. package/lib/threads/ThreadsChild.js +3 -0
  25. package/lib/threads/ThreadsWorker.d.ts +15 -0
  26. package/lib/threads/ThreadsWorker.js +10 -2
  27. package/lib/types.d.ts +52 -0
  28. package/lib/web/WebChild.d.ts +8 -0
  29. package/lib/web/WebChild.js +6 -1
  30. package/lib/web/WebWorker.d.ts +15 -0
  31. package/lib/web/WebWorker.js +20 -4
  32. package/package.json +16 -12
  33. package/src/{Handle.js → Handle.ts} +11 -11
  34. package/src/{Worker.js → Worker.ts} +66 -54
  35. package/src/{WorkerFarm.js → WorkerFarm.ts} +198 -141
  36. package/src/{backend.js → backend.ts} +6 -3
  37. package/src/{bus.js → bus.ts} +2 -3
  38. package/src/{child.js → child.ts} +55 -43
  39. package/src/{childState.js → childState.ts} +1 -2
  40. package/src/{cpuCount.js → cpuCount.ts} +10 -7
  41. package/src/{index.js → index.ts} +0 -1
  42. package/src/process/{ProcessChild.js → ProcessChild.ts} +5 -3
  43. package/src/process/{ProcessWorker.js → ProcessWorker.ts} +10 -7
  44. package/src/threads/{ThreadsChild.js → ThreadsChild.ts} +2 -2
  45. package/src/threads/{ThreadsWorker.js → ThreadsWorker.ts} +14 -8
  46. package/src/{types.js → types.ts} +34 -35
  47. package/src/web/{WebChild.js → WebChild.ts} +6 -2
  48. package/src/web/{WebWorker.js → WebWorker.ts} +19 -7
  49. package/test/{cpuCount.test.js → cpuCount.test.ts} +0 -1
  50. package/test/{workerfarm.test.cjs → workerfarm.test.js} +4 -2
  51. package/tsconfig.json +4 -0
package/lib/child.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { CallRequest, WorkerMessage, WorkerRequest, WorkerResponse, ChildImpl } from './types';
2
+ import type { Async, IDisposable } from '@atlaspack/types-internal';
3
+ import type { SharedReference } from './WorkerFarm';
4
+ import { SamplingProfiler } from '@atlaspack/profiler';
5
+ export type Class<T> = new (...args: any[]) => T;
6
+ type ChildCall = WorkerRequest & {
7
+ resolve: (result: Promise<any> | any) => void;
8
+ reject: (error?: any) => void;
9
+ };
10
+ export declare class Child {
11
+ callQueue: Array<ChildCall>;
12
+ childId: number | null | undefined;
13
+ maxConcurrentCalls: number;
14
+ module: any | null | undefined;
15
+ responseId: number;
16
+ responseQueue: Map<number, ChildCall>;
17
+ loggerDisposable: IDisposable;
18
+ tracerDisposable: IDisposable;
19
+ child: ChildImpl;
20
+ profiler: SamplingProfiler | null | undefined;
21
+ handles: Map<number, Handle>;
22
+ sharedReferences: Map<SharedReference, unknown>;
23
+ sharedReferencesByValue: Map<unknown, SharedReference>;
24
+ constructor(ChildBackend: Class<ChildImpl>);
25
+ workerApi: {
26
+ callMaster: (request: CallRequest, awaitResponse?: boolean | null | undefined) => Promise<unknown>;
27
+ createReverseHandle: (fn: (...args: Array<any>) => unknown) => Handle;
28
+ getSharedReference: (ref: SharedReference) => unknown;
29
+ resolveSharedReference: (value: unknown) => undefined | SharedReference;
30
+ runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
31
+ };
32
+ messageListener(message: WorkerMessage): Async<void>;
33
+ send(data: WorkerMessage): void;
34
+ childInit(module: string, childId: number): Promise<void>;
35
+ handleRequest(data: WorkerRequest): Promise<void>;
36
+ handleResponse(data: WorkerResponse): void;
37
+ addCall(request: CallRequest, awaitResponse?: boolean | null): Promise<unknown>;
38
+ sendRequest(call: ChildCall): void;
39
+ processQueue(): void;
40
+ handleEnd(): void;
41
+ createReverseHandle(fn: (...args: Array<any>) => unknown): Handle;
42
+ }
43
+ export {};
package/lib/child.js CHANGED
@@ -51,6 +51,9 @@ var _Handle2 = _interopRequireDefault(require("./Handle"));
51
51
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
52
52
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
53
53
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
54
+ // flow-to-ts helpers
55
+
56
+ // /flow-to-ts helpers
54
57
  // The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
55
58
  const Handle = _Handle2.default;
56
59
  class Child {
@@ -58,6 +61,7 @@ class Child {
58
61
  maxConcurrentCalls = 10;
59
62
  responseId = 0;
60
63
  responseQueue = new Map();
64
+ // @ts-expect-error TS2749
61
65
  handles = new Map();
62
66
  sharedReferences = new Map();
63
67
  sharedReferencesByValue = new Map();
@@ -78,7 +82,9 @@ class Child {
78
82
  }
79
83
  workerApi = {
80
84
  callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
85
+ // @ts-expect-error TS2749
81
86
  createReverseHandle: fn => this.createReverseHandle(fn),
87
+ // @ts-expect-error TS2749
82
88
  runHandle: (handle, args) => this.workerApi.callMaster({
83
89
  handle: handle.id,
84
90
  args
@@ -97,7 +103,6 @@ class Child {
97
103
  this.child.send(data);
98
104
  }
99
105
  async childInit(module, childId) {
100
- // $FlowFixMe
101
106
  this.module = require(module);
102
107
  this.childId = childId;
103
108
  if (this.module.childInit != null) {
@@ -189,7 +194,7 @@ class Child {
189
194
  } else {
190
195
  try {
191
196
  result = responseFromContent(
192
- // $FlowFixMe
197
+ // @ts-expect-error TS2538
193
198
  await this.module[method](this.workerApi, ...args));
194
199
  } catch (e) {
195
200
  result = errorResponseFromError(e);
@@ -222,12 +227,11 @@ class Child {
222
227
 
223
228
  // Keep in mind to make sure responses to these calls are JSON.Stringify safe
224
229
  addCall(request, awaitResponse = true) {
225
- // $FlowFixMe
226
230
  let call = {
227
231
  ...request,
228
232
  type: 'request',
229
233
  child: this.childId,
230
- // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
234
+ // @ts-expect-error TS2322
231
235
  awaitResponse,
232
236
  resolve: () => {},
233
237
  reject: () => {}
@@ -265,6 +269,7 @@ class Child {
265
269
  return;
266
270
  }
267
271
  if (this.responseQueue.size < this.maxConcurrentCalls) {
272
+ // @ts-expect-error TS2345
268
273
  this.sendRequest(this.callQueue.shift());
269
274
  }
270
275
  }
@@ -272,6 +277,8 @@ class Child {
272
277
  this.loggerDisposable.dispose();
273
278
  this.tracerDisposable.dispose();
274
279
  }
280
+
281
+ // @ts-expect-error TS2749
275
282
  createReverseHandle(fn) {
276
283
  let handle = new Handle({
277
284
  fn,
@@ -0,0 +1,3 @@
1
+ import type { Child } from './child';
2
+ export declare let child: Child | null | undefined;
3
+ export declare function setChild(c: Child): void;
@@ -0,0 +1,2 @@
1
+ export declare function detectRealCores(): number;
2
+ export default function getCores(bypassCache?: boolean): number;
package/lib/cpuCount.js CHANGED
@@ -50,18 +50,22 @@ function detectRealCores() {
50
50
  }
51
51
  return amount;
52
52
  }
53
+
54
+ // @ts-expect-error TS7034
53
55
  let cores;
54
56
  function getCores(bypassCache = false) {
55
57
  // Do not re-run commands if we already have the count...
58
+ // @ts-expect-error TS7005
56
59
  if (cores && !bypassCache) {
57
60
  return cores;
58
61
  }
59
62
 
60
- // $FlowFixMe
63
+ // @ts-expect-error TS2339
61
64
  if (process.browser) {
62
- // eslint-disable-next-line no-undef
63
65
  cores = navigator.hardwareConcurrency / 2;
64
66
  }
67
+
68
+ // @ts-expect-error TS7005
65
69
  if (!cores) {
66
70
  try {
67
71
  cores = detectRealCores();
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import WorkerFarm from './WorkerFarm';
2
+ import bus from './bus';
3
+ export default WorkerFarm;
4
+ export { bus };
5
+ export { Handle } from './WorkerFarm';
6
+ export type { WorkerApi, FarmOptions, SharedReference } from './WorkerFarm';
@@ -0,0 +1,9 @@
1
+ import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
2
+ export default class ProcessChild implements ChildImpl {
3
+ onMessage: MessageHandler;
4
+ onExit: ExitHandler;
5
+ constructor(onMessage: MessageHandler, onExit: ExitHandler);
6
+ handleMessage(data: string): void;
7
+ send(data: WorkerMessage): void;
8
+ stop(): void;
9
+ }
@@ -21,6 +21,7 @@ function _nullthrows() {
21
21
  var _child = require("../child");
22
22
  var _childState = require("../childState");
23
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
+ // @ts-expect-error TS2420
24
25
  class ProcessChild {
25
26
  constructor(onMessage, onExit) {
26
27
  if (!process.send) {
@@ -28,6 +29,7 @@ class ProcessChild {
28
29
  }
29
30
  this.onMessage = onMessage;
30
31
  this.onExit = onExit;
32
+ // @ts-expect-error TS2345
31
33
  process.on('message', data => this.handleMessage(data));
32
34
  }
33
35
  handleMessage(data) {
@@ -38,9 +40,10 @@ class ProcessChild {
38
40
  }
39
41
  send(data) {
40
42
  let processSend = (0, _nullthrows().default)(process.send).bind(process);
43
+ // @ts-expect-error TS7006
41
44
  processSend((0, _buildCache().serialize)(data).toString('base64'), err => {
42
45
  if (err && err instanceof Error) {
43
- // $FlowFixMe[prop-missing]
46
+ // @ts-expect-error TS2339
44
47
  if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
45
48
  // IPC connection closed
46
49
  // no need to keep the worker running if it can't send or receive data
@@ -54,5 +57,7 @@ class ProcessChild {
54
57
  process.exit();
55
58
  }
56
59
  }
60
+
61
+ // @ts-expect-error TS2345
57
62
  exports.default = ProcessChild;
58
63
  (0, _childState.setChild)(new _child.Child(ProcessChild));
@@ -0,0 +1,16 @@
1
+ import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
2
+ import { ChildProcess } from 'child_process';
3
+ export declare let WORKER_PATH: string;
4
+ export default class ProcessWorker implements WorkerImpl {
5
+ execArgv: any;
6
+ onMessage: MessageHandler;
7
+ onError: ErrorHandler;
8
+ onExit: ExitHandler;
9
+ child: ChildProcess;
10
+ processQueue: boolean;
11
+ sendQueue: Array<any>;
12
+ constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
13
+ start(): Promise<void>;
14
+ stop(): Promise<void>;
15
+ send(data: WorkerMessage): void;
16
+ }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.WORKER_PATH = void 0;
7
7
  function _child_process() {
8
8
  const data = _interopRequireDefault(require("child_process"));
9
9
  _child_process = function () {
@@ -26,8 +26,15 @@ function _buildCache() {
26
26
  return data;
27
27
  }
28
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
- const WORKER_PATH = _path().default.join(__dirname, './ProcessChild.js');
29
+ let WORKER_PATH = exports.WORKER_PATH = _path().default.join(__dirname, 'ProcessChild.js');
30
+ if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
31
+ exports.WORKER_PATH = WORKER_PATH = _path().default.join(__dirname, 'ProcessChild.ts');
32
+ }
33
+
34
+ // @ts-expect-error TS2420
30
35
  class ProcessWorker {
36
+ // @ts-expect-error TS2564
37
+
31
38
  processQueue = true;
32
39
  sendQueue = [];
33
40
  constructor(execArgv, onMessage, onError, onExit) {
@@ -0,0 +1,8 @@
1
+ import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
2
+ export default class ThreadsChild implements ChildImpl {
3
+ onMessage: MessageHandler;
4
+ onExit: ExitHandler;
5
+ constructor(onMessage: MessageHandler, onExit: ExitHandler);
6
+ handleMessage(data: WorkerMessage): void;
7
+ send(data: WorkerMessage): void;
8
+ }
@@ -28,6 +28,7 @@ function _nullthrows() {
28
28
  var _child = require("../child");
29
29
  var _childState = require("../childState");
30
30
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
+ // @ts-expect-error TS2420
31
32
  class ThreadsChild {
32
33
  constructor(onMessage, onExit) {
33
34
  if (_worker_threads().isMainThread || !_worker_threads().parentPort) {
@@ -45,5 +46,7 @@ class ThreadsChild {
45
46
  (0, _nullthrows().default)(_worker_threads().parentPort).postMessage((0, _buildCache().prepareForSerialization)(data));
46
47
  }
47
48
  }
49
+
50
+ // @ts-expect-error TS2345
48
51
  exports.default = ThreadsChild;
49
52
  (0, _childState.setChild)(new _child.Child(ThreadsChild));
@@ -0,0 +1,15 @@
1
+ import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
2
+ import { Worker } from 'worker_threads';
3
+ export declare let WORKER_PATH: string;
4
+ export default class ThreadsWorker implements WorkerImpl {
5
+ execArgv: any;
6
+ onMessage: MessageHandler;
7
+ onError: ErrorHandler;
8
+ onExit: ExitHandler;
9
+ worker: Worker;
10
+ constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
11
+ start(): Promise<void>;
12
+ stop(): Promise<void>;
13
+ handleMessage(data: WorkerMessage): void;
14
+ send(data: WorkerMessage): void;
15
+ }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.WORKER_PATH = void 0;
7
7
  function _worker_threads() {
8
8
  const data = require("worker_threads");
9
9
  _worker_threads = function () {
@@ -26,8 +26,15 @@ function _buildCache() {
26
26
  return data;
27
27
  }
28
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
- const WORKER_PATH = _path().default.join(__dirname, './ThreadsChild.js');
29
+ let WORKER_PATH = exports.WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.js');
30
+ if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
31
+ exports.WORKER_PATH = WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.ts');
32
+ }
33
+
34
+ // @ts-expect-error TS2420
30
35
  class ThreadsWorker {
36
+ // @ts-expect-error TS2564
37
+
31
38
  constructor(execArgv, onMessage, onError, onExit) {
32
39
  this.execArgv = execArgv;
33
40
  this.onMessage = onMessage;
@@ -49,6 +56,7 @@ class ThreadsWorker {
49
56
  stop() {
50
57
  // In node 12, this returns a promise, but previously it accepted a callback
51
58
  // TODO: Pass a callback in earlier versions of Node
59
+ // @ts-expect-error TS2322
52
60
  return Promise.resolve(this.worker.terminate());
53
61
  }
54
62
  handleMessage(data) {
package/lib/types.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { Diagnostic } from '@atlaspack/diagnostic';
2
+ import type { FilePath } from '@atlaspack/types-internal';
3
+ export type LocationCallRequest = {
4
+ args: ReadonlyArray<unknown>;
5
+ location: string;
6
+ method?: string;
7
+ };
8
+ export type HandleCallRequest = {
9
+ args: ReadonlyArray<unknown>;
10
+ handle: number;
11
+ };
12
+ export type CallRequest = LocationCallRequest | HandleCallRequest;
13
+ export type WorkerRequest = {
14
+ args: ReadonlyArray<any>;
15
+ awaitResponse?: boolean;
16
+ child?: number | null | undefined;
17
+ idx?: number;
18
+ location?: FilePath;
19
+ method?: string | null | undefined;
20
+ type: 'request';
21
+ handle?: number;
22
+ };
23
+ export type WorkerDataResponse = {
24
+ idx?: number;
25
+ child?: number;
26
+ type: 'response';
27
+ contentType: 'data';
28
+ content: string;
29
+ };
30
+ export type WorkerErrorResponse = {
31
+ idx?: number;
32
+ child?: number;
33
+ type: 'response';
34
+ contentType: 'error';
35
+ content: Diagnostic | Array<Diagnostic>;
36
+ };
37
+ export type WorkerResponse = WorkerDataResponse | WorkerErrorResponse;
38
+ export type WorkerMessage = WorkerRequest | WorkerResponse;
39
+ export type MessageHandler = (data: WorkerMessage) => void;
40
+ export type ErrorHandler = (err: Error) => void;
41
+ export type ExitHandler = (code: number) => void;
42
+ export interface WorkerImpl {
43
+ constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler): void;
44
+ start(): Promise<void>;
45
+ stop(): Promise<void>;
46
+ send(data: WorkerMessage): void;
47
+ }
48
+ export interface ChildImpl {
49
+ constructor(onMessage: MessageHandler, onExit: ExitHandler): void;
50
+ send(data: WorkerMessage): void;
51
+ }
52
+ export type BackendType = 'threads' | 'process' | 'web';
@@ -0,0 +1,8 @@
1
+ import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
2
+ export default class WebChild implements ChildImpl {
3
+ onMessage: MessageHandler;
4
+ onExit: ExitHandler;
5
+ constructor(onMessage: MessageHandler, onExit: ExitHandler);
6
+ handleMessage(data: WorkerMessage): void;
7
+ send(data: WorkerMessage): void;
8
+ }
@@ -14,6 +14,10 @@ function _buildCache() {
14
14
  var _child = require("../child");
15
15
  var _childState = require("../childState");
16
16
  /* eslint-env worker*/
17
+
18
+ // Type declarations for Web Worker environment
19
+
20
+ // @ts-expect-error TS2420
17
21
  class WebChild {
18
22
  constructor(onMessage, onExit) {
19
23
  if (!(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)) {
@@ -28,7 +32,6 @@ class WebChild {
28
32
  this.onExit(0);
29
33
  self.postMessage('stopped');
30
34
  }
31
- // $FlowFixMe assume WorkerMessage as data
32
35
  this.handleMessage(data);
33
36
  });
34
37
  self.postMessage('online');
@@ -40,5 +43,7 @@ class WebChild {
40
43
  self.postMessage((0, _buildCache().prepareForSerialization)(data));
41
44
  }
42
45
  }
46
+
47
+ // @ts-expect-error TS2345
43
48
  exports.default = WebChild;
44
49
  (0, _childState.setChild)(new _child.Child(WebChild));
@@ -0,0 +1,15 @@
1
+ import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
2
+ export declare let WORKER_PATH: import("url").URL;
3
+ export default class WebWorker implements WorkerImpl {
4
+ execArgv: any;
5
+ onMessage: MessageHandler;
6
+ onError: ErrorHandler;
7
+ onExit: ExitHandler;
8
+ worker: Worker;
9
+ stopping: Promise<undefined> | null | undefined;
10
+ constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
11
+ start(): Promise<void>;
12
+ stop(): Promise<void>;
13
+ handleMessage(data: WorkerMessage): void;
14
+ send(data: WorkerMessage): void;
15
+ }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.WORKER_PATH = void 0;
7
7
  function _buildCache() {
8
8
  const data = require("@atlaspack/build-cache");
9
9
  _buildCache = function () {
@@ -19,7 +19,18 @@ function _utils() {
19
19
  return data;
20
20
  }
21
21
  let id = 0;
22
+
23
+ // @ts-expect-error This is actually a module
24
+ let WORKER_PATH = exports.WORKER_PATH = new URL('./WebChild.js', import.meta.url);
25
+ if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
26
+ // @ts-expect-error This is actually a module
27
+ exports.WORKER_PATH = WORKER_PATH = new URL('./WebChild.ts', import.meta.url);
28
+ }
29
+
30
+ // @ts-expect-error TS2420
22
31
  class WebWorker {
32
+ // @ts-expect-error TS2564
33
+
23
34
  constructor(execArgv, onMessage, onError, onExit) {
24
35
  this.execArgv = execArgv;
25
36
  this.onMessage = onMessage;
@@ -27,7 +38,7 @@ class WebWorker {
27
38
  this.onExit = onExit;
28
39
  }
29
40
  start() {
30
- // $FlowFixMe[incompatible-call]
41
+ // @ts-expect-error TS1470
31
42
  this.worker = new Worker(new URL('./WebChild.js', import.meta.url), {
32
43
  name: `Parcel Worker ${id++}`,
33
44
  type: 'module'
@@ -36,25 +47,28 @@ class WebWorker {
36
47
  deferred,
37
48
  promise
38
49
  } = (0, _utils().makeDeferredWithPromise)();
50
+
51
+ // @ts-expect-error TS7031
39
52
  this.worker.onmessage = ({
40
53
  data
41
54
  }) => {
42
55
  if (data === 'online') {
56
+ // @ts-expect-error TS2554
43
57
  deferred.resolve();
44
58
  return;
45
59
  }
46
-
47
- // $FlowFixMe assume WorkerMessage as data
48
60
  this.handleMessage(data);
49
61
  };
50
62
  this.worker.onerror = this.onError;
51
63
  // Web workers can't crash or intentionally stop on their own, apart from stop() below
52
64
  // this.worker.on('exit', this.onExit);
53
65
 
66
+ // @ts-expect-error TS2322
54
67
  return promise;
55
68
  }
56
69
  stop() {
57
70
  if (!this.stopping) {
71
+ // @ts-expect-error TS2322
58
72
  this.stopping = (async () => {
59
73
  this.worker.postMessage('stop');
60
74
  let {
@@ -65,6 +79,7 @@ class WebWorker {
65
79
  data
66
80
  }) => {
67
81
  if (data === 'stopped') {
82
+ // @ts-expect-error TS2554
68
83
  deferred.resolve();
69
84
  }
70
85
  });
@@ -73,6 +88,7 @@ class WebWorker {
73
88
  this.onExit(0);
74
89
  })();
75
90
  }
91
+ // @ts-expect-error TS2322
76
92
  return this.stopping;
77
93
  }
78
94
  handleMessage(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/workers",
3
- "version": "2.14.21",
3
+ "version": "2.14.22-typescript-5ad950d33.0",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "publishConfig": {
@@ -10,24 +10,28 @@
10
10
  "type": "git",
11
11
  "url": "https://github.com/atlassian-labs/atlaspack.git"
12
12
  },
13
- "main": "lib/index.js",
14
- "source": "src/index.js",
15
- "types": "index.d.ts",
13
+ "main": "./lib/index.js",
14
+ "source": "./src/index.ts",
15
+ "types": "./lib/index.d.ts",
16
16
  "engines": {
17
17
  "node": ">= 16.0.0"
18
18
  },
19
19
  "dependencies": {
20
- "@atlaspack/build-cache": "2.13.3",
21
- "@atlaspack/diagnostic": "2.14.1",
22
- "@atlaspack/logger": "2.14.13",
23
- "@atlaspack/profiler": "2.14.18",
24
- "@atlaspack/types-internal": "2.16.0",
25
- "@atlaspack/utils": "2.17.3",
20
+ "@atlaspack/build-cache": "2.13.4-typescript-5ad950d33.0",
21
+ "@atlaspack/diagnostic": "2.14.2-typescript-5ad950d33.0",
22
+ "@atlaspack/logger": "2.14.14-typescript-5ad950d33.0",
23
+ "@atlaspack/profiler": "2.14.19-typescript-5ad950d33.0",
24
+ "@atlaspack/types-internal": "2.16.1-typescript-5ad950d33.0",
25
+ "@atlaspack/utils": "2.17.4-typescript-5ad950d33.0",
26
26
  "nullthrows": "^1.1.1"
27
27
  },
28
28
  "browser": {
29
29
  "./src/process/ProcessWorker.js": false,
30
30
  "./src/threads/ThreadsWorker.js": false
31
31
  },
32
- "type": "commonjs"
33
- }
32
+ "type": "commonjs",
33
+ "scripts": {
34
+ "check-ts": "tsc --emitDeclarationOnly --rootDir src"
35
+ },
36
+ "gitHead": "5ad950d33a5f2255ebeb10c04a2e84c346e2de85"
37
+ }
@@ -1,24 +1,21 @@
1
- // @flow strict-local
2
1
  import {registerSerializableClass} from '@atlaspack/build-cache';
3
- // $FlowFixMe
4
2
  import packageJson from '../package.json';
5
3
 
6
4
  let HANDLE_ID = 0;
7
- // $FlowFixMe
8
5
  export type HandleFunction = (...args: Array<any>) => any;
9
6
 
10
- type HandleOpts = {|
11
- fn?: HandleFunction,
12
- childId?: ?number,
13
- id?: number,
14
- |};
7
+ type HandleOpts = {
8
+ fn?: HandleFunction;
9
+ childId?: number | null | undefined;
10
+ id?: number;
11
+ };
15
12
 
16
13
  const handleById: Map<number, Handle> = new Map();
17
14
 
18
15
  export default class Handle {
19
16
  id: number;
20
- childId: ?number;
21
- fn: ?HandleFunction;
17
+ childId: number | null | undefined;
18
+ fn: HandleFunction | null | undefined;
22
19
 
23
20
  constructor(opts: HandleOpts) {
24
21
  this.id = opts.id ?? ++HANDLE_ID;
@@ -31,7 +28,10 @@ export default class Handle {
31
28
  handleById.delete(this.id);
32
29
  }
33
30
 
34
- serialize(): {|childId: ?number, id: number|} {
31
+ serialize(): {
32
+ childId: number | null | undefined;
33
+ id: number;
34
+ } {
35
35
  return {
36
36
  id: this.id,
37
37
  childId: this.childId,