@lenne.tech/nest-server 12.8.7 → 12.8.9
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/configurable-features.md +1 -0
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +7 -1
- package/dist/core/common/helpers/idle-stream.helper.d.ts +14 -0
- package/dist/core/common/helpers/idle-stream.helper.js +94 -0
- package/dist/core/common/helpers/idle-stream.helper.js.map +1 -0
- package/dist/core/common/interfaces/server-options.interface.d.ts +6 -0
- package/dist/core/common/services/core-s3.service.d.ts +27 -1
- package/dist/core/common/services/core-s3.service.js +89 -3
- package/dist/core/common/services/core-s3.service.js.map +1 -1
- package/dist/core/modules/file/core-file.controller.d.ts +16 -0
- package/dist/core/modules/file/core-file.controller.js +215 -8
- package/dist/core/modules/file/core-file.controller.js.map +1 -1
- package/dist/core/modules/file/core-file.service.d.ts +5 -0
- package/dist/core/modules/file/core-file.service.js +17 -7
- package/dist/core/modules/file/core-file.service.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 +4 -1
- package/dist/core/modules/file/s3-file.helper.js +2 -2
- 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/main.js +1 -3
- package/dist/main.js.map +1 -1
- package/docs/REQUEST-LIFECYCLE.md +2 -2
- package/migration-guides/12.8.7-to-12.8.8.md +44 -0
- package/migration-guides/12.8.8-to-12.8.9.md +149 -0
- package/package.json +3 -3
- package/src/core/common/helpers/idle-stream.helper.ts +192 -0
- package/src/core/common/interfaces/server-options.interface.ts +78 -0
- package/src/core/common/services/core-s3.service.ts +186 -3
- package/src/core/modules/file/INTEGRATION-CHECKLIST.md +15 -14
- package/src/core/modules/file/README.md +48 -0
- package/src/core/modules/file/core-file.controller.ts +356 -10
- package/src/core/modules/file/core-file.service.ts +46 -7
- package/src/core/modules/file/interfaces/file-service-options.interface.ts +22 -0
- package/src/core/modules/file/s3-file.helper.ts +6 -2
- package/src/index.ts +1 -0
- package/src/main.ts +3 -3
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,WAAW,MAAM,aAAa,CAAC;AACtC,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAC5G,OAAO,EAAE,sBAAsB,EAAE,MAAM,oDAAoD,CAAC;AAC5F,OAAO,EAAE,uBAAuB,EAAE,MAAM,mDAAmD,CAAC;AAC5F,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,qDAAqD,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,4CAA4C,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,yCAAyC,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAC/E,OAAO,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAKzD,KA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,CAAC;IAGrE,IAAI,SAAS,CAAC,aAAa,EAAE,CAAC;QAC5B,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YACtD,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO,CAAC,KAAK,CAAC,UAAU,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;gBACzC,OAAO;YACT,CAAC;YAED,IAAI,MAAM,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,WAAW,MAAM,EAAE,CAAC,CAAC;gBACnC,OAAO;YACT,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAKD,SAAS,EAAE,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC"}
|
|
@@ -42,7 +42,7 @@ The `CoreModule` is a dynamic module that bootstraps the entire framework:
|
|
|
42
42
|
| **Mongoose Plugins** | Auto-registration of ID, password, audit, and role guard plugins |
|
|
43
43
|
| **GraphQL Subscriptions** | WebSocket support with JWT/session authentication (cluster-wide when `redis` is configured — see the Subscriptions row under GraphQL Features) |
|
|
44
44
|
| **Central Redis (optional)** | `CoreRedisService` — globally provided **and exported** by `CoreModule`, always present but **inert unless `redis` is configured** ("presence implies enabled"). Injected with `@Optional()`; every consumer keeps a process-local fallback. One service serves all features: shared client (`getClient()`), one cached subscriber (`getSubscriber()` — a subscribing client cannot run commands), dedicated connections (`createClient(label)`); all tracked and quit on shutdown. Keys are namespaced by the framework per key via `key(...)`, **not** through ioredis's own `keyPrefix` (that would collide with BullMQ's prefix). Requires the OPTIONAL peer `ioredis` — configured but missing **fails the boot**. Switches on: exact cross-replica rate limits, cron deduplication, `CoreRedisPubSub` as `PUB_SUB`, tenant-cache invalidation broadcast, Better-Auth user-cache (roles/verification) invalidation broadcast, Hub collector mirroring, MCP session registry |
|
|
45
|
-
| **Central S3 (optional)** | `CoreS3Service` — globally provided **and exported** by `CoreModule`, inert unless `s3` is configured *and* names a `bucket` (a bucket-less block is ignored with a warning). Backs `file.storage: 's3'` and TUS staging (`tus.s3Staging`). Requires the OPTIONAL peers `@aws-sdk/client-s3` (+ `@aws-sdk/s3-request-presigner` for `presignedDownloads`) — configured but missing **fails the boot**. `@aws-sdk/lib-storage` (optional) uploads streams of unknown length in parts; without it they are buffered in memory, with one boot warning |
|
|
45
|
+
| **Central S3 (optional)** | `CoreS3Service` — globally provided **and exported** by `CoreModule`, inert unless `s3` is configured *and* names a `bucket` (a bucket-less block is ignored with a warning). Backs `file.storage: 's3'` and TUS staging (`tus.s3Staging`). Requires the OPTIONAL peers `@aws-sdk/client-s3` (+ `@aws-sdk/s3-request-presigner` for `presignedDownloads`) — configured but missing **fails the boot**. `@aws-sdk/lib-storage` (optional) uploads streams of unknown length in parts; without it they are buffered in memory, with one boot warning. Since 12.8.9 the client runs on its own agents (`s3.maxSockets` 200, `connectionTimeoutMs`, `socketTimeoutMs`), every body from `getObject()` is idle-guarded (`s3.downloadIdleTimeoutMs`, `guardIdleStream()`), and an exhausted socket pool is logged as an ERROR once per minute (`getSocketPoolStats()`) |
|
|
46
46
|
| **Configuration System** | `config.env.ts` with ENV variables, `NEST_SERVER_CONFIG` JSON, `NSC__*` prefixes |
|
|
47
47
|
| **Cookie Handling** | Enabled by default (`cookies: true`), configurable via `ICookiesConfig` with `exposeTokenInBody` option |
|
|
48
48
|
| **Body Parser Limits** | `bodyParser.json.limit` / `bodyParser.urlencoded.limit` (default 100 kB each, body-parser's own). Applied in place to the parsers NestJS registers, so they hold under `Test.createTestingModule()` too. Every 413 is answered with `#LTNS_0304` plus size and limit, and logged with the route and the config key — see [0d. Body parsers](#0d-body-parsers) |
|
|
@@ -170,7 +170,7 @@ JWT-based authentication for existing projects:
|
|
|
170
170
|
| Feature | Description |
|
|
171
171
|
|---------|-------------|
|
|
172
172
|
| **File Module** | Upload/download with MongoDB GridFS storage |
|
|
173
|
-
| **REST Endpoints** | `GET /files/id/:id`, `GET /files/:filename` (core, gated by `file.downloadRoles`, default ADMIN); `POST /files/upload`, `DELETE /files/:id` (project-specific) |
|
|
173
|
+
| **REST Endpoints** | `GET /files/id/:id`, `GET /files/:filename` (core, gated by `file.downloadRoles`, default ADMIN); `POST /files/upload`, `DELETE /files/:id` (project-specific). Since 12.8.9 the download routes answer after the rights check: weak `ETag` + `If-None-Match` → `304` without a store read, `Cache-Control` per `file.downloadCaching`, `Content-Length`; the store request carries an abort signal (`downloadAbortSignal()`), delivery goes through `pipeFileToResponse()` (releases the source when the client goes), every stream is idle-guarded (`file.downloadIdleTimeoutMs`), and a store that gives no connection in time answers `503` + `Retry-After` |
|
|
174
174
|
| **GraphQL Endpoints** | `getFileInfo` (`file.downloadRoles`), `uploadFile` / `uploadFiles` (`file.uploadRoles`), `deleteFile` (`file.deleteRoles`) — all default ADMIN |
|
|
175
175
|
| **File access control** | Roles are the coarse filter; per-file rules go in `CoreFileService.checkRights()` using metadata written at upload time. Cover BOTH the `id` and the `filename` branch — the filename route authorizes on the by-name lookup alone when presigned S3 downloads are on, and `deleteFileByName()` always does. Working reference: `src/server/modules/file/file.service.ts` (with `file.downloadRoles: [S_USER]` in `src/config.env.ts`, so the rule is actually reached). Both file classes carry `@SkipTenantCheck()` — GridFS is not tenant-scoped |
|
|
176
176
|
| **TUS Module** | Resumable uploads via tus.io protocol (creation, termination, expiration), gated by `tus.roles` (default `S_USER`); `OPTIONS` stays public for the CORS preflight. Records owner and, with `multiTenancy` only, the validated tenant at upload creation; a custom service plugs in via `TusModule.forRoot({ service })`. Configured by `TusModule.forRoot({ config })`, else the server config's `tus` key (11.42.9+). Switched-off `creation` / `termination` answer 501 (enforced by `CoreTusService`, not `@tus/server`); upload URLs follow the path the upload was created at. The response that completes an upload carries the stored file's ID as `X-File-Id` (11.42.13+, listed in `Access-Control-Expose-Headers`) — the last segment of the TUS URL is the upload id, not the file id |
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Migration Guide: 12.8.7 → 12.8.8
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | None |
|
|
9
|
+
| **Bugfixes** | Security: undici 7.30.2 and nodemailer 10.4.1 close eight advisories in the framework's runtime dependencies (§1) |
|
|
10
|
+
| **Migration Effort** | `pnpm run update`. A project that pins undici or nodemailer itself — vendored cores included — raises them (§1) |
|
|
11
|
+
|
|
12
|
+
12.8.8 is a security patch for 12.8.7 (`latest`): dependencies only, the framework's code is unchanged.
|
|
13
|
+
The same update ships in 12.6.9 (`latest-12.6`) and 11.46.2 (`latest-11`).
|
|
14
|
+
|
|
15
|
+
## Quick Migration
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @lenne.tech/nest-server@12.8.8
|
|
19
|
+
pnpm run update # nest-server-starter based projects
|
|
20
|
+
pnpm run build
|
|
21
|
+
pnpm test
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual, and raise undici and nodemailer in the project's
|
|
25
|
+
own `package.json` (§1).
|
|
26
|
+
|
|
27
|
+
## 1. undici 7.30.2 and nodemailer 10.4.1 (security)
|
|
28
|
+
|
|
29
|
+
| Package | From | To | Advisories |
|
|
30
|
+
|---|---|---|---|
|
|
31
|
+
| `undici` | 7.30.1 | 7.30.2 | GHSA-mqg3-3v4r-cm32 (high: cache poisoning and cross-user disclosure via interceptors), GHSA-7xg3-fphp-28hg, GHSA-3g9m-rv3r-w5w2, GHSA-5rm8-gv32-r954, GHSA-9v6h-8rgp-m69f, GHSA-r4pc-4jcp-7pqc (medium) |
|
|
32
|
+
| `nodemailer` | 10.4.0 | 10.4.1 | GHSA-3q53-mvfw-q9v8, GHSA-2g53-87p2-4qq8 (medium: address parsing and the IDN allow-list) |
|
|
33
|
+
|
|
34
|
+
The framework uses undici for `outboundFetch()` (the SSRF-guarded fetch for user-entered URLs) and nodemailer for
|
|
35
|
+
`EmailService`. Both versions are patch or minor releases of the line the framework already used; nothing in their API
|
|
36
|
+
that the framework calls changed.
|
|
37
|
+
|
|
38
|
+
**A project that pins either package itself** (a direct `nodemailer` or `undici` dependency, an `overrides:` entry, a
|
|
39
|
+
vendored core whose `package.json` carries the framework's dependencies): raise it to the version above. Two copies of
|
|
40
|
+
undici or nodemailer otherwise stay in the tree, and the project's own one keeps the advisories.
|
|
41
|
+
|
|
42
|
+
**pnpm's release-age hold-back:** nodemailer 10.4.1 was published on 2026-11-10 08:17 UTC. A project that enforces
|
|
43
|
+
`minimumReleaseAge` (pnpm 11 default: one day) and installs before 2026-11-11 08:17 UTC needs
|
|
44
|
+
`minimumReleaseAgeExclude: [nodemailer@10.4.1]` in `pnpm-workspace.yaml`; after that time the entry is inert and can go.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Migration Guide: 12.8.8 → 12.8.9
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None in the API. **Behaviour change:** a download stream that makes no read progress for 30 s is now closed — code that opens a stream and reads it much later (a ZIP export that appends every file first) must open it right before reading or opt out per call (§5). A download that cannot get a store connection answers `503` instead of waiting without end (§2) |
|
|
8
|
+
| **New Features** | `s3.maxSockets`, `s3.connectionTimeoutMs`, `s3.socketTimeoutMs`, `s3.downloadIdleTimeoutMs`, `file.downloadIdleTimeoutMs`, `file.downloadCaching`; `ETag`, `304` and `Content-Length` on downloads; `CoreS3Service.getSocketPoolStats()`; `guardIdleStream()`, `isResponseGone()`, `downloadAbortSignal()`; `abortSignal` and `downloadIdleTimeoutMs` in `FileServiceOptions`, `abortSignal` and `idleTimeoutMs` in `CoreS3Service.getObject()` |
|
|
9
|
+
| **Bugfixes** | S3 downloads could exhaust the S3 client's socket pool for good (§1) |
|
|
10
|
+
| **Migration Effort** | `pnpm run update`. A project with its OWN download route replaces `stream.pipe(res)` with `pipeFileToResponse(stream, res)` (§3). Check code that reads file streams late (§5). Alert on the new error log line (§4) |
|
|
11
|
+
|
|
12
|
+
## Quick Migration
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @lenne.tech/nest-server@12.8.9
|
|
16
|
+
pnpm run update # nest-server-starter based projects
|
|
17
|
+
grep -rn "\.pipe(res" src/ # own download routes: switch them to pipeFileToResponse() (§3)
|
|
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. What happened
|
|
25
|
+
|
|
26
|
+
A project delivered S3 downloads with a bare `stream.pipe(res)`. When a client aborted, `pipe()` only unpiped: the S3
|
|
27
|
+
body was never read and never destroyed, and it kept one socket of the S3 client's pool. The pool had the SDK's default
|
|
28
|
+
size of 50 and no timeout that frees an unread body. After 50 aborted downloads every S3 request waited without end;
|
|
29
|
+
the health check stayed green because it does not touch S3, and the process ran out of heap hours later. Under load it
|
|
30
|
+
became an avalanche: slow downloads made waiting clients give up, and every one of them leaked the next socket.
|
|
31
|
+
|
|
32
|
+
12.8.9 makes that impossible in several layers, each effective on its own:
|
|
33
|
+
|
|
34
|
+
| Layer | What it does |
|
|
35
|
+
|---|---|
|
|
36
|
+
| Idle guard on every download stream | `CoreS3Service.getObject()`, `CoreFileService.getFileStream()` and `getFileStreamByName()` return a stream that destroys its source after `downloadIdleTimeoutMs` (30 s) without read progress. A slow reader that keeps reading is never cut off |
|
|
37
|
+
| `pipeFileToResponse()` | destroys the source when the client goes — on the response's `close`, the request's `close` before its end or the socket's `end`/`close` (under an abort storm about one in six aborted downloads reported it only through the request or the socket) — and at once when the client had already gone (a client that gave up while waiting for a socket) |
|
|
38
|
+
| Download routes | pass an `abortSignal` to the store (an S3 request still waiting for a socket is dropped when the client leaves), skip the store when the client has gone, destroy a stream that arrives late |
|
|
39
|
+
| S3 client | own agents with `maxSockets` 200, `connectionTimeoutMs` 15 000, `socketTimeoutMs` 120 000 |
|
|
40
|
+
| Visibility | an exhausted pool is logged as an ERROR once per minute with numbers |
|
|
41
|
+
|
|
42
|
+
## 2. Configuration
|
|
43
|
+
|
|
44
|
+
All optional; the defaults are what 12.8.9 runs with.
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
s3: {
|
|
48
|
+
bucket: 'files',
|
|
49
|
+
maxSockets: 200, // SDK default was 50
|
|
50
|
+
connectionTimeoutMs: 15_000, // per attempt: wait for a free socket + connect; 3 attempts → 503 after ~46 s
|
|
51
|
+
socketTimeoutMs: 120_000, // inactivity backstop for every S3 request
|
|
52
|
+
downloadIdleTimeoutMs: 30_000, // S3 download stream without read progress is closed
|
|
53
|
+
},
|
|
54
|
+
file: {
|
|
55
|
+
downloadIdleTimeoutMs: 30_000, // the same for GridFS and filesystem streams
|
|
56
|
+
downloadCaching: 'none', // or 'revalidate' (see below)
|
|
57
|
+
},
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Environment: `NSC__S3__MAX_SOCKETS`, `NSC__S3__CONNECTION_TIMEOUT_MS`, `NSC__S3__SOCKET_TIMEOUT_MS`,
|
|
61
|
+
`NSC__S3__DOWNLOAD_IDLE_TIMEOUT_MS`, `NSC__FILE__DOWNLOAD_IDLE_TIMEOUT_MS`, `NSC__FILE__DOWNLOAD_CACHING`.
|
|
62
|
+
|
|
63
|
+
A non-positive or non-numeric value uses the default; the guards cannot be switched off globally (§5 has the per-call
|
|
64
|
+
exception). `connectionTimeoutMs` bounds the wait for a free socket too (the SDK starts it when the request is created),
|
|
65
|
+
and the SDK retries a timed-out attempt — 3 attempts in all, so the `503` comes after up to about 3 × `connectionTimeoutMs`
|
|
66
|
+
plus a short backoff (about 46 s with the default). Keep 3 × the value below your reverse proxy's read timeout (60 s for
|
|
67
|
+
nginx and AWS ALB by default; otherwise the proxy answers `504` first) and well above normal queue waits — a short value
|
|
68
|
+
fails healthy downloads under load.
|
|
69
|
+
|
|
70
|
+
**`file.downloadCaching: 'revalidate'`** sends `Cache-Control: private, no-cache`: the browser keeps the file but asks
|
|
71
|
+
again on every use; that request runs the full role and rights check and is answered `304 Not Modified` without reading
|
|
72
|
+
the store. For image-heavy apps it removes most store reads. The default `'none'` keeps `private, no-store` (nothing is
|
|
73
|
+
kept on the device). Both modes send a weak `ETag` (`W/"<file id>"` — weak because a `compression` middleware may
|
|
74
|
+
deliver the same file gzip-encoded or not) and `Content-Length`.
|
|
75
|
+
|
|
76
|
+
**Compression:** a `compression({ filter: () => true })` gzips already compressed downloads (JPEG, PNG, PDF, ZIP) and only
|
|
77
|
+
burns CPU under many parallel downloads. nest-server's reference `main.ts` now uses `filter: compression.filter` (the
|
|
78
|
+
package default); nest-server-starter follows.
|
|
79
|
+
|
|
80
|
+
## 3. Own download routes
|
|
81
|
+
|
|
82
|
+
Replace every `stream.pipe(res)` that delivers a file with `pipeFileToResponse(stream, res)`, and pass an abort signal:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { downloadAbortSignal, pipeFileToResponse } from '@lenne.tech/nest-server';
|
|
86
|
+
|
|
87
|
+
@Get('image/:id')
|
|
88
|
+
async image(@Param('id') id: string, @Res() res: Response, @CurrentUser() currentUser) {
|
|
89
|
+
// Fires when the client goes (response or request close), so a store request waiting for a socket is dropped
|
|
90
|
+
const abortSignal = downloadAbortSignal(res);
|
|
91
|
+
const stream = await this.fileService.getFileStream(id, { abortSignal, currentUser });
|
|
92
|
+
if (!stream) {
|
|
93
|
+
throw new NotFoundException();
|
|
94
|
+
}
|
|
95
|
+
res.header('Content-Type', 'image/png');
|
|
96
|
+
return pipeFileToResponse(stream, res);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The idle guard would release a bare-piped stream after 30 s as well, but under load 30 s per aborted download is enough
|
|
101
|
+
to fill a pool.
|
|
102
|
+
|
|
103
|
+
## 4. Monitoring
|
|
104
|
+
|
|
105
|
+
Alert on `S3 socket pool exhausted:` in the log (level ERROR, at most once per minute per process). It names the
|
|
106
|
+
sockets in use, the waiting requests and the download streams closed for lack of progress. Genuine load → raise
|
|
107
|
+
`s3.maxSockets`; a growing count without load → a download path that does not use `pipeFileToResponse()`.
|
|
108
|
+
`CoreS3Service.getSocketPoolStats()` returns the same numbers for a metrics endpoint.
|
|
109
|
+
|
|
110
|
+
Downloads the idle guard closed are reported as ONE warning per minute (`N file download(s) closed after no read
|
|
111
|
+
progress …`), split into clients that stopped reading without closing and stores that delivered nothing. A client that
|
|
112
|
+
stops reading is not a server error, so it never produces an ERROR line per download. A store that delivered nothing
|
|
113
|
+
before the first byte answers `503` with `Retry-After`. The health check deliberately does
|
|
114
|
+
not fail on it: a short S3 disturbance would take the whole API, sign-in included, out of the load balancer.
|
|
115
|
+
|
|
116
|
+
## 5. Streams that are read late
|
|
117
|
+
|
|
118
|
+
Every stream from `getFileStream()`, `getFileStreamByName()` and `CoreS3Service.getObject()` must be read within
|
|
119
|
+
`downloadIdleTimeoutMs` (30 s) of being obtained and keep making progress. Code that collects streams first and reads
|
|
120
|
+
them later breaks: a ZIP export with `archiver.append(await getFileStream(id), …)` in a loop pipes each source into a
|
|
121
|
+
buffer at once but reads it only when that entry's turn comes. A stream the guard closes ends WITHOUT `end`: a `.pipe()`
|
|
122
|
+
consumer waits forever, `pipeline()` and `for await` reject (`ERR_STREAM_PREMATURE_CLOSE` / `StreamIdleTimeoutError`).
|
|
123
|
+
|
|
124
|
+
| Fix | How |
|
|
125
|
+
|---|---|
|
|
126
|
+
| Open each stream right before it is read (preferred) | archiver: append the next file only after the previous entry finished (`entry` event) |
|
|
127
|
+
| Raise the idle time | `file.downloadIdleTimeoutMs` / `s3.downloadIdleTimeoutMs` (capped at 2^31 − 1 ms, about 24.8 days) |
|
|
128
|
+
| Opt out for one call | `getFileStream(id, { downloadIdleTimeoutMs: false, … })` / `getObject(key, { idleTimeoutMs: false })` — only when the code is certain to read the stream to the end or destroy it; until then it holds its socket, file handle or cursor |
|
|
129
|
+
|
|
130
|
+
For S3 both have a second limit: a socket without traffic for `s3.socketTimeoutMs` (120 s) is closed by the SDK, so an
|
|
131
|
+
S3 stream that is not read for that long ends with an error even with the opt-out — raise `s3.socketTimeoutMs` as well
|
|
132
|
+
for exports that let an entry wait longer. The opt-out keeps the guard's error handling, so that error never ends the
|
|
133
|
+
process.
|
|
134
|
+
|
|
135
|
+
## Troubleshooting
|
|
136
|
+
|
|
137
|
+
| Symptom | Cause | Fix |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| Downloads answer `503` with `Retry-After` | no store connection within `s3.connectionTimeoutMs` — the pool is exhausted | genuine load: raise `s3.maxSockets`; otherwise look for downloads that do not go through `pipeFileToResponse()` |
|
|
140
|
+
| A very slow client gets a truncated file | it read nothing for `downloadIdleTimeoutMs` | raise `s3.downloadIdleTimeoutMs` / `file.downloadIdleTimeoutMs` |
|
|
141
|
+
| A ZIP export or batch job hangs or produces truncated entries | it opens file streams first and reads them later (§5) | open each stream right before reading it, or opt out per call (§5) |
|
|
142
|
+
| The proxy answers `504` while the pool is exhausted, the API never shows its `503` | 3 × `connectionTimeoutMs` (SDK retries) exceeds the proxy's read timeout | lower `s3.connectionTimeoutMs` (§2) |
|
|
143
|
+
| `S3 socket pool exhausted` in the log | see §4 | see §4 |
|
|
144
|
+
|
|
145
|
+
## Module Documentation
|
|
146
|
+
|
|
147
|
+
- `src/core/modules/file/README.md` — "Delivering a file from your own route (12.8.9)"
|
|
148
|
+
- `src/core/modules/file/INTEGRATION-CHECKLIST.md` — Common Mistakes
|
|
149
|
+
- `src/core/common/helpers/idle-stream.helper.ts`, `src/core/common/services/core-s3.service.ts`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "12.8.
|
|
3
|
+
"version": "12.8.9",
|
|
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": [
|
|
@@ -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.4.
|
|
142
|
+
"nodemailer": "10.4.1",
|
|
143
143
|
"passport": "0.7.0",
|
|
144
144
|
"passport-jwt": "4.0.1",
|
|
145
145
|
"reflect-metadata": "0.2.2",
|
|
@@ -147,7 +147,7 @@
|
|
|
147
147
|
"rxjs": "7.8.2",
|
|
148
148
|
"supertest": "7.3.2",
|
|
149
149
|
"ts-morph": "28.0.0",
|
|
150
|
-
"undici": "7.30.
|
|
150
|
+
"undici": "7.30.2",
|
|
151
151
|
"ws": "8.22.1",
|
|
152
152
|
"yuml-diagram": "1.2.0"
|
|
153
153
|
},
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { PassThrough } from 'stream';
|
|
2
|
+
|
|
3
|
+
import type { Readable } from 'stream';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Marks a stream returned by {@link guardIdleStream}, so a second guard on the same stream is a no-op.
|
|
7
|
+
* A module-private symbol: a property name could arrive on an object from elsewhere, a symbol cannot.
|
|
8
|
+
*/
|
|
9
|
+
const IDLE_GUARDED = Symbol('lt.idleGuardedStream');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Default idle time after which a download stream that makes no read progress is destroyed: 30 s.
|
|
13
|
+
*
|
|
14
|
+
* Long enough for a slow client: a reader that receives any bytes at all resets the timer, so only a
|
|
15
|
+
* transfer that has made NO progress for this long ends. Short enough that a stream nobody reads
|
|
16
|
+
* releases what it holds (an S3 socket, a file handle, a GridFS cursor) within half a minute instead
|
|
17
|
+
* of never.
|
|
18
|
+
*/
|
|
19
|
+
export const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 30_000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Longest delay a Node timer accepts (2^31 - 1 ms, about 24.8 days). A larger delay is clamped by Node to 1 ms — an
|
|
23
|
+
* idle time configured as "practically forever" would otherwise close every download within a millisecond.
|
|
24
|
+
*/
|
|
25
|
+
export const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Error a guarded stream is destroyed with when it made no read progress for the configured time.
|
|
29
|
+
* Only delivered when someone listens for `error` on the guarded stream (see {@link guardIdleStream}).
|
|
30
|
+
*/
|
|
31
|
+
export class StreamIdleTimeoutError extends Error {
|
|
32
|
+
readonly code = 'LT_STREAM_IDLE_TIMEOUT';
|
|
33
|
+
|
|
34
|
+
constructor(readonly idleMs: number) {
|
|
35
|
+
super(`Download stream made no read progress for ${idleMs} ms and was closed to release its resources`);
|
|
36
|
+
this.name = 'StreamIdleTimeoutError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Options of {@link guardIdleStream}.
|
|
42
|
+
*/
|
|
43
|
+
export interface GuardIdleStreamOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Idle time in milliseconds; a non-positive or non-finite value falls back to {@link DEFAULT_STREAM_IDLE_TIMEOUT_MS},
|
|
46
|
+
* a value above {@link MAX_TIMER_DELAY_MS} is capped there. `false` keeps every other part of the guard (error
|
|
47
|
+
* handling, release on close) but starts no idle timer — for a caller that reads late on purpose.
|
|
48
|
+
*/
|
|
49
|
+
idleMs?: false | number;
|
|
50
|
+
/** Called once when the guard closes the stream for lack of progress (logging, metrics). */
|
|
51
|
+
onIdle?: (idleMs: number) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The stream a guard returns: a `PassThrough` that reports every read of its consumer.
|
|
56
|
+
*
|
|
57
|
+
* Progress has to be measured on the CONSUMER side. The source only delivers when the buffers between it and the
|
|
58
|
+
* reader run low, and those buffers hold several chunks: a slow reader working through them makes progress for
|
|
59
|
+
* a while without the source delivering anything (measured: four 16 KB chunks over 125 ms). Judged by the source
|
|
60
|
+
* alone, such a reader was cut off. `read()` is what every consumer calls — `pipe()` and `'data'` listeners through
|
|
61
|
+
* the stream's flow, async iteration directly — and nothing calls it while nobody reads, which is exactly the
|
|
62
|
+
* stream that must be released.
|
|
63
|
+
*/
|
|
64
|
+
class IdleGuardStream extends PassThrough {
|
|
65
|
+
onConsumerRead?: () => void;
|
|
66
|
+
|
|
67
|
+
override read(size?: number): any {
|
|
68
|
+
this.onConsumerRead?.();
|
|
69
|
+
return super.read(size);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether the stream is already guarded by {@link guardIdleStream}.
|
|
75
|
+
*/
|
|
76
|
+
export function isIdleGuardedStream(stream: unknown): boolean {
|
|
77
|
+
return !!stream && (stream as Record<symbol, unknown>)[IDLE_GUARDED] === true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Wrap a download stream so it releases its source when nobody reads it any more.
|
|
82
|
+
*
|
|
83
|
+
* WHY THIS EXISTS: a download source holds something finite while it is open — an S3 body holds a socket
|
|
84
|
+
* of the SDK's pool, a file stream a file handle, a GridFS stream a server-side cursor. A caller that
|
|
85
|
+
* pipes it with a bare `stream.pipe(res)` stops reading when the client aborts: `pipe()` only unpipes,
|
|
86
|
+
* nothing destroys the source, and it stays paused for the life of the process. For S3 that is a socket
|
|
87
|
+
* taken from a pool of a fixed size; enough aborted downloads and every further S3 request waits forever
|
|
88
|
+
* (measured in production: 50 aborts filled the SDK's default pool, followed by a heap OOM hours later).
|
|
89
|
+
* The same happens to a stream that is obtained and then simply dropped.
|
|
90
|
+
*
|
|
91
|
+
* The guard returns a `PassThrough` the source is piped into. Every read of the consumer, and every chunk the
|
|
92
|
+
* source delivers, resets a timer; nothing resets it while nobody reads (back-pressure pauses the source). When
|
|
93
|
+
* the timer runs out — no read progress for `idleMs` — the source AND the returned stream are destroyed,
|
|
94
|
+
* which releases the socket, handle or cursor. A slow reader that keeps reading is never cut off; only a
|
|
95
|
+
* transfer without ANY progress for the whole period ends.
|
|
96
|
+
*
|
|
97
|
+
* Also: destroying the returned stream (as `pipeFileToResponse()` does when the response closes) destroys
|
|
98
|
+
* the source at once, and an error of the source is forwarded — never left on a stream without a listener,
|
|
99
|
+
* which Node turns into an uncaught exception that ends the process. The idle error itself is only emitted
|
|
100
|
+
* when someone listens for `error` on the returned stream; a stream nobody holds any more is closed
|
|
101
|
+
* silently for the same reason.
|
|
102
|
+
*
|
|
103
|
+
* Guarding an already guarded stream returns it unchanged.
|
|
104
|
+
*/
|
|
105
|
+
export function guardIdleStream(source: Readable, options: GuardIdleStreamOptions = {}): Readable {
|
|
106
|
+
if (isIdleGuardedStream(source)) {
|
|
107
|
+
return source;
|
|
108
|
+
}
|
|
109
|
+
const idleMs =
|
|
110
|
+
options.idleMs === false
|
|
111
|
+
? undefined
|
|
112
|
+
: typeof options.idleMs === 'number' && Number.isFinite(options.idleMs) && options.idleMs > 0
|
|
113
|
+
? Math.min(options.idleMs, MAX_TIMER_DELAY_MS)
|
|
114
|
+
: DEFAULT_STREAM_IDLE_TIMEOUT_MS;
|
|
115
|
+
|
|
116
|
+
const guarded = new IdleGuardStream();
|
|
117
|
+
Object.defineProperty(guarded, IDLE_GUARDED, { enumerable: false, value: true });
|
|
118
|
+
|
|
119
|
+
let timer: NodeJS.Timeout | undefined;
|
|
120
|
+
const stopTimer = () => {
|
|
121
|
+
if (timer) {
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
timer = undefined;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// `source.pipe(guarded)` registers an `error` listener of its own on `guarded`, which rethrows when it is the
|
|
128
|
+
// only one. Only listeners beyond it belong to a consumer — counting it made an unread stream's idle error
|
|
129
|
+
// an uncaught exception, the very crash this function exists to prevent.
|
|
130
|
+
let pipeErrorListeners = 0;
|
|
131
|
+
const hasConsumerErrorListener = () => guarded.listenerCount('error') > pipeErrorListeners;
|
|
132
|
+
|
|
133
|
+
// An error is only handed on to a consumer's listener; without one, emitting it would end the process
|
|
134
|
+
const destroyGuarded = (error?: Error) => {
|
|
135
|
+
if (guarded.destroyed) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
guarded.destroy(error && hasConsumerErrorListener() ? error : undefined);
|
|
139
|
+
};
|
|
140
|
+
const destroySource = () => {
|
|
141
|
+
if (!source.destroyed) {
|
|
142
|
+
source.destroy();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const onIdle = () => {
|
|
147
|
+
timer = undefined;
|
|
148
|
+
if (source.readableEnded || guarded.destroyed) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
options.onIdle?.(idleMs as number);
|
|
153
|
+
} catch {
|
|
154
|
+
// A failing callback must not keep the resources alive
|
|
155
|
+
}
|
|
156
|
+
destroySource();
|
|
157
|
+
destroyGuarded(new StreamIdleTimeoutError(idleMs as number));
|
|
158
|
+
};
|
|
159
|
+
// Without an idle time (`idleMs: false`) the rest of the guard still applies: the source keeps an error listener —
|
|
160
|
+
// an unread S3 body is destroyed WITH an error by the SDK's socket timeout, which ends the process without one
|
|
161
|
+
if (idleMs !== undefined) {
|
|
162
|
+
timer = setTimeout(onIdle, idleMs);
|
|
163
|
+
// The timer must never keep a process alive on its own (a test run, a graceful shutdown)
|
|
164
|
+
timer.unref?.();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Progress: the consumer reads (see `IdleGuardStream`), or the source delivers
|
|
168
|
+
guarded.onConsumerRead = () => timer?.refresh();
|
|
169
|
+
source.on('data', () => timer?.refresh());
|
|
170
|
+
source.once('end', stopTimer);
|
|
171
|
+
source.once('close', stopTimer);
|
|
172
|
+
source.on('error', (error: Error) => {
|
|
173
|
+
stopTimer();
|
|
174
|
+
destroyGuarded(error);
|
|
175
|
+
});
|
|
176
|
+
guarded.once('close', () => {
|
|
177
|
+
stopTimer();
|
|
178
|
+
// The consumer is gone (response closed, stream destroyed): release the source now, not at the timeout
|
|
179
|
+
if (!source.readableEnded) {
|
|
180
|
+
destroySource();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const before = guarded.listenerCount('error');
|
|
185
|
+
source.pipe(guarded);
|
|
186
|
+
pipeErrorListeners = guarded.listenerCount('error') - before;
|
|
187
|
+
// `pipe()` removes its listener when the source ends; from then on every listener is a consumer's
|
|
188
|
+
source.once('end', () => {
|
|
189
|
+
pipeErrorListeners = 0;
|
|
190
|
+
});
|
|
191
|
+
return guarded;
|
|
192
|
+
}
|
|
@@ -1283,6 +1283,33 @@ export interface IFileConfig {
|
|
|
1283
1283
|
*/
|
|
1284
1284
|
deleteRoles?: string[];
|
|
1285
1285
|
|
|
1286
|
+
/**
|
|
1287
|
+
* Browser caching of file downloads (`GET /files/id/:id`, `GET /files/:filename`).
|
|
1288
|
+
*
|
|
1289
|
+
* - `'none'`: `Cache-Control: private, no-store` — nothing is kept, every view reads the store again.
|
|
1290
|
+
* - `'revalidate'`: `Cache-Control: private, no-cache` plus `ETag` — the browser keeps the file but asks
|
|
1291
|
+
* again on every use; the request runs the full role and rights check and is answered `304 Not Modified`
|
|
1292
|
+
* without reading the store. Recommended for image-heavy apps: it removes most store reads (S3 sockets)
|
|
1293
|
+
* while every view stays authorized. Shared caches stay excluded (`private`). Note that the browser then
|
|
1294
|
+
* stores authorized file content on the device.
|
|
1295
|
+
*
|
|
1296
|
+
* Both modes send a weak `ETag` (`W/"<file id>"`) and answer a matching `If-None-Match` with 304.
|
|
1297
|
+
* @default 'none'
|
|
1298
|
+
* @since 12.8.9
|
|
1299
|
+
*/
|
|
1300
|
+
downloadCaching?: 'none' | 'revalidate';
|
|
1301
|
+
|
|
1302
|
+
/**
|
|
1303
|
+
* Time in milliseconds a download stream from `CoreFileService.getFileStream()` / `getFileStreamByName()` may
|
|
1304
|
+
* go without ANY read progress before it is destroyed, releasing the file handle (filesystem) or the cursor
|
|
1305
|
+
* (GridFS) it holds. Protects against a stream piped with a bare `stream.pipe(res)` to a client that aborts,
|
|
1306
|
+
* and a stream obtained and never read. A slow reader that keeps reading is never cut off. S3 streams use
|
|
1307
|
+
* `s3.downloadIdleTimeoutMs`. A non-positive or non-numeric value uses the default.
|
|
1308
|
+
* @default 30000
|
|
1309
|
+
* @since 12.8.9
|
|
1310
|
+
*/
|
|
1311
|
+
downloadIdleTimeoutMs?: number;
|
|
1312
|
+
|
|
1286
1313
|
/**
|
|
1287
1314
|
* Roles allowed to DOWNLOAD files and read file info
|
|
1288
1315
|
* (`GET /files/id/:id`, `GET /files/:filename`, `getFileInfo` query).
|
|
@@ -3968,6 +3995,34 @@ export interface IS3Config {
|
|
|
3968
3995
|
*/
|
|
3969
3996
|
bucket: string;
|
|
3970
3997
|
|
|
3998
|
+
/**
|
|
3999
|
+
* Upper bound in milliseconds for ONE attempt of a request to get a socket: the time it WAITS for a free socket of
|
|
4000
|
+
* the pool (see `maxSockets`) plus the time to connect. The SDK starts this timer when the request is created, not
|
|
4001
|
+
* when it gets a socket, so it bounds the queue wait as well — which keeps the queue from growing without end under
|
|
4002
|
+
* overload (each waiting request holds memory). The SDK retries a timed-out attempt (3 attempts in all), so a
|
|
4003
|
+
* download that cannot get a socket is answered `503` with `Retry-After` after up to about 3 × this value plus a
|
|
4004
|
+
* short backoff — with the default about 46 s, below the common 60 s read timeout of reverse proxies. Keep
|
|
4005
|
+
* 3 × this value below your proxy's read timeout, and well above normal queue waits: a short value fails healthy
|
|
4006
|
+
* downloads under load. A non-positive or non-numeric value uses the default.
|
|
4007
|
+
* @default 15000
|
|
4008
|
+
* @since 12.8.9
|
|
4009
|
+
*/
|
|
4010
|
+
connectionTimeoutMs?: number;
|
|
4011
|
+
|
|
4012
|
+
/**
|
|
4013
|
+
* Time in milliseconds a download stream from S3 (`CoreS3Service.getObject()`, and with it
|
|
4014
|
+
* `CoreFileService.getFileStream()` for S3-stored files) may go without ANY read progress before it is
|
|
4015
|
+
* destroyed and its socket returns to the pool.
|
|
4016
|
+
*
|
|
4017
|
+
* A stream that is obtained and never read, or piped with a bare `stream.pipe(res)` to a client that
|
|
4018
|
+
* aborts, otherwise holds its socket for the life of the process — and the pool is finite (see
|
|
4019
|
+
* `maxSockets`). A slow reader that keeps reading is never cut off: every chunk resets the timer.
|
|
4020
|
+
* A non-positive or non-numeric value uses the default; the guard cannot be switched off.
|
|
4021
|
+
* @default 30000
|
|
4022
|
+
* @since 12.8.9
|
|
4023
|
+
*/
|
|
4024
|
+
downloadIdleTimeoutMs?: number;
|
|
4025
|
+
|
|
3971
4026
|
/**
|
|
3972
4027
|
* Whether S3 is enabled.
|
|
3973
4028
|
* Presence of the config object implies true.
|
|
@@ -3988,6 +4043,19 @@ export interface IS3Config {
|
|
|
3988
4043
|
*/
|
|
3989
4044
|
forcePathStyle?: boolean;
|
|
3990
4045
|
|
|
4046
|
+
/**
|
|
4047
|
+
* Maximum number of concurrent sockets to the S3 endpoint (per protocol). Requests beyond it wait for a
|
|
4048
|
+
* free socket — and are reported as an error in the log while they do (pool saturation).
|
|
4049
|
+
*
|
|
4050
|
+
* The AWS SDK's own default is 50, which a single process reaches with 50 simultaneous downloads; every
|
|
4051
|
+
* stream holds its socket until it is read to the end or destroyed. 200 lets that many transfers run at
|
|
4052
|
+
* once — each costs one file descriptor and a few KB — while staying far below what S3 and S3-compatible
|
|
4053
|
+
* stores accept per client. A non-positive or non-numeric value uses the default.
|
|
4054
|
+
* @default 200
|
|
4055
|
+
* @since 12.8.9
|
|
4056
|
+
*/
|
|
4057
|
+
maxSockets?: number;
|
|
4058
|
+
|
|
3991
4059
|
/**
|
|
3992
4060
|
* Serve downloads as presigned URL redirects instead of streaming
|
|
3993
4061
|
* through the API (offloads traffic from the server).
|
|
@@ -4039,6 +4107,16 @@ export interface IS3Config {
|
|
|
4039
4107
|
*/
|
|
4040
4108
|
secretAccessKey?: string;
|
|
4041
4109
|
|
|
4110
|
+
/**
|
|
4111
|
+
* Time in milliseconds a socket to S3 may stay without any traffic before the SDK destroys it — the
|
|
4112
|
+
* backstop under `downloadIdleTimeoutMs` for every request, uploads and copies included. Kept well above
|
|
4113
|
+
* the download idle time, because an S3-compatible store may take a while without sending a byte to
|
|
4114
|
+
* finish a large copy or multipart upload. A non-positive or non-numeric value uses the default.
|
|
4115
|
+
* @default 120000
|
|
4116
|
+
* @since 12.8.9
|
|
4117
|
+
*/
|
|
4118
|
+
socketTimeoutMs?: number;
|
|
4119
|
+
|
|
4042
4120
|
/**
|
|
4043
4121
|
* Bucket used as staging area for resumable TUS uploads.
|
|
4044
4122
|
* Configure a lifecycle rule on this bucket to expire aborted uploads.
|