Skip to content

Commit

Permalink
chore: fix various codesmells (#2669)
Browse files Browse the repository at this point in the history
* chore: fix two codesmells

* fix more code smells
  • Loading branch information
Uzlopak authored Jan 30, 2024
1 parent 2144da4 commit 82477d7
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 49 deletions.
12 changes: 6 additions & 6 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ class Client extends DispatcherBase {
// TODO: for H2 we need to gracefully flush the remaining enqueued
// request and close each stream.
return new Promise((resolve) => {
if (!this[kSize]) {
resolve(null)
} else {
if (this[kSize]) {
this[kClosedResolve] = resolve
} else {
resolve(null)
}
})
}
Expand Down Expand Up @@ -401,10 +401,10 @@ class Client extends DispatcherBase {
this[kHTTP2SessionState] = null
}

if (!this[kSocket]) {
queueMicrotask(callback)
} else {
if (this[kSocket]) {
util.destroy(this[kSocket].on('close', callback), err)
} else {
queueMicrotask(callback)
}

resume(this)
Expand Down
29 changes: 17 additions & 12 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,11 @@ class Request {

if (value == null || value.length === 0) continue

if (headers[key]) headers[key] += `,${value}`
else headers[key] = value
if (headers[key]) {
headers[key] += `,${value}`
} else {
headers[key] = value
}
}

return headers
Expand Down Expand Up @@ -433,20 +436,22 @@ function processHeader (request, key, val, skipAppend = false) {
}
} else if (headerName === 'expect') {
throw new NotSupportedError('expect header not supported')
} else {
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
if (skipAppend) {
if (request.headers[key]) request.headers[key] += `,${processHeaderValue(key, val[i], skipAppend)}`
else request.headers[key] = processHeaderValue(key, val[i], skipAppend)
} else if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
if (skipAppend) {
if (request.headers[key]) {
request.headers[key] += `,${processHeaderValue(key, val[i], skipAppend)}`
} else {
request.headers += processHeaderValue(key, val[i])
request.headers[key] = processHeaderValue(key, val[i], skipAppend)
}
} else {
request.headers += processHeaderValue(key, val[i])
}
} else {
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
else request.headers += processHeaderValue(key, val)
}
} else if (skipAppend) {
request.headers[key] = processHeaderValue(key, val, skipAppend)
} else {
request.headers += processHeaderValue(key, val)
}
}

Expand Down
8 changes: 3 additions & 5 deletions lib/core/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ class TstNode {
} else {
this.left = new TstNode(key, value, index)
}
} else if (this.right !== null) {
this.right.add(key, value, index)
} else {
if (this.right !== null) {
this.right.add(key, value, index)
} else {
this.right = new TstNode(key, value, index)
}
this.right = new TstNode(key, value, index)
}
}

Expand Down
14 changes: 7 additions & 7 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,19 @@ function parseHeaders (headers, obj) {
const key = headerNameToString(headers[i])
let val = obj[key]

if (!val) {
if (val) {
if (typeof val === 'string') {
val = [val]
obj[key] = val
}
val.push(headers[i + 1].toString('utf8'))
} else {
const headersValue = headers[i + 1]
if (typeof headersValue === 'string') {
obj[key] = headersValue
} else {
obj[key] = Array.isArray(headersValue) ? headersValue.map(x => x.toString('utf8')) : headersValue.toString('utf8')
}
} else {
if (typeof val === 'string') {
val = [val]
obj[key] = val
}
val.push(headers[i + 1].toString('utf8'))
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/eventsource/eventsource-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class EventSourceStream extends Transform {
if (event[field] === undefined) {
event[field] = value
} else {
event[field] += '\n' + value
event[field] += `\n${value}`
}
break
case 'retry':
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function extractBody (object, keepalive = false) {
// Set type to `multipart/form-data; boundary=`,
// followed by the multipart/form-data boundary string generated
// by the multipart/form-data encoding algorithm.
type = 'multipart/form-data; boundary=' + boundary
type = `multipart/form-data; boundary=${boundary}`
} else if (isBlobLike(object)) {
// Blob

Expand Down Expand Up @@ -455,7 +455,7 @@ function bodyMixinMethods (instance) {
} catch (err) {
// istanbul ignore next: Unclear when new URLSearchParams can fail on a string.
// 2. If entries is failure, then throw a TypeError.
throw Object.assign(new TypeError(), { cause: err })
throw new TypeError(undefined, { cause: err })
}

// 3. Return a new FormData object whose entries are entries.
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ function processBlobParts (parts, options) {
// 2. If element is a BufferSource, get a copy of the
// bytes held by the buffer source, and append those
// bytes to bytes.
if (!element.buffer) { // ArrayBuffer
bytes.push(new Uint8Array(element))
} else {
if (element.buffer) {
bytes.push(
new Uint8Array(element.buffer, element.byteOffset, element.byteLength)
)
} else { // ArrayBuffer
bytes.push(new Uint8Array(element))
}
} else if (isBlobLike(element)) {
// 3. If element is a Blob, append the bytes it represents
Expand Down
4 changes: 1 addition & 3 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ function fetch (input, init = undefined) {
// 3. If response is a network error, then reject p with a TypeError
// and terminate these substeps.
if (response.type === 'error') {
p.reject(
Object.assign(new TypeError('fetch failed'), { cause: response.error })
)
p.reject(new TypeError('fetch failed', { cause: response.error }))
return Promise.resolve()
}

Expand Down
8 changes: 3 additions & 5 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ class Response {
try {
parsedURL = new URL(url, getGlobalOrigin())
} catch (err) {
throw Object.assign(new TypeError('Failed to parse URL from ' + url), {
cause: err
})
throw new TypeError(`Failed to parse URL from ${url}`, { cause: err })
}

// 3. If status is not a redirect status, then throw a RangeError.
if (!redirectStatusSet.has(status)) {
throw new RangeError('Invalid status code ' + status)
throw new RangeError(`Invalid status code ${status}`)
}

// 4. Let responseObject be the result of creating a Response object,
Expand Down Expand Up @@ -472,7 +470,7 @@ function initializeResponse (response, init, body) {
if (nullBodyStatus.includes(response.status)) {
throw webidl.errors.exception({
header: 'Response constructor',
message: 'Invalid response status code ' + response.status
message: `Invalid response status code ${response.status}`
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/handler/RetryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class RetryHandler {
let retryAfterHeader = headers != null && headers['retry-after']
if (retryAfterHeader) {
retryAfterHeader = Number(retryAfterHeader)
retryAfterHeader = isNaN(retryAfterHeader)
retryAfterHeader = Number.isNaN(retryAfterHeader)
? calculateRetryAfterHeader(retryAfterHeader)
: retryAfterHeader * 1e3 // Retry-After is in seconds
}
Expand Down
4 changes: 1 addition & 3 deletions lib/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ class ByteParser extends Writable {
}
}

if (this.#byteOffset > 0) {
continue
} else {
if (this.#byteOffset === 0) {
callback()
break
}
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function isClosed (ws) {
* @param {EventTarget} target
* @param {EventInit | undefined} eventInitDict
*/
function fireEvent (e, target, eventConstructor = Event, eventInitDict) {
function fireEvent (e, target, eventConstructor = Event, eventInitDict = {}) {
// 1. If eventConstructor is not given, then let eventConstructor be Event.

// 2. Let event be the result of creating an event given eventConstructor,
Expand Down

0 comments on commit 82477d7

Please sign in to comment.