11// @ts -check
2- // deno-lint-ignore-file no-var prefer-const no-unused-vars no-explicit-any
2+ // deno-lint-ignore-file prefer-const no-unused-vars
33import { decode as _base64Decode } from 'https://deno.land/std@0.175.0/encoding/base64.ts'
4- import { AsyncLocalStorage as ALSCompat } from 'https://deno.land/std@0.175.0/node/async_hooks.ts'
4+ import BufferCompat from 'https://deno.land/std@0.175.0/node/buffer.ts'
5+ import EventsCompat from 'https://deno.land/std@0.175.0/node/events.ts'
6+ import AsyncHooksCompat from 'https://deno.land/std@0.175.0/node/async_hooks.ts'
7+ import AssertCompat from 'https://deno.land/std@0.175.0/node/assert.ts'
8+ import UtilCompat from 'https://deno.land/std@0.175.0/node/util.ts'
59
610/**
711 * These are the shims, polyfills and other kludges to make Next.js work in standards-compliant runtime.
@@ -18,7 +22,7 @@ globalThis.EdgeRuntime = 'netlify-edge'
1822let _ENTRIES = { }
1923
2024// Next.js expects this as a global
21- globalThis . AsyncLocalStorage = ALSCompat
25+ globalThis . AsyncLocalStorage = AsyncHooksCompat . AsyncLocalStorage
2226
2327// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
2428if ( ! ( 'getAll' in Headers . prototype ) ) {
@@ -48,6 +52,26 @@ const fetch /* type {typeof globalThis.fetch} */ = async (url, init) => {
4852 }
4953}
5054
55+ // Shim native modules that Vercel makes available
56+ if ( typeof require === 'undefined' ) {
57+ globalThis . require = ( name ) => {
58+ switch ( name . replace ( / ^ n o d e : / , '' ) ) {
59+ case 'buffer' :
60+ return BufferCompat
61+ case 'events' :
62+ return EventsCompat
63+ case 'async_hooks' :
64+ return AsyncHooksCompat
65+ case 'assert' :
66+ return AssertCompat
67+ case 'util' :
68+ return UtilCompat
69+ default :
70+ throw new ReferenceError ( `Native module not found: ${ name } ` )
71+ }
72+ }
73+ }
74+
5175// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis
5276// See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills
5377const self = { ...globalThis , fetch }
0 commit comments