From ce22bcd12a736648e05d76d644d86cb841ba901f Mon Sep 17 00:00:00 2001 From: vexorian Date: Tue, 1 Jun 2021 20:45:50 -0400 Subject: [PATCH] Processing Time Slots / Random Slots will be less likely to cause a stream freeze --- src/api.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/api.js b/src/api.js index 5c87f82..64599bc 100644 --- a/src/api.js +++ b/src/api.js @@ -281,10 +281,10 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService return res.status(404).send("Channel doesn't have programs?"); } res.writeHead(200, { - 'Content-Type': 'application.json' + 'Content-Type': 'application/json' }); - let transformStream = JSONStream.stringify(); //false makes it not add 'separators' + let transformStream = JSONStream.stringify(); transformStream.pipe(res); for (let i = 0; i < programs.length; i++) { @@ -1003,7 +1003,7 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService console.error("time slots error: " + toolRes.userError); return res.status(400).send(toolRes.userError); } - res.status(200).send(toolRes); + await streamToolResult(toolRes, res); } catch(err) { console.error(err); res.status(500).send("Internal error"); @@ -1017,7 +1017,7 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService console.error("random slots error: " + toolRes.userError); return res.status(400).send(toolRes.userError); } - res.status(200).send(toolRes); + await streamToolResult(toolRes, res); } catch(err) { console.error(err); res.status(500).send("Internal error"); @@ -1069,6 +1069,30 @@ function api(db, channelDB, fillerDB, customShowDB, xmltvInterval, guideService channel.fallback.forEach( cleanUpProgram ); } + async function streamToolResult(toolRes, res) { + let programs = toolRes.programs; + delete toolRes.programs; + let s = JSON.stringify(toolRes); + s = s.slice(0, -1); + console.log( JSON.stringify(toolRes)); + + res.writeHead(200, { + 'Content-Type': 'application/json' + }); + + let transformStream = JSONStream.stringify( + s + ',"programs":[', + ',' , + ']}'); + transformStream.pipe(res); + + for (let i = 0; i < programs.length; i++) { + transformStream.write( programs[i] ); + await throttle(); + } + transformStream.end(); + } + return router }