Archnemix commited on
Commit
42cbdd3
·
verified ·
1 Parent(s): 9a8f7d3

Update src/index.js

Browse files
Files changed (1) hide show
  1. src/index.js +13 -8
src/index.js CHANGED
@@ -8,7 +8,6 @@ const REQUIRED_ENV = [
8
  'HF_BUCKET_NAME',
9
  'API_SECRET',
10
  ]
11
-
12
  for (const key of REQUIRED_ENV) {
13
  if (!process.env[key]) {
14
  console.error(`[startup] Missing required environment variable: ${key}`)
@@ -18,14 +17,20 @@ for (const key of REQUIRED_ENV) {
18
 
19
  const app = express()
20
 
21
- // Raw body for /interactions — must come first, before any JSON middleware.
22
- // type: '*/*' captures whatever Content-Type Discord sends.
23
- app.use('/interactions', express.raw({ type: '*/*' }))
24
-
25
- // JSON body for every other route — skip /interactions so the raw Buffer is preserved.
26
  app.use((req, res, next) => {
27
- if (req.path === '/interactions') return next()
28
- express.json()(req, res, next)
 
 
 
 
 
 
 
 
29
  })
30
 
31
  app.use('/', router)
 
8
  'HF_BUCKET_NAME',
9
  'API_SECRET',
10
  ]
 
11
  for (const key of REQUIRED_ENV) {
12
  if (!process.env[key]) {
13
  console.error(`[startup] Missing required environment variable: ${key}`)
 
17
 
18
  const app = express()
19
 
20
+ // Custom middleware — must come before the router.
21
+ // Every request gets req.rawBody as the original string.
22
+ // Non-/interactions routes also get req.body as parsed JSON.
 
 
23
  app.use((req, res, next) => {
24
+ let data = ''
25
+ req.setEncoding('utf8')
26
+ req.on('data', (chunk) => { data += chunk })
27
+ req.on('end', () => {
28
+ req.rawBody = data
29
+ if (req.path !== '/interactions') {
30
+ try { req.body = JSON.parse(data) } catch { req.body = {} }
31
+ }
32
+ next()
33
+ })
34
  })
35
 
36
  app.use('/', router)