Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove request library #275

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const https = require('https');

const post = (url, data, cb) => {
if (!url) {
cb(new Error('request.post(url, data, cb): url must be non-empty.'));
return;
}

if (typeof url !== 'string') {
cb(new Error('request.post(url, data, cb): url must be a String.'));
return;
}

if (!data) {
cb(new Error('request.post(url, data, cb): data must be non-empty.'));
return;
}

if (typeof data !== 'string') {
cb(new Error('request.post(url, data, cb): data must be a String.'));
Copy link

@jimmywarting jimmywarting May 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise + throw instead? Do think this is kind of verbose

return;
}

const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
},
};

let errored = false;

const req = https.request(url, options, (res) => {
res.setEncoding('utf8');

const { statusCode } = res;
const body = [];

res.on('data', chunk => {
// Buffer.concat below will complain if the chunks are strings.
// this should only ever happen in the nock https mock,
// but let's make sure.
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk);
}

body.push(chunk);
});

res.on('end', () => {
if (!errored) {
cb(null, { statusCode }, Buffer.concat(body).toString());
}
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How i would have written it:

let body = ''
for await (const chunk of res) {
  body += chunk
}

cb(null, { statusCode }, body)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would also take the opportunity to use Promise instead

});

req.on('error', (e) => {
errored = true;
cb(e);
});

// Write data to request body
req.write(`json=${encodeURIComponent(data)}`);
req.end();
};

module.exports = {
post,
};
11 changes: 2 additions & 9 deletions lib/sendToCoveralls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const request = require('request');
const request = require('./request');
const index = require('..');

const sendToCoveralls = (obj, cb) => {
Expand All @@ -16,14 +16,7 @@ const sendToCoveralls = (obj, cb) => {
process.stdout.write(str);
cb(null, { statusCode: 200 }, '');
} else {
request.post({
url,
form: {
json: str
}
}, (err, response, body) => {
cb(err, response, body);
});
request.post(url, str, cb);
}
};

Expand Down
Loading