1/** 2 * Copyright © 2022 650 Industries. 3 * 4 * This source code is licensed under the MIT license found in the 5 * LICENSE file in the root directory of this source tree. 6 */ 7import fs from 'fs'; 8import fetch from 'node-fetch'; 9import path from 'path'; 10import requireString from 'require-from-string'; 11import resolveFrom from 'resolve-from'; 12 13import { stripAnsi } from '../../utils/ansi'; 14import { delayAsync } from '../../utils/delay'; 15import { SilentError } from '../../utils/errors'; 16import { memoize } from '../../utils/fn'; 17import { profile } from '../../utils/profile'; 18import { logMetroError } from './metro/metroErrorInterface'; 19import { getMetroServerRoot } from './middleware/ManifestMiddleware'; 20 21const debug = require('debug')('expo:start:server:node-renderer') as typeof console.log; 22 23function wrapBundle(str: string) { 24 // Skip the metro runtime so debugging is a bit easier. 25 // Replace the __r() call with an export statement. 26 // Use gm to apply to the last require line. This is needed when the bundle has side-effects. 27 return str.replace(/^(__r\(.*\);)$/gm, 'module.exports = $1'); 28} 29 30function stripProcess(str: string) { 31 // TODO: Remove from the metro prelude 32 return str.replace(/process=this\.process\|\|{},/m, ''); 33} 34 35// TODO(EvanBacon): Group all the code together and version. 36const getRenderModuleId = (projectRoot: string): string => { 37 const moduleId = resolveFrom.silent(projectRoot, 'expo-router/node/render.js'); 38 if (!moduleId) { 39 throw new Error( 40 `A version of expo-router with Node.js support is not installed in the project.` 41 ); 42 } 43 44 return moduleId; 45}; 46 47type StaticRenderOptions = { 48 // Ensure the style format is `css-xxxx` (prod) instead of `css-view-xxxx` (dev) 49 dev?: boolean; 50 minify?: boolean; 51 platform?: string; 52 environment?: 'node'; 53}; 54 55const moveStaticRenderFunction = memoize(async (projectRoot: string, requiredModuleId: string) => { 56 // Copy the file into the project to ensure it works in monorepos. 57 // This means the file cannot have any relative imports. 58 const tempDir = path.join(projectRoot, '.expo/static'); 59 await fs.promises.mkdir(tempDir, { recursive: true }); 60 const moduleId = path.join(tempDir, 'render.js'); 61 await fs.promises.writeFile(moduleId, await fs.promises.readFile(requiredModuleId, 'utf8')); 62 // Sleep to give watchman time to register the file. 63 await delayAsync(50); 64 return moduleId; 65}); 66 67/** @returns the js file contents required to generate the static generation function. */ 68export async function getStaticRenderFunctionsContentAsync( 69 projectRoot: string, 70 devServerUrl: string, 71 { dev = false, minify = false, environment }: StaticRenderOptions = {} 72): Promise<string> { 73 const root = getMetroServerRoot(projectRoot); 74 const requiredModuleId = getRenderModuleId(root); 75 let moduleId = requiredModuleId; 76 77 // Cannot be accessed using Metro's server API, we need to move the file 78 // into the project root and try again. 79 if (path.relative(root, moduleId).startsWith('..')) { 80 moduleId = await moveStaticRenderFunction(projectRoot, requiredModuleId); 81 } 82 83 return requireFileContentsWithMetro(root, devServerUrl, moduleId, { dev, minify, environment }); 84} 85 86async function ensureFileInRootDirectory(projectRoot: string, otherFile: string) { 87 // Cannot be accessed using Metro's server API, we need to move the file 88 // into the project root and try again. 89 if (!path.relative(projectRoot, otherFile).startsWith('../')) { 90 return otherFile; 91 } 92 93 // Copy the file into the project to ensure it works in monorepos. 94 // This means the file cannot have any relative imports. 95 const tempDir = path.join(projectRoot, '.expo/static-tmp'); 96 await fs.promises.mkdir(tempDir, { recursive: true }); 97 const moduleId = path.join(tempDir, path.basename(otherFile)); 98 await fs.promises.writeFile(moduleId, await fs.promises.readFile(otherFile, 'utf8')); 99 // Sleep to give watchman time to register the file. 100 await delayAsync(50); 101 return moduleId; 102} 103 104export async function createMetroEndpointAsync( 105 projectRoot: string, 106 devServerUrl: string, 107 absoluteFilePath: string, 108 { dev = false, platform = 'web', minify = false, environment }: StaticRenderOptions = {} 109): Promise<string> { 110 const root = getMetroServerRoot(projectRoot); 111 const safeOtherFile = await ensureFileInRootDirectory(projectRoot, absoluteFilePath); 112 const serverPath = path.relative(root, safeOtherFile).replace(/\.[jt]sx?$/, '.bundle'); 113 debug('fetching from Metro:', root, serverPath); 114 115 let url = `${devServerUrl}/${serverPath}?platform=${platform}&dev=${dev}&minify=${minify}`; 116 117 if (environment) { 118 url += `&resolver.environment=${environment}&transform.environment=${environment}`; 119 } 120 return url; 121} 122 123export class MetroNodeError extends Error { 124 constructor(message: string, public rawObject: any) { 125 super(message); 126 } 127} 128 129export async function requireFileContentsWithMetro( 130 projectRoot: string, 131 devServerUrl: string, 132 absoluteFilePath: string, 133 props: StaticRenderOptions = {} 134): Promise<string> { 135 const url = await createMetroEndpointAsync(projectRoot, devServerUrl, absoluteFilePath, props); 136 137 const res = await fetch(url); 138 139 // TODO: Improve error handling 140 if (res.status === 500) { 141 const text = await res.text(); 142 if (text.startsWith('{"originModulePath"') || text.startsWith('{"type":"TransformError"')) { 143 const errorObject = JSON.parse(text); 144 145 throw new MetroNodeError(stripAnsi(errorObject.message) ?? errorObject.message, errorObject); 146 } 147 throw new Error(`[${res.status}]: ${res.statusText}\n${text}`); 148 } 149 150 if (!res.ok) { 151 throw new Error(`Error fetching bundle for static rendering: ${res.status} ${res.statusText}`); 152 } 153 154 const content = await res.text(); 155 156 let bun = wrapBundle(content); 157 158 // This exposes the entire environment to the bundle. 159 if (props.environment === 'node') { 160 bun = stripProcess(bun); 161 } 162 163 return bun; 164} 165export async function requireWithMetro<T>( 166 projectRoot: string, 167 devServerUrl: string, 168 absoluteFilePath: string, 169 options: StaticRenderOptions = {} 170): Promise<T> { 171 const content = await requireFileContentsWithMetro( 172 projectRoot, 173 devServerUrl, 174 absoluteFilePath, 175 options 176 ); 177 return evalMetro(content); 178} 179 180export async function getStaticRenderFunctions( 181 projectRoot: string, 182 devServerUrl: string, 183 options: StaticRenderOptions = {} 184): Promise<Record<string, (...args: any[]) => Promise<any>>> { 185 const scriptContents = await getStaticRenderFunctionsContentAsync( 186 projectRoot, 187 devServerUrl, 188 options 189 ); 190 191 const contents = evalMetro(scriptContents); 192 193 // wrap each function with a try/catch that uses Metro's error formatter 194 return Object.keys(contents).reduce((acc, key) => { 195 const fn = contents[key]; 196 if (typeof fn !== 'function') { 197 return { ...acc, [key]: fn }; 198 } 199 200 acc[key] = async function (...props: any[]) { 201 try { 202 return await fn.apply(this, props); 203 } catch (error: any) { 204 await logMetroError(projectRoot, { error }); 205 throw new SilentError(error); 206 } 207 }; 208 return acc; 209 }, {} as any); 210} 211 212function evalMetro(src: string) { 213 return profile(requireString, 'eval-metro-bundle')(src); 214} 215