WIP: initial prototype

- Hide auth data from Node ID
- Added support for sending streams
This commit is contained in:
delvedor
2018-11-20 18:51:23 +01:00
parent 4ad6c255d6
commit 79b4187f30
3 changed files with 42 additions and 9 deletions

View File

@ -34,7 +34,6 @@ class Transport {
}
}
// TODO: should be able to send a stream of json data
request (params, callback) {
callback = once(callback)
const result = { body: null, statusCode: null, headers: null, warnings: null }
@ -47,7 +46,7 @@ class Transport {
params.headers = params.headers || {}
// handle json body
if (params.body != null) {
if (typeof params.body !== 'string') {
if (shouldSerialize(params.body) === true) {
try {
params.body = this.serializer.serialize(params.body)
} catch (err) {
@ -55,10 +54,12 @@ class Transport {
}
}
params.headers['Content-Type'] = 'application/json'
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body)
if (isStream(params.body) === false) {
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body)
}
// handle ndjson body
} else if (params.bulkBody != null) {
if (typeof params.bulkBody !== 'string') {
if (shouldSerialize(params.bulkBody) === true) {
try {
params.body = this.serializer.ndserialize(params.bulkBody)
} catch (err) {
@ -68,7 +69,9 @@ class Transport {
params.body = params.bulkBody
}
params.headers['Content-Type'] = 'application/x-ndjson'
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body)
if (isStream(params.body) === false) {
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body)
}
}
if (this.suggestCompression === true) {
@ -239,4 +242,14 @@ function toMs (time) {
return time
}
function shouldSerialize (obj) {
return typeof obj !== 'string' &&
typeof obj.pipe !== 'function' &&
Buffer.isBuffer(obj) === false
}
function isStream (obj) {
return typeof obj.pipe === 'function'
}
module.exports = Transport