Skip to content

Missing piping for stream responses #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mboutay opened this issue Mar 19, 2025 · 0 comments
Open

Missing piping for stream responses #464

mboutay opened this issue Mar 19, 2025 · 0 comments

Comments

@mboutay
Copy link

mboutay commented Mar 19, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant