@lenne.tech/nest-server 11.41.8 → 11.41.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.
@@ -1,4 +1,5 @@
1
1
  import { DynamicModule, Global, Logger, Module, OnModuleInit, Type } from '@nestjs/common';
2
+ import { ModuleRef } from '@nestjs/core';
2
3
  import { getConnectionToken } from '@nestjs/mongoose';
3
4
  import { Connection } from 'mongoose';
4
5
 
@@ -54,6 +55,32 @@ export interface TusModuleOptions {
54
55
  * ```
55
56
  */
56
57
  controller?: Type<CoreTusController>;
58
+
59
+ /**
60
+ * Custom service class to use instead of CoreTusService.
61
+ * The class must extend CoreTusService and keep its constructor signature
62
+ * `(connection, options?)`: the module constructs it with exactly those arguments, so it cannot
63
+ * declare constructor dependencies of its own. Project providers are reached at call time through
64
+ * `this.options?.moduleRef`.
65
+ *
66
+ * The seam for project-specific upload rules — e.g. override `onUploadCreate()` to enforce a quota
67
+ * from the declared `Upload-Length` before a single byte is written, or `onUploadComplete()` to
68
+ * post-process the finished file.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * export class TusService extends CoreTusService {
73
+ * protected override async onUploadCreate(req: any, upload: Upload) {
74
+ * const quota = this.options?.moduleRef?.get(QuotaService, { strict: false });
75
+ * await quota?.assertRoomFor(req, upload.size); // throw an Error with status_code 413 to refuse
76
+ * return super.onUploadCreate(req, upload);
77
+ * }
78
+ * }
79
+ *
80
+ * TusModule.forRoot({ service: TusService })
81
+ * ```
82
+ */
83
+ service?: Type<CoreTusService>;
57
84
  }
58
85
 
59
86
  /**
@@ -98,6 +125,7 @@ export class TusModule implements OnModuleInit {
98
125
  private static tusEnabled = false;
99
126
  private static currentConfig: ITusConfig | null = null;
100
127
  private static customController: null | Type<CoreTusController> = null;
128
+ private static customService: null | Type<CoreTusService> = null;
101
129
 
102
130
  constructor(private readonly tusService?: CoreTusService) {}
103
131
 
@@ -157,15 +185,17 @@ export class TusModule implements OnModuleInit {
157
185
  * @returns Dynamic module configuration
158
186
  */
159
187
  static forRoot(options: TusModuleOptions = {}): DynamicModule {
160
- const { config: rawConfig, controller } = options;
188
+ const { config: rawConfig, controller, service } = options;
161
189
 
162
190
  // Normalize config: undefined/true → enabled with defaults, false → disabled
163
191
  const config = normalizeTusConfig(rawConfig);
164
192
 
165
193
  // Store config for service configuration
166
194
  this.currentConfig = config;
167
- // Store custom controller if provided
195
+ // Store custom controller and service if provided
168
196
  this.customController = controller || null;
197
+ this.customService = service || null;
198
+ const ServiceClass = this.customService || CoreTusService;
169
199
 
170
200
  // If TUS is disabled, return minimal module
171
201
  if (config === null) {
@@ -183,9 +213,9 @@ export class TusModule implements OnModuleInit {
183
213
  inject: [getConnectionToken()],
184
214
  provide: CoreTusService,
185
215
  useFactory: (connection: Connection) => {
186
- const service = new CoreTusService(connection);
187
- service.configure(false);
188
- return service;
216
+ const tusService = new ServiceClass(connection);
217
+ tusService.configure(false);
218
+ return tusService;
189
219
  },
190
220
  },
191
221
  ],
@@ -230,6 +260,7 @@ export class TusModule implements OnModuleInit {
230
260
  ConfigService,
231
261
  { optional: true, token: CoreS3Service },
232
262
  { optional: true, token: CoreRedisService },
263
+ ModuleRef,
233
264
  ],
234
265
  provide: CoreTusService,
235
266
  useFactory: async (
@@ -238,14 +269,15 @@ export class TusModule implements OnModuleInit {
238
269
  configService: ConfigService,
239
270
  s3Service?: CoreS3Service,
240
271
  redisService?: CoreRedisService,
272
+ moduleRef?: ModuleRef,
241
273
  ) => {
242
- const service = new CoreTusService(connection, { configService, redisService, s3Service });
243
- service.configure(tusConfig);
274
+ const tusService = new ServiceClass(connection, { configService, moduleRef, redisService, s3Service });
275
+ tusService.configure(tusConfig);
244
276
  // NestJS DOES call onModuleInit on a factory-provided instance — its hook iterates
245
277
  // every non-alias provider, however it was constructed. Calling it here as well ran
246
278
  // init TWICE per boot: two TUS servers, two S3 stores, and two hourly expiration
247
279
  // intervals of which onModuleDestroy clears only the second.
248
- return service;
280
+ return tusService;
249
281
  },
250
282
  },
251
283
  ],
@@ -260,5 +292,6 @@ export class TusModule implements OnModuleInit {
260
292
  this.tusEnabled = false;
261
293
  this.currentConfig = null;
262
294
  this.customController = null;
295
+ this.customService = null;
263
296
  }
264
297
  }