@lenne.tech/nest-server 12.9.2 → 12.9.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/.claude/rules/package-management.md +7 -2
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/common/helpers/byte-range.helper.d.ts +8 -0
- package/dist/core/common/helpers/byte-range.helper.js +71 -0
- package/dist/core/common/helpers/byte-range.helper.js.map +1 -0
- package/dist/core/common/helpers/gridfs.helper.d.ts +7 -2
- package/dist/core/common/helpers/gridfs.helper.js +7 -4
- package/dist/core/common/helpers/gridfs.helper.js.map +1 -1
- package/dist/core/common/services/core-s3.service.d.ts +3 -0
- package/dist/core/common/services/core-s3.service.js +8 -1
- package/dist/core/common/services/core-s3.service.js.map +1 -1
- package/dist/core/modules/error-code/error-codes.d.ts +9 -0
- package/dist/core/modules/error-code/error-codes.js +8 -0
- package/dist/core/modules/error-code/error-codes.js.map +1 -1
- package/dist/core/modules/file/core-file.controller.d.ts +5 -0
- package/dist/core/modules/file/core-file.controller.js +66 -4
- package/dist/core/modules/file/core-file.controller.js.map +1 -1
- package/dist/core/modules/file/core-file.service.d.ts +2 -0
- package/dist/core/modules/file/core-file.service.js +17 -6
- package/dist/core/modules/file/core-file.service.js.map +1 -1
- package/dist/core/modules/file/filesystem-file.helper.d.ts +2 -1
- package/dist/core/modules/file/filesystem-file.helper.js +2 -2
- package/dist/core/modules/file/filesystem-file.helper.js.map +1 -1
- package/dist/core/modules/file/interfaces/file-service-options.interface.d.ts +2 -0
- package/dist/core/modules/file/s3-file.helper.d.ts +2 -0
- package/dist/core/modules/file/s3-file.helper.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/server/modules/error-code/error-codes.d.ts +1 -0
- package/dist/test/test.helper.d.ts +1 -0
- package/dist/test/test.helper.js +6 -0
- package/dist/test/test.helper.js.map +1 -1
- package/docs/REQUEST-LIFECYCLE.md +1 -1
- package/docs/error-codes.md +14 -0
- package/docs/security-overrides.md +44 -10
- package/migration-guides/12.9.2-to-12.9.3.md +127 -0
- package/migration-guides/12.9.3-to-12.9.4.md +135 -0
- package/package.json +27 -27
- package/src/core/common/helpers/byte-range.helper.ts +144 -0
- package/src/core/common/helpers/gridfs.helper.ts +20 -6
- package/src/core/common/services/core-s3.service.ts +16 -2
- package/src/core/modules/error-code/error-codes.ts +13 -0
- package/src/core/modules/file/INTEGRATION-CHECKLIST.md +1 -0
- package/src/core/modules/file/README.md +74 -1
- package/src/core/modules/file/core-file.controller.ts +139 -3
- package/src/core/modules/file/core-file.service.ts +33 -13
- package/src/core/modules/file/filesystem-file.helper.ts +8 -3
- package/src/core/modules/file/interfaces/file-service-options.interface.ts +13 -0
- package/src/core/modules/file/s3-file.helper.ts +3 -2
- package/src/index.ts +1 -0
- package/src/test/README.md +8 -4
- package/src/test/test.helper.ts +12 -0
|
@@ -21,6 +21,17 @@ used to **exact-pin**, to a vulnerable version:
|
|
|
21
21
|
| `@graphql-tools/utils` | [GHSA-7mx3-vvmw-hjmv](https://github.com/advisories/GHSA-7mx3-vvmw-hjmv) — high: prototype pollution in `mergeDeep`, which runs while the GraphQL schema is assembled. Covers `<=12.0.0`, patched `>=12.0.1` (added 2026-10-06); [GHSA-c4qx-w738-7hr3](https://github.com/advisories/GHSA-c4qx-w738-7hr3) — high: prototype pollution in `mergeIncrementalResult` via non-primitive path segments. Covers `>=10.10.1 <=12.0.1`, patched `>=12.0.2` (published 2026-10-22) | `@nestjs/graphql` declares utils as an **exact pin**: 13.4.5 — the newest 13.x — `12.0.0`, and 14.0.3, the version this framework declares since 12.0.0, `12.0.1`. 12.0.1 clears the first advisory and is covered by the second, so **this entry is load-bearing on 13.x and on 14.0.x**. 14.1.0, the version this framework declares since 12.1.0, pins `12.1.0`, which clears both: there the entry is inert. `@apollo/server` reaches utils too, through `@graphql-tools/schema` / `merge` — and in a lockfile that still holds schema 10.0.x (resolved before 2026-08-12), as **utils 11.x**: vulnerable too, with no 11.x fix, and outside the override key. Lift it with `pnpm update --depth Infinity @graphql-tools/schema @graphql-tools/merge`; the current releases request utils `^12.1.0`, which needs Node >=22.15 — the framework's own floor since 11.42.10 |
|
|
22
22
|
| `multer` | [GHSA-wc9g-mqfw-jrwm](https://github.com/advisories/GHSA-wc9g-mqfw-jrwm), [GHSA-535w-7cp7-47q4](https://github.com/advisories/GHSA-535w-7cp7-47q4), [GHSA-qfvm-cv95-jqjf](https://github.com/advisories/GHSA-qfvm-cv95-jqjf) — high: DoS via crafted multipart input; [GHSA-qvfw-j98x-7q72](https://github.com/advisories/GHSA-qvfw-j98x-7q72) — low: file size limit bypass. Patched `>=2.3.0`; [GHSA-q7qp-rjg7-q26m](https://github.com/advisories/GHSA-q7qp-rjg7-q26m) — high: DoS via a pending asynchronous `fileFilter` after request teardown. Covers `>=2.0.0 <2.5.0`, patched `>=2.5.0` (published 2026-10-22) | `@nestjs/platform-express` declares multer as an **exact pin**: up to 11.2.5 `2.2.0`, 11.2.6 / 11.2.7 `2.4.0`, and so does 12.2.0, the version this framework declared in 12.0.x. 2.4.0 is covered by GHSA-q7qp-rjg7-q26m, so **this entry is load-bearing up to @nestjs/platform-express 12.2.0**. 12.3.2, the version this framework declares since 12.1.0, pins `2.5.0`: there the entry is inert. A direct `multer` dependency does not move the pin: you get both copies, and FileInterceptor uses the vulnerable one |
|
|
23
23
|
|
|
24
|
+
**Status since 12.9.4** (verified 2026-11-14): **`ws` needs an entry again.**
|
|
25
|
+
[GHSA-37g9-w4hg-h9qr](https://github.com/websockets/ws/security/advisories/GHSA-37g9-w4hg-h9qr) — high: a client
|
|
26
|
+
floods ping frames without reading the replies, the server buffers the automatic pongs, and the process runs out of
|
|
27
|
+
memory. No authentication is needed, so every application with GraphQL subscriptions over WebSocket is reachable.
|
|
28
|
+
Patched in `ws` 8.23.0 (published 2026-11-12). `@nestjs/graphql` 14.2.1, the newest release and the version this
|
|
29
|
+
framework declares since 12.9.4, exact-pins `8.22.1`, and the GraphQL WebSocket server runs on that copy, so the `ws`
|
|
30
|
+
dependency nest-server declares (`8.23.0` since 12.9.4) does not lift it. Add the `ws` entry from "The fix" below,
|
|
31
|
+
keep it in lockstep with the declared version, and remove it once `@nestjs/graphql` pins `ws` >= 8.23.0. Until GitHub's
|
|
32
|
+
global advisory database lists the advisory, `pnpm audit` reports nothing for the vulnerable version; do not read a
|
|
33
|
+
green audit as "not affected".
|
|
34
|
+
|
|
24
35
|
**Status since 12.1.0** (verified 2026-10-29): with the `@nestjs/*` versions this framework declares, **no
|
|
25
36
|
entry is needed**. `@nestjs/platform-express` 12.3.2 pins `multer` 2.5.0 and `@nestjs/graphql` 14.1.0 pins
|
|
26
37
|
`@graphql-tools/utils` 12.1.0, both past their advisories, and nest-server's own `overrides:` block is empty:
|
|
@@ -62,9 +73,22 @@ entries. None of them is optional in practice.
|
|
|
62
73
|
## The fix
|
|
63
74
|
|
|
64
75
|
Add this to your project's `pnpm-workspace.yaml` (pnpm 11+; in pnpm 10 and earlier it lives under
|
|
65
|
-
`pnpm.overrides` in `package.json`).
|
|
66
|
-
|
|
67
|
-
**12.
|
|
76
|
+
`pnpm.overrides` in `package.json`).
|
|
77
|
+
|
|
78
|
+
**On nest-server 12.9.4 and later** this one entry is needed (see "Status since 12.9.4"):
|
|
79
|
+
|
|
80
|
+
```yaml
|
|
81
|
+
overrides:
|
|
82
|
+
# GHSA-37g9-w4hg-h9qr (high): memory-exhaustion DoS through unbounded automatic pong replies, before any
|
|
83
|
+
# authentication. Patched in 8.23.0. @nestjs/graphql 14.2.1 exact-pins ws 8.22.1, and the GraphQL WebSocket
|
|
84
|
+
# server runs on that copy. Keep the target in LOCKSTEP with the ws version @lenne.tech/nest-server declares
|
|
85
|
+
# (8.23.0 since 12.9.4). Remove once @nestjs/graphql pins ws >= 8.23.0.
|
|
86
|
+
'ws@>=8.0.0 <8.23.0': '8.23.0'
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
On nest-server **12.0.x** the two entries below are all it takes — a fresh resolve of nest-server 12.0.0's own tree
|
|
90
|
+
with and without every other former entry audits identically. On **12.1.x** neither is needed once your `@nestjs/*`
|
|
91
|
+
pins match the framework's (see "Status since 12.1.0"):
|
|
68
92
|
|
|
69
93
|
```yaml
|
|
70
94
|
overrides:
|
|
@@ -109,7 +133,8 @@ way, and remove the entries once your consumers move to minimatch 11 / glob 14.
|
|
|
109
133
|
`@nestjs/graphql` 13 and `@nestjs/swagger` 11 exact-pin vulnerable versions of these two; their 12.x
|
|
110
134
|
successors pin patched ones (`ws` 8.22.0, `js-yaml` 5.5.0). On nest-server 12 remove both entries — an
|
|
111
135
|
inert `ws` entry is not harmless there: pnpm rewrites `graphql-ws`'s `ws` peer range to the override
|
|
112
|
-
target, so a target below 8.22.0 makes `pnpm peers check` report `unmet peer ws`.
|
|
136
|
+
target, so a target below 8.22.0 makes `pnpm peers check` report `unmet peer ws`. Since 12.9.4 nest-server 12
|
|
137
|
+
needs its own `ws` entry again, for GHSA-37g9-w4hg-h9qr: replace the 11.x entry with the one from "The fix".
|
|
113
138
|
|
|
114
139
|
```yaml
|
|
115
140
|
# nest-server 11.x only (@nestjs/graphql 13 exact-pins ws; GHSA-96hv-2xvq-fx4p, patched >=8.21.0)
|
|
@@ -134,8 +159,8 @@ tree to `2.0.11` even though nothing needed help any more.
|
|
|
134
159
|
|
|
135
160
|
### The `ws` target must stay in lockstep with the declared `ws` version
|
|
136
161
|
|
|
137
|
-
This applies while a project carries a `ws` entry at all
|
|
138
|
-
`8.21.3` before) is not an arbitrary "latest patch" — it is the version
|
|
162
|
+
This applies while a project carries a `ws` entry at all: on nest-server 11.x, and on 12.9.4 and later. `8.23.0`
|
|
163
|
+
(since 12.9.4; `8.22.0` since 12.0.0, `8.21.3` before) is not an arbitrary "latest patch" — it is the version
|
|
139
164
|
`@lenne.tech/nest-server` declares as an ordinary **dependency**, so it is already in your tree whether
|
|
140
165
|
or not you list `ws` yourself. The override target has to equal it.
|
|
141
166
|
|
|
@@ -147,8 +172,15 @@ when you read a manifest. The target was raised `8.21.1` → `8.21.3` on 2026-08
|
|
|
147
172
|
reason, and to `8.22.0` with nest-server 12.0.0.
|
|
148
173
|
|
|
149
174
|
**Rule:** whenever the `ws` version declared by `@lenne.tech/nest-server` (or by your own
|
|
150
|
-
`package.json`, if you list it) moves, move this override target with it in the
|
|
151
|
-
same applies to any other override whose package also appears as a declared dependency.
|
|
175
|
+
`package.json`, if you list it) moves, move this override's target **and the upper bound of its key** with it in the
|
|
176
|
+
same commit. The same applies to any other override whose package also appears as a declared dependency.
|
|
177
|
+
|
|
178
|
+
The key matters as much as the target. pnpm matches a key against the REQUESTED range, and `@nestjs/graphql` requests
|
|
179
|
+
an exact version. A starter-derived project carries `'ws@>=8.0.0 <8.22.1': 8.22.1` (since 12.8.4): raising only its
|
|
180
|
+
target to `8.23.0` leaves `@nestjs/graphql`'s request for exactly `8.22.1` outside the key, the tree keeps `ws@8.22.1`
|
|
181
|
+
next to `8.23.0`, and the GraphQL WebSocket server stays on the vulnerable copy — while `pnpm audit` is green, because
|
|
182
|
+
GHSA-37g9-w4hg-h9qr is not in GitHub's global database yet. Replace that entry with
|
|
183
|
+
`'ws@>=8.0.0 <8.23.0': '8.23.0'` and check that `pnpm why ws` lists a single `ws@8.23.0`.
|
|
152
184
|
|
|
153
185
|
Then:
|
|
154
186
|
|
|
@@ -160,8 +192,10 @@ pnpm test # nothing should regress
|
|
|
160
192
|
|
|
161
193
|
Commit `package.json`/`pnpm-workspace.yaml` **and** `pnpm-lock.yaml` together.
|
|
162
194
|
|
|
163
|
-
> Projects generated from `nest-server-starter` or `lt-monorepo`
|
|
164
|
-
>
|
|
195
|
+
> Projects generated from `nest-server-starter` or `lt-monorepo` carry the starter's overrides as they were when the
|
|
196
|
+
> project was created. Since 12.9.4 that is not enough for `ws`: their entry `'ws@>=8.0.0 <8.22.1': 8.22.1` misses the
|
|
197
|
+
> exact `8.22.1` that `@nestjs/graphql` 14.2.1 requests and must be **replaced** by the entry from "The fix" (see
|
|
198
|
+
> the rule above).
|
|
165
199
|
|
|
166
200
|
## Rules for writing your own overrides
|
|
167
201
|
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Migration Guide: 12.9.2 → 12.9.3
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | Byte ranges from every file store: `getFileStream(id, { range })`, `CoreS3Service.getObject(key, { range })`, `resolveDownloadRange()`, `setDownloadRangeHeaders()`, `parseRangeHeader()`; `TestDownloadOptions.headers` |
|
|
9
|
+
| **Bugfixes** | `GET /files/id/:id` and `GET /files/:filename` answered every `Range` request with `200` and the whole file — Safari does not play a video from such a server (§1) |
|
|
10
|
+
| **Migration Effort** | Projects with their OWN route for video, audio or large downloads: answer `Range` (§2). Everyone else: `pnpm run update` |
|
|
11
|
+
|
|
12
|
+
## Quick Migration
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @lenne.tech/nest-server@12.9.3
|
|
16
|
+
pnpm run update # nest-server-starter based projects
|
|
17
|
+
grep -rn "pipeFileToResponse(" src/ # own download routes: serve ranges where media is delivered (§2)
|
|
18
|
+
pnpm run build
|
|
19
|
+
pnpm test
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual.
|
|
23
|
+
|
|
24
|
+
## 1. The framework's download routes answer ranges
|
|
25
|
+
|
|
26
|
+
Safari refuses to play a video, and stalls audio, from a server that answers a `Range` request with `200` and the
|
|
27
|
+
whole file. Chrome plays it anyway, so the problem showed neither locally nor in a Chromium-based Playwright run — it
|
|
28
|
+
showed on the first iPhone. Until 12.9.2 the core could not read a byte range at all: `CoreS3Service.getObject()` sent
|
|
29
|
+
no `Range`, and `getFileStream()` always opened the whole file. A project could serve ranges from GridFS and the
|
|
30
|
+
filesystem with its own code, but not from S3, which every deployed stage with `s3` configured uses.
|
|
31
|
+
|
|
32
|
+
Since 12.9.3 both download routes answer ranges on every store, without any change in the project:
|
|
33
|
+
|
|
34
|
+
| Request | Answer |
|
|
35
|
+
|---|---|
|
|
36
|
+
| `Range: bytes=0-1` (one range inside the file) | `206`, `Content-Range: bytes 0-1/<length>`, `Content-Length: 2`, only those bytes |
|
|
37
|
+
| `Range: bytes=1000-` / `bytes=-500` | `206` with the rest of the file / its last 500 bytes |
|
|
38
|
+
| A range starting at or past the end of the file | `416`, `Content-Range: bytes */<length>`, `#LTNS_0503` |
|
|
39
|
+
| Several ranges, an invalid header, `If-Range` | `200` with the whole file, as before |
|
|
40
|
+
| No `Range` | `200` as before, now with `Accept-Ranges: bytes` |
|
|
41
|
+
|
|
42
|
+
The rights check runs first, as before: a refused or unknown file is `404` whatever the `Range` says. A `206` carries
|
|
43
|
+
`no-transform` in `Cache-Control` (`private, no-store, no-transform` by default), which keeps a `compression`
|
|
44
|
+
middleware from gzipping the partial body.
|
|
45
|
+
|
|
46
|
+
## 2. Own download routes
|
|
47
|
+
|
|
48
|
+
A route of your own that delivers media (or large files a client may resume) answers ranges with three calls:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import {
|
|
52
|
+
downloadAbortSignal,
|
|
53
|
+
openDownloadStream,
|
|
54
|
+
pipeFileToResponse,
|
|
55
|
+
resolveDownloadRange,
|
|
56
|
+
RESPONSE_GONE,
|
|
57
|
+
setDownloadRangeHeaders,
|
|
58
|
+
} from '@lenne.tech/nest-server';
|
|
59
|
+
|
|
60
|
+
@Get('video/:id')
|
|
61
|
+
async video(@Param('id') id: string, @Res() res: Response, @CurrentUser() currentUser) {
|
|
62
|
+
const serviceOptions = { abortSignal: downloadAbortSignal(res), currentUser };
|
|
63
|
+
const resolved = await this.fileService.resolveFile(id, serviceOptions); // rights check, length, store
|
|
64
|
+
if (!resolved) {
|
|
65
|
+
throw new NotFoundException();
|
|
66
|
+
}
|
|
67
|
+
const range = resolveDownloadRange(res, resolved.info.length); // throws 416 past the end
|
|
68
|
+
const stream = await openDownloadStream(res, () =>
|
|
69
|
+
this.fileService.getFileStream(id, { ...serviceOptions, range }, resolved.store),
|
|
70
|
+
);
|
|
71
|
+
if (stream === RESPONSE_GONE) {
|
|
72
|
+
return res;
|
|
73
|
+
}
|
|
74
|
+
if (!stream) {
|
|
75
|
+
throw new NotFoundException();
|
|
76
|
+
}
|
|
77
|
+
res.header('Content-Type', resolved.info.contentType || 'video/mp4');
|
|
78
|
+
res.header('Cache-Control', 'private, no-store');
|
|
79
|
+
setDownloadRangeHeaders(res, range, resolved.info.length); // 206 + Content-Range, or Accept-Ranges only
|
|
80
|
+
return pipeFileToResponse(stream, res);
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- `resolveDownloadRange()` must run AFTER the rights check: a `416` confirms that the file exists.
|
|
85
|
+
- `setDownloadRangeHeaders()` goes after your own headers — it replaces `Content-Length` with the length of the range
|
|
86
|
+
and appends `no-transform` to `Cache-Control`.
|
|
87
|
+
- The range reaches every store: S3 with a ranged `GetObject`, GridFS from the chunk holding the first byte, the
|
|
88
|
+
filesystem from that offset — with the same idle guard and abort signal as a whole file, so an aborted range
|
|
89
|
+
releases its S3 socket at once.
|
|
90
|
+
|
|
91
|
+
**A project that reads ranges itself** (GridFS `openDownloadStream(id, { start, end })`, `createReadStream({ start,
|
|
92
|
+
end })`, a hand-written Range parser) can switch to these functions and drop its own code — and gets S3, which such
|
|
93
|
+
code could not reach. Mind the end: `range.end` is INCLUSIVE, like HTTP and `createReadStream()`; GridFS counts it
|
|
94
|
+
exclusive, which `getFileStream()` translates.
|
|
95
|
+
|
|
96
|
+
Lower-level pieces, for code that does not go through `CoreFileService`:
|
|
97
|
+
|
|
98
|
+
- `CoreS3Service.getObject(key, { range: { start, end } })` — the result carries `contentRange` (S3's
|
|
99
|
+
`bytes <start>-<end>/<total>`) and the range's `contentLength`.
|
|
100
|
+
- `parseRangeHeader(header, length)` — the pure parser (`{ start, end }`, `'unsatisfiable'` or `undefined`), for a
|
|
101
|
+
route that decides `If-Range` with a strong validator of its own.
|
|
102
|
+
- A malformed range (`start > end`, negative, not an integer) throws a `RangeError` before any store is asked.
|
|
103
|
+
|
|
104
|
+
## 3. Tests
|
|
105
|
+
|
|
106
|
+
`testHelper.download()` / `downloadBuffer()` take request headers:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const res = await testHelper.download(`/files/id/${id}`, { headers: { Range: 'bytes=0-1' }, token });
|
|
110
|
+
expect(res.statusCode).toBe(206);
|
|
111
|
+
expect(res.headers['content-range']).toBe(`bytes 0-1/${length}`);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Troubleshooting
|
|
115
|
+
|
|
116
|
+
| Symptom | Cause | Fix |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| A video plays in Chrome but not in Safari (iPhone, macOS) | The route answers `Range` with `200` and the whole file | Framework routes: update. Own route: §2 |
|
|
119
|
+
| `416` with `#LTNS_0503` | The client asked for bytes past the end of the file; `Content-Range: bytes */<length>` names the real size | Usually a player with a cached, outdated length — reload |
|
|
120
|
+
| `RangeError: Invalid byte range` | A `range` with `start > end`, a negative or fractional value reached `getFileStream()` / `getObject()` | Produce it with `resolveDownloadRange()` |
|
|
121
|
+
| `InvalidRange` from S3, or a GridFS stream error `Stream end (…) must not be more than the length of the file` | A `range` past the end of the file reached the store | Resolve it against the file's `length` with `resolveDownloadRange()`, which cuts it at the last byte |
|
|
122
|
+
|
|
123
|
+
## Module Documentation
|
|
124
|
+
|
|
125
|
+
- `src/core/modules/file/README.md` — "Byte ranges: video, audio, resumed downloads (12.9.3)"
|
|
126
|
+
- `src/core/modules/file/INTEGRATION-CHECKLIST.md` — Common Mistakes
|
|
127
|
+
- `docs/error-codes.md` — `LTNS_0503`
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# Migration Guide: 12.9.3 → 12.9.4
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None in nest-server. NestJS 12.5.0, which `pnpm run update` installs, adds startup errors for configurations it used to ignore (§2) |
|
|
8
|
+
| **New Features** | None |
|
|
9
|
+
| **Bugfixes** | Every S3 download the client aborted mid-transfer logged `ERROR File download failed: aborted` with a stack trace (§1) |
|
|
10
|
+
| **Security** | NestJS 12.5.0 closes three high advisories of the Express stack (process crashes; §2). `ws` GHSA-37g9-w4hg-h9qr (high, unauthenticated memory-exhaustion DoS through the GraphQL WebSocket) — add one override to your `pnpm-workspace.yaml` (§3) |
|
|
11
|
+
| **Migration Effort** | `pnpm run update`, one override (§3), a test run against NestJS 12.5.0 (§2) |
|
|
12
|
+
|
|
13
|
+
## Quick Migration
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @lenne.tech/nest-server@12.9.4
|
|
17
|
+
pnpm run update # nest-server-starter based projects
|
|
18
|
+
# add the ws override from §3 to pnpm-workspace.yaml, then: pnpm install
|
|
19
|
+
pnpm run build
|
|
20
|
+
pnpm test
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual.
|
|
24
|
+
|
|
25
|
+
## 1. No ERROR for downloads the client aborts
|
|
26
|
+
|
|
27
|
+
A client that leaves while an S3 download is still streaming is normal traffic, and with 12.9.3 a frequent one: a video
|
|
28
|
+
player aborts the running range on every seek. Each of them logged
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
ERROR [CoreFileController] File download failed: aborted
|
|
32
|
+
Error: aborted
|
|
33
|
+
at Socket.socketCloseListener (node:_http_client:…)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
through the framework's download routes and every project route that delivers with `pipeFileToResponse()` — under the
|
|
37
|
+
load in which the `S3 socket pool exhausted:` line has to stand out.
|
|
38
|
+
|
|
39
|
+
The cause is an order of listeners. `downloadAbortSignal(res)` is created before the rights check (the client may leave
|
|
40
|
+
while it runs), so its listeners come first. On the event that reports the client gone it aborts the S3 request, and the
|
|
41
|
+
SDK fails the body with `aborted` inside that same dispatch, before `pipeFileToResponse()` has seen the client leave.
|
|
42
|
+
It shows through a Nest route, whose GET request is not read to its end, so the request's `close` reports the abort.
|
|
43
|
+
|
|
44
|
+
Since 12.9.4 `pipeFileToResponse()` reads a stream error while the client is gone (`isResponseGone(res)`, or one of its
|
|
45
|
+
own client-gone signals) as the end of an aborted download: nothing is sent, one debug line is logged, the socket was
|
|
46
|
+
already released. A store error while the client is still connected is an ERROR as before.
|
|
47
|
+
|
|
48
|
+
Nothing to change in a project. A route that delivers with its own code instead of `pipeFileToResponse()` should switch
|
|
49
|
+
to it (see the 12.8.9 guide).
|
|
50
|
+
|
|
51
|
+
## 2. Dependencies: NestJS 12.5.0
|
|
52
|
+
|
|
53
|
+
12.9.4 declares NestJS 12.5.0. `pnpm run update` raises these pins of a nest-server-starter based project:
|
|
54
|
+
|
|
55
|
+
| Package | From | To |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| `@nestjs/common`, `@nestjs/core`, `@nestjs/platform-express` | 12.4.2 | 12.5.0 |
|
|
58
|
+
| `@nestjs/graphql` | 14.2.0 | 14.2.1 |
|
|
59
|
+
| `@nestjs/schedule` | 12.0.3 | 12.0.4 |
|
|
60
|
+
| `@nestjs/swagger` | 12.1.1 | 12.1.2 |
|
|
61
|
+
| `@nestjs/cli` (dev) | 12.0.12 | 12.0.13 |
|
|
62
|
+
| `@nestjs/testing` (dev) | 12.4.2 | 12.5.0 |
|
|
63
|
+
| `oxlint-tsgolint` (dev) | 7.0.2006 | 7.0.2007 |
|
|
64
|
+
| `vite` (dev) | 8.3.7 | 8.3.8 |
|
|
65
|
+
|
|
66
|
+
Update `@nestjs/core` and the platform package together: the adapter passes new route-matching options to the core.
|
|
67
|
+
|
|
68
|
+
**Why now: NestJS 12.5.0 is a security release.** Its advisories of 2026-11-12 include three high ones for an
|
|
69
|
+
application on Express, all fixed only in 12.5.0:
|
|
70
|
+
|
|
71
|
+
| Advisory | Package | What |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| [GHSA-q223-fc7f-xg75](https://github.com/nestjs/nest/security/advisories/GHSA-q223-fc7f-xg75) | `@nestjs/core` 12.4.x | Process crash when a handler returns a stream that fails before it is read |
|
|
74
|
+
| [GHSA-c8fr-c7pf-58pf](https://github.com/nestjs/nest/security/advisories/GHSA-c8fr-c7pf-58pf) | `@nestjs/platform-express` < 12.5.0 | Process crash via a multipart request with many fields |
|
|
75
|
+
| [GHSA-jgpx-8x6j-fcpf](https://github.com/nestjs/nest/security/advisories/GHSA-jgpx-8x6j-fcpf) | `@nestjs/common` < 12.5.0 | Process crash via many files validated at once by `ParseFilePipe` |
|
|
76
|
+
|
|
77
|
+
They were not in GitHub's global advisory database on 2026-11-14, so `pnpm audit` does not report them yet. A project
|
|
78
|
+
that pins `@nestjs/*` itself below 12.5.0 stays affected whatever nest-server version it runs.
|
|
79
|
+
|
|
80
|
+
NestJS 12.5.0 changes behaviour that a REST / GraphQL application on Express can meet (full list in its
|
|
81
|
+
[release notes](https://github.com/nestjs/nest/releases/tag/v12.5.0)):
|
|
82
|
+
|
|
83
|
+
- **New startup errors.** A guard, interceptor or pipe that cannot run (an abstract class as token, a class given as
|
|
84
|
+
the `useValue` of `APP_GUARD`) and an invalid `forRoutes()` / `exclude()` entry now stop the application at startup.
|
|
85
|
+
They did nothing before, so the fix is to correct or remove the entry — boot the application once after the update.
|
|
86
|
+
- Middleware passed to `forRoutes()` is now also mounted on routes excluded from the global prefix.
|
|
87
|
+
- Multipart requests are capped at 1000 parts by default (`limits.parts` raises it), which also caps a `maxCount` above 1000.
|
|
88
|
+
- A `SyntaxError` / `URIError` without a status thrown by an `app.use()` middleware answers 500 instead of 400.
|
|
89
|
+
- An `@Optional()` dependency that exists but fails to build now fails its consumer instead of arriving as `undefined`.
|
|
90
|
+
- A limit given as `NaN` (`Number(process.env.UNSET)`) now gets its default instead of switching the limit off.
|
|
91
|
+
|
|
92
|
+
`@apollo/server` stays at 5.5.1. 5.5.2 caps `@graphql-tools/utils` below 12.0.2 for Node 20 installs and thereby brings
|
|
93
|
+
back GHSA-c4qx-w738-7hr3 (high); this framework requires Node >= 22.15, so 5.5.2 adds nothing. Do not raise it in a
|
|
94
|
+
project either.
|
|
95
|
+
|
|
96
|
+
**better-auth** is your project's own pin; nest-server 12.9.4 is tested with 1.7.23 and its peer range is unchanged.
|
|
97
|
+
If you raise it past 1.7.21, two changes from better-auth apply: a throw from `sendVerificationEmail` no longer rejects
|
|
98
|
+
a sign-up (it is logged, the sign-up succeeds — the user stays unverified and can request the mail again), and the
|
|
99
|
+
MongoDB adapter builds indexes for fields marked `unique` / `index` at startup — one that fails (duplicate e-mail
|
|
100
|
+
addresses, a conflicting index) has to be fixed before the application starts cleanly.
|
|
101
|
+
|
|
102
|
+
## 3. Security: one override for `ws`
|
|
103
|
+
|
|
104
|
+
[GHSA-37g9-w4hg-h9qr](https://github.com/websockets/ws/security/advisories/GHSA-37g9-w4hg-h9qr) (high): a client floods
|
|
105
|
+
ping frames without reading the replies, the server buffers the automatic pongs, and the process runs out of memory. It
|
|
106
|
+
needs no authentication, and every application with GraphQL subscriptions accepts the WebSocket. Patched in `ws`
|
|
107
|
+
8.23.0. The GraphQL WebSocket server runs on the `ws` copy of `@nestjs/graphql`, which 14.2.1 — the newest release —
|
|
108
|
+
exact-pins at 8.22.1. nest-server's own `overrides:` do not reach your install (`docs/security-overrides.md`), so put the
|
|
109
|
+
entry into your project's `pnpm-workspace.yaml` and run `pnpm install`. A nest-server-starter based project already has
|
|
110
|
+
an 8.x entry, `'ws@>=8.0.0 <8.22.1': 8.22.1`: **replace** it, do not only raise its target — its key does not match
|
|
111
|
+
the exact `8.22.1` that `@nestjs/graphql` requests, so the vulnerable copy would stay. The 7.x entry stays as it is.
|
|
112
|
+
|
|
113
|
+
```yaml
|
|
114
|
+
overrides:
|
|
115
|
+
# GHSA-37g9-w4hg-h9qr (high): @nestjs/graphql 14.2.1 exact-pins ws 8.22.1. Keep the target in lockstep with the
|
|
116
|
+
# ws version @lenne.tech/nest-server declares (8.23.0 since 12.9.4). Remove once @nestjs/graphql pins ws >= 8.23.0.
|
|
117
|
+
'ws@>=8.0.0 <8.23.0': '8.23.0'
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`pnpm why ws` must then show a single `ws@8.23.0`. Until GitHub's global advisory database lists the advisory,
|
|
121
|
+
`pnpm audit` does not report the vulnerable version — a green audit does not mean the project is not affected. A
|
|
122
|
+
vendor-mode project adds the same entry.
|
|
123
|
+
|
|
124
|
+
## Troubleshooting
|
|
125
|
+
|
|
126
|
+
| Symptom | Cause | Fix |
|
|
127
|
+
|---|---|---|
|
|
128
|
+
| `ERROR [CoreFileController] File download failed: aborted` with `socketCloseListener` in the stack, one per aborted download | 12.8.9 – 12.9.3 on S3 | Update to 12.9.4 |
|
|
129
|
+
| The application stops at startup after the update, naming a guard, interceptor, pipe or a `forRoutes()` / `exclude()` entry | NestJS 12.5.0 refuses what 12.4 ignored (§2) | Correct or remove the entry; it never took effect |
|
|
130
|
+
| `pnpm why ws` lists `ws@8.22.1` under `@nestjs/graphql` | The override from §3 is missing, or the old `<8.22.1` key was kept with only its target raised | Use exactly `'ws@>=8.0.0 <8.23.0': '8.23.0'`, `pnpm install` |
|
|
131
|
+
|
|
132
|
+
## Module Documentation
|
|
133
|
+
|
|
134
|
+
- `src/core/modules/file/README.md` — "Error responses"
|
|
135
|
+
- `docs/security-overrides.md` — "Status since 12.9.4", "The fix"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "12.9.
|
|
3
|
+
"version": "12.9.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Server framework in TypeScript on top of NestJS: GraphQL and REST APIs on MongoDB, PostgreSQL, MySQL or MariaDB, with authentication, field-level permissions, multi-tenancy, offline sync and an AI assistant.",
|
|
6
6
|
"keywords": [
|
|
@@ -106,15 +106,15 @@
|
|
|
106
106
|
"@as-integrations/express5": "1.1.2",
|
|
107
107
|
"@getbrevo/brevo": "6.2.0",
|
|
108
108
|
"@modelcontextprotocol/sdk": "1.32.2",
|
|
109
|
-
"@nestjs/apollo": "14.2.
|
|
110
|
-
"@nestjs/common": "12.
|
|
111
|
-
"@nestjs/core": "12.
|
|
112
|
-
"@nestjs/graphql": "14.2.
|
|
113
|
-
"@nestjs/jwt": "12.0.
|
|
114
|
-
"@nestjs/passport": "12.0.
|
|
115
|
-
"@nestjs/platform-express": "12.
|
|
116
|
-
"@nestjs/schedule": "12.0.
|
|
117
|
-
"@nestjs/swagger": "12.1.
|
|
109
|
+
"@nestjs/apollo": "14.2.1",
|
|
110
|
+
"@nestjs/common": "12.5.0",
|
|
111
|
+
"@nestjs/core": "12.5.0",
|
|
112
|
+
"@nestjs/graphql": "14.2.1",
|
|
113
|
+
"@nestjs/jwt": "12.0.5",
|
|
114
|
+
"@nestjs/passport": "12.0.2",
|
|
115
|
+
"@nestjs/platform-express": "12.5.0",
|
|
116
|
+
"@nestjs/schedule": "12.0.4",
|
|
117
|
+
"@nestjs/swagger": "12.1.2",
|
|
118
118
|
"@nestjs/terminus": "12.1.0",
|
|
119
119
|
"@tus/file-store": "2.1.2",
|
|
120
120
|
"@tus/server": "2.4.7",
|
|
@@ -128,7 +128,7 @@
|
|
|
128
128
|
"dotenv": "18.0.10",
|
|
129
129
|
"ejs": "7.0.2",
|
|
130
130
|
"express": "5.3.0",
|
|
131
|
-
"graphql": "16.
|
|
131
|
+
"graphql": "16.15.0",
|
|
132
132
|
"graphql-query-complexity": "2.0.0",
|
|
133
133
|
"graphql-subscriptions": "3.0.0",
|
|
134
134
|
"graphql-upload": "17.1.0",
|
|
@@ -139,7 +139,7 @@
|
|
|
139
139
|
"lodash": "4.18.1",
|
|
140
140
|
"multer": "2.5.0",
|
|
141
141
|
"node-mailjet": "6.0.11",
|
|
142
|
-
"nodemailer": "10.
|
|
142
|
+
"nodemailer": "10.5.0",
|
|
143
143
|
"passport": "0.7.0",
|
|
144
144
|
"passport-jwt": "4.0.1",
|
|
145
145
|
"reflect-metadata": "0.2.2",
|
|
@@ -148,7 +148,7 @@
|
|
|
148
148
|
"supertest": "7.3.2",
|
|
149
149
|
"ts-morph": "28.0.0",
|
|
150
150
|
"undici": "7.30.2",
|
|
151
|
-
"ws": "8.
|
|
151
|
+
"ws": "8.23.0",
|
|
152
152
|
"yuml-diagram": "1.2.0"
|
|
153
153
|
},
|
|
154
154
|
"peerDependencies": {
|
|
@@ -228,16 +228,16 @@
|
|
|
228
228
|
}
|
|
229
229
|
},
|
|
230
230
|
"devDependencies": {
|
|
231
|
-
"@aws-sdk/client-s3": "3.
|
|
232
|
-
"@aws-sdk/lib-storage": "3.
|
|
233
|
-
"@aws-sdk/s3-request-presigner": "3.
|
|
234
|
-
"@better-auth/core": "1.7.
|
|
235
|
-
"@better-auth/passkey": "1.7.
|
|
231
|
+
"@aws-sdk/client-s3": "3.1168.0",
|
|
232
|
+
"@aws-sdk/lib-storage": "3.1168.0",
|
|
233
|
+
"@aws-sdk/s3-request-presigner": "3.1168.0",
|
|
234
|
+
"@better-auth/core": "1.7.23",
|
|
235
|
+
"@better-auth/passkey": "1.7.23",
|
|
236
236
|
"@compodoc/compodoc": "2.0.0",
|
|
237
|
-
"@nestjs/cli": "12.0.
|
|
237
|
+
"@nestjs/cli": "12.0.13",
|
|
238
238
|
"@nestjs/mongoose": "12.0.1",
|
|
239
239
|
"@nestjs/schematics": "12.0.13",
|
|
240
|
-
"@nestjs/testing": "12.
|
|
240
|
+
"@nestjs/testing": "12.5.0",
|
|
241
241
|
"@swc/cli": "0.8.2",
|
|
242
242
|
"@swc/core": "1.16.22",
|
|
243
243
|
"@tus/s3-store": "2.1.0",
|
|
@@ -251,12 +251,12 @@
|
|
|
251
251
|
"@types/passport": "1.0.17",
|
|
252
252
|
"@types/pg": "8.23.1",
|
|
253
253
|
"@vitest/coverage-v8": "5.1.0",
|
|
254
|
-
"better-auth": "1.7.
|
|
255
|
-
"bullmq": "6.7.
|
|
254
|
+
"better-auth": "1.7.23",
|
|
255
|
+
"bullmq": "6.7.6",
|
|
256
256
|
"cross-env": "10.1.0",
|
|
257
257
|
"drizzle-orm": "1.0.0-rc.5",
|
|
258
258
|
"ioredis": "6.0.0",
|
|
259
|
-
"kysely": "0.29.
|
|
259
|
+
"kysely": "0.29.9",
|
|
260
260
|
"mongodb": "7.8.0",
|
|
261
261
|
"mongoose": "9.12.1",
|
|
262
262
|
"mysql2": "3.24.6",
|
|
@@ -264,17 +264,17 @@
|
|
|
264
264
|
"otpauth": "9.6.0",
|
|
265
265
|
"oxfmt": "0.78.0",
|
|
266
266
|
"oxlint": "1.93.0",
|
|
267
|
-
"oxlint-tsgolint": "7.0.
|
|
267
|
+
"oxlint-tsgolint": "7.0.2007",
|
|
268
268
|
"pg": "8.23.1",
|
|
269
269
|
"playwright-core": "1.65.0",
|
|
270
270
|
"rimraf": "6.1.4",
|
|
271
|
-
"smtp-server": "3.21.
|
|
271
|
+
"smtp-server": "3.21.4",
|
|
272
272
|
"tsx": "4.23.15",
|
|
273
273
|
"tus-js-client": "4.3.1",
|
|
274
|
-
"typeorm": "1.1.
|
|
274
|
+
"typeorm": "1.1.3",
|
|
275
275
|
"typescript": "6.0.3",
|
|
276
276
|
"unplugin-swc": "2.0.0",
|
|
277
|
-
"vite": "8.3.
|
|
277
|
+
"vite": "8.3.8",
|
|
278
278
|
"vitest": "5.1.0"
|
|
279
279
|
},
|
|
280
280
|
"main": "dist/index.js",
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A byte range of a file: the bytes `start` through `end`, both INCLUSIVE — the way HTTP counts them
|
|
3
|
+
* (`Range: bytes=0-1` is two bytes) and `fs.createReadStream()` takes them.
|
|
4
|
+
*
|
|
5
|
+
* Read with `CoreFileService.getFileStream(id, { range })` / `getFileStreamByName()` or
|
|
6
|
+
* `CoreS3Service.getObject(key, { range })`. The range must lie inside the file (`end` below its
|
|
7
|
+
* length); {@link parseRangeHeader} / `resolveDownloadRange()` produce such a range from a request.
|
|
8
|
+
*/
|
|
9
|
+
export interface ByteRange {
|
|
10
|
+
/** Last byte, inclusive */
|
|
11
|
+
end: number;
|
|
12
|
+
/** First byte, counted from 0 */
|
|
13
|
+
start: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What {@link parseRangeHeader} decided about a `Range` header:
|
|
18
|
+
* - a {@link ByteRange} — deliver these bytes (`206 Partial Content`);
|
|
19
|
+
* - `'unsatisfiable'` — the range starts at or past the end of the file (`416 Range Not Satisfiable`);
|
|
20
|
+
* - `undefined` — deliver the whole file (`200`), the header asks for nothing this server serves.
|
|
21
|
+
*/
|
|
22
|
+
export type RangeHeaderDecision = 'unsatisfiable' | ByteRange | undefined;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Longest `Range` header that is parsed at all. A single byte range of a file the size of any store's limit is
|
|
26
|
+
* well below 100 characters; a longer header is a list of ranges or garbage, and both are answered with the whole
|
|
27
|
+
* file anyway.
|
|
28
|
+
*/
|
|
29
|
+
const MAX_RANGE_HEADER_LENGTH = 200;
|
|
30
|
+
|
|
31
|
+
/** One range of the set, `first-last`, `first-` or `-suffix`. Anchored and without nested quantifiers. */
|
|
32
|
+
const RANGE_SPEC = /^(\d*)[ \t]*-[ \t]*(\d*)$/;
|
|
33
|
+
|
|
34
|
+
/** The unit and the range set: `bytes=<set>`, the unit case-insensitive (RFC 9110 §14.1) */
|
|
35
|
+
const RANGE_UNIT = /^[ \t]*bytes[ \t]*=(.*)$/i;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Decide what a `Range` request header asks for, against a file of `length` bytes (RFC 9110 §14.1.2, §14.2).
|
|
39
|
+
*
|
|
40
|
+
* Only ONE range is served. The whole file (`undefined`) is the answer to:
|
|
41
|
+
* - no header, an empty one, or a unit other than `bytes`;
|
|
42
|
+
* - an invalid header (`bytes=5-2`, `bytes=abc`, `bytes=-`) — RFC 9110 lets a server ignore it, and the whole file
|
|
43
|
+
* is what a client that sent garbage can still use;
|
|
44
|
+
* - SEVERAL ranges (`bytes=0-1,5-9`): a `multipart/byteranges` answer is not served, the way Express's `send` does
|
|
45
|
+
* it. Media players and download managers ask for one range at a time;
|
|
46
|
+
* - a file whose length is unknown (`undefined`, negative, not an integer): a `206` must name the total length.
|
|
47
|
+
*
|
|
48
|
+
* `'unsatisfiable'` is the answer to a single range that starts at or past the end of the file, or a suffix range
|
|
49
|
+
* of length 0 — and to every range of an EMPTY file, which has no byte to start at.
|
|
50
|
+
*
|
|
51
|
+
* A range that ends past the file is cut at its last byte, and a suffix longer than the file is the whole file —
|
|
52
|
+
* both are satisfiable by definition, so a client may ask for `bytes=0-` or `bytes=0-999999999` without knowing
|
|
53
|
+
* the size.
|
|
54
|
+
*
|
|
55
|
+
* Pure function, no I/O — the HTTP glue (`resolveDownloadRange()`, `setDownloadRangeHeaders()`) lives next to
|
|
56
|
+
* `pipeFileToResponse()` in the file module.
|
|
57
|
+
*/
|
|
58
|
+
export function parseRangeHeader(
|
|
59
|
+
header: string | string[] | undefined,
|
|
60
|
+
length: number | undefined,
|
|
61
|
+
): RangeHeaderDecision {
|
|
62
|
+
if (typeof length !== 'number' || !Number.isSafeInteger(length) || length < 0) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
// Node joins repeated headers with ', ' — a repeated Range header is a list of ranges, answered with the whole file
|
|
66
|
+
const value = Array.isArray(header) ? header.join(',') : header;
|
|
67
|
+
if (typeof value !== 'string' || !value || value.length > MAX_RANGE_HEADER_LENGTH) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const unit = RANGE_UNIT.exec(value);
|
|
71
|
+
if (!unit) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
// Empty list elements are allowed and ignored (RFC 9110 §5.6.1)
|
|
75
|
+
const specs = unit[1]
|
|
76
|
+
.split(',')
|
|
77
|
+
.map((spec) => spec.trim())
|
|
78
|
+
.filter(Boolean);
|
|
79
|
+
if (specs.length !== 1) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
const match = RANGE_SPEC.exec(specs[0]);
|
|
83
|
+
if (!match) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const [, first, last] = match;
|
|
87
|
+
if (!first && !last) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
if (!first) {
|
|
91
|
+
// Suffix range: the last N bytes
|
|
92
|
+
const suffix = toSafeInteger(last);
|
|
93
|
+
if (suffix === undefined) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
if (suffix === 0 || length === 0) {
|
|
97
|
+
return 'unsatisfiable';
|
|
98
|
+
}
|
|
99
|
+
return { end: length - 1, start: Math.max(0, length - suffix) };
|
|
100
|
+
}
|
|
101
|
+
const start = toSafeInteger(first);
|
|
102
|
+
const end = last ? toSafeInteger(last) : length - 1;
|
|
103
|
+
if (start === undefined || end === undefined) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
if (last && end < start) {
|
|
107
|
+
// `bytes=5-2` is invalid, not unsatisfiable — ignored
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
if (start >= length) {
|
|
111
|
+
return 'unsatisfiable';
|
|
112
|
+
}
|
|
113
|
+
return { end: Math.min(end, length - 1), start };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Check a range handed to a store read: two safe integers, `0 <= start <= end`. Returns it unchanged, `undefined`
|
|
118
|
+
* for no range, and throws a `RangeError` for anything else — the same answer from every store, before any of them
|
|
119
|
+
* is asked (each would otherwise fail in its own way: S3 with `InvalidRange`, GridFS with a driver error on the
|
|
120
|
+
* stream, the filesystem with a silently empty stream).
|
|
121
|
+
*/
|
|
122
|
+
export function assertByteRange(range: unknown): ByteRange | undefined {
|
|
123
|
+
if (range === undefined || range === null) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
const { end, start } = range as { end: number; start: number };
|
|
127
|
+
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end < start) {
|
|
128
|
+
throw new RangeError(
|
|
129
|
+
`Invalid byte range { start: ${String(start)}, end: ${String(end)} }: start and end must be integers with 0 <= start <= end (end inclusive)`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return { end, start };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The `Content-Range` value of a range of a file with `length` bytes: `bytes 0-1/1234` */
|
|
136
|
+
export function contentRangeValue(range: ByteRange, length: number): string {
|
|
137
|
+
return `bytes ${range.start}-${range.end}/${length}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Digits as a safe integer, `undefined` when they exceed what a Number holds exactly */
|
|
141
|
+
function toSafeInteger(digits: string): number | undefined {
|
|
142
|
+
const value = Number(digits);
|
|
143
|
+
return Number.isSafeInteger(value) ? value : undefined;
|
|
144
|
+
}
|