温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/v8/startupSnapshot
点击访问原文链接

Node v8.startupSnapshot object | API Reference | Bun

Node v8.startupSnapshot object | API Reference | Bun { // process.env and process.argv are refreshed during snapshot // deserialization. const lang = process.env.BOOK_LANG || 'en_US'; const book = process.argv[1]; const name = `${book}.${lang}.txt`; console.log(shelf.storage.get(name)); }, shelf); ``` The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed `process.env` and `process.argv` of the launched process: ```bash $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1 # Prints content of book1.es_ES.txt deserialized from the snapshot. ``` Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot."/> { // process.env and process.argv are refreshed during snapshot // deserialization. const lang = process.env.BOOK_LANG || 'en_US'; const book = process.argv[1]; const name = `${book}.${lang}.txt`; console.log(shelf.storage.get(name)); }, shelf); ``` The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed `process.env` and `process.argv` of the launched process: ```bash $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1 # Prints content of book1.es_ES.txt deserialized from the snapshot. ``` Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot."/>BuildDocsReferenceGuidesBlogDiscord/node:v8/startupSnapshotFaddDeserializeCallbackFaddSerializeCallbackFisBuildingSnapshotFsetDeserializeMainFunction

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:v8/startupSnapshotFaddDeserializeCallbackFaddSerializeCallbackFisBuildingSnapshotFsetDeserializeMainFunction

namespace

v8.startupSnapshot { // process.env and process.argv are refreshed during snapshot // deserialization. const lang = process.env.BOOK_LANG || 'en_US'; const book = process.argv[1]; const name = `${book}.${lang}.txt`; console.log(shelf.storage.get(name)); }, shelf); ``` The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed `process.env` and `process.argv` of the launched process: ```bash $ BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1 # Prints content of book1.es_ES.txt deserialized from the snapshot. ``` Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot." data-algolia-static="false" data-algolia-merged="false" data-type="Namespace">namespace startupSnapshot

The v8.startupSnapshot interface can be used to add serialization and deserialization hooks for custom startup snapshots.

node --snapshot-blob snapshot.blob --build-snapshot entry.js
# This launches a process with the snapshot
node --snapshot-blob snapshot.blob

In the example above, entry.js can use methods from the v8.startupSnapshot interface to specify how to save information for custom objects in the snapshot during serialization and how the information can be used to synchronize these objects during deserialization of the snapshot. For example, if the entry.js contains the following script:

'use strict';

import fs from 'node:fs';
import zlib from 'node:zlib';
import path from 'node:path';
import assert from 'node:assert';

import v8 from 'node:v8';

class BookShelf {
storage = new Map();

// Reading a series of files from directory and store them into storage.
constructor(directory, books) {
for (const book of books) {
this.storage.set(book, fs.readFileSync(path.join(directory, book)));
}
}

static compressAll(shelf) {
for (const [ book, content ] of shelf.storage) {
shelf.storage.set(book, zlib.gzipSync(content));
}
}

static decompressAll(shelf) {
for (const [ book, content ] of shelf.storage) {
shelf.storage.set(book, zlib.gunzipSync(content));
}
}
}

// __dirname here is where the snapshot script is placed
// during snapshot building time.
const shelf = new BookShelf(__dirname, [
'book1.en_US.txt',
'book1.es_ES.txt',
'book2.zh_CN.txt',
]);

assert(v8.startupSnapshot.isBuildingSnapshot());
// On snapshot serialization, compress the books to reduce size.
v8.startupSnapshot.addSerializeCallback(BookShelf.compressAll, shelf);
// On snapshot deserialization, decompress the books.
v8.startupSnapshot.addDeserializeCallback(BookShelf.decompressAll, shelf);
v8.startupSnapshot.setDeserializeMainFunction((shelf) => {
// process.env and process.argv are refreshed during snapshot
// deserialization.
const lang = process.env.BOOK_LANG || 'en_US';
const book = process.argv[1];
const name = `${book}.${lang}.txt`;
console.log(shelf.storage.get(name));
}, shelf);

The resulted binary will get print the data deserialized from the snapshot during start up, using the refreshed process.env and process.argv of the launched process:

BOOK_LANG=es_ES node --snapshot-blob snapshot.blob book1
# Prints content of book1.es_ES.txt deserialized from the snapshot.

Currently the application deserialized from a user-land snapshot cannot be snapshotted again, so these APIs are only available to applications that are not deserialized from a user-land snapshot.

function addDeserializeCallback(callback: StartupSnapshotCallbackFn,data?: any): void;

Add a callback that will be called when the Node.js instance is deserialized from a snapshot. The callback and the data (if provided) will be serialized into the snapshot, they can be used to re-initialize the state of the application or to re-acquire resources that the application needs when the application is restarted from the snapshot.

function addSerializeCallback(callback: StartupSnapshotCallbackFn,data?: any): void;

Add a callback that will be called when the Node.js instance is about to get serialized into a snapshot and exit. This can be used to release resources that should not or cannot be serialized or to convert user data into a form more suitable for serialization.

function isBuildingSnapshot(): boolean;

Returns true if the Node.js instance is run to build a snapshot.

function setDeserializeMainFunction(callback: StartupSnapshotCallbackFn,data?: any): void;

This sets the entry point of the Node.js application when it is deserialized from a snapshot. This can be called only once in the snapshot building script. If called, the deserialized application no longer needs an additional entry point script to start up and will simply invoke the callback along with the deserialized data (if provided), otherwise an entry point script still needs to be provided to the deserialized application.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Node v8.startupSnapshot object | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    The `v8.startupSnapshot` interface can be used to add serialization and deserialization hooks for custom startup snapshots. ```bash $ node --snapshot-blob snapshot.blob --build-snapshot entry.js # This launches a process with the snapshot $ node --snapshot-blob snapshot.blob ``` In the example above, `entry.js` can use methods from the `v8.startupSnapshot` interface to specify how to save information for custom objects in the snapshot during serialization and how the information can be used to synchronize these objects during deserialization of the snapshot. For example, if the `entry.js` contains the following script: ```js