Avoid the release of Zalgo (#1295)

This commit is contained in:
Tomas Della Vedova
2020-09-10 15:27:27 +02:00
committed by delvedor
parent 457c30eb89
commit 59203811dd
4 changed files with 105 additions and 19 deletions

View File

@ -124,14 +124,28 @@ class Transport {
? 0 : (typeof options.maxRetries === 'number' ? options.maxRetries : this.maxRetries)
const compression = options.compression !== undefined ? options.compression : this.compression
var request = { abort: noop }
const transportReturn = {
then (onFulfilled, onRejected) {
return p.then(onFulfilled, onRejected)
},
catch (onRejected) {
return p.catch(onRejected)
},
abort () {
meta.aborted = true
request.abort()
debug('Aborting request', params)
return this
}
}
const makeRequest = () => {
if (meta.aborted === true) {
return callback(new RequestAbortedError(), result)
return process.nextTick(callback, new RequestAbortedError(), result)
}
meta.connection = this.getConnection({ requestId: meta.request.id })
if (meta.connection == null) {
return callback(new NoLivingConnectionsError(), result)
return process.nextTick(callback, new NoLivingConnectionsError(), result)
}
this.emit('request', null, result)
// perform the actual http request
@ -266,7 +280,8 @@ class Transport {
try {
params.body = this.serializer.serialize(params.body)
} catch (err) {
return callback(err, result)
process.nextTick(callback, err, result)
return transportReturn
}
}
@ -280,7 +295,8 @@ class Transport {
try {
params.body = this.serializer.ndserialize(params.bulkBody)
} catch (err) {
return callback(err, result)
process.nextTick(callback, err, result)
return transportReturn
}
} else {
params.body = params.bulkBody
@ -333,20 +349,7 @@ class Transport {
makeRequest()
}
return {
then (onFulfilled, onRejected) {
return p.then(onFulfilled, onRejected)
},
catch (onRejected) {
return p.catch(onRejected)
},
abort () {
meta.aborted = true
request.abort()
debug('Aborting request', params)
return this
}
}
return transportReturn
}
getConnection (opts) {