Skip to content

Commit 1e957e8

Browse files
committed
First commit
1 parent 7540685 commit 1e957e8

File tree

7 files changed

+1955
-2
lines changed

7 files changed

+1955
-2
lines changed

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"ignore": [
3+
"node_modules/**"
4+
],
5+
"presets": [
6+
[
7+
"@babel/env",
8+
{
9+
"modules": false
10+
}
11+
]
12+
]
13+
}

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sudo: false
2+
3+
language: node_js
4+
5+
node_js:
6+
- '10'
7+
- '11'
8+
- '12'
9+
- '13'
10+
- '14'
11+
12+
script:
13+
- echo "skipping tests"

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,91 @@
1-
# base64-uint8array
2-
📦 A simple, lightweight, and efficient JavaScript library to encode/decode a base64 string into Uint8Array
1+
# base64 <-> Uint8Array <-> ArrayBuffer
2+
[![Build Status](https://travis-ci.org/PropreCity/base64-u8-arraybuffer.svg)](https://travis-ci.org/PropreCity/base64-u8-arraybuffer)
3+
[![npm version](https://badge.fury.io/js/base64-u8-arraybuffer.svg)](https://www.npmjs.com/package/base64-u8-arraybuffer)
4+
5+
📦 A simple, lightweight, and efficient JavaScript library to manage encoding and decoding between base64 data, Uint8Arrays, and ArrayBuffers. This library perfectly works with Node.js and the browser.
6+
7+
- **< 900 bytes** gzipped!
8+
- No dependency
9+
- Accepts base64url and replace non-url compatible chars with base64 standard chars
10+
- Add missing padding to base64 string
11+
- Uses modern functions and shorter solutions
12+
- Supports ES modules, AMD, CommonJS and IIFE
13+
14+
## Installation
15+
16+
### CDN
17+
18+
The easiest way to use base64-u8-arraybuffer is to include the library from a CDN:
19+
```html
20+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/base64-u8-arraybuffer.min.js"></script>
21+
```
22+
23+
Then, in your JavaScript code:
24+
```js
25+
// Unpacking needed functions from the global object
26+
const { base64ToArrayBuffer } = base64u8ArrayBuffer
27+
28+
const u8 = base64ToUint8Array('base64 string here')
29+
```
30+
31+
### Local
32+
33+
You can also install base64-u8-arraybuffer in your project.
34+
35+
```bash
36+
npm i base64-u8-arraybuffer
37+
```
38+
39+
And then, you can import the library as ES Module:
40+
```js
41+
import { base64ToArrayBuffer } from 'base64-u8-arraybuffer'
42+
43+
const u8 = base64ToUint8Array('base64 string here')
44+
```
45+
You can also use commonJS syntax with `require()`
46+
47+
Note ES Module syntax also works in modern browsers, you just need to add `type="module"` to your `<script>` tag.
48+
```html
49+
<script type="module" src="..."></script>
50+
```
51+
52+
## Available functions
53+
54+
| Function name | Description |
55+
| --- | --- |
56+
| `base64ToUint8Array(base64String)` | base64 to Uint8Array |
57+
| `uint8ArrayToBase64(uint8Array)` | Uint8Array to Base64 (works with any TypedArray, you can use `typedArrayToBase64()` alias) |
58+
| `uint8ArrayToArrayBuffer(uint8Array)` | Uint8Array to ArrayBuffer (works with any TypedArray, you can use `typedArrayToArrayBuffer()` alias) |
59+
| `arrayBufferToUint8Array(arrayBuffer)` | ArrayBuffer to Uint8Array |
60+
| `base64ToArrayBuffer(base64String)` | base64 to ArrayBuffer |
61+
| `arrayBufferToBase64(arrayBuffer)` | ArrayBuffer to base64 |
62+
63+
## Useful case
64+
65+
An example for this library would be to convert a base64url VAPID application server key into an Uint8Array to suscribe to Web Push Notifications. You can achieve this by using `base64ToUint8Array(base64String)` function.
66+
67+
```js
68+
const { base64ToUint8Array } = base64u8ArrayBuffer
69+
70+
const applicationServerPublicKey = 'base64url public key'
71+
72+
async function subscribeUserToPush(registration) {
73+
// ...
74+
const subscription = await registration.pushManager.subscribe({
75+
userVisibleOnly: true,
76+
applicationServerKey: base64ToUint8Array(applicationServerPublicKey)
77+
})
78+
// ...
79+
}
80+
```
81+
82+
## Known issues
83+
84+
```txt
85+
RangeError: Maximum call stack size exceeded
86+
```
87+
This error occurs when using too large data (more than 30kb) to encode a base64 string (e.g, `uint8ArrayToBase64()` and `arrayBufferToBase64()`).
88+
89+
## Tests
90+
91+
There is no tests for the moment.

0 commit comments

Comments
 (0)