Skip to content

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikk221 committed May 15, 2023
2 parents 1ab5969 + 6508288 commit a861936
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ webserver.post('/api/v1/delete_user/:id', async (request, response) => {
webserver.post('/api/v1/delete_user/:id', async (request, response) => {
// Some bad stuff happened and this request is now forbidden

// We multiple response network calls in atomic callback for best performance
// We wrap multiple response network calls in atomic callback for best performance
response.atomic(() => {
// All methods EXCEPT "response ending methods" such as send(), json(), upgrade() support chaining
response
Expand Down Expand Up @@ -149,4 +149,4 @@ webserver.ws('/ws/connect', (ws) => {
// You may do some cleanup here regarding analytics
});
});
```
```
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"mime-types": "^2.1.33",
"range-parser": "^1.2.1",
"type-is": "^1.6.18",
"typed-emitter": "^2.1.0",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.24.0"
},
"devDependencies": {
Expand Down
15 changes: 9 additions & 6 deletions types/components/ws/Websocket.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import * as uWebsockets from 'uWebSockets.js';
import { EventEmitter } from "events";
import { Readable, Writable } from 'stream';
import TypedEmitter from 'typed-emitter';
import { SendableData } from "../http/Response";

export type WebsocketContext = {
[key: string]: string
}

export class Websocket<TUserData = unknown> extends EventEmitter {
/* EventEmitter Overrides */
on(eventName: 'message' | 'close' | 'drain' | 'ping' | 'pong', listener: (...args: any[]) => void): this;
once(eventName: 'message' | 'close' | 'drain' | 'ping' | 'pong', listener: (...args: any[]) => void): this;

/* Websocket Methods */
type Events = {
message: (...args: any[]) => void | Promise<void>;
close: (...args: any[]) => void | Promise<void>;
drain: (...args: any[]) => void | Promise<void>;
ping: (...args: any[]) => void | Promise<void>;
pong: (...args: any[]) => void | Promise<void>;
}

export class Websocket<TUserData = unknown> extends (EventEmitter as new () => TypedEmitter<Events>) {
/**
* Alias of uWS.cork() method. Accepts a callback with multiple operations for network efficiency.
*
Expand Down

0 comments on commit a861936

Please sign in to comment.