From bfddd202ab2d13d88826a13c0124c9cbba2a5ade Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 15 May 2024 21:18:20 -0300 Subject: write: live-dev-server: pipe from file stream to response --- src/write/build-modes/live-dev-server.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index 21791da4..7401656b 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -1,7 +1,8 @@ import {spawn} from 'node:child_process'; import * as http from 'node:http'; -import {readFile, stat} from 'node:fs/promises'; +import {open, stat} from 'node:fs/promises'; import * as path from 'node:path'; +import {pipeline} from 'node:stream/promises'; import {inspect as nodeInspect} from 'node:util'; import {ENABLE_COLOR, logInfo, logWarn, progressCallAll} from '#cli'; @@ -299,15 +300,10 @@ export async function go({ 'zip': 'application/zip', }[extname]; + let fd, size; try { - const {size} = await stat(filePath); - const buffer = await readFile(filePath) - response.writeHead(200, { - ...contentType ? {'Content-Type': contentType} : {}, - 'Content-Length': size, - }); - response.end(buffer); - if (loudResponses) console.log(`${requestHead} [200] ${pathname}`); + ({size} = await stat(filePath)); + fd = await open(filePath); } catch (error) { if (error.code === 'EISDIR') { response.writeHead(404, contentTypePlain); @@ -319,7 +315,18 @@ export async function go({ console.error(`${requestHead} [500] ${pathname}`); showError(error); } + return; } + + response.writeHead(200, { + ...contentType ? {'Content-Type': contentType} : {}, + 'Content-Length': size, + }); + + await pipeline(fd.createReadStream(), response); + + if (loudResponses) console.log(`${requestHead} [200] ${pathname}`); + return; } -- cgit 1.3.0-6-gf8a5