Skip to content

Commit 04544e8

Browse files
authored
Merge pull request #6 from Speuta/master
node shell.download(...) use stream chunks and have a progress callback too
2 parents 5bbfbb8 + b23dc86 commit 04544e8

File tree

8 files changed

+221
-38
lines changed

8 files changed

+221
-38
lines changed

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare module shell {
3434
* @param url
3535
* @param dest
3636
*/
37-
const download: (url: string, dest: string, progressCallback? : (progress: any) => {}) => Promise<ShellEntry>;
37+
const download: (url: string, dest: string, progressCallback? : (progress: any) => void) => Promise<ShellEntry>;
3838
/**
3939
* Check if a file or a directory exists
4040
* @param url

dist/node-shell.js

Lines changed: 60 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node-shell.js.map

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-test/node-shell.js

Lines changed: 91 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ export default [{
3131
resolve(),
3232
commonjs()
3333
]
34-
}];
34+
}];

rollup.dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default [{
1818
input: 'src/node/index.ts',
1919
output: [{
2020
file: 'node-test/node-shell.js',
21-
format: 'commonjs',
21+
format: 'umd',
2222
sourcemap: 'inline',
2323
name : 'shell',
2424
}],
@@ -27,4 +27,4 @@ export default [{
2727
resolve(),
2828
commonjs(),
2929
]
30-
}];
30+
}];

src/cordova/download.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {mkdir} from "./mkdir";
33
import * as log from "./log/index";
44
import {ShellEntry} from "../ShellEntry";
55

6-
export const download = (url : string, dest : string, progressCallback? : (progress: any) => {}) : Promise<ShellEntry> => {
6+
export const download = (url : string, dest : string, progressCallback? : (progress: any) => void) : Promise<ShellEntry> => {
77
return new Promise((resolve, reject) => {
88
let extract = extractFileName(dest);
99
let newName = extract.file;
@@ -23,7 +23,7 @@ export const download = (url : string, dest : string, progressCallback? : (progr
2323
});
2424
};
2525

26-
const launchDownloadStreamXHR = (url : string, entry : FileEntry, progressCallback? : (progress: any) => {}) :Promise<ShellEntry> => {
26+
const launchDownloadStreamXHR = (url : string, entry : FileEntry, progressCallback? : (progress: any) => void) :Promise<ShellEntry> => {
2727
return new Promise((resolve, reject) => {
2828
let xhr = new XMLHttpRequest();
2929
xhr.open('GET', url, true);
@@ -65,7 +65,7 @@ const launchDownloadStreamXHR = (url : string, entry : FileEntry, progressCallba
6565
});
6666
};
6767

68-
const writeFile = (entry : FileEntry, data : Blob, progressCallback? : (progress: any) => {}) :Promise<ShellEntry> => {
68+
const writeFile = (entry : FileEntry, data : Blob, progressCallback? : (progress: any) => void) :Promise<ShellEntry> => {
6969
return new Promise((resolve, reject) => {
7070
entry.createWriter((fileWriter : FileWriter) => {
7171
fileWriter.onerror = (err) => {

src/node/download.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,72 @@
11
import {ShellEntry} from "../ShellEntry";
2-
const http = require('http');
32
const fs = require('fs');
4-
const fetch = require('node-fetch');
53
const path = require('path');
64
import {getEntry} from "./utils/getEntry";
75
import * as log from './log';
86
import {mkdir} from "./mkdir";
97

10-
export const download = (url : string, dest : string) : Promise<ShellEntry> => {
8+
export const download = (url : string, dest : string, progressCallback? : (progress: any) => void) : Promise<ShellEntry> => {
119
return new Promise((resolve, reject) => {
12-
fetch(url).then((res) => {
13-
// dest could be deep folder
1410
let parent = path.dirname(dest);
1511
mkdir(parent).then(() => {
16-
const writeStream = fs.createWriteStream(dest);
17-
res.body.pipe(writeStream);
18-
getEntry(dest).then(entry => {
19-
log.info("file downloaded");
20-
resolve(entry);
21-
}, reject);
12+
launchDownloadStreamXHR(url, dest, progressCallback).then((file) => {
13+
resolve(file);
14+
}).catch((err) => {
15+
reject(err);
16+
});
2217
}, reject);
23-
}, reject);
2418
});
25-
}
19+
};
20+
21+
const launchDownloadStreamXHR = (url : string, dest : string, progressCallback? : (progress: any) => void) :Promise<ShellEntry> => {
22+
return new Promise((resolve, reject) => {
23+
let xhr = new XMLHttpRequest();
24+
let fileStream = fs.createWriteStream(dest);
25+
xhr.open('GET', url, true);
26+
xhr.responseType = 'arraybuffer';
27+
xhr.onerror = (err) => {
28+
reject(err);
29+
};
30+
if(typeof progressCallback === 'function'){
31+
xhr.onprogress = (e) => {
32+
if(e.lengthComputable){
33+
progressCallback({
34+
action: 'downloading',
35+
loaded: e.loaded,
36+
total: e.total,
37+
percentDownloading: (e.loaded > 0) ? ((e.loaded / e.total) * 100) : 0,
38+
percent: (e.loaded > 0) ? ((e.loaded / e.total) * 50) : 0,
39+
});
40+
}
41+
};
42+
}
43+
xhr.onload = function() {
44+
if (this.status == 200) {
45+
progressCallback({
46+
action: 'downloading',
47+
loaded: this.response.byteLength,
48+
total: this.response.byteLength,
49+
percentDownloading: 100,
50+
percent: 50,
51+
});
52+
fileStream.write(Buffer.from(this.response));
53+
fileStream.end(() => {
54+
getEntry(dest).then(entry => {
55+
log.info("file downloaded");
56+
if(typeof progressCallback === 'function'){
57+
progressCallback({
58+
action: 'writing',
59+
written: entry.size,
60+
total: entry.size,
61+
percentWriting: 100,
62+
percent: 100,
63+
});
64+
resolve(entry);
65+
}
66+
}, reject);
67+
});
68+
}
69+
};
70+
xhr.send();
71+
});
72+
};

0 commit comments

Comments
 (0)