ReferenceHelpers
Localdrive
Local filesystem adapter with a Hyperdrive-like API.
stable
Localdrive exposes a local directory through an API that closely matches Hyperdrive. It is the simplest way to mirror between on-disk files and distributed drives with Mirrordrive. For upstream implementation details, see the Localdrive repository.
Install
npm i localdriveQuickstart
import Localdrive from 'localdrive'
const drive = new Localdrive('./site')
await drive.put('/index.html', Buffer.from('<h1>Hello</h1>'))
const html = await drive.get('/index.html')
console.log(html?.toString())
console.log(await drive.entry('/index.html'))API Reference
Constructor and lifecycle
const drive = new Localdrive(root, [options])
- Signature:
const drive = new Localdrive(root, [options]) - Parameters:
rootis the local directory to expose.options.followLinksfollows symlinks during entry resolution,options.followExternalLinkspermits following symlinks that leaveroot,options.metadatacan be either a{ get, put, del }hook object or aMap, andoptions.atomicwrites through temporary files before renaming into place. - Returns: A
Localdriveinstance rooted at the resolved absolute path. - Example:
const metadata = new Map()
const drive = new Localdrive('./assets', {
atomic: true,
metadata
})await drive.ready()
- Signature:
await drive.ready() - Parameters: None.
- Returns: A promise that resolves immediately. It exists for compatibility with drive-like APIs such as Hyperdrive.
- Example:
await drive.ready()await drive.close()
- Signature:
await drive.close() - Parameters: None.
- Returns: A promise that resolves immediately. Localdrive does not keep a background storage process open.
- Example:
await drive.close()await drive.flush()
- Signature:
await drive.flush() - Parameters: None.
- Returns: A promise that resolves immediately. It exists so code written for batched drive APIs can treat Localdrive as a drop-in target.
- Example:
await drive.flush()Drive properties
drive.root
- Returns: The resolved absolute filesystem path backing the drive.
drive.supportsMetadata
- Returns:
truewhen metadata hooks were configured, otherwisefalse.
Compatibility helpers
const batch = drive.batch()
- Signature:
const batch = drive.batch() - Parameters: None.
- Returns: The same
Localdriveinstance. This lets batch-oriented code reuse a Localdrive target without branching. - Example:
const batch = drive.batch()
await batch.put('/a.txt', Buffer.from('a'))
await batch.flush()const snapshot = drive.checkout()
- Signature:
const snapshot = drive.checkout() - Parameters: None.
- Returns: The same
Localdriveinstance. Unlike Hyperdrive, Localdrive does not expose historical versions. - Example:
const snapshot = drive.checkout()
console.log(snapshot === drive)const filename = drive.toPath(key)
- Signature:
const filename = drive.toPath(key) - Parameters:
keyis a drive path such as'/images/logo.png'. - Returns: The resolved local filesystem path for that key under
drive.root. - Example:
console.log(drive.toPath('/images/logo.png'))Reading and writing entries
const entry = await drive.entry(key, [options])
- Signature:
const entry = await drive.entry(key, [options]) - Parameters:
keyis the drive path to inspect.options.followfollows symlink chains up to 16 hops before throwing a recursive-link error. - Returns: An entry object with
key,mtime, andvaluefields (executable,linkname,blob,metadata), ornullwhen the path is missing or resolves to a directory. - Example:
const entry = await drive.entry('/index.html')
console.log(entry?.value.blob?.byteLength)const buffer = await drive.get(key, [options])
- Signature:
const buffer = await drive.get(key, [options]) - Parameters:
keyis the path to read.optionsare forwarded todrive.entry(...), includingfollowfor symlink-aware lookups. - Returns: A
Buffercontaining the file contents, ornullif the path is missing or points at a symlink rather than file data. - Example:
const buffer = await drive.get('/index.html')
console.log(buffer?.toString())await drive.put(key, buffer, [options])
- Signature:
await drive.put(key, buffer, [options]) - Parameters:
keyis the destination path.bufferis the file content to write.options.executablesets executable mode bits andoptions.metadatastores metadata through the configured metadata hooks. - Returns: A promise that resolves once the write stream closes and the file is fully persisted.
- Example:
await drive.put('/bin/run.sh', Buffer.from('#!/usr/bin/env sh\necho hi\n'), {
executable: true,
metadata: { type: 'shell' }
})await drive.del(key)
- Signature:
await drive.del(key) - Parameters:
keyis the path to remove. - Returns: A promise that resolves after the file is deleted. Missing files are ignored.
- Example:
await drive.del('/old/index.html')await drive.symlink(key, linkname)
- Signature:
await drive.symlink(key, linkname) - Parameters:
keyis the symlink entry to create.linknameis the target path, either inside the drive or a relative path. - Returns: A promise that resolves after the symlink is created.
- Example:
await drive.symlink('/current', '/releases/v2')const exists = await drive.exists(key)
- Signature:
const exists = await drive.exists(key) - Parameters:
keyis the path to check. - Returns:
truewhendrive.entry(key)resolves to a non-null entry, otherwisefalse. - Example:
const exists = await drive.exists('/index.html')const comparison = drive.compare(entryA, entryB)
- Signature:
const comparison = drive.compare(entryA, entryB) - Parameters:
entryAandentryBare entry objects returned bydrive.entry(...)or yielded bydrive.list(...). - Returns:
1whenentryAis newer,-1whenentryBis newer, or0when their modification times match. - Example:
const previous = await drive.entry('/v1.txt')
const current = await drive.entry('/v2.txt')
console.log(drive.compare(previous, current))Listing and mirroring
const iterator = drive.list([folder], [options])
- Signature:
const iterator = drive.list([folder], [options]) - Parameters:
folderoptionally scopes the walk to one subtree.options.ignorecan be a function, string, or array of paths to skip. - Returns: An async iterator of full entry objects for every non-directory entry under the requested subtree.
- Example:
for await (const entry of drive.list('/images', { ignore: '/images/tmp' })) {
console.log(entry.key)
}const iterator = drive.readdir([folder])
- Signature:
const iterator = drive.readdir([folder]) - Parameters:
folderoptionally scopes the listing to one directory prefix. - Returns: An async iterator of child path names under that folder.
- Example:
for await (const name of drive.readdir('/images')) {
console.log(name)
}const mirror = drive.mirror(out, [options])
- Signature:
const mirror = drive.mirror(out, [options]) - Parameters:
outis another drive-like destination such as Hyperdrive or another Localdrive.optionsare passed to Mirrordrive. - Returns: A
MirrorDriveinstance. Callawait mirror.done()or iterate over it to watch diffs as they apply. - Example:
const mirror = drive.mirror(otherDrive, { prune: true })
await mirror.done()Stream APIs
const rs = drive.createReadStream(key, [options])
- Signature:
const rs = drive.createReadStream(key, [options]) - Parameters:
keyis the file path to stream.options.start,options.end, andoptions.lengthselect the byte range to read. - Returns: A readable stream for the target file.
- Example:
for await (const chunk of drive.createReadStream('/index.html')) {
console.log(chunk)
}const ws = drive.createWriteStream(key, [options])
- Signature:
const ws = drive.createWriteStream(key, [options]) - Parameters:
keyis the file path to write.options.executablecontrols the output mode bits andoptions.metadataupdates the metadata hooks when the stream finishes. - Returns: A writable stream that writes into the drive and, when
atomicmode is enabled, renames a temporary file into place on close. - Example:
const ws = drive.createWriteStream('/notes.txt', {
metadata: { section: 'docs' }
})
ws.end('hello from a stream')See also
- Create a full peer-to-peer filesystem with Hyperdrive — shows Localdrive mirroring in a full writer/reader flow.
- Hyperdrive — the distributed filesystem API Localdrive is designed to interoperate with.
- Mirrordrive — the sync engine that copies between Localdrive and Hyperdrive.
- Drives — CLI tool that mirrors between local directories and Hyperdrives using the same drive-like interface.
- Upstream Localdrive repository — source, releases, and implementation details.