@wtasnorg/node-lib 0.0.10 → 0.0.11
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/changelog.txt +14 -0
- package/docs/README.md +12 -0
- package/docs/docs.json +1438 -211
- package/docs/functions/createFindDirectories.md +1 -1
- package/docs/functions/decode.md +1 -1
- package/docs/functions/decode32.md +49 -0
- package/docs/functions/decode58.md +49 -0
- package/docs/functions/decode85.md +49 -0
- package/docs/functions/encode.md +1 -1
- package/docs/functions/encode32.md +45 -0
- package/docs/functions/encode58.md +45 -0
- package/docs/functions/encode85.md +45 -0
- package/docs/functions/hello.md +1 -1
- package/docs/functions/parseUserAgent.md +1 -1
- package/docs/functions/pojo.md +1 -1
- package/docs/interfaces/FileSystemDependencies.md +3 -3
- package/docs/interfaces/FindDirectoriesOptions.md +5 -5
- package/docs/interfaces/UserAgentInfo.md +6 -6
- package/docs/type-aliases/Base32CharsetType.md +13 -0
- package/docs/type-aliases/Base58CharsetType.md +13 -0
- package/docs/type-aliases/Base64CharsetType.md +1 -1
- package/docs/type-aliases/Base85CharsetType.md +13 -0
- package/docs/variables/Base32Charset.md +16 -0
- package/docs/variables/Base58Charset.md +16 -0
- package/docs/variables/Base64Charset.md +1 -1
- package/docs/variables/Base85Charset.md +16 -0
- package/package.json +41 -1
- package/readme.txt +12 -0
- package/src/base32.d.ts +58 -0
- package/src/base32.js +143 -0
- package/src/base32.test.d.ts +2 -0
- package/src/base32.test.js +121 -0
- package/src/base32.test.ts +144 -0
- package/src/base32.ts +169 -0
- package/src/base58.d.ts +58 -0
- package/src/base58.js +155 -0
- package/src/base58.test.d.ts +2 -0
- package/src/base58.test.js +108 -0
- package/src/base58.test.ts +128 -0
- package/src/base58.ts +177 -0
- package/src/base85.d.ts +58 -0
- package/src/base85.js +173 -0
- package/src/base85.test.d.ts +2 -0
- package/src/base85.test.js +107 -0
- package/src/base85.test.ts +125 -0
- package/src/base85.ts +199 -0
- package/src/index.d.ts +8 -2
- package/src/index.js +4 -1
- package/src/index.ts +20 -2
package/src/base85.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base85 encoding/decoding with multiple charset variants.
|
|
3
|
+
* Base85 provides ~25% better efficiency than Base64 (4:5 vs 3:4 ratio).
|
|
4
|
+
* @module base85
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Available Base85 charset variants.
|
|
8
|
+
* - `ascii85`: Adobe Ascii85 (btoa format)
|
|
9
|
+
* - `z85`: ZeroMQ Base85 (no quotes or backslash)
|
|
10
|
+
* - `rfc1924`: RFC 1924 IPv6 encoding
|
|
11
|
+
*/
|
|
12
|
+
const Base85Charset = ["ascii85", "z85", "rfc1924"];
|
|
13
|
+
/**
|
|
14
|
+
* Charset alphabets for Base85 variants.
|
|
15
|
+
* Each has exactly 85 printable ASCII characters.
|
|
16
|
+
*/
|
|
17
|
+
const CHARSETS = {
|
|
18
|
+
ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu",
|
|
19
|
+
z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#",
|
|
20
|
+
rfc1924: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Build a reverse lookup table for decoding.
|
|
24
|
+
* @param alphabet - The 85-character alphabet string.
|
|
25
|
+
* @returns Map of character to index.
|
|
26
|
+
*/
|
|
27
|
+
function buildDecodeTable(alphabet) {
|
|
28
|
+
const table = new Map();
|
|
29
|
+
for (let i = 0; i < alphabet.length; i++) {
|
|
30
|
+
const char = alphabet[i];
|
|
31
|
+
if (char !== undefined) {
|
|
32
|
+
table.set(char, i);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return table;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Pre-built decode tables for each charset.
|
|
39
|
+
*/
|
|
40
|
+
const DECODE_TABLES = {
|
|
41
|
+
ascii85: buildDecodeTable(CHARSETS.ascii85),
|
|
42
|
+
z85: buildDecodeTable(CHARSETS.z85),
|
|
43
|
+
rfc1924: buildDecodeTable(CHARSETS.rfc1924)
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Encode a string to Base85.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { encode85 } from "./base85.js";
|
|
51
|
+
*
|
|
52
|
+
* encode85("Hello");
|
|
53
|
+
* // => "87cURDZ"
|
|
54
|
+
*
|
|
55
|
+
* encode85("test", "z85");
|
|
56
|
+
* // => "wrx.P"
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @param input - The string to encode.
|
|
60
|
+
* @param charset - The charset variant to use (default: "ascii85").
|
|
61
|
+
* @returns The Base85 encoded string.
|
|
62
|
+
*/
|
|
63
|
+
function encode85(input, charset = "ascii85") {
|
|
64
|
+
if (input === "") {
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
const alphabet = CHARSETS[charset];
|
|
68
|
+
const bytes = new TextEncoder().encode(input);
|
|
69
|
+
let result = "";
|
|
70
|
+
// Process 4 bytes at a time
|
|
71
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
72
|
+
const chunkSize = Math.min(4, bytes.length - i);
|
|
73
|
+
// Pack up to 4 bytes into a value using multiplication to avoid
|
|
74
|
+
// 32-bit signed integer overflow from bitwise operations
|
|
75
|
+
let value = 0;
|
|
76
|
+
for (let j = 0; j < chunkSize; j++) {
|
|
77
|
+
value = value * 256 + (bytes[i + j] ?? 0);
|
|
78
|
+
}
|
|
79
|
+
// Pad with zeros for incomplete chunks
|
|
80
|
+
for (let j = chunkSize; j < 4; j++) {
|
|
81
|
+
value = value * 256;
|
|
82
|
+
}
|
|
83
|
+
// Convert to 5 base-85 digits
|
|
84
|
+
const encoded = [];
|
|
85
|
+
for (let j = 0; j < 5; j++) {
|
|
86
|
+
encoded.unshift(alphabet[value % 85] ?? "");
|
|
87
|
+
value = Math.floor(value / 85);
|
|
88
|
+
}
|
|
89
|
+
// For the last chunk, only output as many characters as needed
|
|
90
|
+
// (chunkSize bytes -> chunkSize + 1 characters)
|
|
91
|
+
if (i + 4 > bytes.length) {
|
|
92
|
+
result += encoded.slice(0, chunkSize + 1).join("");
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
result += encoded.join("");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Decode a Base85 string.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { decode85 } from "./base85.js";
|
|
106
|
+
*
|
|
107
|
+
* decode85("87cURDZ");
|
|
108
|
+
* // => "Hello"
|
|
109
|
+
*
|
|
110
|
+
* decode85("wrx.P", "z85");
|
|
111
|
+
* // => "test"
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @param input - The Base85 encoded string.
|
|
115
|
+
* @param charset - The charset variant to use (default: "ascii85").
|
|
116
|
+
* @returns The decoded string.
|
|
117
|
+
* @throws Error if the input contains invalid characters.
|
|
118
|
+
*/
|
|
119
|
+
function decode85(input, charset = "ascii85") {
|
|
120
|
+
if (input === "") {
|
|
121
|
+
return "";
|
|
122
|
+
}
|
|
123
|
+
const decodeTable = DECODE_TABLES[charset];
|
|
124
|
+
const bytes = [];
|
|
125
|
+
const inputLen = input.length;
|
|
126
|
+
// Calculate expected output length
|
|
127
|
+
// Full groups of 5 chars -> 4 bytes each
|
|
128
|
+
// Partial group: n chars -> n-1 bytes
|
|
129
|
+
const fullGroups = Math.floor(inputLen / 5);
|
|
130
|
+
const remainder = inputLen % 5;
|
|
131
|
+
const totalBytes = fullGroups * 4 + (remainder > 0 ? remainder - 1 : 0);
|
|
132
|
+
// Process 5 characters at a time
|
|
133
|
+
let byteCount = 0;
|
|
134
|
+
for (let i = 0; i < inputLen; i += 5) {
|
|
135
|
+
const chunkSize = Math.min(5, inputLen - i);
|
|
136
|
+
// Decode up to 5 characters into a 32-bit value
|
|
137
|
+
let value = 0;
|
|
138
|
+
for (let j = 0; j < chunkSize; j++) {
|
|
139
|
+
const char = input[i + j];
|
|
140
|
+
if (char === undefined) {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
const digit = decodeTable.get(char);
|
|
144
|
+
if (digit === undefined) {
|
|
145
|
+
throw new Error(`Invalid Base85 character: ${char}`);
|
|
146
|
+
}
|
|
147
|
+
value = value * 85 + digit;
|
|
148
|
+
}
|
|
149
|
+
// Pad with 84 (highest digit) for incomplete chunks
|
|
150
|
+
for (let j = chunkSize; j < 5; j++) {
|
|
151
|
+
value = value * 85 + 84;
|
|
152
|
+
}
|
|
153
|
+
// Calculate how many bytes this chunk should produce
|
|
154
|
+
const bytesToExtract = chunkSize === 5 ? 4 : chunkSize - 1;
|
|
155
|
+
// Extract bytes from high to low
|
|
156
|
+
const extracted = [];
|
|
157
|
+
for (let j = 0; j < 4; j++) {
|
|
158
|
+
extracted.unshift(value & 0xff);
|
|
159
|
+
value = value >>> 8;
|
|
160
|
+
}
|
|
161
|
+
// Only push the bytes we need
|
|
162
|
+
for (let j = 0; j < bytesToExtract && byteCount < totalBytes; j++) {
|
|
163
|
+
const byte = extracted[j];
|
|
164
|
+
if (byte !== undefined) {
|
|
165
|
+
bytes.push(byte);
|
|
166
|
+
byteCount++;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return new TextDecoder().decode(new Uint8Array(bytes));
|
|
171
|
+
}
|
|
172
|
+
export { encode85, decode85, Base85Charset };
|
|
173
|
+
//# sourceMappingURL=base85.js.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import { strictEqual, throws } from "node:assert";
|
|
3
|
+
import { encode85, decode85, Base85Charset } from "./base85.js";
|
|
4
|
+
describe("Base85 encode85", () => {
|
|
5
|
+
it("encodes empty string", () => {
|
|
6
|
+
strictEqual(encode85(""), "");
|
|
7
|
+
});
|
|
8
|
+
it("encodes 'Hello' with ascii85 charset", () => {
|
|
9
|
+
const encoded = encode85("Hello");
|
|
10
|
+
strictEqual(encoded.length > 0, true);
|
|
11
|
+
});
|
|
12
|
+
it("encodes single character", () => {
|
|
13
|
+
const encoded = encode85("A");
|
|
14
|
+
strictEqual(encoded.length, 2);
|
|
15
|
+
});
|
|
16
|
+
it("encodes 4 characters (full block)", () => {
|
|
17
|
+
const encoded = encode85("test");
|
|
18
|
+
strictEqual(encoded.length, 5);
|
|
19
|
+
});
|
|
20
|
+
it("encodes 5 characters (4 + 1)", () => {
|
|
21
|
+
const encoded = encode85("tests");
|
|
22
|
+
strictEqual(encoded.length, 7);
|
|
23
|
+
});
|
|
24
|
+
it("encodes with z85 charset", () => {
|
|
25
|
+
const ascii85 = encode85("test", "ascii85");
|
|
26
|
+
const z85 = encode85("test", "z85");
|
|
27
|
+
strictEqual(ascii85 !== z85, true);
|
|
28
|
+
});
|
|
29
|
+
it("encodes with rfc1924 charset", () => {
|
|
30
|
+
const ascii85 = encode85("test", "ascii85");
|
|
31
|
+
const rfc1924 = encode85("test", "rfc1924");
|
|
32
|
+
strictEqual(ascii85 !== rfc1924, true);
|
|
33
|
+
});
|
|
34
|
+
it("encodes UTF-8 characters", () => {
|
|
35
|
+
const input = "こんにちは";
|
|
36
|
+
const encoded = encode85(input);
|
|
37
|
+
const decoded = decode85(encoded);
|
|
38
|
+
strictEqual(decoded, input);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("Base85 decode85", () => {
|
|
42
|
+
it("decodes empty string", () => {
|
|
43
|
+
strictEqual(decode85(""), "");
|
|
44
|
+
});
|
|
45
|
+
it("decodes encoded 'Hello'", () => {
|
|
46
|
+
const encoded = encode85("Hello");
|
|
47
|
+
strictEqual(decode85(encoded), "Hello");
|
|
48
|
+
});
|
|
49
|
+
it("decodes single character encoded", () => {
|
|
50
|
+
const encoded = encode85("A");
|
|
51
|
+
strictEqual(decode85(encoded), "A");
|
|
52
|
+
});
|
|
53
|
+
it("decodes 4-char block correctly", () => {
|
|
54
|
+
const encoded = encode85("test");
|
|
55
|
+
strictEqual(decode85(encoded), "test");
|
|
56
|
+
});
|
|
57
|
+
it("throws on invalid character", () => {
|
|
58
|
+
// Use a character not in ascii85 alphabet
|
|
59
|
+
throws(() => decode85("\x7F"), /Invalid Base85 character/);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("Base85 round-trip", () => {
|
|
63
|
+
const testCases = [
|
|
64
|
+
"",
|
|
65
|
+
"a",
|
|
66
|
+
"ab",
|
|
67
|
+
"abc",
|
|
68
|
+
"abcd",
|
|
69
|
+
"abcde",
|
|
70
|
+
"Hello, World!",
|
|
71
|
+
"The quick brown fox jumps over the lazy dog",
|
|
72
|
+
"こんにちは世界",
|
|
73
|
+
"🎉🚀✨",
|
|
74
|
+
"1234567890"
|
|
75
|
+
];
|
|
76
|
+
for (const charset of Base85Charset) {
|
|
77
|
+
describe(`charset: ${charset}`, () => {
|
|
78
|
+
for (const input of testCases) {
|
|
79
|
+
it(`round-trips: ${JSON.stringify(input)}`, () => {
|
|
80
|
+
const encoded = encode85(input, charset);
|
|
81
|
+
const decoded = decode85(encoded, charset);
|
|
82
|
+
strictEqual(decoded, input);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
describe("Base85Charset", () => {
|
|
89
|
+
it("exports array with three charsets", () => {
|
|
90
|
+
strictEqual(Base85Charset.length, 3);
|
|
91
|
+
strictEqual(Base85Charset[0], "ascii85");
|
|
92
|
+
strictEqual(Base85Charset[1], "z85");
|
|
93
|
+
strictEqual(Base85Charset[2], "rfc1924");
|
|
94
|
+
});
|
|
95
|
+
it("charset type works correctly", () => {
|
|
96
|
+
const cs = "z85";
|
|
97
|
+
strictEqual(encode85("test", cs), encode85("test", "z85"));
|
|
98
|
+
});
|
|
99
|
+
it("each charset has exactly 85 characters", () => {
|
|
100
|
+
// This is a compile-time guarantee, but let's verify at runtime too
|
|
101
|
+
for (const charset of Base85Charset) {
|
|
102
|
+
const encoded = encode85("test", charset);
|
|
103
|
+
strictEqual(encoded.length > 0, true);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
//# sourceMappingURL=base85.test.js.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import { strictEqual, throws } from "node:assert";
|
|
3
|
+
import { encode85, decode85, Base85Charset } from "./base85.js";
|
|
4
|
+
import type { Base85CharsetType } from "./base85.js";
|
|
5
|
+
|
|
6
|
+
describe("Base85 encode85", () => {
|
|
7
|
+
it("encodes empty string", () => {
|
|
8
|
+
strictEqual(encode85(""), "");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("encodes 'Hello' with ascii85 charset", () => {
|
|
12
|
+
const encoded = encode85("Hello");
|
|
13
|
+
strictEqual(encoded.length > 0, true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("encodes single character", () => {
|
|
17
|
+
const encoded = encode85("A");
|
|
18
|
+
strictEqual(encoded.length, 2);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("encodes 4 characters (full block)", () => {
|
|
22
|
+
const encoded = encode85("test");
|
|
23
|
+
strictEqual(encoded.length, 5);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("encodes 5 characters (4 + 1)", () => {
|
|
27
|
+
const encoded = encode85("tests");
|
|
28
|
+
strictEqual(encoded.length, 7);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("encodes with z85 charset", () => {
|
|
32
|
+
const ascii85 = encode85("test", "ascii85");
|
|
33
|
+
const z85 = encode85("test", "z85");
|
|
34
|
+
strictEqual(ascii85 !== z85, true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("encodes with rfc1924 charset", () => {
|
|
38
|
+
const ascii85 = encode85("test", "ascii85");
|
|
39
|
+
const rfc1924 = encode85("test", "rfc1924");
|
|
40
|
+
strictEqual(ascii85 !== rfc1924, true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("encodes UTF-8 characters", () => {
|
|
44
|
+
const input = "こんにちは";
|
|
45
|
+
const encoded = encode85(input);
|
|
46
|
+
const decoded = decode85(encoded);
|
|
47
|
+
strictEqual(decoded, input);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("Base85 decode85", () => {
|
|
52
|
+
it("decodes empty string", () => {
|
|
53
|
+
strictEqual(decode85(""), "");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("decodes encoded 'Hello'", () => {
|
|
57
|
+
const encoded = encode85("Hello");
|
|
58
|
+
strictEqual(decode85(encoded), "Hello");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("decodes single character encoded", () => {
|
|
62
|
+
const encoded = encode85("A");
|
|
63
|
+
strictEqual(decode85(encoded), "A");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("decodes 4-char block correctly", () => {
|
|
67
|
+
const encoded = encode85("test");
|
|
68
|
+
strictEqual(decode85(encoded), "test");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("throws on invalid character", () => {
|
|
72
|
+
// Use a character not in ascii85 alphabet
|
|
73
|
+
throws(() => decode85("\x7F"), /Invalid Base85 character/);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("Base85 round-trip", () => {
|
|
78
|
+
const testCases = [
|
|
79
|
+
"",
|
|
80
|
+
"a",
|
|
81
|
+
"ab",
|
|
82
|
+
"abc",
|
|
83
|
+
"abcd",
|
|
84
|
+
"abcde",
|
|
85
|
+
"Hello, World!",
|
|
86
|
+
"The quick brown fox jumps over the lazy dog",
|
|
87
|
+
"こんにちは世界",
|
|
88
|
+
"🎉🚀✨",
|
|
89
|
+
"1234567890"
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
for (const charset of Base85Charset) {
|
|
93
|
+
describe(`charset: ${charset}`, () => {
|
|
94
|
+
for (const input of testCases) {
|
|
95
|
+
it(`round-trips: ${JSON.stringify(input)}`, () => {
|
|
96
|
+
const encoded = encode85(input, charset);
|
|
97
|
+
const decoded = decode85(encoded, charset);
|
|
98
|
+
strictEqual(decoded, input);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("Base85Charset", () => {
|
|
106
|
+
it("exports array with three charsets", () => {
|
|
107
|
+
strictEqual(Base85Charset.length, 3);
|
|
108
|
+
strictEqual(Base85Charset[0], "ascii85");
|
|
109
|
+
strictEqual(Base85Charset[1], "z85");
|
|
110
|
+
strictEqual(Base85Charset[2], "rfc1924");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("charset type works correctly", () => {
|
|
114
|
+
const cs: Base85CharsetType = "z85";
|
|
115
|
+
strictEqual(encode85("test", cs), encode85("test", "z85"));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("each charset has exactly 85 characters", () => {
|
|
119
|
+
// This is a compile-time guarantee, but let's verify at runtime too
|
|
120
|
+
for (const charset of Base85Charset) {
|
|
121
|
+
const encoded = encode85("test", charset);
|
|
122
|
+
strictEqual(encoded.length > 0, true);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
package/src/base85.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base85 encoding/decoding with multiple charset variants.
|
|
3
|
+
* Base85 provides ~25% better efficiency than Base64 (4:5 vs 3:4 ratio).
|
|
4
|
+
* @module base85
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Available Base85 charset variants.
|
|
9
|
+
* - `ascii85`: Adobe Ascii85 (btoa format)
|
|
10
|
+
* - `z85`: ZeroMQ Base85 (no quotes or backslash)
|
|
11
|
+
* - `rfc1924`: RFC 1924 IPv6 encoding
|
|
12
|
+
*/
|
|
13
|
+
const Base85Charset = ["ascii85", "z85", "rfc1924"] as const;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Base85 charset type.
|
|
17
|
+
*/
|
|
18
|
+
type Base85CharsetType = (typeof Base85Charset)[number];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Charset alphabets for Base85 variants.
|
|
22
|
+
* Each has exactly 85 printable ASCII characters.
|
|
23
|
+
*/
|
|
24
|
+
const CHARSETS: Record<Base85CharsetType, string> = {
|
|
25
|
+
ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu",
|
|
26
|
+
z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#",
|
|
27
|
+
rfc1924: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build a reverse lookup table for decoding.
|
|
32
|
+
* @param alphabet - The 85-character alphabet string.
|
|
33
|
+
* @returns Map of character to index.
|
|
34
|
+
*/
|
|
35
|
+
function buildDecodeTable(alphabet: string): Map<string, number> {
|
|
36
|
+
const table = new Map<string, number>();
|
|
37
|
+
for (let i = 0; i < alphabet.length; i++) {
|
|
38
|
+
const char = alphabet[i];
|
|
39
|
+
if (char !== undefined) {
|
|
40
|
+
table.set(char, i);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return table;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Pre-built decode tables for each charset.
|
|
48
|
+
*/
|
|
49
|
+
const DECODE_TABLES: Record<Base85CharsetType, Map<string, number>> = {
|
|
50
|
+
ascii85: buildDecodeTable(CHARSETS.ascii85),
|
|
51
|
+
z85: buildDecodeTable(CHARSETS.z85),
|
|
52
|
+
rfc1924: buildDecodeTable(CHARSETS.rfc1924)
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Encode a string to Base85.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { encode85 } from "./base85.js";
|
|
61
|
+
*
|
|
62
|
+
* encode85("Hello");
|
|
63
|
+
* // => "87cURDZ"
|
|
64
|
+
*
|
|
65
|
+
* encode85("test", "z85");
|
|
66
|
+
* // => "wrx.P"
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param input - The string to encode.
|
|
70
|
+
* @param charset - The charset variant to use (default: "ascii85").
|
|
71
|
+
* @returns The Base85 encoded string.
|
|
72
|
+
*/
|
|
73
|
+
function encode85(input: string, charset: Base85CharsetType = "ascii85"): string {
|
|
74
|
+
if (input === "") {
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const alphabet = CHARSETS[charset];
|
|
79
|
+
const bytes = new TextEncoder().encode(input);
|
|
80
|
+
let result = "";
|
|
81
|
+
|
|
82
|
+
// Process 4 bytes at a time
|
|
83
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
84
|
+
const chunkSize = Math.min(4, bytes.length - i);
|
|
85
|
+
|
|
86
|
+
// Pack up to 4 bytes into a value using multiplication to avoid
|
|
87
|
+
// 32-bit signed integer overflow from bitwise operations
|
|
88
|
+
let value = 0;
|
|
89
|
+
for (let j = 0; j < chunkSize; j++) {
|
|
90
|
+
value = value * 256 + (bytes[i + j] ?? 0);
|
|
91
|
+
}
|
|
92
|
+
// Pad with zeros for incomplete chunks
|
|
93
|
+
for (let j = chunkSize; j < 4; j++) {
|
|
94
|
+
value = value * 256;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Convert to 5 base-85 digits
|
|
98
|
+
const encoded: string[] = [];
|
|
99
|
+
for (let j = 0; j < 5; j++) {
|
|
100
|
+
encoded.unshift(alphabet[value % 85] ?? "");
|
|
101
|
+
value = Math.floor(value / 85);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// For the last chunk, only output as many characters as needed
|
|
105
|
+
// (chunkSize bytes -> chunkSize + 1 characters)
|
|
106
|
+
if (i + 4 > bytes.length) {
|
|
107
|
+
result += encoded.slice(0, chunkSize + 1).join("");
|
|
108
|
+
} else {
|
|
109
|
+
result += encoded.join("");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Decode a Base85 string.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* import { decode85 } from "./base85.js";
|
|
122
|
+
*
|
|
123
|
+
* decode85("87cURDZ");
|
|
124
|
+
* // => "Hello"
|
|
125
|
+
*
|
|
126
|
+
* decode85("wrx.P", "z85");
|
|
127
|
+
* // => "test"
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @param input - The Base85 encoded string.
|
|
131
|
+
* @param charset - The charset variant to use (default: "ascii85").
|
|
132
|
+
* @returns The decoded string.
|
|
133
|
+
* @throws Error if the input contains invalid characters.
|
|
134
|
+
*/
|
|
135
|
+
function decode85(input: string, charset: Base85CharsetType = "ascii85"): string {
|
|
136
|
+
if (input === "") {
|
|
137
|
+
return "";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const decodeTable = DECODE_TABLES[charset];
|
|
141
|
+
const bytes: number[] = [];
|
|
142
|
+
const inputLen = input.length;
|
|
143
|
+
|
|
144
|
+
// Calculate expected output length
|
|
145
|
+
// Full groups of 5 chars -> 4 bytes each
|
|
146
|
+
// Partial group: n chars -> n-1 bytes
|
|
147
|
+
const fullGroups = Math.floor(inputLen / 5);
|
|
148
|
+
const remainder = inputLen % 5;
|
|
149
|
+
const totalBytes = fullGroups * 4 + (remainder > 0 ? remainder - 1 : 0);
|
|
150
|
+
|
|
151
|
+
// Process 5 characters at a time
|
|
152
|
+
let byteCount = 0;
|
|
153
|
+
for (let i = 0; i < inputLen; i += 5) {
|
|
154
|
+
const chunkSize = Math.min(5, inputLen - i);
|
|
155
|
+
|
|
156
|
+
// Decode up to 5 characters into a 32-bit value
|
|
157
|
+
let value = 0;
|
|
158
|
+
for (let j = 0; j < chunkSize; j++) {
|
|
159
|
+
const char = input[i + j];
|
|
160
|
+
if (char === undefined) {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
const digit = decodeTable.get(char);
|
|
164
|
+
if (digit === undefined) {
|
|
165
|
+
throw new Error(`Invalid Base85 character: ${char}`);
|
|
166
|
+
}
|
|
167
|
+
value = value * 85 + digit;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Pad with 84 (highest digit) for incomplete chunks
|
|
171
|
+
for (let j = chunkSize; j < 5; j++) {
|
|
172
|
+
value = value * 85 + 84;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Calculate how many bytes this chunk should produce
|
|
176
|
+
const bytesToExtract = chunkSize === 5 ? 4 : chunkSize - 1;
|
|
177
|
+
|
|
178
|
+
// Extract bytes from high to low
|
|
179
|
+
const extracted: number[] = [];
|
|
180
|
+
for (let j = 0; j < 4; j++) {
|
|
181
|
+
extracted.unshift(value & 0xff);
|
|
182
|
+
value = value >>> 8;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Only push the bytes we need
|
|
186
|
+
for (let j = 0; j < bytesToExtract && byteCount < totalBytes; j++) {
|
|
187
|
+
const byte = extracted[j];
|
|
188
|
+
if (byte !== undefined) {
|
|
189
|
+
bytes.push(byte);
|
|
190
|
+
byteCount++;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return new TextDecoder().decode(new Uint8Array(bytes));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export { encode85, decode85, Base85Charset };
|
|
199
|
+
export type { Base85CharsetType };
|
package/src/index.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ import type { UserAgentInfo } from "./user-agent.js";
|
|
|
6
6
|
import { parseUserAgent } from "./user-agent.js";
|
|
7
7
|
import type { Base64CharsetType } from "./base64.js";
|
|
8
8
|
import { encode, decode, Base64Charset } from "./base64.js";
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
import type { Base58CharsetType } from "./base58.js";
|
|
10
|
+
import { encode58, decode58, Base58Charset } from "./base58.js";
|
|
11
|
+
import type { Base85CharsetType } from "./base85.js";
|
|
12
|
+
import { encode85, decode85, Base85Charset } from "./base85.js";
|
|
13
|
+
import type { Base32CharsetType } from "./base32.js";
|
|
14
|
+
import { encode32, decode32, Base32Charset } from "./base32.js";
|
|
15
|
+
export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset };
|
|
16
|
+
export type { FindDirectoriesOptions, FileSystemDependencies, UserAgentInfo, Base64CharsetType, Base58CharsetType, Base85CharsetType, Base32CharsetType };
|
|
11
17
|
//# sourceMappingURL=index.d.ts.map
|
package/src/index.js
CHANGED
|
@@ -3,5 +3,8 @@ import { pojo } from "./pojo.js";
|
|
|
3
3
|
import { createFindDirectories } from "./find.js";
|
|
4
4
|
import { parseUserAgent } from "./user-agent.js";
|
|
5
5
|
import { encode, decode, Base64Charset } from "./base64.js";
|
|
6
|
-
|
|
6
|
+
import { encode58, decode58, Base58Charset } from "./base58.js";
|
|
7
|
+
import { encode85, decode85, Base85Charset } from "./base85.js";
|
|
8
|
+
import { encode32, decode32, Base32Charset } from "./base32.js";
|
|
9
|
+
export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset };
|
|
7
10
|
//# sourceMappingURL=index.js.map
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,12 @@ import type { UserAgentInfo } from "./user-agent.js";
|
|
|
6
6
|
import { parseUserAgent } from "./user-agent.js";
|
|
7
7
|
import type { Base64CharsetType } from "./base64.js";
|
|
8
8
|
import { encode, decode, Base64Charset } from "./base64.js";
|
|
9
|
+
import type { Base58CharsetType } from "./base58.js";
|
|
10
|
+
import { encode58, decode58, Base58Charset } from "./base58.js";
|
|
11
|
+
import type { Base85CharsetType } from "./base85.js";
|
|
12
|
+
import { encode85, decode85, Base85Charset } from "./base85.js";
|
|
13
|
+
import type { Base32CharsetType } from "./base32.js";
|
|
14
|
+
import { encode32, decode32, Base32Charset } from "./base32.js";
|
|
9
15
|
|
|
10
16
|
export {
|
|
11
17
|
hello,
|
|
@@ -14,12 +20,24 @@ export {
|
|
|
14
20
|
parseUserAgent,
|
|
15
21
|
encode,
|
|
16
22
|
decode,
|
|
17
|
-
Base64Charset
|
|
23
|
+
Base64Charset,
|
|
24
|
+
encode58,
|
|
25
|
+
decode58,
|
|
26
|
+
Base58Charset,
|
|
27
|
+
encode85,
|
|
28
|
+
decode85,
|
|
29
|
+
Base85Charset,
|
|
30
|
+
encode32,
|
|
31
|
+
decode32,
|
|
32
|
+
Base32Charset
|
|
18
33
|
};
|
|
19
34
|
|
|
20
35
|
export type {
|
|
21
36
|
FindDirectoriesOptions,
|
|
22
37
|
FileSystemDependencies,
|
|
23
38
|
UserAgentInfo,
|
|
24
|
-
Base64CharsetType
|
|
39
|
+
Base64CharsetType,
|
|
40
|
+
Base58CharsetType,
|
|
41
|
+
Base85CharsetType,
|
|
42
|
+
Base32CharsetType
|
|
25
43
|
};
|