1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
16 | if (kind === "m") throw new TypeError("Private method is not writable");
|
17 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
18 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
19 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
20 | };
|
21 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
22 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
23 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
24 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
25 | };
|
26 | var _HashStreamValidator_crc32cHash, _HashStreamValidator_md5Hash, _HashStreamValidator_md5Digest;
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | exports.HashStreamValidator = void 0;
|
29 | const crypto_1 = require("crypto");
|
30 | const stream_1 = require("stream");
|
31 | const crc32c_js_1 = require("./crc32c.js");
|
32 | const file_js_1 = require("./file.js");
|
33 | class HashStreamValidator extends stream_1.Transform {
|
34 | constructor(options = {}) {
|
35 | super();
|
36 | this.updateHashesOnly = false;
|
37 | _HashStreamValidator_crc32cHash.set(this, undefined);
|
38 | _HashStreamValidator_md5Hash.set(this, undefined);
|
39 | _HashStreamValidator_md5Digest.set(this, '');
|
40 | this.crc32cEnabled = !!options.crc32c;
|
41 | this.md5Enabled = !!options.md5;
|
42 | this.updateHashesOnly = !!options.updateHashesOnly;
|
43 | this.crc32cExpected = options.crc32cExpected;
|
44 | this.md5Expected = options.md5Expected;
|
45 | if (this.crc32cEnabled) {
|
46 | if (options.crc32cInstance) {
|
47 | __classPrivateFieldSet(this, _HashStreamValidator_crc32cHash, options.crc32cInstance, "f");
|
48 | }
|
49 | else {
|
50 | const crc32cGenerator = options.crc32cGenerator || crc32c_js_1.CRC32C_DEFAULT_VALIDATOR_GENERATOR;
|
51 | __classPrivateFieldSet(this, _HashStreamValidator_crc32cHash, crc32cGenerator(), "f");
|
52 | }
|
53 | }
|
54 | if (this.md5Enabled) {
|
55 | __classPrivateFieldSet(this, _HashStreamValidator_md5Hash, (0, crypto_1.createHash)('md5'), "f");
|
56 | }
|
57 | }
|
58 | |
59 |
|
60 |
|
61 | get crc32c() {
|
62 | var _a;
|
63 | return (_a = __classPrivateFieldGet(this, _HashStreamValidator_crc32cHash, "f")) === null || _a === void 0 ? void 0 : _a.toString();
|
64 | }
|
65 | _flush(callback) {
|
66 | if (__classPrivateFieldGet(this, _HashStreamValidator_md5Hash, "f")) {
|
67 | __classPrivateFieldSet(this, _HashStreamValidator_md5Digest, __classPrivateFieldGet(this, _HashStreamValidator_md5Hash, "f").digest('base64'), "f");
|
68 | }
|
69 | if (this.updateHashesOnly) {
|
70 | callback();
|
71 | return;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | let failed = this.crc32cEnabled || this.md5Enabled;
|
79 | if (this.crc32cEnabled && this.crc32cExpected) {
|
80 | failed = !this.test('crc32c', this.crc32cExpected);
|
81 | }
|
82 | if (this.md5Enabled && this.md5Expected) {
|
83 | failed = !this.test('md5', this.md5Expected);
|
84 | }
|
85 | if (failed) {
|
86 | const mismatchError = new file_js_1.RequestError(file_js_1.FileExceptionMessages.DOWNLOAD_MISMATCH);
|
87 | mismatchError.code = 'CONTENT_DOWNLOAD_MISMATCH';
|
88 | callback(mismatchError);
|
89 | }
|
90 | else {
|
91 | callback();
|
92 | }
|
93 | }
|
94 | _transform(chunk, encoding, callback) {
|
95 | this.push(chunk, encoding);
|
96 | try {
|
97 | if (__classPrivateFieldGet(this, _HashStreamValidator_crc32cHash, "f"))
|
98 | __classPrivateFieldGet(this, _HashStreamValidator_crc32cHash, "f").update(chunk);
|
99 | if (__classPrivateFieldGet(this, _HashStreamValidator_md5Hash, "f"))
|
100 | __classPrivateFieldGet(this, _HashStreamValidator_md5Hash, "f").update(chunk);
|
101 | callback();
|
102 | }
|
103 | catch (e) {
|
104 | callback(e);
|
105 | }
|
106 | }
|
107 | test(hash, sum) {
|
108 | const check = Buffer.isBuffer(sum) ? sum.toString('base64') : sum;
|
109 | if (hash === 'crc32c' && __classPrivateFieldGet(this, _HashStreamValidator_crc32cHash, "f")) {
|
110 | return __classPrivateFieldGet(this, _HashStreamValidator_crc32cHash, "f").validate(check);
|
111 | }
|
112 | if (hash === 'md5' && __classPrivateFieldGet(this, _HashStreamValidator_md5Hash, "f")) {
|
113 | return __classPrivateFieldGet(this, _HashStreamValidator_md5Digest, "f") === check;
|
114 | }
|
115 | return false;
|
116 | }
|
117 | }
|
118 | exports.HashStreamValidator = HashStreamValidator;
|
119 | _HashStreamValidator_crc32cHash = new WeakMap(), _HashStreamValidator_md5Hash = new WeakMap(), _HashStreamValidator_md5Digest = new WeakMap();
|