@studious-lms/server 1.4.1 → 1.4.2

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 (149) hide show
  1. package/.env.example +6 -0
  2. package/.env.test.example +2 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +36 -50
  5. package/dist/index.js.map +1 -1
  6. package/dist/lib/config/cors.d.ts +16 -0
  7. package/dist/lib/config/cors.d.ts.map +1 -0
  8. package/dist/lib/config/cors.js +75 -0
  9. package/dist/lib/config/cors.js.map +1 -0
  10. package/dist/lib/config/env.d.ts +14 -0
  11. package/dist/lib/config/env.d.ts.map +1 -1
  12. package/dist/lib/config/env.js +9 -2
  13. package/dist/lib/config/env.js.map +1 -1
  14. package/dist/lib/prisma.d.ts +14 -2
  15. package/dist/lib/prisma.d.ts.map +1 -1
  16. package/dist/lib/prisma.js +27 -8
  17. package/dist/lib/prisma.js.map +1 -1
  18. package/dist/middleware/security.d.ts.map +1 -1
  19. package/dist/middleware/security.js +3 -3
  20. package/dist/middleware/security.js.map +1 -1
  21. package/dist/models/agenda.d.ts +16 -16
  22. package/dist/models/announcement.d.ts +59 -23
  23. package/dist/models/announcement.d.ts.map +1 -1
  24. package/dist/models/assignment.d.ts +363 -276
  25. package/dist/models/assignment.d.ts.map +1 -1
  26. package/dist/models/attendance.d.ts +63 -21
  27. package/dist/models/attendance.d.ts.map +1 -1
  28. package/dist/models/auth.d.ts +102 -18
  29. package/dist/models/auth.d.ts.map +1 -1
  30. package/dist/models/class.d.ts +112 -64
  31. package/dist/models/class.d.ts.map +1 -1
  32. package/dist/models/comment.d.ts +52 -16
  33. package/dist/models/comment.d.ts.map +1 -1
  34. package/dist/models/conversation.d.ts +46 -16
  35. package/dist/models/conversation.d.ts.map +1 -1
  36. package/dist/models/event.d.ts +107 -53
  37. package/dist/models/event.d.ts.map +1 -1
  38. package/dist/models/file.d.ts +213 -165
  39. package/dist/models/file.d.ts.map +1 -1
  40. package/dist/models/folder.d.ts +161 -77
  41. package/dist/models/folder.d.ts.map +1 -1
  42. package/dist/models/labChat.d.ts +73 -31
  43. package/dist/models/labChat.d.ts.map +1 -1
  44. package/dist/models/marketing.d.ts +25 -7
  45. package/dist/models/marketing.d.ts.map +1 -1
  46. package/dist/models/message.d.ts +31 -13
  47. package/dist/models/message.d.ts.map +1 -1
  48. package/dist/models/newtonChat.d.ts +34 -10
  49. package/dist/models/newtonChat.d.ts.map +1 -1
  50. package/dist/models/notification.d.ts +25 -7
  51. package/dist/models/notification.d.ts.map +1 -1
  52. package/dist/models/section.d.ts +71 -23
  53. package/dist/models/section.d.ts.map +1 -1
  54. package/dist/models/user.d.ts +27 -9
  55. package/dist/models/user.d.ts.map +1 -1
  56. package/dist/models/worksheet.d.ts +237 -108
  57. package/dist/models/worksheet.d.ts.map +1 -1
  58. package/dist/pipelines/aiLabChat.d.ts +22 -2
  59. package/dist/pipelines/aiLabChat.d.ts.map +1 -1
  60. package/dist/pipelines/aiLabChat.js +125 -95
  61. package/dist/pipelines/aiLabChat.js.map +1 -1
  62. package/dist/pipelines/aiLabChatContract.d.ts +22 -22
  63. package/dist/pipelines/gradeWorksheet.d.ts +8 -8
  64. package/dist/pipelines/gradeWorksheet.js +4 -4
  65. package/dist/pipelines/gradeWorksheet.js.map +1 -1
  66. package/dist/pipelines/labChatPrompt.d.ts +27 -0
  67. package/dist/pipelines/labChatPrompt.d.ts.map +1 -1
  68. package/dist/pipelines/labChatPrompt.js +143 -69
  69. package/dist/pipelines/labChatPrompt.js.map +1 -1
  70. package/dist/routers/_app.d.ts +1439 -1223
  71. package/dist/routers/_app.d.ts.map +1 -1
  72. package/dist/routers/agenda.d.ts +16 -16
  73. package/dist/routers/announcement.d.ts +19 -19
  74. package/dist/routers/assignment.d.ts +307 -291
  75. package/dist/routers/assignment.d.ts.map +1 -1
  76. package/dist/routers/assignment.js +3 -2
  77. package/dist/routers/assignment.js.map +1 -1
  78. package/dist/routers/attendance.d.ts +7 -7
  79. package/dist/routers/auth.d.ts +1 -1
  80. package/dist/routers/class.d.ts +77 -71
  81. package/dist/routers/class.d.ts.map +1 -1
  82. package/dist/routers/comment.d.ts +6 -6
  83. package/dist/routers/conversation.d.ts +11 -11
  84. package/dist/routers/event.d.ts +35 -35
  85. package/dist/routers/file.d.ts +12 -12
  86. package/dist/routers/folder.d.ts +54 -54
  87. package/dist/routers/labChat.d.ts +12 -12
  88. package/dist/routers/marketing.d.ts +2 -2
  89. package/dist/routers/message.d.ts +2 -2
  90. package/dist/routers/newtonChat.d.ts +1 -1
  91. package/dist/routers/notifications.d.ts +4 -4
  92. package/dist/routers/section.d.ts +7 -7
  93. package/dist/routers/studentProgress.d.ts +86 -0
  94. package/dist/routers/studentProgress.d.ts.map +1 -1
  95. package/dist/routers/studentProgress.js +14 -4
  96. package/dist/routers/studentProgress.js.map +1 -1
  97. package/dist/routers/user.d.ts +1 -1
  98. package/dist/routers/worksheet.d.ts +58 -58
  99. package/dist/seedDatabase.d.ts +1 -1
  100. package/dist/services/agenda.d.ts +16 -16
  101. package/dist/services/announcement.d.ts +8 -8
  102. package/dist/services/assignment.d.ts +299 -283
  103. package/dist/services/assignment.d.ts.map +1 -1
  104. package/dist/services/assignment.js +24 -5
  105. package/dist/services/assignment.js.map +1 -1
  106. package/dist/services/attendance.d.ts +7 -7
  107. package/dist/services/auth.d.ts +1 -1
  108. package/dist/services/class.d.ts +73 -67
  109. package/dist/services/class.d.ts.map +1 -1
  110. package/dist/services/comment.d.ts +6 -6
  111. package/dist/services/conversation.d.ts +11 -11
  112. package/dist/services/event.d.ts +31 -31
  113. package/dist/services/file.d.ts +12 -12
  114. package/dist/services/folder.d.ts +52 -52
  115. package/dist/services/labChat.d.ts +12 -12
  116. package/dist/services/marketing.d.ts +2 -2
  117. package/dist/services/notification.d.ts +4 -4
  118. package/dist/services/section.d.ts +6 -6
  119. package/dist/services/studentProgress.d.ts +75 -0
  120. package/dist/services/studentProgress.d.ts.map +1 -1
  121. package/dist/services/studentProgress.js +296 -106
  122. package/dist/services/studentProgress.js.map +1 -1
  123. package/dist/services/worksheet.d.ts +49 -49
  124. package/dist/utils/inference.d.ts +0 -11
  125. package/dist/utils/inference.d.ts.map +1 -1
  126. package/dist/utils/inference.js +2 -50
  127. package/dist/utils/inference.js.map +1 -1
  128. package/package.json +1 -1
  129. package/prisma/migrations/20260410124000_add_submission_recommendation_state/migration.sql +14 -0
  130. package/prisma/schema.prisma +14 -0
  131. package/src/index.ts +39 -51
  132. package/src/lib/config/cors.ts +96 -0
  133. package/src/lib/config/env.ts +12 -1
  134. package/src/lib/prisma.ts +25 -6
  135. package/src/middleware/security.ts +1 -1
  136. package/src/pipelines/aiLabChat.ts +175 -104
  137. package/src/pipelines/gradeWorksheet.ts +2 -2
  138. package/src/pipelines/labChatPrompt.ts +196 -68
  139. package/src/routers/assignment.ts +1 -0
  140. package/src/routers/studentProgress.ts +25 -1
  141. package/src/services/assignment.ts +30 -2
  142. package/src/services/studentProgress.ts +421 -120
  143. package/src/utils/inference.ts +0 -61
  144. package/tests/lib/cors.test.ts +103 -0
  145. package/tests/pipelines/aiLabChat.test.ts +64 -84
  146. package/tests/routers/studentProgress.test.ts +2 -31
  147. package/tests/utils/aiLabChatPrompt.test.ts +114 -6
  148. package/tests/utils/studentProgress.test.ts +361 -0
  149. package/vitest.unit.config.ts +1 -0
package/.env.example CHANGED
@@ -9,6 +9,12 @@ DIRECT_URL="postgresql://postgres:password@localhost:5432/postgres"
9
9
  # Application Configuration
10
10
  # Frontend URL for CORS configuration
11
11
  NEXT_PUBLIC_APP_URL="http://localhost:3000"
12
+ # Extra frontend origins allowed to call the API, comma-separated.
13
+ # Example: CORS_ALLOWED_ORIGINS="https://studious-git-replicas-stu-15-coderabbit-gua-8eec83-studious-lms.vercel.app"
14
+ CORS_ALLOWED_ORIGINS=""
15
+ # Extra frontend origin regex patterns, comma-separated. Keep these narrow.
16
+ # Example: CORS_ALLOWED_ORIGIN_PATTERNS="^https://studious-git-[a-z0-9-]+-studious-lms\.vercel\.app$"
17
+ CORS_ALLOWED_ORIGIN_PATTERNS=""
12
18
  # Server port
13
19
  PORT=3001
14
20
  # Environment: development, test, production
package/.env.test.example CHANGED
@@ -7,6 +7,8 @@ DIRECT_URL="postgresql://postgres:password@localhost:5434/postgres"
7
7
 
8
8
  # Application Configuration
9
9
  NEXT_PUBLIC_APP_URL="http://localhost:3000"
10
+ CORS_ALLOWED_ORIGINS=""
11
+ CORS_ALLOWED_ORIGIN_PATTERNS=""
10
12
  PORT=3002
11
13
  NODE_ENV=test
12
14
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAwBA,OAAO,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAyBA,OAAO,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="e28b4205-9ed2-5148-b94c-3326b3073261")}catch(e){}}();
2
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="0a09f7dd-016d-5f2f-bdf3-5a5a294b50cb")}catch(e){}}();
3
3
  import express from 'express';
4
4
  import { createServer } from 'http';
5
5
  import { Server } from 'socket.io';
@@ -13,6 +13,7 @@ import { bucket } from './lib/googleCloudStorage.js';
13
13
  import { prisma } from './lib/prisma.js';
14
14
  import { pusher } from './lib/pusher.js';
15
15
  import { connectRedis, disconnectRedis } from './lib/redis.js';
16
+ import { createCorsOriginMatcher } from './lib/config/cors.js';
16
17
  import { authLimiter, generalLimiter, helmetConfig, uploadLimiter } from './middleware/security.js';
17
18
  import * as Sentry from "@sentry/node";
18
19
  import { env } from './lib/config/env.js';
@@ -20,6 +21,7 @@ import compression from 'compression';
20
21
  import { v4 as uuidv4 } from 'uuid';
21
22
  import "./instrument.js";
22
23
  const app = express();
24
+ app.set('trust proxy', 1);
23
25
  app.use(helmetConfig);
24
26
  app.use(compression());
25
27
  app.use(express.json());
@@ -29,30 +31,22 @@ app.use((req, res, next) => {
29
31
  res.setHeader('X-Request-ID', requestId);
30
32
  next();
31
33
  });
32
- const allowedOrigins = env.NODE_ENV === 'production'
33
- ? [
34
- 'https://www.studious.sh',
35
- 'https://studious.sh',
36
- 'https://dev.studious.sh',
37
- 'https://www.dev.studious.sh',
38
- env.NEXT_PUBLIC_APP_URL,
39
- 'http://localhost:3000',
40
- ].filter(Boolean)
41
- : [
42
- 'http://localhost:3000',
43
- 'http://localhost:3001',
44
- 'http://127.0.0.1:3000',
45
- 'http://127.0.0.1:3001',
46
- env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
47
- ];
34
+ const { allowedOrigins, allowedOriginPatterns, isAllowedOrigin } = createCorsOriginMatcher(env);
48
35
  // CORS middleware
49
36
  app.use(cors({
50
- origin: allowedOrigins,
37
+ origin: (origin, callback) => {
38
+ if (!origin || isAllowedOrigin(origin)) {
39
+ callback(null, true);
40
+ return;
41
+ }
42
+ logger.warn('CORS origin rejected', { origin, allowedOrigins });
43
+ callback(null, false);
44
+ },
51
45
  credentials: true,
52
46
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
53
47
  allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'x-user'],
54
- preflightContinue: false, // Important: stop further handling of OPTIONS
55
- optionsSuccessStatus: 204, // Recommended for modern browsers
48
+ preflightContinue: false,
49
+ optionsSuccessStatus: 204,
56
50
  }));
57
51
  app.use(generalLimiter);
58
52
  // CORS debugging middleware
@@ -208,15 +202,9 @@ app.post('/api/pusher/auth', async (req, res) => {
208
202
  // Setup Socket.IO
209
203
  const io = new Server(httpServer, {
210
204
  cors: {
211
- origin: [
212
- 'http://localhost:3000', // Frontend development server
213
- 'http://localhost:3001', // Server port
214
- 'http://127.0.0.1:3000', // Alternative localhost
215
- 'http://127.0.0.1:3001', // Alternative localhost
216
- 'https://www.studious.sh', // Production frontend
217
- 'https://studious.sh', // Production frontend (without www)
218
- env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
219
- ],
205
+ origin: (origin, callback) => {
206
+ callback(null, !origin || isAllowedOrigin(origin));
207
+ },
220
208
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
221
209
  credentials: true,
222
210
  allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Access-Control-Allow-Origin', 'x-user']
@@ -429,20 +417,11 @@ function handleFileUpload(req, res) {
429
417
  const filePath = decodeURIComponent(req.params.filePath);
430
418
  // Set CORS headers for upload endpoint
431
419
  const origin = req.headers.origin;
432
- const allowedOrigins = [
433
- 'http://localhost:3000',
434
- 'http://localhost:3001',
435
- 'http://127.0.0.1:3000',
436
- 'http://127.0.0.1:3001',
437
- 'https://www.studious.sh', // Production frontend
438
- 'https://studious.sh', // Production frontend (without www)
439
- env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
440
- ];
441
- if (origin && allowedOrigins.includes(origin)) {
442
- res.header('Access-Control-Allow-Origin', origin);
420
+ if (origin && !isAllowedOrigin(origin)) {
421
+ return res.status(403).json({ error: 'Origin not allowed' });
443
422
  }
444
- else {
445
- res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
423
+ if (origin) {
424
+ res.header('Access-Control-Allow-Origin', origin);
446
425
  }
447
426
  res.header('Access-Control-Allow-Credentials', 'true');
448
427
  // Get content type from headers
@@ -479,6 +458,18 @@ function handleFileUpload(req, res) {
479
458
  }
480
459
  // Create caller
481
460
  const createCaller = createCallerFactory(appRouter);
461
+ // Fallback OPTIONS handler — prevents tRPC from rejecting preflight requests
462
+ // when the cors middleware passes through without ending the response.
463
+ app.options('/trpc/*', (req, res) => {
464
+ const origin = req.headers.origin;
465
+ if (origin && isAllowedOrigin(origin)) {
466
+ res.setHeader('Access-Control-Allow-Origin', origin);
467
+ res.setHeader('Access-Control-Allow-Credentials', 'true');
468
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
469
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, x-user');
470
+ }
471
+ res.sendStatus(204);
472
+ });
482
473
  // Setup tRPC middleware
483
474
  app.use('/trpc', createExpressMiddleware({
484
475
  router: appRouter,
@@ -513,13 +504,8 @@ logger.info('Configurations', {
513
504
  });
514
505
  // Log CORS configuration
515
506
  logger.info('CORS Configuration', {
516
- allowedOrigins: [
517
- 'http://localhost:3000',
518
- 'http://localhost:3001',
519
- 'http://127.0.0.1:3000',
520
- 'http://127.0.0.1:3001',
521
- env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
522
- ]
507
+ allowedOrigins,
508
+ allowedOriginPatterns: allowedOriginPatterns.map((pattern) => pattern.source),
523
509
  });
524
510
  const gracefulShutdown = (signal) => {
525
511
  logger.info(`Received ${signal}, shutting down gracefully`);
@@ -545,4 +531,4 @@ const gracefulShutdown = (signal) => {
545
531
  process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
546
532
  process.on('SIGINT', () => gracefulShutdown('SIGINT'));
547
533
  //# sourceMappingURL=index.js.map
548
- //# debugId=e28b4205-9ed2-5148-b94c-3326b3073261
534
+ //# debugId=0a09f7dd-016d-5f2f-bdf3-5a5a294b50cb
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["index.ts"],"sourceRoot":"/","sourcesContent":["import express from 'express';\nimport type { Request, Response } from 'express';\nimport { createServer } from 'http';\nimport { Server } from 'socket.io';\nimport cors from 'cors';\nimport dotenv from 'dotenv';\nimport { createExpressMiddleware } from '@trpc/server/adapters/express';\nimport { appRouter } from './routers/_app.js';\nimport { createTRPCContext, createCallerFactory } from './trpc.js';\nimport { logger } from './utils/logger.js';\nimport { setupSocketHandlers } from './socket/handlers.js';\nimport { bucket } from './lib/googleCloudStorage.js';\nimport { prisma } from './lib/prisma.js';\nimport { pusher } from './lib/pusher.js';\nimport { connectRedis, disconnectRedis } from './lib/redis.js';\n\nimport { authLimiter, generalLimiter, helmetConfig, uploadLimiter } from './middleware/security.js';\n\nimport * as Sentry from \"@sentry/node\";\nimport { env } from './lib/config/env.js';\nimport compression from 'compression';\nimport { v4 as uuidv4 } from 'uuid';\n\n\nimport \"./instrument.js\";\nimport { openAIClient } from './utils/inference.js';\n\nconst app = express();\n\napp.use(helmetConfig);\napp.use(compression());\napp.use(express.json());\napp.use(express.urlencoded({ extended: true }));\n\napp.use((req, res, next) => {\n const requestId = uuidv4();\n res.setHeader('X-Request-ID', requestId);\n next();\n});\n\nconst allowedOrigins = env.NODE_ENV === 'production'\n? [\n 'https://www.studious.sh',\n 'https://studious.sh',\n 'https://dev.studious.sh',\n 'https://www.dev.studious.sh',\n env.NEXT_PUBLIC_APP_URL,\n 'http://localhost:3000',\n\n ].filter(Boolean)\n: [\n 'http://localhost:3000',\n 'http://localhost:3001',\n 'http://127.0.0.1:3000',\n 'http://127.0.0.1:3001',\n\n env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'\n ];\n\n// CORS middleware\napp.use(cors({\n origin: allowedOrigins,\n credentials: true,\n methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'x-user'],\n preflightContinue: false, // Important: stop further handling of OPTIONS\n optionsSuccessStatus: 204, // Recommended for modern browsers\n\n}));\n\napp.use(generalLimiter);\n\n// CORS debugging middleware\napp.use((req, res, next) => {\n if (req.method === 'OPTIONS' || req.path.includes('trpc')) {\n logger.info('CORS Request', {\n method: req.method,\n path: req.path,\n origin: req.headers.origin,\n userAgent: req.headers['user-agent']\n });\n }\n next();\n});\n\n// Response time logging middleware\napp.use((req, res, next) => {\n const start = Date.now();\n res.on('finish', () => {\n const duration = Date.now() - start;\n logger.info('Request completed', {\n method: req.method,\n path: req.path,\n statusCode: res.statusCode,\n duration: `${duration}ms`\n });\n });\n next();\n});\n\n// app.use(\"/panel\", async (_, res) => {\n// if (env.NODE_ENV !== \"development\") {\n// return res.status(404).send(\"Not Found\");\n// }\n\n// // Dynamically import renderTrpcPanel only in development\n// const { renderTrpcPanel } = await import(\"trpc-ui\");\n\n// return res.send(\n// renderTrpcPanel(appRouter, {\n// url: \"/trpc\", // Base url of your trpc server\n// meta: {\n// title: \"Studious Backend\",\n// description:\n// \"This is the backend for the Studious application.\",\n// },\n// })\n// );\n// });\n\n\n// Create HTTP server\nconst httpServer = createServer(app);\n\napp.get('/health', async (req, res) => {\n\n try {\n // Check database connectivity\n await prisma.$queryRaw`SELECT 1`;\n \n res.status(200).json({ \n status: 'OK',\n timestamp: new Date().toISOString(),\n uptime: process.uptime(),\n database: 'connected'\n });\n } catch (error) {\n res.status(503).json({ \n status: 'ERROR',\n database: 'disconnected',\n error: error instanceof Error ? error.message : 'Unknown error'\n });\n }\n});\n\n// Pusher channel auth (for private-* and presence-* channels)\n// Token from: x-user header, or cookie (same-origin requests send cookies automatically)\napp.post('/api/pusher/auth', async (req, res) => {\n try {\n let token = req.headers['x-user'] as string | undefined;\n if (!token && req.headers.cookie) {\n const cookieName = env.PUSHER_AUTH_COOKIE_NAME || 'token';\n const match = req.headers.cookie.match(new RegExp(`${cookieName}=([^;]+)`));\n token = match?.[1]?.trim();\n }\n const { socket_id, channel_name } = req.body as { socket_id?: string; channel_name?: string };\n\n if (!socket_id || !channel_name) {\n return res.status(400).json({ error: 'socket_id and channel_name required' });\n }\n\n if (!token) {\n return res.status(401).json({ error: 'Authentication required' });\n }\n\n const user = await prisma.user.findFirst({\n where: { sessions: { some: { id: token } } },\n select: { id: true, username: true },\n });\n\n if (!user) {\n return res.status(401).json({ error: 'Invalid or expired session' });\n }\n\n // Verify channel access for private-conversation-* channels\n if (channel_name.startsWith('private-conversation-')) {\n const conversationId = channel_name.replace('private-conversation-', '');\n const member = await prisma.conversationMember.findFirst({\n where: { conversationId, userId: user.id },\n });\n \n if (!member) {\n return res.status(403).json({ error: 'Not a member of this conversation' });\n }\n }\n\n if (channel_name.startsWith('private-worksheet-')) {\n const worksheetId = channel_name.replace('private-worksheet-', '');\n const worksheet = await prisma.studentWorksheetResponse.findFirst({\n where: { id: worksheetId, OR: [\n { studentId: user.id },\n { submission: { assignment: { class: { teachers: { some: { id: user.id } } } } } },\n ] },\n });\n if (!worksheet) {\n return res.status(403).json({ error: 'No access to this worksheet' });\n }\n }\n\n if (channel_name.startsWith('private-teacher-')) {\n const classId = channel_name.replace('private-teacher-', '');\n const isTeacher = await prisma.class.findFirst({\n where: { id: classId, teachers: { some: { id: user.id } } },\n });\n if (!isTeacher) {\n return res.status(403).json({ error: 'Not a teacher of this class' });\n }\n }\n\n // Verify channel access for private-class-* channels\n // if (channel_name.startsWith('private-class-')) {\n // const classId = channel_name.replace('private-class-', '');\n // const isMember = await prisma.class.findFirst({\n // where: {\n // id: classId,\n // OR: [\n // { students: { some: { id: user.id } } },\n // { teachers: { some: { id: user.id } } },\n // ],\n // },\n // });\n // if (!isMember) {\n // return res.status(403).json({ error: 'Not a member of this class' });\n // }\n // }\n\n if (channel_name.startsWith('presence-')) {\n const authResponse = pusher.authorizeChannel(socket_id, channel_name, {\n user_id: user.id,\n user_info: { username: user.username },\n });\n return res.json(authResponse);\n }\n\n const authResponse = pusher.authorizeChannel(socket_id, channel_name);\n return res.json(authResponse);\n } catch (error) {\n logger.error('Pusher auth error', { error });\n return res.status(500).json({ error: 'Authentication failed' });\n }\n});\n\n// Setup Socket.IO\nconst io = new Server(httpServer, {\n cors: {\n origin: [\n 'http://localhost:3000', // Frontend development server\n 'http://localhost:3001', // Server port\n 'http://127.0.0.1:3000', // Alternative localhost\n 'http://127.0.0.1:3001', // Alternative localhost\n 'https://www.studious.sh', // Production frontend\n 'https://studious.sh', // Production frontend (without www)\n env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'\n ],\n methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n credentials: true,\n allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Access-Control-Allow-Origin', 'x-user']\n },\n transports: ['websocket', 'polling'],\n pingTimeout: 60000,\n pingInterval: 25000,\n connectTimeout: 45000,\n path: '/socket.io/',\n allowEIO3: true\n});\n\n// Add server-level logging\nio.engine.on('connection_error', (err: Error) => {\n logger.error('Socket connection error', { error: err.message });\n});\n\n// Setup socket handlers\nsetupSocketHandlers(io);\n\n// File serving endpoint for secure file access\napp.get('/api/files/:fileId', async (req, res) => {\n try {\n const fileId = decodeURIComponent(req.params.fileId);\n // console.log('File request:', { fileId, originalPath: req.params.fileId });\n \n // Get user from request headers\n const userHeader = req.headers['x-user'];\n if (!userHeader) {\n return res.status(401).json({ error: 'Authentication required' });\n }\n\n const token = typeof userHeader === 'string' ? userHeader : userHeader[0];\n\n // Find user by session token\n const user = await prisma.user.findFirst({\n where: {\n sessions: {\n some: {\n id: token\n }\n }\n },\n select: {\n id: true,\n username: true,\n }\n });\n\n if (!user) {\n return res.status(401).json({ error: 'Invalid or expired session' });\n }\n\n // Find file in database by path\n const fileRecord = await prisma.file.findFirst({\n where: { id: fileId },\n include: {\n user: true,\n assignment: {\n include: {\n class: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n },\n submission: {\n include: {\n student: true,\n assignment: {\n include: {\n class: {\n include: {\n teachers: true\n }\n }\n }\n }\n }\n },\n annotations: {\n include: {\n student: true,\n assignment: {\n include: {\n class: {\n include: {\n teachers: true\n }\n }\n }\n }\n }\n },\n folder: {\n include: {\n class: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n },\n classDraft: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n });\n\n if (!fileRecord) {\n return res.status(404).json({ error: 'File not found in database' });\n }\n\n // Check if user has permission to access this file\n let hasPermission = false;\n\n // Check if user created the file\n if (fileRecord.userId === user.id) {\n hasPermission = true;\n }\n\n // Check if file is related to a class where user is a member\n if (!hasPermission) {\n // Check assignment files\n if (fileRecord.assignment?.class) {\n const classData = fileRecord.assignment.class;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n\n if (!hasPermission && fileRecord.annotations) {\n const annotation = fileRecord.annotations;\n if (annotation.studentId === user.id) {\n hasPermission = true;\n } else if (annotation.assignment?.class?.teachers.some(teacher => teacher.id === user.id)) {\n hasPermission = true;\n }\n }\n\n // Check submission files (student can access their own submissions, teachers can access all submissions in their class)\n if (!hasPermission && fileRecord.submission) {\n const submission = fileRecord.submission;\n if (submission.studentId === user.id) {\n hasPermission = true; // Student accessing their own submission\n } else if (submission.assignment?.class?.teachers.some(teacher => teacher.id === user.id)) {\n hasPermission = true; // Teacher accessing submission in their class\n }\n }\n\n // Check folder files\n if (!hasPermission && fileRecord.folder?.class) {\n const classData = fileRecord.folder.class;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n\n // Check class draft files\n if (!hasPermission && fileRecord.classDraft) {\n const classData = fileRecord.classDraft;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n }\n\n if (!hasPermission) {\n return res.status(403).json({ error: 'Access denied - insufficient permissions' });\n }\n \n const filePath = fileRecord.path;\n \n // Get file from Google Cloud Storage\n const file = bucket.file(filePath);\n const [exists] = await file.exists();\n \n if (!exists) {\n return res.status(404).json({ error: 'File not found in storage', filePath });\n }\n \n // Get file metadata\n const [metadata] = await file.getMetadata();\n \n // Set appropriate headers\n res.set({\n 'Content-Type': metadata.contentType || 'application/octet-stream',\n 'Content-Length': metadata.size,\n 'Cache-Control': 'public, max-age=31536000', // 1 year cache\n 'ETag': metadata.etag,\n });\n \n // Stream file to response\n const stream = file.createReadStream();\n stream.pipe(res);\n \n stream.on('error', (error) => {\n logger.error('Error streaming file:', {error});\n if (!res.headersSent) {\n res.status(500).json({ error: 'Error streaming file' });\n }\n });\n \n } catch (error) {\n logger.error('Error serving file:', {error});\n res.status(500).json({ error: 'Internal server error' });\n }\n});\n\napp.use('/trpc/auth.login', authLimiter);\napp.use('/trpc/auth.register', authLimiter);\n\n// File upload endpoint for secure file uploads (supports both POST and PUT)\napp.post('/api/upload/:filePath', uploadLimiter, async (req, res) => {\n handleFileUpload(req, res);\n});\n\napp.put('/api/upload/:filePath', uploadLimiter, async (req, res) => {\n handleFileUpload(req, res);\n});\n\nfunction handleFileUpload(req: any, res: any) {\n try {\n const filePath = decodeURIComponent(req.params.filePath);\n \n // Set CORS headers for upload endpoint\n const origin = req.headers.origin;\n const allowedOrigins = [\n 'http://localhost:3000',\n 'http://localhost:3001', \n 'http://127.0.0.1:3000',\n 'http://127.0.0.1:3001',\n 'https://www.studious.sh', // Production frontend\n 'https://studious.sh', // Production frontend (without www)\n env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'\n ];\n \n if (origin && allowedOrigins.includes(origin)) {\n res.header('Access-Control-Allow-Origin', origin);\n } else {\n res.header('Access-Control-Allow-Origin', 'http://localhost:3000');\n }\n \n res.header('Access-Control-Allow-Credentials', 'true');\n \n // Get content type from headers\n const contentType = req.headers['content-type'] || 'application/octet-stream';\n \n // Create a new file in the bucket\n const file = bucket.file(filePath);\n \n // Create a write stream to Google Cloud Storage\n const writeStream = file.createWriteStream({\n metadata: {\n contentType,\n },\n });\n \n // Handle stream events\n writeStream.on('error', (error) => {\n logger.error('Error uploading file:', {error});\n if (!res.headersSent) {\n res.status(500).json({ error: 'Error uploading file' });\n }\n });\n \n writeStream.on('finish', () => {\n res.status(200).json({ \n success: true, \n filePath,\n message: 'File uploaded successfully' \n });\n });\n \n // Pipe the request body to the write stream\n req.pipe(writeStream);\n \n } catch (error) {\n logger.error('Error handling file upload:', {error});\n res.status(500).json({ error: 'Internal server error' });\n }\n}\n\n// Create caller\nconst createCaller = createCallerFactory(appRouter);\n\n// Setup tRPC middleware\napp.use(\n '/trpc',\n createExpressMiddleware({\n router: appRouter,\n createContext: async ({ req, res }: { req: Request; res: Response }) => {\n return createTRPCContext({ req, res });\n },\n })\n);\n\n// IMPORTANT: Sentry error handler must be added AFTER all other middleware and routes\n// but BEFORE any other error handlers\nSentry.setupExpressErrorHandler(app);\n\n// app.use(function onError(err, req, res, next) {\n// // The error id is attached to `res.sentry` to be returned\n// // and optionally displayed to the user for support.\n// res.statusCode = 500;\n// res.end(res.sentry + \"\\n\");\n// });\n\n\nconst PORT = env.PORT || 3001;\n\nconnectRedis().then(() => {\n httpServer.listen(PORT, () => {\n logger.info(`Server running on port ${PORT}`, {\n port: PORT,\n services: ['tRPC', 'Socket.IO', env.REDIS_URL ? 'Redis' : null].filter(Boolean),\n });\n });\n}); \n\n// log all env variables\nlogger.info('Configurations', {\n NODE_ENV: env.NODE_ENV,\n PORT: env.PORT,\n NEXT_PUBLIC_APP_URL: env.NEXT_PUBLIC_APP_URL,\n LOG_MODE: env.LOG_MODE,\n});\n\n// Log CORS configuration\nlogger.info('CORS Configuration', {\n allowedOrigins: [\n 'http://localhost:3000',\n 'http://localhost:3001', \n 'http://127.0.0.1:3000',\n 'http://127.0.0.1:3001',\n env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'\n ]\n});\n\nconst gracefulShutdown = (signal: string) => {\n logger.info(`Received ${signal}, shutting down gracefully`);\n \n httpServer.close(() => {\n logger.info('HTTP server closed');\n \n io.close(() => {\n logger.info('Socket.IO server closed');\n\n disconnectRedis().then(() =>\n prisma.$disconnect().then(() => {\n logger.info('Database connections closed');\n process.exit(0);\n }).catch((err) => {\n logger.error('Error disconnecting from database', { error: err });\n process.exit(1);\n })\n );\n });\n });\n \n // Force shutdown after 10 seconds\n setTimeout(() => {\n logger.error('Forced shutdown after timeout');\n process.exit(1);\n }, 10000);\n};\n\nprocess.on('SIGTERM', () => gracefulShutdown('SIGTERM'));\nprocess.on('SIGINT', () => gracefulShutdown('SIGINT'));"],"names":[],"mappings":";;AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEpG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,WAAW,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAGpC,OAAO,iBAAiB,CAAC;AAGzB,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC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
+ {"version":3,"file":"index.js","sources":["index.ts"],"sourceRoot":"/","sourcesContent":["import express from 'express';\nimport type { Request, Response } from 'express';\nimport { createServer } from 'http';\nimport { Server } from 'socket.io';\nimport cors from 'cors';\nimport dotenv from 'dotenv';\nimport { createExpressMiddleware } from '@trpc/server/adapters/express';\nimport { appRouter } from './routers/_app.js';\nimport { createTRPCContext, createCallerFactory } from './trpc.js';\nimport { logger } from './utils/logger.js';\nimport { setupSocketHandlers } from './socket/handlers.js';\nimport { bucket } from './lib/googleCloudStorage.js';\nimport { prisma } from './lib/prisma.js';\nimport { pusher } from './lib/pusher.js';\nimport { connectRedis, disconnectRedis } from './lib/redis.js';\nimport { createCorsOriginMatcher } from './lib/config/cors.js';\n\nimport { authLimiter, generalLimiter, helmetConfig, uploadLimiter } from './middleware/security.js';\n\nimport * as Sentry from \"@sentry/node\";\nimport { env } from './lib/config/env.js';\nimport compression from 'compression';\nimport { v4 as uuidv4 } from 'uuid';\n\n\nimport \"./instrument.js\";\nimport { openAIClient } from './utils/inference.js';\n\nconst app = express();\n\napp.set('trust proxy', 1);\n\napp.use(helmetConfig);\napp.use(compression());\napp.use(express.json());\napp.use(express.urlencoded({ extended: true }));\n\napp.use((req, res, next) => {\n const requestId = uuidv4();\n res.setHeader('X-Request-ID', requestId);\n next();\n});\n\nconst { allowedOrigins, allowedOriginPatterns, isAllowedOrigin } = createCorsOriginMatcher(env);\n\n// CORS middleware\napp.use(cors({\n origin: (origin, callback) => {\n if (!origin || isAllowedOrigin(origin)) {\n callback(null, true);\n return;\n }\n\n logger.warn('CORS origin rejected', { origin, allowedOrigins });\n callback(null, false);\n },\n credentials: true,\n methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'x-user'],\n preflightContinue: false,\n optionsSuccessStatus: 204,\n}));\n\napp.use(generalLimiter);\n\n// CORS debugging middleware\napp.use((req, res, next) => {\n if (req.method === 'OPTIONS' || req.path.includes('trpc')) {\n logger.info('CORS Request', {\n method: req.method,\n path: req.path,\n origin: req.headers.origin,\n userAgent: req.headers['user-agent']\n });\n }\n next();\n});\n\n// Response time logging middleware\napp.use((req, res, next) => {\n const start = Date.now();\n res.on('finish', () => {\n const duration = Date.now() - start;\n logger.info('Request completed', {\n method: req.method,\n path: req.path,\n statusCode: res.statusCode,\n duration: `${duration}ms`\n });\n });\n next();\n});\n\n// app.use(\"/panel\", async (_, res) => {\n// if (env.NODE_ENV !== \"development\") {\n// return res.status(404).send(\"Not Found\");\n// }\n\n// // Dynamically import renderTrpcPanel only in development\n// const { renderTrpcPanel } = await import(\"trpc-ui\");\n\n// return res.send(\n// renderTrpcPanel(appRouter, {\n// url: \"/trpc\", // Base url of your trpc server\n// meta: {\n// title: \"Studious Backend\",\n// description:\n// \"This is the backend for the Studious application.\",\n// },\n// })\n// );\n// });\n\n\n// Create HTTP server\nconst httpServer = createServer(app);\n\napp.get('/health', async (req, res) => {\n\n try {\n // Check database connectivity\n await prisma.$queryRaw`SELECT 1`;\n \n res.status(200).json({ \n status: 'OK',\n timestamp: new Date().toISOString(),\n uptime: process.uptime(),\n database: 'connected'\n });\n } catch (error) {\n res.status(503).json({ \n status: 'ERROR',\n database: 'disconnected',\n error: error instanceof Error ? error.message : 'Unknown error'\n });\n }\n});\n\n// Pusher channel auth (for private-* and presence-* channels)\n// Token from: x-user header, or cookie (same-origin requests send cookies automatically)\napp.post('/api/pusher/auth', async (req, res) => {\n try {\n let token = req.headers['x-user'] as string | undefined;\n if (!token && req.headers.cookie) {\n const cookieName = env.PUSHER_AUTH_COOKIE_NAME || 'token';\n const match = req.headers.cookie.match(new RegExp(`${cookieName}=([^;]+)`));\n token = match?.[1]?.trim();\n }\n const { socket_id, channel_name } = req.body as { socket_id?: string; channel_name?: string };\n\n if (!socket_id || !channel_name) {\n return res.status(400).json({ error: 'socket_id and channel_name required' });\n }\n\n if (!token) {\n return res.status(401).json({ error: 'Authentication required' });\n }\n\n const user = await prisma.user.findFirst({\n where: { sessions: { some: { id: token } } },\n select: { id: true, username: true },\n });\n\n if (!user) {\n return res.status(401).json({ error: 'Invalid or expired session' });\n }\n\n // Verify channel access for private-conversation-* channels\n if (channel_name.startsWith('private-conversation-')) {\n const conversationId = channel_name.replace('private-conversation-', '');\n const member = await prisma.conversationMember.findFirst({\n where: { conversationId, userId: user.id },\n });\n \n if (!member) {\n return res.status(403).json({ error: 'Not a member of this conversation' });\n }\n }\n\n if (channel_name.startsWith('private-worksheet-')) {\n const worksheetId = channel_name.replace('private-worksheet-', '');\n const worksheet = await prisma.studentWorksheetResponse.findFirst({\n where: { id: worksheetId, OR: [\n { studentId: user.id },\n { submission: { assignment: { class: { teachers: { some: { id: user.id } } } } } },\n ] },\n });\n if (!worksheet) {\n return res.status(403).json({ error: 'No access to this worksheet' });\n }\n }\n\n if (channel_name.startsWith('private-teacher-')) {\n const classId = channel_name.replace('private-teacher-', '');\n const isTeacher = await prisma.class.findFirst({\n where: { id: classId, teachers: { some: { id: user.id } } },\n });\n if (!isTeacher) {\n return res.status(403).json({ error: 'Not a teacher of this class' });\n }\n }\n\n // Verify channel access for private-class-* channels\n // if (channel_name.startsWith('private-class-')) {\n // const classId = channel_name.replace('private-class-', '');\n // const isMember = await prisma.class.findFirst({\n // where: {\n // id: classId,\n // OR: [\n // { students: { some: { id: user.id } } },\n // { teachers: { some: { id: user.id } } },\n // ],\n // },\n // });\n // if (!isMember) {\n // return res.status(403).json({ error: 'Not a member of this class' });\n // }\n // }\n\n if (channel_name.startsWith('presence-')) {\n const authResponse = pusher.authorizeChannel(socket_id, channel_name, {\n user_id: user.id,\n user_info: { username: user.username },\n });\n return res.json(authResponse);\n }\n\n const authResponse = pusher.authorizeChannel(socket_id, channel_name);\n return res.json(authResponse);\n } catch (error) {\n logger.error('Pusher auth error', { error });\n return res.status(500).json({ error: 'Authentication failed' });\n }\n});\n\n// Setup Socket.IO\nconst io = new Server(httpServer, {\n cors: {\n origin: (origin, callback) => {\n callback(null, !origin || isAllowedOrigin(origin));\n },\n methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],\n credentials: true,\n allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Access-Control-Allow-Origin', 'x-user']\n },\n transports: ['websocket', 'polling'],\n pingTimeout: 60000,\n pingInterval: 25000,\n connectTimeout: 45000,\n path: '/socket.io/',\n allowEIO3: true\n});\n\n// Add server-level logging\nio.engine.on('connection_error', (err: Error) => {\n logger.error('Socket connection error', { error: err.message });\n});\n\n// Setup socket handlers\nsetupSocketHandlers(io);\n\n// File serving endpoint for secure file access\napp.get('/api/files/:fileId', async (req, res) => {\n try {\n const fileId = decodeURIComponent(req.params.fileId);\n // console.log('File request:', { fileId, originalPath: req.params.fileId });\n \n // Get user from request headers\n const userHeader = req.headers['x-user'];\n if (!userHeader) {\n return res.status(401).json({ error: 'Authentication required' });\n }\n\n const token = typeof userHeader === 'string' ? userHeader : userHeader[0];\n\n // Find user by session token\n const user = await prisma.user.findFirst({\n where: {\n sessions: {\n some: {\n id: token\n }\n }\n },\n select: {\n id: true,\n username: true,\n }\n });\n\n if (!user) {\n return res.status(401).json({ error: 'Invalid or expired session' });\n }\n\n // Find file in database by path\n const fileRecord = await prisma.file.findFirst({\n where: { id: fileId },\n include: {\n user: true,\n assignment: {\n include: {\n class: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n },\n submission: {\n include: {\n student: true,\n assignment: {\n include: {\n class: {\n include: {\n teachers: true\n }\n }\n }\n }\n }\n },\n annotations: {\n include: {\n student: true,\n assignment: {\n include: {\n class: {\n include: {\n teachers: true\n }\n }\n }\n }\n }\n },\n folder: {\n include: {\n class: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n },\n classDraft: {\n include: {\n students: true,\n teachers: true\n }\n }\n }\n });\n\n if (!fileRecord) {\n return res.status(404).json({ error: 'File not found in database' });\n }\n\n // Check if user has permission to access this file\n let hasPermission = false;\n\n // Check if user created the file\n if (fileRecord.userId === user.id) {\n hasPermission = true;\n }\n\n // Check if file is related to a class where user is a member\n if (!hasPermission) {\n // Check assignment files\n if (fileRecord.assignment?.class) {\n const classData = fileRecord.assignment.class;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n\n if (!hasPermission && fileRecord.annotations) {\n const annotation = fileRecord.annotations;\n if (annotation.studentId === user.id) {\n hasPermission = true;\n } else if (annotation.assignment?.class?.teachers.some(teacher => teacher.id === user.id)) {\n hasPermission = true;\n }\n }\n\n // Check submission files (student can access their own submissions, teachers can access all submissions in their class)\n if (!hasPermission && fileRecord.submission) {\n const submission = fileRecord.submission;\n if (submission.studentId === user.id) {\n hasPermission = true; // Student accessing their own submission\n } else if (submission.assignment?.class?.teachers.some(teacher => teacher.id === user.id)) {\n hasPermission = true; // Teacher accessing submission in their class\n }\n }\n\n // Check folder files\n if (!hasPermission && fileRecord.folder?.class) {\n const classData = fileRecord.folder.class;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n\n // Check class draft files\n if (!hasPermission && fileRecord.classDraft) {\n const classData = fileRecord.classDraft;\n const isStudent = classData.students.some(student => student.id === user.id);\n const isTeacher = classData.teachers.some(teacher => teacher.id === user.id);\n if (isStudent || isTeacher) {\n hasPermission = true;\n }\n }\n }\n\n if (!hasPermission) {\n return res.status(403).json({ error: 'Access denied - insufficient permissions' });\n }\n \n const filePath = fileRecord.path;\n \n // Get file from Google Cloud Storage\n const file = bucket.file(filePath);\n const [exists] = await file.exists();\n \n if (!exists) {\n return res.status(404).json({ error: 'File not found in storage', filePath });\n }\n \n // Get file metadata\n const [metadata] = await file.getMetadata();\n \n // Set appropriate headers\n res.set({\n 'Content-Type': metadata.contentType || 'application/octet-stream',\n 'Content-Length': metadata.size,\n 'Cache-Control': 'public, max-age=31536000', // 1 year cache\n 'ETag': metadata.etag,\n });\n \n // Stream file to response\n const stream = file.createReadStream();\n stream.pipe(res);\n \n stream.on('error', (error) => {\n logger.error('Error streaming file:', {error});\n if (!res.headersSent) {\n res.status(500).json({ error: 'Error streaming file' });\n }\n });\n \n } catch (error) {\n logger.error('Error serving file:', {error});\n res.status(500).json({ error: 'Internal server error' });\n }\n});\n\napp.use('/trpc/auth.login', authLimiter);\napp.use('/trpc/auth.register', authLimiter);\n\n// File upload endpoint for secure file uploads (supports both POST and PUT)\napp.post('/api/upload/:filePath', uploadLimiter, async (req, res) => {\n handleFileUpload(req, res);\n});\n\napp.put('/api/upload/:filePath', uploadLimiter, async (req, res) => {\n handleFileUpload(req, res);\n});\n\nfunction handleFileUpload(req: any, res: any) {\n try {\n const filePath = decodeURIComponent(req.params.filePath);\n \n // Set CORS headers for upload endpoint\n const origin = req.headers.origin;\n \n if (origin && !isAllowedOrigin(origin)) {\n return res.status(403).json({ error: 'Origin not allowed' });\n }\n\n if (origin) {\n res.header('Access-Control-Allow-Origin', origin);\n }\n \n res.header('Access-Control-Allow-Credentials', 'true');\n \n // Get content type from headers\n const contentType = req.headers['content-type'] || 'application/octet-stream';\n \n // Create a new file in the bucket\n const file = bucket.file(filePath);\n \n // Create a write stream to Google Cloud Storage\n const writeStream = file.createWriteStream({\n metadata: {\n contentType,\n },\n });\n \n // Handle stream events\n writeStream.on('error', (error) => {\n logger.error('Error uploading file:', {error});\n if (!res.headersSent) {\n res.status(500).json({ error: 'Error uploading file' });\n }\n });\n \n writeStream.on('finish', () => {\n res.status(200).json({ \n success: true, \n filePath,\n message: 'File uploaded successfully' \n });\n });\n \n // Pipe the request body to the write stream\n req.pipe(writeStream);\n \n } catch (error) {\n logger.error('Error handling file upload:', {error});\n res.status(500).json({ error: 'Internal server error' });\n }\n}\n\n// Create caller\nconst createCaller = createCallerFactory(appRouter);\n\n// Fallback OPTIONS handler — prevents tRPC from rejecting preflight requests\n// when the cors middleware passes through without ending the response.\napp.options('/trpc/*', (req, res) => {\n const origin = req.headers.origin;\n if (origin && isAllowedOrigin(origin)) {\n res.setHeader('Access-Control-Allow-Origin', origin);\n res.setHeader('Access-Control-Allow-Credentials', 'true');\n res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');\n res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, x-user');\n }\n res.sendStatus(204);\n});\n\n// Setup tRPC middleware\napp.use(\n '/trpc',\n createExpressMiddleware({\n router: appRouter,\n createContext: async ({ req, res }: { req: Request; res: Response }) => {\n return createTRPCContext({ req, res });\n },\n })\n);\n\n// IMPORTANT: Sentry error handler must be added AFTER all other middleware and routes\n// but BEFORE any other error handlers\nSentry.setupExpressErrorHandler(app);\n\n// app.use(function onError(err, req, res, next) {\n// // The error id is attached to `res.sentry` to be returned\n// // and optionally displayed to the user for support.\n// res.statusCode = 500;\n// res.end(res.sentry + \"\\n\");\n// });\n\n\nconst PORT = env.PORT || 3001;\n\nconnectRedis().then(() => {\n httpServer.listen(PORT, () => {\n logger.info(`Server running on port ${PORT}`, {\n port: PORT,\n services: ['tRPC', 'Socket.IO', env.REDIS_URL ? 'Redis' : null].filter(Boolean),\n });\n });\n}); \n\n// log all env variables\nlogger.info('Configurations', {\n NODE_ENV: env.NODE_ENV,\n PORT: env.PORT,\n NEXT_PUBLIC_APP_URL: env.NEXT_PUBLIC_APP_URL,\n LOG_MODE: env.LOG_MODE,\n});\n\n// Log CORS configuration\nlogger.info('CORS Configuration', {\n allowedOrigins,\n allowedOriginPatterns: allowedOriginPatterns.map((pattern) => pattern.source),\n});\n\nconst gracefulShutdown = (signal: string) => {\n logger.info(`Received ${signal}, shutting down gracefully`);\n \n httpServer.close(() => {\n logger.info('HTTP server closed');\n \n io.close(() => {\n logger.info('Socket.IO server closed');\n\n disconnectRedis().then(() =>\n prisma.$disconnect().then(() => {\n logger.info('Database connections closed');\n process.exit(0);\n }).catch((err) => {\n logger.error('Error disconnecting from database', { error: err });\n process.exit(1);\n })\n );\n });\n });\n \n // Force shutdown after 10 seconds\n setTimeout(() => {\n logger.error('Forced shutdown after timeout');\n process.exit(1);\n }, 10000);\n};\n\nprocess.on('SIGTERM', () => gracefulShutdown('SIGTERM'));\nprocess.on('SIGINT', () => gracefulShutdown('SIGINT'));\n"],"names":[],"mappings":";;AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEpG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,WAAW,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAGpC,OAAO,iBAAiB,C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ACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,kCAAkC;IAClC,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;QAC9C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,EAAE,KAAK,CAAC,CAAC;AACZ,CAAC,CAAC;AAEF,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC;AACzD,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC","debug_id":"0a09f7dd-016d-5f2f-bdf3-5a5a294b50cb"}
@@ -0,0 +1,16 @@
1
+ type CorsEnv = {
2
+ NODE_ENV?: 'development' | 'production' | 'test';
3
+ NEXT_PUBLIC_APP_URL?: string;
4
+ CORS_ALLOWED_ORIGINS?: string;
5
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string;
6
+ };
7
+ export declare const parseCorsOrigins: (origins?: string) => string[];
8
+ export declare const parseCorsOriginPatterns: (patterns?: string) => RegExp[];
9
+ export declare const isValidCorsOriginPatternList: (patterns?: string) => boolean;
10
+ export declare const createCorsOriginMatcher: (env: CorsEnv) => {
11
+ allowedOrigins: string[];
12
+ allowedOriginPatterns: RegExp[];
13
+ isAllowedOrigin: (origin?: string) => boolean;
14
+ };
15
+ export {};
16
+ //# sourceMappingURL=cors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cors.d.ts","sourceRoot":"/","sources":["lib/config/cors.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG;IACb,QAAQ,CAAC,EAAE,aAAa,GAAG,YAAY,GAAG,MAAM,CAAC;IACjD,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,4BAA4B,CAAC,EAAE,MAAM,CAAC;CACvC,CAAC;AAqCF,eAAO,MAAM,gBAAgB,GAAI,UAAU,MAAM,aACX,CAAC;AAiBvC,eAAO,MAAM,uBAAuB,GAAI,WAAW,MAAM,aAC6B,CAAC;AAEvF,eAAO,MAAM,4BAA4B,GAAI,WAAW,MAAM,YAO7D,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,KAAK,OAAO;;;+BAShB,MAAM;CAczC,CAAC"}
@@ -0,0 +1,75 @@
1
+
2
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="2499986f-b37e-5e23-a023-556de6317b0f")}catch(e){}}();
3
+ const productionOrigins = [
4
+ 'https://www.studious.sh',
5
+ 'https://studious.sh',
6
+ 'https://dev.studious.sh',
7
+ 'https://www.dev.studious.sh',
8
+ ];
9
+ const nonProductionOrigins = [
10
+ 'http://localhost:3000',
11
+ 'http://localhost:3001',
12
+ 'http://127.0.0.1:3000',
13
+ 'http://127.0.0.1:3001',
14
+ ];
15
+ const MAX_PATTERN_LENGTH = 200;
16
+ // This heuristic only targets common nested-quantifier cases like `(a+)+`.
17
+ // It does not catch every risky construct (for example `.*.*` or deep alternation nesting),
18
+ // so `parseCorsOriginPatterns` should still be limited to operator-controlled, anchored patterns.
19
+ const SUSPICIOUS_QUANTIFIER_PATTERN = /(\([^)]*[+*{][^)]*\)|\[[^\]]+[+*{][^\]]*\])[+*{]/;
20
+ const parseList = (value) => value
21
+ ?.split(',')
22
+ .map((item) => item.trim())
23
+ .filter(Boolean) ?? [];
24
+ const isDefined = (value) => Boolean(value);
25
+ const toCorsOrigin = (value) => {
26
+ try {
27
+ return new URL(value).origin;
28
+ }
29
+ catch {
30
+ return value.replace(/\/+$/, '');
31
+ }
32
+ };
33
+ export const parseCorsOrigins = (origins) => parseList(origins).map(toCorsOrigin);
34
+ const validateCorsOriginPattern = (pattern) => {
35
+ if (pattern.length > MAX_PATTERN_LENGTH) {
36
+ throw new Error(`CORS pattern exceeds maximum length of ${MAX_PATTERN_LENGTH}`);
37
+ }
38
+ // Prefer short, anchored patterns and avoid other complex regex features even if they pass this check.
39
+ if (SUSPICIOUS_QUANTIFIER_PATTERN.test(pattern)) {
40
+ throw new Error("CORS pattern contains nested or ambiguous quantifiers");
41
+ }
42
+ return pattern;
43
+ };
44
+ // Patterns are operator-controlled and matched against bounded URL origins, so ReDoS risk is low.
45
+ // Syntax validation does not guarantee safety; prefer anchored patterns and avoid nested quantifiers.
46
+ export const parseCorsOriginPatterns = (patterns) => parseList(patterns).map((pattern) => new RegExp(validateCorsOriginPattern(pattern)));
47
+ export const isValidCorsOriginPatternList = (patterns) => {
48
+ try {
49
+ parseCorsOriginPatterns(patterns);
50
+ return true;
51
+ }
52
+ catch {
53
+ return false;
54
+ }
55
+ };
56
+ export const createCorsOriginMatcher = (env) => {
57
+ const allowedOrigins = Array.from(new Set([
58
+ ...(env.NODE_ENV === 'production' ? productionOrigins : nonProductionOrigins),
59
+ env.NEXT_PUBLIC_APP_URL ? toCorsOrigin(env.NEXT_PUBLIC_APP_URL) : undefined,
60
+ ...parseCorsOrigins(env.CORS_ALLOWED_ORIGINS),
61
+ ].filter(isDefined)));
62
+ const allowedOriginPatterns = parseCorsOriginPatterns(env.CORS_ALLOWED_ORIGIN_PATTERNS);
63
+ const isAllowedOrigin = (origin) => {
64
+ const corsOrigin = origin ? toCorsOrigin(origin) : undefined;
65
+ return Boolean(corsOrigin && (allowedOrigins.includes(corsOrigin) ||
66
+ allowedOriginPatterns.some((pattern) => pattern.test(corsOrigin))));
67
+ };
68
+ return {
69
+ allowedOrigins,
70
+ allowedOriginPatterns,
71
+ isAllowedOrigin,
72
+ };
73
+ };
74
+ //# sourceMappingURL=cors.js.map
75
+ //# debugId=2499986f-b37e-5e23-a023-556de6317b0f
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cors.js","sources":["lib/config/cors.ts"],"sourceRoot":"/","sourcesContent":["type CorsEnv = {\n NODE_ENV?: 'development' | 'production' | 'test';\n NEXT_PUBLIC_APP_URL?: string;\n CORS_ALLOWED_ORIGINS?: string;\n CORS_ALLOWED_ORIGIN_PATTERNS?: string;\n};\n\nconst productionOrigins = [\n 'https://www.studious.sh',\n 'https://studious.sh',\n 'https://dev.studious.sh',\n 'https://www.dev.studious.sh',\n];\n\nconst nonProductionOrigins = [\n 'http://localhost:3000',\n 'http://localhost:3001',\n 'http://127.0.0.1:3000',\n 'http://127.0.0.1:3001',\n];\nconst MAX_PATTERN_LENGTH = 200;\n// This heuristic only targets common nested-quantifier cases like `(a+)+`.\n// It does not catch every risky construct (for example `.*.*` or deep alternation nesting),\n// so `parseCorsOriginPatterns` should still be limited to operator-controlled, anchored patterns.\nconst SUSPICIOUS_QUANTIFIER_PATTERN = /(\\([^)]*[+*{][^)]*\\)|\\[[^\\]]+[+*{][^\\]]*\\])[+*{]/;\n\nconst parseList = (value?: string) =>\n value\n ?.split(',')\n .map((item) => item.trim())\n .filter(Boolean) ?? [];\n\nconst isDefined = <T>(value: T | undefined | null): value is T => Boolean(value);\n\nconst toCorsOrigin = (value: string) => {\n try {\n return new URL(value).origin;\n } catch {\n return value.replace(/\\/+$/, '');\n }\n};\n\nexport const parseCorsOrigins = (origins?: string) =>\n parseList(origins).map(toCorsOrigin);\n\nconst validateCorsOriginPattern = (pattern: string) => {\n if (pattern.length > MAX_PATTERN_LENGTH) {\n throw new Error(`CORS pattern exceeds maximum length of ${MAX_PATTERN_LENGTH}`);\n }\n\n // Prefer short, anchored patterns and avoid other complex regex features even if they pass this check.\n if (SUSPICIOUS_QUANTIFIER_PATTERN.test(pattern)) {\n throw new Error(\"CORS pattern contains nested or ambiguous quantifiers\");\n }\n\n return pattern;\n};\n\n// Patterns are operator-controlled and matched against bounded URL origins, so ReDoS risk is low.\n// Syntax validation does not guarantee safety; prefer anchored patterns and avoid nested quantifiers.\nexport const parseCorsOriginPatterns = (patterns?: string) =>\n parseList(patterns).map((pattern) => new RegExp(validateCorsOriginPattern(pattern)));\n\nexport const isValidCorsOriginPatternList = (patterns?: string) => {\n try {\n parseCorsOriginPatterns(patterns);\n return true;\n } catch {\n return false;\n }\n};\n\nexport const createCorsOriginMatcher = (env: CorsEnv) => {\n const allowedOrigins = Array.from(new Set([\n ...(env.NODE_ENV === 'production' ? productionOrigins : nonProductionOrigins),\n env.NEXT_PUBLIC_APP_URL ? toCorsOrigin(env.NEXT_PUBLIC_APP_URL) : undefined,\n ...parseCorsOrigins(env.CORS_ALLOWED_ORIGINS),\n ].filter(isDefined)));\n\n const allowedOriginPatterns = parseCorsOriginPatterns(env.CORS_ALLOWED_ORIGIN_PATTERNS);\n\n const isAllowedOrigin = (origin?: string) => {\n const corsOrigin = origin ? toCorsOrigin(origin) : undefined;\n\n return Boolean(corsOrigin && (\n allowedOrigins.includes(corsOrigin) ||\n allowedOriginPatterns.some((pattern) => pattern.test(corsOrigin))\n ));\n };\n\n return {\n allowedOrigins,\n allowedOriginPatterns,\n isAllowedOrigin,\n };\n};\n"],"names":[],"mappings":";;AAOA,MAAM,iBAAiB,GAAG;IACxB,yBAAyB;IACzB,qBAAqB;IACrB,yBAAyB;IACzB,6BAA6B;CAC9B,CAAC;AAEF,MAAM,oBAAoB,GAAG;IAC3B,uBAAuB;IACvB,uBAAuB;IACvB,uBAAuB;IACvB,uBAAuB;CACxB,CAAC;AACF,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAC/B,2EAA2E;AAC3E,4FAA4F;AAC5F,kGAAkG;AAClG,MAAM,6BAA6B,GAAG,kDAAkD,CAAC;AAEzF,MAAM,SAAS,GAAG,CAAC,KAAc,EAAE,EAAE,CACnC,KAAK;IACH,EAAE,KAAK,CAAC,GAAG,CAAC;KACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;KAC1B,MAAM,C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<z.ZodString>;
8
+ CORS_ALLOWED_ORIGINS: z.ZodOptional<z.ZodString>;
9
+ CORS_ALLOWED_ORIGIN_PATTERNS: z.ZodEffects<z.ZodOptional<z.ZodString>, string | undefined, string | undefined>;
8
10
  BACKEND_URL: z.ZodDefault<z.ZodString>;
9
11
  SENTRY_DSN: z.ZodOptional<z.ZodString>;
10
12
  EMAIL_HOST: z.ZodString;
@@ -46,6 +48,8 @@ declare const envSchema: z.ZodObject<{
46
48
  PUSHER_KEY: string;
47
49
  PUSHER_SECRET: string;
48
50
  PUSHER_CLUSTER: string;
51
+ CORS_ALLOWED_ORIGINS?: string | undefined;
52
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string | undefined;
49
53
  SENTRY_DSN?: string | undefined;
50
54
  PUSHER_AUTH_COOKIE_NAME?: string | undefined;
51
55
  REDIS_URL?: string | undefined;
@@ -69,6 +73,8 @@ declare const envSchema: z.ZodObject<{
69
73
  LOG_MODE?: "normal" | "verbose" | "quiet" | undefined;
70
74
  PORT?: string | undefined;
71
75
  NEXT_PUBLIC_APP_URL?: string | undefined;
76
+ CORS_ALLOWED_ORIGINS?: string | undefined;
77
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string | undefined;
72
78
  BACKEND_URL?: string | undefined;
73
79
  SENTRY_DSN?: string | undefined;
74
80
  EMAIL_PORT?: string | undefined;
@@ -84,6 +90,8 @@ declare const envSchema: z.ZodObject<{
84
90
  DATABASE_URL: z.ZodString;
85
91
  } & {
86
92
  NEXT_PUBLIC_APP_URL: z.ZodDefault<z.ZodOptional<z.ZodString>>;
93
+ CORS_ALLOWED_ORIGINS: z.ZodOptional<z.ZodString>;
94
+ CORS_ALLOWED_ORIGIN_PATTERNS: z.ZodEffects<z.ZodOptional<z.ZodString>, string | undefined, string | undefined>;
87
95
  BACKEND_URL: z.ZodDefault<z.ZodOptional<z.ZodString>>;
88
96
  SENTRY_DSN: z.ZodOptional<z.ZodString>;
89
97
  EMAIL_HOST: z.ZodDefault<z.ZodOptional<z.ZodString>>;
@@ -125,6 +133,8 @@ declare const envSchema: z.ZodObject<{
125
133
  PUSHER_KEY: string;
126
134
  PUSHER_SECRET: string;
127
135
  PUSHER_CLUSTER: string;
136
+ CORS_ALLOWED_ORIGINS?: string | undefined;
137
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string | undefined;
128
138
  SENTRY_DSN?: string | undefined;
129
139
  PUSHER_AUTH_COOKIE_NAME?: string | undefined;
130
140
  REDIS_URL?: string | undefined;
@@ -137,6 +147,8 @@ declare const envSchema: z.ZodObject<{
137
147
  LOG_MODE?: "normal" | "verbose" | "quiet" | undefined;
138
148
  PORT?: string | undefined;
139
149
  NEXT_PUBLIC_APP_URL?: string | undefined;
150
+ CORS_ALLOWED_ORIGINS?: string | undefined;
151
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string | undefined;
140
152
  BACKEND_URL?: string | undefined;
141
153
  SENTRY_DSN?: string | undefined;
142
154
  EMAIL_HOST?: string | undefined;
@@ -178,6 +190,8 @@ export declare const env: {
178
190
  PUSHER_KEY: string;
179
191
  PUSHER_SECRET: string;
180
192
  PUSHER_CLUSTER: string;
193
+ CORS_ALLOWED_ORIGINS?: string | undefined;
194
+ CORS_ALLOWED_ORIGIN_PATTERNS?: string | undefined;
181
195
  SENTRY_DSN?: string | undefined;
182
196
  PUSHER_AUTH_COOKIE_NAME?: string | undefined;
183
197
  REDIS_URL?: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"env.d.ts","sourceRoot":"/","sources":["lib/config/env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAqFxB,QAAA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAmC,CAAC;AA2CnD,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;CAAgB,CAAC;AAGjC,MAAM,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAC"}
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"/","sources":["lib/config/env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgGxB,QAAA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAmC,CAAC;AA2CnD,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAAgB,CAAC;AAGjC,MAAM,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAC"}
@@ -1,9 +1,10 @@
1
1
 
2
- !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="6fe796c8-7781-5540-892a-a320464bfbae")}catch(e){}}();
2
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="2c09f0e4-0311-5ba3-bfea-895c59d8b314")}catch(e){}}();
3
3
  import { z } from 'zod';
4
4
  import dotenv from 'dotenv';
5
5
  import { resolve } from 'path';
6
6
  import { logger } from '../../utils/logger.js';
7
+ import { isValidCorsOriginPatternList } from './cors.js';
7
8
  // Determine which env file to load based on NODE_ENV
8
9
  const nodeEnv = process.env.NODE_ENV || 'development';
9
10
  const envFileMap = {
@@ -20,6 +21,8 @@ dotenv.config(); // Load .env first (base config)
20
21
  dotenv.config({ path: envPath, override: true }); // Override with env-specific
21
22
  const isTest = nodeEnv === 'test';
22
23
  const isProduction = nodeEnv === 'production';
24
+ const corsAllowedOriginsSchema = z.string().optional();
25
+ const corsAllowedOriginPatternsSchema = z.string().optional().refine(isValidCorsOriginPatternList, { message: 'CORS_ALLOWED_ORIGIN_PATTERNS must contain valid regex patterns' });
23
26
  // Base schema with required vars for all environments
24
27
  const baseSchema = z.object({
25
28
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
@@ -29,6 +32,8 @@ const baseSchema = z.object({
29
32
  // Production/development schema with all required vars
30
33
  const fullSchema = baseSchema.extend({
31
34
  NEXT_PUBLIC_APP_URL: z.string().url().default('http://localhost:3000'),
35
+ CORS_ALLOWED_ORIGINS: corsAllowedOriginsSchema,
36
+ CORS_ALLOWED_ORIGIN_PATTERNS: corsAllowedOriginPatternsSchema,
32
37
  BACKEND_URL: z.string().url().default('http://localhost:3001'),
33
38
  SENTRY_DSN: z.string().url().optional(),
34
39
  EMAIL_HOST: z.string().min(1, 'EMAIL_HOST is required'),
@@ -54,6 +59,8 @@ const fullSchema = baseSchema.extend({
54
59
  // Test schema - only require what's needed for tests
55
60
  const testSchema = baseSchema.extend({
56
61
  NEXT_PUBLIC_APP_URL: z.string().url().optional().default('http://localhost:3000'),
62
+ CORS_ALLOWED_ORIGINS: corsAllowedOriginsSchema,
63
+ CORS_ALLOWED_ORIGIN_PATTERNS: corsAllowedOriginPatternsSchema,
57
64
  BACKEND_URL: z.string().url().optional().default('http://localhost:3001'),
58
65
  SENTRY_DSN: z.string().url().optional(),
59
66
  EMAIL_HOST: z.string().optional().default('smtp.test.com'),
@@ -118,4 +125,4 @@ function validateEnv() {
118
125
  // Export validated environment variables
119
126
  export const env = validateEnv();
120
127
  //# sourceMappingURL=env.js.map
121
- //# debugId=6fe796c8-7781-5540-892a-a320464bfbae
128
+ //# debugId=2c09f0e4-0311-5ba3-bfea-895c59d8b314