Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
chore: update to es6 promises and remove when dependency (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccarruitero authored and kumar303 committed Sep 24, 2019
1 parent d01a51e commit 817e651
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mz": "2.7.0",
"request": "2.88.0",
"source-map-support": "0.4.6",
"stream-to-promise": "2.2.0",
"when": "3.7.8"
"stream-to-promise": "2.2.0"
},
"devDependencies": {
"@babel/core": "7.5.5",
Expand Down
19 changes: 11 additions & 8 deletions src/amo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import url from "url";
import path from "path";
import defaultJwt from "jsonwebtoken";
import {default as defaultRequest} from "request";
import when from "when";
import nodefn from "when/node";

const defaultSetInterval = setInterval;
const defaultClearInterval = clearInterval;
Expand Down Expand Up @@ -159,7 +157,7 @@ export class Client {
...opt,
};

return when.promise((resolve, reject) => {
return new Promise((resolve, reject) => {
this._validateProgress.animate();
var statusCheckTimeout;
var nextStatusCheck;
Expand Down Expand Up @@ -276,7 +274,7 @@ export class Client {
}

const download = (fileUrl) => {
return when.promise((resolve, reject) => {
return new Promise((resolve, reject) => {
// The API will give us a signed file named in a sane way.
var fileName = path.join(this.downloadDir, getUrlBasename(fileUrl));
var out = createWriteStream(fileName);
Expand Down Expand Up @@ -313,7 +311,7 @@ export class Client {
});
};

return when.promise((resolve, reject) => {
return new Promise((resolve, reject) => {
var foundUnsignedFiles = false;
signedFiles.forEach((file) => {
if (file.signed) {
Expand All @@ -330,7 +328,7 @@ export class Client {
"Some files were not signed. Re-run with --verbose for details.");
}
showProgress();
resolve(when.all(allDownloads));
resolve(Promise.all(allDownloads));
} else {
reject(new Error(
"The XPI was processed but no signed files were found. Check your " +
Expand Down Expand Up @@ -471,7 +469,7 @@ export class Client {
*/
request(method, requestConf, {throwOnBadResponse=true} = {}) {
method = method.toLowerCase();
return when.promise((resolve) => {
return new Promise((resolve) => {
requestConf = this.configureRequest(requestConf);
this.debug(`[API] ${method.toUpperCase()} request:\n`, requestConf);

Expand All @@ -484,8 +482,13 @@ export class Client {
// // promise gets resolved here
// })
//
resolve(nodefn.call(requestMethod, requestConf));
resolve(requestMethod(requestConf, (error, response, body) => {
return new Promise((res, rej) => {
if (error) { rej(error); }

res([response, body]);
});
}));
}).then((responseResult) => {
var httpResponse = responseResult[0];
var body = responseResult[1];
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Importing this like `import fs from "mz/fs"` was causing usage on
// npm 2.x to throw missing dependency errors. *shrug*
import {fs} from "mz";
import when from "when";

import {Client as DefaultAMOClient} from "./amo-client";

Expand Down Expand Up @@ -42,7 +41,7 @@ export default function signAddon(
AMOClient=DefaultAMOClient,
}) {

return when.promise(
return new Promise(
(resolve) => {

function reportEmpty(name) {
Expand Down
33 changes: 18 additions & 15 deletions tests/test.amo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from "path";
import {expect} from "chai";
import sinon from "sinon";
import jwt from "jsonwebtoken";
import when from "when";

import * as amoClient from "../src/amo-client";

Expand Down Expand Up @@ -832,7 +831,7 @@ describe("amoClient.Client", function() {
}));

});
return when.all(requests);
return Promise.all(requests);
});

it("configures a request timeout based on JWT expiration", function() {
Expand Down Expand Up @@ -1082,7 +1081,7 @@ class MockRequest {
this.responseError = conf.responseError;
}

_mockRequest(method, conf, callback) {
_mockRequest(method, conf) {
var info = {conf: conf};
this.calls.push({...info, name: method});
this.callMap[method] = info;
Expand All @@ -1098,27 +1097,31 @@ class MockRequest {
response = {};
response.responseError = new Error("Response queue is empty");
}
callback(response.responseError, response.httpResponse,
response.responseBody);

if (response.responseError instanceof Error) {
throw response.responseError;
} else {
return [response.httpResponse, response.responseBody];
}
}

get(conf, callback) {
return this._mockRequest("get", conf, callback);
get(conf) {
return this._mockRequest("get", conf);
}

post(conf, callback) {
return this._mockRequest("post", conf, callback);
post(conf) {
return this._mockRequest("post", conf);
}

put(conf, callback) {
return this._mockRequest("put", conf, callback);
put(conf) {
return this._mockRequest("put", conf);
}

patch(conf, callback) {
return this._mockRequest("patch", conf, callback);
patch(conf) {
return this._mockRequest("patch", conf);
}

delete(conf, callback) {
return this._mockRequest("delete", conf, callback);
delete(conf) {
return this._mockRequest("delete", conf);
}
}
3 changes: 1 addition & 2 deletions tests/test.sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {beforeEach, describe, it} from "mocha";
import path from "path";
import {expect} from "chai";
import sinon from "sinon";
import when from "when";

import {signAddonAndExit} from "../src";

Expand Down Expand Up @@ -38,7 +37,7 @@ describe("sign", function() {
this.debug = function() {};
}

signingCall = sinon.spy(() => when.promise((resolve) => {
signingCall = sinon.spy(() => new Promise((resolve) => {
if (options.errorToThrow) {
throw options.errorToThrow;
}
Expand Down

0 comments on commit 817e651

Please sign in to comment.