From 46717e7387c132654823aa309becebf120ebf3da Mon Sep 17 00:00:00 2001 From: Martin Acosta Date: Sun, 5 Nov 2023 14:04:29 -0300 Subject: [PATCH] improve uws types --- scripts/generate-dts.js | 9 -------- src/server.js | 12 +++++++++-- src/websocket-server.js | 29 ++++++++++++++++++------- types/server.d.ts | 12 +++++++---- types/websocket-server.d.ts | 42 +++++++++++++++++++------------------ 5 files changed, 61 insertions(+), 43 deletions(-) diff --git a/scripts/generate-dts.js b/scripts/generate-dts.js index 941d3ae..7fe8b22 100644 --- a/scripts/generate-dts.js +++ b/scripts/generate-dts.js @@ -8,12 +8,3 @@ fs.writeFileSync( 'types/plugin.d.ts', types + 'import "./fastify-overload.d.ts"' ) - -// for some reason tsc exclude uwebsocket.js module -for (const file of ['server.d.ts', 'websocket-server.d.ts']) { - const types = fs.readFileSync(`types/${file}`, 'utf-8') - fs.writeFileSync( - `types/${file}`, - types + 'import uws from "uWebSockets.js"' - ) -} diff --git a/src/server.js b/src/server.js index 2fd15e5..ad3bfa6 100644 --- a/src/server.js +++ b/src/server.js @@ -1,7 +1,15 @@ +/** + * @typedef {import('uWebSockets.js').TemplatedApp} TemplatedApp + */ + +/** + * @typedef {import('uWebSockets.js').SSLApp} SSLApp + */ + /** * @typedef {{ * connectionTimeout?: number - * https?: { key: string, cert: string } | Parameters[0] + * https?: { key: string, cert: string } | Parameters[0] * }} ServerOptions */ @@ -204,7 +212,7 @@ export const serverFactory = (handler, opts) => { /** * * @param {import('fastify').FastifyInstance} fastify - * @returns {uws.TemplatedApp} + * @returns {TemplatedApp} */ export const getUws = (fastify) => { const { server } = fastify diff --git a/src/websocket-server.js b/src/websocket-server.js index c60e6be..d059583 100644 --- a/src/websocket-server.js +++ b/src/websocket-server.js @@ -1,9 +1,22 @@ /** - * @typedef {{ req: Request, handler: (ws: uws.WebSocket) => void }} UserData + * @template T + * @typedef {import('uWebSockets.js').WebSocket} UWebSocket */ /** - * @typedef {uws.WebSocket & { req: Request, websocket: WebSocket }} UWSocket + * @typedef {import('uWebSockets.js').TemplatedApp} TemplatedApp + */ + +/** + * @typedef {import('uWebSockets.js').RecognizedString} RecognizedString + */ + +/** + * @typedef {{ req: Request, handler: (ws: UWebSocket) => void }} UserData + */ + +/** + * @typedef {UWebSocket & { req: Request, websocket: WebSocket }} UWSocket */ /** @@ -87,7 +100,7 @@ export class WebSocket extends EventEmitter { /** @type {Buffer} */ this.namespace = namespace - /** @type {uws.WebSocket} */ + /** @type {UWebSocket} */ this.connection = connection connection.websocket = this this.topics = topics // we maintain a cache of buffer topics @@ -108,7 +121,7 @@ export class WebSocket extends EventEmitter { } /** - * @param {uws.RecognizedString} message + * @param {RecognizedString} message * @param {boolean} [isBinary] * @param {boolean} [compress] */ @@ -119,7 +132,7 @@ export class WebSocket extends EventEmitter { /** * @param {Buffer | string} topic - * @param {uws.RecognizedString} message + * @param {RecognizedString} message * @param {boolean} [isBinary] * @param {boolean} [compress] */ @@ -165,7 +178,7 @@ export class WebSocket extends EventEmitter { /** * @param {number} [code] - * @param {uws.RecognizedString} [shortMessage] + * @param {RecognizedString} [shortMessage] */ end (code, shortMessage) { if (this[kEnded]) return @@ -187,7 +200,7 @@ export class WebSocket extends EventEmitter { } /** - * @param {uws.RecognizedString} message + * @param {RecognizedString} message */ ping (message) { if (this[kEnded]) return @@ -290,7 +303,7 @@ export class WebSocketServer extends EventEmitter { */ addServer (server) { const { options } = this - /** @type {uws.TemplatedApp} */ + /** @type {TemplatedApp} */ const app = server[kApp] const listenerHandler = server[kHandler] diff --git a/types/server.d.ts b/types/server.d.ts index d18fb5f..2dafd6d 100644 --- a/types/server.d.ts +++ b/types/server.d.ts @@ -38,7 +38,10 @@ export class Server { host: any; }): Promise; [kHandler]: (req: Request, res: Response) => void; - [kHttps]: any; + [kHttps]: boolean | import("uWebSockets.js").AppOptions | { + key: string; + cert: string; + }; /** @type {import('./websocket-server.js').WebSocketServer} */ [kWs]: import('./websocket-server.js').WebSocketServer; [kAddress]: any; @@ -48,14 +51,16 @@ export class Server { } /** @type {FastifyServerFactory} */ export const serverFactory: FastifyServerFactory; -export function getUws(fastify: import('fastify').FastifyInstance): uws.TemplatedApp; +export function getUws(fastify: import('fastify').FastifyInstance): TemplatedApp; export { WebSocketStream } from "./websocket-server.js"; +export type TemplatedApp = import('uWebSockets.js').TemplatedApp; +export type SSLApp = typeof import("uWebSockets.js").SSLApp; export type ServerOptions = { connectionTimeout?: number; https?: { key: string; cert: string; - } | Parameters[0]; + } | Parameters[0]; }; export type FastifyServerFactory = import('fastify').FastifyServerFactory; import { kListen } from './symbols.js'; @@ -69,4 +74,3 @@ import { kListenSocket } from './symbols.js'; import { kApp } from './symbols.js'; import { kClosed } from './symbols.js'; export { DEDICATED_COMPRESSOR_128KB, DEDICATED_COMPRESSOR_16KB, DEDICATED_COMPRESSOR_256KB, DEDICATED_COMPRESSOR_32KB, DEDICATED_COMPRESSOR_3KB, DEDICATED_COMPRESSOR_4KB, DEDICATED_COMPRESSOR_64KB, DEDICATED_COMPRESSOR_8KB, DEDICATED_DECOMPRESSOR, DEDICATED_DECOMPRESSOR_16KB, DEDICATED_DECOMPRESSOR_1KB, DEDICATED_DECOMPRESSOR_2KB, DEDICATED_DECOMPRESSOR_32KB, DEDICATED_DECOMPRESSOR_4KB, DEDICATED_DECOMPRESSOR_512B, DEDICATED_DECOMPRESSOR_8KB, DISABLED, SHARED_COMPRESSOR, SHARED_DECOMPRESSOR } from "uWebSockets.js"; -import uws from "uWebSockets.js" \ No newline at end of file diff --git a/types/websocket-server.d.ts b/types/websocket-server.d.ts index 86aff5e..129da9e 100644 --- a/types/websocket-server.d.ts +++ b/types/websocket-server.d.ts @@ -8,8 +8,8 @@ export class WebSocket { constructor(namespace: any, connection: any, topics?: {}); /** @type {Buffer} */ namespace: Buffer; - /** @type {uws.WebSocket} */ - connection: uws.WebSocket; + /** @type {UWebSocket} */ + connection: UWebSocket; topics: {}; get uws(): boolean; /** @@ -18,46 +18,46 @@ export class WebSocket { */ allocTopic(topic: Buffer | string): Buffer; /** - * @param {uws.RecognizedString} message + * @param {RecognizedString} message * @param {boolean} [isBinary] * @param {boolean} [compress] */ - send(message: uws.RecognizedString, isBinary?: boolean, compress?: boolean): any; + send(message: RecognizedString, isBinary?: boolean, compress?: boolean): number; /** * @param {Buffer | string} topic - * @param {uws.RecognizedString} message + * @param {RecognizedString} message * @param {boolean} [isBinary] * @param {boolean} [compress] */ - publish(topic: Buffer | string, message: uws.RecognizedString, isBinary?: boolean, compress?: boolean): any; + publish(topic: Buffer | string, message: RecognizedString, isBinary?: boolean, compress?: boolean): boolean; /** * @param {Buffer | string} topic */ - subscribe(topic: Buffer | string): any; + subscribe(topic: Buffer | string): boolean; /** * @param {Buffer | string} topic */ - unsubscribe(topic: Buffer | string): any; + unsubscribe(topic: Buffer | string): boolean; /** * @param {Buffer | string} topic */ - isSubscribed(topic: Buffer | string): any; - getTopics(): any; - close(): any; + isSubscribed(topic: Buffer | string): boolean; + getTopics(): string[]; + close(): void; /** * @param {number} [code] - * @param {uws.RecognizedString} [shortMessage] + * @param {RecognizedString} [shortMessage] */ - end(code?: number, shortMessage?: uws.RecognizedString): any; + end(code?: number, shortMessage?: RecognizedString): void; /** * @param {() => void} cb */ - cork(cb: () => void): any; - getBufferedAmount(): any; + cork(cb: () => void): import("uWebSockets.js").WebSocket; + getBufferedAmount(): number; /** - * @param {uws.RecognizedString} message + * @param {RecognizedString} message */ - ping(message: uws.RecognizedString): any; + ping(message: RecognizedString): number; /** * @template {keyof WebsocketEvent} T * @param {T} eventName @@ -146,11 +146,14 @@ export class WebSocketServer { */ once(eventName: T_1, listener: WebsocketServerEvent[T_1]): any; } +export type UWebSocket = import('uWebSockets.js').WebSocket; +export type TemplatedApp = import('uWebSockets.js').TemplatedApp; +export type RecognizedString = import('uWebSockets.js').RecognizedString; export type UserData = { req: Request; - handler: (ws: uws.WebSocket) => void; + handler: (ws: UWebSocket) => void; }; -export type UWSocket = uws.WebSocket & { +export type UWSocket = UWebSocket & { req: Request; websocket: WebSocket; }; @@ -181,4 +184,3 @@ export type WebsocketServerEvent = { import { kEnded } from './symbols.js'; import { Duplex } from 'streamx'; import { Request } from './request.js'; -import uws from "uWebSockets.js" \ No newline at end of file