-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63ac6e2
commit 83927b3
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |