Spaces:
Running
Running
Update server.js
Browse files
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 |
-
|
657 |
-
|
658 |
-
|
659 |
-
const
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
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 |
-
|
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);
|