base128-ascii 5.0.1 → 5.0.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 -4
- package/main.js +21 -14
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -4,10 +4,8 @@ Build for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plu
|
|
|
4
4
|
|
|
5
5
|
## Setup
|
|
6
6
|
|
|
7
|
-
### npm
|
|
8
|
-
|
|
9
7
|
```
|
|
10
|
-
npm i base128-ascii
|
|
8
|
+
npm i base128-ascii@latest
|
|
11
9
|
```
|
|
12
10
|
|
|
13
11
|
```js
|
|
@@ -25,16 +23,40 @@ const decodedBytes = base128.decode(eval(encodedTemplate))
|
|
|
25
23
|
|
|
26
24
|
## Effect
|
|
27
25
|
|
|
28
|
-
Encode this jpg file, use base128 is `
|
|
26
|
+
Encode this jpg file, use base128 is `112487 Bytes` smaller than base64:
|
|
29
27
|
|
|
30
28
|
```
|
|
31
29
|
screenshot-45.519.jpg
|
|
32
30
|
file length: 682086
|
|
33
31
|
|
|
34
32
|
base128:
|
|
33
|
+
time encode: 7.306ms
|
|
34
|
+
time toString: 0.168ms
|
|
35
|
+
time toJSTemplateLiterals: 1.876ms
|
|
35
36
|
toJSTemplateLiterals length: 796961
|
|
37
|
+
time eval: 6.242ms
|
|
38
|
+
time decode: 10.614ms
|
|
36
39
|
equal: true
|
|
37
40
|
|
|
38
41
|
base64:
|
|
39
42
|
encoded length: 909448
|
|
40
43
|
```
|
|
44
|
+
|
|
45
|
+
Encode `50MB` file, use base128 is `8180525 Bytes` smaller than base64:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
50MB
|
|
49
|
+
file length: 50000000
|
|
50
|
+
|
|
51
|
+
base128:
|
|
52
|
+
time encode: 67.717ms
|
|
53
|
+
time toString: 10.025ms
|
|
54
|
+
time toJSTemplateLiterals: 197.364ms
|
|
55
|
+
toJSTemplateLiterals length: 58486143
|
|
56
|
+
time eval: 1.083s
|
|
57
|
+
time decode: 171.262ms
|
|
58
|
+
equal: true
|
|
59
|
+
|
|
60
|
+
base64:
|
|
61
|
+
encoded length: 66666668
|
|
62
|
+
```
|
package/main.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
let _bytesToStr = (
|
|
2
|
+
(
|
|
3
|
+
typeof Buffer == 'function' &&
|
|
4
|
+
Buffer.prototype &&
|
|
5
|
+
typeof Buffer.prototype.latin1Slice == 'function' &&
|
|
6
|
+
// Exclude Deno. See https://github.com/bddjr/base128/pull/4
|
|
7
|
+
typeof Deno == 'undefined'
|
|
8
|
+
)
|
|
9
|
+
? (bytes) => Buffer.prototype.latin1Slice.call(bytes)
|
|
10
|
+
: (bytes) => {
|
|
11
|
+
// TextDecoder keeps the default UTF-8, which is already the fastest.
|
|
12
|
+
const td = new TextDecoder
|
|
13
|
+
return (_bytesToStr = (bytes) => td.decode(bytes))(bytes)
|
|
14
|
+
}
|
|
15
|
+
)
|
|
16
|
+
|
|
1
17
|
export class EncodeResult {
|
|
2
18
|
/**
|
|
3
19
|
* @param {Uint8Array<ArrayBuffer>} bytes
|
|
@@ -5,6 +21,9 @@ export class EncodeResult {
|
|
|
5
21
|
constructor(bytes) {
|
|
6
22
|
this.bytes = bytes
|
|
7
23
|
}
|
|
24
|
+
toString() {
|
|
25
|
+
return _bytesToStr(this.bytes)
|
|
26
|
+
}
|
|
8
27
|
toJSTemplateLiterals() {
|
|
9
28
|
return `\`${this.toString().replace(
|
|
10
29
|
/[\r\\`]|\$\{|<\/script/g,
|
|
@@ -19,17 +38,6 @@ export class EncodeResult {
|
|
|
19
38
|
}
|
|
20
39
|
}
|
|
21
40
|
|
|
22
|
-
if (typeof Buffer == 'function' && Buffer.prototype && typeof Buffer.prototype.latin1Slice == 'function') {
|
|
23
|
-
EncodeResult.prototype.toString = function () {
|
|
24
|
-
return Buffer.prototype.latin1Slice.call(this.bytes)
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
let _textDecoder
|
|
28
|
-
EncodeResult.prototype.toString = function () {
|
|
29
|
-
return (_textDecoder || (_textDecoder = new TextDecoder)).decode(this.bytes)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
41
|
/**
|
|
34
42
|
* @param {Uint8Array} input
|
|
35
43
|
*/
|
|
@@ -73,9 +81,8 @@ export function decode(input) {
|
|
|
73
81
|
// 0 1 2 3 4 5 6 7
|
|
74
82
|
// in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
|
|
75
83
|
// out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
84
|
+
k || next(k = 7)
|
|
85
|
+
out[oi++] = cache << 8 - k | next() >> --k
|
|
79
86
|
}
|
|
80
87
|
return out
|
|
81
88
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "base128-ascii",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"author": "bddjr",
|
|
5
5
|
"license": "Unlicense",
|
|
6
6
|
"type": "module",
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "es-check es6 --checkFeatures --module main.js && tsc && node scripts/test.mjs",
|
|
14
|
+
"test:deno": "deno run --allow-read=. --allow-write=. scripts/test.mjs",
|
|
15
|
+
"test:bun": "bun scripts/test.mjs",
|
|
14
16
|
"prepublishOnly": "node --run test"
|
|
15
17
|
},
|
|
16
18
|
"repository": {
|