UNPKG

botbuilder

Version:

Bot Builder is a framework for building rich bots on virtually any platform.

71 lines 3.11 kB
"use strict"; /** * @module botbuilder */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ 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.StreamingHttpClient = void 0; const botframework_streaming_1 = require("botframework-streaming"); /** * An implementation of `HttpClient` that adds compatibility with streaming connections. */ class StreamingHttpClient { /** * Creates a new streaming Http client. * * @param server Transport server implementation to be used. */ constructor(server) { if (!server) { throw new Error('StreamingHttpClient: Expected server.'); } this.server = server; } /** * This function hides the default sendRequest of the HttpClient, replacing it * with a version that takes the WebResource created by the BotFrameworkAdapter * and converting it to a form that can be sent over a streaming transport. * * @param httpRequest The outgoing request created by the BotframeworkAdapter. * @returns The streaming transport compatible response to send back to the client. */ sendRequest(httpRequest) { return __awaiter(this, void 0, void 0, function* () { if (!httpRequest) { throw new Error('StreamingHttpClient.sendRequest(): missing "httpRequest" parameter'); } if (!this.server.isConnected) { throw new Error('StreamingHttpClient.sendRequest(): Streaming connection is disconnected, and the request could not be sent.'); } const request = this.mapHttpRequestToProtocolRequest(httpRequest); request.path = request.path.substring(request.path.indexOf('/v3')); const res = yield this.server.send(request); return { request: httpRequest, status: res.statusCode, headers: httpRequest.headers, readableStreamBody: res.streams.length > 0 ? res.streams[0].getStream() : undefined, }; }); } /** * @private */ mapHttpRequestToProtocolRequest(httpRequest) { return botframework_streaming_1.StreamingRequest.create(httpRequest.method, httpRequest.url, httpRequest.body); } } exports.StreamingHttpClient = StreamingHttpClient; //# sourceMappingURL=streamingHttpClient.js.map