UNPKG

@google-cloud/storage

Version:
115 lines (114 loc) • 3.98 kB
// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { createHash } from 'crypto'; import { Transform } from 'stream'; import { CRC32C_DEFAULT_VALIDATOR_GENERATOR, } from './crc32c.js'; import { FileExceptionMessages, RequestError } from './file.js'; class HashStreamValidator extends Transform { crc32cEnabled; md5Enabled; crc32cExpected; md5Expected; updateHashesOnly = false; #crc32cHash = undefined; #md5Hash = undefined; #md5Digest = ''; constructor(options = {}) { super(); this.crc32cEnabled = !!options.crc32c; this.md5Enabled = !!options.md5; this.updateHashesOnly = !!options.updateHashesOnly; this.crc32cExpected = options.crc32cExpected; this.md5Expected = options.md5Expected; if (this.crc32cEnabled) { if (options.crc32cInstance) { this.#crc32cHash = options.crc32cInstance; } else { const crc32cGenerator = options.crc32cGenerator || CRC32C_DEFAULT_VALIDATOR_GENERATOR; this.#crc32cHash = crc32cGenerator(); } } if (this.md5Enabled) { this.#md5Hash = createHash('md5'); } } /** * Return the current CRC32C value, if available. */ get crc32c() { return this.#crc32cHash?.toString(); } /** * Return the calculated MD5 value, if available. */ get md5Digest() { if (this.#md5Hash && !this.#md5Digest) { this.#md5Digest = this.#md5Hash.digest('base64'); } return this.#md5Digest; } _flush(callback) { // Triggers the getter logic to finalize and cache the MD5 digest this.md5Digest; if (this.updateHashesOnly) { callback(); return; } // If we're doing validation, assume the worst-- a data integrity // mismatch. If not, these tests won't be performed, and we can assume // the best. // We must check if the server decompressed the data on serve because hash // validation is not possible in this case. let failed = this.crc32cEnabled || this.md5Enabled; if (this.crc32cEnabled && this.crc32cExpected) { failed = !this.test('crc32c', this.crc32cExpected); } if (this.md5Enabled && this.md5Expected) { failed = !this.test('md5', this.md5Expected); } if (failed) { const mismatchError = new RequestError(FileExceptionMessages.DOWNLOAD_MISMATCH); mismatchError.code = 'CONTENT_DOWNLOAD_MISMATCH'; callback(mismatchError); } else { callback(); } } _transform(chunk, encoding, callback) { this.push(chunk, encoding); try { if (this.#crc32cHash) this.#crc32cHash.update(chunk); if (this.#md5Hash) this.#md5Hash.update(chunk); callback(); } catch (e) { callback(e); } } test(hash, sum) { const check = Buffer.isBuffer(sum) ? sum.toString('base64') : sum; if (hash === 'crc32c' && this.#crc32cHash) { return this.#crc32cHash.validate(check); } if (hash === 'md5' && this.#md5Hash) { return this.#md5Digest === check; } return false; } } export { HashStreamValidator };