Skip to content

Commit 409354d

Browse files
committed
feat: shim additional native modules
1 parent b0b994b commit 409354d

File tree

1 file changed

+21
-8
lines changed
  • packages/runtime/src/templates/edge

1 file changed

+21
-8
lines changed

packages/runtime/src/templates/edge/shims.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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
33
import { 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'
5-
import { Buffer as BufferCompat } from 'https://deno.land/std@0.175.0/io/buffer.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'
69

710
/**
811
* These are the shims, polyfills and other kludges to make Next.js work in standards-compliant runtime.
@@ -19,7 +22,7 @@ globalThis.EdgeRuntime = 'netlify-edge'
1922
let _ENTRIES = {}
2023

2124
// Next.js expects this as a global
22-
globalThis.AsyncLocalStorage = ALSCompat
25+
globalThis.AsyncLocalStorage = AsyncHooksCompat.AsyncLocalStorage
2326

2427
// Next.js uses this extension to the Headers API implemented by Cloudflare workerd
2528
if (!('getAll' in Headers.prototype)) {
@@ -49,13 +52,23 @@ const fetch /* type {typeof globalThis.fetch} */ = async (url, init) => {
4952
}
5053
}
5154

52-
// Turbopack aliases "Buffer" to a module import, so we need to provide a shim for that
55+
// Shim native modules that Vercel makes available
5356
if (typeof require === 'undefined') {
5457
globalThis.require = (name) => {
55-
if (name === 'buffer' || name === 'node:buffer') {
56-
return { Buffer: BufferCompat }
58+
switch (name.replace(/^node:/, '')) {
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}`)
5771
}
58-
throw new ReferenceError('require is not defined')
5972
}
6073
}
6174

0 commit comments

Comments
 (0)