jetpack-id
Advanced tools
Comparing version 0.0.3 to 0.0.4
34
index.js
@@ -7,10 +7,17 @@ /** | ||
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 (isGUID(manifest.id) || isDomain(manifest.id)) | ||
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, | ||
@@ -20,6 +27,12 @@ // as we transition to `name` being an id, similar to node/npm, but | ||
if (manifest.name) { | ||
return "@" + 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 | ||
// If no `id` or `name` property, return null as this manifest | ||
// is invalid | ||
@@ -31,8 +44,9 @@ return null; | ||
function isGUID (s) { | ||
return /^\{[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\}$/i.test(s); | ||
/** | ||
* 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 || ""); | ||
} | ||
function isDomain (s) { | ||
return /^[0-9a-zA-Z\-_]*\@[0-9a-zA-Z\-]+(\.[0-9a-zA-Z\-]+)*$/.test(s); | ||
} |
{ | ||
"name": "jetpack-id", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Creates an ID from a Firefox Jetpack manifest", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,5 +11,9 @@ jetpack-id [![Build Status](https://travis-ci.org/jsantell/jetpack-id.png)](https://travis-ci.org/jsantell/jetpack-id) | ||
getID({ id: "jid1-JtUwP0fsy08AKw" }); // "jid1-JtUwP0fsy08AKw@jetpack" | ||
getID({ name: "my-addon" }); // "@my-addon" | ||
getID({ id: "tab-fixer@addon" }); // "tab-fixer@addon" | ||
// Manifest's generated by cfx generate an ID that is invalid -- this | ||
// should be manually changed to be compatable with AMO by appending | ||
// a `@jetpack` at the end, and this module does NOT do that. | ||
getID({ id: "jid1-JtUwP0fsy08AKw" }); // null | ||
``` | ||
@@ -16,0 +20,0 @@ |
@@ -6,2 +6,3 @@ var getID = require("../"); | ||
describe("valid", function () { | ||
it("Returns GUID when `id` GUID", function () { | ||
@@ -11,2 +12,3 @@ var guid = "{8490ae4f-93bc-13af-80b3-39adf9e7b243}"; | ||
}); | ||
it("Returns domain id when `id` domain id", function () { | ||
@@ -16,2 +18,3 @@ var id = "my-addon@jetpack"; | ||
}); | ||
it("allows underscores in name", function () { | ||
@@ -21,2 +24,3 @@ var name = "my_addon"; | ||
}); | ||
it("allows underscores in id", function () { | ||
@@ -26,2 +30,3 @@ var id = "my_addon@jetpack"; | ||
}); | ||
it("Returns valid name when `name` exists", function () { | ||
@@ -37,5 +42,7 @@ var id = "my-addon"; | ||
}); | ||
it("Returns null when no object passed in", function () { | ||
expect(getID()).to.be.equal(null); | ||
}); | ||
it("Returns null when `id` exists but not GUID/domain", function () { | ||
@@ -45,3 +52,20 @@ var id = "my-addon"; | ||
}); | ||
it("Returns null when `id` contains multiple @", function () { | ||
expect(getID({ id: "my@addon@yeah" })).to.be.equal(null); | ||
}); | ||
it("Returns null when `id` or `name` specified in domain format but has invalid characters", function () { | ||
[" ", "!", "/", "$", " ", "~", "("].forEach(function (sym) { | ||
expect(getID({ id: "my" + sym + "addon@domain" })).to.be.equal(null); | ||
expect(getID({ name: "my" + sym + "addon" })).to.be.equal(null); | ||
}); | ||
}); | ||
it("Returns null, does not crash, when providing non-string properties for `name` and `id`", function () { | ||
expect(getID({ id: 5 })).to.be.equal(null); | ||
expect(getID({ name: 5 })).to.be.equal(null); | ||
expect(getID({ name: {} })).to.be.equal(null); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6106
92
27