spooder 6.1.2 → 6.1.4
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 +26 -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,30 @@ 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
|
+
let jar = cookie_map.get(req);
|
|
1151
|
+
if (jar !== undefined)
|
|
1152
|
+
return jar;
|
|
1153
|
+
|
|
1154
|
+
jar = new Bun.CookieMap(req.headers.get('Cookie') ?? undefined);
|
|
1155
|
+
cookie_map.set(req, jar);
|
|
1156
|
+
return jar;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
function apply_cookies(req: Request, res: Response) {
|
|
1160
|
+
const jar = cookie_map.get(req);
|
|
1161
|
+
if (jar === undefined)
|
|
1162
|
+
return;
|
|
1163
|
+
|
|
1164
|
+
const cookies = jar.toSetCookieHeaders();
|
|
1165
|
+
for (const cookie of cookies)
|
|
1166
|
+
res.headers.append('Set-Cookie', cookie);
|
|
1167
|
+
}
|
|
1168
|
+
// endregion
|
|
1169
|
+
|
|
1146
1170
|
// region serving
|
|
1147
1171
|
export const HTTP_STATUS_TEXT: Record<number, string> = {
|
|
1148
1172
|
// 1xx Informational Response
|
|
@@ -1736,6 +1760,8 @@ export function http_serve(port: number, hostname?: string) {
|
|
|
1736
1760
|
|
|
1737
1761
|
const response = await generate_response(req, url);
|
|
1738
1762
|
const request_time = Date.now() - request_start;
|
|
1763
|
+
|
|
1764
|
+
apply_cookies(req, response);
|
|
1739
1765
|
|
|
1740
1766
|
const is_known_slow = slow_requests.has(req);
|
|
1741
1767
|
if (slow_request_callback !== null && request_time > slow_request_threshold && !is_known_slow)
|
package/test.ts
DELETED