Debian Codegraph @

lib/dispatcher/h2c-client.js

raw
1 'use strict'
2
3 const { InvalidArgumentError } = require('../core/errors')
4 const Client = require('./client')
5
6 class H2CClient extends Client {
7 constructor (origin, clientOpts) {
8 if (typeof origin === 'string') {
9 origin = new URL(origin)
10 }
11
12 if (origin.protocol !== 'http:') {
13 throw new InvalidArgumentError(
14 'h2c-client: Only h2c protocol is supported'
15 )
16 }
17
18 const { maxConcurrentStreams, pipelining, ...opts } =
19 clientOpts ?? {}
20 const defaultMaxConcurrentStreams = maxConcurrentStreams ?? 100
21 let defaultPipelining = 100
22
23 if (
24 maxConcurrentStreams != null &&
25 (!Number.isInteger(maxConcurrentStreams) ||
26 maxConcurrentStreams < 1)
27 ) {
28 throw new InvalidArgumentError('maxConcurrentStreams must be a positive integer, greater than 0')
29 }
30
31 if (pipelining != null && Number.isInteger(pipelining) && pipelining > 0) {
32 defaultPipelining = pipelining
33 }
34
35 if (defaultPipelining > defaultMaxConcurrentStreams) {
36 throw new InvalidArgumentError(
37 'h2c-client: pipelining cannot be greater than maxConcurrentStreams'
38 )
39 }
40
41 super(origin, {
42 ...opts,
43 maxConcurrentStreams: defaultMaxConcurrentStreams,
44 pipelining: defaultPipelining,
45 allowH2: true,
46 useH2c: true
47 })
48 }
49 }
50
51 module.exports = H2CClient