Skip to content
← Back to Changelog

Monday, April 3rd 2023

Improved Node.js compatibility for Edge Middleware and Edge Functions

Posted by

Avatar for javivelasco

Javi Velasco

Software Engineer, Infrastructure

Avatar for schniz

Gal Schlezinger

Software Engineer, Infrastructure

Vercel Edge Middleware and Edge Functions now support more Node.js modules. You may want to make use of these modules directly, but many of these low-level APIs are pieces of core functionality that other modules depend on. Adding support for these APIs expands the compatibility of existing npm packages.

export default function (request: Request) {
const url = new URL(request.url);
const message = url.searchParams.get("message") ?? '';
return Response.json({
decoded: Buffer.from(message, "base64").toString(),
message,
});
}
export const config = {
runtime: "edge",
};
Edge Function using Buffer global

The following APIs are now supported:

  • AsyncLocalStorage: Support for maintaining data for an invocation between different asynchronous execution contexts, which allows you to pass state to the context even when the function is hot and module context is preserved.
  • EventEmitter: A flexible API to build event-driven systems that serves as a core building block for communication between libraries that control I/O and listeners that process data when events occur.
  • Buffer: The most common way of handling binary data in Node.js, available globally or importable from buffer.
  • assert: A set of assertion functions to validate invariants and logical rules that are very useful to explicitly test assumptions in your code path that need to run in Edge Functions.
  • util.promisify and util.callbackify: A helper function to convert a callback-style function signature into one that returns a Promise, and a helper function to convert a function that returns a Promise into one that accepts a callback.
  • util.types: A set of functions to validate that objects are of a given type.

You can take advantage of these additional APIs in Edge Middleware and Edge Functions with your next deployment. To deploy Edge Functions on Vercel you can get started with any framework or one of our templates.

Post