mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
refactor: add more typing info and upgrade prettier (#3725)
This upgrades prettier to 2.2.0 to gain support for TypeScript's new type-only-imports feature.
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { Decoder, Encoder, Packet, PacketType } from "socket.io-parser";
|
||||
import debugModule = require("debug");
|
||||
import { IncomingMessage } from "http";
|
||||
import { Server } from "./index";
|
||||
import { Socket } from "./socket";
|
||||
import { SocketId } from "socket.io-adapter";
|
||||
import { ParentNamespace } from "./parent-namespace";
|
||||
import type { IncomingMessage } from "http";
|
||||
import type { Namespace, Server } from "./index";
|
||||
import type { Socket } from "./socket";
|
||||
import type { SocketId } from "socket.io-adapter";
|
||||
|
||||
const debug = debugModule("socket.io:client");
|
||||
|
||||
@@ -17,16 +16,16 @@ export class Client {
|
||||
private readonly decoder: Decoder;
|
||||
private sockets: Map<SocketId, Socket> = new Map();
|
||||
private nsps: Map<string, Socket> = new Map();
|
||||
private connectTimeout: NodeJS.Timeout;
|
||||
private connectTimeout?: NodeJS.Timeout;
|
||||
|
||||
/**
|
||||
* Client constructor.
|
||||
*
|
||||
* @param {Server} server instance
|
||||
* @param {Socket} conn
|
||||
* @param server instance
|
||||
* @param conn
|
||||
* @package
|
||||
*/
|
||||
constructor(server: Server, conn) {
|
||||
constructor(server: Server, conn: Socket) {
|
||||
this.server = server;
|
||||
this.conn = conn;
|
||||
this.encoder = server.encoder;
|
||||
@@ -84,27 +83,31 @@ export class Client {
|
||||
return this.doConnect(name, auth);
|
||||
}
|
||||
|
||||
this.server._checkNamespace(name, auth, (dynamicNsp: ParentNamespace) => {
|
||||
if (dynamicNsp) {
|
||||
debug("dynamic namespace %s was created", dynamicNsp.name);
|
||||
this.doConnect(name, auth);
|
||||
} else {
|
||||
debug("creation of namespace %s was denied", name);
|
||||
this._packet({
|
||||
type: PacketType.CONNECT_ERROR,
|
||||
nsp: name,
|
||||
data: {
|
||||
message: "Invalid namespace"
|
||||
}
|
||||
});
|
||||
this.server._checkNamespace(
|
||||
name,
|
||||
auth,
|
||||
(dynamicNspName: Namespace | false) => {
|
||||
if (dynamicNspName) {
|
||||
debug("dynamic namespace %s was created", dynamicNspName);
|
||||
this.doConnect(name, auth);
|
||||
} else {
|
||||
debug("creation of namespace %s was denied", name);
|
||||
this._packet({
|
||||
type: PacketType.CONNECT_ERROR,
|
||||
nsp: name,
|
||||
data: {
|
||||
message: "Invalid namespace",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects a client to a namespace.
|
||||
*
|
||||
* @param {String} name - the namespace
|
||||
* @param name - the namespace
|
||||
* @param {Object} auth - the auth parameters
|
||||
*
|
||||
* @private
|
||||
@@ -112,7 +115,7 @@ export class Client {
|
||||
private doConnect(name: string, auth: object) {
|
||||
if (this.connectTimeout) {
|
||||
clearTimeout(this.connectTimeout);
|
||||
this.connectTimeout = null;
|
||||
this.connectTimeout = undefined;
|
||||
}
|
||||
const nsp = this.server.of(name);
|
||||
|
||||
@@ -142,7 +145,7 @@ export class Client {
|
||||
*/
|
||||
_remove(socket: Socket) {
|
||||
if (this.sockets.has(socket.id)) {
|
||||
const nsp = this.sockets.get(socket.id).nsp.name;
|
||||
const nsp = this.sockets.get(socket.id)!.nsp.name;
|
||||
this.sockets.delete(socket.id);
|
||||
this.nsps.delete(nsp);
|
||||
} else {
|
||||
@@ -221,7 +224,7 @@ export class Client {
|
||||
} else {
|
||||
const socket = this.nsps.get(packet.nsp);
|
||||
if (socket) {
|
||||
process.nextTick(function() {
|
||||
process.nextTick(function () {
|
||||
socket._onpacket(packet);
|
||||
});
|
||||
} else {
|
||||
|
||||
171
lib/index.ts
171
lib/index.ts
@@ -11,11 +11,11 @@ import { ExtendedError, Namespace } from "./namespace";
|
||||
import { ParentNamespace } from "./parent-namespace";
|
||||
import { Adapter, Room, SocketId } from "socket.io-adapter";
|
||||
import * as parser from "socket.io-parser";
|
||||
import { Encoder } from "socket.io-parser";
|
||||
import type { Encoder } from "socket.io-parser";
|
||||
import debugModule from "debug";
|
||||
import { Socket } from "./socket";
|
||||
import { CookieSerializeOptions } from "cookie";
|
||||
import { CorsOptions } from "cors";
|
||||
import type { CookieSerializeOptions } from "cookie";
|
||||
import type { CorsOptions } from "cors";
|
||||
|
||||
const debug = debugModule("socket.io:server");
|
||||
|
||||
@@ -23,6 +23,11 @@ const clientVersion = require("../package.json").version;
|
||||
const dotMapRegex = /\.map/;
|
||||
|
||||
type Transport = "polling" | "websocket";
|
||||
type ParentNspNameMatchFn = (
|
||||
name: string,
|
||||
query: object,
|
||||
fn: (err: Error | null, success: boolean) => void
|
||||
) => void;
|
||||
|
||||
interface EngineOptions {
|
||||
/**
|
||||
@@ -149,7 +154,7 @@ export class Server extends EventEmitter {
|
||||
public readonly sockets: Namespace;
|
||||
|
||||
/** @private */
|
||||
readonly _parser;
|
||||
readonly _parser: typeof parser;
|
||||
/** @private */
|
||||
readonly encoder: Encoder;
|
||||
|
||||
@@ -157,17 +162,8 @@ export class Server extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
_nsps: Map<string, Namespace> = new Map();
|
||||
private parentNsps: Map<
|
||||
| string
|
||||
| RegExp
|
||||
| ((
|
||||
name: string,
|
||||
query: object,
|
||||
fn: (err: Error, success: boolean) => void
|
||||
) => void),
|
||||
ParentNamespace
|
||||
> = new Map();
|
||||
private _adapter: any;
|
||||
private parentNsps: Map<ParentNspNameMatchFn, ParentNamespace> = new Map();
|
||||
private _adapter?: typeof Adapter;
|
||||
private _serveClient: boolean;
|
||||
private opts: Partial<EngineOptions>;
|
||||
private eio;
|
||||
@@ -184,18 +180,24 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Server constructor.
|
||||
*
|
||||
* @param {http.Server|Number|Object} srv http server, port or options
|
||||
* @param {Object} [opts]
|
||||
* @param srv http server, port, or options
|
||||
* @param [opts]
|
||||
* @public
|
||||
*/
|
||||
constructor(opts?: Partial<ServerOptions>);
|
||||
constructor(srv: http.Server, opts?: Partial<ServerOptions>);
|
||||
constructor(srv: number, opts?: Partial<ServerOptions>);
|
||||
constructor(srv?: any, opts: Partial<ServerOptions> = {}) {
|
||||
constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
|
||||
constructor(
|
||||
srv: undefined | Partial<ServerOptions> | http.Server | number,
|
||||
opts: Partial<ServerOptions> = {}
|
||||
) {
|
||||
super();
|
||||
if ("object" == typeof srv && srv instanceof Object && !srv.listen) {
|
||||
opts = srv;
|
||||
srv = null;
|
||||
if (
|
||||
"object" === typeof srv &&
|
||||
srv instanceof Object &&
|
||||
!(srv as Partial<http.Server>).listen
|
||||
) {
|
||||
opts = srv as Partial<ServerOptions>;
|
||||
srv = undefined;
|
||||
}
|
||||
this.path(opts.path || "/socket.io");
|
||||
this.connectTimeout(opts.connectTimeout || 45000);
|
||||
@@ -205,44 +207,44 @@ export class Server extends EventEmitter {
|
||||
this.adapter(opts.adapter || Adapter);
|
||||
this.sockets = this.of("/");
|
||||
this.opts = opts;
|
||||
if (srv) this.attach(srv);
|
||||
if (srv) this.attach(srv as http.Server);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets/gets whether client code is being served.
|
||||
*
|
||||
* @param {Boolean} v - whether to serve client code
|
||||
* @return {Server|Boolean} self when setting or value when getting
|
||||
* @param v - whether to serve client code
|
||||
* @return self when setting or value when getting
|
||||
* @public
|
||||
*/
|
||||
public serveClient(v: boolean): Server;
|
||||
public serveClient(): boolean;
|
||||
public serveClient(v?: boolean): Server | boolean {
|
||||
if (!arguments.length) return this._serveClient;
|
||||
this._serveClient = v;
|
||||
this._serveClient = v!;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the middleware for an incoming namespace not already created on the server.
|
||||
*
|
||||
* @param {String} name - name of incoming namespace
|
||||
* @param name - name of incoming namespace
|
||||
* @param {Object} auth - the auth parameters
|
||||
* @param {Function} fn - callback
|
||||
* @param fn - callback
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_checkNamespace(
|
||||
name: string,
|
||||
auth: object,
|
||||
fn: (nsp: Namespace | boolean) => void
|
||||
fn: (nsp: Namespace | false) => void
|
||||
) {
|
||||
if (this.parentNsps.size === 0) return fn(false);
|
||||
|
||||
const keysIterator = this.parentNsps.keys();
|
||||
|
||||
const run = () => {
|
||||
let nextFn = keysIterator.next();
|
||||
const nextFn = keysIterator.next();
|
||||
if (nextFn.done) {
|
||||
return fn(false);
|
||||
}
|
||||
@@ -250,7 +252,7 @@ export class Server extends EventEmitter {
|
||||
if (err || !allow) {
|
||||
run();
|
||||
} else {
|
||||
fn(this.parentNsps.get(nextFn.value).createChild(name));
|
||||
fn(this.parentNsps.get(nextFn.value)!.createChild(name));
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -270,7 +272,7 @@ export class Server extends EventEmitter {
|
||||
public path(v?: string): Server | string {
|
||||
if (!arguments.length) return this._path;
|
||||
|
||||
this._path = v.replace(/\/$/, "");
|
||||
this._path = v!.replace(/\/$/, "");
|
||||
|
||||
const escapedPath = this._path.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
this.clientPathRegex = new RegExp(
|
||||
@@ -297,13 +299,13 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Sets the adapter for rooms.
|
||||
*
|
||||
* @param {Adapter} v pathname
|
||||
* @return {Server|Adapter} self when setting or value when getting
|
||||
* @param v pathname
|
||||
* @return self when setting or value when getting
|
||||
* @public
|
||||
*/
|
||||
public adapter(): any;
|
||||
public adapter(v: any);
|
||||
public adapter(v?): Server | any {
|
||||
public adapter(): typeof Adapter | undefined;
|
||||
public adapter(v: typeof Adapter): Server;
|
||||
public adapter(v?: typeof Adapter): typeof Adapter | undefined | Server {
|
||||
if (!arguments.length) return this._adapter;
|
||||
this._adapter = v;
|
||||
for (const nsp of this._nsps.values()) {
|
||||
@@ -315,28 +317,30 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Attaches socket.io to a server or port.
|
||||
*
|
||||
* @param {http.Server|Number} srv - server or port
|
||||
* @param {Object} opts - options passed to engine.io
|
||||
* @return {Server} self
|
||||
* @param srv - server or port
|
||||
* @param opts - options passed to engine.io
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public listen(srv: http.Server, opts?: Partial<ServerOptions>): Server;
|
||||
public listen(srv: number, opts?: Partial<ServerOptions>): Server;
|
||||
public listen(srv: any, opts: Partial<ServerOptions> = {}): Server {
|
||||
public listen(
|
||||
srv: http.Server | number,
|
||||
opts: Partial<ServerOptions> = {}
|
||||
): Server {
|
||||
return this.attach(srv, opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches socket.io to a server or port.
|
||||
*
|
||||
* @param {http.Server|Number} srv - server or port
|
||||
* @param {Object} opts - options passed to engine.io
|
||||
* @return {Server} self
|
||||
* @param srv - server or port
|
||||
* @param opts - options passed to engine.io
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public attach(srv: http.Server, opts?: Partial<ServerOptions>): Server;
|
||||
public attach(port: number, opts?: Partial<ServerOptions>): Server;
|
||||
public attach(srv: any, opts: Partial<ServerOptions> = {}): Server {
|
||||
public attach(
|
||||
srv: http.Server | number,
|
||||
opts: Partial<ServerOptions> = {}
|
||||
): Server {
|
||||
if ("function" == typeof srv) {
|
||||
const msg =
|
||||
"You are trying to attach socket.io to an express " +
|
||||
@@ -394,10 +398,10 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Attaches the static file serving.
|
||||
*
|
||||
* @param {Function|http.Server} srv http server
|
||||
* @param srv http server
|
||||
* @private
|
||||
*/
|
||||
private attachServe(srv) {
|
||||
private attachServe(srv: http.Server) {
|
||||
debug("attaching client serving req handler");
|
||||
|
||||
const evs = srv.listeners("request").slice(0);
|
||||
@@ -416,12 +420,12 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Handles a request serving of client source and map
|
||||
*
|
||||
* @param {http.IncomingMessage} req
|
||||
* @param {http.ServerResponse} res
|
||||
* @param req
|
||||
* @param res
|
||||
* @private
|
||||
*/
|
||||
private serve(req: http.IncomingMessage, res: http.ServerResponse) {
|
||||
const filename = req.url.replace(this._path, "");
|
||||
const filename = req.url!.replace(this._path, "");
|
||||
const isMap = dotMapRegex.test(filename);
|
||||
const type = isMap ? "map" : "source";
|
||||
|
||||
@@ -470,7 +474,7 @@ export class Server extends EventEmitter {
|
||||
);
|
||||
const encoding = accepts(req).encodings(["br", "gzip", "deflate"]);
|
||||
|
||||
const onError = err => {
|
||||
const onError = (err: NodeJS.ErrnoException | null) => {
|
||||
if (err) {
|
||||
res.end();
|
||||
}
|
||||
@@ -500,7 +504,7 @@ export class Server extends EventEmitter {
|
||||
* Binds socket.io to an engine.io instance.
|
||||
*
|
||||
* @param {engine.Server} engine engine.io (or compatible) server
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public bind(engine): Server {
|
||||
@@ -513,7 +517,7 @@ export class Server extends EventEmitter {
|
||||
* Called with each incoming transport connection.
|
||||
*
|
||||
* @param {engine.Socket} conn
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @private
|
||||
*/
|
||||
private onconnection(conn): Server {
|
||||
@@ -526,18 +530,11 @@ export class Server extends EventEmitter {
|
||||
* Looks up a namespace.
|
||||
*
|
||||
* @param {String|RegExp|Function} name nsp name
|
||||
* @param {Function} [fn] optional, nsp `connection` ev handler
|
||||
* @param [fn] optional, nsp `connection` ev handler
|
||||
* @public
|
||||
*/
|
||||
public of(
|
||||
name:
|
||||
| string
|
||||
| RegExp
|
||||
| ((
|
||||
name: string,
|
||||
query: object,
|
||||
fn: (err: Error, success: boolean) => void
|
||||
) => void),
|
||||
name: string | RegExp | ParentNspNameMatchFn,
|
||||
fn?: (socket: Socket) => void
|
||||
) {
|
||||
if (typeof name === "function" || name instanceof RegExp) {
|
||||
@@ -573,7 +570,7 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Closes server connection
|
||||
*
|
||||
* @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed
|
||||
* @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
||||
* @public
|
||||
*/
|
||||
public close(fn?: (err?: Error) => void): void {
|
||||
@@ -593,7 +590,7 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Sets up namespace middleware.
|
||||
*
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public use(
|
||||
@@ -606,8 +603,8 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when emitting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Server} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public to(name: Room): Server {
|
||||
@@ -618,8 +615,8 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when emitting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Server} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public in(name: Room): Server {
|
||||
@@ -630,24 +627,22 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public send(...args): Server {
|
||||
args.unshift("message");
|
||||
this.sockets.emit.apply(this.sockets, args);
|
||||
public send(...args: readonly any[]): Server {
|
||||
this.sockets.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public write(...args): Server {
|
||||
args.unshift("message");
|
||||
this.sockets.emit.apply(this.sockets, args);
|
||||
public write(...args: readonly any[]): Server {
|
||||
this.sockets.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -663,8 +658,8 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Sets the compress flag.
|
||||
*
|
||||
* @param {Boolean} compress - if `true`, compresses the sending data
|
||||
* @return {Server} self
|
||||
* @param compress - if `true`, compresses the sending data
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public compress(compress: boolean): Server {
|
||||
@@ -677,7 +672,7 @@ export class Server extends EventEmitter {
|
||||
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
||||
* and is in the middle of a request-response cycle).
|
||||
*
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public get volatile(): Server {
|
||||
@@ -688,7 +683,7 @@ export class Server extends EventEmitter {
|
||||
/**
|
||||
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
||||
*
|
||||
* @return {Server} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public get local(): Server {
|
||||
@@ -701,14 +696,14 @@ export class Server extends EventEmitter {
|
||||
* Expose main namespace (/).
|
||||
*/
|
||||
|
||||
const emitterMethods = Object.keys(EventEmitter.prototype).filter(function(
|
||||
const emitterMethods = Object.keys(EventEmitter.prototype).filter(function (
|
||||
key
|
||||
) {
|
||||
return typeof EventEmitter.prototype[key] === "function";
|
||||
});
|
||||
|
||||
emitterMethods.forEach(function(fn) {
|
||||
Server.prototype[fn] = function() {
|
||||
emitterMethods.forEach(function (fn) {
|
||||
Server.prototype[fn] = function () {
|
||||
return this.sockets[fn].apply(this.sockets, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Socket, RESERVED_EVENTS } from "./socket";
|
||||
import { Server } from "./index";
|
||||
import { Client } from "./client";
|
||||
import type { Server } from "./index";
|
||||
import type { Client } from "./client";
|
||||
import { EventEmitter } from "events";
|
||||
import { PacketType } from "socket.io-parser";
|
||||
import debugModule from "debug";
|
||||
import { Adapter, Room, SocketId } from "socket.io-adapter";
|
||||
import type { Adapter, Room, SocketId } from "socket.io-adapter";
|
||||
|
||||
const debug = debugModule("socket.io:namespace");
|
||||
|
||||
@@ -23,7 +23,7 @@ export class Namespace extends EventEmitter {
|
||||
|
||||
/** @private */
|
||||
_fns: Array<
|
||||
(socket: Socket, next: (err: ExtendedError) => void) => void
|
||||
(socket: Socket, next: (err?: ExtendedError) => void) => void
|
||||
> = [];
|
||||
|
||||
/** @private */
|
||||
@@ -38,8 +38,8 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Namespace constructor.
|
||||
*
|
||||
* @param {Server} server instance
|
||||
* @param {string} name
|
||||
* @param server instance
|
||||
* @param name
|
||||
*/
|
||||
constructor(server: Server, name: string) {
|
||||
super();
|
||||
@@ -56,13 +56,13 @@ export class Namespace extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
_initAdapter(): void {
|
||||
this.adapter = new (this.server.adapter())(this);
|
||||
this.adapter = new (this.server.adapter()!)(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up namespace middleware.
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public use(
|
||||
@@ -75,16 +75,16 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Executes the middleware for an incoming client.
|
||||
*
|
||||
* @param {Socket} socket - the socket that will get added
|
||||
* @param {Function} fn - last fn call in the middleware
|
||||
* @param socket - the socket that will get added
|
||||
* @param fn - last fn call in the middleware
|
||||
* @private
|
||||
*/
|
||||
private run(socket: Socket, fn: (err: ExtendedError) => void) {
|
||||
private run(socket: Socket, fn: (err: ExtendedError | null) => void) {
|
||||
const fns = this._fns.slice(0);
|
||||
if (!fns.length) return fn(null);
|
||||
|
||||
function run(i) {
|
||||
fns[i](socket, function(err) {
|
||||
function run(i: number) {
|
||||
fns[i](socket, function (err) {
|
||||
// upon error, short-circuit
|
||||
if (err) return fn(err);
|
||||
|
||||
@@ -102,8 +102,8 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when emitting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Namespace} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public to(name: Room): Namespace {
|
||||
@@ -114,8 +114,8 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when emitting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Namespace} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public in(name: Room): Namespace {
|
||||
@@ -132,13 +132,13 @@ export class Namespace extends EventEmitter {
|
||||
_add(client: Client, query, fn?: () => void): Socket {
|
||||
debug("adding socket to nsp %s", this.name);
|
||||
const socket = new Socket(this, client, query);
|
||||
this.run(socket, err => {
|
||||
this.run(socket, (err) => {
|
||||
process.nextTick(() => {
|
||||
if ("open" == client.conn.readyState) {
|
||||
if (err)
|
||||
return socket._error({
|
||||
message: err.message,
|
||||
data: err.data
|
||||
data: err.data,
|
||||
});
|
||||
|
||||
// track socket
|
||||
@@ -178,10 +178,10 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Emits to all clients.
|
||||
*
|
||||
* @return {Boolean} Always true
|
||||
* @return Always true
|
||||
* @public
|
||||
*/
|
||||
public emit(ev: string, ...args: any[]): boolean {
|
||||
public emit(ev: string | Symbol, ...args: any[]): true {
|
||||
if (RESERVED_EVENTS.has(ev)) {
|
||||
throw new Error(`"${ev}" is a reserved event name`);
|
||||
}
|
||||
@@ -189,7 +189,7 @@ export class Namespace extends EventEmitter {
|
||||
args.unshift(ev);
|
||||
const packet = {
|
||||
type: PacketType.EVENT,
|
||||
data: args
|
||||
data: args,
|
||||
};
|
||||
|
||||
if ("function" == typeof args[args.length - 1]) {
|
||||
@@ -205,7 +205,7 @@ export class Namespace extends EventEmitter {
|
||||
|
||||
this.adapter.broadcast(packet, {
|
||||
rooms: rooms,
|
||||
flags: flags
|
||||
flags: flags,
|
||||
});
|
||||
|
||||
return true;
|
||||
@@ -214,31 +214,29 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public send(...args): Namespace {
|
||||
args.unshift("message");
|
||||
this.emit.apply(this, args);
|
||||
public send(...args: readonly any[]): Namespace {
|
||||
this.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public write(...args): Namespace {
|
||||
args.unshift("message");
|
||||
this.emit.apply(this, args);
|
||||
public write(...args: readonly any[]): Namespace {
|
||||
this.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of clients.
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public allSockets(): Promise<Set<SocketId>> {
|
||||
@@ -255,8 +253,8 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Sets the compress flag.
|
||||
*
|
||||
* @param {Boolean} compress - if `true`, compresses the sending data
|
||||
* @return {Namespace} self
|
||||
* @param compress - if `true`, compresses the sending data
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public compress(compress: boolean): Namespace {
|
||||
@@ -269,7 +267,7 @@ export class Namespace extends EventEmitter {
|
||||
* receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
||||
* and is in the middle of a request-response cycle).
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public get volatile(): Namespace {
|
||||
@@ -280,7 +278,7 @@ export class Namespace extends EventEmitter {
|
||||
/**
|
||||
* Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
||||
*
|
||||
* @return {Namespace} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public get local(): Namespace {
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
import { Namespace } from "./namespace";
|
||||
import type { Server } from "./index";
|
||||
|
||||
export class ParentNamespace extends Namespace {
|
||||
private static count: number = 0;
|
||||
private children: Set<Namespace> = new Set();
|
||||
|
||||
constructor(server) {
|
||||
constructor(server: Server) {
|
||||
super(server, "/_" + ParentNamespace.count++);
|
||||
}
|
||||
|
||||
_initAdapter() {}
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_initAdapter(): void {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
public emit(...args: any[]): boolean {
|
||||
this.children.forEach(nsp => {
|
||||
public emit(ev: string | Symbol, ...args: [...any]): true {
|
||||
this.children.forEach((nsp) => {
|
||||
nsp._rooms = this._rooms;
|
||||
nsp._flags = this._flags;
|
||||
nsp.emit.apply(nsp, args);
|
||||
nsp.emit(ev, ...args);
|
||||
});
|
||||
this._rooms.clear();
|
||||
this._flags = {};
|
||||
@@ -22,16 +28,14 @@ export class ParentNamespace extends Namespace {
|
||||
return true;
|
||||
}
|
||||
|
||||
createChild(name) {
|
||||
createChild(name: string): Namespace {
|
||||
const namespace = new Namespace(this.server, name);
|
||||
namespace._fns = this._fns.slice(0);
|
||||
this.listeners("connect").forEach(listener =>
|
||||
// @ts-ignore
|
||||
namespace.on("connect", listener)
|
||||
this.listeners("connect").forEach((listener) =>
|
||||
namespace.on("connect", listener as (...args: any[]) => void)
|
||||
);
|
||||
this.listeners("connection").forEach(listener =>
|
||||
// @ts-ignore
|
||||
namespace.on("connection", listener)
|
||||
this.listeners("connection").forEach((listener) =>
|
||||
namespace.on("connection", listener as (...args: any[]) => void)
|
||||
);
|
||||
this.children.add(namespace);
|
||||
this.server._nsps.set(name, namespace);
|
||||
|
||||
@@ -2,24 +2,29 @@ import { EventEmitter } from "events";
|
||||
import { PacketType } from "socket.io-parser";
|
||||
import url = require("url");
|
||||
import debugModule from "debug";
|
||||
import { Server } from "./index";
|
||||
import { Client } from "./client";
|
||||
import { Namespace } from "./namespace";
|
||||
import { IncomingMessage } from "http";
|
||||
import { Adapter, BroadcastFlags, Room, SocketId } from "socket.io-adapter";
|
||||
import type { Server } from "./index";
|
||||
import type { Client } from "./client";
|
||||
import type { Namespace } from "./namespace";
|
||||
import type { IncomingMessage } from "http";
|
||||
import type {
|
||||
Adapter,
|
||||
BroadcastFlags,
|
||||
Room,
|
||||
SocketId,
|
||||
} from "socket.io-adapter";
|
||||
import base64id from "base64id";
|
||||
|
||||
const debug = debugModule("socket.io:socket");
|
||||
|
||||
export const RESERVED_EVENTS = new Set([
|
||||
export const RESERVED_EVENTS = new Set(<const>[
|
||||
"connect",
|
||||
"connect_error",
|
||||
"disconnect",
|
||||
"disconnecting",
|
||||
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
|
||||
"newListener",
|
||||
"removeListener"
|
||||
]);
|
||||
"removeListener",
|
||||
]) as ReadonlySet<string | Symbol>;
|
||||
|
||||
/**
|
||||
* The handshake details
|
||||
@@ -86,7 +91,7 @@ export class Socket extends EventEmitter {
|
||||
> = [];
|
||||
private flags: BroadcastFlags = {};
|
||||
private _rooms: Set<Room> = new Set();
|
||||
private _anyListeners: Array<(...args: any[]) => void>;
|
||||
private _anyListeners?: Array<(...args: any[]) => void>;
|
||||
|
||||
/**
|
||||
* Interface to a `Client` for a given `Namespace`.
|
||||
@@ -120,16 +125,16 @@ export class Socket extends EventEmitter {
|
||||
// @ts-ignore
|
||||
secure: !!this.request.connection.encrypted,
|
||||
issued: +new Date(),
|
||||
url: this.request.url,
|
||||
query: url.parse(this.request.url, true).query,
|
||||
auth
|
||||
url: this.request.url!,
|
||||
query: url.parse(this.request.url!, true).query,
|
||||
auth,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits to this client.
|
||||
*
|
||||
* @return {Boolean} Always true
|
||||
* @return Always returns `true`.
|
||||
* @public
|
||||
*/
|
||||
public emit(ev: string, ...args: any[]): boolean {
|
||||
@@ -139,7 +144,7 @@ export class Socket extends EventEmitter {
|
||||
args.unshift(ev);
|
||||
const packet: any = {
|
||||
type: PacketType.EVENT,
|
||||
data: args
|
||||
data: args,
|
||||
};
|
||||
|
||||
// access last argument to see if it's an ACK callback
|
||||
@@ -164,7 +169,7 @@ export class Socket extends EventEmitter {
|
||||
this.adapter.broadcast(packet, {
|
||||
except: new Set([this.id]),
|
||||
rooms: rooms,
|
||||
flags: flags
|
||||
flags: flags,
|
||||
});
|
||||
} else {
|
||||
// dispatch packet
|
||||
@@ -176,11 +181,11 @@ export class Socket extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when broadcasting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Socket} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public to(name: Room) {
|
||||
public to(name: Room): Socket {
|
||||
this._rooms.add(name);
|
||||
return this;
|
||||
}
|
||||
@@ -188,8 +193,8 @@ export class Socket extends EventEmitter {
|
||||
/**
|
||||
* Targets a room when broadcasting.
|
||||
*
|
||||
* @param {String} name
|
||||
* @return {Socket} self
|
||||
* @param name
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public in(name: Room): Socket {
|
||||
@@ -200,24 +205,22 @@ export class Socket extends EventEmitter {
|
||||
/**
|
||||
* Sends a `message` event.
|
||||
*
|
||||
* @return {Socket} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public send(...args): Socket {
|
||||
args.unshift("message");
|
||||
this.emit.apply(this, args);
|
||||
public send(...args: readonly any[]): Socket {
|
||||
this.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a `message` event.
|
||||
*
|
||||
* @return {Socket} self
|
||||
* @return self
|
||||
* @public
|
||||
*/
|
||||
public write(...args): Socket {
|
||||
args.unshift("message");
|
||||
this.emit.apply(this, args);
|
||||
public write(...args: readonly any[]): Socket {
|
||||
this.emit("message", ...args);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -353,7 +356,7 @@ export class Socket extends EventEmitter {
|
||||
private ack(id: number) {
|
||||
const self = this;
|
||||
let sent = false;
|
||||
return function() {
|
||||
return function () {
|
||||
// prevent double callbacks
|
||||
if (sent) return;
|
||||
const args = Array.prototype.slice.call(arguments);
|
||||
@@ -362,7 +365,7 @@ export class Socket extends EventEmitter {
|
||||
self.packet({
|
||||
id: id,
|
||||
type: PacketType.ACK,
|
||||
data: args
|
||||
data: args,
|
||||
});
|
||||
|
||||
sent = true;
|
||||
@@ -417,7 +420,7 @@ export class Socket extends EventEmitter {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_onclose(reason: string) {
|
||||
_onclose(reason: string): Socket | undefined {
|
||||
if (!this.connected) return this;
|
||||
debug("closing socket - reason %s", reason);
|
||||
super.emit("disconnecting", reason);
|
||||
@@ -427,6 +430,7 @@ export class Socket extends EventEmitter {
|
||||
this.connected = false;
|
||||
this.disconnected = true;
|
||||
super.emit("disconnect", reason);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user