Avoid the release of Zalgo (#1295)
This commit is contained in:
committed by
delvedor
parent
f02567491f
commit
584a8799d8
@ -664,7 +664,10 @@ function ESAPI (opts) {
|
|||||||
return apis
|
return apis
|
||||||
|
|
||||||
function handleError (err, callback) {
|
function handleError (err, callback) {
|
||||||
if (callback) return callback(err, result)
|
if (callback) {
|
||||||
|
process.nextTick(callback, err, result)
|
||||||
|
return { then: noop, catch: noop, abort: noop }
|
||||||
|
}
|
||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -699,4 +702,6 @@ function lazyLoad (file, opts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function noop () {}
|
||||||
|
|
||||||
module.exports = ESAPI
|
module.exports = ESAPI
|
||||||
|
|||||||
@ -124,14 +124,28 @@ class Transport {
|
|||||||
? 0 : (typeof options.maxRetries === 'number' ? options.maxRetries : this.maxRetries)
|
? 0 : (typeof options.maxRetries === 'number' ? options.maxRetries : this.maxRetries)
|
||||||
const compression = options.compression !== undefined ? options.compression : this.compression
|
const compression = options.compression !== undefined ? options.compression : this.compression
|
||||||
var request = { abort: noop }
|
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 = () => {
|
const makeRequest = () => {
|
||||||
if (meta.aborted === true) {
|
if (meta.aborted === true) {
|
||||||
return callback(new RequestAbortedError(), result)
|
return process.nextTick(callback, new RequestAbortedError(), result)
|
||||||
}
|
}
|
||||||
meta.connection = this.getConnection({ requestId: meta.request.id })
|
meta.connection = this.getConnection({ requestId: meta.request.id })
|
||||||
if (meta.connection == null) {
|
if (meta.connection == null) {
|
||||||
return callback(new NoLivingConnectionsError(), result)
|
return process.nextTick(callback, new NoLivingConnectionsError(), result)
|
||||||
}
|
}
|
||||||
this.emit('request', null, result)
|
this.emit('request', null, result)
|
||||||
// perform the actual http request
|
// perform the actual http request
|
||||||
@ -266,7 +280,8 @@ class Transport {
|
|||||||
try {
|
try {
|
||||||
params.body = this.serializer.serialize(params.body)
|
params.body = this.serializer.serialize(params.body)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return callback(err, result)
|
process.nextTick(callback, err, result)
|
||||||
|
return transportReturn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +295,8 @@ class Transport {
|
|||||||
try {
|
try {
|
||||||
params.body = this.serializer.ndserialize(params.bulkBody)
|
params.body = this.serializer.ndserialize(params.bulkBody)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return callback(err, result)
|
process.nextTick(callback, err, result)
|
||||||
|
return transportReturn
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
params.body = params.bulkBody
|
params.body = params.bulkBody
|
||||||
@ -333,20 +349,7 @@ class Transport {
|
|||||||
makeRequest()
|
makeRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getConnection (opts) {
|
getConnection (opts) {
|
||||||
|
|||||||
@ -299,6 +299,22 @@ test('ConfigurationError (promises)', t => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('The callback with a sync error should be called in the next tick', t => {
|
||||||
|
t.plan(4)
|
||||||
|
|
||||||
|
const client = new Client({
|
||||||
|
node: 'http://localhost:9200'
|
||||||
|
})
|
||||||
|
|
||||||
|
const transportReturn = client.index({ body: { foo: 'bar' } }, (err, result) => {
|
||||||
|
t.ok(err instanceof errors.ConfigurationError)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.type(transportReturn.then, 'function')
|
||||||
|
t.type(transportReturn.catch, 'function')
|
||||||
|
t.type(transportReturn.abort, 'function')
|
||||||
|
})
|
||||||
|
|
||||||
if (Number(process.version.split('.')[0].slice(1)) >= 8) {
|
if (Number(process.version.split('.')[0].slice(1)) >= 8) {
|
||||||
require('./api-async')(test)
|
require('./api-async')(test)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2537,3 +2537,65 @@ test('Lowercase headers utilty', t => {
|
|||||||
|
|
||||||
t.strictEqual(lowerCaseHeaders(undefined), undefined)
|
t.strictEqual(lowerCaseHeaders(undefined), undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('The callback with a sync error should be called in the next tick - json', t => {
|
||||||
|
t.plan(4)
|
||||||
|
const pool = new ConnectionPool({ Connection })
|
||||||
|
pool.addConnection('http://localhost:9200')
|
||||||
|
|
||||||
|
const transport = new Transport({
|
||||||
|
emit: () => {},
|
||||||
|
connectionPool: pool,
|
||||||
|
serializer: new Serializer(),
|
||||||
|
maxRetries: 3,
|
||||||
|
requestTimeout: 30000,
|
||||||
|
sniffInterval: false,
|
||||||
|
sniffOnStart: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const body = { a: true }
|
||||||
|
body.o = body
|
||||||
|
|
||||||
|
const transportReturn = transport.request({
|
||||||
|
method: 'POST',
|
||||||
|
path: '/hello',
|
||||||
|
body
|
||||||
|
}, (err, { body }) => {
|
||||||
|
t.ok(err instanceof SerializationError)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.type(transportReturn.then, 'function')
|
||||||
|
t.type(transportReturn.catch, 'function')
|
||||||
|
t.type(transportReturn.abort, 'function')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('The callback with a sync error should be called in the next tick - ndjson', t => {
|
||||||
|
t.plan(4)
|
||||||
|
const pool = new ConnectionPool({ Connection })
|
||||||
|
pool.addConnection('http://localhost:9200')
|
||||||
|
|
||||||
|
const transport = new Transport({
|
||||||
|
emit: () => {},
|
||||||
|
connectionPool: pool,
|
||||||
|
serializer: new Serializer(),
|
||||||
|
maxRetries: 3,
|
||||||
|
requestTimeout: 30000,
|
||||||
|
sniffInterval: false,
|
||||||
|
sniffOnStart: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const field = { a: true }
|
||||||
|
field.o = field
|
||||||
|
|
||||||
|
const transportReturn = transport.request({
|
||||||
|
method: 'POST',
|
||||||
|
path: '/hello',
|
||||||
|
bulkBody: [field]
|
||||||
|
}, (err, { body }) => {
|
||||||
|
t.ok(err instanceof SerializationError)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.type(transportReturn.then, 'function')
|
||||||
|
t.type(transportReturn.catch, 'function')
|
||||||
|
t.type(transportReturn.abort, 'function')
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user