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