@hugoalh/adler32 0.4.0 → 0.5.1

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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright © 2024\~2025 hugoalh
3
+ Copyright © 2024\~2026 HUGOALH
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/README.md CHANGED
@@ -2,54 +2,52 @@
2
2
 
3
3
  [**⚖️** MIT](./LICENSE.md)
4
4
 
5
- [![GitHub: hugoalh/adler32-es](https://img.shields.io/github/v/release/hugoalh/adler32-es?label=hugoalh/adler32-es&labelColor=181717&logo=github&logoColor=ffffff&sort=semver&style=flat "GitHub: hugoalh/adler32-es")](https://github.com/hugoalh/adler32-es)
6
- [![JSR: @hugoalh/adler32](https://img.shields.io/jsr/v/@hugoalh/adler32?label=@hugoalh/adler32&labelColor=F7DF1E&logo=jsr&logoColor=000000&style=flat "JSR: @hugoalh/adler32")](https://jsr.io/@hugoalh/adler32)
7
- [![NPM: @hugoalh/adler32](https://img.shields.io/npm/v/@hugoalh/adler32?label=@hugoalh/adler32&labelColor=CB3837&logo=npm&logoColor=ffffff&style=flat "NPM: @hugoalh/adler32")](https://www.npmjs.com/package/@hugoalh/adler32)
5
+ 🔗
6
+ [DistBoard @hugoalh](https://hugoalh.github.io/distboard/adler32_ecmascript)
7
+ [GitHub](https://github.com/hugoalh/adler32-es)
8
+ ● [JSR](https://jsr.io/@hugoalh/adler32)
9
+ ● [NPM](https://www.npmjs.com/package/@hugoalh/adler32)
8
10
 
9
- An ECMAScript (JavaScript & TypeScript) module to get the checksum of the data with algorithm Adler32.
11
+ An ECMAScript module to get the checksum of the data with algorithm Adler32.
10
12
 
11
- ## 🔰 Begin
13
+ ## 🎯 Runtime Targets
12
14
 
13
- ### 🎯 Targets
15
+ Any runtime which support ECMAScript should able to use this; These runtimes are officially supported:
14
16
 
15
- | **Targets** | **Remote** | **JSR** | **NPM** |
16
- |:--|:-:|:-:|:-:|
17
- | **[Bun](https://bun.sh/)** >= v1.1.0 | ❌ | ✔️ | ✔️ |
18
- | **[Deno](https://deno.land/)** >= v2.1.0 | ✔️ | ✔️ | ✔️ |
19
- | **[NodeJS](https://nodejs.org/)** >= v20.9.0 | ❌ | ✔️ | ✔️ |
17
+ - **[Bun](https://bun.sh/)** >= v1.1.0
18
+ - **[Deno](https://deno.land/)** >= v2.1.0
19
+ - **[NodeJS](https://nodejs.org/)** >= v20.9.0
20
20
 
21
- > [!NOTE]
22
- > - It is possible to use this module in other methods/ways which not listed in here, however those methods/ways are not officially supported, and should beware maybe cause security issues.
21
+ ## 🛡️ Runtime Permissions
22
+
23
+ This does not request any runtime permission.
23
24
 
24
- ### #️⃣ Resources Identifier
25
+ ## #️⃣ Sources & Entrypoints
25
26
 
26
- - **Remote - GitHub Raw:**
27
+ - GitHub Raw
27
28
  ```
28
29
  https://raw.githubusercontent.com/hugoalh/adler32-es/{Tag}/mod.ts
29
30
  ```
30
- - **JSR:**
31
+ - JSR
31
32
  ```
32
- [jsr:]@hugoalh/adler32[@{Tag}]
33
+ jsr:@hugoalh/adler32[@{Tag}]
33
34
  ```
34
- - **NPM:**
35
+ - NPM
35
36
  ```
36
- [npm:]@hugoalh/adler32[@{Tag}]
37
+ npm:@hugoalh/adler32[@{Tag}]
37
38
  ```
38
39
 
39
- > [!NOTE]
40
- > - For usage of remote resources, it is recommended to import the entire module with the main path `mod.ts`, however it is also able to import part of the module with sub path if available, but do not import if:
41
- >
42
- > - it's path has an underscore prefix (e.g.: `_foo.ts`, `_util/bar.ts`), or
43
- > - it is a benchmark or test file (e.g.: `foo.bench.ts`, `foo.test.ts`), or
44
- > - it's symbol has an underscore prefix (e.g.: `_bar`, `_foo`).
45
- >
46
- > These elements are not considered part of the public API, thus no stability is guaranteed for them.
47
- > - For usage of JSR or NPM resources, it is recommended to import the entire module with the main entrypoint, however it is also able to import part of the module with sub entrypoint if available, please visit the [file `jsr.jsonc`](./jsr.jsonc) property `exports` for available sub entrypoints.
48
- > - It is recommended to use this module with tag for immutability.
40
+ | **Name** | **Path** | **Description** |
41
+ |:--|:--|:--|
42
+ | `.` | `./mod.ts` | Default. |
49
43
 
50
- ### 🛡️ Runtime Permissions
51
-
52
- *This module does not request any runtime permission.*
44
+ > [!NOTE]
45
+ > - Different runtimes have vary support for the sources and entrypoints, visit the runtime documentation for more information.
46
+ > - It is recommended to include tag for immutability.
47
+ > - These are not part of the public APIs hence should not be used:
48
+ > - Benchmark/Test file (e.g.: `example.bench.ts`, `example.test.ts`).
49
+ > - Entrypoint name or path include any underscore prefix (e.g.: `_example.ts`, `foo/_example.ts`).
50
+ > - Identifier/Namespace/Symbol include any underscore prefix (e.g.: `_example`, `Foo._example`).
53
51
 
54
52
  ## 🧩 APIs
55
53
 
@@ -58,33 +56,29 @@ An ECMAScript (JavaScript & TypeScript) module to get the checksum of the data w
58
56
  constructor(data?: Adler32AcceptDataType);
59
57
  get freezed(): boolean;
60
58
  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;
59
+ hash(): Uint8Array;
69
60
  hashHex(): string;
70
- hashHexPadding(): string;
71
- hashNumber(): number;
72
61
  update(data: Adler32AcceptDataType): this;
73
- static fromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<Adler32>;
62
+ updateFromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<this>;
74
63
  }
75
64
  ```
76
65
  - ```ts
77
- type Adler32AcceptDataType = string | BigUint64Array | Uint8Array | Uint16Array | Uint32Array;
66
+ type Adler32AcceptDataType =
67
+ | string
68
+ | BigUint64Array
69
+ | Uint8Array
70
+ | Uint16Array
71
+ | Uint32Array;
78
72
  ```
79
73
 
80
74
  > [!NOTE]
81
75
  > - For the full or prettier documentation, can visit via:
82
- > - [Deno CLI `deno doc`](https://docs.deno.com/runtime/reference/cli/documentation_generator/)
76
+ > - [Deno CLI `deno doc`](https://docs.deno.com/runtime/reference/cli/doc/)
83
77
  > - [JSR](https://jsr.io/@hugoalh/adler32)
84
78
 
85
79
  ## ✍️ Examples
86
80
 
87
81
  - ```ts
88
- new Adler32("Wikipedia").hashHexPadding();
82
+ new Adler32("Wikipedia").hashHex();
89
83
  //=> "11E60398"
90
84
  ```
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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ACH,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;IAIlB;;;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,7 @@
1
- import { Buffer } from "node:buffer";
1
+ if (typeof Uint8Array.fromHex === "undefined") {
2
+ //deno-lint-ignore hugoalh/no-import-dynamic -- Polyfill.
3
+ await import("es-arraybuffer-base64/Uint8Array.fromHex/auto");
4
+ }
2
5
  /**
3
6
  * Get the checksum of the data with algorithm Adler32.
4
7
  */
@@ -7,34 +10,19 @@ export class Adler32 {
7
10
  return "Adler32";
8
11
  }
9
12
  #freezed = false;
10
- #hash = null;
11
- #hashBase16 = null;
12
- #hashBase32Hex = null;
13
- #hashBase36 = null;
14
- #hashBase64 = null;
15
- #hashBase64URL = null;
13
+ #hashHex = null;
14
+ #hashUint8Array = null;
16
15
  #a = 1n;
17
16
  #b = 0n;
18
17
  /**
19
18
  * Initialize.
20
- * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update}.
19
+ * @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update} and {@linkcode Adler32.updateFromStream}.
21
20
  */
22
21
  constructor(data) {
23
22
  if (typeof data !== "undefined") {
24
23
  this.update(data);
25
24
  }
26
25
  }
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
26
  /**
39
27
  * Whether the instance is freezed.
40
28
  * @returns {boolean}
@@ -51,92 +39,25 @@ export class Adler32 {
51
39
  return this;
52
40
  }
53
41
  /**
54
- * Get the checksum of the data, in original format.
55
- * @returns {bigint}
42
+ * Get the checksum of the data, in Uint8Array.
43
+ * @returns {Uint8Array}
56
44
  */
57
45
  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;
46
+ this.#hashUint8Array ??= Uint8Array.fromHex(this.hashHex());
47
+ return Uint8Array.from(this.#hashUint8Array);
68
48
  }
69
49
  /**
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");
119
- }
120
- /**
121
- * Get the checksum of the data, in hex/hexadecimal without padding.
50
+ * Get the checksum of the data, in hexadecimal with padding.
122
51
  * @returns {string}
123
52
  */
124
53
  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());
54
+ if (this.#hashHex === null) {
55
+ this.#hashHex = (this.#b * 65536n + this.#a).toString(16).toUpperCase().padStart(8, "0");
56
+ if (this.#hashHex.length !== 8) {
57
+ throw new Error(`Unexpected hash hex result \`${this.#hashHex}\`! Please submit a bug report.`);
58
+ }
59
+ }
60
+ return this.#hashHex;
140
61
  }
141
62
  /**
142
63
  * Append data.
@@ -144,24 +65,28 @@ export class Adler32 {
144
65
  * @returns {this}
145
66
  */
146
67
  update(data) {
147
- this.#clearStorage();
148
- for (const byte of ((typeof data === "string") ? new TextEncoder().encode(data) : data)) {
68
+ if (this.#freezed) {
69
+ throw new Error(`Instance is freezed!`);
70
+ }
71
+ this.#hashHex = null;
72
+ this.#hashUint8Array = null;
73
+ const dataFmt = (typeof data === "string") ? new TextEncoder().encode(data) : data;
74
+ for (const byte of dataFmt) {
149
75
  this.#a = (this.#a + BigInt(byte)) % 65521n;
150
76
  this.#b = (this.#b + this.#a) % 65521n;
151
77
  }
152
78
  return this;
153
79
  }
154
80
  /**
155
- * Initialize from the readable stream.
156
- * @param {ReadableStream<Adler32AcceptDataType>} stream Readable stream.
157
- * @returns {Promise<Adler32>}
81
+ * Append data from the readable stream.
82
+ * @param {ReadableStream<Adler32AcceptDataType>} stream Data from the readable stream.
83
+ * @returns {Promise<this>}
158
84
  */
159
- static async fromStream(stream) {
160
- const instance = new this();
85
+ async updateFromStream(stream) {
161
86
  for await (const chunk of stream) {
162
- instance.update(chunk);
87
+ this.update(chunk);
163
88
  }
164
- return instance;
89
+ return this;
165
90
  }
166
91
  }
167
92
  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.1",
4
4
  "description": "A module to get the checksum of the data with algorithm Adler32.",
5
5
  "keywords": [
6
6
  "adler32",
@@ -28,11 +28,12 @@
28
28
  "type": "git",
29
29
  "url": "git+https://github.com/hugoalh/adler32-es.git"
30
30
  },
31
- "scripts": {},
31
+ "dependencies": {
32
+ "es-arraybuffer-base64": "^1.1.2"
33
+ },
32
34
  "devDependencies": {
33
- "@types/node": "^20.9.0"
35
+ "@types/node": "^24.5.0"
34
36
  },
35
- "engines": {},
36
37
  "private": false,
37
38
  "publishConfig": {
38
39
  "access": "public"