-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathipc.js
196 lines (169 loc) · 4.26 KB
/
ipc.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const { getPipePath, encodeIpcData, parseIpcData } = require('./env')
const net = require('net')
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.IpcMessenger = class IpcMessenger {
constructor (options = {}) {
options = Object.assign({}, DEFAULT_OPTIONS, options)
this.id = options.networkId
this.retry = 1500
this.ipcTimer = null
this.reserveData = {
contentLength: -1,
rawData: ''
}
this.socket = null
this.connected = false
this.connecting = false
this.disconnected = false
this.disconnecting = false
this.queue = null
this.options = options
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
}
this._reset()
}
checkConnection () {
if (!this.socket) {
this.connected = false
}
}
send (data, type = 'message') {
this.checkConnection()
if (this.connected) {
if (this.options.namespaceOnProject && PROJECT_ID) {
data = {
_projectId: PROJECT_ID,
_data: data
}
}
const massages = encodeIpcData(type, data)
this.socket.write(massages)
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 () {
this.checkConnection()
if (this.connected || this.connecting) return
this.connecting = true
this.disconnecting = false
this._connectTo()
}
disconnect () {
this.checkConnection()
if (!this.connected || this.disconnecting) return
this.disconnecting = true
this.connecting = false
this.ipcTimer = setTimeout(() => {
this._disconnect()
}, this.disconnectTimeout)
this.send({ done: true }, 'ack')
}
on (listener) {
this.listeners.push(listener)
}
off (listener) {
const index = this.listeners.indexOf(listener)
if (index !== -1) this.listeners.splice(index, 1)
}
_reset () {
this.queue = []
this.connected = false
this.socket = null
}
_disconnect () {
if (!this.socket) {
return
}
this.connected = false
this.disconnecting = false
this.disconnected = true
this.socket.destroy()
this._reset()
}
_onMessage (massage) {
let { type, data } = massage
if (type === 'ack') {
if (data.ok) {
clearTimeout(this.ipcTimer)
this._disconnect()
}
} else {
this.listeners.forEach((resolve, reject) => {
if (this.options.namespaceOnProject && data._projectId) {
if (data._projectId === PROJECT_ID) {
data = data._data
} else {
return
}
}
if (type === 'error') {
reject(data)
} else {
resolve(data)
}
})
}
}
_connectTo () {
const pipPath = getPipePath(this.id)
const socket = net.createConnection({ path: pipPath })
socket.setEncoding('utf-8')
socket.on('connect', () => {
this.connected = true
this.connecting = false
this.queue && this.queue.forEach(data => this.send(data))
this.queue = null
})
socket.on('data', (massages) => {
const queue = parseIpcData(massages, this.reserveData)
queue.forEach(massage => {
this._onMessage(massage)
})
})
socket.on('close', () => {
if (this.disconnected) {
return
}
setTimeout(() => {
if (this.disconnected) {
this._disconnect()
return
}
this._connectTo()
}, this.retry)
})
socket.on('error', (error) => {
const massage = {
type: 'error',
data: error
}
this._onMessage(massage)
})
this.socket = socket
}
}