@softchef/cdk-iot-device-management
Version:
IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.
27 lines (26 loc) • 1.01 kB
JavaScript
import { createReadStream } from "fs";
import { HashCalculator } from "./HashCalculator";
export var fileStreamHasher = function (hashCtor, fileStream) {
return new Promise(function (resolve, reject) {
if (!isReadStream(fileStream)) {
reject(new Error("Unable to calculate hash for non-file streams."));
return;
}
var fileStreamTee = createReadStream(fileStream.path, {
start: fileStream.start,
end: fileStream.end,
});
var hash = new hashCtor();
var hashCalculator = new HashCalculator(hash);
fileStreamTee.pipe(hashCalculator);
fileStreamTee.on("error", function (err) {
hashCalculator.end();
reject(err);
});
hashCalculator.on("error", reject);
hashCalculator.on("finish", function () {
hash.digest().then(resolve).catch(reject);
});
});
};
var isReadStream = function (stream) { return typeof stream.path === "string"; };