selenium-webdriver 3.4.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +29 -1
- package/NOTICE +1 -1
- package/README.md +1 -4
- package/chrome.js +78 -36
- package/edge.js +2 -2
- package/example/chrome_headless.js +47 -0
- package/example/chrome_mobile_emulation.js +1 -1
- package/firefox/extension.js +118 -81
- package/firefox/index.js +10 -106
- package/firefox/profile.js +13 -49
- package/ie.js +2 -2
- package/index.js +4 -1
- package/io/index.js +59 -0
- package/io/zip.js +214 -0
- package/lib/atoms/getAttribute.js +8 -11
- package/lib/atoms/is-displayed.js +73 -78
- package/lib/capabilities.js +1 -1
- package/lib/error.js +13 -1
- package/lib/http.js +1 -1
- package/lib/test/data/firefox/webextension.xpi +0 -0
- package/lib/test/data/key_logger.html +34 -0
- package/lib/test/data/single_text_input.html +12 -0
- package/lib/test/index.js +1 -34
- package/lib/webdriver.js +3 -12
- package/opera.js +1 -1
- package/package.json +2 -2
- package/phantomjs.js +2 -2
- package/remote/index.js +11 -10
- package/safari.js +2 -2
- package/test/firefox/extension_test.js +41 -17
- package/test/firefox/firefox_test.js +44 -9
- package/test/firefox/profile_test.js +14 -59
- package/test/http/http_test.js +18 -0
- package/test/{io_test.js → io/io_test.js} +40 -1
- package/test/io/zip_test.js +128 -0
- package/test/lib/error_test.js +20 -1
- package/test/lib/webdriver_test.js +5 -3
- package/test/page_loading_test.js +0 -6
- package/test/remote_test.js +3 -3
- package/test/session_test.js +68 -23
- package/lib/firefox/webdriver.xpi +0 -0
package/firefox/index.js
CHANGED
|
@@ -114,16 +114,6 @@
|
|
|
114
114
|
* .setFirefoxOptions(options)
|
|
115
115
|
* .build();
|
|
116
116
|
*
|
|
117
|
-
* __Testing Older Versions of Firefox__
|
|
118
|
-
*
|
|
119
|
-
* To test versions of Firefox prior to Firefox 47, you must disable the use of
|
|
120
|
-
* the geckodriver using the {@link Options} class.
|
|
121
|
-
*
|
|
122
|
-
* let options = new firefox.Options().useGeckoDriver(false);
|
|
123
|
-
*
|
|
124
|
-
* Alternatively, you may disable the geckodriver at runtime by setting the
|
|
125
|
-
* environment variable `SELENIUM_MARIONETTE=false`.
|
|
126
|
-
*
|
|
127
117
|
* [geckodriver release]: https://github.com/mozilla/geckodriver/releases/
|
|
128
118
|
* [PATH]: http://en.wikipedia.org/wiki/PATH_%28variable%29
|
|
129
119
|
*/
|
|
@@ -163,8 +153,8 @@ const Capability = {
|
|
|
163
153
|
BINARY: 'firefox_binary',
|
|
164
154
|
|
|
165
155
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
156
|
+
* @deprecated This will be removed in a future release. There is no
|
|
157
|
+
* replacement.
|
|
168
158
|
*/
|
|
169
159
|
MARIONETTE: 'marionette',
|
|
170
160
|
|
|
@@ -193,9 +183,6 @@ class Options {
|
|
|
193
183
|
|
|
194
184
|
/** @private {?capabilities.ProxyConfig} */
|
|
195
185
|
this.proxy_ = null;
|
|
196
|
-
|
|
197
|
-
/** @private {boolean} */
|
|
198
|
-
this.marionette_ = true;
|
|
199
186
|
}
|
|
200
187
|
|
|
201
188
|
/**
|
|
@@ -259,14 +246,10 @@ class Options {
|
|
|
259
246
|
}
|
|
260
247
|
|
|
261
248
|
/**
|
|
262
|
-
*
|
|
263
|
-
* is
|
|
264
|
-
*
|
|
265
|
-
* @param {boolean} enable Whether to enable the geckodriver.
|
|
266
|
-
* @see https://github.com/mozilla/geckodriver
|
|
249
|
+
* @deprecated This method has been deprecated and will be removed in the next
|
|
250
|
+
* release. It is a no-op.
|
|
267
251
|
*/
|
|
268
252
|
useGeckoDriver(enable) {
|
|
269
|
-
this.marionette_ = enable;
|
|
270
253
|
return this;
|
|
271
254
|
}
|
|
272
255
|
|
|
@@ -289,7 +272,6 @@ class Options {
|
|
|
289
272
|
if (this.profile_) {
|
|
290
273
|
caps.set(Capability.PROFILE, this.profile_);
|
|
291
274
|
}
|
|
292
|
-
caps.set(Capability.MARIONETTE, this.marionette_);
|
|
293
275
|
return caps;
|
|
294
276
|
}
|
|
295
277
|
}
|
|
@@ -325,33 +307,13 @@ function findGeckoDriver() {
|
|
|
325
307
|
throw Error(
|
|
326
308
|
'The ' + GECKO_DRIVER_EXE + ' executable could not be found on the current ' +
|
|
327
309
|
'PATH. Please download the latest version from ' +
|
|
328
|
-
'https://github.com/mozilla/geckodriver/releases/' +
|
|
329
|
-
'
|
|
310
|
+
'https://github.com/mozilla/geckodriver/releases/ ' +
|
|
311
|
+
'and ensure it can be found on your PATH.');
|
|
330
312
|
}
|
|
331
313
|
return exe;
|
|
332
314
|
}
|
|
333
315
|
|
|
334
316
|
|
|
335
|
-
/**
|
|
336
|
-
* @param {(Profile|string)} profile The profile to prepare.
|
|
337
|
-
* @param {number} port The port the FirefoxDriver should listen on.
|
|
338
|
-
* @return {!Promise<string>} a promise for the path to the profile directory.
|
|
339
|
-
*/
|
|
340
|
-
function prepareProfile(profile, port) {
|
|
341
|
-
if (typeof profile === 'string') {
|
|
342
|
-
return decodeProfile(/** @type {string} */(profile)).then(dir => {
|
|
343
|
-
profile = new Profile(dir);
|
|
344
|
-
profile.setPreference('webdriver_firefox_port', port);
|
|
345
|
-
return profile.writeToDisk();
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
profile = profile || new Profile;
|
|
350
|
-
profile.setPreference('webdriver_firefox_port', port);
|
|
351
|
-
return profile.writeToDisk();
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
317
|
function normalizeProxyConfiguration(config) {
|
|
356
318
|
if ('manual' === config.proxyType) {
|
|
357
319
|
if (config.ftpProxy && !config.ftpProxyPort) {
|
|
@@ -484,7 +446,7 @@ var DriverSpec;
|
|
|
484
446
|
* @return {DriverSpec}
|
|
485
447
|
*/
|
|
486
448
|
function createGeckoDriver(executor, caps, profile, binary) {
|
|
487
|
-
let firefoxOptions = {};
|
|
449
|
+
let firefoxOptions = caps.get('moz:firefoxOptions') || {};
|
|
488
450
|
caps.set('moz:firefoxOptions', firefoxOptions);
|
|
489
451
|
|
|
490
452
|
if (binary) {
|
|
@@ -556,47 +518,6 @@ function createGeckoDriver(executor, caps, profile, binary) {
|
|
|
556
518
|
}
|
|
557
519
|
|
|
558
520
|
|
|
559
|
-
/**
|
|
560
|
-
* @param {!capabilities.Capabilities} caps
|
|
561
|
-
* @param {Profile} profile
|
|
562
|
-
* @param {!Binary} binary
|
|
563
|
-
* @return {DriverSpec}
|
|
564
|
-
*/
|
|
565
|
-
function createLegacyDriver(caps, profile, binary, flow) {
|
|
566
|
-
profile = profile || new Profile;
|
|
567
|
-
|
|
568
|
-
let freePort = portprober.findFreePort();
|
|
569
|
-
let preparedProfile =
|
|
570
|
-
freePort.then(port => prepareProfile(profile, port));
|
|
571
|
-
let command = preparedProfile.then(dir => binary.launch(dir));
|
|
572
|
-
|
|
573
|
-
let serverUrl = command.then(() => freePort)
|
|
574
|
-
.then(function(/** number */port) {
|
|
575
|
-
let serverUrl = url.format({
|
|
576
|
-
protocol: 'http',
|
|
577
|
-
hostname: net.getLoopbackAddress(),
|
|
578
|
-
port: port + '',
|
|
579
|
-
pathname: '/hub'
|
|
580
|
-
});
|
|
581
|
-
let ready = httpUtil.waitForServer(serverUrl, 45 * 1000);
|
|
582
|
-
return ready.then(() => serverUrl);
|
|
583
|
-
});
|
|
584
|
-
|
|
585
|
-
return {
|
|
586
|
-
executor: createExecutor(serverUrl),
|
|
587
|
-
capabilities: caps,
|
|
588
|
-
onQuit: function() {
|
|
589
|
-
return command.then(command => {
|
|
590
|
-
command.kill();
|
|
591
|
-
return preparedProfile.then(io.rmDir)
|
|
592
|
-
.then(() => command.result(),
|
|
593
|
-
() => command.result());
|
|
594
|
-
});
|
|
595
|
-
}
|
|
596
|
-
};
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
|
|
600
521
|
/**
|
|
601
522
|
* A WebDriver client for Firefox.
|
|
602
523
|
*/
|
|
@@ -646,26 +567,9 @@ class Driver extends webdriver.WebDriver {
|
|
|
646
567
|
caps.delete(Capability.PROFILE);
|
|
647
568
|
}
|
|
648
569
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
caps.get(Capability.MARIONETTE) === false
|
|
653
|
-
|| /^0|false$/i.test(process.env['SELENIUM_MARIONETTE']);
|
|
654
|
-
let useMarionette = !noMarionette;
|
|
655
|
-
|
|
656
|
-
let spec;
|
|
657
|
-
if (useMarionette) {
|
|
658
|
-
spec = createGeckoDriver(opt_executor, caps, profile, binary);
|
|
659
|
-
} else {
|
|
660
|
-
if (opt_executor) {
|
|
661
|
-
throw Error('You may not use a custom command executor with the legacy'
|
|
662
|
-
+ ' FirefoxDriver');
|
|
663
|
-
}
|
|
664
|
-
spec = createLegacyDriver(caps, profile, binary, opt_flow);
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
return /** @type {!Driver} */(webdriver.WebDriver.createSession(
|
|
668
|
-
spec.executor, spec.capabilities, opt_flow, this, spec.onQuit));
|
|
570
|
+
let spec = createGeckoDriver(opt_executor, caps, profile, binary);
|
|
571
|
+
return /** @type {!Driver} */(super.createSession(
|
|
572
|
+
spec.executor, spec.capabilities, opt_flow, spec.onQuit));
|
|
669
573
|
}
|
|
670
574
|
|
|
671
575
|
/**
|
package/firefox/profile.js
CHANGED
|
@@ -22,14 +22,14 @@
|
|
|
22
22
|
|
|
23
23
|
'use strict';
|
|
24
24
|
|
|
25
|
-
const
|
|
26
|
-
fs = require('fs'),
|
|
25
|
+
const fs = require('fs'),
|
|
27
26
|
path = require('path'),
|
|
28
27
|
vm = require('vm');
|
|
29
28
|
|
|
30
29
|
const isDevMode = require('../lib/devmode'),
|
|
31
30
|
Symbols = require('../lib/symbols'),
|
|
32
31
|
io = require('../io'),
|
|
32
|
+
{Zip, unzip} = require('../io/zip'),
|
|
33
33
|
extension = require('./extension');
|
|
34
34
|
|
|
35
35
|
|
|
@@ -38,17 +38,6 @@ const WEBDRIVER_PREFERENCES_PATH = isDevMode
|
|
|
38
38
|
? path.join(__dirname, '../../../firefox-driver/webdriver.json')
|
|
39
39
|
: path.join(__dirname, '../lib/firefox/webdriver.json');
|
|
40
40
|
|
|
41
|
-
/** @const */
|
|
42
|
-
const WEBDRIVER_EXTENSION_PATH = isDevMode
|
|
43
|
-
? path.join(__dirname,
|
|
44
|
-
'../../../../build/javascript/firefox-driver/webdriver.xpi')
|
|
45
|
-
: path.join(__dirname, '../lib/firefox/webdriver.xpi');
|
|
46
|
-
|
|
47
|
-
/** @const */
|
|
48
|
-
const WEBDRIVER_EXTENSION_NAME = 'fxdriver@googlecode.com';
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
41
|
/** @type {Object} */
|
|
53
42
|
var defaultPreferences = null;
|
|
54
43
|
|
|
@@ -130,13 +119,10 @@ function writeUserPrefs(prefs, dir) {
|
|
|
130
119
|
* @param {!Array.<string>} extensions The extensions to install, as a
|
|
131
120
|
* path to an unpacked extension directory or a path to a xpi file.
|
|
132
121
|
* @param {string} dir The profile directory to install to.
|
|
133
|
-
* @param {boolean=} opt_excludeWebDriverExt Whether to skip installation of
|
|
134
|
-
* the default WebDriver extension.
|
|
135
122
|
* @return {!Promise<string>} A promise for the main profile directory
|
|
136
123
|
* once all extensions have been installed.
|
|
137
124
|
*/
|
|
138
|
-
function installExtensions(extensions, dir
|
|
139
|
-
var hasWebDriver = !!opt_excludeWebDriverExt;
|
|
125
|
+
function installExtensions(extensions, dir) {
|
|
140
126
|
var next = 0;
|
|
141
127
|
var extensionDir = path.join(dir, 'extensions');
|
|
142
128
|
|
|
@@ -145,11 +131,7 @@ function installExtensions(extensions, dir, opt_excludeWebDriverExt) {
|
|
|
145
131
|
|
|
146
132
|
function installNext() {
|
|
147
133
|
if (next >= extensions.length) {
|
|
148
|
-
|
|
149
|
-
fulfill(dir);
|
|
150
|
-
} else {
|
|
151
|
-
install(WEBDRIVER_EXTENSION_PATH);
|
|
152
|
-
}
|
|
134
|
+
fulfill(dir);
|
|
153
135
|
} else {
|
|
154
136
|
install(extensions[next++]);
|
|
155
137
|
}
|
|
@@ -157,7 +139,6 @@ function installExtensions(extensions, dir, opt_excludeWebDriverExt) {
|
|
|
157
139
|
|
|
158
140
|
function install(ext) {
|
|
159
141
|
extension.install(ext, extensionDir).then(function(id) {
|
|
160
|
-
hasWebDriver = hasWebDriver || (id === WEBDRIVER_EXTENSION_NAME);
|
|
161
142
|
installNext();
|
|
162
143
|
}, reject);
|
|
163
144
|
}
|
|
@@ -173,14 +154,10 @@ function installExtensions(extensions, dir, opt_excludeWebDriverExt) {
|
|
|
173
154
|
*/
|
|
174
155
|
function decode(data) {
|
|
175
156
|
return io.tmpFile().then(function(file) {
|
|
176
|
-
|
|
157
|
+
let buf = new Buffer(data, 'base64');
|
|
177
158
|
return io.write(file, buf)
|
|
178
159
|
.then(io.tmpDir)
|
|
179
|
-
.then(
|
|
180
|
-
var zip = new AdmZip(file);
|
|
181
|
-
zip.extractAllTo(dir); // Sync only? Why?? :-(
|
|
182
|
-
return dir;
|
|
183
|
-
});
|
|
160
|
+
.then(dir => unzip(file, dir));
|
|
184
161
|
});
|
|
185
162
|
}
|
|
186
163
|
|
|
@@ -353,14 +330,10 @@ class Profile {
|
|
|
353
330
|
|
|
354
331
|
/**
|
|
355
332
|
* Writes this profile to disk.
|
|
356
|
-
* @param {boolean=} opt_excludeWebDriverExt Whether to exclude the WebDriver
|
|
357
|
-
* extension from the generated profile. Used to reduce the size of an
|
|
358
|
-
* {@link #encode() encoded profile} since the server will always install
|
|
359
|
-
* the extension itself.
|
|
360
333
|
* @return {!Promise<string>} A promise for the path to the new profile
|
|
361
334
|
* directory.
|
|
362
335
|
*/
|
|
363
|
-
writeToDisk(
|
|
336
|
+
writeToDisk() {
|
|
364
337
|
var profileDir = io.tmpDir();
|
|
365
338
|
if (this.template_) {
|
|
366
339
|
profileDir = profileDir.then(function(dir) {
|
|
@@ -382,7 +355,7 @@ class Profile {
|
|
|
382
355
|
return profileDir.then(function(dir) {
|
|
383
356
|
return writeUserPrefs(prefs, dir);
|
|
384
357
|
}).then(function(dir) {
|
|
385
|
-
return installExtensions(extensions, dir
|
|
358
|
+
return installExtensions(extensions, dir);
|
|
386
359
|
});
|
|
387
360
|
}
|
|
388
361
|
|
|
@@ -395,20 +368,11 @@ class Profile {
|
|
|
395
368
|
*
|
|
396
369
|
*/
|
|
397
370
|
encode() {
|
|
398
|
-
return this.writeToDisk(
|
|
399
|
-
|
|
400
|
-
zip.
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
entry.header.method = 0;
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
return io.tmpFile().then(function(file) {
|
|
407
|
-
zip.writeZip(file); // Sync! Why oh why :-(
|
|
408
|
-
return io.read(file);
|
|
409
|
-
});
|
|
410
|
-
}).then(function(data) {
|
|
411
|
-
return data.toString('base64');
|
|
371
|
+
return this.writeToDisk().then(function(dir) {
|
|
372
|
+
let zip = new Zip;
|
|
373
|
+
return zip.addDir(dir)
|
|
374
|
+
.then(() => zip.toBuffer())
|
|
375
|
+
.then(buf => buf.toString('base64'));
|
|
412
376
|
});
|
|
413
377
|
}
|
|
414
378
|
|
package/ie.js
CHANGED
|
@@ -420,8 +420,8 @@ class Driver extends webdriver.WebDriver {
|
|
|
420
420
|
var client = service.start().then(url => new http.HttpClient(url));
|
|
421
421
|
var executor = new http.Executor(client);
|
|
422
422
|
|
|
423
|
-
return /** @type {!Driver} */(
|
|
424
|
-
executor, caps, opt_flow,
|
|
423
|
+
return /** @type {!Driver} */(super.createSession(
|
|
424
|
+
executor, caps, opt_flow, () => service.kill()));
|
|
425
425
|
}
|
|
426
426
|
|
|
427
427
|
/**
|
package/index.js
CHANGED
|
@@ -131,7 +131,10 @@ const THENABLE_DRIVERS = new Map;
|
|
|
131
131
|
function createDriver(ctor, ...args) {
|
|
132
132
|
let thenableWebDriverProxy = THENABLE_DRIVERS.get(ctor);
|
|
133
133
|
if (!thenableWebDriverProxy) {
|
|
134
|
-
/**
|
|
134
|
+
/**
|
|
135
|
+
* @extends {WebDriver} // Needed since `ctor` is dynamically typed.
|
|
136
|
+
* @implements {ThenableWebDriver}
|
|
137
|
+
*/
|
|
135
138
|
thenableWebDriverProxy = class extends ctor {
|
|
136
139
|
/**
|
|
137
140
|
* @param {!IThenable<!Session>} session
|
package/io/index.js
CHANGED
|
@@ -298,3 +298,62 @@ exports.mkdir = function(aPath) {
|
|
|
298
298
|
});
|
|
299
299
|
});
|
|
300
300
|
};
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Recursively creates a directory and any ancestors that do not yet exist.
|
|
305
|
+
*
|
|
306
|
+
* @param {string} dir The directory path to create.
|
|
307
|
+
* @return {!Promise<string>} A promise that will resolve with the path of the
|
|
308
|
+
* created directory.
|
|
309
|
+
*/
|
|
310
|
+
exports.mkdirp = function mkdirp(dir) {
|
|
311
|
+
return checkedCall(callback => {
|
|
312
|
+
fs.mkdir(dir, undefined, err => {
|
|
313
|
+
if (!err) {
|
|
314
|
+
callback(null, dir);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
switch (err.code) {
|
|
319
|
+
case 'EEXIST':
|
|
320
|
+
callback(null, dir);
|
|
321
|
+
return;
|
|
322
|
+
case 'ENOENT':
|
|
323
|
+
return mkdirp(path.dirname(dir))
|
|
324
|
+
.then(() => mkdirp(dir))
|
|
325
|
+
.then(() => callback(null, dir), err => callback(err));
|
|
326
|
+
default:
|
|
327
|
+
callback(err);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Recursively walks a directory, returning a promise that will resolve with
|
|
337
|
+
* a list of all files/directories seen.
|
|
338
|
+
*
|
|
339
|
+
* @param {string} rootPath the directory to walk.
|
|
340
|
+
* @return {!Promise<!Array<{path: string, dir: boolean}>>} a promise that will
|
|
341
|
+
* resolve with a list of entries seen. For each entry, the recorded path
|
|
342
|
+
* will be relative to `rootPath`.
|
|
343
|
+
*/
|
|
344
|
+
exports.walkDir = function(rootPath) {
|
|
345
|
+
let seen = [];
|
|
346
|
+
return (function walk(dir) {
|
|
347
|
+
return checkedCall(callback => fs.readdir(dir, callback))
|
|
348
|
+
.then(files => Promise.all(files.map(file => {
|
|
349
|
+
file = path.join(dir, file);
|
|
350
|
+
return checkedCall(cb => fs.stat(file, cb)).then(stats => {
|
|
351
|
+
seen.push({
|
|
352
|
+
path: path.relative(rootPath, file),
|
|
353
|
+
dir: stats.isDirectory()
|
|
354
|
+
});
|
|
355
|
+
return stats.isDirectory() && walk(file);
|
|
356
|
+
});
|
|
357
|
+
})));
|
|
358
|
+
})(rootPath).then(() => seen);
|
|
359
|
+
};
|
package/io/zip.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
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
|
+
'use strict';
|
|
19
|
+
|
|
20
|
+
const jszip = require('jszip');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
|
|
23
|
+
const io = require('./index');
|
|
24
|
+
const {InvalidArgumentError} = require('../lib/error');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Manages a zip archive.
|
|
28
|
+
*/
|
|
29
|
+
class Zip {
|
|
30
|
+
constructor() {
|
|
31
|
+
/** @private @const */
|
|
32
|
+
this.z_ = new jszip;
|
|
33
|
+
|
|
34
|
+
/** @private @const {!Set<!Promise<?>>} */
|
|
35
|
+
this.pendingAdds_ = new Set;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Adds a file to this zip.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} filePath path to the file to add.
|
|
42
|
+
* @param {string=} zipPath path to the file in the zip archive, defaults
|
|
43
|
+
* to the basename of `filePath`.
|
|
44
|
+
* @return {!Promise<?>} a promise that will resolve when added.
|
|
45
|
+
*/
|
|
46
|
+
addFile(filePath, zipPath = path.basename(filePath)) {
|
|
47
|
+
let add = io.read(filePath)
|
|
48
|
+
.then(buffer => this.z_.file(/** @type {string} */(zipPath), buffer));
|
|
49
|
+
this.pendingAdds_.add(add);
|
|
50
|
+
return add.then(
|
|
51
|
+
() => this.pendingAdds_.delete(add),
|
|
52
|
+
(e) => {
|
|
53
|
+
this.pendingAdds_.delete(add);
|
|
54
|
+
throw e;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Recursively adds a directory and all of its contents to this archive.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} dirPath path to the directory to add.
|
|
62
|
+
* @param {string=} zipPath path to the folder in the archive to add the
|
|
63
|
+
* directory contents to. Defaults to the root folder.
|
|
64
|
+
* @return {!Promise<?>} returns a promise that will resolve when the
|
|
65
|
+
* the operation is complete.
|
|
66
|
+
*/
|
|
67
|
+
addDir(dirPath, zipPath = '') {
|
|
68
|
+
return io.walkDir(dirPath).then(entries => {
|
|
69
|
+
let archive = this.z_;
|
|
70
|
+
if (zipPath) {
|
|
71
|
+
archive = archive.folder(zipPath);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let files = [];
|
|
75
|
+
entries.forEach(spec => {
|
|
76
|
+
if (spec.dir) {
|
|
77
|
+
archive.folder(spec.path);
|
|
78
|
+
} else {
|
|
79
|
+
files.push(
|
|
80
|
+
this.addFile(
|
|
81
|
+
path.join(dirPath, spec.path),
|
|
82
|
+
path.join(zipPath, spec.path)));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return Promise.all(files);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string} path File path to test for within the archive.
|
|
92
|
+
* @return {boolean} Whether this zip archive contains an entry with the given
|
|
93
|
+
* path.
|
|
94
|
+
*/
|
|
95
|
+
has(path) {
|
|
96
|
+
return this.z_.file(path) !== null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Returns the contents of the file in this zip archive with the given `path`.
|
|
101
|
+
* The returned promise will be rejected with an {@link InvalidArgumentError}
|
|
102
|
+
* if either `path` does not exist within the archive, or if `path` refers
|
|
103
|
+
* to a directory.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} path the path to the file whose contents to return.
|
|
106
|
+
* @return {!Promise<!Buffer>} a promise that will be resolved with the file's
|
|
107
|
+
* contents as a buffer.
|
|
108
|
+
*/
|
|
109
|
+
getFile(path) {
|
|
110
|
+
let file = this.z_.file(path);
|
|
111
|
+
if (!file) {
|
|
112
|
+
return Promise.reject(
|
|
113
|
+
new InvalidArgumentError(`No such file in zip archive: ${path}`));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (file.dir) {
|
|
117
|
+
return Promise.reject(
|
|
118
|
+
new InvalidArgumentError(
|
|
119
|
+
`The requested file is a directory: ${path}`));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return Promise.resolve(file.async('nodebuffer'));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns the compressed data for this archive in a buffer. _This method will
|
|
127
|
+
* not wait for any outstanding {@link #addFile add}
|
|
128
|
+
* {@link #addDir operations} before encoding the archive._
|
|
129
|
+
*
|
|
130
|
+
* @param {string} compression The desired compression.
|
|
131
|
+
* Must be `STORE` (the default) or `DEFLATE`.
|
|
132
|
+
* @return {!Promise<!Buffer>} a promise that will resolve with this archive
|
|
133
|
+
* as a buffer.
|
|
134
|
+
*/
|
|
135
|
+
toBuffer(compression = 'STORE') {
|
|
136
|
+
if (compression !== 'STORE' && compression !== 'DEFLATE') {
|
|
137
|
+
return Promise.reject(
|
|
138
|
+
new InvalidArgumentError(
|
|
139
|
+
`compression must be one of {STORE, DEFLATE}, got ${compression}`));
|
|
140
|
+
}
|
|
141
|
+
return Promise.resolve(
|
|
142
|
+
this.z_.generateAsync({compression, type: 'nodebuffer'}));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Asynchronously opens a zip archive.
|
|
149
|
+
*
|
|
150
|
+
* @param {string} path to the zip archive to load.
|
|
151
|
+
* @return {!Promise<!Zip>} a promise that will resolve with the opened
|
|
152
|
+
* archive.
|
|
153
|
+
*/
|
|
154
|
+
function load(path) {
|
|
155
|
+
return io.read(path).then(data => {
|
|
156
|
+
let zip = new Zip;
|
|
157
|
+
return zip.z_.loadAsync(data).then(() => zip);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Asynchronously unzips an archive file.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} src path to the source file to unzip.
|
|
166
|
+
* @param {string} dst path to the destination directory.
|
|
167
|
+
* @return {!Promise<string>} a promise that will resolve with `dst` once the
|
|
168
|
+
* archive has been unzipped.
|
|
169
|
+
*/
|
|
170
|
+
function unzip(src, dst) {
|
|
171
|
+
return load(src).then(zip => {
|
|
172
|
+
let promisedDirs = new Map;
|
|
173
|
+
let promises = [];
|
|
174
|
+
|
|
175
|
+
zip.z_.forEach((relPath, file) => {
|
|
176
|
+
let p;
|
|
177
|
+
if (file.dir) {
|
|
178
|
+
p = createDir(relPath);
|
|
179
|
+
} else {
|
|
180
|
+
let dirname = path.dirname(relPath);
|
|
181
|
+
if (dirname === '.') {
|
|
182
|
+
p = writeFile(relPath, file);
|
|
183
|
+
} else {
|
|
184
|
+
p = createDir(dirname).then(() => writeFile(relPath, file));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
promises.push(p);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
return Promise.all(promises).then(() => dst);
|
|
191
|
+
|
|
192
|
+
function createDir(dir) {
|
|
193
|
+
let p = promisedDirs.get(dir);
|
|
194
|
+
if (!p) {
|
|
195
|
+
p = io.mkdirp(path.join(dst, dir));
|
|
196
|
+
promisedDirs.set(dir, p);
|
|
197
|
+
}
|
|
198
|
+
return p;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function writeFile(relPath, file) {
|
|
202
|
+
return file.async('nodebuffer')
|
|
203
|
+
.then(buffer => io.write(path.join(dst, relPath), buffer));
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
// PUBLIC API
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
exports.Zip = Zip;
|
|
213
|
+
exports.load = load;
|
|
214
|
+
exports.unzip = unzip;
|