| 21 | // Checks the bundles and every other file the binary ships under packages/server, so a new read of a local override |
| 22 | // that is missing from the rewrites, or a dependency change that makes a bundle two-byte, fails the build |
| 23 | const validateShippedServerCode = async (serverDir, testKey) => { |
| 24 | testKey ??= await getTestEncryptionKey() |
| 25 | |
| 26 | const files = glob.sync('**/*.{js,cjs}', { cwd: serverDir, ignore: ['node_modules/**'], nodir: true }) |
| 27 | const problems = [] |
| 28 | |
| 29 | await Promise.all(files.map(async (file) => { |
| 30 | const contents = await fs.readFile(path.join(serverDir, file), 'utf8') |
| 31 | |
| 32 | for (const variable of LOCAL_OVERRIDE_VARIABLES) { |
| 33 | if (contents.includes(variable)) { |
| 34 | problems.push(`${file} reads ${variable}`) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (contents.includes(testKey)) { |
| 39 | problems.push(`${file} contains the test encryption key`) |
| 40 | } |
| 41 | |
| 42 | const nonLatin1 = ONE_BYTE_SOURCES.includes(file) && contents.match(NON_LATIN1) |
| 43 | |
| 44 | if (nonLatin1) { |
| 45 | const codePoint = `U+${nonLatin1[0].charCodeAt(0).toString(16).toUpperCase().padStart(4, '0')}` |
| 46 | |
| 47 | problems.push(`${file} contains ${codePoint}, so V8 stores it as two-byte: ...${contents.slice(Math.max(0, nonLatin1.index - 60), nonLatin1.index + 20)}...`) |
| 48 | } |
| 49 | })) |
| 50 | |
| 51 | if (problems.length) { |
| 52 | throw new Error(`The binary's server code failed validation:\n${problems.sort().join('\n')}`) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const getEncryptionFileSource = async (encryptionFilePath) => { |
| 57 | const fileContents = await fs.readFile(encryptionFilePath, 'utf8') |