Updated the files.
This commit is contained in:
parent
1553e6b971
commit
753967d4f5
23418 changed files with 3784666 additions and 0 deletions
26
my-app/node_modules/wbuf/README.md
generated
vendored
Executable file
26
my-app/node_modules/wbuf/README.md
generated
vendored
Executable file
|
@ -0,0 +1,26 @@
|
|||
# wbuf
|
||||
|
||||
#### LICENSE
|
||||
|
||||
This software is licensed under the MIT License.
|
||||
|
||||
Copyright Fedor Indutny, 2014.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
persons to whom the Software is furnished to do so, subject to the
|
||||
following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
425
my-app/node_modules/wbuf/index.js
generated
vendored
Executable file
425
my-app/node_modules/wbuf/index.js
generated
vendored
Executable file
|
@ -0,0 +1,425 @@
|
|||
var assert = require('minimalistic-assert');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
function WBuf() {
|
||||
this.buffers = [];
|
||||
this.toReserve = 0;
|
||||
this.size = 0;
|
||||
this.maxSize = 0;
|
||||
this.avail = 0;
|
||||
|
||||
this.last = null;
|
||||
this.offset = 0;
|
||||
|
||||
// Used in slicing
|
||||
this.sliceQueue = null;
|
||||
|
||||
this.forceReserve = false;
|
||||
|
||||
// Mostly a constant
|
||||
this.reserveRate = 64;
|
||||
}
|
||||
module.exports = WBuf;
|
||||
|
||||
WBuf.prototype.reserve = function reserve(n) {
|
||||
this.toReserve += n;
|
||||
|
||||
// Force reservation of extra bytes
|
||||
if (this.forceReserve)
|
||||
this.toReserve = Math.max(this.toReserve, this.reserveRate);
|
||||
};
|
||||
|
||||
WBuf.prototype._ensure = function _ensure(n) {
|
||||
if (this.avail >= n)
|
||||
return;
|
||||
|
||||
if (this.toReserve === 0)
|
||||
this.toReserve = this.reserveRate;
|
||||
|
||||
this.toReserve = Math.max(n - this.avail, this.toReserve);
|
||||
|
||||
if (this.avail === 0)
|
||||
this._next();
|
||||
};
|
||||
|
||||
WBuf.prototype._next = function _next() {
|
||||
var buf;
|
||||
if (this.sliceQueue === null) {
|
||||
// Most common case
|
||||
buf = new Buffer(this.toReserve);
|
||||
} else {
|
||||
// Only for `.slice()` results
|
||||
buf = this.sliceQueue.shift();
|
||||
if (this.sliceQueue.length === 0)
|
||||
this.sliceQueue = null;
|
||||
}
|
||||
|
||||
this.toReserve = 0;
|
||||
|
||||
this.buffers.push(buf);
|
||||
this.avail = buf.length;
|
||||
this.offset = 0;
|
||||
this.last = buf;
|
||||
};
|
||||
|
||||
WBuf.prototype._rangeCheck = function _rangeCheck() {
|
||||
if (this.maxSize !== 0 && this.size > this.maxSize)
|
||||
throw new RangeError('WBuf overflow');
|
||||
};
|
||||
|
||||
WBuf.prototype._move = function _move(n) {
|
||||
this.size += n;
|
||||
if (this.avail === 0)
|
||||
this.last = null;
|
||||
|
||||
this._rangeCheck();
|
||||
};
|
||||
|
||||
WBuf.prototype.slice = function slice(start, end) {
|
||||
assert(0 <= start && start <= this.size);
|
||||
assert(0 <= end && end <= this.size);
|
||||
|
||||
if (this.last === null)
|
||||
this._next();
|
||||
|
||||
var res = new WBuf();
|
||||
|
||||
// Only last chunk is requested
|
||||
if (start >= this.size - this.offset) {
|
||||
res.buffers.push(this.last);
|
||||
res.last = this.last;
|
||||
res.offset = start - this.size + this.offset;
|
||||
res.maxSize = end - start;
|
||||
res.avail = res.maxSize;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
var startIndex = -1;
|
||||
var startOffset = 0;
|
||||
var endIndex = -1;
|
||||
|
||||
// Find buffer indices
|
||||
var offset = 0;
|
||||
for (var i = 0; i < this.buffers.length; i++) {
|
||||
var buf = this.buffers[i];
|
||||
var next = offset + buf.length;
|
||||
|
||||
// Found the start
|
||||
if (start >= offset && start <= next) {
|
||||
startIndex = i;
|
||||
startOffset = start - offset;
|
||||
if (endIndex !== -1)
|
||||
break;
|
||||
}
|
||||
if (end >= offset && end <= next) {
|
||||
endIndex = i;
|
||||
if (startIndex !== -1)
|
||||
break;
|
||||
}
|
||||
|
||||
offset = next;
|
||||
}
|
||||
|
||||
res.last = this.buffers[startIndex];
|
||||
res.offset = startOffset;
|
||||
res.maxSize = end - start;
|
||||
|
||||
// Multi-buffer slice
|
||||
if (startIndex < endIndex) {
|
||||
res.sliceQueue = this.buffers.slice(startIndex + 1, endIndex + 1);
|
||||
|
||||
res.last = res.last.slice(res.offset);
|
||||
res.offset = 0;
|
||||
}
|
||||
|
||||
res.avail = res.last.length - res.offset;
|
||||
res.buffers.push(res.last);
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
WBuf.prototype.skip = function skip(n) {
|
||||
if (n === 0)
|
||||
return this.slice(this.size, this.size);
|
||||
|
||||
this._ensure(n);
|
||||
|
||||
var left = n;
|
||||
while (left > 0) {
|
||||
var toSkip = Math.min(left, this.avail);
|
||||
left -= toSkip;
|
||||
this.size += toSkip;
|
||||
if (toSkip === this.avail) {
|
||||
if (left !== 0) {
|
||||
this._next();
|
||||
} else {
|
||||
this.avail -= toSkip;
|
||||
this.offset += toSkip;
|
||||
}
|
||||
} else {
|
||||
this.offset += toSkip;
|
||||
this.avail -= toSkip;
|
||||
}
|
||||
}
|
||||
|
||||
this._rangeCheck();
|
||||
|
||||
return this.slice(this.size - n, this.size);
|
||||
};
|
||||
|
||||
WBuf.prototype.write = function write(str) {
|
||||
var len = 0;
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
var c = str.charCodeAt(i);
|
||||
if (c > 255)
|
||||
len += 2;
|
||||
else
|
||||
len += 1;
|
||||
}
|
||||
this.reserve(len);
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
var c = str.charCodeAt(i);
|
||||
var hi = c >>> 8;
|
||||
var lo = c & 0xff;
|
||||
|
||||
if (hi)
|
||||
this.writeUInt8(hi);
|
||||
this.writeUInt8(lo);
|
||||
}
|
||||
};
|
||||
|
||||
WBuf.prototype.copyFrom = function copyFrom(buf, start, end) {
|
||||
var off = start === undefined ? 0 : start;
|
||||
var len = end === undefined ? buf.length : end;
|
||||
if (off === len)
|
||||
return;
|
||||
|
||||
this._ensure(len - off);
|
||||
while (off < len) {
|
||||
var toCopy = Math.min(len - off, this.avail);
|
||||
buf.copy(this.last, this.offset, off, off + toCopy);
|
||||
off += toCopy;
|
||||
this.size += toCopy;
|
||||
if (toCopy === this.avail) {
|
||||
if (off !== len) {
|
||||
this._next();
|
||||
} else {
|
||||
this.avail = 0;
|
||||
this.offset += toCopy;
|
||||
}
|
||||
} else {
|
||||
this.offset += toCopy;
|
||||
this.avail -= toCopy;
|
||||
}
|
||||
}
|
||||
|
||||
this._rangeCheck();
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt8 = function writeUInt8(v) {
|
||||
this._ensure(1);
|
||||
|
||||
this.last[this.offset++] = v;
|
||||
this.avail--;
|
||||
this._move(1);
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt16BE = function writeUInt16BE(v) {
|
||||
this._ensure(2);
|
||||
|
||||
// Fast case - everything fits into the last buffer
|
||||
if (this.avail >= 2) {
|
||||
this.last.writeUInt16BE(v, this.offset);
|
||||
this.offset += 2;
|
||||
this.avail -= 2;
|
||||
|
||||
// One byte here, one byte there
|
||||
} else {
|
||||
this.last[this.offset] = (v >>> 8);
|
||||
this._next();
|
||||
this.last[this.offset++] = v & 0xff;
|
||||
this.avail--;
|
||||
}
|
||||
|
||||
this._move(2);
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt24BE = function writeUInt24BE(v) {
|
||||
this._ensure(3);
|
||||
|
||||
// Fast case - everything fits into the last buffer
|
||||
if (this.avail >= 3) {
|
||||
this.last.writeUInt16BE(v >>> 8, this.offset);
|
||||
this.last[this.offset + 2] = v & 0xff;
|
||||
this.offset += 3;
|
||||
this.avail -= 3;
|
||||
this._move(3);
|
||||
|
||||
// Two bytes here
|
||||
} else if (this.avail >= 2) {
|
||||
this.last.writeUInt16BE(v >>> 8, this.offset);
|
||||
this._next();
|
||||
this.last[this.offset++] = v & 0xff;
|
||||
this.avail--;
|
||||
this._move(3);
|
||||
|
||||
// Just one byte here
|
||||
} else {
|
||||
this.last[this.offset] = v >>> 16;
|
||||
this._move(1);
|
||||
this._next();
|
||||
this.writeUInt16BE(v & 0xffff);
|
||||
}
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt32BE = function writeUInt32BE(v) {
|
||||
this._ensure(4);
|
||||
|
||||
// Fast case - everything fits into the last buffer
|
||||
if (this.avail >= 4) {
|
||||
this.last.writeUInt32BE(v, this.offset);
|
||||
this.offset += 4;
|
||||
this.avail -= 4;
|
||||
this._move(4);
|
||||
|
||||
// Three bytes here
|
||||
} else if (this.avail >= 3) {
|
||||
this.writeUInt24BE(v >>> 8);
|
||||
this._next();
|
||||
this.last[this.offset++] = v & 0xff;
|
||||
this.avail--;
|
||||
this._move(1);
|
||||
|
||||
// Slow case, who cares
|
||||
} else {
|
||||
this.writeUInt16BE(v >>> 16);
|
||||
this.writeUInt16BE(v & 0xffff);
|
||||
}
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt16LE = function writeUInt16LE(num) {
|
||||
var r = ((num & 0xff) << 8) | (num >>> 8);
|
||||
this.writeUInt16BE(r);
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt24LE = function writeUInt24LE(num) {
|
||||
var r = ((num & 0xff) << 16) | (((num >>> 8) & 0xff) << 8) | (num >>> 16);
|
||||
this.writeUInt24BE(r);
|
||||
};
|
||||
|
||||
WBuf.prototype.writeUInt32LE = function writeUInt32LE(num) {
|
||||
this._ensure(4);
|
||||
|
||||
// Fast case - everything fits into the last buffer
|
||||
if (this.avail >= 4) {
|
||||
this.last.writeUInt32LE(num, this.offset);
|
||||
this.offset += 4;
|
||||
this.avail -= 4;
|
||||
this._move(4);
|
||||
|
||||
// Three bytes here
|
||||
} else if (this.avail >= 3) {
|
||||
this.writeUInt24LE(num & 0xffffff);
|
||||
this._next();
|
||||
this.last[this.offset++] = num >>> 24;
|
||||
this.avail--;
|
||||
this._move(1);
|
||||
|
||||
// Slow case, who cares
|
||||
} else {
|
||||
this.writeUInt16LE(num & 0xffff);
|
||||
this.writeUInt16LE(num >>> 16);
|
||||
}
|
||||
};
|
||||
|
||||
WBuf.prototype.render = function render() {
|
||||
var left = this.size;
|
||||
var out = [];
|
||||
|
||||
for (var i = 0; i < this.buffers.length && left >= 0; i++) {
|
||||
var buf = this.buffers[i];
|
||||
left -= buf.length;
|
||||
if (left >= 0) {
|
||||
out.push(buf);
|
||||
} else {
|
||||
out.push(buf.slice(0, buf.length + left));
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
// Signed APIs
|
||||
WBuf.prototype.writeInt8 = function writeInt8(num) {
|
||||
if (num < 0)
|
||||
return this.writeUInt8(0x100 + num);
|
||||
else
|
||||
return this.writeUInt8(num);
|
||||
};
|
||||
|
||||
function toUnsigned16(num) {
|
||||
if (num < 0)
|
||||
return 0x10000 + num;
|
||||
else
|
||||
return num;
|
||||
}
|
||||
|
||||
WBuf.prototype.writeInt16LE = function writeInt16LE(num) {
|
||||
this.writeUInt16LE(toUnsigned16(num));
|
||||
};
|
||||
|
||||
WBuf.prototype.writeInt16BE = function writeInt16BE(num) {
|
||||
this.writeUInt16BE(toUnsigned16(num));
|
||||
};
|
||||
|
||||
function toUnsigned24(num) {
|
||||
if (num < 0)
|
||||
return 0x1000000 + num;
|
||||
else
|
||||
return num;
|
||||
}
|
||||
|
||||
WBuf.prototype.writeInt24LE = function writeInt24LE(num) {
|
||||
this.writeUInt24LE(toUnsigned24(num));
|
||||
};
|
||||
|
||||
WBuf.prototype.writeInt24BE = function writeInt24BE(num) {
|
||||
this.writeUInt24BE(toUnsigned24(num));
|
||||
};
|
||||
|
||||
function toUnsigned32(num) {
|
||||
if (num < 0)
|
||||
return (0xffffffff + num) + 1;
|
||||
else
|
||||
return num;
|
||||
}
|
||||
|
||||
WBuf.prototype.writeInt32LE = function writeInt32LE(num) {
|
||||
this.writeUInt32LE(toUnsigned32(num));
|
||||
};
|
||||
|
||||
WBuf.prototype.writeInt32BE = function writeInt32BE(num) {
|
||||
this.writeUInt32BE(toUnsigned32(num));
|
||||
};
|
||||
|
||||
WBuf.prototype.writeComb = function writeComb(size, endian, value) {
|
||||
if (size === 1)
|
||||
return this.writeUInt8(value);
|
||||
|
||||
if (endian === 'le') {
|
||||
if (size === 2)
|
||||
this.writeUInt16LE(value);
|
||||
else if (size === 3)
|
||||
this.writeUInt24LE(value);
|
||||
else if (size === 4)
|
||||
this.writeUInt32LE(value);
|
||||
} else {
|
||||
if (size === 2)
|
||||
this.writeUInt16BE(value);
|
||||
else if (size === 3)
|
||||
this.writeUInt24BE(value);
|
||||
else if (size === 4)
|
||||
this.writeUInt32BE(value);
|
||||
}
|
||||
};
|
29
my-app/node_modules/wbuf/package.json
generated
vendored
Executable file
29
my-app/node_modules/wbuf/package.json
generated
vendored
Executable file
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"name": "wbuf",
|
||||
"version": "1.7.3",
|
||||
"description": "Write buffer",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "mocha test/**/*-test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:indutny/wbuf"
|
||||
},
|
||||
"keywords": [
|
||||
"Write",
|
||||
"Buffer"
|
||||
],
|
||||
"author": "Fedor Indutny <fedor@indutny.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/indutny/wbuf/issues"
|
||||
},
|
||||
"homepage": "https://github.com/indutny/wbuf",
|
||||
"devDependencies": {
|
||||
"mocha": "^5.0.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"minimalistic-assert": "^1.0.0"
|
||||
}
|
||||
}
|
357
my-app/node_modules/wbuf/test/wbuf-test.js
generated
vendored
Executable file
357
my-app/node_modules/wbuf/test/wbuf-test.js
generated
vendored
Executable file
|
@ -0,0 +1,357 @@
|
|||
var assert = require('assert');
|
||||
var WriteBuffer = require('../');
|
||||
|
||||
describe('WriteBuffer', function() {
|
||||
var w;
|
||||
beforeEach(function() {
|
||||
w = new WriteBuffer();
|
||||
});
|
||||
|
||||
function join(arr) {
|
||||
return arr.map(function(buf) {
|
||||
return buf.toString('hex');
|
||||
}).join('');
|
||||
}
|
||||
|
||||
describe('.writeUInt8', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt8(1);
|
||||
w.writeUInt8(2);
|
||||
w.writeUInt8(3);
|
||||
w.writeUInt8(4);
|
||||
assert.equal(join(w.render()), '01020304');
|
||||
});
|
||||
|
||||
it('should correctly handle overflow', function() {
|
||||
w.reserve(3);
|
||||
w.writeUInt8(1);
|
||||
w.writeUInt8(2);
|
||||
w.writeUInt8(3);
|
||||
w.writeUInt8(4);
|
||||
assert.equal(join(w.render()), '01020304');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt8', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt8(-1);
|
||||
w.writeInt8(2);
|
||||
assert.equal(join(w.render()), 'ff02');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt16BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt16BE(0x0102);
|
||||
w.writeUInt16BE(0x0304);
|
||||
assert.equal(join(w.render()), '01020304');
|
||||
});
|
||||
|
||||
it('should correctly handle overflow', function() {
|
||||
w.reserve(2);
|
||||
w.reserve(3);
|
||||
w.writeUInt16BE(0x0102);
|
||||
w.writeUInt16BE(0x0304);
|
||||
w.writeUInt16BE(0x0506);
|
||||
assert.equal(join(w.render()), '010203040506');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt16BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt16BE(-0x0102);
|
||||
w.writeInt16BE(0x0304);
|
||||
assert.equal(join(w.render()), 'fefe0304');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt16LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt16LE(0x0102);
|
||||
w.writeUInt16LE(0x0304);
|
||||
assert.equal(join(w.render()), '02010403');
|
||||
});
|
||||
|
||||
it('should correctly handle overflow', function() {
|
||||
w.reserve(2);
|
||||
w.reserve(3);
|
||||
w.writeUInt16LE(0x0102);
|
||||
w.writeUInt16LE(0x0304);
|
||||
w.writeUInt16LE(0x0506);
|
||||
assert.equal(join(w.render()), '020104030605');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt16LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt16LE(-0x0201);
|
||||
w.writeInt16LE(0x0304);
|
||||
assert.equal(join(w.render()), 'fffd0403');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt24BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt24BE(0x010203);
|
||||
w.writeUInt24BE(0x040506);
|
||||
assert.equal(join(w.render()), '010203040506');
|
||||
});
|
||||
|
||||
it('should correctly set avail on boundary', function() {
|
||||
w = new WriteBuffer();
|
||||
w.reserveRate = 4;
|
||||
w.writeUInt16BE(1);
|
||||
w.writeUInt24BE(1);
|
||||
assert.equal(w.avail, 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt24BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt24BE(-0x010203);
|
||||
w.writeInt24BE(0x040506);
|
||||
assert.equal(join(w.render()), 'fefdfd040506');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt24LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt24LE(0x010203);
|
||||
w.writeUInt24LE(0x040506);
|
||||
assert.equal(join(w.render()), '030201060504');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt24LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt24LE(-0x010203);
|
||||
w.writeInt24LE(0x040506);
|
||||
assert.equal(join(w.render()), 'fdfdfe060504');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt32BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt32BE(0x01020304);
|
||||
w.writeUInt32BE(0x05060708);
|
||||
assert.equal(join(w.render()), '0102030405060708');
|
||||
});
|
||||
|
||||
it('should write bytes on the boundary', function() {
|
||||
w.reserve(4);
|
||||
w.writeUInt8(0x00);
|
||||
w.writeUInt32BE(0x01020304);
|
||||
assert.equal(join(w.render()), '0001020304');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt32BE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt32BE(-0x01020304);
|
||||
w.writeInt32BE(0x05060708);
|
||||
assert.equal(join(w.render()), 'fefdfcfc05060708');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeUInt32LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeUInt32LE(0x01020304);
|
||||
w.writeUInt32LE(0x05060708);
|
||||
assert.equal(join(w.render()), '0403020108070605');
|
||||
});
|
||||
|
||||
it('should write max uint32 value', function() {
|
||||
w.writeUInt32LE(0xffffffff);
|
||||
assert.equal(join(w.render()), 'ffffffff');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.combWrite', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeComb(1, 'le', 0x01);
|
||||
w.writeComb(1, 'be', 0x02);
|
||||
w.writeComb(2, 'le', 0x0102);
|
||||
w.writeComb(2, 'be', 0x0304);
|
||||
w.writeComb(3, 'le', 0x010203);
|
||||
w.writeComb(3, 'be', 0x040506);
|
||||
w.writeComb(4, 'le', 0x01020304);
|
||||
w.writeComb(4, 'be', 0x05060708);
|
||||
assert.equal(join(w.render()),
|
||||
'0102020103040302010405060403020105060708');
|
||||
});
|
||||
|
||||
it('should write max uint32 value', function() {
|
||||
w.writeUInt32LE(0xffffffff);
|
||||
assert.equal(join(w.render()), 'ffffffff');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.writeInt32LE', function() {
|
||||
it('should write bytes', function() {
|
||||
w.writeInt32LE(-0x01020304);
|
||||
w.writeInt32LE(0x05060708);
|
||||
assert.equal(join(w.render()), 'fcfcfdfe08070605');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.skip', function() {
|
||||
it('should skip bytes', function() {
|
||||
w.skip(4);
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
assert(/^.{8}deadbeef$/.test(join(w.render())));
|
||||
});
|
||||
|
||||
it('should skip 0 bytes', function() {
|
||||
var skip = w.skip(0);
|
||||
assert.equal(skip.size, 0);
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
assert(/^deadbeef$/.test(join(w.render())));
|
||||
});
|
||||
|
||||
it('should skip bytes on the boundary', function() {
|
||||
w.reserve(4);
|
||||
w.writeUInt8(0x01);
|
||||
var skip = w.skip(4);
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
skip.writeUInt32BE(0xabbabaab);
|
||||
assert(/^01abbabaabdeadbeef$/.test(join(w.render())));
|
||||
});
|
||||
|
||||
it('should skip bytes on the boundary in two chunks', function() {
|
||||
w.reserve(4);
|
||||
var skip1 = w.skip(2);
|
||||
var skip2 = w.skip(2);
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
skip1.writeUInt16BE(0xabba);
|
||||
skip2.writeUInt16BE(0xbaba);
|
||||
assert(/^abbababadeadbeef$/.test(join(w.render())));
|
||||
});
|
||||
});
|
||||
|
||||
describe('.slice', function() {
|
||||
it('should return empty slice', function() {
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
assert.equal(join(w.slice(4, 4).render()), '');
|
||||
assert.equal(join(w.render()), 'abbadead');
|
||||
});
|
||||
|
||||
it('should return full slice', function() {
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
var slice = w.slice(0, 4);
|
||||
slice.writeUInt32BE(0xdeadbeef);
|
||||
assert.equal(join(slice.render()), 'deadbeef');
|
||||
assert.equal(join(w.render()), 'deadbeef');
|
||||
});
|
||||
|
||||
it('should return partial slice', function() {
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
var slice = w.slice(0, 3);
|
||||
slice.writeUInt24BE(0xdeadbe);
|
||||
assert.equal(join(slice.render()), 'deadbe');
|
||||
assert.equal(join(w.render()), 'deadbead');
|
||||
});
|
||||
|
||||
it('should return over-the-boundary slice', function() {
|
||||
for (var i = 0; i < 16; i++) {
|
||||
w.reserve(3);
|
||||
w.writeUInt24BE(i);
|
||||
}
|
||||
assert.equal(join(w.render()),
|
||||
'000000000001000002000003000004000005000006000007' +
|
||||
'00000800000900000a00000b00000c00000d00000e00000f');
|
||||
|
||||
var slice = w.slice(5, 12);
|
||||
slice.writeUInt24BE(0xaaabac);
|
||||
slice.writeUInt24BE(0xbabbbc);
|
||||
slice.writeUInt8(0xcc);
|
||||
|
||||
assert.equal(join(slice.render()), 'aaabacbabbbccc');
|
||||
assert.equal(join(w.render()),
|
||||
'0000000000aaabacbabbbccc000004000005000006000007' +
|
||||
'00000800000900000a00000b00000c00000d00000e00000f');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.copyFrom', function() {
|
||||
it('should copy bytes', function() {
|
||||
var tmp = new Buffer(128);
|
||||
for (var i = 0; i < tmp.length; i++)
|
||||
tmp[i] = i;
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
w.copyFrom(tmp);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
|
||||
assert.equal(
|
||||
join(w.render()),
|
||||
'deadbeef000102030405060708090a0b0c0d0e0f101112131415161718191a1b' +
|
||||
'1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b' +
|
||||
'3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b' +
|
||||
'5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b' +
|
||||
'7c7d7e7fabbadead');
|
||||
});
|
||||
|
||||
it('should copy bytes using offset', function() {
|
||||
var tmp = new Buffer(128);
|
||||
for (var i = 0; i < tmp.length; i++)
|
||||
tmp[i] = i;
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
w.copyFrom(tmp, 10, 12);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
|
||||
assert.equal(
|
||||
join(w.render()),
|
||||
'deadbeef0a0babbadead');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.write', function() {
|
||||
it('should write utf8 string', function() {
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
w.write('ohai\u1023');
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
|
||||
assert.equal(
|
||||
join(w.render()),
|
||||
'deadbeef' +
|
||||
'6f6861691023' +
|
||||
'abbadead');
|
||||
});
|
||||
|
||||
it('should copy bytes using offset', function() {
|
||||
var tmp = new Buffer(128);
|
||||
for (var i = 0; i < tmp.length; i++)
|
||||
tmp[i] = i;
|
||||
w.writeUInt32BE(0xdeadbeef);
|
||||
w.copyFrom(tmp, 10, 12);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
|
||||
assert.equal(
|
||||
join(w.render()),
|
||||
'deadbeef0a0babbadead');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.skip', function() {
|
||||
it('should copy bytes', function() {
|
||||
w.reserve(5);
|
||||
var h = w.skip(4);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
h.writeUInt32BE(0xdeadbeef);
|
||||
|
||||
assert.equal(
|
||||
join(w.render()),
|
||||
'deadbeefabbadead');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.forceReserve = true', function() {
|
||||
it('should allocate more bytes', function() {
|
||||
w.forceReserve = true;
|
||||
w.reserve(4);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
w.writeUInt32BE(0xabbadead);
|
||||
|
||||
assert.equal(w.render().length, 1);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue