forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewIpcMessenger.js
113 lines (94 loc) · 2.74 KB
/
NewIpcMessenger.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const net = require('net')
const path = require('path')
const DEFAULT_ID = process.env.VUE_CLI_IPC || 'vue-cli'
const DEFAULT_IDLE_TIMEOUT = 3000
const DEFAULT_OPTIONS = {
networkId: DEFAULT_ID,
autoConnect: true,
disconnectOnIdle: false,
idleTimeout: DEFAULT_IDLE_TIMEOUT,
namespaceOnProject: true
}
const PROJECT_ID = process.env.VUE_CLI_PROJECT_ID
exports.NewIpcMessenger = class NewIpcMessenger {
constructor (options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options)
this.id = this.options.networkId
// per the node-ipc documentation
// TODO: windows socket path
this.socketPath = path.join('/tmp/', `app.${this.id}`)
this.connected = false
this.connecting = false
this.disconnecting = false
this.queue = []
this.listeners = []
this.disconnectTimeout = 15000
this.idleTimer = null
// Prevent forced process exit
// (or else ipc messages may not be sent before kill)
process.exit = code => {
process.exitCode = code
}
}
checkConnection () {
// TODO: not sure how to abstract this under the current implementation
}
send (data, type = 'message') {
if (this.connected) {
if (this.options.namespaceOnProject && PROJECT_ID) {
data = {
_projectId: PROJECT_ID,
_data: data
}
}
// the packet format is compatible with node-ipc default
this._client.write(JSON.stringify({ type, data }) + '\f', 'utf8')
clearTimeout(this.idleTimer)
if (this.options.disconnectOnIdle) {
this.idleTimer = setTimeout(() => {
this.disconnect()
}, this.options.idleTimeout)
}
} else {
this.queue.push(data)
if (this.options.autoConnect && !this.connecting) {
this.connect()
}
}
}
connect () {
if (this.connected || this.connecting) return
this.connecting = true
this.disconnecting = false
// TODO: check the socketPath, unlink if existed
// TODO: server side
// net.createServer().listen(this.socketPath)
// client side
this._client = net.createConnection({ path: this.socketPath }, () => {
this.connected = true
this.connecting = false
this.queue && this.queue.forEach(data => this.send(data))
this.queue = []
})
}
disconnect () {}
on (listener) {
this.listeners.push(listener)
}
off (listener) {
const index = this.listeners.indexOf(listener)
if (index !== -1) this.listeners.splice(index, 1)
}
_onMessage (data) {
this.listeners.forEach(fn => {
if (this.options.namespaceOnProject && data._projectId) {
if (data._projectId === PROJECT_ID) {
data = data._data
} else {
return
}
}
fn(data)
})
}
}