@lenne.tech/nest-server 11.46.3 → 11.46.5

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 (59) hide show
  1. package/.claude/rules/configurable-features.md +1 -1
  2. package/.claude/rules/testing.md +4 -4
  3. package/FRAMEWORK-API.md +1 -1
  4. package/dist/core/common/helpers/byte-range.helper.d.ts +8 -0
  5. package/dist/core/common/helpers/byte-range.helper.js +76 -0
  6. package/dist/core/common/helpers/byte-range.helper.js.map +1 -0
  7. package/dist/core/common/helpers/gridfs.helper.d.ts +7 -2
  8. package/dist/core/common/helpers/gridfs.helper.js +7 -4
  9. package/dist/core/common/helpers/gridfs.helper.js.map +1 -1
  10. package/dist/core/common/services/core-s3.service.d.ts +3 -0
  11. package/dist/core/common/services/core-s3.service.js +8 -1
  12. package/dist/core/common/services/core-s3.service.js.map +1 -1
  13. package/dist/core/modules/ai/core-ai.controller.js +8 -4
  14. package/dist/core/modules/ai/core-ai.controller.js.map +1 -1
  15. package/dist/core/modules/error-code/error-codes.d.ts +9 -0
  16. package/dist/core/modules/error-code/error-codes.js +8 -0
  17. package/dist/core/modules/error-code/error-codes.js.map +1 -1
  18. package/dist/core/modules/file/core-file.controller.d.ts +6 -0
  19. package/dist/core/modules/file/core-file.controller.js +94 -26
  20. package/dist/core/modules/file/core-file.controller.js.map +1 -1
  21. package/dist/core/modules/file/core-file.service.d.ts +2 -0
  22. package/dist/core/modules/file/core-file.service.js +17 -6
  23. package/dist/core/modules/file/core-file.service.js.map +1 -1
  24. package/dist/core/modules/file/filesystem-file.helper.d.ts +2 -1
  25. package/dist/core/modules/file/filesystem-file.helper.js +2 -2
  26. package/dist/core/modules/file/filesystem-file.helper.js.map +1 -1
  27. package/dist/core/modules/file/interfaces/file-service-options.interface.d.ts +2 -0
  28. package/dist/core/modules/file/s3-file.helper.d.ts +2 -0
  29. package/dist/core/modules/file/s3-file.helper.js.map +1 -1
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +1 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/server/modules/error-code/error-codes.d.ts +1 -0
  34. package/dist/test/test.helper.d.ts +1 -0
  35. package/dist/test/test.helper.js +6 -0
  36. package/dist/test/test.helper.js.map +1 -1
  37. package/dist/tsconfig.build.tsbuildinfo +1 -1
  38. package/docs/REQUEST-LIFECYCLE.md +1 -1
  39. package/docs/error-codes.md +14 -0
  40. package/docs/security-overrides.md +21 -6
  41. package/migration-guides/11.46.2-to-11.46.3.md +15 -3
  42. package/migration-guides/11.46.3-to-11.46.4.md +75 -0
  43. package/migration-guides/11.46.4-to-11.46.5.md +183 -0
  44. package/package.json +6 -6
  45. package/src/core/common/helpers/byte-range.helper.ts +144 -0
  46. package/src/core/common/helpers/gridfs.helper.ts +20 -6
  47. package/src/core/common/services/core-s3.service.ts +16 -2
  48. package/src/core/modules/ai/core-ai.controller.ts +12 -4
  49. package/src/core/modules/error-code/error-codes.ts +13 -0
  50. package/src/core/modules/file/INTEGRATION-CHECKLIST.md +17 -15
  51. package/src/core/modules/file/README.md +91 -3
  52. package/src/core/modules/file/core-file.controller.ts +202 -32
  53. package/src/core/modules/file/core-file.service.ts +33 -13
  54. package/src/core/modules/file/filesystem-file.helper.ts +8 -3
  55. package/src/core/modules/file/interfaces/file-service-options.interface.ts +13 -0
  56. package/src/core/modules/file/s3-file.helper.ts +3 -2
  57. package/src/index.ts +1 -0
  58. package/src/test/README.md +8 -4
  59. package/src/test/test.helper.ts +12 -0
@@ -19,6 +19,7 @@ import { decideFileAccess, fileAccessNeedsRawDocument, resolveFileAccessPreset }
19
19
  import { warnOnUndecidedFileAccess } from './file-roles.config';
20
20
  import { FileServiceOptions } from './interfaces/file-service-options.interface';
21
21
  import { FileUploadSource } from './interfaces/file-upload.interface';
22
+ import { assertByteRange } from '../../common/helpers/byte-range.helper';
22
23
  import {
23
24
  assertFileStorageAvailable,
24
25
  FileStorageDriver,
@@ -475,6 +476,10 @@ export abstract class CoreFileService {
475
476
 
476
477
  /**
477
478
  * Get file stream (for big files) via file ID
479
+ *
480
+ * With `serviceOptions.range`, only those bytes (both inclusive) — from every store: S3 with a ranged
481
+ * `GetObject`, GridFS from the chunk that holds the first byte, the filesystem from that offset. See
482
+ * {@link FileServiceOptions.range} for the contract and `resolveDownloadRange()` for producing one from a request.
478
483
  */
479
484
  async getFileStream(
480
485
  id: string | Types.ObjectId,
@@ -484,6 +489,8 @@ export abstract class CoreFileService {
484
489
  if (this.isMalformedFileId(id)) {
485
490
  return null;
486
491
  }
492
+ // Checked before anything is asked: a malformed range is a caller's bug, answered the same for every store
493
+ assertByteRange(serviceOptions?.range);
487
494
  if (!(await this.checkRights(id, { ...serviceOptions, checkInputType: 'id' }))) {
488
495
  return null;
489
496
  }
@@ -493,6 +500,19 @@ export abstract class CoreFileService {
493
500
  // the same one-to-three metadata round trips are not repeated per download.
494
501
  // Omitting it keeps the previous behaviour exactly.
495
502
  const store = knownStore ?? (await this.locateFile(id));
503
+ return this.openStoreStream(store, id, serviceOptions);
504
+ }
505
+
506
+ /**
507
+ * Open the download stream of a file in the store that holds it — the whole file, or `serviceOptions.range` —
508
+ * guarded against a caller that stops reading. The rights check is the caller's: this only reads bytes.
509
+ */
510
+ protected async openStoreStream(
511
+ store: FileStorageDriver,
512
+ id: string | Types.ObjectId,
513
+ serviceOptions?: FileServiceOptions,
514
+ ): Promise<Readable> {
515
+ const range = assertByteRange(serviceOptions?.range);
496
516
  if (store === 's3') {
497
517
  return this.guardDownloadStream(
498
518
  await S3FileHelper.getStream(this.options.s3Service, id, this.s3StreamOptions(serviceOptions)),
@@ -500,10 +520,10 @@ export abstract class CoreFileService {
500
520
  );
501
521
  }
502
522
  if (store === 'filesystem') {
503
- return this.guardDownloadStream(FilesystemFileHelper.getStream(this.filesystemDir, id), serviceOptions);
523
+ return this.guardDownloadStream(FilesystemFileHelper.getStream(this.filesystemDir, id, range), serviceOptions);
504
524
  }
505
525
  return this.guardDownloadStream(
506
- GridFSHelper.openDownloadStream(this.files, getObjectIds(id)) as mongo.GridFSBucketReadStream,
526
+ GridFSHelper.openDownloadStream(this.files, getObjectIds(id), range) as mongo.GridFSBucketReadStream,
507
527
  serviceOptions,
508
528
  );
509
529
  }
@@ -526,12 +546,17 @@ export abstract class CoreFileService {
526
546
  });
527
547
  }
528
548
 
529
- /** Options for the S3 stream of a download: the caller's abort signal and idle time. */
549
+ /** Options for the S3 stream of a download: the caller's abort signal, idle time and byte range. */
530
550
  protected s3StreamOptions(serviceOptions?: FileServiceOptions): {
531
551
  abortSignal?: AbortSignal;
532
552
  idleTimeoutMs?: false | number;
553
+ range?: FileServiceOptions['range'];
533
554
  } {
534
- return { abortSignal: serviceOptions?.abortSignal, idleTimeoutMs: serviceOptions?.downloadIdleTimeoutMs };
555
+ return {
556
+ abortSignal: serviceOptions?.abortSignal,
557
+ idleTimeoutMs: serviceOptions?.downloadIdleTimeoutMs,
558
+ ...(serviceOptions?.range ? { range: serviceOptions.range } : {}),
559
+ };
535
560
  }
536
561
 
537
562
  /**
@@ -639,22 +664,17 @@ export abstract class CoreFileService {
639
664
  * unchanged; what changed is that the document authorization inspected is the one being served.
640
665
  */
641
666
  async getFileStreamByName(filename: string, serviceOptions?: FileServiceOptions): Promise<Readable> {
667
+ assertByteRange(serviceOptions?.range);
642
668
  if (!(await this.checkRights(filename, { ...serviceOptions, checkInputType: 'filename' }))) {
643
669
  return null;
644
670
  }
645
671
  const s3FileInfo = await this.findS3FileByName(filename);
646
672
  if (s3FileInfo) {
647
- return this.guardDownloadStream(
648
- await S3FileHelper.getStream(this.options.s3Service, s3FileInfo._id, this.s3StreamOptions(serviceOptions)),
649
- serviceOptions,
650
- );
673
+ return this.openStoreStream('s3', s3FileInfo._id, serviceOptions);
651
674
  }
652
675
  const fsFileInfo = await this.findFilesystemFileByName(filename);
653
676
  if (fsFileInfo) {
654
- return this.guardDownloadStream(
655
- FilesystemFileHelper.getStream(this.filesystemDir, fsFileInfo._id),
656
- serviceOptions,
657
- );
677
+ return this.openStoreStream('filesystem', fsFileInfo._id, serviceOptions);
658
678
  }
659
679
  const gridFsInfo = await GridFSHelper.findFileByName(this.files, filename);
660
680
  if (!gridFsInfo) {
@@ -662,7 +682,7 @@ export abstract class CoreFileService {
662
682
  // itself, asynchronously on the stream, which the controller turns into a 404.
663
683
  return this.guardDownloadStream(GridFSHelper.openDownloadStreamByName(this.files, filename), serviceOptions);
664
684
  }
665
- return this.guardDownloadStream(GridFSHelper.openDownloadStream(this.files, gridFsInfo._id), serviceOptions);
685
+ return this.openStoreStream('gridfs', gridFsInfo._id, serviceOptions);
666
686
  }
667
687
 
668
688
  /**
@@ -14,6 +14,7 @@ import {
14
14
  findMetadataByName,
15
15
  } from './file-metadata.helper';
16
16
  import { streamToBuffer } from './s3-file.helper';
17
+ import type { ByteRange } from '../../common/helpers/byte-range.helper';
17
18
 
18
19
  /** Metadata collection for files stored on the local filesystem */
19
20
  export const FILESYSTEM_FILES_COLLECTION = 'filesystem-files';
@@ -169,10 +170,14 @@ export class FilesystemFileHelper {
169
170
  }
170
171
 
171
172
  /**
172
- * Get the download stream of a file
173
+ * Get the download stream of a file — with `range`, only those bytes (both inclusive, as `createReadStream()`
174
+ * counts them)
173
175
  */
174
- static getStream(directory: string, id: string | Types.ObjectId): Readable {
175
- return createReadStream(FilesystemFileHelper.filePath(directory, id));
176
+ static getStream(directory: string, id: string | Types.ObjectId, range?: ByteRange): Readable {
177
+ return createReadStream(
178
+ FilesystemFileHelper.filePath(directory, id),
179
+ range ? { end: range.end, start: range.start } : undefined,
180
+ );
176
181
  }
177
182
 
178
183
  /**
@@ -1,3 +1,5 @@
1
+ import type { ByteRange } from '../../../common/helpers/byte-range.helper';
2
+
1
3
  /**
2
4
  * Interface for service options in file services
3
5
  */
@@ -40,5 +42,16 @@ export interface FileServiceOptions {
40
42
  */
41
43
  metadata?: Record<string, any>;
42
44
 
45
+ /**
46
+ * Read only these bytes of the file (both inclusive) — `getFileStream()` / `getFileStreamByName()`, every store.
47
+ *
48
+ * What a route needs to answer a `Range` request with `206 Partial Content` (video and audio in Safari and every
49
+ * media player, resumed downloads). The range must lie inside the file (`end` below its `length`): produce it with
50
+ * `resolveDownloadRange(res, file.length)`, which also answers an unsatisfiable range with `416`. A malformed range
51
+ * (negative, not an integer, `start > end`) throws a `RangeError` before any store is asked. The rights check runs
52
+ * first, as for a whole file. Since 11.46.5.
53
+ */
54
+ range?: ByteRange;
55
+
43
56
  roles?: string | string[];
44
57
  }
@@ -2,6 +2,7 @@ import { Types } from 'mongoose';
2
2
  import { Readable } from 'stream';
3
3
 
4
4
  import { CoreS3Service } from '../../common/services/core-s3.service';
5
+ import type { ByteRange } from '../../common/helpers/byte-range.helper';
5
6
  import {
6
7
  ensureFilenameIndex,
7
8
  FileCollection,
@@ -212,12 +213,12 @@ export class S3FileHelper {
212
213
  }
213
214
 
214
215
  /**
215
- * Get the download stream of a file
216
+ * Get the download stream of a file — with `range`, only those bytes (both inclusive)
216
217
  */
217
218
  static async getStream(
218
219
  s3Service: CoreS3Service,
219
220
  id: string | Types.ObjectId,
220
- options?: { abortSignal?: AbortSignal; idleTimeoutMs?: false | number },
221
+ options?: { abortSignal?: AbortSignal; idleTimeoutMs?: false | number; range?: ByteRange },
221
222
  ): Promise<Readable> {
222
223
  const objectId = typeof id === 'string' ? new Types.ObjectId(id) : id;
223
224
  return (await s3Service.getObject(objectId.toHexString(), options)).body;
package/src/index.ts CHANGED
@@ -30,6 +30,7 @@ export * from './core/common/enums/sort-order.emum';
30
30
  export * from './core/common/exceptions/access-denied.exception';
31
31
  export * from './core/common/filters/http-exception-log.filter';
32
32
  export * from './core/common/filters/mongoose-cast-error.filter';
33
+ export * from './core/common/helpers/byte-range.helper';
33
34
  export * from './core/common/helpers/common.helper';
34
35
  export * from './core/common/helpers/config.helper';
35
36
  export * from './core/common/helpers/content-disposition.helper';
@@ -205,10 +205,11 @@ await fs.promises.writeFile('/tmp/downloaded.bin', buffer);
205
205
 
206
206
  The second parameter accepts either a plain token string or a `TestDownloadOptions` object:
207
207
 
208
- | Option | Type | Description |
209
- | --------- | -------- | ------------------------------------------------------------- |
210
- | `token` | `string` | Bearer token via Authorization header (JWT) |
211
- | `cookies` | `string` | Plain session token, converted via `buildBetterAuthCookies()` |
208
+ | Option | Type | Description |
209
+ | --------- | ------------------------ | ------------------------------------------------------------- |
210
+ | `token` | `string` | Bearer token via Authorization header (JWT) |
211
+ | `cookies` | `string` | Plain session token, converted via `buildBetterAuthCookies()` |
212
+ | `headers` | `Record<string, string>` | Additional request headers, e.g. `Range` (since 11.46.5) |
212
213
 
213
214
  Both can be used simultaneously — `token` sets the Authorization header while `cookies` sets the Cookie header.
214
215
 
@@ -224,6 +225,9 @@ await testHelper.download('/files/id/abc123', { cookies: sessionToken });
224
225
 
225
226
  // Both simultaneously
226
227
  await testHelper.download('/files/id/abc123', { cookies: sessionToken, token: jwtToken });
228
+
229
+ // A byte range: 206, `Content-Range: bytes 0-1/<length>`, two bytes in `res.data`
230
+ const res = await testHelper.download('/files/id/abc123', { headers: { Range: 'bytes=0-1' }, token: jwtToken });
227
231
  ```
228
232
 
229
233
  ## GraphQL Testing (`testHelper.graphQl()`)
@@ -189,6 +189,12 @@ export interface TestDownloadOptions {
189
189
  */
190
190
  cookies?: string;
191
191
 
192
+ /**
193
+ * Additional request headers, e.g. `{ Range: 'bytes=0-1' }` for a ranged download or `If-None-Match`.
194
+ * Set before `cookies` and `token`, which win for their own header. Since 11.46.5.
195
+ */
196
+ headers?: Record<string, string>;
197
+
192
198
  /**
193
199
  * Bearer token via Authorization header (JWT authentication).
194
200
  * Can be used simultaneously with `cookies` — token sets the Authorization header
@@ -379,6 +385,9 @@ export class TestHelper {
379
385
  if (typeof tokenOrOptions === 'string') {
380
386
  request.set('Authorization', `bearer ${tokenOrOptions}`);
381
387
  } else if (tokenOrOptions) {
388
+ for (const [name, value] of Object.entries(tokenOrOptions.headers ?? {})) {
389
+ request.set(name, value);
390
+ }
382
391
  if (tokenOrOptions.cookies) {
383
392
  const cookieRecord = TestHelper.buildBetterAuthCookies(tokenOrOptions.cookies);
384
393
  request.set(
@@ -424,6 +433,9 @@ export class TestHelper {
424
433
  if (typeof tokenOrOptions === 'string') {
425
434
  request.set('Authorization', `bearer ${tokenOrOptions}`);
426
435
  } else if (tokenOrOptions) {
436
+ for (const [name, value] of Object.entries(tokenOrOptions.headers ?? {})) {
437
+ request.set(name, value);
438
+ }
427
439
  if (tokenOrOptions.cookies) {
428
440
  const cookieRecord = TestHelper.buildBetterAuthCookies(tokenOrOptions.cookies);
429
441
  request.set(