-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
50 lines (38 loc) · 1.23 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
var rados = require('bindings')('rados');
var ReadableStream = require('stream').Readable;
var WritableStream = require('stream').Writable;
rados.Ioctx.prototype.createReadStream = function(oid, options) {
var ioctx = this;
var stream = new ReadableStream(options);
stream._offset = 0;
stream._read = function(size) {
ioctx.aio_read(oid, size, stream._offset, function(err, chunk) {
if (err) return stream.emit('error', err);
stream._offset += chunk.length;
stream.push((chunk.length > 0) ? chunk : null);
});
};
return stream;
}
rados.Ioctx.prototype.createWriteStream = function(oid, options) {
var ioctx = this;
var stream = new WritableStream(oid, options);
stream._offset = 0;
stream._write = function(data, encoding, cb) {
if (!(data instanceof Buffer))
return this.emit('error', new Error('Invalid data'));
ioctx.aio_write(oid, data, data.length, stream._offset, function(err) {
if (err) return stream.emit('error', err);
stream._offset += data.length;
cb();
});
};
stream.flush = function(cb) {
ioctx.aio_flush_async(function (err) {
if (err) stream.emit('error', err);
cb();
});
}
return stream;
}
exports = module.exports = rados;