@lenne.tech/nest-server 12.8.8 → 12.9.0

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.
Files changed (47) hide show
  1. package/.claude/rules/architecture.md +1 -1
  2. package/.claude/rules/configurable-features.md +1 -0
  3. package/.claude/rules/testing.md +4 -4
  4. package/CLAUDE.md +2 -2
  5. package/FRAMEWORK-API.md +7 -1
  6. package/bin/check-unified-field-types.js +15 -3
  7. package/dist/core/common/decorators/unified-field.decorator.js +10 -11
  8. package/dist/core/common/decorators/unified-field.decorator.js.map +1 -1
  9. package/dist/core/common/helpers/idle-stream.helper.d.ts +14 -0
  10. package/dist/core/common/helpers/idle-stream.helper.js +94 -0
  11. package/dist/core/common/helpers/idle-stream.helper.js.map +1 -0
  12. package/dist/core/common/interfaces/server-options.interface.d.ts +6 -0
  13. package/dist/core/common/services/core-s3.service.d.ts +27 -1
  14. package/dist/core/common/services/core-s3.service.js +89 -3
  15. package/dist/core/common/services/core-s3.service.js.map +1 -1
  16. package/dist/core/modules/file/core-file.controller.d.ts +16 -0
  17. package/dist/core/modules/file/core-file.controller.js +215 -8
  18. package/dist/core/modules/file/core-file.controller.js.map +1 -1
  19. package/dist/core/modules/file/core-file.service.d.ts +5 -0
  20. package/dist/core/modules/file/core-file.service.js +17 -7
  21. package/dist/core/modules/file/core-file.service.js.map +1 -1
  22. package/dist/core/modules/file/interfaces/file-service-options.interface.d.ts +2 -0
  23. package/dist/core/modules/file/s3-file.helper.d.ts +4 -1
  24. package/dist/core/modules/file/s3-file.helper.js +2 -2
  25. package/dist/core/modules/file/s3-file.helper.js.map +1 -1
  26. package/dist/index.d.ts +1 -0
  27. package/dist/index.js +1 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/main.js +1 -3
  30. package/dist/main.js.map +1 -1
  31. package/docs/REQUEST-LIFECYCLE.md +6 -6
  32. package/migration-guides/12.8.6-to-12.8.7.md +3 -3
  33. package/migration-guides/12.8.8-to-12.8.9.md +149 -0
  34. package/migration-guides/12.8.x-to-12.9.x.md +105 -0
  35. package/package.json +4 -4
  36. package/src/core/common/decorators/unified-field.decorator.ts +20 -22
  37. package/src/core/common/helpers/idle-stream.helper.ts +192 -0
  38. package/src/core/common/interfaces/server-options.interface.ts +78 -0
  39. package/src/core/common/services/core-s3.service.ts +186 -3
  40. package/src/core/modules/file/INTEGRATION-CHECKLIST.md +15 -14
  41. package/src/core/modules/file/README.md +48 -0
  42. package/src/core/modules/file/core-file.controller.ts +356 -10
  43. package/src/core/modules/file/core-file.service.ts +46 -7
  44. package/src/core/modules/file/interfaces/file-service-options.interface.ts +22 -0
  45. package/src/core/modules/file/s3-file.helper.ts +6 -2
  46. package/src/index.ts +1 -0
  47. package/src/main.ts +3 -3
@@ -8,6 +8,7 @@ import { accessDeniedException } from '../../common/exceptions/access-denied.exc
8
8
  import { getObjectIds, getStringIds } from '../../common/helpers/db.helper.js';
9
9
  import { convertFilterArgsToQuery } from '../../common/helpers/filter.helper.js';
10
10
  import { GridFSHelper } from '../../common/helpers/gridfs.helper.js';
11
+ import { guardIdleStream } from '../../common/helpers/idle-stream.helper.js';
11
12
  import { check } from '../../common/helpers/input.helper.js';
12
13
  import { prepareOutput } from '../../common/helpers/service.helper.js';
13
14
  import { isWellFormedId } from '../../common/persistence/id-format.js';
@@ -502,12 +503,44 @@ export abstract class CoreFileService {
502
503
  // Omitting it keeps the previous behaviour exactly.
503
504
  const store = knownStore ?? (await this.locateFile(id));
504
505
  if (store === 's3') {
505
- return S3FileHelper.getStream(this.options.s3Service, id);
506
+ return this.guardDownloadStream(
507
+ await S3FileHelper.getStream(this.options.s3Service, id, this.s3StreamOptions(serviceOptions)),
508
+ serviceOptions,
509
+ );
506
510
  }
507
511
  if (store === 'filesystem') {
508
- return FilesystemFileHelper.getStream(this.filesystemDir, id);
512
+ return this.guardDownloadStream(FilesystemFileHelper.getStream(this.filesystemDir, id), serviceOptions);
509
513
  }
510
- return GridFSHelper.openDownloadStream(this.files, getObjectIds(id)) as mongo.GridFSBucketReadStream;
514
+ return this.guardDownloadStream(
515
+ GridFSHelper.openDownloadStream(this.files, getObjectIds(id)) as mongo.GridFSBucketReadStream,
516
+ serviceOptions,
517
+ );
518
+ }
519
+
520
+ /**
521
+ * Guard a download stream against a caller that stops reading (see `guardIdleStream()`).
522
+ *
523
+ * Every store holds something finite while its stream is open — S3 a socket of the client's pool, the
524
+ * filesystem a file handle, GridFS a server-side cursor. A stream piped with a bare `stream.pipe(res)` to a
525
+ * client that aborts, or one that is obtained and dropped, would hold it for the life of the process. The
526
+ * guard releases it after `file.downloadIdleTimeoutMs` without read progress (S3 streams are already guarded
527
+ * by `CoreS3Service` with `s3.downloadIdleTimeoutMs`; guarding twice is a no-op). Errors of the store — a
528
+ * GridFS file without chunks, a missing file on disk — reach the returned stream as before.
529
+ */
530
+ protected guardDownloadStream(stream: Readable, serviceOptions?: FileServiceOptions): Readable {
531
+ const idleMs = serviceOptions?.downloadIdleTimeoutMs;
532
+ // `false` keeps the guard without its timer — the error handling and the release on close stay
533
+ return guardIdleStream(stream, {
534
+ idleMs: idleMs === false ? false : (idleMs ?? ConfigService.configFastButReadOnly?.file?.downloadIdleTimeoutMs),
535
+ });
536
+ }
537
+
538
+ /** Options for the S3 stream of a download: the caller's abort signal and idle time. */
539
+ protected s3StreamOptions(serviceOptions?: FileServiceOptions): {
540
+ abortSignal?: AbortSignal;
541
+ idleTimeoutMs?: false | number;
542
+ } {
543
+ return { abortSignal: serviceOptions?.abortSignal, idleTimeoutMs: serviceOptions?.downloadIdleTimeoutMs };
511
544
  }
512
545
 
513
546
  /**
@@ -620,19 +653,25 @@ export abstract class CoreFileService {
620
653
  }
621
654
  const s3FileInfo = await this.findS3FileByName(filename);
622
655
  if (s3FileInfo) {
623
- return S3FileHelper.getStream(this.options.s3Service, s3FileInfo._id);
656
+ return this.guardDownloadStream(
657
+ await S3FileHelper.getStream(this.options.s3Service, s3FileInfo._id, this.s3StreamOptions(serviceOptions)),
658
+ serviceOptions,
659
+ );
624
660
  }
625
661
  const fsFileInfo = await this.findFilesystemFileByName(filename);
626
662
  if (fsFileInfo) {
627
- return FilesystemFileHelper.getStream(this.filesystemDir, fsFileInfo._id);
663
+ return this.guardDownloadStream(
664
+ FilesystemFileHelper.getStream(this.filesystemDir, fsFileInfo._id),
665
+ serviceOptions,
666
+ );
628
667
  }
629
668
  const gridFsInfo = await GridFSHelper.findFileByName(this.files, filename);
630
669
  if (!gridFsInfo) {
631
670
  // Unchanged answer for an unknown name: GridFS is the terminal store and reports the miss
632
671
  // itself, asynchronously on the stream, which the controller turns into a 404.
633
- return GridFSHelper.openDownloadStreamByName(this.files, filename);
672
+ return this.guardDownloadStream(GridFSHelper.openDownloadStreamByName(this.files, filename), serviceOptions);
634
673
  }
635
- return GridFSHelper.openDownloadStream(this.files, gridFsInfo._id);
674
+ return this.guardDownloadStream(GridFSHelper.openDownloadStream(this.files, gridFsInfo._id), serviceOptions);
636
675
  }
637
676
 
638
677
  /**
@@ -2,7 +2,29 @@
2
2
  * Interface for service options in file services
3
3
  */
4
4
  export interface FileServiceOptions {
5
+ /**
6
+ * Aborts the store request of a download (`getFileStream()` / `getFileStreamByName()`) when it fires.
7
+ *
8
+ * For S3 this matters under load: a request that waits for a free socket of the client's pool stays queued
9
+ * after its HTTP client has gone, and every one of them later takes a socket and allocates a body. The
10
+ * framework's download routes abort it when the response closes. Since 12.8.9.
11
+ */
12
+ abortSignal?: AbortSignal;
13
+
5
14
  currentUser?: { hasRole: (roles: string[]) => boolean; id: any };
15
+
16
+ /**
17
+ * Idle time of a download stream from `getFileStream()` / `getFileStreamByName()` for THIS call (default
18
+ * `file.downloadIdleTimeoutMs`, for S3 `s3.downloadIdleTimeoutMs`). A stream that makes no read progress for that
19
+ * long is closed to release its socket, file handle or cursor.
20
+ *
21
+ * `false` returns the stream unguarded — only for a caller that reads it late on purpose (an archive that appends
22
+ * every file first and pipes each entry when its turn comes) AND is certain to read it to the end or destroy it:
23
+ * such a stream holds its socket, handle or cursor until then. Prefer opening each stream right before it is read.
24
+ * The guard's error handling stays; for S3 the SDK still closes a socket idle for `s3.socketTimeoutMs`.
25
+ * Since 12.8.9.
26
+ */
27
+ downloadIdleTimeoutMs?: false | number;
6
28
  force?: boolean;
7
29
 
8
30
  /**
@@ -218,9 +218,13 @@ export class S3FileHelper {
218
218
  /**
219
219
  * Get the download stream of a file
220
220
  */
221
- static async getStream(s3Service: CoreS3Service, id: string | Types.ObjectId): Promise<Readable> {
221
+ static async getStream(
222
+ s3Service: CoreS3Service,
223
+ id: string | Types.ObjectId,
224
+ options?: { abortSignal?: AbortSignal; idleTimeoutMs?: false | number },
225
+ ): Promise<Readable> {
222
226
  const objectId = typeof id === 'string' ? newObjectId(id) : id;
223
- return (await s3Service.getObject(objectId.toHexString())).body;
227
+ return (await s3Service.getObject(objectId.toHexString(), options)).body;
224
228
  }
225
229
 
226
230
  /**
package/src/index.ts CHANGED
@@ -45,6 +45,7 @@ export * from './core/common/helpers/filter.helper.js';
45
45
  export * from './core/common/helpers/graphql.helper.js';
46
46
  export * from './core/common/helpers/graphql-ws-connection.helper.js';
47
47
  export * from './core/common/helpers/graphql-ws-context.helper.js';
48
+ export * from './core/common/helpers/idle-stream.helper.js';
48
49
  export * from './core/common/helpers/interceptor.helper.js';
49
50
  export * from './core/common/helpers/gridfs.helper.js';
50
51
  export * from './core/common/helpers/input.helper.js';
package/src/main.ts CHANGED
@@ -49,10 +49,10 @@ async function bootstrap() {
49
49
  if (typeof envConfig.compression === 'object') {
50
50
  envCompressionOptions = envConfig.compression;
51
51
  }
52
+ // `compression.filter` is the package's own default: JSON, GraphQL, text, SSE and SVG are compressed, already
53
+ // compressed formats (JPEG, PNG, PDF, ZIP) are not — gzipping them only burns CPU under many parallel downloads
52
54
  const compressionOptions = {
53
- filter: () => {
54
- return true;
55
- },
55
+ filter: compression.filter,
56
56
  threshold: 0,
57
57
  ...envCompressionOptions,
58
58
  };