Skip to content

Commit f1e3038

Browse files
committed
Update documentation
1 parent 14bb3af commit f1e3038

File tree

1 file changed

+38
-54
lines changed

1 file changed

+38
-54
lines changed

docs/index.d.ts

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ export type RecognizedString = string | ArrayBuffer | Uint8Array | Int8Array | U
4848
* Read more about this in the user manual.
4949
*/
5050
export interface WebSocket<UserData> {
51-
/** Sends a message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure that will drain over time. You can check backpressure before or after sending by calling getBufferedAmount().
52-
*
53-
* Make sure you properly understand the concept of backpressure. Check the backpressure example file.
54-
*/
51+
/** Sends a message. Returns 1 for success, 2 for dropped due to backpressure limit, and 0 for built up backpressure that will drain over time.
52+
* You can check backpressure before or after sending by calling getBufferedAmount().
53+
* Make sure you properly understand the concept of backpressure. Check the backpressure example file. */
5554
send(message: RecognizedString, isBinary?: boolean, compress?: boolean) : number;
5655

5756
/** Returns the bytes buffered in backpressure. This is similar to the bufferedAmount property in the browser counterpart.
@@ -110,61 +109,47 @@ export interface WebSocket<UserData> {
110109

111110
/** An HttpResponse is valid until either onAborted callback or any of the .end/.tryEnd calls succeed. You may attach user data to this object. */
112111
export interface HttpResponse {
113-
/** Writes the HTTP status message such as "200 OK".
114-
* This has to be called first in any response, otherwise
115-
* it will be called automatically with "200 OK".
116-
*
117-
* If you want to send custom headers in a WebSocket
118-
* upgrade response, you have to call writeStatus with
119-
* "101 Switching Protocols" before you call writeHeader,
120-
* otherwise your first call to writeHeader will call
121-
* writeStatus with "200 OK" and the upgrade will fail.
112+
/** Writes the HTTP status message, such as "200 OK".
113+
* This must be called first in any response, otherwise the default status "200 OK" will be used.
114+
* We format outgoing responses in a linear buffer, not in a hash table, you can read about this in the user manual under "corking".
122115
*
123-
* As you can imagine, we format outgoing responses in a linear
124-
* buffer, not in a hash table. You can read about this in
125-
* the user manual under "corking".
126-
*/
127-
128-
/** Pause http body streaming (throttle) */
129-
pause() : void;
130-
131-
/** Resume http body streaming (unthrottle) */
132-
resume() : void;
133-
116+
* If you want to send custom headers in a WebSocket upgrade response, you must call writeStatus with "101 Switching Protocols" first.
117+
* Otherwise the status will be set to "200 OK" and the upgrade will fail. */
134118
writeStatus(status: RecognizedString) : HttpResponse;
135-
/** Writes key and value to HTTP response.
136-
* See writeStatus and corking.
137-
*/
119+
/** Writes key and value to HTTP response. See writeStatus and corking. */
138120
writeHeader(key: RecognizedString, value: RecognizedString) : HttpResponse;
139121
/** Enters or continues chunked encoding mode. Writes part of the response. End with zero length write. Returns true if no backpressure was added. */
140122
write(chunk: RecognizedString) : boolean;
141123
/** Ends this response by copying the contents of body. */
142124
end(body?: RecognizedString, closeConnection?: boolean) : HttpResponse;
143125
/** Ends this response without a body. */
144126
endWithoutBody(reportedContentLength?: number, closeConnection?: boolean) : HttpResponse;
145-
/** Ends this response, or tries to, by streaming appropriately sized chunks of body. Use in conjunction with onWritable. Returns tuple [ok, hasResponded].*/
146-
tryEnd(fullBodyOrChunk: RecognizedString, totalSize: number) : [boolean, boolean];
147-
148-
/** Immediately force closes the connection. Any onAborted callback will run. */
149-
close() : HttpResponse;
150127

128+
/** Ends this response, or tries to, by streaming appropriately sized chunks of body.
129+
* Use in conjunction with onWritable. Returns tuple [sent, done]. */
130+
tryEnd(fullBodyOrChunk: RecognizedString, totalSize: number) : [boolean, boolean];
131+
/** Handler for retrying previously failed write attempts.
132+
* You MUST return false on rewrite failure and true otherwise. */
133+
onWritable(handler: (offset: number) => boolean) : HttpResponse;
151134
/** Returns the global byte write offset for this response. Use with onWritable. */
152135
getWriteOffset() : number;
153136

154-
/** Registers a handler for writable events. Continue failed write attempts in here.
155-
* You MUST return true for success, false for failure.
156-
* Writing nothing is always success, so by default you must return true.
157-
*/
158-
onWritable(handler: (offset: number) => boolean) : HttpResponse;
159-
160-
/** Every HttpResponse MUST have an attached abort handler IF you do not respond
161-
* to it immediately inside of the callback. Returning from an Http request handler
162-
* without attaching (by calling onAborted) an abort handler is ill-use and will terminate.
163-
* When this event emits, the response has been aborted and may not be used. */
137+
/** Every HttpResponse MUST have an attached abort handler IF you perform any asynchronous operation.
138+
* Returning from an HTTP request handler without attaching an abort handler is ill-use and will terminate.
139+
* When this event is emitted, the response has been aborted and may not be used. */
164140
onAborted(handler: () => void) : HttpResponse;
141+
/** Immediately force closes the connection. Any onAborted callback will run. */
142+
close() : HttpResponse;
165143

166-
/** Handler for reading data from POST and such requests. You MUST copy the data of chunk if isLast is not true. We Neuter ArrayBuffers on return, making it zero length.*/
144+
/** Handler for reading HTTP request's body data.
145+
* Must be attached before performing any asynchronous operation, otherwise data may be lost.
146+
* You MUST copy the ArrayBuffer's data if isLast is not true. We Neuter ArrayBuffers on return, making them zero length. */
167147
onData(handler: (chunk: ArrayBuffer, isLast: boolean) => void) : HttpResponse;
148+
/** Pause HTTP request's body streaming (throttle).
149+
* Some buffered data may still be sent to onData. */
150+
pause() : void;
151+
/** Resume HTTP request's body streaming (unthrottle). */
152+
resume() : void;
168153

169154
/** Returns the remote IP address in binary format (4 or 16 bytes). */
170155
getRemoteAddress() : ArrayBuffer;
@@ -179,17 +164,16 @@ export interface HttpResponse {
179164
getProxiedRemoteAddressAsText() : ArrayBuffer;
180165

181166
/** Corking a response is a performance improvement in both CPU and network, as you ready the IO system for writing multiple chunks at once.
182-
* By default, you're corked in the immediately executing top portion of the route handler. In all other cases, such as when returning from
183-
* await, or when being called back from an async database request or anything that isn't directly executing in the route handler, you'll want
184-
* to cork before calling writeStatus, writeHeader or just write. Corking takes a callback in which you execute the writeHeader, writeStatus and
185-
* such calls, in one atomic IO operation. This is important, not only for TCP but definitely for TLS where each write would otherwise result
186-
* in one TLS block being sent off, each with one send syscall.
167+
* This is important, not only for TCP but definitely for TLS where each write would otherwise result in one syscall and TLS block being sent off.
168+
* By default, you're corked in the HTTP request handler, until you perform any asynchronous operation.
169+
* After an asynchronous operation, you should cork before calling any "write" or "end" functions.
170+
* Corking takes a callback in which you execute such calls, in one atomic IO operation.
187171
*
188172
* Example usage:
189173
*
190174
* ```
191175
* res.cork(() => {
192-
* res.writeStatus("200 OK").writeHeader("Some", "Value").write("Hello world!");
176+
* res.writeStatus("200 OK").writeHeader("Key", "Value").end("Hello world!");
193177
* });
194178
* ```
195179
*/
@@ -218,7 +202,7 @@ export interface HttpRequest {
218202
getQuery() : string;
219203
/** Returns a decoded query parameter value or undefined. */
220204
getQuery(key: string) : string | undefined;
221-
/** Loops over all headers. */
205+
/** Loops over all headers. Keys and values are in lowercase. */
222206
forEach(cb: (key: string, value: string) => void) : void;
223207
/** Setting yield to true is to say that this route handler did not handle the route, causing the router to continue looking for a matching route handler, or fail. */
224208
setYield(_yield: boolean) : HttpRequest;
@@ -248,15 +232,15 @@ export interface WebSocketBehavior<UserData> {
248232
upgrade?: (res: HttpResponse, req: HttpRequest, context: us_socket_context_t) => void | Promise<void>;
249233
/** Handler for new WebSocket connection. WebSocket is valid from open to close, no errors. */
250234
open?: (ws: WebSocket<UserData>) => void | Promise<void>;
251-
/** Handler for a WebSocket message. Messages are given as ArrayBuffer no matter if they are binary or not. Given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered. */
235+
/** Handler for a WebSocket message. Messages are given as ArrayBuffer no matter if they are binary or not. We Neuter ArrayBuffers on return, making them zero length. */
252236
message?: (ws: WebSocket<UserData>, message: ArrayBuffer, isBinary: boolean) => void | Promise<void>;
253-
/** Handler for a dropped WebSocket message. Messages can be dropped due to specified backpressure settings. Messages are given as ArrayBuffer no matter if they are binary or not. Given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered. */
237+
/** Handler for a dropped WebSocket message. Messages can be dropped due to specified backpressure settings. We Neuter ArrayBuffers on return, making them zero length. */
254238
dropped?: (ws: WebSocket<UserData>, message: ArrayBuffer, isBinary: boolean) => void | Promise<void>;
255239
/** Handler for when WebSocket backpressure drains. Check ws.getBufferedAmount(). Use this to guide / drive your backpressure throttling. */
256240
drain?: (ws: WebSocket<UserData>) => void;
257241
/** Handler for close event, no matter if error, timeout or graceful close. You may not use WebSocket after this event. Do not send on this WebSocket from within here, it is closed. */
258242
close?: (ws: WebSocket<UserData>, code: number, message: ArrayBuffer) => void;
259-
/** Handler for received ping control message. You do not need to handle this, pong messages are automatically sent as per the standard. */
243+
/** Handler for received ping control message. You don't need to send pong messages, they are automatically sent as per the standard. */
260244
ping?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void;
261245
/** Handler for received pong control message. */
262246
pong?: (ws: WebSocket<UserData>, message: ArrayBuffer) => void;
@@ -328,7 +312,7 @@ export interface TemplatedApp {
328312
/** Registers a synchronous callback on missing server names. See /examples/ServerName.js. */
329313
missingServerName(cb: (hostname: string) => void) : TemplatedApp;
330314
/** Attaches a "filter" function to track socket connections / disconnections */
331-
filter(cb: (res: HttpResponse, count: Number) => void | Promise<void>) : TemplatedApp;
315+
filter(cb: (res: HttpResponse, count: number) => void | Promise<void>) : TemplatedApp;
332316
/** Closes all sockets including listen sockets. This will forcefully terminate all connections. */
333317
close() : TemplatedApp;
334318
}

0 commit comments

Comments
 (0)