mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
When "vfsArchive" names a ZIP archive in the SEA configuration (with "useVfs": true), --build-sea embeds the archive verbatim as one reserved asset, and at runtime the SEA virtual file system mounts the existing ZipProvider over a zero-copy view of the embedded archive instead of the SEAProvider. The main script is injected into the in-memory archive index as a stored entry, so the mounted tree looks the same as with "assets". The archive can be produced with any ZIP tool or with the ZIP support in node:zlib; no ZIP serialization logic is added to the build side. This trades asset read speed (entries are inflated when opened) for a substantially smaller executable when the assets are compressible. Also make FindSingleExecutableResource() return the deserialized SeaResource by reference instead of by value: the copy included the whole assets map, and getAsset() calls it once per asset read, so every fs operation served by the SEA virtual file system paid a cost linear in the number of bundled assets - with 8192 assets, reading each of them took seconds instead of milliseconds. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/65810 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
105 lines
3.4 KiB
C++
105 lines
3.4 KiB
C++
#ifndef SRC_NODE_SEA_H_
|
|
#define SRC_NODE_SEA_H_
|
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#include <cinttypes>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <tuple>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "node.h"
|
|
#include "node_exit_code.h"
|
|
|
|
namespace node {
|
|
class Environment;
|
|
namespace sea {
|
|
// A special number that will appear at the beginning of the single executable
|
|
// preparation blobs ready to be injected into the binary. We use this to check
|
|
// that the data given to us are intended for building single executable
|
|
// applications.
|
|
const uint32_t kMagic = 0x143da20;
|
|
|
|
enum class SeaFlags : uint32_t {
|
|
kDefault = 0,
|
|
kDisableExperimentalSeaWarning = 1 << 0,
|
|
kUseSnapshot = 1 << 1,
|
|
kUseCodeCache = 1 << 2,
|
|
kIncludeAssets = 1 << 3,
|
|
kIncludeExecArgv = 1 << 4,
|
|
kEnableVfs = 1 << 5,
|
|
kVfsArchive = 1 << 6,
|
|
};
|
|
|
|
enum class SeaExecArgvExtension : uint8_t {
|
|
kNone = 0,
|
|
kEnv = 1,
|
|
kCli = 2,
|
|
};
|
|
|
|
struct SeaConfig {
|
|
std::string main_path;
|
|
std::string output_path;
|
|
std::string executable_path;
|
|
SeaFlags flags = SeaFlags::kDefault;
|
|
SeaExecArgvExtension exec_argv_extension = SeaExecArgvExtension::kEnv;
|
|
ModuleFormat main_format = ModuleFormat::kCommonJS;
|
|
std::unordered_map<std::string, std::string> assets;
|
|
std::vector<std::string> exec_argv;
|
|
std::string vfs_archive_path;
|
|
};
|
|
|
|
struct SeaResource {
|
|
SeaFlags flags = SeaFlags::kDefault;
|
|
SeaExecArgvExtension exec_argv_extension = SeaExecArgvExtension::kEnv;
|
|
std::string_view code_path;
|
|
std::string_view main_code_or_snapshot;
|
|
ModuleFormat main_code_format = ModuleFormat::kCommonJS;
|
|
std::optional<std::string_view> code_cache;
|
|
std::unordered_map<std::string_view, std::string_view> assets;
|
|
std::vector<std::string_view> exec_argv;
|
|
|
|
bool use_snapshot() const;
|
|
bool use_code_cache() const;
|
|
|
|
static constexpr size_t kHeaderSize = sizeof(kMagic) + sizeof(SeaFlags) +
|
|
sizeof(SeaExecArgvExtension) +
|
|
sizeof(ModuleFormat);
|
|
};
|
|
|
|
bool IsSingleExecutable();
|
|
std::string_view FindSingleExecutableBlob();
|
|
const SeaResource& FindSingleExecutableResource();
|
|
std::tuple<int, char**> FixupArgsForSEA(int argc,
|
|
char** argv,
|
|
std::vector<std::string>* errors);
|
|
node::ExitCode WriteSingleExecutableBlob(
|
|
const std::string& config_path,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args);
|
|
|
|
// Try loading the Environment as a single-executable application.
|
|
// Returns true if it is loaded as a single-executable application.
|
|
// Otherwise returns false and the caller is expected to call LoadEnvironment()
|
|
// differently.
|
|
bool MaybeLoadSingleExecutableApplication(Environment* env);
|
|
std::optional<SeaConfig> ParseSingleExecutableConfig(
|
|
const std::string& config_path);
|
|
ExitCode BuildSingleExecutable(const std::string& sea_config_path,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args);
|
|
ExitCode GenerateSingleExecutableBlob(
|
|
std::vector<char>* out,
|
|
const SeaConfig& config,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args);
|
|
} // namespace sea
|
|
} // namespace node
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#endif // SRC_NODE_SEA_H_
|