Skip to content

Commit

Permalink
Revert "fix: revert skip validation (#1718)" (#1721)
Browse files Browse the repository at this point in the history
This reverts commit 0c75e33.
  • Loading branch information
shaffeeullah committed Nov 24, 2021
1 parent db4e2df commit 6490abf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/file.ts
Expand Up @@ -1272,7 +1272,7 @@ class File extends ServiceObject<File> {

const throughStream = streamEvents(new PassThrough());

let isCompressed = true;
let isServedCompressed = true;
let crc32c = true;
let md5 = false;

Expand Down Expand Up @@ -1379,7 +1379,7 @@ class File extends ServiceObject<File> {
rawResponseStream.on('error', onComplete);

const headers = rawResponseStream.toJSON().headers;
isCompressed = headers['content-encoding'] === 'gzip';
isServedCompressed = headers['content-encoding'] === 'gzip';
const throughStreams: Writable[] = [];

if (shouldRunValidation) {
Expand All @@ -1400,7 +1400,7 @@ class File extends ServiceObject<File> {
throughStreams.push(validateStream);
}

if (isCompressed && options.decompress) {
if (isServedCompressed && options.decompress) {
throughStreams.push(zlib.createGunzip());
}

Expand Down Expand Up @@ -1440,13 +1440,23 @@ class File extends ServiceObject<File> {
return;
}

if (!isCompressed) {
// TODO(https://github.com/googleapis/nodejs-storage/issues/709):
// Remove once the backend issue is fixed.
// If object is stored compressed (having
// metadata.contentEncoding === 'gzip') and was served decompressed,
// then skip checksum validation because the remote checksum is computed
// against the compressed version of the object.
if (!isServedCompressed) {
try {
await this.getMetadata({userProject: options.userProject});
} catch (e) {
throughStream.destroy(e);
return;
}
if (this.metadata.contentEncoding === 'gzip') {
throughStream.end();
return;
}
}

// If we're doing validation, assume the worst-- a data integrity
Expand Down
27 changes: 27 additions & 0 deletions system-test/storage.ts
Expand Up @@ -2502,6 +2502,33 @@ describe('storage', () => {
});
});

it('should skip validation if file is served decompressed', async () => {
const filename = 'logo-gzipped.png';
await bucket.upload(FILES.logo.path, {destination: filename, gzip: true});

tmp.setGracefulCleanup();
const {name: tmpFilePath} = tmp.fileSync();

const file = bucket.file(filename);

await new Promise((resolve, reject) => {
file
.createReadStream()
.on('error', reject)
.on('response', raw => {
assert.strictEqual(
raw.toJSON().headers['content-encoding'],
undefined
);
})
.pipe(fs.createWriteStream(tmpFilePath))
.on('error', reject)
.on('finish', resolve);
});

await file.delete();
});

describe('simple write', () => {
it('should save arbitrary data', done => {
const file = bucket.file('TestFile');
Expand Down
18 changes: 18 additions & 0 deletions test/file.ts
Expand Up @@ -1387,6 +1387,24 @@ describe('File', () => {
file.requestStream = getFakeSuccessfulRequest(data);
});

describe('server decompression', () => {
it('should skip validation if file was stored compressed', done => {
file.metadata.contentEncoding = 'gzip';

const validateStub = sinon.stub().returns(true);
fakeValidationStream.test = validateStub;

file
.createReadStream({validation: 'crc32c'})
.on('error', done)
.on('end', () => {
assert(validateStub.notCalled);
done();
})
.resume();
});
});

it('should emit errors from the validation stream', done => {
const error = new Error('Error.');

Expand Down

0 comments on commit 6490abf

Please sign in to comment.