$
This commit is contained in:
607
node_modules/websocket-extensions/lib/pipeline/README.md
generated
vendored
Normal file
607
node_modules/websocket-extensions/lib/pipeline/README.md
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
53
node_modules/websocket-extensions/lib/pipeline/cell.js
generated
vendored
Normal file
53
node_modules/websocket-extensions/lib/pipeline/cell.js
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
var Functor = require('./functor'),
|
||||
Pledge = require('./pledge');
|
||||
|
||||
var Cell = function(tuple) {
|
||||
this._ext = tuple[0];
|
||||
this._session = tuple[1];
|
||||
|
||||
this._functors = {
|
||||
incoming: new Functor(this._session, 'processIncomingMessage'),
|
||||
outgoing: new Functor(this._session, 'processOutgoingMessage')
|
||||
};
|
||||
};
|
||||
|
||||
Cell.prototype.pending = function(direction) {
|
||||
var functor = this._functors[direction];
|
||||
if (!functor._stopped) functor.pending += 1;
|
||||
};
|
||||
|
||||
Cell.prototype.incoming = function(error, message, callback, context) {
|
||||
this._exec('incoming', error, message, callback, context);
|
||||
};
|
||||
|
||||
Cell.prototype.outgoing = function(error, message, callback, context) {
|
||||
this._exec('outgoing', error, message, callback, context);
|
||||
};
|
||||
|
||||
Cell.prototype.close = function() {
|
||||
this._closed = this._closed || new Pledge();
|
||||
this._doClose();
|
||||
return this._closed;
|
||||
};
|
||||
|
||||
Cell.prototype._exec = function(direction, error, message, callback, context) {
|
||||
this._functors[direction].call(error, message, function(err, msg) {
|
||||
if (err) err.message = this._ext.name + ': ' + err.message;
|
||||
callback.call(context, err, msg);
|
||||
this._doClose();
|
||||
}, this);
|
||||
};
|
||||
|
||||
Cell.prototype._doClose = function() {
|
||||
var fin = this._functors.incoming,
|
||||
fout = this._functors.outgoing;
|
||||
|
||||
if (!this._closed || fin.pending + fout.pending !== 0) return;
|
||||
if (this._session) this._session.close();
|
||||
this._session = null;
|
||||
this._closed.done();
|
||||
};
|
||||
|
||||
module.exports = Cell;
|
||||
72
node_modules/websocket-extensions/lib/pipeline/functor.js
generated
vendored
Normal file
72
node_modules/websocket-extensions/lib/pipeline/functor.js
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
|
||||
var RingBuffer = require('./ring_buffer');
|
||||
|
||||
var Functor = function(session, method) {
|
||||
this._session = session;
|
||||
this._method = method;
|
||||
this._queue = new RingBuffer(Functor.QUEUE_SIZE);
|
||||
this._stopped = false;
|
||||
this.pending = 0;
|
||||
};
|
||||
|
||||
Functor.QUEUE_SIZE = 8;
|
||||
|
||||
Functor.prototype.call = function(error, message, callback, context) {
|
||||
if (this._stopped) return;
|
||||
|
||||
var record = { error: error, message: message, callback: callback, context: context, done: false },
|
||||
called = false,
|
||||
self = this;
|
||||
|
||||
this._queue.push(record);
|
||||
|
||||
if (record.error) {
|
||||
record.done = true;
|
||||
this._stop();
|
||||
return this._flushQueue();
|
||||
}
|
||||
|
||||
var handler = function(err, msg) {
|
||||
if (!(called ^ (called = true))) return;
|
||||
|
||||
if (err) {
|
||||
self._stop();
|
||||
record.error = err;
|
||||
record.message = null;
|
||||
} else {
|
||||
record.message = msg;
|
||||
}
|
||||
|
||||
record.done = true;
|
||||
self._flushQueue();
|
||||
};
|
||||
|
||||
try {
|
||||
this._session[this._method](message, handler);
|
||||
} catch (err) {
|
||||
handler(err);
|
||||
}
|
||||
};
|
||||
|
||||
Functor.prototype._stop = function() {
|
||||
this.pending = this._queue.length;
|
||||
this._stopped = true;
|
||||
};
|
||||
|
||||
Functor.prototype._flushQueue = function() {
|
||||
var queue = this._queue, record;
|
||||
|
||||
while (queue.length > 0 && queue.peek().done) {
|
||||
record = queue.shift();
|
||||
if (record.error) {
|
||||
this.pending = 0;
|
||||
queue.clear();
|
||||
} else {
|
||||
this.pending -= 1;
|
||||
}
|
||||
record.callback.call(record.context, record.error, record.message);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Functor;
|
||||
47
node_modules/websocket-extensions/lib/pipeline/index.js
generated
vendored
Normal file
47
node_modules/websocket-extensions/lib/pipeline/index.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
var Cell = require('./cell'),
|
||||
Pledge = require('./pledge');
|
||||
|
||||
var Pipeline = function(sessions) {
|
||||
this._cells = sessions.map(function(session) { return new Cell(session) });
|
||||
this._stopped = { incoming: false, outgoing: false };
|
||||
};
|
||||
|
||||
Pipeline.prototype.processIncomingMessage = function(message, callback, context) {
|
||||
if (this._stopped.incoming) return;
|
||||
this._loop('incoming', this._cells.length - 1, -1, -1, message, callback, context);
|
||||
};
|
||||
|
||||
Pipeline.prototype.processOutgoingMessage = function(message, callback, context) {
|
||||
if (this._stopped.outgoing) return;
|
||||
this._loop('outgoing', 0, this._cells.length, 1, message, callback, context);
|
||||
};
|
||||
|
||||
Pipeline.prototype.close = function(callback, context) {
|
||||
this._stopped = { incoming: true, outgoing: true };
|
||||
|
||||
var closed = this._cells.map(function(a) { return a.close() });
|
||||
if (callback)
|
||||
Pledge.all(closed).then(function() { callback.call(context) });
|
||||
};
|
||||
|
||||
Pipeline.prototype._loop = function(direction, start, end, step, message, callback, context) {
|
||||
var cells = this._cells,
|
||||
n = cells.length,
|
||||
self = this;
|
||||
|
||||
while (n--) cells[n].pending(direction);
|
||||
|
||||
var pipe = function(index, error, msg) {
|
||||
if (index === end) return callback.call(context, error, msg);
|
||||
|
||||
cells[index][direction](error, msg, function(err, m) {
|
||||
if (err) self._stopped[direction] = true;
|
||||
pipe(index + step, err, m);
|
||||
});
|
||||
};
|
||||
pipe(start, null, message);
|
||||
};
|
||||
|
||||
module.exports = Pipeline;
|
||||
37
node_modules/websocket-extensions/lib/pipeline/pledge.js
generated
vendored
Normal file
37
node_modules/websocket-extensions/lib/pipeline/pledge.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
var RingBuffer = require('./ring_buffer');
|
||||
|
||||
var Pledge = function() {
|
||||
this._complete = false;
|
||||
this._callbacks = new RingBuffer(Pledge.QUEUE_SIZE);
|
||||
};
|
||||
|
||||
Pledge.QUEUE_SIZE = 4;
|
||||
|
||||
Pledge.all = function(list) {
|
||||
var pledge = new Pledge(),
|
||||
pending = list.length,
|
||||
n = pending;
|
||||
|
||||
if (pending === 0) pledge.done();
|
||||
|
||||
while (n--) list[n].then(function() {
|
||||
pending -= 1;
|
||||
if (pending === 0) pledge.done();
|
||||
});
|
||||
return pledge;
|
||||
};
|
||||
|
||||
Pledge.prototype.then = function(callback) {
|
||||
if (this._complete) callback();
|
||||
else this._callbacks.push(callback);
|
||||
};
|
||||
|
||||
Pledge.prototype.done = function() {
|
||||
this._complete = true;
|
||||
var callbacks = this._callbacks, callback;
|
||||
while (callback = callbacks.shift()) callback();
|
||||
};
|
||||
|
||||
module.exports = Pledge;
|
||||
66
node_modules/websocket-extensions/lib/pipeline/ring_buffer.js
generated
vendored
Normal file
66
node_modules/websocket-extensions/lib/pipeline/ring_buffer.js
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
var RingBuffer = function(bufferSize) {
|
||||
this._bufferSize = bufferSize;
|
||||
this.clear();
|
||||
};
|
||||
|
||||
RingBuffer.prototype.clear = function() {
|
||||
this._buffer = new Array(this._bufferSize);
|
||||
this._ringOffset = 0;
|
||||
this._ringSize = this._bufferSize;
|
||||
this._head = 0;
|
||||
this._tail = 0;
|
||||
this.length = 0;
|
||||
};
|
||||
|
||||
RingBuffer.prototype.push = function(value) {
|
||||
var expandBuffer = false,
|
||||
expandRing = false;
|
||||
|
||||
if (this._ringSize < this._bufferSize) {
|
||||
expandBuffer = (this._tail === 0);
|
||||
} else if (this._ringOffset === this._ringSize) {
|
||||
expandBuffer = true;
|
||||
expandRing = (this._tail === 0);
|
||||
}
|
||||
|
||||
if (expandBuffer) {
|
||||
this._tail = this._bufferSize;
|
||||
this._buffer = this._buffer.concat(new Array(this._bufferSize));
|
||||
this._bufferSize = this._buffer.length;
|
||||
|
||||
if (expandRing)
|
||||
this._ringSize = this._bufferSize;
|
||||
}
|
||||
|
||||
this._buffer[this._tail] = value;
|
||||
this.length += 1;
|
||||
if (this._tail < this._ringSize) this._ringOffset += 1;
|
||||
this._tail = (this._tail + 1) % this._bufferSize;
|
||||
};
|
||||
|
||||
RingBuffer.prototype.peek = function() {
|
||||
if (this.length === 0) return void 0;
|
||||
return this._buffer[this._head];
|
||||
};
|
||||
|
||||
RingBuffer.prototype.shift = function() {
|
||||
if (this.length === 0) return void 0;
|
||||
|
||||
var value = this._buffer[this._head];
|
||||
this._buffer[this._head] = void 0;
|
||||
this.length -= 1;
|
||||
this._ringOffset -= 1;
|
||||
|
||||
if (this._ringOffset === 0 && this.length > 0) {
|
||||
this._head = this._ringSize;
|
||||
this._ringOffset = this.length;
|
||||
this._ringSize = this._bufferSize;
|
||||
} else {
|
||||
this._head = (this._head + 1) % this._ringSize;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
module.exports = RingBuffer;
|
||||
Reference in New Issue
Block a user