Skip to content

Instantly share code, notes, and snippets.

@lukaslihotzki
Last active November 24, 2023 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaslihotzki/c60fef03d5a14d1c8723bc1251ede0ee to your computer and use it in GitHub Desktop.
Save lukaslihotzki/c60fef03d5a14d1c8723bc1251ede0ee to your computer and use it in GitHub Desktop.
Embed binary files into Node SEA by reading them into the snapshot
{
"main": "server-with-assets.js",
"output": "sea-prep.blob",
"useSnapshot": true,
"codeCache": true
}
function main(assets) {
const http = require("http");
const requestListener = function(req, res) {
let asset = assets[req.url];
if (asset != null) {
res.end(asset);
} else {
res.statusCode = 404;
res.end("Not Found");
}
};
const server = http.createServer(requestListener);
const [host, port] = ['localhost', 8000];
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
}
const fs = require('node:fs');
const assets = {
'/binary.dat': fs.readFileSync('binary.dat'),
};
const v8 = require('node:v8');
if (v8.startupSnapshot.isBuildingSnapshot()) {
v8.startupSnapshot.setDeserializeMainFunction(main, assets);
} else {
main(assets);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment