bufferbase 1.0.3 → 1.0.5

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.
@@ -0,0 +1,55 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * A collection of common bases.
4
+ */
5
+ export declare const Chars: {
6
+ Decimal: string;
7
+ Base16: string;
8
+ Base32: string;
9
+ Base32Crockford: string;
10
+ Base36: string;
11
+ Base58: string;
12
+ Base64_STD: string;
13
+ Base64_URL_SAFE: string;
14
+ Base64_XML_NMTOKEN: string;
15
+ Base64_XML_NAME: string;
16
+ Ascii85: string;
17
+ Base85: string;
18
+ Z85: string;
19
+ };
20
+ export declare class InvalidCharacterError extends Error {
21
+ message: string;
22
+ }
23
+ /**
24
+ * Encodes and decodes buffers to and from a base.
25
+ */
26
+ export declare class BufferEncoder {
27
+ private baseChars;
28
+ constructor(baseChars: string);
29
+ /**
30
+ * Encodes a buffer to a string.
31
+ */
32
+ encode(buffer: Buffer): string;
33
+ /**
34
+ * Decodes a string to a buffer.
35
+ */
36
+ decode(encoded: string): Buffer;
37
+ }
38
+ /**
39
+ * Creates a converter function that can convert between two bases.
40
+ */
41
+ export declare class Converter {
42
+ decoder: BufferEncoder;
43
+ encoder: BufferEncoder;
44
+ constructor(inputBase: string, outputBase: string);
45
+ /**
46
+ * Converts a string from the input base to the output base.
47
+ */
48
+ convert(input: string): string;
49
+ }
50
+ export declare class Validator {
51
+ decorder: BufferEncoder;
52
+ constructor(inputBase: string);
53
+ validate(input: string): boolean;
54
+ }
55
+ export declare const validate: (input: string, base: string) => boolean;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * A collection of common bases.
3
+ */
4
+ export const Chars = {
5
+ Decimal: "0123456789",
6
+ Base16: "0123456789ABCDEF",
7
+ Base32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
8
+ Base32Crockford: "0123456789ABCDEFGHJKMNPQRSTVWXYZ",
9
+ Base36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
10
+ Base58: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
11
+ Base64_STD: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
12
+ Base64_URL_SAFE: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
13
+ Base64_XML_NMTOKEN: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._",
14
+ Base64_XML_NAME: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_:",
15
+ Ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstu",
16
+ Base85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~",
17
+ Z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#",
18
+ };
19
+ export class InvalidCharacterError extends Error {
20
+ message = "Invalid character found";
21
+ }
22
+ /**
23
+ * Encodes and decodes buffers to and from a base.
24
+ */
25
+ export class BufferEncoder {
26
+ baseChars;
27
+ constructor(baseChars) {
28
+ this.baseChars = baseChars;
29
+ }
30
+ /**
31
+ * Encodes a buffer to a string.
32
+ */
33
+ encode(buffer) {
34
+ let result = [];
35
+ for (const byte of buffer) {
36
+ let carry = byte;
37
+ for (let j = 0; j < result.length; j++) {
38
+ carry += result[j] * 256;
39
+ result[j] = carry % this.baseChars.length;
40
+ carry = Math.floor(carry / this.baseChars.length);
41
+ }
42
+ while (carry > 0) {
43
+ result.push(carry % this.baseChars.length);
44
+ carry = Math.floor(carry / this.baseChars.length);
45
+ }
46
+ }
47
+ for (const byte of buffer) {
48
+ if (byte === 0) {
49
+ result.push(0);
50
+ }
51
+ else {
52
+ break;
53
+ }
54
+ }
55
+ return result
56
+ .reverse()
57
+ .map((index) => this.baseChars[index])
58
+ .join("");
59
+ }
60
+ /**
61
+ * Decodes a string to a buffer.
62
+ */
63
+ decode(encoded) {
64
+ let result = Buffer.alloc(0);
65
+ for (const char of encoded) {
66
+ const value = this.baseChars.indexOf(char);
67
+ if (value === -1)
68
+ throw new InvalidCharacterError();
69
+ let carry = value;
70
+ let tempResult = Buffer.alloc(result.length);
71
+ let i;
72
+ for (i = 0; i < result.length; i++) {
73
+ carry += result[i] * this.baseChars.length;
74
+ tempResult[i] = carry % 256;
75
+ carry = Math.floor(carry / 256);
76
+ }
77
+ while (carry > 0) {
78
+ tempResult = Buffer.concat([tempResult, Buffer.from([carry % 256])]);
79
+ carry = Math.floor(carry / 256);
80
+ }
81
+ result = tempResult;
82
+ }
83
+ return result.reverse();
84
+ }
85
+ }
86
+ /**
87
+ * Creates a converter function that can convert between two bases.
88
+ */
89
+ export class Converter {
90
+ decoder;
91
+ encoder;
92
+ constructor(inputBase, outputBase) {
93
+ this.decoder = new BufferEncoder(inputBase);
94
+ this.encoder = new BufferEncoder(outputBase);
95
+ }
96
+ /**
97
+ * Converts a string from the input base to the output base.
98
+ */
99
+ convert(input) {
100
+ return this.encoder.encode(this.decoder.decode(input));
101
+ }
102
+ }
103
+ export class Validator {
104
+ decorder;
105
+ constructor(inputBase) {
106
+ this.decorder = new BufferEncoder(inputBase);
107
+ }
108
+ validate(input) {
109
+ try {
110
+ this.decorder.decode(input);
111
+ return true;
112
+ }
113
+ catch (e) {
114
+ if (e instanceof InvalidCharacterError) {
115
+ return false;
116
+ }
117
+ throw e;
118
+ }
119
+ }
120
+ }
121
+ export const validate = (input, base) => {
122
+ return new Validator(base).validate(input);
123
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { Chars, BufferEncoder } from "./bufferbase.js";
2
+ import { Buffer } from "node:buffer";
3
+ import test from "node:test"; // ①
4
+ import assert from "node:assert";
5
+ test("anybase", async (t) => {
6
+ await t.test("encodes and decodes a string correctly with base58chars", () => {
7
+ const encoder = new BufferEncoder(Chars.Base58);
8
+ const buffer = Buffer.from("Hello, World!", "utf8");
9
+ const encoded = encoder.encode(buffer);
10
+ const decoded = encoder.decode(encoded);
11
+ assert.strictEqual(decoded.toString("utf8"), "Hello, World!");
12
+ });
13
+ await t.test("handles empty buffer correctly", () => {
14
+ const encoder = new BufferEncoder(Chars.Base58);
15
+ const buffer = Buffer.alloc(0);
16
+ const encoded = encoder.encode(buffer);
17
+ const decoded = encoder.decode(encoded);
18
+ assert.strictEqual(decoded.toString("utf8"), "");
19
+ });
20
+ await t.test("handles long buffer correctly", () => {
21
+ const encoder = new BufferEncoder(Chars.Base58);
22
+ const buffer = Buffer.from("Hello, World!".repeat(100), "utf8");
23
+ const encoded = encoder.encode(buffer);
24
+ const decoded = encoder.decode(encoded);
25
+ assert.strictEqual(decoded.toString("utf8"), "Hello, World!".repeat(100));
26
+ });
27
+ await t.test("encode and decode various chars correctly", () => {
28
+ for (const charTable of Object.values(Chars)) {
29
+ const encoder = new BufferEncoder(charTable);
30
+ const buffer = Buffer.from("Hello, World!", "utf8");
31
+ const encoded = encoder.encode(buffer);
32
+ const decoded = encoder.decode(encoded);
33
+ assert.strictEqual(decoded.toString("utf8"), "Hello, World!");
34
+ }
35
+ });
36
+ await t.test("throws error on invalid characters in decode", () => {
37
+ assert.throws(() => {
38
+ const encoder = new BufferEncoder(Chars.Base58);
39
+ encoder.decode("InvalidString!");
40
+ }, new Error("Invalid character found"));
41
+ });
42
+ });
@@ -0,0 +1 @@
1
+ export { BufferEncoder, Converter, InvalidCharacterError, Validator, validate, } from "./bufferbase";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BufferEncoder, Converter, InvalidCharacterError, Validator, validate, } from "./bufferbase";
@@ -0,0 +1 @@
1
+ export {};
package/dist/sample.js ADDED
@@ -0,0 +1,36 @@
1
+ import { Chars, BufferEncoder, Converter, Validator } from "./bufferbase";
2
+ // Example buffer
3
+ const bytes = Buffer.from("Hello, World!", "utf8");
4
+ // Encoding buffer to Base32
5
+ const encoder = new BufferEncoder(Chars.Base32Crockford);
6
+ const base32encoded = encoder.encode(bytes);
7
+ // Converting Base32 to Base58
8
+ const converter_32to58 = new Converter(Chars.Base32Crockford, Chars.Base58);
9
+ const base58encoded = converter_32to58.convert(base32encoded);
10
+ // Converting Base32 to Base58
11
+ const converter_58to64 = new Converter(Chars.Base58, Chars.Base64_URL_SAFE);
12
+ const base64encoded = converter_58to64.convert(base58encoded);
13
+ // Validating Base64
14
+ const validatorB64 = new Validator(Chars.Base64_URL_SAFE);
15
+ const isValidAsBase64 = validatorB64.validate(base64encoded);
16
+ // Decoding Base64
17
+ const decoder = new BufferEncoder(Chars.Base64_URL_SAFE);
18
+ const decoded = decoder.decode(base64encoded);
19
+ console.table({
20
+ bytes: bytes.toString("utf8"),
21
+ base32encoded,
22
+ base58encoded,
23
+ base64encoded,
24
+ isValidAsBase64,
25
+ decoded: decoded.toString("utf8"),
26
+ });
27
+ // ┌─────────────────┬─────────────────────────┐
28
+ // │ (index) │ Values │
29
+ // ├─────────────────┼─────────────────────────┤
30
+ // │ bytes │ 'Hello, World!' │
31
+ // │ base32encoded │ '4GSBCDHQJR82QDXS6RS11' │
32
+ // │ base58encoded │ '72k1xXWG59fYdzSNoA' │
33
+ // │ base64encoded │ 'BIZWxsbywgV29ybGQh' │
34
+ // │ isValidAsBase64 │ true │
35
+ // │ decoded │ 'Hello, World!' │
36
+ // └─────────────────┴─────────────────────────┘
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bufferbase",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Buffer-to-BaseN Encoder, Decoder, Converter, and Validator",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -12,13 +12,17 @@
12
12
  },
13
13
  "devDependencies": {
14
14
  "@types/node": "^20.11.5",
15
+ "ts-loader": "^9.5.1",
15
16
  "tsx": "^4.7.0",
16
- "typescript": "^5.3.3"
17
+ "typescript": "^5.3.3",
18
+ "webpack": "^5.89.0",
19
+ "webpack-cli": "^5.1.4"
17
20
  },
18
21
  "repository": {
19
22
  "type": "git",
20
23
  "url": "git+https://github.com/misebox/bufferbase.git"
21
24
  },
25
+ "files": ["dist", "src"],
22
26
  "keywords": [
23
27
  "number",
24
28
  "encode",
package/tsconfig.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "esnext",
4
- "module": "esnext",
5
- "moduleResolution": "bundler",
6
- "lib": ["esnext"],
7
- "esModuleInterop": true,
8
- "strict": true,
9
- "skipLibCheck": true,
10
- "allowJs": false,
11
- "types": [
12
- "node",
13
- ],
14
- "declaration": true,
15
- "pretty": true,
16
- "newLine": "lf",
17
- "outDir": "./dist",
18
- },
19
- }