Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 202042e

Browse filesBrowse files
joyeecheungtargos
authored andcommitted
bootstrap: support namespaced builtins in snapshot scripts
PR-URL: #47467 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 9d30f46 commit 202042e
Copy full SHA for 202042e

File tree

Expand file treeCollapse file tree

2 files changed

+55
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+55
-5
lines changed
Open diff view settings
Collapse file

‎lib/internal/main/mksnapshot.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/mksnapshot.js
+13-5Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const {
77
ObjectSetPrototypeOf,
88
SafeArrayIterator,
99
SafeSet,
10+
StringPrototypeStartsWith,
11+
StringPrototypeSlice,
1012
} = primordials;
1113

1214
const binding = internalBinding('mksnapshot');
@@ -96,23 +98,29 @@ function supportedInUserSnapshot(id) {
9698
}
9799

98100
function requireForUserSnapshot(id) {
99-
if (!BuiltinModule.canBeRequiredByUsers(id)) {
101+
let normalizedId = id;
102+
if (StringPrototypeStartsWith(id, 'node:')) {
103+
normalizedId = StringPrototypeSlice(id, 5);
104+
}
105+
if (!BuiltinModule.canBeRequiredByUsers(normalizedId) ||
106+
(id !== normalizedId &&
107+
!BuiltinModule.canBeRequiredWithoutScheme(normalizedId))) {
100108
// eslint-disable-next-line no-restricted-syntax
101109
const err = new Error(
102110
`Cannot find module '${id}'. `,
103111
);
104112
err.code = 'MODULE_NOT_FOUND';
105113
throw err;
106114
}
107-
if (!supportedInUserSnapshot(id)) {
108-
if (!warnedModules.has(id)) {
115+
if (!supportedInUserSnapshot(normalizedId)) {
116+
if (!warnedModules.has(normalizedId)) {
109117
process.emitWarning(
110118
`built-in module ${id} is not yet supported in user snapshots`);
111-
warnedModules.add(id);
119+
warnedModules.add(normalizedId);
112120
}
113121
}
114122

115-
return require(id);
123+
return require(normalizedId);
116124
}
117125

118126
function main() {
Collapse file
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
// This tests snapshot JS API using the example in the docs.
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const { spawnSync } = require('child_process');
8+
const tmpdir = require('../common/tmpdir');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
tmpdir.refresh();
13+
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
14+
{
15+
// The list of modules supported in the snapshot is unstable, so just check
16+
// a few that are known to work.
17+
const code = `
18+
require("node:v8");
19+
require("node:fs");
20+
require("node:fs/promises");
21+
`;
22+
fs.writeFileSync(
23+
path.join(tmpdir.path, 'entry.js'),
24+
code,
25+
'utf8'
26+
);
27+
const child = spawnSync(process.execPath, [
28+
'--snapshot-blob',
29+
blobPath,
30+
'--build-snapshot',
31+
'entry.js',
32+
], {
33+
cwd: tmpdir.path
34+
});
35+
if (child.status !== 0) {
36+
console.log(child.stderr.toString());
37+
console.log(child.stdout.toString());
38+
assert.strictEqual(child.status, 0);
39+
}
40+
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
41+
assert(stats.isFile());
42+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.