"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshService = void 0;
const vscode_jsonrpc_1 = require("vscode-jsonrpc");
const sshSession_1 = require("../sshSession");
/**
 * Base class for SSH session services that handle incoming requests.
 *
 * Services can be on either the server side or the client side, because either side may
 * send requests to the other's services.
 *
 * Service subclasses must have one or more `serviceActivation` decorators applied to them to
 * declare the type(s) of requests that cause the service to be activated. Only one instance
 * of each service type gets activated for a session, even if there are multiple activation
 * rules. After activation, a service remains active for the duration of the session,
 * handling any additional requests, until it is disposed when the session is disposed.
 *
 * To enable activation of a service, add the service type to
 * `SshSessionConfiguration.services`. When a service is activated, the session raises a
 * `SshSession.serviceActivated` event.
 */
class SshService {
    constructor(session) {
        this.session = session;
        this.disposed = false;
        this.disposedEmitter = new vscode_jsonrpc_1.Emitter();
        this.onDisposed = this.disposedEmitter.event;
        if (!(session instanceof sshSession_1.SshSession)) {
            // Other packages provide services that inherit from SshService. When they do, they
            // reference the SshSession type from a specific version of this package. But at runtime,
            // the service may be activated with a session from a different version of this package,
            // if there are multiple instances due to version mismatches. Multiple instances of SSH
            // types causes problems, so it's best to detect the error at initialization time.
            throw new TypeError('Session is not an instance of SshSession. ' +
                '(This may be due to a version mismatch between SSH packages.)');
        }
    }
    get trace() {
        return this.session.trace;
    }
    raiseReportProgress(progress) {
        this.session.raiseReportProgress(progress);
    }
    dispose() {
        if (this.disposed)
            return;
        this.disposed = true;
        this.disposedEmitter.fire();
    }
    /**
     * Services that are activated via session requests must override this method to handle
     * incoming session requests.
     *
     * Implementations must set `SshRequestEventArgs.isAuthorized` or
     * `SshRequestEventArgs.responsePromise` to indicate whether the request was allowed.
     */
    async onSessionRequest(request, cancellation) {
        this.session.raiseSessionRequest(request);
    }
    /**
     * Services that are activated via channel types must override this method to handle
     * incoming requests to open a channel.
     *
     * Implementations may set `SshChannelOpeningEventArgs.failureReason` or
     * `SshChannelOpeningEventArgs.openingPromise` to block opening of the channel.
     * The default behavior allows the channel to open.
     *
     * Requests on the opened channel will not be directed to `onChannelRequest`
     * unless the service also declares activation on specific channel request(s). Otherwise,
     * an implementation of this method may add any event-handlers to the
     * `SshChannelOpeningEventArgs.channel` including a request event handler.
     */
    async onChannelOpening(request, cancellation) {
        if (!request)
            throw new TypeError('Request is required.');
        return this.session.handleChannelOpening(request, cancellation, false);
    }
    /**
     * Services that are activated via channel requests must override this method to handle
     * incoming channel requests.
     *
     * Implementations must set `SshRequestEventArgs.isAuthorized` or
     * `SshRequestEventArgs.responsePromise` to indicate whether the request was allowed.
     */
    async onChannelRequest(channel, request, cancellation) { }
    /**
     * Sends any message.
     */
    async sendMessage(message, cancellation) {
        await this.session.sendMessage(message, cancellation);
    }
}
exports.SshService = SshService;
//# sourceMappingURL=https://main.vscode-cdn.net/sourcemaps/6a44c352bd24569c417e530095901b649960f9f8/node_modules/@microsoft/dev-tunnels-ssh/services/sshService.js.map