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