-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (52 loc) · 1.4 KB
/
index.js
File metadata and controls
57 lines (52 loc) · 1.4 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
var cp =require('child_process'),
Transform = require('stream').Transform;
class CompressionStream extends Transform {
constructor(options) {
super(options);
}
_transform(chunk, encoding, callback) {
var proc = cp.spawn('zstd', [ '-c' , '-']);
var self = this;
proc.stdout.on('data', (data) => {
self.push(data);
});
proc.stdout.on('finish', () => {
callback();
});
proc.stderr.on('error', (err) => {
self.push(null);
callback(err);
});
proc.stdin.write(chunk);
proc.stdin.end();
}
_flush() {
this.push(null);
}
}
class DecompressionStream extends Transform {
constructor(options) {
super(options);
this.proc = cp.spawn('zstd', ['-c','-d', '-']);
var self = this;
this.proc.stdout.on('data', (data) => {
self.push(data);
});
this.proc.on('exit', () => {
self.push(null);
});
}
_transform(chunk, encoding, callback) {
this.proc.stderr.on('error', (err) => {
self.push(null);
callback(err);
});
this.proc.stdin.write(chunk);
callback();
}
_flush() {
this.proc.stdin.end();
}
}
module.exports.CompressionStream = CompressionStream;
module.exports.DecompressionStream = DecompressionStream;