Commit Graph

19 Commits

Author SHA1 Message Date
Nathan Rajlich
139e8cdb17 [node] Use vitest for unit tests (#11631)
Similar to #11302, but for the `@vercel/node` package.
2024-05-23 18:20:18 +00:00
Kiko Beats
a5ea04154b Make waitUntil consistent for Node.js & Edge (#11553)
This PR makes `waitUntil` consistent when interacting with it via `vc
dev`. That includes:

**be possible to call `waitUntil` from the context second argument in
web handlers functions (Node.js & Edge)**:

```js
export function GET(request, { waitUntil }) {
  waitUntil(fetch('https://vercel.com'));
  return new Response('OK');
}
```

**be possible to call `waitUntil` imported from `@vercel/functions`**
(Node.js & Edge):

```js
import { waitUntil } from '@vercel/functions';

export function GET(request) {
  waitUntil(fetch('https://vercel.com'));
  return new Response('OK');
}
```

Additionally, `@vercel/functions` can be adopted for other framework to
take advantage of this pattern at Vercel.

---------

Co-authored-by: Javi Velasco <javier.velasco86@gmail.com>
2024-05-07 15:36:43 +02:00
Kiko Beats
10e200e0bf build: upgrade edge-runtime (#11148)
Upgrade Edge Runtime and related dependencies 🙂

---------

Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
2024-02-13 11:20:11 -06:00
Seiya Nuta
c536a74bc9 [node] Await waitUntil promises to resolve before exiting (#10915)
Say you have a middleware (`middleware.js`) that looks like this:

```js
export async default function middleware(req, ctx) {
  ctx.waitUntil(tooLongFunction())
}

async function tooLongFunction() {
  console.log('tooLongFunction started')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  console.log('tooLongFunction finished')
}
```

When you run this middleware locally with `vercel dev`, you won't see the `tooLongFunction finished` message because the server process is killed right after finishing the response. While Edge Runtime's `server.close()`, which awaits all `waitUntil` promises, is called, but `exit-hook`'s callback in CLI doesn't wait for the `close` promise to resolve.

This PR fixes this by allowing an optional graceful shutdown callback instead of killing the process immediately.
2023-12-21 10:11:34 +00:00
Ethan Arrowood
89c1e03233 [node] swap undici.fetch for undici.request in serverless-handler.mts (#10767)
In a recent undici update, setting the `host` header for fetch requests became invalid (https://github.com/nodejs/undici/pull/2322). 

We relied on this in order to proxy serverless dev server requests via `@vercel/node`. 

This PR replaces the usage of `undici.fetch` with `undici.request`. 

It is blocked by an `undici` type change: https://github.com/nodejs/undici/pull/2373
2023-11-08 17:31:03 +00:00
Kiko Beats
3f6d99470d [node] use undici instead of node-fetch (#10387) 2023-08-24 10:06:33 +02:00
Kiko Beats
b56639b624 [node] fix: runs edge user code inside IIFE (#10220)
In this way, `self` is isolated and modify it doesn't break some Edge Runtime internals

Originally reported by @jawj at https://github.com/jawj/neon-vercel-zapatos-minimal-crash
2023-07-17 16:45:05 +00:00
Kiko Beats
5e5332fbc9 [node] fix decompress mismatching (#10184)
This PR disabled `node-fetch` default compression handling when the
response is not streamed.

The default behavior in node-fetch is to handle the compression. That's
great if you interact with node-fetch as user, but bad if you use it as
intermediate proxy server, since it's creating a mismatching related
with the body format and length expectations.

Instead, we disable compression, get the buffered response and compress
it again if needed.

---------

Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
2023-07-07 12:33:05 -06:00
Sean Massa
91406abdb0 [node] add tests to getBodyParser helper (#10109) 2023-06-15 12:31:47 -05:00
Kiko Beats
0039c8b5ce [node] Add isomorphic functions (#9947)
This PR allow using Web APIs in Serverless functions

```js
// api/serverless.js
export const GET = () => {
  return new Response(`new Response('👋 Hello from Serverless Web!)`)
}
```

More about that
https://nextjs.org/docs/app/building-your-application/routing/router-handlers

---------

Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
2023-06-06 23:15:10 +02:00
Chris Barber
fa443035f6 [tests] Add test for set-cookie with multiple cookies (#9916)
This adds the missing test for https://github.com/vercel/vercel/pull/9899.
2023-05-06 10:18:52 +00:00
JJ Kasper
cc140d6e28 [node] Fix exposing WebSocket to edge runtime and legacy next tests (#9871)
This ensures we expose WebSocket correctly for edge runtime for Node.js
v16 and v18 properly. This also fixes failing legacy Next.js tests due
to a bad `@babel/compat-data` package publish from 8 hours ago.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04R82GSDBN/p1682709850008189?thread_ts=1682707749.424459&cid=C04R82GSDBN)
x-ref: https://github.com/vercel/next.js/pull/48924
x-ref: https://github.com/vercel/vercel/pull/9860
Closes: https://github.com/vercel/vercel/pull/9870
2023-04-29 10:43:40 +02:00
Kiko Beats
09ac96ae21 [node] expose WebSocket constructor in Edge functions (#9860)
This PR exposes `WebSocket` to be used for Edge Runtime for `vc dev`.

Ideally, that's a thing we should to handle inside `edge-runtime`
dependency.

Unfortunately, that is not as easy as it sounds, since Edge Runtime is
doing a heavy use of vm module and `undici` is using some Node.js APIs
that are not available, or they are hard to make them working inside vm.

Related PR: https://github.com/vercel/edge-runtime/pull/309

While we're finishing that work, in order to don't delay users to start
using `WebSocket`, we can simply expose it directly from `undici` as
dependency.

---------

Co-authored-by: Chris Barber <chris.barber@vercel.com>
2023-04-28 23:03:35 +02:00
Chris Barber
65a6e713c8 [node] Fix ESM dependency support (#9692) 2023-04-26 12:23:43 -05:00
Kiko Beats
6dded87426 [node] Add streaming support for vc dev (#9745)
Until now, the user code response it's buffered and serialized. This is
mismatching how Vercel works these days.

This PR enables streaming response in `vc dev` for Edge/Serverless.

As part of the implementation, the `node-bridge` which spawns a process
to consume the user code is not necessary anymore.

Some necessary files (like HTTP server helpers) have been moved to live
in node builder package instead.

---------

Co-authored-by: Ethan Arrowood <ethan.arrowood@vercel.com>
Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
2023-04-19 23:56:41 +02:00
Kiko Beats
434c794713 [tests] move test files to subdirectory (#9749)
Some unit tests didn't run since Aug 2022 because they are not included as part of the testing command.

This PR introduce `unit` and `integration` folders for testing, so anything that it's placed there will be run as part of the testing command.
2023-04-04 16:49:41 +00:00
Sean Massa
0fb0601d19 [node] Improve edge-handler-template error handling (#9697) 2023-03-22 11:31:44 -05:00
Vladislav Ponomarev
af7b34aa45 [node] Handle multi-protocol in Edge Functions (#9502)
Co-authored-by: Chris Barber <chris.barber@vercel.com>
Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
fix #9501
2023-03-16 11:01:07 -05:00
Sean Massa
66fe3db888 [tests] add tests to edge handler template (#9528)
The Edge Handler wrapping logic we add during `vc dev` was not testable. This PR refactors it to be testable and adds some basic tests.
2023-03-08 22:56:51 +00:00