From d59dae07075f0c09b340f3a2aed324e46c68d971 Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Sat, 16 Aug 2025 17:20:27 +0200 Subject: [PATCH] glory --- h2.mjs | 162 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 107 insertions(+), 55 deletions(-) diff --git a/h2.mjs b/h2.mjs index ed66178..ab3b472 100644 --- a/h2.mjs +++ b/h2.mjs @@ -971,6 +971,11 @@ const handleConnection = const payload = Buffer.from(body); const h2stream = streams.get(frame.streamIdentifier); + if (!h2stream) { + setImmediate(cb); + return; + } + const doWrite = () => { h2stream.flowControlWindowSize -= payload.length; @@ -1117,6 +1122,36 @@ const handleConnection = break; } + case FRAME_TYPE.RST_STREAM: { + const errorCode = frame.payload.readUint32BE(); + + if (frame.streamIdentifier === 0) { + socket.destroy(); + } + + streams.delete(frame.streamIdentifier); + + if (debug) { + console.log("stream reset because of", ERROR_CODE_NAME[errorCode]); + } + + break; + } + case FRAME_TYPE.PING: { + if (frame.streamIdentifier !== 0) { + socket.destroy(); + } + + socket.write( + encodeFrame({ + type: FRAME_TYPE.PING, + flags: 0x01, // ACK + streamIdentifier: 0, + payload: frame.payload, + }) + ); + break; + } case FRAME_TYPE.WINDOW_UPDATE: { // whatever const increment = frame.payload.readUint32BE(); @@ -1202,6 +1237,8 @@ export const createH2Server = () => { const { server, onConnection } = createH2Server(); +const rootPath = process.env.ROOT_PATH ?? "."; + server.on( "request", /** @@ -1215,10 +1252,40 @@ server.on( res.writeHead({ status: 405 }); } - const filepath = path.join(".", path.normalize(req.path)); + const filepath = path.join(rootPath, path.normalize(req.path)); console.log("opening", filepath); + const serveFile = (filepath, size) => { + const contentType = + { + ".html": "text/html; charset=utf-8", + ".css": "text/css; charset=utf-8", + ".js": "text/javascript; charset=utf-8", + ".png": "image/png", + ".jpg": "image/jpeg", + ".jpeg": "image/jpeg", + ".webp": "image/webp", + ".avif": "image/avif", + ".svg": "image/svg+xml", + ".woff": "font/woff", + ".woff2": "font/woff2", + ".txt": "text/plain", + }[path.extname(filepath)] ?? "application/octet-stream"; + + res.writeHead({ + status: 200, + contentLength: size, + headers: [["content-type", contentType]], + }); + + const fileStream = fs.createReadStream(filepath, { + highWaterMark: 1_048_576, + }); + + fileStream.pipe(res.body); + }; + fs.stat(filepath, (err, stat) => { if (err) { if (err.code === "ENOENT") { @@ -1231,69 +1298,54 @@ server.on( } if (stat.isDirectory()) { - fs.readdir(filepath, (err, files) => { + const indexhtml = path.join(filepath, "index.html"); + fs.stat(indexhtml, (err, stat) => { if (err) { - console.error("failed to read dir", err); - res.writeHead({ status: 500 }); + if (err.code === "ENOENT") { + fs.readdir(filepath, (err, files) => { + if (err) { + console.error("failed to read dir", err); + res.writeHead({ status: 500 }); + return; + } + + const html = `