protocol-buffers-encodings
Advanced tools
Comparing version 1.1.1 to 1.2.0
41
index.js
var varint = require('varint') | ||
var svarint = require('signed-varint') | ||
var b4a = require('b4a') | ||
@@ -46,4 +47,4 @@ exports.make = encoder | ||
if (Buffer.isBuffer(val)) val.copy(buffer, offset) | ||
else buffer.write(val, offset, len) | ||
if (b4a.isBuffer(val)) b4a.copy(val, buffer, offset) | ||
else b4a.write(buffer, val, offset, len) | ||
offset += len | ||
@@ -60,3 +61,3 @@ | ||
var val = buffer.slice(offset, offset + len) | ||
var val = buffer.subarray(offset, offset + len) | ||
offset += val.length | ||
@@ -76,3 +77,3 @@ | ||
var oldOffset = offset | ||
var len = Buffer.byteLength(val) | ||
var len = b4a.byteLength(val) | ||
@@ -82,3 +83,3 @@ varint.encode(len, buffer, offset, 'utf-8') | ||
buffer.write(val, offset, len) | ||
b4a.write(buffer, val, offset, len) | ||
offset += len | ||
@@ -95,3 +96,3 @@ | ||
var val = buffer.toString('utf-8', offset, offset + len) | ||
var val = b4a.toString(buffer, 'utf-8', offset, offset + len) | ||
offset += len | ||
@@ -103,3 +104,3 @@ | ||
function encodingLength (val) { | ||
var len = Buffer.byteLength(val) | ||
var len = b4a.byteLength(val) | ||
return varint.encodingLength(len) + len | ||
@@ -166,4 +167,4 @@ } | ||
limit = limit || 9 | ||
var subset = Buffer.allocUnsafe(limit) | ||
buffer.copy(subset, 0, offset, offset + limit) | ||
var subset = b4a.allocUnsafe(limit) | ||
b4a.copy(buffer, subset, 0, offset, offset + limit) | ||
subset[limit - 1] = subset[limit - 1] & 0x7f | ||
@@ -202,3 +203,3 @@ val = -1 * varint.decode(subset, 0) | ||
function encode (val, buffer, offset) { | ||
val.copy(buffer, offset) | ||
b4a.copy(val, buffer, offset) | ||
encode.bytes = 8 | ||
@@ -208,3 +209,3 @@ return buffer | ||
function decode (buffer, offset) { | ||
var val = buffer.slice(offset, offset + 8) | ||
var val = buffer.subarray(offset, offset + 8) | ||
decode.bytes = 8 | ||
@@ -220,3 +221,3 @@ return val | ||
function encode (val, buffer, offset) { | ||
buffer.writeDoubleLE(val, offset) | ||
b4a.writeDoubleLE(buffer, val, offset) | ||
encode.bytes = 8 | ||
@@ -226,3 +227,3 @@ return buffer | ||
function decode (buffer, offset) { | ||
var val = buffer.readDoubleLE(offset) | ||
var val = b4a.readDoubleLE(buffer, offset) | ||
decode.bytes = 8 | ||
@@ -238,3 +239,3 @@ return val | ||
function encode (val, buffer, offset) { | ||
buffer.writeUInt32LE(val, offset) | ||
b4a.writeUInt32LE(buffer, val, offset) | ||
encode.bytes = 4 | ||
@@ -244,3 +245,3 @@ return buffer | ||
function decode (buffer, offset) { | ||
var val = buffer.readUInt32LE(offset) | ||
var val = b4a.readUInt32LE(buffer, offset) | ||
decode.bytes = 4 | ||
@@ -256,3 +257,3 @@ return val | ||
function encode (val, buffer, offset) { | ||
buffer.writeInt32LE(val, offset) | ||
b4a.writeInt32LE(buffer, val, offset) | ||
encode.bytes = 4 | ||
@@ -262,3 +263,3 @@ return buffer | ||
function decode (buffer, offset) { | ||
var val = buffer.readInt32LE(offset) | ||
var val = b4a.readInt32LE(buffer, offset) | ||
decode.bytes = 4 | ||
@@ -274,3 +275,3 @@ return val | ||
function encode (val, buffer, offset) { | ||
buffer.writeFloatLE(val, offset) | ||
b4a.writeFloatLE(buffer, val, offset) | ||
encode.bytes = 4 | ||
@@ -280,3 +281,3 @@ return buffer | ||
function decode (buffer, offset) { | ||
var val = buffer.readFloatLE(offset) | ||
var val = b4a.readFloatLE(buffer, offset) | ||
decode.bytes = 4 | ||
@@ -302,3 +303,3 @@ return val | ||
function bufferLength (val) { | ||
return Buffer.isBuffer(val) ? val.length : Buffer.byteLength(val) | ||
return b4a.isBuffer(val) ? val.length : b4a.byteLength(val) | ||
} |
{ | ||
"name": "protocol-buffers-encodings", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Base encodings for protocol-buffers", | ||
"main": "index.js", | ||
"dependencies": { | ||
"b4a": "^1.6.0", | ||
"signed-varint": "^2.0.1", | ||
@@ -8,0 +9,0 @@ "varint": "5.0.0" |
@@ -9,5 +9,7 @@ # protocol-buffers-encodings | ||
Note: use Node.js >= 16.15.0 to avoid a performance regression due to a slower `Buffer.subarray` function. | ||
[![build status](https://travis-ci.org/mafintosh/protocol-buffers-encodings.svg?branch=master)](https://travis-ci.org/mafintosh/protocol-buffers-encodings) | ||
Moved into it's own module for lighter installs | ||
Moved into its own module for lighter installs | ||
@@ -31,3 +33,3 @@ ## Usage | ||
Encode a value. `buffer` should be a buffer big enough to fit the value, `offset` should be the byte offset in the buffer where you want to write it. | ||
The buffer is returned for conveinience. | ||
The buffer is returned for convenience. | ||
@@ -38,3 +40,3 @@ After a value has been encoded `enc.encode.bytes` contains the amount of bytes used in the buffer. | ||
Decode a value. `buffer` shoudl be an encoded value and `offset` should be the byte offset where you want to start decoding. | ||
Decode a value. `buffer` should be an encoded value and `offset` should be the byte offset where you want to start decoding. | ||
@@ -41,0 +43,0 @@ After a value has been decoded `enc.decode.bytes` contains the amount of bytes that was consumed from the buffer. |
103
test.js
@@ -18,6 +18,20 @@ var tape = require('tape') | ||
tape('bytes', function (t) { | ||
test(t, encodings.bytes, [Buffer.alloc(4096), Buffer.from('hi')]) | ||
tape('bytes (node style)', function (t) { | ||
test( | ||
t, | ||
encodings.bytes, | ||
[Buffer.alloc(4096), Buffer.from('hi')], | ||
[allocBuffer] | ||
) | ||
}) | ||
tape('bytes (browser style)', function (t) { | ||
test( | ||
t, | ||
encodings.bytes, | ||
[new Uint8Array(4096), new Uint8Array([104, 105])], | ||
[allocUint8Array] | ||
) | ||
}) | ||
tape('bool', function (t) { | ||
@@ -43,6 +57,20 @@ test(t, encodings.bool, [true, false]) | ||
tape('fixed64', function (t) { | ||
test(t, encodings.fixed64, [Buffer.from([0, 0, 0, 0, 0, 0, 0, 1])]) | ||
tape('fixed64 (node style)', function (t) { | ||
test( | ||
t, | ||
encodings.fixed64, | ||
[Buffer.from([0, 0, 0, 0, 0, 0, 0, 1])], | ||
[allocBuffer] | ||
) | ||
}) | ||
tape('fixed64 (browser style)', function (t) { | ||
test( | ||
t, | ||
encodings.fixed64, | ||
[new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1])], | ||
[allocUint8Array] | ||
) | ||
}) | ||
tape('double', function (t) { | ||
@@ -64,23 +92,24 @@ test(t, encodings.double, [0, 2, 0.5, 0.4]) | ||
function test (t, enc, vals) { | ||
function test (t, enc, vals, allocFunctions = [allocBuffer, allocUint8Array]) { | ||
if (!Array.isArray(vals)) vals = [vals] | ||
for (var i = 0; i < vals.length; i++) { | ||
var val = vals[i] | ||
var buf = Buffer.alloc(enc.encodingLength(val)) | ||
for (const allocFunction of allocFunctions) { | ||
for (const val of vals) { | ||
let buf = allocFunction(enc.encodingLength(val)) | ||
enc.encode(val, buf, 0) | ||
enc.encode(val, buf, 0) | ||
t.same(enc.encode.bytes, buf.length) | ||
t.same(enc.encodingLength(val), buf.length) | ||
t.same(enc.decode(buf, 0), val) | ||
t.same(enc.decode.bytes, buf.length) | ||
t.same(enc.encode.bytes, buf.length) | ||
t.same(enc.encodingLength(val), buf.length) | ||
t.same(enc.decode(buf, 0), val) | ||
t.same(enc.decode.bytes, buf.length) | ||
var anotherBuf = Buffer.alloc(enc.encodingLength(val) + 1000) | ||
const anotherBuf = allocFunction(enc.encodingLength(val) + 1000) | ||
buf = enc.encode(val, anotherBuf, 10) | ||
t.same(buf, anotherBuf) | ||
t.ok(enc.encode.bytes < anotherBuf.length) | ||
t.same(enc.decode(buf, 10, 10 + enc.encodingLength(val)), val) | ||
t.ok(enc.decode.bytes < anotherBuf.length) | ||
buf = enc.encode(val, anotherBuf, 10) | ||
t.same(buf, anotherBuf) | ||
t.ok(enc.encode.bytes < anotherBuf.length) | ||
t.same(enc.decode(buf, 10, 10 + enc.encodingLength(val)), val) | ||
t.ok(enc.decode.bytes < anotherBuf.length) | ||
} | ||
} | ||
@@ -90,1 +119,37 @@ | ||
} | ||
tape('test browser-style buffer', function (t) { | ||
const enc = encodings.string | ||
const val = 'value' | ||
let buf = new Uint8Array(enc.encodingLength(val)) | ||
enc.encode(val, buf, 0) | ||
// First elem is the length (5). Others are the encoded 'value' | ||
const expectedEncodedVal = new Uint8Array([5, 118, 97, 108, 117, 101]) | ||
t.same(buf, expectedEncodedVal) | ||
t.same(enc.encode.bytes, buf.length) | ||
t.same(enc.encodingLength(val), buf.length) | ||
t.same(enc.decode(buf, 0), val) | ||
t.same(enc.decode.bytes, buf.length) | ||
const anotherBuf = new Uint8Array(enc.encodingLength(val) + 1000) | ||
buf = enc.encode(val, anotherBuf, 10) | ||
t.same(buf, anotherBuf) | ||
t.ok(enc.encode.bytes < anotherBuf.length) | ||
t.same(enc.decode(buf, 10, 10 + enc.encodingLength(val)), val) | ||
t.ok(enc.decode.bytes < anotherBuf.length) | ||
t.end() | ||
}) | ||
function allocBuffer (length) { | ||
// Node style | ||
return Buffer.alloc(length) | ||
} | ||
function allocUint8Array (length) { | ||
// Browser style | ||
return new Uint8Array(length) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14407
372
74
3
+ Addedb4a@^1.6.0
+ Addedb4a@1.6.7(transitive)