base128-ascii 0.0.1
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.txt +8 -0
- package/README.md +38 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +68 -0
- package/package.json +29 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2025 bddjr
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Smaller than Base64, only use ASCII, can run in web browser.
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Char Code (ASCII) :
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
'\0\1\2\3\4\5\6\7\b\t\n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The first byte indicates the length of the last incomplete block, 0 means no incomplete block.
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
`4LF3 `
|
|
15
|
+
`0LF3!Tl7`
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Test effect, Base128 is 10KB smaller than Base64:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
screenshot-45.519.jpg
|
|
22
|
+
file length: 682086
|
|
23
|
+
|
|
24
|
+
encodeToTemplateLiterals:
|
|
25
|
+
bytes length: 804861
|
|
26
|
+
eval length: 779528
|
|
27
|
+
decoded length: 682086
|
|
28
|
+
equal: true
|
|
29
|
+
|
|
30
|
+
base64:
|
|
31
|
+
length: 909448
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
|
|
37
|
+
|
|
38
|
+
Made by bddjr, using MIT License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function encode(input: Uint8Array): string;
|
|
2
|
+
export declare function stringToTemplateLiterals(input: string): string;
|
|
3
|
+
export declare function encodeToTemplateLiterals(input: Uint8Array): string;
|
|
4
|
+
export declare function decode(input: string): Uint8Array<ArrayBuffer>;
|
|
5
|
+
declare const base128: {
|
|
6
|
+
encode: typeof encode;
|
|
7
|
+
encodeToTemplateLiterals: typeof encodeToTemplateLiterals;
|
|
8
|
+
decode: typeof decode;
|
|
9
|
+
};
|
|
10
|
+
export default base128;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export function encode(input) {
|
|
2
|
+
let out = new Uint8Array(Math.ceil(input.length / 7) * 8 + 1);
|
|
3
|
+
const suffixSize = input.length % 7;
|
|
4
|
+
out[0] = suffixSize + 48;
|
|
5
|
+
for (let ii = 0, oi = 1; ii < input.length;) {
|
|
6
|
+
let chunk = BigInt(input[ii++] || 0) << 48n;
|
|
7
|
+
chunk |= BigInt(input[ii++] || 0) << 40n;
|
|
8
|
+
chunk |= BigInt(input[ii++] || 0) << 32n;
|
|
9
|
+
chunk |= BigInt(input[ii++] || 0) << 24n;
|
|
10
|
+
chunk |= BigInt(input[ii++] || 0) << 16n;
|
|
11
|
+
chunk |= BigInt(input[ii++] || 0) << 8n;
|
|
12
|
+
chunk |= BigInt(input[ii++] || 0);
|
|
13
|
+
out[oi++] = Number(chunk >> 49n);
|
|
14
|
+
out[oi++] = Number((chunk >> 42n) & 127n);
|
|
15
|
+
out[oi++] = Number((chunk >> 35n) & 127n);
|
|
16
|
+
out[oi++] = Number((chunk >> 28n) & 127n);
|
|
17
|
+
out[oi++] = Number((chunk >> 21n) & 127n);
|
|
18
|
+
out[oi++] = Number((chunk >> 14n) & 127n);
|
|
19
|
+
out[oi++] = Number((chunk >> 7n) & 127n);
|
|
20
|
+
out[oi++] = Number(chunk & 127n);
|
|
21
|
+
}
|
|
22
|
+
if (suffixSize)
|
|
23
|
+
out = out.slice(0, Math.ceil(suffixSize / 7 * 8) - 8);
|
|
24
|
+
return new TextDecoder().decode(out);
|
|
25
|
+
}
|
|
26
|
+
export function stringToTemplateLiterals(input) {
|
|
27
|
+
return '`' + input.replace(/[\r\\`]|\${|\0\d?/g, (match) => match[0] == '\0'
|
|
28
|
+
? match.length == 2
|
|
29
|
+
? '\\x00' + match[1]
|
|
30
|
+
: '\\0'
|
|
31
|
+
: match == '\r'
|
|
32
|
+
? '\\r'
|
|
33
|
+
: '\\' + match) + '`';
|
|
34
|
+
}
|
|
35
|
+
export function encodeToTemplateLiterals(input) {
|
|
36
|
+
return stringToTemplateLiterals(encode(input));
|
|
37
|
+
}
|
|
38
|
+
export function decode(input) {
|
|
39
|
+
const u8a = new TextEncoder().encode(input);
|
|
40
|
+
const out = new Uint8Array(Math.ceil((u8a.length - 1) / 8) * 7);
|
|
41
|
+
for (let ii = 1, oi = 0; ii < u8a.length;) {
|
|
42
|
+
let chunk = BigInt(u8a[ii++] || 0) << 49n;
|
|
43
|
+
chunk |= BigInt(u8a[ii++] || 0) << 42n;
|
|
44
|
+
chunk |= BigInt(u8a[ii++] || 0) << 35n;
|
|
45
|
+
chunk |= BigInt(u8a[ii++] || 0) << 28n;
|
|
46
|
+
chunk |= BigInt(u8a[ii++] || 0) << 21n;
|
|
47
|
+
chunk |= BigInt(u8a[ii++] || 0) << 14n;
|
|
48
|
+
chunk |= BigInt(u8a[ii++] || 0) << 7n;
|
|
49
|
+
chunk |= BigInt(u8a[ii++] || 0);
|
|
50
|
+
out[oi++] = Number(chunk >> 48n);
|
|
51
|
+
out[oi++] = Number((chunk >> 40n) & 255n);
|
|
52
|
+
out[oi++] = Number((chunk >> 32n) & 255n);
|
|
53
|
+
out[oi++] = Number((chunk >> 24n) & 255n);
|
|
54
|
+
out[oi++] = Number((chunk >> 16n) & 255n);
|
|
55
|
+
out[oi++] = Number((chunk >> 8n) & 255n);
|
|
56
|
+
out[oi++] = Number(chunk & 255n);
|
|
57
|
+
}
|
|
58
|
+
const suffixSize = u8a[0] - 48;
|
|
59
|
+
if (suffixSize)
|
|
60
|
+
return out.slice(0, suffixSize - 7);
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
const base128 = {
|
|
64
|
+
encode,
|
|
65
|
+
encodeToTemplateLiterals,
|
|
66
|
+
decode
|
|
67
|
+
};
|
|
68
|
+
export default base128;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "base128-ascii",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rimraf dist && tsc",
|
|
8
|
+
"test": "npm run build && node test.js",
|
|
9
|
+
"prepublishOnly": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/bddjr/base128.git"
|
|
14
|
+
},
|
|
15
|
+
"author": "bddjr",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"description": "",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"package.json",
|
|
22
|
+
"package-lock.json"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@types/node": "^22.10.5",
|
|
26
|
+
"rimraf": "^6.0.1",
|
|
27
|
+
"typescript": "^5.7.2"
|
|
28
|
+
}
|
|
29
|
+
}
|