@dra2020/baseclient 1.0.51 → 1.0.54
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/dist/baseclient.js +96 -0
- package/dist/baseclient.js.map +1 -1
- package/dist/util/all.d.ts +1 -0
- package/dist/util/bitset.d.ts +19 -0
- package/lib/util/all.ts +1 -0
- package/lib/util/bitset.ts +105 -0
- package/package.json +1 -1
package/dist/util/all.d.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface Converter {
|
|
2
|
+
atob?: (s: string) => string;
|
|
3
|
+
btoa?: (s: string) => string;
|
|
4
|
+
}
|
|
5
|
+
export declare class ListToBitset {
|
|
6
|
+
list: string[];
|
|
7
|
+
index: {
|
|
8
|
+
[s: string]: number;
|
|
9
|
+
};
|
|
10
|
+
size: number;
|
|
11
|
+
converter: Converter;
|
|
12
|
+
constructor(list: string[], converter?: Converter);
|
|
13
|
+
toBits(l: string[]): Uint8Array;
|
|
14
|
+
toList(u8: Uint8Array): string[];
|
|
15
|
+
base64tou8(base64: string): Uint8Array;
|
|
16
|
+
u8ToBase64(u8: Uint8Array): string;
|
|
17
|
+
fromBitString(base64: string): string[];
|
|
18
|
+
toBitString(l: string[]): string;
|
|
19
|
+
}
|
package/lib/util/all.ts
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const BitLookup = [ 1, 2, 4, 8, 16, 32, 64, 128 ];
|
|
2
|
+
|
|
3
|
+
export interface Converter
|
|
4
|
+
{
|
|
5
|
+
atob?: (s: string) => string;
|
|
6
|
+
btoa?: (s: string) => string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// In NodeJS:
|
|
10
|
+
|
|
11
|
+
// {
|
|
12
|
+
// btoa: (s: string) => { return Buffer.from(s, 'binary').toString('base64') },
|
|
13
|
+
// atob: (s: string) => { return Buffer.from(s, 'base64').toString('binary') },
|
|
14
|
+
// }
|
|
15
|
+
|
|
16
|
+
// In Browser:
|
|
17
|
+
|
|
18
|
+
// {
|
|
19
|
+
// btoa: window.btoa,
|
|
20
|
+
// atob: window.atob,
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
export class ListToBitset
|
|
24
|
+
{
|
|
25
|
+
list: string[];
|
|
26
|
+
index: { [s: string]: number };
|
|
27
|
+
size: number;
|
|
28
|
+
converter: Converter;
|
|
29
|
+
|
|
30
|
+
constructor(list: string[], converter?: Converter)
|
|
31
|
+
{
|
|
32
|
+
this.list = list;
|
|
33
|
+
this.converter = converter;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
toBits(l: string[]): Uint8Array
|
|
37
|
+
{
|
|
38
|
+
if (! this.index)
|
|
39
|
+
{
|
|
40
|
+
this.size = Math.floor((this.list.length+7)/8);
|
|
41
|
+
this.index = {};
|
|
42
|
+
this.list.forEach((s: string, i: number) => { this.index[s] = i });
|
|
43
|
+
}
|
|
44
|
+
let ab = new ArrayBuffer(this.size);
|
|
45
|
+
let u8 = new Uint8Array(ab);
|
|
46
|
+
if (l) l.forEach(s => {
|
|
47
|
+
let n = this.index[s];
|
|
48
|
+
let i = Math.floor(n/8);
|
|
49
|
+
u8[i] |= BitLookup[n % 8];
|
|
50
|
+
});
|
|
51
|
+
return u8;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
toList(u8: Uint8Array): string[]
|
|
55
|
+
{
|
|
56
|
+
let list: string[] = [];
|
|
57
|
+
for (let i = 0; i < u8.length; i++)
|
|
58
|
+
{
|
|
59
|
+
let u = u8[i];
|
|
60
|
+
if (u)
|
|
61
|
+
{
|
|
62
|
+
if (u & 1) list.push(this.list[i*8+0]);
|
|
63
|
+
if (u & 2) list.push(this.list[i*8+1]);
|
|
64
|
+
if (u & 4) list.push(this.list[i*8+2]);
|
|
65
|
+
if (u & 8) list.push(this.list[i*8+3]);
|
|
66
|
+
if (u & 16) list.push(this.list[i*8+4]);
|
|
67
|
+
if (u & 32) list.push(this.list[i*8+5]);
|
|
68
|
+
if (u & 64) list.push(this.list[i*8+6]);
|
|
69
|
+
if (u & 128) list.push(this.list[i*8+7]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return list;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
base64tou8(base64: string): Uint8Array
|
|
76
|
+
{
|
|
77
|
+
let raw = this.converter.atob(base64);
|
|
78
|
+
let rawLength = raw.length;
|
|
79
|
+
let u8 = new Uint8Array(new ArrayBuffer(rawLength));
|
|
80
|
+
for (let i = 0; i < rawLength; i++)
|
|
81
|
+
u8[i] = raw.charCodeAt(i);
|
|
82
|
+
return u8;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
u8ToBase64(u8: Uint8Array): string
|
|
86
|
+
{
|
|
87
|
+
let binary: string[] = [];
|
|
88
|
+
let len = u8.byteLength;
|
|
89
|
+
for (let i = 0; i < len; i++) {
|
|
90
|
+
binary.push(String.fromCharCode(u8[i]));
|
|
91
|
+
}
|
|
92
|
+
return this.converter.btoa(binary.join(''));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fromBitString(base64: string): string[]
|
|
96
|
+
{
|
|
97
|
+
return this.toList(this.base64tou8(base64));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toBitString(l: string[]): string
|
|
101
|
+
{
|
|
102
|
+
// Note: On server, Buffer.from(this.toBits(l)).toString('base64') probably faster
|
|
103
|
+
return this.u8ToBase64(this.toBits(l));
|
|
104
|
+
}
|
|
105
|
+
}
|