forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsync_writestream.js
48 lines (41 loc) · 1.21 KB
/
fsync_writestream.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
"use strict";
var fs = require('fs');
var util = require('util');
module.exports = FsyncWriteStream;
function FsyncWriteStream (path, options) {
if (!(this instanceof FsyncWriteStream)) return new FsyncWriteStream(path, options);
fs.WriteStream.call(this, path, options);
}
util.inherits(FsyncWriteStream, fs.WriteStream);
FsyncWriteStream.prototype.close = function (cb) {
var self = this;
if (cb)
this.once('close', cb);
if (this.closed || 'number' !== typeof this.fd) {
if ('number' !== typeof this.fd) {
this.once('open', close);
return;
}
return setImmediate(this.emit.bind(this, 'close'));
}
this.closed = true;
close();
function close (fd) {
fs.fsync(fd || self.fd, function (er) {
if (er) {
self.emit('error', er);
}
else {
fs.close(fd || self.fd, function (err) {
if (err) {
self.emit('error', err);
}
else {
self.emit('close');
}
});
self.fd = null;
}
});
}
};