Skip to content

Commit

Permalink
Refactor bridge.js for lazy loading and boot sequence improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Aug 19, 2024
1 parent 836170e commit be93de3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [16, 18]
node-version: [16, 18, 22]
os: [ubuntu-latest, windows-latest]
include:
- coverage: true
Expand Down
51 changes: 39 additions & 12 deletions lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,35 @@ const Flatted = require('flatted'),
${bridgeClientCode()}
(function (emit, id) {
__uvm_startBoot = (function (uvmEmit, uvmTimeout) {
return function () {
// A 5ms delay is added to ensure the required modules are loaded.
uvmTimeout(() => {
try {
${bootCode};
} catch (error) {
uvmTimeout(() => { throw error; }, 0);
}
uvmEmit('${Flatted.stringify(['load.' + id])}');
}, 5);
};
}(__uvm_emit, setTimeout));
(function (emit, boot, id) {
__uvm_addEventListener("message", function (e) {
const { __emit_uvm, __id_uvm } = e?.data || e || {};
if (typeof __emit_uvm === 'string' && __id_uvm === id) {
emit(__emit_uvm);
if (__emit_uvm === '${Flatted.stringify(['boot-start'])}') {
boot();
} else {
emit(__emit_uvm);
}
}
});
}(__uvm_dispatch, "${id}"));
}(__uvm_dispatch, __uvm_startBoot, "${id}"));
__uvm_dispatch = null; delete __uvm_dispatch;
__uvm_startBoot = null; delete __uvm_startBoot;
__uvm_addEventListener = null; delete __uvm_addEventListener;
(function (self, bridge, setTimeout) {
Expand All @@ -48,16 +68,12 @@ const Flatted = require('flatted'),
__self = null; delete __self;
// boot code starts hereafter
__uvm_setTimeout = setTimeout;
try {
${bootCode};
} catch (error) {
__uvm_setTimeout(() => { throw error; }, 0);
}
__uvm_emit('${Flatted.stringify(['load.' + id])}');
// 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])}');
__uvm_emit = null; delete __uvm_emit;
__uvm_setTimeout = null; delete __uvm_setTimeout;
`;
};

Expand Down Expand Up @@ -133,6 +149,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

0 comments on commit be93de3

Please sign in to comment.