Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion resources/main-template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <openssl/rsa.h>
#include <openssl/rand.h>
#endif
#include <type_traits> // injected code may refer to std::underlying_type

using namespace node;
using namespace v8;
Expand All @@ -33,6 +34,11 @@ using namespace v8;
#define NODE_VERSION_SUPPORTS_EMBEDDER_SNAPSHOT 1
#endif

// Snapshot config is supported since https://github.com/nodejs/node/pull/50453
#if NODE_VERSION_AT_LEAST(21, 6, 0) && !defined(BOXEDNODE_SNAPSHOT_CONFIG_FLAGS)
#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (SnapshotFlags::kWithoutCodeCache)
#endif

// 18.1.0 is the current minimum version that has https://github.com/nodejs/node/pull/42809,
// which introduced crashes when using workers, and later 18.9.0 is the current
// minimum version to contain https://github.com/nodejs/node/pull/44252, which
Expand Down Expand Up @@ -167,7 +173,15 @@ static int RunNodeInstance(MultiIsolatePlatform* platform,
int exit_code = 0;
std::vector<std::string> errors;
std::unique_ptr<CommonEnvironmentSetup> setup =
CommonEnvironmentSetup::CreateForSnapshotting(platform, &errors, args, exec_args);
CommonEnvironmentSetup::CreateForSnapshotting(
platform,
&errors,
args,
exec_args
#ifdef BOXEDNODE_SNAPSHOT_CONFIG_FLAGS
, SnapshotConfig { BOXEDNODE_SNAPSHOT_CONFIG_FLAGS, std::nullopt }
#endif
);

Isolate* isolate = setup->isolate();

Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ type CompilationOptions = {
useLegacyDefaultUvLoop?: boolean;
useCodeCache?: boolean,
useNodeSnapshot?: boolean,
nodeSnapshotConfigFlags?: string[], // e.g. 'WithoutCodeCache'
executableMetadata?: ExecutableMetadata,
preCompileHook?: (nodeSourceTree: string, options: CompilationOptions) => void | Promise<void>
}
Expand Down Expand Up @@ -421,6 +422,14 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
if (snapshotMode === 'consume') {
mainSource = `#define BOXEDNODE_CONSUME_SNAPSHOT 1\n${mainSource}`;
}
if (options.nodeSnapshotConfigFlags) {
const flags = [
'0',
...options.nodeSnapshotConfigFlags.map(flag =>
`static_cast<std::underlying_type<SnapshotFlags>::type>(SnapshotFlags::k${flag})`)
].join(' | ');
mainSource = `#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (static_cast<SnapshotFlags>(${flags}))\n${mainSource}`;
}
await fs.writeFile(path.join(nodeSourcePath, 'src', 'node_main.cc'), mainSource);
logger.stepCompleted();

Expand Down
3 changes: 2 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ describe('basic functionality', () => {
it('works with snapshot support', async function () {
this.timeout(2 * 60 * 60 * 1000); // 2 hours
await compileJSFileAsBinary({
nodeVersionRange: 'v21.0.0-nightly20230801d396a041f7',
nodeVersionRange: '^21.6.2',
sourceFile: path.resolve(__dirname, 'resources/snapshot-echo-args.js'),
targetFile: path.resolve(__dirname, `resources/snapshot-echo-args${exeSuffix}`),
useNodeSnapshot: true,
nodeSnapshotConfigFlags: ['WithoutCodeCache'],
// the nightly path name is too long for Windows...
tmpdir: process.platform === 'win32' ? path.join(os.tmpdir(), 'bn') : undefined
});
Expand Down