« get me outta code hell

write: live-dev-server: pipe from file stream to response - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-15 21:18:20 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-15 21:18:20 -0300
commitbfddd202ab2d13d88826a13c0124c9cbba2a5ade (patch)
treedb68a8240f8727ea5fe862616c6f705b9e3bba33 /src/write
parent232f2ed5e96c7a605b294a619a8715dd2a9fd682 (diff)
write: live-dev-server: pipe from file stream to response
Diffstat (limited to 'src/write')
-rw-r--r--src/write/build-modes/live-dev-server.js25
1 files changed, 16 insertions, 9 deletions
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;
     }