|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Build-time counterpart of the SEA virtual file system: run by --build-sea |
| 4 | +// (src/node_sea.cc) in a regular Node.js instance to compile every module of |
| 5 | +// the virtual file system and collect their compile cache, which the |
| 6 | +// executable then carries as assets. The assets are mounted the way the |
| 7 | +// executable mounts them at startup, so the modules are compiled from the |
| 8 | +// same paths and sources that the loaders compile them from in the |
| 9 | +// executable, and the cache is keyed and hashed exactly as the loaders look |
| 10 | +// it up there. |
| 11 | + |
| 12 | +const { |
| 13 | + ArrayPrototypeIncludes, |
| 14 | + StringPrototypeEndsWith, |
| 15 | + StringPrototypeReplaceAll, |
| 16 | +} = primordials; |
| 17 | + |
| 18 | +const { Buffer } = require('buffer'); |
| 19 | +const fs = require('fs'); |
| 20 | +const path = require('path'); |
| 21 | +const { pathToFileURL } = require('internal/url'); |
| 22 | +const { getOptionValue } = require('internal/options'); |
| 23 | +const { |
| 24 | + enableCompileCache: enableNativeCompileCache, |
| 25 | +} = internalBinding('modules'); |
| 26 | +const { |
| 27 | + constants: { compileCacheStatus }, |
| 28 | + stringify, |
| 29 | +} = require('internal/modules/helpers'); |
| 30 | +const { |
| 31 | + codes: { |
| 32 | + ERR_INVALID_STATE, |
| 33 | + }, |
| 34 | +} = require('internal/errors'); |
| 35 | + |
| 36 | +let debug = require('internal/util/debuglog').debuglog('sea', (fn) => { |
| 37 | + debug = fn; |
| 38 | +}); |
| 39 | + |
| 40 | +// Only files the module loaders compile are cached. Everything else in the |
| 41 | +// mount is data. |
| 42 | +const kScriptExtensions = ['.js', '.cjs', '.mjs']; |
| 43 | +const kTypeScriptExtensions = ['.ts', '.cts', '.mts']; |
| 44 | + |
| 45 | +/** |
| 46 | + * Mounts the assets of the SEA configuration the way the executable mounts |
| 47 | + * them: the main script at the mount point root, at the layer reserved for |
| 48 | + * --vfs-load when they come from an archive (see lib/internal/vfs/sea.js). |
| 49 | + * @param {object} options The SEA configuration, see buildSeaCompileCache() |
| 50 | + * @param {string} mainName The name of the main script at the mount root |
| 51 | + * @returns {string} The mount point |
| 52 | + */ |
| 53 | +function mountAssets(options, mainName) { |
| 54 | + const { VirtualFileSystem, kLoadLayer } = require('internal/vfs/file_system'); |
| 55 | + const isArchive = options.vfsArchive !== undefined; |
| 56 | + |
| 57 | + let provider; |
| 58 | + if (isArchive) { |
| 59 | + const { ZipBuffer } = require('internal/zip'); |
| 60 | + const { ZipProvider } = require('internal/vfs/providers/ziparchive'); |
| 61 | + const zip = new ZipBuffer(fs.readFileSync(options.vfsArchive)); |
| 62 | + zip.addSync(mainName, Buffer.from(options.mainSource), |
| 63 | + { __proto__: null, method: 'store' }); |
| 64 | + provider = new ZipProvider(zip); |
| 65 | + } else { |
| 66 | + const { MemoryProvider } = require('internal/vfs/providers/memory'); |
| 67 | + provider = new MemoryProvider(); |
| 68 | + } |
| 69 | + |
| 70 | + const vfs = new VirtualFileSystem(provider, { |
| 71 | + __proto__: null, |
| 72 | + emitExperimentalWarning: false, |
| 73 | + [kLoadLayer]: isArchive, |
| 74 | + }); |
| 75 | + |
| 76 | + if (!isArchive) { |
| 77 | + // Every asset is a file at its key, as the SEAProvider serves them. |
| 78 | + const { assetKeys, assetPaths } = options; |
| 79 | + for (let i = 0; i < assetKeys.length; i++) { |
| 80 | + const target = path.posix.normalize( |
| 81 | + '/' + StringPrototypeReplaceAll(assetKeys[i], '\\', '/')); |
| 82 | + const dir = path.posix.dirname(target); |
| 83 | + if (dir !== '/') { |
| 84 | + vfs.mkdirSync(dir, { __proto__: null, recursive: true }); |
| 85 | + } |
| 86 | + vfs.writeFileSync(target, fs.readFileSync(assetPaths[i])); |
| 87 | + } |
| 88 | + vfs.writeFileSync('/' + mainName, options.mainSource); |
| 89 | + provider.setReadOnly(); |
| 90 | + } |
| 91 | + |
| 92 | + return vfs.mount(); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Compiles a module without running it, the way the loaders compile it when |
| 97 | + * it is loaded from the mount, so that its compile cache entry is created. |
| 98 | + * @param {string} filename The path of the module inside the mount |
| 99 | + * @returns {boolean} Whether the file is a module the loaders compile |
| 100 | + */ |
| 101 | +function compileModule(filename) { |
| 102 | + const { Module, wrapSafe } = require('internal/modules/cjs/loader'); |
| 103 | + const { |
| 104 | + defaultGetFormatWithoutErrors, |
| 105 | + } = require('internal/modules/esm/get_format'); |
| 106 | + const { |
| 107 | + compileSourceTextModule, |
| 108 | + SourceTextModuleTypes: { kUser }, |
| 109 | + } = require('internal/modules/esm/utils'); |
| 110 | + |
| 111 | + const url = pathToFileURL(filename); |
| 112 | + const buffer = fs.readFileSync(filename); |
| 113 | + let format = defaultGetFormatWithoutErrors(url, { |
| 114 | + __proto__: null, |
| 115 | + source: buffer, |
| 116 | + }); |
| 117 | + |
| 118 | + let source; |
| 119 | + switch (format) { |
| 120 | + case 'module': |
| 121 | + // The ESM loader decodes the source it loads. |
| 122 | + source = stringify(buffer); |
| 123 | + break; |
| 124 | + case 'commonjs': |
| 125 | + case 'commonjs-typescript': |
| 126 | + case 'module-typescript': |
| 127 | + case 'typescript': |
| 128 | + // The CommonJS loader reads the source as a UTF-8 string. |
| 129 | + source = buffer.toString('utf8'); |
| 130 | + break; |
| 131 | + default: |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if (StringPrototypeEndsWith(format, 'typescript')) { |
| 136 | + const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); |
| 137 | + source = stripTypeScriptModuleTypes(source, filename, url.href); |
| 138 | + switch (format) { |
| 139 | + case 'commonjs-typescript': |
| 140 | + format = 'commonjs'; |
| 141 | + break; |
| 142 | + case 'module-typescript': |
| 143 | + format = 'module'; |
| 144 | + break; |
| 145 | + default: |
| 146 | + // Detected from the type-stripped source by wrapSafe(), like |
| 147 | + // Module.prototype._compile() does. |
| 148 | + format = undefined; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (format !== 'module') { |
| 154 | + const result = wrapSafe(filename, source, new Module(filename, null), format); |
| 155 | + if (!result.canParseAsESM) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + compileSourceTextModule(url.href, source, kUser); |
| 160 | + return true; |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Compiles every module under a directory of the mount. |
| 165 | + * @param {string} dir The directory |
| 166 | + * @param {boolean} stripTypes Whether TypeScript files are modules too |
| 167 | + * @returns {number} The number of modules compiled |
| 168 | + */ |
| 169 | +function compileTree(dir, stripTypes) { |
| 170 | + let compiled = 0; |
| 171 | + const dirents = fs.readdirSync(dir, { __proto__: null, withFileTypes: true }); |
| 172 | + for (let i = 0; i < dirents.length; i++) { |
| 173 | + const dirent = dirents[i]; |
| 174 | + const filename = path.join(dir, dirent.name); |
| 175 | + if (dirent.isDirectory()) { |
| 176 | + compiled += compileTree(filename, stripTypes); |
| 177 | + continue; |
| 178 | + } |
| 179 | + if (!dirent.isFile()) continue; |
| 180 | + const ext = path.extname(filename); |
| 181 | + if (!ArrayPrototypeIncludes(kScriptExtensions, ext) && |
| 182 | + !(stripTypes && ArrayPrototypeIncludes(kTypeScriptExtensions, ext))) { |
| 183 | + continue; |
| 184 | + } |
| 185 | + // A module that does not compile is left out of the cache: it fails the |
| 186 | + // same way at run time if the application ever loads it. |
| 187 | + try { |
| 188 | + if (compileModule(filename)) compiled++; |
| 189 | + } catch (err) { |
| 190 | + debug('not caching %s: %s', filename, err?.message); |
| 191 | + } |
| 192 | + } |
| 193 | + return compiled; |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * Compiles the modules of the SEA virtual file system into the compile cache |
| 198 | + * of this Node.js instance, from which --build-sea collects them. The cache |
| 199 | + * is keyed relative to the mount point, which is the same in the executable. |
| 200 | + * @param {object} options |
| 201 | + * @param {string} options.main The "main" of the SEA configuration |
| 202 | + * @param {string} options.mainSource The source of the main script |
| 203 | + * @param {string} [options.vfsArchive] The "vfsArchive" of the configuration |
| 204 | + * @param {string[]} [options.assetKeys] The keys of the "assets" |
| 205 | + * @param {string[]} [options.assetPaths] The paths of the "assets" |
| 206 | + * @returns {number} The number of modules compiled |
| 207 | + */ |
| 208 | +function buildSeaCompileCache(options) { |
| 209 | + const { getSeaMainName } = require('internal/vfs/sea'); |
| 210 | + const mountPoint = mountAssets(options, getSeaMainName(options.main)); |
| 211 | + |
| 212 | + // Enabling the compile cache takes a directory, but nothing is persisted |
| 213 | + // to it: the entries are collected from memory by --build-sea. |
| 214 | + const cacheDir = fs.mkdtempSync( |
| 215 | + path.join(require('os').tmpdir(), 'node-sea-compile-cache-')); |
| 216 | + try { |
| 217 | + const { 0: status, 1: message } = enableNativeCompileCache( |
| 218 | + cacheDir, true /* portable */, false /* readOnly */, mountPoint); |
| 219 | + if (status !== compileCacheStatus.ENABLED) { |
| 220 | + throw new ERR_INVALID_STATE( |
| 221 | + `cannot enable the compile cache for ${mountPoint}: ${message}`); |
| 222 | + } |
| 223 | + const compiled = compileTree(mountPoint, getOptionValue('--strip-types')); |
| 224 | + debug('compiled %d modules in %s for the compile cache', |
| 225 | + compiled, mountPoint); |
| 226 | + return compiled; |
| 227 | + } finally { |
| 228 | + fs.rmSync(cacheDir, { __proto__: null, recursive: true, force: true }); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +module.exports = { |
| 233 | + buildSeaCompileCache, |
| 234 | +}; |
0 commit comments