UNPKG

botframework-streaming

Version:

Streaming library for the Microsoft Bot Framework

92 lines 4.74 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProtocolAdapter = void 0; const payloadAssemblerManager_1 = require("./payloads/payloadAssemblerManager"); const sendOperations_1 = require("./payloads/sendOperations"); const streamManager_1 = require("./payloads/streamManager"); const protocol_base_1 = require("./utilities/protocol-base"); /** * Creates a protocol adapter for Streaming. */ class ProtocolAdapter { /** * Creates a new instance of the protocol adapter class. * * @param requestHandler The [RequestHandler](xref:botframework-streaming.RequestHandler) that will process incoming requests. * @param requestManager The [RequestManager](xref:botframework-streaming.RequestManager) that will process outgoing requests. * @param sender The [PayloadSender](xref:botframework-streaming.PayloadSender) for use with outgoing requests. * @param receiver The [PayloadReceiver](xref:botframework-streaming.PayloadReceiver) for use with incoming requests. */ constructor(requestHandler, requestManager, sender, receiver) { this.requestHandler = requestHandler; this.requestManager = requestManager; this.payloadSender = sender; this.payloadReceiver = receiver; this.sendOperations = new sendOperations_1.SendOperations(this.payloadSender); this.streamManager = new streamManager_1.StreamManager(this.onCancelStream); this.assemblerManager = new payloadAssemblerManager_1.PayloadAssemblerManager(this.streamManager, (id, response) => this.onReceiveResponse(id, response), (id, request) => this.onReceiveRequest(id, request)); this.payloadReceiver.subscribe((header) => this.assemblerManager.getPayloadStream(header), (header, contentStream, contentLength) => this.assemblerManager.onReceive(header, contentStream, contentLength)); } /** * Sends a request over the attached request manager. * * @param request The outgoing request to send. * @returns The response to the specified request. */ sendRequest(request) { return __awaiter(this, void 0, void 0, function* () { const requestId = (0, protocol_base_1.generateGuid)(); // Register the request in the request manager before sending it to the server. // Otherwise, if the server respond quickly, it may miss the request. const getResponsePromise = this.requestManager.getResponse(requestId); yield this.sendOperations.sendRequest(requestId, request); return getResponsePromise; }); } /** * Executes the receive pipeline when a request comes in. * * @param id The id the resources created for the response will be assigned. * @param request The incoming request to process. */ onReceiveRequest(id, request) { return __awaiter(this, void 0, void 0, function* () { if (this.requestHandler) { const response = yield this.requestHandler.processRequest(request); if (response) { yield this.sendOperations.sendResponse(id, response); } } }); } /** * Executes the receive pipeline when a response comes in. * * @param id The id the resources created for the response will be assigned. * @param response The incoming response to process. */ onReceiveResponse(id, response) { return __awaiter(this, void 0, void 0, function* () { yield this.requestManager.signalResponse(id, response); }); } /** * Executes the receive pipeline when a cancellation comes in. * * @param contentStreamAssembler The payload assembler processing the incoming data that this cancellation request targets. */ onCancelStream(contentStreamAssembler) { this.sendOperations.sendCancelStream(contentStreamAssembler.id).catch(); } } exports.ProtocolAdapter = ProtocolAdapter; //# sourceMappingURL=protocolAdapter.js.map