@fails-components/webtransport 1.6.8 → 1.6.9

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.
@@ -5,18 +5,6 @@ import { PriorityScheduler } from './priorityscheduler.js'
5
5
  const pid = typeof process !== 'undefined' ? process.pid : 0
6
6
  const log = logger(`webtransport:parserbase(${pid})`)
7
7
 
8
- /**
9
- * @param{Number|bigint} int
10
- * @returns {Number}
11
- */
12
- export function lengthVarInt(int) {
13
- if (BigInt(int) < 64n) return 1
14
- if (BigInt(int) < 16384n) return 2
15
- if (BigInt(int) < 1073741824n) return 4
16
- /* if (BigInt(int) < 4611686018427387904 ) */
17
- return 8
18
- }
19
-
20
8
  export class ParserBase {
21
9
  static PADDING = 0x190b4d38
22
10
  static WT_RESET_STREAM = 0x190b4d39
@@ -66,7 +54,7 @@ export class ParserBase {
66
54
 
67
55
  /**
68
56
  * @abstract
69
- * @param {Buffer|Uint8Array} data
57
+ * @param {Uint8Array[]} data
70
58
  */
71
59
  // eslint-disable-next-line no-unused-vars
72
60
  parseData(data) {
@@ -282,10 +270,17 @@ export class ParserBase {
282
270
  * @param {{code: number, reason: string}} opts
283
271
  */
284
272
  onCloseWebTransportSession({ code, reason }) {
285
- this.session.jsobj.onClose({
286
- errorcode: code,
287
- error: reason
288
- })
273
+ if (
274
+ !(
275
+ this.session.jsobj.state === 'failed' ||
276
+ this.session.jsobj.state === 'closed'
277
+ )
278
+ ) {
279
+ this.session.jsobj.onClose({
280
+ errorcode: code,
281
+ error: reason
282
+ })
283
+ }
289
284
  this.closeHttp2Stream(code) // is this necessary
290
285
  }
291
286
 
@@ -4,68 +4,6 @@ import { ParserBase } from './parserbase.js'
4
4
  * @typedef {import('node:http2').Http2Stream} Http2Stream
5
5
  */
6
6
 
7
- /**
8
- * @param{{offset: Number, buffer: Buffer, size: Number}} bs
9
- */
10
-
11
- export function readUint32(bs) {
12
- if (bs.offset + 4 > bs.size) return undefined
13
- const toret = bs.buffer.readUInt32BE(bs.offset)
14
- bs.offset += 4
15
- return toret
16
- }
17
-
18
- /**
19
- * @param{{offset: Number, buffer: Buffer, size: Number}} bs
20
- */
21
- export function readVarInt(bs) {
22
- if (bs.offset + 1 > bs.size) return undefined
23
- let val = BigInt(bs.buffer.readUInt8(bs.offset))
24
- bs.offset++
25
- const prefix = Number(val) >>> 6
26
- const intlength = 1 << prefix
27
-
28
- if (bs.offset + intlength - 1 > bs.size) {
29
- return undefined
30
- }
31
- val = val & 0x3fn
32
- for (let i = 0; i < intlength - 1; i++) {
33
- val = (val << 8n) | BigInt(bs.buffer.readUInt8(bs.offset))
34
- bs.offset++
35
- }
36
- return val
37
- }
38
-
39
- /**
40
- * @param{{offset: Number, buffer: Buffer, size: Number}} bs
41
- * @param{Number|bigint} int
42
- */
43
- export function writeVarInt(bs, int) {
44
- let numbytes = 8n
45
- let msb = 0xc0n
46
- const bint = BigInt(int)
47
- if (bint < 64) {
48
- numbytes = 1n
49
- msb = 0x0n
50
- } else if (bint < 16384) {
51
- numbytes = 2n
52
- msb = 0x40n
53
- } else if (bint < 1073741824) {
54
- numbytes = 4n
55
- msb = 0x80n
56
- }
57
- bs.buffer.writeUInt8(
58
- Number(msb | ((bint >> ((numbytes - 1n) * 8n)) & 0xffn)),
59
- bs.offset
60
- )
61
- bs.offset++
62
-
63
- for (let i = numbytes - 2n; i >= 0; i--) {
64
- bs.buffer.writeUInt8(Number((bint >> (i * 8n)) & 0xffn), bs.offset)
65
- bs.offset++
66
- }
67
- }
68
-
69
7
  export class ParserBaseHttp2 extends ParserBase {
70
8
  /**
71
9
  * @param {import('../types').ParserHttp2Init} stream
@@ -96,7 +34,7 @@ export class ParserBaseHttp2 extends ParserBase {
96
34
  this.stream.on('readable', () => {
97
35
  let data
98
36
  while ((data = this.stream.read()) !== null) {
99
- this.parseData(data)
37
+ this.parseData([data])
100
38
  }
101
39
  })
102
40
 
@@ -110,10 +48,17 @@ export class ParserBaseHttp2 extends ParserBase {
110
48
  * @param {Error} error
111
49
  */ (error) => {
112
50
  // readable error
113
- this.session.jsobj.onClose({
114
- errorcode: this.stream.rstCode,
115
- error: error.toString()
116
- })
51
+ if (
52
+ !(
53
+ this.session.jsobj.state === 'failed' ||
54
+ this.session.jsobj.state === 'closed'
55
+ )
56
+ ) {
57
+ this.session.jsobj.onClose({
58
+ errorcode: this.stream.rstCode,
59
+ error: error.toString()
60
+ })
61
+ }
117
62
  }
118
63
  )
119
64
 
@@ -92,17 +92,26 @@ export class Http2WebTransportStream {
92
92
 
93
93
  /**
94
94
  * @param {Object} obj
95
- * @param {Uint8Array|undefined} obj.data
95
+ * @param {Uint8Array[]|undefined} obj.data
96
96
  * @param {Boolean} obj.fin
97
97
  */
98
98
  recvData({ data, fin }) {
99
- this.incomdata.push({ data, fin })
100
- if (data && data?.byteLength > 0) {
101
- const checkstream = this.flowController.updateHighestReceivedOffset(
102
- data?.byteLength
103
- )
99
+ let datasize = 0
100
+ if (data) {
101
+ const newincomdata = data.map((el, ind, array) => ({
102
+ data: el,
103
+ fin: fin && ind + 1 === array.length
104
+ }))
105
+ datasize = data.reduce((acc, curVal) => acc + curVal.byteLength, 0)
106
+ this.incomdata.push(...newincomdata)
107
+ } else if (fin) {
108
+ this.incomdata.push({ data: undefined, fin })
109
+ }
110
+ if (datasize > 0) {
111
+ const checkstream =
112
+ this.flowController.updateHighestReceivedOffset(datasize)
104
113
  const checksession =
105
- this.sessionFlowController.updateHighestReceivedOffset(data?.byteLength)
114
+ this.sessionFlowController.updateHighestReceivedOffset(datasize)
106
115
  if (checksession && checkstream) {
107
116
  // As the highest received offset has changed, check to see if this is a
108
117
  // violation of flow control.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fails-components/webtransport",
3
- "version": "1.6.8",
3
+ "version": "1.6.9",
4
4
  "description": "A component to add webtransport support (server and client) to node.js using libquiche",
5
5
  "exports": {
6
6
  ".": {