node-build.js
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
var events = require('events');
var http = require('http');
var https = require('https');
var fs = require('fs');
function __extends(d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var Util = (function () {
function Util() {
}
Util.getNameHash = function (path) {
for (var r = 0, i = 0; i < path.length; i++) {
r = (r << 5) - r + path.charCodeAt(i), r &= r;
}
return "bsync_" + Math.abs(r);
};
/**
* index >= 0, localFile is in files
* index < 0, localFile is not in files
*/
Util.getFileIndex = function (files, localFile) {
for (var i = 0; i < files.length; i++) {
if (localFile == files[i].target) {
return i;
}
}
return -1;
};
Util.getFilesForCleanup = function (files, localFiles) {
var filesForCleanup = [];
for (var _i = 0, localFiles_1 = localFiles; _i < localFiles_1.length; _i++) {
var localFile = localFiles_1[_i];
var index = -1;
index = Util.getFileIndex(files, localFile);
if (index < 0) {
filesForCleanup.push(localFile);
}
else {
// splice for performance improvement only
files.splice(index, 1);
}
}
return filesForCleanup;
};
return Util;
}());
var NodeFileHandler = (function (_super) {
__extends(NodeFileHandler, _super);
function NodeFileHandler() {
_super.apply(this, arguments);
}
NodeFileHandler.prototype.selectProtocol = function (url) {
if (url.search(/^http:\/\//) === 0) {
return http;
}
else if (url.search(/^https:\/\//) === 0) {
return https;
}
else {
return null;
}
};
NodeFileHandler.prototype.download = function (source, target) {
var _this = this;
var handler = this.selectProtocol(source);
if (!handler) {
this.emit("error", "No handler for source: " + source);
return this;
}
// file already exists and is not empty
if (fs.existsSync(target) && (fs.statSync(target)['size'] > 0)) {
this.emit("complete");
return this;
}
else {
var file_1 = fs.createWriteStream(target, { 'flags': 'a' });
handler.get(source, function (response) {
var size = response.headers['content-length']; // in bytes
var prog = 0; // already downloaded
var progCounts = 100; // how many progress events should be triggerd (1-100 %)
var nextProg = (1 / progCounts);
response.on('data', function (chunk) {
prog += chunk.length;
file_1.write(chunk, 'binary');
if ((prog / size) > nextProg) {
_this.emit('progress', prog / size);
nextProg += (1 / progCounts);
}
});
response.once('end', function () {
file_1.end();
_this.emit('complete');
});
}).on('error', function (error) {
fs.unlink(target);
_this.emit("error", "Error while downloading: " + error);
});
return this;
}
};
NodeFileHandler.prototype.cleanup = function (files, basePath) {
try {
var localFiles = fs.readdirSync(basePath);
Util.getFilesForCleanup(files, localFiles)
.forEach(function (file) {
fs.unlinkSync(basePath + "/" + file);
});
}
catch (e) {
}
return this;
};
return NodeFileHandler;
}(events.EventEmitter));
var Bsync = (function () {
function Bsync() {
}
Bsync.configIpcMain = function (ipcMain, basePath) {
ipcMain.on('bsync-download', function (event, args) {
var nodeFileHander = new NodeFileHandler();
nodeFileHander.download(args.source, basePath + "/" + args.target)
.on('progress', function (progress) { event.sender.send('bsync-download-progress', progress); })
.once('error', function (error) { nodeFileHander.removeAllListeners(); event.sender.send('bsync-download-error', error); })
.once('complete', function () { nodeFileHander.removeAllListeners(); event.sender.send('bsync-download-complete'); });
});
ipcMain.on('bsync-cleanup', function (event, args) {
var nodeFileHandler = new NodeFileHandler();
nodeFileHandler.cleanup(args.files, basePath);
event.sender.send('bsync-cleanup-complete');
});
};
return Bsync;
}());
module.exports = Bsync;
//# sourceMappingURL=node-build.js.map