Skip to content

Commit

Permalink
fix(websocket): typescript typing improved and fixed
Browse files Browse the repository at this point in the history
fix(websocket): expose utility now gets better typings and auto-complete

fix(deps): upgraded dependencies for latest updates and fixes

fix(deps): core uWebSockets.js v20.20.0 for better and latest features, fixes and updates

BREAKING CHANGE: Now minimum Node.js required for running is v16
  • Loading branch information
dalisoft committed Mar 30, 2023
1 parent a7dc98e commit a0d0069
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 92 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prepublishOnly": "yarn build"
},
"engines": {
"node": ">=14.18.1"
"node": ">=16.19.1"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,24 +48,24 @@
"uWebSockets.js": "https://github.com/uNetworking/uWebSockets.js/archive/v20.20.0.tar.gz"
},
"devDependencies": {
"@commitlint/cli": "^17.4.4",
"@commitlint/cli": "^17.5.1",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-node-resolve": "^15.0.1",
"@semantic-release/git": "^10.0.1",
"@types/node": "^18.15.0",
"@types/node": "^18.15.11",
"baretest": "^2.x",
"commitlint-config-airlight": "^6.0.0",
"eslint": "^8.35.0",
"eslint": "^8.37.0",
"eslint-config-airlight-node": "^4.0.1",
"husky": "^8.0.3",
"lint-staged": "^13.1.2",
"lint-staged": "^13.2.0",
"lint-staged-config-airlight": "^4.0.1",
"prettier": "^2.8.4",
"prettier": "^2.8.7",
"prettier-config-airlight": "^2.0.1",
"rollup": "^3.19.0",
"rollup": "^3.20.2",
"rollup-plugin-typescript2": "^0.34.1",
"smartlint": "^6.0.1",
"typedoc": "^0.23.26",
"typescript": "^4.9.5"
"typedoc": "^0.23.28",
"typescript": "^5.0.2"
}
}
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class App extends RouterTemplate {
return this;
}

ws(path: RecognizedString, options: WebSocketBehavior): this {
ws<T>(path: RecognizedString, options: WebSocketBehavior<T>): this {
this._app.ws(path, options);

return this;
Expand Down
52 changes: 46 additions & 6 deletions src/exposes/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,55 @@ import {
HttpRequest,
HttpResponse,
us_socket_context_t,
WebSocketBehavior
WebSocketBehavior,
WebSocket
} from 'uWebSockets.js';
export default function exposeWebsocket(

interface IWebSocket<UserData> extends WebSocket<UserData> {
on(
eventName:
| 'connection'
| 'error'
| 'upgrade'
| 'willUpgrade'
| 'upgraded'
| 'message'
| 'drain'
| 'close',
// rome-ignore lint/suspicious/noExplicitAny: <explanation>
listener: (...args: any[]) => void
): void;
emit(eventName: 'message', message: ArrayBuffer, isBinary: boolean): void;
emit(eventName: 'connection', ws: IWebSocket<UserData>): void;
emit(eventName: 'drain', drained: number): void;
emit(eventName: 'close', code: number, message: ArrayBuffer): void;
}

type WebSocketOptions<UserData> = Omit<
WebSocketBehavior<UserData>,
'open' | 'message' | 'drain' | 'close'
>;

interface IWebSocketBehavior<UserData> extends WebSocketOptions<UserData> {
open: (ws: IWebSocket<UserData>) => void;
message: (
ws: IWebSocket<UserData>,
message: ArrayBuffer,
isBinary: boolean
) => void;
drain: (ws: IWebSocket<UserData>) => void;
close: (ws: IWebSocket<UserData>, code: number, message: ArrayBuffer) => void;
}

export default function exposeWebsocket<UserData>(
handler: (req: HttpRequest, res: HttpResponse) => void | Promise<void>,
options = {}
): WebSocketBehavior {
if (typeof (options as WebSocketBehavior).open === 'function') {
options: WebSocketBehavior<UserData> | WebSocketOptions<UserData> = {}
): IWebSocketBehavior<UserData> | WebSocketBehavior<UserData> {
if (typeof (options as WebSocketBehavior<UserData>).open === 'function') {
return options;
}

return {
const behavior: IWebSocketBehavior<UserData> = {
...options,
open(ws): void {
ws.emit('connection', ws);
Expand Down Expand Up @@ -70,4 +108,6 @@ export default function exposeWebsocket(
ws.emit('close', code, message);
}
};

return behavior;
}
2 changes: 2 additions & 0 deletions src/nanoexpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ export {
useRef,
useState
};

console.log('test');
9 changes: 6 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Router {

[routerInstances]: UnpreparedRoute[];

[wsInstances]: IWebsocketRoute[];
[wsInstances]: IWebsocketRoute<any>[];

_basePath: string;

Expand Down Expand Up @@ -226,7 +226,10 @@ export default class Router {
);
}

ws(path: RecognizedString, options?: WebSocketBehavior): this {
ws<UserData>(
path: RecognizedString,
options?: WebSocketBehavior<UserData>
): this {
const normalisedPath =
this._basePath === '*'
? '*'
Expand All @@ -237,7 +240,7 @@ export default class Router {
this[wsInstances].push({
path: normalisedPath,
options
} as IWebsocketRoute);
} as IWebsocketRoute<UserData>);

return this;
}
Expand Down
8 changes: 5 additions & 3 deletions types/nanoexpress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export interface INanoexpressOptions {
}
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'OPTIONS' | 'DEL' | 'ANY';

export type WebSocketHandler = (ws: WebSocket) => void | WebSocketBehavior;
export type WebSocketHandler<T> = (
ws: WebSocket<T>
) => void | WebSocketBehavior<T>;

export interface IWebsocketRoute {
export interface IWebsocketRoute<UserData = unknown> {
path: RecognizedString;
options: WebSocketBehavior;
options: WebSocketBehavior<UserData>;
}
Loading

0 comments on commit a0d0069

Please sign in to comment.