spooder 4.4.3 → 4.4.5
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 +26 -0
- package/package.json +1 -1
- package/src/api.ts +12 -1
package/README.md
CHANGED
|
@@ -46,7 +46,9 @@ The `CLI` component of `spooder` is a global command-line tool for running serve
|
|
|
46
46
|
- [`server.handle(status_code: number, handler: RequestHandler)`](#api-routing-server-handle)
|
|
47
47
|
- [`server.default(handler: DefaultHandler)`](#api-routing-server-default)
|
|
48
48
|
- [`server.error(handler: ErrorHandler)`](#api-routing-server-error)
|
|
49
|
+
- [API > Routing > Slow Requests](#api-routing-slow-requests)
|
|
49
50
|
- [`server.on_slow_request(callback: SlowRequestCallback, threshold: number)`](#api-routing-server-on-slow-request)
|
|
51
|
+
- [`server.allow_slow_request(req: Request)`](#api-routing-server-allow-slow-request)
|
|
50
52
|
- [API > Routing > Validation](#api-routing-validation)
|
|
51
53
|
- [`validate_req_json(handler: JSONRequestHandler)`](#api-routing-validate-req-json)
|
|
52
54
|
- [API > Routing > Directory Serving](#api-routing-directory-serving)
|
|
@@ -671,6 +673,9 @@ server.error((err, req, url) => {
|
|
|
671
673
|
});
|
|
672
674
|
```
|
|
673
675
|
|
|
676
|
+
<a id="api-routing-slow-requests"></a>
|
|
677
|
+
## API > Routing > Slow Requests
|
|
678
|
+
|
|
674
679
|
<a id="api-routing-server-on-slow-request"></a>
|
|
675
680
|
### 🔧 `server.on_slow_request(callback: SlowRequestCallback, threshold: number)`
|
|
676
681
|
|
|
@@ -692,6 +697,27 @@ server.on_slow_request(async (req, time, url) => {
|
|
|
692
697
|
> [!NOTE]
|
|
693
698
|
> The callback is not awaited internally, so you can use `async/await` freely without blocking the server/request.
|
|
694
699
|
|
|
700
|
+
<a id="api-routing-server-allow-slow-request"></a>
|
|
701
|
+
### 🔧 `server.allow_slow_request(req: Request)`
|
|
702
|
+
|
|
703
|
+
In some scenarios, mitigation throttling or heavy workloads may cause slow requests intentionally. To prevent these triggering a caution, requests can be marked as slow.
|
|
704
|
+
|
|
705
|
+
```ts
|
|
706
|
+
server.on_slow_request(async (req, time, url) => {
|
|
707
|
+
await caution('Slow request warning', { req, time });
|
|
708
|
+
}, 500);
|
|
709
|
+
|
|
710
|
+
server.route('/test', async (req) => {
|
|
711
|
+
// this request is marked as slow, therefore won't
|
|
712
|
+
// trigger on_slow_request despite taking 5000ms+
|
|
713
|
+
server.allow_slow_request(req);
|
|
714
|
+
await new Promise(res => setTimeout(res, 5000));
|
|
715
|
+
});
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
> [!NOTE]
|
|
719
|
+
> This will have no effect if a handler hasn't been registered with `on_slow_request`.
|
|
720
|
+
|
|
695
721
|
<a id="api-routing-validation"></a>
|
|
696
722
|
## API > Routing > Validation
|
|
697
723
|
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -698,6 +698,8 @@ export function serve(port: number) {
|
|
|
698
698
|
let slow_request_callback: SlowRequestCallback | null = null;
|
|
699
699
|
let slow_request_threshold: number = 1000;
|
|
700
700
|
|
|
701
|
+
const slow_requests = new WeakSet();
|
|
702
|
+
|
|
701
703
|
const server = Bun.serve({
|
|
702
704
|
port,
|
|
703
705
|
development: false,
|
|
@@ -709,9 +711,13 @@ export function serve(port: number) {
|
|
|
709
711
|
const response = await generate_response(req, url);
|
|
710
712
|
const request_time = Date.now() - request_start;
|
|
711
713
|
|
|
712
|
-
|
|
714
|
+
const is_known_slow = slow_requests.has(req);
|
|
715
|
+
if (slow_request_callback !== null && request_time > slow_request_threshold && !is_known_slow)
|
|
713
716
|
slow_request_callback(req, request_time, url);
|
|
714
717
|
|
|
718
|
+
if (is_known_slow)
|
|
719
|
+
slow_requests.delete(req);
|
|
720
|
+
|
|
715
721
|
return print_request_info(req, response, url, request_time);
|
|
716
722
|
}
|
|
717
723
|
});
|
|
@@ -758,6 +764,11 @@ export function serve(port: number) {
|
|
|
758
764
|
slow_request_threshold = threshold;
|
|
759
765
|
},
|
|
760
766
|
|
|
767
|
+
/** Mark a request as slow, preventing it from triggering slow request callback. */
|
|
768
|
+
allow_slow_request: (req: Request): void => {
|
|
769
|
+
slow_requests.add(req);
|
|
770
|
+
},
|
|
771
|
+
|
|
761
772
|
/** Register a default handler for all status codes. */
|
|
762
773
|
default: (handler: DefaultHandler): void => {
|
|
763
774
|
default_handler = handler;
|