Links

Localdrive

A view over your local filesystem that provides the same API as Hyperdrive. This tool comes in handy when you want to mirror files from your filesystem to a drive, and vice-versa.

Installation

Install with npm:
npm install localdrive

Usage

import Localdrive from 'localdrive'
const drive = new Localdrive('my-project')
await drive.put('/blob.txt', Buffer.from('example'))
await drive.put('/images/logo.png', Buffer.from('..'))
await drive.put('/images/old-logo.png', Buffer.from('..'))
const buffer = await drive.get('/blob.txt')
console.log(buffer) // => <Buffer ..> 'example'
const entry = await drive.entry('/blob.txt')
console.log(entry) // => { executable, linkname, blob, metadata }
await drive.del('/images/old-logo.png')
await drive.symlink('/images/logo.shortcut', '/images/logo.png')
for await (const file of drive.list('/images')) {
console.log('list', file) // => { key, value }
}
const rs = drive.createReadStream('/blob.txt')
for await (const chunk of rs) {
console.log('rs', chunk) // => <Buffer ..>
}
const ws = drive.createWriteStream('/blob.txt')
ws.write('new example')
ws.end()
ws.once('close', () => console.log('file saved'))

API

Minimal API that is similar to Hyperdrive
const drive = new Localdrive(root, [options])
Creates a drive based on a root directory. root can be relative or absolute.
options include:
Property
Description
Type
Default
followLinks
followLinks does entry(key) to follow the linkname.
Boolean
false
metadata
metadata hook functions are called accordingly. del() could be called with non-existing metadata keys.
Object
{ get (key) {}, put (key) {}, del (key) {} }
drive.root
String with the resolved (absolute) drive path.
drive.supportsMetadata
Boolean that indicates if the drive handles or not metadata. Default false.
If you pass opts.metadata hooks then supportsMetadata becomes true.
await drive.put(key, buffer, [options])
Creates a file at key path in the drive. options are the same as in createWriteStream.
const buffer = await drive.get(key)
Returns the blob at key path in the drive. If no blob exists, returns null.
It also returns null for symbolic links.
const entry = await drive.entry(key)
Returns the entry at key path in the drive. It looks like this:
{
key: '/blob.txt',
value: {
executable: Boolean,
linkname: null,
blob: {
blockOffset: Number,
blockLength: Number,
byteOffset: Number,
byteLength: Number
},
metadata: null
}
}
await drive.del(key)
Deletes the file at key path from the drive.
await drive.symlink(key, linkname)
Creates an entry in drive at key path that points to the entry at linkname.
If a blob entry currently exists at key path then it will be overwritten and drive.get(key) will return null, while drive.entry(key) will return the entry with symlink information.
const iterator = drive.list([folder], [options])
Returns a stream of all entries in the drive inside of specified folder.
Default options:
Property
Description
Type
Default
filter
filter based on key value
Function
(key) => true
const mirror = drive.mirror(out, [options])
Efficiently mirror this drive into another. Returns a Mirrordrive instance constructed with options.
Call await mirror.done() to wait for the mirroring to finish.
const rs = drive.createReadStream(key, [options])
Returns a stream to read out the blob stored in the drive at key path.
options include:
Property
Description
Type
Default
start
Starting offset of the desired readstream interval
Integer
null
end
Ending offset of the desired readstream interval
Integer
null
length
Length of the desired readstream interval
Integer
null
  • start and end are inclusive.
  • length overrides end, they're not meant to be used together.
const ws = drive.createWriteStream(key, [options])
Stream a blob into the drive at key path.
options include:
Property
Description
Type
Default
executable
whether the blob is executable or not
Boolean
true

Examples

Metadata hooks

Metadata backed by Map:
const meta = new Map()
const metadata = {
get: (key) => meta.has(key) ? meta.get(key) : null,
put: (key, value) => meta.set(key, value),
del: (key) => meta.delete(key)
}
const drive = new Localdrive('./my-app', { metadata })
// ...
metadata.del() will also be called when metadata is null
await drive.put('/file.txt', Buffer.from('a')) // Default metadata is null