@lvce-editor/pty-host 0.15.17 → 0.15.19

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/pty-host",
3
- "version": "0.15.17",
3
+ "version": "0.15.19",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,10 +1,13 @@
1
- import * as PtyController from '../PtyController/PtyController.js'
2
- import * as HandleWebSocket from '../HandleWebSocket/HandleWebSocket.js'
3
1
  import * as HandleElectronMessagePort from '../HandleElectronMessagePort/HandleElectronMessagePort.js'
2
+ import * as HandleNodeMessagePort from '../HandleNodeMessagePort/HandleNodeMessagePort.js'
3
+ import * as HandleWebSocket from '../HandleWebSocket/HandleWebSocket.js'
4
+ import * as PtyController from '../PtyController/PtyController.js'
4
5
 
5
6
  export const commandMap = {
7
+ 'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
8
+ 'HandleNodeMessagePort.handleNodeMessagePort': HandleNodeMessagePort.handleNodeMessagePort,
9
+ 'HandleWebSocket.handleWebSocket': HandleWebSocket.handleWebSocket,
6
10
  'Terminal.create': PtyController.create,
7
11
  'Terminal.write': PtyController.write,
8
- 'HandleWebSocket.handleWebSocket': HandleWebSocket.handleWebSocket,
9
- 'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
12
+ 'Terminal.dispose': PtyController.dispose,
10
13
  }
@@ -0,0 +1,12 @@
1
+ import * as HandleIpc from '../HandleIpc/HandleIpc.js'
2
+ import * as IpcChild from '../IpcChild/IpcChild.js'
3
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
4
+
5
+ export const handleNodeMessagePort = async (messagePort) => {
6
+ const ipc = await IpcChild.listen({
7
+ method: IpcChildType.NodeMessagePort,
8
+ messagePort,
9
+ })
10
+ HandleIpc.handleIpc(ipc)
11
+ messagePort.start()
12
+ }
@@ -12,6 +12,8 @@ export const getModule = (method) => {
12
12
  return import('../IpcChildWithElectronUtilityProcess/IpcChildWithElectronUtilityProcess.js')
13
13
  case IpcChildType.ElectronMessagePort:
14
14
  return import('../IpcChildWithElectronMessagePort/IpcChildWithElectronMessagePort.js')
15
+ case IpcChildType.NodeMessagePort:
16
+ return import('../IpcChildWithNodeMessagePort/IpcChildWithNodeMessagePort.js')
15
17
  default:
16
18
  throw new Error('unexpected ipc type')
17
19
  }
@@ -3,6 +3,7 @@ export const NodeForkedProcess = 2
3
3
  export const WebSocket = 3
4
4
  export const ElectronUtilityProcess = 4
5
5
  export const ElectronMessagePort = 5
6
+ export const NodeMessagePort = 6
6
7
 
7
8
  export const Auto = () => {
8
9
  const { argv } = process
@@ -1,4 +1,10 @@
1
+ import { IpcError } from '../IpcError/IpcError.js'
2
+ import * as IsMessagePortMain from '../IsMessagePortMain/IsMessagePortMain.js'
3
+
1
4
  export const listen = ({ messagePort }) => {
5
+ if (!IsMessagePortMain.isMessagePortMain(messagePort)) {
6
+ throw new IpcError('port must be of type MessagePortMain')
7
+ }
2
8
  return messagePort
3
9
  }
4
10
 
@@ -1,8 +1,10 @@
1
+ import { IpcError } from '../IpcError/IpcError.js'
2
+
1
3
  export const listen = () => {
2
4
  // @ts-ignore
3
5
  const parentPort = process.parentPort
4
6
  if (!parentPort) {
5
- throw new Error('parent port must be defined')
7
+ throw new IpcError('parent port must be defined')
6
8
  }
7
9
  parentPort.postMessage('ready')
8
10
  return parentPort
@@ -1,6 +1,8 @@
1
+ import { IpcError } from '../IpcError/IpcError.js'
2
+
1
3
  export const listen = async () => {
2
4
  if (!process.send) {
3
- throw new Error('process ipc is not available')
5
+ throw new IpcError('process ipc is not available')
4
6
  }
5
7
  process.send('ready')
6
8
  return process
@@ -0,0 +1,27 @@
1
+ import { IpcError } from '../IpcError/IpcError.js'
2
+ import * as IsMessagePort from '../IsMessagePort/IsMessagePort.js'
3
+
4
+ export const listen = async ({ messagePort }) => {
5
+ if (!IsMessagePort.isMessagePort(messagePort)) {
6
+ throw new IpcError(`port must be of type MessagePort`)
7
+ }
8
+ return messagePort
9
+ }
10
+
11
+ export const wrap = (port) => {
12
+ return {
13
+ port,
14
+ on(event, listener) {
15
+ this.port.on(event, listener)
16
+ },
17
+ off(event, listener) {
18
+ this.port.off(event, listener)
19
+ },
20
+ send(message) {
21
+ this.port.postMessage(message)
22
+ },
23
+ dispose() {
24
+ this.port.close()
25
+ },
26
+ }
27
+ }
@@ -0,0 +1,6 @@
1
+ export class IpcError extends Error {
2
+ constructor(message) {
3
+ super(message)
4
+ this.name = 'IpcError'
5
+ }
6
+ }
@@ -0,0 +1,3 @@
1
+ export const isMessagePort = (value) => {
2
+ return value && value instanceof MessagePort
3
+ }
@@ -0,0 +1,3 @@
1
+ export const isMessagePortMain = (value) => {
2
+ return value && value.constructor && value.constructor.name === 'MessagePortMain'
3
+ }
@@ -0,0 +1,47 @@
1
+ import { spawn } from 'node-pty'
2
+ import VError from 'verror'
3
+ import * as Assert from '../Assert/Assert.js'
4
+
5
+ export const create = ({ env = {}, cwd, command, args } = {}) => {
6
+ try {
7
+ Assert.string(cwd)
8
+ Assert.string(command)
9
+ Assert.array(args)
10
+ const pty = spawn(command, args, {
11
+ encoding: null,
12
+ cwd,
13
+ // cols: 10,
14
+ // rows: 10,
15
+ })
16
+ return pty
17
+ } catch (error) {
18
+ // @ts-ignore
19
+ throw new VError(error, `Failed to create terminal`)
20
+ }
21
+ }
22
+
23
+ export const onData = (pty, fn) => {
24
+ Assert.object(pty)
25
+ pty.onData(fn)
26
+ }
27
+
28
+ export const write = (pty, data) => {
29
+ try {
30
+ Assert.object(pty)
31
+ pty.write(data)
32
+ } catch (error) {
33
+ // @ts-ignore
34
+ throw new VError(error, `Failed to write data to terminal`)
35
+ }
36
+ }
37
+
38
+ export const resize = (pty, columns, rows) => {
39
+ Assert.object(pty)
40
+ Assert.number(columns)
41
+ Assert.number(rows)
42
+ pty.resize(columns, rows)
43
+ }
44
+
45
+ export const dispose = (pty) => {
46
+ pty.kill()
47
+ }
@@ -1,60 +1 @@
1
- import { spawn } from 'node-pty'
2
- import * as Platform from '../Platform/Platform.js'
3
- import * as Assert from '../Assert/Assert.js'
4
- import VError from 'verror'
5
-
6
- const getSpawnOptions = () => {
7
- if (Platform.isWindows) {
8
- return {
9
- command: 'powershell.exe',
10
- args: [],
11
- }
12
- }
13
- return {
14
- command: 'bash',
15
- args: ['-i'],
16
- }
17
- }
18
-
19
- export const create = ({ env = {}, cwd } = {}) => {
20
- try {
21
- Assert.string(cwd)
22
- const spawnOptions = getSpawnOptions()
23
- const pty = spawn(spawnOptions.command, spawnOptions.args, {
24
- encoding: null,
25
- cwd,
26
- // cols: 10,
27
- // rows: 10,
28
- })
29
- return pty
30
- } catch (error) {
31
- // @ts-ignore
32
- throw new VError(error, `Failed to create terminal`)
33
- }
34
- }
35
-
36
- export const onData = (pty, fn) => {
37
- Assert.object(pty)
38
- pty.onData(fn)
39
- }
40
-
41
- export const write = (pty, data) => {
42
- try {
43
- Assert.object(pty)
44
- pty.write(data)
45
- } catch (error) {
46
- // @ts-ignore
47
- throw new VError(error, `Failed to write data to terminal`)
48
- }
49
- }
50
-
51
- export const resize = (pty, columns, rows) => {
52
- Assert.object(pty)
53
- Assert.number(columns)
54
- Assert.number(rows)
55
- pty.resize(columns, rows)
56
- }
57
-
58
- export const dispose = (pty) => {
59
- pty.kill()
60
- }
1
+ export * from '../NodePty/NodePty.js'
@@ -7,12 +7,14 @@ export const state = {
7
7
  }
8
8
 
9
9
  // TODO maybe merge pty and pty controller
10
- export const create = (id, cwd, ipc) => {
10
+ export const create = (id, cwd, command, args, ipc) => {
11
11
  Assert.number(id)
12
12
  Assert.string(cwd)
13
+ Assert.string(command)
14
+ Assert.array(args)
13
15
  Assert.object(ipc)
14
16
  Debug.debug(`create ${id} ${cwd}`)
15
- const pty = Pty.create({ cwd })
17
+ const pty = Pty.create({ cwd, command, args })
16
18
  const handleData = (data) => {
17
19
  ipc.send({
18
20
  jsonrpc: '2.0',
@@ -33,7 +35,8 @@ export const write = (id, data) => {
33
35
  }
34
36
 
35
37
  export const dispose = (id) => {
36
- const pty = state.ptyMap[id]
38
+ const { ptyMap } = state
39
+ const pty = ptyMap[id]
37
40
  if (!pty) {
38
41
  throw new Error(`pty ${id} not found`)
39
42
  }