From 27763ecbb2e5883cc047d48b157cb5ee73b566a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Sopy=C5=82o?= Date: Mon, 14 May 2018 10:50:50 +0200 Subject: [PATCH] [@types/socket.io] fix: socket.use (#24574) --- types/socket.io/index.d.ts | 22 ++++++++++++++++++++++ types/socket.io/socket.io-tests.ts | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/types/socket.io/index.d.ts b/types/socket.io/index.d.ts index d63effe544..6f7c0ef321 100644 --- a/types/socket.io/index.d.ts +++ b/types/socket.io/index.d.ts @@ -474,6 +474,21 @@ declare namespace SocketIO { compress( compress: boolean ): Namespace; } + interface Packet extends Array { + /** + * Event name + */ + [0]: string; + /** + * Packet data + */ + [1]: any; + /** + * Ack function + */ + [2]: (...args: any[]) => void; + } + /** * The socket, which handles our connection for a namespace. NOTE: while * we technically extend NodeJS.EventEmitter, we're not putting it here @@ -612,6 +627,13 @@ declare namespace SocketIO { */ in( room: string ): Socket; + /** + * Registers a middleware, which is a function that gets executed for every incoming Packet and receives as parameter the packet and a function to optionally defer execution to the next registered middleware. + * + * Errors passed to middleware callbacks are sent as special error packets to clients. + */ + use( fn: ( packet: Packet, next: (err?: any) => void ) => void ): Socket; + /** * Sends a 'message' event * @see emit( event, ...args ) diff --git a/types/socket.io/socket.io-tests.ts b/types/socket.io/socket.io-tests.ts index 7bfddb1271..60f0d70551 100644 --- a/types/socket.io/socket.io-tests.ts +++ b/types/socket.io/socket.io-tests.ts @@ -180,3 +180,12 @@ function testVolatileServerMessages() { var io = socketIO.listen(80); io.volatile.emit('volatile', 'Lost data'); } + +function testSocketUse() { + var io = socketIO.listen(80); + io.on('connection', (socket) => { + socket.use((packet, next) => { + console.log(packet); + }); + }); +} \ No newline at end of file