73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
'use strict'
|
|
|
|
/**
|
|
* Pear OTA worker (hello-pear-bare pattern).
|
|
* P2P mesh/tunnel work will expand here in later phases.
|
|
* Squid does NOT run here as Node — main process owns Squid on Bare (ADR-0013).
|
|
*/
|
|
|
|
const PearRuntime = require('pear-runtime')
|
|
const Hyperswarm = require('hyperswarm')
|
|
const Corestore = require('corestore')
|
|
const goodbye = require('graceful-goodbye')
|
|
const FramedStream = require('framed-stream')
|
|
const path = require('path')
|
|
|
|
function argv(index) {
|
|
// Bare worker argv layout from PearRuntime.run
|
|
const base = typeof Bare !== 'undefined' ? Bare.argv : process.argv
|
|
// worker: [bare, entry, ...args] or similar
|
|
const offset = typeof Bare !== 'undefined' ? 2 : 2
|
|
return base[index + offset]
|
|
}
|
|
|
|
const updaterConfig = {
|
|
updates: argv(0) !== 'false',
|
|
version: argv(1),
|
|
upgrade: argv(2),
|
|
name: argv(3),
|
|
dir: argv(4),
|
|
app: argv(5)
|
|
}
|
|
|
|
const pipe = new FramedStream(typeof Bare !== 'undefined' ? Bare.IPC : process.stdin)
|
|
const store = new Corestore(path.join(updaterConfig.dir || '.', 'pear-runtime', 'corestore'))
|
|
const swarm = new Hyperswarm()
|
|
const pear = new PearRuntime({ ...updaterConfig, swarm, store })
|
|
|
|
pear.updater.on('error', (err) => console.error('[updater]', err))
|
|
|
|
if (updaterConfig.updates !== false && updaterConfig.upgrade && !String(updaterConfig.upgrade).includes('<YOUR_KEY')) {
|
|
swarm.on('connection', (connection) => store.replicate(connection))
|
|
try {
|
|
swarm.join(pear.updater.drive.core.discoveryKey, {
|
|
client: true,
|
|
server: false
|
|
})
|
|
} catch (err) {
|
|
console.error('[swarm join]', err.message)
|
|
}
|
|
}
|
|
|
|
console.log('Flying Jib worker storage:', pear.storage || updaterConfig.dir)
|
|
|
|
pear.updater.on('updating', () => pipe.write('updating'))
|
|
pear.updater.on('updated', () => pipe.write('updated'))
|
|
|
|
goodbye(async () => {
|
|
await swarm.destroy()
|
|
await pear.close()
|
|
await store.close()
|
|
})
|
|
|
|
pipe.on('data', async (data) => {
|
|
const message = data.toString()
|
|
if (message === 'pear:applyUpdate') {
|
|
await pear.ready()
|
|
await pear.updater.applyUpdate()
|
|
pipe.write('pear:updateApplied')
|
|
}
|
|
})
|
|
|
|
pipe.write('worker:ready')
|