UNPKG

@microsoft/omnichannel-chat-sdk

Version:
275 lines 14.6 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var AMSFileManager_1 = require("./AMSFileManager"); var WebUtils_1 = require("../../utils/WebUtils"); var activityUtils_1 = require("./activityUtils"); var defaultScanPollingInterval = 7 * 1000; var defaultScanStatusRetrievalDelay = 1000; var AMSFileScanner = /** @class */ (function () { function AMSFileScanner(amsClient, options) { if (options === void 0) { options = {}; } this.scanResults = null; this.amsClient = amsClient; this.scanResults = new Map(); this.shouldQueueScan = false; this.test = false; this.options = options; this.queueScan(); } AMSFileScanner.prototype.queueScan = function () { return __awaiter(this, void 0, void 0, function () { var e_1, _a; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: this.shouldQueueScan = true; _c.label = 1; case 1: _c.trys.push([1, 3, 4, 8]); return [4 /*yield*/, this.scanFiles()]; case 2: _c.sent(); return [3 /*break*/, 8]; case 3: e_1 = _c.sent(); console.error(e_1); return [3 /*break*/, 8]; case 4: return [4 /*yield*/, WebUtils_1.default.sleep(((_b = this.options) === null || _b === void 0 ? void 0 : _b.pollingInterval) || defaultScanPollingInterval)]; case 5: _c.sent(); _a = this.shouldQueueScan && !this.test; if (!_a) return [3 /*break*/, 7]; return [4 /*yield*/, this.queueScan()]; case 6: _a = (_c.sent()); _c.label = 7; case 7: _a; return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }); }; AMSFileScanner.prototype.retrieveFileScanResult = function (id) { var _a; return (_a = this.scanResults) === null || _a === void 0 ? void 0 : _a.get(id); }; AMSFileScanner.prototype.addOrUpdateFile = function (id, fileMetadata, scan) { var _a; var scanResult = this.retrieveFileScanResult(id); (_a = this.scanResults) === null || _a === void 0 ? void 0 : _a.set(id, __assign(__assign({}, (scanResult || {})), { fileMetadata: fileMetadata, scan: scan })); }; AMSFileScanner.prototype.addNext = function (id, next) { var _a, _b; var scanResult = (_a = this.scanResults) === null || _a === void 0 ? void 0 : _a.get(id); if (scanResult) { (_b = this.scanResults) === null || _b === void 0 ? void 0 : _b.set(id, __assign(__assign({}, scanResult), { next: next })); } }; AMSFileScanner.prototype.addActivity = function (id, activity) { var _a, _b; var scanResult = (_a = this.scanResults) === null || _a === void 0 ? void 0 : _a.get(id); if (scanResult) { (_b = this.scanResults) === null || _b === void 0 ? void 0 : _b.set(id, __assign(__assign({}, scanResult), { activity: activity })); } }; AMSFileScanner.prototype.scanFileCallback = function (scanResult, id) { return __awaiter(this, void 0, void 0, function () { var fileMetadata, next, activity, scan, response, view_location, scan_1, e_2; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: fileMetadata = scanResult.fileMetadata, next = scanResult.next, activity = scanResult.activity, scan = scanResult.scan; if (!((scan === null || scan === void 0 ? void 0 : scan.status) === AMSFileManager_1.AMSViewScanStatus.INPROGRESS)) return [3 /*break*/, 10]; _b.label = 1; case 1: _b.trys.push([1, 7, , 8]); return [4 /*yield*/, this.amsClient.getViewStatus(fileMetadata)]; case 2: response = _b.sent(); view_location = response.view_location, scan_1 = response.scan; this.addOrUpdateFile(id, scanResult.fileMetadata, scan_1); if (!(scan_1.status === AMSFileManager_1.AMSViewScanStatus.PASSED && next && activity)) return [3 /*break*/, 4]; return [4 /*yield*/, this.renderAttachmentActivity(scanResult, view_location)]; case 3: _b.sent(); _b.label = 4; case 4: if (!(scan_1.status === AMSFileManager_1.AMSViewScanStatus.MALWARE && next && activity)) return [3 /*break*/, 6]; return [4 /*yield*/, this.renderMalwareActivity(scanResult)]; case 5: _b.sent(); _b.label = 6; case 6: return [3 /*break*/, 8]; case 7: e_2 = _b.sent(); console.error(e_2); return [3 /*break*/, 8]; case 8: return [4 /*yield*/, WebUtils_1.default.sleep(((_a = this.options) === null || _a === void 0 ? void 0 : _a.scanStatusRetrievalDelay) || defaultScanStatusRetrievalDelay)]; case 9: _b.sent(); _b.label = 10; case 10: return [2 /*return*/]; } }); }); }; AMSFileScanner.prototype.scanFiles = function () { var _this = this; // eslint-disable-next-line no-async-promise-executor return new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { var _this = this; var _a; return __generator(this, function (_b) { (_a = this.scanResults) === null || _a === void 0 ? void 0 : _a.forEach(function (scanResult, id) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.scanFileCallback(scanResult, id)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); resolve(); return [2 /*return*/]; }); }); }); }; AMSFileScanner.prototype.end = function () { this.shouldQueueScan = false; }; AMSFileScanner.prototype.retrieveFileBlob = function (fileMetadata, view_location) { return __awaiter(this, void 0, void 0, function () { var blob, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.amsClient.getView(fileMetadata, view_location)]; case 1: blob = _a.sent(); // eslint-disable-line @typescript-eslint/no-explicit-any return [3 /*break*/, 3]; case 2: error_1 = _a.sent(); console.error(error_1); return [3 /*break*/, 3]; case 3: return [2 /*return*/, blob]; } }); }); }; AMSFileScanner.prototype.addAttachmentToActivity = function (activity, file) { return __awaiter(this, void 0, void 0, function () { var attachmentData, attachmentSizes, index; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, activityUtils_1.default.getAttachments([file])]; case 1: attachmentData = _a.sent(); return [4 /*yield*/, activityUtils_1.default.getAttachmentSizes([file])]; case 2: attachmentSizes = _a.sent(); index = activity.attachments.findIndex(function (attachment) { return (attachment.name === file.name); }); activity.attachments[index] = attachmentData[0]; activity.channelData.attachmentSizes[index] = attachmentSizes[0]; return [2 /*return*/]; } }); }); }; AMSFileScanner.prototype.renderAttachmentActivity = function (scanResult, view_location) { return __awaiter(this, void 0, void 0, function () { var fileMetadata, next, activity, blob, file, index, hasMultipleAttachments, amsreferences, fileIds; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: fileMetadata = scanResult.fileMetadata, next = scanResult.next, activity = scanResult.activity; return [4 /*yield*/, this.retrieveFileBlob(fileMetadata, view_location)]; case 1: blob = _a.sent(); file = new File([blob], fileMetadata.name, { type: fileMetadata.type }); return [4 /*yield*/, this.addAttachmentToActivity(activity, file)]; case 2: _a.sent(); index = activity.attachments.findIndex(function (attachment) { return (attachment.name === file.name); }); activity.channelData.fileScan[index] = { status: AMSFileManager_1.AMSViewScanStatus.PASSED }; hasMultipleAttachments = index > 0; if (hasMultipleAttachments) { amsreferences = activity.channelData.metadata.amsreferences; fileIds = JSON.parse(amsreferences); fileIds.forEach(function (fileId) { _this.addActivity(fileId, activity); }); } next(activity); // Send updated activity to webchat return [2 /*return*/]; } }); }); }; AMSFileScanner.prototype.renderMalwareActivity = function (scanResult) { return __awaiter(this, void 0, void 0, function () { var fileMetadata, next, activity, index; return __generator(this, function (_a) { fileMetadata = scanResult.fileMetadata, next = scanResult.next, activity = scanResult.activity; index = activity.attachments.findIndex(function (attachment) { return (attachment.name === fileMetadata.name); }); activity.channelData.fileScan[index] = { status: AMSFileManager_1.AMSViewScanStatus.MALWARE }; next(activity); return [2 /*return*/]; }); }); }; return AMSFileScanner; }()); exports.default = AMSFileScanner; //# sourceMappingURL=AMSFileScanner.js.map