mudb
Version:
Real-time database for multiplayer games
85 lines • 2.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tape = require("tape");
const local_1 = require("../local");
const schema_1 = require("../../schema");
const server_1 = require("../server");
const client_1 = require("../client");
tape('basic rpc', async (t) => {
const protocol = {
name: 'test rpc',
api: {
hello: {
arg: new schema_1.MuUTF8(),
ret: new schema_1.MuUTF8(),
},
fib: {
arg: new schema_1.MuStruct({
a: new schema_1.MuVarint(),
b: new schema_1.MuVarint(),
}),
ret: new schema_1.MuStruct({
a: new schema_1.MuVarint(),
b: new schema_1.MuVarint(),
}),
},
brokenRoute: {
arg: new schema_1.MuVoid(),
ret: new schema_1.MuVoid(),
},
logout: {
arg: new schema_1.MuVoid(),
ret: new schema_1.MuVoid(),
},
},
};
const transport = new local_1.MuRPCLocalTransport();
const server = new server_1.MuRPCServer({
protocol,
transport,
authorize: async ({ auth }) => auth !== 'bad guy',
handlers: {
hello: async ({ auth }, arg) => {
if (auth === 'admin') {
return 'administrator';
}
return 'hello ' + arg;
},
fib: async (conn, { a, b }, ret) => {
ret.a = a + b;
ret.b = a;
return ret;
},
brokenRoute: async () => {
throw new Error('not implemented');
},
logout: async (conn) => {
conn.setAuth('');
},
},
});
const client = new client_1.MuRPCClient(protocol, transport.client('user'));
t.equals(await client.api.hello('world'), 'hello world', 'hello world');
t.same(await client.api.fib({ a: 2, b: 1 }), { a: 3, b: 2 }, 'fibonacci');
try {
await client.api.brokenRoute();
t.fail('should throw');
}
catch (e) {
t.pass(`throws ${e}`);
}
const badClient = new client_1.MuRPCClient(protocol, transport.client('bad guy'));
try {
await badClient.api.hello('i am a jerk');
t.fail('should throw');
}
catch (e) {
t.pass('bad api throws when called');
}
const adminClient = new client_1.MuRPCClient(protocol, transport.client('admin'));
t.equals(await adminClient.api.hello('guest'), 'administrator', 'admin auth ok');
await adminClient.api.logout();
t.equals(await adminClient.api.hello('guest'), 'hello guest', 'log out ok');
t.end();
});
//# sourceMappingURL=basic.js.map