Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.map(async (url) => {
const extension = path.extname(new URL(url).pathname);
const sourceFile = s3data.find((d) => d.endsWith(extension));
const sourceChecksum = await generateChecksumFromStream(
'cksum',
fs.createReadStream(require.resolve(sourceFile))
);
const file = files.find((f) => f.name.endsWith(extension));
const filepath = `/${file.bucket}/${file.filepath}`;
const fileStream = await getDistributionApiFileStream(filepath, accessToken);
// Compare checksum of downloaded file with expected checksum.
const downloadChecksum = await generateChecksumFromStream('cksum', fileStream);
return downloadChecksum === sourceChecksum;
})
);
.map(async (url) => {
const extension = path.extname(new URL(url).pathname);
const sourceFile = s3data.find((d) => d.endsWith(extension));
const sourceChecksum = await generateChecksumFromStream(
'cksum',
fs.createReadStream(require.resolve(sourceFile))
);
const file = files.find((f) => f.name.endsWith(extension));
const filepath = `/${file.bucket}/${file.filepath}`;
const fileStream = await getDistributionApiFileStream(filepath, accessToken);
// Compare checksum of downloaded file with expected checksum.
const downloadChecksum = await generateChecksumFromStream('cksum', fileStream);
return downloadChecksum === sourceChecksum;
})
);
let fileStream;
if (bucketsConfig.type(file.bucket) === 'protected') {
const fileUrl = getDistributionFileUrl({
bucket: file.bucket,
key: file.filepath
});
fileStream = await getDistributionApiFileStream(fileUrl, accessToken);
}
else if (bucketsConfig.type(file.bucket) === 'public') {
fileStream = got.stream(url);
}
// Compare checksum of downloaded file with expected checksum.
const downloadChecksum = await generateChecksumFromStream('cksum', fileStream);
return downloadChecksum === sourceChecksum;
})
);
it('downloads a public science file', async () => {
const s3SignedUrl = await getDistributionApiRedirect(publicFilePath);
const parts = new URL(s3SignedUrl);
const userName = parts.searchParams.get('x-EarthdataLoginUsername');
const fileStream = got.stream(s3SignedUrl);
const downloadChecksum = await generateChecksumFromStream('cksum', fileStream);
expect(userName).toEqual('unauthenticated user');
expect(downloadChecksum).toEqual(fileChecksum);
});
});
beforeAll(async () => {
accessToken = await getTestAccessToken();
fileChecksum = await generateChecksumFromStream(
'cksum',
fs.createReadStream(require.resolve(s3Data[0]))
);
publicFilePath = `/${publicBucketName}/${fileKey}`;
protectedFilePath = `/${protectedBucketName}/${fileKey}`;
privateFilePath = `/${privateBucketName}/${fileKey}`;
});
exports.calculateS3ObjectChecksum = async ({
algorithm,
bucket,
key,
options
}) => {
const fileStream = exports.getS3ObjectReadStream(bucket, key);
return generateChecksumFromStream(algorithm, fileStream, options);
};
.map(async (url) => {
const extension = path.extname(new URL(url).pathname);
const sourceFile = s3data.find((d) => d.endsWith(extension));
const sourceChecksum = await generateChecksumFromStream(
'cksum',
fs.createReadStream(require.resolve(sourceFile))
);
const file = files.find((f) => f.name.endsWith(extension));
let fileStream;
if (bucketsConfig.type(file.bucket) === 'protected') {
const fileUrl = getDistributionFileUrl({
bucket: file.bucket,
key: file.filepath
});
fileStream = await getDistributionApiFileStream(fileUrl, accessToken);
}
else if (bucketsConfig.type(file.bucket) === 'public') {
fileStream = got.stream(url);
exports.calculateS3ObjectChecksum = async ({
algorithm,
bucket,
key,
options
}) => {
const fileStream = exports.getS3ObjectReadStream(bucket, key);
return generateChecksumFromStream(algorithm, fileStream, options);
};
exports.validateS3ObjectChecksum = async ({
algorithm,
bucket,
key,
expectedSum,
options
}) => {
const fileStream = exports.getS3ObjectReadStream(bucket, key);
if (await validateChecksumFromStream(algorithm, fileStream, expectedSum, options)) {
return true;
}
const msg = `Invalid checksum for S3 object s3://${bucket}/${key} with type ${algorithm} and expected sum ${expectedSum}`;
throw new errors.InvalidChecksum(msg);
};
exports.validateS3ObjectChecksum = async ({
algorithm,
bucket,
key,
expectedSum,
options
}) => {
const fileStream = exports.getS3ObjectReadStream(bucket, key);
if (await validateChecksumFromStream(algorithm, fileStream, expectedSum, options)) {
return true;
}
const msg = `Invalid checksum for S3 object s3://${bucket}/${key} with type ${algorithm} and expected sum ${expectedSum}`;
throw new InvalidChecksum(msg);
};