-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathbuild.js
89 lines (74 loc) · 2.62 KB
/
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
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const jsonPath = require('jsonpath')
const webpack = require('webpack')
const webpackConfig = require('./webpack.config')
build()
async function build() {
const blacklist = await initializeImageSafety()
console.log('Invoking Webpack...')
webpack(webpackConfig({ blacklist }, { mode: 'production' }), (err, stats) => {
if (err) {
return console.error(err);
}
console.log(stats.toString({ chunks: false, colors: true }))
})
}
async function initializeImageSafety () {
if (!process.env.IMAGE_SAFETY_BLACKLIST_URL) {
console.warn('Skip remote fetching of the image blacklist because IMAGE_SAFETY_BLACKLIST_URL is not defined')
return []
}
console.log('Fetching image blacklist...')
try {
const blacklist = await fetchBlacklist(
process.env.IMAGE_SAFETY_BLACKLIST_URL,
process.env.IMAGE_SAFETY_BLACKLIST_HEADERS,
process.env.IMAGE_SAFETY_BLACKLIST_JSON_PATH
)
if (blacklist.length) {
console.log('Image blacklist fetched successfully:', ...blacklist.map(src => '\n\t' + src))
} else {
console.warn('The retrieved image blacklist is empty')
}
return blacklist
} catch (err) {
console.error('Error fetching the image blacklist: ', err)
}
return []
}
function fetchBlacklist (url, headersText, imgsPath = '*') {
const protocol = url.startsWith('http://') ? 'http' : 'https'
const headers = !headersText ? {} : Object.fromEntries(
headersText.split('\n').flatMap((header) => {
const [, key, value] = header.match(/(\w[^\s:]+)\s*:\s*(.+)/) ?? []
return (!key || !value) ? [] : [[key, value]]
})
)
return new Promise((resolve, reject) => {
require(protocol).get(url, { headers }, (res) => {
const resolveIf200 = (resolved) => {
if (res.statusCode !== 200) {
return reject(new Error(`Request Failed. Status ${res.statusCode}: ${res.statusMessage}`))
}
return resolve(resolved)
}
let text = ''
res
.on('error', reject)
.on('data', (chunk) => text += chunk)
.on('end', () => {
if (!res.headers['content-type'].includes('application/json')) {
return resolveIf200(text.split('\n'))
}
const data = JSON.parse(text)
if ('error' in data) {
return reject(data.error)
}
const path = imgsPath.replace(/^(?!\$)/, '$..')
const onlyStrings = (item) => item && typeof item === 'string'
return resolveIf200(jsonPath.query(data, path).filter(onlyStrings))
})
})
})
}