beakerbrowser / beaker

An experimental peer-to-peer Web browser
https://beakerbrowser.com/
MIT License
6.75k stars 544 forks source link

HyperTrie api #1623

Open mafintosh opened 4 years ago

mafintosh commented 4 years ago

When writing more dynamic sites I’m missing a p2p database api in addition to the file one. Particularly I miss:

We already have HyperTrie which supports all those 3 things.

Would love to get access to webified version of that that exposes in some form put, del, get, iterator, batch, diff, checkout

pfrazee commented 4 years ago

Okay taking a first pass at the API spec:

beaker.hypertrie.put(url: String, value: String|ArrayBuffer): Promise<void>
beaker.hypertrie.del(url: String): Promise<void>
beaker.hypertrie.get(url: String): Promise<String|ArrayBuffer>
beaker.hypertrie.iterator(url: String, { recursive: Boolean, random: Boolean }): HypertrieIterator
beaker.hypertrie.batch(actions: BatchAction[]): Promise<void>
beaker.hypertrie.diff(url: String, version: Number): Promise<DiffDesc[]>
beaker.hypertrie.checkout(url: String, version: Number): Hypertrie
beaker.hypertrie.trie(url): Hypertrie
Hypertrie#put(prefix: String, value: String|ArrayBuffer): Promise<void>
Hypertrie#del(prefix: String): Promise<void>
Hypertrie#get(prefix: String): Promise<String|ArrayBuffer>
Hypertrie#iterator(prefix: String, { recursive: Boolean, random: Boolean }): HypertrieIterator
Hypertrie#batch(actions: BatchAction[]): Promise<void>
Hypertrie#diff(prefix: String, version: Number): Promise<DiffDesc[]>
Hypertrie#checkout(version: Number): Hypertrie

type BatchAction {
  type: 'put' | 'del',
  key: String,
  value: Optional<String|ArrayBuffer>
}

HypertrieIterator ??
mafintosh commented 4 years ago

beaker.hypertrie.diff(url: String, version: Number): Promise<DiffDesc[]> should return an iterator also with <left: Node, right: Node>

beaker.hypertrie.get(url: String): Promise<String|ArrayBuffer> and the iterator returns Node

type Node {
  seq: PositiveInteger,
  key: String,
  value: Optional<String|Uint8Array>
}

In put/BatchAction the value should also be Uint8Array instead of ArrayBuffer

pfrazee commented 4 years ago

Updated spec:

beaker.hypertrie.put(url: String, value: String|Uint8Array): Promise<void>
beaker.hypertrie.del(url: String): Promise<void>
beaker.hypertrie.get(url: String): Promise<HypertrieNode>
beaker.hypertrie.iterator(url: String, { recursive: Boolean, random: Boolean }): HypertrieIterator
beaker.hypertrie.batch(actions: BatchAction[]): Promise<void>
beaker.hypertrie.diff(url: String, version: Number): HypertrieDiffIterator
beaker.hypertrie.checkout(url: String, version: Number): Hypertrie
beaker.hypertrie.trie(url): Hypertrie
Hypertrie#put(prefix: String, value: String|Uint8Array): Promise<void>
Hypertrie#del(prefix: String): Promise<void>
Hypertrie#get(prefix: String): Promise<HypertrieNode>
Hypertrie#iterator(prefix: String, { recursive: Boolean, random: Boolean }): HypertrieIterator
Hypertrie#batch(actions: HypertrieBatchAction[]): Promise<void>
Hypertrie#diff(prefix: String, version: Number): HypertrieDiffIterator
Hypertrie#checkout(version: Number): Hypertrie

type HypertrieNode {
  seq: Number,
  key: String,
  value: Optional<String|Uint8Array>
}

type HypertrieDiff {
  left: Optional<HypertrieNode>,
  right: Optional<HypertrieNode>
}

type HypertrieBatchAction {
  type: 'put' | 'del',
  key: String,
  value: Optional<String|Uint8Array>
}

type HypertrieIterator {
  next: Function(Function(node: HypertrieNode): Void):Void,
  [Symbol.asyncIterator]: AsyncGeneratorFunction(): HypertrieNode
}

type HypertrieDiffIterator {
  next: Function(Function(node: HypertrieDiff): Void):Void,
  [Symbol.asyncIterator]: AsyncGeneratorFunction(): HypertrieDiff
}

@mafintosh any interest in making the iterators work with for await? EDIT: updated to support that

for await (let node of mytrie.iterator('/')) {
  console.log(node)
}
pfrazee commented 4 years ago

In addition to settling the API spec, we need to decide how Hypertries are managed for the user.

mafintosh commented 4 years ago

@pfrazee def async iterators

mafintosh commented 4 years ago

Is there an API for creating new Hypertries? (prob yes)

yes

Is there an API for deleteing a Hypertrie?

v2 feature

Is there a browser UI for creating new Hypertries? (prob no)

v3 feature

Where do users find their created Hypertries? (in the library? somehow attached to the site that created the trie?)

a simple list in the library would be fine for now

How do users manage their Hypertries? (is it like a drive or is it more like localStorage values which are managed via devtools and cleared when you clear a site's storage?)

they are like drives

What does Beaker render when you visit a Hypertrie?

Just a page saying "this is a hypertrie with X changes"