# @minecraft-js/bufwrapper

> Encode and decode data using buffers

Latest version **1.4.6** (published 2022-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @minecraft-js/bufwrapper
pnpm add @minecraft-js/bufwrapper
yarn add @minecraft-js/bufwrapper
bun add @minecraft-js/bufwrapper
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.6 |
| Published | 2022-08-28 |
| First published | 2022-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 31.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | RichardDorian |
| Maintainers | richarddorian |
| Keywords | minecraftjs |

## Links

- npm: https://www.npmjs.com/package/@minecraft-js/bufwrapper
- Repository: https://github.com/MinecraftJS/BufWrapper
- Homepage: https://github.com/MinecraftJS/BufWrapper#readme
- Issues: https://github.com/MinecraftJS/BufWrapper/issues
- npm.io page: https://npm.io/package/@minecraft-js/bufwrapper

## Dependencies (2)

- [varint](https://npm.io/package/varint.md) ^6.0.0
- [@minecraft-js/uuid](https://npm.io/package/@minecraft-js/uuid.md) ^1.0.3

## Recent versions

- 1.4.6 (latest) — 2022-08-28
- 1.4.5 — 2022-08-28
- 1.4.4 — 2022-08-03
- 1.4.3 — 2022-08-01
- 1.4.2 — 2022-07-08
- 1.4.1 — 2022-07-07
- 1.4.0 — 2022-07-06
- 1.3.0 — 2022-07-06
- 1.2.5 — 2022-07-03
- 1.2.4 — 2022-06-14
- 1.2.3 — 2022-04-06
- 1.2.2 — 2022-04-05
- 1.2.1 — 2022-04-04
- 1.2.0 — 2022-04-04
- 1.0.1 — 2022-03-30
- … 1 more at https://npm.io/package/@minecraft-js/bufwrapper/versions

## README

# BufWrapper

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/MinecraftJS/BufWrapper/Build?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/MinecraftJS/BufWrapper?style=for-the-badge)
![npm (scoped)](https://img.shields.io/npm/v/@minecraft-js/bufwrapper?style=for-the-badge)

Encode and decode data using buffers

# Features

Encode and decode:

- Varint
- String
- Int
- Long
- Array of strings
- Array of ints
- UUID
- Byte
- Boolean
- Float
- Short
- Double

⚠️ They are all signed and big endian (As Java does)

# Documentation

## Installation

Install the package:

```bash
$ npm install @minecraft-js/bufwrapper
```

And then import it in your JavaScript/TypeScript file

```ts
const { BufWrapper } = require('@minecraft-js/bufwrapper'); // CommonJS

import { BufWrapper } from '@minecraft-js/bufwrapper'; // ES6
```

## Writing a buffer

First, instantiate a new BufWrapper

```js
const buf = new BufWrapper();
```

And then you can use any method to write data into it

```js
buf.writeInt(42);
buf.writeString('Hello World');
buf.writeLong(123456789);
```

## Reading a buffer

To read a buffer content, you need to instantiate a new BufWrapper and pass the buffer to the constructor

```js
// Buffer with dummy data
const buffer = Buffer.from(
  '0000002a0b48656c6c6f20576f726c6400000000075bcd15',
  'hex'
);

const buf = new BufWrapper(buffer);
buf.readInt(); // 42
buf.readString(); // 'Hello World'
buf.readLong(); // 123456789
```

## Note on the speed

By default the library can be slow if you are writing a lot of data.
The writing process can be optimized by using the `oneConcat` option.

This option requires an additional call after writing the data.

```js
const buf = new BufWrapper(null, { oneConcat: true });
buf.writeInt(42);
buf.writeString('Hello World');
buf.writeLong(123456789);

// The call required
buf.finish();
```

Before you call the `BufWrapper#finish` method, the `BufWrapper#buffer` property will be an empty buffer.

You can see by yourself the speed upgrade by running the `test/time.js` file. Here is
what I got on my computer when writing 150,000 integers

```
$ node test/time.js
oneConcat = false : 7.261s
oneConcat = true  : 63.869ms
```

See more here: https://minecraftjs.github.io/BufWrapper/

---
_Source: https://npm.io/package/@minecraft-js/bufwrapper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
