mia-platform/custom-plugin-lib

getServiceProxy does not return on 204 response and returnAs: 'BUFFER' if using node >= 13.0.0

fabio-percivaldi opened this issue · 1 comments

The feauture or bug you are proposing

getServiceProxy does not return on 204 response and returnAs: 'BUFFER'

The description of the bug or the rationale of your proposal

If the service is using a version of node >= 13.0.0 and if the getServiceProxy is used to call a DELETE on the microservice-gateway and the response is a 204 No Content, the service get stuck.

The problem seams to be in the simple-concat function called by simpleGet.concat in simple-get/index.js that got stuck on the never called data and end event.

simpleGet.concat = (opts, cb) => {
  return simpleGet(opts, (err, res) => {
    if (err) return cb(err)
    concat(res, (err, data) => {
    if (err) return cb(err)
      if (opts.json) {
        try {
          data = JSON.parse(data.toString())
        } catch (err) {
          return cb(err, res, data)
        }
      }
      cb(null, res, data)
    })
  })
}

simple-concat

module.exports = function (stream, cb) {
  var chunks = []
  stream.on('data', function (chunk) {
    chunks.push(chunk)
  })
  stream.once('end', function () {
    if (cb) cb(null, Buffer.concat(chunks))
    cb = null
  })
  stream.once('error', function (err) {
    if (cb) cb(err)
    cb = null
  })
}

A snippet of code for replicating the issue or showing the proposal usage if applicable

Teorically it should be possible to reproduce the bug using this code snippet

const query = {
    bucket: 'TestBucket1',
  }

  const customProxy = getServiceProxy(MICROSERVICE_GATEWAY_SERVICE_NAME, { port: 3001 })
  nock('http://localhost:3001', { 'encodedQueryParams': true })
    .delete('/media-storage/media-id', {})
    .query({ 'bucket': 'TestBucket1' })
    .reply(204, '', [
      'date',
      'Wed, 21 Apr 2021 16:40:26 GMT',
      'content-type',
      'application/json; charset=utf-8',
      'content-length',
      '4',
      'Connection',
      'close',
    ])

  let result
  try {
    result = await customProxy.delete('/media-storage/media-id', {}, query, {
      headers: {
        miauserproperties: '{"permissions": ["MediaStorage.TestBucket1"]}',
      },
      returnAs: 'STREAM' })
  } catch (error) {
    console.log('error', error.message)
  }

but actually i managed to reproduce the bug only calling the actual microservice gateway.

The expected result for your bug

The called endpoint should correctly return a response

Your environment

node: ^13.0.0 || ^14.0.0

custom-plugin-lib: 2.3.0

os: tested on k8s

This should be fixed with the new http client and it will be released soon. I close the issue, since the other proxies are now deprecated.