UNPKG

mudb

Version:

Real-time database for multiplayer games

87 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const http = require("http"); const tape = require("tape"); const server_1 = require("../http/server"); const schema_1 = require("../../schema"); const server_2 = require("../server"); const port_1 = require("../../util/port"); const client_1 = require("../client"); const client_2 = require("../http/client"); tape('http server', async (t) => { const protocol = { name: 'test', api: { login: { arg: new schema_1.MuUTF8(), ret: new schema_1.MuVoid(), }, logout: { arg: new schema_1.MuVoid(), ret: new schema_1.MuVoid(), }, hello: { arg: new schema_1.MuVoid(), ret: new schema_1.MuUTF8(), }, add: { arg: new schema_1.MuStruct({ a: new schema_1.MuFloat64(), b: new schema_1.MuFloat64(), }), ret: new schema_1.MuFloat64(), }, }, }; const transport = new server_1.MuRPCHttpServerTransport({ route: 'api', byteLimit: 1 << 20, cookie: 'auth', }); const httpServer = http.createServer(async (req, res) => { if (!transport.handler(req, res)) { t.fail('unhandled route'); res.statusCode = 404; res.end(); } }); const port = await port_1.findPortAsync(); httpServer.listen(port); const server = new server_2.MuRPCServer({ protocol, transport, authorize: async (connection) => { if (connection.auth === 'bad') { return false; } return true; }, handlers: { login: async (conn, handle) => { conn.setAuth(handle); }, logout: async (conn) => { conn.setAuth(''); }, hello: async (conn) => { if (conn.auth) { return `hello ${conn.auth}`; } return `hello guest`; }, add: async (conn, { a, b }) => { return a + b; }, }, }); const client = new client_1.MuRPCClient(protocol, new client_2.MuRPCHttpClientTransport({ url: `http://127.0.0.1:${port}/api`, timeout: Infinity, })); t.equals(await client.api.hello(), `hello guest`); await client.api.login('user'); t.equals(await client.api.hello(), `hello user`); httpServer.close(); t.end(); }); //# sourceMappingURL=http.js.map