Skip to content

Commit

Permalink
Use updated API to run user script
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 6, 2025
1 parent 9faf9ac commit 68c81d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/asar_monkey_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const util = require('util')

// The root dir of asar archive.
const rootDir = path._makeLong(path.join(execPath, 'asar'))
const rootDir = path._makeLong(path.join(process.execPath, 'asar'))

// Convert asar archive's Stats object to fs's Stats object.
let nextInode = 0
Expand Down Expand Up @@ -208,7 +208,7 @@ exports.wrapFsWithAsar = function(fs) {
if (info.unpacked)
return real
else
return path.join(func(execPath), 'asar', real)
return path.join(func(process.execPath), 'asar', real)
}
}

Expand All @@ -232,7 +232,7 @@ exports.wrapFsWithAsar = function(fs) {
if (info.unpacked) {
callback(null, real)
} else {
func(execPath, function(err, p) {
func(process.execPath, function(err, p) {
if (err)
return callback(err)
return callback(null, path.join(p, 'asar', real))
Expand Down Expand Up @@ -358,7 +358,7 @@ exports.wrapFsWithAsar = function(fs) {
}

const buffer = Buffer.alloc(info.size)
fs.open(execPath, 'r', function(error, fd) {
fs.open(process.execPath, 'r', function(error, fd) {
if (error)
return callback(error)
fs.read(fd, buffer, 0, info.size, info.offset, function(error) {
Expand Down
4 changes: 3 additions & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function wrapWithActivateUvLoop(func) {
require('asar_monkey_patch').wrapFsWithAsar(require('fs'))

// Redirect Node to execute from current ASAR archive.
return dirname
const {executeUserEntryPoint} = internalRequire('internal/modules/run_main')
process.argv.splice(1, 0, dirname)
executeUserEntryPoint(dirname)
} catch (error) {
// Not an ASAR archive, continue to Node's default routine.
if (error.message != 'Not an ASAR archive')
Expand Down
22 changes: 7 additions & 15 deletions src/yode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,10 @@ void Bootstrap(node::Environment* env,
// Invoke the |bootstrap| with |exports|.
std::vector<v8::Local<v8::Value>> args = { process, require, exports };
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
v8::MaybeLocal<v8::Value> ret = bootstrap->Call(env->context(),
env->context()->Global(),
args.size(),
args.data());
// Change process.argv if the binary starts itself.
v8::Local<v8::Value> r;
if (ret.ToLocal(&r) && r->IsString()) {
auto& argv = const_cast<std::vector<std::string>&>(env->argv());
argv.insert(++argv.begin(), *v8::String::Utf8Value(env->isolate(), r));
}
bootstrap->Call(env->context(),
env->context()->Global(),
args.size(),
args.data()).ToLocalChecked();
}

// Like SpinEventLoop but replaces the uv_run with RunLoop.
Expand Down Expand Up @@ -145,13 +139,11 @@ int Start(int argc, char* argv[]) {
g_node_integration->Init();
}

// Load bootstrap script.
if (g_node_integration)
env->set_embedder_preload(&Bootstrap);

// Load node.
{
node::LoadEnvironment(env.get(), node::StartExecutionCallback{});
node::LoadEnvironment(env.get(),
node::StartExecutionCallback{},
g_node_integration ? &Bootstrap : nullptr);
// Enter event loop.
if (g_node_integration) {
g_node_integration->UvRunOnce();
Expand Down

0 comments on commit 68c81d9

Please sign in to comment.