-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.js
242 lines (205 loc) · 6.36 KB
/
index.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"use strict";
var path = require("path");
var join = path.join;
var crypto = require("crypto");
var RSA = require("node-rsa");
var archiver = require("archiver");
var resolve = require("./resolver.js");
var crx2 = require("./crx2.js");
var crx3 = require("./crx3.js");
const DEFAULTS = {
appId: null,
rootDirectory: "",
publicKey: null,
privateKey: null,
codebase: null,
path: null,
src: "**",
version: 3,
};
class ChromeExtension {
constructor(attrs) {
// Setup defaults
Object.assign(this, DEFAULTS, attrs);
this.loaded = false;
}
/**
* Packs the content of the extension in a crx file.
*
* @param {Buffer=} contentsBuffer
* @returns {Promise}
* @example
*
* crx.pack().then(function(crxContent){
* // do something with the crxContent binary data
* });
*
*/
pack (contentsBuffer) {
if (!this.loaded) {
return this.load().then(this.pack.bind(this, contentsBuffer));
}
var selfie = this;
var packP = [
this.generatePublicKey(),
contentsBuffer || selfie.loadContents()
];
return Promise.all(packP).then(function(outputs) {
var publicKey = outputs[0];
var contents = outputs[1];
selfie.publicKey = publicKey;
if (selfie.version === 2) {
return crx2(selfie.privateKey, publicKey, contents);
}
return crx3(selfie.privateKey, publicKey, contents);
});
}
/**
* Loads extension manifest and copies its content to a workable path.
*
* @param {string=} path
* @returns {Promise}
*/
load (path) {
var selfie = this;
return resolve(path || selfie.rootDirectory).then(function(metadata) {
selfie.path = metadata.path;
selfie.src = metadata.src;
var manifestPath = join(selfie.path, "manifest.json");
delete require.cache[manifestPath];
selfie.manifest = require(manifestPath);
selfie.loaded = true;
return selfie;
});
}
/**
* Generates a public key.
*
* BC BREAK `this.publicKey` is not stored anymore (since 1.0.0)
* BC BREAK callback parameter has been removed in favor to the promise interface.
*
* @returns {Promise} Resolves to {Buffer} containing the public key
* @example
*
* crx.generatePublicKey(function(publicKey){
* // do something with publicKey
* });
*/
generatePublicKey () {
var privateKey = this.privateKey;
return new Promise(function(resolve, reject) {
if (!privateKey) {
return reject(
"Impossible to generate a public key: privateKey option has not been defined or is empty."
);
}
var key = new RSA(privateKey);
resolve(key.exportKey("pkcs8-public-der"));
});
}
/**
*
* BC BREAK `this.contents` is not stored anymore (since 1.0.0)
*
* @returns {Promise}
*/
loadContents () {
var selfie = this;
return new Promise(function(resolve, reject) {
var archive = archiver("zip", { zlib: { level: 9 }});
var contents = Buffer.from("");
if (!selfie.loaded) {
throw new Error(
"crx.load needs to be called first in order to prepare the workspace."
);
}
archive.on("error", reject);
/*
TODO: Remove in v4.
It will be better to resolve an archive object
rather than fitting everything in memory.
@see https://github.com/oncletom/crx/issues/61
*/
archive.on("data", function(buf) {
contents = Buffer.concat([contents, buf]);
});
archive.on("finish", function() {
resolve(contents);
});
archive
.glob(selfie.src, {
cwd: selfie.path,
matchBase: true,
ignore: ["*.pem", ".git", "*.crx"]
})
.finalize();
});
}
/**
* Generates an appId from the publicKey.
* Public key has to be set for this to work, otherwise an error is thrown.
*
* BC BREAK `this.appId` is not stored anymore (since 1.0.0)
* BC BREAK introduced `publicKey` parameter as it is not stored any more since 2.0.0
*
* @param {Buffer|string} [publicKey] the public key to use to generate the app ID
* @returns {string}
*/
generateAppId (keyOrPath) {
keyOrPath = keyOrPath || this.publicKey;
if (typeof keyOrPath !== "string" && !(keyOrPath instanceof Buffer)) {
throw new Error("Public key is neither set, nor given");
}
// Handling Windows Path
// Possibly to be moved in a different method
if (typeof keyOrPath === "string") {
var charCode = keyOrPath.charCodeAt(0);
// 65 (A) < charCode < 122 (z)
if (charCode >= 65 && charCode <= 122 && keyOrPath[1] === ":") {
keyOrPath = keyOrPath[0].toUpperCase() + keyOrPath.slice(1);
keyOrPath = Buffer.from(keyOrPath, "utf-16le");
}
}
return crypto
.createHash("sha256")
.update(keyOrPath)
.digest()
.toString("hex")
.split("")
.map(x => (parseInt(x, 16) + 0x0a).toString(26))
.join("")
.slice(0, 32);
}
/**
* Generates an updateXML file from the extension content.
*
* If manifest does not include `minimum_chrome_version`, defaults to:
* - '29.0.0' for CRX2, which is earliest extensions API available
* - '64.0.3242' for CRX3, which is when Chrome etension packager switched to CRX3
*
* BC BREAK `this.updateXML` is not stored anymore (since 1.0.0)
*
* @see
* [Chrome Extensions APIs]{@link https://developer.chrome.com/extensions/api_index}
* @see
* [Chrome verions]{@link https://en.wikipedia.org/wiki/Google_Chrome_version_history}
* @see
* [Chromium switches to CRX3]{@link https://chromium.googlesource.com/chromium/src.git/+/b8bc9f99ef4ad6223dfdcafd924051561c05ac75}
* @returns {Buffer}
*/
generateUpdateXML () {
if (!this.codebase) {
throw new Error("No URL provided for update.xml.");
}
var browserVersion = this.manifest.minimum_chrome_version
|| (this.version < 3 && "29.0.0") // Earliest version with extensions API
|| "64.0.3242"; // Chrome started generating CRX3 packages
return Buffer.from(`<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='${this.appId || this.generateAppId()}'>
<updatecheck codebase='${this.codebase}' version='${this.manifest.version}' prodversionmin='${browserVersion}' />
</app>
</gupdate>`);
}
}
module.exports = ChromeExtension;