forked from webiscool/postman-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbff.js
189 lines (158 loc) · 4.83 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const fs = require('fs');
const sh = require('shelljs');
const crypto = require('crypto');
const fetch = require('node-fetch');
const path = require("path");
const { allow } = require('./package.json');
const bffData = require('./build/bffData');
const { pmTech: allowedPmTech } = allow;
const delay = 1000;
const runtime = {
pm: [''],
};
const cacheCdn = (url, name) =>
new Promise((resolve) => {
sh.exec('mkdir -p public');
fetch(url).then((res) => {
res.text().then((resp) => {
if (resp) {
fs.writeFile(path.join('public', `${name}.js`), resp, (err) => {
if (err) {
throw err;
} else {
resolve(true);
}
});
}
});
});
});
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 bffData();
let pmt = '';
if (process.env.PM_TECH_RT) {
sh.config.silent = true;
pmt = sh.exec('cat bff-data/pmt.js').stdout.split('\n').shift();
sh.config.silent = false;
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);
}
});
}
const UACode = 'G-CX7P9K6W67';
const GCode = UACode;
const RampMetrics = `
var _rmq=_rmq||[],_rmq_domain="postman.com";
_rmq.push(["setAccount","asLtEBCh7D4pMetaFgSz","asLtEBCh7D4pMetaFgSz:LsgKvmHz1TMJTazA_M37"]);
_rmq.push(["trackPageview"]);_rmq.push(["trackSubmit"]);
(function(){var a=document.createElement("script");
a.type="text/javascript";a['src']="/rm.min.js";
var b=document.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)})();
`;
const script = (process.env.PM_TECH_RT
&& `
${RampMetrics}
${pmt}
setTimeout(function(){
var propertyName = 'postman-docs';
if (window.pmt) {
window.pmt('setScalp', [{
property: propertyName
}]);
window.pmt('scalp', [
'pm-analytics',
'load',
document.location.pathname
]);
window.pmt('trackClicks');
var dnt = (parseInt(navigator.doNotTrack) === 1 || parseInt(window.doNotTrack) === 1 || parseInt(navigator.msDoNotTrack) === 1 || navigator.doNotTrack === "yes");
window.pmt('log', ['navigator.doNotTrack: ' + dnt]);
if(!dnt) {
load('/_ga.js');
load('/_gtag.js');
var d = 1000, int;
var int = setInterval(function(){
if (window.ga) {
window.pmt('set', ['ga', function(){
if (typeof window.ga === 'function') {
window.ga.apply(this, arguments);
}
return window.ga;
}]);
var sitename = document.location.hostname;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
window.gtag = gtag;
gtag('js', new Date());
gtag('config', '${GCode}'); // UACode & GCode are the same
window.pmt('log', ['[gtag] config: ${GCode}']);
window.pmt('ga', ['${UACode}', sitename]);
window.pmt('log', ['initialized GA: ' + sitename + ' (' + '${UACode}' + ')']);
window._iaq = window._iaq || {};
clearInterval(int);
}
}, d);
}
}
}, 1000);
function load(src, cb) {
var e = document.createElement('script');
e.src = src;
e.async = true;
e.onreadystatechange = function(){
if (this.readyState === 'complete' || this.readyState === 'loaded') {
if (typeof cb === 'function') {
cb();
}
}
};
e.onload = cb;
document.head.appendChild(e);
}
`)
|| `
console.info('Postman OSS');
`;
fs.writeFile('bff.json', JSON.stringify({ script }), async (err) => {
if (err) {
throw err;
}
await cacheCdn('https://www.google-analytics.com/analytics.js', '_ga');
await cacheCdn('https://www.googletagmanager.com/gtag/js', '_gtag');
sh.exec('cp node_modules/@rampmetrics/rm/rm.min.js public/');
});
};
prefetch();