-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsidecar.js
108 lines (91 loc) · 2.99 KB
/
sidecar.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const Localdrive = require('localdrive')
const Corestore = require('corestore')
const Hyperdrive = require('hyperdrive')
const hypercoreid = require('hypercore-id-encoding')
const fs = require('bare-fs')
const Rache = require('rache')
const subsystem = require('./subsystem.js')
const crasher = require('./lib/crasher')
const teardown = require('./lib/teardown')
const Logger = require('./lib/logger')
const {
SWAP,
GC,
PLATFORM_CORESTORE,
CHECKOUT,
LOCALDEV,
UPGRADE_LOCK,
PLATFORM_DIR,
WAKEUP
} = require('./constants')
const registerUrlHandler = require('./url-handler')
const gunk = require('./gunk')
const { flags = {} } = require('./shell')(Bare.argv.slice(1))
crasher('sidecar', SWAP, flags.log)
global.LOG = new Logger({
level: flags.logLevel,
labels: flags.logLabels,
fields: flags.logFields,
stacks: flags.logStacks,
pretty: flags.log
})
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 corestore = new Corestore(PLATFORM_CORESTORE, { globalCache, manifestVersion: 1, compat: false })
await corestore.ready()
const drive = await createPlatformDrive()
const Sidecar = await subsystem(drive, '/subsystems/sidecar/index.js')
const updater = createUpdater()
const sidecar = new Sidecar({ updater, drive, corestore, gunk, flags })
teardown(() => sidecar.close())
await sidecar.ipc.ready()
registerUrlHandler(WAKEUP)
function createUpdater () {
if (LOCALDEV) return null
const { checkout, swap } = getUpgradeTarget()
const updateDrive = checkout === CHECKOUT || hypercoreid.normalize(checkout.key) === CHECKOUT.key
? drive
: new Hyperdrive(corestore.session(), checkout.key)
return new Sidecar.Updater(updateDrive, { directory: PLATFORM_DIR, swap, lock: UPGRADE_LOCK, checkout })
}
async function createPlatformDrive () {
if (LOCALDEV) return new Localdrive(SWAP)
const drive = new Hyperdrive(corestore.session(), CHECKOUT.key)
const checkout = drive.checkout(CHECKOUT.length)
await checkout.ready()
checkout.on('close', () => drive.close())
return checkout
}
}
function getUpgradeTarget () {
if (LOCALDEV) return { checkout: CHECKOUT, swap: SWAP }
let key = null
for (let i = 0; i < Bare.argv.length; i++) {
const arg = Bare.argv[i]
if (arg.startsWith('--key=')) {
key = hypercoreid.normalize(arg.slice(6))
break
}
if (arg === '--key' && Bare.argv.length > i + 1) {
key = hypercoreid.normalize(Bare.argv[i + 1])
break
}
}
if (key === null || key === CHECKOUT.key) return { checkout: CHECKOUT, swap: SWAP }
return {
checkout: { key, length: 0, fork: 0 },
swap: null
}
}