forked from AdguardTeam/Scriptlets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirects.build.js
215 lines (188 loc) · 6.99 KB
/
redirects.build.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import sha256 from 'crypto-js/sha256';
import Base64 from 'crypto-js/enc-base64';
import yaml from 'js-yaml';
import * as redirectsList from './src/redirects/redirects-list';
import { version } from './package.json';
const fs = require('fs');
const path = require('path');
const { EOL } = require('os');
const { redirectsFilesList, getDataFromFiles } = require('./scripts/build-docs');
// define global variable redirects
// because require('./tmp/tmpRedirects') trying to put redirects code in it
global.redirects = {};
// eslint-disable-next-line import/no-unresolved
require('./tmp/tmpRedirects');
const FILE_NAME = 'redirects.yml';
const CORELIBS_FILE_NAME = 'redirects.json';
const PATH_TO_DIST = './dist';
const RESULT_PATH = path.resolve(PATH_TO_DIST, FILE_NAME);
const REDIRECT_FILES_PATH = path.resolve(PATH_TO_DIST, 'redirect-files');
const CORELIBS_RESULT_PATH = path.resolve(PATH_TO_DIST, CORELIBS_FILE_NAME);
const REDIRECTS_DIRECTORY = '../src/redirects';
const STATIC_REDIRECTS_PATH = './src/redirects/static-redirects.yml';
const BLOCKING_REDIRECTS_PATH = './src/redirects/blocking-redirects.yml';
const banner = `#
# AdGuard Scriptlets (Redirects Source)
# Version ${version}
#
`;
let staticRedirects;
try {
staticRedirects = yaml.safeLoad(fs.readFileSync(STATIC_REDIRECTS_PATH, 'utf8'));
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Unable to load yaml because of: ${e}`);
throw e;
}
let blockingRedirects;
try {
const rawBlockingRedirects = yaml.safeLoad(fs.readFileSync(BLOCKING_REDIRECTS_PATH, 'utf8'));
blockingRedirects = rawBlockingRedirects.map((raw) => {
// get bundled html file as content for redirect
const content = fs.readFileSync(path.resolve(REDIRECT_FILES_PATH, raw.title), 'utf8');
// get babelized script content
const scriptPath = path.resolve(REDIRECT_FILES_PATH, raw.scriptPath);
const scriptContent = fs.readFileSync(scriptPath, 'utf8');
// needed for CSP in browser extension
const sha = `sha256-${Base64.stringify(sha256(scriptContent))}`;
// remove not needed dist script file
fs.unlinkSync(scriptPath);
return {
...raw,
isBlocking: true,
content,
sha,
};
});
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Unable to load yaml because of: ${e}`);
throw e;
}
const redirectsObject = Object
.values(redirectsList)
.map((rr) => {
const [name, ...aliases] = rr.names;
const source = { name, args: [] };
const redirect = global.redirects.getCode(source);
return { name, redirect, aliases };
});
const redirectsDescriptions = getDataFromFiles(redirectsFilesList, REDIRECTS_DIRECTORY).flat(1);
/**
* Returns first line of describing comment from redirect resource file
* @param {string} rrName redirect resource name
*/
const getComment = (rrName) => {
const { description } = redirectsDescriptions.find((rr) => rr.name === rrName);
const descArr = description.split('\n');
return descArr.find((str) => str !== '');
};
/**
* Copies non-static redirects sources to dist
*
* @param redirectsData
*/
const prepareJsRedirectFiles = (redirectsData) => {
Object.values(redirectsData).forEach((redirect) => {
const redirectPath = `${REDIRECT_FILES_PATH}/${redirect.file}`;
fs.writeFileSync(redirectPath, redirect.content, 'utf8');
});
};
/**
* Prepares static redirects sources to dist
*
* @param redirectsData
*/
const prepareStaticRedirectFiles = (redirectsData) => {
Object.values(redirectsData).forEach((redirect) => {
const {
contentType, content, file,
} = redirect;
const redirectPath = `${REDIRECT_FILES_PATH}/${file}`;
let contentToWrite = content;
if (contentType.match(';base64')) {
// yaml leaves new lines or spaces
// replace them all because base64 isn't supposed to have them
contentToWrite = content.replace(/(\r\n|\n|\r|\s)/gm, '');
const buff = Buffer.from(contentToWrite, 'base64');
fs.writeFileSync(redirectPath, buff);
} else {
fs.writeFileSync(redirectPath, contentToWrite, 'utf8');
}
});
};
const jsRedirects = redirectsFilesList.map((fileName) => {
const rrName = fileName.replace(/\.js/, '');
const complement = redirectsObject.find((obj) => obj.name === rrName);
const comment = getComment(rrName);
if (complement) {
return {
title: rrName,
comment,
aliases: complement.aliases,
contentType: 'application/javascript',
content: complement.redirect,
file: fileName,
};
}
throw new Error(`Couldn't find source for non-static redirect: ${fileName}`);
});
const mergedRedirects = [
...staticRedirects,
...blockingRedirects,
...jsRedirects,
];
if (process.env.REDIRECTS !== 'CORELIBS') {
try {
let yamlRedirects = yaml.safeDump(mergedRedirects);
// add empty line before titles
yamlRedirects = yamlRedirects.split('- title:').join(`${EOL}- title:`).trimLeft();
// add version and title to the top
yamlRedirects = `${banner}${yamlRedirects}`;
fs.writeFileSync(RESULT_PATH, yamlRedirects, 'utf8');
// redirect files
if (!fs.existsSync(REDIRECT_FILES_PATH)) {
fs.mkdirSync(REDIRECT_FILES_PATH);
}
prepareStaticRedirectFiles(staticRedirects);
prepareJsRedirectFiles(jsRedirects);
// blocking redirects have already been bundled to dist by rollup
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Couldn't save to ${RESULT_PATH}, because of: ${e.message}`);
throw e;
}
}
if (process.env.REDIRECTS === 'CORELIBS') {
// Build scriptlets.json. It is used in the corelibs
const base64Redirects = Object.values(mergedRedirects).map((redirect) => {
const {
contentType, content, title, aliases, isBlocking = false,
} = redirect;
let base64Content;
let bas64ContentType = contentType;
if (contentType.match(';base64')) {
// yaml leaves new lines or spaces
// replace them all because base64 isn't supposed to have them
base64Content = content.replace(/(\r\n|\n|\r|\s)/gm, '');
} else {
base64Content = Buffer.from(content, 'binary').toString('base64');
bas64ContentType = `${contentType};base64`;
}
return {
title,
aliases,
isBlocking,
contentType: bas64ContentType,
content: base64Content.trim(),
};
});
try {
const jsonString = JSON.stringify(base64Redirects, null, 4);
fs.writeFileSync(CORELIBS_RESULT_PATH, jsonString, 'utf8');
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Couldn't save to ${CORELIBS_RESULT_PATH}, because of: ${e.message}`);
throw e;
}
}