Skip to content

Commit

Permalink
improve uws types
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Nov 5, 2023
1 parent f88642c commit 46717e7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 43 deletions.
9 changes: 0 additions & 9 deletions scripts/generate-dts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
)
}
12 changes: 10 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -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<typeof uws.SSLApp>[0]
* https?: { key: string, cert: string } | Parameters<SSLApp>[0]
* }} ServerOptions
*/

Expand Down Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions src/websocket-server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
/**
* @typedef {{ req: Request, handler: (ws: uws.WebSocket<UserData>) => void }} UserData
* @template T
* @typedef {import('uWebSockets.js').WebSocket<T>} UWebSocket
*/

/**
* @typedef {uws.WebSocket<UserData> & { req: Request, websocket: WebSocket }} UWSocket
* @typedef {import('uWebSockets.js').TemplatedApp} TemplatedApp
*/

/**
* @typedef {import('uWebSockets.js').RecognizedString} RecognizedString
*/

/**
* @typedef {{ req: Request, handler: (ws: UWebSocket<UserData>) => void }} UserData
*/

/**
* @typedef {UWebSocket<UserData> & { req: Request, websocket: WebSocket }} UWSocket
*/

/**
Expand Down Expand Up @@ -87,7 +100,7 @@ export class WebSocket extends EventEmitter {

/** @type {Buffer} */
this.namespace = namespace
/** @type {uws.WebSocket<any>} */
/** @type {UWebSocket<any>} */
this.connection = connection
connection.websocket = this
this.topics = topics // we maintain a cache of buffer topics
Expand All @@ -108,7 +121,7 @@ export class WebSocket extends EventEmitter {
}

/**
* @param {uws.RecognizedString} message
* @param {RecognizedString} message
* @param {boolean} [isBinary]
* @param {boolean} [compress]
*/
Expand All @@ -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]
*/
Expand Down Expand Up @@ -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
Expand All @@ -187,7 +200,7 @@ export class WebSocket extends EventEmitter {
}

/**
* @param {uws.RecognizedString} message
* @param {RecognizedString} message
*/
ping (message) {
if (this[kEnded]) return
Expand Down Expand Up @@ -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]

Expand Down
12 changes: 8 additions & 4 deletions types/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export class Server {
host: any;
}): Promise<any>;
[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;
Expand All @@ -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<typeof uws.SSLApp>[0];
} | Parameters<SSLApp>[0];
};
export type FastifyServerFactory = import('fastify').FastifyServerFactory;
import { kListen } from './symbols.js';
Expand All @@ -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"
42 changes: 22 additions & 20 deletions types/websocket-server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class WebSocket {
constructor(namespace: any, connection: any, topics?: {});
/** @type {Buffer} */
namespace: Buffer;
/** @type {uws.WebSocket<any>} */
connection: uws.WebSocket<any>;
/** @type {UWebSocket<any>} */
connection: UWebSocket<any>;
topics: {};
get uws(): boolean;
/**
Expand All @@ -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<any>;
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
Expand Down Expand Up @@ -146,11 +146,14 @@ export class WebSocketServer {
*/
once<T_1 extends keyof WebsocketServerEvent>(eventName: T_1, listener: WebsocketServerEvent[T_1]): any;
}
export type UWebSocket<T> = import('uWebSockets.js').WebSocket<T>;
export type TemplatedApp = import('uWebSockets.js').TemplatedApp;
export type RecognizedString = import('uWebSockets.js').RecognizedString;
export type UserData = {
req: Request;
handler: (ws: uws.WebSocket<UserData>) => void;
handler: (ws: UWebSocket<UserData>) => void;
};
export type UWSocket = uws.WebSocket<UserData> & {
export type UWSocket = UWebSocket<UserData> & {
req: Request;
websocket: WebSocket;
};
Expand Down Expand Up @@ -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"

0 comments on commit 46717e7

Please sign in to comment.