package/package.json000644 0000000752 3560116604 011552 0ustar00000000 000000 { "name": "buffer-writer", "version": "2.0.0", "description": "a fast, efficient buffer writer", "main": "index.js", "scripts": { "test": "mocha --throw-deprecation" }, "repository": { "type": "git", "url": "git://github.com/brianc/node-buffer-writer.git" }, "keywords": [ "buffer", "writer", "builder" ], "author": "Brian M. Carlson", "license": "MIT", "devDependencies": { "mocha": "5.2.0" }, "engines": { "node": ">=4" } } package/.travis.yml000644 0000000073 3560116604 011371 0ustar00000000 000000 language: node_js node_js: - 4 - 6 - 8 - 10 - 11 package/index.js000644 0000007073 3560116604 010734 0ustar00000000 000000 //binary data writer tuned for creating //postgres message packets as effeciently as possible by reusing the //same buffer to avoid memcpy and limit memory allocations var Writer = module.exports = function (size) { this.size = size || 1024; this.buffer = Buffer.alloc(this.size + 5); this.offset = 5; this.headerPosition = 0; }; //resizes internal buffer if not enough size left Writer.prototype._ensure = function (size) { var remaining = this.buffer.length - this.offset; if (remaining < size) { var oldBuffer = this.buffer; // exponential growth factor of around ~ 1.5 // https://stackoverflow.com/questions/2269063/buffer-growth-strategy var newSize = oldBuffer.length + (oldBuffer.length >> 1) + size; this.buffer = Buffer.alloc(newSize); oldBuffer.copy(this.buffer); } }; Writer.prototype.addInt32 = function (num) { this._ensure(4); this.buffer[this.offset++] = (num >>> 24 & 0xFF); this.buffer[this.offset++] = (num >>> 16 & 0xFF); this.buffer[this.offset++] = (num >>> 8 & 0xFF); this.buffer[this.offset++] = (num >>> 0 & 0xFF); return this; }; Writer.prototype.addInt16 = function (num) { this._ensure(2); this.buffer[this.offset++] = (num >>> 8 & 0xFF); this.buffer[this.offset++] = (num >>> 0 & 0xFF); return this; }; //for versions of node requiring 'length' as 3rd argument to buffer.write var writeString = function (buffer, string, offset, len) { buffer.write(string, offset, len); }; //overwrite function for older versions of node if (Buffer.prototype.write.length === 3) { writeString = function (buffer, string, offset, len) { buffer.write(string, offset); }; } Writer.prototype.addCString = function (string) { //just write a 0 for empty or null strings if (!string) { this._ensure(1); } else { var len = Buffer.byteLength(string); this._ensure(len + 1); //+1 for null terminator writeString(this.buffer, string, this.offset, len); this.offset += len; } this.buffer[this.offset++] = 0; // null terminator return this; }; Writer.prototype.addChar = function (c) { this._ensure(1); writeString(this.buffer, c, this.offset, 1); this.offset++; return this; }; Writer.prototype.addString = function (string) { string = string || ""; var len = Buffer.byteLength(string); this._ensure(len); this.buffer.write(string, this.offset); this.offset += len; return this; }; Writer.prototype.getByteLength = function () { return this.offset - 5; }; Writer.prototype.add = function (otherBuffer) { this._ensure(otherBuffer.length); otherBuffer.copy(this.buffer, this.offset); this.offset += otherBuffer.length; return this; }; Writer.prototype.clear = function () { this.offset = 5; this.headerPosition = 0; this.lastEnd = 0; }; //appends a header block to all the written data since the last //subsequent header or to the beginning if there is only one data block Writer.prototype.addHeader = function (code, last) { var origOffset = this.offset; this.offset = this.headerPosition; this.buffer[this.offset++] = code; //length is everything in this packet minus the code this.addInt32(origOffset - (this.headerPosition + 1)); //set next header position this.headerPosition = origOffset; //make space for next header this.offset = origOffset; if (!last) { this._ensure(5); this.offset += 5; } }; Writer.prototype.join = function (code) { if (code) { this.addHeader(code, true); } return this.buffer.slice(code ? 0 : 5, this.offset); }; Writer.prototype.flush = function (code) { var result = this.join(code); this.clear(); return result; }; package/LICENSE000644 0000002026 3560116604 010265 0ustar00000000 000000 The MIT License (MIT) 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. package/README.md000644 0000003277 3560116604 010550 0ustar00000000 000000 # buffer-writer [![Build Status](https://secure.travis-ci.org/brianc/node-buffer-writer.png?branch=master)](http://travis-ci.org/brianc/node-buffer-writer) Fast & efficient buffer writer used to keep memory usage low by internally recycling a single large buffer. Used as the binary protocol writer in [node-postgres](https://github.com/brianc/node-postgres) Since postgres requires big endian encoding, this only writes big endian numbers for now, but can & probably will easily be extended to write little endian as well. I'll admit this has a few postgres specific things I might need to take out in the future, such as `addHeader` ## api `var writer = new (require('buffer-writer')());` ### writer.addInt32(num) Writes a 4-byte big endian binary encoded number to the end of the buffer. ### writer.addInt16(num) Writes a 2-byte big endian binary encoded number to the end of the buffer. ### writer.addCString(string) Writes a string to the buffer `utf8` encoded and adds a null character (`\0`) at the end. ### var buffer = writer.addHeader(char) Writes the 5 byte PostgreSQL required header to the beginning of the buffer. (1 byte for character, 1 BE Int32 for length of the buffer) ### var buffer = writer.join() Collects all data in the writer and joins it into a single, new buffer. ### var buffer = writer.flush(char) Writes the 5 byte postgres required message header, collects all data in the writer and joins it into a single, new buffer, and then resets the writer. ## thoughts This is kind of node-postgres specific. If you're interested in using this for a more general purpose thing, lemme know. I would love to work with you on getting this more reusable for your needs. ## license MIT package/test/mocha.opts000644 0000000011 3560116604 012225 0ustar00000000 000000 --ui tdd package/test/writer-tests.js000644 0000015114 3560116604 013253 0ustar00000000 000000 var Writer = require(__dirname + "/../"); var assert = require('assert'); var util = require('util'); assert.equalBuffers = function (actual, expected) { var spit = function (actual, expected) { console.log(""); console.log("actual " + util.inspect(actual)); console.log("expect " + util.inspect(expected)); console.log(""); }; if (actual.length != expected.length) { spit(actual, expected); assert.strictEqual(actual.length, expected.length); } for (var i = 0; i < actual.length; i++) { if (actual[i] != expected[i]) { spit(actual, expected); } assert.strictEqual(actual[i], expected[i]); } }; suite('adding int32', function () { var testAddingInt32 = function (int, expectedBuffer) { test('writes ' + int, function () { var subject = new Writer(); var result = subject.addInt32(int).join(); assert.equalBuffers(result, expectedBuffer); }); }; testAddingInt32(0, [0, 0, 0, 0]); testAddingInt32(1, [0, 0, 0, 1]); testAddingInt32(256, [0, 0, 1, 0]); test('writes largest int32', function () { //todo need to find largest int32 when I have internet access return false; }); test('writing multiple int32s', function () { var subject = new Writer(); var result = subject.addInt32(1).addInt32(10).addInt32(0).join(); assert.equalBuffers(result, [0, 0, 0, 1, 0, 0, 0, 0x0a, 0, 0, 0, 0]); }); suite('having to resize the buffer', function () { test('after resize correct result returned', function () { var subject = new Writer(10); subject.addInt32(1).addInt32(1).addInt32(1); assert.equalBuffers(subject.join(), [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]); }); }); }); suite('int16', function () { test('writes 0', function () { var subject = new Writer(); var result = subject.addInt16(0).join(); assert.equalBuffers(result, [0, 0]); }); test('writes 400', function () { var subject = new Writer(); var result = subject.addInt16(400).join(); assert.equalBuffers(result, [1, 0x90]); }); test('writes many', function () { var subject = new Writer(); var result = subject.addInt16(0).addInt16(1).addInt16(2).join(); assert.equalBuffers(result, [0, 0, 0, 1, 0, 2]); }); test('resizes if internal buffer fills up', function () { var subject = new Writer(3); var result = subject.addInt16(2).addInt16(3).join(); assert.equalBuffers(result, [0, 2, 0, 3]); }); }); suite('cString', function () { test('writes empty cstring', function () { var subject = new Writer(); var result = subject.addCString().join(); assert.equalBuffers(result, [0]); }); test('writes two empty cstrings', function () { var subject = new Writer(); var result = subject.addCString("").addCString("").join(); assert.equalBuffers(result, [0, 0]); }); test('writes non-empty cstring', function () { var subject = new Writer(); var result = subject.addCString("!!!").join(); assert.equalBuffers(result, [33, 33, 33, 0]); }); test('resizes if reached end', function () { var subject = new Writer(3); var result = subject.addCString("!!!").join(); assert.equalBuffers(result, [33, 33, 33, 0]); }); test('writes multiple cstrings', function () { var subject = new Writer(); var result = subject.addCString("!").addCString("!").join(); assert.equalBuffers(result, [33, 0, 33, 0]); }); }); test('writes char', function () { var subject = new Writer(2); var result = subject.addChar('a').addChar('b').addChar('c').join(); assert.equalBuffers(result, [0x61, 0x62, 0x63]); }); test('gets correct byte length', function () { var subject = new Writer(5); assert.strictEqual(subject.getByteLength(), 0); subject.addInt32(0); assert.strictEqual(subject.getByteLength(), 4); subject.addCString("!"); assert.strictEqual(subject.getByteLength(), 6); }); test('can add arbitrary buffer to the end', function () { var subject = new Writer(4); subject.addCString("!!!") var result = subject.add(Buffer.from("@@@")).join(); assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]); }); suite('can write normal string', function () { var subject = new Writer(4); var result = subject.addString("!").join(); assert.equalBuffers(result, [33]); test('can write cString too', function () { var result = subject.addCString("!").join(); assert.equalBuffers(result, [33, 33, 0]); }); test('can resize', function () { var result = subject.addString("!!").join(); assert.equalBuffers(result, [33, 33, 0, 33, 33]); }); }); suite('clearing', function () { var subject = new Writer(); subject.addCString("@!!#!#"); subject.addInt32(10401); test('clears', function () { subject.clear(); assert.equalBuffers(subject.join(), []); }); test('writing more', function () { var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join(); assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]); }); test('returns result', function () { var flushedResult = subject.flush(); assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2]) }); test('clears the writer', function () { assert.equalBuffers(subject.join(), []) assert.equalBuffers(subject.flush(), []) }); }); test("resizing to much larger", function () { var subject = new Writer(2); var string = "!!!!!!!!"; var result = subject.addCString(string).flush(); assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0]); }); suite("flush", function () { test('added as a hex code to a full writer', function () { var subject = new Writer(2); var result = subject.addCString("!").flush(0x50); assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]); }); test('added as a hex code to a non-full writer', function () { var subject = new Writer(10).addCString("!"); var joinedResult = subject.join(0x50); var result = subject.flush(0x50); assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]); }); test('added as a hex code to a buffer which requires resizing', function () { var result = new Writer(2).addCString("!!!!!!!!").flush(0x50); assert.equalBuffers(result, [0x50, 0, 0, 0, 0x0D, 33, 33, 33, 33, 33, 33, 33, 33, 0]); }); }); suite("header", function () { test('adding two packets with headers', function () { var subject = new Writer(10).addCString("!"); subject.addHeader(0x50); subject.addCString("!!"); subject.addHeader(0x40); subject.addCString("!"); var result = subject.flush(0x10); assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0, 0x40, 0, 0, 0, 7, 33, 33, 0, 0x10, 0, 0, 0, 6, 33, 0]); }); });