Last active
November 24, 2023 17:24
-
-
Save lukaslihotzki/c60fef03d5a14d1c8723bc1251ede0ee to your computer and use it in GitHub Desktop.
Embed binary files into Node SEA by reading them into the snapshot
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
{ | |
"main": "server-with-assets.js", | |
"output": "sea-prep.blob", | |
"useSnapshot": true, | |
"codeCache": true | |
} |
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
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