Repository navigation
Expand file tree
/
Copy pathpackWorker.ts
More file actions
102 lines (85 loc) · 2.91 KB
/
Copy pathpackWorker.ts
File metadata and controls
102 lines (85 loc) · 2.91 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { parentPort as rawParentPort, type MessagePort } from 'node:worker_threads';
import { Terminal } from '@rushstack/terminal/lib/Terminal';
import { StringBufferTerminalProvider } from '@rushstack/terminal/lib/StringBufferTerminalProvider';
import { type IZipSyncPackOptions, type IZipSyncPackResult, pack } from './pack';
import { defaultBufferSize } from './zipSyncUtils';
export { type IZipSyncPackOptions, type IZipSyncPackResult } from './pack';
export interface IHashWorkerData {
basePath: string;
}
export interface IZipSyncPackCommandMessage {
type: 'zipsync-pack';
id: number;
options: Omit<IZipSyncPackOptions, 'terminal'>;
}
export interface IZipSyncPackWorkerResult {
zipSyncReturn: IZipSyncPackResult;
zipSyncLogs: string;
}
interface IZipSyncSuccessMessage {
id: number;
type: 'zipsync-pack';
result: IZipSyncPackWorkerResult;
}
export interface IZipSyncPackErrorMessage {
type: 'error';
id: number;
args: {
message: string;
stack: string;
zipSyncLogs: string;
};
}
export type IHostToWorkerMessage = IZipSyncPackCommandMessage;
export type IWorkerToHostMessage = IZipSyncSuccessMessage | IZipSyncPackErrorMessage;
if (!rawParentPort) {
throw new Error('This module must be run in a worker thread.');
}
const parentPort: MessagePort = rawParentPort;
let inputBuffer: Buffer<ArrayBuffer> | undefined = undefined;
let outputBuffer: Buffer<ArrayBuffer> | undefined = undefined;
function handleMessage(message: IHostToWorkerMessage | false): void {
if (message === false) {
parentPort.removeAllListeners();
parentPort.close();
return;
}
const terminalProvider: StringBufferTerminalProvider = new StringBufferTerminalProvider();
const terminal: Terminal = new Terminal(terminalProvider);
try {
switch (message.type) {
case 'zipsync-pack': {
const { options } = message;
if (!inputBuffer) {
inputBuffer = Buffer.allocUnsafeSlow(defaultBufferSize);
}
if (!outputBuffer) {
outputBuffer = Buffer.allocUnsafeSlow(defaultBufferSize);
}
const successMessage: IZipSyncSuccessMessage = {
type: message.type,
id: message.id,
result: {
zipSyncReturn: pack({ ...options, terminal, inputBuffer, outputBuffer }),
zipSyncLogs: terminalProvider.getOutput()
}
};
return parentPort.postMessage(successMessage);
}
}
} catch (err) {
const errorMessage: IZipSyncPackErrorMessage = {
type: 'error',
id: message.id,
args: {
message: (err as Error).message,
stack: (err as Error).stack || '',
zipSyncLogs: terminalProvider.getOutput()
}
};
parentPort.postMessage(errorMessage);
}
}
parentPort.on('message', handleMessage);