Skip to content

Commit

Permalink
chore: bump socket.io-adapter
Browse files Browse the repository at this point in the history
Breaking changes:

- Namespace#connected is now a Map instead of an object.

- Namespace#clients() is renamed to Namespace#allSockets() and now
returns a Promise

Diff: socketio/socket.io-adapter@1.1.2...2.0.0
  • Loading branch information
darrachequesne committed Sep 25, 2020
1 parent 2464de7 commit 84437dc
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 2,502 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Client } from "./client";
import { EventEmitter } from "events";
import { Namespace } from "./namespace";
import { ParentNamespace } from "./parent-namespace";
import Adapter from "socket.io-adapter";
import { Adapter } from "socket.io-adapter";
import parser from "socket.io-parser";
import url from "url";
import debugModule from "debug";
Expand Down
29 changes: 14 additions & 15 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EventEmitter } from "events";
import parser from "socket.io-parser";
import hasBin from "has-binary2";
import debugModule from "debug";
import { Adapter, Room, SocketId } from "socket.io-adapter";

const debug = debugModule("socket.io:namespace");

Expand All @@ -20,16 +21,16 @@ const events = [

export class Namespace extends EventEmitter {
public readonly name: string;
public readonly connected: object = {};
public readonly connected: Map<SocketId, Socket> = new Map();

public adapter;
public adapter: Adapter;

/** @package */
public readonly server;
/** @package */
public fns: Array<(socket: Socket, next: (err: Error) => void) => void> = [];
/** @package */
public rooms: Array<string> = [];
public rooms: Set<Room> = new Set();
/** @package */
public flags: any = {};
/** @package */
Expand Down Expand Up @@ -107,8 +108,8 @@ export class Namespace extends EventEmitter {
* @param {String} name
* @return {Namespace} self
*/
public to(name: string): Namespace {
if (!~this.rooms.indexOf(name)) this.rooms.push(name);
public to(name: Room): Namespace {
this.rooms.add(name);
return this;
}

Expand All @@ -118,8 +119,8 @@ export class Namespace extends EventEmitter {
* @param {String} name
* @return {Namespace} self
*/
public in(name: string): Namespace {
if (!~this.rooms.indexOf(name)) this.rooms.push(name);
public in(name: Room): Namespace {
this.rooms.add(name);
return this;
}

Expand Down Expand Up @@ -197,11 +198,11 @@ export class Namespace extends EventEmitter {
throw new Error("Callbacks are not supported when broadcasting");
}

const rooms = this.rooms.slice(0);
const rooms = new Set(this.rooms);
const flags = Object.assign({}, this.flags);

// reset flags
this.rooms = [];
this.rooms.clear();
this.flags = {};

this.adapter.broadcast(packet, {
Expand Down Expand Up @@ -239,17 +240,15 @@ export class Namespace extends EventEmitter {
*
* @return {Namespace} self
*/
public clients(fn: (clients: Array<string>) => void): Namespace {
public allSockets(): Promise<Set<SocketId>> {
if (!this.adapter) {
throw new Error(
"No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?"
);
}
this.adapter.clients(this.rooms, fn);
// reset rooms for scenario:
// .in('room').clients() (GH-1978)
this.rooms = [];
return this;
const rooms = new Set(this.rooms);
this.rooms.clear();
return this.adapter.sockets(rooms);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/parent-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ParentNamespace extends Namespace {
nsp.flags = this.flags;
nsp.emit.apply(nsp, args);
});
this.rooms = [];
this.rooms.clear();
this.flags = {};
}

Expand Down
62 changes: 30 additions & 32 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import url from "url";
import debugModule from "debug";
import { Client, Namespace, Server } from "./index";
import { IncomingMessage } from "http";
import { Adapter, BroadcastFlags, Room, SocketId } from "socket.io-adapter";

const debug = debugModule("socket.io:socket");

Expand Down Expand Up @@ -67,21 +68,21 @@ export interface Handshake {
}

export class Socket extends EventEmitter {
public readonly id: string;
public readonly id: SocketId;
public readonly handshake: Handshake;

public rooms = {};
public connected: boolean;
public disconnected: boolean;

private readonly server: Server;
private readonly adapter;
private readonly adapter: Adapter;
private acks: object = {};
private fns: Array<
(event: Array<any>, next: (err: Error) => void) => void
> = [];
private flags: any = {};
private _rooms: Array<string> = [];
private flags: BroadcastFlags = {};
private _rooms: Set<Room> = new Set();

/**
* Interface to a `Client` for a given `Namespace`.
Expand Down Expand Up @@ -149,7 +150,7 @@ export class Socket extends EventEmitter {

// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === "function") {
if (this._rooms.length || this.flags.broadcast) {
if (this._rooms.size || this.flags.broadcast) {
throw new Error("Callbacks are not supported when broadcasting");
}

Expand All @@ -158,16 +159,16 @@ export class Socket extends EventEmitter {
packet.id = this.nsp.ids++;
}

const rooms = this._rooms.slice(0);
const rooms = new Set(this._rooms);
const flags = Object.assign({}, this.flags);

// reset flags
this._rooms = [];
this._rooms.clear();
this.flags = {};

if (rooms.length || flags.broadcast) {
if (rooms.size || flags.broadcast) {
this.adapter.broadcast(packet, {
except: [this.id],
except: new Set([this.id]),
rooms: rooms,
flags: flags
});
Expand All @@ -184,8 +185,8 @@ export class Socket extends EventEmitter {
* @param {String} name
* @return {Socket} self
*/
public to(name: string) {
if (!~this._rooms.indexOf(name)) this._rooms.push(name);
public to(name: Room) {
this._rooms.add(name);
return this;
}

Expand All @@ -195,8 +196,8 @@ export class Socket extends EventEmitter {
* @param {String} name
* @return {Socket} self
*/
public in(name: string): Socket {
if (!~this._rooms.indexOf(name)) this._rooms.push(name);
public in(name: Room): Socket {
this._rooms.add(name);
return this;
}

Expand Down Expand Up @@ -243,25 +244,23 @@ export class Socket extends EventEmitter {
*/
public join(rooms, fn?: (err: Error) => void): Socket {
debug("joining room %s", rooms);
const self = this;

if (!Array.isArray(rooms)) {
rooms = [rooms];
}
rooms = rooms.filter(function(room) {
return !self.rooms.hasOwnProperty(room);
rooms = rooms.filter(room => {
return !this.rooms.hasOwnProperty(room);
});
if (!rooms.length) {
fn && fn(null);
return this;
}
this.adapter.addAll(this.id, rooms, function(err) {
if (err) return fn && fn(err);
debug("joined room %s", rooms);
rooms.forEach(function(room) {
self.rooms[room] = room;
});
fn && fn(null);
this.adapter.addAll(this.id, rooms);
debug("joined room %s", rooms);
rooms.forEach(room => {
this.rooms[room] = room;
});
fn && fn(null);
return this;
}

Expand All @@ -274,13 +273,12 @@ export class Socket extends EventEmitter {
*/
public leave(room: string, fn?: (err: Error) => void): Socket {
debug("leave room %s", room);
const self = this;
this.adapter.del(this.id, room, function(err) {
if (err) return fn && fn(err);
debug("left room %s", room);
delete self.rooms[room];
fn && fn(null);
});
this.adapter.del(this.id, room);

debug("left room %s", room);
delete this.rooms[room];
fn && fn(null);

return this;
}

Expand All @@ -302,7 +300,7 @@ export class Socket extends EventEmitter {
*/
public onconnect(): void {
debug("socket connected - writing packet");
this.nsp.connected[this.id] = this;
this.nsp.connected.set(this.id, this);
this.join(this.id);
const skip = this.nsp.name === "/" && this.nsp.fns.length === 0;
if (skip) {
Expand Down Expand Up @@ -436,7 +434,7 @@ export class Socket extends EventEmitter {
this.client.remove(this);
this.connected = false;
this.disconnected = true;
delete this.nsp.connected[this.id];
this.nsp.connected.delete(this.id);
super.emit("disconnect", reason);
}

Expand Down

0 comments on commit 84437dc

Please sign in to comment.