-
Notifications
You must be signed in to change notification settings - Fork 4
/
bff.js
113 lines (93 loc) · 2.43 KB
/
bff.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
const fs = require('fs');
const sh = require('shelljs');
const crypto = require('crypto');
const pingWebHook = require('./scripts/pingWebHook');
const fetchPmTech = require('./scripts/fetchPmTech');
const { allow } = require('./package.json');
const { pmTech: allowedPmTech } = allow;
const delay = 1000;
const runtime = {
pm: [''],
};
if (process.env.PM_TECH) {
sh.exec('mkdir -p public');
Object.keys(runtime).forEach((key) => {
if (runtime[key][0]) {
const fileBuffer = fs.readFileSync(runtime[key][0]);
const hashSum = crypto.createHash('sha1');
const ext = runtime[key][0]
.split('/')
.pop()
.split('.')
.pop();
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
runtime[key].push(`_${hex}.${ext}`);
sh.exec(`cp ${runtime[key][0]} public/${runtime[key][1]}`);
}
});
}
const prefetch = async () => {
sh.exec('mkdir -p bff-data');
await pingWebHook();
let pmTech = '';
if (process.env.PM_TECH) {
pmTech = await fetchPmTech();
pmTech = pmTech;
sh.exec('mkdir -p public');
Object.keys(runtime).forEach((key) => {
if (runtime[key][0]) {
const fileBuffer = fs.readFileSync(runtime[key][0]);
const hashSum = crypto.createHash('sha1');
const ext = runtime[key][0]
.split('/')
.pop()
.split('.')
.pop();
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
runtime[key].push(`_${hex}.${ext}`);
setTimeout(() => {
sh.exec(`cp ${runtime[key][0]} public/${runtime[key][1]}`);
}, delay);
}
});
}
/* eslint-disable */
const script = (process.env.PM_TECH
&& `
${pmTech}
if (typeof window.pm.scalp !== 'function') {
window.pm.setScalp({
property: 'api-gateways'
});
}
window.pm.driveCampaignId();
function isPmTechAllowed(documentLocationPathname) {
return ${allowedPmTech[0] === '*' ||
allowedPmTech.indexOf(documentLocationPathname) !== -1};
}
var d = 1000, int;
var int = setInterval(function(){
if (document.body) {
window.pm.scalp(
'pm-analytics',
'load',
document.location.pathname
);
window.pm.trackClicks();
clearInterval(int);
}
}, d);
`)
|| `
console.info('Postman OSS');
`;
/* eslint-enable */
fs.writeFile('bff.json', JSON.stringify({ script }), (err) => {
if (err) {
throw err;
}
});
};
prefetch();