@lvce-editor/pty-host 0.12.1 → 0.14.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/pty-host",
3
- "version": "0.12.1",
3
+ "version": "0.14.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,8 @@
1
+ import * as IpcChildModule from '../IpcChildModule/IpcChildModule.js'
2
+
3
+ export const listen = async ({ method }) => {
4
+ const module = await IpcChildModule.getModule(method)
5
+ const rawIpc = await module.listen()
6
+ const ipc = module.wrap(rawIpc)
7
+ return ipc
8
+ }
@@ -0,0 +1,12 @@
1
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
2
+
3
+ export const getModule = (method) => {
4
+ switch (method) {
5
+ case IpcChildType.NodeForkedProcess:
6
+ return import('../IpcChildWithNodeForkedProcess/IpcChildWithNodeForkedProcess.js')
7
+ case IpcChildType.NodeWorker:
8
+ return import('../IpcChildWithNodeWorker/IpcChildWithNodeWorker.js')
9
+ default:
10
+ throw new Error('unexpected ipc type')
11
+ }
12
+ }
@@ -0,0 +1,13 @@
1
+ export const NodeWorker = 1
2
+ export const NodeForkedProcess = 2
3
+
4
+ export const Auto = () => {
5
+ const { argv } = process
6
+ if (argv.includes('--ipc-type=node-worker')) {
7
+ return NodeWorker
8
+ }
9
+ if (argv.includes('--ipc-type=node-forked-process')) {
10
+ return NodeForkedProcess
11
+ }
12
+ throw new Error(`[pty-host] unknown ipc type`)
13
+ }
@@ -0,0 +1,23 @@
1
+ export const listen = async () => {
2
+ if (!process.send) {
3
+ throw new Error('process ipc is not available')
4
+ }
5
+ process.send('ready')
6
+ return process
7
+ }
8
+
9
+ export const wrap = (process) => {
10
+ return {
11
+ process,
12
+ on(event, listener) {
13
+ this.process.on(event, listener)
14
+ },
15
+ off(event, listener) {
16
+ this.process.off(event, listener)
17
+ },
18
+ send(message) {
19
+ this.process.send(message)
20
+ },
21
+ dispose() {},
22
+ }
23
+ }
@@ -0,0 +1,27 @@
1
+ import { parentPort } from 'node:worker_threads'
2
+
3
+ export const listen = async () => {
4
+ if (!parentPort) {
5
+ throw new Error('parentPort is required')
6
+ }
7
+ parentPort.postMessage('ready')
8
+ return parentPort
9
+ }
10
+
11
+ export const wrap = (parentPort) => {
12
+ return {
13
+ parentPort,
14
+ on(event, listener) {
15
+ this.parentPort.on(event, listener)
16
+ },
17
+ off(event, listener) {
18
+ this.parentPort.off(event, listener)
19
+ },
20
+ send(message) {
21
+ this.parentPort.postMessage(message)
22
+ },
23
+ dispose() {
24
+ this.parentPort.close()
25
+ },
26
+ }
27
+ }
@@ -1,5 +1,7 @@
1
- import * as PtyController from './parts/PtyController/PtyController.js'
2
1
  import * as Debug from './parts/Debug/Debug.js'
2
+ import * as IpcChild from './parts/IpcChild/IpcChild.js'
3
+ import * as IpcChildType from './parts/IpcChildType/IpcChildType.js'
4
+ import * as PtyController from './parts/PtyController/PtyController.js'
3
5
 
4
6
  const handleMessage = (message) => {
5
7
  switch (message.method) {
@@ -17,10 +19,8 @@ const handleMessage = (message) => {
17
19
  }
18
20
 
19
21
  const main = async () => {
20
- process.on('message', handleMessage)
21
- if (process.send) {
22
- process.send('ready')
23
- }
22
+ const ipc = await IpcChild.listen({ method: IpcChildType.Auto() })
23
+ ipc.on('message', handleMessage)
24
24
  }
25
25
 
26
26
  main()