Skip to content

Commit 0342309

Browse files
committed
[js] Replace uses of managed-promises with native promises where the promise
manager isn't required.
1 parent 4227381 commit 0342309

23 files changed

+452
-371
lines changed

javascript/node/selenium-webdriver/CHANGES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
`builder.Builder#usingHttpAgent()`
55
* Added new wait conditions: `until.urlIs()`, `until.urlContains()`,
66
`until.urlMatches()`
7-
* Added work around for [GeckoDriver bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1274924) raising a type conversion error
7+
* Added work around for [GeckoDriver bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1274924)
8+
raising a type conversion error
9+
* Internal cleanup replacing uses of managed promises with native promises
810
* Removed the mandatory use of Firefox Dev Edition, when using Marionette driver
911
* Fixed timeouts' URL
1012

javascript/node/selenium-webdriver/chrome.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ const Command = {
151151

152152
/**
153153
* Creates a command executor with support for ChromeDriver's custom commands.
154-
* @param {!promise.Promise<string>} url The server's URL.
154+
* @param {!Promise<string>} url The server's URL.
155155
* @return {!command.Executor} The new command executor.
156156
*/
157157
function createExecutor(url) {
158-
return new executors.DeferredExecutor(url.then(function(url) {
158+
return new executors.DeferredExecutor(url.then(url => {
159159
let client = new http.HttpClient(url);
160160
let executor = new http.Executor(client);
161161
executor.defineCommand(
@@ -321,7 +321,7 @@ class ServiceBuilder {
321321
loopback: true,
322322
path: this.path_,
323323
port: port,
324-
args: promise.when(port, function(port) {
324+
args: Promise.resolve(port).then(function(port) {
325325
return args.concat('--port=' + port);
326326
}),
327327
env: this.env_,
@@ -746,8 +746,8 @@ class Options {
746746
if (Buffer.isBuffer(extension)) {
747747
return extension.toString('base64');
748748
}
749-
return promise.checkedNodeCall(
750-
fs.readFile, extension, 'base64');
749+
return io.read(/** @type {string} */(extension))
750+
.then(buffer => buffer.toString('base64'));
751751
});
752752
}
753753
return json;

javascript/node/selenium-webdriver/edge.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class ServiceBuilder {
285285
// (or can we?), force the DriverService to use "localhost".
286286
hostname: 'localhost',
287287
port: port,
288-
args: promise.fulfilled(port).then(function(port) {
288+
args: Promise.resolve(port).then(function(port) {
289289
return args.concat('--port=' + port);
290290
}),
291291
env: this.env_,

javascript/node/selenium-webdriver/executors.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
const HttpClient = require('./http').HttpClient,
2626
HttpExecutor = require('./http').Executor,
27-
DeferredExecutor = require('./lib/command').DeferredExecutor,
28-
promise = require('./lib/promise');
27+
DeferredExecutor = require('./lib/command').DeferredExecutor;
2928

3029

3130
// PUBLIC API
@@ -36,16 +35,16 @@ exports.DeferredExecutor = DeferredExecutor;
3635

3736
/**
3837
* Creates a command executor that uses WebDriver's JSON wire protocol.
39-
* @param {(string|!promise.Promise<string>)} url The server's URL,
40-
* or a promise that will resolve to that URL.
41-
* @param {http.Agent=} opt_agent (optional) The Http.Agent for the client to
42-
* use.
38+
* @param {(string|!Promise<string>)} url The server's URL, or a promise that
39+
* will resolve to that URL.
40+
* @param {http.Agent=} opt_agent (optional) The Http.Agent for the client to
41+
* use.
4342
* @param {(string|null)=} opt_proxy (optional) The URL of the HTTP proxy for
4443
* the client to use.
4544
* @returns {!./lib/command.Executor} The new command executor.
4645
*/
4746
exports.createExecutor = function(url, opt_agent, opt_proxy) {
48-
return new DeferredExecutor(promise.when(url, function(url) {
47+
return new DeferredExecutor(url.then(url => {
4948
var client = new HttpClient(url, opt_agent, opt_proxy);
5049
return new HttpExecutor(client);
5150
}));

javascript/node/selenium-webdriver/firefox/binary.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const child = require('child_process'),
2828
util = require('util');
2929

3030
const isDevMode = require('../lib/devmode'),
31-
promise = require('../lib/promise'),
3231
Symbols = require('../lib/symbols'),
3332
io = require('../io'),
3433
exec = require('../io/exec');
@@ -126,29 +125,21 @@ function findFirefox(opt_dev) {
126125
/**
127126
* Copies the no focus libs into the given profile directory.
128127
* @param {string} profileDir Path to the profile directory to install into.
129-
* @return {!promise.Promise.<string>} The LD_LIBRARY_PATH prefix string to use
128+
* @return {!Promise<string>} The LD_LIBRARY_PATH prefix string to use
130129
* for the installed libs.
131130
*/
132131
function installNoFocusLibs(profileDir) {
133132
var x86 = path.join(profileDir, 'x86');
134133
var amd64 = path.join(profileDir, 'amd64');
135134

136-
return mkdir(x86)
137-
.then(copyLib.bind(null, NO_FOCUS_LIB_X86, x86))
138-
.then(mkdir.bind(null, amd64))
139-
.then(copyLib.bind(null, NO_FOCUS_LIB_AMD64, amd64))
135+
return io.mkdir(x86)
136+
.then(() => copyLib(NO_FOCUS_LIB_X86, x86))
137+
.then(() => io.mkdir(amd64))
138+
.then(() => copyLib(NO_FOCUS_LIB_AMD64, amd64))
140139
.then(function() {
141140
return x86 + ':' + amd64;
142141
});
143142

144-
function mkdir(dir) {
145-
return io.exists(dir).then(function(exists) {
146-
if (!exists) {
147-
return promise.checkedNodeCall(fs.mkdir, dir);
148-
}
149-
});
150-
}
151-
152143
function copyLib(src, dir) {
153144
return io.copy(src, path.join(dir, X_IGNORE_NO_FOCUS_LIB));
154145
}
@@ -226,19 +217,19 @@ class Binary {
226217
* executable path when this instance was created. Otherwise, an attempt will
227218
* be made to find Firefox on the current system.
228219
*
229-
* @return {!promise.Promise<string>} a promise for the path to the Firefox
230-
* executable used by this instance.
220+
* @return {!Promise<string>} a promise for the path to the Firefox executable
221+
* used by this instance.
231222
*/
232223
locate() {
233-
return promise.fulfilled(this.exe_ || findFirefox(this.devEdition_));
224+
return Promise.resolve(this.exe_ || findFirefox(this.devEdition_));
234225
}
235226

236227
/**
237228
* Launches Firefox and returns a promise that will be fulfilled when the
238229
* process terminates.
239230
* @param {string} profile Path to the profile directory to use.
240-
* @return {!promise.Promise<!exec.Command>} A promise for the handle to the
241-
* started subprocess.
231+
* @return {!Promise<!exec.Command>} A promise for the handle to the started
232+
* subprocess.
242233
*/
243234
launch(profile) {
244235
let env = {};
@@ -264,8 +255,7 @@ class Binary {
264255
* the wire; all command line arguments and environment variables will be
265256
* discarded.
266257
*
267-
* @return {!promise.Promise<string>} A promise for this binary's wire
268-
* representation.
258+
* @return {!Promise<string>} A promise for this binary's wire representation.
269259
*/
270260
[Symbols.serialize]() {
271261
return this.locate();

javascript/node/selenium-webdriver/firefox/extension.js

+29-23
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ const AdmZip = require('adm-zip'),
2424
path = require('path'),
2525
xml = require('xml2js');
2626

27-
const promise = require('../lib/promise'),
28-
checkedCall = promise.checkedNodeCall,
29-
io = require('../io');
27+
const io = require('../io');
3028

3129

3230
/**
@@ -48,25 +46,24 @@ class AddonFormatError extends Error {
4846
* @param {string} extension Path to the extension to install, as either a xpi
4947
* file or a directory.
5048
* @param {string} dir Path to the directory to install the extension in.
51-
* @return {!promise.Promise.<string>} A promise for the add-on ID once
49+
* @return {!Promise<string>} A promise for the add-on ID once
5250
* installed.
5351
*/
5452
function install(extension, dir) {
5553
return getDetails(extension).then(function(details) {
56-
function returnId() { return details.id; }
57-
5854
var dst = path.join(dir, details.id);
5955
if (extension.slice(-4) === '.xpi') {
6056
if (!details.unpack) {
61-
return io.copy(extension, dst + '.xpi').then(returnId);
57+
return io.copy(extension, dst + '.xpi').then(() => details.id);
6258
} else {
63-
return checkedCall(fs.readFile, extension).then(function(buff) {
59+
return Promise.resolve().then(function() {
6460
// TODO: find an async library for inflating a zip archive.
65-
new AdmZip(buff).extractAllTo(dst, true);
66-
}).then(returnId);
61+
new AdmZip(extension).extractAllTo(dst, true);
62+
return details.id;
63+
});
6764
}
6865
} else {
69-
return io.copyDir(extension, dst).then(returnId);
66+
return io.copyDir(extension, dst).then(() => details.id);
7067
}
7168
});
7269
}
@@ -86,7 +83,7 @@ var RdfRoot;
8683
/**
8784
* Extracts the details needed to install an add-on.
8885
* @param {string} addonPath Path to the extension directory.
89-
* @return {!promise.Promise.<!AddonDetails>} A promise for the add-on details.
86+
* @return {!Promise<!AddonDetails>} A promise for the add-on details.
9087
*/
9188
function getDetails(addonPath) {
9289
return readManifest(addonPath).then(function(doc) {
@@ -143,34 +140,43 @@ function getDetails(addonPath) {
143140
/**
144141
* Reads the manifest for a Firefox add-on.
145142
* @param {string} addonPath Path to a Firefox add-on as a xpi or an extension.
146-
* @return {!promise.Promise<!Object>} A promise for the parsed manifest.
143+
* @return {!Promise<!Object>} A promise for the parsed manifest.
147144
*/
148145
function readManifest(addonPath) {
149146
var manifest;
150147

151148
if (addonPath.slice(-4) === '.xpi') {
152-
manifest = checkedCall(fs.readFile, addonPath).then(function(buff) {
153-
var zip = new AdmZip(buff);
149+
manifest = new Promise((resolve, reject) => {
150+
let zip = new AdmZip(addonPath);
151+
154152
if (!zip.getEntry('install.rdf')) {
155-
throw new AddonFormatError(
156-
'Could not find install.rdf in ' + addonPath);
153+
reject(new AddonFormatError(
154+
'Could not find install.rdf in ' + addonPath));
155+
return;
157156
}
158-
var done = promise.defer();
159-
zip.readAsTextAsync('install.rdf', done.fulfill);
160-
return done.promise;
157+
158+
zip.readAsTextAsync('install.rdf', resolve);
161159
});
162160
} else {
163-
manifest = checkedCall(fs.stat, addonPath).then(function(stats) {
161+
manifest = io.stat(addonPath).then(function(stats) {
164162
if (!stats.isDirectory()) {
165163
throw Error(
166164
'Add-on path is niether a xpi nor a directory: ' + addonPath);
167165
}
168-
return checkedCall(fs.readFile, path.join(addonPath, 'install.rdf'));
166+
return io.read(path.join(addonPath, 'install.rdf'));
169167
});
170168
}
171169

172170
return manifest.then(function(content) {
173-
return checkedCall(xml.parseString, 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+
});
174180
});
175181
}
176182

javascript/node/selenium-webdriver/firefox/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function createGeckoDriverService(binary) {
283283
return new remote.DriverService(geckoDriver, {
284284
loopback: true,
285285
port: port,
286-
args: promise.all([exe, port]).then(args => {
286+
args: Promise.all([exe, port]).then(args => {
287287
return ['-b', args[0], '--webdriver-port', args[1]];
288288
})
289289
// ,stdio: 'inherit'
@@ -370,11 +370,12 @@ class Driver extends webdriver.WebDriver {
370370
});
371371

372372
onQuit = function() {
373-
let finishCommand = command.then(command => {
373+
return command.then(command => {
374374
command.kill();
375-
return command.result();
375+
return preparedProfile.then(io.rmDir)
376+
.then(() => command.result(),
377+
() => command.result());
376378
});
377-
return finishCommand.finally(() => preparedProfile.then(io.rmDir));
378379
};
379380
}
380381

@@ -386,7 +387,7 @@ class Driver extends webdriver.WebDriver {
386387

387388
/** @override */
388389
this.quit = function() {
389-
return boundQuit().thenFinally(onQuit);
390+
return boundQuit().finally(onQuit);
390391
};
391392
}
392393

0 commit comments

Comments
 (0)