-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (36 loc) · 1.18 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
const fs = require('fs')
const path = require('path')
const Koa = require('koa')
const Router = require('koa-router')
const profiler = require('v8-profiler')
const thunkify = require('thunkify')
const logger = require('winston')
const app = new Koa()
const fileName = `v8-${Date.now()}.heapsnapshot`
const filePath = path.resolve(__dirname, fileName)
const router = new Router()
router.get('/snapshot', function * () {
const snapshot = profiler.takeSnapshot()
logger.info('Exporting snapshot...')
const snappy = yield new Promise((resolve, reject) => {
snapshot.export((err, snappy) => {
if (err) {
reject(err)
}
snapshot.delete()
resolve(snappy)
})
})
logger.info('Writing file...')
yield thunkify(fs.writeFile)(filePath, snappy)
logger.info('Stating file...')
const stats = yield thunkify(fs.stat)(filePath)
this.set('Last-Modified', stats.mtime.toUTCString())
this.set('Content-Length', stats.size)
this.set('Cache-Control', 'max-age=' + 0)
this.set('Content-disposition', 'attachment; filename=' + fileName)
this.type = 'heapsnapshot'
this.body = fs.createReadStream(filePath)
})
app.use(router.routes())
app.listen(3001)