Skip to content

Commit b35c591

Browse files
committed
sea: support embedding assets
With this patch: Users can now include assets by adding a key-path dictionary to the configuration as the `assets` field. At build time, Node.js would read the assets from the specified paths and bundle them into the preparation blob. In the generated executable, users can retrieve the assets using the `sea.getAsset()` and `sea.getAssetAsBlob()` API. ```json { "main": "/path/to/bundled/script.js", "output": "/path/to/write/the/generated/blob.blob", "assets": { "a.jpg": "/path/to/a.jpg", "b.txt": "/path/to/b.txt" } } ``` The single-executable application can access the assets as follows: ```cjs const { getAsset } = require('node:sea'); // Returns a copy of the data in an ArrayBuffer const image = getAsset('a.jpg'); // Returns a string decoded from the asset as UTF8. const text = getAsset('b.txt', 'utf8'); // Returns a Blob containing the asset without copying. const blob = getAssetAsBlob('a.jpg'); ``` Drive-by: update the documentation to include a section dedicated to the injected main script and refer to it as "injected main script" instead of "injected module" because it's a script, not a module.
1 parent 500ff24 commit b35c591

12 files changed

+529
-12
lines changed

doc/api/errors.md

+14
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,13 @@ error indicates that the idle loop has failed to stop.
23602360
An attempt was made to use operations that can only be used when building
23612361
V8 startup snapshot even though Node.js isn't building one.
23622362

2363+
<a id="ERR_NOT_IN_SEA"></a>
2364+
2365+
### `ERR_NOT_IN_SEA`
2366+
2367+
The operation cannot be performed when it's not in a single-executable
2368+
application.
2369+
23632370
<a id="ERR_NOT_SUPPORTED_IN_SNAPSHOT"></a>
23642371

23652372
### `ERR_NOT_SUPPORTED_IN_SNAPSHOT`
@@ -2490,6 +2497,13 @@ example, <kbd>Ctrl</kbd>+<kbd>C</kbd> was pressed.)
24902497

24912498
Script execution timed out, possibly due to bugs in the script being executed.
24922499

2500+
<a id="ERR_SEA_ASSET_NOT_FOUND"></a>
2501+
2502+
### `ERR_SEA_ASSET_NOT_FOUND`
2503+
2504+
A key was passed to single exectuable application APIs to identify an asset,
2505+
but no match could be found.
2506+
24932507
<a id="ERR_SERVER_ALREADY_LISTEN"></a>
24942508

24952509
### `ERR_SERVER_ALREADY_LISTEN`

doc/api/single-executable-applications.md

+100-10
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,52 @@ The configuration currently reads the following top-level fields:
178178
"output": "/path/to/write/the/generated/blob.blob",
179179
"disableExperimentalSEAWarning": true, // Default: false
180180
"useSnapshot": false, // Default: false
181-
"useCodeCache": true // Default: false
181+
"useCodeCache": true, // Default: false
182+
"assets": { // Optional
183+
"a.dat": "/path/to/a.dat",
184+
"b.txt": "/path/to/b.txt"
185+
}
182186
}
183187
```
184188
185189
If the paths are not absolute, Node.js will use the path relative to the
186190
current working directory. The version of the Node.js binary used to produce
187191
the blob must be the same as the one to which the blob will be injected.
188192
193+
### Assets
194+
195+
Users can include assets by adding a key-path dictionary to the configuration
196+
as the `assets` field. At build time, Node.js would read the assets from the
197+
specified paths and bundle them into the preparation blob. In the generated
198+
executable, users can retrieve the assets using the [`sea.getAsset()`][] and
199+
[`sea.getAssetAsBlob()`][] APIs.
200+
201+
```json
202+
{
203+
"main": "/path/to/bundled/script.js",
204+
"output": "/path/to/write/the/generated/blob.blob",
205+
"assets": {
206+
"a.jpg": "/path/to/a.jpg",
207+
"b.txt": "/path/to/b.txt"
208+
}
209+
}
210+
```
211+
212+
The single-executable application can access the assets as follows:
213+
214+
```cjs
215+
const { getAsset } = require('node:sea');
216+
// Returns a copy of the data in an ArrayBuffer.
217+
const image = getAsset('a.jpg');
218+
// Returns a string decoded from the asset as UTF8.
219+
const text = getAsset('b.txt', 'utf8');
220+
// Returns a Blob containing the asset without copying.
221+
const blob = getAssetAsBlob('a.jpg');
222+
```
223+
224+
See documentation of the [`sea.getAsset()`][] and [`sea.getAssetAsBlob()`][]
225+
APIs for more information.
226+
189227
### Startup snapshot support
190228
191229
The `useSnapshot` field can be used to enable startup snapshot support. In this
@@ -229,11 +267,58 @@ execute the script, which would improve the startup performance.
229267
230268
**Note:** `import()` does not work when `useCodeCache` is `true`.
231269
232-
## Notes
270+
## In the injected main script
233271
234-
### `require(id)` in the injected module is not file based
272+
### Single-executable application API
235273
236-
`require()` in the injected module is not the same as the [`require()`][]
274+
The `node:sea` builtin allows interaction with the single-executable application
275+
from the JavaScript main script embedded into the executable.
276+
277+
#### `sea.isSea()`
278+
279+
<!-- YAML
280+
added: REPLACEME
281+
-->
282+
283+
* Returns: {boolean} Whether this script is running inside a single-executable
284+
application.
285+
286+
### `sea.getAsset(key[, encoding])`
287+
288+
<!-- YAML
289+
added: REPLACEME
290+
-->
291+
292+
This method can be used to retrieve the assets configured to be bundled into the
293+
single-executable application at build time.
294+
295+
* `key` {string} the key for the asset in the dictionary specified by the
296+
`assets` field in the single-executable applciation configuratoin.
297+
* `encoding` {undefined|string} If specified, the asset will be decoded as
298+
a string. Any encoding supported by the `TextDecoder` is accpeted.
299+
If unspecified, an `ArrayBuffer` containing a copy of the asset would be
300+
returned instead.
301+
* Returns: {string|ArrayBuffer}
302+
303+
### `sea.getAssetAsBlob(key[, options])`
304+
305+
<!-- YAML
306+
added: REPLACEME
307+
-->
308+
309+
Similar to [`sea.getAsset()`][], but returns the result in a [`Blob`][]
310+
which allows users to read the data from the embedded asset without
311+
copying it first.
312+
313+
* `key` {string} the key for the asset in the dictionary specified by the
314+
`assets` field in the single-executable applciation configuratoin.
315+
* `options` {Object}
316+
* `type` {string} An optional mime type for the blob.
317+
* Returns: {Blob}
318+
319+
### `require(id)` in the injected main script is not file based
320+
321+
`require()` in the injected main script is not the same as the [`require()`][]
237322
available to modules that are not injected. It also does not have any of the
238323
properties that non-injected [`require()`][] has except [`require.main`][]. It
239324
can only be used to load built-in modules. Attempting to load a module that can
@@ -250,15 +335,17 @@ const { createRequire } = require('node:module');
250335
require = createRequire(__filename);
251336
```
252337
253-
### `__filename` and `module.filename` in the injected module
338+
### `__filename` and `module.filename` in the injected main script
254339
255-
The values of `__filename` and `module.filename` in the injected module are
256-
equal to [`process.execPath`][].
340+
The values of `__filename` and `module.filename` in the injected main script
341+
are equal to [`process.execPath`][].
257342
258-
### `__dirname` in the injected module
343+
### `__dirname` in the injected main script
259344
260-
The value of `__dirname` in the injected module is equal to the directory name
261-
of [`process.execPath`][].
345+
The value of `__dirname` in the injected main script is equal to the directory
346+
name of [`process.execPath`][].
347+
348+
## Notes
262349
263350
### Single executable application creation process
264351
@@ -298,9 +385,12 @@ to help us document them.
298385
[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
299386
[PE]: https://en.wikipedia.org/wiki/Portable_Executable
300387
[Windows SDK]: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
388+
[`Blob`]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
301389
[`process.execPath`]: process.md#processexecpath
302390
[`require()`]: modules.md#requireid
303391
[`require.main`]: modules.md#accessing-the-main-module
392+
[`sea.getAsset()`]: #seagetassetkey-encoding
393+
[`sea.getAssetAsBlob()`]: #seagetassetasblobkey-options
304394
[`v8.startupSnapshot.setDeserializeMainFunction()`]: v8.md#v8startupsnapshotsetdeserializemainfunctioncallback-data
305395
[`v8.startupSnapshot` API]: v8.md#startup-snapshot-api
306396
[documentation about startup snapshot support in Node.js]: cli.md#--build-snapshot

lib/internal/bootstrap/realm.js

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const legacyWrapperList = new SafeSet([
128128
// beginning with "internal/".
129129
// Modules that can only be imported via the node: scheme.
130130
const schemelessBlockList = new SafeSet([
131+
'sea',
131132
'test',
132133
'test/reporters',
133134
]);

lib/internal/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,8 @@ E('ERR_NETWORK_IMPORT_DISALLOWED',
16011601
"import of '%s' by %s is not supported: %s", Error);
16021602
E('ERR_NOT_BUILDING_SNAPSHOT',
16031603
'Operation cannot be invoked when not building startup snapshot', Error);
1604+
E('ERR_NOT_IN_SEA',
1605+
'Operation cannot be invoked when not in a single-executable application', Error);
16041606
E('ERR_NOT_SUPPORTED_IN_SNAPSHOT', '%s is not supported in startup snapshot', Error);
16051607
E('ERR_NO_CRYPTO',
16061608
'Node.js is not compiled with OpenSSL crypto support', Error);
@@ -1681,6 +1683,8 @@ E('ERR_REQUIRE_ESM',
16811683
}, Error);
16821684
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
16831685
'Script execution was interrupted by `SIGINT`', Error);
1686+
E('ERR_SEA_ASSET_NOT_FOUND',
1687+
'Cannot find asset %s for the single executable application', Error);
16841688
E('ERR_SERVER_ALREADY_LISTEN',
16851689
'Listen method has been called more than once without closing.', Error);
16861690
E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);

lib/sea.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const { isSea, getAsset: getAssetInternal } = internalBinding('sea');
4+
const { TextDecoder } = require('internal/encoding');
5+
const { validateString } = require('internal/validators');
6+
const {
7+
ERR_NOT_IN_SEA,
8+
ERR_SEA_ASSET_NOT_FOUND,
9+
} = require('internal/errors').codes;
10+
const { Blob } = require('internal/blob');
11+
12+
function getRawAsset(key) {
13+
validateString(key, 'key');
14+
15+
if (!isSea()) {
16+
throw new ERR_NOT_IN_SEA();
17+
}
18+
19+
const asset = getAssetInternal(key);
20+
if (asset === undefined) {
21+
throw new ERR_SEA_ASSET_NOT_FOUND(key);
22+
}
23+
return asset;
24+
}
25+
26+
function getAsset(key, encoding) {
27+
if (encoding !== undefined) {
28+
validateString(encoding, 'encoding');
29+
}
30+
const asset = getRawAsset(key);
31+
if (encoding === undefined) {
32+
return asset.slice();
33+
}
34+
const decoder = new TextDecoder(encoding);
35+
return decoder.decode(asset);
36+
}
37+
38+
function getAssetAsBlob(key, options) {
39+
const asset = getRawAsset(key);
40+
return new Blob([asset], options);
41+
}
42+
43+
module.exports = {
44+
isSea,
45+
getAsset,
46+
getAssetAsBlob,
47+
};

src/json_parser.cc

+47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "util-inl.h"
55

66
namespace node {
7+
using v8::Array;
78
using v8::Context;
89
using v8::Isolate;
910
using v8::Local;
@@ -101,4 +102,50 @@ std::optional<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
101102
return value->BooleanValue(isolate);
102103
}
103104

105+
std::optional<std::map<std::string, std::string>>
106+
JSONParser::GetTopLevelDictOfStrings(std::string_view field) {
107+
Isolate* isolate = isolate_.get();
108+
v8::HandleScope handle_scope(isolate);
109+
Local<Context> context = context_.Get(isolate);
110+
Local<Object> content_object = content_.Get(isolate);
111+
Local<Value> value;
112+
bool has_field;
113+
// It's not a real script, so don't print the source line.
114+
errors::PrinterTryCatch bootstrapCatch(
115+
isolate, errors::PrinterTryCatch::kDontPrintSourceLine);
116+
Local<Value> field_local;
117+
if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) {
118+
return std::nullopt;
119+
}
120+
if (!content_object->Has(context, field_local).To(&has_field)) {
121+
return std::nullopt;
122+
}
123+
if (!has_field) {
124+
return std::map<std::string, std::string>{};
125+
}
126+
if (!content_object->Get(context, field_local).ToLocal(&value) ||
127+
!value->IsObject()) {
128+
return std::nullopt;
129+
}
130+
Local<Object> dict = value.As<Object>();
131+
Local<Array> keys;
132+
if (!dict->GetOwnPropertyNames(context).ToLocal(&keys)) {
133+
return std::nullopt;
134+
}
135+
std::map<std::string, std::string> result;
136+
uint32_t length = keys->Length();
137+
for (uint32_t i = 0; i < length; ++i) {
138+
Local<Value> key;
139+
Local<Value> value;
140+
if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) return {};
141+
if (!dict->Get(context, key).ToLocal(&value) || !value->IsString())
142+
return {};
143+
144+
Utf8Value key_utf8(isolate, key);
145+
Utf8Value value_utf8(isolate, value);
146+
result.emplace(*key_utf8, *value_utf8);
147+
}
148+
return result;
149+
}
150+
104151
} // namespace node

src/json_parser.h

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include <map>
67
#include <memory>
78
#include <optional>
89
#include <string>
@@ -20,6 +21,8 @@ class JSONParser {
2021
bool Parse(const std::string& content);
2122
std::optional<std::string> GetTopLevelStringField(std::string_view field);
2223
std::optional<bool> GetTopLevelBoolField(std::string_view field);
24+
std::optional<std::map<std::string, std::string>> GetTopLevelDictOfStrings(
25+
std::string_view field);
2326

2427
private:
2528
// We might want a lighter-weight JSON parser for this use case. But for now

0 commit comments

Comments
 (0)