@algorithm.ts/base64 1.0.24
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/LICENSE +21 -0
- package/README.md +136 -0
- package/lib/cjs/index.js +88 -0
- package/lib/esm/index.js +81 -0
- package/lib/types/index.d.ts +9 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2021 guanghechen (me.guanghechen@gmail.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
<header>
|
|
2
|
+
<h1 align="center">
|
|
3
|
+
<a href="https://github.com/guanghechen/algorithm.ts/tree/main/packages/base64#readme">@algorithm.ts/base64</a>
|
|
4
|
+
</h1>
|
|
5
|
+
<div align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@algorithm.ts/base64">
|
|
7
|
+
<img
|
|
8
|
+
alt="Npm Version"
|
|
9
|
+
src="https://img.shields.io/npm/v/@algorithm.ts/base64.svg"
|
|
10
|
+
/>
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@algorithm.ts/base64">
|
|
13
|
+
<img
|
|
14
|
+
alt="Npm Download"
|
|
15
|
+
src="https://img.shields.io/npm/dm/@algorithm.ts/base64.svg"
|
|
16
|
+
/>
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://www.npmjs.com/package/@algorithm.ts/base64">
|
|
19
|
+
<img
|
|
20
|
+
alt="Npm License"
|
|
21
|
+
src="https://img.shields.io/npm/l/@algorithm.ts/base64.svg"
|
|
22
|
+
/>
|
|
23
|
+
</a>
|
|
24
|
+
<a href="#install">
|
|
25
|
+
<img
|
|
26
|
+
alt="Module Formats: cjs, esm"
|
|
27
|
+
src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
|
|
28
|
+
/>
|
|
29
|
+
</a>
|
|
30
|
+
<a href="https://github.com/nodejs/node">
|
|
31
|
+
<img
|
|
32
|
+
alt="Node.js Version"
|
|
33
|
+
src="https://img.shields.io/node/v/@algorithm.ts/base64"
|
|
34
|
+
/>
|
|
35
|
+
</a>
|
|
36
|
+
<a href="https://github.com/facebook/jest">
|
|
37
|
+
<img
|
|
38
|
+
alt="Tested with Jest"
|
|
39
|
+
src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
|
|
40
|
+
/>
|
|
41
|
+
</a>
|
|
42
|
+
<a href="https://github.com/prettier/prettier">
|
|
43
|
+
<img
|
|
44
|
+
alt="Code Style: prettier"
|
|
45
|
+
src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
|
|
46
|
+
/>
|
|
47
|
+
</a>
|
|
48
|
+
</div>
|
|
49
|
+
</header>
|
|
50
|
+
<br/>
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
A typescript implementation of the **base64** encoding. Unlike traditional implementations, this
|
|
54
|
+
project uses `Uint8Array` to represent byte streams.
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
* npm
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install --save @algorithm.ts/base64
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
* yarn
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
yarn add @algorithm.ts/base64
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
* `encode`: Encode a Uint8Array into base64 encoded string.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { encode } from '@algorithm.ts/base64'
|
|
79
|
+
|
|
80
|
+
const textEncoder = new TextEncoder('utf-8')
|
|
81
|
+
const data: Uint8Array = textEncoder.encode(text)
|
|
82
|
+
|
|
83
|
+
encode('Hello, world!') // => 'SGVsbG8sIHdvcmxkIQ=='
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
* `decode`: Decode a base64 encoded string to Uint8Array
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { decode } from '@algorithm.ts/base64'
|
|
90
|
+
|
|
91
|
+
const data: Uint8Array = decode('SGVsbG8sIHdvcmxkIQ==')
|
|
92
|
+
const textDecoder = new TextDecoder('utf-8')
|
|
93
|
+
textDecoder.decode(data) // => 'Hello, world!'
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
* `validate`: Check if a string is base64 encoded.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
validate('SGVsbG8sIHdvcmxkIQ==') // => true
|
|
100
|
+
validate('SGVsbG8sIHdvcmxkIQ===') // => false
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
## Related
|
|
105
|
+
|
|
106
|
+
* BASE64 Table
|
|
107
|
+
|
|
108
|
+
Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | Index | Binary | Char
|
|
109
|
+
:----:|:-------:|:-----:|:-----:|:-------:|:-----:|:-----:|:-------:|:-----:|:-----:|:-------:|:-----
|
|
110
|
+
0 | 000000 | A | 16 | 010000 | Q | 32 | 100000 | g | 48 | 110000 | w
|
|
111
|
+
1 | 000001 | B | 17 | 010001 | R | 33 | 100001 | h | 49 | 110001 | x
|
|
112
|
+
2 | 000010 | C | 18 | 010010 | S | 34 | 100010 | i | 50 | 110010 | y
|
|
113
|
+
3 | 000011 | D | 19 | 010011 | T | 35 | 100011 | j | 51 | 110011 | z
|
|
114
|
+
4 | 000100 | E | 20 | 010100 | U | 36 | 100100 | k | 52 | 110100 | 0
|
|
115
|
+
5 | 000101 | F | 21 | 010101 | V | 37 | 100101 | l | 53 | 110101 | 1
|
|
116
|
+
6 | 000110 | G | 22 | 010110 | W | 38 | 100110 | m | 54 | 110110 | 2
|
|
117
|
+
7 | 000111 | H | 23 | 010111 | X | 39 | 100111 | n | 55 | 110111 | 3
|
|
118
|
+
8 | 001000 | I | 24 | 011000 | Y | 40 | 101000 | o | 56 | 111000 | 4
|
|
119
|
+
9 | 001001 | J | 25 | 011001 | Z | 41 | 101001 | p | 57 | 111001 | 5
|
|
120
|
+
10 | 001010 | K | 26 | 011010 | a | 42 | 101010 | q | 58 | 111010 | 6
|
|
121
|
+
11 | 001011 | L | 27 | 011011 | b | 43 | 101011 | r | 59 | 111011 | 7
|
|
122
|
+
12 | 001100 | M | 28 | 011100 | c | 44 | 101100 | s | 60 | 111100 | 8
|
|
123
|
+
13 | 001101 | N | 29 | 011101 | d | 45 | 101101 | t | 61 | 111101 | 9
|
|
124
|
+
14 | 001110 | O | 30 | 011110 | e | 46 | 101110 | u | 62 | 111110 | +
|
|
125
|
+
15 | 001111 | P | 31 | 011111 | f | 47 | 101111 | v | 63 | 111111 | /
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
* [Base64 | Wikipedia](https://en.wikipedia.org/wiki/Base64)
|
|
131
|
+
* [base-64 | github](https://github.com/mathiasbynens/base64)
|
|
132
|
+
* [`atob()` | MDN](https://developer.mozilla.org/en-US/docs/Web/API/atob)
|
|
133
|
+
* [`btoa()` | MDN](https://developer.mozilla.org/en-US/docs/Web/API/btoa)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
[homepage]: https://github.com/guanghechen/algorithm.ts/tree/main/packages/base64#readme
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const BASE64_CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
6
|
+
const BASE64_CODE_PADDING = '=';
|
|
7
|
+
const BASE64_CODE_LIST = BASE64_CODES.split('');
|
|
8
|
+
const BASE64_CODE_MAP = BASE64_CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
|
|
9
|
+
[BASE64_CODE_PADDING]: 0,
|
|
10
|
+
});
|
|
11
|
+
const BASE64_VALIDATE_REGEX = new RegExp(`^[${BASE64_CODES}]+={0,2}$`);
|
|
12
|
+
var index = { encode, decode, validate };
|
|
13
|
+
function encode(data) {
|
|
14
|
+
let result = '';
|
|
15
|
+
const _end = Math.floor(data.length / 3) * 3;
|
|
16
|
+
for (let i = 0; i < _end; i += 3) {
|
|
17
|
+
result += encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
|
|
18
|
+
}
|
|
19
|
+
switch (data.length - _end) {
|
|
20
|
+
case 1: {
|
|
21
|
+
const tail = encodeUnit(data[_end] << 16);
|
|
22
|
+
result += tail.slice(0, 2) + BASE64_CODE_PADDING + BASE64_CODE_PADDING;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case 2: {
|
|
26
|
+
const tail = encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
|
|
27
|
+
result += tail.slice(0, 3) + BASE64_CODE_PADDING;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
function decode(text) {
|
|
33
|
+
if (text.length <= 0 || !validate(text)) {
|
|
34
|
+
throw new TypeError('Invalid base64 string.');
|
|
35
|
+
}
|
|
36
|
+
let countOfPadding = 0;
|
|
37
|
+
for (let i = text.length - 1; i >= 0 && text[i] === BASE64_CODE_PADDING; --i)
|
|
38
|
+
countOfPadding += 1;
|
|
39
|
+
const _size = (text.length >> 2) * 3 - countOfPadding;
|
|
40
|
+
const result = new Uint8Array(_size);
|
|
41
|
+
const _end = ((text.length - countOfPadding) >> 2) << 2;
|
|
42
|
+
let i = 0;
|
|
43
|
+
let j = 0;
|
|
44
|
+
for (; i < _end; i += 4, j += 3) {
|
|
45
|
+
const v = decodeUint(text, i);
|
|
46
|
+
result[j] = v >> 16;
|
|
47
|
+
result[j + 1] = (v >> 8) & 0xff;
|
|
48
|
+
result[j + 2] = v & 0xff;
|
|
49
|
+
}
|
|
50
|
+
if (countOfPadding > 0) {
|
|
51
|
+
const v = decodeUint(text, i);
|
|
52
|
+
switch (countOfPadding) {
|
|
53
|
+
case 1: {
|
|
54
|
+
result[j] = v >> 16;
|
|
55
|
+
result[j + 1] = (v >> 8) & 0xff;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case 2: {
|
|
59
|
+
result[j] = v >> 16;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
function validate(text) {
|
|
67
|
+
if (text.length & 3)
|
|
68
|
+
return false;
|
|
69
|
+
return BASE64_VALIDATE_REGEX.test(text);
|
|
70
|
+
}
|
|
71
|
+
function encodeUnit(v) {
|
|
72
|
+
return (BASE64_CODE_LIST[(v >> 18) & 0x3f] +
|
|
73
|
+
BASE64_CODE_LIST[(v >> 12) & 0x3f] +
|
|
74
|
+
BASE64_CODE_LIST[(v >> 6) & 0x3f] +
|
|
75
|
+
BASE64_CODE_LIST[v & 0x3f]);
|
|
76
|
+
}
|
|
77
|
+
function decodeUint(text, startIdx) {
|
|
78
|
+
const v = (BASE64_CODE_MAP[text.charAt(startIdx)] << 18) |
|
|
79
|
+
(BASE64_CODE_MAP[text.charAt(startIdx | 1)] << 12) |
|
|
80
|
+
(BASE64_CODE_MAP[text.charAt(startIdx | 2)] << 6) |
|
|
81
|
+
BASE64_CODE_MAP[text.charAt(startIdx | 3)];
|
|
82
|
+
return v;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.decode = decode;
|
|
86
|
+
exports["default"] = index;
|
|
87
|
+
exports.encode = encode;
|
|
88
|
+
exports.validate = validate;
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const BASE64_CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
2
|
+
const BASE64_CODE_PADDING = '=';
|
|
3
|
+
const BASE64_CODE_LIST = BASE64_CODES.split('');
|
|
4
|
+
const BASE64_CODE_MAP = BASE64_CODE_LIST.reduce((acc, v, idx) => ({ ...acc, [v]: idx }), {
|
|
5
|
+
[BASE64_CODE_PADDING]: 0,
|
|
6
|
+
});
|
|
7
|
+
const BASE64_VALIDATE_REGEX = new RegExp(`^[${BASE64_CODES}]+={0,2}$`);
|
|
8
|
+
var index = { encode, decode, validate };
|
|
9
|
+
function encode(data) {
|
|
10
|
+
let result = '';
|
|
11
|
+
const _end = Math.floor(data.length / 3) * 3;
|
|
12
|
+
for (let i = 0; i < _end; i += 3) {
|
|
13
|
+
result += encodeUnit((data[i] << 16) | (data[i + 1] << 8) | data[i + 2]);
|
|
14
|
+
}
|
|
15
|
+
switch (data.length - _end) {
|
|
16
|
+
case 1: {
|
|
17
|
+
const tail = encodeUnit(data[_end] << 16);
|
|
18
|
+
result += tail.slice(0, 2) + BASE64_CODE_PADDING + BASE64_CODE_PADDING;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
case 2: {
|
|
22
|
+
const tail = encodeUnit((data[_end] << 16) | (data[_end + 1] << 8));
|
|
23
|
+
result += tail.slice(0, 3) + BASE64_CODE_PADDING;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
function decode(text) {
|
|
29
|
+
if (text.length <= 0 || !validate(text)) {
|
|
30
|
+
throw new TypeError('Invalid base64 string.');
|
|
31
|
+
}
|
|
32
|
+
let countOfPadding = 0;
|
|
33
|
+
for (let i = text.length - 1; i >= 0 && text[i] === BASE64_CODE_PADDING; --i)
|
|
34
|
+
countOfPadding += 1;
|
|
35
|
+
const _size = (text.length >> 2) * 3 - countOfPadding;
|
|
36
|
+
const result = new Uint8Array(_size);
|
|
37
|
+
const _end = ((text.length - countOfPadding) >> 2) << 2;
|
|
38
|
+
let i = 0;
|
|
39
|
+
let j = 0;
|
|
40
|
+
for (; i < _end; i += 4, j += 3) {
|
|
41
|
+
const v = decodeUint(text, i);
|
|
42
|
+
result[j] = v >> 16;
|
|
43
|
+
result[j + 1] = (v >> 8) & 0xff;
|
|
44
|
+
result[j + 2] = v & 0xff;
|
|
45
|
+
}
|
|
46
|
+
if (countOfPadding > 0) {
|
|
47
|
+
const v = decodeUint(text, i);
|
|
48
|
+
switch (countOfPadding) {
|
|
49
|
+
case 1: {
|
|
50
|
+
result[j] = v >> 16;
|
|
51
|
+
result[j + 1] = (v >> 8) & 0xff;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 2: {
|
|
55
|
+
result[j] = v >> 16;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function validate(text) {
|
|
63
|
+
if (text.length & 3)
|
|
64
|
+
return false;
|
|
65
|
+
return BASE64_VALIDATE_REGEX.test(text);
|
|
66
|
+
}
|
|
67
|
+
function encodeUnit(v) {
|
|
68
|
+
return (BASE64_CODE_LIST[(v >> 18) & 0x3f] +
|
|
69
|
+
BASE64_CODE_LIST[(v >> 12) & 0x3f] +
|
|
70
|
+
BASE64_CODE_LIST[(v >> 6) & 0x3f] +
|
|
71
|
+
BASE64_CODE_LIST[v & 0x3f]);
|
|
72
|
+
}
|
|
73
|
+
function decodeUint(text, startIdx) {
|
|
74
|
+
const v = (BASE64_CODE_MAP[text.charAt(startIdx)] << 18) |
|
|
75
|
+
(BASE64_CODE_MAP[text.charAt(startIdx | 1)] << 12) |
|
|
76
|
+
(BASE64_CODE_MAP[text.charAt(startIdx | 2)] << 6) |
|
|
77
|
+
BASE64_CODE_MAP[text.charAt(startIdx | 3)];
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { decode, index as default, encode, validate };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
encode: typeof encode;
|
|
3
|
+
decode: typeof decode;
|
|
4
|
+
validate: typeof validate;
|
|
5
|
+
};
|
|
6
|
+
export default _default;
|
|
7
|
+
export declare function encode(data: Uint8Array): string;
|
|
8
|
+
export declare function decode(text: string): Uint8Array | never;
|
|
9
|
+
export declare function validate(text: string): boolean;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@algorithm.ts/base64",
|
|
3
|
+
"version": "1.0.24",
|
|
4
|
+
"description": "base64 + Euclidean algorithm",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "guanghechen",
|
|
7
|
+
"url": "https://github.com/guanghechen/"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/guanghechen/algorithm.ts.git",
|
|
12
|
+
"directory": "packages/base64"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/guanghechen/algorithm.ts/tree/main/packages/base64#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"algorithm",
|
|
17
|
+
"base64"
|
|
18
|
+
],
|
|
19
|
+
"main": "lib/cjs/index.js",
|
|
20
|
+
"module": "lib/esm/index.js",
|
|
21
|
+
"types": "lib/types/index.d.ts",
|
|
22
|
+
"source": "src/index.ts",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">= 14.15.0"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"lib/",
|
|
29
|
+
"!lib/**/*.js.map",
|
|
30
|
+
"!lib/**/*.d.ts.map",
|
|
31
|
+
"package.json",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js",
|
|
38
|
+
"prebuild": "rimraf lib/ && tsc -p tsconfig.src.json --emitDeclarationOnly",
|
|
39
|
+
"prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
|
|
40
|
+
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "be5ff5ef055a514bd2f9113b541e9f49ad6008b4"
|
|
43
|
+
}
|