bun-types 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/types.d.ts +602 -13
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Type definitions for bun 0.2.
|
|
1
|
+
// Type definitions for bun 0.2.1
|
|
2
2
|
// Project: https://github.com/oven-sh/bun
|
|
3
3
|
// Definitions by: Jarred Sumner <https://github.com/Jarred-Sumner>
|
|
4
4
|
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
@@ -13822,7 +13822,477 @@ declare module "bun" {
|
|
|
13822
13822
|
| "entry-point";
|
|
13823
13823
|
}
|
|
13824
13824
|
|
|
13825
|
-
|
|
13825
|
+
/**
|
|
13826
|
+
* **0** means the message was **dropped**
|
|
13827
|
+
*
|
|
13828
|
+
* **-1** means **backpressure**
|
|
13829
|
+
*
|
|
13830
|
+
* **> 0** is the **number of bytes sent**
|
|
13831
|
+
*
|
|
13832
|
+
*/
|
|
13833
|
+
type ServerWebSocketSendStatus = 0 | -1 | number;
|
|
13834
|
+
|
|
13835
|
+
/**
|
|
13836
|
+
* Fast WebSocket API designed for server environments.
|
|
13837
|
+
*
|
|
13838
|
+
* Features:
|
|
13839
|
+
* - **Message compression** - Messages can be compressed
|
|
13840
|
+
* - **Backpressure** - If the client is not ready to receive data, the server will tell you.
|
|
13841
|
+
* - **Dropped messages** - If the client cannot receive data, the server will tell you.
|
|
13842
|
+
* - **Topics** - Messages can be {@link ServerWebSocket.publish}ed to a specific topic and the client can {@link ServerWebSocket.subscribe} to topics
|
|
13843
|
+
*
|
|
13844
|
+
* This is slightly different than the browser {@link WebSocket} which Bun supports for clients.
|
|
13845
|
+
*
|
|
13846
|
+
* Powered by [uWebSockets](https://github.com/uNetworking/uWebSockets)
|
|
13847
|
+
*/
|
|
13848
|
+
export interface ServerWebSocket<T = undefined> {
|
|
13849
|
+
/**
|
|
13850
|
+
*
|
|
13851
|
+
* Send a message to the client.
|
|
13852
|
+
*
|
|
13853
|
+
* @param data The message to send
|
|
13854
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
13855
|
+
*
|
|
13856
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
13857
|
+
*
|
|
13858
|
+
* @example
|
|
13859
|
+
*
|
|
13860
|
+
* ```js
|
|
13861
|
+
* const status = ws.send("Hello World");
|
|
13862
|
+
* if (status === 0) {
|
|
13863
|
+
* console.log("Message was dropped");
|
|
13864
|
+
* } else if (status === -1) {
|
|
13865
|
+
* console.log("Backpressure was applied");
|
|
13866
|
+
* } else {
|
|
13867
|
+
* console.log(`Message sent! ${status} bytes sent`);
|
|
13868
|
+
* }
|
|
13869
|
+
* ```
|
|
13870
|
+
*
|
|
13871
|
+
* @example
|
|
13872
|
+
*
|
|
13873
|
+
* ```js
|
|
13874
|
+
* ws.send("Feeling very compressed", true);
|
|
13875
|
+
* ```
|
|
13876
|
+
*
|
|
13877
|
+
* @example
|
|
13878
|
+
*
|
|
13879
|
+
* ```js
|
|
13880
|
+
* ws.send(new Uint8Array([1, 2, 3, 4]));
|
|
13881
|
+
* ```
|
|
13882
|
+
*
|
|
13883
|
+
* @example
|
|
13884
|
+
*
|
|
13885
|
+
* ```js
|
|
13886
|
+
* ws.send(new ArrayBuffer(4));
|
|
13887
|
+
* ```
|
|
13888
|
+
*
|
|
13889
|
+
* @example
|
|
13890
|
+
*
|
|
13891
|
+
* ```js
|
|
13892
|
+
* ws.send(new DataView(new ArrayBuffer(4)));
|
|
13893
|
+
* ```
|
|
13894
|
+
*
|
|
13895
|
+
*/
|
|
13896
|
+
send(
|
|
13897
|
+
data: string | ArrayBufferView | ArrayBuffer,
|
|
13898
|
+
compress?: boolean
|
|
13899
|
+
): ServerWebSocketSendStatus;
|
|
13900
|
+
|
|
13901
|
+
/**
|
|
13902
|
+
*
|
|
13903
|
+
* Send a message to the client.
|
|
13904
|
+
*
|
|
13905
|
+
* This function is the same as {@link ServerWebSocket.send} but it only accepts a string. This function includes a fast path.
|
|
13906
|
+
*
|
|
13907
|
+
* @param data The message to send
|
|
13908
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
13909
|
+
*
|
|
13910
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
13911
|
+
*
|
|
13912
|
+
* @example
|
|
13913
|
+
*
|
|
13914
|
+
* ```js
|
|
13915
|
+
* const status = ws.send("Hello World");
|
|
13916
|
+
* if (status === 0) {
|
|
13917
|
+
* console.log("Message was dropped");
|
|
13918
|
+
* } else if (status === -1) {
|
|
13919
|
+
* console.log("Backpressure was applied");
|
|
13920
|
+
* } else {
|
|
13921
|
+
* console.log(`Message sent! ${status} bytes sent`);
|
|
13922
|
+
* }
|
|
13923
|
+
* ```
|
|
13924
|
+
*
|
|
13925
|
+
* @example
|
|
13926
|
+
*
|
|
13927
|
+
* ```js
|
|
13928
|
+
* ws.send("Feeling very compressed", true);
|
|
13929
|
+
* ```
|
|
13930
|
+
*
|
|
13931
|
+
*
|
|
13932
|
+
*/
|
|
13933
|
+
sendText(data: string, compress?: boolean): ServerWebSocketSendStatus;
|
|
13934
|
+
|
|
13935
|
+
/**
|
|
13936
|
+
*
|
|
13937
|
+
* Send a message to the client.
|
|
13938
|
+
*
|
|
13939
|
+
* This function is the same as {@link ServerWebSocket.send} but it only accepts Uint8Array.
|
|
13940
|
+
*
|
|
13941
|
+
* @param data The message to send
|
|
13942
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
13943
|
+
*
|
|
13944
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
13945
|
+
*
|
|
13946
|
+
*
|
|
13947
|
+
* ```js
|
|
13948
|
+
* ws.sendBinary(new Uint8Array([1, 2, 3, 4]));
|
|
13949
|
+
* ```
|
|
13950
|
+
*
|
|
13951
|
+
* @example
|
|
13952
|
+
*
|
|
13953
|
+
* ```js
|
|
13954
|
+
* ws.sendBinary(new ArrayBuffer(4));
|
|
13955
|
+
* ```
|
|
13956
|
+
*
|
|
13957
|
+
* @example
|
|
13958
|
+
*
|
|
13959
|
+
* ```js
|
|
13960
|
+
* ws.sendBinary(new DataView(new ArrayBuffer(4)));
|
|
13961
|
+
* ```
|
|
13962
|
+
*
|
|
13963
|
+
*/
|
|
13964
|
+
sendBinary(data: Uint8Array, compress?: boolean): ServerWebSocketSendStatus;
|
|
13965
|
+
|
|
13966
|
+
/**
|
|
13967
|
+
* Gently close the connection.
|
|
13968
|
+
*
|
|
13969
|
+
* @param code The close code
|
|
13970
|
+
*
|
|
13971
|
+
* @param reason The close reason
|
|
13972
|
+
*
|
|
13973
|
+
* To close the connection abruptly, use `close(0, "")`
|
|
13974
|
+
*/
|
|
13975
|
+
close(code?: number, reason?: string): void;
|
|
13976
|
+
|
|
13977
|
+
/**
|
|
13978
|
+
* Send a message to all subscribers of a topic
|
|
13979
|
+
*
|
|
13980
|
+
* @param topic The topic to publish to
|
|
13981
|
+
* @param data The data to send
|
|
13982
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
13983
|
+
*
|
|
13984
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
13985
|
+
*
|
|
13986
|
+
* @example
|
|
13987
|
+
*
|
|
13988
|
+
* ```js
|
|
13989
|
+
* ws.publish("chat", "Hello World");
|
|
13990
|
+
* ```
|
|
13991
|
+
*
|
|
13992
|
+
* @example
|
|
13993
|
+
* ```js
|
|
13994
|
+
* ws.publish("chat", new Uint8Array([1, 2, 3, 4]));
|
|
13995
|
+
* ```
|
|
13996
|
+
*
|
|
13997
|
+
* @example
|
|
13998
|
+
* ```js
|
|
13999
|
+
* ws.publish("chat", new ArrayBuffer(4), true);
|
|
14000
|
+
* ```
|
|
14001
|
+
*
|
|
14002
|
+
* @example
|
|
14003
|
+
* ```js
|
|
14004
|
+
* ws.publish("chat", new DataView(new ArrayBuffer(4)));
|
|
14005
|
+
* ```
|
|
14006
|
+
*/
|
|
14007
|
+
publish(
|
|
14008
|
+
topic: string,
|
|
14009
|
+
data: string | ArrayBufferView | ArrayBuffer,
|
|
14010
|
+
compress?: boolean
|
|
14011
|
+
): ServerWebSocketSendStatus;
|
|
14012
|
+
|
|
14013
|
+
/**
|
|
14014
|
+
* Send a message to all subscribers of a topic
|
|
14015
|
+
*
|
|
14016
|
+
* This function is the same as {@link publish} but only accepts string input. This function has a fast path.
|
|
14017
|
+
*
|
|
14018
|
+
* @param topic The topic to publish to
|
|
14019
|
+
* @param data The data to send
|
|
14020
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
14021
|
+
*
|
|
14022
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
14023
|
+
*
|
|
14024
|
+
* @example
|
|
14025
|
+
*
|
|
14026
|
+
* ```js
|
|
14027
|
+
* ws.publishText("chat", "Hello World");
|
|
14028
|
+
* ```
|
|
14029
|
+
*
|
|
14030
|
+
*/
|
|
14031
|
+
publishText(
|
|
14032
|
+
topic: string,
|
|
14033
|
+
data: string,
|
|
14034
|
+
compress?: bool
|
|
14035
|
+
): ServerWebSocketSendStatus;
|
|
14036
|
+
|
|
14037
|
+
/**
|
|
14038
|
+
* Send a message to all subscribers of a topic
|
|
14039
|
+
*
|
|
14040
|
+
* This function is the same as {@link publish} but only accepts a Uint8Array. This function has a fast path.
|
|
14041
|
+
*
|
|
14042
|
+
* @param topic The topic to publish to
|
|
14043
|
+
* @param data The data to send
|
|
14044
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
14045
|
+
*
|
|
14046
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
14047
|
+
*
|
|
14048
|
+
* @example
|
|
14049
|
+
*
|
|
14050
|
+
* ```js
|
|
14051
|
+
* ws.publishBinary("chat", "Hello World");
|
|
14052
|
+
* ```
|
|
14053
|
+
*
|
|
14054
|
+
* @example
|
|
14055
|
+
* ```js
|
|
14056
|
+
* ws.publishBinary("chat", new Uint8Array([1, 2, 3, 4]));
|
|
14057
|
+
* ```
|
|
14058
|
+
*
|
|
14059
|
+
* @example
|
|
14060
|
+
* ```js
|
|
14061
|
+
* ws.publishBinary("chat", new ArrayBuffer(4), true);
|
|
14062
|
+
* ```
|
|
14063
|
+
*
|
|
14064
|
+
* @example
|
|
14065
|
+
* ```js
|
|
14066
|
+
* ws.publishBinary("chat", new DataView(new ArrayBuffer(4)));
|
|
14067
|
+
* ```
|
|
14068
|
+
*/
|
|
14069
|
+
publishBinary(
|
|
14070
|
+
topic: string,
|
|
14071
|
+
data: Uint8Array,
|
|
14072
|
+
compress?: boolean
|
|
14073
|
+
): ServerWebSocketSendStatus;
|
|
14074
|
+
|
|
14075
|
+
/**
|
|
14076
|
+
* Subscribe to a topic
|
|
14077
|
+
* @param topic The topic to subscribe to
|
|
14078
|
+
*
|
|
14079
|
+
* @example
|
|
14080
|
+
* ```js
|
|
14081
|
+
* ws.subscribe("chat");
|
|
14082
|
+
* ```
|
|
14083
|
+
*/
|
|
14084
|
+
subscribe(topic: string): void;
|
|
14085
|
+
|
|
14086
|
+
/**
|
|
14087
|
+
* Unsubscribe from a topic
|
|
14088
|
+
* @param topic The topic to unsubscribe from
|
|
14089
|
+
*
|
|
14090
|
+
* @example
|
|
14091
|
+
* ```js
|
|
14092
|
+
* ws.unsubscribe("chat");
|
|
14093
|
+
* ```
|
|
14094
|
+
*
|
|
14095
|
+
*/
|
|
14096
|
+
unsubscribe(topic: string): void;
|
|
14097
|
+
|
|
14098
|
+
/**
|
|
14099
|
+
* Is the socket subscribed to a topic?
|
|
14100
|
+
* @param topic The topic to check
|
|
14101
|
+
*
|
|
14102
|
+
* @returns `true` if the socket is subscribed to the topic, `false` otherwise
|
|
14103
|
+
*/
|
|
14104
|
+
isSubscribed(topic: string): boolean;
|
|
14105
|
+
|
|
14106
|
+
/**
|
|
14107
|
+
* The remote address of the client
|
|
14108
|
+
* @example
|
|
14109
|
+
* ```js
|
|
14110
|
+
* console.log(socket.remoteAddress); // "127.0.0.1"
|
|
14111
|
+
* ```
|
|
14112
|
+
*/
|
|
14113
|
+
readonly remoteAddress: string;
|
|
14114
|
+
|
|
14115
|
+
/**
|
|
14116
|
+
* Ready state of the socket
|
|
14117
|
+
*
|
|
14118
|
+
* @example
|
|
14119
|
+
* ```js
|
|
14120
|
+
* console.log(socket.readyState); // 1
|
|
14121
|
+
* ```
|
|
14122
|
+
*/
|
|
14123
|
+
readonly readyState: -1 | 0 | 1 | 2 | 3;
|
|
14124
|
+
|
|
14125
|
+
/**
|
|
14126
|
+
* The data from the {@link Server.upgrade} function
|
|
14127
|
+
*
|
|
14128
|
+
* Put any data you want to share between the `fetch` function and the websocket here.
|
|
14129
|
+
*
|
|
14130
|
+
* You can read/write to this property at any time.
|
|
14131
|
+
*/
|
|
14132
|
+
data: T;
|
|
14133
|
+
|
|
14134
|
+
/**
|
|
14135
|
+
* Batch data sent to a {@link ServerWebSocket}
|
|
14136
|
+
*
|
|
14137
|
+
* This makes it significantly faster to {@link ServerWebSocket.send} or {@link ServerWebSocket.publish} multiple messages
|
|
14138
|
+
*
|
|
14139
|
+
* The `message`, `open`, and `drain` callbacks are automatically corked, so
|
|
14140
|
+
* you only need to call this if you are sending messages outside of those
|
|
14141
|
+
* callbacks or in async functions
|
|
14142
|
+
*/
|
|
14143
|
+
cork?: (callback: (ws: ServerWebSocket<T>) => any) => void | Promise<void>;
|
|
14144
|
+
|
|
14145
|
+
/**
|
|
14146
|
+
* Configure the {@link WebSocketHandler.message} callback to return a {@link ArrayBuffer} instead of a {@link Uint8Array}
|
|
14147
|
+
*
|
|
14148
|
+
* @default "uint8array"
|
|
14149
|
+
*/
|
|
14150
|
+
binaryType?: "arraybuffer" | "uint8array";
|
|
14151
|
+
}
|
|
14152
|
+
|
|
14153
|
+
type WebSocketCompressor =
|
|
14154
|
+
| "disable"
|
|
14155
|
+
| "shared"
|
|
14156
|
+
| "dedicated"
|
|
14157
|
+
| "3KB"
|
|
14158
|
+
| "4KB"
|
|
14159
|
+
| "8KB"
|
|
14160
|
+
| "16KB"
|
|
14161
|
+
| "32KB"
|
|
14162
|
+
| "64KB"
|
|
14163
|
+
| "128KB"
|
|
14164
|
+
| "256KB";
|
|
14165
|
+
|
|
14166
|
+
/**
|
|
14167
|
+
* Create a server-side {@link ServerWebSocket} handler for use with {@link Bun.serve}
|
|
14168
|
+
*
|
|
14169
|
+
* @example
|
|
14170
|
+
* ```ts
|
|
14171
|
+
* import { websocket, serve } from "bun";
|
|
14172
|
+
*
|
|
14173
|
+
* serve({
|
|
14174
|
+
* port: 3000,
|
|
14175
|
+
* websocket: websocket<{name: string}>({
|
|
14176
|
+
* open: (ws) => {
|
|
14177
|
+
* console.log("Client connected");
|
|
14178
|
+
* },
|
|
14179
|
+
* message: (ws, message) => {
|
|
14180
|
+
* console.log(`${ws.data.name}: ${message}`);
|
|
14181
|
+
* },
|
|
14182
|
+
* close: (ws) => {
|
|
14183
|
+
* console.log("Client disconnected");
|
|
14184
|
+
* },
|
|
14185
|
+
* }),
|
|
14186
|
+
*
|
|
14187
|
+
* fetch(req, server) {
|
|
14188
|
+
* if (req.url === "/chat") {
|
|
14189
|
+
* const upgraded = server.upgrade(req, {
|
|
14190
|
+
* data: {
|
|
14191
|
+
* name: new URL(req.url).searchParams.get("name"),
|
|
14192
|
+
* },
|
|
14193
|
+
* });
|
|
14194
|
+
* if (!upgraded) {
|
|
14195
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
14196
|
+
* }
|
|
14197
|
+
* return;
|
|
14198
|
+
* }
|
|
14199
|
+
* return new Response("Hello World");
|
|
14200
|
+
* },
|
|
14201
|
+
* });
|
|
14202
|
+
*/
|
|
14203
|
+
export interface WebSocketHandler<T = undefined> {
|
|
14204
|
+
/**
|
|
14205
|
+
* Handle an incoming message to a {@link ServerWebSocket}
|
|
14206
|
+
*
|
|
14207
|
+
* @param ws The {@link ServerWebSocket} that received the message
|
|
14208
|
+
* @param message The message received
|
|
14209
|
+
*
|
|
14210
|
+
* To change `message` to be an `ArrayBuffer` instead of a `Uint8Array`, set `ws.binaryType = "arraybuffer"`
|
|
14211
|
+
*/
|
|
14212
|
+
message: (
|
|
14213
|
+
ws: ServerWebSocket<T>,
|
|
14214
|
+
message: string | Uint8Array
|
|
14215
|
+
) => void | Promise<void>;
|
|
14216
|
+
|
|
14217
|
+
/**
|
|
14218
|
+
* The {@link ServerWebSocket} has been opened
|
|
14219
|
+
*
|
|
14220
|
+
* @param ws The {@link ServerWebSocket} that was opened
|
|
14221
|
+
*/
|
|
14222
|
+
open?: (ws: ServerWebSocket<T>) => void | Promise<void>;
|
|
14223
|
+
/**
|
|
14224
|
+
* The {@link ServerWebSocket} is ready for more data
|
|
14225
|
+
*
|
|
14226
|
+
* @param ws The {@link ServerWebSocket} that is ready
|
|
14227
|
+
*/
|
|
14228
|
+
drain?: (ws: ServerWebSocket<T>) => void | Promise<void>;
|
|
14229
|
+
/**
|
|
14230
|
+
* The {@link ServerWebSocket} is being closed
|
|
14231
|
+
* @param ws The {@link ServerWebSocket} that was closed
|
|
14232
|
+
* @param code The close code
|
|
14233
|
+
* @param message The close message
|
|
14234
|
+
*/
|
|
14235
|
+
close?: (
|
|
14236
|
+
ws: ServerWebSocket<T>,
|
|
14237
|
+
code: number,
|
|
14238
|
+
message: string
|
|
14239
|
+
) => void | Promise<void>;
|
|
14240
|
+
|
|
14241
|
+
/**
|
|
14242
|
+
* Enable compression for clients that support it. By default, compression is disabled.
|
|
14243
|
+
*
|
|
14244
|
+
* @default false
|
|
14245
|
+
*
|
|
14246
|
+
* `true` is equivalent to `"shared"
|
|
14247
|
+
*/
|
|
14248
|
+
perMessageDeflate?:
|
|
14249
|
+
| true
|
|
14250
|
+
| false
|
|
14251
|
+
| {
|
|
14252
|
+
/**
|
|
14253
|
+
* Enable compression on the {@link ServerWebSocket}
|
|
14254
|
+
*
|
|
14255
|
+
* @default false
|
|
14256
|
+
*
|
|
14257
|
+
* `true` is equivalent to `"shared"
|
|
14258
|
+
*/
|
|
14259
|
+
compress?: WebSocketCompressor | false | true;
|
|
14260
|
+
/**
|
|
14261
|
+
* Configure decompression
|
|
14262
|
+
*
|
|
14263
|
+
* @default false
|
|
14264
|
+
*
|
|
14265
|
+
* `true` is equivalent to `"shared"
|
|
14266
|
+
*/
|
|
14267
|
+
decompress?: WebSocketCompressor | false | true;
|
|
14268
|
+
};
|
|
14269
|
+
|
|
14270
|
+
/**
|
|
14271
|
+
* The maximum size of a message
|
|
14272
|
+
*/
|
|
14273
|
+
maxPayloadLength?: number;
|
|
14274
|
+
/**
|
|
14275
|
+
* After a connection has not received a message for this many seconds, it will be closed.
|
|
14276
|
+
* @default 120 (2 minutes)
|
|
14277
|
+
*/
|
|
14278
|
+
idleTimeout?: number;
|
|
14279
|
+
/**
|
|
14280
|
+
* The maximum number of bytes that can be buffered for a single connection.
|
|
14281
|
+
* @default 16MB
|
|
14282
|
+
*/
|
|
14283
|
+
backpressureLimit?: number;
|
|
14284
|
+
/**
|
|
14285
|
+
* Close the connection if the backpressure limit is reached.
|
|
14286
|
+
* @default false
|
|
14287
|
+
* @see {@link backpressureLimit}
|
|
14288
|
+
* @see {@link ServerWebSocketSendStatus}
|
|
14289
|
+
* @see {@link ServerWebSocket.send}
|
|
14290
|
+
* @see {@link ServerWebSocket.publish}
|
|
14291
|
+
*/
|
|
14292
|
+
closeOnBackpressureLimit?: boolean;
|
|
14293
|
+
}
|
|
14294
|
+
|
|
14295
|
+
interface GenericServeOptions {
|
|
13826
14296
|
/**
|
|
13827
14297
|
* What port should the server listen on?
|
|
13828
14298
|
* @default process.env.PORT || "3000"
|
|
@@ -13884,18 +14354,78 @@ declare module "bun" {
|
|
|
13884
14354
|
*/
|
|
13885
14355
|
development?: boolean;
|
|
13886
14356
|
|
|
14357
|
+
error?: (
|
|
14358
|
+
this: Server,
|
|
14359
|
+
request: Errorlike
|
|
14360
|
+
) => Response | Promise<Response> | undefined | Promise<undefined>;
|
|
14361
|
+
}
|
|
14362
|
+
|
|
14363
|
+
export interface ServeOptions extends GenericServeOptions {
|
|
13887
14364
|
/**
|
|
13888
14365
|
* Handle HTTP requests
|
|
13889
14366
|
*
|
|
13890
14367
|
* Respond to {@link Request} objects with a {@link Response} object.
|
|
13891
14368
|
*
|
|
13892
14369
|
*/
|
|
13893
|
-
fetch(
|
|
14370
|
+
fetch(
|
|
14371
|
+
this: Server,
|
|
14372
|
+
request: Request,
|
|
14373
|
+
server: Server
|
|
14374
|
+
): Response | Promise<Response>;
|
|
14375
|
+
}
|
|
13894
14376
|
|
|
13895
|
-
|
|
14377
|
+
export interface WebSocketServeOptions<WebSocketDataType = undefined>
|
|
14378
|
+
extends GenericServeOptions {
|
|
14379
|
+
/**
|
|
14380
|
+
* Enable websockets with {@link Bun.serve}
|
|
14381
|
+
*
|
|
14382
|
+
* For simpler type safety, see {@link Bun.websocket}
|
|
14383
|
+
*
|
|
14384
|
+
* @example
|
|
14385
|
+
* ```js
|
|
14386
|
+
*import { serve, websocket } from "bun";
|
|
14387
|
+
*serve({
|
|
14388
|
+
* websocket: websocket({
|
|
14389
|
+
* open: (ws) => {
|
|
14390
|
+
* console.log("Client connected");
|
|
14391
|
+
* },
|
|
14392
|
+
* message: (ws, message) => {
|
|
14393
|
+
* console.log("Client sent message", message);
|
|
14394
|
+
* },
|
|
14395
|
+
* close: (ws) => {
|
|
14396
|
+
* console.log("Client disconnected");
|
|
14397
|
+
* },
|
|
14398
|
+
* }),
|
|
14399
|
+
* fetch(req, server) {
|
|
14400
|
+
* if (req.url === "/chat") {
|
|
14401
|
+
* const upgraded = server.upgrade(req);
|
|
14402
|
+
* if (!upgraded) {
|
|
14403
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
14404
|
+
* }
|
|
14405
|
+
* }
|
|
14406
|
+
* return new Response("Hello World");
|
|
14407
|
+
* },
|
|
14408
|
+
*});
|
|
14409
|
+
*```
|
|
14410
|
+
* Upgrade a {@link Request} to a {@link ServerWebSocket} via {@link Server.upgrade}
|
|
14411
|
+
*
|
|
14412
|
+
* Pass `data` in @{link Server.upgrade} to attach data to the {@link ServerWebSocket.data} property
|
|
14413
|
+
*
|
|
14414
|
+
*
|
|
14415
|
+
*/
|
|
14416
|
+
websocket: WebSocketHandler<WebSocketDataType>;
|
|
14417
|
+
|
|
14418
|
+
/**
|
|
14419
|
+
* Handle HTTP requests or upgrade them to a {@link ServerWebSocket}
|
|
14420
|
+
*
|
|
14421
|
+
* Respond to {@link Request} objects with a {@link Response} object.
|
|
14422
|
+
*
|
|
14423
|
+
*/
|
|
14424
|
+
fetch(
|
|
13896
14425
|
this: Server,
|
|
13897
|
-
request:
|
|
13898
|
-
|
|
14426
|
+
request: Request,
|
|
14427
|
+
server: Server
|
|
14428
|
+
): Response | undefined | Promise<Response | undefined>;
|
|
13899
14429
|
}
|
|
13900
14430
|
|
|
13901
14431
|
export interface Errorlike extends Error {
|
|
@@ -13931,7 +14461,10 @@ declare module "bun" {
|
|
|
13931
14461
|
certFile: string;
|
|
13932
14462
|
}
|
|
13933
14463
|
|
|
13934
|
-
export type SSLServeOptions =
|
|
14464
|
+
export type SSLServeOptions<WebSocketDataType = undefined> = (
|
|
14465
|
+
| WebSocketServeOptions<WebSocketDataType>
|
|
14466
|
+
| ServerWebSocket
|
|
14467
|
+
) &
|
|
13935
14468
|
SSLOptions &
|
|
13936
14469
|
SSLAdvancedOptions & {
|
|
13937
14470
|
/**
|
|
@@ -13946,10 +14479,6 @@ declare module "bun" {
|
|
|
13946
14479
|
*
|
|
13947
14480
|
* To start the server, see {@link serve}
|
|
13948
14481
|
*
|
|
13949
|
-
* Often, you don't need to interact with this object directly. It exists to help you with the following tasks:
|
|
13950
|
-
* - Stop the server
|
|
13951
|
-
* - How many requests are currently being handled?
|
|
13952
|
-
*
|
|
13953
14482
|
* For performance, Bun pre-allocates most of the data for 2048 concurrent requests.
|
|
13954
14483
|
* That means starting a new server allocates about 500 KB of memory. Try to
|
|
13955
14484
|
* avoid starting and stopping the server often (unless it's a new instance of bun).
|
|
@@ -13957,7 +14486,7 @@ declare module "bun" {
|
|
|
13957
14486
|
* Powered by a fork of [uWebSockets](https://github.com/uNetworking/uWebSockets). Thank you @alexhultman.
|
|
13958
14487
|
*
|
|
13959
14488
|
*/
|
|
13960
|
-
interface Server {
|
|
14489
|
+
export interface Server {
|
|
13961
14490
|
/**
|
|
13962
14491
|
* Stop listening to prevent new connections from being accepted.
|
|
13963
14492
|
*
|
|
@@ -14004,10 +14533,67 @@ declare module "bun" {
|
|
|
14004
14533
|
*/
|
|
14005
14534
|
fetch(request: Request): Response | Promise<Response>;
|
|
14006
14535
|
|
|
14536
|
+
/**
|
|
14537
|
+
* Upgrade a {@link Request} to a {@link ServerWebSocket}
|
|
14538
|
+
*
|
|
14539
|
+
* @param request The {@link Request} to upgrade
|
|
14540
|
+
* @param options Pass headers or attach data to the {@link ServerWebSocket}
|
|
14541
|
+
*
|
|
14542
|
+
* @returns `true` if the upgrade was successful and `false` if it failed
|
|
14543
|
+
*
|
|
14544
|
+
* @example
|
|
14545
|
+
* ```js
|
|
14546
|
+
* import { serve, websocket } from "bun";
|
|
14547
|
+
* serve({
|
|
14548
|
+
* websocket: websocket({
|
|
14549
|
+
* open: (ws) => {
|
|
14550
|
+
* console.log("Client connected");
|
|
14551
|
+
* },
|
|
14552
|
+
* message: (ws, message) => {
|
|
14553
|
+
* console.log("Client sent message", message);
|
|
14554
|
+
* },
|
|
14555
|
+
* close: (ws) => {
|
|
14556
|
+
* console.log("Client disconnected");
|
|
14557
|
+
* },
|
|
14558
|
+
* }),
|
|
14559
|
+
* fetch(req, server) {
|
|
14560
|
+
* if (req.url === "/chat") {
|
|
14561
|
+
* const upgraded = server.upgrade(req);
|
|
14562
|
+
* if (!upgraded) {
|
|
14563
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
14564
|
+
* }
|
|
14565
|
+
* }
|
|
14566
|
+
* return new Response("Hello World");
|
|
14567
|
+
* },
|
|
14568
|
+
* });
|
|
14569
|
+
* ```
|
|
14570
|
+
* What you pass to `data` is available on the {@link ServerWebSocket.data} property
|
|
14571
|
+
*
|
|
14572
|
+
*/
|
|
14573
|
+
upgrade<T = undefined>(
|
|
14574
|
+
request: Request,
|
|
14575
|
+
options?: {
|
|
14576
|
+
/**
|
|
14577
|
+
* Send any additional headers while upgrading, like cookies
|
|
14578
|
+
*/
|
|
14579
|
+
headers?: HeadersInit;
|
|
14580
|
+
/**
|
|
14581
|
+
* This value is passed to the {@link ServerWebSocket.data} property
|
|
14582
|
+
*/
|
|
14583
|
+
data?: T;
|
|
14584
|
+
}
|
|
14585
|
+
): boolean;
|
|
14586
|
+
|
|
14007
14587
|
/**
|
|
14008
14588
|
* How many requests are in-flight right now?
|
|
14009
14589
|
*/
|
|
14010
14590
|
readonly pendingRequests: number;
|
|
14591
|
+
|
|
14592
|
+
/**
|
|
14593
|
+
* How many {@link ServerWebSocket}s are in-flight right now?
|
|
14594
|
+
*/
|
|
14595
|
+
readonly pendingWebSockets: number;
|
|
14596
|
+
|
|
14011
14597
|
readonly port: number;
|
|
14012
14598
|
/**
|
|
14013
14599
|
* The hostname the server is listening on. Does not include the port
|
|
@@ -14029,7 +14615,10 @@ declare module "bun" {
|
|
|
14029
14615
|
readonly development: boolean;
|
|
14030
14616
|
}
|
|
14031
14617
|
|
|
14032
|
-
export type Serve =
|
|
14618
|
+
export type Serve<WebSocketDataType = undefined> =
|
|
14619
|
+
| SSLServeOptions<WebSocketDataType>
|
|
14620
|
+
| WebSocketServeOptions<WebSocketDataType>
|
|
14621
|
+
| ServeOptions;
|
|
14033
14622
|
|
|
14034
14623
|
/**
|
|
14035
14624
|
* [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
|