@@ -178,14 +178,52 @@ The configuration currently reads the following top-level fields:
178
178
"output": "/path/to/write/the/generated/blob.blob",
179
179
"disableExperimentalSEAWarning": true, // Default: false
180
180
"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
+ }
182
186
}
183
187
```
184
188
185
189
If the paths are not absolute, Node.js will use the path relative to the
186
190
current working directory. The version of the Node.js binary used to produce
187
191
the blob must be the same as the one to which the blob will be injected.
188
192
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.
221
+ const blob = getAssetAsBlob(' a.jpg' );
222
+ ```
223
+
224
+ See documentation of the [`sea.getAsset()`][] and [`sea.getAssetAsBlob()`][]
225
+ APIs for more information.
226
+
189
227
### Startup snapshot support
190
228
191
229
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.
229
267
230
268
**Note:** `import()` does not work when `useCodeCache` is `true`.
231
269
232
- ## Notes
270
+ ## In the injected main script
233
271
234
- ### `require(id)` in the injected module is not file based
272
+ ### Single-executable application API
235
273
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
+ An error is thrown when no matching asset can be found.
295
+
296
+ * `key` {string} the key for the asset in the dictionary specified by the
297
+ `assets` field in the single-executable application configuration.
298
+ * `encoding` {string} If specified, the asset will be decoded as
299
+ a string. Any encoding supported by the `TextDecoder` is accepted.
300
+ If unspecified, an `ArrayBuffer` containing a copy of the asset would be
301
+ returned instead.
302
+ * Returns: {string|ArrayBuffer}
303
+
304
+ ### `sea.getAssetAsBlob(key[, options])`
305
+
306
+ <!-- YAML
307
+ added: REPLACEME
308
+ -->
309
+
310
+ Similar to [`sea.getAsset()`][], but returns the result in a [`Blob`][].
311
+ An error is thrown when no matching asset can be found.
312
+
313
+ * `key` {string} the key for the asset in the dictionary specified by the
314
+ `assets` field in the single-executable application configuration.
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()`][]
237
322
available to modules that are not injected. It also does not have any of the
238
323
properties that non-injected [`require()`][] has except [`require.main`][]. It
239
324
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');
250
335
require = createRequire(__filename);
251
336
```
252
337
253
- ### `__filename` and `module.filename` in the injected module
338
+ ### `__filename` and `module.filename` in the injected main script
254
339
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`][].
257
342
258
- ### `__dirname` in the injected module
343
+ ### `__dirname` in the injected main script
259
344
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
262
349
263
350
### Single executable application creation process
264
351
@@ -298,9 +385,12 @@ to help us document them.
298
385
[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
299
386
[PE]: https://en.wikipedia.org/wiki/Portable_Executable
300
387
[Windows SDK]: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
388
+ [`Blob`]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
301
389
[`process.execPath`]: process.md#processexecpath
302
390
[`require()`]: modules.md#requireid
303
391
[`require.main`]: modules.md#accessing-the-main-module
392
+ [`sea.getAsset()`]: #seagetassetkey-encoding
393
+ [`sea.getAssetAsBlob()`]: #seagetassetasblobkey-options
304
394
[`v8.startupSnapshot.setDeserializeMainFunction()`]: v8.md#v8startupsnapshotsetdeserializemainfunctioncallback-data
305
395
[`v8.startupSnapshot` API]: v8.md#startup-snapshot-api
306
396
[documentation about startup snapshot support in Node.js]: cli.md#--build-snapshot
0 commit comments