MirrorDrive
Diff and sync engine for copying between drive-like APIs.
Mirrordrive computes diffs between two drive-like APIs and applies them efficiently. It is the helper that powers sync flows between Hyperdrive and Localdrive, including the drive.mirror(...) shortcut on Hyperdrive. A MirrorDrive instance is itself an async iterator, so iterating it with for await (const diff of mirror) drives the diff/copy pass and yields one diff object per entry. For source and release notes, see the mirror-drive repository.
Async iteration
for await (const diff of mirror)
MirrorDrive is itself an async iterable. Iterating it with for await drives the entire diff/copy pass; each yield produces one { op, key } object where op is 'put' or 'del'.
for await (const diff of mirror) {
console.log(diff.op, diff.key)
}Option patterns
transformers
- Returns: A copy pipeline hook. Each transformer is a function
(key) => stream | null; returningnullskips that transform for the current file. - Example:
const mirror = new MirrorDrive(src, dst, {
transformers: [
(key) => key.endsWith('.txt') ? myTransformStream() : null
]
})entries
- Returns: A fixed list of entries to mirror instead of reading them from
src.list(...). When this option is used,prefixis ignored. - Example:
const entries = [{ key: '/index.html' }, { key: '/styles.css' }]
const mirror = new MirrorDrive(src, dst, { entries })metadataEquals
- Returns: A predicate used to decide whether metadata changes should count as a diff when both drives support metadata.
- Example:
const mirror = new MirrorDrive(src, dst, {
metadataEquals(left, right) {
return JSON.stringify(left) === JSON.stringify(right)
}
})Install
npm i mirror-driveQuickstart
import Corestore from 'corestore'
import Hyperdrive from 'hyperdrive'
import Localdrive from 'localdrive'
import MirrorDrive from 'mirror-drive'
const store = new Corestore('./storage')
const src = new Localdrive('./site')
const dst = new Hyperdrive(store)
await dst.ready()
const mirror = new MirrorDrive(src, dst, { prune: true })
for await (const diff of mirror) {
console.log(diff)
}API Reference
Constructor and lifecycle
new MirrorDrive(src, dst, [options])
Creates a mirror instance to efficiently move src drive into dst drive.
| Parameter | Type | Default | Description |
|---|---|---|---|
src | Hyperdrive|Localdrive | — | Source drive to read from. |
dst | Hyperdrive|Localdrive | — | Destination drive to write into (must be writable). |
options | MirrorDriveOptions | {} | Mirror options. |
await mirror.done()
It starts processing all the diffing until is done.
- Returns:
Promise<void>— Resolves when all diffs have been processed.
const mirror = new MirrorDrive(src, dst)
await mirror.done()
console.log(mirror.count) // { files: 3, add: 2, remove: 0, change: 1 }Mirror state
mirror.count
It counts the total files proccessed, added, removed, and changed.
- Returns:
{ files: number, add: number, remove: number, change: number }
mirror.bytesAdded
The total number of bytes copied into the destination so far.
- Returns:
number
mirror.bytesRemoved
The total number of bytes removed from the destination so far.
- Returns:
number
mirror.finished
true once the mirror pass has completed.
- Returns:
boolean
mirror.preloaded
true once any configured preload phase has finished fetching the blob ranges or block maps it needs.
- Returns:
boolean
mirror.peers
The source drive's current peer list when src.core exists, otherwise an empty array.
- Returns:
Array<object>
mirror.downloadProgress
A 0..1 estimate of preload/download progress when progress tracking is enabled.
- Returns:
number
mirror.downloadedBlocks
The number of blob blocks downloaded from the source during the current run.
- Returns:
number
mirror.downloadedBytes
The number of blob bytes downloaded from the source during the current run.
- Returns:
number
mirror.uploadedBlocks
The number of destination blob blocks uploaded or written during the current run.
- Returns:
number
mirror.uploadedBytes
The number of destination blob bytes uploaded or written during the current run.
- Returns:
number
Monitoring progress
mirror.monitor(opts)
A monitor object that emits progress updates and exposes the latest stats snapshot.
| Parameter | Type | Description |
|---|---|---|
opts | MonitorOptions | Monitor options. |
- Returns:
Monitor— A new monitor instance attached to this mirror.
const monitor = mirror.monitor({ interval: 500 })
monitor.on('update', (stats) => {
console.log('downloaded', stats.download.bytes, 'bytes')
})monitor.stats
- Returns: The most recent immutable stats snapshot, shaped like:
{ peers, download: { bytes, blocks, speed, progress }, upload: { bytes, blocks, speed } }
monitor.preloaded
monitor.destroyed
- Returns:
trueafter the monitor has been torn down.
monitor.destroy()
Stops update polling and detaches the monitor from the mirror.
monitor.on('update', stats)
Emitted every interval milliseconds with the latest monitor.stats snapshot.
monitor.on('preloaded', listener)
Emitted once preload has completed.
monitor.on('destroy', listener)
Emitted when monitor.destroy() runs or when the parent mirror finishes and cleans up remaining monitors.
Types
MirrorDriveOptions
Options for controlling how MirrorDrive synchronises drives.
| Property | Type | Default | Description |
|---|---|---|---|
prefix | string|Array<string> | '/' | One or more path prefixes to mirror. When an array, each prefix is mirrored in order. |
dryRun | boolean | false | Walk the diff and emit events without writing any changes to dst. |
prune | boolean | true | Remove files from dst that no longer exist in src. |
includeEquals | boolean | false | Emit { op: 'equal' } events for files that are identical in both drives. |
filter | function(string): boolean | — | Called with each source key; return false to skip that entry. |
metadataEquals | function(*,*): boolean | — | Custom equality check for file metadata; receives (srcMetadata, dstMetadata). |
batch | boolean | false | Accumulate destination writes in a single batch and flush at the end. |
entries | Array<object>|null | null | Pre-computed list of source entries to mirror; when provided, prefix is ignored. |
ignore | string|Array<string> | — | Path(s) to exclude from the source listing. |
transformers | Array<function> | [] | Array of factory functions (key) => stream | null; each returned stream is piped between source read and destination write. |
progress | boolean | false | Enable download/upload progress tracking (requires src.getBlobs). |
dedup | boolean | false | Enable content-addressed deduplication using Rabin chunking on the destination. |
MonitorOptions
Options for the monitor() method.
| Property | Type | Default | Description |
|---|---|---|---|
interval | number | 250 | How often (in milliseconds) the monitor emits update events with fresh stats. |
See also
- Create a full peer-to-peer filesystem with Hyperdrive—end-to-end workflow that mirrors between Localdrive and Hyperdrive.
- Hyperdrive—exposes
drive.mirror(...)and is the canonical distributed destination/source for Mirrordrive. - Localdrive—the local filesystem companion API most often paired with Mirrordrive.
- Drives—CLI tool whose
drives mirrorcommand is powered by the same sync logic. - Upstream mirror-drive repository—source, releases, and implementation details.