@lvce-editor/ipc 2.0.0 → 2.1.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.
@@ -0,0 +1,21 @@
1
+ import * as GetHelpfulChildProcessError from '../GetHelpfulChildProcessError/GetHelpfulChildProcessError.js'
2
+ import * as JoinLines from '../JoinLines/JoinLines.js'
3
+ import * as SplitLines from '../SplitLines/SplitLines.js'
4
+
5
+ export class ChildProcessError extends Error {
6
+ constructor(stderr) {
7
+ const { message, code, stack } = GetHelpfulChildProcessError.getHelpfulChildProcessError('', stderr)
8
+ super(message || 'child process error')
9
+ this.name = 'ChildProcessError'
10
+ if (code) {
11
+ this.code = code
12
+ }
13
+ if (stack) {
14
+ const lines = SplitLines.splitLines(this.stack)
15
+ const [firstLine, ...stackLines] = lines
16
+ const newStackLines = [firstLine, ...stack, ...stackLines]
17
+ const newStack = JoinLines.joinLines(newStackLines)
18
+ this.stack = newStack
19
+ }
20
+ }
21
+ }
@@ -1,20 +1,34 @@
1
1
  import { fork } from 'node:child_process'
2
2
  import * as Assert from '../Assert/Assert.js'
3
+ import { ChildProcessError } from '../ChildProcessError/ChildProcessError.js'
4
+ import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
5
+ import * as GetFirstNodeChildProcessEvent from '../GetFirstNodeChildProcessEvent/GetFirstNodeChildProcessEvent.js'
6
+ import { VError } from '../VError/VError.js'
3
7
 
4
- /**
5
- *
6
- * @param {{path:string, argv?:string[], env?:any, execArgv?:string[], stdio?:'inherit'}} param0
7
- * @returns
8
- */
9
- export const create = async ({ path, argv = [], env = process.env, execArgv = [], stdio = 'inherit' }) => {
10
- Assert.string(path)
11
- const actualArgv = ['--ipc-type=node-forked-process', ...argv]
12
- const childProcess = fork(path, actualArgv, {
13
- env,
14
- execArgv,
15
- stdio,
16
- })
17
- return childProcess
8
+ export const create = async ({ path, argv = [], env, execArgv = [], stdio = 'inherit', name = 'child process' }) => {
9
+ try {
10
+ Assert.string(path)
11
+ const actualArgv = ['--ipc-type=node-forked-process', ...argv]
12
+ const childProcess = fork(path, actualArgv, {
13
+ env,
14
+ execArgv,
15
+ stdio: 'pipe',
16
+ })
17
+ const { type, event, stdout, stderr } = await GetFirstNodeChildProcessEvent.getFirstNodeChildProcessEvent(childProcess)
18
+ if (type === FirstNodeWorkerEventType.Exit) {
19
+ throw new ChildProcessError(stderr)
20
+ }
21
+ if (type === FirstNodeWorkerEventType.Error) {
22
+ throw new Error(`child process had an error ${event}`)
23
+ }
24
+ if (stdio === 'inherit' && childProcess.stdout && childProcess.stderr) {
25
+ childProcess.stdout.pipe(process.stdout)
26
+ childProcess.stderr.pipe(process.stderr)
27
+ }
28
+ return childProcess
29
+ } catch (error) {
30
+ throw new VError(error, `Failed to launch ${name}`)
31
+ }
18
32
  }
19
33
 
20
34
  export const wrap = (childProcess) => {
@@ -23,11 +37,14 @@ export const wrap = (childProcess) => {
23
37
  on(event, listener) {
24
38
  this.childProcess.on(event, listener)
25
39
  },
40
+ off(event, listener) {
41
+ this.childProcess.off(event, listener)
42
+ },
26
43
  send(message) {
27
44
  this.childProcess.send(message)
28
45
  },
29
- sendAndTransfer(message, transfer) {
30
- throw new Error('transfer is not supported')
46
+ sendAndTransfer(message, handle) {
47
+ this.childProcess.send(message, handle)
31
48
  },
32
49
  dispose() {
33
50
  this.childProcess.kill()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",