Skip to content

Commit

Permalink
Merge pull request #1228 from w3c/superagent
Browse files Browse the repository at this point in the history
* replace request with superagent and add a user-agent

* change response type so we can still process different type of files
  • Loading branch information
deniak authored Dec 11, 2024
2 parents 8c9c62a + 632e253 commit 08c445b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
27 changes: 12 additions & 15 deletions lib/document-downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

import Fs from 'fs';
import Request from 'request';
import sua from 'superagent';
import pkg from 'immutable';
import Promise from 'promise';
import Url from 'url';
Expand All @@ -24,28 +24,25 @@ const DocumentDownloader = {};

DocumentDownloader.fetch = url =>
new Promise((resolve, reject) => {
Request.get(
url,
{
timeout: 30000,
encoding: null, // If not specified, utf8 by default
},
(error, response, body) => {
if (error) {
sua
.get(url)
.set('User-Agent', 'W3C/Echidna')
.responseType('blob')
.end((err, res) => {
if (err) {
reject(
new Error(
`Fetching ${url} triggered a network error: ${error.message}`,
`Fetching ${url} triggered a network error: ${err.message}`,
),
);
} else if (response.statusCode !== 200) {
} else if (res.statusCode !== 200) {
reject(
new Error(
`Fetching ${url} triggered an HTTP error: code ${response.statusCode}`,
`Fetching ${url} triggered an HTTP error: code ${res.statusCode}`,
),
);
} else resolve(body);
},
);
} else resolve(res.body);
});
});

DocumentDownloader.fetchAll = urls =>
Expand Down
24 changes: 10 additions & 14 deletions lib/json-http-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

import Promise from 'promise';
import Request from 'request';
import sua from 'superagent';

/**
* @exports lib/json-http-service
Expand Down Expand Up @@ -45,19 +45,15 @@ JsonHttpService.prototype = {
const self = this;

return new Promise((resolve, reject) => {
Request.post(
{
url: self.url,
json: true,
auth: { user: self.user, pass: self.pass },
headers: self.headers,
body,
},
(error, response, bodyResult) => {
if (error) reject(error);
else resolve({ response, body: bodyResult });
},
);
sua.post(self.url)
.set('Content-Type', 'application/json')
.auth(self.user, self.pass)
.set(self.headers)
.send(body)
.end((err, res) => {
if (err) reject(err);
else resolve({ response: res, body: res.body });
});
});
},
};
Expand Down
30 changes: 14 additions & 16 deletions lib/token-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

import Promise from 'promise';
import Request from 'request';
import sua from 'superagent';
import pkg from 'immutable';

const { List } = pkg;
Expand All @@ -23,25 +23,23 @@ const TokenChecker = {};
TokenChecker.check = (url, token) =>
// url shortlink
new Promise((resolve, reject) => {
const options = {
uri: global.TOKEN_ENDPOINT,
headers: { 'X-W3C-Sso': 'bypass' },
qs: { spec: url, token },
// USERNAME & PASSWORD is a credential for https://www.w3.org/Web/publications/authorize?
auth: { user: global.USERNAME, pass: global.PASSWORD },
};

Request.get(options, (err, res, body) => {
if (err)
reject(new Error('There was an error while checking the token: ', err));
sua
.get(global.TOKEN_ENDPOINT)
.set('User-Agent', 'W3C/Echidna')
.set('X-W3C-Sso', 'bypass')
.query({ spec: url, token })
.auth(global.USERNAME, global.PASSWORD)
.end((err, res) => {
if (err) reject(new Error('There was an error while checking the token: ', err));

const report = JSON.parse(body);
let errors = new List();
const report = res.body;
let errors = new List();

if (!report.authorized) errors = errors.push('not-authorized');
if (!report.authorized) errors = errors.push('not-authorized');

resolve(errors);
});
resolve(errors);
});
});

export default TokenChecker;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"passport": "0.7.0",
"passport-http": "0.3",
"promise": "8.3.0",
"request": "2.88.2",
"specberus": "11.4.3",
"superagent": "10.1.0",
"tar-stream": "3.1.1",
"uuid": "11.0.3"
},
Expand Down
5 changes: 2 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ describe('DocumentDownloader', () => {
const notFound = DocumentDownloader.fetch(
`${server.location()}/et/si/tu/n/existais/pas`,
);

return expect(notFound).to.eventually.be.rejectedWith(/code 404/);
return expect(notFound).to.eventually.be.rejectedWith(/Not Found/);
});

it('should reject if the server is not reachable', () => {
Expand Down Expand Up @@ -241,7 +240,7 @@ describe('DocumentDownloader', () => {
'/tmp/whatever',
);

return expect(notFound).to.eventually.be.rejectedWith(/code 404/);
return expect(notFound).to.eventually.be.rejectedWith(/Not Found/);
});

it('should reject if the server is not reachable', () => {
Expand Down

0 comments on commit 08c445b

Please sign in to comment.