Skip to content

Commit

Permalink
feat: buil factory
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 18, 2023
1 parent 58fc027 commit 31671c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const {
kHTTP2Session,
kHTTP2SessionState,
kHTTP2BuildRequest,
kHTTP2CopyHeaders
kHTTP2CopyHeaders,
kHTTP1BuildRequest
} = require('./core/symbols')
const {
constants: {
Expand Down Expand Up @@ -337,8 +338,8 @@ class Client extends DispatcherBase {
const origin = opts.origin || this[kUrl].origin

let request
if (this[kHTTP2Session]) request = Request[kHTTP2BuildRequest](origin, opts, handler)
else request = new Request(origin, opts, handler)
if (this[kHTTPConnVersion] === 'h2') request = Request[kHTTP2BuildRequest](origin, opts, handler)
else request = Request[kHTTP1BuildRequest](origin, opts, handler)

this[kQueue].push(request)
if (this[kResuming]) {
Expand Down Expand Up @@ -1234,10 +1235,10 @@ async function connect (client) {
socket[kReset] = false
socket[kBlocking] = false
socket[kParser] = new Parser(client, socket, llhttpInstance)
socket[kCounter] = 0
socket[kMaxRequests] = client[kMaxRequests]
}

socket[kCounter] = 0
socket[kMaxRequests] = client[kMaxRequests]
socket[kClient] = client
socket[kError] = null

Expand Down
8 changes: 7 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
NotSupportedError
} = require('./errors')
const assert = require('assert')
const { kHTTP2BuildRequest, kHTTP2CopyHeaders } = require('./symbols')
const { kHTTP2BuildRequest, kHTTP2CopyHeaders, kHTTP1BuildRequest } = require('./symbols')
const util = require('./util')

// tokenRegExp and headerCharRegex have been lifted from
Expand Down Expand Up @@ -284,6 +284,12 @@ class Request {
return this
}

static [kHTTP1BuildRequest] (origin, opts, handler) {
// TODO: Migrate header parsing here, to make Requests
// HTTP agnostic
return new Request(origin, opts, handler)
}

static [kHTTP2BuildRequest] (origin, opts, handler) {
const headers = opts.headers
opts = { ...opts, headers: null }
Expand Down
1 change: 1 addition & 0 deletions lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
kHTTP2Session: Symbol('http2Session'),
kHTTP2SessionState: Symbol('http2Session state'),
kHTTP2BuildRequest: Symbol('http2 build request'),
kHTTP1BuildRequest: Symbol('http1 build request'),
kHTTP2CopyHeaders: Symbol('http2 copy headers'),
kHTTPConnVersion: Symbol('http connection version')
}

0 comments on commit 31671c6

Please sign in to comment.