Localdrive
A file system API that is similar to Hyperdrive
. This tool comes in handy when mirroring files from user filesystem to a drive, and vice-versa.
Installation
Install with npm:
Usage
API
const drive = new Localdrive(root, [options])
Creates a drive based on a root
directory. root
can be relative or absolute.
options
include:
followLinks
If enabled then entry(key)
will follow the linkname
.
Boolean
false
metadata
Hook functions are called accordingly.
Object
null
atomic
Enables atomicity for file writing (tmp file and rename).
Boolean
false
roots
For mapping key prefixes to different roots.
Object
{}
The metadata hook
del()
could be called with non-existing metadata keys.
drive.root
String with the resolved (absolute) drive path.
drive.supportsMetadata
Boolean indicating whether the drive handles metadata. Default false
.
If options.metadata
hooks are passed 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, [options])
Returns the entry at key
path in the drive. It looks like this:
Available options
:
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 anddrive.get(key)
will return null, whiledrive.entry(key)
will return the entry with symlink information.
const comparison = drive.compare(entryA, entryB)
const comparison = drive.compare(entryA, entryB)
Returns 0
if entries are the same, 1
if entryA
is older, and -1
if entryB
is older.
const iterator = drive.list([folder])
Returns a stream of all entries in the drive inside of specified folder
.
const iterator = drive.readdir([folder])
Returns a stream of all subpaths of entries in drive stored at paths prefixed by folder
.
const mirror = drive.mirror(out, [options])
Mirrors 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:
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
andend
are inclusive.
length
overridesend
, they're not meant to be used together.
const ws = drive.createWriteStream(key, [options])
Streams a blob into the drive at key
path.
options
include:
executable
whether the blob is executable or not
Boolean
true
Examples
Metadata hooks
Metadata backed by Map
:
metadata.del()
will also be called when metadata isnull
Last updated