-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·141 lines (120 loc) · 4.86 KB
/
index.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
#!/usr/bin/env node
import fs from 'fs'
import http from 'http'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import { URL } from 'url'
import isBot from './lib/is-bot.js'
import trackEvent from './lib/track-event.js'
import trackPageview from './lib/track-pageview.js'
import parseOptions from './lib/parse-options.js'
import parseEvent from './lib/parse-event.js'
import parsePageview from './lib/parse-pageview.js'
import readMemory from './lib/read-memory.js'
import * as analyticsCache from './lib/analytics-cache.js'
import * as backup from './lib/backup.js'
import * as cacheScheduler from './lib/cache-scheduler.js'
export {
start
}
if (import.meta.url === `file://${process.argv[1]}`) {
start(process.env)
}
async function start (env = process.env, memory) {
const options = parseOptions(env)
if (!options.STATS_BASE_URL) throw new Error('MISSING_STATS_BASE_URL')
if (!options.SITE_BASE_URL) throw new Error('MISSING_SITE_BASE_URL')
process.stdout.write(`starting with options ${JSON.stringify(options)}\n\n`)
process.stdout.write(`
minimal-analytics listening on ${options.STATS_BASE_URL}
tracking pageviews from ${options.SITE_BASE_URL}\n`)
backup.start(options)
const serve = serveStatic('dashboard/dist')
const CLIENT_JS = fs.readFileSync(new URL('./client.js', import.meta.url).pathname, 'utf-8').replace('{{STATS_BASE_URL}}', options.STATS_BASE_URL)
memory = readMemory(options, memory)
console.log('memory read', memory.length)
cacheScheduler.start(memory)
const live = {}
const connections = []
const server = http.createServer(function (req, res) {
if (isBot(req.headers['user-agent'])) return res.end()
res.setHeader('Access-Control-Allow-Origin', options.SITE_BASE_URL)
res.setHeader('Access-Control-Request-Method', 'POST,GET')
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS,POST,GET')
res.setHeader('Access-Control-Allow-Headers', '*')
if (req.method === 'OPTIONS') return res.end()
process.stdout.write(`${new Date().toISOString()} ${req.method} ${req.url}\n`)
if (req.method === 'POST' && req.url === '/p') {
parsePageview(req, (err, pageview) => {
if (err) return console.error('error parsing pageview', err.message)
trackPageview(pageview, options, memory, live)
process.stdout.write(`${pageview.d} ${pageview.p} ${pageview.v}`)
broadcastSSE(JSON.stringify(live), connections)
})
res.setHeader('Content-type', 'application/json')
return res.end('"ok"')
}
if (req.method === 'POST' && req.url === '/e') {
parseEvent(req, (err, event) => {
if (err) return console.error('error parsing event', err.message)
trackEvent(event, options, memory, live)
process.stdout.write(`${event.d} ${event.p} ${event.v}`)
broadcastSSE(JSON.stringify(live), connections)
})
return replyJSON(res, 'ok')
}
if (req.method === 'GET' && /^\/live/.test(req.url)) {
return replyJSON(res, live)
}
if (req.method === 'GET' && /^\/api\/pageviews\/.*/.test(req.url)) {
const [_, url] = req.url.match(/^\/api\/pageviews(\/.*)/)
const result = analyticsCache.getAll(`/?timeframe=all&p=${url}`, memory, live)
return replyJSON(res, result.pageviewsCount)
}
if (req.method === 'GET' && /^\/api/.test(req.url)) {
const result = analyticsCache.getAll(req.url, memory, live)
return replyJSON(res, result)
}
if (req.method === 'GET' && req.url === '/client.js') {
res.setHeader('Content-type', 'text/javascript')
return res.end(CLIENT_JS)
}
if (req.headers.accept && req.headers.accept.indexOf('text/event-stream') >= 0) {
handleSSE(res, connections)
return broadcastSSE(JSON.stringify(live), [res])
}
return serve(req, res, finalhandler(req, res))
})
setInterval(() => {
Object.keys(live).forEach(visitor => {
if (live[visitor] && live[visitor].heartbeat < Date.now() - 60000) delete live[visitor]
})
broadcastSSE(JSON.stringify(live), connections)
}, 5000)
process.stdout.write(`listening on http://127.0.0.1:${options.HTTP_PORT}\n`)
server.listen(options.HTTP_PORT)
return { server, memory }
function handleSSE (res, connections = []) {
connections.push(res)
res.on('close', () => {
connections.splice(connections.findIndex(c => res === c), 1)
})
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive'
})
}
function broadcastSSE (data, connections = []) {
connections.forEach(connection => {
if (!connection) return
const id = new Date().toISOString()
connection.write('id: ' + id + '\n')
connection.write('data: ' + data + '\n\n')
})
}
function replyJSON (res, obj) {
res.setHeader('Content-type', 'application/json')
return res.end(JSON.stringify(obj))
}
}