mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
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.
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: https://github.com/nodejs/node/pull/50960
Refs: https://github.com/nodejs/single-executable/issues/68
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
12 files changed
+578
-12
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include "util-inl.h"
|
||||
|
||||
namespace node {
|
||||
using v8::Array;
|
||||
using v8::Context;
|
||||
using v8::Isolate;
|
||||
using v8::Local;
|
||||
@@ -101,4 +102,51 @@ std::optional<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
|
||||
return value->BooleanValue(isolate);
|
||||
}
|
||||
|
||||
std::optional<JSONParser::StringDict> JSONParser::GetTopLevelStringDict(
|
||||
std::string_view field) {
|
||||
Isolate* isolate = isolate_.get();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
Local<Context> context = context_.Get(isolate);
|
||||
Local<Object> content_object = content_.Get(isolate);
|
||||
Local<Value> value;
|
||||
bool has_field;
|
||||
// It's not a real script, so don't print the source line.
|
||||
errors::PrinterTryCatch bootstrapCatch(
|
||||
isolate, errors::PrinterTryCatch::kDontPrintSourceLine);
|
||||
Local<Value> field_local;
|
||||
if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!content_object->Has(context, field_local).To(&has_field)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!has_field) {
|
||||
return StringDict();
|
||||
}
|
||||
if (!content_object->Get(context, field_local).ToLocal(&value) ||
|
||||
!value->IsObject()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
Local<Object> dict = value.As<Object>();
|
||||
Local<Array> keys;
|
||||
if (!dict->GetOwnPropertyNames(context).ToLocal(&keys)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::unordered_map<std::string, std::string> result;
|
||||
uint32_t length = keys->Length();
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
Local<Value> key;
|
||||
Local<Value> value;
|
||||
if (!keys->Get(context, i).ToLocal(&key) || !key->IsString())
|
||||
return StringDict();
|
||||
if (!dict->Get(context, key).ToLocal(&value) || !value->IsString())
|
||||
return StringDict();
|
||||
|
||||
Utf8Value key_utf8(isolate, key);
|
||||
Utf8Value value_utf8(isolate, value);
|
||||
result.emplace(*key_utf8, *value_utf8);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
Reference in new issue
Block a user