Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor bridge.js for lazy loading and boot sequence improvement #703

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
os: [ubuntu-latest, windows-latest]
include:
- coverage: true
node-version: 20
node-version: latest
os: ubuntu-latest

steps:
Expand Down
37 changes: 28 additions & 9 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,24 @@ const Flatted = require('flatted'),
__self = null; delete __self;

// boot code starts hereafter
__uvm_setTimeout = setTimeout;
try {
${bootCode};
} catch (error) {
__uvm_setTimeout(() => { throw error; }, 0);
}
// Post node v22.3.0, node uses undici's MessageEvent class which is
// loaded lazily and reads certain modules from the global scope when.
// loaded. We are doing a round trip to main thread to ensure the everything
// required for uvm to work is loaded before running the boot code, which
// might mutate the global scope.
__uvm_emit('${Flatted.stringify(['boot-ready.' + id])}');
bridge.once('boot-start', function () {
__uvm_timeout = setTimeout;
try {
${bootCode};
} catch (error) {
__uvm_timeout(() => { throw error; }, 0);
}

__uvm_emit('${Flatted.stringify(['load.' + id])}');
__uvm_emit = null; delete __uvm_emit;
__uvm_setTimeout = null; delete __uvm_setTimeout;
__uvm_emit('${Flatted.stringify(['load.' + id])}');
__uvm_timeout = null; delete __uvm_timeout;
__uvm_emit = null; delete __uvm_emit;
});
`;
};

Expand Down Expand Up @@ -133,6 +141,17 @@ module.exports = function (bridge, options, callback) {
callback(null, bridge);
});

bridge.once('boot-ready.' + id, () => {
if (!worker) {
return callback(new Error('uvm : boot sequence failed due to worker termination.'));
}

worker.postMessage({
__emit_uvm: Flatted.stringify(['boot-start']),
__id_uvm: id
});
});

// get firmware code string with boot code
firmwareCode = sandboxFirmware(options.bootCode, id, options.debug);

Expand Down
Loading