Skip to content

Commit

Permalink
expose createWriteStream
Browse files Browse the repository at this point in the history
  • Loading branch information
ksperis committed Mar 1, 2016
1 parent ba035f1 commit 5bf1a53
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand All @@ -19,4 +20,31 @@ rados.Ioctx.prototype.createReadStream = function(oid, options) {
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;

0 comments on commit 5bf1a53

Please sign in to comment.