Files
peardock/electron/preload.cjs
T
2026-07-11 11:39:59 -04:00

111 lines
2.5 KiB
JavaScript

/**
* Preload + Pear polyfill for the full peardock GUI under Electron.
* Runs with contextIsolation: false / nodeIntegration: true so app.js ESM
* can import HyperDHT packages (pear-electron UI equivalent).
*/
'use strict'
const { ipcRenderer } = require('electron')
const path = require('path')
const fs = require('fs')
const os = require('os')
const cfg = ipcRenderer.sendSync('peardock:get-pear-config') || {}
const storage =
cfg.storage ||
process.env.PEARDOCK_STORAGE ||
path.join(os.homedir(), '.config', 'peardock', 'storage')
try {
fs.mkdirSync(storage, { recursive: true })
} catch {
// ignore
}
const teardownHooks = []
/** Pear surface used by peardock index/holesailLocal/UI */
const Pear = {
config: {
storage,
name: cfg.name || 'peardock',
version: cfg.version || '0.0.0',
},
// alias some runtimes use
app: {
storage,
name: cfg.name || 'peardock',
},
exit(code = 0) {
try {
ipcRenderer.invoke('peardock:exit', code)
} catch {
// ignore
}
},
teardown(fn) {
if (typeof fn === 'function') teardownHooks.push(fn)
},
constructor: {
IPC: null,
UI: null,
CUTOVER: false,
},
}
globalThis.Pear = Pear
// Also on window for any non-module scripts
try {
window.Pear = Pear
} catch {
// ignore
}
// pear-ctrl custom element is a no-op stub outside pear platform chrome
try {
if (typeof customElements !== 'undefined' && !customElements.get('pear-ctrl')) {
class PearCtrl extends HTMLElement {
connectedCallback() {
this.style.display = 'none'
}
}
customElements.define('pear-ctrl', PearCtrl)
}
} catch {
// ignore
}
window.addEventListener('beforeunload', () => {
for (const fn of teardownHooks) {
try {
const r = fn()
if (r && typeof r.then === 'function') r.catch(() => {})
} catch {
// ignore
}
}
})
// OTA status from main (pear-runtime)
try {
const { ipcRenderer } = require('electron')
ipcRenderer.on('peardock:ota', (_evt, data) => {
try {
window.dispatchEvent(new CustomEvent('peardock-ota', { detail: data }))
} catch {
// ignore
}
if (data?.type === 'updating') {
console.log('[peardock] OTA downloading update…')
} else if (data?.type === 'updated') {
console.log('[peardock] OTA update ready', data.nextVersion)
} else if (data?.type === 'applied') {
console.log('[peardock] OTA applied — relaunching')
}
})
} catch {
// ignore
}
console.log('[peardock] Pear polyfill ready, storage=', storage)