122 lines
No EOL
4.7 KiB
JavaScript
122 lines
No EOL
4.7 KiB
JavaScript
"use strict";
|
|
/**
|
|
* @module botframework-streaming
|
|
*/
|
|
/**
|
|
* 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.ContentStream = void 0;
|
|
/**
|
|
* A stream of fixed or infinite length containing content to be decoded.
|
|
*/
|
|
class ContentStream {
|
|
/**
|
|
* Initializes a new instance of the [ContentStream](xref:botframework-streaming.ContentStream) class.
|
|
*
|
|
* @param id The ID assigned to this instance.
|
|
* @param assembler The [PayloadAssembler](xref:botframework-streaming.PayloadAssembler) assigned to this instance.
|
|
*/
|
|
constructor(id, assembler) {
|
|
if (!assembler) {
|
|
throw Error('Null Argument Exception');
|
|
}
|
|
this.id = id;
|
|
this.assembler = assembler;
|
|
}
|
|
/**
|
|
* Gets the name of the type of the object contained within this [ContentStream](xref:botframework-streaming.ContentStream).
|
|
*
|
|
* @returns The [PayloadType](xref:botframework-streaming.PayloadType) of this [ContentStream](xref:botframework-streaming.ContentStream).
|
|
*/
|
|
get contentType() {
|
|
return this.assembler.payloadType;
|
|
}
|
|
/**
|
|
* Gets the length of this [ContentStream](xref:botframework-streaming.ContentStream).
|
|
*
|
|
* @returns A number representing the length of this [ContentStream](xref:botframework-streaming.ContentStream).
|
|
*/
|
|
get length() {
|
|
return this.assembler.contentLength;
|
|
}
|
|
/**
|
|
* Gets the data contained within this [ContentStream](xref:botframework-streaming.ContentStream).
|
|
*
|
|
* @returns This [ContentStream's](xref:botframework-streaming.ContentStream) [SubscribableStream](xref:botframework-streaming.SubscribableStream).
|
|
*/
|
|
getStream() {
|
|
if (!this.stream) {
|
|
this.stream = this.assembler.getPayloadStream();
|
|
}
|
|
return this.stream;
|
|
}
|
|
/**
|
|
* Closes the assembler.
|
|
*/
|
|
cancel() {
|
|
this.assembler.close();
|
|
}
|
|
/**
|
|
* Gets the [SubscribableStream](xref:botframework-streaming.SubscribableStream) content as a string.
|
|
*
|
|
* @returns A string Promise with [SubscribableStream](xref:botframework-streaming.SubscribableStream) content.
|
|
*/
|
|
readAsString() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const { bufferArray } = yield this.readAll();
|
|
return (bufferArray || []).map((result) => result.toString('utf8')).join('');
|
|
});
|
|
}
|
|
/**
|
|
* Gets the [SubscribableStream](xref:botframework-streaming.SubscribableStream) content as a typed JSON object.
|
|
*
|
|
* @returns A typed object Promise with `SubscribableStream` content.
|
|
*/
|
|
readAsJson() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const stringToParse = yield this.readAsString();
|
|
return JSON.parse(stringToParse);
|
|
});
|
|
}
|
|
readAll() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
// do a read-all
|
|
const allData = [];
|
|
let count = 0;
|
|
const stream = this.getStream();
|
|
// populate the array with any existing buffers
|
|
while (count < stream.length) {
|
|
const chunk = stream.read(stream.length);
|
|
allData.push(chunk);
|
|
count += chunk.length;
|
|
}
|
|
if (count < this.length) {
|
|
const readToEnd = new Promise((resolve) => {
|
|
const callback = (cs) => (chunk) => {
|
|
allData.push(chunk);
|
|
count += chunk.length;
|
|
if (count === cs.length) {
|
|
resolve(true);
|
|
}
|
|
};
|
|
stream.subscribe(callback(this));
|
|
});
|
|
yield readToEnd;
|
|
}
|
|
return { bufferArray: allData, size: count };
|
|
});
|
|
}
|
|
}
|
|
exports.ContentStream = ContentStream;
|
|
//# sourceMappingURL=contentStream.js.map
|