"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamForwarder = void 0;
const dev_tunnels_ssh_1 = require("@microsoft/dev-tunnels-ssh");
const net_1 = require("net");
class StreamForwarder {
    /* @internal */
    constructor(localStream, remoteStream, trace) {
        this.localStream = localStream;
        this.remoteStream = remoteStream;
        this.trace = trace;
        this.disposed = false;
        if (!localStream)
            throw new TypeError('Local stream is required.');
        if (!remoteStream)
            throw new TypeError('Remote stream is required.');
        localStream.pipe(remoteStream);
        remoteStream.pipe(localStream);
    }
    close(abort, errorMessage) {
        try {
            if (abort && this.localStream instanceof net_1.Socket) {
                this.localStream.destroy();
            }
            else {
                this.localStream.end();
            }
            if (this.remoteStream instanceof dev_tunnels_ssh_1.SshStream) {
                this.remoteStream.channel.close('SIGABRT', errorMessage).catch((e) => { });
            }
            else {
                this.remoteStream.end();
            }
            this.trace(dev_tunnels_ssh_1.TraceLevel.Verbose, dev_tunnels_ssh_1.SshTraceEventIds.portForwardChannelClosed, `Stream forwarder ${abort ? 'aborted' : 'closed'} connection.`);
        }
        catch (e) {
            if (!(e instanceof Error))
                throw e;
            this.trace(dev_tunnels_ssh_1.TraceLevel.Warning, dev_tunnels_ssh_1.SshTraceEventIds.unknownError, `Stream forwarder unexpected error closing connection:  ${e.message}`);
        }
    }
    dispose() {
        if (!this.disposed) {
            this.disposed = true;
            this.close(true);
        }
    }
}
exports.StreamForwarder = StreamForwarder;
//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-4c0b0c6cc561d2d3636d1ec250935431876ce4dc/node_modules-@microsoft-dev-tunnels-ssh-tcp-services-streamForwarder.js.map