Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
6b09bc86
Unverified
Commit
6b09bc86
authored
Mar 20, 2023
by
Gal Schlezinger
Committed by
GitHub
Mar 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[edge] limit the api surface of `util` (#47292)
This makes sure that what works locally will work in production.
parent
7cc41595
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
32 deletions
+56
-32
middleware-plugin.ts
packages/next/src/build/webpack/plugins/middleware-plugin.ts
+5
-3
context.ts
packages/next/src/server/web/sandbox/context.ts
+47
-25
edge-runtime-node-compatibility.test.ts
...ode-compatibility/edge-runtime-node-compatibility.test.ts
+4
-4
No files found.
packages/next/src/build/webpack/plugins/middleware-plugin.ts
View file @
6b09bc86
...
...
@@ -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
}
`
}
...
...
packages/next/src/server/web/sandbox/context.ts
View file @
6b09bc86
...
...
@@ -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
...
...
test/e2e/app-dir/edge-runtime-node-compatibility/edge-runtime-node-compatibility.test.ts
View file @
6b09bc86
...
...
@@ -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'
,
],
]
)
,
})
})
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment