From fb609f9841713be5d4e7cd1c877ac165b1f9f883 Mon Sep 17 00:00:00 2001 From: Sebastien DUMETZ Date: Wed, 12 Jun 2024 12:20:15 +0200 Subject: [PATCH] simpler scene zip generation iterator. Ensure fix #27 --- source/server/routes/api/v1/scenes/get.ts | 8 +++----- source/server/routes/api/v1/scenes/scene/get.ts | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/source/server/routes/api/v1/scenes/get.ts b/source/server/routes/api/v1/scenes/get.ts index 7e932dd2..2dba8251 100644 --- a/source/server/routes/api/v1/scenes/get.ts +++ b/source/server/routes/api/v1/scenes/get.ts @@ -7,6 +7,7 @@ import { HTTPError } from "../../../../utils/errors.js"; import { getHost, getUser, getVfs } from "../../../../utils/locals.js"; import { wrapFormat } from "../../../../utils/wrapAsync.js"; import { ZipEntry, zip } from "../../../../utils/zip/index.js"; +import { once } from "events"; export default async function getScenes(req :Request, res :Response){ let vfs = getVfs(req); @@ -130,11 +131,8 @@ export default async function getScenes(req :Request, res :Response){ // It would also allow for strong ETag generation, which would be desirable res.status(200); for await (let data of zip(getFiles())){ - await new Promise(resolve=>{ - let again = res.write(data); - if(again) resolve(); - else res.once("drain", resolve); - }); + let again = res.write(data); + if(!again) await once(res, "drain"); } res.end(); } diff --git a/source/server/routes/api/v1/scenes/scene/get.ts b/source/server/routes/api/v1/scenes/scene/get.ts index dce4a0c1..3b6639e7 100644 --- a/source/server/routes/api/v1/scenes/scene/get.ts +++ b/source/server/routes/api/v1/scenes/scene/get.ts @@ -5,6 +5,7 @@ import { getUserId, getVfs } from "../../../../../utils/locals.js"; import { wrapFormat } from "../../../../../utils/wrapAsync.js"; import { ZipEntry, zip } from "../../../../../utils/zip/index.js"; import { HTTPError } from "../../../../../utils/errors.js"; +import { once } from "events"; @@ -56,11 +57,8 @@ export default async function getScene(req :Request, res :Response){ res.status(200); for await (let data of zip(getFiles())){ - await new Promise(resolve=>{ - let again = res.write(data); - if(again) resolve(); - else res.once("drain", resolve); - }); + let again = res.write(data); + if(!again) once(res, "drain"); } res.end(); }