Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'hyperdrive' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
if (!key) {
const keyPair = crypto.keyPair()
key = keyPair.publicKey
finalOpts.secretKey = keyPair.secretKey
}
const stringkey = DatEncoding.encode(key)
const db = (file) => {
const db = finalOpts.db || ram
return db(stringkey + '/' + file)
}
// console.log('Resolved key', key.toString('hex'))
const archive = new Hyperdrive(db, key, finalOpts)
this.archives.push(archive)
return new Promise((resolve, reject) => {
console.log('Waiting for ready')
archive.ready(() => {
console.log('Ready')
// archive.metadata.update((err) => {
// if (err) reject(err)
// else resolve(archive)
resolve(archive)
this.emit('repo', archive)
// })
this.swarm.join(archive.discoveryKey, {
announce: true
export function setup () {
drive = hyperdrive(archivesDb.getLevelInstance())
// wire up event handlers
archivesDb.on('update:archive-user-settings', (key, settings) => {
archivesEvents.emit('update-user-settings', { key, isSaved: settings.isSaved })
configureArchive(key, settings)
})
// load and configure all saved archives
archivesDb.queryArchiveUserSettings({ isSaved: true }).then(
archives => archives.forEach(a => configureArchive(a.key, a)),
err => console.error('Failed to load networked archives', err)
)
}
async function main (argv) {
// Run a server if the `--serve` option is given
if (argv.serve) return serve(argv.serve)
if (!argv._.length) {
console.error('Usage: drop-dat files...')
return process.exit(2)
}
// Create a new in-memory hyperdrive
let archive = hyperdrive(name => ram())
// Wait for it to be ready
await E(archive, 'ready')
console.error('Importing file(s):')
let cwd = process.cwd()
await importList('.', argv._)
async function importList (base, names) {
for (let name of names) {
let path = join(base, name)
let fullPath = resolve(cwd, path)
let meta = await F(stat, fullPath)
if (meta.isDirectory()) {
let children = await F(readdir, fullPath)
await importList(path, children.filter(name => name[0] !== '.'))
try {
userSettings = await archivesDb.getUserSettings(0, key)
} catch (e) {
userSettings = {networked: true}
}
}
if (!('networked' in userSettings)) {
userSettings.networked = true
}
// ensure the folders exist
var metaPath = archivesDb.getArchiveMetaPath(key)
mkdirp.sync(metaPath)
// create the archive instance
var archive = hyperdrive(metaPath, key, {
sparse: true,
secretKey,
metadataStorageCacheSize: 0,
contentStorageCacheSize: 0,
treeCacheSize: 2048
})
archive.on('error', err => {
console.error('Error in archive', key.toString('hex'), err)
debug('Error in archive', key.toString('hex'), err)
})
archive.metadata.on('peer-add', () => onNetworkChanged(archive))
archive.metadata.on('peer-remove', () => onNetworkChanged(archive))
archive.replicationStreams = [] // list of all active replication streams
archive.peerHistory = [] // samples of the peer count
// wait for ready
export function setup () {
// open databases
dbPath = path.join(app.getPath('userData'), 'Hyperdrive')
mkdirp.sync(path.join(dbPath, 'Archives')) // make sure the folders exist
db = level(dbPath)
archiveMetaDb = subleveldown(db, 'archive-meta', { valueEncoding: 'json' })
subscribedArchivesDb = subleveldown(db, 'subscribed-archives', { valueEncoding: 'json' })
ownedArchivesDb = subleveldown(db, 'owned-archives', { valueEncoding: 'json' })
drive = hyperdrive(db)
// watch archives for FS changes
var watcher = chokidar.watch(path.join(dbPath, 'Archives'), { persistent: true, cwd: path.join(dbPath, 'Archives') })
watcher.on('ready', () => { // wait till ready, otherwise we get an 'add' for each existing file
watcher.on('add', onArchiveFSChange.bind(null, 'add', 'file'))
watcher.on('addDir', onArchiveFSChange.bind(null, 'add', 'directory'))
watcher.on('change', onArchiveFSChange.bind(null, 'change', 'file'))
// watcher.on('unlink', onArchiveFSChange.bind(null, 'unlink', 'file')) TODO: dat doesnt support deletes yet
// watcher.on('unlinkDir', onArchiveFSChange.bind(null, 'unlink', 'directory')) TODO: dat doesnt support deletes yet
})
app.once('will-quit', () => watcher.close())
// create webrtc
wrtc = electronWebrtc()
wrtc.on('error', err => log('[WRTC]', err))
metadata.get(i, {valueEncoding: messages.Node}, function (err, node) {
if (err) return cb(err)
var st = node.value && stat.decode(node.value)
if (!node.value || (!st.offset && !st.blocks) || (!st.byteOffset && !st.blocks)) {
return get(metadata, btm, i - 1, cb) // TODO: check the index instead for fast lookup
}
cb(null, i, node, st)
})
}
async function handleClient (socket) {
let key
while (true) {
key = socket.read(32)
if (key) break
await E(socket, 'readable')
}
let hex = key.toString('hex')
let archive = hyperdrive(name => ram(), key, { sparse: true })
await E(archive, 'ready')
console.log(`Added site dat://${hex}`)
sites[hex] = hyperdriveHttp(archive, {
exposeHeaders: true,
live: true,
footer: `Served by Drop-Dat`
})
try {
await F(pump, socket, archive.replicate(), socket)
} catch (err) {
if (!err.message.match(/premature close/)) throw err
} finally {
console.log('Removed site', hex)
delete sites[hex]
metadata.get(i, {valueEncoding: messages.Node}, function(err, node) {
if (err) {
return cb(err)
}
const st = node.value && Stat.decode(node.value)
if (!node.value || (!st.offset && !st.blocks) || (!st.byteOffset && !st.blocks)) {
return get(metadata, btm, i - 1, cb)
}
cb(null, i, node, st)
})
}
function get () {
debug('getting index message')
core.get(0, {valueEncoding: messages.Index}, async (err, index) => {
if (err) return cb()
if (index && index.type === 'hyperdrive') {
var contentKey = index.content.toString('hex')
debug('contentKey', contentKey)
return cb(contentKey)
}
debug('calling back')
return cb()
})
}
}
var raf = require('random-access-file')
var secretStorage = require('dat-secret-storage')
var multi = require('multi-random-access')
var messages = require('append-tree/messages')
var stat = require('hyperdrive/lib/messages').Stat
var path = require('path')
module.exports = function (dir, opts) {
if (!opts) opts = {}
var prefix = opts.prefix || '.dat/'
return {
metadata: function (name, metaOpts) {
if (typeof dir === 'function') return dir(prefix + 'metadata.' + name)
if (name === 'secret_key') return secretStorage(opts.secretDir)(path.join(dir, prefix + 'metadata.ogd'), {key: metaOpts.key, discoveryKey: metaOpts.discoveryKey})
return raf(path.join(dir, prefix + 'metadata.' + name))
},
content: function (name, contentOpts, archive) {
if (!archive) archive = contentOpts
if (name === 'data') return createStorage(archive, dir)
if (typeof dir === 'function') return dir(prefix + 'content.' + name)
return raf(path.join(dir, prefix + 'content.' + name))