@arcblock/ws 1.31.9 → 1.32.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.
@@ -110,7 +110,11 @@ declare class WsServer extends EventEmitter {
110
110
  pendingConnections: number;
111
111
  pendingIpConnections: Map<string, number>;
112
112
  broadcastEventName: string;
113
+ private _broadcastSubscribed;
114
+ private _onBroadcastEvent;
113
115
  constructor(opts?: WsServerOptions);
116
+ private _detachBroadcastListener;
117
+ close(): void;
114
118
  attach(server: http.Server): this;
115
119
  onConnect(request: http.IncomingMessage, socket: Duplex, head: Buffer): void;
116
120
  /**
@@ -205,6 +205,8 @@ const resolveClientIpFromHeaders = (headers, trustProxyHops, trustedIpHeader) =>
205
205
  var WsServer = class extends EventEmitter {
206
206
  constructor(opts = {}) {
207
207
  super();
208
+ this._broadcastSubscribed = false;
209
+ this._onBroadcastEvent = (data) => this._doBroadCast(data);
208
210
  this.pathname = opts.pathname;
209
211
  this.authenticate = opts.authenticate;
210
212
  this.hooks = Object.assign({}, defaultHooks, opts.hooks || {});
@@ -239,16 +241,38 @@ var WsServer = class extends EventEmitter {
239
241
  this.heartbeatTimer.unref?.();
240
242
  }
241
243
  this.broadcastEventName = opts.broadcastEventName || "@arcblock/ws:broadcast";
242
- eventHub.on(this.broadcastEventName, (data) => this._doBroadCast(data));
244
+ eventHub.on(this.broadcastEventName, this._onBroadcastEvent);
245
+ this._broadcastSubscribed = true;
246
+ }
247
+ _detachBroadcastListener() {
248
+ if (this._broadcastSubscribed) {
249
+ eventHub.off(this.broadcastEventName, this._onBroadcastEvent);
250
+ this._broadcastSubscribed = false;
251
+ }
252
+ }
253
+ close() {
254
+ if (this.heartbeatTimer) {
255
+ clearInterval(this.heartbeatTimer);
256
+ this.heartbeatTimer = void 0;
257
+ }
258
+ this._detachBroadcastListener();
259
+ this.wss.close();
243
260
  }
244
261
  attach(server) {
245
- server.on("upgrade", this.onConnect.bind(this));
262
+ if (!this._broadcastSubscribed) {
263
+ eventHub.on(this.broadcastEventName, this._onBroadcastEvent);
264
+ this._broadcastSubscribed = true;
265
+ }
266
+ const onUpgrade = this.onConnect.bind(this);
267
+ server.on("upgrade", onUpgrade);
246
268
  const origClose = server.close.bind(server);
247
269
  server.close = ((cb) => {
248
270
  if (this.heartbeatTimer) {
249
271
  clearInterval(this.heartbeatTimer);
250
272
  this.heartbeatTimer = void 0;
251
273
  }
274
+ this._detachBroadcastListener();
275
+ server.off("upgrade", onUpgrade);
252
276
  server.closeAllConnections?.();
253
277
  return origClose(cb);
254
278
  });
@@ -726,6 +750,7 @@ var WsServer = class extends EventEmitter {
726
750
  clearInterval(this.heartbeatTimer);
727
751
  this.heartbeatTimer = void 0;
728
752
  }
753
+ this._detachBroadcastListener();
729
754
  this.logger.debug("ws server onclose");
730
755
  this.emit("close");
731
756
  }
@@ -210,6 +210,8 @@ const resolveClientIpFromHeaders = (headers, trustProxyHops, trustedIpHeader) =>
210
210
  var WsServer = class extends node_events.default {
211
211
  constructor(opts = {}) {
212
212
  super();
213
+ this._broadcastSubscribed = false;
214
+ this._onBroadcastEvent = (data) => this._doBroadCast(data);
213
215
  this.pathname = opts.pathname;
214
216
  this.authenticate = opts.authenticate;
215
217
  this.hooks = Object.assign({}, defaultHooks, opts.hooks || {});
@@ -244,16 +246,38 @@ var WsServer = class extends node_events.default {
244
246
  this.heartbeatTimer.unref?.();
245
247
  }
246
248
  this.broadcastEventName = opts.broadcastEventName || "@arcblock/ws:broadcast";
247
- eventHub.on(this.broadcastEventName, (data) => this._doBroadCast(data));
249
+ eventHub.on(this.broadcastEventName, this._onBroadcastEvent);
250
+ this._broadcastSubscribed = true;
251
+ }
252
+ _detachBroadcastListener() {
253
+ if (this._broadcastSubscribed) {
254
+ eventHub.off(this.broadcastEventName, this._onBroadcastEvent);
255
+ this._broadcastSubscribed = false;
256
+ }
257
+ }
258
+ close() {
259
+ if (this.heartbeatTimer) {
260
+ clearInterval(this.heartbeatTimer);
261
+ this.heartbeatTimer = void 0;
262
+ }
263
+ this._detachBroadcastListener();
264
+ this.wss.close();
248
265
  }
249
266
  attach(server) {
250
- server.on("upgrade", this.onConnect.bind(this));
267
+ if (!this._broadcastSubscribed) {
268
+ eventHub.on(this.broadcastEventName, this._onBroadcastEvent);
269
+ this._broadcastSubscribed = true;
270
+ }
271
+ const onUpgrade = this.onConnect.bind(this);
272
+ server.on("upgrade", onUpgrade);
251
273
  const origClose = server.close.bind(server);
252
274
  server.close = ((cb) => {
253
275
  if (this.heartbeatTimer) {
254
276
  clearInterval(this.heartbeatTimer);
255
277
  this.heartbeatTimer = void 0;
256
278
  }
279
+ this._detachBroadcastListener();
280
+ server.off("upgrade", onUpgrade);
257
281
  server.closeAllConnections?.();
258
282
  return origClose(cb);
259
283
  });
@@ -731,6 +755,7 @@ var WsServer = class extends node_events.default {
731
755
  clearInterval(this.heartbeatTimer);
732
756
  this.heartbeatTimer = void 0;
733
757
  }
758
+ this._detachBroadcastListener();
734
759
  this.logger.debug("ws server onclose");
735
760
  this.emit("close");
736
761
  }
@@ -110,7 +110,11 @@ declare class WsServer extends EventEmitter {
110
110
  pendingConnections: number;
111
111
  pendingIpConnections: Map<string, number>;
112
112
  broadcastEventName: string;
113
+ private _broadcastSubscribed;
114
+ private _onBroadcastEvent;
113
115
  constructor(opts?: WsServerOptions);
116
+ private _detachBroadcastListener;
117
+ close(): void;
114
118
  attach(server: http.Server): this;
115
119
  onConnect(request: http.IncomingMessage, socket: Duplex, head: Buffer): void;
116
120
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ws",
3
- "version": "1.31.9",
3
+ "version": "1.32.0",
4
4
  "description": "OCAP Chain websocket server and client",
5
5
  "keywords": [
6
6
  "websocket"
@@ -75,7 +75,7 @@
75
75
  "url": "https://github.com/ArcBlock/blockchain/issues"
76
76
  },
77
77
  "dependencies": {
78
- "@arcblock/event-hub": "1.31.9",
78
+ "@arcblock/event-hub": "1.32.0",
79
79
  "debug": "^4.4.3",
80
80
  "eventemitter3": "^4.0.7",
81
81
  "lodash": "^4.17.23",