-
Notifications
You must be signed in to change notification settings - Fork 4
/
pgoCommonUtils.js
53 lines (51 loc) · 1.35 KB
/
pgoCommonUtils.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
const os = require('os');
const path = require('path');
const fs = require('fs');
let rrc = null;
let loadedPgo = false;
let recordPgo = false;
let pgoEntries = null;
let pgoFilePath = null;
let rrcErr;
let recorded = false;
exports.start = () => {
try {
rrc = require('alinode/relational_require_cache');
} catch (err) {
rrcErr = err.message;
}
if(!rrc) { // 兼容老版本
try {
rrc = require('strontium/relational_require_cache');
rrcErr = null;
} catch (err) { }
}
recordPgo = process.env.PGO_RECORD || process.argv.indexOf('--record-pgo') > -1;
pgoEntries = [ process.cwd(), path.join(__dirname, 'node_modules') ];
loadedPgo = false;
if(recordPgo) {
if (rrc && !recorded) {
recorded = true;
rrc.record(pgoEntries);
}
} else if(rrc) {
pgoFilePath = path.join(__dirname, 'require_cache.strrc');
if (fs.existsSync(pgoFilePath)) {
rrc.load(pgoFilePath, pgoEntries);
loadedPgo = true;
}
}
}
exports.end = () => {
if(recordPgo && rrc) {
pgoFilePath = path.join(os.tmpdir(), 'require_cache.strrc');
rrc.dump(pgoFilePath);
}
}
exports.info = (event) => {
const { type, start, size } = JSON.parse(event.toString());
if (type === 'size') {
return fs.statSync(pgoFilePath).size;
}
return fs.readFileSync(pgoFilePath).slice(start,start + size).toString('base64')
}