Skip to content

Commit

Permalink
fix: run asserts first if possible (#3541) (#3674)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3faf140)

Co-authored-by: Aras Abbasi <[email protected]>
  • Loading branch information
github-actions[bot] and Uzlopak authored Oct 4, 2024
1 parent 6764626 commit a7bffd4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/api/api-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class UpgradeHandler extends AsyncResource {
}

onUpgrade (statusCode, rawHeaders, socket) {
const { callback, opaque, context } = this
assert(statusCode === 101)

assert.strictEqual(statusCode, 101)
const { callback, opaque, context } = this

removeSignal(this)

Expand Down
4 changes: 2 additions & 2 deletions lib/core/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
servername = servername || options.servername || util.getServerName(host) || null

const sessionKey = servername || hostname
const session = customSession || sessionCache.get(sessionKey) || null

assert(sessionKey)

const session = customSession || sessionCache.get(sessionKey) || null

socket = tls.connect({
highWaterMark: 16384, // TLS in node can't have bigger HWM anyway...
...options,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function getServerName (host) {
return null
}

assert.strictEqual(typeof host, 'string')
assert(typeof host === 'string')

const servername = getHostname(host)
if (net.isIP(servername)) {
Expand Down
41 changes: 20 additions & 21 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,35 @@ async function lazyllhttp () {
return 0
},
wasm_on_status: (p, at, len) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
const start = at - currentBufferPtr + currentBufferRef.byteOffset
return currentParser.onStatus(new FastBuffer(currentBufferRef.buffer, start, len)) || 0
},
wasm_on_message_begin: (p) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
return currentParser.onMessageBegin() || 0
},
wasm_on_header_field: (p, at, len) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
const start = at - currentBufferPtr + currentBufferRef.byteOffset
return currentParser.onHeaderField(new FastBuffer(currentBufferRef.buffer, start, len)) || 0
},
wasm_on_header_value: (p, at, len) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
const start = at - currentBufferPtr + currentBufferRef.byteOffset
return currentParser.onHeaderValue(new FastBuffer(currentBufferRef.buffer, start, len)) || 0
},
wasm_on_headers_complete: (p, statusCode, upgrade, shouldKeepAlive) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
return currentParser.onHeadersComplete(statusCode, Boolean(upgrade), Boolean(shouldKeepAlive)) || 0
},
wasm_on_body: (p, at, len) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
const start = at - currentBufferPtr + currentBufferRef.byteOffset
return currentParser.onBody(new FastBuffer(currentBufferRef.buffer, start, len)) || 0
},
wasm_on_message_complete: (p) => {
assert.strictEqual(currentParser.ptr, p)
assert(currentParser.ptr === p)
return currentParser.onMessageComplete() || 0
}

Expand Down Expand Up @@ -363,20 +363,19 @@ class Parser {
const { upgrade, client, socket, headers, statusCode } = this

assert(upgrade)
assert(client[kSocket] === socket)
assert(!socket.destroyed)
assert(!this.paused)
assert((headers.length & 1) === 0)

const request = client[kQueue][client[kRunningIdx]]
assert(request)

assert(!socket.destroyed)
assert(socket === client[kSocket])
assert(!this.paused)
assert(request.upgrade || request.method === 'CONNECT')

this.statusCode = null
this.statusText = ''
this.shouldKeepAlive = null

assert(this.headers.length % 2 === 0)
this.headers = []
this.headersSize = 0

Expand Down Expand Up @@ -433,7 +432,7 @@ class Parser {
return -1
}

assert.strictEqual(this.timeoutType, TIMEOUT_HEADERS)
assert(this.timeoutType === TIMEOUT_HEADERS)

this.statusCode = statusCode
this.shouldKeepAlive = (
Expand Down Expand Up @@ -466,7 +465,7 @@ class Parser {
return 2
}

assert(this.headers.length % 2 === 0)
assert((this.headers.length & 1) === 0)
this.headers = []
this.headersSize = 0

Expand Down Expand Up @@ -523,7 +522,7 @@ class Parser {
const request = client[kQueue][client[kRunningIdx]]
assert(request)

assert.strictEqual(this.timeoutType, TIMEOUT_BODY)
assert(this.timeoutType === TIMEOUT_BODY)
if (this.timeout) {
// istanbul ignore else: only for jest
if (this.timeout.refresh) {
Expand Down Expand Up @@ -556,19 +555,19 @@ class Parser {
return
}

assert(statusCode >= 100)
assert((this.headers.length & 1) === 0)

const request = client[kQueue][client[kRunningIdx]]
assert(request)

assert(statusCode >= 100)

this.statusCode = null
this.statusText = ''
this.bytesRead = 0
this.contentLength = ''
this.keepAlive = ''
this.connection = ''

assert(this.headers.length % 2 === 0)
this.headers = []
this.headersSize = 0

Expand All @@ -587,7 +586,7 @@ class Parser {
client[kQueue][client[kRunningIdx]++] = null

if (socket[kWriting]) {
assert.strictEqual(client[kRunning], 0)
assert(client[kRunning] === 0)
// Response completed before request.
util.destroy(socket, new InformationalError('reset'))
return constants.ERROR.PAUSED
Expand Down Expand Up @@ -646,10 +645,10 @@ async function connectH1 (client, socket) {
socket[kParser] = new Parser(client, socket, llhttpInstance)

addListener(socket, 'error', function (err) {
const parser = this[kParser]

assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')

const parser = this[kParser]

// On Mac OS, we get an ECONNRESET even if there is a full body to be forwarded
// to the user.
if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
Expand Down
4 changes: 4 additions & 0 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ function onError (client, err) {
}
}

/**
* @param {Client} client
* @returns
*/
async function connect (client) {
assert(!client[kConnecting])
assert(!client[kHTTPContext])
Expand Down

0 comments on commit a7bffd4

Please sign in to comment.