UNPKG

6.04 kBJavaScriptView Raw
1"use strict";
2// Copyright 2022 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15var __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};
21var __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};
26var _HashStreamValidator_crc32cHash, _HashStreamValidator_md5Hash, _HashStreamValidator_md5Digest;
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.HashStreamValidator = void 0;
29const crypto_1 = require("crypto");
30const stream_1 = require("stream");
31const crc32c_js_1 = require("./crc32c.js");
32const file_js_1 = require("./file.js");
33class 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 * Return the current CRC32C value, if available.
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 // If we're doing validation, assume the worst-- a data integrity
74 // mismatch. If not, these tests won't be performed, and we can assume
75 // the best.
76 // We must check if the server decompressed the data on serve because hash
77 // validation is not possible in this case.
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}
118exports.HashStreamValidator = HashStreamValidator;
119_HashStreamValidator_crc32cHash = new WeakMap(), _HashStreamValidator_md5Hash = new WeakMap(), _HashStreamValidator_md5Digest = new WeakMap();