Created
July 13, 2025 17:51
-
-
Save guest271314/e699b9abf4b91e09d74a488a5f48bb48 to your computer and use it in GitHub Desktop.
Deno WebTrasport server
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 certificates from "./cert.json" with { type: "json" }; | |
const serverCertificateHashes = [{ | |
algorithm: "sha-256", | |
value: Uint8Array.from(certificates[0].hash.digest) | |
}]; | |
let requests = 0; | |
async function handleTransport(wt) { | |
let incomingTotalLength = 0; | |
let incomingCurrentLength = 0; | |
const buffer = new ArrayBuffer(0, { maxByteLength: 4 }); | |
const view = new DataView(buffer); | |
const encoder = new TextEncoder; | |
const decoder = new TextDecoder; | |
await wt.ready; | |
try { | |
for await (const { readable, writable } of wt.incomingBidirectionalStreams) { | |
const writer = writable.getWriter(); | |
await readable.pipeTo(new WritableStream({ | |
async write(value) { | |
if (incomingTotalLength === 0 && incomingCurrentLength === 0) { | |
buffer.resize(4); | |
for (let i = 0;i < 4; i++) { | |
view.setUint8(i, value[i]); | |
} | |
incomingTotalLength = view.getUint32(0, true); | |
console.log(value.length, incomingTotalLength); | |
value = value.subarray(4); | |
} | |
const encoded = encoder.encode(decoder.decode(value).toUpperCase()); | |
await writer.ready; | |
await writer.write(encoded); | |
await writer.ready; | |
incomingCurrentLength += encoded.length; | |
console.log(`Done writing ${encoded.length} bytes to writable, ${incomingCurrentLength} of ${incomingTotalLength} bytes written.`); | |
}, | |
close() { | |
console.log("readable closed"); | |
} | |
})).then(() => console.log("writable closed")).catch((e) => console.log(e)); | |
buffer.resize(0); | |
incomingTotalLength = incomingCurrentLength = 0; | |
console.log(`Done streaming request ${requests++}`); | |
continue; | |
} | |
} catch (e) { | |
console.log("handleTransport caught", e); | |
} | |
return `Done streaming`; | |
} | |
const server = new Deno.QuicEndpoint({ | |
hostname: "0.0.0.0", | |
port: 8080 | |
}); | |
const listener = server.listen({ | |
cert: certificates[0].pem, | |
key: certificates[0].privateKey, | |
alpnProtocols: ["h3"] | |
}); | |
console.log({ server, listener }); | |
try { | |
for await (const conn of listener) { | |
const wt = await Deno.upgradeWebTransport(conn).catch((e) => console.log("upgrade caught", e)); | |
try { | |
await handleTransport(wt).then(console.log).catch(console.error); | |
} catch (e) { | |
console.log(e); | |
} finally { | |
continue; | |
} | |
} | |
} catch (e) { | |
console.log("caught", e); | |
} finally { | |
try { | |
listener.stop(); | |
server.close(); | |
} catch (e) { | |
console.log("caught again", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Client