@thoughtpivot/flight 3.0.0 → 3.0.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/CHANGELOG.md +17 -0
- package/LICENSE +202 -0
- package/NOTICE +1 -0
- package/README.md +53 -10
- package/package.json +4 -2
- package/src/bun/app.test.ts +275 -1
- package/src/bun/app.ts +24 -5
- package/src/bun/cache.ts +2 -1
- package/src/bun/config.ts +19 -0
- package/src/bun/koa-compat.ts +230 -15
- package/src/bun/middleware.ts +44 -5
- package/src/bun/session.ts +9 -6
- package/LICENSE.md +0 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.0.1
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- **Koa streaming on Bun.** A `router.routes()` backend can server-sent-event the usual Koa way (`ctx.respond = false`, `writeHead`, `write`, `end`). `flight-bun` returns a `ReadableStream` when that body starts, instead of buffering until `res.end()`. A stream that never calls `end()` no longer hangs with an empty body.
|
|
8
|
+
- A Koa route that finishes the response inside the handler is kept. `flight-bun` no longer drops it and answers with the SPA `404`. A router mounted ahead of the one that handles the request is included: later routers append to `ctx.matched` with `push`, and the match is read when the stream opens or the response finishes.
|
|
9
|
+
- Client disconnect emits `close` on the mock response, so `res.on('close')` can clear heartbeats and subscribers.
|
|
10
|
+
- Gzip skips `text/event-stream` and any body that is still streaming, so compression cannot wait on an open stream. The response cache skips those bodies too.
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **`FLIGHT_IDLE_TIMEOUT`** (seconds). Default `10` (Bun's own default). An explicit `0` disables the server idle timeout. Positive fractions round up to at least `1`. Negative values and values that are not numbers use `10`. Values above `255` are clamped to Bun's maximum. Server-sent event responses disable the idle timeout for that request.
|
|
15
|
+
|
|
16
|
+
### Documentation
|
|
17
|
+
|
|
18
|
+
- On Bun, `FLIGHT_MAX_WORKERS` and Node's `cluster` module do not apply (`flight-bun` is one process). The Bun rate limiter is an in-memory per-process `Map` and does not use Redis.
|
|
19
|
+
|
|
3
20
|
## 3.0.0
|
|
4
21
|
|
|
5
22
|
### Breaking
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright 2026 ThoughtPivot, Inc.
|
package/README.md
CHANGED
|
@@ -10,6 +10,37 @@ Think **platform-agnostic**: not framework-as-a-platform, but a clear runtime yo
|
|
|
10
10
|
|
|
11
11
|
Flight is **open source** from **[ThoughtPivot](https://github.com/thoughtpivot)**.
|
|
12
12
|
|
|
13
|
+
## ⚡ Now on Bun
|
|
14
|
+
|
|
15
|
+
`flight-bun` runs Flight on `Bun.serve`. On a single pinned core, at the same commit, compared with the Node `flight` binary (hello-route micro-benchmark):
|
|
16
|
+
|
|
17
|
+
| | Node (`flight`) | Bun (`flight-bun`) |
|
|
18
|
+
| ------------------------- | --------------------------- | ------------------ |
|
|
19
|
+
| Req/s, Bun-native route | 19,651 | **61,110** (3.1x) |
|
|
20
|
+
| Req/s, existing Koa route | 19,651 | **38,826** (2.0x) |
|
|
21
|
+
| p99 latency | 10-25 ms | **~3 ms** (native) |
|
|
22
|
+
| Start to first response | 1.18 s (TypeScript via tsx) | **47 ms** |
|
|
23
|
+
| Idle RSS | 218 MB (primary + 1 worker) | **27 MB** |
|
|
24
|
+
|
|
25
|
+
**What changed:**
|
|
26
|
+
|
|
27
|
+
- native `Bun.serve` routes
|
|
28
|
+
- Bun-native middleware (security headers, CORS, gzip, rate limit, sessions, cache)
|
|
29
|
+
- `.backend.ts` loaded directly, with no build step
|
|
30
|
+
- Redis is optional and falls back to memory
|
|
31
|
+
|
|
32
|
+
Existing `export default router.routes()` backends mount unchanged through the Koa-compat layer.
|
|
33
|
+
|
|
34
|
+
**Note:** `flight-bun` is a single process. `FLIGHT_MAX_WORKERS` and Node's `cluster` module are not used. On 4 cores, a 4-worker Node cluster served more requests (80.7k vs 53.8k req/s), so scale `flight-bun` with replicas. The Bun rate limiter is an in-memory `Map` in that process and does not use Redis. The two sides run their own default middleware, which differs. Method and raw data: https://github.com/thoughtpivot/flight/blob/main/bench/BENCHMARKS.md (scripts in [`bench/`](https://github.com/thoughtpivot/flight/tree/main/bench)).
|
|
35
|
+
|
|
36
|
+
`bun src/bun/server.ts` or `flight-bun`
|
|
37
|
+
|
|
38
|
+
Early Bun research and benchmarks for Flight and Scribe by Vasilen Lyubomirov ([github.com/VasilenL](https://github.com/VasilenL)), whose work helped start this.
|
|
39
|
+
|
|
40
|
+
Setup, environment variables and the backend contract: [Bun runtime](#bun-runtime).
|
|
41
|
+
|
|
42
|
+
#thoughtpivot
|
|
43
|
+
|
|
13
44
|
## Highlights
|
|
14
45
|
|
|
15
46
|
- **Two runtimes**: **Node / Koa** (`flight`) and **Bun** (`flight-bun`). The Bun server loads TypeScript directly, exposes `GET /healthz`, and still accepts existing Koa backends
|
|
@@ -387,6 +418,17 @@ FLIGHT_MODE=development bun src/bun/server.ts
|
|
|
387
418
|
|
|
388
419
|
The Bun server uses `Bun.serve` and loads TypeScript directly. It exposes `GET /healthz` and the same edge jobs as the Node server (logging, security headers, CORS, gzip, rate limit, sessions, response cache) without Koa packages on the native path. Redis is optional (`FLIGHT_REDIS_URL`); without it, sessions and cache use memory.
|
|
389
420
|
|
|
421
|
+
**What does not carry over from the Node server**
|
|
422
|
+
|
|
423
|
+
- **Cluster and `FLIGHT_MAX_WORKERS`.** The Node server forks workers up to `FLIGHT_MAX_WORKERS` (default: one per CPU). `flight-bun` does not read that variable and does not use Node's `cluster` module. It is one process. Use more replicas to fill more cores.
|
|
424
|
+
- **Rate-limit storage.** The Bun limiter keeps counters in a process-local `Map`. It does not use Redis, including when `FLIGHT_REDIS_URL` is set. That URL is for sessions and the response cache. Replicas do not share a window, so a limit of 100 across four processes allows 400 requests in the window. The Node production limiter is Redis-backed.
|
|
425
|
+
|
|
426
|
+
**Streaming and server-sent events**
|
|
427
|
+
|
|
428
|
+
Koa routes can stream. Set `ctx.respond = false`, send the status line with `writeHead` (for example `content-type: text/event-stream`), and write chunks with `ctx.res.write`. `flight-bun` returns that body as a `ReadableStream` as soon as the stream starts, instead of waiting for `res.end()`. Gzip skips `text/event-stream` and any other body that is still streaming. A client disconnect emits `close` on `ctx.res`; stop heartbeats and drop subscribers there. Set the headers and the first write before the first `await` so they are on the response that is already in flight.
|
|
429
|
+
|
|
430
|
+
`FLIGHT_IDLE_TIMEOUT` is the `Bun.serve` idle timeout in seconds. The default is `10`, the same as Bun. An explicit `0` disables it. Positive fractions round up to at least `1` (`0.5` becomes `1`). Negative values and values that are not numbers use the default `10`. Values above `255` are clamped to Bun's maximum. Event-stream responses call `server.timeout(req, 0)` so a heartbeat slower than the idle timeout stays connected.
|
|
431
|
+
|
|
390
432
|
**Two backend contracts can be mixed in one app.**
|
|
391
433
|
|
|
392
434
|
Bun-native files default-export a path map. Handlers are `(req: Request) => Response`. Path params use Bun's `:id` routes.
|
|
@@ -403,16 +445,17 @@ Existing Koa files keep working. `export default router.routes()` is mounted thr
|
|
|
403
445
|
|
|
404
446
|
In development (`FLIGHT_MODE=development`) the API stays on `FLIGHT_PORT` (default 3000) and Vite is spawned on 3001. In production the built SPA is read from `FLIGHT_DIST_PATH` (default `<app home>/dist`) with an `index.html` fallback. `/api`, `/health`, and `/healthz` do not fall back.
|
|
405
447
|
|
|
406
|
-
| Variable | Default | Bun runtime
|
|
407
|
-
| ------------------------ | ---------------- |
|
|
408
|
-
| `FLIGHT_SESSION_SECRET` | unset | Sessions stay **off** until this is set. There is no baked-in secret.
|
|
409
|
-
| `FLIGHT_RATE_LIMIT_MAX` | `0` | Rate limit stays **off** until this is greater than zero.
|
|
410
|
-
| `FLIGHT_TRUST_PROXY` | false | When `1` / `true` / `yes`, the limiter uses `X-Forwarded-For`. Otherwise that header is ignored.
|
|
411
|
-
| `FLIGHT_REDIS_URL` | unset | `redis://host:6379` for shared sessions and cache.
|
|
412
|
-
| `FLIGHT_CACHE_ENABLED` | false | Cache GET responses that send `Cache-Control: max-age`.
|
|
413
|
-
| `FLIGHT_STATIC_PREFIXES` | `/assets,/fonts` | GET/HEAD under these prefixes skip the rate limit.
|
|
448
|
+
| Variable | Default | Bun runtime |
|
|
449
|
+
| ------------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
450
|
+
| `FLIGHT_SESSION_SECRET` | unset | Sessions stay **off** until this is set. There is no baked-in secret. |
|
|
451
|
+
| `FLIGHT_RATE_LIMIT_MAX` | `0` | Rate limit stays **off** until this is greater than zero. |
|
|
452
|
+
| `FLIGHT_TRUST_PROXY` | false | When `1` / `true` / `yes`, the limiter uses `X-Forwarded-For`. Otherwise that header is ignored. |
|
|
453
|
+
| `FLIGHT_REDIS_URL` | unset | `redis://host:6379` for shared sessions and cache. |
|
|
454
|
+
| `FLIGHT_CACHE_ENABLED` | false | Cache GET responses that send `Cache-Control: max-age`. |
|
|
455
|
+
| `FLIGHT_STATIC_PREFIXES` | `/assets,/fonts` | GET/HEAD under these prefixes skip the rate limit. |
|
|
456
|
+
| `FLIGHT_IDLE_TIMEOUT` | `10` | Idle seconds before Bun closes a socket. Explicit `0` disables it. Fractions round up to at least 1. Negative or invalid values use `10`. Above `255` is clamped. SSE turns this off per request. |
|
|
414
457
|
|
|
415
|
-
Other `FLIGHT_*` knobs from the Node server (`FLIGHT_MODE`, `FLIGHT_PORT`, `FLIGHT_APP_HOME`, `FLIGHT_EXCLUDE_PATHS`, `FLIGHT_SPA_DENY_PREFIXES`, `FLIGHT_PAYLOAD_LIMIT`) apply here too. `GET /healthz` returns `ok` and is not rate limited.
|
|
458
|
+
Other `FLIGHT_*` knobs from the Node server (`FLIGHT_MODE`, `FLIGHT_PORT`, `FLIGHT_APP_HOME`, `FLIGHT_EXCLUDE_PATHS`, `FLIGHT_SPA_DENY_PREFIXES`, `FLIGHT_PAYLOAD_LIMIT`) apply here too. `FLIGHT_MAX_WORKERS` does not: see the cluster note above. `GET /healthz` returns `ok` and is not rate limited.
|
|
416
459
|
|
|
417
460
|
## Requirements
|
|
418
461
|
|
|
@@ -455,7 +498,7 @@ Dry-run without publishing: Actions → **Publish** → **Run workflow** → lea
|
|
|
455
498
|
|
|
456
499
|
## License
|
|
457
500
|
|
|
458
|
-
|
|
501
|
+
Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
|
459
502
|
|
|
460
503
|
## Contributing
|
|
461
504
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thoughtpivot/flight",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "A web server designed for running full stack, component based applications at enterprise scale.",
|
|
5
5
|
"main": "src/flight.ts",
|
|
6
6
|
"publishConfig": {
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"url": "git+https://github.com/thoughtpivot/flight.git"
|
|
28
28
|
},
|
|
29
29
|
"author": "",
|
|
30
|
-
"license": "
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
31
|
"bugs": {
|
|
32
32
|
"url": "https://github.com/thoughtpivot/flight/issues"
|
|
33
33
|
},
|
|
@@ -39,6 +39,8 @@
|
|
|
39
39
|
"src/bun",
|
|
40
40
|
"README.md",
|
|
41
41
|
"CHANGELOG.md",
|
|
42
|
+
"LICENSE",
|
|
43
|
+
"NOTICE",
|
|
42
44
|
"readme-assets"
|
|
43
45
|
],
|
|
44
46
|
"dependencies": {
|
package/src/bun/app.test.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { afterEach, expect, test } from 'bun:test'
|
|
2
|
-
import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
|
|
3
3
|
import { tmpdir } from 'node:os'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
5
|
|
|
6
|
+
import Router from '@koa/router'
|
|
7
|
+
|
|
6
8
|
import { openFlight, type FlightServer } from './app'
|
|
7
9
|
import { loadConfig, type Config } from './config'
|
|
10
|
+
import { createKoaDispatcher } from './koa-compat'
|
|
8
11
|
import { safeTarget } from './static'
|
|
9
12
|
|
|
10
13
|
const servers: FlightServer[] = []
|
|
@@ -189,3 +192,274 @@ test('opt-in response cache serves the second GET from the store', async () => {
|
|
|
189
192
|
expect(hit.headers.get('x-flight-cache')).toBe('HIT')
|
|
190
193
|
expect(await hit.json()).toEqual({ n: 1 })
|
|
191
194
|
})
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Add a Koa backend that streams server-sent events and ends one route early.
|
|
198
|
+
* A router that does not handle these paths is mounted first, in this file, so the
|
|
199
|
+
* order does not depend on how the directory is listed.
|
|
200
|
+
*/
|
|
201
|
+
async function writeStreamBackend(dir: string, flagPath: string, gapMs: number): Promise<void> {
|
|
202
|
+
await writeFile(
|
|
203
|
+
join(dir, 'stream.backend.ts'),
|
|
204
|
+
`import Router from '@koa/router'
|
|
205
|
+
import { writeFileSync } from 'node:fs'
|
|
206
|
+
const miss = new Router()
|
|
207
|
+
miss.get('/api/not-events', (ctx) => {
|
|
208
|
+
ctx.body = { ok: true }
|
|
209
|
+
})
|
|
210
|
+
const router = new Router()
|
|
211
|
+
const flag = ${JSON.stringify(flagPath)}
|
|
212
|
+
const gapMs = ${gapMs}
|
|
213
|
+
|
|
214
|
+
router.get('/api/events', async (ctx) => {
|
|
215
|
+
ctx.respond = false
|
|
216
|
+
ctx.res.writeHead(200, {
|
|
217
|
+
'content-type': 'text/event-stream',
|
|
218
|
+
'cache-control': 'no-cache',
|
|
219
|
+
connection: 'keep-alive'
|
|
220
|
+
})
|
|
221
|
+
ctx.res.write('data: one\\n\\n')
|
|
222
|
+
await new Promise((resolve) => setTimeout(resolve, gapMs))
|
|
223
|
+
ctx.res.write('data: two\\n\\n')
|
|
224
|
+
ctx.res.end()
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
router.get('/api/events-open', (ctx) => {
|
|
228
|
+
ctx.respond = false
|
|
229
|
+
ctx.res.writeHead(200, {
|
|
230
|
+
'content-type': 'text/event-stream',
|
|
231
|
+
'cache-control': 'no-cache'
|
|
232
|
+
})
|
|
233
|
+
ctx.res.write('data: hello\\n\\n')
|
|
234
|
+
const timer = setInterval(() => {
|
|
235
|
+
ctx.res.write('data: beat\\n\\n')
|
|
236
|
+
}, 400)
|
|
237
|
+
timer.unref()
|
|
238
|
+
ctx.res.on('close', () => {
|
|
239
|
+
clearInterval(timer)
|
|
240
|
+
writeFileSync(flag, 'closed')
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
router.get('/api/early-end', (ctx) => {
|
|
245
|
+
ctx.respond = false
|
|
246
|
+
ctx.res.writeHead(201, { 'content-type': 'text/plain; charset=utf-8' })
|
|
247
|
+
try {
|
|
248
|
+
ctx.res.write('from-route')
|
|
249
|
+
} finally {
|
|
250
|
+
ctx.res.end()
|
|
251
|
+
}
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
router.get('/api/big-text', (ctx) => {
|
|
255
|
+
ctx.body = 'z'.repeat(2048)
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
export default (ctx, next) => miss.routes()(ctx, () => router.routes()(ctx, next))
|
|
259
|
+
`
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Read from an open body until `needle` shows up, and record how long that chunk took.
|
|
265
|
+
*/
|
|
266
|
+
async function readUntil(
|
|
267
|
+
reader: ReadableStreamDefaultReader<Uint8Array>,
|
|
268
|
+
needle: string
|
|
269
|
+
): Promise<{ text: string; firstByteMs: number }> {
|
|
270
|
+
const decoder = new TextDecoder()
|
|
271
|
+
const started = performance.now()
|
|
272
|
+
let text = ''
|
|
273
|
+
let firstByteMs = 0
|
|
274
|
+
while (!text.includes(needle)) {
|
|
275
|
+
const { value, done } = await reader.read()
|
|
276
|
+
if (done) break
|
|
277
|
+
if (!text) firstByteMs = performance.now() - started
|
|
278
|
+
text += decoder.decode(value, { stream: true })
|
|
279
|
+
}
|
|
280
|
+
return { text, firstByteMs }
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
test('FLIGHT_IDLE_TIMEOUT defaults to 10 seconds and clamps to Bun maximum', () => {
|
|
284
|
+
expect(loadConfig({}).idleTimeout).toBe(10)
|
|
285
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '0' }).idleTimeout).toBe(0)
|
|
286
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '0.0' }).idleTimeout).toBe(0)
|
|
287
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '0.5' }).idleTimeout).toBe(1)
|
|
288
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '1.2' }).idleTimeout).toBe(2)
|
|
289
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '30' }).idleTimeout).toBe(30)
|
|
290
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '999' }).idleTimeout).toBe(255)
|
|
291
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '-5' }).idleTimeout).toBe(10)
|
|
292
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: 'nope' }).idleTimeout).toBe(10)
|
|
293
|
+
expect(loadConfig({ FLIGHT_IDLE_TIMEOUT: '' }).idleTimeout).toBe(10)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Two routers in a fixed order. The first does not handle the path under test.
|
|
298
|
+
* `@koa/router` assigns `ctx.matched` for that first router and only `push`es after that.
|
|
299
|
+
*/
|
|
300
|
+
function orderedRouters(setup: (router: Router) => void) {
|
|
301
|
+
const miss = new Router()
|
|
302
|
+
miss.get('/api/not-events', (ctx) => {
|
|
303
|
+
ctx.body = { ok: true }
|
|
304
|
+
})
|
|
305
|
+
const hit = new Router()
|
|
306
|
+
setup(hit)
|
|
307
|
+
return [miss.routes(), hit.routes()]
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
test('a non-matching router mounted first still streams the later router', async () => {
|
|
311
|
+
const dispatch = await createKoaDispatcher(
|
|
312
|
+
orderedRouters((router) => {
|
|
313
|
+
router.get('/api/events', (ctx) => {
|
|
314
|
+
ctx.respond = false
|
|
315
|
+
ctx.res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' })
|
|
316
|
+
ctx.res.write('data: one\n\n')
|
|
317
|
+
})
|
|
318
|
+
}),
|
|
319
|
+
'1mb'
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
const response = await dispatch(new Request('http://127.0.0.1/api/events'))
|
|
323
|
+
expect(response).not.toBeNull()
|
|
324
|
+
expect(response!.status).toBe(200)
|
|
325
|
+
expect(response!.headers.get('content-type')).toContain('text/event-stream')
|
|
326
|
+
const reader = response!.body?.getReader()
|
|
327
|
+
if (!reader) throw new Error('expected a response body')
|
|
328
|
+
const first = await readUntil(reader, 'one')
|
|
329
|
+
expect(first.text).toContain('data: one')
|
|
330
|
+
await reader.cancel()
|
|
331
|
+
|
|
332
|
+
const missed = await dispatch(new Request('http://127.0.0.1/nowhere'))
|
|
333
|
+
expect(missed).toBeNull()
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('a non-matching router mounted first still returns an early end', async () => {
|
|
337
|
+
const dispatch = await createKoaDispatcher(
|
|
338
|
+
orderedRouters((router) => {
|
|
339
|
+
router.get('/api/early-end', (ctx) => {
|
|
340
|
+
ctx.respond = false
|
|
341
|
+
ctx.res.writeHead(201, { 'content-type': 'text/plain; charset=utf-8' })
|
|
342
|
+
try {
|
|
343
|
+
ctx.res.write('from-route')
|
|
344
|
+
} finally {
|
|
345
|
+
ctx.res.end()
|
|
346
|
+
}
|
|
347
|
+
})
|
|
348
|
+
}),
|
|
349
|
+
'1mb'
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
const response = await dispatch(new Request('http://127.0.0.1/api/early-end'))
|
|
353
|
+
expect(response).not.toBeNull()
|
|
354
|
+
expect(response!.status).toBe(201)
|
|
355
|
+
expect(await response!.text()).toBe('from-route')
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
test('Koa SSE chunks arrive before the stream ends', async () => {
|
|
359
|
+
const dir = await makeApp()
|
|
360
|
+
await writeStreamBackend(dir, join(dir, 'closed.flag'), 300)
|
|
361
|
+
const { url } = await boot({
|
|
362
|
+
FLIGHT_APP_HOME: dir,
|
|
363
|
+
FLIGHT_SECURITY_ENABLED: 'true',
|
|
364
|
+
FLIGHT_CORS_ENABLED: 'true'
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
const response = await fetch(`${url}/api/events`, { headers: { accept: 'text/event-stream' } })
|
|
368
|
+
expect(response.status).toBe(200)
|
|
369
|
+
expect(response.headers.get('content-type')).toContain('text/event-stream')
|
|
370
|
+
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
|
|
371
|
+
const reader = response.body?.getReader()
|
|
372
|
+
if (!reader) throw new Error('expected a response body')
|
|
373
|
+
|
|
374
|
+
const first = await readUntil(reader, 'one')
|
|
375
|
+
expect(first.text).toContain('data: one')
|
|
376
|
+
expect(first.text).not.toContain('two')
|
|
377
|
+
expect(first.firstByteMs).toBeLessThan(250)
|
|
378
|
+
|
|
379
|
+
const rest = await readUntil(reader, 'two')
|
|
380
|
+
expect(rest.text).toContain('data: two')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
test('closing the SSE client emits close on the Koa response', async () => {
|
|
384
|
+
const dir = await makeApp()
|
|
385
|
+
const flag = join(dir, 'closed.flag')
|
|
386
|
+
await writeStreamBackend(dir, flag, 300)
|
|
387
|
+
const { url } = await boot({ FLIGHT_APP_HOME: dir })
|
|
388
|
+
|
|
389
|
+
const controller = new AbortController()
|
|
390
|
+
const response = await fetch(`${url}/api/events-open`, { signal: controller.signal })
|
|
391
|
+
expect(response.status).toBe(200)
|
|
392
|
+
const reader = response.body?.getReader()
|
|
393
|
+
const first = await reader?.read()
|
|
394
|
+
expect(new TextDecoder().decode(first?.value)).toContain('hello')
|
|
395
|
+
|
|
396
|
+
controller.abort()
|
|
397
|
+
const started = Date.now()
|
|
398
|
+
let body = ''
|
|
399
|
+
while (Date.now() - started < 2000) {
|
|
400
|
+
try {
|
|
401
|
+
body = await readFile(flag, 'utf8')
|
|
402
|
+
break
|
|
403
|
+
} catch {
|
|
404
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
expect(body).toBe('closed')
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
test('a Koa route that ends inside the handler is not replaced by the SPA 404', async () => {
|
|
411
|
+
const dir = await makeApp()
|
|
412
|
+
await writeStreamBackend(dir, join(dir, 'closed.flag'), 300)
|
|
413
|
+
const { url } = await boot({ FLIGHT_APP_HOME: dir })
|
|
414
|
+
|
|
415
|
+
const response = await fetch(`${url}/api/early-end`, { headers: { accept: 'text/html' } })
|
|
416
|
+
expect(response.status).toBe(201)
|
|
417
|
+
expect(await response.text()).toBe('from-route')
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
test('gzip does not buffer a Koa event stream', async () => {
|
|
421
|
+
const dir = await makeApp()
|
|
422
|
+
await writeStreamBackend(dir, join(dir, 'closed.flag'), 300)
|
|
423
|
+
const { url } = await boot({
|
|
424
|
+
FLIGHT_APP_HOME: dir,
|
|
425
|
+
FLIGHT_COMPRESS_ENABLED: 'true',
|
|
426
|
+
FLIGHT_COMPRESS_THRESHOLD: '1024'
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
const plain = await fetch(`${url}/api/big-text`, { headers: { 'accept-encoding': 'gzip' } })
|
|
430
|
+
expect(plain.status).toBe(200)
|
|
431
|
+
expect(plain.headers.get('content-encoding')).toBe('gzip')
|
|
432
|
+
expect((await plain.arrayBuffer()).byteLength).toBe(2048)
|
|
433
|
+
|
|
434
|
+
const events = await fetch(`${url}/api/events`, {
|
|
435
|
+
headers: { accept: 'text/event-stream', 'accept-encoding': 'gzip' }
|
|
436
|
+
})
|
|
437
|
+
expect(events.status).toBe(200)
|
|
438
|
+
expect(events.headers.get('content-encoding')).toBeNull()
|
|
439
|
+
const reader = events.body?.getReader()
|
|
440
|
+
if (!reader) throw new Error('expected a response body')
|
|
441
|
+
const first = await readUntil(reader, 'one')
|
|
442
|
+
expect(first.text).toContain('data: one')
|
|
443
|
+
expect(first.text).not.toContain('two')
|
|
444
|
+
expect(first.firstByteMs).toBeLessThan(250)
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
test(
|
|
448
|
+
'SSE stays open past FLIGHT_IDLE_TIMEOUT',
|
|
449
|
+
async () => {
|
|
450
|
+
const dir = await makeApp()
|
|
451
|
+
await writeStreamBackend(dir, join(dir, 'closed.flag'), 5000)
|
|
452
|
+
const { url } = await boot({ FLIGHT_APP_HOME: dir, FLIGHT_IDLE_TIMEOUT: '1' })
|
|
453
|
+
|
|
454
|
+
const response = await fetch(`${url}/api/events`)
|
|
455
|
+
expect(response.headers.get('content-type')).toContain('text/event-stream')
|
|
456
|
+
const reader = response.body?.getReader()
|
|
457
|
+
if (!reader) throw new Error('expected a response body')
|
|
458
|
+
const first = await readUntil(reader, 'one')
|
|
459
|
+
expect(first.text).toContain('data: one')
|
|
460
|
+
expect(first.text).not.toContain('two')
|
|
461
|
+
const rest = await readUntil(reader, 'two')
|
|
462
|
+
expect(rest.text).toContain('data: two')
|
|
463
|
+
},
|
|
464
|
+
{ timeout: 12000 }
|
|
465
|
+
)
|
package/src/bun/app.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
compose,
|
|
8
8
|
compress,
|
|
9
9
|
cors,
|
|
10
|
+
isEventStream,
|
|
10
11
|
logger,
|
|
11
12
|
rateLimit,
|
|
12
13
|
security,
|
|
@@ -24,6 +25,19 @@ export interface FlightServer extends AddressServer {
|
|
|
24
25
|
stop(closeActiveConnections?: boolean): void
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
/** Slice of `Bun.serve`'s server used to adjust the idle timeout for one request. */
|
|
29
|
+
interface BunHttpServer {
|
|
30
|
+
timeout(request: Request, seconds: number): void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Turn the idle timeout off for server-sent events so a quiet heartbeat is not cut off.
|
|
35
|
+
*/
|
|
36
|
+
function relaxIdleTimeout(req: Request, response: Response, bunServer?: BunHttpServer): Response {
|
|
37
|
+
if (bunServer && isEventStream(response)) bunServer.timeout(req, 0)
|
|
38
|
+
return response
|
|
39
|
+
}
|
|
40
|
+
|
|
27
41
|
/**
|
|
28
42
|
* Wrap discovered routes so every method runs through the middleware onion.
|
|
29
43
|
* OPTIONS is registered when CORS is on, otherwise Bun answers 405 before our middleware runs.
|
|
@@ -37,11 +51,13 @@ function wrapRoutes(
|
|
|
37
51
|
for (const [path, value] of Object.entries(routes)) {
|
|
38
52
|
if (typeof value === 'function') {
|
|
39
53
|
const handler = value
|
|
40
|
-
wrapped[path] = (req: Request
|
|
54
|
+
wrapped[path] = (req: Request, bunServer?: BunHttpServer) =>
|
|
55
|
+
run(req, () => handler(req)).then((response) => relaxIdleTimeout(req, response, bunServer))
|
|
41
56
|
} else {
|
|
42
57
|
const methods: MethodMap = {}
|
|
43
58
|
for (const [method, handler] of Object.entries(value)) {
|
|
44
|
-
methods[method] = (req: Request
|
|
59
|
+
methods[method] = (req: Request, bunServer?: BunHttpServer) =>
|
|
60
|
+
run(req, () => handler(req)).then((response) => relaxIdleTimeout(req, response, bunServer))
|
|
45
61
|
}
|
|
46
62
|
if (corsEnabled && !methods.OPTIONS) {
|
|
47
63
|
methods.OPTIONS = (req: Request) => run(req, () => new Response(null, { status: 204 }))
|
|
@@ -99,12 +115,13 @@ export async function createApp(cfg: Config, server?: () => AddressServer | unde
|
|
|
99
115
|
return {
|
|
100
116
|
enabled,
|
|
101
117
|
routes,
|
|
102
|
-
fetch: async (req: Request) => {
|
|
103
|
-
|
|
118
|
+
fetch: async (req: Request, bunServer?: BunHttpServer) => {
|
|
119
|
+
const response = await run(req, async () => {
|
|
104
120
|
const fromKoa = koa ? await koa(req) : null
|
|
105
121
|
if (fromKoa) return fromKoa
|
|
106
122
|
return (await serveStatic(req)) ?? new Response('Not Found', { status: 404 })
|
|
107
123
|
})
|
|
124
|
+
return relaxIdleTimeout(req, response, bunServer)
|
|
108
125
|
},
|
|
109
126
|
error(err: unknown) {
|
|
110
127
|
console.error('flight-bun error:', err)
|
|
@@ -125,8 +142,9 @@ export async function openFlight(
|
|
|
125
142
|
server = Bun.serve({
|
|
126
143
|
port: listen.port ?? cfg.port,
|
|
127
144
|
hostname: listen.hostname ?? cfg.hostname,
|
|
145
|
+
idleTimeout: cfg.idleTimeout,
|
|
128
146
|
routes: app.routes as Record<string, Handler | MethodMap>,
|
|
129
|
-
fetch: app.fetch,
|
|
147
|
+
fetch: (req: Request, bunServer: BunHttpServer) => app.fetch(req, bunServer),
|
|
130
148
|
error: app.error
|
|
131
149
|
}) as FlightServer
|
|
132
150
|
|
|
@@ -134,6 +152,7 @@ export async function openFlight(
|
|
|
134
152
|
console.log(
|
|
135
153
|
`flight-bun (${cfg.mode}) listening on :${server.port} — app_home=${cfg.appHome}${dist}\n` +
|
|
136
154
|
`flight-bun: middleware [${app.enabled.join(', ') || 'none'}]` +
|
|
155
|
+
`, idleTimeout=${cfg.idleTimeout}s` +
|
|
137
156
|
(cfg.redisUrl ? `, redis=${cfg.redisUrl}` : ', redis=(in-memory)')
|
|
138
157
|
)
|
|
139
158
|
return server
|
package/src/bun/cache.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CacheConfig } from './config'
|
|
2
|
-
import { withHeaders, type Middleware } from './middleware'
|
|
2
|
+
import { isStreamingResponse, withHeaders, type Middleware } from './middleware'
|
|
3
3
|
import type { KVStore } from './store'
|
|
4
4
|
|
|
5
5
|
/** Stored GET response. Only text and JSON bodies are kept. */
|
|
@@ -35,6 +35,7 @@ export function cache(cfg: CacheConfig, store: KVStore): Middleware {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
const response = await next()
|
|
38
|
+
if (isStreamingResponse(response)) return withHeaders(response, { 'x-flight-cache': 'MISS' })
|
|
38
39
|
const cacheControl = response.headers.get('cache-control') || ''
|
|
39
40
|
const match = /max-age=(\d+)/.exec(cacheControl)
|
|
40
41
|
if (response.status === 200 && match && req.method === 'GET') {
|
package/src/bun/config.ts
CHANGED
|
@@ -57,6 +57,11 @@ export interface Config {
|
|
|
57
57
|
spaDenyPrefixes: string[]
|
|
58
58
|
payloadLimit: string
|
|
59
59
|
logging: boolean
|
|
60
|
+
/**
|
|
61
|
+
* Seconds `Bun.serve` waits on an idle socket.
|
|
62
|
+
* Explicit `0` disables it. Positive fractions round up to at least 1. Clamped to 255.
|
|
63
|
+
*/
|
|
64
|
+
idleTimeout: number
|
|
60
65
|
redisUrl?: string
|
|
61
66
|
cors: CorsConfig
|
|
62
67
|
security: SecurityConfig
|
|
@@ -85,6 +90,19 @@ function num(value: string | undefined, fallback: number): number {
|
|
|
85
90
|
return Number.isFinite(parsed) ? parsed : fallback
|
|
86
91
|
}
|
|
87
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Parse `FLIGHT_IDLE_TIMEOUT` seconds. Missing, blank, negative, and non-numeric values use 10.
|
|
95
|
+
* An explicit `0` disables the timeout. Positive fractions round up to at least 1.
|
|
96
|
+
* Anything above Bun's maximum of 255 is clamped.
|
|
97
|
+
*/
|
|
98
|
+
function idleSeconds(value: string | undefined): number {
|
|
99
|
+
if (value === undefined || value.trim() === '') return 10
|
|
100
|
+
const parsed = Number(value)
|
|
101
|
+
if (!Number.isFinite(parsed) || parsed < 0) return 10
|
|
102
|
+
if (parsed === 0) return 0
|
|
103
|
+
return Math.min(255, Math.max(1, Math.ceil(parsed)))
|
|
104
|
+
}
|
|
105
|
+
|
|
88
106
|
/**
|
|
89
107
|
* Split a comma-separated prefix list and ensure each entry starts with `/`.
|
|
90
108
|
*/
|
|
@@ -121,6 +139,7 @@ export function loadConfig(env: Env = process.env): Config {
|
|
|
121
139
|
spaDenyPrefixes: prefixes(env.FLIGHT_SPA_DENY_PREFIXES, '/api,/health,/healthz'),
|
|
122
140
|
payloadLimit: env.FLIGHT_PAYLOAD_LIMIT || '1mb',
|
|
123
141
|
logging: bool(env.FLIGHT_LOGGING, mode === 'development'),
|
|
142
|
+
idleTimeout: idleSeconds(env.FLIGHT_IDLE_TIMEOUT),
|
|
124
143
|
redisUrl: env.FLIGHT_REDIS_URL || undefined,
|
|
125
144
|
cors: {
|
|
126
145
|
enabled: bool(env.FLIGHT_CORS_ENABLED, true),
|
package/src/bun/koa-compat.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { EventEmitter } from 'node:events'
|
|
|
2
2
|
import { Readable } from 'node:stream'
|
|
3
3
|
|
|
4
4
|
import type { KoaMiddleware } from './backends'
|
|
5
|
+
import { markStreaming } from './middleware'
|
|
6
|
+
|
|
7
|
+
type WriteCallback = (error?: Error | null) => void
|
|
5
8
|
|
|
6
9
|
/** Node-style response stand-in that Koa's `res.end()` can finish. */
|
|
7
10
|
class MockResponse extends EventEmitter {
|
|
@@ -12,8 +15,17 @@ class MockResponse extends EventEmitter {
|
|
|
12
15
|
/** Left unset so `on-finished` does not treat a plain object as a socket. */
|
|
13
16
|
socket: EventEmitter | null = null
|
|
14
17
|
flightMatched = false
|
|
18
|
+
/** Re-read whether any mounted router has matched. Runs before the dispatcher decides. */
|
|
19
|
+
refreshMatch: () => void = () => {}
|
|
15
20
|
private chunks: Buffer[] = []
|
|
16
21
|
private headers = new Map<string, string | string[]>()
|
|
22
|
+
/** True once `writeHead`, `write`, or `ctx.respond = false` starts a live body. */
|
|
23
|
+
private streaming = false
|
|
24
|
+
/** True while `end()` is copying its final chunk, so that write does not start a stream. */
|
|
25
|
+
private ending = false
|
|
26
|
+
private closeEmitted = false
|
|
27
|
+
private clientAborted = false
|
|
28
|
+
private controller: ReadableStreamDefaultController<Uint8Array> | null = null
|
|
17
29
|
|
|
18
30
|
/** Store a response header. Repeated Set-Cookie values accumulate. */
|
|
19
31
|
setHeader(name: string, value: string | number | string[]) {
|
|
@@ -38,40 +50,124 @@ class MockResponse extends EventEmitter {
|
|
|
38
50
|
this.headers.delete(name.toLowerCase())
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
/** Record the status line.
|
|
42
|
-
writeHead(
|
|
53
|
+
/** Record the status line. Calling this begins a streamed body. */
|
|
54
|
+
writeHead(
|
|
55
|
+
status: number,
|
|
56
|
+
reason?: string | Record<string, string | number | string[]>,
|
|
57
|
+
maybeHeaders?: Record<string, string | number | string[]>
|
|
58
|
+
) {
|
|
43
59
|
this.statusCode = status
|
|
60
|
+
const headers = typeof reason === 'string' || reason === undefined ? maybeHeaders : reason
|
|
44
61
|
if (headers) {
|
|
45
62
|
for (const [key, value] of Object.entries(headers)) this.setHeader(key, value)
|
|
46
63
|
}
|
|
47
64
|
this.headersSent = true
|
|
65
|
+
if (!this.finished && !this.ending) this.startStream()
|
|
48
66
|
return this
|
|
49
67
|
}
|
|
50
68
|
|
|
51
|
-
/**
|
|
69
|
+
/** Flush headers and begin a streamed body. */
|
|
52
70
|
flushHeaders() {
|
|
53
71
|
this.headersSent = true
|
|
72
|
+
if (!this.finished && !this.ending) this.startStream()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Begin a streamed body without waiting for `end()`.
|
|
77
|
+
* Headers set in the same turn are included; the Fetch response resolves on the next microtask.
|
|
78
|
+
*/
|
|
79
|
+
startStream() {
|
|
80
|
+
if (this.streaming || this.finished || this.writableEnded) return
|
|
81
|
+
this.streaming = true
|
|
82
|
+
this.refreshMatch()
|
|
83
|
+
queueMicrotask(() => {
|
|
84
|
+
this.emit('stream-open')
|
|
85
|
+
})
|
|
54
86
|
}
|
|
55
87
|
|
|
56
|
-
/** Buffer a chunk. */
|
|
57
|
-
write(chunk?: string | Buffer) {
|
|
58
|
-
if (
|
|
88
|
+
/** Buffer a chunk, or enqueue it when a live stream is already open. */
|
|
89
|
+
write(chunk?: string | Buffer | Uint8Array, encoding?: BufferEncoding | WriteCallback, callback?: WriteCallback) {
|
|
90
|
+
if (this.writableEnded || this.clientAborted) return false
|
|
91
|
+
let cb: WriteCallback | undefined
|
|
92
|
+
let enc: BufferEncoding | undefined
|
|
93
|
+
if (typeof encoding === 'function') cb = encoding
|
|
94
|
+
else {
|
|
95
|
+
enc = encoding
|
|
96
|
+
if (typeof callback === 'function') cb = callback
|
|
97
|
+
}
|
|
98
|
+
if (chunk != null && chunk.length !== 0) {
|
|
99
|
+
const bytes = typeof chunk === 'string' ? Buffer.from(chunk, enc) : Buffer.from(chunk)
|
|
100
|
+
this.enqueue(bytes)
|
|
101
|
+
}
|
|
102
|
+
if (!this.ending) this.startStream()
|
|
103
|
+
if (cb) queueMicrotask(() => cb!())
|
|
59
104
|
return true
|
|
60
105
|
}
|
|
61
106
|
|
|
62
107
|
/** Finish the response and emit the events `on-finished` listens for. */
|
|
63
|
-
end(chunk?: string | Buffer) {
|
|
64
|
-
if (
|
|
108
|
+
end(chunk?: string | Buffer | Uint8Array, encoding?: BufferEncoding | WriteCallback, callback?: WriteCallback) {
|
|
109
|
+
if (this.writableEnded) return this
|
|
110
|
+
this.ending = true
|
|
111
|
+
if (chunk != null && chunk !== '') this.write(chunk, encoding, callback)
|
|
112
|
+
else if (typeof encoding === 'function') queueMicrotask(() => encoding())
|
|
113
|
+
else if (typeof callback === 'function') queueMicrotask(() => callback())
|
|
65
114
|
this.finished = true
|
|
66
115
|
this.writableEnded = true
|
|
67
116
|
this.headersSent = true
|
|
117
|
+
this.refreshMatch()
|
|
118
|
+
this.closeController()
|
|
68
119
|
this.emit('finish')
|
|
69
|
-
this.
|
|
120
|
+
this.emitClose()
|
|
70
121
|
return this
|
|
71
122
|
}
|
|
72
123
|
|
|
124
|
+
/** Client went away. `res.on('close')` handlers use this to drop heartbeats and subscribers. */
|
|
125
|
+
clientDisconnect() {
|
|
126
|
+
if (this.clientAborted) return
|
|
127
|
+
this.clientAborted = true
|
|
128
|
+
this.closeController()
|
|
129
|
+
this.emitClose()
|
|
130
|
+
}
|
|
131
|
+
|
|
73
132
|
/** Build the Fetch response from the buffered status, headers, and body. */
|
|
74
133
|
toResponse(): Response {
|
|
134
|
+
if (this.streaming && !this.finished) return this.liveResponse()
|
|
135
|
+
const body = this.chunks.length ? Buffer.concat(this.chunks) : null
|
|
136
|
+
return new Response(body, { status: this.statusCode, headers: this.headerInit() })
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Push bytes into the live controller, or hold them until that controller exists. */
|
|
140
|
+
private enqueue(bytes: Buffer) {
|
|
141
|
+
if (!this.controller) {
|
|
142
|
+
this.chunks.push(bytes)
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
this.controller.enqueue(bytes)
|
|
147
|
+
} catch {
|
|
148
|
+
// The stream is already closed.
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Close the live body if the consumer has already attached. */
|
|
153
|
+
private closeController() {
|
|
154
|
+
if (!this.controller) return
|
|
155
|
+
try {
|
|
156
|
+
this.controller.close()
|
|
157
|
+
} catch {
|
|
158
|
+
// Already closed or cancelled.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Emit `close` once, whether the route finished or the client aborted. */
|
|
163
|
+
private emitClose() {
|
|
164
|
+
if (this.closeEmitted) return
|
|
165
|
+
this.closeEmitted = true
|
|
166
|
+
this.emit('close')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Copy stored headers into a Fetch `Headers` object. */
|
|
170
|
+
private headerInit(): Headers {
|
|
75
171
|
const headers = new Headers()
|
|
76
172
|
for (const [key, value] of this.headers) {
|
|
77
173
|
if (Array.isArray(value)) {
|
|
@@ -80,8 +176,26 @@ class MockResponse extends EventEmitter {
|
|
|
80
176
|
headers.set(key, value)
|
|
81
177
|
}
|
|
82
178
|
}
|
|
83
|
-
|
|
84
|
-
|
|
179
|
+
return headers
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Return a Fetch response whose body stays open until `end()` or the client disconnects.
|
|
184
|
+
* Chunks written before this runs are flushed first.
|
|
185
|
+
*/
|
|
186
|
+
private liveResponse(): Response {
|
|
187
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
188
|
+
start: (controller) => {
|
|
189
|
+
this.controller = controller
|
|
190
|
+
for (const chunk of this.chunks.splice(0)) controller.enqueue(chunk)
|
|
191
|
+
if (this.finished || this.clientAborted) this.closeController()
|
|
192
|
+
},
|
|
193
|
+
cancel: () => {
|
|
194
|
+
this.clientDisconnect()
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
markStreaming(stream)
|
|
198
|
+
return new Response(stream, { status: this.statusCode, headers: this.headerInit() })
|
|
85
199
|
}
|
|
86
200
|
}
|
|
87
201
|
|
|
@@ -108,6 +222,18 @@ function requestFromFetch(request: Request, body: Buffer): Readable {
|
|
|
108
222
|
return req
|
|
109
223
|
}
|
|
110
224
|
|
|
225
|
+
/** Koa context fields the compat layer observes. */
|
|
226
|
+
interface KoaCtx {
|
|
227
|
+
matched?: unknown
|
|
228
|
+
/** Set by `@koa/router` when the path and the method both match. */
|
|
229
|
+
_matchedRoute?: unknown
|
|
230
|
+
respond?: unknown
|
|
231
|
+
res: MockResponse
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Arrays whose `push` already updates `flightMatched`. Later routers append instead of assigning. */
|
|
235
|
+
const observedMatches = new WeakSet<object>()
|
|
236
|
+
|
|
111
237
|
/**
|
|
112
238
|
* Run one Fetch request through a Koa callback. Returns null when no route matched,
|
|
113
239
|
* so the caller can still serve the SPA.
|
|
@@ -121,13 +247,33 @@ function callbackToFetch(callback: (req: Readable, res: MockResponse) => void) {
|
|
|
121
247
|
const req = requestFromFetch(request, body)
|
|
122
248
|
const res = new MockResponse()
|
|
123
249
|
|
|
250
|
+
const onAbort = () => res.clientDisconnect()
|
|
251
|
+
if (request.signal.aborted) onAbort()
|
|
252
|
+
else request.signal.addEventListener('abort', onAbort, { once: true })
|
|
253
|
+
res.once('finish', () => request.signal.removeEventListener('abort', onAbort))
|
|
254
|
+
|
|
124
255
|
await new Promise<void>((resolve, reject) => {
|
|
125
|
-
|
|
126
|
-
|
|
256
|
+
let settled = false
|
|
257
|
+
const finish = () => {
|
|
258
|
+
res.refreshMatch()
|
|
259
|
+
if (settled) return
|
|
260
|
+
settled = true
|
|
261
|
+
resolve()
|
|
262
|
+
}
|
|
263
|
+
res.on('finish', finish)
|
|
264
|
+
res.on('stream-open', finish)
|
|
265
|
+
res.on('error', (err) => {
|
|
266
|
+
if (settled) return
|
|
267
|
+
settled = true
|
|
268
|
+
reject(err)
|
|
269
|
+
})
|
|
127
270
|
try {
|
|
128
271
|
callback(req, res)
|
|
129
272
|
} catch (err) {
|
|
130
|
-
|
|
273
|
+
if (!settled) {
|
|
274
|
+
settled = true
|
|
275
|
+
reject(err)
|
|
276
|
+
}
|
|
131
277
|
}
|
|
132
278
|
})
|
|
133
279
|
|
|
@@ -136,6 +282,73 @@ function callbackToFetch(callback: (req: Readable, res: MockResponse) => void) {
|
|
|
136
282
|
}
|
|
137
283
|
}
|
|
138
284
|
|
|
285
|
+
/**
|
|
286
|
+
* True when this request hit a route.
|
|
287
|
+
* `@koa/router` assigns `ctx.matched` for the first router only. Every later router
|
|
288
|
+
* `push`es onto that same array, and sets `_matchedRoute` when the method matches.
|
|
289
|
+
*/
|
|
290
|
+
function hasMatch(ctx: KoaCtx): boolean {
|
|
291
|
+
const matched = ctx.matched
|
|
292
|
+
if (Array.isArray(matched) && matched.length > 0) return true
|
|
293
|
+
return typeof ctx._matchedRoute === 'string' && ctx._matchedRoute.length > 0
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Notice routes appended with `push` after the first router assigned `ctx.matched`.
|
|
298
|
+
*/
|
|
299
|
+
function observePushes(ctx: KoaCtx, layers: unknown[]) {
|
|
300
|
+
if (observedMatches.has(layers)) return
|
|
301
|
+
observedMatches.add(layers)
|
|
302
|
+
const original = Array.prototype.push
|
|
303
|
+
layers.push = function (this: unknown[], ...items: unknown[]) {
|
|
304
|
+
const result = original.apply(this, items)
|
|
305
|
+
ctx.res.refreshMatch()
|
|
306
|
+
return result
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Keep `flightMatched` current for every router, including ones that only `push`.
|
|
312
|
+
* The dispatcher reads it again when a stream opens or the response finishes.
|
|
313
|
+
*/
|
|
314
|
+
function trackMatch(ctx: KoaCtx) {
|
|
315
|
+
ctx.res.refreshMatch = () => {
|
|
316
|
+
ctx.res.flightMatched = hasMatch(ctx)
|
|
317
|
+
}
|
|
318
|
+
let matched = ctx.matched
|
|
319
|
+
if (Array.isArray(matched)) observePushes(ctx, matched)
|
|
320
|
+
Object.defineProperty(ctx, 'matched', {
|
|
321
|
+
configurable: true,
|
|
322
|
+
enumerable: true,
|
|
323
|
+
get() {
|
|
324
|
+
return matched
|
|
325
|
+
},
|
|
326
|
+
set(value: unknown) {
|
|
327
|
+
matched = value
|
|
328
|
+
if (Array.isArray(value)) observePushes(ctx, value)
|
|
329
|
+
ctx.res.refreshMatch()
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* `ctx.respond = false` means the route owns the raw response and Koa must not wait for `end()`.
|
|
336
|
+
*/
|
|
337
|
+
function trackRespond(ctx: KoaCtx) {
|
|
338
|
+
let respond = ctx.respond
|
|
339
|
+
Object.defineProperty(ctx, 'respond', {
|
|
340
|
+
configurable: true,
|
|
341
|
+
enumerable: true,
|
|
342
|
+
get() {
|
|
343
|
+
return respond
|
|
344
|
+
},
|
|
345
|
+
set(value: unknown) {
|
|
346
|
+
respond = value
|
|
347
|
+
if (value === false) ctx.res.startStream()
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
139
352
|
/**
|
|
140
353
|
* Build a Fetch dispatcher for existing Koa `router.routes()` backends.
|
|
141
354
|
* JSON bodies are parsed with `koa-bodyparser`. Unmatched paths return null.
|
|
@@ -150,7 +363,9 @@ export async function createKoaDispatcher(
|
|
|
150
363
|
const bodyParser = bodyParserModule.default
|
|
151
364
|
const app = new Koa()
|
|
152
365
|
|
|
153
|
-
app.use(async (ctx:
|
|
366
|
+
app.use(async (ctx: KoaCtx, next: () => Promise<void>) => {
|
|
367
|
+
trackMatch(ctx)
|
|
368
|
+
trackRespond(ctx)
|
|
154
369
|
await next()
|
|
155
370
|
ctx.res.flightMatched = Array.isArray(ctx.matched) && ctx.matched.length > 0
|
|
156
371
|
})
|
package/src/bun/middleware.ts
CHANGED
|
@@ -34,11 +34,22 @@ export function compose(middlewares: Middleware[]): (req: Request, core: Handler
|
|
|
34
34
|
export function withHeaders(response: Response, extra: Record<string, string>): Response {
|
|
35
35
|
const headers = new Headers(response.headers)
|
|
36
36
|
for (const [key, value] of Object.entries(extra)) headers.set(key, value)
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
return preserveStreaming(
|
|
38
|
+
response,
|
|
39
|
+
new Response(response.body, {
|
|
40
|
+
status: response.status,
|
|
41
|
+
statusText: response.statusText,
|
|
42
|
+
headers
|
|
43
|
+
})
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Keep a live-stream mark when a middleware copies the body onto a new `Response`.
|
|
49
|
+
*/
|
|
50
|
+
export function preserveStreaming(source: Response, target: Response): Response {
|
|
51
|
+
if (source.body && target.body && streamingBodies.has(source.body)) streamingBodies.add(target.body)
|
|
52
|
+
return target
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
/**
|
|
@@ -104,6 +115,33 @@ export function cors(cfg: CorsConfig): Middleware {
|
|
|
104
115
|
}
|
|
105
116
|
}
|
|
106
117
|
|
|
118
|
+
/** Bodies that must not be read to completion. Keyed by the stream so header copies keep the mark. */
|
|
119
|
+
const streamingBodies = new WeakSet<ReadableStream<Uint8Array>>()
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Remember a body that stays open until the producer ends it.
|
|
123
|
+
* Compression and the response cache must not buffer it.
|
|
124
|
+
*/
|
|
125
|
+
export function markStreaming(body: ReadableStream<Uint8Array>) {
|
|
126
|
+
streamingBodies.add(body)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* True for `text/event-stream` and for any body marked as a live stream.
|
|
131
|
+
*/
|
|
132
|
+
export function isStreamingResponse(response: Response): boolean {
|
|
133
|
+
const type = response.headers.get('content-type') || ''
|
|
134
|
+
if (/text\/event-stream/i.test(type)) return true
|
|
135
|
+
return response.body != null && streamingBodies.has(response.body)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* True when this response is a server-sent event stream.
|
|
140
|
+
*/
|
|
141
|
+
export function isEventStream(response: Response): boolean {
|
|
142
|
+
return /text\/event-stream/i.test(response.headers.get('content-type') || '')
|
|
143
|
+
}
|
|
144
|
+
|
|
107
145
|
/**
|
|
108
146
|
* True when the content type is worth gzipping.
|
|
109
147
|
*/
|
|
@@ -131,6 +169,7 @@ export function compress(cfg: CompressionConfig): Middleware {
|
|
|
131
169
|
}
|
|
132
170
|
if (response.headers.get('content-encoding')) return response
|
|
133
171
|
if (!(req.headers.get('accept-encoding') || '').includes('gzip')) return response
|
|
172
|
+
if (isStreamingResponse(response)) return response
|
|
134
173
|
if (!compressible(response.headers.get('content-type') || '')) return response
|
|
135
174
|
|
|
136
175
|
const bytes = new Uint8Array(await response.arrayBuffer())
|
package/src/bun/session.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHmac, randomUUID, timingSafeEqual } from 'node:crypto'
|
|
2
2
|
|
|
3
3
|
import type { SessionConfig } from './config'
|
|
4
|
-
import type
|
|
4
|
+
import { preserveStreaming, type Middleware } from './middleware'
|
|
5
5
|
import type { KVStore } from './store'
|
|
6
6
|
|
|
7
7
|
/** Request carrying the mutable session object. */
|
|
@@ -63,11 +63,14 @@ function serializeCookie(name: string, value: string, maxAgeSec: number): string
|
|
|
63
63
|
function appendSetCookie(response: Response, cookie: string): Response {
|
|
64
64
|
const headers = new Headers(response.headers)
|
|
65
65
|
headers.append('set-cookie', cookie)
|
|
66
|
-
return
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
return preserveStreaming(
|
|
67
|
+
response,
|
|
68
|
+
new Response(response.body, {
|
|
69
|
+
status: response.status,
|
|
70
|
+
statusText: response.statusText,
|
|
71
|
+
headers
|
|
72
|
+
})
|
|
73
|
+
)
|
|
71
74
|
}
|
|
72
75
|
|
|
73
76
|
/**
|
package/LICENSE.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
9
|
-
|
|
10
|
-
The above copyright notice and this permission notice shall be included in all
|
|
11
|
-
copies or substantial portions of the Software.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
-
SOFTWARE.
|