-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. 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. PR-URL: #50960 Refs: nodejs/single-executable#68 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
- Loading branch information
1 parent
a58c98e
commit db0efa3
Showing
12 changed files
with
578 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
const { | ||
ArrayBufferPrototypeSlice, | ||
} = primordials; | ||
|
||
const { isSea, getAsset: getAssetInternal } = internalBinding('sea'); | ||
const { TextDecoder } = require('internal/encoding'); | ||
const { validateString } = require('internal/validators'); | ||
const { | ||
ERR_NOT_IN_SINGLE_EXECUTABLE_APPLICATION, | ||
ERR_SINGLE_EXECUTABLE_APPLICATION_ASSET_NOT_FOUND, | ||
} = require('internal/errors').codes; | ||
const { Blob } = require('internal/blob'); | ||
|
||
/** | ||
* Look for the asset in the injected SEA blob using the key. If | ||
* no matching asset is found an error is thrown. The returned | ||
* ArrayBuffer should not be mutated or otherwise the process | ||
* can crash due to access violation. | ||
* @param {string} key | ||
* @returns {ArrayBuffer} | ||
*/ | ||
function getRawAsset(key) { | ||
validateString(key, 'key'); | ||
|
||
if (!isSea()) { | ||
throw new ERR_NOT_IN_SINGLE_EXECUTABLE_APPLICATION(); | ||
} | ||
|
||
const asset = getAssetInternal(key); | ||
if (asset === undefined) { | ||
throw new ERR_SINGLE_EXECUTABLE_APPLICATION_ASSET_NOT_FOUND(key); | ||
} | ||
return asset; | ||
} | ||
|
||
/** | ||
* Look for the asset in the injected SEA blob using the key. If the | ||
* encoding is specified, return a string decoded from it by TextDecoder, | ||
* otherwise return *a copy* of the original data in an ArrayBuffer. If | ||
* no matching asset is found an error is thrown. | ||
* @param {string} key | ||
* @param {string|undefined} encoding | ||
* @returns {string|ArrayBuffer} | ||
*/ | ||
function getAsset(key, encoding) { | ||
if (encoding !== undefined) { | ||
validateString(encoding, 'encoding'); | ||
} | ||
const asset = getRawAsset(key); | ||
if (encoding === undefined) { | ||
return ArrayBufferPrototypeSlice(asset); | ||
} | ||
const decoder = new TextDecoder(encoding); | ||
return decoder.decode(asset); | ||
} | ||
|
||
/** | ||
* Look for the asset in the injected SEA blob using the key. If | ||
* no matching asset is found an error is thrown. The data is returned | ||
* in a Blob. If no matching asset is found an error is thrown. | ||
* @param {string} key | ||
* @param {ConstructorParameters<Blob>[1]} [options] | ||
* @returns {Blob} | ||
*/ | ||
function getAssetAsBlob(key, options) { | ||
const asset = getRawAsset(key); | ||
return new Blob([asset], options); | ||
} | ||
|
||
module.exports = { | ||
isSea, | ||
getAsset, | ||
getAssetAsBlob, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.