-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_template.js
70 lines (59 loc) · 1.94 KB
/
script_template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function base64ToArrayBuffer(base64) {
var binaryString = atob(base64);
var bytes = new Uint8Array(binaryString.length);
for (var i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
async function establishSession(url) {
const transport = new WebTransport(url, {
"serverCertificateHashes": [{
"algorithm": "sha-256",
"value": base64ToArrayBuffer("%%CERTHASH%%")
}]
});
transport.closed.then(() => {
console.log(`The HTTP/3 connection to ${url} closed gracefully.`);
}).catch((error) => {
console.error(`The HTTP/3 connection to ${url} closed due to ${error}.`);
});
// Once .ready fulfills, the connection can be used.
await transport.ready;
return transport;
}
async function readFromStream(reader) {
let chunks = [];
let totalLength = 0;
while (true) {
const { value, done } = await reader.read();
if (done) { break; }
chunks.push(value);
totalLength += value.length;
}
// Combine all chunks into a single Uint8Array
let data = new Uint8Array(totalLength);
let position = 0;
for (let chunk of chunks) {
data.set(chunk, position);
position += chunk.length;
}
return data
}
async function request(path) {
const transport = await establishSession('https://%%SERVER%%/webtransport');
const data = new TextEncoder().encode("GET " + path + "\r\n");
const { writable, readable } = await transport.createBidirectionalStream();
console.log("Opened stream.");
const writer = writable.getWriter();
await writer.write(data);
try {
await writer.close();
console.log("All data has been sent on stream.");
} catch(error) {
console.error(`An error occurred: ${error}`);
}
const rsp = await readFromStream(readable.getReader());
transport.close();
return rsp;
}