File size: 5,212 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * We extend node:crypto APIs during builds and revalidates to ensure that prerenders don't observe random bytes
 * When cacheComponents is enabled. Random bytes are a form of IO even if they resolve synchronously. When cacheComponents is
 * enabled we need to ensure that random bytes are excluded from prerenders unless they are cached.
 *
 *
 * The extensions here never error nor alter the underlying return values and thus should be transparent to callers.
 */

import { io } from './utils'

if (process.env.NEXT_RUNTIME === 'edge') {
  // nothing to patch
} else {
  const nodeCrypto = require('node:crypto') as typeof import('node:crypto')

  // require('node:crypto').getRandomValues is an alias for
  // crypto.getRandomValues which is extended in web-crypto.tsx

  // require('node:crypto').randomUUID is not an alias for crypto.randomUUID

  const randomUUIDExpression = "`require('node:crypto').randomUUID()`"
  try {
    const _randomUUID = nodeCrypto.randomUUID
    nodeCrypto.randomUUID = function randomUUID() {
      io(randomUUIDExpression, 'random')
      return _randomUUID.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${randomUUIDExpression} extension. When using \`experimental.cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
    )
  }

  const randomBytesExpression = "`require('node:crypto').randomBytes(size)`"
  try {
    const _randomBytes = nodeCrypto.randomBytes
    // @ts-expect-error -- TODO: tell TS the overloads are preserved
    nodeCrypto.randomBytes = function randomBytes() {
      if (typeof arguments[1] !== 'function') {
        // randomBytes is sync if the second arg is undefined
        io(randomBytesExpression, 'random')
      }
      return _randomBytes.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${randomBytesExpression} extension. When using \`experimental.cacheComponents\` calling this function without a callback argument will not correctly trigger dynamic behavior.`
    )
  }

  const randomFillSyncExpression =
    "`require('node:crypto').randomFillSync(...)`"
  try {
    const _randomFillSync = nodeCrypto.randomFillSync
    // @ts-expect-error -- TODO: tell TS the overloads are preserved
    nodeCrypto.randomFillSync = function randomFillSync() {
      io(randomFillSyncExpression, 'random')
      return _randomFillSync.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${randomFillSyncExpression} extension. When using \`experimental.cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
    )
  }

  const randomIntExpression = "`require('node:crypto').randomInt(min, max)`"
  try {
    const _randomInt = nodeCrypto.randomInt
    // @ts-expect-error -- TODO: tell TS the overloads are preserved
    nodeCrypto.randomInt = function randomInt() {
      if (typeof arguments[2] !== 'function') {
        // randomInt is sync if the third arg is undefined
        io(randomIntExpression, 'random')
      }
      return _randomInt.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${randomBytesExpression} extension. When using \`experimental.cacheComponents\` calling this function without a callback argument will not correctly trigger dynamic behavior.`
    )
  }

  const generatePrimeSyncExpression =
    "`require('node:crypto').generatePrimeSync(...)`"
  try {
    const _generatePrimeSync = nodeCrypto.generatePrimeSync
    // @ts-expect-error -- TODO: tell TS the overloads are preserved
    nodeCrypto.generatePrimeSync = function generatePrimeSync() {
      io(generatePrimeSyncExpression, 'random')
      return _generatePrimeSync.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${generatePrimeSyncExpression} extension. When using \`experimental.cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
    )
  }

  const generateKeyPairSyncExpression =
    "`require('node:crypto').generateKeyPairSync(...)`"
  try {
    const _generateKeyPairSync = nodeCrypto.generateKeyPairSync
    // @ts-expect-error -- TODO: tell TS the overloads are preserved
    nodeCrypto.generateKeyPairSync = function generateKeyPairSync() {
      io(generateKeyPairSyncExpression, 'random')
      return _generateKeyPairSync.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${generateKeyPairSyncExpression} extension. When using \`experimental.cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
    )
  }

  const generateKeySyncExpression =
    "`require('node:crypto').generateKeySync(...)`"
  try {
    const _generateKeySync = nodeCrypto.generateKeySync
    nodeCrypto.generateKeySync = function generateKeySync() {
      io(generateKeySyncExpression, 'random')
      return _generateKeySync.apply(this, arguments as any)
    }
  } catch {
    console.error(
      `Failed to install ${generateKeySyncExpression} extension. When using \`experimental.cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
    )
  }
}