Skip to content

Commit

Permalink
[edge] limit the api surface of util (#47292)
Browse files Browse the repository at this point in the history
This makes sure that what works locally will work in production.
  • Loading branch information
Schniz committed Mar 20, 2023
1 parent 7cc4159 commit 6b09bc8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
8 changes: 5 additions & 3 deletions packages/next/src/build/webpack/plugins/middleware-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Expand Down
72 changes: 47 additions & 25 deletions packages/next/src/server/web/sandbox/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]),
})
})

Expand All @@ -38,13 +38,13 @@ createNextDescribe(
'typeof B2.Buffer': 'function',
'typeof Buffer': 'function',
encoded: 'SGVsbG8sIHdvcmxkIQ==',
exposedKeys: [
exposedKeys: expect.arrayContaining([
'constants',
'kMaxLength',
'kStringMaxLength',
'Buffer',
'SlowBuffer',
],
]),
})
})
}
Expand Down

0 comments on commit 6b09bc8

Please sign in to comment.