@jsonjoy.com/reactive-rpc
Version:
Reactive-RPC is a library for building reactive APIs over WebSocket, HTTP, and other RPCs.
68 lines • 2.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Http2Server = exports.Http1EndpointMatch = void 0;
const tslib_1 = require("tslib");
const http2 = tslib_1.__importStar(require("http2"));
const Writer_1 = require("@jsonjoy.com/util/lib/buffers/Writer");
const Codecs_1 = require("@jsonjoy.com/json-pack/lib/codecs/Codecs");
const jit_router_1 = require("@jsonjoy.com/jit-router");
const printTree_1 = require("sonic-forest/lib/print/printTree");
const RpcCodecs_1 = require("../../common/codec/RpcCodecs");
const RpcMessageCodecs_1 = require("../../common/codec/RpcMessageCodecs");
class Http1EndpointMatch {
constructor(handler) {
this.handler = handler;
}
}
exports.Http1EndpointMatch = Http1EndpointMatch;
class Http2Server {
static start(opts = {}, port = 8000) {
const rawServer = http2.createSecureServer({
allowHTTP1: true,
});
rawServer.listen(port);
const server = new Http2Server({ server: rawServer });
return server;
}
constructor(opts) {
this.opts = opts;
this.onnotfound = () => {
};
this.oninternalerror = () => {
};
this.httpRouter = new jit_router_1.Router();
this.httpMatcher = () => undefined;
this.onStream = async (stream) => {
stream.end('Hello, World!');
};
this.server = opts.server;
const writer = opts.writer ?? new Writer_1.Writer();
this.codecs = opts.codecs ?? new RpcCodecs_1.RpcCodecs(opts.codecs ?? new Codecs_1.Codecs(writer), new RpcMessageCodecs_1.RpcMessageCodecs());
}
start() {
const server = this.server;
this.httpMatcher = this.httpRouter.compile();
server.on('stream', this.onStream);
server.on('sessionError', (error, session) => {
session.close();
});
}
route(def) {
let path = def.path;
if (path[0] !== '/')
path = '/' + path;
const method = def.method ? def.method.toUpperCase() : 'GET';
const route = method + path;
Number(route);
const match = new Http1EndpointMatch(def.handler);
this.httpRouter.add(route, match);
}
toString(tab = '') {
return (`${this.constructor.name}` +
(0, printTree_1.printTree)(tab, [
(tab) => `HTTP ${this.httpRouter.toString(tab)}`,
]));
}
}
exports.Http2Server = Http2Server;
//# sourceMappingURL=Http2Server.js.map
;