@@ -876,6 +876,137 @@ Called immediately after a promise continuation executes. This may be after a
876
876
Called when the promise receives a resolution or rejection value. This may
877
877
occur synchronously in the case of ` Promise.resolve() ` or ` Promise.reject() ` .
878
878
879
+ ## Startup Snapshot API
880
+
881
+ <!-- YAML
882
+ added: REPLACEME
883
+ -->
884
+
885
+ > Stability: 1 - Experimental
886
+
887
+ The ` v8.startupSnapshot ` interface can be used to add serialization and
888
+ deserialization hooks for custom startup snapshots. Currently the startup
889
+ snapshots can only be built into the Node.js binary from source.
890
+
891
+ ``` console
892
+ $ cd /path/to/node
893
+ $ ./configure --node-snapshot-main=entry.js
894
+ $ make node
895
+ # This binary contains the result of the execution of entry.js
896
+ $ out/Release/node
897
+ ```
898
+
899
+ In the example above, ` entry.js ` can use methods from the ` v8.startupSnapshot `
900
+ interface to specify how to save information for custom objects in the snapshot
901
+ during serialization and how the information can be used to synchronize these
902
+ objects during deserialization of the snapshot. For example, if the ` entry.js `
903
+ contains the following script:
904
+
905
+ ``` cjs
906
+ ' use strict' ;
907
+
908
+ const fs = require (' fs' );
909
+ const zlib = require (' zlib' );
910
+ const path = require (' path' );
911
+ const assert = require (' assert' );
912
+
913
+ const {
914
+ isBuildingSnapshot ,
915
+ addSerializeCallback ,
916
+ addDeserializeCallback ,
917
+ setDeserializeMainFunction
918
+ } = require (' v8' ).startupSnapshot ;
919
+
920
+ const filePath = path .resolve (__dirname , ' ../x1024.txt' );
921
+ const storage = {};
922
+
923
+ assert (isBuildingSnapshot ());
924
+
925
+ addSerializeCallback (({ filePath }) => {
926
+ storage[filePath] = zlib .gzipSync (fs .readFileSync (filePath));
927
+ }, { filePath });
928
+
929
+ addDeserializeCallback (({ filePath }) => {
930
+ storage[filePath] = zlib .gunzipSync (storage[filePath]);
931
+ }, { filePath });
932
+
933
+ setDeserializeMainFunction (({ filePath }) => {
934
+ console .log (storage[filePath].toString ());
935
+ }, { filePath });
936
+ ```
937
+
938
+ The resulted binary will simply print the data deserialized from the snapshot
939
+ during start up:
940
+
941
+ ``` console
942
+ $ out/Release/node
943
+ # Prints content of ./test/fixtures/x1024.txt
944
+ ```
945
+
946
+ Currently the API is only available to a Node.js instance launched from the
947
+ default snapshot, that is, the application deserialized from a user-land
948
+ snapshot cannot use these APIs again.
949
+
950
+ ### ` v8.startupSnapshot.addSerializeCallback(callback[, data]) `
951
+
952
+ <!-- YAML
953
+ added: REPLACEME
954
+ -->
955
+
956
+ * ` callback ` {Function} Callback to be invoked before serialization.
957
+ * ` data ` {any} Optional data that will be passed to the ` callback ` when it
958
+ gets called.
959
+
960
+ Add a callback that will be called when the Node.js instance is about to
961
+ get serialized into a snapshot and exit. This can be used to release
962
+ resources that should not or cannot be serialized or to convert user data
963
+ into a form more suitable for serialization.
964
+
965
+ ### ` v8.startupSnapshot.addDeserializeCallback(callback[, data]) `
966
+
967
+ <!-- YAML
968
+ added: REPLACEME
969
+ -->
970
+
971
+ * ` callback ` {Function} Callback to be invoked after the snapshot is
972
+ deserialized.
973
+ * ` data ` {any} Optional data that will be passed to the ` callback ` when it
974
+ gets called.
975
+
976
+ Add a callback that will be called when the Node.js instance is deserialized
977
+ from a snapshot. The ` callback ` and the ` data ` (if provided) will be
978
+ serialized into the snapshot, they can be used to re-initialize the state
979
+ of the application or to re-acquire resources that the application needs
980
+ when the application is restarted from the snapshot.
981
+
982
+ ### ` v8.startupSnapshot.setDeserializeMainFunction(callback[, data]) `
983
+
984
+ <!-- YAML
985
+ added: REPLACEME
986
+ -->
987
+
988
+ * ` callback ` {Function} Callback to be invoked as the entry point after the
989
+ snapshot is deserialized.
990
+ * ` data ` {any} Optional data that will be passed to the ` callback ` when it
991
+ gets called.
992
+
993
+ This sets the entry point of the Node.js application when it is deserialized
994
+ from a snapshot. This can be called only once in the snapshot building
995
+ script. If called, the deserialized application no longer needs an additional
996
+ entry point script to start up and will simply invoke the callback along with
997
+ the deserialized data (if provided), otherwise an entry point script still
998
+ needs to be provided to the deserialized application.
999
+
1000
+ ### ` v8.startupSnapshot.isBuildingSnapshot() `
1001
+
1002
+ <!-- YAML
1003
+ added: REPLACEME
1004
+ -->
1005
+
1006
+ * Returns: {boolean}
1007
+
1008
+ Returns true if the Node.js instance is run to build a snapshot.
1009
+
879
1010
[ HTML structured clone algorithm ] : https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
880
1011
[ Hook Callbacks ] : #hook-callbacks
881
1012
[ V8 ] : https://developers.google.com/v8/
0 commit comments