protobuf-codec
Advanced tools
Comparing version 1.0.6 to 2.0.0
@@ -1,5 +0,5 @@ | ||
import { decoders, tag } from './wire-types.js' | ||
import assert from 'nanoassert' | ||
const assert = require('nanoassert') | ||
const { decoders, tag } = require('./wire-types.js') | ||
export default function * reader ( | ||
module.exports = function * reader ( | ||
buf, | ||
@@ -6,0 +6,0 @@ byteOffset = 0, |
@@ -1,2 +0,2 @@ | ||
export function tag (bigint) { | ||
function tag (bigint) { | ||
const int = Number(bigint) // Safe as protoc only allows fieldNumber up to int32 + 3 bits for wireType | ||
@@ -9,35 +9,35 @@ const wireType = int & 0b111 | ||
export function uint64 (bigint) { | ||
function uint64 (bigint) { | ||
return BigInt.asUintN(64, bigint) | ||
} | ||
export function uint32 (bigint) { | ||
function uint32 (bigint) { | ||
return Number(BigInt.asUintN(32, bigint)) | ||
} | ||
export function int64 (bigint) { | ||
function int64 (bigint) { | ||
return BigInt.asIntN(64, bigint) | ||
} | ||
export function int32 (bigint) { | ||
function int32 (bigint) { | ||
return Number(BigInt.asIntN(32, bigint)) | ||
} | ||
export function sint64 (bigint) { | ||
function sint64 (bigint) { | ||
return ((bigint >> 1n) ^ (-1n * (bigint & 1n))) | ||
} | ||
export function sint32 (bigint) { | ||
function sint32 (bigint) { | ||
return Number((bigint >> 1n) ^ (-1n * (bigint & 1n))) | ||
} | ||
export function bool (bigint) { | ||
function bool (bigint) { | ||
return bigint !== 0n | ||
} | ||
export function enumerable (uint) { | ||
function enumerable (uint) { | ||
return Number(uint) | 0 // trick to cast uint to int | ||
} | ||
export function bytes (bytes) { | ||
function bytes (bytes) { | ||
return bytes | ||
@@ -47,27 +47,27 @@ } | ||
const _dec = new TextDecoder() | ||
export function string (bytes) { | ||
function string (bytes) { | ||
return _dec.decode(bytes) | ||
} | ||
export function fixed64 (bytes) { | ||
function fixed64 (bytes) { | ||
return _view(bytes).getBigUint64(0, true) | ||
} | ||
export function sfixed64 (bytes) { | ||
function sfixed64 (bytes) { | ||
return _view(bytes).getBigInt64(0, true) | ||
} | ||
export function double (bytes) { | ||
function double (bytes) { | ||
return _view(bytes).getFloat64(0, true) | ||
} | ||
export function fixed32 (bytes) { | ||
function fixed32 (bytes) { | ||
return _view(bytes).getUint32(0, true) | ||
} | ||
export function sfixed32 (bytes) { | ||
function sfixed32 (bytes) { | ||
return _view(bytes).geInt32(0, true) | ||
} | ||
export function float (bytes) { | ||
function float (bytes) { | ||
return _view(bytes).getFloat32(0, true) | ||
@@ -79,1 +79,21 @@ } | ||
} | ||
module.exports = { | ||
tag, | ||
uint64, | ||
uint32, | ||
int64, | ||
int32, | ||
sint64, | ||
sint32, | ||
bool, | ||
enumerable, | ||
bytes, | ||
string, | ||
fixed64, | ||
sfixed64, | ||
double, | ||
fixed32, | ||
sfixed32, | ||
float | ||
} |
@@ -1,2 +0,2 @@ | ||
import assert from 'nanoassert' | ||
const assert = require('nanoassert') | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
export const decoders = { | ||
const decoders = { | ||
0: varint, | ||
@@ -16,3 +16,3 @@ 1: fixed64bit, | ||
export function tag (buf, byteOffset = 0) { | ||
function tag (buf, byteOffset = 0) { | ||
const int = Number(varint(buf, byteOffset)) // Safe as protoc only allows fieldNumber up to int32 + 3 bits for wireType | ||
@@ -27,3 +27,3 @@ const wireType = int & 0b111 | ||
export function varint (buf, byteOffset = 0) { | ||
function varint (buf, byteOffset = 0) { | ||
let o = byteOffset | ||
@@ -45,3 +45,3 @@ let acc = 0n | ||
export function bytes (buf, byteOffset = 0) { | ||
function bytes (buf, byteOffset = 0) { | ||
const len = varint(buf, byteOffset) | ||
@@ -56,3 +56,3 @@ | ||
export function fixed64bit (buf, byteOffset = 0) { | ||
function fixed64bit (buf, byteOffset = 0) { | ||
assert(buf.byteLength - byteOffset >= 8, 'Malformed 64-bit') | ||
@@ -63,3 +63,3 @@ fixed64bit.bytes = 8 | ||
export function fixed32bit (buf, byteOffset = 0) { | ||
function fixed32bit (buf, byteOffset = 0) { | ||
assert(buf.byteLength - byteOffset >= 4, 'Malformed 64-bit') | ||
@@ -69,1 +69,10 @@ fixed32bit.bytes = 4 | ||
} | ||
module.exports = { | ||
decoders, | ||
tag, | ||
varint, | ||
bytes, | ||
fixed64bit, | ||
fixed32bit | ||
} |
@@ -1,1 +0,1 @@ | ||
export * from './wire-types.js' | ||
module.exports = require('./wire-types.js') |
@@ -1,4 +0,4 @@ | ||
import assert from 'nanoassert' | ||
const assert = require('nanoassert') | ||
export const wireTypes = { | ||
const wireTypes = { | ||
VARINT: 0, | ||
@@ -10,3 +10,3 @@ BYTES: 2, | ||
export const varint = { | ||
const varint = { | ||
encode ( | ||
@@ -63,3 +63,3 @@ int, | ||
export const bytes = { | ||
const bytes = { | ||
encode (src, buf = alloc(this, src), byteOffset = 0) { | ||
@@ -79,3 +79,3 @@ let o = byteOffset | ||
export const tag = { | ||
const tag = { | ||
encode ( | ||
@@ -104,3 +104,3 @@ fieldNumber, | ||
export const string = { | ||
const string = { | ||
encode (str, buf = alloc(this, str), byteOffset = 0) { | ||
@@ -120,3 +120,3 @@ assert(typeof str === 'string') | ||
export const uint64 = { | ||
const uint64 = { | ||
encode (uint, buf = alloc(this, uint), byteOffset = 0) { | ||
@@ -139,3 +139,3 @@ assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
export const int64 = { | ||
const int64 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
@@ -158,3 +158,3 @@ assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
export const sint64 = { | ||
const sint64 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
@@ -178,3 +178,3 @@ assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
export const uint32 = { | ||
const uint32 = { | ||
encode (uint, buf = alloc(this, uint), byteOffset = 0) { | ||
@@ -197,3 +197,3 @@ assert(uint >= this.MIN_VALUE, 'uint exceeds MIN_VALUE') | ||
export const int32 = { | ||
const int32 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
@@ -216,3 +216,3 @@ assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
export const sint32 = { | ||
const sint32 = { | ||
encode (int, buf = alloc(this, int), byteOffset = 0) { | ||
@@ -236,3 +236,3 @@ assert(int >= this.MIN_VALUE, 'int exceeds MIN_VALUE') | ||
export const bool = { | ||
const bool = { | ||
encode (val, buf = alloc(this), byteOffset = 0) { | ||
@@ -248,3 +248,3 @@ varint.encode(val === true ? 1 : 0, buf, byteOffset) | ||
export const double = { | ||
const double = { | ||
encode (val, buf = alloc(this), byteOffset = 0) { | ||
@@ -260,3 +260,3 @@ _view(buf).setFloat64(val) | ||
export const float = { | ||
const float = { | ||
encode (val, buf = alloc(this), byteOffset = 0) { | ||
@@ -276,3 +276,3 @@ _view(buf).setFloat32(val) | ||
export const enumerable = { | ||
const enumerable = { | ||
encode (en, buf = alloc(this, en), byteOffset = 0) { | ||
@@ -299,9 +299,29 @@ assert(en >= enumerable.MIN_VALUE, 'enum value exceeds MIN_VALUE') | ||
export function alloc (ctx, ...data) { | ||
function alloc (ctx, ...data) { | ||
return new Uint8Array(ctx.encodingLength(...data)) | ||
} | ||
export const utf8 = { | ||
const utf8 = { | ||
encode (buf) { return dec.decode(buf) }, | ||
decode (str) { return enc.encode(str) } | ||
} | ||
module.exports = { | ||
wireTypes, | ||
varint, | ||
bytes, | ||
tag, | ||
string, | ||
int64, | ||
uint64, | ||
sint64, | ||
int32, | ||
uint32, | ||
sint32, | ||
bool, | ||
double, | ||
float, | ||
enumerable, | ||
utf8, | ||
alloc | ||
} |
@@ -1,6 +0,6 @@ | ||
import { varint, tag, uint64, bytes, uint32, wireTypes } from './wire-types.js' | ||
const { varint, tag, uint64, bytes, uint32, wireTypes } = require('./wire-types.js') | ||
const PAGE_SIZE = 256 | ||
export default class Writer { | ||
module.exports = class Writer { | ||
/** | ||
@@ -7,0 +7,0 @@ * |
{ | ||
"name": "protobuf-codec", | ||
"version": "1.0.6", | ||
"version": "2.0.0", | ||
"description": "Minimal Protocol Buffers wire encoding/decoding", | ||
"type": "module", | ||
"exports": { | ||
"./*": "./*.js", | ||
"./*.js": "./*.js" | ||
}, | ||
"scripts": { | ||
"test": "tape 'test/**/*.mjs'", | ||
"test": "tape 'test/**/*.{mjs,js}'", | ||
"posttest": "standard ." | ||
@@ -13,0 +8,0 @@ }, |
@@ -1,6 +0,6 @@ | ||
import test from 'tape' | ||
const test = require('tape') | ||
import * as decode from '../decode/types.js' | ||
import * as decodeWire from '../decode/wire-types.js' | ||
import * as encode from '../encode/types.js' | ||
const decode = require('../decode/types.js') | ||
const decodeWire = require('../decode/wire-types.js') | ||
const encode = require('../encode/types.js') | ||
@@ -7,0 +7,0 @@ test('enumerable', assert => { |
@@ -1,3 +0,3 @@ | ||
import Writer from '../encode/writer.js' | ||
import test from 'tape' | ||
const Writer = require('../encode/writer.js') | ||
const test = require('tape') | ||
@@ -4,0 +4,0 @@ test('', (assert) => { |
26930
16
680
No