@lvce-editor/pty-host 7.0.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 +155 -60
  2. package/package.json +1 -1
@@ -1,8 +1,9 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { fileURLToPath } from 'node:url';
1
3
  import { Console } from 'node:console';
2
4
  import { createWriteStream, readFileSync } from 'node:fs';
3
5
  import { tmpdir } from 'node:os';
4
- import { fileURLToPath } from 'node:url';
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
  };
@@ -1358,6 +1444,12 @@ class Pty extends EventTarget {
1358
1444
  this.pty.write(data);
1359
1445
  }
1360
1446
  }
1447
+ const toPath = pathOrUri => {
1448
+ if (pathOrUri && pathOrUri.startsWith('file://')) {
1449
+ return fileURLToPath(pathOrUri).toString();
1450
+ }
1451
+ return pathOrUri;
1452
+ };
1361
1453
 
1362
1454
  /**
1363
1455
  *
@@ -1376,8 +1468,9 @@ const create$1 = async ({
1376
1468
  const {
1377
1469
  spawn
1378
1470
  } = await import('node-pty');
1471
+ const cwdPath = toPath(cwd);
1379
1472
  const pty = spawn(command, args, {
1380
- cwd,
1473
+ cwd: cwdPath,
1381
1474
  encoding: null
1382
1475
  // cols: 10,
1383
1476
  // rows: 10,
@@ -1452,6 +1545,7 @@ const HandleElectronMessagePort = 'HandleElectronMessagePort.handleElectronMessa
1452
1545
  const HandleNodeMessagePort = 'HandleNodeMessagePort.handleNodeMessagePort';
1453
1546
  const HandleWebSocket = 'HandleWebSocket.handleWebSocket';
1454
1547
  const TerminalCreate = 'Terminal.create';
1548
+ const TerminalExecuteShellCommand = 'Terminal.executeShellCommand';
1455
1549
  const TerminalResize = 'Terminal.resize';
1456
1550
  const TerminalWrite = 'Terminal.write';
1457
1551
  const TerminalDispose = 'Terminal.dispose';
@@ -1462,6 +1556,7 @@ const commandMap = {
1462
1556
  [HandleWebSocket]: handleWebSocket,
1463
1557
  [TerminalCreate]: create,
1464
1558
  [TerminalDispose]: dispose,
1559
+ [TerminalExecuteShellCommand]: executeShellCommand,
1465
1560
  [TerminalResize]: resize,
1466
1561
  [TerminalWrite]: write
1467
1562
  };
@@ -2314,7 +2409,7 @@ const doSocketUpgrade = (webSocketServer, request, socket) => {
2314
2409
  promise,
2315
2410
  resolve
2316
2411
  } = Promise.withResolvers();
2317
- webSocketServer.handleUpgrade(request, socket, Buffer.alloc(0), resolve);
2412
+ webSocketServer.handleUpgrade(request, socket, Buffer$1.alloc(0), resolve);
2318
2413
  return promise;
2319
2414
  };
2320
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.0.0",
3
+ "version": "8.0.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "pty-host"