Skip to content

Commit af5363f

Browse files
committed
fix: compatile with older version of cli-service
1 parent 2bc581a commit af5363f

File tree

3 files changed

+105
-159
lines changed

3 files changed

+105
-159
lines changed

packages/@vue/cli-shared-utils/lib/env.js

+26-53
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const _gitProjects = new LRU({
1515
max: 10,
1616
maxAge: 1000
1717
})
18-
const CRLF = '\r\n'
18+
const DELIMITER = '\f'
1919

2020
// env detection
2121
exports.hasYarn = () => {
@@ -219,70 +219,43 @@ exports.getInstalledBrowsers = () => {
219219
return browsers
220220
}
221221

222-
exports.getPipePath = (id) => {
223-
if (exports.isWindows && !id.startsWith('\\\\.\\pipe\\')) {
222+
exports.getIpcPath = (id) => {
223+
id = '/tmp/app.' + id
224+
if (exports.isWindows) {
224225
id = id.replace(/^\//, '')
225226
id = id.replace(/\//g, '-')
226227
id = `\\\\.\\pipe\\${id}`
227-
} else {
228-
id = `/tmp/app.${id}`
229228
}
230229
return id
231230
}
232231

233232
exports.encodeIpcData = (type, data) => {
234-
const jsonstr = JSON.stringify({
235-
data,
236-
type
237-
})
238-
const massage = `Content-Length: ${Buffer.byteLength(jsonstr)}${CRLF + CRLF}${jsonstr}`
239-
return Buffer.from(massage)
233+
if (!data && data !== false && data !== 0) {
234+
data = {}
235+
}
236+
if (data._maxListeners) {
237+
data = {}
238+
}
239+
const message = JSON.stringify({ type, data })
240+
return Buffer.from(message + DELIMITER)
240241
}
241242

242-
exports.parseIpcData = (data, reserveData) => {
243-
let { contentLength, rawData } = reserveData
244-
rawData += data
243+
exports.decodeIpcData = (data) => {
244+
if (data.slice(-1) !== DELIMITER || data.indexOf(DELIMITER) === -1) {
245+
return
246+
}
245247
const messages = []
246-
while (true) {
247-
if (contentLength >= 0) {
248-
if (rawData.length >= contentLength) {
249-
const message = rawData.slice(0, contentLength)
250-
rawData = rawData.slice(contentLength)
251-
contentLength = -1
252-
if (message.length > 0) {
253-
let msg
254-
try {
255-
msg = JSON.parse(message)
256-
} catch (error) {
257-
msg = {
258-
type: 'error',
259-
data: `Error handling data: ${error}`
260-
}
261-
}
262-
messages.push(msg)
263-
}
264-
continue
265-
}
266-
} else {
267-
const idx = rawData.indexOf(CRLF + CRLF)
268-
if (idx !== -1) {
269-
const header = rawData.slice(0, idx)
270-
const lines = header.split(CRLF)
271-
for (let i = 0; i < lines.length; i++) {
272-
const pair = lines[i].split(/: +/)
273-
if (pair[0] === 'Content-Length') {
274-
contentLength = +pair[1]
275-
}
276-
}
277-
rawData = rawData.slice(idx + (CRLF + CRLF).length)
278-
continue
279-
}
248+
const lines = data.split(DELIMITER)
249+
lines.pop()
250+
for (const line of lines) {
251+
try {
252+
messages.push(JSON.parse(line))
253+
} catch (error) {
254+
messages.push({
255+
type: 'error',
256+
data: `Error handling data: ${error}`
257+
})
280258
}
281-
break
282259
}
283-
284-
reserveData.contentLength = contentLength
285-
reserveData.rawData = rawData
286-
287260
return messages
288261
}

packages/@vue/cli-shared-utils/lib/ipc.js

+25-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getPipePath, encodeIpcData, parseIpcData } = require('./env')
1+
const { getIpcPath, encodeIpcData, decodeIpcData } = require('./env')
22
const net = require('net')
33

44
const DEFAULT_ID = process.env.VUE_CLI_IPC || 'vue-cli'
@@ -19,15 +19,11 @@ exports.IpcMessenger = class IpcMessenger {
1919
this.id = options.networkId
2020
this.retry = 1500
2121
this.ipcTimer = null
22-
this.reserveData = {
23-
contentLength: -1,
24-
rawData: ''
25-
}
2622
this.socket = null
2723

2824
this.connected = false
2925
this.connecting = false
30-
this.disconnected = false
26+
this.explicitlyDisconnected = false
3127
this.disconnecting = false
3228
this.queue = null
3329
this.options = options
@@ -62,8 +58,8 @@ exports.IpcMessenger = class IpcMessenger {
6258
}
6359
}
6460

65-
const massages = encodeIpcData(type, data)
66-
this.socket.write(massages)
61+
const message = encodeIpcData(type, data)
62+
this.socket.write(message)
6763

6864
clearTimeout(this.idleTimer)
6965
if (this.options.disconnectOnIdle) {
@@ -92,6 +88,7 @@ exports.IpcMessenger = class IpcMessenger {
9288
if (!this.connected || this.disconnecting) return
9389
this.disconnecting = true
9490
this.connecting = false
91+
this.explicitlyDisconnected = true
9592

9693
this.ipcTimer = setTimeout(() => {
9794
this._disconnect()
@@ -116,44 +113,38 @@ exports.IpcMessenger = class IpcMessenger {
116113
}
117114

118115
_disconnect () {
119-
if (!this.socket) {
120-
return
121-
}
122116
this.connected = false
123117
this.disconnecting = false
124-
this.disconnected = true
125-
this.socket.destroy()
118+
if (this.socket) {
119+
this.socket.destroy()
120+
}
126121
this._reset()
127122
}
128123

129-
_onMessage (massage) {
130-
let { type, data } = massage
124+
_onMessage (message) {
125+
let { type, data } = message
131126
if (type === 'ack') {
132127
if (data.ok) {
133128
clearTimeout(this.ipcTimer)
134129
this._disconnect()
135130
}
136-
} else {
137-
this.listeners.forEach((resolve, reject) => {
131+
} else if (type === 'message') {
132+
this.listeners.forEach((fn) => {
138133
if (this.options.namespaceOnProject && data._projectId) {
139134
if (data._projectId === PROJECT_ID) {
140135
data = data._data
141136
} else {
142137
return
143138
}
144139
}
145-
if (type === 'error') {
146-
reject(data)
147-
} else {
148-
resolve(data)
149-
}
140+
fn(data)
150141
})
151142
}
152143
}
153144

154145
_connectTo () {
155-
const pipPath = getPipePath(this.id)
156-
const socket = net.createConnection({ path: pipPath })
146+
const ipcPath = getIpcPath(this.id)
147+
const socket = net.createConnection({ path: ipcPath })
157148
socket.setEncoding('utf-8')
158149

159150
socket.on('connect', () => {
@@ -163,32 +154,28 @@ exports.IpcMessenger = class IpcMessenger {
163154
this.queue = null
164155
})
165156

166-
socket.on('data', (massages) => {
167-
const queue = parseIpcData(massages, this.reserveData)
168-
queue.forEach(massage => {
169-
this._onMessage(massage)
157+
socket.on('data', (data) => {
158+
const messages = decodeIpcData(data)
159+
messages.forEach(message => {
160+
this._onMessage(message)
170161
})
171162
})
172163

173164
socket.on('close', () => {
174-
if (this.disconnected) {
165+
if (this.explicitlyDisconnected) {
166+
this._disconnect()
175167
return
176168
}
177169
setTimeout(() => {
178-
if (this.disconnected) {
179-
this._disconnect()
180-
return
181-
}
182170
this._connectTo()
183171
}, this.retry)
184172
})
185173

186-
socket.on('error', (error) => {
187-
const massage = {
174+
socket.on('error', (err) => {
175+
this._onMessage({
188176
type: 'error',
189-
data: error
190-
}
191-
this._onMessage(massage)
177+
data: err
178+
})
192179
})
193180

194181
this.socket = socket

packages/@vue/cli-ui/apollo-server/util/ipc.js

+54-68
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,72 @@ const fs = require('fs')
33

44
// Utils
55
const { log, dumpObject } = require('../util/logger')
6-
const { getPipePath, encodeIpcData, parseIpcData } = require('@vue/cli-shared-utils')
6+
const { getIpcPath, encodeIpcData, decodeIpcData } = require('@vue/cli-shared-utils')
77

88
const id = process.env.VUE_CLI_IPC || 'vue-cli'
99

1010
const listeners = []
11+
let ipcSocket = null
1112

12-
const pipePath = getPipePath(id)
13+
function start () {
14+
const ipcPath = getIpcPath(id)
1315

14-
let curSocket = null
15-
16-
let reserveData = {
17-
contentLength: -1,
18-
rawData: ''
19-
}
16+
fs.unlink(ipcPath, () => {
17+
const server = net.createServer((socket) => {
18+
ipcSocket = socket
19+
if (socket.setEncoding) {
20+
socket.setEncoding('utf-8')
21+
}
2022

21-
fs.unlink(pipePath, () => {
22-
const server = net.createServer((socket) => {
23-
curSocket = socket
24-
if (socket.setEncoding) {
25-
socket.setEncoding('utf-8')
26-
}
23+
socket.on('data', (data) => {
24+
const messages = decodeIpcData(data)
25+
messages.forEach(message => {
26+
_onMessage(message)
27+
})
28+
})
2729

28-
socket.on('data', (massages) => {
29-
const queue = parseIpcData(massages, reserveData)
30-
queue.forEach(massage => {
31-
_onMessage(massage)
30+
socket.on('close', () => {
31+
if (socket && socket.destroy) {
32+
socket.destroy()
33+
}
34+
ipcSocket = null
3235
})
33-
})
3436

35-
socket.on('close', () => {
36-
if (curSocket && curSocket.destroy) {
37-
curSocket.destroy()
38-
}
39-
reserveData = {
40-
contentLength: -1,
41-
rawData: ''
42-
}
43-
curSocket = null
37+
socket.on('error', (error) => {
38+
_onMessage({
39+
type: 'error',
40+
data: error
41+
}, socket)
42+
})
4443
})
4544

46-
socket.on('error', (error) => {
47-
const massage = {
48-
type: 'error',
49-
data: error
50-
}
51-
_onMessage(massage)
45+
server.listen({
46+
path: ipcPath
5247
})
5348
})
49+
}
5450

55-
server.listen({
56-
path: pipePath
57-
})
58-
})
51+
function _onMessage (massage, socket) {
52+
const { type, data } = massage
53+
if (type === 'ack') {
54+
log('IPC ack', dumpObject(data))
55+
if (data.done) {
56+
socket.write(encodeIpcData('ack', { ok: true }))
57+
}
58+
} else if (type === 'message') {
59+
log('IPC message', dumpObject(data))
60+
for (const listener of listeners) {
61+
listener({
62+
data,
63+
emit: data => {
64+
socket.write(encodeIpcData('message', data))
65+
}
66+
})
67+
}
68+
}
69+
}
70+
71+
start()
5972

6073
function on (cb) {
6174
listeners.push(cb)
@@ -69,36 +82,9 @@ function off (cb) {
6982

7083
function send (data) {
7184
log('IPC send', dumpObject(data))
72-
const massages = encodeIpcData('message', data)
73-
curSocket.write(massages)
74-
}
75-
76-
function _onMessage (massage) {
77-
const { type, data } = massage
78-
if (type === 'ack') {
79-
log('IPC ack', dumpObject(data))
80-
if (data.done) {
81-
curSocket.write(encodeIpcData('ack', { ok: true }))
82-
}
83-
} else {
84-
log('IPC message', dumpObject(data))
85-
listeners.forEach((resolve, reject) => {
86-
if (type === 'error') {
87-
reject({
88-
data,
89-
emit: data => {
90-
curSocket.write(encodeIpcData('error', data))
91-
}
92-
})
93-
} else {
94-
resolve({
95-
data,
96-
emit: data => {
97-
curSocket.write(encodeIpcData('message', data))
98-
}
99-
})
100-
}
101-
})
85+
const message = encodeIpcData('message', data)
86+
if (ipcSocket) {
87+
ipcSocket.write(message)
10288
}
10389
}
10490

0 commit comments

Comments
 (0)