This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1931 from Gozala/jetpack-id
Bug 1152126 - Add jetpack-id package. r=@erikvold
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Takes parsed `package.json` manifest and returns | ||
* valid add-on id for it. | ||
*/ | ||
function getID(manifest) { | ||
manifest = manifest || {}; | ||
|
||
if (manifest.id) { | ||
|
||
if (typeof manifest.id !== "string") { | ||
return null; | ||
} | ||
|
||
// If manifest.id is already valid (as domain or GUID), use it | ||
if (isValidAOMName(manifest.id)) { | ||
return manifest.id; | ||
} | ||
// Otherwise, this ID is invalid so return `null` | ||
return null; | ||
} | ||
|
||
// If no `id` defined, turn `name` into a domain ID, | ||
// as we transition to `name` being an id, similar to node/npm, but | ||
// append a '@' to make it compatible with Firefox requirements | ||
if (manifest.name) { | ||
|
||
if (typeof manifest.name !== "string") { | ||
return null; | ||
} | ||
|
||
var modifiedName = "@" + manifest.name; | ||
return isValidAOMName(modifiedName) ? modifiedName : null; | ||
} | ||
|
||
// If no `id` or `name` property, return null as this manifest | ||
// is invalid | ||
return null; | ||
} | ||
|
||
module.exports = getID; | ||
|
||
/** | ||
* Regex taken from XPIProvider.jsm in the Addon Manager to validate proper | ||
* IDs that are able to be used. | ||
* http://mxr.mozilla.org/mozilla-central/source/toolkit/mozapps/extensions/internal/XPIProvider.jsm#209 | ||
*/ | ||
function isValidAOMName (s) { | ||
return /^(\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}|[a-z0-9-\._]*\@[a-z0-9-\._]+)$/i.test(s || ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "jetpack-id", | ||
"version": "1.0.0", | ||
"description": "Creates an ID from a Firefox Jetpack manifest", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/jsantell/jetpack-id" | ||
}, | ||
"author": { | ||
"name": "Jordan Santell", | ||
"url": "http://github.com/jsantell" | ||
}, | ||
"license": "MPL 2.0", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --reporter spec --ui bdd" | ||
}, | ||
"keywords": [ | ||
"jetpack", | ||
"addon", | ||
"mozilla", | ||
"firefox" | ||
], | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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/. */ | ||
"use strict"; | ||
|
||
var getID = require("jetpack-id/index"); | ||
|
||
exports["test Returns GUID when `id` GUID"] = assert => { | ||
var guid = "{8490ae4f-93bc-13af-80b3-39adf9e7b243}"; | ||
assert.equal(getID({ id: guid }), guid); | ||
}; | ||
|
||
exports["test Returns domain id when `id` domain id"] = assert => { | ||
var id = "my-addon@jetpack"; | ||
assert.equal(getID({ id: id }), id); | ||
}; | ||
|
||
exports["test allows underscores in name"] = assert => { | ||
var name = "my_addon"; | ||
assert.equal(getID({ name: name }), `@${name}`); | ||
}; | ||
|
||
exports["test allows underscores in id"] = assert => { | ||
var id = "my_addon@jetpack"; | ||
assert.equal(getID({ id: id }), id); | ||
}; | ||
|
||
exports["test Returns valid name when `name` exists"] = assert => { | ||
var id = "my-addon"; | ||
assert.equal(getID({ name: id }), `@${id}`); | ||
}; | ||
|
||
|
||
exports["test Returns null when `id` and `name` do not exist"] = assert => { | ||
assert.equal(getID({}), null) | ||
} | ||
|
||
exports["test Returns null when no object passed in"] = assert => { | ||
assert.equal(getID(), null) | ||
} | ||
|
||
exports["test Returns null when `id` exists but not GUID/domain"] = assert => { | ||
var id = "my-addon"; | ||
assert.equal(getID({ id: id }), null); | ||
} | ||
|
||
exports["test Returns null when `id` contains multiple @"] = assert => { | ||
assert.equal(getID({ id: "my@addon@yeah" }), null); | ||
}; | ||
|
||
exports["test Returns null when `id` or `name` specified in domain format but has invalid characters"] = assert => { | ||
[" ", "!", "/", "$", " ", "~", "("].forEach(sym => { | ||
assert.equal(getID({ id: "my" + sym + "addon@domain" }), null); | ||
assert.equal(getID({ name: "my" + sym + "addon" }), null); | ||
}); | ||
}; | ||
|
||
exports["test Returns null, does not crash, when providing non-string properties for `name` and `id`"] = assert => { | ||
assert.equal(getID({ id: 5 }), null); | ||
assert.equal(getID({ name: 5 }), null); | ||
assert.equal(getID({ name: {} }), null); | ||
}; | ||
|
||
require("sdk/test").run(exports); |