[Updater > Request] Clean up source further
This commit is contained in:
parent
8b2ef7cfd5
commit
8f77c6d363
1 changed files with 12 additions and 37 deletions
|
@ -1,36 +1,23 @@
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
|
|
||||||
const nodeRequest = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve, reject) => {
|
const nodeRequest = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve, reject) => {
|
||||||
const req = request({
|
const req = request({ method, url, headers, qs, timeout: timeout ?? 30000, body });
|
||||||
method,
|
|
||||||
url,
|
|
||||||
qs,
|
|
||||||
headers,
|
|
||||||
followAllRedirects: true,
|
|
||||||
encoding: null,
|
|
||||||
timeout: timeout ?? 30000,
|
|
||||||
body
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('response', (response) => {
|
req.on('response', (response) => {
|
||||||
const totalBytes = parseInt(response.headers['content-length'] || 1, 10);
|
const total = parseInt(response.headers['content-length'] || 1, 10);
|
||||||
let receivedBytes = 0;
|
let outOf = 0;
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
|
|
||||||
const badStatus = response.statusCode >= 300;
|
const badStatus = response.statusCode >= 300;
|
||||||
if (badStatus) stream = null;
|
if (badStatus) stream = null;
|
||||||
|
|
||||||
response.on('data', chunk => {
|
response.on('data', chunk => {
|
||||||
if (stream != null) {
|
|
||||||
receivedBytes += chunk.length;
|
|
||||||
stream.write(chunk);
|
|
||||||
return stream.emit('progress', {
|
|
||||||
totalBytes,
|
|
||||||
receivedBytes
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
|
|
||||||
|
if (!stream) return;
|
||||||
|
outOf += chunk.length;
|
||||||
|
stream.write(chunk);
|
||||||
|
stream.emit('progress', { total, outOf });
|
||||||
});
|
});
|
||||||
|
|
||||||
response.on('end', () => {
|
response.on('end', () => {
|
||||||
|
@ -39,26 +26,14 @@ const nodeRequest = ({ method, url, headers, qs, timeout, body, stream }) => new
|
||||||
return stream.end();
|
return stream.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (badStatus) {
|
if (badStatus) return reject(new Error('Req error:' + response.statusCode));
|
||||||
const err = new Error('HTTP Error: Status Code ' + response.statusCode);
|
|
||||||
err.response = response;
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve({
|
resolve({ ...response, body: Buffer.concat(chunks) });
|
||||||
...response,
|
|
||||||
body: Buffer.concat(chunks)
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', err => reject(err));
|
req.on('error', err => reject(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
const withMethod = (method, options) => {
|
const meth = (method, opt) => nodeRequest({ ...(typeof opt === 'string' ? { url: opt } : opt), method });
|
||||||
if (typeof options === 'string') options = { url: options };
|
exports.get = meth.bind(null, 'GET');
|
||||||
|
|
||||||
return nodeRequest({ ...options, method });
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.get = withMethod.bind(null, 'GET');
|
|
Loading…
Add table
Add a link
Reference in a new issue