@sendsafely/sendsafely 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/FileUtil.js +38 -47
- package/package.json +1 -1
package/lib/FileUtil.js
CHANGED
|
@@ -23,13 +23,9 @@ function FileUtil(param) {
|
|
|
23
23
|
this.SEGMENT_SIZE = 2621440;
|
|
24
24
|
this.filePath = param.filePath;
|
|
25
25
|
this.callback = param.callback;
|
|
26
|
-
this.file = {size: 0, name: '', totalParts: 0};
|
|
27
|
-
this.readableStream = fs.createReadStream(myself.filePath);
|
|
28
|
-
this.reading = false;
|
|
29
26
|
this.eof = false;
|
|
30
27
|
this.data = [];
|
|
31
28
|
this.tempSize = 0;
|
|
32
|
-
this.part = 1;
|
|
33
29
|
|
|
34
30
|
this.init = function() {
|
|
35
31
|
return new Promise(function(resolve) {
|
|
@@ -37,20 +33,9 @@ function FileUtil(param) {
|
|
|
37
33
|
if (err) {
|
|
38
34
|
throw new Error('FileUtil: File does not exist, ' + myself.filePath);
|
|
39
35
|
} else {
|
|
40
|
-
myself.
|
|
41
|
-
myself.
|
|
42
|
-
|
|
43
|
-
if(myself.file.size > (myself.SEGMENT_SIZE/4)) {
|
|
44
|
-
myself.file.totalParts = 1 + Math.ceil((myself.file.size-(myself.SEGMENT_SIZE/4))/myself.SEGMENT_SIZE);
|
|
45
|
-
} else {
|
|
46
|
-
myself.file.totalParts = 1;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
resolve(myself.file);
|
|
50
|
-
|
|
51
|
-
myself.readableStream.on('readable', function() {
|
|
52
|
-
// keep reading chunk until it reaches SEGMENT_SIZE
|
|
53
|
-
if(myself.reading && !myself.eof) {
|
|
36
|
+
myself.readableStream = fs.createReadStream(myself.filePath);
|
|
37
|
+
myself.readableStream.on('readable', function () {
|
|
38
|
+
if (myself.waitingForRead) {
|
|
54
39
|
processChunk();
|
|
55
40
|
}
|
|
56
41
|
});
|
|
@@ -58,15 +43,19 @@ function FileUtil(param) {
|
|
|
58
43
|
myself.readableStream.on('end', function() {
|
|
59
44
|
// done reading file
|
|
60
45
|
myself.eof = true;
|
|
61
|
-
if(myself.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// increment extra part if file size is less than segment/4
|
|
65
|
-
// so that we callback with an empty array
|
|
66
|
-
myself.part++;
|
|
67
|
-
callback(false);
|
|
46
|
+
if (myself.waitingForRead) {
|
|
47
|
+
// Handle timing edge case where 'end' fires while a read is pending
|
|
48
|
+
processChunk();
|
|
68
49
|
}
|
|
69
50
|
});
|
|
51
|
+
|
|
52
|
+
const totalParts = stats.size === 0 ? 1 : Math.ceil(stats.size / myself.SEGMENT_SIZE);
|
|
53
|
+
|
|
54
|
+
resolve({
|
|
55
|
+
name: path.basename(myself.filePath),
|
|
56
|
+
size: stats.size,
|
|
57
|
+
totalParts: totalParts,
|
|
58
|
+
});
|
|
70
59
|
}
|
|
71
60
|
});
|
|
72
61
|
|
|
@@ -74,34 +63,36 @@ function FileUtil(param) {
|
|
|
74
63
|
}
|
|
75
64
|
|
|
76
65
|
this.read = function() {
|
|
77
|
-
if(
|
|
78
|
-
|
|
79
|
-
processChunk();
|
|
80
|
-
} else if(myself.part === myself.file.totalParts) {
|
|
81
|
-
//extra part padding with empty array if file size is less than segment/4
|
|
82
|
-
callback(true);
|
|
66
|
+
if (myself.eof && myself.tempSize === 0) {
|
|
67
|
+
return;
|
|
83
68
|
}
|
|
69
|
+
|
|
70
|
+
myself.waitingForRead = true;
|
|
71
|
+
processChunk();
|
|
84
72
|
}
|
|
85
73
|
|
|
86
74
|
function processChunk() {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
} else {
|
|
92
|
-
let chunk = myself.readableStream.read();
|
|
93
|
-
if(chunk !== null) {
|
|
94
|
-
myself.data.push(new Uint8Array(chunk));
|
|
95
|
-
myself.tempSize += chunk.length;
|
|
96
|
-
}
|
|
75
|
+
let chunk;
|
|
76
|
+
while ((chunk = myself.readableStream.read()) !== null) { // Completely drain the stream's internal buffer
|
|
77
|
+
myself.data.push(new Uint8Array(chunk));
|
|
78
|
+
myself.tempSize += chunk.length;
|
|
97
79
|
}
|
|
98
|
-
}
|
|
99
80
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
81
|
+
if (myself.tempSize >= myself.SEGMENT_SIZE || myself.eof) {
|
|
82
|
+
if (myself.tempSize > 0) {
|
|
83
|
+
// We have a full segment
|
|
84
|
+
const buf = concatenate(myself.data);
|
|
85
|
+
const seg = buf.slice(0, myself.SEGMENT_SIZE);
|
|
86
|
+
const remainder = buf.slice(myself.SEGMENT_SIZE);
|
|
87
|
+
myself.data = remainder.length > 0 ? [remainder] : [];
|
|
88
|
+
myself.tempSize = remainder.length;
|
|
89
|
+
myself.callback({data: seg, complete: myself.eof && myself.tempSize === 0});
|
|
90
|
+
} else {
|
|
91
|
+
// Emit an empty final segment to signal completion & handles empty files
|
|
92
|
+
myself.callback({data: new Uint8Array(0), complete: true});
|
|
93
|
+
}
|
|
94
|
+
myself.waitingForRead = false;
|
|
95
|
+
}
|
|
105
96
|
}
|
|
106
97
|
|
|
107
98
|
function concatenate(arrays) {
|