3

I was searching the internet for hours on end hoping to find any kind of built-in functionality to support byte-range requests (https://www.keycdn.com/support/byte-range-requests) within nestjs. I am coming from the .NET world where we have got something like this:

[Route("/streams/{id}")]
public IActionResult Clip(string Id) {
    ... more code ...
    return File(myDataStream, "video/mp4", enableRangeProcessing: true);
}

Is there any analogous, similiar or comparable functionality within nestjs which I had then simply overlooked or do I really have to develop this rather cumbersome yet pretty common use-case on my own?

Kind regards Samuel

Silverdust
  • 1,503
  • 14
  • 26

1 Answers1

1

NestJS supports the standard Range request:

  @Get()
  @Header('Accept-Ranges', 'bytes')
  async findOne(
    @Query('key') filePath: string,
    @Headers('Range') range: string,
    @Res({ passthrough: true }) res: Response,
  ) {
    console.log(range);
    const fileName = this.s3FilesService.getFileNameFromPath(filePath);
    const fileStream = await this.s3FilesService.findOne(filePath, range);
    res.set({
      'Content-Disposition': `attachment; filename="${fileName}"`,
    });
    return new StreamableFile(fileStream);
  }

In this case, we're passing on the byte range (e.g. bytes=204993022- or similar, etc) to the s3 getObject request, which also supports range requests. But you can return a byte range from a node readStream request as well:

https://stackoverflow.com/a/70628753/228369

chrismarx
  • 11,488
  • 9
  • 84
  • 97