Skip to content

Missing piping for stream responses #464

Description

@mboutay

Context

After updating to v.6.4.10, I noticed the addition of stream responses #326 and decided to implement it on a project.

Current behavior

The Stream object is sent in the response and we receive it as follows :
{"fd":null,"path":"/workspaces/lib-nodejs/packages/base-inversify-server/test/mock/files/openapi.yaml","flags":"r","mode":438,"end":null,"bytesRead":0,"_events":{},"_readableState":{"highWaterMark":65536,"buffer":[],"bufferIndex":0,"length":0,"pipes":[],"awaitDrainWriters":null},"_eventsCount":1}

Current Code :

@controller('/openapi')
export class OpenApiController extends BaseController {
  constructor (
    @inject(TYPE.OpenApiPath) private readonly filePath: string
  ) {
    super();
  }

  @httpGet('/')
  async getOpenAPI () {
    return this.file(this.filePath);
  }
}

with the following call to the controller stream function :

protected file (path: string, statusCode = StatusCodes.OK, mimeType?: string): StreamResult {
    const stream = createReadStream(path);
    return this.stream(
      stream,
      mimeType || this.getFileMediaType(path),
      statusCode
    );
  }

Expected

I expect to receive the file's content on GET /openapi without additional code.
To do so, the handlerFactory has to pipe stream typed content inside the library.

Workaround

I added the following middleware in inversifyServer.setConfig to make it work the intended way :

app.use((req, res, next) => {
  const originalSend = res.send;
  res.send = (body) => {
    if (body instanceof Readable) {
      body.pipe(res);
      return res;
    } else {
      return originalSend.call(res, body);
    }
  };
  next();
});

Related

I saw the issue #456 that is somewhat related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions