-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 971 Bytes
/
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
'use strict';
var es = require('event-stream'),
util = require('util'),
DEFAULT_READ_LENGTH = 64 * 1024; // no evidence for this value
exports.createWriteStream = function(options) {
var client = options.client;
return es.through(client.append.bind(client, options.key));
};
exports.createReadStream = function(options) {
var key = new Buffer(options.key),
client = options.client,
maxReadLength = options.maxReadLength || DEFAULT_READ_LENGTH,
current = 0;
return es.readable(function(count, callback) {
var that = this;
client.getrange(key, current, current + maxReadLength - 1, function(err, data) {
if (err) {
that.emit('error', err);
} else {
if (data.length === 0) {
// finished
that.emit('end');
} else {
current += data.length;
that.emit('data', data);
}
}
callback();
});
});
};
// vim: ts=2:sw=2:sts=2:expandtab: