Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(peer): add peer.close() and peer.terminate() support #36

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/adapters/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,12 @@ class BunPeer extends Peer<{
unsubscribe(topic: string): void {
this.ctx.bun.ws.unsubscribe(topic);
}

close(code?: number, reason?: string) {
this.ctx.bun.ws.close(code, reason);
}

terminate() {
this.ctx.bun.ws.terminate();
}
}
11 changes: 11 additions & 0 deletions src/adapters/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ class CloudflarePeer extends Peer<{
this.ctx.cloudflare.server.send(toBufferLike(message));
return 0;
}

close(code?: number, reason?: string) {
this.ctx.cloudflare.client.close(code, reason);
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Cloudflare WebSockets do not support termination. This calls the `close()` method.
*/
terminate(): void {
this.close();
}
}
8 changes: 8 additions & 0 deletions src/adapters/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ class DenoPeer extends Peer<{
this.ctx.deno.ws.send(toBufferLike(message));
return 0;
}

close(code?: number, reason?: string) {
this.ctx.deno.ws.close(code, reason);
}

terminate(): void {
this.ctx.deno.ws.terminate()
}
}
8 changes: 8 additions & 0 deletions src/adapters/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,12 @@ class NodePeer extends Peer<{
}
}
}

close(code?: number, data?: string | Buffer) {
this.ctx.node.ws.close(code, data);
}

terminate() {
this.ctx.node.ws.terminate();
}
}
9 changes: 9 additions & 0 deletions src/adapters/uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
WebSocket,
HttpRequest,
HttpResponse,
RecognizedString,
} from "uWebSockets.js";
import { Peer } from "../peer";
import { Message } from "../message";
Expand Down Expand Up @@ -194,6 +195,14 @@ class UWSPeer extends Peer<{
this.ctx.uws.ws.publish(topic, message, options?.binary, options?.compress);
return 0;
}

close(code?: number, reason?: RecognizedString) {
this.ctx.uws.ws.end(code, reason);
}

terminate(): void {
this.ctx.uws.ws.close();
}
}

function _getHeaders(req: HttpRequest): HeadersInit {
Expand Down
26 changes: 26 additions & 0 deletions src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,30 @@ export abstract class Peer<AdapterContext = any> implements WSRequest {

return `${_id}${_addr}${_state}`;
}

/**
* Closes the connection.
*
* Here is a list of close codes:
*
* - `1000` means "normal closure" (default)
* - `1009` means a message was too big and was rejected
* - `1011` means the server encountered an error
* - `1012` means the server is restarting
* - `1013` means the server is too busy or the client is rate-limited
* - `4000` through `4999` are reserved for applications (you can use it!)
*
* To close the connection abruptly, use `terminate()`.
*
* @param code The close code to send
* @param reason The close reason to send
*/
abstract close(code?: number, reason?: string): void;
pi0 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Abruptly close the connection.
*
* To gracefully close the connection, use `close()`.
*/
abstract terminate(): void;
}
Loading