1
- const ipc = require ( '@achrinza/node-ipc' )
1
+ const { getPipePath, encodeIpcData, parseIpcData } = require ( './env' )
2
+ const net = require ( 'net' )
2
3
3
4
const DEFAULT_ID = process . env . VUE_CLI_IPC || 'vue-cli'
4
5
const DEFAULT_IDLE_TIMEOUT = 3000
@@ -15,12 +16,18 @@ const PROJECT_ID = process.env.VUE_CLI_PROJECT_ID
15
16
exports . IpcMessenger = class IpcMessenger {
16
17
constructor ( options = { } ) {
17
18
options = Object . assign ( { } , DEFAULT_OPTIONS , options )
18
- ipc . config . id = this . id = options . networkId
19
- ipc . config . retry = 1500
20
- ipc . config . silent = true
19
+ this . id = options . networkId
20
+ this . retry = 1500
21
+ this . ipcTimer = null
22
+ this . reserveData = {
23
+ contentLength : - 1 ,
24
+ rawData : ''
25
+ }
26
+ this . socket = null
21
27
22
28
this . connected = false
23
29
this . connecting = false
30
+ this . disconnected = false
24
31
this . disconnecting = false
25
32
this . queue = null
26
33
this . options = options
@@ -40,7 +47,7 @@ exports.IpcMessenger = class IpcMessenger {
40
47
}
41
48
42
49
checkConnection ( ) {
43
- if ( ! ipc . of [ this . id ] ) {
50
+ if ( ! this . socket ) {
44
51
this . connected = false
45
52
}
46
53
}
@@ -55,7 +62,8 @@ exports.IpcMessenger = class IpcMessenger {
55
62
}
56
63
}
57
64
58
- ipc . of [ this . id ] . emit ( type , data )
65
+ const massages = encodeIpcData ( type , data )
66
+ this . socket . write ( massages )
59
67
60
68
clearTimeout ( this . idleTimer )
61
69
if ( this . options . disconnectOnIdle ) {
@@ -76,14 +84,7 @@ exports.IpcMessenger = class IpcMessenger {
76
84
if ( this . connected || this . connecting ) return
77
85
this . connecting = true
78
86
this . disconnecting = false
79
- ipc . connectTo ( this . id , ( ) => {
80
- this . connected = true
81
- this . connecting = false
82
- this . queue && this . queue . forEach ( data => this . send ( data ) )
83
- this . queue = null
84
-
85
- ipc . of [ this . id ] . on ( 'message' , this . _onMessage )
86
- } )
87
+ this . _connectTo ( )
87
88
}
88
89
89
90
disconnect ( ) {
@@ -92,18 +93,11 @@ exports.IpcMessenger = class IpcMessenger {
92
93
this . disconnecting = true
93
94
this . connecting = false
94
95
95
- const ipcTimer = setTimeout ( ( ) => {
96
+ this . ipcTimer = setTimeout ( ( ) => {
96
97
this . _disconnect ( )
97
98
} , this . disconnectTimeout )
98
99
99
100
this . send ( { done : true } , 'ack' )
100
-
101
- ipc . of [ this . id ] . on ( 'ack' , data => {
102
- if ( data . ok ) {
103
- clearTimeout ( ipcTimer )
104
- this . _disconnect ( )
105
- }
106
- } )
107
101
}
108
102
109
103
on ( listener ) {
@@ -118,25 +112,85 @@ exports.IpcMessenger = class IpcMessenger {
118
112
_reset ( ) {
119
113
this . queue = [ ]
120
114
this . connected = false
115
+ this . socket = null
121
116
}
122
117
123
118
_disconnect ( ) {
119
+ if ( ! this . socket ) {
120
+ return
121
+ }
124
122
this . connected = false
125
123
this . disconnecting = false
126
- ipc . disconnect ( this . id )
124
+ this . disconnected = true
125
+ this . socket . destroy ( )
127
126
this . _reset ( )
128
127
}
129
128
130
- _onMessage ( data ) {
131
- this . listeners . forEach ( fn => {
132
- if ( this . options . namespaceOnProject && data . _projectId ) {
133
- if ( data . _projectId === PROJECT_ID ) {
134
- data = data . _data
129
+ _onMessage ( massage ) {
130
+ let { type, data } = massage
131
+ if ( type === 'ack' ) {
132
+ if ( data . ok ) {
133
+ clearTimeout ( this . ipcTimer )
134
+ this . _disconnect ( )
135
+ }
136
+ } else {
137
+ this . listeners . forEach ( ( resolve , reject ) => {
138
+ if ( this . options . namespaceOnProject && data . _projectId ) {
139
+ if ( data . _projectId === PROJECT_ID ) {
140
+ data = data . _data
141
+ } else {
142
+ return
143
+ }
144
+ }
145
+ if ( type === 'error' ) {
146
+ reject ( data )
135
147
} else {
148
+ resolve ( data )
149
+ }
150
+ } )
151
+ }
152
+ }
153
+
154
+ _connectTo ( ) {
155
+ const pipPath = getPipePath ( this . id )
156
+ const socket = net . createConnection ( { path : pipPath } )
157
+ socket . setEncoding ( 'utf-8' )
158
+
159
+ socket . on ( 'connect' , ( ) => {
160
+ this . connected = true
161
+ this . connecting = false
162
+ this . queue && this . queue . forEach ( data => this . send ( data ) )
163
+ this . queue = null
164
+ } )
165
+
166
+ socket . on ( 'data' , ( massages ) => {
167
+ const queue = parseIpcData ( massages , this . reserveData )
168
+ queue . forEach ( massage => {
169
+ this . _onMessage ( massage )
170
+ } )
171
+ } )
172
+
173
+ socket . on ( 'close' , ( ) => {
174
+ if ( this . disconnected ) {
175
+ return
176
+ }
177
+ setTimeout ( ( ) => {
178
+ if ( this . disconnected ) {
179
+ this . _disconnect ( )
136
180
return
137
181
}
182
+ this . _connectTo ( )
183
+ } , this . retry )
184
+ } )
185
+
186
+ socket . on ( 'error' , ( error ) => {
187
+ const massage = {
188
+ type : 'error' ,
189
+ data : error
138
190
}
139
- fn ( data )
191
+ this . _onMessage ( massage )
140
192
} )
193
+
194
+ this . socket = socket
141
195
}
142
196
}
0 commit comments