summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/jetpack-id
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
commit37d5300335d81cecbecc99812747a657588c63eb (patch)
tree765efa3b6a56bb715d9813a8697473e120436278 /toolkit/jetpack/jetpack-id
parentb2bdac20c02b12f2057b9ef70b0a946113a00e00 (diff)
parent4fb11cd5966461bccc3ed1599b808237be6b0de9 (diff)
downloadUXP-37d5300335d81cecbecc99812747a657588c63eb.tar
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.gz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.lz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.xz
UXP-37d5300335d81cecbecc99812747a657588c63eb.zip
Merge branch 'ext-work'
Diffstat (limited to 'toolkit/jetpack/jetpack-id')
-rw-r--r--toolkit/jetpack/jetpack-id/index.js53
-rw-r--r--toolkit/jetpack/jetpack-id/package.json28
2 files changed, 81 insertions, 0 deletions
diff --git a/toolkit/jetpack/jetpack-id/index.js b/toolkit/jetpack/jetpack-id/index.js
new file mode 100644
index 000000000..6c1493f1d
--- /dev/null
+++ b/toolkit/jetpack/jetpack-id/index.js
@@ -0,0 +1,53 @@
+/* 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/. */
+
+/**
+ * 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 || "");
+}
diff --git a/toolkit/jetpack/jetpack-id/package.json b/toolkit/jetpack/jetpack-id/package.json
new file mode 100644
index 000000000..62a1c73ba
--- /dev/null
+++ b/toolkit/jetpack/jetpack-id/package.json
@@ -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": "*"
+ }
+}