Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

push => write for sys.pump(), lots of tests fail

  • Loading branch information...
commit 1989afce26dd7032f78b907f1bd486a6246634d8 1 parent 3204a4a
@substack authored
View
36 bufferlist.js
@@ -9,6 +9,7 @@ module.exports.BufferList = BufferList; // backwards compatibility
BufferList.prototype = new EventEmitter;
function BufferList(opts) {
if (!(this instanceof BufferList)) return new BufferList(opts);
+ var self = this;
if (typeof(opts) == 'undefined') opts = {}
@@ -32,24 +33,33 @@ function BufferList(opts) {
// keep an offset of the head to decide when to head = head.next
var offset = 0;
- // Push buffers to the end of the linked list.
+ // Write to the bufferlist. Emits 'write'. Always returns true.
+ self.write = function (buf) {
+ if (!head.buffer) {
+ head.buffer = buf;
+ last = head;
+ }
+ else {
+ last.next = { next : null, buffer : buf };
+ last = last.next;
+ }
+ length += buf.length;
+ self.emit('write', buf);
+ return true;
+ };
+
+ this.end = function (buf) {
+ if (buf instanceof Buffer) self.write(buf);
+ };
+
+ // Push buffers to the end of the linked list. (deprecated)
// Return this (self).
this.push = function () {
var args = [].concat.apply([], arguments);
args.forEach(function (buf) {
- if (!head.buffer) {
- head.buffer = buf;
- last = head;
- }
- else {
- last.next = { next : null, buffer : buf };
- last = last.next;
- }
- length += buf.length;
+ self.write(buf);
});
-
- this.emit('push', args);
- return this;
+ return self;
};
// For each buffer, perform some action.
View
37 lib/binary.js
@@ -8,9 +8,8 @@ module.exports.Binary = Binary; // backwards compatibility
Binary.prototype = new EventEmitter;
function Binary(buffer) {
if (!(this instanceof Binary)) return new Binary(buffer);
+ var self = this;
- var binary = this;
-
this.vars = {};
this.offset = 0;
this.actions = [];
@@ -18,8 +17,8 @@ function Binary(buffer) {
// an explicit end loads all the actions before any evaluation happens
this.end = function () {
- if (buffer.listeners('push').indexOf(update) < 0) {
- buffer.addListener('push', update);
+ if (buffer.listeners('write').indexOf(update) < 0) {
+ buffer.on('write', update);
}
update();
return this;
@@ -38,30 +37,30 @@ function Binary(buffer) {
};
function update () {
- var action = binary.actions[0];
+ var action = self.actions[0];
if (!action) {
- buffer.removeListener('push', update);
- binary.emit('end', binary.vars);
+ buffer.removeListener('write', update);
+ self.emit('end', self.vars);
}
- else if (action.ready.call(binary, binary.vars)) {
- binary.actions.shift();
+ else if (action.ready.call(self, self.vars)) {
+ self.actions.shift();
if (action.context == false) {
- action.action.call(binary, binary.vars);
- binary.end();
+ action.action.call(self, self.vars);
+ self.end();
}
else {
- buffer.removeListener('push', update);
+ buffer.removeListener('write', update);
var child = new Binary(buffer);
- child.vars = binary.vars;
- child.parent = binary;
- child.offset = binary.offset;
+ child.vars = self.vars;
+ child.parent = self;
+ child.offset = self.offset;
- child.addListener('end', function () {
- binary.offset = child.offset;
- buffer.addListener('push', update);
- binary.end();
+ child.on('end', function () {
+ self.offset = child.offset;
+ buffer.on('write', update);
+ self.end();
});
action.action.call(child, child.vars);
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "bufferlist",
- "version": "0.0.3",
+ "version": "0.0.4",
"description": "Create linked lists of Buffer objects",
"directories": { "lib": "./lib" },
"main": "./bufferlist",
View
12 test/advance.js
@@ -8,16 +8,16 @@ exports.advance = function (assert) {
var buf2 = new Buffer(3); buf2.write('xyz');
var buf3 = new Buffer(5); buf3.write('11358');
- b.push(buf1);
- assert.equal(b.take(b.length), 'abcde', 'pushed correctly');
+ b.write(buf1);
+ assert.equal(b.take(b.length), 'abcde', 'wrote correctly');
b.advance(3);
assert.equal(b.take(b.length), 'de', 'advanced with one buffer');
b.advance(3);
assert.equal(b.take(b.length), '', 'advanced one buffer past the end');
- b.push(buf2);
+ b.write(buf2);
assert.equal(b.take(b.length), 'yz', 'offset preserved past the end');
- b.push(buf3);
- assert.equal(b.take(b.length), 'yz11358', 'second push after advance');
+ b.write(buf3);
+ assert.equal(b.take(b.length), 'yz11358', 'second write after advance');
b.advance(4);
- assert.equal(b.take(b.length), '358', 'advance after two pushes');
+ assert.equal(b.take(b.length), '358', 'advance after two writes');
};
View
5 test/binary_event.js
@@ -5,9 +5,8 @@ var Binary = require('bufferlist/binary');
function parser (sock) {
var bufferList = new BufferList;
- sock.on('data', function (data) {
- bufferList.push(data);
- });
+ //sys.pump(sock, bufferList);
+ sock.on('data', function (buf) { bufferList.write(buf) });
return Binary(bufferList)
.getWord16be('xLen')
View
36 test/event.js
@@ -7,12 +7,13 @@ var BufferList = require('bufferlist');
exports.events = function (assert) {
var b = new BufferList;
var times = 0;
-
- b.addListener('push', function (args) {
+
+ var wrote = null;
+ b.addListener('write', function (buf) {
assert.equal(
- pushed.toString(),
- args.toString(),
- 'pushed callback gives its arguments'
+ wrote.toString(),
+ buf.toString(),
+ 'write callback gives its arguments'
);
times ++;
});
@@ -20,21 +21,20 @@ exports.events = function (assert) {
var buf1 = new Buffer(5); buf1.write('abcde');
var buf2 = new Buffer(3); buf2.write('xyz');
var buf3 = new Buffer(5); buf3.write('11358');
-
- var pushed = [buf1,buf2];
- b.push(buf1,buf2);
-
- assert.equal(times, 1, 'pushed once');
-
- var pushed = [buf3];
- b.push(buf3);
-
- assert.equal(times, 2, 'pushed two times');
-
+
+ var wrote = buf1; b.write(buf1);
+ var wrote = buf2; b.write(buf2);
+
+ assert.equal(times, 2, 'wrote twice');
+
+ var wrote = buf3; b.write(buf3);
+
+ assert.equal(times, 3, 'wrote thrice');
+
assert.equal(b.take(), 'abcdexyz11358', 'entire buffer check');
-
+
var advanced = 0;
- b.addListener('advance', function (n) {
+ b.on('advance', function (n) {
assert.equal(n, 3, 'n = 3 in advance callback')
advanced ++;
});
Please sign in to comment.
Something went wrong with that request. Please try again.