UNPKG

@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.

29 lines (28 loc) 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileStreamHasher = void 0; const fs_1 = require("fs"); const HashCalculator_1 = require("./HashCalculator"); const fileStreamHasher = (hashCtor, fileStream) => new Promise((resolve, reject) => { if (!isReadStream(fileStream)) { reject(new Error("Unable to calculate hash for non-file streams.")); return; } const fileStreamTee = fs_1.createReadStream(fileStream.path, { start: fileStream.start, end: fileStream.end, }); const hash = new hashCtor(); const hashCalculator = new HashCalculator_1.HashCalculator(hash); fileStreamTee.pipe(hashCalculator); fileStreamTee.on("error", (err) => { hashCalculator.end(); reject(err); }); hashCalculator.on("error", reject); hashCalculator.on("finish", function () { hash.digest().then(resolve).catch(reject); }); }); exports.fileStreamHasher = fileStreamHasher; const isReadStream = (stream) => typeof stream.path === "string";