-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnaivePush.js
More file actions
34 lines (29 loc) · 888 Bytes
/
Copy pathnaivePush.js
File metadata and controls
34 lines (29 loc) · 888 Bytes
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
const debug = require('debug')
const fs = require('fs')
const {args:[path = '.']} = require('./options')
const [pushDebug, pushError] = [
'http2:push', 'http2:push:error'
].map(debug)
let fileByRefferer = {}
const servePush = (req, res, next) => {
if (fileByRefferer[req.url] && res.push) {
fileByRefferer[req.url].forEach(file => {
const stream = res.push(file, {})
fs.readFile(path+'/'+file, 'utf8', (e, content) => {
e && pushError(e)
stream.end(content)
})
})
}
next()
}
const rememberReferrers = (req, res, next) => {
if (req.headers.referer) {
const referer = req.headers.referer.replace(/.*:\d*/,'')
pushDebug(referer, req.url)
if (!fileByRefferer[referer]) fileByRefferer[referer] = []
fileByRefferer[referer].push(req.url)
}
next()
}
module.exports = [servePush, rememberReferrers]