UNPKG

5.2 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/** @fileoverview Utilities for working with Firefox extensions. */
19
20'use strict';
21
22const AdmZip = require('adm-zip'),
23 fs = require('fs'),
24 path = require('path'),
25 xml = require('xml2js');
26
27const io = require('../io');
28
29
30/**
31 * Thrown when there an add-on is malformed.
32 */
33class AddonFormatError extends Error {
34 /** @param {string} msg The error message. */
35 constructor(msg) {
36 super(msg);
37 /** @override */
38 this.name = this.constructor.name;
39 }
40}
41
42
43
44/**
45 * Installs an extension to the given directory.
46 * @param {string} extension Path to the extension to install, as either a xpi
47 * file or a directory.
48 * @param {string} dir Path to the directory to install the extension in.
49 * @return {!Promise<string>} A promise for the add-on ID once
50 * installed.
51 */
52function install(extension, dir) {
53 return getDetails(extension).then(function(details) {
54 var dst = path.join(dir, details.id);
55 if (extension.slice(-4) === '.xpi') {
56 if (!details.unpack) {
57 return io.copy(extension, dst + '.xpi').then(() => details.id);
58 } else {
59 return Promise.resolve().then(function() {
60 // TODO: find an async library for inflating a zip archive.
61 new AdmZip(extension).extractAllTo(dst, true);
62 return details.id;
63 });
64 }
65 } else {
66 return io.copyDir(extension, dst).then(() => details.id);
67 }
68 });
69}
70
71
72/**
73 * Describes a Firefox add-on.
74 * @typedef {{id: string, name: string, version: string, unpack: boolean}}
75 */
76var AddonDetails;
77
78/** @typedef {{$: !Object<string, string>}} */
79var RdfRoot;
80
81
82
83/**
84 * Extracts the details needed to install an add-on.
85 * @param {string} addonPath Path to the extension directory.
86 * @return {!Promise<!AddonDetails>} A promise for the add-on details.
87 */
88function getDetails(addonPath) {
89 return readManifest(addonPath).then(function(doc) {
90 var em = getNamespaceId(doc, 'http://www.mozilla.org/2004/em-rdf#');
91 var rdf = getNamespaceId(
92 doc, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
93
94 var description = doc[rdf + 'RDF'][rdf + 'Description'][0];
95 var details = {
96 id: getNodeText(description, em + 'id'),
97 name: getNodeText(description, em + 'name'),
98 version: getNodeText(description, em + 'version'),
99 unpack: getNodeText(description, em + 'unpack') || false
100 };
101
102 if (typeof details.unpack === 'string') {
103 details.unpack = details.unpack.toLowerCase() === 'true';
104 }
105
106 if (!details.id) {
107 throw new AddonFormatError('Could not find add-on ID for ' + addonPath);
108 }
109
110 return details;
111 });
112
113 function getNodeText(node, name) {
114 return node[name] && node[name][0] || '';
115 }
116
117 function getNamespaceId(doc, url) {
118 var keys = Object.keys(doc);
119 if (keys.length !== 1) {
120 throw new AddonFormatError('Malformed manifest for add-on ' + addonPath);
121 }
122
123 var namespaces = /** @type {!RdfRoot} */(doc[keys[0]]).$;
124 var id = '';
125 Object.keys(namespaces).some(function(ns) {
126 if (namespaces[ns] !== url) {
127 return false;
128 }
129
130 if (ns.indexOf(':') != -1) {
131 id = ns.split(':')[1] + ':';
132 }
133 return true;
134 });
135 return id;
136 }
137}
138
139
140/**
141 * Reads the manifest for a Firefox add-on.
142 * @param {string} addonPath Path to a Firefox add-on as a xpi or an extension.
143 * @return {!Promise<!Object>} A promise for the parsed manifest.
144 */
145function readManifest(addonPath) {
146 var manifest;
147
148 if (addonPath.slice(-4) === '.xpi') {
149 manifest = new Promise((resolve, reject) => {
150 let zip = new AdmZip(addonPath);
151
152 if (!zip.getEntry('install.rdf')) {
153 reject(new AddonFormatError(
154 'Could not find install.rdf in ' + addonPath));
155 return;
156 }
157
158 zip.readAsTextAsync('install.rdf', resolve);
159 });
160 } else {
161 manifest = io.stat(addonPath).then(function(stats) {
162 if (!stats.isDirectory()) {
163 throw Error(
164 'Add-on path is niether a xpi nor a directory: ' + addonPath);
165 }
166 return io.read(path.join(addonPath, 'install.rdf'));
167 });
168 }
169
170 return manifest.then(function(content) {
171 return new Promise((resolve, reject) => {
172 xml.parseString(content, (err, data) => {
173 if (err) {
174 reject(err);
175 } else {
176 resolve(data);
177 }
178 });
179 });
180 });
181}
182
183
184// PUBLIC API
185
186
187exports.install = install;