• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / botbuilder-js / 11579379955

29 Oct 2024 05:34PM UTC coverage: 84.703% (-0.5%) from 85.23%
11579379955

push

github

web-flow
refactor: [#4684] Replace browserify with tsup (#4774)

* Replace browserify with tsup in adaptive-expressions

* Remove remaining browserify packages

* Fix streaming tests

* Fix yarn.lock

* fix depcheck

8186 of 10820 branches covered (75.66%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

23 existing lines in 13 files now uncovered.

20514 of 23063 relevant lines covered (88.95%)

7296.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.51
/libraries/botframework-streaming/src/contentStream.ts
1
/**
6✔
2
 * @module botframework-streaming
3
 */
4
/**
5
 * Copyright (c) Microsoft Corporation. All rights reserved.
6
 * Licensed under the MIT License.
7
 */
8

9
import { INodeBuffer } from './interfaces/INodeBuffer';
10
import type { PayloadAssembler } from './assemblers';
11
import { PayloadTypes } from './payloads';
12
import type { SubscribableStream } from './subscribableStream';
13

14
/**
15
 * A stream of fixed or infinite length containing content to be decoded.
16
 */
17
export class ContentStream {
2✔
18
    id: string;
19
    private readonly assembler: PayloadAssembler;
20
    private stream: SubscribableStream;
21

22
    /**
23
     * Initializes a new instance of the [ContentStream](xref:botframework-streaming.ContentStream) class.
24
     *
25
     * @param id The ID assigned to this instance.
26
     * @param assembler The [PayloadAssembler](xref:botframework-streaming.PayloadAssembler) assigned to this instance.
27
     */
28
    constructor(id: string, assembler: PayloadAssembler) {
29
        if (!assembler) {
50!
30
            throw Error('Null Argument Exception');
2✔
31
        }
32
        this.id = id;
48✔
33
        this.assembler = assembler;
48✔
34
    }
35

36
    /**
37
     * Gets the name of the type of the object contained within this [ContentStream](xref:botframework-streaming.ContentStream).
38
     *
39
     * @returns The [PayloadType](xref:botframework-streaming.PayloadType) of this [ContentStream](xref:botframework-streaming.ContentStream).
40
     */
41
    get contentType(): string | PayloadTypes {
42
        return this.assembler.payloadType;
2✔
43
    }
44

45
    /**
46
     * Gets the length of this [ContentStream](xref:botframework-streaming.ContentStream).
47
     *
48
     * @returns A number representing the length of this [ContentStream](xref:botframework-streaming.ContentStream).
49
     */
50
    get length(): number {
51
        return this.assembler.contentLength;
40✔
52
    }
53

54
    /**
55
     * Gets the data contained within this [ContentStream](xref:botframework-streaming.ContentStream).
56
     *
57
     * @returns This [ContentStream's](xref:botframework-streaming.ContentStream) [SubscribableStream](xref:botframework-streaming.SubscribableStream).
58
     */
59
    getStream(): SubscribableStream {
60
        if (!this.stream) {
28!
61
            this.stream = this.assembler.getPayloadStream();
28✔
62
        }
63

64
        return this.stream;
28✔
65
    }
66

67
    /**
68
     * Closes the assembler.
69
     */
70
    cancel(): void {
71
        this.assembler.close();
2✔
72
    }
73

74
    /**
75
     * Gets the [SubscribableStream](xref:botframework-streaming.SubscribableStream) content as a string.
76
     *
77
     * @returns A string Promise with [SubscribableStream](xref:botframework-streaming.SubscribableStream) content.
78
     */
UNCOV
79
    async readAsString(): Promise<string> {
×
80
        const { bufferArray } = await this.readAll();
26✔
81
        return (bufferArray || []).map((result) => result.toString('utf8')).join('');
22!
82
    }
83

84
    /**
85
     * Gets the [SubscribableStream](xref:botframework-streaming.SubscribableStream) content as a typed JSON object.
86
     *
87
     * @returns A typed object Promise with `SubscribableStream` content.
88
     */
UNCOV
89
    async readAsJson<T>(): Promise<T> {
×
90
        const stringToParse = await this.readAsString();
4✔
91
        return <T>JSON.parse(stringToParse);
4✔
92
    }
93

94
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
UNCOV
95
    private async readAll(): Promise<Record<string, any>> {
×
96
        // do a read-all
97
        const allData: INodeBuffer[] = [];
26✔
98
        let count = 0;
26✔
99
        const stream = this.getStream();
26✔
100

101
        // populate the array with any existing buffers
102
        while (count < stream.length) {
26✔
103
            const chunk = stream.read(stream.length);
8✔
104
            allData.push(chunk);
8✔
105
            count += (chunk as INodeBuffer).length;
8✔
106
        }
107

108
        if (count < this.length) {
26!
109
            const readToEnd = new Promise<boolean>((resolve): void => {
16✔
110
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
                const callback = (cs: ContentStream) => (chunk: any): void => {
16✔
112
                    allData.push(chunk);
12✔
113
                    count += (chunk as INodeBuffer).length;
12✔
114
                    if (count === cs.length) {
12!
115
                        resolve(true);
12✔
116
                    }
117
                };
118

119
                stream.subscribe(callback(this));
16✔
120
            });
121

122
            await readToEnd;
16✔
123
        }
124

125
        return { bufferArray: allData, size: count };
22✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc