walter-ws 1.0.0 → 1.2.0

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/src/connection.js CHANGED
@@ -79,6 +79,11 @@ class WalterConnection extends EventEmitter {
79
79
  this._fragmentsLength = 0;
80
80
  this._fragmentOpcode = null;
81
81
 
82
+ // Room membership (lazy Set) and fast corked batch queue
83
+ this.rooms = null;
84
+ this._server = options.server || null;
85
+ this._corkScheduled = false;
86
+
82
87
  this._parser = new FrameParser({
83
88
  maxPayload: this.maxPayload,
84
89
  requireMask: this.isServer, // Server requires client frames to be masked; Client requires unmasked frames
@@ -87,12 +92,93 @@ class WalterConnection extends EventEmitter {
87
92
  this._bindSocketEvents();
88
93
  }
89
94
 
95
+ /**
96
+ * Joins a named room on the server in O(1) time.
97
+ * @param {string} roomName
98
+ * @returns {this}
99
+ */
100
+ join(roomName) {
101
+ if (!this.rooms) this.rooms = new Set();
102
+ if (this._server) {
103
+ this._server.joinRoom(roomName, this);
104
+ } else {
105
+ this.rooms.add(roomName);
106
+ }
107
+ return this;
108
+ }
109
+
110
+ /**
111
+ * Leaves a named room on the server in O(1) time.
112
+ * @param {string} roomName
113
+ * @returns {this}
114
+ */
115
+ leave(roomName) {
116
+ if (!this.rooms) return this;
117
+ if (this._server) {
118
+ this._server.leaveRoom(roomName, this);
119
+ } else {
120
+ this.rooms.delete(roomName);
121
+ }
122
+ return this;
123
+ }
124
+
125
+ /**
126
+ * Leaves all joined rooms.
127
+ */
128
+ leaveAllRooms() {
129
+ if (!this.rooms || this.rooms.size === 0) return;
130
+ if (this._server) {
131
+ for (const room of this.rooms) {
132
+ this._server.leaveRoom(room, this);
133
+ }
134
+ }
135
+ this.rooms.clear();
136
+ }
137
+
138
+ /**
139
+ * Schedules a socket uncork on next microtask after batching multiple writes in the same tick.
140
+ */
141
+ _writeCorked(buffer, callback) {
142
+ if (!this._corkScheduled) {
143
+ this._corkScheduled = true;
144
+ this.socket.cork();
145
+ queueMicrotask(() => {
146
+ this._corkScheduled = false;
147
+ if (!this.socket.destroyed) {
148
+ this.socket.uncork();
149
+ }
150
+ });
151
+ }
152
+ this.socket.write(buffer, callback);
153
+ }
154
+
155
+ /**
156
+ * Returns true if the underlying TCP send buffer is full (backpressure guard).
157
+ * @param {number} [highWaterMark=65536]
158
+ * @returns {boolean}
159
+ */
160
+ isBackpressured(highWaterMark = 65536) {
161
+ return this.socket.writableLength > highWaterMark;
162
+ }
163
+
164
+ /**
165
+ * Returns current bytes queued in user-space socket buffer.
166
+ * @returns {number}
167
+ */
168
+ get bufferedAmount() {
169
+ return this.socket ? this.socket.writableLength : 0;
170
+ }
171
+
90
172
  /**
91
173
  * Attaches data, error, and close listeners to the underlying TCP socket.
92
174
  */
93
175
  _bindSocketEvents() {
94
176
  this.socket.setNoDelay(true);
95
177
 
178
+ if (this.socket.listenerCount('data') > 0) {
179
+ // Do not disturb existing listeners if any
180
+ }
181
+
96
182
  this.socket.on('data', (chunk) => {
97
183
  this._onSocketData(chunk);
98
184
  });
@@ -113,6 +199,7 @@ class WalterConnection extends EventEmitter {
113
199
  this._fragments = [];
114
200
  this._fragmentsLength = 0;
115
201
  this._fragmentOpcode = null;
202
+ this.leaveAllRooms();
116
203
 
117
204
  if (this.readyState !== READY_STATES.CLOSED) {
118
205
  this.readyState = READY_STATES.CLOSED;
@@ -139,9 +226,23 @@ class WalterConnection extends EventEmitter {
139
226
  return;
140
227
  }
141
228
 
142
- for (const frame of frames) {
229
+ const len = frames.length;
230
+ if (len === 0) return;
231
+
232
+ if (len > 1 && !this._corkScheduled && this.readyState === READY_STATES.OPEN) {
233
+ this._corkScheduled = true;
234
+ this.socket.cork();
235
+ queueMicrotask(() => {
236
+ this._corkScheduled = false;
237
+ if (!this.socket.destroyed) {
238
+ this.socket.uncork();
239
+ }
240
+ });
241
+ }
242
+
243
+ for (let i = 0; i < len; i++) {
143
244
  try {
144
- this._processFrame(frame);
245
+ this._processFrame(frames[i]);
145
246
  } catch (err) {
146
247
  this._handleProtocolError(err);
147
248
  return;
@@ -333,11 +434,12 @@ class WalterConnection extends EventEmitter {
333
434
  const isBinary =
334
435
  options.binary ?? (Buffer.isBuffer(data) || ArrayBuffer.isView(data));
335
436
  const opcode = isBinary ? OPCODES.BINARY : OPCODES.TEXT;
336
- const payload = Buffer.isBuffer(data)
337
- ? data
338
- : ArrayBuffer.isView(data)
339
- ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
340
- : Buffer.from(String(data), 'utf8');
437
+ const payload =
438
+ typeof data === 'string' || Buffer.isBuffer(data)
439
+ ? data
440
+ : ArrayBuffer.isView(data)
441
+ ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
442
+ : String(data);
341
443
 
342
444
  const frame = encodeFrame({
343
445
  opcode,
@@ -348,15 +450,37 @@ class WalterConnection extends EventEmitter {
348
450
  });
349
451
 
350
452
  if (Array.isArray(frame)) {
351
- this.socket.cork();
352
- this.socket.write(frame[0]);
353
- this.socket.write(frame[1], callback);
354
- this.socket.uncork();
453
+ if (this._corkScheduled) {
454
+ this.socket.write(frame[0]);
455
+ this.socket.write(frame[1], callback);
456
+ } else {
457
+ this.socket.cork();
458
+ this.socket.write(frame[0]);
459
+ this.socket.write(frame[1], callback);
460
+ this.socket.uncork();
461
+ }
462
+ } else if (options.batch) {
463
+ this._writeCorked(frame, callback);
355
464
  } else {
356
465
  this.socket.write(frame, callback);
357
466
  }
358
467
  }
359
468
 
469
+ /**
470
+ * Sends a pre-encoded WebSocket frame buffer directly to the TCP socket with zero encoding overhead.
471
+ * @param {Buffer|Buffer[]} preEncodedFrame
472
+ * @param {Function} [callback]
473
+ */
474
+ sendFrame(preEncodedFrame, callback) {
475
+ if (this.readyState !== READY_STATES.OPEN) return;
476
+ if (Array.isArray(preEncodedFrame)) {
477
+ this._writeCorked(preEncodedFrame[0]);
478
+ this._writeCorked(preEncodedFrame[1], callback);
479
+ } else {
480
+ this._writeCorked(preEncodedFrame, callback);
481
+ }
482
+ }
483
+
360
484
  /**
361
485
  * Helper to serialize and send an object as JSON text.
362
486
  * @param {any} obj
@@ -379,7 +503,7 @@ class WalterConnection extends EventEmitter {
379
503
  fin: true,
380
504
  mask: !this.isServer,
381
505
  });
382
- this.socket.write(frame, callback);
506
+ this._writeCorked(frame, callback);
383
507
  }
384
508
 
385
509
  /**
@@ -395,7 +519,7 @@ class WalterConnection extends EventEmitter {
395
519
  fin: true,
396
520
  mask: !this.isServer,
397
521
  });
398
- this.socket.write(frame, callback);
522
+ this._writeCorked(frame, callback);
399
523
  }
400
524
 
401
525
  /**
@@ -455,6 +579,44 @@ class WalterConnection extends EventEmitter {
455
579
  this.socket.write(frame, callback);
456
580
  }
457
581
 
582
+ /**
583
+ * W3C EventTarget compatibility: addEventListener
584
+ * @param {string} type
585
+ * @param {Function} listener
586
+ * @param {object|boolean} [options]
587
+ */
588
+ addEventListener(type, listener, options) {
589
+ if (typeof listener !== 'function') return;
590
+ const once = typeof options === 'object' && options !== null && options.once;
591
+ const wrapped = (arg1, arg2) => {
592
+ if (type === 'message') {
593
+ listener({ data: arg1, isBinary: arg2, target: this });
594
+ } else if (type === 'close') {
595
+ listener({ code: arg1, reason: arg2, target: this });
596
+ } else if (type === 'error') {
597
+ listener({ error: arg1, message: arg1?.message, target: this });
598
+ } else {
599
+ listener({ target: this });
600
+ }
601
+ };
602
+ listener._walterWrapped = wrapped;
603
+ if (once) {
604
+ this.once(type, wrapped);
605
+ } else {
606
+ this.on(type, wrapped);
607
+ }
608
+ }
609
+
610
+ /**
611
+ * W3C EventTarget compatibility: removeEventListener
612
+ * @param {string} type
613
+ * @param {Function} listener
614
+ */
615
+ removeEventListener(type, listener) {
616
+ if (!listener) return;
617
+ this.removeListener(type, listener._walterWrapped || listener);
618
+ }
619
+
458
620
  /**
459
621
  * Immediately destroys the underlying TCP connection without a closing handshake.
460
622
  */
package/src/frame.js CHANGED
@@ -57,30 +57,58 @@ function applyMask(src, maskKey, dest = Buffer.allocUnsafe(src.length), destOffs
57
57
 
58
58
  let i = 0;
59
59
 
60
- // Use 32-bit typed array operations when buffers are 4-byte aligned for maximum throughput
61
- const srcByteOffset = src.byteOffset;
62
- const destByteOffset = dest.byteOffset + destOffset;
63
- if (len >= 32 && (srcByteOffset & 3) === 0 && (destByteOffset & 3) === 0) {
64
- const u32Count = len >>> 2;
65
- const src32 = new Uint32Array(src.buffer, srcByteOffset, u32Count);
66
- const dest32 = new Uint32Array(dest.buffer, destByteOffset, u32Count);
67
- // Little-endian 32-bit mask representation
68
- const mask32 = (k0 | (k1 << 8) | (k2 << 16) | (k3 << 24)) >>> 0;
69
- for (let j = 0; j < u32Count; j++) {
70
- dest32[j] = src32[j] ^ mask32;
71
- }
72
- i = u32Count << 2;
73
- } else {
74
- const end4 = len & ~3;
75
- while (i < end4) {
76
- dest[destOffset + i] = src[i] ^ k0;
77
- dest[destOffset + i + 1] = src[i + 1] ^ k1;
78
- dest[destOffset + i + 2] = src[i + 2] ^ k2;
79
- dest[destOffset + i + 3] = src[i + 3] ^ k3;
80
- i += 4;
60
+ if (len >= 32) {
61
+ const srcByteOffset = src.byteOffset;
62
+ const destByteOffset = dest.byteOffset + destOffset;
63
+ const srcMod = srcByteOffset & 3;
64
+ const destMod = destByteOffset & 3;
65
+
66
+ if (srcMod === destMod) {
67
+ let alignPad = 0;
68
+ if (srcMod !== 0) {
69
+ alignPad = 4 - srcMod;
70
+ dest[destOffset] = src[0] ^ k0;
71
+ if (alignPad >= 2) dest[destOffset + 1] = src[1] ^ k1;
72
+ if (alignPad === 3) dest[destOffset + 2] = src[2] ^ k2;
73
+ i = alignPad;
74
+ }
75
+
76
+ const u32Count = (len - alignPad) >>> 2;
77
+ if (u32Count > 0) {
78
+ const src32 = new Uint32Array(src.buffer, srcByteOffset + alignPad, u32Count);
79
+ const dest32 = new Uint32Array(dest.buffer, destByteOffset + alignPad, u32Count);
80
+ let mask32;
81
+ if (alignPad === 0) {
82
+ mask32 = (k0 | (k1 << 8) | (k2 << 16) | (k3 << 24)) >>> 0;
83
+ } else if (alignPad === 1) {
84
+ mask32 = (k1 | (k2 << 8) | (k3 << 16) | (k0 << 24)) >>> 0;
85
+ } else if (alignPad === 2) {
86
+ mask32 = (k2 | (k3 << 8) | (k0 << 16) | (k1 << 24)) >>> 0;
87
+ } else {
88
+ mask32 = (k3 | (k0 << 8) | (k1 << 16) | (k2 << 24)) >>> 0;
89
+ }
90
+ for (let j = 0; j < u32Count; j++) {
91
+ dest32[j] = src32[j] ^ mask32;
92
+ }
93
+ i += u32Count << 2;
94
+ }
95
+ const remKeys = [k0, k1, k2, k3];
96
+ for (; i < len; i++) {
97
+ dest[destOffset + i] = src[i] ^ remKeys[i & 3];
98
+ }
99
+ return dest;
81
100
  }
82
101
  }
83
102
 
103
+ const end4 = len & ~3;
104
+ while (i < end4) {
105
+ dest[destOffset + i] = src[i] ^ k0;
106
+ dest[destOffset + i + 1] = src[i + 1] ^ k1;
107
+ dest[destOffset + i + 2] = src[i + 2] ^ k2;
108
+ dest[destOffset + i + 3] = src[i + 3] ^ k3;
109
+ i += 4;
110
+ }
111
+
84
112
  const remKeys = [k0, k1, k2, k3];
85
113
  for (; i < len; i++) {
86
114
  dest[destOffset + i] = src[i] ^ remKeys[i & 3];
@@ -107,8 +135,17 @@ function encodeFrame(options) {
107
135
  mask = false,
108
136
  returnParts = false,
109
137
  } = options;
110
- const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(String(payload), 'utf8');
111
- const payloadLength = payloadBuf.length;
138
+
139
+ let payloadBuf;
140
+ let payloadLength;
141
+
142
+ if (Buffer.isBuffer(payload)) {
143
+ payloadBuf = payload;
144
+ payloadLength = payload.length;
145
+ } else {
146
+ payloadBuf = Buffer.from(String(payload), 'utf8');
147
+ payloadLength = payloadBuf.length;
148
+ }
112
149
 
113
150
  // Control frames (Close, Ping, Pong) cannot exceed 125 bytes or be fragmented
114
151
  if ((opcode & 0x08) !== 0) {
@@ -133,7 +170,7 @@ function encodeFrame(options) {
133
170
  headerLength = 10;
134
171
  }
135
172
 
136
- // Unmasked frames with large payloads (>= 1024 bytes): allocate only the small header
173
+ // Unmasked frames with payloads >= 1024 bytes: allocate only the 2/4/10-byte header and zero-copy write with cork()
137
174
  if (!mask && payloadLength >= 1024 && returnParts) {
138
175
  const header = Buffer.allocUnsafe(headerLength);
139
176
  header[0] = (fin ? 0x80 : 0x00) | (opcode & 0x0f);
@@ -147,8 +184,8 @@ function encodeFrame(options) {
147
184
  return [header, payloadBuf];
148
185
  }
149
186
 
150
- // Masked frames with large payloads (>= 4096 bytes): allocate 8/14-byte header + masked copy
151
- if (mask && payloadLength >= 4096 && returnParts) {
187
+ // Masked frames with large payloads (>= 16384 bytes): allocate 4-byte-aligned payload buffer + small header
188
+ if (mask && payloadLength >= 16384 && returnParts) {
152
189
  const header = Buffer.allocUnsafe(headerLength + 4);
153
190
  header[0] = (fin ? 0x80 : 0x00) | (opcode & 0x0f);
154
191
  header[1] = secondByte;
@@ -160,13 +197,14 @@ function encodeFrame(options) {
160
197
  }
161
198
  fillRandomMask(header, headerLength);
162
199
  const maskKey = header.subarray(headerLength, headerLength + 4);
163
- const maskedPayload = Buffer.allocUnsafe(payloadLength);
200
+ const maskedPayload = Buffer.allocUnsafeSlow(payloadLength);
164
201
  applyMask(payloadBuf, maskKey, maskedPayload, 0);
165
202
  return [header, maskedPayload];
166
203
  }
167
204
 
168
205
  const maskKeyLength = mask ? 4 : 0;
169
- const frame = Buffer.allocUnsafe(headerLength + maskKeyLength + payloadLength);
206
+ const totalLen = headerLength + maskKeyLength + payloadLength;
207
+ const frame = Buffer.allocUnsafe(totalLen);
170
208
 
171
209
  // Byte 0: FIN (bit 7), RSV1-3 (bits 6-4 = 0), Opcode (bits 3-0)
172
210
  frame[0] = (fin ? 0x80 : 0x00) | (opcode & 0x0f);
@@ -364,7 +402,6 @@ class FrameParser {
364
402
  let payload;
365
403
  if (masked) {
366
404
  const maskKey = chunk.subarray(offset + headerSize, payloadStart);
367
- // Copy the 4 mask bytes into local primitives before in-place unmasking
368
405
  payload = applyMask(rawPayload, maskKey, rawPayload, 0);
369
406
  } else {
370
407
  payload = rawPayload;
@@ -391,9 +428,39 @@ class FrameParser {
391
428
  }
392
429
  }
393
430
 
431
+ /**
432
+ * Pre-encodes a server-side (unmasked) WebSocket message into a reusable RFC 6455 frame Buffer.
433
+ * Ideal for high-frequency broadcasting or sending the same static/cached payload to thousands of clients.
434
+ *
435
+ * @param {string|Buffer|Uint8Array} data
436
+ * @param {object} [options]
437
+ * @param {boolean} [options.binary]
438
+ * @returns {Buffer}
439
+ */
440
+ function prepareMessage(data, options = {}) {
441
+ const isBinary =
442
+ options.binary ?? (Buffer.isBuffer(data) || ArrayBuffer.isView(data));
443
+ const opcode = isBinary ? OPCODES.BINARY : OPCODES.TEXT;
444
+ const payload =
445
+ typeof data === 'string' || Buffer.isBuffer(data)
446
+ ? data
447
+ : ArrayBuffer.isView(data)
448
+ ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
449
+ : String(data);
450
+
451
+ return encodeFrame({
452
+ opcode,
453
+ payload,
454
+ fin: true,
455
+ mask: false,
456
+ returnParts: false,
457
+ });
458
+ }
459
+
394
460
  module.exports = {
395
461
  WebSocketProtocolError,
396
462
  applyMask,
397
463
  encodeFrame,
464
+ prepareMessage,
398
465
  FrameParser,
399
466
  };
package/src/index.d.ts CHANGED
@@ -36,8 +36,11 @@ export const CLOSE_CODES: Readonly<{
36
36
 
37
37
  export interface SendOptions {
38
38
  binary?: boolean;
39
+ batch?: boolean;
39
40
  }
40
41
 
42
+ export function prepareMessage(data: string | Buffer | Uint8Array, options?: { binary?: boolean }): Buffer;
43
+
41
44
  export class WalterConnection extends EventEmitter {
42
45
  id: number;
43
46
  socket: net.Socket;
@@ -47,6 +50,8 @@ export class WalterConnection extends EventEmitter {
47
50
  readyState: 0 | 1 | 2 | 3;
48
51
  isAlive: boolean;
49
52
  protocol?: string | null;
53
+ rooms: Set<string>;
54
+ readonly bufferedAmount: number;
50
55
 
51
56
  constructor(
52
57
  socket: net.Socket,
@@ -57,12 +62,19 @@ export class WalterConnection extends EventEmitter {
57
62
  }
58
63
  );
59
64
 
65
+ join(roomName: string): this;
66
+ leave(roomName: string): this;
67
+ leaveAllRooms(): void;
68
+ isBackpressured(highWaterMark?: number): boolean;
60
69
  send(data: string | Buffer | Uint8Array, options?: SendOptions | ((err?: Error) => void), callback?: (err?: Error) => void): void;
70
+ sendFrame(preEncodedFrame: Buffer | Buffer[], callback?: (err?: Error) => void): void;
61
71
  sendJSON(obj: unknown, callback?: (err?: Error) => void): void;
62
72
  ping(data?: Buffer | string, callback?: (err?: Error) => void): void;
63
73
  pong(data?: Buffer | string, callback?: (err?: Error) => void): void;
64
74
  close(code?: number, reason?: string): void;
65
75
  terminate(): void;
76
+ addEventListener(type: string, listener: (event: any) => void, options?: { once?: boolean } | boolean): void;
77
+ removeEventListener(type: string, listener: (event: any) => void): void;
66
78
 
67
79
  on(event: 'message', listener: (data: string | Buffer, isBinary: boolean) => void): this;
68
80
  on(event: 'ping' | 'pong', listener: (data: Buffer) => void): this;
@@ -86,14 +98,31 @@ export interface WalterServerOptions {
86
98
  heartbeatInterval?: number;
87
99
  }
88
100
 
101
+ export interface BroadcastOptions {
102
+ except?: WalterConnection;
103
+ binary?: boolean;
104
+ maxBackpressure?: number;
105
+ }
106
+
89
107
  export class WalterServer extends EventEmitter {
90
108
  clients: Set<WalterConnection>;
109
+ rooms: Map<string, Set<WalterConnection>>;
91
110
  options: WalterServerOptions;
92
111
 
93
112
  constructor(options: WalterServerOptions);
94
113
  address(): net.AddressInfo | string | null;
95
114
  handleUpgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer): Promise<void>;
96
- broadcast(data: string | Buffer | Uint8Array, options?: { except?: WalterConnection; binary?: boolean }): void;
115
+ joinRoom(roomName: string, ws: WalterConnection): void;
116
+ leaveRoom(roomName: string, ws: WalterConnection): void;
117
+ prepareMessage(data: string | Buffer | Uint8Array, options?: { binary?: boolean }): Buffer;
118
+ to(roomName: string): {
119
+ broadcast: (data: string | Buffer | Uint8Array, options?: BroadcastOptions) => void;
120
+ broadcastJSON: (obj: unknown, options?: BroadcastOptions) => void;
121
+ size: () => number;
122
+ };
123
+ broadcastToRoom(roomName: string, data: string | Buffer | Uint8Array, options?: BroadcastOptions): void;
124
+ broadcast(data: string | Buffer | Uint8Array, options?: BroadcastOptions): void;
125
+ broadcastJSON(obj: unknown, options?: BroadcastOptions): void;
97
126
  close(callback?: (err?: Error) => void): void;
98
127
 
99
128
  on(event: 'connection', listener: (ws: WalterConnection, req: http.IncomingMessage) => void): this;
@@ -106,6 +135,11 @@ export interface WalterClientOptions {
106
135
  protocols?: string | string[];
107
136
  maxPayload?: number;
108
137
  handshakeTimeout?: number;
138
+ autoReconnect?: boolean;
139
+ reconnectInterval?: number;
140
+ maxReconnectInterval?: number;
141
+ maxReconnectAttempts?: number;
142
+ offlineQueue?: boolean;
109
143
  }
110
144
 
111
145
  export class WalterClient extends EventEmitter {
@@ -115,6 +149,7 @@ export class WalterClient extends EventEmitter {
115
149
  static WalterServer: typeof WalterServer;
116
150
  static WalterClient: typeof WalterClient;
117
151
  static WalterConnection: typeof WalterConnection;
152
+ static prepareMessage: typeof prepareMessage;
118
153
  static CONNECTING: 0;
119
154
  static OPEN: 1;
120
155
  static CLOSING: 2;
@@ -123,6 +158,10 @@ export class WalterClient extends EventEmitter {
123
158
  url: URL;
124
159
  readyState: 0 | 1 | 2 | 3;
125
160
  protocol: string | null;
161
+ onopen: ((event: { target: WalterClient }) => void) | null;
162
+ onmessage: ((event: { data: string | Buffer; isBinary: boolean; target: WalterClient }) => void) | null;
163
+ onclose: ((event: { code: number; reason: string; target: WalterClient }) => void) | null;
164
+ onerror: ((event: { error: Error; message: string; target: WalterClient }) => void) | null;
126
165
 
127
166
  constructor(url: string | URL, options?: WalterClientOptions);
128
167
  send(data: string | Buffer | Uint8Array, options?: SendOptions | ((err?: Error) => void), callback?: (err?: Error) => void): void;
@@ -131,6 +170,8 @@ export class WalterClient extends EventEmitter {
131
170
  pong(data?: Buffer | string, callback?: (err?: Error) => void): void;
132
171
  close(code?: number, reason?: string): void;
133
172
  terminate(): void;
173
+ addEventListener(type: string, listener: (event: any) => void, options?: { once?: boolean } | boolean): void;
174
+ removeEventListener(type: string, listener: (event: any) => void): void;
134
175
 
135
176
  on(event: 'open', listener: () => void): this;
136
177
  on(event: 'message', listener: (data: string | Buffer, isBinary: boolean) => void): this;
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  const { WalterServer, computeAcceptKey, isValidSecWebSocketKey } = require('./server');
4
4
  const { WalterClient } = require('./client');
5
5
  const { WalterConnection } = require('./connection');
6
- const { FrameParser, encodeFrame, applyMask, WebSocketProtocolError } = require('./frame');
6
+ const { FrameParser, encodeFrame, prepareMessage, applyMask, WebSocketProtocolError } = require('./frame');
7
7
  const { GUID, READY_STATES, OPCODES, CLOSE_CODES, isValidCloseCode } = require('./constants');
8
8
 
9
9
  // Drop-in compatibility with standard `ws` package (`const WebSocket = require('walter-ws')`)
@@ -15,6 +15,7 @@ WalterClient.WalterClient = WalterClient;
15
15
  WalterClient.WalterConnection = WalterConnection;
16
16
  WalterClient.FrameParser = FrameParser;
17
17
  WalterClient.encodeFrame = encodeFrame;
18
+ WalterClient.prepareMessage = prepareMessage;
18
19
  WalterClient.applyMask = applyMask;
19
20
  WalterClient.computeAcceptKey = computeAcceptKey;
20
21
  WalterClient.isValidSecWebSocketKey = isValidSecWebSocketKey;
package/src/index.mjs CHANGED
@@ -9,6 +9,7 @@ export const {
9
9
  WebSocketServer,
10
10
  FrameParser,
11
11
  encodeFrame,
12
+ prepareMessage,
12
13
  applyMask,
13
14
  computeAcceptKey,
14
15
  isValidSecWebSocketKey,