LogoPear Docs
ReferencesHelpers

MirrorDrive

Diff and sync engine for copying between drive-like APIs.

stable

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

API definition on GitHub

  • Returns: A copy pipeline hook. Each transformer is a function (key) => stream | null; returning null skips that transform for the current file.
  • Example:
const mirror = new MirrorDrive(src, dst, {
  transformers: [
    (key) => key.endsWith('.txt') ? myTransformStream() : null
  ]
})

entries

API definition on GitHub

  • Returns: A fixed list of entries to mirror instead of reading them from src.list(...). When this option is used, prefix is ignored.
  • Example:
const entries = [{ key: '/index.html' }, { key: '/styles.css' }]
const mirror = new MirrorDrive(src, dst, { entries })

metadataEquals

API definition on GitHub

  • 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-drive

Quickstart

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])

src

Creates a mirror instance to efficiently move src drive into dst drive.

ParameterTypeDefaultDescription
srcHyperdrive|LocaldriveSource drive to read from.
dstHyperdrive|LocaldriveDestination drive to write into (must be writable).
optionsMirrorDriveOptions{}Mirror options.

await mirror.done()

src

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

src

It counts the total files proccessed, added, removed, and changed.

  • Returns: { files: number, add: number, remove: number, change: number }

mirror.bytesAdded

src

The total number of bytes copied into the destination so far.

  • Returns: number

mirror.bytesRemoved

src

The total number of bytes removed from the destination so far.

  • Returns: number

mirror.finished

src

true once the mirror pass has completed.

  • Returns: boolean

mirror.preloaded

src

true once any configured preload phase has finished fetching the blob ranges or block maps it needs.

  • Returns: boolean

mirror.peers

src

The source drive's current peer list when src.core exists, otherwise an empty array.

mirror.downloadProgress

src

A 0..1 estimate of preload/download progress when progress tracking is enabled.

  • Returns: number

mirror.downloadedBlocks

src

The number of blob blocks downloaded from the source during the current run.

  • Returns: number

mirror.downloadedBytes

src

The number of blob bytes downloaded from the source during the current run.

  • Returns: number

mirror.uploadedBlocks

src

The number of destination blob blocks uploaded or written during the current run.

  • Returns: number

mirror.uploadedBytes

src

The number of destination blob bytes uploaded or written during the current run.

  • Returns: number

Monitoring progress

mirror.monitor(opts)

src

A monitor object that emits progress updates and exposes the latest stats snapshot.

ParameterTypeDescription
optsMonitorOptionsMonitor 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

src

  • Returns: The most recent immutable stats snapshot, shaped like: { peers, download: { bytes, blocks, speed, progress }, upload: { bytes, blocks, speed } }

monitor.preloaded

src

monitor.destroyed

src

  • Returns: true after the monitor has been torn down.

monitor.destroy()

src

Stops update polling and detaches the monitor from the mirror.

monitor.on('update', stats)

src

Emitted every interval milliseconds with the latest monitor.stats snapshot.

monitor.on('preloaded', listener)

src

Emitted once preload has completed.

monitor.on('destroy', listener)

src

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.

PropertyTypeDefaultDescription
prefixstring|Array<string>'/'One or more path prefixes to mirror. When an array, each prefix is mirrored in order.
dryRunbooleanfalseWalk the diff and emit events without writing any changes to dst.
prunebooleantrueRemove files from dst that no longer exist in src.
includeEqualsbooleanfalseEmit { op: 'equal' } events for files that are identical in both drives.
filterfunction(string): booleanCalled with each source key; return false to skip that entry.
metadataEqualsfunction(*,*): booleanCustom equality check for file metadata; receives (srcMetadata, dstMetadata).
batchbooleanfalseAccumulate destination writes in a single batch and flush at the end.
entriesArray<object>|nullnullPre-computed list of source entries to mirror; when provided, prefix is ignored.
ignorestring|Array<string>Path(s) to exclude from the source listing.
transformersArray<function>[]Array of factory functions (key) => stream | null; each returned stream is piped between source read and destination write.
progressbooleanfalseEnable download/upload progress tracking (requires src.getBlobs).
dedupbooleanfalseEnable content-addressed deduplication using Rabin chunking on the destination.

MonitorOptions

Options for the monitor() method.

PropertyTypeDefaultDescription
intervalnumber250How often (in milliseconds) the monitor emits update events with fresh stats.

See also

On this page