Skip to content

Commit cc6865e

Browse files
authored
Replace request-promise with node-fetch (#201)
1 parent 4996b39 commit cc6865e

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Browserstack TestCafe browser provider plugin.",
55
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
66
"engines": {
7-
"node": ">=8.0.0"
7+
"node": ">=10"
88
},
99
"homepage": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
1010
"author": {
@@ -34,13 +34,13 @@
3434
"dedent": "^0.7.0",
3535
"desired-capabilities": "^0.1.0",
3636
"firefox-profile": "^4.2.2",
37+
"https-proxy-agent": "^7.0.0",
3738
"lodash": "^4.17.15",
3839
"mime-db": "^1.43.0",
40+
"node-fetch": "^2.6.11",
3941
"os-family": "^1.0.0",
4042
"pinkie": "^2.0.4",
4143
"promisify-event": "^1.0.0",
42-
"request": "^2.88.0",
43-
"request-promise": "^4.2.4",
4444
"sharp": "^0.30.7",
4545
"tmp": "0.0.31"
4646
},

src/backends/automate.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ export default class AutomateBackend extends BaseBackend {
151151

152152
this.sessions[id] = await requestApi(BROWSERSTACK_API_PATHS.newSession, {
153153
body: { desiredCapabilities: capabilities },
154-
155-
executeImmediately: true
156154
});
157155

158156
AutomateBackend._ensureSessionId(this.sessions[id]);
@@ -168,7 +166,7 @@ export default class AutomateBackend extends BaseBackend {
168166

169167
var sessionId = this.sessions[id].sessionId;
170168

171-
this.sessions[id].interval = setInterval(() => requestApi(BROWSERSTACK_API_PATHS.getUrl(sessionId), { executeImmediately: true }), API_POLLING_INTERVAL);
169+
this.sessions[id].interval = setInterval(() => requestApi(BROWSERSTACK_API_PATHS.getUrl(sessionId)), API_POLLING_INTERVAL);
172170

173171
await requestApi(BROWSERSTACK_API_PATHS.openUrl(sessionId), { body: { url: pageUrl } });
174172
}
@@ -191,7 +189,7 @@ export default class AutomateBackend extends BaseBackend {
191189
async takeScreenshot (id, screenshotPath) {
192190
var base64Data = await requestApi(BROWSERSTACK_API_PATHS.screenshot(this.sessions[id].sessionId));
193191
var buffer = Buffer.from(base64Data.value, 'base64');
194-
192+
195193
await sharp(buffer).toFile(screenshotPath);
196194
}
197195

src/backends/js-testing.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ export default class JSTestingBackend extends BaseBackend {
8383
};
8484

8585
this.workers[id] = await requestApi(BROWSERSTACK_API_PATHS.newWorker, {
86-
executeImmediately: true,
87-
88-
...capabilities,
86+
body: { ...capabilities }
8987
});
9088

9189
const sessionInfo = await this._requestSessionInfo(id);

src/utils/request-api.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1-
import Promise from 'pinkie';
2-
import request from 'request-promise';
1+
import fetch from 'node-fetch';
2+
import { HttpsProxyAgent } from 'https-proxy-agent';
33
import * as ERROR_MESSAGES from '../templates/error-messages';
44

5-
const apiRequestPromise = Promise.resolve(null);
6-
7-
export default function (apiPath, params = {}) {
5+
export default async function (apiPath, params = {}) {
86
if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY'])
97
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
108

11-
var { body, executeImmediately, ...queryParams } = params;
9+
const { body, ...queryParams } = params;
1210

13-
var opts = {
14-
url: apiPath.url,
15-
auth: {
16-
user: process.env['BROWSERSTACK_USERNAME'],
17-
pass: process.env['BROWSERSTACK_ACCESS_KEY'],
18-
},
11+
const urlObj = new URL(apiPath.url);
1912

20-
headers: {
21-
'user-agent': 'testcafe-browserstack',
22-
},
13+
for (const [key, value] of Object.entries(queryParams))
14+
urlObj.searchParams.append(key, value);
15+
16+
const url = urlObj.toString();
2317

24-
qs: { ...queryParams },
18+
const user = process.env['BROWSERSTACK_USERNAME'];
19+
const pass = process.env['BROWSERSTACK_ACCESS_KEY'];
2520

26-
method: apiPath.method || 'GET',
27-
json: apiPath.encoding === void 0
21+
const options = {
22+
method: apiPath.method || 'GET',
23+
headers: {
24+
'Authorization': `Basic ${Buffer.from(user + ':' + pass).toString('base64')}`,
25+
'User-Agent': 'testcafe-browserstack',
26+
},
2827
};
2928

3029
const proxy = process.env['BROWSERSTACK_PROXY'];
3130

3231
if (proxy)
33-
opts.proxy = `http://${proxy}`;
32+
options.agent = new HttpsProxyAgent(`http://${proxy}`);
3433

35-
if (body)
36-
opts.body = body;
34+
if (body) {
35+
options.body = JSON.stringify(body);
36+
options.headers['Content-Type'] = 'application/json';
37+
}
3738

38-
if (apiPath.encoding !== void 0)
39-
opts.encoding = apiPath.encoding;
39+
const res = await fetch(url, options);
4040

41-
const chainPromise = executeImmediately ? Promise.resolve(null) : apiRequestPromise;
42-
43-
const currentRequestPromise = chainPromise
44-
.then(() => request(opts))
45-
.catch(error => {
46-
if (error.statusCode === 401)
47-
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
41+
if (res.status === 401)
42+
throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED());
4843

49-
throw error;
50-
});
44+
if (apiPath.encoding === null)
45+
return Buffer.from(await res.arrayBuffer());
5146

52-
return currentRequestPromise;
47+
return res.json();
5348
}

0 commit comments

Comments
 (0)