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
Show file tree
Hide file tree
Changes from 13 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
43 changes: 30 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 @@ -99,6 +97,9 @@ async function * downloader (key, all) {
swarm.on('connection', (socket) => { runtimes.corestore.replicate(socket) })

await runtimes.ready()
if (isTTY) {
monitorDrive(runtimes)
}

swarm.join(runtimes.discoveryKey, { server: false, client: true })
const done = runtimes.corestore.findingPeers()
Expand All @@ -109,30 +110,46 @@ 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\n`)

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

for await (const { op, key, bytesAdded } of runtime) {
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) + ']')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should only add colors if its a tty

}
}

yield '\x1B[2K\x1B[200D Runtime extraction complete\x1b[K\n'
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
*/
async function monitorDrive (drive) {
const downloadSpeedometer = speedometer()
let downloadedBytes = 0
const blobs = await drive.getBlobs()
blobs.core.on('download', (_index, bytes) => {
downloadedBytes += bytes
const speed = downloadSpeedometer(bytes)
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Downloaded: ${byteSize(downloadedBytes)} - Speed: ${byteSize(speed)}/s`);
maidh91 marked this conversation as resolved.
Show resolved Hide resolved
maidh91 marked this conversation as resolved.
Show resolved Hide resolved
})
}
20 changes: 20 additions & 0 deletions sidecar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const {
} = require('./constants')
const registerUrlHandler = require('./url-handler')
const gunk = require('./gunk')
const speedometer = require('speedometer')
const isTTY = isBare ? false : process.stdout.isTTY // TODO: support Bare
const { flags = {} } = require('./shell')(Bare.argv.slice(1))
crasher('sidecar', SWAP)
global.LOG = new Logger({
Expand Down Expand Up @@ -66,6 +68,8 @@ async function bootSidecar () {
? drive
: new Hyperdrive(corestore.session(), checkout.key)

monitorDrive(updateDrive)

return new Sidecar.Updater(updateDrive, { directory: PLATFORM_DIR, swap, lock: UPGRADE_LOCK, checkout })
}

Expand Down Expand Up @@ -106,3 +110,19 @@ function getUpgradeTarget () {
swap: null
}
}

/**
* @param {Hyperdrive} drive
*/
async function monitorDrive (drive) {
const downloadSpeedometer = speedometer()
let downloadedBytes = 0
const blobs = await drive.getBlobs()
blobs.core.on('download', (_index, bytes) => {
downloadedBytes += bytes
const speed = downloadSpeedometer(bytes)
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Downloaded: ${byteSize(downloadedBytes)} - Speed: ${byteSize(speed)}/s`);
})
}
Loading