Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extension ID calculation from path #81

Merged
merged 4 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/crx.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ChromeExtension.prototype = {
.then(function(metadata){
selfie.path = metadata.path;
selfie.src = metadata.src;

var manifestPath = join(selfie.path, 'manifest.json')
delete require.cache[manifestPath];

Expand Down Expand Up @@ -256,19 +256,39 @@ ChromeExtension.prototype = {
* @param {Buffer|string} [publicKey] the public key to use to generate the app ID
* @returns {string}
*/
generateAppId: function (publicKey) {
publicKey = publicKey || this.publicKey;
if (typeof publicKey !== 'string' && !(publicKey instanceof Buffer)) {
generateAppId: function (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);

// TODO move to Buffer.from when drop old Node versions in crx@4
/* istanbul ignore next */
keyOrPath = ('from' in Buffer)
? Buffer.from(keyOrPath, "utf-16le")
: new Buffer(keyOrPath, "utf-16le");
}
}

return crypto
.createHash("sha256")
.update(publicKey)
.digest("hex")
.slice(0, 32)
.replace(/./g, function (x) {
return (parseInt(x, 16) + 10).toString(26);
});
.update(keyOrPath)
.digest()
.toString("hex")
.split("")
.map(function(x) { return (parseInt(x, 16) + 0x0A).toString(26); })
.join("")
.slice(0, 32);
},

/**
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,23 @@ test('#generatePublicKey', function(t) {
});

test('#generateAppId', function(t) {
t.plan(2);
t.plan(4);

t.throws(function() { newCrx().generateAppId(); }, /Public key is neither set, nor given/);

var crx = newCrx()

// from Public Key
crx.generatePublicKey().then(function(publicKey){
t.equals(crx.generateAppId(publicKey), 'eoilidhiokfphdhpmhoaengdkehanjif');
})
.catch(t.error.bind(t));

// from Linux Path
t.equals(crx.generateAppId('/usr/local/extension'), 'ioglhmppkolgcgoonkfdbjkcedfjhbcd');

// from Windows Path
t.equals(crx.generateAppId('c:\\a'), 'igchicfaapedlfgmepccnpolhajaphik');
});

test('end to end', function (t) {
Expand Down