base128-ascii 5.0.0 → 5.0.2

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.
Files changed (4) hide show
  1. package/README.md +26 -4
  2. package/main.d.ts +8 -3
  3. package/main.js +30 -26
  4. package/package.json +4 -3
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 `109.85 KiB` smaller than base64:
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.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- export declare class EncodeResult {
2
- constructor(bytes: Uint8Array<ArrayBuffer>);
3
-
1
+ interface EncodeResultPrototype {
4
2
  /**
5
3
  * Returns a base128 string.
6
4
  */
@@ -10,10 +8,17 @@ export declare class EncodeResult {
10
8
  * Returns a base128 [Template literals](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
11
9
  */
12
10
  toJSTemplateLiterals(): string;
11
+ }
13
12
 
13
+ interface EncodeResult extends EncodeResultPrototype {
14
14
  bytes: Uint8Array<ArrayBuffer>;
15
15
  }
16
16
 
17
+ export declare var EncodeResult: {
18
+ new(bytes: Uint8Array<ArrayBuffer>): EncodeResult;
19
+ prototype: EncodeResultPrototype;
20
+ };
21
+
17
22
  export declare function encode(input: Uint8Array): EncodeResult;
18
23
 
19
24
  export declare function decode(input: string): Uint8Array<ArrayBuffer>;
package/main.js CHANGED
@@ -1,5 +1,3 @@
1
- //@ts-nocheck
2
-
3
1
  export class EncodeResult {
4
2
  /**
5
3
  * @param {Uint8Array<ArrayBuffer>} bytes
@@ -7,16 +5,13 @@ export class EncodeResult {
7
5
  constructor(bytes) {
8
6
  this.bytes = bytes
9
7
  }
10
- toString() {
11
- return new TextDecoder().decode(this.bytes)
12
- }
13
8
  toJSTemplateLiterals() {
14
9
  return `\`${this.toString().replace(
15
10
  /[\r\\`]|\$\{|<\/script/g,
16
11
  (match) => (
17
- match == '\r'
12
+ match === '\r'
18
13
  ? '\\r'
19
- : match == '</script'
14
+ : match === '</script'
20
15
  ? '<\\/script'
21
16
  : '\\' + match
22
17
  )
@@ -24,6 +19,21 @@ export class EncodeResult {
24
19
  }
25
20
  }
26
21
 
22
+ if (
23
+ typeof Buffer == 'function' &&
24
+ Buffer.prototype &&
25
+ typeof Buffer.prototype.latin1Slice == 'function'
26
+ ) {
27
+ EncodeResult.prototype.toString = function () {
28
+ return Buffer.prototype.latin1Slice.call(this.bytes)
29
+ }
30
+ } else {
31
+ let _textDecoder
32
+ EncodeResult.prototype.toString = function () {
33
+ return (_textDecoder || (_textDecoder = new TextDecoder)).decode(this.bytes)
34
+ }
35
+ }
36
+
27
37
  /**
28
38
  * @param {Uint8Array} input
29
39
  */
@@ -37,14 +47,14 @@ export function encode(input) {
37
47
  // in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
38
48
  // out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
39
49
 
40
- /* 0 */ out[oi++] = input[ii] >> 1 & 127
41
- /* 1 */ out[oi++] = (input[ii++] << 6 | input[ii] >> 2) & 127
42
- /* 2 */ out[oi++] = (input[ii++] << 5 | input[ii] >> 3) & 127
43
- /* 3 */ out[oi++] = (input[ii++] << 4 | input[ii] >> 4) & 127
44
- /* 4 */ out[oi++] = (input[ii++] << 3 | input[ii] >> 5) & 127
45
- /* 5 */ out[oi++] = (input[ii++] << 2 | input[ii] >> 6) & 127
46
- /* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
47
- /* 7 */ out[oi++] = input[ii++] & 127
50
+ /* 0 */ out[oi++] = 127 & input[ii] >> 1
51
+ /* 1 */ out[oi++] = 127 & (input[ii++] << 6 | input[ii] >> 2)
52
+ /* 2 */ out[oi++] = 127 & (input[ii++] << 5 | input[ii] >> 3)
53
+ /* 3 */ out[oi++] = 127 & (input[ii++] << 4 | input[ii] >> 4)
54
+ /* 4 */ out[oi++] = 127 & (input[ii++] << 3 | input[ii] >> 5)
55
+ /* 5 */ out[oi++] = 127 & (input[ii++] << 2 | input[ii] >> 6)
56
+ /* 6 */ out[oi++] = 127 & (input[ii++] << 1 | input[ii] >> 7)
57
+ /* 7 */ out[oi++] = 127 & input[ii++]
48
58
  }
49
59
  return new EncodeResult(out)
50
60
  }
@@ -57,24 +67,18 @@ export function decode(input) {
57
67
  , out = new Uint8Array(il / 8 * 7)
58
68
  , ii = 0
59
69
  , oi = 0
70
+ , k
60
71
  , cache
61
- , next = () => (
62
- (cache = input.charCodeAt(ii++)) > 127
72
+ , next = _ =>
73
+ (cache = input.charCodeAt(ii++)) >> 7
63
74
  ? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
64
75
  : cache
65
- )
66
76
  while (ii < il) {
67
77
  // 0 1 2 3 4 5 6 7
68
78
  // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
69
79
  // out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
70
-
71
- /* 0 */ out[oi++] = next() << 1 | next() >> 6
72
- /* 1 */ out[oi++] = cache << 2 | next() >> 5
73
- /* 2 */ out[oi++] = cache << 3 | next() >> 4
74
- /* 3 */ out[oi++] = cache << 4 | next() >> 3
75
- /* 4 */ out[oi++] = cache << 5 | next() >> 2
76
- /* 5 */ out[oi++] = cache << 6 | next() >> 1
77
- /* 6 */ out[oi++] = cache << 7 | next()
80
+ k || next(k = 7)
81
+ out[oi++] = cache << 8 - k | next() >> --k
78
82
  }
79
83
  return out
80
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "author": "bddjr",
5
5
  "license": "Unlicense",
6
6
  "type": "module",
@@ -10,7 +10,7 @@
10
10
  "main.d.ts"
11
11
  ],
12
12
  "scripts": {
13
- "test": "es-check es6 --checkFeatures --module main.js && node scripts/test.mjs",
13
+ "test": "es-check es6 --checkFeatures --module main.js && tsc && node scripts/test.mjs",
14
14
  "prepublishOnly": "node --run test"
15
15
  },
16
16
  "repository": {
@@ -28,6 +28,7 @@
28
28
  "btoa"
29
29
  ],
30
30
  "devDependencies": {
31
- "es-check": "^9.5.3"
31
+ "es-check": "^9.5.3",
32
+ "typescript": "^6.0.3"
32
33
  }
33
34
  }