@@ -921,15 +921,12 @@ added:
921
921
> Stability: 1 - Experimental
922
922
923
923
The ` v8.startupSnapshot ` interface can be used to add serialization and
924
- deserialization hooks for custom startup snapshots. Currently the startup
925
- snapshots can only be built into the Node.js binary from source.
924
+ deserialization hooks for custom startup snapshots.
926
925
927
926
``` console
928
- $ cd /path/to/node
929
- $ ./configure --node-snapshot-main=entry.js
930
- $ make node
931
- # This binary contains the result of the execution of entry.js
932
- $ out/Release/node
927
+ $ node --snapshot-blob snapshot.blob --build-snapshot entry.js
928
+ # This launches a process with the snapshot
929
+ $ node --snapshot-blob snapshot.blob
933
930
```
934
931
935
932
In the example above, ` entry.js ` can use methods from the ` v8.startupSnapshot `
@@ -946,42 +943,66 @@ const zlib = require('node:zlib');
946
943
const path = require (' node:path' );
947
944
const assert = require (' node:assert' );
948
945
949
- const {
950
- isBuildingSnapshot ,
951
- addSerializeCallback ,
952
- addDeserializeCallback ,
953
- setDeserializeMainFunction ,
954
- } = require (' node:v8' ).startupSnapshot ;
946
+ const v8 = require (' node:v8' );
955
947
956
- const filePath = path . resolve ( __dirname , ' ../x1024.txt ' );
957
- const storage = {} ;
948
+ class BookShelf {
949
+ storage = new Map () ;
958
950
959
- assert (isBuildingSnapshot ());
951
+ // Reading a series of files from directory and store them into storage.
952
+ constructor (directory , books ) {
953
+ for (const book of books) {
954
+ this .storage .set (book, fs .readFileSync (path .join (directory, book)));
955
+ }
956
+ }
960
957
961
- addSerializeCallback (({ filePath }) => {
962
- storage[filePath] = zlib .gzipSync (fs .readFileSync (filePath));
963
- }, { filePath });
958
+ static compressAll (shelf ) {
959
+ for (const [ book , content ] of shelf .storage ) {
960
+ shelf .storage .set (book, zlib .gzipSync (content));
961
+ }
962
+ }
964
963
965
- addDeserializeCallback (({ filePath }) => {
966
- storage[filePath] = zlib .gunzipSync (storage[filePath]);
967
- }, { filePath });
964
+ static decompressAll (shelf ) {
965
+ for (const [ book , content ] of shelf .storage ) {
966
+ shelf .storage .set (book, zlib .gunzipSync (content));
967
+ }
968
+ }
969
+ }
968
970
969
- setDeserializeMainFunction (({ filePath }) => {
970
- console .log (storage[filePath].toString ());
971
- }, { filePath });
971
+ // __dirname here is where the snapshot script is placed
972
+ // during snapshot building time.
973
+ const shelf = new BookShelf (__dirname , [
974
+ ' book1.en_US.txt' ,
975
+ ' book1.es_ES.txt' ,
976
+ ' book2.zh_CN.txt' ,
977
+ ]);
978
+
979
+ assert (v8 .startupSnapshot .isBuildingSnapshot ());
980
+ // On snapshot serialization, compress the books to reduce size.
981
+ v8 .startupSnapshot .addSerializeCallback (BookShelf .compressAll , shelf);
982
+ // On snapshot deserialization, decompress the books.
983
+ v8 .startupSnapshot .addDeserializeCallback (BookShelf .decompressAll , shelf);
984
+ v8 .startupSnapshot .setDeserializeMainFunction ((shelf ) => {
985
+ // process.env and process.argv are refreshed during snapshot
986
+ // deserialization.
987
+ const lang = process .env .BOOK_LANG || ' en_US' ;
988
+ const book = process .argv [1 ];
989
+ const name = ` ${ book} .${ lang} .txt` ;
990
+ console .log (shelf .storage .get (name));
991
+ }, shelf);
972
992
```
973
993
974
- The resulted binary will simply print the data deserialized from the snapshot
975
- during start up:
994
+ The resulted binary will get print the data deserialized from the snapshot
995
+ during start up, using the refreshed ` process.env ` and ` process.argv ` of
996
+ the launched process:
976
997
977
998
``` console
978
- $ out/Release/ node
979
- # Prints content of ./test/fixtures/x1024 .txt
999
+ $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
1000
+ # Prints content of book1.es_ES .txt deserialized from the snapshot.
980
1001
```
981
1002
982
- Currently the API is only available to a Node.js instance launched from the
983
- default snapshot, that is, the application deserialized from a user-land
984
- snapshot cannot use these APIs again .
1003
+ Currently the application deserialized from a user-land snapshot cannot
1004
+ be snapshotted again, so these APIs are only available to applications
1005
+ that are not deserialized from a user-land snapshot .
985
1006
986
1007
### ` v8.startupSnapshot.addSerializeCallback(callback[, data]) `
987
1008
0 commit comments