Skip to content

v8: add v8.startupSnapshot utils

This adds several APIs under the v8.startupSnapshot namespace for specifying hooks into the startup snapshot serialization and deserialization.

  • isBuildingSnapshot()
  • addSerializeCallback()
  • addDeserializeCallback()
  • setDeserializeMainFunction()

Example snapshot entry point that reads a file from disk, compresses it with gzip and makes it part of the serialized heap so that the deserialized instance can decompress and log the content of the file after startup:

'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 });

Fixes: https://github.com/nodejs/node/issues/42617 Refs: https://github.com/nodejs/node/issues/35711

Merge request reports

Loading