Unverified Commit f93250cd authored by Josh Story's avatar Josh Story Committed by GitHub

Defensively install patches (#71043)

Some builtins and node library modules may make patching impossible by
freezing objects or making propeties readonly. The patch installation is
done as early as possible before we know if we're even going to need the
patch and thus is done unconditionally whether DIO is enabled or not.
This is generally preferable to how for instance the fetch patch is
installed which is observable becasue user code can run before it.

In this change all the patch installs are for dynamicIO augmented
behavior. We can allow the process to run but will warn that
`experimental.dynamicIO` will not have expected behavior when calling
the functions that were not installed.
parent 01c42a8d
...@@ -47,5 +47,11 @@ function createDate(originalConstructor: typeof Date): typeof Date { ...@@ -47,5 +47,11 @@ function createDate(originalConstructor: typeof Date): typeof Date {
return newConstructor as typeof Date return newConstructor as typeof Date
} }
// eslint-disable-next-line no-native-reassign try {
Date = createDate(Date) // eslint-disable-next-line no-native-reassign
Date = createDate(Date)
} catch {
console.error(
'Failed to install `Date` class extension. When using `experimental.dynamicIO`, APIs that read the current time will not correctly trigger dynamic behavior.'
)
}
...@@ -18,51 +18,105 @@ if (process.env.NEXT_RUNTIME === 'edge') { ...@@ -18,51 +18,105 @@ if (process.env.NEXT_RUNTIME === 'edge') {
// crypto.getRandomValues which is extended in web-crypto.tsx // crypto.getRandomValues which is extended in web-crypto.tsx
// require('node:crypto').randomUUID is not an alias for crypto.randomUUID // require('node:crypto').randomUUID is not an alias for crypto.randomUUID
const _randomUUID = nodeCrypto.randomUUID
nodeCrypto.randomUUID = function randomUUID() { const randomUUIDExpression = "`require('node:crypto').randomUUID()`"
io("`require('node:crypto').randomUUID()`") try {
return _randomUUID.apply(this, arguments as any) const _randomUUID = nodeCrypto.randomUUID
nodeCrypto.randomUUID = function randomUUID() {
io(randomUUIDExpression)
return _randomUUID.apply(this, arguments as any)
}
} catch {
console.error(
`Failed to install ${randomUUIDExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
const _randomBytes = nodeCrypto.randomBytes const randomBytesExpression = "`require('node:crypto').randomBytes(size)`"
nodeCrypto.randomBytes = function randomBytes() { try {
if (typeof arguments[1] !== 'function') { const _randomBytes = nodeCrypto.randomBytes
// randomBytes is sync if the second arg is undefined nodeCrypto.randomBytes = function randomBytes() {
io("`require('node:crypto').randomBytes(size)`") if (typeof arguments[1] !== 'function') {
// randomBytes is sync if the second arg is undefined
io(randomBytesExpression)
}
return _randomBytes.apply(this, arguments as any)
} }
return _randomBytes.apply(this, arguments as any) } catch {
console.error(
`Failed to install ${randomBytesExpression} extension. When using \`experimental.dynamicIO\` calling this function without a callback argument will not correctly trigger dynamic behavior.`
)
} }
const _randomFillSync = nodeCrypto.randomFillSync const randomFillSyncExpression =
nodeCrypto.randomFillSync = function randomFillSync() { "`require('node:crypto').randomFillSync(...)`"
io("`require('node:crypto').randomFillSync(...)`") try {
return _randomFillSync.apply(this, arguments as any) const _randomFillSync = nodeCrypto.randomFillSync
nodeCrypto.randomFillSync = function randomFillSync() {
io(randomFillSyncExpression)
return _randomFillSync.apply(this, arguments as any)
}
} catch {
console.error(
`Failed to install ${randomFillSyncExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
const _randomInt = nodeCrypto.randomInt const randomIntExpression = "`require('node:crypto').randomInt(min, max)`"
nodeCrypto.randomInt = function randomInt() { try {
if (typeof arguments[2] !== 'function') { const _randomInt = nodeCrypto.randomInt
// randomInt is sync if the third arg is undefined nodeCrypto.randomInt = function randomInt() {
io("`require('node:crypto').randomInt(min, max)`") if (typeof arguments[2] !== 'function') {
// randomInt is sync if the third arg is undefined
io(randomIntExpression)
}
return _randomInt.apply(this, arguments as any)
} }
return _randomInt.apply(this, arguments as any) } catch {
console.error(
`Failed to install ${randomBytesExpression} extension. When using \`experimental.dynamicIO\` calling this function without a callback argument will not correctly trigger dynamic behavior.`
)
} }
const _generatePrimeSync = nodeCrypto.generatePrimeSync const generatePrimeSyncExpression =
nodeCrypto.generatePrimeSync = function generatePrimeSync() { "`require('node:crypto').generatePrimeSync(...)`"
io("`require('node:crypto').generatePrimeSync(...)`") try {
return _generatePrimeSync.apply(this, arguments as any) const _generatePrimeSync = nodeCrypto.generatePrimeSync
nodeCrypto.generatePrimeSync = function generatePrimeSync() {
io(generatePrimeSyncExpression)
return _generatePrimeSync.apply(this, arguments as any)
}
} catch {
console.error(
`Failed to install ${generatePrimeSyncExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
const _generateKeyPairSync = nodeCrypto.generateKeyPairSync const generateKeyPairSyncExpression =
nodeCrypto.generateKeyPairSync = function generateKeyPairSync() { "`require('node:crypto').generateKeyPairSync(...)`"
io("`require('node:crypto').generateKeyPairSync(...)`") try {
return _generateKeyPairSync.apply(this, arguments as any) const _generateKeyPairSync = nodeCrypto.generateKeyPairSync
nodeCrypto.generateKeyPairSync = function generateKeyPairSync() {
io(generateKeyPairSyncExpression)
return _generateKeyPairSync.apply(this, arguments as any)
}
} catch {
console.error(
`Failed to install ${generateKeyPairSyncExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
const _generateKeySync = nodeCrypto.generateKeySync const generateKeySyncExpression =
nodeCrypto.generateKeySync = function generateKeySync() { "`require('node:crypto').generateKeySync(...)`"
io("`require('node:crypto').generateKeySync(...)`") try {
return _generateKeySync.apply(this, arguments as any) const _generateKeySync = nodeCrypto.generateKeySync
nodeCrypto.generateKeySync = function generateKeySync() {
io(generateKeySyncExpression)
return _generateKeySync.apply(this, arguments as any)
}
} catch {
console.error(
`Failed to install ${generateKeySyncExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
} }
...@@ -8,14 +8,19 @@ ...@@ -8,14 +8,19 @@
import { io } from './utils' import { io } from './utils'
const _random = Math.random const expression = '`Math.random()`'
try {
const _random = Math.random
Math.random = function random() {
io(expression)
return _random.apply(null, arguments as any)
Math.random = function random() { // We bind here to alter the `toString` printing to match `Math.random`'s native `toString`.
io('`Math.random()`') // eslint-disable-next-line no-extra-bind
return _random.apply(null, arguments as any) }.bind(null)
Object.defineProperty(Math.random, 'name', { value: 'random' })
// We bind here to alter the `toString` printing to match `Math.random`'s native `toString`. } catch {
// eslint-disable-next-line no-extra-bind console.error(
}.bind(null) `Failed to install ${expression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
Object.defineProperty(Math.random, 'name', { value: 'random' }) }
...@@ -20,14 +20,28 @@ if (process.env.NEXT_RUNTIME === 'edge') { ...@@ -20,14 +20,28 @@ if (process.env.NEXT_RUNTIME === 'edge') {
} }
} }
const originalGetRandomValues = webCrypto.getRandomValues const getRandomValuesExpression = '`crypto.getRandomValues()`'
webCrypto.getRandomValues = function getRandomValues() { try {
io('`crypto.getRandomValues()`') const _getRandomValues = webCrypto.getRandomValues
return originalGetRandomValues.apply(webCrypto, arguments as any) webCrypto.getRandomValues = function getRandomValues() {
io(getRandomValuesExpression)
return _getRandomValues.apply(webCrypto, arguments as any)
}
} catch {
console.error(
`Failed to install ${getRandomValuesExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
} }
const _randomUUID = webCrypto.randomUUID const randomUUIDExpression = '`crypto.randomUUID()`'
webCrypto.randomUUID = function randomUUID() { try {
io('`crypto.randomUUID()`') const _randomUUID = webCrypto.randomUUID
return _randomUUID.apply(webCrypto, arguments as any) webCrypto.randomUUID = function randomUUID() {
} as typeof _randomUUID io(randomUUIDExpression)
return _randomUUID.apply(webCrypto, arguments as any)
} as typeof _randomUUID
} catch {
console.error(
`Failed to install ${getRandomValuesExpression} extension. When using \`experimental.dynamicIO\` calling this function will not correctly trigger dynamic behavior.`
)
}
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