ReferenceBuilding blocks
Hyperdrive
Secure real-time distributed filesystem built on Hypercore-based storage.
stable
Hyperdrive stores filesystem metadata in a Hyperbee and file contents in a blob store, typically backed by one Corestore. Use this page for the instance surface; use the linked how-to guides for end-to-end replication flows.
Install
npm i hyperdriveQuickstart
import Corestore from 'corestore'
import Hyperdrive from 'hyperdrive'
const store = new Corestore('./storage')
const drive = new Hyperdrive(store)
await drive.ready()
await drive.put('/hello.txt', Buffer.from('hello world'), {
metadata: { type: 'text/plain' }
})
const buffer = await drive.get('/hello.txt')
const entry = await drive.entry('/hello.txt')
console.log(buffer.toString())
console.log(entry?.value.metadata)
await drive.close()API Reference
Constructor and lifecycle
new Hyperdrive(store, [key])
- Signature:
new Hyperdrive(store, [key])(GitHub) - Parameters:
storeis theCorestoreused for metadata and blob storage;keyoptionally opens an existing drive by public key instead of using the default named core. (GitHub) - Returns: A
Hyperdriveinstance. (GitHub) - Example:
const drive = new Hyperdrive(store, key)
await drive.ready()await drive.ready()
- Signature:
await drive.ready()(GitHub) - Parameters: None. (GitHub)
- Returns: A promise that resolves when synchronous properties such as
drive.discoveryKeyare safe to read. (GitHub) - Example:
await drive.ready()
console.log(drive.discoveryKey)await drive.close()
- Signature:
await drive.close()(GitHub) - Parameters: None. (GitHub)
- Returns: A promise that resolves after the drive and its underlying storage objects are closed. (GitHub)
- Example:
await drive.close()await drive.purge()
- Signature:
await drive.purge()(GitHub) - Parameters: None. (GitHub)
- Returns: A promise that resolves after both the metadata core and blob storage are removed from local storage. (GitHub)
- Example:
await drive.purge()Drive properties
drive.corestore
- Returns: The
Corestoreinstance backing the drive. (GitHub)
drive.db
- Returns: The underlying Hyperbee that stores the drive's directory structure and entry metadata. (GitHub)
drive.core
- Returns: The Hypercore used by
drive.db. (GitHub)
drive.id
- Returns: A z-base-32 string identifier derived from the drive's public key. (GitHub)
drive.key
- Returns: The public key of the metadata Hypercore backing the drive. (GitHub)
drive.discoveryKey
- Returns: The discovery key derived from
drive.key, typically used as a Hyperswarm topic. (GitHub)
drive.contentKey
- Returns: The public key of the Hyperblobs instance that stores file contents. (GitHub)
drive.writable
- Returns:
truewhen the current instance can mutate entries in the drive. (GitHub)
drive.readable
- Returns:
truewhen the instance can still read data; becomesfalseafterclose(). (GitHub)
drive.version
- Returns: The current drive version number, incremented as filesystem mutations are committed. (GitHub)
drive.supportsMetadata
- Returns:
true. Hyperdrive always supports entry metadata. (GitHub)
Files
await drive.put(path, buffer, [options])
- Signature:
await drive.put(path, buffer, [options])(GitHub) - Parameters:
pathis the absolute drive path to write;bufferis the file content;optionsmatchesdrive.createWriteStream(...)and supportsexecutableandmetadata. (GitHub) - Returns: A promise that resolves after the file entry and blob are committed. (GitHub)
- Example:
await drive.put('/docs/readme.txt', Buffer.from('hello'))const buffer = await drive.get(path, [options])
- Signature:
const buffer = await drive.get(path, [options])(GitHub) - Parameters:
pathis the file path to read;options.waitwaits for remote blocks andoptions.timeoutbounds that wait in milliseconds. (GitHub) - Returns: A
Bufferfor the file contents, ornullif the file is missing or the path is a symlink. (GitHub) - Example:
const buffer = await drive.get('/docs/readme.txt')
console.log(buffer?.toString())const entry = await drive.entry(path, [options])
- Signature:
const entry = await drive.entry(path, [options])(GitHub) - Parameters:
pathis the path to inspect;options.followfollows symlinks, whilewaitandtimeoutcontrol remote reads. (GitHub) - Returns: The current entry object, including
seq,key, andvaluemetadata, ornullwhen no entry exists. (GitHub) - Example:
const entry = await drive.entry('/docs/readme.txt')
console.log(entry?.value.metadata)const exists = await drive.exists(path)
- Signature:
const exists = await drive.exists(path)(GitHub) - Parameters:
pathis the path to test. (GitHub) - Returns:
trueif an entry exists at that path, otherwisefalse. (GitHub) - Example:
const exists = await drive.exists('/docs/readme.txt')await drive.has(path)
- Signature:
await drive.has(path)(GitHub) - Parameters:
pathis the file path to check. (GitHub) - Returns:
truewhen the data for that path is already stored locally. (GitHub) - Example:
const hasLocalData = await drive.has('/docs/readme.txt')await drive.del(path)
- Signature:
await drive.del(path)(GitHub) - Parameters:
pathis the file path to remove. (GitHub) - Returns: A promise that resolves after the entry is deleted from the drive. (GitHub)
- Example:
await drive.del('/docs/old.txt')const cleared = await drive.clear(path, [options])
- Signature:
const cleared = await drive.clear(path, [options])(GitHub) - Parameters:
pathis the file path whose blob data should be discarded locally;options.diffincludes a byte-count diff in the return value. (GitHub) - Returns: Information about cleared bytes when
diffis enabled; otherwisenull. (GitHub) - Example:
await drive.clear('/videos/demo.mp4', { diff: true })const cleared = await drive.clearAll([options])
- Signature:
const cleared = await drive.clearAll([options])(GitHub) - Parameters:
options.diffincludes a byte-count diff in the return value. (GitHub) - Returns: Information about cleared bytes when
diffis enabled; otherwisenull. (GitHub) - Example:
await drive.clearAll({ diff: true })await drive.symlink(path, linkname)
- Signature:
await drive.symlink(path, linkname)(GitHub) - Parameters:
pathis the symlink entry to create;linknameis the destination path inside the drive. (GitHub) - Returns: A promise that resolves after the symlink entry is written. (GitHub)
- Example:
await drive.symlink('/images/logo.shortcut', '/images/logo.png')const rs = drive.createReadStream(path, [options])
- Signature:
const rs = drive.createReadStream(path, [options])(GitHub) - Parameters:
pathis the file path to stream;options.start,end, andlengthselect a byte range, whilewaitandtimeoutcontrol remote reads. (GitHub) - Returns: A readable stream for the blob at
path. (GitHub) - Example:
for await (const chunk of drive.createReadStream('/hello.txt')) {
console.log(chunk)
}const ws = drive.createWriteStream(path, [options])
- Signature:
const ws = drive.createWriteStream(path, [options])(GitHub) - Parameters:
pathis the file path to write;options.executablemarks the file executable andoptions.metadatastores arbitrary JSON metadata. (GitHub) - Returns: A writable stream that commits the file when closed. (GitHub)
- Example:
const ws = drive.createWriteStream('/notes.txt')
ws.end('hello from a stream')Directories and listings
const stream = drive.list(folder, [options])
- Signature:
const stream = drive.list(folder, [options])(GitHub) - Parameters:
folderscopes the prefix to list;options.recursivecontrols descent,options.ignoreskips names, andoptions.waitblocks for remote metadata. (GitHub) - Returns: A stream of full entry records under the requested folder prefix. (GitHub)
- Example:
for await (const entry of drive.list('/images')) {
console.log(entry.key)
}const stream = drive.readdir(folder, [options])
- Signature:
const stream = drive.readdir(folder, [options])(GitHub) - Parameters:
folderis the directory to enumerate;options.waitwaits for remote metadata before yielding names. (GitHub) - Returns: A stream of child paths under the requested folder. (GitHub)
- Example:
for await (const name of drive.readdir('/images')) {
console.log(name)
}const stream = await drive.entries([range], [options])
- Signature:
const stream = await drive.entries([range], [options])(GitHub) - Parameters:
rangelimits the key space to read;optionsfollows the same stream options as the underlying Hyperbee read stream. (GitHub) - Returns: A read stream of drive entries in key order. (GitHub)
- Example:
for await (const entry of await drive.entries()) {
console.log(entry.key)
}const comparison = drive.compare(entryA, entryB)
- Signature:
const comparison = drive.compare(entryA, entryB)(GitHub) - Parameters:
entryAandentryBare entry objects returned by Hyperdrive. (GitHub) - Returns:
0when the entries match,1whenentryAis older, and-1whenentryBis older. (GitHub) - Example:
const comparison = drive.compare(currentEntry, previousEntry)const watcher = drive.watch([folder])
- Signature:
const watcher = drive.watch([folder])(GitHub) - Parameters:
folderoptionally narrows change detection to a subtree; defaults to/. (GitHub) - Returns: An async iterator that yields
[current, previous]snapshots. The watcher also exposesawait watcher.ready()andawait watcher.destroy(). (GitHub) - Example:
const watcher = drive.watch('/docs')
await watcher.ready()
for await (const [current, previous] of watcher) {
console.log(previous.version, '->', current.version)
break
}
await watcher.destroy()Versions and batched mutations
const batch = drive.batch()
- Signature:
const batch = drive.batch()(GitHub) - Parameters: None. (GitHub)
- Returns: A batch object with the same mutation interface as
drive, allowing multiple writes to be staged atomically. (GitHub) - Example:
const batch = drive.batch()
await batch.put('/a.txt', Buffer.from('a'))
await batch.put('/b.txt', Buffer.from('b'))
await batch.flush()await batch.flush()
- Signature:
await batch.flush()(GitHub) - Parameters: None. (GitHub)
- Returns: A promise that resolves when the staged batch mutations are committed to the underlying drive. (GitHub)
- Example:
const batch = drive.batch()
await batch.del('/old.txt')
await batch.flush()const snapshot = drive.checkout(version)
- Signature:
const snapshot = drive.checkout(version)(GitHub) - Parameters:
versionis the earlier drive version to open. (GitHub) - Returns: A read-only snapshot of that version of the drive. (GitHub)
- Example:
const snapshot = drive.checkout(10)
const buffer = await snapshot.get('/hello.txt')const stream = drive.diff(version, folder, [options])
- Signature:
const stream = drive.diff(version, folder, [options])(GitHub) - Parameters:
versionis the baseline version to compare against;folderscopes the diff to one subtree;optionspasses through to the diff stream. (GitHub) - Returns: A stream of
{ left, right }objects describing shallow changes betweenversionand the current drive version. (GitHub) - Example:
for await (const change of drive.diff(10, '/docs')) {
console.log(change.left, change.right)
}await drive.truncate(version, [options])
- Signature:
await drive.truncate(version, [options])(GitHub) - Parameters:
versionis the drive version to roll back to;options.blobscan be provided when the matching blob length is already known. (GitHub) - Returns: A promise that resolves after the drive is truncated to that earlier state. (GitHub)
- Example:
await drive.truncate(10)Replication, downloads, and blob access
const mirror = drive.mirror(out, [options])
- Signature:
const mirror = drive.mirror(out, [options])(GitHub) - Parameters:
outis another drive-like destination such as Localdrive;optionsare forwarded to the underlying Mirrordrive instance. (GitHub) - Returns: A
MirrorDriveinstance. Callawait mirror.done()to wait for the copy to finish. (GitHub) - Example:
const mirror = drive.mirror(localdrive)
await mirror.done()const download = drive.download(folder, [options])
- Signature:
const download = drive.download(folder, [options])(GitHub) - Parameters:
folderscopes which subtree to fetch;optionsmatchdrive.list(...). (GitHub) - Returns: A
Downloadhandle for blob downloads. Useawait download.done()to wait for completion ordownload.destroy()to cancel. (GitHub) - Example:
const download = drive.download('/images')
await download.done()const download = await drive.downloadDiff(version, folder, [options])
- Signature:
const download = await drive.downloadDiff(version, folder, [options])(GitHub) - Parameters:
versionis the historical version to compare;folderscopes the subtree;optionsmatchdrive.list(...). (GitHub) - Returns: A
Downloadhandle for blobs that exist indrive.checkout(version)but not in the current drive state. (GitHub) - Example:
const download = await drive.downloadDiff(10, '/images')
await download.done()const download = await drive.downloadRange(dbRanges, blobRanges)
- Signature:
const download = await drive.downloadRange(dbRanges, blobRanges)(GitHub) - Parameters:
dbRangesselects metadata ranges andblobRangesselects blob ranges, using the range format accepted by Hypercore. (GitHub) - Returns: A
Downloadhandle for the requested metadata and blob ranges. (GitHub) - Example:
const download = await drive.downloadRange([{ start: 0, end: 10 }], [])
await download.done()const done = drive.findingPeers()
- Signature:
const done = drive.findingPeers()(GitHub) - Parameters: None. (GitHub)
- Returns: A callback that should be invoked when the current peer-discovery pass finishes, allowing pending updates to unblock. (GitHub)
- Example:
const done = drive.findingPeers()
swarm.flush().then(done, done)const stream = drive.replicate(isInitiatorOrStream)
- Signature:
const stream = drive.replicate(isInitiatorOrStream)(GitHub) - Parameters: Pass a replication stream or the initiator/options form accepted by the underlying Corestore replication API. (GitHub)
- Returns: The replication stream for the drive and its loaded backing data. (GitHub)
- Example:
swarm.on('connection', (socket) => drive.replicate(socket))const updated = await drive.update([options])
- Signature:
const updated = await drive.update([options])(GitHub) - Parameters:
options.waitcan force a blocking wait for a newer version. (GitHub) - Returns: A boolean indicating whether the drive observed a new version. (GitHub)
- Example:
const done = drive.findingPeers()
swarm.flush().then(done, done)
const updated = await drive.update()const blobs = await drive.getBlobs()
- Signature:
const blobs = await drive.getBlobs()(GitHub) - Parameters: None. (GitHub)
- Returns: The Hyperblobs instance storing file contents for the drive. (GitHub)
- Example:
const blobs = await drive.getBlobs()
const entry = await drive.entry('/hello.txt')
const buffer = await blobs.get(entry.value.blob)const blobsLength = await drive.getBlobsLength(checkout)
- Signature:
const blobsLength = await drive.getBlobsLength(checkout)(GitHub) - Parameters:
checkoutoptionally selects the drive version whose blob length should be resolved. (GitHub) - Returns: The Hyperblobs length associated with that version of the drive. (GitHub)
- Example:
const blobsLength = await drive.getBlobsLength()See also
- Create a full peer-to-peer filesystem with Hyperdrive — end-to-end filesystem replication walkthrough.
- Work with many Hypercores using Corestore — recommended pattern when one app manages multiple drives or related cores.
- Localdrive — map a Hyperdrive-like API onto the local filesystem.
- Mirrordrive — sync between Hyperdrive and other drive-like destinations.
- Corestore — shared storage and replication manager for drive metadata and content.
- Hyperbee — Hyperdrive uses a Hyperbee internally for metadata indexing.
- Drives — CLI tool for creating, mirroring, and seeding Hyperdrives.