You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP

live-dev-server

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

live-dev-server - npm Package Compare versions

Comparing version

to
0.8.0

#!/usr/bin/env node
'use strict';
const execa = require('execa');
const path = require('path');
'use strict'
const execa = require('execa')
const path = require('path')
process.title = 'live-dev-server';
process.title = 'live-dev-server'
execa('node', [path.resolve(__dirname, '../lib/index.js')]).stdout.pipe(process.stdout);
execa('node', [path.resolve(__dirname, '../lib/index.js')]).stdout.pipe(
process.stdout
)

@@ -11,70 +11,9 @@ const http = require('http')

const WebSocket = require('ws')
const { Log } = require('./utils')
const colors={
reset: "\x1b[0m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m"
};
const Log = {
error: function(...parm) {
console.log(`${colors.red}[Error]`, ...parm, colors.reset);
},
log: function(...parm) {
console.log(`[Log]`, ...parm);
},
warn: function(...parm) {
console.log(`${colors.yellow}[Warning]`, ...parm, colors.reset);
},
info: function(...parm) {
console.log(`${colors.green}[Info]`, ...parm, colors.reset);
},
}
const { options } = require('./config')
const projectRoot = process.cwd()
const defaultWorkspace = path.join(projectRoot, `./src`)
const opts = {
host: '0.0.0.0',
port: '3002',
proxy: {
'/api': {
target: 'http://xhx.xstable.kaikela.cn',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
},
},
before: function () {},
workspace: defaultWorkspace,
root: projectRoot,
send: {},
inject: function (event) {
// 自定义处理socket onmessage方法
let edata = event.data
if (edata.indexOf('origin') < 0) return
let data = JSON.parse(edata)
if (data.origin == 'watcher') {
window.location.reload()
}
},
}
// 读取配置文件
try {
let config = require(path.join(projectRoot, `./lds.config.js`))
let { workspace, ...restConfig } = config
let extra = {
workspace: workspace ? path.join(projectRoot, workspace) : defaultWorkspace,
}
// TODO deep merge
Object.assign(opts, restConfig, extra)
} catch (error) {
Log.info('没有lds.config.js配置文件')
}
const protocol = 'http'
const { port, host, proxy } = opts
const { port, host, proxy, workspace } = options
const openHost = host === '0.0.0.0' ? 'localhost' : host

@@ -87,9 +26,12 @@

const CUSTOME_INJECT_CODE_MARK = `//////`
const INJECT_SCRIPT_CODE = fs.readFileSync(path.join(__dirname, './inject.html'), {
encoding: 'utf-8',
})
const INJECT_SCRIPT_CODE = fs.readFileSync(
path.join(__dirname, './inject.html'),
{
encoding: 'utf-8',
}
)
const server = http.createServer(app)
const wsIns = new WebSocket.Server({ server })
const watcher = chokidar.watch(opts.workspace, {
const watcher = chokidar.watch(workspace, {
ignored: /(^|[\/\\])\../, // ignore dotfiles

@@ -107,9 +49,9 @@ persistent: true,

// 处理自定义inject片段
assertFunc(opts, 'inject', function () {
customInjectCode = `;(${opts.inject.toString()})(event);`
assertFunc(options, 'inject', function () {
customInjectCode = `;(${options.inject.toString()})(event);`
})
// 处理before钩子
assertFunc(opts, 'before', function () {
opts.before(app)
assertFunc(options, 'before', function () {
options.before(app)
})

@@ -121,2 +63,26 @@

// 处理代理
if (proxy) {
let proxyServer = null
for (const p in proxy) {
if (reqPath.slice(0, p.length) == p) {
proxyServer = httpProxy.createProxyServer({})
let proxyItemOpts = JSON.parse(JSON.stringify(proxy[p]))
let { target, pathRewrite } = proxyItemOpts
// 处理pathRewrite
let resultReqPath = reqPath
for (let pr in pathRewrite) {
resultReqPath = resultReqPath.replace(new RegExp(pr), pathRewrite[pr])
}
delete proxyItemOpts.pathRewrite
req.url = resultReqPath + originalUrl.search
target && proxyServer.web(req, res, proxyItemOpts)
return
}
}
}
if (req.method !== 'GET' && req.method !== 'HEAD') {

@@ -142,8 +108,8 @@ // method not allowed

originalUrl.pathname.substr(-1) === '/'
let sendOpts = Object.assign(
{
root: opts.root,
root: options.root,
},
opts.send
options.send
)

@@ -163,3 +129,6 @@

len += cisLen
injectCodeResult = injectCodeResult.replace(CUSTOME_INJECT_CODE_MARK, customInjectCode + CUSTOME_INJECT_CODE_MARK)
injectCodeResult = injectCodeResult.replace(
CUSTOME_INJECT_CODE_MARK,
customInjectCode + CUSTOME_INJECT_CODE_MARK
)
}

@@ -187,30 +156,2 @@

// 处理代理
if (proxy) {
app.use(function (req, res, next) {
var originalUrl = parseUrl.original(req)
var reqPath = parseUrl(req).pathname
let proxyServer = null
for (const p in proxy) {
if (reqPath.slice(0, p.length) == p) {
proxyServer = httpProxy.createProxyServer({})
let proxyItemOpts = JSON.parse(JSON.stringify(proxy[p]))
let { target, pathRewrite } = proxyItemOpts
// 处理pathRewrite
let resultReqPath = reqPath
for (let pr in pathRewrite) {
resultReqPath = resultReqPath.replace(new RegExp(pr), pathRewrite[pr])
}
delete proxyItemOpts.pathRewrite
req.url = resultReqPath + originalUrl.search
target && proxyServer.web(req, res, proxyItemOpts)
return
}
}
})
}
wsIns.on('connection', function connection(ws) {

@@ -273,7 +214,12 @@ ws.on('message', function incoming(message) {

let tmpPath = filePath.replace(projectRoot, '').split(path.sep).join('/')
return generateMessage(origin, { url: openURL + tmpPath, host: openURL, path: tmpPath, ext })
return generateMessage(origin, {
url: openURL + tmpPath,
host: openURL,
path: tmpPath,
ext,
})
}
/**
*
*
* @param {Object} option

@@ -280,0 +226,0 @@ * @param {*} propName 判断的属性名

{
"name": "live-dev-server",
"version": "0.7.2",
"version": "0.8.0",
"description": "develop env, watch files change, mock data",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

Sorry, the diff of this file is not supported yet