1
1
// @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
3
3
import { decode as _base64Decode } from 'https://deno.land/[email protected] /encoding/base64.ts'
4
- import { AsyncLocalStorage as ALSCompat } from 'https://deno.land/[email protected] /node/async_hooks.ts'
4
+ import BufferCompat from 'https://deno.land/[email protected] /node/buffer.ts'
5
+ import EventsCompat from 'https://deno.land/[email protected] /node/events.ts'
6
+ import AsyncHooksCompat from 'https://deno.land/[email protected] /node/async_hooks.ts'
7
+ import AssertCompat from 'https://deno.land/[email protected] /node/assert.ts'
8
+ import UtilCompat from 'https://deno.land/[email protected] /node/util.ts'
5
9
6
10
/**
7
11
* 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'
18
22
let _ENTRIES = { }
19
23
20
24
// Next.js expects this as a global
21
- globalThis . AsyncLocalStorage = ALSCompat
25
+ globalThis . AsyncLocalStorage = AsyncHooksCompat . AsyncLocalStorage
22
26
23
27
// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
24
28
if ( ! ( 'getAll' in Headers . prototype ) ) {
@@ -48,6 +52,26 @@ const fetch /* type {typeof globalThis.fetch} */ = async (url, init) => {
48
52
}
49
53
}
50
54
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
+
51
75
// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis
52
76
// See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills
53
77
const self = { ...globalThis , fetch }
0 commit comments