-
Notifications
You must be signed in to change notification settings - Fork 12
/
index_test.js
98 lines (90 loc) · 2.07 KB
/
index_test.js
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
/**
* Created by tdzl2003 on 2/28/16.
*/
var native;
try {
native = require('./build/Release/bsdiff');
} catch(e) {
console.error(e);
native = require('./build/Debug/bsdiff');
}
exports.native = native;
var compressjs = require("compressjs");
var algorithm = compressjs.Bzip2;
exports.diff = function(oldBuf, newBuf) {
var buffers = [];
native.diff(oldBuf, newBuf, function(output){
buffers.push(output);
});
var full = Buffer.concat(buffers);
// Generate bzip2 package with header.
var header = Buffer.alloc(32);
Buffer.from('ENDSLEY/BSDIFF43').copy(header, 0);
header.writeUInt32LE(newBuf.length, 16);
var buffers1 = [header];
// var compressed = algorithm.compressFile(full, null, 9);
// buffers1.push(compressed);
native.compress(full, function(output){
buffers1.push(output);
});
return Buffer.concat(buffers1);
}
function makeInStream(in_fd) {
var stream = new compressjs.Stream()
var stat = fs.fstatSync(in_fd)
if (stat.size) {
stream.size = stat.size
}
stream.buffer = Buffer.alloc(4096)
stream.filePos = null
stream.pos = 0
stream.end = 0
stream._fillBuffer = function() {
this.end = fs.readSync(
in_fd,
this.buffer,
0,
this.buffer.length,
this.filePos
)
this.pos = 0
if (this.filePos !== null && this.end > 0) {
this.filePos += this.end
}
}
stream.readByte = function() {
if (this.pos >= this.end) {
this._fillBuffer()
}
if (this.pos < this.end) {
return this.buffer[this.pos++]
}
return -1
}
stream.read = function(
buffer,
bufOffset,
length
) {
if (this.pos >= this.end) {
this._fillBuffer()
}
var bytesRead = 0
while (bytesRead < length && this.pos < this.end) {
buffer[bufOffset++] = this.buffer[this.pos++]
bytesRead++
}
return bytesRead
}
stream.seek = function(seek_pos) {
this.filePos = seek_pos
this.pos = this.end = 0
}
stream.eof = function() {
if (this.pos >= this.end) {
this._fillBuffer()
}
return this.pos >= this.end
}
return stream
}