Files
bun.sh/test/js/node/http2/helpers.js
Ciro Spaciari c23579d66c feat(http2) Client Support (#6761)
* init

* WIP fix post data/refactor

* make it compile again

* more things

* undo padding and continue + fix posting + update lshpack

* re-add fixes

* really simple tests + fixes

* add aborted event

* fix trailers

* add getDefaultSettings, getPackedSettings and getUnpackedSettings

* fix + fmt

* fixes

* fix enablePush o be boolean

* fix sendTrailers

* fmt

* fix goaway, fix some error messages

* oops

* revert some changes

* more reverts

* WIP

* get CMAKE building lspack + ping behavior

* remove files that should not be added anymore

* remove old out files

* remove old file

* fix header reduce

* bunch of fixes

* fix socket unref

* fix abort signal, rebase and fmt

* socket unref tests

* oops re-add cmake

* fix stream state

* more tests and fixes

* fixes and ping tests

* lshpack in Dockerfile

* just copy lshpack

* oops

* fix end

* wantTrailers

* encode/decode fixes + grpc

* channel credentials test

* rebase

* support h2c

* fix h2c

* fix h2c connect event + h2c tests

* 'copy ls-hpack

* ls-hpack build sh

* oops

* changing CMake + Docker

* add ps1 build for ls-hpack fix clean

* optimizations + fixes

* remove protect/unprotect from handlers

* more consistent errors

* fix error code

* oops

* add goaway tests

* oops uncoment tests

* better errors more tests

* add broken priority frame

* better memory leak, some fixes and less flask tests

* zig update .Big -> .big

* closer threshold + h2 fix

* remove log

* test should not be flask

* increase timeout on leak memory test

* windows build

* less flasky

* always 127.0.0.1

* [autofix.ci] apply automated fixes

* remove .call and use primordials

* apply socket fix

* fix win-build

* should properly mark inactive

* postgres fix

* increase deadline

* test tests

* high light deadline timeouts

* event loop test

* make memory leak test faster

* use buffer on payload test

* check for socket.data before use

* reduce iterations to see if timeout on mac intel

* fix assertions

* avoid localhost and simplify things

* refactor memory leak test

* Update src/js/node/tls.js

* fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: cirospaciari <ciro.spaciai@gmail.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
2023-11-17 18:14:54 -08:00

173 lines
4.3 KiB
JavaScript

"use strict";
// An HTTP/2 testing tool used to create mock frames for direct testing
// of HTTP/2 endpoints.
const kFrameData = Symbol("frame-data");
const FLAG_EOS = 0x1;
const FLAG_ACK = 0x1;
const FLAG_EOH = 0x4;
const FLAG_PADDED = 0x8;
const PADDING = Buffer.alloc(255);
const kClientMagic = Buffer.from("505249202a20485454502f322" + "e300d0a0d0a534d0d0a0d0a", "hex");
const kFakeResponseHeaders = Buffer.from(
"4803333032580770726976617465611d" +
"4d6f6e2c203231204f63742032303133" +
"2032303a31333a323120474d546e1768" +
"747470733a2f2f7777772e6578616d70" +
"6c652e636f6d",
"hex",
);
function isUint32(val) {
return val >>> 0 === val;
}
function isUint24(val) {
return val >>> 0 === val && val <= 0xffffff;
}
function isUint8(val) {
return val >>> 0 === val && val <= 0xff;
}
function write32BE(array, pos, val) {
if (!isUint32(val)) throw new RangeError("val is not a 32-bit number");
array[pos++] = (val >> 24) & 0xff;
array[pos++] = (val >> 16) & 0xff;
array[pos++] = (val >> 8) & 0xff;
array[pos++] = val & 0xff;
}
function write24BE(array, pos, val) {
if (!isUint24(val)) throw new RangeError("val is not a 24-bit number");
array[pos++] = (val >> 16) & 0xff;
array[pos++] = (val >> 8) & 0xff;
array[pos++] = val & 0xff;
}
function write8(array, pos, val) {
if (!isUint8(val)) throw new RangeError("val is not an 8-bit number");
array[pos] = val;
}
class Frame {
constructor(length, type, flags, id) {
this[kFrameData] = Buffer.alloc(9);
write24BE(this[kFrameData], 0, length);
write8(this[kFrameData], 3, type);
write8(this[kFrameData], 4, flags);
write32BE(this[kFrameData], 5, id);
}
get data() {
return this[kFrameData];
}
}
class SettingsFrame extends Frame {
constructor(ack = false) {
let flags = 0;
if (ack) flags |= FLAG_ACK;
super(0, 4, flags, 0);
}
}
class DataFrame extends Frame {
constructor(id, payload, padlen = 0, final = false) {
let len = payload.length;
let flags = 0;
if (final) flags |= FLAG_EOS;
const buffers = [payload];
if (padlen > 0) {
buffers.unshift(Buffer.from([padlen]));
buffers.push(PADDING.slice(0, padlen));
len += padlen + 1;
flags |= FLAG_PADDED;
}
super(len, 0, flags, id);
buffers.unshift(this[kFrameData]);
this[kFrameData] = Buffer.concat(buffers);
}
}
class HeadersFrame extends Frame {
constructor(id, payload, padlen = 0, endOfHeaders, final = false) {
let len = payload.length;
let flags = endOfHeaders ? FLAG_EOH : 0;
if (final) flags |= FLAG_EOS;
const buffers = [payload];
if (padlen > 0) {
buffers.unshift(Buffer.from([padlen]));
buffers.push(PADDING.slice(0, padlen));
len += padlen + 1;
flags |= FLAG_PADDED;
}
super(len, 1, flags, id);
buffers.unshift(this[kFrameData]);
this[kFrameData] = Buffer.concat(buffers);
}
}
class ContinuationFrame extends Frame {
constructor(id, payload, padlen = 0, final = false) {
let len = payload.length;
let flags = FLAG_EOH;
if (final) flags |= FLAG_EOS;
const buffers = [payload];
if (padlen > 0) {
buffers.unshift(Buffer.from([padlen]));
buffers.push(PADDING.slice(0, padlen));
len += padlen + 1;
flags |= FLAG_PADDED;
}
super(len, 9, flags, id);
buffers.unshift(this[kFrameData]);
this[kFrameData] = Buffer.concat(buffers);
}
}
class PingFrame extends Frame {
constructor(ack = false) {
const buffers = [Buffer.alloc(8)];
super(8, 6, ack ? 1 : 0, 0);
buffers.unshift(this[kFrameData]);
this[kFrameData] = Buffer.concat(buffers);
}
}
class GoAwayFrame extends Frame {
constructor(rst_code, last_id, debug_data) {
debug_data = debug_data || Buffer.alloc(0);
super(8 + debug_data.byteLength, 7, 0, 0);
const buffer = Buffer.alloc(8);
buffer.writeUInt32BE(last_id, 0);
buffer.writeUInt32BE(rst_code, 4);
this[kFrameData] = Buffer.concat([this[kFrameData], buffer, debug_data]);
}
}
class AltSvcFrame extends Frame {
constructor(size) {
const buffers = [Buffer.alloc(size)];
super(size, 10, 0, 0);
buffers.unshift(this[kFrameData]);
this[kFrameData] = Buffer.concat(buffers);
}
}
module.exports = {
Frame,
AltSvcFrame,
DataFrame,
HeadersFrame,
SettingsFrame,
PingFrame,
GoAwayFrame,
kClientMagic,
kFakeResponseHeaders,
ContinuationFrame,
};