New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

live-dev-server

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

live-dev-server - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

12

extra.inject.js

@@ -1,10 +0,6 @@

// 文件示例说明
if (msgData.indexOf('sign') < 0) return
let data = JSON.parse(msgData)
// example inject code
if (msgData.indexOf('sign') < 0) return;
let data = JSON.parse(msgData);
if (data.sign == 'reload') {
window.location.reload()
window.location.reload();
}
if (data.sign == 'example') {
console.log('---- -----')
}

@@ -17,3 +17,12 @@ const http = require('http')

port: '3002',
proxy: {},
proxy: {
'/api': {
target: 'http://xhx.xstable.kaikela.cn',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
},
},
before: null,

@@ -25,9 +34,26 @@ workspace: './demo',

}
const protocol = 'http'
const { port, host, proxy } = opts
const openHost = host === '0.0.0.0' ? 'localhost' : host
const app = connect()
const onDirectory = createNotFoundDirectoryListener()
const { port, host } = opts
const INJECT_TAG = '</body>'
const CUSTOME_INJECT_POSITION = `//////`
const INJECT_SCRIPT = fs.readFileSync('./inject.html', { encoding: 'utf-8' })
const server = http.createServer(app)
const wsIns = new WebSocket.Server({ server })
const watcher = chokidar.watch(opts.workspace, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
})
let openURL = ''
let clients = []
let customInjectScript = ''
let handleInject = function () {}
// 读取自定义inject片段
try {

@@ -39,14 +65,35 @@ customInjectScript = fs.readFileSync(

} catch (error) {
console.error(error)
console.log('没有找到自定义的inject文件')
}
const INJECT_TAG = '</body>'
const CUSTOME_INJECT_POSITION = `//////`
app.use(bodyParser.urlencoded({ extended: false }))
const onDirectory = createNotFoundDirectoryListener()
app.use(function (req, res, next) {
var originalUrl = parseUrl.original(req)
var reqPath = parseUrl(req).pathname
let proxyServer = null
app.use(function (req, res, next) {
if (proxy) {
// 处理代理
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') {

@@ -61,5 +108,2 @@ // method not allowed

var originalUrl = parseUrl.original(req)
var reqPath = parseUrl(req).pathname
// make sure redirect occurs at mount

@@ -70,2 +114,3 @@ if (reqPath === '/' && originalUrl.pathname.substr(-1) !== '/') {

let needInject = reqPath == '' || reqPath.indexOf('.html') > -1
let sendOpts = Object.assign(

@@ -78,6 +123,5 @@ {

var handleInject = function () {}
if (reqPath == '/' || reqPath.indexOf('.html') > -1) {
if (needInject) {
handleInject = function (stream) {
var len = INJECT_SCRIPT.length
let len = INJECT_SCRIPT.length
if (customInjectScript.length) {

@@ -87,8 +131,7 @@ len += customInjectScript.length

len += res.getHeader('Content-Length')
res.setHeader('Content-Length', len)
var originalPipe = stream.pipe
let originalPipe = stream.pipe
stream.pipe = function (resq) {
// TODO 此处优化掉es模块
let tmpStream = originalPipe.call(

@@ -114,16 +157,10 @@ stream,

// create send stream
const sendStream = send(req, reqPath, sendOpts)
let sendStream = send(req, reqPath, sendOpts)
// add directory handler
sendStream.on('directory', onDirectory)
// pipe
sendStream.on('stream', handleInject).pipe(res)
needInject && sendStream.on('stream', handleInject)
sendStream.pipe(res)
})
const server = http.createServer(app)
const wsIns = new WebSocket.Server({ server })
let clients = []
wsIns.on('connection', function connection(ws) {

@@ -143,6 +180,2 @@ ws.on('message', function incoming(message) {

const watcher = chokidar.watch(opts.workspace, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
})
watcher.on('change', (path) => {

@@ -155,7 +188,5 @@ clients.forEach((ws) => {

server.addListener('listening', function () {
const protocol = 'http'
const address = server.address()
var openHost = host === '0.0.0.0' ? 'localhost' : host
let serveHost = address.address === '0.0.0.0' ? '127.0.0.1' : address.address
var openURL = protocol + '://' + openHost + ':' + address.port
// let serveHost = address.address === '0.0.0.0' ? '127.0.0.1' : address.address
openURL = protocol + '://' + openHost + ':' + address.port

@@ -165,16 +196,2 @@ console.log(openURL)

server.addListener('upgrade', function (request, socket, head) {
let ws = new WebSocket(request, socket, head)
ws.onopen = () => ws.send('connected')
clients.push(ws)
ws.onclose = () => {
// refresh page, remove match ws
clients = clients.filter((itm) => {
return ws != itm
})
}
})
server.listen(port)

@@ -206,3 +223,3 @@

function generateWatchMessage(sign, path) {
return generateMessage(sign, { path })
return generateMessage(sign, { path, host: openURL })
}
{
"name": "live-dev-server",
"version": "0.1.0",
"version": "0.2.0",
"description": "develop env, watch files change, mock data",

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc