-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathpackageAdBlock.js
More file actions
133 lines (118 loc) · 4.83 KB
/
packageAdBlock.js
File metadata and controls
133 lines (118 loc) · 4.83 KB
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
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
// Example usage:
// npm run package-ad-block -- --binary "/Applications/Google\\ Chrome\\ Canary.app/Contents/MacOS/Google\\ Chrome\\ Canary" --key-file path/to/ad-block-updater-regional-component-keys
import commander from 'commander'
import fs from 'fs-extra'
import path from 'path'
import util from '../lib/util.js'
import { getListCatalog, regionalCatalogComponentId, resourcesComponentId } from '../lib/adBlockRustUtils.js'
async function stageFiles (version, outputDir) {
// ad-block components are already written in the output directory
// so we don't need to stage anything
const originalManifest = path.join(outputDir, 'manifest.json')
// note - in-place manifest replacement, unlike other components
util.copyManifestWithVersion(originalManifest, outputDir, version)
}
const postNextVersionWork = (componentSubdir, key, publisherProofKey,
binary, localRun, version, contentHash) => {
const stagingDir = path.join('build', 'ad-block-updater', componentSubdir)
const crxOutputDir = path.join('build', 'ad-block-updater')
const crxFile = path.join(crxOutputDir, `ad-block-updater-${componentSubdir}.crx`)
const contentHashFile = path.join(crxOutputDir, `ad-block-updater-${componentSubdir}.contentHash`)
stageFiles(version, stagingDir).then(() => {
// Remove any existing `.contentHash` file for determinism
if (fs.existsSync(contentHashFile)) {
fs.unlinkSync(contentHashFile)
}
if (!localRun) {
const privateKeyFile = path.join(key, `ad-block-updater-${componentSubdir}.pem`)
util.generateCRXFile(binary, crxFile, privateKeyFile, publisherProofKey,
stagingDir)
}
if (contentHash !== undefined) {
fs.writeFileSync(contentHashFile, contentHash)
}
console.log(`Generated ${crxFile} with version number ${version}`)
})
}
const getOriginalManifest = (componentSubdir) => {
const manifestsDir = path.join('build', 'ad-block-updater')
return path.join(manifestsDir, componentSubdir, 'manifest.json')
}
const processComponent = (binary, endpoint, region, keyDir,
publisherProofKey, localRun, componentSubdir) => {
const originalManifest = getOriginalManifest(componentSubdir)
const parsedManifest = util.parseManifest(originalManifest)
const id = util.getIDFromBase64PublicKey(parsedManifest.key)
// TODO - previous download failures should prevent the attempt to package the component.
if (!fs.existsSync(originalManifest)) {
console.warn(`Missing manifest for ${componentSubdir}. Skipping.`)
return
}
let fileToHash
if (componentSubdir === regionalCatalogComponentId) {
fileToHash = 'regional_catalog.json'
} else if (componentSubdir === resourcesComponentId) {
fileToHash = 'resources.json'
} else {
fileToHash = 'list.txt'
}
let contentHash
if (fileToHash !== undefined) {
const contentFile = path.join('build', 'ad-block-updater', componentSubdir, fileToHash)
contentHash = util.generateSHA256HashOfFile(contentFile)
}
if (!localRun) {
util.getNextVersion(endpoint, region, id, contentHash).then((version) => {
if (version !== undefined) {
postNextVersionWork(componentSubdir, keyDir, publisherProofKey,
binary, localRun, version, contentHash)
} else {
console.log('content for ' + id + ' was not updated, skipping!')
}
})
} else {
postNextVersionWork(componentSubdir, undefined, publisherProofKey,
binary, localRun, '1.0.0', contentHash)
}
}
const getComponentList = async () => {
const output = [
regionalCatalogComponentId,
resourcesComponentId
]
const catalog = await getListCatalog()
catalog.forEach(entry => {
output.push(entry.list_text_component.component_id)
})
return output
}
const processJob = async (commander, keyDir) => {
(await getComponentList())
.forEach(processComponent.bind(null, commander.binary, commander.endpoint,
commander.region, keyDir,
commander.publisherProofKey,
commander.localRun))
}
util.installErrorHandlers()
util.addCommonScriptOptions(
commander
.option('-d, --keys-directory <dir>', 'directory containing private keys for signing crx files')
.option('-l, --local-run', 'Runs updater job without connecting anywhere remotely'))
.parse(process.argv)
if (!commander.localRun) {
let keyDir = ''
if (fs.existsSync(commander.keysDirectory)) {
keyDir = commander.keysDirectory
} else {
throw new Error('Missing or invalid private key file/directory')
}
util.createTableIfNotExists(commander.endpoint, commander.region).then(async () => {
await processJob(commander, keyDir)
})
} else {
processJob(commander, undefined)
}