-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
56ccd59
commit 41b91c2
Showing
3 changed files
with
118 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
|
||
const { | ||
isBuildingSnapshot, | ||
addSerializeCallback, | ||
addDeserializeCallback, | ||
setDeserializeMainFunction | ||
} = require('v8').startupSnapshot; | ||
|
||
const filePath = path.resolve(__dirname, '../x1024.txt'); | ||
const storage = {}; | ||
|
||
assert(isBuildingSnapshot()); | ||
|
||
addSerializeCallback(({ filePath }) => { | ||
console.error('serializing', filePath); | ||
storage[filePath] = zlib.gzipSync(fs.readFileSync(filePath)); | ||
}, { filePath }); | ||
|
||
addDeserializeCallback(({ filePath }) => { | ||
console.error('deserializing', filePath); | ||
storage[filePath] = zlib.gunzipSync(storage[filePath]); | ||
}, { filePath }); | ||
|
||
setDeserializeMainFunction(({ filePath }) => { | ||
console.log(storage[filePath].toString()); | ||
}, { filePath }); | ||
const fs = require('node:fs'); | ||
const zlib = require('node:zlib'); | ||
const path = require('node:path'); | ||
const assert = require('node:assert'); | ||
|
||
const v8 = require('node:v8'); | ||
|
||
class BookShelf { | ||
storage = new Map(); | ||
|
||
// Reading a series of files from directory and store them into storage. | ||
constructor(directory, books) { | ||
for (const book of books) { | ||
this.storage.set(book, fs.readFileSync(path.join(directory, book))); | ||
}; | ||
} | ||
|
||
static compressAll(shelf) { | ||
for (const [ book, content ] of shelf.storage) { | ||
shelf.storage.set(book, zlib.gzipSync(content)); | ||
} | ||
} | ||
|
||
static decompressAll(shelf) { | ||
for (const [ book, content ] of shelf.storage) { | ||
shelf.storage.set(book, zlib.gunzipSync(content)); | ||
} | ||
} | ||
} | ||
|
||
// __dirname here is where the snapshot script is placed | ||
// during snapshot building time. | ||
const shelf = new BookShelf(__dirname, [ | ||
'book1.en_US.txt', | ||
'book1.es_ES.txt', | ||
'book2.zh_CN.txt', | ||
]); | ||
|
||
assert(v8.startupSnapshot.isBuildingSnapshot()); | ||
// On snapshot serialization, compress the books to reduce size. | ||
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf); | ||
// On snapshot deserialization, decompress the books. | ||
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf); | ||
v8.startupSnapshot.setDeserializeMainFunction((shelf) => { | ||
// process.env and process.argv are refreshed during snapshot | ||
// deserialization. | ||
const lang = process.env.BOOK_LANG || 'en_US'; | ||
const book = process.argv[1]; | ||
const name = `${book}.${lang}.txt`; | ||
console.error('Reading', name); | ||
console.log(shelf.storage.get(name).toString()); | ||
}, shelf); |
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