shashwatIDR commited on
Commit
b3af32d
·
verified ·
1 Parent(s): 0ffbe2f

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +18 -22
server.js CHANGED
@@ -653,32 +653,28 @@ app.get('/api/generate/flux', authenticateToken, async (req, res) => {
653
  await new Promise(resolve => setTimeout(resolve, 2000));
654
  continue;
655
  }
656
- const reader = response.body.getReader();
657
- let done = false;
658
- while (!done) {
659
- const { value, done: streamDone } = await reader.read();
660
- if (value) {
661
- buffer += Buffer.from(value).toString('utf8');
662
- const lines = buffer.split('\n');
663
- for (const line of lines) {
664
- if (line.startsWith('data: ')) {
665
- try {
666
- const json = JSON.parse(line.slice(6));
667
- console.log('[Flux] Event:', json);
668
- if (json.msg === 'process_completed' && json.output?.data?.[0]?.url) {
669
- const imageUrl = json.output.data[0].url;
670
- console.log(`[Flux] Success! Image URL: ${imageUrl}`);
671
- found = true;
672
- return res.json({ success: true, imageUrl });
673
- }
674
- } catch (e) {
675
- console.error('[Flux] Parse error:', e, line);
676
  }
 
 
677
  }
678
  }
679
- buffer = lines[lines.length - 1]; // keep incomplete line
680
  }
681
- done = streamDone;
682
  }
683
  } catch (pollError) {
684
  console.error(`[Flux] Poll error on attempt ${attempts}:`, pollError);
 
653
  await new Promise(resolve => setTimeout(resolve, 2000));
654
  continue;
655
  }
656
+ // Use Node.js stream reading
657
+ for await (const chunk of response.body) {
658
+ buffer += chunk.toString('utf8');
659
+ const lines = buffer.split('\n');
660
+ for (let i = 0; i < lines.length - 1; i++) {
661
+ const line = lines[i];
662
+ if (line.startsWith('data: ')) {
663
+ try {
664
+ const json = JSON.parse(line.slice(6));
665
+ console.log('[Flux] Event:', json);
666
+ if (json.msg === 'process_completed' && json.output?.data?.[0]?.url) {
667
+ const imageUrl = json.output.data[0].url;
668
+ console.log(`[Flux] Success! Image URL: ${imageUrl}`);
669
+ found = true;
670
+ return res.json({ success: true, imageUrl });
 
 
 
 
 
671
  }
672
+ } catch (e) {
673
+ console.error('[Flux] Parse error:', e, line);
674
  }
675
  }
 
676
  }
677
+ buffer = lines[lines.length - 1]; // keep incomplete line
678
  }
679
  } catch (pollError) {
680
  console.error(`[Flux] Poll error on attempt ${attempts}:`, pollError);