Skip to content

Commit

Permalink
added polyfill README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
vekexasia committed Mar 16, 2024
1 parent 41e4e75 commit 979beb9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
51 changes: 51 additions & 0 deletions packages/bigint-buffer-polyfill/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# @vekexasia/bigint-buffer-polyfill: Buffer polyfill for BigInt

<img src="https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white"/> <img
src="https://img.shields.io/badge/rollup-323330?style=for-the-badge&logo=rollup.js&logoColor=Brown"/> <img
src="https://img.shields.io/badge/eslint-3A33D1?style=for-the-badge&logo=eslint&logoColor=white"/> <img
src="https://img.shields.io/badge/vitest-6E9F18?style=for-the-badge&logo=vitest&logoColor=white"/>

This project is part of the [bigint-swissknife](https://github.com/vekexasia/bigint-swissknife) project. It aims to monkeypatch the Buffer native class adding
support for BigInts. This is useful when working with Node.js.

## Documentation

You can find typedoc documentation [here](https://vekexasia.github.io/bigint-swissknife/interfaces/_vekexasia_bigint_buffer_polyfill.Buffer.html).

## Installation

Add the library to your project:

```bash
npm install @vekexasia/bigint-buffer-polyfill
```

or

```bash
yarn add @vekexasia/bigint-buffer-polyfill
```

## Usage

Simply import the library and start using it on your Buffers.


```typescript
import '@vekexasia/bigint-buffer-polyfill';

const buf = Buffer.alloc(16);
buf.writeBigIntBE(-42n, 8);
buf.writeBigUIntLE(69n, 8, 8);

console.log(buf.readBigIntBE(8)); // -42n
console.log(buf.readBigUIntLE(8, 8)); // 69n
```

## TypeScript

The library is entirely written in TypeScript and comes with its own type definitions.

## License

This project is licensed under the MIT License - see the [LICENSE](../LICENSE) file for details.
31 changes: 23 additions & 8 deletions packages/bigint-buffer-polyfill/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ Buffer.prototype.readBigUIntLE = function (width: number, offset = 0): bigint {

// declaration for polyfill
declare global {
/**
* Polyfill for BigInt support in Buffer
* @example
*
* ```ts
* import '@vekexasia/bigint-buffer-polyfill';
*
* const buf = Buffer.alloc(16);
* buf.writeBigIntBE(-42n, 8);
* buf.writeBigUIntLE(69n, 8, 8);
*
* console.log(buf.readBigIntBE(8)); // -42n
* console.log(buf.readBigUIntLE(8, 8)); // 69n
* ```
*/
interface Buffer {
/**
* Write a signed BigInt to a buffer in Big Endian format
Expand All @@ -46,29 +61,29 @@ declare global {
* @param offset - the offset to write at
* @returns the number of bytes written
*/
writeBigIntBE: (value: bigint, width: number, offset?: number) => number
writeBigIntBE(value: bigint, width: number, offset?: number): number
/**
* Write a signed BigInt to a buffer in Little Endian format
* @param value - the value to write
* @param width - the number of bytes to write
* @param offset - the offset to write at
* @returns the number of bytes written
*/
writeBigIntLE: (value: bigint, width: number, offset?: number) => number
writeBigIntLE(value: bigint, width: number, offset?: number): number
/**
* Read a signed BigInt from a buffer in Big Endian format
* @param width - the number of bytes to read
* @param offset - the offset to read from
* @returns the value read
*/
readBigIntBE: (width: number, offset?: number) => bigint
readBigIntBE(width: number, offset?: number): bigint
/**
* Read a signed BigInt from a buffer in Little Endian format
* @param width - the number of bytes to read
* @param offset - the offset to read from
* @returns the value read
*/
readBigIntLE: (width: number, offset?: number) => bigint
readBigIntLE(width: number, offset?: number): bigint

/**
* Write an unsigned BigInt to a buffer in Big Endian format
Expand All @@ -77,28 +92,28 @@ declare global {
* @param offset - the offset to write at
* @returns the number of bytes written
*/
writeBigUIntBE: (value: bigint, width: number, offset?: number) => number
writeBigUIntBE(value: bigint, width: number, offset?: number): number
/**
* Write an unsigned BigInt to a buffer in Little Endian format
* @param value - the value to write
* @param width - the number of bytes to write
* @param offset - the offset to write at
* @returns the number of bytes written
*/
writeBigUIntLE: (value: bigint, width: number, offset?: number) => number
writeBigUIntLE(value: bigint, width: number, offset?: number): number
/**
* Read an unsigned BigInt from a buffer in Big Endian format
* @param width - the number of bytes to read
* @param offset - the offset to read from
* @returns the value read
*/
readBigUIntBE: (width: number, offset?: number) => bigint
readBigUIntBE(width: number, offset?: number): bigint
/**
* Read an unsigned BigInt from a buffer in Little Endian format
* @param width - the number of bytes to read
* @param offset - the offset to read from
* @returns the value read
*/
readBigUIntLE: (width: number, offset?: number) => bigint
readBigUIntLE(width: number, offset?: number): bigint
}
}
2 changes: 1 addition & 1 deletion packages/bigint-uint8array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The library is entirely written in TypeScript and comes with its own type defini

## Documentation

You can find typedoc documentation [here](https://vekexasia.github.io/bigint-uint8array/).
You can find typedoc documentation [here](https://vekexasia.github.io/bigint-swissknife/modules/_vekexasia_bigint_uint8array.html).

## Usage

Expand Down

0 comments on commit 979beb9

Please sign in to comment.