-
Notifications
You must be signed in to change notification settings - Fork 30
/
livereload.js
159 lines (138 loc) · 4.02 KB
/
livereload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
var parseUrl = require('parseurl')
var WS = require('ws').Server
var EventEmitter = require('events').EventEmitter
var prefix = '/__lightserver__'
var clientJsPath = prefix + '/reload-client.js'
var triggerPath = prefix + '/trigger'
var triggerCSSPath = prefix + '/triggercss'
var clientJsContent = [
'var ws',
'function socket() {',
' ws = new WebSocket("%WS_PROTOCOL%://" + window.location.host)',
' ws.onmessage = function (e) {',
' var data = JSON.parse(e.data)',
' if (data.r) {',
' location.reload()',
' }',
' if (data.rcss) {',
' refreshCSS()',
' }',
' }',
'}',
'function refreshCSS() {',
' console.log("reload css at:" + new Date())',
' var sheets = document.getElementsByTagName("link");',
' for (var i = 0; i < sheets.length; i++) {',
' var elem = sheets[i];',
' var rel = elem.rel;',
' if (elem.href && elem.href.substring(0, 5) !== "data:" && (typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet")) {',
' var url = elem.href.replace(/(&|\\?)_cacheOverride=\\d+/, "");',
' elem.href = url + (url.indexOf("?") >= 0 ? "&" : "?") + "_cacheOverride=" + (new Date().valueOf());',
' }',
' }',
'}',
'socket()',
'setInterval(function () {',
' if (ws) {',
' if (ws.readyState !== 1) {',
' socket()',
' }',
' } else {',
' socket()',
' }',
'}, 3000)'
].join('\n')
var emitter = new EventEmitter()
var wss
var wsArray = []
function Livereload (options) {
if (!(this instanceof Livereload)) return new Livereload(options)
this.options = options
clientJsContent = clientJsContent.replace('%WS_PROTOCOL%', this.options.http2 ? 'wss' : 'ws')
}
Livereload.prototype.writeLog = function (logLine) {
!this.options.quiet && console.log(logLine)
}
Livereload.prototype.middleFunc = function livereload (req, res, next) {
var pathname = parseUrl(req).pathname
if (req.method === 'GET' && pathname === '/favicon.ico') {
res.writeHead(204)
res.end('favicon not found')
return
}
if (pathname.indexOf(prefix) === -1) {
next()
return
}
if (req.method === 'GET' && pathname === clientJsPath) {
res.writeHead(200, { 'Content-Type': 'application/javascript' })
res.end(clientJsContent)
return
}
if (pathname === triggerPath) {
res.writeHead(200)
res.end('ok')
emitter.emit('reload')
return
}
if (pathname === triggerCSSPath) {
res.writeHead(200)
res.end('ok')
emitter.emit('reloadcss')
}
next()
}
Livereload.prototype.startWS = function (server) {
var _this = this
wss = new WS({ server: server })
wss.on('connection', function (ws) {
wsArray.push(ws)
ws.on('close', function () {
var index = wsArray.indexOf(ws)
if (index > -1) {
wsArray.splice(index, 1)
}
})
})
emitter.on('reload', function () {
_this.writeLog('## send reload event via websocket to browser')
wsArray.forEach(function (w) {
w.send(JSON.stringify({ r: Date.now().toString() }), function (e) {
if (e) { console.log('websocket send error: ' + e) }
})
})
})
emitter.on('reloadcss', function () {
_this.writeLog('## send reloadcss event via websocket to browser')
wsArray.forEach(function (w) {
w.send(JSON.stringify({ rcss: Date.now().toString() }), function (e) {
if (e) { console.log('websocket send error: ' + e) }
})
})
})
}
Livereload.prototype.triggerReload = function (delay) {
if (delay) {
this.writeLog('delay reload for ' + delay + ' ms')
}
setTimeout(function () {
emitter.emit('reload')
}, delay)
}
Livereload.prototype.triggerCSSReload = function (delay) {
if (delay) {
this.writeLog('delay reloadcss for ' + delay + ' ms')
}
setTimeout(function () {
emitter.emit('reloadcss')
}, delay)
}
Livereload.prototype.trigger = function (action, delay) {
if (action === 'reloadcss') {
this.triggerCSSReload(delay)
} else if (action === 'reload') {
this.triggerReload(delay)
}
}
module.exports = Livereload