Unverified Commit 6b09bc86 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

[edge] limit the api surface of `util` (#47292)

This makes sure that what works locally will work in production.
parent 7cc41595
......@@ -858,17 +858,19 @@ export default class MiddlewarePlugin {
}
}
const supportedEdgePolyfills = new Set([
export const SUPPORTED_NATIVE_MODULES = [
'buffer',
'events',
'assert',
'util',
'async_hooks',
])
] as const
const supportedEdgePolyfills = new Set<string>(SUPPORTED_NATIVE_MODULES)
export function getEdgePolyfilledModules() {
const records: Record<string, string> = {}
for (const mod of supportedEdgePolyfills) {
for (const mod of SUPPORTED_NATIVE_MODULES) {
records[mod] = `commonjs node:${mod}`
records[`node:${mod}`] = `commonjs node:${mod}`
}
......
......@@ -13,7 +13,10 @@ import { readFileSync, promises as fs } from 'fs'
import { validateURL } from '../utils'
import { pick } from '../../../lib/pick'
import { fetchInlineAsset } from './fetch-inline-assets'
import type { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'
import type {
EdgeFunctionDefinition,
SUPPORTED_NATIVE_MODULES,
} from '../../../build/webpack/plugins/middleware-plugin'
import { UnwrapPromise } from '../../../lib/coalesced-function'
import { runInContext } from 'vm'
import BufferImplementation from 'node:buffer'
......@@ -144,20 +147,19 @@ function getDecorateUnhandledRejection(runtime: EdgeRuntime) {
}
}
const NativeModuleMap = new Map<string, unknown>([
[
'node:buffer',
pick(BufferImplementation, [
const NativeModuleMap = (() => {
const mods: Record<
`node:${typeof SUPPORTED_NATIVE_MODULES[number]}`,
unknown
> = {
'node:buffer': pick(BufferImplementation, [
'constants',
'kMaxLength',
'kStringMaxLength',
'Buffer',
'SlowBuffer',
]),
],
[
'node:events',
pick(EventsImplementation, [
'node:events': pick(EventsImplementation, [
'EventEmitter',
'captureRejectionSymbol',
'defaultMaxListeners',
......@@ -166,22 +168,42 @@ const NativeModuleMap = new Map<string, unknown>([
'on',
'once',
]),
],
[
'node:async_hooks',
pick(AsyncHooksImplementation, ['AsyncLocalStorage', 'AsyncResource']),
],
[
'node:assert',
// TODO: check if need to pick specific properties
AssertImplementation,
],
[
'node:util',
// TODO: check if need to pick specific properties
UtilImplementation,
],
])
'node:async_hooks': pick(AsyncHooksImplementation, [
'AsyncLocalStorage',
'AsyncResource',
]),
'node:assert': pick(AssertImplementation, [
'AssertionError',
'deepEqual',
'deepStrictEqual',
'doesNotMatch',
'doesNotReject',
'doesNotThrow',
'equal',
'fail',
'ifError',
'match',
'notDeepEqual',
'notDeepStrictEqual',
'notEqual',
'notStrictEqual',
'ok',
'rejects',
'strict',
'strictEqual',
'throws',
]),
'node:util': pick(UtilImplementation, [
'_extend' as any,
'callbackify',
'format',
'inherits',
'promisify',
'types',
]),
}
return new Map(Object.entries(mods))
})()
/**
* Create a module cache specific for the provided parameters. It includes
......
......@@ -15,13 +15,13 @@ createNextDescribe(
expect(json).toEqual({
'Buffer === B.Buffer': true,
encoded: Buffer.from('Hello, world!').toString('base64'),
exposedKeys: [
exposedKeys: expect.arrayContaining([
'constants',
'kMaxLength',
'kStringMaxLength',
'Buffer',
'SlowBuffer',
],
]),
})
})
......@@ -38,13 +38,13 @@ createNextDescribe(
'typeof B2.Buffer': 'function',
'typeof Buffer': 'function',
encoded: 'SGVsbG8sIHdvcmxkIQ==',
exposedKeys: [
exposedKeys: expect.arrayContaining([
'constants',
'kMaxLength',
'kStringMaxLength',
'Buffer',
'SlowBuffer',
],
]),
})
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment