Socket
Socket
Sign inDemoInstall

varintes

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

varintes - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

66

dist/decode.js

@@ -0,15 +1,4 @@

import { N4, N5, N6, N7 } from "./encoding-length.js";
const MSB = 0x80; // 1000 0000
const REST = 0x7f; // 0111 1111
const SHIFTS = {
0: Math.pow(2, 0),
7: Math.pow(2, 7),
14: Math.pow(2, 14),
21: Math.pow(2, 21),
28: Math.pow(2, 28),
35: Math.pow(2, 35),
42: Math.pow(2, 42),
49: Math.pow(2, 49),
56: Math.pow(2, 56),
63: Math.pow(2, 63),
};
/**

@@ -23,16 +12,43 @@ * Decode varint from input `buffer`. Return a decoded number and an amount of bytes read while decoding.

export function decode(buffer, offset = 0) {
let result = 0;
let bytesRead = offset;
let byte = 0;
let shift = 0;
do {
byte = buffer[bytesRead++];
// @ts-ignore
result += shift < 28 ? (byte & REST) << shift : (byte & REST) * SHIFTS[shift];
shift += 7;
} while (byte >= MSB && shift <= 56);
if (shift > 56 || bytesRead > buffer.length) {
throw new RangeError("Could not decode varint");
let byte = buffer[offset];
let result = byte & REST;
if (byte < MSB) {
return [result, 1];
}
return [result, bytesRead - offset];
byte = buffer[offset + 1];
result += (byte & REST) << 7;
if (byte < MSB) {
return [result, 2];
}
byte = buffer[offset + 2];
result += (byte & REST) << 14;
if (byte < MSB) {
return [result, 3];
}
byte = buffer[offset + 3];
result += (byte & REST) << 21;
if (byte < MSB) {
return [result, 4];
}
byte = buffer[offset + 4];
result += (byte & REST) * N4;
if (byte < MSB) {
return [result, 5];
}
byte = buffer[offset + 5];
result += (byte & REST) * N5;
if (byte < MSB) {
return [result, 6];
}
byte = buffer[offset + 6];
result += (byte & REST) * N6;
if (byte < MSB) {
return [result, 7];
}
byte = buffer[offset + 7];
result += (byte & REST) * N7;
if (byte < MSB) {
return [result, 8];
}
throw new RangeError("Could not decode varint");
}

@@ -0,1 +1,10 @@

export declare const N1: number;
export declare const N2: number;
export declare const N3: number;
export declare const N4: number;
export declare const N5: number;
export declare const N6: number;
export declare const N7: number;
export declare const N8: number;
export declare const N9: number;
/**

@@ -2,0 +11,0 @@ * Calculate amount of bytes that it takes to represent a `value` as varint, in bytes.

@@ -1,10 +0,10 @@

const N1 = Math.pow(2, 7);
const N2 = Math.pow(2, 14);
const N3 = Math.pow(2, 21);
const N4 = Math.pow(2, 28);
const N5 = Math.pow(2, 35);
const N6 = Math.pow(2, 42);
const N7 = Math.pow(2, 49);
const N8 = Math.pow(2, 56);
const N9 = Math.pow(2, 63);
export const N1 = Math.pow(2, 7);
export const N2 = Math.pow(2, 7 * 2);
export const N3 = Math.pow(2, 7 * 3);
export const N4 = Math.pow(2, 7 * 4);
export const N5 = Math.pow(2, 7 * 5);
export const N6 = Math.pow(2, 7 * 6);
export const N7 = Math.pow(2, 7 * 7);
export const N8 = Math.pow(2, 7 * 8);
export const N9 = Math.pow(2, 7 * 9);
/**

@@ -11,0 +11,0 @@ * Calculate amount of bytes that it takes to represent a `value` as varint, in bytes.

{
"name": "varintes",
"version": "2.0.1",
"version": "2.0.2",
"description": "Unsigned Varint encoding and decoding, exposed as ESModule",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc