|
1 |
| -# base64-uint8array |
2 |
| -📦 A simple, lightweight, and efficient JavaScript library to encode/decode a base64 string into Uint8Array |
| 1 | +# base64 <-> Uint8Array <-> ArrayBuffer |
| 2 | +[](https://travis-ci.org/PropreCity/base64-u8-arraybuffer) |
| 3 | +[](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