Repository: next.js
Issue: `ServerResponse.end` override in `apiResolver` doesn't follow the Liskov substitution principle
Labels: bug

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
N/A
```


### Which area(s) of Next.js are affected? (leave empty if unsure)

_No response_

### Link to the code that reproduces this issue

N/A

### To Reproduce

N/A

### Describe the Bug

[This](https://github.com/vercel/next.js/blob/7bf5dcbbbf83b96f2fd1da31288a371c17792e47/packages/next/src/server/api-utils/node.ts#L491-L505) `end` property override does not follow the Liskov substitution principle, because:

- The property being overridden has a [return type](https://nodejs.org/api/http.html#http_response_end_data_encoding_callback) of `<this>`.
- The overriding property has a return type of `void`.

There are next-auth bugs that were caused by this Next.js bug:

- https://github.com/nextauthjs/next-auth/issues/2806
- https://github.com/nextauthjs/next-auth/issues/2945
- https://github.com/nextauthjs/next-auth/issues/3321
- https://github.com/nextauthjs/next-auth/discussions/3591

# How this Next.js bug causes the above next-auth bugs:

1. When next-auth detects an invalid `state` query parameter, an exception is [thrown](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/oauth/state-handler.js#L29).
2. The exception is caught locally (within the same function) and next-auth attempts to respond with a [redirect](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/oauth/state-handler.js#L33).
3. This redirect of course results in the response stream being [ended](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/extend-res.js#L13).
4. The problem is that the [return result](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/oauth/state-handler.js#L33) (of the redirect call) is `undefined`:
    1. next-auth wraps the response `.redirect` function [here](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/extend-res.js#L33).
    2. next-auth wraps the response `.end` function [here](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/extend-res.js#L13).
    3. Next.js wraps the response `.end` function [here](https://github.com/vercel/next.js/blob/7bf5dcbbbf83b96f2fd1da31288a371c17792e47/packages/next/src/server/api-utils/node.ts#L491-L505).
5. This results in the negative branch of [this](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/index.js#L192) line being taken and crucially the `return` statement not being executed.
6. next-auth then [chugs along](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/index.js#L193) as if the response stream had not already ended, which results in the uncaught exception.

It appears as though other (next-auth) [callback handlers](https://github.com/nextauthjs/next-auth/blob/v3.29.1/src/server/lib/oauth/pkce-handler.js#L46) are affected by this bug class too.

### Expected Behavior

https://github.com/vercel/next.js/blob/7bf5dcbbbf83b96f2fd1da31288a371c17792e47/packages/next/src/server/api-utils/node.ts#L504 should be:

```Typescript
return endResponse.apply(apiRes, args)
```

### Which browser are you using? (if relevant)

_No response_

### How are you deploying your application? (if relevant)

_No response_