base128-ascii 2.1.1 → 2.1.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 +32 -2
- package/dist/browser.min.js +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +21 -20
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ The project was born for [vite-plugin-singlefile-compression](https://github.com
|
|
|
4
4
|
|
|
5
5
|
## Setup
|
|
6
6
|
|
|
7
|
+
### npm
|
|
8
|
+
|
|
7
9
|
```
|
|
8
10
|
npm i base128-ascii
|
|
9
11
|
```
|
|
@@ -12,11 +14,39 @@ npm i base128-ascii
|
|
|
12
14
|
import base128 from "base128-ascii"
|
|
13
15
|
import fs from 'fs'
|
|
14
16
|
|
|
15
|
-
const encodedTemplate = base128.encode(
|
|
17
|
+
const encodedTemplate = base128.encode(fs.readFileSync("example.gz")).toJSTemplateLiterals()
|
|
16
18
|
|
|
17
19
|
const decoded = base128.decode(eval(encodedTemplate))
|
|
18
20
|
```
|
|
19
21
|
|
|
22
|
+
### Browser (jsDelivr CDN)
|
|
23
|
+
|
|
24
|
+
#### HTML
|
|
25
|
+
```html
|
|
26
|
+
<script src="https://cdn.jsdelivr.net/npm/base128-ascii/dist/browser.min.js"></script>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### JS
|
|
30
|
+
```js
|
|
31
|
+
new Promise((rs) => {
|
|
32
|
+
if (self.base128?.EncodeOutput) return rs();
|
|
33
|
+
Object.defineProperty(self, "base128", {
|
|
34
|
+
configurable: true,
|
|
35
|
+
set(value) {
|
|
36
|
+
Object.defineProperty(this, "base128", {
|
|
37
|
+
configurable: true,
|
|
38
|
+
value,
|
|
39
|
+
});
|
|
40
|
+
rs();
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
document.head.appendChild(document.createElement("script")).src =
|
|
44
|
+
"https://cdn.jsdelivr.net/npm/base128-ascii/dist/browser.min.js";
|
|
45
|
+
}).then(() => {
|
|
46
|
+
console.log("base128 loaded!", self.base128);
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
20
50
|
---
|
|
21
51
|
|
|
22
52
|
## Effect
|
|
@@ -37,4 +67,4 @@ base64:
|
|
|
37
67
|
length: 909448
|
|
38
68
|
```
|
|
39
69
|
|
|
40
|
-

|
|
70
|
+

|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var base128=(()=>{var s=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var w=(e,r)=>{for(var o in r)s(e,o,{get:r[o],enumerable:!0})},x=(e,r,o,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of d(r))!g.call(e,a)&&a!==o&&s(e,a,{get:()=>r[a],enumerable:!(t=i(r,a))||t.enumerable});return e};var A=e=>x(s({},"__esModule",{value:!0}),e);var S={};w(S,{EncodeOutput:()=>l,decode:()=>n,default:()=>f,encode:()=>h});var l=class{constructor(r){this.uint8Array=r}uint8Array;toString(){return new TextDecoder().decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,r=>r=="\r"?"\\r":r=="<\/script"?"<\\/script":"\\"+r)}\``}};function h(e){for(var r=new Uint8Array(Math.ceil(e.length/7*8)),o=0,t=0;o<e.length;)r[t++]=e[o]>>1,r[t++]=e[o++]<<6&127|e[o]>>2,r[t++]=e[o++]<<5&127|e[o]>>3,r[t++]=e[o++]<<4&127|e[o]>>4,r[t++]=e[o++]<<3&127|e[o]>>5,r[t++]=e[o++]<<2&127|e[o]>>6,r[t++]=e[o++]<<1&127|e[o]>>7,r[t++]=e[o++]&127;return new l(r)}function n(e){for(var r=new Uint8Array(Math.floor(e.length/8*7)),o=0,t=0,a,c=()=>(a=e.charCodeAt(o++))>>7?a=0:a;o<e.length;)r[t++]=c()<<1|c()>>6,r[t++]=a<<2|c()>>5,r[t++]=a<<3|c()>>4,r[t++]=a<<4|c()>>3,r[t++]=a<<5|c()>>2,r[t++]=a<<6|c()>>1,r[t++]=a<<7|c();return r}var y={EncodeOutput:l,encode:h,decode:n},f=y;return A(S);})();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
1
|
+
export declare class EncodeOutput {
|
|
2
|
+
constructor(out: Uint8Array);
|
|
3
|
+
uint8Array: Uint8Array;
|
|
3
4
|
toString(): string;
|
|
4
5
|
toJSTemplateLiterals(): string;
|
|
5
|
-
}
|
|
6
|
-
export declare function
|
|
6
|
+
}
|
|
7
|
+
export declare function encode(input: ArrayLike<number>): EncodeOutput;
|
|
8
|
+
export declare function decode(input: string): Uint8Array;
|
|
7
9
|
declare const base128: {
|
|
10
|
+
EncodeOutput: typeof EncodeOutput;
|
|
8
11
|
encode: typeof encode;
|
|
9
12
|
decode: typeof decode;
|
|
10
13
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
export class EncodeOutput {
|
|
2
|
+
constructor(out) {
|
|
3
|
+
this.uint8Array = out;
|
|
4
|
+
}
|
|
5
|
+
uint8Array;
|
|
6
|
+
toString() {
|
|
7
|
+
return new TextDecoder().decode(this.uint8Array);
|
|
8
|
+
}
|
|
9
|
+
toJSTemplateLiterals() {
|
|
10
|
+
return `\`${this.toString().replace(/[\r\\`]|\${|<\/script/g, (match) => (match == '\r'
|
|
11
|
+
? '\\r'
|
|
12
|
+
: match == '</script'
|
|
13
|
+
? '<\\/script'
|
|
14
|
+
: '\\' + match))}\``;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
1
17
|
export function encode(input) {
|
|
2
|
-
|
|
3
|
-
let ii = 0, oi = 0;
|
|
18
|
+
var out = new Uint8Array(Math.ceil(input.length / 7 * 8)), ii = 0, oi = 0;
|
|
4
19
|
while (ii < input.length) {
|
|
5
20
|
// 0 1 2 3 4 5 6 7
|
|
6
21
|
// in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
|
|
@@ -14,26 +29,11 @@ export function encode(input) {
|
|
|
14
29
|
/* 6 */ out[oi++] = input[ii++] << 1 & 127 | input[ii] >> 7;
|
|
15
30
|
/* 7 */ out[oi++] = input[ii++] & 127;
|
|
16
31
|
}
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
uint8Array: out,
|
|
20
|
-
toString() {
|
|
21
|
-
return str ??= new TextDecoder().decode(out);
|
|
22
|
-
},
|
|
23
|
-
toJSTemplateLiterals() {
|
|
24
|
-
return jstl ??= `\`${this.toString().replace(/[\r\\`]|\${|<\/script/g, (match) => (match == '\r'
|
|
25
|
-
? '\\r'
|
|
26
|
-
: match == '</script'
|
|
27
|
-
? '<\\/script'
|
|
28
|
-
: '\\' + match))}\``;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
32
|
+
return new EncodeOutput(out);
|
|
31
33
|
}
|
|
32
34
|
export function decode(input) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const next = () => ((cache = input.charCodeAt(ii++)) >> 7
|
|
36
|
-
? cache = 0 // In HTML, 0 is likely to be converted to 65533
|
|
35
|
+
var out = new Uint8Array(Math.floor(input.length / 8 * 7)), ii = 0, oi = 0, cache, next = () => ((cache = input.charCodeAt(ii++)) >> 7
|
|
36
|
+
? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
|
|
37
37
|
: cache);
|
|
38
38
|
while (ii < input.length) {
|
|
39
39
|
// 0 1 2 3 4 5 6 7
|
|
@@ -50,6 +50,7 @@ export function decode(input) {
|
|
|
50
50
|
return out;
|
|
51
51
|
}
|
|
52
52
|
const base128 = {
|
|
53
|
+
EncodeOutput,
|
|
53
54
|
encode,
|
|
54
55
|
decode
|
|
55
56
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "base128-ascii",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
|
+
"browser": "dist/browser.min.js",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"build": "rimraf dist && tsc",
|
|
8
|
+
"build": "rimraf dist && tsc && esbuild dist/index.js --bundle --global-name=base128 --minify --outfile=dist/browser.min.js",
|
|
8
9
|
"test": "npm run build && node test.js",
|
|
9
10
|
"prepublishOnly": "npm run build"
|
|
10
11
|
},
|
|
@@ -24,6 +25,7 @@
|
|
|
24
25
|
],
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"@types/node": "^24.0.3",
|
|
28
|
+
"esbuild": "^0.27.2",
|
|
27
29
|
"rimraf": "^6.0.1",
|
|
28
30
|
"typescript": "^5.7.3"
|
|
29
31
|
}
|