UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

79 lines (78 loc) 3.33 kB
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); var controller_exports = {}; __export(controller_exports, { default: () => controller_default }); module.exports = __toCommonJS(controller_exports); var import_Protocol = require("../Protocol.js"); var import_ServerError = require("../errors/ServerError.js"); var matchMaker = __toESM(require("../MatchMaker.js")); var controller_default = { DEFAULT_CORS_HEADERS: { "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization", "Access-Control-Allow-Methods": "OPTIONS, POST, GET", "Access-Control-Allow-Credentials": "true", "Access-Control-Allow-Origin": "*", "Access-Control-Max-Age": "2592000" // ... }, exposedMethods: ["joinOrCreate", "create", "join", "joinById", "reconnect"], allowedRoomNameChars: /([a-zA-Z_\-0-9]+)/gi, matchmakeRoute: "matchmake", /** * You can manually change the default corsHeaders by overwriting the `getCorsHeaders()` method: * ``` * import { matchMaker } from "@colyseus/core"; * matchMaker.controller.getCorsHeaders = function(req) { * if (req.headers.referer !== "xxx") { * } * * return { * 'Access-Control-Allow-Origin': 'safedomain.com', * } * } * ``` */ getCorsHeaders(req) { const origin = req.headers && req.headers["origin"] || req.getHeader && req.getHeader("origin"); return { ["Access-Control-Allow-Origin"]: origin || "*" }; }, async invokeMethod(method, roomName, clientOptions = {}, authOptions) { if (this.exposedMethods.indexOf(method) === -1) { throw new import_ServerError.ServerError(import_Protocol.ErrorCode.MATCHMAKE_NO_HANDLER, `invalid method "${method}"`); } try { return await matchMaker[method](roomName, clientOptions, authOptions); } catch (e) { throw new import_ServerError.ServerError(e.code || import_Protocol.ErrorCode.MATCHMAKE_UNHANDLED, e.message); } } };