Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show downloaded bytes + speed on hyperdrive mirror #468

Merged
merged 29 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 74 additions & 13 deletions scripts/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const byteSize = require('tiny-byte-size')
const { decode } = require('hypercore-id-encoding')
const safetyCatch = require('safety-catch')
const Rache = require('rache')
const speedometer = require('speedometer')
const isTTY = isBare ? false : process.stdout.isTTY // TODO: support Bare

const argv = global.Pear?.config.args || global.Bare?.argv || global.process.argv

Expand Down Expand Up @@ -78,12 +80,8 @@ function advise () {
}

async function download (key, all = false) {
for await (const output of downloader(key, all)) console.log(output)
}

async function * downloader (key, all) {
if (all) yield '🍐 Fetching all runtimes from: \n ' + key
else yield '🍐 [ localdev ] - no local runtime: fetching runtime'
if (all) console.log('🍐 Fetching all runtimes from: \n ' + key)
else console.log('🍐 [ localdev ] - no local runtime: fetching runtime')

const store = CORESTORE || path.join(PEAR, 'corestores', 'platform')

Expand All @@ -109,30 +107,93 @@ async function * downloader (key, all) {
runtimes = runtimes.checkout(runtimes.version)
goodbye(() => runtimes.close())

yield `\n Extracting platform runtime${all ? 's' : ''} to disk\n`
console.log(`\n Extracting platform runtime${all ? 's' : ''} to disk`)

const runtime = runtimes.mirror(new Localdrive(SWAP), {
prefix: '/by-arch' + (all ? '' : '/' + ADDON_HOST)
})

const monitor = monitorDrive(runtimes)
goodbye(() => monitor.stop())

for await (const { op, key, bytesAdded } of runtime) {
if (isTTY) monitor.clear()
if (op === 'add') {
yield '\x1B[32m+\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']'
console.log('\x1B[32m+\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']')
} else if (op === 'change') {
yield '\x1B[33m~\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']'
console.log('\x1B[33m~\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']')
} else if (op === 'remove') {
yield '\x1B[31m-\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']'
console.log('\x1B[31m-\x1B[39m ' + key + ' [' + byteSize(bytesAdded) + ']')
maidh91 marked this conversation as resolved.
Show resolved Hide resolved
}
}

yield '\x1B[2K\x1B[200D Runtime extraction complete\x1b[K\n'
monitor.stop()

console.log('\x1B[2K\x1B[200D Runtime extraction complete\x1b[K\n')

await runtimes.close()
await swarm.destroy()
await corestore.close()

const tick = isWindows ? '^' : '✔'

if (all) yield '\x1B[32m' + tick + '\x1B[39m Download complete\n'
else yield '\x1B[32m' + tick + '\x1B[39m Download complete, initalizing...\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n'
if (all) console.log('\x1B[32m' + tick + '\x1B[39m Download complete\n')
else console.log('\x1B[32m' + tick + '\x1B[39m Download complete, initalizing...\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n')
}

/**
* @param {Hyperdrive} drive
*/
function monitorDrive (drive) {
if (!isTTY) {
return {
clear: () => null,
stop: () => null
}
}

const downloadSpeedometer = speedometer()
const uploadSpeedometer = speedometer()
let peers = 0
let downloadedBytes = 0
let uploadedBytes = 0

drive.getBlobs().then(blobs => {
blobs.core.on('download', (_index, bytes) => {
downloadedBytes += bytes
downloadSpeedometer(bytes)
})
blobs.core.on('upload', (_index, bytes) => {
uploadedBytes += bytes
uploadSpeedometer(bytes)
})
blobs.core.on('peer-add', () => {
peers = blobs.core.peers.length
})
blobs.core.on('peer-remove', () => {
peers = blobs.core.peers.length
})
}).catch(() => {
// ignore
})

const clear = () => {
process.stdout.clearLine()
process.stdout.cursorTo(0)
}

const interval = setInterval(() => {
clear()
process.stdout.write(`[⬇ ${byteSize(downloadedBytes)} - ${byteSize(downloadSpeedometer())}/s - ${peers} peers] [⬆ ${byteSize(uploadedBytes)} - ${byteSize(uploadSpeedometer())}/s - ${peers} peers]`)
}, 500)

const stop = () => {
clearInterval(interval)
clear()
}

return {
clear,
stop
}
}
Loading