Skip to content

Commit

Permalink
Explicit casting of header values, number to string
Browse files Browse the repository at this point in the history
  • Loading branch information
DraganDanicic committed Aug 24, 2024
1 parent 4e83ea3 commit 01165f7
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,6 @@ class BaseUpload {
})
}

static convertToStringHeaderValue(val) {
if (val === null || val === undefined) {
return ''
} else if (Array.isArray(val)) {
return val.join(',')
} else if (
typeof val === 'string' ||
typeof val === 'number' ||
typeof val === 'boolean' ||
typeof val === 'symbol'
) {
return val.toString()
} else if (typeof val === 'object') {
return Object.values(val).join(',')
}
}

findPreviousUploads() {
return this.options
.fingerprint(this.file, this.options)
Expand Down Expand Up @@ -395,7 +378,7 @@ class BaseUpload {
// Add metadata if values have been added
const metadata = encodeMetadata(this.options.metadata)
if (metadata !== '') {
req.setHeader('Upload-Metadata', BaseUpload.convertToStringHeaderValue(metadata))
req.setHeader('Upload-Metadata', metadata)
}

return this._sendRequest(req, null)
Expand Down Expand Up @@ -598,13 +581,13 @@ class BaseUpload {
if (this.options.uploadLengthDeferred) {
req.setHeader('Upload-Defer-Length', '1')
} else {
req.setHeader('Upload-Length', BaseUpload.convertToStringHeaderValue(this._size))
req.setHeader('Upload-Length',`${this._size}`))
}

// Add metadata if values have been added
const metadata = encodeMetadata(this.options.metadata)
if (metadata !== '') {
req.setHeader('Upload-Metadata', BaseUpload.convertToStringHeaderValue(metadata))
req.setHeader('Upload-Metadata', metadata)
}

let promise
Expand Down Expand Up @@ -771,7 +754,7 @@ class BaseUpload {
req = this._openRequest('PATCH', this.url)
}

req.setHeader('Upload-Offset', BaseUpload.convertToStringHeaderValue(this._offset))
req.setHeader('Upload-Offset', `${this._offset}`)
const promise = this._addChunkToRequest(req)

promise
Expand Down Expand Up @@ -827,7 +810,7 @@ class BaseUpload {
// upload size and can tell the tus server.
if (this.options.uploadLengthDeferred && done) {
this._size = this._offset + valueSize
req.setHeader('Upload-Length', BaseUpload.convertToStringHeaderValue(this._size))
req.setHeader('Upload-Length', `${this._size}`)
}

// The specified uploadSize might not match the actual amount of data that a source
Expand Down Expand Up @@ -990,7 +973,7 @@ function openRequest(method, url, options) {
const headers = options.headers || {}

for (const [name, value] of Object.entries(headers)) {
req.setHeader(name, BaseUpload.convertToStringHeaderValue(value))
req.setHeader(name, value)
}

if (options.addRequestId) {
Expand Down

0 comments on commit 01165f7

Please sign in to comment.