@colyseus/playground 0.17.0 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
1
  {
2
2
  "name": "@colyseus/playground",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "main": "./build/index.js",
5
5
  "module": "./build/index.mjs",
6
6
  "typings": "./build/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
9
+ "@source": "./src-backend/index.ts",
9
10
  "types": "./build/index.d.ts",
10
11
  "import": "./build/index.mjs",
11
12
  "require": "./build/index.js"
12
13
  },
13
14
  "./*": {
15
+ "@source": "./src-backend/*.ts",
14
16
  "types": "./build/*.d.ts",
15
- "import": "./src-backend/*.ts",
17
+ "import": "./build/*.mjs",
16
18
  "require": "./build/*.js"
17
19
  }
18
20
  },
19
21
  "files": [
20
22
  "build",
21
- "README.md",
22
- "LICENSE",
23
- "screenshot.png"
23
+ "src-backend"
24
24
  ],
25
25
  "peerDependencies": {
26
26
  "express": ">=4.16.0",
27
27
  "zod": "^4.1.12",
28
- "@colyseus/auth": "^0.17.0",
29
- "@colyseus/core": "^0.17.0"
28
+ "@colyseus/auth": "^0.17.2",
29
+ "@colyseus/core": "^0.17.2"
30
30
  },
31
31
  "browserslist": {
32
32
  "production": [
@@ -70,7 +70,7 @@
70
70
  "vite": "^5.0.11",
71
71
  "vitest": "^1.1.3",
72
72
  "web-vitals": "^2.1.4",
73
- "@colyseus/sdk": "^0.17.0"
73
+ "@colyseus/sdk": "^0.17.2"
74
74
  },
75
75
  "dependencies": {
76
76
  "@colyseus/better-call": "^1.0.26"
@@ -0,0 +1,29 @@
1
+ import { Room, Client, ClientState, ClientPrivate, AuthContext } from '@colyseus/core';
2
+
3
+ export async function applyMonkeyPatch() {
4
+ /**
5
+ * Optional: if zod is available, we can use toJSONSchema() for body and query types
6
+ */
7
+ let z: any = undefined;
8
+ try { z = await import("zod"); } catch (e: any) { /* zod not installed */ }
9
+
10
+ const _onJoin = Room.prototype['_onJoin'];
11
+ Room.prototype['_onJoin'] = async function (this: Room, client: Client & ClientPrivate) {
12
+ const result = await _onJoin.apply(this, arguments as any);
13
+
14
+ if (client.state === ClientState.JOINING) {
15
+
16
+ const messages: any = {};
17
+ Object.keys(this['onMessageEvents'].events).sort().forEach((type) => {
18
+ if (type.indexOf("__") === 0 || type === "*") { return; }
19
+
20
+ const messageValidator = this['onMessageValidators'][type];
21
+ messages[type] = z && messageValidator && z.toJSONSchema(messageValidator) || null;
22
+ });
23
+
24
+ client.send("__playground_message_types", messages);
25
+ }
26
+
27
+ return result;
28
+ }
29
+ }
@@ -0,0 +1,74 @@
1
+ import path from 'path';
2
+ import express, { Router } from 'express';
3
+ import { auth, JWT } from '@colyseus/auth';
4
+ import { matchMaker, IRoomCache, __globalEndpoints } from '@colyseus/core';
5
+ import type { Endpoint } from "@colyseus/better-call";
6
+ import { applyMonkeyPatch } from './colyseus.ext.js';
7
+
8
+ import { fileURLToPath } from 'url'; // required for ESM build (see build.mjs)
9
+
10
+ export type AuthConfig = {
11
+ oauth: string[],
12
+ register: boolean,
13
+ anonymous: boolean,
14
+ };
15
+
16
+ export function playground(): Router {
17
+ applyMonkeyPatch();
18
+
19
+ const router = express.Router();
20
+
21
+ // serve static frontend
22
+ router.use("/", express.static(path.resolve(__dirname, "..", "build")));
23
+
24
+ // expose matchmaking stats
25
+ router.get("/rooms", async (req, res) => {
26
+ const rooms = await matchMaker.driver.query({});
27
+
28
+ const roomsByType: { [roomName: string]: number } = {};
29
+ const roomsById: { [roomName: string]: IRoomCache } = {};
30
+
31
+ rooms.forEach((room) => {
32
+ if (!roomsByType[room.name]) { roomsByType[room.name] = 0; }
33
+ roomsByType[room.name]++;
34
+ roomsById[room.roomId] = room;
35
+ });
36
+
37
+ res.json({
38
+ rooms: Object.keys(matchMaker.getAllHandlers()),
39
+
40
+ roomsByType,
41
+ roomsById,
42
+
43
+ auth: {
44
+ // list of OAuth providers
45
+ oauth: Object.keys(auth.oauth.providers),
46
+ register: typeof(auth.settings.onRegisterWithEmailAndPassword) === "function",
47
+ anonymous: typeof(JWT.settings.secret) === "string",
48
+ } as AuthConfig
49
+ });
50
+ });
51
+
52
+ // serve API docs for playground
53
+ // (workaround to use better-call route inside express.Router)
54
+ router.get("/__apidocs", async (_, res) => {
55
+ /**
56
+ * Optional: if zod is available, we can use toJSONSchema() for body and query types
57
+ */
58
+ let z: any = undefined;
59
+ try { z = await import("zod"); } catch (e: any) { /* zod not installed */ }
60
+
61
+ res.json(Object.values(__globalEndpoints).map((endpoint: Endpoint) => {
62
+ return {
63
+ method: endpoint.options.method,
64
+ path: endpoint.path,
65
+ body: z && endpoint.options.body && z.toJSONSchema(endpoint.options.body),
66
+ query: z && endpoint.options.query && z.toJSONSchema(endpoint.options.query),
67
+ metadata: endpoint.options.metadata,
68
+ description: endpoint.options.metadata?.openapi?.description,
69
+ };
70
+ }));
71
+ });
72
+
73
+ return router;
74
+ }
package/screenshot.png DELETED
Binary file