UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

88 lines (86 loc) 3.55 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/core/src/router/default_routes.ts var default_routes_exports = {}; __export(default_routes_exports, { getDefaultRouter: () => getDefaultRouter, postMatchmakeMethod: () => postMatchmakeMethod }); module.exports = __toCommonJS(default_routes_exports); var import_better_call = require("@colyseus/better-call"); var matchMaker = __toESM(require("../MatchMaker.cjs"), 1); var import_Utils = require("../utils/Utils.cjs"); var import_Transport = require("../Transport.cjs"); var postMatchmakeMethod = (0, import_better_call.createEndpoint)("/matchmake/:method/:roomName", { method: "POST" }, async (ctx) => { if (matchMaker.state === matchMaker.MatchMakerState.SHUTTING_DOWN) { throw ctx.error(503); } const requestHeaders = ctx.request.headers; const method = ctx.params.method; const roomName = ctx.params.roomName; try { const clientOptions = ctx.body; const response = await matchMaker.controller.invokeMethod( method, roomName, clientOptions, { token: (0, import_Utils.getBearerToken)(ctx.request.headers.get("authorization")), headers: ctx.request.headers, ip: requestHeaders.get("x-forwarded-for") ?? requestHeaders.get("x-client-ip") ?? requestHeaders.get("x-real-ip"), req: ctx.request } ); const transport = (0, import_Transport.getTransport)(); if (transport.protocol !== void 0) { response.protocol = transport.protocol; } const json = JSON.stringify(response); return new Response(json, { headers: { "content-type": "application/json", // // Set content length manually to avoid "chunked" transfer-encoding header // See https://github.com/haxetink/tink_http/issues/27 // "content-length": json.length.toString() } }); } catch (e) { throw ctx.error(e.code, { code: e.code, error: e.message }); } }); function getDefaultRouter() { return (0, import_better_call.createRouter)({ postMatchmakeMethod }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getDefaultRouter, postMatchmakeMethod });