# pack-bytes-to-utf8

> [![CircleCI](https://dl.circleci.com/status-badge/img/gh/spb-web/pack-bytes-to-utf8/tree/master.svg?style=svg)](https://app.circleci.com/pipelines/github/spb-web/pack-bytes-to-utf8?branch=master) [![minified + gzip](https://badgen.net/bundlephobia/minzip/

Latest version **1.0.6** (published 2022-12-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install pack-bytes-to-utf8
pnpm add pack-bytes-to-utf8
yarn add pack-bytes-to-utf8
bun add pack-bytes-to-utf8
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2022-12-28 |
| First published | 2022-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Vyacheslav Ruzanov |
| Maintainers | spb-web |

## Links

- npm: https://www.npmjs.com/package/pack-bytes-to-utf8
- Repository: https://github.com/spb-web/pack-bytes-to-utf8
- npm.io page: https://npm.io/package/pack-bytes-to-utf8

## Recent versions

- 1.0.6 (latest) — 2022-12-28
- 1.0.5 — 2022-12-28
- 1.0.4 — 2022-12-28
- 1.0.3 — 2022-12-27
- 1.0.2 — 2022-12-27
- 1.0.1 — 2022-12-27
- 1.0.0 — 2022-12-26

## README

# pack-bytes-to-utf8 🔢
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/spb-web/pack-bytes-to-utf8/tree/master.svg?style=svg)](https://app.circleci.com/pipelines/github/spb-web/pack-bytes-to-utf8?branch=master)
[![minified + gzip](https://badgen.net/bundlephobia/minzip/pack-bytes-to-utf8?color=green)](https://bundlephobia.com/package/pack-bytes-to-utf8)
[![tree shaking](https://badgen.net/bundlephobia/tree-shaking/pack-bytes-to-utf8)](https://bundlephobia.com/package/pack-bytes-to-utf8)

![](https://raw.githubusercontent.com/spb-web/pack-bytes-to-utf8/master/docs/bytesToUtf8.webp)

## Install
### yarn
```
yarn add pack-bytes-to-utf8
```

### npm
```
npm insall pack-bytes-to-utf8
```
### A quick example

```ts
import {uint8ArrayToUtf8, utf8ToUint8Array} from 'pack-bytes-to-utf8'

const bytes = new Uint8Array([0xff, 0xff, 0xff, 0xff])

const bytesInString = uint8ArrayToUtf8(bytes)

const parsedBytes = utf8ToUint8Array(bytesInString)
```

### Server-sent events (SSE) example
#### Server
```ts
import {uint8ArrayToUtf8} from 'pack-bytes-to-utf8'

const bytes = new Uint8Array(Buffer.from('0xaa344...64f456', 'hex'))

// Messages in the event stream are separated by a pair of newline 
// characters, so we exclude the line break characters.
//
// Excluded characters will be replaced with double-byte characters.
const excludeCharacters = new Set([0xA, 0xB, 0xC, 0xD, 0xE])

const str = uint8ArrayToUtf8(
  bytes,
  excludeCharacters,
)

app.get('/sse', (req, res) => {
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Connection', 'keep-alive');

  res.write(`data: ${str}\n\n`)
})
```

#### Client
```ts
import {utf8ToUint8Array} from 'pack-bytes-to-utf8'

const eventSource = new EventSource('http://localhost:3000/sse')

eventSource.addEventListener('message', ({data}) => {
  console.log(utf8ToUint8Array(data))
})
```

---
_Source: https://npm.io/package/pack-bytes-to-utf8 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
