OverviewFilesCommitsIssuesDocsReleasesPeers

favela-surface-android / lib / loader.js

31 lines · javascript ref:
// The APK's worklet entry. Preloads any OTA-staged native addons, then runs the chosen bundle.
//
// ⛔ WHY THE ENTRY MOVED HERE. worklet.js hands bare-kit a path with a null source, so Bare loads that
// file as its own entry — there is no "before" inside it. And preload code placed inside the staged
// bundle would be circular anyway: its own import of bare-fs resolves to
// `linked:libbare-fs.<staged version>.so`, the very soname that might be the one missing. So the entry
// is this file, which ships in the APK and is therefore packed against the APK's addon set, and the
// staged bundle is loaded afterwards — by which time its addons are already in Addon.cache.
//
// Everything here is wiring. The decisions are in host/loader-core.js and the loading is in
// host/preload-addons.js, both pure or injectable and both tested without a device.
import { runLoader } from './host/loader-core.js'
import { preloadAddons } from './host/preload-addons.js'

// ⛔ NEVER `import 'bare-module'` HERE. It is the runtime's OWN module system, already live inside the
// host binary — resolving it from node_modules bundles a SECOND native copy, and dlopening that copy
// dies with `bare-module.bare: undefined symbol: js_get_module_id` because its bindings are compiled
// against a different build of Bare. Measured: the preload ran and the process died on the next line.
// Dynamic import needs none of it, and a file: URL built by hand needs no bare-url either.
await runLoader({
  argv: Bare.argv,
  preload: preloadAddons,
  // ⛔ DYNAMIC import, not a static one: the whole point is that this resolves AFTER preloadAddons has
  // populated Addon.cache. A static import would be hoisted and evaluated first, which is the ordering
  // bug this file exists to avoid.
  load: (bundlePath) => import('file://' + bundlePath),
  // The worklet has no error or exit event on the RN side, so these lines are the only account of what
  // the loader did. Cheap, and the difference between "0 preloaded" and a crash is the whole diagnosis.
  say: (m) => console.log('[loader] ' + m)
})