Skip to content

Commit

Permalink
Experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 14, 2018
1 parent 63ac6e2 commit 83927b3
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
32 changes: 32 additions & 0 deletions fetchtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

// Modules
const fs = require('fs');

// Imports
const fetch = require('./lib/');

// Run
const stream = fetch('http://releases.ubuntu.com/16.04.2/ubuntu-16.04.2-desktop-amd64.iso', {
//length: 100 * 1024 * 1024,
//offset: 0,
attempts: 50,
log: (str, obj) => console.log(new Date(), str, obj)
});

let total = 0;

stream.on('error', err => console.log('ERROR:', err));
stream.on('data', chunk => {
total += chunk.length;
console.log(`Received ${chunk.length} bytes, ${total} (${total / 1024}K) total`);
});
stream.on('end', () => console.log(`END ${total} bytes (${total / 1024}K)`));

const writeStream = fs.createWriteStream('/Users/jim/Downloads/ubuntu.iso');
stream.pipe(writeStream);

//setTimeout(() => console.log('Timeout'), 60 * 60 * 1000); // 1 hour

// Cancel after 10 secs
setTimeout(() => stream.cancel(), 10 * 1000);
25 changes: 25 additions & 0 deletions fstest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

// Modules
const fs = require('fs');

const readStream = fs.createReadStream('/Users/jim/Downloads/ubuntu.iso');
const writeStream = fs.createWriteStream('/Users/jim/Downloads/ubuntu2.iso');

console.log('Piping');

readStream.pipe(writeStream);

readStream.on('open', () => console.log('Read stream opened'));
readStream.on('end', () => console.log('Read stream ended'));
readStream.on('close', () => console.log('Read stream closed'));
readStream.on('finish', () => console.log('Read stream finished'));
readStream.on('unpipe', () => console.log('Read stream unpiped'));
readStream.on('complete', () => console.log('Read stream completed'));
writeStream.on('end', () => console.log('Write stream ended'));
writeStream.on('close', () => console.log('Write stream closed'));
writeStream.on('finish', () => console.log('Write stream finished'));
writeStream.on('unpipe', () => console.log('Write stream unpiped'));
writeStream.on('complete', () => console.log('Write stream completed'));

setTimeout(() => console.log('Timeout'), 5 * 1000);
54 changes: 54 additions & 0 deletions gottest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

// Modules
const got = require('got');

let count = 0;
run();

function run() {
count++;
console.log(new Date(), `Starting attempt ${count}`);

let bytes = 0, timer, req, reqError = false;

const stream = got.stream('http://releases.ubuntu.com/16.04.2/ubuntu-16.04.2-desktop-amd64.iso', {
retries: 0,
timeout: {connect: 1000, socket: 1000, request: 1000}
});

stream.on('error', err => {
console.log(new Date(), 'Error', err);
if (timer) clearTimeout(timer);

if (err.message == 'Request timed out') {
reqError = true;
stream.emit('end');
}
});

stream.on('end', () => {
console.log(new Date(), 'Ended');
if (!reqError) setTimeout(run, 5000);
});

stream.on('request', _req => {
console.log(new Date(), 'Sent HTTP request');
req = _req;
});

stream.on('response', res => console.log(new Date(), 'headers', res.headers));

stream.on('data', chunk => {
bytes += chunk.length;
console.log(new Date(), `Received ${chunk.length} bytes, ${bytes} (${bytes / 1024}K) total`);
});

//const writeStream = fs.createWriteStream('/Users/jim/Downloads/ubuntu.iso');
//stream.pipe(writeStream);

timer = setTimeout(() => {
console.log(new Date(), 'Aborting');
req.abort();
}, 2000);
}
4 changes: 4 additions & 0 deletions lib/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function Transfer(options) {
this.position = this.options.offset;
this.total = undefined;
this.cancelled = false;
this.errors = [];

// Create output stream
this.stream = new streamModule.PassThrough();
Expand Down Expand Up @@ -295,6 +296,9 @@ Transfer.prototype.get = function() {
Transfer.prototype.failed = function(err, empty) {
if (this.log) this.log('Transfer failed', {err});

// Add error to errs array
this.errors.push(err);

// If transfer cancelled, emit error on stream and stop
if (this.cancelled) {
this.fatal();
Expand Down
31 changes: 31 additions & 0 deletions promisetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

// Modules
const Promise = require('bluebird'); // jshint ignore:line

// Imports
const gotResume = require('./lib/');

// Run
Promise.each([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], (n) => {
n = (n < 10 ? '0' : '') + n;
console.log(`ATTEMPT ${n}`);

return gotResume.toFile(`/Users/jim/Downloads/download test/ubuntu${n}.iso`, 'http://releases.ubuntu.com/16.04.2/ubuntu-16.04.2-desktop-amd64.iso', {
//length: 100 * 1024 * 1024,
//offset: 0,
//attempts: 10,
log: (str, obj) => console.log(new Date(), str, obj),
onProgress: progress => console.log(new Date(), 'Progress:', progress)
})
.then(() => console.log('DONE'))
.catch(err => {
console.log('ERROR', err);
throw err;
});
});

// Cancel after 3 secs
//setTimeout(() => promise.cancel(), 3 * 1000);

//setTimeout(() => console.log('TIMEOUT'), 20 * 1000);

0 comments on commit 83927b3

Please sign in to comment.