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

microsoft / botbuilder-js / 4792076511

pending completion
4792076511

Pull #4461

github

GitHub
Merge 33c92d5e3 into f9cf5fd25
Pull Request #4461: fix: [#4452][#4456][#4460][botframework-streaming] Should reject pending requests on disconnection

9716 of 12704 branches covered (76.48%)

Branch coverage included in aggregate %.

8 of 8 new or added lines in 4 files covered. (100.0%)

20035 of 22409 relevant lines covered (89.41%)

3733.49 hits per line

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

84.09
/libraries/botframework-streaming/src/namedPipe/namedPipeClient.ts
1
/**
2
 * @module botframework-streaming
3
 */
4
/**
5
 * Copyright (c) Microsoft Corporation. All rights reserved.
6
 * Licensed under the MIT License.
7
 */
8
import { connect } from 'net';
1✔
9
import { ProtocolAdapter } from '../protocolAdapter';
1✔
10
import { RequestHandler } from '../requestHandler';
11
import { StreamingRequest } from '../streamingRequest';
12
import { RequestManager } from '../payloads';
1✔
13
import { PayloadReceiver, PayloadSender } from '../payloadTransport';
1✔
14
import { NamedPipeTransport } from './namedPipeTransport';
1✔
15
import { IStreamingTransportClient, IReceiveResponse } from '../interfaces';
16

17
/**
18
 * Streaming transport client implementation that uses named pipes for inter-process communication.
19
 */
20
export class NamedPipeClient implements IStreamingTransportClient {
1✔
21
    private readonly _baseName: string;
22
    private readonly _requestHandler: RequestHandler;
23
    private readonly _sender: PayloadSender;
24
    private readonly _receiver: PayloadReceiver;
25
    private readonly _requestManager: RequestManager;
26
    private readonly _protocolAdapter: ProtocolAdapter;
27
    private readonly _autoReconnect: boolean;
28
    private _isDisconnecting: boolean;
29

30
    /**
31
     * Creates a new instance of the [NamedPipeClient](xref:botframework-streaming.NamedPipeClient) class.
32
     *
33
     * @param baseName The named pipe to connect to.
34
     * @param requestHandler Optional [RequestHandler](xref:botframework-streaming.RequestHandler) to process incoming messages received by this client.
35
     * @param autoReconnect Optional setting to determine if the client sould attempt to reconnect automatically on disconnection events. Defaults to true.
36
     */
37
    constructor(baseName: string, requestHandler?: RequestHandler, autoReconnect = true) {
×
38
        this._baseName = baseName;
1✔
39
        this._requestHandler = requestHandler;
1✔
40
        this._autoReconnect = autoReconnect;
1✔
41
        this._requestManager = new RequestManager();
1✔
42
        this._sender = new PayloadSender();
1✔
43
        this._sender.disconnected = this.onConnectionDisconnected.bind(this);
1✔
44
        this._receiver = new PayloadReceiver();
1✔
45
        this._receiver.disconnected = this.onConnectionDisconnected.bind(this);
1✔
46
        this._protocolAdapter = new ProtocolAdapter(
1✔
47
            this._requestHandler,
48
            this._requestManager,
49
            this._sender,
50
            this._receiver
51
        );
52
    }
53

54
    /**
55
     * Establish a connection with no custom headers.
56
     */
57
    async connect(): Promise<void> {
58
        const outgoingPipeName: string =
59
            NamedPipeTransport.PipePath + this._baseName + NamedPipeTransport.ServerIncomingPath;
1✔
60
        const outgoing = connect(outgoingPipeName);
1✔
61
        const incomingPipeName: string =
62
            NamedPipeTransport.PipePath + this._baseName + NamedPipeTransport.ServerOutgoingPath;
1✔
63
        const incoming = connect(incomingPipeName);
1✔
64
        this._sender.connect(new NamedPipeTransport(outgoing));
1✔
65
        this._receiver.connect(new NamedPipeTransport(incoming));
1✔
66
    }
67

68
    /**
69
     * Disconnect the client.
70
     */
71
    disconnect(): void {
72
        this._sender.disconnect();
3✔
73
        this._receiver.disconnect();
3✔
74
    }
75

76
    /**
77
     * Task used to send data over this client connection.
78
     *
79
     * @param request The [StreamingRequest](xref:botframework-streaming.StreamingRequest) to send.
80
     * @returns A promise for an instance of [IReceiveResponse](xref:botframework-streaming.IReceiveResponse) on completion of the send operation.
81
     */
82
    async send(request: StreamingRequest): Promise<IReceiveResponse> {
83
        return this._protocolAdapter.sendRequest(request);
×
84
    }
85

86
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
    private onConnectionDisconnected(sender: Record<string, unknown>, args: any): void {
88
        if (!this._isDisconnecting) {
3✔
89
            this._isDisconnecting = true;
1✔
90
            try {
1✔
91
                if (this._sender.isConnected) {
1!
92
                    this._sender.disconnect();
1✔
93
                }
94

95
                if (this._receiver.isConnected) {
1!
96
                    this._receiver.disconnect();
1✔
97
                }
98

99
                if (this._autoReconnect) {
1!
100
                    this.connect()
×
101
                        .then((): void => {})
102
                        .catch((error): void => {
103
                            throw new Error(
×
104
                                `Failed to reconnect. Reason: ${error.message} Sender: ${sender} Args: ${args}. `
105
                            );
106
                        });
107
                }
108
            } finally {
109
                this._isDisconnecting = false;
1✔
110
            }
111
        }
112
    }
113
}
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

© 2025 Coveralls, Inc