11.8.1 • Published 23 days ago

hyperdrive v11.8.1

Weekly downloads
752
License
Apache-2.0
Repository
github
Last release
23 days ago

Hyperdrive

See API docs at docs.holepunch.to

Hyperdrive is a secure, real-time distributed file system

Install

npm install hyperdrive

Usage

const Hyperdrive = require('hyperdrive')
const Corestore = require('corestore')

const store = new Corestore('./storage')
const drive = new Hyperdrive(store)

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) // => { seq, key, value: { 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

const drive = new Hyperdrive(store, [key])

Creates a new Hyperdrive instance. store must be an instance of Corestore.

By default it uses the core at { name: 'db' } from store, unless you set the public key.

await drive.ready()

Waits until internal state is loaded.

Use it once before reading synchronous properties like drive.discoveryKey, unless you called any of the other APIs.

await drive.close()

Fully close this drive, including its underlying Hypercore backed datastructures.

drive.corestore

The Corestore instance used as storage.

drive.db

The underlying Hyperbee backing the drive file structure.

drive.core

The Hypercore used for drive.db.

drive.id

String containing the id (z-base-32 of the public key) identifying this drive.

drive.key

The public key of the Hypercore backing the drive.

drive.discoveryKey

The hash of the public key of the Hypercore backing the drive.

Can be used as a topic to seed the drive using Hyperswarm.

drive.contentKey

The public key of the Hyperblobs instance holding blobs associated with entries in the drive.

drive.writable

Boolean indicating if we can write or delete data in this drive.

drive.readable

Boolean indicating if we can read from this drive. After closing the drive this will be false.

drive.version

Number that indicates how many modifications were made, useful as a version identifier.

drive.supportsMetadata

Boolean indicating if the drive handles or not metadata. Always true.

await drive.put(path, buffer, [options])

Creates a file at path in the drive. options are the same as in createWriteStream.

const buffer = await drive.get(path, [options])

Returns the blob at path in the drive. If no blob exists, returns null.

It also returns null for symbolic links.

options include:

{
  wait: true, // Wait for block to be downloaded
  timeout: 0 // Wait at max some milliseconds (0 means no timeout)
}

const entry = await drive.entry(path, [options])

Returns the entry at path in the drive. It looks like this:

{
  seq: Number,
  key: String,
  value: {
    executable: Boolean, // Whether the blob at path is an executable
    linkname: null, // If entry not symlink, otherwise a string to the entry this links to
    blob: { // Hyperblobs id that can be used to fetch the blob associated with this entry
      blockOffset: Number,
      blockLength: Number,
      byteOffset: Number,
      byteLength: Number
    },
    metadata: null
  }
}

options include:

{
  follow: false, // Follow symlinks, 16 max or throws an error
  wait: true, // Wait for block to be downloaded
  timeout: 0 // Wait at max some milliseconds (0 means no timeout)
}

const exists = await drive.exists(path)

Returns true if the entry at path does exists, otherwise false.

await drive.del(path)

Deletes the file at path from the drive.

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 cleared = await drive.clear(path, [options])

Deletes the blob from storage to free up space, but the file structure reference is kept.

options include:

{
  diff: false // Returned `cleared` bytes object is null unless you enable this
}

const cleared = await drive.clearAll([options])

Deletes all the blobs from storage to free up space, similar to how drive.clear() works.

options include:

{
  diff: false // Returned `cleared` bytes object is null unless you enable this
}

await drive.purge()

Purge both cores (db and blobs) from your storage, completely removing all the drive's data.

await drive.symlink(path, linkname)

Creates an entry in drive at path that points to the entry at linkname.

If a blob entry currently exists at path then it will get overwritten and drive.get(key) will return null, while drive.entry(key) will return the entry with symlink information.

const batch = drive.batch()

Useful for atomically mutate the drive, has the same interface as Hyperdrive.

await batch.flush()

Commit a batch of mutations to the underlying drive.

const stream = drive.list(folder, [options])

Returns a stream of all entries in the drive at paths prefixed with folder.

options include:

{
  recursive: true | false // Whether to descend into all subfolders or not
}

const stream = drive.readdir(folder)

Returns a stream of all subpaths of entries in drive stored at paths prefixed by folder.

const stream = await drive.entries([range], [options])

Returns a read stream of entries in the drive.

options are the same as Hyperbee().createReadStream([range], [options]).

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 watcher = drive.watch([folder])

Returns an iterator that listens on folder to yield changes, by default on /.

Usage example:

for await (const [current, previous] of watcher) {
  console.log(current.version)
  console.log(previous.version)
}

Those current and previous are snapshots that are auto-closed before next value.

Don't close those snapshots yourself because they're used internally, let them be auto-closed.

await watcher.ready()

Waits until the watcher is loaded and detecting changes.

await watcher.destroy()

Stops the watcher. You could also stop it by using break in the loop.

const rs = drive.createReadStream(path, [options])

Returns a stream to read out the blob stored in the drive at path.

options include:

{
  start: Number, // `start` and `end` are inclusive
  end: Number,
  length: Number, // `length` overrides `end`, they're not meant to be used together
  wait: true, // Wait for blocks to be downloaded
  timeout: 0 // Wait at max some milliseconds (0 means no timeout)
}

const ws = drive.createWriteStream(path, [options])

Stream a blob into the drive at path.

options include:

{
  executable: Boolean,
  metadata: null // Extended file information i.e. arbitrary JSON value
}

await drive.download(folder, [options])

Downloads the blobs corresponding to all entries in the drive at paths prefixed with folder.

options are the same as those for drive.list(folder, [options]).

const snapshot = drive.checkout(version)

Get a read-only snapshot of a previous version.

const stream = drive.diff(version, folder, [options])

Efficiently create a stream of the shallow changes to folder between version and drive.version.

Each entry is sorted by key and looks like this:

{
  left: Object, // Entry in folder at drive.version for some path
  right: Object, // Entry in folder at drive.checkout(version) for some path
}

If an entry exists in drive.version of the folder but not in version, then left is set and right will be null, and vice versa.

await drive.downloadDiff(version, folder, [options])

Downloads all the blobs in folder corresponding to entries in drive.checkout(version) that are not in drive.version.

In other words, downloads all the blobs added to folder up to version of the drive.

await drive.downloadRange(dbRanges, blobRanges)

Downloads the entries and blobs stored in the ranges dbRanges and blobRanges.

const done = drive.findingPeers()

Indicate to Hyperdrive that you're finding peers in the background, requests will be on hold until this is done.

Call done() when your current discovery iteration is done, i.e. after swarm.flush() finishes.

const stream = drive.replicate(isInitiatorOrStream)

Usage example:

const swarm = new Hyperswarm()
const done = drive.findingPeers()
swarm.on('connection', (socket) => drive.replicate(socket))
swarm.join(drive.discoveryKey)
swarm.flush().then(done, done)

See more about how replicate works at corestore.replicate.

const updated = await drive.update([options])

Waits for initial proof of the new drive version until all findingPeers are done.

options include:

{
  wait: false
}

Use drive.findingPeers() or { wait: true } to make await drive.update() blocking.

const blobs = await drive.getBlobs()

Returns the Hyperblobs instance storing the blobs indexed by drive entries.

await drive.put('/file.txt', Buffer.from('hi'))

const buffer1 = await drive.get('/file.txt')

const blobs = await drive.getBlobs()
const entry = await drive.entry('/file.txt')
const buffer2 = await blobs.get(entry.value.blob)

// => buffer1 and buffer2 are equals

License

Apache-2.0

@pearjs/dev@lightwork/corehyperdrive-inception@everything-registry/sub-chunk-1884kaba-indexholepunch-hophyperbeathyperbrowsehyper-content-dbhyperservehypersharehyper-sdkhypervaulthypertronhypertorrenthypercore-fetchhypercopyhypercore-signing-requesthypercloudhypersource-clienthyperspace-hophyperspace-mirroring-servicehypersparkhypersynchyperdrive-archiverhyperdrive-clihyperdrive-daemonhyperdrive-daemon-clienthyperdrive-helpershyperdrive-http-serverhyperdrive-importhyperfeedhyperdrive-nested-archivehyperdrive-rss-importerhyperdrive-swarmhyperidentityhyperhealthhypermirrorhypermountkappa-drivelayerdrivemixmap-peermapsnative-messaging-swarmnest-hyperpunchlog-fetchdrivesdrop-datfritter-crawlergit-pitgitpearp2p-resourcespeer-npmpear-updater-bootstrappeermapspeermaps-ingestpipette-hugo-worker@hyperspace/cli@hyperspace/hyperdrive@hyperdrives/classtab@hyperdrives/webassembly.org@infinitebrahmanuniverse/nolb-hyperd@hypermachines/runtime-isolatestrac-tap-readertree-tagged-hyperdrive@kabacorp/dat-legacy-toolsweb-to-datzip-to-hyperdrivetrimet-live-archive@geut/hyperdrive-promisesimple-seedersimple-seeder1single-page-walkthroughsoundcloud-to-datreplaydbsharef@mapeo/core@pepa65/datcp@pqmcgill/dat-js@pqmcgill/dat-js-hyperdb@pqmcgill/dat-node@pqmcgill/dat-storage@trac-network/tap-reader@rubenrodriguez/mixmap-peermaps@sammacbeth/dat2-api@sammacbeth/discovery-swarm-test@sammacbeth/dat-api-v1@sammacbeth/dat-api-v1wrtc@sammacbeth/dat-archive-web@synonymdev/slashtags-cli@synonymdev/slashtags-core-data@synonymdev/slashtags-sdk@zalastax/nolb-hyperdarchiver-serverbare-devbeaker-indexbeaker-plugin-dat@arso-project/sonar-core@arso-project/sonar-datbdatartefact-store
11.8.0

23 days ago

11.8.1

23 days ago

11.7.1

1 month ago

11.7.0

1 month ago

11.6.3

4 months ago

11.6.2

5 months ago

11.6.0

6 months ago

11.6.1

6 months ago

11.0.0-alpha.19

10 months ago

11.0.0-alpha.18

10 months ago

11.0.0-alpha.17

10 months ago

11.5.1

8 months ago

11.5.2

8 months ago

11.5.0

8 months ago

11.5.3

8 months ago

11.0.2

10 months ago

11.0.0

10 months ago

11.0.1

10 months ago

11.2.0

10 months ago

11.1.3

10 months ago

11.1.1

10 months ago

11.1.2

10 months ago

11.1.0

10 months ago

11.4.0

9 months ago

11.3.0

9 months ago

11.0.0-alpha.15

11 months ago

11.0.0-alpha.14

11 months ago

11.0.0-alpha.16

11 months ago

11.0.0-alpha.11

12 months ago

11.0.0-alpha.13

11 months ago

11.0.0-alpha.12

11 months ago

11.0.0-alpha.9

1 year ago

11.0.0-alpha.10

1 year ago

11.0.0-alpha.7

1 year ago

11.0.0-alpha.8

1 year ago

11.0.0-alpha.5

2 years ago

11.0.0-alpha.6

2 years ago

11.0.0-alpha.3

2 years ago

11.0.0-alpha.4

2 years ago

11.0.0-alpha.2

2 years ago

11.0.0-alpha.1

2 years ago

10.21.0

3 years ago

10.20.0

3 years ago

10.19.1

3 years ago

10.19.0

3 years ago

10.18.0

4 years ago

10.17.2

4 years ago

10.17.1

4 years ago

10.17.0

4 years ago

10.16.1

4 years ago

10.16.2

4 years ago

10.16.0

4 years ago

10.15.1

4 years ago

10.15.0

4 years ago

10.14.0

4 years ago

10.14.1

4 years ago

10.13.0

4 years ago

10.12.3

4 years ago

10.12.2

4 years ago

10.12.1

4 years ago

10.11.4

4 years ago

10.12.0

4 years ago

10.11.3

4 years ago

10.11.2

4 years ago

10.11.1

4 years ago

10.11.0

4 years ago

10.10.4

4 years ago

10.10.2

4 years ago

10.10.3

4 years ago

10.10.1

4 years ago

10.10.0

4 years ago

10.9.1

4 years ago

10.9.0

4 years ago

10.8.20

4 years ago

10.8.19

4 years ago

10.8.18

4 years ago

10.8.17

4 years ago

10.8.16

4 years ago

10.8.15

4 years ago

10.8.14

4 years ago

10.8.13

4 years ago

10.8.12

4 years ago

10.8.11

4 years ago

10.8.10

4 years ago

10.8.9

4 years ago

10.8.8

4 years ago

10.8.7

4 years ago

10.8.6

4 years ago

10.8.5

4 years ago

10.8.4

4 years ago

10.8.3

4 years ago

10.8.2

4 years ago

10.8.1

4 years ago

10.8.0

4 years ago

10.7.4

4 years ago

10.7.3

4 years ago

10.7.2

4 years ago

10.7.1

4 years ago

10.7.0

4 years ago

10.6.1

4 years ago

10.6.0

4 years ago

10.5.3

4 years ago

10.5.4

4 years ago

10.5.2

4 years ago

10.5.1

4 years ago

10.4.0

4 years ago

10.5.0

4 years ago

10.4.4

4 years ago

10.4.3

4 years ago

10.4.2

4 years ago

10.3.2

5 years ago

10.3.1

5 years ago

10.3.0

5 years ago

10.2.0

5 years ago

10.1.0

5 years ago

10.0.2

5 years ago

10.0.1

5 years ago

10.0.0

5 years ago

9.16.0

5 years ago

10.0.0-rc10

5 years ago

10.0.0-rc9

5 years ago

10.0.0-rc8

5 years ago

9.15.0

5 years ago

10.0.0-rc7

5 years ago

10.0.0-rc6

5 years ago

10.0.0-rc5

5 years ago

10.0.0-rc3

5 years ago

10.0.0-rc2

5 years ago

10.0.0-rc1

5 years ago

10.0.0-rc0

5 years ago

9.14.5

5 years ago

9.14.4

5 years ago

9.14.3

5 years ago

9.14.2

5 years ago

9.14.1

5 years ago

9.14.0

6 years ago

9.13.0

6 years ago

9.12.3

6 years ago

9.12.2

6 years ago

9.12.1

6 years ago

9.12.0

6 years ago

9.11.1

6 years ago

9.11.0

6 years ago

9.10.1

6 years ago

9.10.0

6 years ago

9.9.2

6 years ago

9.9.1

6 years ago

9.9.0

6 years ago

9.8.2

6 years ago

9.8.1

6 years ago

9.8.0

6 years ago

9.7.0

7 years ago

9.6.0

7 years ago

9.5.2

7 years ago

9.5.1

7 years ago

9.5.0

7 years ago

9.4.8

7 years ago

9.4.7

7 years ago

9.4.6

7 years ago

9.4.5

7 years ago

9.4.4

7 years ago

9.4.3

7 years ago

9.4.1

7 years ago

9.4.0

7 years ago

9.3.0

7 years ago

9.2.9

7 years ago

9.2.8

7 years ago

9.2.7

7 years ago

9.2.6

7 years ago

9.2.5

7 years ago

9.2.4

7 years ago

9.2.3

7 years ago

9.2.1

7 years ago

9.2.0

7 years ago

9.1.1

7 years ago

9.1.0

7 years ago

9.0.0

7 years ago

8.3.2

7 years ago

8.3.1

7 years ago

8.3.0

7 years ago

8.2.1

7 years ago

8.2.0

7 years ago

8.1.0

7 years ago

8.0.1

7 years ago

8.0.0

7 years ago

7.14.5

7 years ago

7.14.4

7 years ago

7.14.3

7 years ago

7.14.2

7 years ago

7.14.1

7 years ago

7.14.0

7 years ago

7.13.2

7 years ago

7.13.1

7 years ago

7.13.0

7 years ago

7.12.2

7 years ago

7.12.1

7 years ago

7.12.0

7 years ago

7.11.0

7 years ago

7.10.0

7 years ago

7.9.0

7 years ago

7.8.0

7 years ago

7.7.1

7 years ago

7.7.0

7 years ago

7.6.0

8 years ago

7.5.0

8 years ago

7.4.0

8 years ago

7.3.0

8 years ago

7.2.0

8 years ago

7.1.1

8 years ago

7.1.0

8 years ago

7.0.0

8 years ago

6.6.2

8 years ago

6.6.1

8 years ago

6.6.0

8 years ago

6.5.1

8 years ago

6.5.0

8 years ago

6.4.0

8 years ago

6.3.0

8 years ago

6.2.3

8 years ago

6.2.2

8 years ago

6.2.1

8 years ago

6.2.0

8 years ago

6.1.0

8 years ago

6.0.1

8 years ago

6.0.0

8 years ago

5.2.0

8 years ago

5.1.5

8 years ago

5.1.4

8 years ago

5.1.3

8 years ago

5.1.2

8 years ago

5.1.1

8 years ago

5.1.0

8 years ago

5.0.0

8 years ago

4.1.0

8 years ago

4.0.0

8 years ago

3.5.1

8 years ago

3.5.0

8 years ago

3.4.1

8 years ago

3.4.0

8 years ago

3.3.1

8 years ago

3.3.0

8 years ago

3.2.1

8 years ago

3.2.0

8 years ago

3.1.0

8 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.2.2

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.0-alpha

11 years ago