Last active
March 11, 2025 02:24
-
-
Save guest271314/c56d769bca04d92dc941c04d9ac40ba5 to your computer and use it in GitHub Desktop.
HTTP/2 server using Node.js builtins and WHATWG Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSecureServer } from "node:http2"; | |
import { createWriteStream, readFileSync } from "node:fs"; | |
import { open, stat } from "node:fs/promises"; | |
import { Readable, Writable } from "node:stream"; | |
import process from "node:process"; | |
const stdout = Writable.toWeb(process.stdout); | |
const encoder = new TextEncoder(); | |
const headers = { | |
"Cache-Control": "no-cache", | |
"Content-Type": "text/plain; charset=UTF-8", | |
"Cross-Origin-Opener-Policy": "unsafe-none", | |
"Cross-Origin-Embedder-Policy": "unsafe-none", | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Private-Network": "true", | |
"Access-Control-Allow-Headers": "Access-Control-Request-Private-Network", | |
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,HEAD,QUERY,query", | |
}; | |
const key = readFileSync( | |
`${import.meta.dirname}/certificate.key`, | |
); | |
const cert = readFileSync( | |
`${import.meta.dirname}/certificate.pem`, | |
); | |
let controller; | |
const requestStream = new ReadableStream({ | |
start(_) { | |
return (controller = _); | |
}, | |
}); | |
// https://nodejs.org/api/http2.html#compatibility-api | |
async function onRequestHandler(request, response) { | |
controller.enqueue({ request, response }); | |
} | |
const server = createSecureServer({ | |
key, | |
cert, | |
}, onRequestHandler); | |
server.listen(8443); | |
(async () => { | |
for await (const { request, response } of requestStream) { | |
if (request.method === "OPTIONS") { | |
response.writeHead(204, headers); | |
continue; | |
} | |
if (request.method === "POST" || /query/i.test(request.method)) { | |
response.writeHead(200, headers); | |
} | |
const readable = Readable.toWeb(request); | |
const writable = Writable.toWeb(response); | |
try { | |
const stream = await readable | |
.pipeThrough(new TextDecoderStream()) | |
.pipeThrough( | |
new TransformStream({ | |
start(c) { | |
console.log(c); | |
}, | |
transform(value, c) { | |
console.log(value); | |
c.enqueue(value.toUpperCase()); | |
}, | |
async flush() { | |
console.log("flush"); | |
}, | |
}), | |
).pipeThrough(new TextEncoderStream()) | |
.pipeTo(writable).catch((e) => { | |
throw e; | |
}); | |
} catch (e) { | |
console.log(e); | |
continue; | |
} | |
continue; | |
} | |
})().catch((e) => { | |
console.log(e); | |
process.exit(); | |
}); |
Author
guest271314
commented
Mar 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment