@miqro/core 5.0.17 → 5.0.20

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.
@@ -31,6 +31,7 @@ import {
31
31
  APIRouteSchema,
32
32
  MinimalLogger,
33
33
  removeStartingBackSlashes,
34
+ SchemaProperties,
34
35
  } from "../types.js";
35
36
  import { ServerOptions } from "https";
36
37
  import { SessionHandler, GroupPolicyHandler } from "./session.js";
@@ -67,34 +68,62 @@ export class Router implements MinimalRouter {
67
68
  this.errorHandlers.splice(0, this.errorHandlers.length);
68
69
  }
69
70
 
70
- public get(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
71
- return this.use(handler, path, "get", options);
71
+ public get<
72
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
73
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
74
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
75
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions<TBody, TParams, TQuery>): Router {
76
+ return this.use(handler as any, path, "get", options);
72
77
  }
73
78
 
74
- public post(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
75
- return this.use(handler, path, "post", options);
79
+ public post<
80
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
81
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
82
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
83
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions): Router {
84
+ return this.use(handler as any, path, "post", options);
76
85
  }
77
86
 
78
- public patch(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
79
- return this.use(handler, path, "patch", options);
87
+ public patch<
88
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
89
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
90
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
91
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions): Router {
92
+ return this.use(handler as any, path, "patch", options);
80
93
  }
81
94
 
82
- public delete(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
83
- return this.use(handler, path, "delete", options);
95
+ public delete<
96
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
97
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
98
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
99
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions): Router {
100
+ return this.use(handler as any, path, "delete", options);
84
101
  }
85
102
 
86
- public put(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
87
- return this.use(handler, path, "put", options);
103
+ public put<
104
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
105
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
106
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
107
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions): Router {
108
+ return this.use(handler as any, path, "put", options);
88
109
  }
89
110
 
90
- public options(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
91
- return this.use(handler, path, "options", options);
111
+ public options<
112
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
113
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
114
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
115
+ >(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions<TBody, TParams, TQuery>, options?: RouterHandlerOptions): Router {
116
+ return this.use(handler as any, path, "options", options);
92
117
  }
93
118
 
94
- public use(handler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
119
+ public use<
120
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
121
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
122
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined
123
+ >(handler: Array<Handler> | HandlerWithOptions<TBody, TParams, TQuery> | Handler | MinimalRouter, path?: string, method?: Method, options?: RouterHandlerOptions<TBody, TParams, TQuery>): Router {
95
124
  const nPath = path !== undefined ? normalizePath(path) : undefined;
96
125
  const nMethod = method ? method.toLowerCase() as Method : method;
97
- this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
126
+ this.handlers.push(new RouterHandler(handler as any, nPath, nMethod, options));
98
127
  return this;
99
128
  }
100
129
 
@@ -200,7 +229,7 @@ class RouterHandler {
200
229
  private readonly originalHandler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter,
201
230
  public readonly path: string | undefined,
202
231
  private readonly method: Method | undefined,
203
- options?: RouterHandlerOptions) {
232
+ options?: RouterHandlerOptions<any, any, any>) {
204
233
  this.tokens = tokenizePath(path);
205
234
 
206
235
  if (options) {
@@ -513,7 +542,7 @@ class StaticRouter extends Router {
513
542
  allowListJSON: boolean;
514
543
  allowGetJSON: boolean;
515
544
  index: string;
516
- index404: string;
545
+ index404?: string;
517
546
  index404Status: number;
518
547
  contentTypes: any;
519
548
  defaultContentType: string;
@@ -862,7 +891,7 @@ async function traverseAPIRouteDir(
862
891
  if (route.identifier && (route.identifier === "__prototype__" || route.identifier === "__proto__")) {
863
892
  throw new Error(`invalid feature name ${route.identifier}`);
864
893
  }
865
- const useCallArgs: UseCallArg = [route.handler, path, method !== "use" && method !== "USE" ? method : undefined, {
894
+ const useCallArgs: UseCallArg = [route.handler as Handler | Handler[] | MinimalRouter, path, method !== "use" && method !== "USE" ? method : undefined, {
866
895
  identifier: route.identifier,
867
896
  apiName: route.apiName,
868
897
  name: route.name,
@@ -871,7 +900,7 @@ async function traverseAPIRouteDir(
871
900
  middleware: route.middleware,
872
901
  session: route.session,
873
902
  policy: route.policy,
874
- request: route.request,
903
+ request: route.request as any,
875
904
  response: route.response !== undefined ? route.response : true // ResponseHandler added by default to an APIRoute
876
905
  }];
877
906
  //console.dir(useCallArgs);
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ParserInterface, Schema } from "@miqro/parser";
1
+ import { ParserInterface, Schema, InferSchema } from "@miqro/parser";
2
2
  import { SerializeOptions as CookieSerializeOptions, parse, serialize } from "cookie";
3
3
  import { randomUUID } from "crypto";
4
4
  import { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http";
@@ -257,21 +257,25 @@ export const RouteOptionsSchema: Schema<RouteOptions> = {
257
257
  }
258
258
  };
259
259
 
260
- export interface RouteOptions {
260
+ export interface RouteOptions<
261
+ TBody extends SchemaProperties | string | boolean | undefined = undefined,
262
+ TParams extends SchemaProperties | string | boolean | undefined = undefined,
263
+ TQuery extends SchemaProperties | string | boolean | undefined = undefined
264
+ > {
261
265
  name?: string;
262
266
  identifier?: string;
263
267
  apiName?: string;
264
268
  description?: string;
265
269
  policy?: GroupPolicy;
266
270
  request?: {
267
- headers?: string | SchemaProperties[] | SchemaProperties;
268
- headersMode?: ParserMode;
269
- query?: boolean | string | SchemaProperties[] | SchemaProperties;
271
+ body?: TBody;
272
+ params?: TParams;
273
+ query?: TQuery;
274
+ bodyMode?: ParserMode;
270
275
  queryMode?: ParserMode;
271
- params?: string | SchemaProperties | boolean | SchemaProperties[];
272
276
  paramsMode?: ParserMode;
273
- body?: string | SchemaProperties | boolean | SchemaProperties[];
274
- bodyMode?: ParserMode;
277
+ headers?: string | SchemaProperties[] | SchemaProperties;
278
+ headersMode?: ParserMode;
275
279
  };
276
280
  response?: {
277
281
  status?: number | number[];
@@ -312,12 +316,43 @@ export const RouterHandlerOptionsSchema: Schema<RouterHandlerOptions> = {
312
316
  }
313
317
  };
314
318
 
315
- export interface RouterHandlerOptions extends RouteOptions {
319
+ export interface RouterHandlerOptions<
320
+ TBody extends SchemaProperties | string | boolean | undefined = undefined,
321
+ TParams extends SchemaProperties | string | boolean | undefined = undefined,
322
+ TQuery extends SchemaProperties | string | boolean | undefined = undefined
323
+ > extends RouteOptions<TBody, TParams, TQuery> {
316
324
  parser?: ParserInterface;
317
325
  middleware?: Handler | Handler[];
318
326
  session?: SessionHandlerOptions | Handler;
319
327
  }
320
328
 
329
+ /**
330
+ * Infer the TypeScript type produced by a request-part schema (body / params / query).
331
+ * - string shorthand → `InferSchema<T>`
332
+ * - plain properties object `{ name: "string", age: "integer" }` → object with inferred fields
333
+ * - `boolean | undefined` → `any` (pass-through, no type enforcement)
334
+ */
335
+ export type InferRequestPart<T> =
336
+ [T] extends [boolean | undefined | null] ? any :
337
+ [T] extends [string] ? InferSchema<T & string> :
338
+ T extends SchemaProperties ? InferSchema<{ type: "object"; properties: T }> :
339
+ any;
340
+
341
+ /**
342
+ * A `Request` narrowed to the typed `body`, `params`, and `query` shapes inferred
343
+ * from the schemas declared on a route's `request` option.
344
+ */
345
+ export type TypedRequest<
346
+ TBody = any,
347
+ TParams = any,
348
+ TQuery = any,
349
+ BaseRequest extends Request = Request
350
+ > = Omit<BaseRequest, "body" | "params" | "query"> & {
351
+ body?: TBody; // optional to stay compatible with Request — ParseRequest validates it at runtime
352
+ params: TParams;
353
+ query: TQuery;
354
+ };
355
+
321
356
  export const HandlerWithOptionsSchema: Schema<HandlerWithOptions> = {
322
357
  type: "object",
323
358
  properties: {
@@ -326,8 +361,16 @@ export const HandlerWithOptionsSchema: Schema<HandlerWithOptions> = {
326
361
  }
327
362
  };
328
363
 
329
- export interface HandlerWithOptions extends RouterHandlerOptions {
330
- handler: Handler | Array<Handler>;
364
+ export interface HandlerWithOptions<
365
+ TBody extends SchemaProperties | string | boolean | undefined = undefined,
366
+ TParams extends SchemaProperties | string | boolean | undefined = undefined,
367
+ TQuery extends SchemaProperties | string | boolean | undefined = undefined,
368
+ BaseRequest extends Request = Request
369
+ > extends RouterHandlerOptions<TBody, TParams, TQuery> {
370
+ handler:
371
+ | ((req: TypedRequest<InferRequestPart<TBody>, InferRequestPart<TParams>, InferRequestPart<TQuery>, BaseRequest>, res: Response) => Promise<boolean | void | HandlerResponse>)
372
+ | ((req: TypedRequest<InferRequestPart<TBody>, InferRequestPart<TParams>, InferRequestPart<TQuery>, BaseRequest>, res: Response) => boolean | void | HandlerResponse)
373
+ | Array<Handler>;
331
374
  }
332
375
 
333
376
  export interface RouterOptions {
@@ -363,17 +406,63 @@ export function isMinimalRouter(obj: any) {
363
406
  *};
364
407
  *export default route;
365
408
  * ```
409
+ *
410
+ * Use the type parameters to get typed `req.body`, `req.params`, and `req.query`
411
+ * in the handler. Use `defineRoute()` for automatic schema inference.
366
412
  */
367
- export interface APIRoute extends RouterHandlerOptions {
413
+ export interface APIRoute<
414
+ BaseRequest extends Request = Request,
415
+ TBody extends SchemaProperties | string | boolean | undefined = undefined,
416
+ TParams extends SchemaProperties | string | boolean | undefined = undefined,
417
+ TQuery extends SchemaProperties | string | boolean | undefined = undefined,
418
+ > extends Omit<RouterHandlerOptions, "request"> {
368
419
  postFolder?: boolean;
369
420
  basePath?: string;
370
421
  method?: Method[] | Method | "use" | "USE";
371
422
  path?: string[] | string | null;
372
423
  ignore?: boolean;
373
- handler: Handler | Array<Handler> | MinimalRouter;
424
+ request?: {
425
+ body?: TBody;
426
+ params?: TParams;
427
+ query?: TQuery;
428
+ bodyMode?: ParserMode;
429
+ queryMode?: ParserMode;
430
+ paramsMode?: ParserMode;
431
+ headers?: string | SchemaProperties[] | SchemaProperties;
432
+ headersMode?: ParserMode;
433
+ };
434
+ handler:
435
+ | ((req: TypedRequest<InferRequestPart<TBody>, InferRequestPart<TParams>, InferRequestPart<TQuery>, BaseRequest>, res: Response) => Promise<boolean | void | HandlerResponse>)
436
+ | ((req: TypedRequest<InferRequestPart<TBody>, InferRequestPart<TParams>, InferRequestPart<TQuery>, BaseRequest>, res: Response) => boolean | void | HandlerResponse)
437
+ | Array<Handler>
438
+ | MinimalRouter;
374
439
  init?: (route: APIRoute) => Promise<void>;
375
440
  }
376
441
 
442
+ /**
443
+ * Type-safe factory for file-based API routes (`export default`).
444
+ *
445
+ * Wrap your route object so TypeScript automatically infers `req.body`,
446
+ * `req.params`, and `req.query` from the schemas in `request`.
447
+ *
448
+ * @example
449
+ * export default defineRoute({
450
+ * request: { body: { name: "string", age: "integer" } },
451
+ * handler: async (req) => {
452
+ * req.body.name; // string
453
+ * req.body.age; // number
454
+ * }
455
+ * });
456
+ */
457
+ export function defineRoute<
458
+ BaseRequest extends Request = Request,
459
+ const TBody extends SchemaProperties | string | boolean | undefined = undefined,
460
+ const TParams extends SchemaProperties | string | boolean | undefined = undefined,
461
+ const TQuery extends SchemaProperties | string | boolean | undefined = undefined,
462
+ >(route: APIRoute<BaseRequest, TBody, TParams, TQuery>): APIRoute<BaseRequest, TBody, TParams, TQuery> {
463
+ return route as unknown as APIRoute<BaseRequest, TBody, TParams, TQuery>;
464
+ }
465
+
377
466
  export interface APIRouterOptions {
378
467
  dirname: string;
379
468
  apiName?: string;
@@ -855,11 +944,12 @@ export interface WebSocketComplexValidateResult {
855
944
 
856
945
  export interface WebSocketServerOptions {
857
946
  maxConnections?: number;
947
+ maxFrameSize?: number;
858
948
  validate?: (req: Request) => Promise<boolean | string | WebSocketComplexValidateResult> | boolean | string | WebSocketComplexValidateResult;
859
- onConnection?: (req: WebSocketClient) => void;
860
- onMessage?: (req: WebSocketClient, data: string | undefined | null) => void;
861
- onDisconnect?: (req: WebSocketClient) => void;
862
- onError?: (req: WebSocketClient, error: Error) => void;
949
+ onConnection?: (req: WebSocketClient) => void | Promise<void>;
950
+ onMessage?: (req: WebSocketClient, data: string | undefined | null) => void | Promise<void>;
951
+ onDisconnect?: (req: WebSocketClient) => void | Promise<void>;
952
+ onError?: (req: WebSocketClient, error: Error) => void | Promise<void>;
863
953
  }
864
954
 
865
955
  export interface WebSocketClient {
package/src/websocket.ts CHANGED
@@ -37,12 +37,21 @@ export class WebSocketServer implements ConnectionUpgrader {
37
37
  if (!client) {
38
38
  throw new Error("client not found");
39
39
  }
40
+ if (!client.socket.writable) {
41
+ this.clients.delete(clientUUID);
42
+ throw new Error("socket not writable");
43
+ }
44
+ if (client.socket.writableLength > (this.options.maxFrameSize ?? DEFAULT_MAX_FRAME_SIZE)) {
45
+ client.socket.destroy();
46
+ this.clients.delete(clientUUID);
47
+ throw new Error("client send buffer full");
48
+ }
40
49
  return new Promise<void>((resolve, reject) => {
41
50
  try {
42
- client.req.socket.write(createFrame(data), (err?: Error | null) => {
51
+ client.socket.write(createFrame(data), (err?: Error | null) => {
43
52
  if (err) {
44
53
  try {
45
- client.req.socket.destroy(err);
54
+ client.socket.destroy(err);
46
55
  } catch (e) {
47
56
  console.error(e);
48
57
  }
@@ -51,10 +60,10 @@ export class WebSocketServer implements ConnectionUpgrader {
51
60
  } else {
52
61
  resolve();
53
62
  }
54
- })
63
+ });
55
64
  } catch (e) {
56
65
  try {
57
- client.req.socket.destroy();
66
+ client.socket.destroy();
58
67
  } catch (e) {
59
68
  console.error(e);
60
69
  }
@@ -114,8 +123,8 @@ export class WebSocketServer implements ConnectionUpgrader {
114
123
 
115
124
  socket.write(createUpgradeHeaders(acceptKey, extraHeaders));
116
125
 
117
- socket.on("data", (data: Buffer) => {
118
- const frame = parseFrame(data);
126
+ socket.on("data", async (data: Buffer) => {
127
+ const frame = parseFrame(data, this.options.maxFrameSize);
119
128
  if (frame === PING) {
120
129
  try {
121
130
  socket.write(Buffer.from([0x8A, 0x00])); // pong frame
@@ -128,7 +137,7 @@ export class WebSocketServer implements ConnectionUpgrader {
128
137
  socket.destroy();
129
138
  } else {
130
139
  try {
131
- this.options.onMessage(client, frame);
140
+ await this.options.onMessage(client, frame);
132
141
  } catch (e) {
133
142
  req.logger.error(e);
134
143
  }
@@ -145,24 +154,24 @@ export class WebSocketServer implements ConnectionUpgrader {
145
154
  }
146
155
  });
147
156
 
148
- socket.on("error", (error) => {
157
+ socket.on("error", async (error) => {
149
158
  req.logger.debug("upgraded connection error!");
150
159
  req.logger.error(error);
151
160
  if (this.options.onError) {
152
161
  try {
153
- this.options.onError(client, error);
162
+ await this.options.onError(client, error);
154
163
  } catch (e) {
155
164
  req.logger.error(e);
156
165
  }
157
166
  }
158
167
  });
159
168
 
160
- socket.on("end", () => {
169
+ socket.on("end", async () => {
161
170
  req.logger.debug("upgraded connection disconnected!");
162
171
  this.clients.delete(uuid);
163
172
  if (this.options.onDisconnect) {
164
173
  try {
165
- this.options.onDisconnect(client);
174
+ await this.options.onDisconnect(client);
166
175
  } catch (e) {
167
176
  req.logger.error(e);
168
177
  }
@@ -175,7 +184,7 @@ export class WebSocketServer implements ConnectionUpgrader {
175
184
  this.clients.set(uuid, client);
176
185
  if (this.options.onConnection) {
177
186
  try {
178
- this.options.onConnection(client);
187
+ await this.options.onConnection(client);
179
188
  } catch (e) {
180
189
  req.logger.error(e);
181
190
  }
@@ -213,41 +222,53 @@ export class ClusterWebSocketServer extends WebSocketServer {
213
222
  return options.validate ? options.validate(req) : true;
214
223
  }
215
224
  },
216
- onError: (req, error) => {
225
+ onError: async (req, error) => {
217
226
  if (process.send) {
218
- process.send({
219
- type: ClusterWebSocketServerMessageType,
220
- action: "error",
221
- clientUUID: req.uuid,
222
- errorMessage: error.message
223
- } as ClusterWebSocketServerMessage);
227
+ try {
228
+ process.send({
229
+ type: ClusterWebSocketServerMessageType,
230
+ action: "error",
231
+ clientUUID: req.uuid,
232
+ errorMessage: error.message
233
+ } as ClusterWebSocketServerMessage);
234
+ } catch (e) {
235
+ req.req.logger.error(e);
236
+ }
224
237
  }
225
238
  if (options.onError) {
226
- options.onError(req, error);
239
+ await options.onError(req, error);
227
240
  }
228
241
  },
229
- onConnection: (req) => {
242
+ onConnection: async (req) => {
230
243
  if (process.send) {
231
- process.send({
232
- type: ClusterWebSocketServerMessageType,
233
- action: "connection",
234
- clientUUID: req.uuid
235
- } as ClusterWebSocketServerMessage);
244
+ try {
245
+ process.send({
246
+ type: ClusterWebSocketServerMessageType,
247
+ action: "connection",
248
+ clientUUID: req.uuid
249
+ } as ClusterWebSocketServerMessage);
250
+ } catch (e) {
251
+ req.req.logger.error(e);
252
+ }
236
253
  }
237
254
  if (options.onConnection) {
238
- options.onConnection(req);
255
+ await options.onConnection(req);
239
256
  }
240
257
  },
241
- onDisconnect: (req) => {
258
+ onDisconnect: async (req) => {
242
259
  if (process.send) {
243
- process.send({
244
- type: ClusterWebSocketServerMessageType,
245
- action: "disconnection",
246
- clientUUID: req.uuid
247
- } as ClusterWebSocketServerMessage);
260
+ try {
261
+ process.send({
262
+ type: ClusterWebSocketServerMessageType,
263
+ action: "disconnection",
264
+ clientUUID: req.uuid
265
+ } as ClusterWebSocketServerMessage);
266
+ } catch (e) {
267
+ req.req.logger.error(e);
268
+ }
248
269
  }
249
270
  if (options.onDisconnect) {
250
- options.onDisconnect(req);
271
+ await options.onDisconnect(req);
251
272
  }
252
273
  }
253
274
  });
@@ -355,7 +376,9 @@ function createUpgradeHeaders(acceptKey: string, extraHeaders?: {
355
376
  return `HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${acceptValue}${extra}\r\n\r\n`;
356
377
  }
357
378
 
358
- function parseFrame(buffer: Buffer) {
379
+ const DEFAULT_MAX_FRAME_SIZE = 1024 * 1024; // 1 MiB
380
+
381
+ function parseFrame(buffer: Buffer, maxFrameSize = DEFAULT_MAX_FRAME_SIZE) {
359
382
  const firstByte = buffer.readUInt8(0);
360
383
  const opCode = firstByte & 0b00001111; // get last 4 bits of a byte
361
384
 
@@ -381,6 +404,10 @@ function parseFrame(buffer: Buffer) {
381
404
  offset += 8;
382
405
  }
383
406
 
407
+ if (payloadLength > maxFrameSize) {
408
+ return null;
409
+ }
410
+
384
411
  const isMasked = Boolean((secondByte >>> 7) & 0x1); // get first bit of a second byte
385
412
 
386
413
  if (isMasked) {