Skip to main content

Encode and decode Protobuf-style varint bytes from a buffer

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.1.0)

varint

Source code

Encode and decode Protobuf-style varint bytes from a buffer.

Fork of chrisdickinson's varint npm package.

// Encoding a varint...
{
	const buffer = new Uint8Array(16);

	const written = encode(300, buffer, 0);
	// -> 2 bytes written

	console.log(buffer.subarray(0, written));
	// -> Uint8Array(2) [0xAC, 0x02]
}

// Decoding a varint...
{
	const buffer = new Uint8Array([0xAC, 0x02]);
	const [num, read] = decode(buffer, 0);
	// -> 2 bytes read, got 300
}

Add Package

deno add @mary/varint

Import symbol

import * as mod from "@mary/varint";

Add Package

npx jsr add @mary/varint

Import symbol

import * as mod from "@mary/varint";

Add Package

yarn dlx jsr add @mary/varint

Import symbol

import * as mod from "@mary/varint";

Add Package

pnpm dlx jsr add @mary/varint

Import symbol

import * as mod from "@mary/varint";

Add Package

bunx jsr add @mary/varint

Import symbol

import * as mod from "@mary/varint";