incognitolm commited on
Commit
de8743f
·
1 Parent(s): 17bb4ab
Files changed (2) hide show
  1. package.json +2 -1
  2. server/index.js +15 -9
package.json CHANGED
@@ -14,6 +14,7 @@
14
  "html-to-text": "^9.0.5",
15
  "ws": "^8.16.0",
16
  "openai": "^6.29.0",
17
- "node-fetch": "^3.3.2"
 
18
  }
19
  }
 
14
  "html-to-text": "^9.0.5",
15
  "ws": "^8.16.0",
16
  "openai": "^6.29.0",
17
+ "node-fetch": "^3.3.2",
18
+ "express-rate-limit": "8.3.2"
19
  }
20
  }
server/index.js CHANGED
@@ -125,27 +125,33 @@ function getMimeType(filePath){
125
  }
126
  }
127
 
128
- // --- Hybrid serving ---
129
  app.get('*', async (req,res)=>{
130
  if(req.path.startsWith('/api/')) return res.status(404).send('Not found');
131
 
132
- const dynamicPages = ['/index.html','/oauth-callback.html','/'];
133
- const filePath = dynamicPages.includes(req.path)?(req.path==='/'?'/index.html':req.path):req.path;
134
- const url = dynamicPages.includes(req.path)?`${CDN_BASE}@${latestSHA}${filePath}`:`${CDN_BASE}@main${filePath}`;
135
 
136
  try{
137
  const response = await fetch(url);
138
  if(!response.ok) return res.status(404).send('File not found');
 
139
  const mimeType = getMimeType(filePath);
140
- res.setHeader('Content-Type',mimeType);
141
 
142
- if(mimeType.startsWith('text') || mimeType==='application/javascript'|| mimeType==='application/json'){
 
143
  const text = await response.text();
144
  res.send(text);
145
- }else{
146
- res.redirect(url);
 
147
  }
148
- }catch(e){ console.error('Error fetching from CDN:',e); res.status(500).send('Server error');}
 
 
 
149
  });
150
 
151
  // --- WebSocket ---
 
125
  }
126
  }
127
 
128
+ // Hybrid serving: everything via latest SHA (auto-refresh / manual refresh)
129
  app.get('*', async (req,res)=>{
130
  if(req.path.startsWith('/api/')) return res.status(404).send('Not found');
131
 
132
+ // All client files are fetched from latest SHA
133
+ const filePath = req.path === '/' ? '/index.html' : req.path;
134
+ const url = `${CDN_BASE}@${latestSHA}${filePath}`;
135
 
136
  try{
137
  const response = await fetch(url);
138
  if(!response.ok) return res.status(404).send('File not found');
139
+
140
  const mimeType = getMimeType(filePath);
141
+ res.setHeader('Content-Type', mimeType);
142
 
143
+ // Stream text files; redirect others (images/fonts) optional
144
+ if(mimeType.startsWith('text') || mimeType==='application/javascript' || mimeType==='application/json'){
145
  const text = await response.text();
146
  res.send(text);
147
+ } else {
148
+ const buffer = await response.arrayBuffer();
149
+ res.send(Buffer.from(buffer));
150
  }
151
+ }catch(e){
152
+ console.error('Error fetching from CDN:',e);
153
+ res.status(500).send('Server error');
154
+ }
155
  });
156
 
157
  // --- WebSocket ---