Commit 394c61c 1 parent a24d72a commit 394c61c Copy full SHA for 394c61c
File tree 2 files changed +55
-5
lines changed
2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 7
7
ObjectSetPrototypeOf,
8
8
SafeArrayIterator,
9
9
SafeSet,
10
+ StringPrototypeStartsWith,
11
+ StringPrototypeSlice,
10
12
} = primordials ;
11
13
12
14
const binding = internalBinding ( 'mksnapshot' ) ;
@@ -95,23 +97,29 @@ function supportedInUserSnapshot(id) {
95
97
}
96
98
97
99
function requireForUserSnapshot ( id ) {
98
- if ( ! BuiltinModule . canBeRequiredByUsers ( id ) ) {
100
+ let normalizedId = id ;
101
+ if ( StringPrototypeStartsWith ( id , 'node:' ) ) {
102
+ normalizedId = StringPrototypeSlice ( id , 5 ) ;
103
+ }
104
+ if ( ! BuiltinModule . canBeRequiredByUsers ( normalizedId ) ||
105
+ ( id !== normalizedId &&
106
+ ! BuiltinModule . canBeRequiredWithoutScheme ( normalizedId ) ) ) {
99
107
// eslint-disable-next-line no-restricted-syntax
100
108
const err = new Error (
101
109
`Cannot find module '${ id } '. ` ,
102
110
) ;
103
111
err . code = 'MODULE_NOT_FOUND' ;
104
112
throw err ;
105
113
}
106
- if ( ! supportedInUserSnapshot ( id ) ) {
107
- if ( ! warnedModules . has ( id ) ) {
114
+ if ( ! supportedInUserSnapshot ( normalizedId ) ) {
115
+ if ( ! warnedModules . has ( normalizedId ) ) {
108
116
process . emitWarning (
109
117
`built-in module ${ id } is not yet supported in user snapshots` ) ;
110
- warnedModules . add ( id ) ;
118
+ warnedModules . add ( normalizedId ) ;
111
119
}
112
120
}
113
121
114
- return require ( id ) ;
122
+ return require ( normalizedId ) ;
115
123
}
116
124
117
125
function main ( ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments