@quentinadam/base64 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/base64.d.ts +42 -0
- package/dist/base64.js +65 -0
- package/package.json +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @quentinadam/base64
|
|
2
|
+
|
|
3
|
+
[![JSR][jsr-image]][jsr-url] [![NPM][npm-image]][npm-url] [![CI][ci-image]][ci-url]
|
|
4
|
+
|
|
5
|
+
A simple library to encode and decode base64 strings.
|
|
6
|
+
|
|
7
|
+
Optionnaly supports specifying the alphabet to use.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import * as base64 from '@quentinadam/base64';
|
|
13
|
+
|
|
14
|
+
base64.encode(new Uint8Array([102, 111, 111, 98])); // returns 'Zm9vYg=='
|
|
15
|
+
|
|
16
|
+
base64.decode('Zm9vYg=='); // returns Uint8Array([102, 111, 111, 98])
|
|
17
|
+
|
|
18
|
+
base64.encode(new Uint8Array([102, 111, 111, 98]), { padding: false }); // returns 'Zm9vYg'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
[ci-image]: https://img.shields.io/github/actions/workflow/status/quentinadam/deno-base64/ci.yml?branch=main&logo=github&style=flat-square
|
|
22
|
+
[ci-url]: https://github.com/quentinadam/deno-base64/actions/workflows/ci.yml
|
|
23
|
+
[npm-image]: https://img.shields.io/npm/v/@quentinadam/base64.svg?style=flat-square
|
|
24
|
+
[npm-url]: https://npmjs.org/package/@quentinadam/base64
|
|
25
|
+
[jsr-image]: https://jsr.io/badges/@quentinadam/base64?style=flat-square
|
|
26
|
+
[jsr-url]: https://jsr.io/@quentinadam/base64
|
package/dist/base64.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Options for {@linkcode encode}. */
|
|
2
|
+
export interface EncodeOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The alphabet to use.
|
|
5
|
+
* The alphabet must be a string of 64 unique characters (or 65 unique characters to specify the padding character to use).
|
|
6
|
+
*
|
|
7
|
+
* @default {'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='}
|
|
8
|
+
*/
|
|
9
|
+
alphabet?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the encoded string should be padded.
|
|
12
|
+
*
|
|
13
|
+
* @default {true}
|
|
14
|
+
*/
|
|
15
|
+
padding?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Options for {@linkcode decode}. */
|
|
18
|
+
export interface DecodeOptions {
|
|
19
|
+
/**
|
|
20
|
+
* The alphabet to use.
|
|
21
|
+
* The alphabet must be a string of 64 unique characters (or 65 unique characters to specify the padding character to use).
|
|
22
|
+
*
|
|
23
|
+
* @default {'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='}
|
|
24
|
+
*/
|
|
25
|
+
alphabet?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Encodes a Uint8Array buffer into a base64 string.
|
|
29
|
+
*
|
|
30
|
+
* @param buffer The buffer to encode.
|
|
31
|
+
* @param options The options to use for encoding.
|
|
32
|
+
* @returns The base64 encoded string.
|
|
33
|
+
*/
|
|
34
|
+
export declare function encode(buffer: Uint8Array, options?: EncodeOptions): string;
|
|
35
|
+
/**
|
|
36
|
+
* Decodes a base64 encoded string into a Uint8Array buffer.
|
|
37
|
+
*
|
|
38
|
+
* @param string The base64 encoded string.
|
|
39
|
+
* @param options The options to use for decoding.
|
|
40
|
+
* @returns The decoded buffer.
|
|
41
|
+
*/
|
|
42
|
+
export declare function decode(string: string, options?: DecodeOptions): Uint8Array;
|
package/dist/base64.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import require from '@quentinadam/require';
|
|
2
|
+
import assert from '@quentinadam/assert';
|
|
3
|
+
function convert({ input, inputBase, outputBase, convertRemainingBits }) {
|
|
4
|
+
const output = new Array();
|
|
5
|
+
let accumulator = 0;
|
|
6
|
+
let bits = 0;
|
|
7
|
+
for (const digit of input) {
|
|
8
|
+
if (digit < (1 << inputBase)) {
|
|
9
|
+
accumulator = accumulator << inputBase | digit;
|
|
10
|
+
bits += inputBase;
|
|
11
|
+
while (bits >= outputBase) {
|
|
12
|
+
output.push(accumulator >> (bits - outputBase));
|
|
13
|
+
bits -= outputBase;
|
|
14
|
+
accumulator &= (1 << bits) - 1;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (convertRemainingBits) {
|
|
19
|
+
if (bits > 0) {
|
|
20
|
+
output.push(accumulator << (outputBase - bits));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
assert(accumulator === 0, 'Remaining bits must be zero');
|
|
25
|
+
}
|
|
26
|
+
return output;
|
|
27
|
+
}
|
|
28
|
+
function getAlphabet(alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') {
|
|
29
|
+
assert(alphabet.length === 64 || alphabet.length === 65, 'Alphabet must be 64 or 65 characters long');
|
|
30
|
+
if (alphabet.length === 64) {
|
|
31
|
+
alphabet += '=';
|
|
32
|
+
}
|
|
33
|
+
assert(new Set(alphabet).size === 65, 'Alphabet must not contain duplicate characters');
|
|
34
|
+
return alphabet;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Encodes a Uint8Array buffer into a base64 string.
|
|
38
|
+
*
|
|
39
|
+
* @param buffer The buffer to encode.
|
|
40
|
+
* @param options The options to use for encoding.
|
|
41
|
+
* @returns The base64 encoded string.
|
|
42
|
+
*/
|
|
43
|
+
export function encode(buffer, options) {
|
|
44
|
+
const alphabet = getAlphabet(options?.alphabet);
|
|
45
|
+
const output = convert({ input: buffer, inputBase: 8, outputBase: 6, convertRemainingBits: true });
|
|
46
|
+
if (options?.padding ?? true) {
|
|
47
|
+
while (output.length % 4 !== 0) {
|
|
48
|
+
output.push(64);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return output.map((digit) => require(alphabet[digit])).join('');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Decodes a base64 encoded string into a Uint8Array buffer.
|
|
55
|
+
*
|
|
56
|
+
* @param string The base64 encoded string.
|
|
57
|
+
* @param options The options to use for decoding.
|
|
58
|
+
* @returns The decoded buffer.
|
|
59
|
+
*/
|
|
60
|
+
export function decode(string, options) {
|
|
61
|
+
const alphabet = getAlphabet(options?.alphabet);
|
|
62
|
+
const map = new Map(Array.from(alphabet).map((character, index) => [character, index]));
|
|
63
|
+
const input = Array.from(string, (character) => require(map.get(character), `Invalid character ${character}`));
|
|
64
|
+
return new Uint8Array(convert({ input, inputBase: 6, outputBase: 8, convertRemainingBits: false }));
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quentinadam/base64",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "A simple library to encode and decode base64 strings",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Quentin Adam",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/quentinadam/deno-base64.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./dist/base64.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@quentinadam/assert": "^0.1.10",
|
|
19
|
+
"@quentinadam/require": "^0.1.4"
|
|
20
|
+
}
|
|
21
|
+
}
|