spooder 4.4.7 → 4.4.9

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.
Files changed (3) hide show
  1. package/README.md +12 -2
  2. package/package.json +2 -2
  3. package/src/api.ts +11 -4
package/README.md CHANGED
@@ -220,7 +220,10 @@ You can utilize this to automatically update your server in response to a webhoo
220
220
 
221
221
  ```ts
222
222
  server.webhook(process.env.WEBHOOK_SECRET, '/webhook', payload => {
223
- setImmediate(() => server.stop(false));
223
+ setImmediate(async () => {
224
+ await server.stop(false);
225
+ process.exit();
226
+ });
224
227
  return 200;
225
228
  });
226
229
  ```
@@ -930,6 +933,12 @@ Stop the server process gracefully, waiting for all in-flight requests to comple
930
933
  server.stop(false);
931
934
  ```
932
935
 
936
+ `server.stop()` returns a promise, which if awaited, resolves when all pending connections have been completed.
937
+ ```ts
938
+ await server.stop(false);
939
+ // do something now all connections are done
940
+ ```
941
+
933
942
  <a id="api-error-handling"></a>
934
943
  ## API > Error Handling
935
944
 
@@ -1306,7 +1315,8 @@ type CookieOptions = {
1306
1315
  http_only?: boolean,
1307
1316
  path?: string,
1308
1317
  expires?: number,
1309
- encode?: boolean
1318
+ encode?: boolean,
1319
+ max_age?: number
1310
1320
  };
1311
1321
  ```
1312
1322
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.4.7",
4
+ "version": "4.4.9",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "module": "./src/api.ts",
12
12
  "devDependencies": {
13
- "@types/bun": "^1.0.5"
13
+ "@types/bun": "^1.1.8"
14
14
  },
15
15
  "optionalDependencies": {
16
16
  "mysql2": "^3.11.0"
package/src/api.ts CHANGED
@@ -406,7 +406,8 @@ export type CookieOptions = {
406
406
  http_only?: boolean,
407
407
  path?: string,
408
408
  expires?: number,
409
- encode?: boolean
409
+ encode?: boolean,
410
+ max_age?: number
410
411
  };
411
412
 
412
413
  export function set_cookie(res: Response, name: string, value: string, options?: CookieOptions): void {
@@ -430,6 +431,9 @@ export function set_cookie(res: Response, name: string, value: string, options?:
430
431
  const date = new Date(Date.now() + options.expires);
431
432
  cookie += '; Expires=' + date.toUTCString();
432
433
  }
434
+
435
+ if (options.max_age !== undefined)
436
+ cookie += '; Max-Age=' + options.max_age;
433
437
  } else {
434
438
  cookie += value;
435
439
  }
@@ -560,7 +564,7 @@ export function validate_req_json(json_handler: JSONRequestHandler): RequestHand
560
564
  if (json === null || typeof json !== 'object' || Array.isArray(json))
561
565
  return 400; // Bad Request
562
566
 
563
- return json_handler(req, url, json);
567
+ return json_handler(req, url, json as JsonObject);
564
568
  } catch (e) {
565
569
  return 400; // Bad Request
566
570
  }
@@ -723,7 +727,7 @@ export function serve(port: number) {
723
727
  development: false,
724
728
 
725
729
  async fetch(req: Request): Promise<Response> {
726
- const url = new URL(req.url);
730
+ const url = new URL(req.url) as URL;
727
731
  const request_start = Date.now();
728
732
 
729
733
  const response = await generate_response(req, url);
@@ -803,8 +807,11 @@ export function serve(port: number) {
803
807
  },
804
808
 
805
809
  /** Stops the server. */
806
- stop: (immediate = false): void => {
810
+ stop: async (immediate = false): Promise<void> => {
807
811
  server.stop(immediate);
812
+
813
+ while (server.pendingRequests > 0)
814
+ await Bun.sleep(1000);
808
815
  },
809
816
 
810
817
  /** Register a handler for server-sent events. */