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 { ...@@ -858,17 +858,19 @@ export default class MiddlewarePlugin {
} }
} }
const supportedEdgePolyfills = new Set([ export const SUPPORTED_NATIVE_MODULES = [
'buffer', 'buffer',
'events', 'events',
'assert', 'assert',
'util', 'util',
'async_hooks', 'async_hooks',
]) ] as const
const supportedEdgePolyfills = new Set<string>(SUPPORTED_NATIVE_MODULES)
export function getEdgePolyfilledModules() { export function getEdgePolyfilledModules() {
const records: Record<string, string> = {} const records: Record<string, string> = {}
for (const mod of supportedEdgePolyfills) { for (const mod of SUPPORTED_NATIVE_MODULES) {
records[mod] = `commonjs node:${mod}` records[mod] = `commonjs node:${mod}`
records[`node:${mod}`] = `commonjs node:${mod}` records[`node:${mod}`] = `commonjs node:${mod}`
} }
......
...@@ -13,7 +13,10 @@ import { readFileSync, promises as fs } from 'fs' ...@@ -13,7 +13,10 @@ import { readFileSync, promises as fs } from 'fs'
import { validateURL } from '../utils' import { validateURL } from '../utils'
import { pick } from '../../../lib/pick' import { pick } from '../../../lib/pick'
import { fetchInlineAsset } from './fetch-inline-assets' 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 { UnwrapPromise } from '../../../lib/coalesced-function'
import { runInContext } from 'vm' import { runInContext } from 'vm'
import BufferImplementation from 'node:buffer' import BufferImplementation from 'node:buffer'
...@@ -144,20 +147,19 @@ function getDecorateUnhandledRejection(runtime: EdgeRuntime) { ...@@ -144,20 +147,19 @@ function getDecorateUnhandledRejection(runtime: EdgeRuntime) {
} }
} }
const NativeModuleMap = new Map<string, unknown>([ const NativeModuleMap = (() => {
[ const mods: Record<
'node:buffer', `node:${typeof SUPPORTED_NATIVE_MODULES[number]}`,
pick(BufferImplementation, [ unknown
> = {
'node:buffer': pick(BufferImplementation, [
'constants', 'constants',
'kMaxLength', 'kMaxLength',
'kStringMaxLength', 'kStringMaxLength',
'Buffer', 'Buffer',
'SlowBuffer', 'SlowBuffer',
]), ]),
], 'node:events': pick(EventsImplementation, [
[
'node:events',
pick(EventsImplementation, [
'EventEmitter', 'EventEmitter',
'captureRejectionSymbol', 'captureRejectionSymbol',
'defaultMaxListeners', 'defaultMaxListeners',
...@@ -166,22 +168,42 @@ const NativeModuleMap = new Map<string, unknown>([ ...@@ -166,22 +168,42 @@ const NativeModuleMap = new Map<string, unknown>([
'on', 'on',
'once', 'once',
]), ]),
], 'node:async_hooks': pick(AsyncHooksImplementation, [
[ 'AsyncLocalStorage',
'node:async_hooks', 'AsyncResource',
pick(AsyncHooksImplementation, ['AsyncLocalStorage', 'AsyncResource']), ]),
], 'node:assert': pick(AssertImplementation, [
[ 'AssertionError',
'node:assert', 'deepEqual',
// TODO: check if need to pick specific properties 'deepStrictEqual',
AssertImplementation, 'doesNotMatch',
], 'doesNotReject',
[ 'doesNotThrow',
'node:util', 'equal',
// TODO: check if need to pick specific properties 'fail',
UtilImplementation, '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 * Create a module cache specific for the provided parameters. It includes
......
...@@ -15,13 +15,13 @@ createNextDescribe( ...@@ -15,13 +15,13 @@ createNextDescribe(
expect(json).toEqual({ expect(json).toEqual({
'Buffer === B.Buffer': true, 'Buffer === B.Buffer': true,
encoded: Buffer.from('Hello, world!').toString('base64'), encoded: Buffer.from('Hello, world!').toString('base64'),
exposedKeys: [ exposedKeys: expect.arrayContaining([
'constants', 'constants',
'kMaxLength', 'kMaxLength',
'kStringMaxLength', 'kStringMaxLength',
'Buffer', 'Buffer',
'SlowBuffer', 'SlowBuffer',
], ]),
}) })
}) })
...@@ -38,13 +38,13 @@ createNextDescribe( ...@@ -38,13 +38,13 @@ createNextDescribe(
'typeof B2.Buffer': 'function', 'typeof B2.Buffer': 'function',
'typeof Buffer': 'function', 'typeof Buffer': 'function',
encoded: 'SGVsbG8sIHdvcmxkIQ==', encoded: 'SGVsbG8sIHdvcmxkIQ==',
exposedKeys: [ exposedKeys: expect.arrayContaining([
'constants', 'constants',
'kMaxLength', 'kMaxLength',
'kStringMaxLength', 'kStringMaxLength',
'Buffer', 'Buffer',
'SlowBuffer', '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