|
1 | 1 | /** |
2 | 2 | * bootstrap.js — HarmonyOS entry point. |
3 | 3 | * |
4 | | - * On HarmonyOS, the Electron 鸿蒙 runtime starts the Node.js process before |
5 | | - * the ArkTS layer has finished calling JsBindingUtils.SetContextPaths(). |
6 | | - * If app.js (which transitively loads app-props.js) is loaded too early, |
7 | | - * app.getPath('appData') throws "Failed to get 'appData' path" and crashes |
8 | | - * the main process. |
| 4 | + * On HarmonyOS, app.getPath('appData') does not work in the Electron runtime |
| 5 | + * due to a C++ binding issue (SetContextPaths is not effective). Instead, |
| 6 | + * AbilityStage.ets writes the sandbox filesDir to a marker file before the |
| 7 | + * Electron runtime starts. This file reads that marker and sets |
| 8 | + * process.env.DATA_PATH so all downstream modules (app-props.js, db.js, etc.) |
| 9 | + * use the correct sandbox path. |
9 | 10 | * |
10 | | - * This bootstrap polls app.getPath('appData') until it succeeds, then loads |
11 | | - * app.js. This guarantees that all downstream modules (db.js, ipc.js, etc.) |
12 | | - * can safely call app.getPath() at module load time. |
13 | | - * |
14 | | - * On non-HarmonyOS platforms, app.getPath('appData') works immediately and |
15 | | - * app.js is loaded without delay. |
| 11 | + * Path derivation: |
| 12 | + * __dirname = /data/storage/el1/bundle/entry/resources/resfile/resources/app |
| 13 | + * filesDir = /data/storage/el2/base/haps/entry/files |
| 14 | + * Transform: replace "/el1/bundle/" with "/el2/base/haps/", strip the |
| 15 | + * resources/resfile/... suffix, append "/files". |
16 | 16 | */ |
17 | | -const { app } = require('electron') |
18 | | - |
19 | | -const POLL_INTERVAL = 50 // ms |
20 | | -const MAX_WAIT = 10000 // 10 seconds |
| 17 | +const fs = require('fs') |
| 18 | +const path = require('path') |
| 19 | +const os = require('os') |
21 | 20 |
|
22 | | -function isPathReady () { |
23 | | - try { |
24 | | - app.getPath('appData') |
25 | | - return true |
26 | | - } catch { |
27 | | - return false |
| 21 | +function deriveSandboxFilesDir () { |
| 22 | + // __dirname is like: /data/storage/el1/bundle/entry/resources/resfile/resources/app |
| 23 | + // sandbox filesDir is like: /data/storage/el2/base/haps/entry/files |
| 24 | + const m = __dirname.match(/^(.+?)\/el1\/bundle\/([^/]+)/) |
| 25 | + if (m) { |
| 26 | + return `${m[1]}/el2/base/haps/${m[2]}/files` |
28 | 27 | } |
| 28 | + return null |
29 | 29 | } |
30 | 30 |
|
31 | | -function startApp () { |
32 | | - require('./app.js') |
33 | | -} |
| 31 | +function getDataPath () { |
| 32 | + const derivedDir = deriveSandboxFilesDir() |
| 33 | + |
| 34 | + if (derivedDir) { |
| 35 | + // 1. Try reading the marker file written by AbilityStage.ets |
| 36 | + const markerPath = path.join(derivedDir, '.electerm-data-path') |
| 37 | + try { |
| 38 | + const data = fs.readFileSync(markerPath, 'utf8').trim() |
| 39 | + if (data) { |
| 40 | + console.log('[bootstrap] got data path from marker file:', data) |
| 41 | + return data |
| 42 | + } |
| 43 | + } catch (e) { |
| 44 | + console.log('[bootstrap] marker file not found at', markerPath) |
| 45 | + } |
34 | 46 |
|
35 | | -if (isPathReady()) { |
36 | | - startApp() |
37 | | -} else { |
38 | | - console.log('[bootstrap] waiting for app.getPath("appData") to become available...') |
39 | | - const startTime = Date.now() |
40 | | - const timer = setInterval(() => { |
41 | | - if (isPathReady()) { |
42 | | - clearInterval(timer) |
43 | | - console.log(`[bootstrap] app.getPath("appData") ready after ${Date.now() - startTime}ms`) |
44 | | - startApp() |
45 | | - } else if (Date.now() - startTime > MAX_WAIT) { |
46 | | - clearInterval(timer) |
47 | | - console.error(`[bootstrap] timed out after ${MAX_WAIT}ms waiting for app.getPath("appData")`) |
48 | | - // Start anyway — app-props.js has a try/catch fallback |
49 | | - startApp() |
| 47 | + // 2. Marker not found — try the derived path directly |
| 48 | + try { |
| 49 | + fs.mkdirSync(derivedDir, { recursive: true }) |
| 50 | + console.log('[bootstrap] using derived sandbox path:', derivedDir) |
| 51 | + return derivedDir |
| 52 | + } catch (e) { |
| 53 | + console.warn('[bootstrap] derived path not writable:', derivedDir, e.message) |
50 | 54 | } |
51 | | - }, POLL_INTERVAL) |
| 55 | + } |
| 56 | + |
| 57 | + // 3. Last resort: try app.getPath('appData') |
| 58 | + try { |
| 59 | + const { app } = require('electron') |
| 60 | + const p = app.getPath('appData') |
| 61 | + console.log('[bootstrap] using app.getPath("appData"):', p) |
| 62 | + return p |
| 63 | + } catch (e) { |
| 64 | + console.warn('[bootstrap] app.getPath("appData") failed:', e.message) |
| 65 | + } |
| 66 | + |
| 67 | + // 4. Final fallback |
| 68 | + console.warn('[bootstrap] falling back to os.tmpdir():', os.tmpdir()) |
| 69 | + return os.tmpdir() |
52 | 70 | } |
| 71 | + |
| 72 | +process.env.DATA_PATH = getDataPath() |
| 73 | +require('./app.js') |
0 commit comments