• Stars
    star
    1,794
  • Rank 25,900 (Top 0.6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hyperdrive is a secure, real time distributed file system

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 = db.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

More Repositories

1

hypercore

Hypercore is a secure, distributed append-only log.
JavaScript
2,403
star
2

hyperswarm

A distributed networking stack for connecting peers.
JavaScript
926
star
3

sodium-native

Low level bindings for libsodium
JavaScript
302
star
4

hyperdht

The DHT powering Hyperswarm
JavaScript
251
star
5

hyperbee

An append-only B-tree running on a Hypercore
JavaScript
224
star
6

autobase

Autobase lets you write concise multiwriter data structures with Hypercore
JavaScript
84
star
7

corestore

A simple corestore that wraps a random-access-storage module
JavaScript
60
star
8

libudx

udx is reliable, multiplexed, and congestion-controlled streams over udp
C
40
star
9

hyperdrive-next

Hyperdrive is a secure, real-time distributed file system
JavaScript
37
star
10

hypercore-crypto

The crypto primitives used in hypercore, extracted into a separate module
JavaScript
36
star
11

hyperswarm-dht-relay

Relaying the Hyperswarm DHT over other transport protocols to bring decentralized networking to everyone
JavaScript
36
star
12

b4a

Bridging the gap between buffers and typed arrays
JavaScript
33
star
13

hyperblobs

A blob store for Hypercore
JavaScript
31
star
14

hypershell

Spawn shells anywhere. Fully peer-to-peer, authenticated, and end to end encrypted
JavaScript
31
star
15

hyperswarm-secret-stream

Secret stream backed by Noise and libsodium's secretstream
JavaScript
27
star
16

fs-native-extensions

Native file system extensions for advanced file operations
C
26
star
17

tiny-fs-native

Native fs for Javascript
JavaScript
24
star
18

keypear

πŸ”‘πŸ Keychain that derives deterministic Ed25519 keypairs and attestations
JavaScript
23
star
19

pear-expo-hello-world

C++
17
star
20

hyperswarm-seeders

A seeders only swarm
JavaScript
17
star
21

simple-seeder

Dead simple seeder with zero bugs
JavaScript
15
star
22

examples

Examples of basic flows for modules in the Holepunch ecosystem
JavaScript
15
star
23

noise-handshake

Simple noise handshake, supporting generic handshake patterns
JavaScript
13
star
24

localdrive

Hyperdrive but it is files
JavaScript
13
star
25

mirror-drive

Mirror two drives
JavaScript
12
star
26

tiny-http-native

Tiny HTTP library made purely on libuv and napi
JavaScript
12
star
27

libquickbit

The fastest bit in the West; a library for working with bit fields, accelerated using SIMD on supported hardware
C
11
star
28

drives

CLI to download, seed, and mirror a Hyperdrive or Localdrive
JavaScript
11
star
29

tiny-timers-native

Native timers for Javascript
JavaScript
9
star
30

udx-native

udx is reliable, multiplexed, and congestion-controlled streams over udp
JavaScript
9
star
31

libpearsync

Simple message passing between a libuv thread and something else
C
9
star
32

hyperswarm-doctor

Debugging tool for the swarm
JavaScript
7
star
33

netpaste

Copy and paste over the DHT
JavaScript
7
star
34

quickbit-universal

Universal wrapper for https://github.com/holepunchto/libquickbit with a JavaScript fallback
JavaScript
6
star
35

libcrc

Cross-platform implementation of CRC32 with hardware acceleration
C
6
star
36

hyperswarm-testnet

Small module to help you spin up a local Hyperswarm testnet
JavaScript
6
star
37

wasm-tools

A collection of useful tools for working with WASM/WAT in JavaScript
JavaScript
5
star
38

planb-summer-school

the workshop stuff
JavaScript
5
star
39

tt-native

https://github.com/holepunchto/libtt JavaScript bindings for Node.js
JavaScript
5
star
40

hypercore-id-encoding

Convert Hypercore keys to/from z-base32 or hex
JavaScript
4
star
41

serve-drive

HTTP drive server for entries delivery. Auto detects types like video, images, etc
JavaScript
4
star
42

autobase-test-helpers

Helpers when writing tests for an Autobased application
JavaScript
4
star
43

pear-radio-backend

Pear radio backend
JavaScript
4
star
44

nanodebug

A tiny, zero overhead debugging utility
JavaScript
4
star
45

crc-universal

Universal wrapper for https://github.com/holepunchto/libcrc with a JavaScript fallback
JavaScript
3
star
46

tiny-buffer-map

A very simple Map for Buffers and Uint8Arrays
JavaScript
3
star
47

quickbit-native

https://github.com/holepunchto/libquickbit JavaScript bindings for Node.js
JavaScript
3
star
48

tiny-paths

path for platforms without path
JavaScript
3
star
49

same-object

Determine if two objects are deeply equal
JavaScript
3
star
50

bare-expo

Example of embedding Bare in an Expo application using https://github.com/holepunchto/react-native-bare-kit
TypeScript
3
star
51

simdle-universal

Universal wrapper for https://github.com/holepunchto/libsimdle with a JavaScript fallback
JavaScript
2
star
52

libtt

Virtual console extensions built on libuv
C
2
star
53

cmake-ios

iOS utilities for CMake
CMake
2
star
54

cmake-binary

cmake binaries for windows, linux and macos
JavaScript
2
star
55

prebuild-containers

Containers for prebuilding native Node.js modules
Dockerfile
2
star
56

libsimdle

Simple and portable SIMD instructions for 128 bit vectors, inspired by the WASM SIMD specification
C
2
star
57

bits-to-bytes

Functions for doing bit manipulation of typed arrays
JavaScript
2
star
58

bare-addon

Template repository for creating Bare native addons
C
2
star
59

compact-encoding-bitfield

Compact codec for bitfields
JavaScript
2
star
60

seedbee

Bee for seeds
JavaScript
2
star
61

crc-native

https://github.com/holepunchto/libcrc JavaScript bindings for Node.js
Python
2
star
62

bare-utils

Node.js-compatible utility functions for Bare
JavaScript
2
star
63

bare-distributable-hello-world

Showing how to make a single distributable of a JS app
C
2
star
64

bare-debug-log

Simple debug log for JavaScript
JavaScript
2
star
65

pw-to-ek

Derive a secure encryption key from a password using the sodium's scrypt implementation.
JavaScript
2
star
66

cmake-drive

Drive utilities for CMake
1
star
67

cmake-bare-bundle

Bare bundling utilities for CMake
CMake
1
star
68

simdle-native

https://github.com/holepunchto/libsimdle JavaScript bindings for Node.js
C
1
star
69

native-pipe

Native named pipes
C
1
star
70

hp-rpc-cli

JavaScript
1
star
71

pear-inspect

JavaScript
1
star
72

drive-resolve

Asynchronous require resolution in Hyperdrive
JavaScript
1
star
73

warmup-encoding

Encode/decode sets of random blocks for warmup
JavaScript
1
star
74

framed-stream

Read/write stream messages prefixed 8, 16, 24 or 32 bit length
JavaScript
1
star
75

http-forward-host

Simple stream proxy that sniffs the HTTP host or x-forwarded-for header and allows you to to forward the stream based on that
JavaScript
1
star
76

hypertrace-prometheus

Add support for Prometheus/Grafana to hypertrace
JavaScript
1
star
77

bare-dns

Domain name resolution for JavaScript
C
1
star
78

fifofile

Userland FIFO file
JavaScript
1
star
79

bare-format

String formatting for JavaScript
JavaScript
1
star
80

secure-prompt

Securely prompt stdio using secure buffers
JavaScript
1
star
81

bare-headers

Development headers for Bare
JavaScript
1
star
82

bare-net

TCP and IPC servers and clients for JavaScript
JavaScript
1
star