spooder 3.2.0 → 3.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/README.md CHANGED
@@ -613,6 +613,14 @@ server.dir('/content', './public/content', { index: 'index.html' });
613
613
 
614
614
  The above will serve `./public/content/index.html` when `/content` is requested.
615
615
 
616
+ #### `server.stop(method: ServerStop)`
617
+
618
+ The `stop` function allows you to stop the server. `method` is one of `ServerStop.IMMEDIATE` or `ServerStop.GRACEFUL`.
619
+
620
+ `ServerStop.GRACEFUL` will stop accepting new requests and wait for all in-flight requests to complete before stopping the server. This is the default behavior.
621
+
622
+ `ServerStop.IMMEDIATE` will immediately stop the server, terminating all in-flight requests.
623
+
616
624
  ---
617
625
 
618
626
  #### `route_location(redirect_url: string)`
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "3.2.0",
4
+ "version": "3.2.1",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.d.ts CHANGED
@@ -13,6 +13,13 @@ type DirOptions = {
13
13
  };
14
14
  /** Built-in route handler for redirecting to a different URL. */
15
15
  export declare function route_location(redirect_url: string): (req: Request, url: URL) => Response;
16
+ export declare const ServerStop: {
17
+ /** Stops the server immediately, terminating in-flight requests. */
18
+ IMMEDIATE: number;
19
+ /** Stops the server after all in-flight requests have completed. */
20
+ GRACEFUL: number;
21
+ };
22
+ type ServerStop = typeof ServerStop[keyof typeof ServerStop];
16
23
  export declare function serve(port: number): {
17
24
  /** Register a handler for a specific route. */
18
25
  route: (path: string, handler: RequestHandler) => void;
@@ -24,5 +31,7 @@ export declare function serve(port: number): {
24
31
  handle: (status_code: number, handler: StatusCodeHandler) => void;
25
32
  /** Register a handler for uncaught errors. */
26
33
  error: (handler: ErrorHandler) => void;
34
+ /** Stops the server. */
35
+ stop: (method?: ServerStop) => void;
27
36
  };
28
37
  export {};
package/src/api.ts CHANGED
@@ -98,6 +98,16 @@ function route_directory(route_path: string, dir: string, options: DirOptions):
98
98
  };
99
99
  }
100
100
 
101
+ export const ServerStop = {
102
+ /** Stops the server immediately, terminating in-flight requests. */
103
+ IMMEDIATE: 0,
104
+
105
+ /** Stops the server after all in-flight requests have completed. */
106
+ GRACEFUL: 1
107
+ };
108
+
109
+ type ServerStop = typeof ServerStop[keyof typeof ServerStop];
110
+
101
111
  export function serve(port: number) {
102
112
  const routes = new Map<string[], RequestHandler>();
103
113
  const handlers = new Map<number, StatusCodeHandler>();
@@ -238,6 +248,11 @@ export function serve(port: number) {
238
248
  /** Register a handler for uncaught errors. */
239
249
  error: (handler: ErrorHandler): void => {
240
250
  error_handler = handler;
251
+ },
252
+
253
+ /** Stops the server. */
254
+ stop: (method: ServerStop = ServerStop.GRACEFUL): void => {
255
+ server.stop(method === ServerStop.IMMEDIATE);
241
256
  }
242
257
  }
243
258
  }