Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 2 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
} from './error'

import type { AnyWSLocalHook } from './ws/types'
import type { WebSocketHandler } from './ws/bun'
import type { WebSocketHandler, WebSocketConfig } from './ws/bun'

import type { Instruction as ExactMirrorInstruction } from 'exact-mirror'
import { BunHTMLBundlelike } from './universal/types'
Expand Down Expand Up @@ -171,10 +171,7 @@ export interface ElysiaConfig<in out Prefix extends string | undefined> {
*
* @see https://bun.sh/docs/api/websockets
*/
websocket?: Omit<
WebSocketHandler<any>,
'open' | 'close' | 'message' | 'drain'
>
websocket?: WebSocketConfig
cookie?: CookieOptions & {
/**
* Specified cookie name to be signed globally
Expand Down
8 changes: 8 additions & 0 deletions src/universal/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Serve as BunServe, type Server as BunServer } from 'bun'
import type { Equal, MaybePromise } from '../types'
import type { WebSocketConfig } from '../ws/bun'

export interface ErrorLike extends Error {
code?: string
Expand Down Expand Up @@ -120,6 +121,13 @@ export interface ServeOptions extends GenericServeOptions {
string,
Function | Response | Record<string, Function | Response>
>

/**
* WebSocket configuration options
*
* @see https://bun.sh/docs/api/websockets
*/
websocket?: WebSocketConfig
}

export type Serve =
Expand Down
118 changes: 56 additions & 62 deletions src/ws/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,61 @@ export interface ServerWebSocket<T = undefined> {
data: T
}

/**
* WebSocket configuration options (without handler methods).
* Use this type when you only need to configure WebSocket behavior
* without defining message handlers.
*/
export type WebSocketConfig = {
/**
* Sets the maximum size of messages in bytes.
* @default 16 MB (1024 * 1024 * 16)
*/
maxPayloadLength?: number

/**
* Sets the maximum number of bytes that can be buffered on a single connection.
* @default 16 MB (1024 * 1024 * 16)
*/
backpressureLimit?: number

/**
* Sets if the connection should be closed if `backpressureLimit` is reached.
* @default false
*/
closeOnBackpressureLimit?: boolean

/**
* Sets the number of seconds to wait before timing out a connection
* due to no messages or pings.
* @default 120 (2 minutes)
*/
idleTimeout?: number

/**
* Should `ws.publish()` also send a message to `ws` (itself), if it is subscribed?
* @default false
*/
publishToSelf?: boolean

/**
* Should the server automatically send and respond to pings to clients?
* @default true
*/
sendPings?: boolean

/**
* Sets the compression level for messages, for clients that support it.
* @default false (disabled)
*/
perMessageDeflate?:
| boolean
| {
compress?: WebSocketCompressor | boolean
decompress?: WebSocketCompressor | boolean
}
}

/**
* Compression options for WebSocket messages.
*/
Expand Down Expand Up @@ -371,7 +426,7 @@ export type WebSocketCompressor =
* });
* ```
*/
export interface WebSocketHandler<in out T = undefined> {
export interface WebSocketHandler<in out T = undefined> extends WebSocketConfig {
/**
* Called when the server receives an incoming message.
*
Expand Down Expand Up @@ -431,65 +486,4 @@ export interface WebSocketHandler<in out T = undefined> {
* @param data The data sent with the ping
*/
pong?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>

/**
* Sets the maximum size of messages in bytes.
*
* Default is 16 MB, or `1024 * 1024 * 16` in bytes.
*/
maxPayloadLength?: number

/**
* Sets the maximum number of bytes that can be buffered on a single connection.
*
* Default is 16 MB, or `1024 * 1024 * 16` in bytes.
*/
backpressureLimit?: number

/**
* Sets if the connection should be closed if `backpressureLimit` is reached.
*
* Default is `false`.
*/
closeOnBackpressureLimit?: boolean

/**
* Sets the the number of seconds to wait before timing out a connection
* due to no messages or pings.
*
* Default is 2 minutes, or `120` in seconds.
*/
idleTimeout?: number

/**
* Should `ws.publish()` also send a message to `ws` (itself), if it is subscribed?
*
* Default is `false`.
*/
publishToSelf?: boolean

/**
* Should the server automatically send and respond to pings to clients?
*
* Default is `true`.
*/
sendPings?: boolean

/**
* Sets the compression level for messages, for clients that supports it. By default, compression is disabled.
*
* Default is `false`.
*/
perMessageDeflate?:
| boolean
| {
/**
* Sets the compression level.
*/
compress?: WebSocketCompressor | boolean
/**
* Sets the decompression level.
*/
decompress?: WebSocketCompressor | boolean
}
}
5 changes: 4 additions & 1 deletion src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import type {
ServerWebSocket,
ServerWebSocketSendStatus,
BufferSource,
WebSocketHandler
WebSocketHandler,
WebSocketConfig
} from './bun'

export type { WebSocketConfig }

import type { TSchema } from '@sinclair/typebox'
import type { TypeCheck } from '../type-system'
import type { ElysiaTypeCheck } from '../schema'
Expand Down