@lenne.tech/nest-server 11.7.3 → 11.8.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 (42) hide show
  1. package/dist/core/common/interfaces/server-options.interface.d.ts +22 -0
  2. package/dist/core/modules/file/core-file.controller.d.ts +1 -0
  3. package/dist/core/modules/file/core-file.controller.js +22 -0
  4. package/dist/core/modules/file/core-file.controller.js.map +1 -1
  5. package/dist/core/modules/tus/core-tus.controller.d.ts +9 -0
  6. package/dist/core/modules/tus/core-tus.controller.js +85 -0
  7. package/dist/core/modules/tus/core-tus.controller.js.map +1 -0
  8. package/dist/core/modules/tus/core-tus.service.d.ts +30 -0
  9. package/dist/core/modules/tus/core-tus.service.js +284 -0
  10. package/dist/core/modules/tus/core-tus.service.js.map +1 -0
  11. package/dist/core/modules/tus/index.d.ts +4 -0
  12. package/dist/core/modules/tus/index.js +21 -0
  13. package/dist/core/modules/tus/index.js.map +1 -0
  14. package/dist/core/modules/tus/interfaces/tus-config.interface.d.ts +10 -0
  15. package/dist/core/modules/tus/interfaces/tus-config.interface.js +59 -0
  16. package/dist/core/modules/tus/interfaces/tus-config.interface.js.map +1 -0
  17. package/dist/core/modules/tus/tus.module.d.ts +21 -0
  18. package/dist/core/modules/tus/tus.module.js +99 -0
  19. package/dist/core/modules/tus/tus.module.js.map +1 -0
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.js +1 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/server/modules/file/file.controller.d.ts +5 -7
  24. package/dist/server/modules/file/file.controller.js +3 -31
  25. package/dist/server/modules/file/file.controller.js.map +1 -1
  26. package/dist/server/server.module.js +3 -1
  27. package/dist/server/server.module.js.map +1 -1
  28. package/dist/tsconfig.build.tsbuildinfo +1 -1
  29. package/package.json +4 -1
  30. package/src/core/common/interfaces/server-options.interface.ts +154 -0
  31. package/src/core/modules/file/README.md +165 -0
  32. package/src/core/modules/file/core-file.controller.ts +27 -1
  33. package/src/core/modules/tus/INTEGRATION-CHECKLIST.md +176 -0
  34. package/src/core/modules/tus/README.md +439 -0
  35. package/src/core/modules/tus/core-tus.controller.ts +88 -0
  36. package/src/core/modules/tus/core-tus.service.ts +424 -0
  37. package/src/core/modules/tus/index.ts +5 -0
  38. package/src/core/modules/tus/interfaces/tus-config.interface.ts +107 -0
  39. package/src/core/modules/tus/tus.module.ts +187 -0
  40. package/src/index.ts +6 -0
  41. package/src/server/modules/file/file.controller.ts +14 -34
  42. package/src/server/server.module.ts +5 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "11.7.3",
3
+ "version": "11.8.0",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -93,6 +93,8 @@
93
93
  "@nestjs/swagger": "11.2.3",
94
94
  "@nestjs/terminus": "11.0.0",
95
95
  "@nestjs/websockets": "11.1.9",
96
+ "@tus/file-store": "2.0.0",
97
+ "@tus/server": "2.3.0",
96
98
  "apollo-server-core": "3.13.0",
97
99
  "bcrypt": "6.0.0",
98
100
  "better-auth": "1.4.8-beta.4",
@@ -167,6 +169,7 @@
167
169
  "ts-morph": "27.0.2",
168
170
  "ts-node": "10.9.2",
169
171
  "tsconfig-paths": "4.2.0",
172
+ "tus-js-client": "4.3.1",
170
173
  "typescript": "5.9.3",
171
174
  "unplugin-swc": "1.5.9",
172
175
  "vite": "7.3.0",
@@ -1132,4 +1132,158 @@ export interface IServerOptions {
1132
1132
  */
1133
1133
  path?: string;
1134
1134
  };
1135
+
1136
+ /**
1137
+ * TUS resumable upload configuration.
1138
+ *
1139
+ * Follows the "Enabled by Default" pattern - tus is automatically enabled
1140
+ * without any configuration. Set `tus: false` to explicitly disable.
1141
+ *
1142
+ * Accepts:
1143
+ * - `true` or `undefined`: Enable with defaults (enabled by default)
1144
+ * - `false`: Disable TUS uploads
1145
+ * - `{ ... }`: Enable with custom configuration
1146
+ *
1147
+ * @example
1148
+ * ```typescript
1149
+ * // Default: TUS enabled with all defaults (no config needed)
1150
+ *
1151
+ * // Disable TUS
1152
+ * tus: false,
1153
+ *
1154
+ * // Custom configuration
1155
+ * tus: {
1156
+ * maxSize: 100 * 1024 * 1024, // 100 MB
1157
+ * path: '/uploads',
1158
+ * },
1159
+ * ```
1160
+ *
1161
+ * @since 11.8.0
1162
+ */
1163
+ tus?: boolean | ITusConfig;
1164
+ }
1165
+
1166
+ /**
1167
+ * TUS Upload Configuration Interface
1168
+ *
1169
+ * Follows the "Enabled by Default" pattern - tus is automatically enabled
1170
+ * without any configuration. Set `tus: false` to explicitly disable.
1171
+ */
1172
+ export interface ITusConfig {
1173
+ /**
1174
+ * Additional allowed HTTP headers for TUS requests (beyond @tus/server defaults).
1175
+ *
1176
+ * Note: @tus/server already includes all TUS protocol headers:
1177
+ * Authorization, Content-Type, Location, Tus-Extension, Tus-Max-Size,
1178
+ * Tus-Resumable, Tus-Version, Upload-Concat, Upload-Defer-Length,
1179
+ * Upload-Length, Upload-Metadata, Upload-Offset, X-HTTP-Method-Override,
1180
+ * X-Requested-With, X-Forwarded-Host, X-Forwarded-Proto, Forwarded
1181
+ *
1182
+ * Use this only for project-specific custom headers.
1183
+ *
1184
+ * @default [] (no additional headers needed)
1185
+ */
1186
+ allowedHeaders?: string[];
1187
+
1188
+ /**
1189
+ * Allowed MIME types for uploads.
1190
+ * If undefined, all types are allowed.
1191
+ * @default undefined (all types allowed)
1192
+ */
1193
+ allowedTypes?: string[];
1194
+
1195
+ /**
1196
+ * Checksum extension configuration.
1197
+ * Enables data integrity verification.
1198
+ * @default true
1199
+ */
1200
+ checksum?: boolean;
1201
+
1202
+ /**
1203
+ * Concatenation extension configuration.
1204
+ * Allows parallel uploads that are merged.
1205
+ * @default true
1206
+ */
1207
+ concatenation?: boolean;
1208
+
1209
+ /**
1210
+ * Creation extension configuration.
1211
+ * Allows creating new uploads via POST.
1212
+ * @default true
1213
+ */
1214
+ creation?: boolean | ITusCreationConfig;
1215
+
1216
+ /**
1217
+ * Creation With Upload extension configuration.
1218
+ * Allows sending data in the initial POST request.
1219
+ * @default true
1220
+ */
1221
+ creationWithUpload?: boolean;
1222
+
1223
+ /**
1224
+ * Whether tus uploads are enabled.
1225
+ * @default true (enabled by default)
1226
+ */
1227
+ enabled?: boolean;
1228
+
1229
+ /**
1230
+ * Expiration extension configuration.
1231
+ * Automatically cleans up incomplete uploads.
1232
+ * @default { expiresIn: '24h' }
1233
+ */
1234
+ expiration?: boolean | ITusExpirationConfig;
1235
+
1236
+ /**
1237
+ * Maximum upload size in bytes
1238
+ * @default 50 * 1024 * 1024 * 1024 (50 GB)
1239
+ */
1240
+ maxSize?: number;
1241
+
1242
+ /**
1243
+ * Base path for tus endpoints
1244
+ * @default '/tus'
1245
+ */
1246
+ path?: string;
1247
+
1248
+ /**
1249
+ * Termination extension configuration.
1250
+ * Allows deleting uploads via DELETE.
1251
+ * @default true
1252
+ */
1253
+ termination?: boolean;
1254
+
1255
+ /**
1256
+ * Directory for temporary upload chunks.
1257
+ * @default 'uploads/tus'
1258
+ */
1259
+ uploadDir?: string;
1260
+ }
1261
+
1262
+ /**
1263
+ * TUS Creation extension configuration
1264
+ */
1265
+ export interface ITusCreationConfig {
1266
+ /**
1267
+ * Whether creation is enabled
1268
+ * @default true
1269
+ */
1270
+ enabled?: boolean;
1271
+ }
1272
+
1273
+ /**
1274
+ * TUS Expiration extension configuration
1275
+ */
1276
+ export interface ITusExpirationConfig {
1277
+ /**
1278
+ * Whether expiration is enabled
1279
+ * @default true
1280
+ */
1281
+ enabled?: boolean;
1282
+
1283
+ /**
1284
+ * Time until incomplete uploads expire
1285
+ * Supports formats: '24h', '1d', '12h', etc.
1286
+ * @default '24h'
1287
+ */
1288
+ expiresIn?: string;
1135
1289
  }
@@ -0,0 +1,165 @@
1
+ # File Module
2
+
3
+ File upload and download functionality with MongoDB GridFS storage.
4
+
5
+ ## Endpoints
6
+
7
+ ### Public Endpoints (via CoreFileController)
8
+
9
+ | Method | Endpoint | Description |
10
+ |--------|----------|-------------|
11
+ | GET | `/files/id/:id` | Download file by ID |
12
+ | GET | `/files/:filename` | Download file by filename |
13
+
14
+ **Note:** These endpoints are public (`S_EVERYONE`) by default. Projects can restrict access by extending `CoreFileController`.
15
+
16
+ ### Admin Endpoints (project-specific)
17
+
18
+ Projects typically add admin-only endpoints like:
19
+
20
+ | Method | Endpoint | Description |
21
+ |--------|----------|-------------|
22
+ | POST | `/files/upload` | Upload file (multipart/form-data) |
23
+ | GET | `/files/info/:id` | Get file metadata |
24
+ | DELETE | `/files/:id` | Delete file |
25
+
26
+ ---
27
+
28
+ ## Usage in Projects
29
+
30
+ ### Basic Setup (Extend CoreFileController)
31
+
32
+ ```typescript
33
+ // src/server/modules/file/file.controller.ts
34
+ import { Controller } from '@nestjs/common';
35
+ import { CoreFileController, Roles, RoleEnum } from '@lenne.tech/nest-server';
36
+ import { FileService } from './file.service';
37
+
38
+ @Controller('files')
39
+ @Roles(RoleEnum.ADMIN)
40
+ export class FileController extends CoreFileController {
41
+ constructor(protected override readonly fileService: FileService) {
42
+ super(fileService);
43
+ }
44
+
45
+ // Add admin-only endpoints here (upload, delete, etc.)
46
+ }
47
+ ```
48
+
49
+ ### Restrict Download Access
50
+
51
+ To require authentication for downloads, override the inherited methods:
52
+
53
+ ```typescript
54
+ @Controller('files')
55
+ @Roles(RoleEnum.ADMIN)
56
+ export class FileController extends CoreFileController {
57
+ constructor(protected override readonly fileService: FileService) {
58
+ super(fileService);
59
+ }
60
+
61
+ // Override to require authentication for ID-based download
62
+ @Get('id/:id')
63
+ @Roles(RoleEnum.S_USER) // Require logged-in user
64
+ override async getFileById(@Param('id') id: string, @Res() res: Response) {
65
+ return super.getFileById(id, res);
66
+ }
67
+
68
+ // Override to require authentication for filename-based download
69
+ @Get(':filename')
70
+ @Roles(RoleEnum.S_USER)
71
+ override async getFile(@Param('filename') filename: string, @Res() res: Response) {
72
+ return super.getFile(filename, res);
73
+ }
74
+ }
75
+ ```
76
+
77
+ ---
78
+
79
+ ## GraphQL Support
80
+
81
+ File operations are also available via GraphQL through `CoreFileResolver`:
82
+
83
+ ```graphql
84
+ # Query file by ID
85
+ query {
86
+ file(id: "...") {
87
+ id
88
+ filename
89
+ contentType
90
+ length
91
+ }
92
+ }
93
+
94
+ # Query file by filename
95
+ query {
96
+ fileByFilename(filename: "...") {
97
+ id
98
+ filename
99
+ contentType
100
+ }
101
+ }
102
+
103
+ # Upload file (via GraphQL Upload scalar)
104
+ mutation {
105
+ uploadFile(file: Upload!) {
106
+ id
107
+ filename
108
+ }
109
+ }
110
+
111
+ # Delete file
112
+ mutation {
113
+ deleteFile(filename: "...") {
114
+ id
115
+ }
116
+ }
117
+ ```
118
+
119
+ ---
120
+
121
+ ## Integration with TUS
122
+
123
+ Files uploaded via TUS are automatically stored in GridFS and can be accessed through the same endpoints:
124
+
125
+ ```bash
126
+ # After TUS upload completes, download by ID
127
+ GET /files/id/<gridfs-file-id>
128
+
129
+ # Or by filename (if unique)
130
+ GET /files/<original-filename>
131
+ ```
132
+
133
+ **Recommendation:** Use ID-based downloads for TUS uploads as filenames may not be unique.
134
+
135
+ ---
136
+
137
+ ## GridFS Storage
138
+
139
+ Files are stored in MongoDB GridFS with the following structure:
140
+
141
+ **fs.files collection:**
142
+ ```json
143
+ {
144
+ "_id": ObjectId,
145
+ "filename": "example.pdf",
146
+ "length": 1048576,
147
+ "uploadDate": ISODate,
148
+ "metadata": {
149
+ "contentType": "application/pdf",
150
+ "tusUploadId": "...", // If uploaded via TUS
151
+ "uploadedAt": ISODate
152
+ }
153
+ }
154
+ ```
155
+
156
+ **fs.chunks collection:**
157
+ - Binary file data split into 255KB chunks
158
+ - Automatically managed by GridFS
159
+
160
+ ---
161
+
162
+ ## Related Documentation
163
+
164
+ - [TUS Module](../tus/README.md) - Resumable upload protocol
165
+ - [CoreFileService](./core-file.service.ts) - File service implementation
@@ -17,7 +17,33 @@ export abstract class CoreFileController {
17
17
  protected constructor(protected fileService: CoreFileService) {}
18
18
 
19
19
  /**
20
- * Download file
20
+ * Download file by ID
21
+ *
22
+ * More reliable than filename-based download as IDs are unique.
23
+ * Recommended for TUS uploads and when filename uniqueness cannot be guaranteed.
24
+ */
25
+ @Get('id/:id')
26
+ @Roles(RoleEnum.S_EVERYONE)
27
+ async getFileById(@Param('id') id: string, @Res() res: Response) {
28
+ if (!id) {
29
+ throw new BadRequestException('Missing file ID for download');
30
+ }
31
+
32
+ const file = await this.fileService.getFileInfo(id);
33
+ if (!file) {
34
+ throw new NotFoundException('File not found');
35
+ }
36
+ const filestream = await this.fileService.getFileStream(id);
37
+ res.header('Content-Type', file.contentType || 'application/octet-stream');
38
+ res.header('Content-Disposition', `attachment; filename=${file.filename}`);
39
+ return filestream.pipe(res);
40
+ }
41
+
42
+ /**
43
+ * Download file by filename
44
+ *
45
+ * Note: If multiple files have the same filename, only the first match is returned.
46
+ * For unique file access, use GET /files/id/:id instead.
21
47
  */
22
48
  @Get(':filename')
23
49
  @Roles(RoleEnum.S_EVERYONE)
@@ -0,0 +1,176 @@
1
+ # TUS Integration Checklist
2
+
3
+ **For customizing TUS uploads in projects using `@lenne.tech/nest-server`.**
4
+
5
+ > **Note:** TUS is **enabled by default** with no configuration needed. This checklist is only for projects that need to customize behavior (e.g., require authentication).
6
+
7
+ ---
8
+
9
+ ## Do You Need This Checklist?
10
+
11
+ | Scenario | Checklist Needed? |
12
+ |----------|-------------------|
13
+ | Use TUS with defaults (everyone can upload) | No - works automatically |
14
+ | Require authentication for uploads | Yes - Step 1 |
15
+ | Custom upload handling (notifications, etc.) | Yes - Step 2 |
16
+ | Disable TUS completely | No - just use `TusModule.forRoot({ config: false })` |
17
+
18
+ ---
19
+
20
+ ## Reference Implementation
21
+
22
+ **Local (in your node_modules):**
23
+ ```
24
+ node_modules/@lenne.tech/nest-server/src/server/server.module.ts
25
+ ```
26
+
27
+ **GitHub:**
28
+ https://github.com/lenneTech/nest-server/tree/develop/src/server
29
+
30
+ ---
31
+
32
+ ## Step 1: Custom Controller (Require Authentication)
33
+
34
+ **Create:** `src/server/modules/tus/tus.controller.ts`
35
+
36
+ ```typescript
37
+ import { Controller } from '@nestjs/common';
38
+ import { CoreTusController, Roles, RoleEnum } from '@lenne.tech/nest-server';
39
+
40
+ @Controller('tus')
41
+ @Roles(RoleEnum.S_USER) // Require authenticated user
42
+ export class TusController extends CoreTusController {
43
+ // All methods inherit the S_USER requirement
44
+ // Override methods here for custom logic
45
+ }
46
+ ```
47
+
48
+ **Update ServerModule:**
49
+
50
+ ```typescript
51
+ // src/server/server.module.ts
52
+ import { TusModule } from '@lenne.tech/nest-server';
53
+ import { TusController } from './modules/tus/tus.controller';
54
+
55
+ @Module({
56
+ imports: [
57
+ // ... other imports
58
+ TusModule.forRoot({
59
+ controller: TusController, // Use custom controller
60
+ }),
61
+ ],
62
+ })
63
+ export class ServerModule {}
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Step 2: Custom Service (Custom Upload Handling)
69
+
70
+ **Create:** `src/server/modules/tus/tus.service.ts`
71
+
72
+ ```typescript
73
+ import { Injectable } from '@nestjs/common';
74
+ import { CoreTusService } from '@lenne.tech/nest-server';
75
+ import { Upload } from '@tus/server';
76
+
77
+ @Injectable()
78
+ export class TusService extends CoreTusService {
79
+ protected override async onUploadComplete(upload: Upload): Promise<void> {
80
+ // Call parent to handle GridFS migration
81
+ await super.onUploadComplete(upload);
82
+
83
+ // Add custom logic
84
+ const metadata = upload.metadata;
85
+ console.log(`Upload complete: ${metadata.filename}`);
86
+ // await this.notificationService.sendUploadComplete(...);
87
+ }
88
+ }
89
+ ```
90
+
91
+ **Note:** To use a custom service, you'll need to create a custom TusModule that provides your service instead of CoreTusService.
92
+
93
+ ---
94
+
95
+ ## Configuration Options
96
+
97
+ ### Default Configuration (No Changes Needed)
98
+
99
+ ```typescript
100
+ // TUS works with these defaults:
101
+ {
102
+ enabled: true,
103
+ path: '/tus',
104
+ maxSize: 50 * 1024 * 1024 * 1024, // 50 GB
105
+ expiration: { expiresIn: '24h' },
106
+ }
107
+ ```
108
+
109
+ ### Custom Configuration
110
+
111
+ ```typescript
112
+ // server.module.ts
113
+ TusModule.forRoot({
114
+ config: {
115
+ maxSize: 100 * 1024 * 1024, // 100 MB
116
+ path: '/uploads',
117
+ expiration: { expiresIn: '12h' },
118
+ },
119
+ })
120
+ ```
121
+
122
+ ### Disable TUS
123
+
124
+ ```typescript
125
+ TusModule.forRoot({ config: false })
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Verification Checklist
131
+
132
+ - [ ] `npm run build` succeeds
133
+ - [ ] `npm test` passes
134
+ - [ ] `OPTIONS /tus` returns TUS capabilities
135
+ - [ ] Upload via tus-js-client works
136
+ - [ ] File appears in GridFS after upload completion
137
+ - [ ] (If customized) Authentication is required for uploads
138
+
139
+ ---
140
+
141
+ ## Common Mistakes
142
+
143
+ | Mistake | Symptom | Fix |
144
+ |---------|---------|-----|
145
+ | Forgot to register custom controller | Default S_EVERYONE permissions | Add `controller: TusController` to forRoot() |
146
+ | Custom controller missing @Roles | No authentication required | Add `@Roles(RoleEnum.S_USER)` to controller class |
147
+ | Using wrong endpoint path | 404 on upload | Ensure client uses same path as config |
148
+
149
+ ---
150
+
151
+ ## Client Configuration
152
+
153
+ ```typescript
154
+ import { Upload } from 'tus-js-client';
155
+
156
+ const upload = new Upload(file, {
157
+ endpoint: 'http://localhost:3000/tus',
158
+ headers: {
159
+ Authorization: `Bearer ${token}`, // If authentication required
160
+ },
161
+ metadata: {
162
+ filename: file.name,
163
+ filetype: file.type,
164
+ },
165
+ onSuccess: () => console.log('Upload complete!'),
166
+ });
167
+
168
+ upload.start();
169
+ ```
170
+
171
+ ---
172
+
173
+ ## Detailed Documentation
174
+
175
+ - **README.md:** `node_modules/@lenne.tech/nest-server/src/core/modules/tus/README.md`
176
+ - **GitHub:** https://github.com/lenneTech/nest-server/blob/develop/src/core/modules/tus/README.md