This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathipc.ts
More file actions
56 lines (50 loc) · 1.31 KB
/
ipc.ts
File metadata and controls
56 lines (50 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ChildProcess } from 'child_process'
import type { CompileParams } from './compiler'
export type IPCMessage = {
cmd?: string
code?: string
error?: string
message?: string
willTerminate?: boolean
required?: string
}
/**
* Checks if the given message is an internal node-dev message.
*/
function isNodeDevMessage(m: any): m is IPCMessage {
return m.cmd === 'NODE_DEV'
}
/**
* Check if the given message is an compile request message.
*/
function isCompileRequestMessage(m: any): m is CompileParams {
return typeof m.compile === 'string' && typeof m.compiledPath === 'string'
}
/**
* Sends a message to the given process.
*/
export const send = function (m: IPCMessage, dest: NodeJS.Process = process) {
m.cmd = 'NODE_DEV'
if (dest.send) dest.send(m)
}
export const on = function (
proc: ChildProcess,
type: string,
cb: (m: IPCMessage) => void
) {
function handleMessage(m: any) {
if (isNodeDevMessage(m) && type in m) cb(m)
}
proc.on('internalMessage', handleMessage)
proc.on('message', handleMessage)
}
export const relay = function (
src: ChildProcess,
dest: NodeJS.Process = process
) {
function relayMessage(m: any) {
if (isNodeDevMessage(m) || isCompileRequestMessage(m)) dest.send!(m)
}
src.on('internalMessage', relayMessage)
src.on('message', relayMessage)
}