Skip to content

Commit bc935b6

Browse files
Refactor: Replace deprecated request package by got (#563)
* Move debug contexts params to config (#552) * build: replace deprecated dependency with got package * build: lock file * feat: implement usage of got package for requests * build: adjust lock file
1 parent b608d28 commit bc935b6

File tree

7 files changed

+744
-797
lines changed

7 files changed

+744
-797
lines changed

docs/WRITE-A-PLUGIN.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,9 @@ Example:
190190
},
191191
{
192192
geturls: function(cb) {
193-
var request = require('request');
194-
request({
195-
url: 'https://api.domain.com/items',
196-
json: true
197-
}, function(error, body, data) {
198-
if (error) {
199-
return cb(error);
200-
}
193+
got('https://api.domain.com/items', { responseType: 'json' })
194+
.then(response => {
195+
const data = response.body;
201196
if (!data || !data.urls) {
202197
return cb('No urls in API data');
203198
}

lib/whitelist.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as _ from 'underscore';
66
import * as utils from './utils.js';
77
import log from '../logging.js';
8-
import request from 'request';
8+
import got from 'got';
99

1010
import { fileURLToPath } from 'url';
1111
import { dirname } from 'path';
@@ -356,38 +356,33 @@
356356
log("Loading domains list from " + CONFIG.WHITELIST_URL);
357357

358358
var options = {
359-
uri: CONFIG.WHITELIST_URL,
360-
json: true,
361-
qs: {
359+
url: CONFIG.WHITELIST_URL,
360+
responseType: 'json',
361+
searchParams: {
362362
domain: CONFIG.baseAppUrl && CONFIG.baseAppUrl.replace(/.+\/\//, ''),
363363
v: CONFIG.VERSION
364364
},
365365
headers: {
366366
// Prevent caching.
367367
'Cache-Control': 'no-cache'
368368
},
369-
// TODO: remove in helix-fetch
370-
gzip: true
371369
};
372-
370+
373371
if (whitelistLastModified) {
374372
options.headers['If-Modified-Since'] = whitelistLastModified;
375373
}
376374

377-
request(utils.prepareRequestOptions(options, {skipPlugins: true}), function(error, r, newWhitelist) {
375+
got(options).then(response => {
376+
const newWhitelist = response.body;
378377

379-
if (error) {
380-
console.error('Error loading domains list from ' + CONFIG.WHITELIST_URL + ' : ' + error);
381-
} else if (r.statusCode === 500) {
378+
if (response.statusCode === 500) {
382379
console.error('Error loading domains list from ' + CONFIG.WHITELIST_URL + ' : ' + newWhitelist);
383-
} else if (r.statusCode === 304) {
380+
} else if (response.statusCode === 304) {
384381
log('Whitelist respond: 304 (not modified)');
385382
} else if (!newWhitelist || typeof newWhitelist === 'string') {
386383
console.error('Error loading domains list from ' + CONFIG.WHITELIST_URL + ' : incorrect data: ' + newWhitelist);
387384
} else {
388-
389-
whitelistLastModified = r.headers['last-modified'];
390-
385+
whitelistLastModified = response.headers['last-modified'];
391386
applyParsedWhitelist(newWhitelist);
392387
}
393388

modules/tests-ui/utils.js

+46-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as _ from 'underscore';
22
import FeedParser from 'feedparser';
3-
import request from 'request';
3+
import got from 'got';
44
import * as async from 'async';
55
import * as url from 'url';
66
import { PageTestLog, TestUrlsSet, PluginTest } from './models.js';
@@ -50,35 +50,32 @@ export function sendQANotification(logEntry, data) {
5050
message += " - " + errors;
5151
}
5252

53-
request({
54-
uri: CONFIG.SLACK_WEBHOOK_FOR_QA,
55-
method: 'POST',
56-
json: true,
57-
body: {
58-
"parse": "none",
59-
"channel": CONFIG.SLACK_CHANNEL_FOR_QA,
60-
"username": SLACK_USERNAME,
61-
"text": previewMessage,
62-
"blocks": [
53+
got.post(CONFIG.SLACK_WEBHOOK_FOR_QA, {
54+
json: {
55+
parse: "none",
56+
channel: CONFIG.SLACK_CHANNEL_FOR_QA,
57+
username: SLACK_USERNAME,
58+
text: previewMessage,
59+
blocks: [
6360
{
64-
"type": "section",
65-
"text": {
66-
"type": "mrkdwn",
67-
"text": message // Message: [domain.com] Failed - errors.
61+
type: "section",
62+
text: {
63+
type: "mrkdwn",
64+
text: message
6865
}
6966
}
7067
],
71-
"attachments": [
68+
attachments: [
7269
{
73-
"blocks": [{
74-
"type": "section",
75-
"text": {
76-
"type": "mrkdwn",
77-
"verbatim": true,
78-
"text": "`<" + (CONFIG.QA_BASE_URL || baseAppUrl) + "/debug?uri=" + encodeURIComponent(logEntry.url) + "|debug>` " + logEntry.url.replace(/^https?:\/\//, '') // Debug link.
70+
blocks: [{
71+
type: "section",
72+
text: {
73+
type: "mrkdwn",
74+
verbatim: true,
75+
text: "`<" + (CONFIG.QA_BASE_URL || baseAppUrl) + "/debug?uri=" + encodeURIComponent(logEntry.url) + "|debug>` " + logEntry.url.replace(/^https?:\/\//, '')
7976
}
8077
}],
81-
"color": COLORS[data.color]
78+
color: COLORS[data.color]
8279
}
8380
]
8481
}
@@ -272,43 +269,43 @@ export function fetchFeedUrls(feedUrl, options, cb) {
272269
cb(error, urls);
273270
};
274271

275-
request({
276-
uri: feedUrl,
277-
agentOptions: {
272+
273+
got.stream(feedUrl, {
274+
https: {
278275
rejectUnauthorized: false
279276
}
280277
})
281-
.pipe(new FeedParser({addmeta: false}))
282-
.on('error', function(error) {
283-
_cb(error);
284-
})
285-
.on('readable', function () {
286-
var stream = this, item;
287-
while (item = stream.read()) {
278+
.pipe(new FeedParser({ addmeta: false }))
279+
.on('error', function(error) {
280+
_cb(error);
281+
})
282+
.on('readable', function () {
283+
var stream = this, item;
284+
while (item = stream.read()) {
288285

289-
if (urls.length < MAX_FEED_URLS) {
286+
if (urls.length < MAX_FEED_URLS) {
290287

291-
var url = item.origlink || item.link;
288+
var url = item.origlink || item.link;
292289

293-
if (options.getUrl) {
294-
url = options.getUrl(url);
295-
}
290+
if (options.getUrl) {
291+
url = options.getUrl(url);
292+
}
296293

297-
if (!url) {
298-
return;
299-
}
294+
if (!url) {
295+
return;
296+
}
300297

301-
urls.push(url);
298+
urls.push(url);
302299

303-
if (MAX_FEED_URLS == urls.length) {
304-
_cb();
305-
}
300+
if (MAX_FEED_URLS === urls.length) {
301+
_cb();
306302
}
307303
}
308-
})
309-
.on('end', function() {
310-
_cb();
311-
});
304+
}
305+
})
306+
.on('end', function() {
307+
_cb();
308+
});
312309
};
313310

314311
export function fetchUrlsByPageOnFeed(pageWithFeed, otpions, cb) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"ejs": "3.1.10",
3131
"entities": "1.1.2",
3232
"express": "^4.19.0",
33+
"got": "^14.4.4",
3334
"graceful-cluster": "^0.0.5",
3435
"htmlparser2": "^7.2.0",
3536
"iconv-lite": "^0.6.3",
@@ -44,7 +45,6 @@
4445
"readabilitySAX": "1.6.1",
4546
"redis": "^4.6.14",
4647
"redis-clustr": "1.7.0",
47-
"request": "^2.88.2",
4848
"sax": "^1.2.4",
4949
"send": "^0.18.0",
5050
"underscore": "^1.13.6"

plugins/domains/dailymotion.com/dailymotion.com.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as querystring from 'querystring';
2-
import request from 'request';
2+
import got from 'got';
33

44
export default {
55

@@ -53,21 +53,17 @@ export default {
5353

5454
tests: [{
5555
getUrls: function(cb) {
56-
request({
57-
url: 'https://api.dailymotion.com/videos',
58-
json: true
59-
}, function(error, body, data) {
60-
if (error) {
61-
return cb(error);
62-
}
56+
got('https://api.dailymotion.com/videos', { responseType: 'json' })
57+
.then(response => {
58+
const data = response.body;
6359
if (!data || !data.list) {
6460
return cb('No videos list in API data');
6561
}
6662
cb(null, data.list.slice(0, 10).map(function(item) {
6763
return 'https://www.dailymotion.com/video/' + item.id;
6864
}));
69-
});
70-
65+
})
66+
.catch(error => cb(error));
7167
}
7268
}, {
7369
skipMixins: ["video", "og-description", "og-image", "canonical"],

pnpm-lock.yaml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)