-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathh2.js
More file actions
57 lines (51 loc) · 1.58 KB
/
h2.js
File metadata and controls
57 lines (51 loc) · 1.58 KB
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 RethinkDNS and its authors
import * as cfg from "../base/cfg.js";
import * as modres from "../base/res.js";
import * as log from "../base/log.js";
/**
* Sends a preset str to socket, writing the output to a Response.
* @param {TransformStream} socket
* @returns {Response}
*/
export async function echo(socket) {
try {
const enc = new TextEncoder();
const u8 = enc.encode("GET-IPADDR\r\n");
const writer = socket.writable.getWriter();
await writer.ready;
await writer.write(u8);
log.d("echo: write done");
return new Response(socket.readable, { headers: cfg.h2header });
} catch (ex) {
log.e("fixed: err", ex);
return modres.r500;
}
}
/**
* Stream req.body to egress, writing the output into res.body.
* @param {Request} req
* @param {TransformStream} egress
* @param {function(Promise)} waiter
* @returns {Promise<Response>}
*/
export async function pipe(req, egress, waiter = (p) => p) {
// ingress is null when request is GET or HEAD
const ingress = req.body;
if (ingress == null) {
log.d("ingress missing");
return modres.r400;
}
try {
// 1. pipe without await
// 2. do not close egress on completion
// 3. always use waiter, regardless of cfg.useWaiter
waiter(ingress.pipeTo(egress.writable, { preventClose: true }));
// stream the response out
// blog.cloudflare.com/workers-optimization-reduces-your-bill
return new Response(egress.readable, { headers: cfg.h2header });
} catch (ex) {
log.e("pipe: err", ex);
return modres.r500;
}
}