@fails-components/webtransport 0.3.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +81 -40
- package/lib/dom.ts +7 -5
- package/lib/http2/browser/browser.js +168 -0
- package/lib/http2/browser/browserparser.js +248 -0
- package/lib/http2/flowcontroller.js +371 -0
- package/lib/http2/node/capsuleparser.js +291 -0
- package/lib/http2/node/client.js +239 -0
- package/lib/http2/node/index.js +2 -0
- package/lib/http2/node/server.js +499 -0
- package/lib/http2/node/websocketparser.js +829 -0
- package/lib/http2/parserbase.js +139 -0
- package/lib/http2/parserbasehttp2.js +128 -0
- package/lib/http2/session.js +225 -0
- package/lib/http2/stream.js +354 -0
- package/lib/http2/websocketcommon.js +1 -0
- package/lib/{index.js → index.browser.js} +6 -10
- package/lib/index.node.js +27 -0
- package/lib/server.js +252 -69
- package/lib/session.js +31 -68
- package/lib/stream.js +103 -179
- package/lib/types.ts +174 -87
- package/lib/utils.js +2 -1
- package/lib/webstreams.browser.js +6 -0
- package/lib/webstreams.js +7 -0
- package/lib/webtransport.browser.js +284 -0
- package/lib/webtransport.node.js +137 -0
- package/lib/{webtransport.js → webtransportbase.js} +47 -41
- package/old_test/test.js +31 -29
- package/old_test/testsuite.js +1 -1
- package/package.json +18 -17
- package/readme.md +94 -30
- package/test/bidirectional-streams.spec.js +51 -9
- package/test/datagrams.spec.js +8 -2
- package/test/fixtures/known-bytes.js +29 -0
- package/test/fixtures/read-stream.js +1 -1
- package/test/fixtures/reader-value.js +1 -1
- package/test/fixtures/server.js +24 -10
- package/test/fixtures/webtransport.browser.js +20 -2
- package/test/fixtures/webtransport.js +1 -1
- package/test/index.js +33 -6
- package/test/pw-no-https-errors.json +5 -0
- package/test/session.spec.js +71 -52
- package/test/unidirectional-streams.spec.js +20 -4
- package/CMakeLists.txt +0 -991
- package/LICENSE.chromium +0 -27
- package/LICENSE.envoy +0 -207
- package/build.js +0 -313
- package/dist/lib/client.d.ts +0 -78
- package/dist/lib/client.d.ts.map +0 -1
- package/dist/lib/dom.d.ts +0 -87
- package/dist/lib/dom.d.ts.map +0 -1
- package/dist/lib/error.d.ts +0 -8
- package/dist/lib/error.d.ts.map +0 -1
- package/dist/lib/event-loop.d.ts +0 -33
- package/dist/lib/event-loop.d.ts.map +0 -1
- package/dist/lib/index.d.ts +0 -79
- package/dist/lib/index.d.ts.map +0 -1
- package/dist/lib/native.d.ts +0 -2
- package/dist/lib/native.d.ts.map +0 -1
- package/dist/lib/server.d.ts +0 -92
- package/dist/lib/server.d.ts.map +0 -1
- package/dist/lib/session.d.ts +0 -298
- package/dist/lib/session.d.ts.map +0 -1
- package/dist/lib/stream.d.ts +0 -135
- package/dist/lib/stream.d.ts.map +0 -1
- package/dist/lib/transport.d.ts +0 -34
- package/dist/lib/transport.d.ts.map +0 -1
- package/dist/lib/types.d.ts +0 -212
- package/dist/lib/types.d.ts.map +0 -1
- package/dist/lib/utils.d.ts +0 -11
- package/dist/lib/utils.d.ts.map +0 -1
- package/dist/lib/webtransport.d.ts +0 -47
- package/dist/lib/webtransport.d.ts.map +0 -1
- package/dist/test/bidirectional-streams.spec.d.ts +0 -5
- package/dist/test/bidirectional-streams.spec.d.ts.map +0 -1
- package/dist/test/datagrams.spec.d.ts +0 -5
- package/dist/test/datagrams.spec.d.ts.map +0 -1
- package/dist/test/event-loop.node.d.ts +0 -2
- package/dist/test/event-loop.node.d.ts.map +0 -1
- package/dist/test/fixtures/certificate.d.ts +0 -21
- package/dist/test/fixtures/certificate.d.ts.map +0 -1
- package/dist/test/fixtures/chai.d.ts +0 -2
- package/dist/test/fixtures/chai.d.ts.map +0 -1
- package/dist/test/fixtures/known-bytes.d.ts +0 -6
- package/dist/test/fixtures/known-bytes.d.ts.map +0 -1
- package/dist/test/fixtures/p-timeout.d.ts +0 -8
- package/dist/test/fixtures/p-timeout.d.ts.map +0 -1
- package/dist/test/fixtures/read-cert-hash.d.ts +0 -6
- package/dist/test/fixtures/read-cert-hash.d.ts.map +0 -1
- package/dist/test/fixtures/read-stream.d.ts +0 -19
- package/dist/test/fixtures/read-stream.d.ts.map +0 -1
- package/dist/test/fixtures/reader-value.d.ts +0 -22
- package/dist/test/fixtures/reader-value.d.ts.map +0 -1
- package/dist/test/fixtures/server.d.ts +0 -5
- package/dist/test/fixtures/server.d.ts.map +0 -1
- package/dist/test/fixtures/webtransport.browser.d.ts +0 -3
- package/dist/test/fixtures/webtransport.browser.d.ts.map +0 -1
- package/dist/test/fixtures/webtransport.d.ts +0 -3
- package/dist/test/fixtures/webtransport.d.ts.map +0 -1
- package/dist/test/fixtures/write-stream.d.ts +0 -10
- package/dist/test/fixtures/write-stream.d.ts.map +0 -1
- package/dist/test/index.d.ts +0 -2
- package/dist/test/index.d.ts.map +0 -1
- package/dist/test/session.spec.d.ts +0 -5
- package/dist/test/session.spec.d.ts.map +0 -1
- package/dist/test/unidirectional-streams.spec.d.ts +0 -2
- package/dist/test/unidirectional-streams.spec.d.ts.map +0 -1
- package/failslogolicensereadme.txt +0 -4
- package/lib/event-loop.js +0 -88
- package/lib/native.js +0 -28
- package/lib/transport.js +0 -82
- package/platform/icufix/fakedata.cpp +0 -8
- package/platform/quiche/quic/core/io/socket_win.inc +0 -3
- package/platform/quiche_platform_impl/quiche_bug_tracker_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_client_stats_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_command_line_flags_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_containers_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_event_loop_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_export_impl.h +0 -6
- package/platform/quiche_platform_impl/quiche_flag_utils_impl.h +0 -11
- package/platform/quiche_platform_impl/quiche_flags_impl.h +0 -8
- package/platform/quiche_platform_impl/quiche_iovec_impl.h +0 -10
- package/platform/quiche_platform_impl/quiche_logging_impl.h +0 -21
- package/platform/quiche_platform_impl/quiche_mem_slice_impl.h +0 -2
- package/platform/quiche_platform_impl/quiche_mutex_impl.h +0 -8
- package/platform/quiche_platform_impl/quiche_prefetch_impl.h +0 -11
- package/platform/quiche_platform_impl/quiche_reference_counted_impl.h +0 -16
- package/platform/quiche_platform_impl/quiche_server_stats_impl.h +0 -7
- package/platform/quiche_platform_impl/quiche_stack_trace_impl.h +0 -12
- package/platform/quiche_platform_impl/quiche_stream_buffer_allocator_impl.h +0 -8
- package/platform/quiche_platform_impl/quiche_testvalue_impl.h +0 -1
- package/platform/quiche_platform_impl/quiche_thread_local_impl.h +0 -6
- package/platform/quiche_platform_impl/quiche_time_utils_impl.h +0 -13
- package/platform/quiche_platform_impl/quiche_udp_socket_platform_impl.h +0 -39
- package/src/http3backendresponse.cc +0 -36
- package/src/http3backendresponse.h +0 -109
- package/src/http3client.cc +0 -1529
- package/src/http3client.h +0 -557
- package/src/http3clientsession.cc +0 -65
- package/src/http3clientsession.h +0 -60
- package/src/http3clientstream.cc +0 -52
- package/src/http3clientstream.h +0 -42
- package/src/http3dispatcher.cc +0 -62
- package/src/http3dispatcher.h +0 -57
- package/src/http3eventloop.cc +0 -1107
- package/src/http3eventloop.h +0 -316
- package/src/http3server.cc +0 -514
- package/src/http3server.h +0 -151
- package/src/http3serverbackend.cc +0 -105
- package/src/http3serverbackend.h +0 -98
- package/src/http3serversession.cc +0 -123
- package/src/http3serversession.h +0 -95
- package/src/http3serverstream.cc +0 -463
- package/src/http3serverstream.h +0 -114
- package/src/http3sessioncache.cc +0 -85
- package/src/http3sessioncache.h +0 -59
- package/src/http3wtsessionvisitor.cc +0 -46
- package/src/http3wtsessionvisitor.h +0 -397
- package/src/http3wtstreamvisitor.cc +0 -155
- package/src/http3wtstreamvisitor.h +0 -404
- package/test/event-loop.node.js +0 -24
- package/tsconfig.json +0 -39
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Http2WebTransportStream } from './stream.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param{Number|bigint} int
|
|
5
|
+
* @returns {Number}
|
|
6
|
+
*/
|
|
7
|
+
export function lengthVarInt(int) {
|
|
8
|
+
if (BigInt(int) < 64n) return 1
|
|
9
|
+
if (BigInt(int) < 16384n) return 2
|
|
10
|
+
if (BigInt(int) < 1073741824n) return 4
|
|
11
|
+
/* if (BigInt(int) < 4611686018427387904 ) */
|
|
12
|
+
return 8
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class ParserBase {
|
|
16
|
+
static PADDING = 0x190b4d38
|
|
17
|
+
static WT_RESET_STREAM = 0x190b4d39
|
|
18
|
+
static WT_STOP_SENDING = 0x190b4d3a
|
|
19
|
+
static WT_STREAM_WOFIN = 0x190b4d3b
|
|
20
|
+
static WT_STREAM_WFIN = 0x190b4d3c
|
|
21
|
+
static WT_MAX_DATA = 0x190b4d3d
|
|
22
|
+
static WT_MAX_STREAM_DATA = 0x190b4d3e
|
|
23
|
+
static WT_MAX_STREAMS_BIDI = 0x190b4d3f
|
|
24
|
+
static WT_MAX_STREAMS_UNIDI = 0x190b4d40
|
|
25
|
+
static WT_DATA_BLOCKED = 0x190b4d41
|
|
26
|
+
static WT_STREAM_DATA_BLOCKED = 0x190b4d42
|
|
27
|
+
static WT_STREAMS_BLOCKED_UNIDI = 0x190b4d43
|
|
28
|
+
static WT_STREAMS_BLOCKED_BIDI = 0x190b4d44
|
|
29
|
+
static DATAGRAM = 0x00
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {import('../types').ParserInit} arg
|
|
33
|
+
*/
|
|
34
|
+
constructor({
|
|
35
|
+
nativesession,
|
|
36
|
+
isclient,
|
|
37
|
+
initialStreamSendWindowOffset,
|
|
38
|
+
initialStreamReceiveWindowOffset,
|
|
39
|
+
streamShouldAutoTuneReceiveWindow,
|
|
40
|
+
streamReceiveWindowSizeLimit
|
|
41
|
+
}) {
|
|
42
|
+
this.session = nativesession
|
|
43
|
+
this.isclient = isclient
|
|
44
|
+
/** @type {boolean} */
|
|
45
|
+
this.blocked = false
|
|
46
|
+
|
|
47
|
+
this.initialStreamSendWindowOffset = initialStreamSendWindowOffset
|
|
48
|
+
this.initialStreamReceiveWindowOffset = initialStreamReceiveWindowOffset
|
|
49
|
+
this.streamShouldAutoTuneReceiveWindow = streamShouldAutoTuneReceiveWindow
|
|
50
|
+
this.streamReceiveWindowSizeLimit = streamReceiveWindowSizeLimit
|
|
51
|
+
|
|
52
|
+
this.wtstreams = new Map()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @abstract
|
|
57
|
+
* @param {Buffer|Uint8Array} data
|
|
58
|
+
*/
|
|
59
|
+
parseData(data) {
|
|
60
|
+
throw new Error('Implement parseData in derived Class')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @abstract
|
|
65
|
+
* @param{{type: Number, headerVints: Array<Number|bigint>, payload: Uint8Array|undefined}} bs
|
|
66
|
+
*/
|
|
67
|
+
writeCapsule({ type, headerVints, payload }) {
|
|
68
|
+
throw new Error('Implement writeCapsule in derived Class')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param{{code: Number, reason: string}}arg
|
|
73
|
+
*/
|
|
74
|
+
sendClose({ code, reason }) {}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Number} streamid
|
|
78
|
+
*/
|
|
79
|
+
newStream(streamid) {
|
|
80
|
+
const stream = new Http2WebTransportStream({
|
|
81
|
+
streamid,
|
|
82
|
+
capsuleParser: this,
|
|
83
|
+
sendWindowOffset: this.initialStreamSendWindowOffset,
|
|
84
|
+
receiveWindowOffset: this.initialStreamReceiveWindowOffset,
|
|
85
|
+
shouldAutoTuneReceiveWindow: this.streamShouldAutoTuneReceiveWindow,
|
|
86
|
+
receiveWindowSizeLimit: this.streamReceiveWindowSizeLimit,
|
|
87
|
+
sessionFlowController: this.session.flowController
|
|
88
|
+
})
|
|
89
|
+
this.wtstreams.set(streamid, stream)
|
|
90
|
+
this.session.jsobj.onStream({
|
|
91
|
+
bidirectional: !(streamid & 0x2),
|
|
92
|
+
incoming: this.isclient ? !(streamid & 0x1) : !!(streamid & 0x1),
|
|
93
|
+
stream
|
|
94
|
+
})
|
|
95
|
+
return stream
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
drainWrites() {
|
|
99
|
+
for (const stream of this.wtstreams.values()) {
|
|
100
|
+
stream.drainWrites()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {bigint|undefined} val
|
|
106
|
+
*/
|
|
107
|
+
onMaxData(val) {
|
|
108
|
+
if (val && this.session.flowController.updateSendWindowOffset(val))
|
|
109
|
+
this.drainWrites()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {bigint|undefined} streamid
|
|
114
|
+
* @param {bigint|undefined} offset
|
|
115
|
+
*/
|
|
116
|
+
onMaxStreamData(streamid, offset) {
|
|
117
|
+
const object = this.wtstreams.get(Number(streamid))
|
|
118
|
+
if (object && offset) {
|
|
119
|
+
if (object.flowController.updateSendWindowOffset(offset))
|
|
120
|
+
object.drainWrites()
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {bigint|undefined} val
|
|
126
|
+
*/
|
|
127
|
+
onDataBlocked(val) {
|
|
128
|
+
this.session.flowController.reportBlocked(val)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {bigint|undefined} streamid
|
|
133
|
+
* @param {bigint|undefined} offset
|
|
134
|
+
*/
|
|
135
|
+
onStreamDataBlocked(streamid, offset) {
|
|
136
|
+
const object = this.wtstreams.get(Number(streamid))
|
|
137
|
+
if (object && offset) object.flowController.reportBlocked(offset)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { ParserBase } from './parserbase.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('node:http2').Http2Stream} Http2Stream
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param{{offset: Number, buffer: Buffer, size: Number}} bs
|
|
9
|
+
*/
|
|
10
|
+
export function readVarInt(bs) {
|
|
11
|
+
if (bs.offset + 1 > bs.size) return undefined
|
|
12
|
+
let val = BigInt(bs.buffer.readUInt8(bs.offset))
|
|
13
|
+
bs.offset++
|
|
14
|
+
const prefix = Number(val) >>> 6
|
|
15
|
+
const intlength = 1 << prefix
|
|
16
|
+
|
|
17
|
+
if (bs.offset + intlength - 1 > bs.size) {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
val = val & 0x3fn
|
|
21
|
+
for (let i = 0; i < intlength - 1; i++) {
|
|
22
|
+
val = (val << 8n) | BigInt(bs.buffer.readUInt8(bs.offset))
|
|
23
|
+
bs.offset++
|
|
24
|
+
}
|
|
25
|
+
return val
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param{{offset: Number, buffer: Buffer, size: Number}} bs
|
|
30
|
+
* @param{Number|bigint} int
|
|
31
|
+
*/
|
|
32
|
+
export function writeVarInt(bs, int) {
|
|
33
|
+
let numbytes = 8n
|
|
34
|
+
let msb = 0xc0n
|
|
35
|
+
const bint = BigInt(int)
|
|
36
|
+
if (bint < 64) {
|
|
37
|
+
numbytes = 1n
|
|
38
|
+
msb = 0x0n
|
|
39
|
+
} else if (bint < 16384) {
|
|
40
|
+
numbytes = 2n
|
|
41
|
+
msb = 0x40n
|
|
42
|
+
} else if (bint < 1073741824) {
|
|
43
|
+
numbytes = 4n
|
|
44
|
+
msb = 0x80n
|
|
45
|
+
}
|
|
46
|
+
bs.buffer.writeUInt8(
|
|
47
|
+
Number(msb | ((bint >> ((numbytes - 1n) * 8n)) & 0xffn)),
|
|
48
|
+
bs.offset
|
|
49
|
+
)
|
|
50
|
+
bs.offset++
|
|
51
|
+
|
|
52
|
+
for (let i = numbytes - 2n; i >= 0; i--) {
|
|
53
|
+
bs.buffer.writeUInt8(Number((bint >> (i * 8n)) & 0xffn), bs.offset)
|
|
54
|
+
bs.offset++
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export class ParserBaseHttp2 extends ParserBase {
|
|
59
|
+
/**
|
|
60
|
+
* @param {import('../types').ParserHttp2Init} stream
|
|
61
|
+
*/
|
|
62
|
+
constructor({
|
|
63
|
+
stream,
|
|
64
|
+
nativesession,
|
|
65
|
+
isclient,
|
|
66
|
+
initialStreamSendWindowOffset,
|
|
67
|
+
initialStreamReceiveWindowOffset,
|
|
68
|
+
streamShouldAutoTuneReceiveWindow,
|
|
69
|
+
streamReceiveWindowSizeLimit
|
|
70
|
+
}) {
|
|
71
|
+
super({
|
|
72
|
+
nativesession,
|
|
73
|
+
isclient,
|
|
74
|
+
initialStreamSendWindowOffset,
|
|
75
|
+
initialStreamReceiveWindowOffset,
|
|
76
|
+
streamShouldAutoTuneReceiveWindow,
|
|
77
|
+
streamReceiveWindowSizeLimit
|
|
78
|
+
})
|
|
79
|
+
this.stream = stream
|
|
80
|
+
this.session = nativesession
|
|
81
|
+
this.isclient = isclient
|
|
82
|
+
|
|
83
|
+
this.stream.on('readable', () => {
|
|
84
|
+
let data
|
|
85
|
+
while ((data = this.stream.read()) !== null) {
|
|
86
|
+
this.parseData(data)
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
this.stream.on('end', () => {
|
|
91
|
+
// readable end
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
this.stream.on(
|
|
95
|
+
'error',
|
|
96
|
+
/**
|
|
97
|
+
* @param {Error} error
|
|
98
|
+
*/ (error) => {
|
|
99
|
+
// readable error
|
|
100
|
+
this.session.jsobj.onClose({
|
|
101
|
+
errorcode: this.stream.rstCode,
|
|
102
|
+
error: error.toString()
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
this.stream.on('drain', () => {
|
|
108
|
+
// writable, can write more data
|
|
109
|
+
this.blocked = false
|
|
110
|
+
this.drainWrites()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
this.stream.on('close', () => {
|
|
114
|
+
if (
|
|
115
|
+
!(
|
|
116
|
+
this.session.jsobj.state === 'failed' ||
|
|
117
|
+
this.session.jsobj.state === 'closed'
|
|
118
|
+
)
|
|
119
|
+
) {
|
|
120
|
+
// writable close
|
|
121
|
+
this.session.jsobj.onClose({
|
|
122
|
+
errorcode: this.stream.rstCode || 0,
|
|
123
|
+
error: ''
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('http2').Http2Stream} Http2Stream
|
|
3
|
+
*/
|
|
4
|
+
import { ParserBase } from './parserbase.js'
|
|
5
|
+
import { FlowController } from './flowcontroller.js'
|
|
6
|
+
import { logger } from '../utils.js'
|
|
7
|
+
|
|
8
|
+
const pid = typeof process !== 'undefined' ? process.pid : 0
|
|
9
|
+
const log = logger(`webtransport:http2webtransportsession(${pid})`)
|
|
10
|
+
|
|
11
|
+
let processnextTick = (/** @type {{ (args: any[]): any }} */ func) =>
|
|
12
|
+
setTimeout(func, 0)
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
if (typeof process !== 'undefined') processnextTick = process.nextTick
|
|
15
|
+
|
|
16
|
+
export class Http2WebTransportSession {
|
|
17
|
+
/**
|
|
18
|
+
* @param {{stream?: Http2Stream, ws?: WebSocket, isclient:boolean,
|
|
19
|
+
* createParser:import('../types.js').CreateParserFunction
|
|
20
|
+
* sendWindowOffset: Number,
|
|
21
|
+
* receiveWindowOffset: Number,
|
|
22
|
+
* shouldAutoTuneReceiveWindow: boolean
|
|
23
|
+
* receiveWindowSizeLimit: Number}} args
|
|
24
|
+
* */
|
|
25
|
+
constructor({
|
|
26
|
+
stream,
|
|
27
|
+
ws,
|
|
28
|
+
isclient,
|
|
29
|
+
createParser,
|
|
30
|
+
sendWindowOffset,
|
|
31
|
+
receiveWindowOffset,
|
|
32
|
+
shouldAutoTuneReceiveWindow,
|
|
33
|
+
receiveWindowSizeLimit
|
|
34
|
+
}) {
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
this.jsobj = undefined // the creator will set this
|
|
37
|
+
if (stream) {
|
|
38
|
+
this.stream = stream
|
|
39
|
+
} else if (ws) {
|
|
40
|
+
this.ws = ws
|
|
41
|
+
} else throw new Error('Neither stream or websocket supplied')
|
|
42
|
+
this.capsParser = createParser(this)
|
|
43
|
+
this.unidiId = 0
|
|
44
|
+
this.bidiId = 0
|
|
45
|
+
this.isclient = isclient
|
|
46
|
+
this.flowController = new FlowController({
|
|
47
|
+
tocontrol: this,
|
|
48
|
+
sendWindowOffset,
|
|
49
|
+
receiveWindowOffset,
|
|
50
|
+
shouldAutoTuneReceiveWindow,
|
|
51
|
+
receiveWindowSizeLimit
|
|
52
|
+
})
|
|
53
|
+
if (stream) {
|
|
54
|
+
if (isclient) {
|
|
55
|
+
stream.on('response', (headers) => {
|
|
56
|
+
processnextTick(() => {
|
|
57
|
+
if (headers[':status'] === 200) {
|
|
58
|
+
// on ready
|
|
59
|
+
this.jsobj.onReady({})
|
|
60
|
+
} else {
|
|
61
|
+
this.jsobj.onClose({
|
|
62
|
+
errorcode: headers[':status'],
|
|
63
|
+
error: 'Session stream errored'
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
} else {
|
|
69
|
+
processnextTick(() => {
|
|
70
|
+
this.jsobj.onReady({})
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
sendInitialParameters() {
|
|
77
|
+
this.flowController.sendWindowUpdate()
|
|
78
|
+
this.capsParser.writeCapsule({
|
|
79
|
+
type: ParserBase.WT_MAX_STREAMS_BIDI,
|
|
80
|
+
headerVints: [0xffffff],
|
|
81
|
+
payload: undefined
|
|
82
|
+
})
|
|
83
|
+
this.capsParser.writeCapsule({
|
|
84
|
+
type: ParserBase.WT_MAX_STREAMS_UNIDI,
|
|
85
|
+
headerVints: [0xffffff],
|
|
86
|
+
payload: undefined
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {Uint8Array} chunk
|
|
92
|
+
*/
|
|
93
|
+
writeDatagram(chunk) {
|
|
94
|
+
this.capsParser.writeCapsule({
|
|
95
|
+
type: ParserBase.DATAGRAM,
|
|
96
|
+
headerVints: [],
|
|
97
|
+
payload: chunk
|
|
98
|
+
})
|
|
99
|
+
processnextTick(() => {
|
|
100
|
+
this.jsobj.onDatagramSend({})
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
orderUnidiStream() {
|
|
105
|
+
let streamid = 0x2 | (this.unidiId << 2)
|
|
106
|
+
if (this.isclient) streamid = streamid | 0x1
|
|
107
|
+
this.capsParser.writeCapsule({
|
|
108
|
+
type: ParserBase.WT_STREAM_WOFIN,
|
|
109
|
+
headerVints: [streamid],
|
|
110
|
+
payload: undefined
|
|
111
|
+
})
|
|
112
|
+
this.capsParser.newStream(streamid)
|
|
113
|
+
this.unidiId++
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
orderBidiStream() {
|
|
117
|
+
let streamid = 0x0 | (this.bidiId << 2)
|
|
118
|
+
if (this.isclient) streamid = streamid | 0x1
|
|
119
|
+
this.capsParser.writeCapsule({
|
|
120
|
+
type: ParserBase.WT_STREAM_WOFIN,
|
|
121
|
+
headerVints: [streamid],
|
|
122
|
+
payload: undefined
|
|
123
|
+
})
|
|
124
|
+
this.capsParser.newStream(streamid)
|
|
125
|
+
this.bidiId++
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
orderSessionStats() {
|
|
129
|
+
this.jsobj.onSessionStats({
|
|
130
|
+
timestamp: 0,
|
|
131
|
+
expiredOutgoing: 0n,
|
|
132
|
+
lostOutgoing: 0n,
|
|
133
|
+
// non Datagram
|
|
134
|
+
minRtt: 0,
|
|
135
|
+
smoothedRtt: 0,
|
|
136
|
+
rttVariation: 0,
|
|
137
|
+
estimatedSendRateBps: 0n
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
orderDatagramStats() {
|
|
142
|
+
this.jsobj.onDatagramStats({
|
|
143
|
+
timestamp: 0,
|
|
144
|
+
expiredOutgoing: 0n,
|
|
145
|
+
lostOutgoing: 0n
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
* @returns {void}
|
|
151
|
+
*/
|
|
152
|
+
notifySessionDraining() {}
|
|
153
|
+
/**
|
|
154
|
+
* @param {{ code: number, reason: string }} arg
|
|
155
|
+
*/
|
|
156
|
+
close({ code, reason }) {
|
|
157
|
+
this.capsParser.sendClose({ code, reason }) // this includes for ws closing the session!
|
|
158
|
+
// what to do with the reason
|
|
159
|
+
if (this.stream) {
|
|
160
|
+
if (this.stream.close) {
|
|
161
|
+
this.stream.close(code)
|
|
162
|
+
this.stream.destroy()
|
|
163
|
+
} else if (this.stream.end) this.stream.end()
|
|
164
|
+
else throw new Error('http2:session not close method')
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {bigint} windowOffset
|
|
170
|
+
*/
|
|
171
|
+
sendWindowUpdate(windowOffset) {
|
|
172
|
+
this.capsParser.writeCapsule({
|
|
173
|
+
type: ParserBase.WT_MAX_DATA,
|
|
174
|
+
headerVints: [windowOffset],
|
|
175
|
+
payload: undefined
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @param {bigint} pos
|
|
181
|
+
*/
|
|
182
|
+
reportBlocked(pos) {
|
|
183
|
+
log('Session was blocked at:', pos)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {bigint} windowOffset
|
|
188
|
+
*/
|
|
189
|
+
sendBlocked(windowOffset) {
|
|
190
|
+
this.capsParser.writeCapsule({
|
|
191
|
+
type: ParserBase.WT_DATA_BLOCKED,
|
|
192
|
+
headerVints: [windowOffset],
|
|
193
|
+
payload: undefined
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
connected() {
|
|
198
|
+
return this.jsobj.state === 'connected'
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @param {{ code: number, reason: string }} arg
|
|
203
|
+
*/
|
|
204
|
+
closeConnection({ code, reason }) {
|
|
205
|
+
console.trace()
|
|
206
|
+
// called in case of failure in parsing or flowcontrol
|
|
207
|
+
this.jsobj.onClose({
|
|
208
|
+
errorcode: code,
|
|
209
|
+
error: reason
|
|
210
|
+
})
|
|
211
|
+
this.close({ code, reason })
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
smoothedRtt() {
|
|
215
|
+
if (this.stream) {
|
|
216
|
+
// we are on node
|
|
217
|
+
// @ts-ignore
|
|
218
|
+
return this.stream.session?.WTrtt || 26
|
|
219
|
+
} else if (this.ws) {
|
|
220
|
+
// we are at the Browser, so we use the connection rtt?
|
|
221
|
+
// @ts-ignore
|
|
222
|
+
return navigator?.connection?.rtt || 26
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|