@litert/televoke
Version:
A simple RPC service framework.
65 lines • 2.61 kB
JavaScript
"use strict";
/**
* Copyright 2025 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TvServer = void 0;
const Shared = require("../shared");
const node_events_1 = require("node:events");
const ServerChannel_1 = require("./ServerChannel");
const LegacyServerChannel_impl_1 = require("./LegacyServerChannel.impl");
let nextChId = Date.now();
class TvServer extends node_events_1.EventEmitter {
constructor(router, _streamManagerFactory = Shared.createSharedStreamManagerFactory()) {
super();
this.router = router;
this._streamManagerFactory = _streamManagerFactory;
this._channels = {};
this.timeout = 60000;
this._onRouterError = (e) => {
this.emit('error', e);
};
this.router.on('error', this._onRouterError);
}
setRouter(router) {
this.router?.off('error', this._onRouterError);
this.router = router
.on('error', this._onRouterError);
}
registerChannel(transporter) {
const id = nextChId++;
this._channels[id] = new ServerChannel_1.TvServerChannelV2(id, transporter, this.timeout, this._streamManagerFactory);
this._channels[id]
.on('api_call', (cb, name, body) => {
this.router.processApi(cb, name, body, {
channel: this._channels[id],
});
})
.on('error', (err) => this.emit('error', err))
.on('warning', (err) => this.emit('warning', err, this._channels[id]))
.on('close', () => { delete this._channels[id]; });
}
processLegacyApi(callback, name, args, transporter) {
this.router.processLegacyApi(callback, name, args, { 'channel': new LegacyServerChannel_impl_1.TvLegacyServerChannelV1(transporter) });
}
getChannel(id) {
return this._channels[id] ?? null;
}
getChannelIdList() {
return Object.keys(this._channels);
}
}
exports.TvServer = TvServer;
//# sourceMappingURL=Server.impl.js.map