@lvce-editor/pty-host 0.15.21 → 0.15.22

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.21",
3
+ "version": "0.15.22",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,42 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as Id from '../Id/Id.js'
3
+
4
+ export const state = {
5
+ callbacks: Object.create(null),
6
+ }
7
+
8
+ export const registerPromise = () => {
9
+ const id = Id.create()
10
+ const promise = new Promise((resolve, reject) => {
11
+ state.callbacks[id] = {
12
+ resolve,
13
+ reject,
14
+ }
15
+ })
16
+ return { id, promise }
17
+ }
18
+
19
+ export const unregister = (id) => {
20
+ delete state.callbacks[id]
21
+ }
22
+
23
+ export const resolve = (id, args) => {
24
+ Assert.number(id)
25
+ if (!(id in state.callbacks)) {
26
+ console.log(args)
27
+ console.warn(`callback ${id} may already be disposed`)
28
+ return
29
+ }
30
+ state.callbacks[id].resolve(args)
31
+ delete state.callbacks[id]
32
+ }
33
+
34
+ export const reject = (id, error) => {
35
+ Assert.number(id)
36
+ if (!(id in state.callbacks)) {
37
+ console.warn(`callback ${id} may already be disposed`)
38
+ return
39
+ }
40
+ state.callbacks[id].reject(error)
41
+ delete state.callbacks[id]
42
+ }
@@ -2,12 +2,13 @@ import * as HandleElectronMessagePort from '../HandleElectronMessagePort/HandleE
2
2
  import * as HandleNodeMessagePort from '../HandleNodeMessagePort/HandleNodeMessagePort.js'
3
3
  import * as HandleWebSocket from '../HandleWebSocket/HandleWebSocket.js'
4
4
  import * as PtyController from '../PtyController/PtyController.js'
5
+ import * as TerminalProcessCommandType from '../TerminalProcessCommandType/TerminalProcessCommandType.js'
5
6
 
6
7
  export const commandMap = {
7
- 'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
8
- 'HandleNodeMessagePort.handleNodeMessagePort': HandleNodeMessagePort.handleNodeMessagePort,
9
- 'HandleWebSocket.handleWebSocket': HandleWebSocket.handleWebSocket,
10
- 'Terminal.create': PtyController.create,
11
- 'Terminal.write': PtyController.write,
12
- 'Terminal.dispose': PtyController.dispose,
8
+ [TerminalProcessCommandType.HandleElectronMessagePort]: HandleElectronMessagePort.handleElectronMessagePort,
9
+ [TerminalProcessCommandType.HandleNodeMessagePort]: HandleNodeMessagePort.handleNodeMessagePort,
10
+ [TerminalProcessCommandType.HandleWebSocket]: HandleWebSocket.handleWebSocket,
11
+ [TerminalProcessCommandType.TerminalCreate]: PtyController.create,
12
+ [TerminalProcessCommandType.TerminalWrite]: PtyController.write,
13
+ [TerminalProcessCommandType.TerminalDispose]: PtyController.dispose,
13
14
  }
@@ -1,13 +1,12 @@
1
- import * as Command from '../Command/Command.js'
2
1
  import * as GetErrorResponse from '../GetErrorResponse/GetErrorResponse.js'
3
2
  import * as GetSuccessResponse from '../GetSuccessResponse/GetSuccessResponse.js'
4
3
  import * as RequiresSocket from '../RequiresSocket/RequiresSocket.js'
5
4
 
6
- export const getResponse = async (message, handle) => {
5
+ export const getResponse = async (message, execute, handle) => {
7
6
  try {
8
7
  const result = RequiresSocket.requiresSocket(message.method)
9
- ? await Command.execute(message.method, ...message.params, handle)
10
- : await Command.execute(message.method, ...message.params)
8
+ ? await execute(message.method, ...message.params, handle)
9
+ : await execute(message.method, ...message.params)
11
10
  return GetSuccessResponse.getSuccessResponse(message, result)
12
11
  } catch (error) {
13
12
  return GetErrorResponse.getErrorResponse(message, error)
@@ -1,13 +1,12 @@
1
1
  import * as Assert from '../Assert/Assert.js'
2
- import * as GetResponse from '../GetResponse/GetResponse.js'
2
+ import * as Command from '../Command/Command.js'
3
+ import * as Callback from '../Callback/Callback.js'
4
+ import * as HandleJsonRpcMessage from '../HandleJsonRpcMessage/HandleJsonRpcMessage.js'
3
5
 
4
6
  export const handleIpc = (ipc) => {
5
7
  Assert.object(ipc)
6
8
  const handleMessage = async (message) => {
7
- const response = await GetResponse.getResponse(message, ipc)
8
- if (response) {
9
- ipc.send(response)
10
- }
9
+ return HandleJsonRpcMessage.handleJsonRpcMessage(ipc, message, Command.execute, Callback.resolve)
11
10
  }
12
11
  ipc.on('message', handleMessage)
13
12
  }
@@ -0,0 +1,10 @@
1
+ import * as GetResponse from '../GetResponse/GetResponse.js'
2
+
3
+ export const handleJsonRpcMessage = async (ipc, message, execute, resolve) => {
4
+ if ('result' in message || 'error' in message) {
5
+ resolve(message.id, message)
6
+ return
7
+ }
8
+ const response = await GetResponse.getResponse(message, execute, ipc)
9
+ ipc.send(response)
10
+ }
@@ -0,0 +1,7 @@
1
+ export const state = {
2
+ id: 0,
3
+ }
4
+
5
+ export const create = () => {
6
+ return ++state.id
7
+ }
@@ -1,7 +1,16 @@
1
1
  import * as GetFirstWebSocketEvent from '../GetFirstWebSocketEvent/GetFirstWebSocketEvent.js'
2
+ import { IpcError } from '../IpcError/IpcError.js'
3
+ import * as IsWebSocket from '../IsWebSocket/IsWebSocket.js'
2
4
  import * as IsWebSocketOpen from '../IsWebSocketOpen/IsWebSocketOpen.js'
5
+ import * as WebSocketSerialization from '../WebSocketSerialization/WebSocketSerialization.js'
3
6
 
4
7
  export const listen = async ({ webSocket }) => {
8
+ if (!webSocket) {
9
+ throw new IpcError('webSocket must be defined')
10
+ }
11
+ if (!IsWebSocket.isWebSocket(webSocket)) {
12
+ throw new IpcError(`webSocket must be of type WebSocket`)
13
+ }
5
14
  if (!IsWebSocketOpen.isWebSocketOpen(webSocket)) {
6
15
  const { type, event } = await GetFirstWebSocketEvent.getFirstWebSocketEvent(webSocket)
7
16
  console.log({ type, event })
@@ -9,14 +18,6 @@ export const listen = async ({ webSocket }) => {
9
18
  return webSocket
10
19
  }
11
20
 
12
- class MessageEvent extends Event {
13
- constructor(data, target) {
14
- super('message')
15
- this.data = data
16
- this.target = target
17
- }
18
- }
19
-
20
21
  export const wrap = (webSocket) => {
21
22
  return {
22
23
  webSocket,
@@ -28,7 +29,7 @@ export const wrap = (webSocket) => {
28
29
  switch (event) {
29
30
  case 'message':
30
31
  const wrappedListener = (message) => {
31
- const data = JSON.parse(message.toString())
32
+ const data = WebSocketSerialization.deserialize(message)
32
33
  listener(data)
33
34
  }
34
35
  webSocket.on('message', wrappedListener)
@@ -41,7 +42,7 @@ export const wrap = (webSocket) => {
41
42
  this.webSocket.off(event, listener)
42
43
  },
43
44
  send(message) {
44
- const stringifiedMessage = JSON.stringify(message)
45
+ const stringifiedMessage = WebSocketSerialization.serialize(message)
45
46
  this.webSocket.send(stringifiedMessage)
46
47
  },
47
48
  dispose() {
@@ -0,0 +1,5 @@
1
+ import { WebSocket } from 'ws'
2
+
3
+ export const isWebSocket = (value) => {
4
+ return value instanceof WebSocket
5
+ }
@@ -0,0 +1,6 @@
1
+ export const HandleElectronMessagePort = 'HandleElectronMessagePort.handleElectronMessagePort'
2
+ export const HandleNodeMessagePort = 'HandleNodeMessagePort.handleNodeMessagePort'
3
+ export const HandleWebSocket = 'HandleWebSocket.handleWebSocket'
4
+ export const TerminalCreate = 'Terminal.create'
5
+ export const TerminalWrite = 'Terminal.write'
6
+ export const TerminalDispose = 'Terminal.dispose'
@@ -0,0 +1,7 @@
1
+ export const serialize = (message) => {
2
+ return JSON.stringify(message)
3
+ }
4
+
5
+ export const deserialize = (message) => {
6
+ return JSON.parse(message.toString())
7
+ }
@@ -1 +0,0 @@
1
- import('./ptyHostMain.js')