base128-ascii 2.1.12 → 3.0.0
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 +5 -17
- package/main.d.ts +18 -0
- package/{src/module.mjs → main.js} +6 -12
- package/package.json +12 -16
- package/dist/browser.min.js +0 -1
- package/src/main.d.ts +0 -12
- package/src/main.js +0 -86
- package/src/module.d.mts +0 -14
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Smaller than base64, only use ASCII, can run in web browser.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Build for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression)
|
|
4
4
|
|
|
5
5
|
## Setup
|
|
6
6
|
|
|
@@ -12,21 +12,13 @@ npm i base128-ascii
|
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
14
|
import base128 from "base128-ascii"
|
|
15
|
-
import fs from
|
|
15
|
+
import fs from "fs"
|
|
16
16
|
|
|
17
17
|
const encodedTemplate = base128.encode(fs.readFileSync("example.gz")).toJSTemplateLiterals()
|
|
18
18
|
|
|
19
19
|
const decoded = base128.decode(eval(encodedTemplate))
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
### Browser
|
|
23
|
-
|
|
24
|
-
```html browser
|
|
25
|
-
<script>
|
|
26
|
-
{class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}this.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=t.length,r=new Uint8Array(Math.ceil(e/7*8)),n=0,c=0;n<e;)r[c++]=t[n]>>1&127,r[c++]=127&(t[n++]<<6|t[n]>>2),r[c++]=127&(t[n++]<<5|t[n]>>3),r[c++]=127&(t[n++]<<4|t[n]>>4),r[c++]=127&(t[n++]<<3|t[n]>>5),r[c++]=127&(t[n++]<<2|t[n]>>6),r[c++]=127&(t[n++]<<1|t[n]>>7),r[c++]=127&t[n++];return new EncodeOutput(r)},decode(t){for(var e,r=t.length,n=new Uint8Array(r/8*7),c=0,o=0,i=()=>(e=t.charCodeAt(c++))>127?e=0:e;c<r;)n[o++]=i()<<1|i()>>6,n[o++]=e<<2|i()>>5,n[o++]=e<<3|i()>>4,n[o++]=e<<4|i()>>3,n[o++]=e<<5|i()>>2,n[o++]=e<<6|i()>>1,n[o++]=e<<7|i();return n}}}
|
|
27
|
-
</script>
|
|
28
|
-
```
|
|
29
|
-
|
|
30
22
|
---
|
|
31
23
|
|
|
32
24
|
## Effect
|
|
@@ -37,14 +29,10 @@ Encode this jpg file, use base128 is `109.85 KiB` smaller than base64:
|
|
|
37
29
|
screenshot-45.519.jpg
|
|
38
30
|
file length: 682086
|
|
39
31
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
eval length: 779527
|
|
43
|
-
decoded length: 682086
|
|
32
|
+
base128:
|
|
33
|
+
toJSTemplateLiterals length: 796961
|
|
44
34
|
equal: true
|
|
45
35
|
|
|
46
36
|
base64:
|
|
47
|
-
length: 909448
|
|
37
|
+
encoded length: 909448
|
|
48
38
|
```
|
|
49
|
-
|
|
50
|
-

|
package/main.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class Base128Bytes extends Uint8Array {
|
|
2
|
+
/**
|
|
3
|
+
* Returns a base128 string.
|
|
4
|
+
*/
|
|
5
|
+
toString(): string;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a base128 [Template literals](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
|
|
8
|
+
*/
|
|
9
|
+
toJSTemplateLiterals(): string;
|
|
10
|
+
}
|
|
11
|
+
export declare function encode(input: Uint8Array | string): Base128Bytes;
|
|
12
|
+
export declare function decode(input: string): Uint8Array;
|
|
13
|
+
declare const base128: {
|
|
14
|
+
Base128Bytes: typeof Base128Bytes;
|
|
15
|
+
encode: typeof encode;
|
|
16
|
+
decode: typeof decode;
|
|
17
|
+
};
|
|
18
|
+
export default base128;
|
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
//@ts-nocheck
|
|
2
2
|
|
|
3
|
-
export class
|
|
4
|
-
/**
|
|
5
|
-
* @param {Uint8Array} out
|
|
6
|
-
*/
|
|
7
|
-
constructor(out) {
|
|
8
|
-
this.uint8Array = out
|
|
9
|
-
}
|
|
3
|
+
export class Base128Bytes extends Uint8Array {
|
|
10
4
|
toString() {
|
|
11
|
-
return new TextDecoder().decode(this
|
|
5
|
+
return new TextDecoder().decode(this)
|
|
12
6
|
}
|
|
13
7
|
toJSTemplateLiterals() {
|
|
14
8
|
return `\`${this.toString().replace(
|
|
15
|
-
/[\r\\`]
|
|
9
|
+
/[\r\\`]|\$\{|<\/script/g,
|
|
16
10
|
(match) => (
|
|
17
11
|
match == '\r'
|
|
18
12
|
? '\\r'
|
|
@@ -31,7 +25,7 @@ export function encode(input) {
|
|
|
31
25
|
if (typeof input == 'string')
|
|
32
26
|
input = new TextEncoder().encode(input)
|
|
33
27
|
var il = input.length
|
|
34
|
-
, out = new
|
|
28
|
+
, out = new Base128Bytes(Math.ceil(il / 7 * 8))
|
|
35
29
|
, ii = 0
|
|
36
30
|
, oi = 0
|
|
37
31
|
while (ii < il) {
|
|
@@ -48,7 +42,7 @@ export function encode(input) {
|
|
|
48
42
|
/* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
|
|
49
43
|
/* 7 */ out[oi++] = input[ii++] & 127
|
|
50
44
|
}
|
|
51
|
-
return
|
|
45
|
+
return out
|
|
52
46
|
}
|
|
53
47
|
|
|
54
48
|
/**
|
|
@@ -82,7 +76,7 @@ export function decode(input) {
|
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
export default {
|
|
85
|
-
|
|
79
|
+
Base128Bytes,
|
|
86
80
|
encode,
|
|
87
81
|
decode
|
|
88
82
|
}
|
package/package.json
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "base128-ascii",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"author": "bddjr",
|
|
5
|
+
"license": "Unlicense",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./main.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"main.js",
|
|
10
|
+
"main.d.ts"
|
|
11
|
+
],
|
|
8
12
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"test": "node --run build && node scripts/test.mjs",
|
|
13
|
+
"test": "es-check es6 --checkFeatures --module main.js && node scripts/test.mjs",
|
|
11
14
|
"prepublishOnly": "node --run test"
|
|
12
15
|
},
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
15
18
|
"url": "git+https://github.com/bddjr/base128.git"
|
|
16
19
|
},
|
|
17
|
-
"author": "bddjr",
|
|
18
|
-
"license": "Unlicense",
|
|
19
|
-
"files": [
|
|
20
|
-
"dist",
|
|
21
|
-
"src"
|
|
22
|
-
],
|
|
23
20
|
"keywords": [
|
|
24
21
|
"base128",
|
|
25
22
|
"base64",
|
|
23
|
+
"base",
|
|
26
24
|
"ascii",
|
|
27
25
|
"encode",
|
|
28
26
|
"decode",
|
|
@@ -30,8 +28,6 @@
|
|
|
30
28
|
"btoa"
|
|
31
29
|
],
|
|
32
30
|
"devDependencies": {
|
|
33
|
-
"es-check": "^9.5.3"
|
|
34
|
-
"rimraf": "^6.0.1",
|
|
35
|
-
"terser": "^5.44.1"
|
|
31
|
+
"es-check": "^9.5.3"
|
|
36
32
|
}
|
|
37
33
|
}
|
package/dist/browser.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}this.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=t.length,r=new Uint8Array(Math.ceil(e/7*8)),n=0,c=0;n<e;)r[c++]=t[n]>>1&127,r[c++]=127&(t[n++]<<6|t[n]>>2),r[c++]=127&(t[n++]<<5|t[n]>>3),r[c++]=127&(t[n++]<<4|t[n]>>4),r[c++]=127&(t[n++]<<3|t[n]>>5),r[c++]=127&(t[n++]<<2|t[n]>>6),r[c++]=127&(t[n++]<<1|t[n]>>7),r[c++]=127&t[n++];return new EncodeOutput(r)},decode(t){for(var e,r=t.length,n=new Uint8Array(r/8*7),c=0,o=0,i=()=>(e=t.charCodeAt(c++))>127?e=0:e;c<r;)n[o++]=i()<<1|i()>>6,n[o++]=e<<2|i()>>5,n[o++]=e<<3|i()>>4,n[o++]=e<<4|i()>>3,n[o++]=e<<5|i()>>2,n[o++]=e<<6|i()>>1,n[o++]=e<<7|i();return n}}}
|
package/src/main.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
declare class EncodeOutput {
|
|
2
|
-
constructor(out: Uint8Array);
|
|
3
|
-
uint8Array: Uint8Array;
|
|
4
|
-
toString(): string;
|
|
5
|
-
toJSTemplateLiterals(): string;
|
|
6
|
-
}
|
|
7
|
-
declare const base128: {
|
|
8
|
-
EncodeOutput: typeof EncodeOutput;
|
|
9
|
-
encode(input: Uint8Array | string): EncodeOutput;
|
|
10
|
-
decode(input: string): Uint8Array;
|
|
11
|
-
};
|
|
12
|
-
export = base128;
|
package/src/main.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
//@ts-nocheck
|
|
2
|
-
|
|
3
|
-
class EncodeOutput {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Uint8Array} out
|
|
6
|
-
*/
|
|
7
|
-
constructor(out) {
|
|
8
|
-
this.uint8Array = out
|
|
9
|
-
}
|
|
10
|
-
toString() {
|
|
11
|
-
return new TextDecoder().decode(this.uint8Array)
|
|
12
|
-
}
|
|
13
|
-
toJSTemplateLiterals() {
|
|
14
|
-
return `\`${this.toString().replace(
|
|
15
|
-
/[\r\\`]|\${|<\/script/g,
|
|
16
|
-
(match) => (
|
|
17
|
-
match == '\r'
|
|
18
|
-
? '\\r'
|
|
19
|
-
: match == '</script'
|
|
20
|
-
? '<\\/script'
|
|
21
|
-
: '\\' + match
|
|
22
|
-
)
|
|
23
|
-
)}\``
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = {
|
|
28
|
-
EncodeOutput,
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @param {Uint8Array | string} input
|
|
32
|
-
*/
|
|
33
|
-
encode(input) {
|
|
34
|
-
if (typeof input == 'string')
|
|
35
|
-
input = new TextEncoder().encode(input)
|
|
36
|
-
var il = input.length
|
|
37
|
-
, out = new Uint8Array(Math.ceil(il / 7 * 8))
|
|
38
|
-
, ii = 0
|
|
39
|
-
, oi = 0
|
|
40
|
-
while (ii < il) {
|
|
41
|
-
// 0 1 2 3 4 5 6 7
|
|
42
|
-
// in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
|
|
43
|
-
// out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
|
|
44
|
-
|
|
45
|
-
/* 0 */ out[oi++] = input[ii] >> 1 & 127
|
|
46
|
-
/* 1 */ out[oi++] = (input[ii++] << 6 | input[ii] >> 2) & 127
|
|
47
|
-
/* 2 */ out[oi++] = (input[ii++] << 5 | input[ii] >> 3) & 127
|
|
48
|
-
/* 3 */ out[oi++] = (input[ii++] << 4 | input[ii] >> 4) & 127
|
|
49
|
-
/* 4 */ out[oi++] = (input[ii++] << 3 | input[ii] >> 5) & 127
|
|
50
|
-
/* 5 */ out[oi++] = (input[ii++] << 2 | input[ii] >> 6) & 127
|
|
51
|
-
/* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
|
|
52
|
-
/* 7 */ out[oi++] = input[ii++] & 127
|
|
53
|
-
}
|
|
54
|
-
return new EncodeOutput(out)
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @param {string} input
|
|
59
|
-
*/
|
|
60
|
-
decode(input) {
|
|
61
|
-
var il = input.length
|
|
62
|
-
, out = new Uint8Array(il / 8 * 7)
|
|
63
|
-
, ii = 0
|
|
64
|
-
, oi = 0
|
|
65
|
-
, cache
|
|
66
|
-
, next = () => (
|
|
67
|
-
(cache = input.charCodeAt(ii++)) > 127
|
|
68
|
-
? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
|
|
69
|
-
: cache
|
|
70
|
-
)
|
|
71
|
-
while (ii < il) {
|
|
72
|
-
// 0 1 2 3 4 5 6 7
|
|
73
|
-
// in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
|
|
74
|
-
// out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
|
|
75
|
-
|
|
76
|
-
/* 0 */ out[oi++] = next() << 1 | next() >> 6
|
|
77
|
-
/* 1 */ out[oi++] = cache << 2 | next() >> 5
|
|
78
|
-
/* 2 */ out[oi++] = cache << 3 | next() >> 4
|
|
79
|
-
/* 3 */ out[oi++] = cache << 4 | next() >> 3
|
|
80
|
-
/* 4 */ out[oi++] = cache << 5 | next() >> 2
|
|
81
|
-
/* 5 */ out[oi++] = cache << 6 | next() >> 1
|
|
82
|
-
/* 6 */ out[oi++] = cache << 7 | next()
|
|
83
|
-
}
|
|
84
|
-
return out
|
|
85
|
-
}
|
|
86
|
-
}
|
package/src/module.d.mts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare class EncodeOutput {
|
|
2
|
-
constructor(out: Uint8Array);
|
|
3
|
-
uint8Array: Uint8Array;
|
|
4
|
-
toString(): string;
|
|
5
|
-
toJSTemplateLiterals(): string;
|
|
6
|
-
}
|
|
7
|
-
export declare function encode(input: Uint8Array | string): EncodeOutput;
|
|
8
|
-
export declare function decode(input: string): Uint8Array;
|
|
9
|
-
declare const base128: {
|
|
10
|
-
EncodeOutput: typeof EncodeOutput;
|
|
11
|
-
encode: typeof encode;
|
|
12
|
-
decode: typeof decode;
|
|
13
|
-
};
|
|
14
|
-
export default base128;
|