@hugoalh/adler32 0.2.0 → 0.3.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 +3 -2
- package/cli.js +68 -5
- package/mod.d.ts +23 -24
- package/mod.d.ts.map +1 -1
- package/mod.js +41 -97
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -58,16 +58,17 @@ An ES (JavaScript & TypeScript) CLI and module to get the checksum of the data w
|
|
|
58
58
|
- ```ts
|
|
59
59
|
class Adler32 {
|
|
60
60
|
constructor(data?: Adler32AcceptDataType);
|
|
61
|
+
get freezed(): boolean;
|
|
62
|
+
freeze(): this;
|
|
61
63
|
hash(): bigint;
|
|
62
64
|
hashBase16(): string;
|
|
63
65
|
hashBase32Hex(): string;
|
|
64
66
|
hashBase36(): string;
|
|
67
|
+
hashBigInt(): bigint;
|
|
65
68
|
hashHex(): string;
|
|
66
69
|
hashHexPadding(): string;
|
|
67
70
|
hashNumber(): number;
|
|
68
71
|
update(data: Adler32AcceptDataType): this;
|
|
69
|
-
static fromFile(filePath: string | URL): Promise<Adler32>;
|
|
70
|
-
static fromFileSync(filePath: string | URL): Adler32;
|
|
71
72
|
static fromStream(stream: ReadableStream<Adler32AcceptDataType>): Promise<Adler32>;
|
|
72
73
|
}
|
|
73
74
|
```
|
package/cli.js
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import "./_dnt.polyfills.js";
|
|
3
3
|
import * as dntShim from "./_dnt.shims.js";
|
|
4
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
5
|
+
if (value !== null && value !== void 0) {
|
|
6
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
7
|
+
var dispose, inner;
|
|
8
|
+
if (async) {
|
|
9
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
10
|
+
dispose = value[Symbol.asyncDispose];
|
|
11
|
+
}
|
|
12
|
+
if (dispose === void 0) {
|
|
13
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
14
|
+
dispose = value[Symbol.dispose];
|
|
15
|
+
if (async) inner = dispose;
|
|
16
|
+
}
|
|
17
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
18
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
19
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
20
|
+
}
|
|
21
|
+
else if (async) {
|
|
22
|
+
env.stack.push({ async: true });
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
26
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
27
|
+
return function (env) {
|
|
28
|
+
function fail(e) {
|
|
29
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
30
|
+
env.hasError = true;
|
|
31
|
+
}
|
|
32
|
+
var r, s = 0;
|
|
33
|
+
function next() {
|
|
34
|
+
while (r = env.stack.pop()) {
|
|
35
|
+
try {
|
|
36
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
37
|
+
if (r.dispose) {
|
|
38
|
+
var result = r.dispose.call(r.value);
|
|
39
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
40
|
+
}
|
|
41
|
+
else s |= 1;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
fail(e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
48
|
+
if (env.hasError) throw env.error;
|
|
49
|
+
}
|
|
50
|
+
return next();
|
|
51
|
+
};
|
|
52
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
53
|
+
var e = new Error(message);
|
|
54
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
55
|
+
});
|
|
4
56
|
import { parseArgs } from "./deps/jsr.io/@std/cli/1.0.9/parse_args.js";
|
|
5
57
|
import { Adler32 } from "./mod.js";
|
|
6
58
|
if (!(import.meta.url === ("file:///" + process.argv[1].replace(/\\/g, "/")).replace(/\/{3,}/, "///"))) {
|
|
@@ -21,13 +73,24 @@ if (fromFile && fromStdin) {
|
|
|
21
73
|
throw new SyntaxError(`Unable to use the sources of file and stdin together!`);
|
|
22
74
|
}
|
|
23
75
|
if (fromFile) {
|
|
24
|
-
|
|
25
|
-
|
|
76
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
77
|
+
try {
|
|
78
|
+
if (argsValues.length === 0) {
|
|
79
|
+
throw new SyntaxError(`File path is not defined!`);
|
|
80
|
+
}
|
|
81
|
+
if (argsValues.length !== 1) {
|
|
82
|
+
throw new SyntaxError(`Too many arguments! Expect: 1; Current: ${argsValues.length}.`);
|
|
83
|
+
}
|
|
84
|
+
const file = __addDisposableResource(env_1, await dntShim.Deno.open(argsValues[0]), false);
|
|
85
|
+
console.log((await Adler32.fromStream(file.readable)).hashHexPadding());
|
|
26
86
|
}
|
|
27
|
-
|
|
28
|
-
|
|
87
|
+
catch (e_1) {
|
|
88
|
+
env_1.error = e_1;
|
|
89
|
+
env_1.hasError = true;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
__disposeResources(env_1);
|
|
29
93
|
}
|
|
30
|
-
console.log((await Adler32.fromFile(argsValues[0])).hashHexPadding());
|
|
31
94
|
}
|
|
32
95
|
else if (fromStdin) {
|
|
33
96
|
if (argsValues.length !== 0) {
|
package/mod.d.ts
CHANGED
|
@@ -5,13 +5,24 @@ export type Adler32AcceptDataType = string | BigUint64Array | Uint8Array | Uint1
|
|
|
5
5
|
*/
|
|
6
6
|
export declare class Adler32 {
|
|
7
7
|
#private;
|
|
8
|
+
get [Symbol.toStringTag](): string;
|
|
8
9
|
/**
|
|
9
10
|
* Initialize.
|
|
10
11
|
* @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update}.
|
|
11
12
|
*/
|
|
12
13
|
constructor(data?: Adler32AcceptDataType);
|
|
13
14
|
/**
|
|
14
|
-
*
|
|
15
|
+
* Whether the instance is freezed.
|
|
16
|
+
* @returns {boolean}
|
|
17
|
+
*/
|
|
18
|
+
get freezed(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Freeze the instance to prevent any update.
|
|
21
|
+
* @returns {this}
|
|
22
|
+
*/
|
|
23
|
+
freeze(): this;
|
|
24
|
+
/**
|
|
25
|
+
* Get the checksum of the data, in original format.
|
|
15
26
|
* @returns {bigint}
|
|
16
27
|
*/
|
|
17
28
|
hash(): bigint;
|
|
@@ -30,6 +41,16 @@ export declare class Adler32 {
|
|
|
30
41
|
* @returns {string}
|
|
31
42
|
*/
|
|
32
43
|
hashBase36(): string;
|
|
44
|
+
/**
|
|
45
|
+
* Get the checksum of the data, in big integer.
|
|
46
|
+
* @returns {bigint}
|
|
47
|
+
*/
|
|
48
|
+
hashBigInt(): bigint;
|
|
49
|
+
/**
|
|
50
|
+
* Get the checksum of the data, in big integer.
|
|
51
|
+
* @returns {bigint}
|
|
52
|
+
*/
|
|
53
|
+
hashBigInteger: () => bigint;
|
|
33
54
|
/**
|
|
34
55
|
* Get the checksum of the data, in hex/hexadecimal without padding.
|
|
35
56
|
* @returns {string}
|
|
@@ -52,29 +73,7 @@ export declare class Adler32 {
|
|
|
52
73
|
*/
|
|
53
74
|
update(data: Adler32AcceptDataType): this;
|
|
54
75
|
/**
|
|
55
|
-
* Initialize from
|
|
56
|
-
*
|
|
57
|
-
* > **🛡️ Runtime Permissions**
|
|
58
|
-
* >
|
|
59
|
-
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
|
|
60
|
-
* > - *Resources*
|
|
61
|
-
* @param {string | URL} filePath Path of the file.
|
|
62
|
-
* @returns {Promise<Adler32>}
|
|
63
|
-
*/
|
|
64
|
-
static fromFile(filePath: string | URL): Promise<Adler32>;
|
|
65
|
-
/**
|
|
66
|
-
* Initialize from file, synchronously.
|
|
67
|
-
*
|
|
68
|
-
* > **🛡️ Runtime Permissions**
|
|
69
|
-
* >
|
|
70
|
-
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
|
|
71
|
-
* > - *Resources*
|
|
72
|
-
* @param {string | URL} filePath Path of the file.
|
|
73
|
-
* @returns {Adler32}
|
|
74
|
-
*/
|
|
75
|
-
static fromFileSync(filePath: string | URL): Adler32;
|
|
76
|
-
/**
|
|
77
|
-
* Initialize from readable stream, asynchronously.
|
|
76
|
+
* Initialize from the readable stream, asynchronously.
|
|
78
77
|
* @param {ReadableStream<Adler32AcceptDataType>} stream Readable stream.
|
|
79
78
|
* @returns {Promise<Adler32>}
|
|
80
79
|
*/
|
package/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAC7B,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;IAKD;;;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,MAAM;IAMd;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;OAGG;IACH,aAAa,IAAI,MAAM;IAGvB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAGpB;;;OAGG;IACH,cAAc,EAAE,MAAM,MAAM,CAAmB;IAC/C;;;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;IAWzC;;;;OAIG;WACU,UAAU,CAAC,MAAM,EAAE,cAAc,CAAC,qBAAqB,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;CAOxF;AACD,eAAe,OAAO,CAAC"}
|
package/mod.js
CHANGED
|
@@ -1,64 +1,15 @@
|
|
|
1
1
|
import "./_dnt.polyfills.js";
|
|
2
|
-
import * as dntShim from "./_dnt.shims.js";
|
|
3
|
-
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
4
|
-
if (value !== null && value !== void 0) {
|
|
5
|
-
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
6
|
-
var dispose, inner;
|
|
7
|
-
if (async) {
|
|
8
|
-
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
9
|
-
dispose = value[Symbol.asyncDispose];
|
|
10
|
-
}
|
|
11
|
-
if (dispose === void 0) {
|
|
12
|
-
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
13
|
-
dispose = value[Symbol.dispose];
|
|
14
|
-
if (async) inner = dispose;
|
|
15
|
-
}
|
|
16
|
-
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
17
|
-
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
18
|
-
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
19
|
-
}
|
|
20
|
-
else if (async) {
|
|
21
|
-
env.stack.push({ async: true });
|
|
22
|
-
}
|
|
23
|
-
return value;
|
|
24
|
-
};
|
|
25
|
-
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
26
|
-
return function (env) {
|
|
27
|
-
function fail(e) {
|
|
28
|
-
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
29
|
-
env.hasError = true;
|
|
30
|
-
}
|
|
31
|
-
var r, s = 0;
|
|
32
|
-
function next() {
|
|
33
|
-
while (r = env.stack.pop()) {
|
|
34
|
-
try {
|
|
35
|
-
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
36
|
-
if (r.dispose) {
|
|
37
|
-
var result = r.dispose.call(r.value);
|
|
38
|
-
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
39
|
-
}
|
|
40
|
-
else s |= 1;
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
fail(e);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
47
|
-
if (env.hasError) throw env.error;
|
|
48
|
-
}
|
|
49
|
-
return next();
|
|
50
|
-
};
|
|
51
|
-
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
52
|
-
var e = new Error(message);
|
|
53
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
54
|
-
});
|
|
55
2
|
/**
|
|
56
3
|
* Get the checksum of the data with algorithm Adler32.
|
|
57
4
|
*/
|
|
58
5
|
export class Adler32 {
|
|
6
|
+
get [Symbol.toStringTag]() {
|
|
7
|
+
return "Adler32";
|
|
8
|
+
}
|
|
9
|
+
#freezed = false;
|
|
10
|
+
#hash = null;
|
|
59
11
|
#a = 1n;
|
|
60
12
|
#b = 0n;
|
|
61
|
-
#hash = null;
|
|
62
13
|
/**
|
|
63
14
|
* Initialize.
|
|
64
15
|
* @param {Adler32AcceptDataType} [data] Data. Can append later via the method {@linkcode Adler32.update}.
|
|
@@ -69,7 +20,22 @@ export class Adler32 {
|
|
|
69
20
|
}
|
|
70
21
|
}
|
|
71
22
|
/**
|
|
72
|
-
*
|
|
23
|
+
* Whether the instance is freezed.
|
|
24
|
+
* @returns {boolean}
|
|
25
|
+
*/
|
|
26
|
+
get freezed() {
|
|
27
|
+
return this.#freezed;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Freeze the instance to prevent any update.
|
|
31
|
+
* @returns {this}
|
|
32
|
+
*/
|
|
33
|
+
freeze() {
|
|
34
|
+
this.#freezed = true;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the checksum of the data, in original format.
|
|
73
39
|
* @returns {bigint}
|
|
74
40
|
*/
|
|
75
41
|
hash() {
|
|
@@ -83,22 +49,34 @@ export class Adler32 {
|
|
|
83
49
|
* @returns {string}
|
|
84
50
|
*/
|
|
85
51
|
hashBase16() {
|
|
86
|
-
return this.
|
|
52
|
+
return this.hashBigInt().toString(16).toUpperCase();
|
|
87
53
|
}
|
|
88
54
|
/**
|
|
89
55
|
* Get the checksum of the data, in Base32Hex ({@link https://datatracker.ietf.org/doc/html/rfc4648#section-7 RFC 4648 §7}).
|
|
90
56
|
* @returns {string}
|
|
91
57
|
*/
|
|
92
58
|
hashBase32Hex() {
|
|
93
|
-
return this.
|
|
59
|
+
return this.hashBigInt().toString(32).toUpperCase();
|
|
94
60
|
}
|
|
95
61
|
/**
|
|
96
62
|
* Get the checksum of the data, in Base36.
|
|
97
63
|
* @returns {string}
|
|
98
64
|
*/
|
|
99
65
|
hashBase36() {
|
|
100
|
-
return this.
|
|
66
|
+
return this.hashBigInt().toString(36).toUpperCase();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the checksum of the data, in big integer.
|
|
70
|
+
* @returns {bigint}
|
|
71
|
+
*/
|
|
72
|
+
hashBigInt() {
|
|
73
|
+
return this.hash();
|
|
101
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Get the checksum of the data, in big integer.
|
|
77
|
+
* @returns {bigint}
|
|
78
|
+
*/
|
|
79
|
+
hashBigInteger = this.hashBigInt;
|
|
102
80
|
/**
|
|
103
81
|
* Get the checksum of the data, in hex/hexadecimal without padding.
|
|
104
82
|
* @returns {string}
|
|
@@ -126,6 +104,9 @@ export class Adler32 {
|
|
|
126
104
|
* @returns {this}
|
|
127
105
|
*/
|
|
128
106
|
update(data) {
|
|
107
|
+
if (this.#freezed) {
|
|
108
|
+
throw new Error(`Instance is freezed!`);
|
|
109
|
+
}
|
|
129
110
|
this.#hash = null;
|
|
130
111
|
for (const byte of ((typeof data === "string") ? new TextEncoder().encode(data) : data)) {
|
|
131
112
|
this.#a = (this.#a + BigInt(byte)) % 65521n;
|
|
@@ -134,49 +115,12 @@ export class Adler32 {
|
|
|
134
115
|
return this;
|
|
135
116
|
}
|
|
136
117
|
/**
|
|
137
|
-
* Initialize from
|
|
138
|
-
*
|
|
139
|
-
* > **🛡️ Runtime Permissions**
|
|
140
|
-
* >
|
|
141
|
-
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
|
|
142
|
-
* > - *Resources*
|
|
143
|
-
* @param {string | URL} filePath Path of the file.
|
|
144
|
-
* @returns {Promise<Adler32>}
|
|
145
|
-
*/
|
|
146
|
-
static async fromFile(filePath) {
|
|
147
|
-
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
148
|
-
try {
|
|
149
|
-
const file = __addDisposableResource(env_1, await dntShim.Deno.open(filePath), false);
|
|
150
|
-
return await Adler32.fromStream(file.readable);
|
|
151
|
-
}
|
|
152
|
-
catch (e_1) {
|
|
153
|
-
env_1.error = e_1;
|
|
154
|
-
env_1.hasError = true;
|
|
155
|
-
}
|
|
156
|
-
finally {
|
|
157
|
-
__disposeResources(env_1);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Initialize from file, synchronously.
|
|
162
|
-
*
|
|
163
|
-
* > **🛡️ Runtime Permissions**
|
|
164
|
-
* >
|
|
165
|
-
* > - File System - Read \[Deno: `read`; NodeJS (>= v20.9.0) 🧪: `fs-read`\]
|
|
166
|
-
* > - *Resources*
|
|
167
|
-
* @param {string | URL} filePath Path of the file.
|
|
168
|
-
* @returns {Adler32}
|
|
169
|
-
*/
|
|
170
|
-
static fromFileSync(filePath) {
|
|
171
|
-
return new this(dntShim.Deno.readFileSync(filePath));
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Initialize from readable stream, asynchronously.
|
|
118
|
+
* Initialize from the readable stream, asynchronously.
|
|
175
119
|
* @param {ReadableStream<Adler32AcceptDataType>} stream Readable stream.
|
|
176
120
|
* @returns {Promise<Adler32>}
|
|
177
121
|
*/
|
|
178
122
|
static async fromStream(stream) {
|
|
179
|
-
const instance = new
|
|
123
|
+
const instance = new this();
|
|
180
124
|
for await (const chunk of stream) {
|
|
181
125
|
instance.update(chunk);
|
|
182
126
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hugoalh/adler32",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A CLI and module to get the checksum of the data with algorithm Adler32.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"adler32"
|
|
6
|
+
"adler32",
|
|
7
|
+
"checksum"
|
|
7
8
|
],
|
|
8
9
|
"homepage": "https://github.com/hugoalh/adler32-es#readme",
|
|
9
10
|
"bugs": {
|