nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.7k stars 7.54k forks source link

'Error: Premature close' whene return StreamableFile #12283

Closed TN19N closed 1 year ago

TN19N commented 1 year ago

Is there an existing issue for this?

Current behavior

I am new to nestJs. i get this error [Nest] 60297 - 08/26/2023, 5:21:03 PM ERROR [ExpressAdapter] Premature close Error: Premature close at new NodeError (node:internal/errors:405:5) at ServerResponse.onclose (node:internal/streams/end-of-stream:154:30) at ServerResponse.emit (node:events:526:35) at emitCloseNT (node:_http_server:996:10) at Socket.onServerResponseClose (node:_http_server:278:5) at Socket.emit (node:events:526:35) at TCP.<anonymous> (node:net:323:12) this is my code ` @Get('avatar') @HttpCode(HttpStatus.OK) @ApiQuery({ name: 'id', required: false }) @ApiConsumes('multipart/form-data') @Header('Content-Disposition', 'inline') async getAvatar( @GetUserId() userId: string, @Res({ passthrough: true }) response: Response, @Query('id') id?: string, ) { const user = await this.userRepository.getUserById(id ?? userId);

if (!user) {
  throw new NotFoundException(`User not found`);
}

const { avatarType } = await this.userRepository.getProfile(id ?? userId);

response.set('Content-Type', avatarType);
return new StreamableFile(
  createReadStream(join(process.cwd(), `./upload/${user.id}`))
);

} `

Minimum reproduction code

i provided snipts code.

Steps to reproduce

No response

Expected behavior

i change it to express version and it works fine ` @Get('avatar') @HttpCode(HttpStatus.OK) @ApiQuery({ name: 'id', required: false }) @ApiConsumes('multipart/form-data') async getAvatar( @GetUserId() userId: string, @Res() response: Response, @Query('id') id?: string, ) { const user = await this.userRepository.getUserById(id ?? userId);

if (!user) {
  throw new NotFoundException(User not found);
}

const { avatarType } = await this.userRepository.getProfile(id ?? userId);

response
  .contentType(avatarType)
  .download(join(process.cwd(), ./upload/${user.id}));

} `

Package

Other package

No response

NestJS version

10.1.16

Packages versions

  "dependencies": {
    "axios": "^1.4.0",
    "framer-motion": "^10.15.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router": "^6.14.2",
    "react-router-dom": "^6.14.2",
    "react-tsparticles": "^2.11.0",
    "tsparticles-engine": "^2.11.1",
    "tsparticles-slim": "^2.11.1",
    "@chakra-ui/react": "^2.0.0",
    "socket.io-client": "^4.7.2",
    "lodash": "^4.17.21",
    "react-p5": "^1.3.35"
  },
  "devDependencies": {
    "@types/node": "^20.5.0",
    "@types/react": "^18.2.14",
    "@types/react-dom": "^18.2.6",
    "@typescript-eslint/eslint-plugin": "^5.61.0",
    "@typescript-eslint/parser": "^5.61.0",
    "@vitejs/plugin-react": "^4.0.1",
    "autoprefixer": "^10.4.14",
    "eslint": "^8.44.0",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.1",
    "postcss": "^8.4.26",
    "tailwindcss": "^3.3.3",
    "typescript": "^5.0.2",
    "vite": "^4.4.8"
  }

Node.js version

18.17.0

In which operating systems have you tested?

Other

i asked in Discord, and I got a response from the moderator this could be an error.

jmcdo29 commented 1 year ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

why reproductions are required

TN19N commented 1 year ago

failed to provide a minimum reproduction