Comparing version 1.0.0 to 1.0.1
@@ -7,2 +7,2 @@ /** | ||
*/ | ||
export declare function decode(buffer: ArrayLike<number>): [number, number]; | ||
export declare function decode(buffer: Uint8Array): [number, number]; |
@@ -1,3 +0,15 @@ | ||
const MSB = 0x80; | ||
const REST = 0x7f; | ||
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), | ||
}; | ||
/** | ||
@@ -15,11 +27,12 @@ * Decode varint from input `buffer`. Return a decoded number and an amount of bytes read while decoding. | ||
do { | ||
if (bytesRead >= buffer.length || shift > 49) { | ||
throw new RangeError("Could not decode varint"); | ||
} | ||
byte = buffer[bytesRead++]; | ||
result += shift < 28 ? (byte & REST) << shift : (byte & REST) * Math.pow(2, shift); | ||
// @ts-ignore | ||
result += shift < 28 ? (byte & REST) << shift : (byte & REST) * SHIFTS[shift]; | ||
shift += 7; | ||
} while (byte >= MSB); | ||
} while (byte >= MSB && shift <= 56); | ||
if (shift > 56 || bytesRead > buffer.length) { | ||
throw new RangeError("Could not decode varint"); | ||
} | ||
return [result, bytesRead]; | ||
} | ||
//# sourceMappingURL=decode.js.map |
@@ -1,3 +0,4 @@ | ||
const MSB = 0x80; | ||
const REST = 0x7f; | ||
import { encodingLength } from "./encoding-length.js"; | ||
const MSB = 0x80; // 1000 0000 | ||
const REST = 0x7f; // 0111 1111 | ||
const MSBALL = ~REST; | ||
@@ -15,3 +16,3 @@ const INT = Math.pow(2, 31); | ||
} | ||
const buffer = []; | ||
const buffer = new Uint8Array(encodingLength(num)); | ||
let bytes = 0; | ||
@@ -27,4 +28,4 @@ while (num >= INT) { | ||
buffer[bytes] = num | 0; | ||
return new Uint8Array(buffer); | ||
return buffer; | ||
} | ||
//# sourceMappingURL=encode.js.map |
{ | ||
"name": "varintes", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Unsigned Varint encoding and decoding, exposed as ESModule", | ||
@@ -17,4 +17,2 @@ "keywords": [ | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"type": "module", | ||
@@ -24,23 +22,58 @@ "files": [ | ||
], | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/*" | ||
] | ||
} | ||
}, | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./decode": "./dist/decode.js", | ||
"./decode-pack": "./dist/decode-pack.js", | ||
"./encode": "./dist/encode.js", | ||
"./encode-pack": "./dist/encode-pack.js", | ||
"./encoding-length": "./dist/encoding-length.js" | ||
".": { | ||
"import": { | ||
"default": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"./decode": { | ||
"import": { | ||
"default": "./dist/decode.js", | ||
"types": "./dist/decode.d.ts" | ||
} | ||
}, | ||
"./decode-pack": { | ||
"import": { | ||
"default": "./dist/decode-pack.js", | ||
"types": "./dist/decode-pack.d.ts" | ||
} | ||
}, | ||
"./encode": { | ||
"import": { | ||
"default": "./dist/encode.js", | ||
"types": "./dist/encode.d.ts" | ||
} | ||
}, | ||
"./encode-pack": { | ||
"import": { | ||
"default": "./dist/encode-pack.js", | ||
"types": "./dist/encode-pack.d.ts" | ||
} | ||
}, | ||
"./encoding-length": { | ||
"import": { | ||
"default": "./dist/encoding-length.js", | ||
"types": "./dist/encoding-length.d.ts" | ||
} | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.0.3", | ||
"jest": "^27.3.1", | ||
"prettier": "^2.4.1", | ||
"ts-jest": "^27.0.7", | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.5.2", | ||
"uint8arrays": "^3.0.0" | ||
"prettier": "^2.7.1", | ||
"tsm": "^2.2.2", | ||
"typescript": "^4.8.3", | ||
"uint8arrays": "^3.1.0", | ||
"uvu": "^0.5.6" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/jest", | ||
"test": "./node_modules/.bin/tsm ./node_modules/uvu/bin.js . .+\\.test\\.ts", | ||
"build": "./node_modules/.bin/tsc --project tsconfig.build.json" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17962
5
179
20