Skip to content

Commit 6c33033

Browse files
authored
feat: add ability to specify node::SnapshotFlags values MONGOSH-1605 (#54)
1 parent 04dfeca commit 6c33033

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

resources/main-template.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <openssl/rsa.h>
1818
#include <openssl/rand.h>
1919
#endif
20+
#include <type_traits> // injected code may refer to std::underlying_type
2021

2122
using namespace node;
2223
using namespace v8;
@@ -33,6 +34,11 @@ using namespace v8;
3334
#define NODE_VERSION_SUPPORTS_EMBEDDER_SNAPSHOT 1
3435
#endif
3536

37+
// Snapshot config is supported since https://github.com/nodejs/node/pull/50453
38+
#if NODE_VERSION_AT_LEAST(21, 6, 0) && !defined(BOXEDNODE_SNAPSHOT_CONFIG_FLAGS)
39+
#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (SnapshotFlags::kWithoutCodeCache)
40+
#endif
41+
3642
// 18.1.0 is the current minimum version that has https://github.com/nodejs/node/pull/42809,
3743
// which introduced crashes when using workers, and later 18.9.0 is the current
3844
// minimum version to contain https://github.com/nodejs/node/pull/44252, which
@@ -167,7 +173,15 @@ static int RunNodeInstance(MultiIsolatePlatform* platform,
167173
int exit_code = 0;
168174
std::vector<std::string> errors;
169175
std::unique_ptr<CommonEnvironmentSetup> setup =
170-
CommonEnvironmentSetup::CreateForSnapshotting(platform, &errors, args, exec_args);
176+
CommonEnvironmentSetup::CreateForSnapshotting(
177+
platform,
178+
&errors,
179+
args,
180+
exec_args
181+
#ifdef BOXEDNODE_SNAPSHOT_CONFIG_FLAGS
182+
, SnapshotConfig { BOXEDNODE_SNAPSHOT_CONFIG_FLAGS, std::nullopt }
183+
#endif
184+
);
171185

172186
Isolate* isolate = setup->isolate();
173187

src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ type CompilationOptions = {
275275
useLegacyDefaultUvLoop?: boolean;
276276
useCodeCache?: boolean,
277277
useNodeSnapshot?: boolean,
278+
nodeSnapshotConfigFlags?: string[], // e.g. 'WithoutCodeCache'
278279
executableMetadata?: ExecutableMetadata,
279280
preCompileHook?: (nodeSourceTree: string, options: CompilationOptions) => void | Promise<void>
280281
}
@@ -421,6 +422,14 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
421422
if (snapshotMode === 'consume') {
422423
mainSource = `#define BOXEDNODE_CONSUME_SNAPSHOT 1\n${mainSource}`;
423424
}
425+
if (options.nodeSnapshotConfigFlags) {
426+
const flags = [
427+
'0',
428+
...options.nodeSnapshotConfigFlags.map(flag =>
429+
`static_cast<std::underlying_type<SnapshotFlags>::type>(SnapshotFlags::k${flag})`)
430+
].join(' | ');
431+
mainSource = `#define BOXEDNODE_SNAPSHOT_CONFIG_FLAGS (static_cast<SnapshotFlags>(${flags}))\n${mainSource}`;
432+
}
424433
await fs.writeFile(path.join(nodeSourcePath, 'src', 'node_main.cc'), mainSource);
425434
logger.stepCompleted();
426435

test/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,11 @@ describe('basic functionality', () => {
217217
it('works with snapshot support', async function () {
218218
this.timeout(2 * 60 * 60 * 1000); // 2 hours
219219
await compileJSFileAsBinary({
220-
nodeVersionRange: 'v21.0.0-nightly20230801d396a041f7',
220+
nodeVersionRange: '^21.6.2',
221221
sourceFile: path.resolve(__dirname, 'resources/snapshot-echo-args.js'),
222222
targetFile: path.resolve(__dirname, `resources/snapshot-echo-args${exeSuffix}`),
223223
useNodeSnapshot: true,
224+
nodeSnapshotConfigFlags: ['WithoutCodeCache'],
224225
// the nightly path name is too long for Windows...
225226
tmpdir: process.platform === 'win32' ? path.join(os.tmpdir(), 'bn') : undefined
226227
});

0 commit comments

Comments
 (0)