@hugoalh/adler32 0.4.0 → 0.5.0

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 CHANGED
@@ -58,23 +58,19 @@ An ECMAScript (JavaScript & TypeScript) module to get the checksum of the data w
58
58
  constructor(data?: Adler32AcceptDataType);
59
59
  get freezed(): boolean;
60
60
  freeze(): this;
61
- hash(): bigint;
62
- hashBase16(): string;
63
- hashBase32Hex(): string;
64
- hashBase36(): string;
65
- hashBase64(): string;
66
- hashBase64URL(): string;
67
- hashBigInt(): bigint;
68
- hashBuffer(): Buffer;
61
+ hash(): Uint8Array;
69
62
  hashHex(): string;
70
- hashHexPadding(): string;
71
- hashNumber(): number;
72
63
  update(data: Adler32AcceptDataType): this;
73
- static fromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<Adler32>;
64
+ updateFromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<this>;
74
65
  }
75
66
  ```
76
67
  - ```ts
77
- type Adler32AcceptDataType = string | BigUint64Array | Uint8Array | Uint16Array | Uint32Array;
68
+ type Adler32AcceptDataType =
69
+ | string
70
+ | BigUint64Array
71
+ | Uint8Array
72
+ | Uint16Array
73
+ | Uint32Array;
78
74
  ```
79
75
 
80
76
  > [!NOTE]
@@ -85,6 +81,6 @@ An ECMAScript (JavaScript & TypeScript) module to get the checksum of the data w
85
81
  ## ✍️ Examples
86
82
 
87
83
  - ```ts
88
- new Adler32("Wikipedia").hashHexPadding();
84
+ new Adler32("Wikipedia").hashHex();
89
85
  //=> "11E60398"
90
86
  ```
package/mod.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Buffer } from "node:buffer";
2
1
  export type Adler32AcceptDataType = string | BigUint64Array | Uint8Array | Uint16Array | Uint32Array;
3
2
  /**
4
3
  * Get the checksum of the data with algorithm Adler32.
@@ -8,7 +7,7 @@ export declare class Adler32 {
8
7
  get [Symbol.toStringTag](): string;
9
8
  /**
10
9
  * Initialize.
11
- * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update}.
10
+ * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update} and {@linkcode Adler32.updateFromStream}.
12
11
  */
13
12
  constructor(data?: Adler32AcceptDataType);
14
13
  /**
@@ -22,65 +21,15 @@ export declare class Adler32 {
22
21
  */
23
22
  freeze(): this;
24
23
  /**
25
- * Get the checksum of the data, in original format.
26
- * @returns {bigint}
24
+ * Get the checksum of the data, in Uint8Array.
25
+ * @returns {Uint8Array}
27
26
  */
28
- hash(): bigint;
27
+ hash(): Uint8Array;
29
28
  /**
30
- * Get the checksum of the data, in Base16.
31
- * @returns {string}
32
- */
33
- hashBase16(): string;
34
- /**
35
- * Get the checksum of the data, in Base32Hex ({@link https://datatracker.ietf.org/doc/html/rfc4648#section-7 RFC 4648 §7}).
36
- * @returns {string}
37
- */
38
- hashBase32Hex(): string;
39
- /**
40
- * Get the checksum of the data, in Base36.
41
- * @returns {string}
42
- */
43
- hashBase36(): string;
44
- /**
45
- * Get the checksum of the data, in Base64.
46
- * @returns {string}
47
- */
48
- hashBase64(): string;
49
- /**
50
- * Get the checksum of the data, in Base64URL.
51
- * @returns {string}
52
- */
53
- hashBase64URL(): string;
54
- /**
55
- * Get the checksum of the data, in big integer.
56
- * @returns {bigint}
57
- */
58
- hashBigInt(): bigint;
59
- /**
60
- * Get the checksum of the data, in big integer.
61
- * @returns {bigint}
62
- */
63
- hashBigInteger: () => bigint;
64
- /**
65
- * Get the checksum of the data, in Buffer.
66
- * @returns {Buffer}
67
- */
68
- hashBuffer(): Buffer;
69
- /**
70
- * Get the checksum of the data, in hex/hexadecimal without padding.
29
+ * Get the checksum of the data, in hexadecimal with padding.
71
30
  * @returns {string}
72
31
  */
73
32
  hashHex(): string;
74
- /**
75
- * Get the checksum of the data, in hex/hexadecimal with padding.
76
- * @returns {string}
77
- */
78
- hashHexPadding(): string;
79
- /**
80
- * Get the checksum of the data, in number.
81
- * @returns {number}
82
- */
83
- hashNumber(): number;
84
33
  /**
85
34
  * Append data.
86
35
  * @param {Adler32AcceptDataType} data Data.
@@ -88,11 +37,11 @@ export declare class Adler32 {
88
37
  */
89
38
  update(data: Adler32AcceptDataType): this;
90
39
  /**
91
- * Initialize from the readable stream.
92
- * @param {ReadableStream<Adler32AcceptDataType>} stream Readable stream.
93
- * @returns {Promise<Adler32>}
40
+ * Append data from the readable stream.
41
+ * @param {ReadableStream<Adler32AcceptDataType>} stream Data from the readable stream.
42
+ * @returns {Promise<this>}
94
43
  */
95
- static fromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<Adler32>;
44
+ updateFromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<this>;
96
45
  }
97
46
  export default Adler32;
98
47
  //# sourceMappingURL=mod.d.ts.map
package/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,cAAc,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAC;AACrG;;GAEG;AACH,qBAAa,OAAO;;IACnB,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAEjC;IAUD;;;OAGG;gBACS,IAAI,CAAC,EAAE,qBAAqB;IAgBxC;;;OAGG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IACD;;;OAGG;IACH,MAAM,IAAI,IAAI;IAId;;;OAGG;IACH,IAAI,IAAI,MAAM;IAId;;;OAGG;IACH,UAAU,IAAI,MAAM;IAIpB;;;OAGG;IACH,aAAa,IAAI,MAAM;IAIvB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAIpB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAIpB;;;OAGG;IACH,aAAa,IAAI,MAAM;IAIvB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;OAGG;IACH,cAAc,EAAE,MAAM,MAAM,CAAmB;IAC/C;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;OAGG;IACH,OAAO,IAAI,MAAM;IAGjB;;;OAGG;IACH,cAAc,IAAI,MAAM;IAGxB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,qBAAqB,GAAG,IAAI;IAQzC;;;;OAIG;WACU,UAAU,CAAC,MAAM,EAAE,cAAc,CAAC,qBAAqB,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CAOxF;AACD,eAAe,OAAO,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["src/mod.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,qBAAqB,GAC9B,MAAM,GACN,cAAc,GACd,UAAU,GACV,WAAW,GACX,WAAW,CAAC;AACf;;GAEG;AACH,qBAAa,OAAO;;IACnB,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAEjC;IAMD;;;OAGG;gBACS,IAAI,CAAC,EAAE,qBAAqB;IAKxC;;;OAGG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IACD;;;OAGG;IACH,MAAM,IAAI,IAAI;IAId;;;OAGG;IACH,IAAI,IAAI,UAAU;IAalB;;;OAGG;IACH,OAAO,IAAI,MAAM;IASjB;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,qBAAqB,GAAG,IAAI;IAazC;;;;OAIG;IACG,gBAAgB,CAAC,MAAM,EAAE,cAAc,CAAC,qBAAqB,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAMpF;AACD,eAAe,OAAO,CAAC"}
package/mod.js CHANGED
@@ -1,4 +1,3 @@
1
- import { Buffer } from "node:buffer";
2
1
  /**
3
2
  * Get the checksum of the data with algorithm Adler32.
4
3
  */
@@ -7,34 +6,19 @@ export class Adler32 {
7
6
  return "Adler32";
8
7
  }
9
8
  #freezed = false;
10
- #hash = null;
11
- #hashBase16 = null;
12
- #hashBase32Hex = null;
13
- #hashBase36 = null;
14
- #hashBase64 = null;
15
- #hashBase64URL = null;
9
+ #hashHex = null;
10
+ #hashUint8Array = null;
16
11
  #a = 1n;
17
12
  #b = 0n;
18
13
  /**
19
14
  * Initialize.
20
- * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update}.
15
+ * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update} and {@linkcode Adler32.updateFromStream}.
21
16
  */
22
17
  constructor(data) {
23
18
  if (typeof data !== "undefined") {
24
19
  this.update(data);
25
20
  }
26
21
  }
27
- #clearStorage() {
28
- if (this.#freezed) {
29
- throw new Error(`Instance is freezed!`);
30
- }
31
- this.#hash = null;
32
- this.#hashBase16 = null;
33
- this.#hashBase32Hex = null;
34
- this.#hashBase36 = null;
35
- this.#hashBase64 = null;
36
- this.#hashBase64URL = null;
37
- }
38
22
  /**
39
23
  * Whether the instance is freezed.
40
24
  * @returns {boolean}
@@ -51,92 +35,34 @@ export class Adler32 {
51
35
  return this;
52
36
  }
53
37
  /**
54
- * Get the checksum of the data, in original format.
55
- * @returns {bigint}
38
+ * Get the checksum of the data, in Uint8Array.
39
+ * @returns {Uint8Array}
56
40
  */
57
41
  hash() {
58
- this.#hash ??= this.#b * 65536n + this.#a;
59
- return this.#hash;
60
- }
61
- /**
62
- * Get the checksum of the data, in Base16.
63
- * @returns {string}
64
- */
65
- hashBase16() {
66
- this.#hashBase16 ??= this.hashBigInt().toString(16).toUpperCase();
67
- return this.#hashBase16;
68
- }
69
- /**
70
- * Get the checksum of the data, in Base32Hex ({@link https://datatracker.ietf.org/doc/html/rfc4648#section-7 RFC 4648 §7}).
71
- * @returns {string}
72
- */
73
- hashBase32Hex() {
74
- this.#hashBase32Hex ??= this.hashBigInt().toString(32).toUpperCase();
75
- return this.#hashBase32Hex;
76
- }
77
- /**
78
- * Get the checksum of the data, in Base36.
79
- * @returns {string}
80
- */
81
- hashBase36() {
82
- this.#hashBase36 ??= this.hashBigInt().toString(36).toUpperCase();
83
- return this.#hashBase36;
84
- }
85
- /**
86
- * Get the checksum of the data, in Base64.
87
- * @returns {string}
88
- */
89
- hashBase64() {
90
- this.#hashBase64 ??= this.hashBuffer().toString("base64");
91
- return this.#hashBase64;
92
- }
93
- /**
94
- * Get the checksum of the data, in Base64URL.
95
- * @returns {string}
96
- */
97
- hashBase64URL() {
98
- this.#hashBase64URL ??= this.hashBuffer().toString("base64url");
99
- return this.#hashBase64URL;
100
- }
101
- /**
102
- * Get the checksum of the data, in big integer.
103
- * @returns {bigint}
104
- */
105
- hashBigInt() {
106
- return this.hash();
107
- }
108
- /**
109
- * Get the checksum of the data, in big integer.
110
- * @returns {bigint}
111
- */
112
- hashBigInteger = this.hashBigInt;
113
- /**
114
- * Get the checksum of the data, in Buffer.
115
- * @returns {Buffer}
116
- */
117
- hashBuffer() {
118
- return Buffer.from(this.hashBase16(), "hex");
42
+ if (this.#hashUint8Array === null) {
43
+ const hex = this.hashHex();
44
+ const bytes = [];
45
+ for (let index = 0; index < hex.length; index += 2) {
46
+ bytes.push(hex.slice(index, index + 2));
47
+ }
48
+ this.#hashUint8Array = Uint8Array.from(bytes.map((byte) => {
49
+ return Number.parseInt(byte, 16);
50
+ }));
51
+ }
52
+ return Uint8Array.from(this.#hashUint8Array);
119
53
  }
120
54
  /**
121
- * Get the checksum of the data, in hex/hexadecimal without padding.
55
+ * Get the checksum of the data, in hexadecimal with padding.
122
56
  * @returns {string}
123
57
  */
124
58
  hashHex() {
125
- return this.hashBase16();
126
- }
127
- /**
128
- * Get the checksum of the data, in hex/hexadecimal with padding.
129
- * @returns {string}
130
- */
131
- hashHexPadding() {
132
- return this.hashHex().padStart(8, "0");
133
- }
134
- /**
135
- * Get the checksum of the data, in number.
136
- * @returns {number}
137
- */
138
- hashNumber() {
139
- return Number(this.hash());
59
+ if (this.#hashHex === null) {
60
+ this.#hashHex = (this.#b * 65536n + this.#a).toString(16).toUpperCase().padStart(8, "0");
61
+ if (this.#hashHex.length !== 8) {
62
+ throw new Error(`Unexpected hash hex result \`${this.#hashHex}\`! Please submit a bug report.`);
63
+ }
64
+ }
65
+ return this.#hashHex;
140
66
  }
141
67
  /**
142
68
  * Append data.
@@ -144,24 +70,28 @@ export class Adler32 {
144
70
  * @returns {this}
145
71
  */
146
72
  update(data) {
147
- this.#clearStorage();
148
- for (const byte of ((typeof data === "string") ? new TextEncoder().encode(data) : data)) {
73
+ if (this.#freezed) {
74
+ throw new Error(`Instance is freezed!`);
75
+ }
76
+ this.#hashHex = null;
77
+ this.#hashUint8Array = null;
78
+ const dataFmt = (typeof data === "string") ? new TextEncoder().encode(data) : data;
79
+ for (const byte of dataFmt) {
149
80
  this.#a = (this.#a + BigInt(byte)) % 65521n;
150
81
  this.#b = (this.#b + this.#a) % 65521n;
151
82
  }
152
83
  return this;
153
84
  }
154
85
  /**
155
- * Initialize from the readable stream.
156
- * @param {ReadableStream<Adler32AcceptDataType>} stream Readable stream.
157
- * @returns {Promise<Adler32>}
86
+ * Append data from the readable stream.
87
+ * @param {ReadableStream<Adler32AcceptDataType>} stream Data from the readable stream.
88
+ * @returns {Promise<this>}
158
89
  */
159
- static async fromStream(stream) {
160
- const instance = new this();
90
+ async updateFromStream(stream) {
161
91
  for await (const chunk of stream) {
162
- instance.update(chunk);
92
+ this.update(chunk);
163
93
  }
164
- return instance;
94
+ return this;
165
95
  }
166
96
  }
167
97
  export default Adler32;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hugoalh/adler32",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "A module to get the checksum of the data with algorithm Adler32.",
5
5
  "keywords": [
6
6
  "adler32",