Repository navigation
Expand file tree
/
Copy pathunpackWorkerAsync.ts
More file actions
61 lines (52 loc) · 1.69 KB
/
Copy pathunpackWorkerAsync.ts
File metadata and controls
61 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { Worker } from 'node:worker_threads';
import type {
IWorkerToHostMessage,
IHostToWorkerMessage,
IZipSyncUnpackWorkerResult,
IZipSyncUnpackOptions
} from './unpackWorker';
export type { IZipSyncUnpackWorkerResult } from './unpackWorker';
export async function unpackWorkerAsync(
options: Omit<IZipSyncUnpackOptions, 'terminal'>
): Promise<IZipSyncUnpackWorkerResult> {
const { Worker } = await import('node:worker_threads');
const worker: Worker = new Worker(require.resolve('./unpackWorker'));
return new Promise<IZipSyncUnpackWorkerResult>((resolve, reject) => {
worker.on('message', (message: IWorkerToHostMessage) => {
switch (message.type) {
case 'zipsync-unpack': {
resolve(message.result);
break;
}
case 'error': {
const error: Error = new Error(message.args.message);
error.stack = message.args.stack;
reject(error);
break;
}
default: {
const exhaustiveCheck: never = message;
throw new Error(`Unexpected message type: ${JSON.stringify(exhaustiveCheck)}`);
}
}
});
worker.on('error', (err) => {
reject(err);
});
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
const commandMessage: IHostToWorkerMessage = {
type: 'zipsync-unpack',
id: 0,
options
};
worker.postMessage(commandMessage);
}).finally(() => {
worker.postMessage(false);
});
}