doc,test: update the v8.startupSnapshot doc and test the example · nodejs/node@811b43c · GitHub
Skip to content

Commit 811b43c

Browse files
joyeecheungtargos
authored andcommitted
doc,test: update the v8.startupSnapshot doc and test the example
The API is now available to user-land run-time snapshots. So update the example. This also makes the intention of the examples a bit clearer and test it in our test suite. PR-URL: #47468 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent f710676 commit 811b43c

3 files changed

Lines changed: 124 additions & 67 deletions

File tree

doc/api/v8.md

Lines changed: 53 additions & 32 deletions

test/fixtures/snapshot/v8-startup-snapshot-api.js

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
11
'use strict';
22

3-
const fs = require('fs');
4-
const zlib = require('zlib');
5-
const path = require('path');
6-
const assert = require('assert');
7-
8-
const {
9-
isBuildingSnapshot,
10-
addSerializeCallback,
11-
addDeserializeCallback,
12-
setDeserializeMainFunction
13-
} = require('v8').startupSnapshot;
14-
15-
const filePath = path.resolve(__dirname, '../x1024.txt');
16-
const storage = {};
17-
18-
assert(isBuildingSnapshot());
19-
20-
addSerializeCallback(({ filePath }) => {
21-
console.error('serializing', filePath);
22-
storage[filePath] = zlib.gzipSync(fs.readFileSync(filePath));
23-
}, { filePath });
24-
25-
addDeserializeCallback(({ filePath }) => {
26-
console.error('deserializing', filePath);
27-
storage[filePath] = zlib.gunzipSync(storage[filePath]);
28-
}, { filePath });
29-
30-
setDeserializeMainFunction(({ filePath }) => {
31-
console.log(storage[filePath].toString());
32-
}, { filePath });
33-
assert.throws(() => setDeserializeMainFunction(() => {
3+
const fs = require('node:fs');
4+
const zlib = require('node:zlib');
5+
const path = require('node:path');
6+
const assert = require('node:assert');
7+
8+
const v8 = require('node:v8');
9+
10+
class BookShelf {
11+
storage = new Map();
12+
13+
// Reading a series of files from directory and store them into storage.
14+
constructor(directory, books) {
15+
for (const book of books) {
16+
this.storage.set(book, fs.readFileSync(path.join(directory, book)));
17+
};
18+
}
19+
20+
static compressAll(shelf) {
21+
for (const [ book, content ] of shelf.storage) {
22+
shelf.storage.set(book, zlib.gzipSync(content));
23+
}
24+
}
25+
26+
static decompressAll(shelf) {
27+
for (const [ book, content ] of shelf.storage) {
28+
shelf.storage.set(book, zlib.gunzipSync(content));
29+
}
30+
}
31+
}
32+
33+
// __dirname here is where the snapshot script is placed
34+
// during snapshot building time.
35+
const shelf = new BookShelf(__dirname, [
36+
'book1.en_US.txt',
37+
'book1.es_ES.txt',
38+
'book2.zh_CN.txt',
39+
]);
40+
41+
assert(v8.startupSnapshot.isBuildingSnapshot());
42+
43+
// On snapshot serialization, compress the books to reduce size.
44+
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
45+
// On snapshot deserialization, decompress the books.
46+
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
47+
v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
48+
// process.env and process.argv are refreshed during snapshot
49+
// deserialization.
50+
const lang = process.env.BOOK_LANG || 'en_US';
51+
const book = process.argv[1];
52+
const name = `${book}.${lang}.txt`;
53+
console.error('Reading', name);
54+
console.log(shelf.storage.get(name).toString());
55+
}, shelf);
56+
57+
assert.throws(() => v8.startupSnapshot.setDeserializeMainFunction(() => {
3458
assert.fail('unreachable duplicated main function');
3559
}), {
3660
code: 'ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION',

test/parallel/test-snapshot-api.js

Lines changed: 16 additions & 4 deletions

0 commit comments

Comments
 (0)