mudb
Version:
Real-time database for multiplayer games
56 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class MuRPCLocalClient {
constructor(auth, _handlers) {
this.auth = auth;
this._handlers = _handlers;
}
setAuth(auth) {
this.auth = auth;
}
async send(schemas, args) {
const handler = this._handlers[schemas.protocol.name];
if (!handler) {
throw new Error('server not registered');
}
const json = await handler(this, schemas.argSchema.toJSON(args));
return schemas.responseSchema.fromJSON(json);
}
}
exports.MuRPCLocalClient = MuRPCLocalClient;
class MuRPCLocalTransport {
constructor() {
this._handlers = {};
}
client(auth) {
return new MuRPCLocalClient(auth, this._handlers);
}
listen(schemas, auth, recv) {
this._handlers[schemas.protocol.name] = async (client, json) => {
const response = schemas.responseSchema.alloc();
try {
if (!await auth(client)) {
throw new Error('unauthorized access');
}
const parsed = schemas.argSchema.fromJSON(json);
await recv(client, parsed, response);
schemas.argSchema.free(parsed);
}
catch (e) {
response.type = 'error';
if (e instanceof Error && e.stack) {
response.data = e.stack;
}
else {
response.data = '' + e;
}
}
const result = schemas.responseSchema.toJSON(response);
schemas.responseSchema.free(response);
return result;
};
return {};
}
}
exports.MuRPCLocalTransport = MuRPCLocalTransport;
//# sourceMappingURL=index.js.map