Socket
Socket
Sign inDemoInstall

socket.io

Package Overview
Dependencies
Maintainers
2
Versions
157
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

socket.io - npm Package Compare versions

Comparing version 3.1.2 to 4.0.0

dist/broadcast-operator.d.ts

16

CHANGELOG.md

@@ -0,1 +1,17 @@

# [4.0.0](https://github.com/socketio/socket.io/compare/3.1.2...4.0.0) (2021-03-10)
### Bug Fixes
* make io.to(...) immutable ([ac9e8ca](https://github.com/socketio/socket.io/commit/ac9e8ca6c71e00d4af45ee03f590fe56f3951186))
### Features
* add some utility methods ([b25495c](https://github.com/socketio/socket.io/commit/b25495c069031674da08e19aed68922c7c7a0e28))
* add support for typed events ([#3822](https://github.com/socketio/socket.io/issues/3822)) ([0107510](https://github.com/socketio/socket.io/commit/0107510ba8a0f148c78029d8be8919b350feb633))
* allow to exclude specific rooms when broadcasting ([#3789](https://github.com/socketio/socket.io/issues/3789)) ([7de2e87](https://github.com/socketio/socket.io/commit/7de2e87e888d849eb2dfc5e362af4c9e86044701))
* allow to pass an array to io.to(...) ([085d1de](https://github.com/socketio/socket.io/commit/085d1de9df909651de8b313cc6f9f253374b702e))
## [3.1.2](https://github.com/socketio/socket.io/compare/3.1.1...3.1.2) (2021-02-26)

@@ -2,0 +18,0 @@

7

dist/client.d.ts

@@ -5,4 +5,5 @@ /// <reference types="node" />

import type { Server } from "./index";
import type { EventsMap } from "./typed-events";
import type { Socket } from "./socket";
export declare class Client {
export declare class Client<ListenEvents extends EventsMap, EmitEvents extends EventsMap> {
readonly conn: any;

@@ -23,3 +24,3 @@ private readonly id;

*/
constructor(server: Server, conn: Socket);
constructor(server: Server<ListenEvents, EmitEvents>, conn: any);
/**

@@ -65,3 +66,3 @@ * @return the reference to the request that originated the Engine.IO connection

*/
_remove(socket: Socket): void;
_remove(socket: Socket<ListenEvents, EmitEvents>): void;
/**

@@ -68,0 +69,0 @@ * Closes the underlying connection.

/// <reference types="node" />
import http = require("http");
import { EventEmitter } from "events";
import { ExtendedError, Namespace } from "./namespace";
import { ExtendedError, Namespace, NamespaceReservedEventsMap } from "./namespace";
import { Adapter, Room, SocketId } from "socket.io-adapter";

@@ -11,2 +10,4 @@ import * as parser from "socket.io-parser";

import type { CorsOptions } from "cors";
import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
import { EventsMap, DefaultEventsMap, EventParams, StrictEventEmitter } from "./typed-events";
declare type Transport = "polling" | "websocket";

@@ -19,3 +20,3 @@ declare type ParentNspNameMatchFn = (name: string, auth: {

* how many ms without a pong packet to consider the connection closed
* @default 5000
* @default 20000
*/

@@ -67,6 +68,8 @@ pingTimeout: number;

* what WebSocket server implementation to use. Specified module must
* conform to the ws interface (see ws module api docs). Default value is ws.
* An alternative c++ addon is also available by installing uws module.
* conform to the ws interface (see ws module api docs).
* An alternative c++ addon is also available by installing eiows module.
*
* @default `require("ws").Server`
*/
wsEngine: string;
wsEngine: Function;
/**

@@ -138,4 +141,4 @@ * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.

}
export declare class Server extends EventEmitter {
readonly sockets: Namespace;
export declare class Server<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends StrictEventEmitter<{}, EmitEvents, NamespaceReservedEventsMap<ListenEvents, EmitEvents>> {
readonly sockets: Namespace<ListenEvents, EmitEvents>;
/** @private */

@@ -148,3 +151,3 @@ readonly _parser: typeof parser;

*/
_nsps: Map<string, Namespace>;
_nsps: Map<string, Namespace<ListenEvents, EmitEvents>>;
private parentNsps;

@@ -194,3 +197,3 @@ private _adapter?;

[key: string]: any;
}, fn: (nsp: Namespace | false) => void): void;
}, fn: (nsp: Namespace<ListenEvents, EmitEvents> | false) => void): void;
/**

@@ -295,3 +298,3 @@ * Sets the client serving path.

*/
of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket) => void): Namespace;
of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: Socket<ListenEvents, EmitEvents>) => void): Namespace<ListenEvents, EmitEvents>;
/**

@@ -310,14 +313,22 @@ * Closes server connection

*/
use(fn: (socket: Socket, next: (err?: ExtendedError) => void) => void): this;
use(fn: (socket: Socket<ListenEvents, EmitEvents>, next: (err?: ExtendedError) => void) => void): this;
/**
* Targets a room when emitting.
*
* @param name
* @param room
* @return self
* @public
*/
to(name: Room): this;
to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Targets a room when emitting.
*
* @param room
* @return self
* @public
*/
in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Excludes a room when emitting.
*
* @param name

@@ -327,3 +338,3 @@ * @return self

*/
in(name: Room): this;
except(name: Room | Room[]): Server<ListenEvents, EmitEvents>;
/**

@@ -335,3 +346,3 @@ * Sends a `message` event to all clients.

*/
send(...args: readonly any[]): this;
send(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -343,3 +354,3 @@ * Sends a `message` event to all clients.

*/
write(...args: readonly any[]): this;
write(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -358,3 +369,3 @@ * Gets a list of socket ids.

*/
compress(compress: boolean): this;
compress(compress: boolean): BroadcastOperator<EmitEvents>;
/**

@@ -368,3 +379,3 @@ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to

*/
get volatile(): this;
get volatile(): BroadcastOperator<EmitEvents>;
/**

@@ -376,4 +387,31 @@ * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

*/
get local(): this;
get local(): BroadcastOperator<EmitEvents>;
/**
* Returns the matching socket instances
*
* @public
*/
fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
/**
* Makes the matching socket instances join the specified rooms
*
* @param room
* @public
*/
socketsJoin(room: Room | Room[]): void;
/**
* Makes the matching socket instances leave the specified rooms
*
* @param room
* @public
*/
socketsLeave(room: Room | Room[]): void;
/**
* Makes the matching socket instances disconnect
*
* @param close - whether to close the underlying connection
* @public
*/
disconnectSockets(close?: boolean): void;
}
export { Socket, ServerOptions, Namespace };
export { Socket, ServerOptions, Namespace, BroadcastOperator, RemoteSocket };

@@ -43,6 +43,7 @@ "use strict";

Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_1.Socket; } });
const typed_events_1 = require("./typed-events");
const debug = debug_1.default("socket.io:server");
const clientVersion = require("../package.json").version;
const dotMapRegex = /\.map/;
class Server extends events_1.EventEmitter {
class Server extends typed_events_1.StrictEventEmitter {
constructor(srv, opts = {}) {

@@ -377,9 +378,8 @@ super();

*
* @param name
* @param room
* @return self
* @public
*/
to(name) {
this.sockets.to(name);
return this;
to(room) {
return this.sockets.to(room);
}

@@ -389,2 +389,12 @@ /**

*
* @param room
* @return self
* @public
*/
in(room) {
return this.sockets.in(room);
}
/**
* Excludes a room when emitting.
*
* @param name

@@ -394,4 +404,4 @@ * @return self

*/
in(name) {
this.sockets.in(name);
except(name) {
this.sockets.except(name);
return this;

@@ -435,4 +445,3 @@ }

compress(compress) {
this.sockets.compress(compress);
return this;
return this.sockets.compress(compress);
}

@@ -448,4 +457,3 @@ /**

get volatile() {
this.sockets.volatile;
return this;
return this.sockets.volatile;
}

@@ -459,5 +467,39 @@ /**

get local() {
this.sockets.local;
return this;
return this.sockets.local;
}
/**
* Returns the matching socket instances
*
* @public
*/
fetchSockets() {
return this.sockets.fetchSockets();
}
/**
* Makes the matching socket instances join the specified rooms
*
* @param room
* @public
*/
socketsJoin(room) {
return this.sockets.socketsJoin(room);
}
/**
* Makes the matching socket instances leave the specified rooms
*
* @param room
* @public
*/
socketsLeave(room) {
return this.sockets.socketsLeave(room);
}
/**
* Makes the matching socket instances disconnect
*
* @param close - whether to close the underlying connection
* @public
*/
disconnectSockets(close = false) {
return this.sockets.disconnectSockets(close);
}
}

@@ -464,0 +506,0 @@ exports.Server = Server;

@@ -1,23 +0,23 @@

/// <reference types="node" />
import { Socket } from "./socket";
import type { Server } from "./index";
import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
import type { Client } from "./client";
import { EventEmitter } from "events";
import type { Adapter, Room, SocketId } from "socket.io-adapter";
import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
export interface ExtendedError extends Error {
data?: any;
}
export declare class Namespace extends EventEmitter {
export interface NamespaceReservedEventsMap<ListenEvents extends EventsMap, EmitEvents extends EventsMap> {
connect: (socket: Socket<ListenEvents, EmitEvents>) => void;
connection: (socket: Socket<ListenEvents, EmitEvents>) => void;
}
export declare class Namespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends StrictEventEmitter<{}, EmitEvents, NamespaceReservedEventsMap<ListenEvents, EmitEvents>> {
readonly name: string;
readonly sockets: Map<SocketId, Socket>;
readonly sockets: Map<SocketId, Socket<ListenEvents, EmitEvents>>;
adapter: Adapter;
/** @private */
readonly server: Server;
readonly server: Server<ListenEvents, EmitEvents>;
/** @private */
_fns: Array<(socket: Socket, next: (err?: ExtendedError) => void) => void>;
_fns: Array<(socket: Socket<ListenEvents, EmitEvents>, next: (err?: ExtendedError) => void) => void>;
/** @private */
_rooms: Set<Room>;
/** @private */
_flags: any;
/** @private */
_ids: number;

@@ -30,3 +30,3 @@ /**

*/
constructor(server: Server, name: string);
constructor(server: Server<ListenEvents, EmitEvents>, name: string);
/**

@@ -46,3 +46,3 @@ * Initializes the `Adapter` for this nsp.

*/
use(fn: (socket: Socket, next: (err?: ExtendedError) => void) => void): this;
use(fn: (socket: Socket<ListenEvents, EmitEvents>, next: (err?: ExtendedError) => void) => void): this;
/**

@@ -59,16 +59,24 @@ * Executes the middleware for an incoming client.

*
* @param name
* @param room
* @return self
* @public
*/
to(name: Room): this;
to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Targets a room when emitting.
*
* @param name
* @param room
* @return self
* @public
*/
in(name: Room): this;
in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Excludes a room when emitting.
*
* @param room
* @return self
* @public
*/
except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Adds a new client.

@@ -79,3 +87,3 @@ *

*/
_add(client: Client, query: any, fn?: () => void): Socket;
_add(client: Client<ListenEvents, EmitEvents>, query: any, fn?: () => void): Socket<ListenEvents, EmitEvents>;
/**

@@ -86,3 +94,3 @@ * Removes a client. Called by each `Socket`.

*/
_remove(socket: Socket): void;
_remove(socket: Socket<ListenEvents, EmitEvents>): void;
/**

@@ -94,3 +102,3 @@ * Emits to all clients.

*/
emit(ev: string | Symbol, ...args: any[]): true;
emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
/**

@@ -102,3 +110,3 @@ * Sends a `message` event to all clients.

*/
send(...args: readonly any[]): this;
send(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -110,3 +118,3 @@ * Sends a `message` event to all clients.

*/
write(...args: readonly any[]): this;
write(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -126,3 +134,3 @@ * Gets a list of clients.

*/
compress(compress: boolean): this;
compress(compress: boolean): BroadcastOperator<EmitEvents>;
/**

@@ -136,3 +144,3 @@ * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to

*/
get volatile(): this;
get volatile(): BroadcastOperator<EmitEvents>;
/**

@@ -144,3 +152,30 @@ * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

*/
get local(): this;
get local(): BroadcastOperator<EmitEvents>;
/**
* Returns the matching socket instances
*
* @public
*/
fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>;
/**
* Makes the matching socket instances join the specified rooms
*
* @param room
* @public
*/
socketsJoin(room: Room | Room[]): void;
/**
* Makes the matching socket instances leave the specified rooms
*
* @param room
* @public
*/
socketsLeave(room: Room | Room[]): void;
/**
* Makes the matching socket instances disconnect
*
* @param close - whether to close the underlying connection
* @public
*/
disconnectSockets(close?: boolean): void;
}

@@ -8,7 +8,7 @@ "use strict";

const socket_1 = require("./socket");
const events_1 = require("events");
const socket_io_parser_1 = require("socket.io-parser");
const typed_events_1 = require("./typed-events");
const debug_1 = __importDefault(require("debug"));
const broadcast_operator_1 = require("./broadcast-operator");
const debug = debug_1.default("socket.io:namespace");
class Namespace extends events_1.EventEmitter {
class Namespace extends typed_events_1.StrictEventEmitter {
/**

@@ -26,6 +26,2 @@ * Namespace constructor.

/** @private */
this._rooms = new Set();
/** @private */
this._flags = {};
/** @private */
this._ids = 0;

@@ -84,9 +80,8 @@ this.server = server;

*
* @param name
* @param room
* @return self
* @public
*/
to(name) {
this._rooms.add(name);
return this;
to(room) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).to(room);
}

@@ -96,11 +91,20 @@ /**

*
* @param name
* @param room
* @return self
* @public
*/
in(name) {
this._rooms.add(name);
return this;
in(room) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).in(room);
}
/**
* Excludes a room when emitting.
*
* @param room
* @return self
* @public
*/
except(room) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).except(room);
}
/**
* Adds a new client.

@@ -138,4 +142,4 @@ *

// fire user-set events
super.emit("connect", socket);
super.emit("connection", socket);
this.emitReserved("connect", socket);
this.emitReserved("connection", socket);
}

@@ -169,24 +173,3 @@ else {

emit(ev, ...args) {
if (socket_1.RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
// set up packet object
args.unshift(ev);
const packet = {
type: socket_io_parser_1.PacketType.EVENT,
data: args,
};
if ("function" == typeof args[args.length - 1]) {
throw new Error("Callbacks are not supported when broadcasting");
}
const rooms = new Set(this._rooms);
const flags = Object.assign({}, this._flags);
// reset flags
this._rooms.clear();
this._flags = {};
this.adapter.broadcast(packet, {
rooms: rooms,
flags: flags,
});
return true;
return new broadcast_operator_1.BroadcastOperator(this.adapter).emit(ev, ...args);
}

@@ -220,8 +203,3 @@ /**

allSockets() {
if (!this.adapter) {
throw new Error("No adapter for this namespace, are you trying to get the list of clients of a dynamic namespace?");
}
const rooms = new Set(this._rooms);
this._rooms.clear();
return this.adapter.sockets(rooms);
return new broadcast_operator_1.BroadcastOperator(this.adapter).allSockets();
}

@@ -236,4 +214,3 @@ /**

compress(compress) {
this._flags.compress = compress;
return this;
return new broadcast_operator_1.BroadcastOperator(this.adapter).compress(compress);
}

@@ -249,4 +226,3 @@ /**

get volatile() {
this._flags.volatile = true;
return this;
return new broadcast_operator_1.BroadcastOperator(this.adapter).volatile;
}

@@ -260,6 +236,40 @@ /**

get local() {
this._flags.local = true;
return this;
return new broadcast_operator_1.BroadcastOperator(this.adapter).local;
}
/**
* Returns the matching socket instances
*
* @public
*/
fetchSockets() {
return new broadcast_operator_1.BroadcastOperator(this.adapter).fetchSockets();
}
/**
* Makes the matching socket instances join the specified rooms
*
* @param room
* @public
*/
socketsJoin(room) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).socketsJoin(room);
}
/**
* Makes the matching socket instances leave the specified rooms
*
* @param room
* @public
*/
socketsLeave(room) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).socketsLeave(room);
}
/**
* Makes the matching socket instances disconnect
*
* @param close - whether to close the underlying connection
* @public
*/
disconnectSockets(close = false) {
return new broadcast_operator_1.BroadcastOperator(this.adapter).disconnectSockets(close);
}
}
exports.Namespace = Namespace;
import { Namespace } from "./namespace";
import type { Server } from "./index";
export declare class ParentNamespace extends Namespace {
import type { EventParams, EventNames, EventsMap, DefaultEventsMap } from "./typed-events";
export declare class ParentNamespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends Namespace<ListenEvents, EmitEvents> {
private static count;
private children;
constructor(server: Server);
constructor(server: Server<ListenEvents, EmitEvents>);
/**

@@ -11,4 +12,4 @@ * @private

_initAdapter(): void;
emit(ev: string | Symbol, ...args: [...any]): true;
createChild(name: string): Namespace;
emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): true;
createChild(name: string): Namespace<ListenEvents, EmitEvents>;
}

@@ -14,12 +14,14 @@ "use strict";

_initAdapter() {
/* no-op */
const broadcast = (packet, opts) => {
this.children.forEach((nsp) => {
nsp.adapter.broadcast(packet, opts);
});
};
// @ts-ignore FIXME is there a way to declare an inner class in TypeScript?
this.adapter = { broadcast };
}
emit(ev, ...args) {
this.children.forEach((nsp) => {
nsp._rooms = this._rooms;
nsp._flags = this._flags;
nsp.emit(ev, ...args);
});
this._rooms.clear();
this._flags = {};
return true;

@@ -26,0 +28,0 @@ }

/// <reference types="node" />
import { EventEmitter } from "events";
import { Packet } from "socket.io-parser";
import { EventParams, EventNames, EventsMap, StrictEventEmitter, DefaultEventsMap } from "./typed-events";
import type { Client } from "./client";

@@ -9,2 +9,12 @@ import type { Namespace } from "./namespace";

import type { ParsedUrlQuery } from "querystring";
import { BroadcastOperator } from "./broadcast-operator";
export interface SocketReservedEventsMap {
disconnect: (reason: string) => void;
disconnecting: (reason: string) => void;
error: (err: Error) => void;
}
export interface EventEmitterReservedEventsMap {
newListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
removeListener: (eventName: string | Symbol, listener: (...args: any[]) => void) => void;
}
export declare const RESERVED_EVENTS: ReadonlySet<string | Symbol>;

@@ -54,7 +64,11 @@ /**

}
export declare class Socket extends EventEmitter {
readonly nsp: Namespace;
readonly client: Client;
export declare class Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> extends StrictEventEmitter<ListenEvents, EmitEvents, SocketReservedEventsMap> {
readonly nsp: Namespace<ListenEvents, EmitEvents>;
readonly client: Client<ListenEvents, EmitEvents>;
readonly id: SocketId;
readonly handshake: Handshake;
/**
* Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
*/
data: any;
connected: boolean;

@@ -67,3 +81,2 @@ disconnected: boolean;

private flags;
private _rooms;
private _anyListeners?;

@@ -78,3 +91,3 @@ /**

*/
constructor(nsp: Namespace, client: Client, auth: object);
constructor(nsp: Namespace<ListenEvents, EmitEvents>, client: Client<ListenEvents, EmitEvents>, auth: object);
/**

@@ -92,20 +105,28 @@ * Builds the `handshake` BC object

*/
emit(ev: string, ...args: any[]): boolean;
emit<Ev extends EventNames<EmitEvents>>(ev: Ev, ...args: EventParams<EmitEvents, Ev>): boolean;
/**
* Targets a room when broadcasting.
*
* @param name
* @param room
* @return self
* @public
*/
to(name: Room): this;
to(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Targets a room when broadcasting.
*
* @param name
* @param room
* @return self
* @public
*/
in(name: Room): this;
in(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Excludes a room when broadcasting.
*
* @param room
* @return self
* @public
*/
except(room: Room | Room[]): BroadcastOperator<EmitEvents>;
/**
* Sends a `message` event.

@@ -116,3 +137,3 @@ *

*/
send(...args: readonly any[]): this;
send(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -124,3 +145,3 @@ * Sends a `message` event.

*/
write(...args: readonly any[]): this;
write(...args: EventParams<EmitEvents, "message">): this;
/**

@@ -203,3 +224,3 @@ * Writes a packet.

*/
_onerror(err: any): void;
_onerror(err: Error): void;
/**

@@ -255,3 +276,3 @@ * Called upon closing. Called by `Client`.

*/
get broadcast(): this;
get broadcast(): BroadcastOperator<EmitEvents>;
/**

@@ -263,3 +284,3 @@ * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.

*/
get local(): this;
get local(): BroadcastOperator<EmitEvents>;
/**

@@ -279,3 +300,3 @@ * Dispatch incoming event to socket listeners.

*/
use(fn: (event: Array<any>, next: (err: Error) => void) => void): this;
use(fn: (event: Array<any>, next: (err?: Error) => void) => void): this;
/**

@@ -335,2 +356,3 @@ * Executes the middleware for an incoming event.

listenersAny(): ((...args: any[]) => void)[];
private newBroadcastOperator;
}

@@ -7,7 +7,8 @@ "use strict";

exports.Socket = exports.RESERVED_EVENTS = void 0;
const events_1 = require("events");
const socket_io_parser_1 = require("socket.io-parser");
const url = require("url");
const debug_1 = __importDefault(require("debug"));
const typed_events_1 = require("./typed-events");
const base64id_1 = __importDefault(require("base64id"));
const broadcast_operator_1 = require("./broadcast-operator");
const debug = debug_1.default("socket.io:socket");

@@ -19,7 +20,6 @@ exports.RESERVED_EVENTS = new Set([

"disconnecting",
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
"newListener",
"removeListener",
]);
class Socket extends events_1.EventEmitter {
class Socket extends typed_events_1.StrictEventEmitter {
/**

@@ -37,6 +37,9 @@ * Interface to a `Client` for a given `Namespace`.

this.client = client;
/**
* Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
*/
this.data = {};
this.acks = new Map();
this.fns = [];
this.flags = {};
this._rooms = new Set();
this.server = nsp.server;

@@ -84,32 +87,16 @@ this.adapter = this.nsp.adapter;

}
args.unshift(ev);
const data = [ev, ...args];
const packet = {
type: socket_io_parser_1.PacketType.EVENT,
data: args,
data: data,
};
// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === "function") {
if (this._rooms.size || this.flags.broadcast) {
throw new Error("Callbacks are not supported when broadcasting");
}
if (typeof data[data.length - 1] === "function") {
debug("emitting packet with ack id %d", this.nsp._ids);
this.acks.set(this.nsp._ids, args.pop());
this.acks.set(this.nsp._ids, data.pop());
packet.id = this.nsp._ids++;
}
const rooms = new Set(this._rooms);
const flags = Object.assign({}, this.flags);
// reset flags
this._rooms.clear();
this.flags = {};
if (rooms.size || flags.broadcast) {
this.adapter.broadcast(packet, {
except: new Set([this.id]),
rooms: rooms,
flags: flags,
});
}
else {
// dispatch packet
this.packet(packet, flags);
}
this.packet(packet, flags);
return true;

@@ -120,9 +107,8 @@ }

*
* @param name
* @param room
* @return self
* @public
*/
to(name) {
this._rooms.add(name);
return this;
to(room) {
return this.newBroadcastOperator().to(room);
}

@@ -132,11 +118,20 @@ /**

*
* @param name
* @param room
* @return self
* @public
*/
in(name) {
this._rooms.add(name);
return this;
in(room) {
return this.newBroadcastOperator().in(room);
}
/**
* Excludes a room when broadcasting.
*
* @param room
* @return self
* @public
*/
except(room) {
return this.newBroadcastOperator().except(room);
}
/**
* Sends a `message` event.

@@ -325,3 +320,3 @@ *

if (this.listeners("error").length) {
super.emit("error", err);
this.emitReserved("error", err);
}

@@ -345,3 +340,3 @@ else {

debug("closing socket - reason %s", reason);
super.emit("disconnecting", reason);
this.emitReserved("disconnecting", reason);
this.leaveAll();

@@ -352,3 +347,3 @@ this.nsp._remove(this);

this.disconnected = true;
super.emit("disconnect", reason);
this.emitReserved("disconnect", reason);
return;

@@ -417,4 +412,3 @@ }

get broadcast() {
this.flags.broadcast = true;
return this;
return this.newBroadcastOperator();
}

@@ -428,4 +422,3 @@ /**

get local() {
this.flags.local = true;
return this;
return this.newBroadcastOperator().local;
}

@@ -446,3 +439,3 @@ /**

if (this.connected) {
super.emit.apply(this, event);
super.emitUntyped.apply(this, event);
}

@@ -570,3 +563,8 @@ else {

}
newBroadcastOperator() {
const flags = Object.assign({}, this.flags);
this.flags = {};
return new broadcast_operator_1.BroadcastOperator(this.adapter, new Set(), new Set([this.id]), flags);
}
}
exports.Socket = Socket;
{
"name": "socket.io",
"version": "3.1.2",
"version": "4.0.0",
"description": "node.js realtime framework server",

@@ -40,3 +40,5 @@ "keywords": [

"compile": "rimraf ./dist && tsc",
"test": "npm run format:check && npm run compile && nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/socket.io.ts",
"test": "npm run format:check && npm run compile && npm run test:types && npm run test:unit",
"test:types": "tsd",
"test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/socket.io.ts",
"format:check": "prettier --check \"lib/**/*.ts\" \"test/**/*.ts\"",

@@ -53,4 +55,4 @@ "format:fix": "prettier --write \"lib/**/*.ts\" \"test/**/*.ts\"",

"debug": "~4.3.1",
"engine.io": "~4.1.0",
"socket.io-adapter": "~2.1.0",
"engine.io": "~5.0.0",
"socket.io-adapter": "~2.2.0",
"socket.io-parser": "~4.0.3"

@@ -68,3 +70,3 @@ },

"rimraf": "^3.0.2",
"socket.io-client": "3.1.2",
"socket.io-client": "4.0.0",
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",

@@ -74,2 +76,3 @@ "superagent": "^6.1.0",

"ts-node": "^9.0.0",
"tsd": "^0.14.0",
"typescript": "^4.1.2"

@@ -97,3 +100,6 @@ },

"node": ">=10.0.0"
},
"tsd": {
"directory": "test"
}
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc