-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsidecar.js
More file actions
79 lines (69 loc) · 2.05 KB
/
Copy pathsidecar.js
File metadata and controls
79 lines (69 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict'
const Corestore = require('corestore')
const fs = require('bare-fs')
const Rache = require('rache')
const crasher = require('./lib/crasher.js')
const gracedown = require('pear-gracedown')
const os = require('bare-os')
const pear = require('./lib/cmd').command
const path = require('bare-path')
const { GC, PLATFORM_CORESTORE, PLATFORM_DIR, LOCALDEV, UPGRADE } = require('./constants.js')
const { version, productName, upgrade } = require('./package.json')
const { cmdArgs } = require('./argv')
crasher('sidecar')
os.setProcessTitle('pear-sidecar')
LOG.info('sidecar', '- Sidecar Booting')
module.exports = bootSidecar().catch((err) => {
LOG.error('internal', 'Sidecar Boot Failed', err)
Bare.exit(1)
})
async function gc() {
try {
await fs.promises.rm(GC, { recursive: true })
} catch {}
await fs.promises.mkdir(GC, { recursive: true })
}
async function bootSidecar() {
await gc()
const maxCacheSize = 65536
const globalCache = new Rache({ maxSize: maxCacheSize })
const nodes = pear(cmdArgs.filter((arg) => arg !== 'sidecar'))
.flags.dhtBootstrap?.split(',')
.map((tuple) => {
const [host, port] = tuple.split(':')
const int = +port
if (Number.isInteger(int) === false) throw new Error(`Invalid port: ${port}`)
return { host, port: int }
})
const corestore = new Corestore(PLATFORM_CORESTORE, {
globalCache,
manifestVersion: 1,
compat: false,
wait: true
})
await corestore.ready()
const Sidecar = require('./subsystems/sidecar/index.js')
const updater = createUpdater()
if (updater) await updater.ready()
const sidecar = new Sidecar({
updater,
corestore,
nodes
})
gracedown(() => sidecar.close())
await sidecar.ipc.ready()
function createUpdater() {
if (LOCALDEV || !upgrade) return null
const app = os.execPath()
if (!app) return null
const name = path.basename(app) || productName || 'pear'
return new Sidecar.Updater({
dir: PLATFORM_DIR,
store: corestore,
version,
upgrade: UPGRADE,
app,
name
})
}
}