@lvce-editor/pty-host 7.1.0 → 8.0.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 (2) hide show
  1. package/dist/ptyHostMain.js +146 -58
  2. package/package.json +1 -1
@@ -1,8 +1,9 @@
1
+ import { spawn } from 'node:child_process';
1
2
  import { fileURLToPath } from 'node:url';
2
3
  import { Console } from 'node:console';
3
4
  import { createWriteStream, readFileSync } from 'node:fs';
4
5
  import { tmpdir } from 'node:os';
5
- import { Buffer } from 'node:buffer';
6
+ import { Buffer as Buffer$1 } from 'node:buffer';
6
7
 
7
8
  class CommandNotFoundError extends Error {
8
9
  constructor(command) {
@@ -25,62 +26,6 @@ const execute = (command, ...args) => {
25
26
  return fn(...args);
26
27
  };
27
28
 
28
- const normalizeLine = line => {
29
- if (line.startsWith('Error: ')) {
30
- return line.slice('Error: '.length);
31
- }
32
- if (line.startsWith('VError: ')) {
33
- return line.slice('VError: '.length);
34
- }
35
- return line;
36
- };
37
- const getCombinedMessage = (error, message) => {
38
- const stringifiedError = normalizeLine(`${error}`);
39
- if (message) {
40
- return `${message}: ${stringifiedError}`;
41
- }
42
- return stringifiedError;
43
- };
44
- const NewLine$4 = '\n';
45
- const getNewLineIndex$2 = (string, startIndex = undefined) => {
46
- return string.indexOf(NewLine$4, startIndex);
47
- };
48
- const mergeStacks = (parent, child) => {
49
- if (!child) {
50
- return parent;
51
- }
52
- const parentNewLineIndex = getNewLineIndex$2(parent);
53
- const childNewLineIndex = getNewLineIndex$2(child);
54
- if (childNewLineIndex === -1) {
55
- return parent;
56
- }
57
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
58
- const childRest = child.slice(childNewLineIndex);
59
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
60
- if (parentFirstLine.includes(childFirstLine)) {
61
- return parentFirstLine + childRest;
62
- }
63
- return child;
64
- };
65
- class VError extends Error {
66
- constructor(error, message) {
67
- const combinedMessage = getCombinedMessage(error, message);
68
- super(combinedMessage);
69
- this.name = 'VError';
70
- if (error instanceof Error) {
71
- this.stack = mergeStacks(this.stack, error.stack);
72
- }
73
- if (error.codeFrame) {
74
- // @ts-ignore
75
- this.codeFrame = error.codeFrame;
76
- }
77
- if (error.code) {
78
- // @ts-ignore
79
- this.code = error.code;
80
- }
81
- }
82
- }
83
-
84
29
  class AssertionError extends Error {
85
30
  constructor(message) {
86
31
  super(message);
@@ -142,6 +87,147 @@ const string = value => {
142
87
  }
143
88
  };
144
89
 
90
+ const serializeError = error => {
91
+ if (error && typeof error === 'object') {
92
+ return {
93
+ errorCode: 'code' in error && typeof error.code === 'string' ? error.code : undefined,
94
+ errorMessage: 'message' in error && typeof error.message === 'string' ? error.message : 'Unknown error',
95
+ errorStack: 'stack' in error && typeof error.stack === 'string' ? error.stack : undefined
96
+ };
97
+ }
98
+ if (typeof error === 'string') {
99
+ return {
100
+ errorCode: undefined,
101
+ errorMessage: error,
102
+ errorStack: undefined
103
+ };
104
+ }
105
+ return {
106
+ errorCode: undefined,
107
+ errorMessage: 'Unknown error',
108
+ errorStack: undefined
109
+ };
110
+ };
111
+
112
+ const toPath$1 = pathOrUri => {
113
+ if (pathOrUri.startsWith('file://')) {
114
+ return fileURLToPath(pathOrUri);
115
+ }
116
+ return pathOrUri;
117
+ };
118
+ const toString = chunks => {
119
+ return Buffer.concat(chunks).toString();
120
+ };
121
+ const executeShellCommand = async ({
122
+ args,
123
+ cwd,
124
+ toSpawn
125
+ }) => {
126
+ array(args);
127
+ string(cwd);
128
+ string(toSpawn);
129
+ try {
130
+ const {
131
+ promise,
132
+ resolve
133
+ } = Promise.withResolvers();
134
+ const childProcess = spawn(toSpawn, args, {
135
+ cwd: toPath$1(cwd)
136
+ });
137
+ const stdoutChunks = [];
138
+ const stderrChunks = [];
139
+ const handleStdoutData = chunk => {
140
+ stdoutChunks.push(chunk);
141
+ };
142
+ const handleStderrData = chunk => {
143
+ stderrChunks.push(chunk);
144
+ };
145
+ const cleanup = () => {
146
+ childProcess.stdout.off('data', handleStdoutData);
147
+ childProcess.stderr.off('data', handleStderrData);
148
+ childProcess.off('error', handleError);
149
+ childProcess.off('close', handleClose);
150
+ };
151
+ const resolveWithCleanup = result => {
152
+ cleanup();
153
+ resolve(result);
154
+ };
155
+ const handleError = error => {
156
+ resolveWithCleanup(serializeError(error));
157
+ };
158
+ const handleClose = exitCode => {
159
+ resolveWithCleanup({
160
+ exitCode,
161
+ stderr: toString(stderrChunks),
162
+ stdout: toString(stdoutChunks)
163
+ });
164
+ };
165
+ childProcess.stdout.on('data', handleStdoutData);
166
+ childProcess.stderr.on('data', handleStderrData);
167
+ childProcess.on('error', handleError);
168
+ childProcess.on('close', handleClose);
169
+ return await promise;
170
+ } catch (error) {
171
+ return serializeError(error);
172
+ }
173
+ };
174
+
175
+ const normalizeLine = line => {
176
+ if (line.startsWith('Error: ')) {
177
+ return line.slice('Error: '.length);
178
+ }
179
+ if (line.startsWith('VError: ')) {
180
+ return line.slice('VError: '.length);
181
+ }
182
+ return line;
183
+ };
184
+ const getCombinedMessage = (error, message) => {
185
+ const stringifiedError = normalizeLine(`${error}`);
186
+ if (message) {
187
+ return `${message}: ${stringifiedError}`;
188
+ }
189
+ return stringifiedError;
190
+ };
191
+ const NewLine$4 = '\n';
192
+ const getNewLineIndex$2 = (string, startIndex = undefined) => {
193
+ return string.indexOf(NewLine$4, startIndex);
194
+ };
195
+ const mergeStacks = (parent, child) => {
196
+ if (!child) {
197
+ return parent;
198
+ }
199
+ const parentNewLineIndex = getNewLineIndex$2(parent);
200
+ const childNewLineIndex = getNewLineIndex$2(child);
201
+ if (childNewLineIndex === -1) {
202
+ return parent;
203
+ }
204
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
205
+ const childRest = child.slice(childNewLineIndex);
206
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
207
+ if (parentFirstLine.includes(childFirstLine)) {
208
+ return parentFirstLine + childRest;
209
+ }
210
+ return child;
211
+ };
212
+ class VError extends Error {
213
+ constructor(error, message) {
214
+ const combinedMessage = getCombinedMessage(error, message);
215
+ super(combinedMessage);
216
+ this.name = 'VError';
217
+ if (error instanceof Error) {
218
+ this.stack = mergeStacks(this.stack, error.stack);
219
+ }
220
+ if (error.codeFrame) {
221
+ // @ts-ignore
222
+ this.codeFrame = error.codeFrame;
223
+ }
224
+ if (error.code) {
225
+ // @ts-ignore
226
+ this.code = error.code;
227
+ }
228
+ }
229
+ }
230
+
145
231
  const isMessagePort = value => {
146
232
  return value && value instanceof MessagePort;
147
233
  };
@@ -1459,6 +1545,7 @@ const HandleElectronMessagePort = 'HandleElectronMessagePort.handleElectronMessa
1459
1545
  const HandleNodeMessagePort = 'HandleNodeMessagePort.handleNodeMessagePort';
1460
1546
  const HandleWebSocket = 'HandleWebSocket.handleWebSocket';
1461
1547
  const TerminalCreate = 'Terminal.create';
1548
+ const TerminalExecuteShellCommand = 'Terminal.executeShellCommand';
1462
1549
  const TerminalResize = 'Terminal.resize';
1463
1550
  const TerminalWrite = 'Terminal.write';
1464
1551
  const TerminalDispose = 'Terminal.dispose';
@@ -1469,6 +1556,7 @@ const commandMap = {
1469
1556
  [HandleWebSocket]: handleWebSocket,
1470
1557
  [TerminalCreate]: create,
1471
1558
  [TerminalDispose]: dispose,
1559
+ [TerminalExecuteShellCommand]: executeShellCommand,
1472
1560
  [TerminalResize]: resize,
1473
1561
  [TerminalWrite]: write
1474
1562
  };
@@ -2321,7 +2409,7 @@ const doSocketUpgrade = (webSocketServer, request, socket) => {
2321
2409
  promise,
2322
2410
  resolve
2323
2411
  } = Promise.withResolvers();
2324
- webSocketServer.handleUpgrade(request, socket, Buffer.alloc(0), resolve);
2412
+ webSocketServer.handleUpgrade(request, socket, Buffer$1.alloc(0), resolve);
2325
2413
  return promise;
2326
2414
  };
2327
2415
  const handleUpgrade = async (request, socket) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/pty-host",
3
- "version": "7.1.0",
3
+ "version": "8.0.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "pty-host"