@colyseus/core 0.18.2 → 0.18.4

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.
Files changed (45) hide show
  1. package/build/MatchMaker.cjs +2 -2
  2. package/build/MatchMaker.cjs.map +2 -2
  3. package/build/MatchMaker.d.ts +10 -4
  4. package/build/MatchMaker.mjs +2 -2
  5. package/build/MatchMaker.mjs.map +2 -2
  6. package/build/Room.cjs +11 -3
  7. package/build/Room.cjs.map +2 -2
  8. package/build/Room.d.ts +1 -1
  9. package/build/Room.mjs +11 -3
  10. package/build/Room.mjs.map +2 -2
  11. package/build/RoomMessages.cjs +2 -1
  12. package/build/RoomMessages.cjs.map +2 -2
  13. package/build/RoomMessages.d.ts +3 -1
  14. package/build/RoomMessages.mjs +2 -1
  15. package/build/RoomMessages.mjs.map +2 -2
  16. package/build/router/default_routes.cjs +2 -1
  17. package/build/router/default_routes.cjs.map +2 -2
  18. package/build/router/default_routes.mjs +2 -1
  19. package/build/router/default_routes.mjs.map +2 -2
  20. package/build/router/index.cjs +6 -1
  21. package/build/router/index.cjs.map +2 -2
  22. package/build/router/index.d.ts +1 -1
  23. package/build/router/index.mjs +6 -1
  24. package/build/router/index.mjs.map +2 -2
  25. package/build/router/node.cjs +22 -2
  26. package/build/router/node.cjs.map +2 -2
  27. package/build/router/node.mjs +22 -2
  28. package/build/router/node.mjs.map +2 -2
  29. package/build/utils/Utils.cjs.map +2 -2
  30. package/build/utils/Utils.d.ts +12 -0
  31. package/build/utils/Utils.mjs.map +2 -2
  32. package/build/utils/nanoevents.cjs +4 -1
  33. package/build/utils/nanoevents.cjs.map +2 -2
  34. package/build/utils/nanoevents.d.ts +3 -1
  35. package/build/utils/nanoevents.mjs +4 -1
  36. package/build/utils/nanoevents.mjs.map +2 -2
  37. package/package.json +9 -8
  38. package/src/MatchMaker.ts +33 -12
  39. package/src/Room.ts +23 -4
  40. package/src/RoomMessages.ts +2 -1
  41. package/src/router/default_routes.ts +2 -1
  42. package/src/router/index.ts +16 -1
  43. package/src/router/node.ts +26 -2
  44. package/src/utils/Utils.ts +18 -0
  45. package/src/utils/nanoevents.ts +4 -1
@@ -13,14 +13,28 @@ import * as matchMaker from '../MatchMaker.ts';
13
13
  import { setResponse } from '@colyseus/better-call/node';
14
14
  import { postMatchmakeMethod } from './default_routes.ts';
15
15
 
16
+ /** Matchmaking options are small — the cap only stops unbounded buffering. */
17
+ const MAX_BODY_SIZE = 1024 * 1024;
18
+
19
+ const badRequest = (status: number, message: string) =>
20
+ Object.assign(new Error(message), { status });
21
+
16
22
  function readBody(req: http.IncomingMessage): Promise<any> {
17
23
  return new Promise((resolve, reject) => {
18
24
  let data = '';
19
25
 
20
26
  req.on('data', (chunk: Buffer | string) => {
21
27
  data += chunk.toString();
28
+ if (data.length > MAX_BODY_SIZE) {
29
+ reject(badRequest(413, 'request body too large'));
30
+ req.destroy();
31
+ }
32
+ });
33
+ // JSON.parse throws on a later tick — uncaught here it kills the process.
34
+ req.on('end', () => {
35
+ try { resolve(data ? JSON.parse(data) : {}); }
36
+ catch { reject(badRequest(400, 'malformed JSON body')); }
22
37
  });
23
- req.on('end', () => resolve(data ? JSON.parse(data) : {}));
24
38
  req.on('error', reject);
25
39
  });
26
40
  }
@@ -112,10 +126,20 @@ export function createNodeMatchmakingMiddleware() {
112
126
 
113
127
  const [, method, roomName] = match;
114
128
 
129
+ let body: any;
130
+ try {
131
+ body = await readBody(req);
132
+ } catch (e: any) {
133
+ // answer here — next() would report a misleading 404 for a bad body
134
+ res.writeHead(e.status ?? 400, { ...corsHeaders, 'content-type': 'application/json' });
135
+ res.end(JSON.stringify({ error: e.message }));
136
+ return;
137
+ }
138
+
115
139
  try {
116
140
  const response = await postMatchmakeMethod({
117
141
  params: { method, roomName },
118
- body: await readBody(req),
142
+ body,
119
143
  headers: req.headers as Record<string, string>,
120
144
  request: { headers } as any,
121
145
  asResponse: true,
@@ -22,6 +22,24 @@ export type ExtractMethodOrPropertyType<
22
22
  ? Awaited<R>
23
23
  : TClass[TKey];
24
24
 
25
+ /**
26
+ * Return type of `remoteRoomCall()`.
27
+ *
28
+ * Resolves to the method's awaited return type (or the property's type) when
29
+ * the method name was captured as a literal type. Falls back to `any` when it
30
+ * wasn't — e.g. `remoteRoomCall<MyRoom>(...)` with only the room type given:
31
+ * TypeScript applies the `TMethod` default instead of inferring the literal
32
+ * once an explicit type argument list is present (microsoft/TypeScript#26242).
33
+ * Pass both type arguments (`remoteRoomCall<MyRoom, 'myMethod'>`) for a
34
+ * precise return type.
35
+ */
36
+ export type RemoteRoomCallReturn<
37
+ TRoom,
38
+ TMethod extends keyof TRoom
39
+ > = keyof TRoom extends TMethod
40
+ ? any
41
+ : ExtractMethodOrPropertyType<TRoom, TMethod>;
42
+
25
43
  // remote room call timeouts
26
44
  export const REMOTE_ROOM_SHORT_TIMEOUT = Number(process.env.COLYSEUS_PRESENCE_SHORT_TIMEOUT || 2000);
27
45
  export const MAX_CONCURRENT_CREATE_ROOM_WAIT_TIME = Number(process.env.COLYSEUS_MAX_CONCURRENT_CREATE_ROOM_WAIT_TIME || 0.5);
@@ -10,7 +10,10 @@ export const createNanoEvents = () => ({
10
10
  callbacks[i](...args)
11
11
  }
12
12
  },
13
- events: {},
13
+ // null-prototype: event names are client-supplied (message types), and on a
14
+ // plain object "__proto__" / "constructor" / "toString" resolve to inherited
15
+ // members instead of missing (colyseus/colyseus#951)
16
+ events: Object.create(null) as { [event: string]: Array<(...args: any[]) => void> },
14
17
  on(event: string, cb: (...args: any[]) => void) {
15
18
  ;(this.events[event] ||= []).push(cb)
16
19
  return () => {