in packages/dubbo-node-test/src/helpers/http2-request.ts [33:84]
export function http2Request(init: Http2RequestInit): Promise<Http2Response> {
return new Promise<Http2Response>((resolve, reject) => {
const options: http2.SecureClientSessionOptions = {};
if (init.rejectUnauthorized !== undefined) {
options.rejectUnauthorized = init.rejectUnauthorized;
}
const headers: http.OutgoingHttpHeaders = {
...init.headers,
":path": new URL(init.url).pathname,
":method": "GET",
};
if (init.ctype !== undefined) {
headers["content-type"] = init.ctype;
}
if (init.method !== undefined) {
headers[":method"] = init.method;
}
http2
.connect(init.url, options, (sess) => {
const stream = sess.request(headers);
stream.once("error", reject);
stream.once("response", (headers) => {
const chunks: Uint8Array[] = [];
function read() {
let chunk: unknown;
while (null !== (chunk = stream.read() as unknown)) {
assert(chunk instanceof Buffer);
chunks.push(chunk);
}
}
stream.on("readable", read);
stream.once("end", () => {
stream.off("readable", read);
stream.off("error", reject);
sess.close();
const body = Buffer.concat(chunks);
resolve({
status: headers[":status"] ?? -1,
body,
});
});
});
if (init.body !== undefined) {
stream.write(init.body);
}
stream.end();
})
.once("error", reject);
});
}