-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (68 loc) · 2.16 KB
/
index.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
/// <reference path="../types/node.d.ts" />
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var stream = require('stream');
var util = require('util');
var Writable = stream.Writable;
var fs = require('fs');
var streamBuffers = require('stream-buffers');
var BaseStream = (function () {
function BaseStream() {
}
return BaseStream;
})();
util.inherits(BaseStream, Writable);
var FileStream = (function (_super) {
__extends(FileStream, _super);
function FileStream(filePath, options) {
var bufferOptions;
if (options) {
bufferOptions = options.bufferOptions;
}
_super.call(this);
Writable.call(this, options);
this.filePath = filePath;
this.buffer = new streamBuffers.WritableStreamBuffer(bufferOptions);
}
FileStream.prototype.flushToDisk = function (done) {
var self = this;
fs.open(this.filePath, 'w', function (err, fd) {
if (err) {
done(err);
} else {
var length = self.buffer.size();
var buffer = self.buffer.getContents();
fs.write(fd, buffer, 0, length, null, function (err) {
if (err) {
done(err);
} else {
fs.close(fd, function () {
done();
});
}
});
}
});
};
FileStream.prototype.end = function () {
var args = Array.prototype.slice.call(arguments, 0);
var self = this;
self.flushToDisk(function (err) {
if (err) {
self.emit("error", err);
} else {
self.emit.apply(self, ['finish'].concat(args));
}
});
};
FileStream.prototype._write = function (chunk, encoding, callback) {
this.buffer.write(chunk, encoding);
callback();
};
return FileStream;
})(BaseStream);
module.exports = FileStream;