Skip to content

refactor(wip): replace node-ipc with a cleanroom IPC implementation #5776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
hasProjectYarn,
hasProjectPnpm,
openBrowser,
IpcMessenger
NewIpcMessenger
} = require('@vue/cli-shared-utils')

const defaults = {
Expand Down Expand Up @@ -301,7 +301,7 @@ module.exports = (api, options) => {

// Send final app URL
if (args.dashboard) {
const ipc = new IpcMessenger()
const ipc = new NewIpcMessenger()
ipc.send({
vueServe: {
url: localUrlForBrowser
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-service/lib/webpack/DashboardPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
const path = require('path')
const fs = require('fs-extra')
const webpack = require('webpack')
const { IpcMessenger } = require('@vue/cli-shared-utils')
const { NewIpcMessenger } = require('@vue/cli-shared-utils')
const { analyzeBundle } = require('./analyzeBundle')

const ID = 'vue-cli-dashboard-plugin'
const ONE_SECOND = 1000
const FILENAME_QUERY_REGEXP = /\?.*$/

const ipc = new IpcMessenger()
const ipc = new NewIpcMessenger()

function getTimeMessage (timer) {
let time = Date.now() - timer
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli-shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'env',
'exit',
'ipc',
'NewIpcMessenger',
'logger',
'module',
'object',
Expand Down
113 changes: 113 additions & 0 deletions packages/@vue/cli-shared-utils/lib/NewIpcMessenger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})
}
}
4 changes: 2 additions & 2 deletions packages/@vue/cli-shared-utils/lib/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.IpcMessenger = class IpcMessenger {
this.connected = false
this.connecting = false
this.disconnecting = false
this.queue = null
this.queue = []
this.options = options

this.listeners = []
Expand Down Expand Up @@ -80,7 +80,7 @@ exports.IpcMessenger = class IpcMessenger {
this.connected = true
this.connecting = false
this.queue && this.queue.forEach(data => this.send(data))
this.queue = null
this.queue = []

ipc.of[this.id].on('message', this._onMessage)
})
Expand Down