415gotit
This commit is contained in:
194
node_modules/msgpack-lite/lib/flex-buffer.js
generated
vendored
Normal file
194
node_modules/msgpack-lite/lib/flex-buffer.js
generated
vendored
Normal file
@@ -0,0 +1,194 @@
|
||||
// flex-buffer.js
|
||||
|
||||
exports.FlexDecoder = FlexDecoder;
|
||||
exports.FlexEncoder = FlexEncoder;
|
||||
|
||||
var Bufferish = require("./bufferish");
|
||||
|
||||
var MIN_BUFFER_SIZE = 2048;
|
||||
var MAX_BUFFER_SIZE = 65536;
|
||||
var BUFFER_SHORTAGE = "BUFFER_SHORTAGE";
|
||||
|
||||
function FlexDecoder() {
|
||||
if (!(this instanceof FlexDecoder)) return new FlexDecoder();
|
||||
}
|
||||
|
||||
function FlexEncoder() {
|
||||
if (!(this instanceof FlexEncoder)) return new FlexEncoder();
|
||||
}
|
||||
|
||||
FlexDecoder.mixin = mixinFactory(getDecoderMethods());
|
||||
FlexDecoder.mixin(FlexDecoder.prototype);
|
||||
|
||||
FlexEncoder.mixin = mixinFactory(getEncoderMethods());
|
||||
FlexEncoder.mixin(FlexEncoder.prototype);
|
||||
|
||||
function getDecoderMethods() {
|
||||
return {
|
||||
bufferish: Bufferish,
|
||||
write: write,
|
||||
fetch: fetch,
|
||||
flush: flush,
|
||||
push: push,
|
||||
pull: pull,
|
||||
read: read,
|
||||
reserve: reserve,
|
||||
offset: 0
|
||||
};
|
||||
|
||||
function write(chunk) {
|
||||
var prev = this.offset ? Bufferish.prototype.slice.call(this.buffer, this.offset) : this.buffer;
|
||||
this.buffer = prev ? (chunk ? this.bufferish.concat([prev, chunk]) : prev) : chunk;
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
function flush() {
|
||||
while (this.offset < this.buffer.length) {
|
||||
var start = this.offset;
|
||||
var value;
|
||||
try {
|
||||
value = this.fetch();
|
||||
} catch (e) {
|
||||
if (e && e.message != BUFFER_SHORTAGE) throw e;
|
||||
// rollback
|
||||
this.offset = start;
|
||||
break;
|
||||
}
|
||||
this.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
function reserve(length) {
|
||||
var start = this.offset;
|
||||
var end = start + length;
|
||||
if (end > this.buffer.length) throw new Error(BUFFER_SHORTAGE);
|
||||
this.offset = end;
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
function getEncoderMethods() {
|
||||
return {
|
||||
bufferish: Bufferish,
|
||||
write: write,
|
||||
fetch: fetch,
|
||||
flush: flush,
|
||||
push: push,
|
||||
pull: pull,
|
||||
read: read,
|
||||
reserve: reserve,
|
||||
send: send,
|
||||
maxBufferSize: MAX_BUFFER_SIZE,
|
||||
minBufferSize: MIN_BUFFER_SIZE,
|
||||
offset: 0,
|
||||
start: 0
|
||||
};
|
||||
|
||||
function fetch() {
|
||||
var start = this.start;
|
||||
if (start < this.offset) {
|
||||
var end = this.start = this.offset;
|
||||
return Bufferish.prototype.slice.call(this.buffer, start, end);
|
||||
}
|
||||
}
|
||||
|
||||
function flush() {
|
||||
while (this.start < this.offset) {
|
||||
var value = this.fetch();
|
||||
if (value) this.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
function pull() {
|
||||
var buffers = this.buffers || (this.buffers = []);
|
||||
var chunk = buffers.length > 1 ? this.bufferish.concat(buffers) : buffers[0];
|
||||
buffers.length = 0; // buffer exhausted
|
||||
return chunk;
|
||||
}
|
||||
|
||||
function reserve(length) {
|
||||
var req = length | 0;
|
||||
|
||||
if (this.buffer) {
|
||||
var size = this.buffer.length;
|
||||
var start = this.offset | 0;
|
||||
var end = start + req;
|
||||
|
||||
// is it long enough?
|
||||
if (end < size) {
|
||||
this.offset = end;
|
||||
return start;
|
||||
}
|
||||
|
||||
// flush current buffer
|
||||
this.flush();
|
||||
|
||||
// resize it to 2x current length
|
||||
length = Math.max(length, Math.min(size * 2, this.maxBufferSize));
|
||||
}
|
||||
|
||||
// minimum buffer size
|
||||
length = Math.max(length, this.minBufferSize);
|
||||
|
||||
// allocate new buffer
|
||||
this.buffer = this.bufferish.alloc(length);
|
||||
this.start = 0;
|
||||
this.offset = req;
|
||||
return 0;
|
||||
}
|
||||
|
||||
function send(buffer) {
|
||||
var length = buffer.length;
|
||||
if (length > this.minBufferSize) {
|
||||
this.flush();
|
||||
this.push(buffer);
|
||||
} else {
|
||||
var offset = this.reserve(length);
|
||||
Bufferish.prototype.copy.call(buffer, this.buffer, offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// common methods
|
||||
|
||||
function write() {
|
||||
throw new Error("method not implemented: write()");
|
||||
}
|
||||
|
||||
function fetch() {
|
||||
throw new Error("method not implemented: fetch()");
|
||||
}
|
||||
|
||||
function read() {
|
||||
var length = this.buffers && this.buffers.length;
|
||||
|
||||
// fetch the first result
|
||||
if (!length) return this.fetch();
|
||||
|
||||
// flush current buffer
|
||||
this.flush();
|
||||
|
||||
// read from the results
|
||||
return this.pull();
|
||||
}
|
||||
|
||||
function push(chunk) {
|
||||
var buffers = this.buffers || (this.buffers = []);
|
||||
buffers.push(chunk);
|
||||
}
|
||||
|
||||
function pull() {
|
||||
var buffers = this.buffers || (this.buffers = []);
|
||||
return buffers.shift();
|
||||
}
|
||||
|
||||
function mixinFactory(source) {
|
||||
return mixin;
|
||||
|
||||
function mixin(target) {
|
||||
for (var key in source) {
|
||||
target[key] = source[key];
|
||||
}
|
||||
return target;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user