spooder 6.1.2 → 6.1.3
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 +30 -0
- package/package.json +1 -1
- package/src/api.ts +22 -0
- package/test.ts +0 -10
package/README.md
CHANGED
|
@@ -104,6 +104,7 @@ The `CLI` component of `spooder` is a global command-line tool for running serve
|
|
|
104
104
|
- [API > HTTP > Webhooks](#api-http-webhooks)
|
|
105
105
|
- [API > HTTP > Websocket Server](#api-http-websockets)
|
|
106
106
|
- [API > HTTP > Bootstrap](#api-http-bootstrap)
|
|
107
|
+
- [API > HTTP > Cookies](#api-http-cookies)
|
|
107
108
|
- [API > Error Handling](#api-error-handling)
|
|
108
109
|
- [API > Workers](#api-workers)
|
|
109
110
|
- [API > Caching](#api-caching)
|
|
@@ -1416,6 +1417,35 @@ server.websocket('/path/to/websocket', {
|
|
|
1416
1417
|
> [!IMPORTANT]
|
|
1417
1418
|
> While it is possible to register multiple routes for websockets, the only handler which is unique per route is `accept()`. The last handlers provided to any route (with the exception of `accept()`) will apply to ALL websocket routes. This is a limitation in Bun.
|
|
1418
1419
|
|
|
1420
|
+
<a id="api-http-cookies"></a>
|
|
1421
|
+
## API > HTTP > Cookies
|
|
1422
|
+
|
|
1423
|
+
### 🔧 `cookies_get(req: Request): Bun.CookieMap`
|
|
1424
|
+
|
|
1425
|
+
When called on a request, the `cookies_get` function will return a `Bun.CookieMap` contains all of the cookies parsed from the `Cookie` header on the request.
|
|
1426
|
+
|
|
1427
|
+
```ts
|
|
1428
|
+
server.route('/', (req, url) => {
|
|
1429
|
+
const cookies = cookies_get(req);
|
|
1430
|
+
return `Hello ${cookies.get('person') ?? 'unknown'}`;
|
|
1431
|
+
});
|
|
1432
|
+
```
|
|
1433
|
+
|
|
1434
|
+
The return `Bun.CookieMap` is an iterable map with a custom API for reading/setting cookies. The full API [can be seen here](https://bun.com/docs/runtime/cookies).
|
|
1435
|
+
|
|
1436
|
+
Any changes made to the cookie map (adding, deletion, editing, etc) will be sent as `Set-Cookie` headers on the response automatically. Unchanged cookies are not sent.
|
|
1437
|
+
|
|
1438
|
+
```ts
|
|
1439
|
+
server.route('/', (req, url) => {
|
|
1440
|
+
const cookies = cookies_get(req);
|
|
1441
|
+
cookies.set('test', 'foobar');
|
|
1442
|
+
return 'Hello, world!';
|
|
1443
|
+
|
|
1444
|
+
// the response automatically gets:
|
|
1445
|
+
// Set-Cookie test=foobar; Path=/; SameSite=Lax
|
|
1446
|
+
});
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1419
1449
|
<a id="api-http-bootstrap"></a>
|
|
1420
1450
|
## API > HTTP > Bootstrap
|
|
1421
1451
|
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -1143,6 +1143,26 @@ export function git_get_hashes_sync(length = 7): Record<string, string> {
|
|
|
1143
1143
|
}
|
|
1144
1144
|
// endregion
|
|
1145
1145
|
|
|
1146
|
+
// region cookies
|
|
1147
|
+
const cookie_map = new WeakMap<Request, Bun.CookieMap>();
|
|
1148
|
+
|
|
1149
|
+
export function cookies_get(req: Request): Bun.CookieMap {
|
|
1150
|
+
const jar = new Bun.CookieMap(req.headers.get('Cookie') ?? undefined);
|
|
1151
|
+
cookie_map.set(req, jar);
|
|
1152
|
+
return jar;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
function apply_cookies(req: Request, res: Response) {
|
|
1156
|
+
const jar = cookie_map.get(req);
|
|
1157
|
+
if (jar === undefined)
|
|
1158
|
+
return;
|
|
1159
|
+
|
|
1160
|
+
const cookies = jar.toSetCookieHeaders();
|
|
1161
|
+
for (const cookie of cookies)
|
|
1162
|
+
res.headers.append('Set-Cookie', cookie);
|
|
1163
|
+
}
|
|
1164
|
+
// endregion
|
|
1165
|
+
|
|
1146
1166
|
// region serving
|
|
1147
1167
|
export const HTTP_STATUS_TEXT: Record<number, string> = {
|
|
1148
1168
|
// 1xx Informational Response
|
|
@@ -1736,6 +1756,8 @@ export function http_serve(port: number, hostname?: string) {
|
|
|
1736
1756
|
|
|
1737
1757
|
const response = await generate_response(req, url);
|
|
1738
1758
|
const request_time = Date.now() - request_start;
|
|
1759
|
+
|
|
1760
|
+
apply_cookies(req, response);
|
|
1739
1761
|
|
|
1740
1762
|
const is_known_slow = slow_requests.has(req);
|
|
1741
1763
|
if (slow_request_callback !== null && request_time > slow_request_threshold && !is_known_slow)
|
package/test.ts
DELETED