-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bootstrap: implement --snapshot-blob and --snapshot-main
This patch introduces --snapshot-main and --snapshot-blob options for creating and using user land snapshots. At the time of the creation of this patch, user land CJS modules and ESM are not yet supported in the snapshot, but a subset of builtins should already work (in particular modules loaded during bootstrap are shipped in the built-in snapshot, so they should work in user land snapshot as well), and support for more builtins are being added. To generate a snapshot using main.js as entry point and write the snapshot blob to snapshot.blob: $ node --snapshot-main main.js --snapshot-blob snapshot.blob To restore application state from snapshot.blob: $ node --snapshot-blob snapshot.blob
- Loading branch information
1 parent
abe40e8
commit 28bc446
Showing
8 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const fs = require('fs'); | ||
|
||
fs.foo = 'I am from the snapshot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
tmpdir.refresh(); | ||
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob'); | ||
const file = fixtures.path('snapshot', 'mutate-fs.js'); | ||
|
||
{ | ||
const child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); | ||
assert(stats.isFile()); | ||
} | ||
|
||
{ | ||
let child = spawnSync(process.execPath, [ | ||
'--snapshot-main', | ||
file, | ||
'--snapshot-blob', | ||
blobPath, | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
const stats = fs.statSync(blobPath); | ||
assert(stats.isFile()); | ||
|
||
child = spawnSync(process.execPath, [ | ||
'--snapshot-blob', | ||
blobPath, | ||
'-p', | ||
'require("fs").foo', | ||
], { | ||
cwd: tmpdir.path | ||
}); | ||
|
||
if (child.status !== 0) { | ||
console.log(child.stderr.toString()); | ||
console.log(child.stdout.toString()); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
assert(/I am from the snapshot/.test(child.stdout.toString())); | ||
} |