Skip to content

Commit

Permalink
fix feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Jul 2, 2017
1 parent a96067a commit 88fc78b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 58 deletions.
73 changes: 26 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const isURL = require('isurl');
const isPlainObj = require('is-plain-obj');
const PCancelable = require('p-cancelable');
const pTimeout = require('p-timeout');
const pify = require('pify');
const pkg = require('./package');

const getMethodRedirectCodes = new Set([300, 301, 302, 303, 304, 305, 307, 308]);
Expand All @@ -33,55 +34,31 @@ const isFormData = body => isStream(body) && typeof body.getBoundary === 'functi
const getBodySize = opts => {
const body = opts.body;

return new Promise((resolve, reject) => {
if (opts.headers['content-length']) {
resolve(Number(opts.headers['content-length']));
return;
}

if (!body && !opts.stream) {
resolve(0);
return;
}

if (typeof body === 'string') {
resolve(Buffer.byteLength(body));
return;
}

if (isFormData(body)) {
body.getLength((err, size) => {
if (err) {
reject(err);
return;
}

resolve(size);
});
if (opts.headers['content-length']) {
return Promise.resolve(Number(opts.headers['content-length']));
}

return;
}
if (!body && !opts.stream) {
return Promise.resolve(0);
}

if (body instanceof fs.ReadStream) {
fs.stat(body.path, (err, stat) => {
if (err) {
reject(err);
return;
}
if (typeof body === 'string') {
return Promise.resolve(Buffer.byteLength(body));
}

resolve(stat.size);
});
if (isFormData(body)) {
return pify(body.getLength.bind(body))();
}

return;
}
if (body instanceof fs.ReadStream) {
return pify(fs.stat)(body.path).then(stat => stat.size);
}

if (isStream(body) && Buffer.isBuffer(body._buffer)) {
resolve(body._buffer.length);
return;
}
if (isStream(body) && Buffer.isBuffer(body._buffer)) {
return Promise.resolve(body._buffer.length);
}

resolve(null);
});
return Promise.resolve(null);
};

function requestAsEventEmitter(opts) {
Expand Down Expand Up @@ -234,6 +211,8 @@ function requestAsEventEmitter(opts) {
});

req.connection.on('connect', () => {
const uploadEventFrequency = 150;

progressInterval = setInterval(() => {
const lastUploaded = uploaded;
const headersSize = Buffer.byteLength(req._header);
Expand All @@ -256,7 +235,7 @@ function requestAsEventEmitter(opts) {
transferred: uploaded,
total: uploadBodySize
});
}, 150);
}, uploadEventFrequency);
});
});

Expand All @@ -274,7 +253,6 @@ function requestAsEventEmitter(opts) {
getBodySize(opts)
.then(size => {
uploadBodySize = size;

get(opts);
})
.catch(err => {
Expand Down Expand Up @@ -358,14 +336,15 @@ function asPromise(opts) {

promise.on = (name, fn) => {
proxy.on(name, fn);

return promise;
};

return promise;
}

function asStream(opts) {
opts.stream = true;

const input = new PassThrough();
const output = new PassThrough();
const proxy = duplexer3(input, output);
Expand Down Expand Up @@ -569,7 +548,7 @@ function got(url, opts) {
}
}

got.stream = (url, opts) => asStream(Object.assign({}, normalizeArguments(url, opts), {stream: true}));
got.stream = (url, opts) => asStream(normalizeArguments(url, opts));

const methods = [
'get',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"mimic-response": "^1.0.0",
"p-cancelable": "^0.3.0",
"p-timeout": "^1.1.1",
"pify": "^3.0.0",
"safe-buffer": "^5.0.1",
"timed-out": "^4.0.0",
"url-parse-lax": "^1.0.0",
Expand All @@ -74,7 +75,6 @@
"get-port": "^3.0.0",
"nyc": "^11.0.2",
"pem": "^1.4.4",
"pify": "^3.0.0",
"slow-stream": "0.0.4",
"tempfile": "^2.0.0",
"tempy": "^0.1.0",
Expand Down
17 changes: 7 additions & 10 deletions test/progress.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import SlowStream from 'slow-stream';
import intoStream from 'into-stream';
import getStream from 'get-stream';
import FormData from 'form-data';
import tempfile from 'tempfile';
import pify from 'pify';
Expand All @@ -18,7 +19,7 @@ const checkEvents = (t, events, bodySize = null) => {
t.is(lastEvent.percent, 0);
}

events.forEach((event, index) => {
for (const [index, event] of events.entries()) {
if (hasBodySize) {
t.is(event.percent, event.transferred / bodySize);
t.true(event.percent > lastEvent.percent);
Expand All @@ -31,7 +32,7 @@ const checkEvents = (t, events, bodySize = null) => {
t.is(event.total, bodySize);

lastEvent = event;
});
}
};

const file = Buffer.alloc(1024 * 1024 * 2);
Expand Down Expand Up @@ -157,19 +158,15 @@ test.cb('upload progress - stream with known body size', t => {
});
});

test.cb('upload progress - stream with unknown body size', t => {
test('upload progress - stream with unknown body size', async t => {
const events = [];

const req = got.stream.post(`${s.url}/upload`)
.on('uploadProgress', e => events.push(e));

intoStream(file)
.pipe(req)
.on('data', () => {})
.on('end', () => {
checkEvents(t, events);
t.end();
});
await getStream(intoStream(file).pipe(req));

checkEvents(t, events);
});

test('upload progress - no body', async t => {
Expand Down

0 comments on commit 88fc78b

Please sign in to comment.