Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit b1a3414

Browse files
authored
Merge branch 'master' into fix/rpc-relay
2 parents e624cd5 + 042d555 commit b1a3414

18 files changed

Lines changed: 164 additions & 678 deletions

declarations.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ declare module 'hypercore' {
392392
sign?: (message: Uint8Array) => Uint8Array;
393393
verify: (message: Uint8Array, signature: Uint8Array) => boolean;
394394
}
395-
export = class Hypercore extends EventEmitter<'close'> {
395+
class Hypercore extends EventEmitter<'close'> {
396396
constructor(storage: any, key?: Opts | Uint8Array, opts?: Opts);
397397

398398
length: number;
@@ -443,6 +443,8 @@ Populated after ready has been emitted. Will be null before the event.
443443

444444
sessions: Hypercore[];
445445
};
446+
447+
export = Hypercore
446448
}
447449

448450
// file://./node_modules/hyperblobs/index.js

package-lock.json

Lines changed: 45 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/drive/README.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,3 @@
22

33
[DEPRECATED] use `@synonymdev/slashtags-core-data` instead.
44

5-
Drivestore is a Hyperdrive factory that makes it easier to manage large collections of named Hyperdrives.
6-
7-
## Features
8-
9-
- Public unencrypted drive
10-
- Creates and keep track of all created encrypted private drives
11-
12-
## Installation
13-
14-
```
15-
npm install @synonymdev/slashdrive
16-
```
17-
18-
## Usage
19-
20-
```js
21-
import Corestore from 'corestore'
22-
import Drivestore from '@synonymdev/slashdrive'
23-
24-
const corestore = new Corestore('./corestore_dir')
25-
const store = new Drivestore(corestore, keyPair)
26-
27-
const publicDrive = store.get('public') // or store.get()
28-
29-
const privateDrive = store.get('foo') // returns an encrypted Hyperdrive
30-
```
31-
32-
## API
33-
34-
#### `const drivestore = new Drivestore(corestore, keyPair)`
35-
36-
Create new Drivestore.
37-
38-
- `corestore` must be an instance of [Corestore](https://github.com/hypercore-protocol/corestore).
39-
40-
If the instance is a [namespace](https://github.com/hypercore-protocol/corestore#const-store--storenamespacename), the internal corestore will reset its namespace to the `DEFAULT_NAMESPACE` (32 0-bytes).
41-
42-
- `keyPair` public and secret keys to create the public Hyperdrive, the secret key will be used as the `primaryKey` for the internal corestore.
43-
44-
#### `await drivestore.ready()`
45-
46-
Awaits opening metadata hypercore. Useful before [async iterating](#for-await-let-name-of-drivestore) over all created drives.
47-
48-
#### `const hyperdrive = drivestore.get([name])`
49-
50-
Returns an encrypted [Hyperdrive](https://github.com/hypercore-protocol/hyperdrive-next) for a given name.
51-
52-
If `name` is undefined or equal to `/public` it will return a public unencrypted drive, by the same keypair passed to the contsructor.
53-
54-
#### `const stream = drivestore.replicate(stream)`
55-
56-
Same as [drivestore.corestore.replicate(stream)](https://github.com/hypercore-protocol/corestore#const-stream--storereplicateoptsorstream)

packages/drive/index.js

Lines changed: 11 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,25 @@
1-
const Hyperdrive = require('hyperdrive')
2-
const Hyperbee = require('hyperbee')
3-
const b4a = require('b4a')
4-
const safetyCatch = require('safety-catch')
5-
6-
const METADATA_KEY = 'slashtags-drivestore-metadata'
7-
81
class Drivestore {
92
/**
10-
* @param {import('corestore')} corestore
11-
* @param {import('hyperdht').KeyPair} keyPair
3+
* @param {import('@synonymdev/slashtags-core-data')} coreData
124
*/
13-
constructor (corestore, keyPair) {
14-
this.fava = Math.random()
15-
this.keyPair = keyPair
16-
/** @type {import('corestore')} */
17-
this.corestore = corestore.session({ primaryKey: this.keyPair.secretKey, namespace: null })
18-
19-
const metadataCore = this.corestore.get({
20-
name: METADATA_KEY,
21-
encryptionKey: this.keyPair.secretKey
22-
})
23-
this._metadata = new Hyperbee(metadataCore, { keyEncoding: 'utf8' })
24-
this._drives = this._metadata.sub('drives')
25-
26-
this._opening = this._open().catch(safetyCatch)
5+
constructor (coreData) {
6+
this._coreData = coreData
7+
this.corestore = this._coreData._corestoreSession
278
}
289

29-
/** @returns {import('hyperbee').Iterator<{name: string}>} */
10+
/**
11+
* @deprecated
12+
*/
3013
[Symbol.asyncIterator] () {
31-
if (!this.opened) return emptyIterator
32-
const iterator = this._drives.createReadStream()[Symbol.asyncIterator]()
33-
return {
34-
async next () {
35-
const node = await iterator.next()
36-
const value = node.value
37-
return { done: node.done, value: value && { name: value.key } }
38-
}
39-
}
14+
return new Error('not supported')
4015
}
4116

4217
get closed () {
43-
return this.corestore._root._closing
44-
}
45-
46-
async _open () {
47-
await this._drives.feed.ready()
48-
this.opened = true
18+
return this.corestore._root.closing
4919
}
5020

5121
ready () {
52-
return this._opening
22+
return this._coreData.ready()
5323
}
5424

5525
/** @param {Parameters<import('corestore')['replicate']>} args */
@@ -61,53 +31,8 @@ class Drivestore {
6131
* Get a Hyperdrive by its name.
6232
*/
6333
get (name = 'public') {
64-
validateName(name)
65-
const ns = this.corestore.namespace(name).session({ primaryKey: this.keyPair.secretKey })
66-
const _preload = ns._preload.bind(ns)
67-
ns._preload = (opts) => this._preload.bind(this)(opts, _preload, ns, name)
68-
return new Hyperdrive(ns)
34+
return this._coreData._getLocalDrive(name)
6935
}
70-
71-
/**
72-
* Set the correct and current key and encryption Key (enables future key rotation)
73-
* @param {Parameters<import('corestore')['get']>[0]} opts
74-
* @param {*} preload orginal ns._preload
75-
* @param {import('corestore')} ns
76-
* @param {string} name
77-
* @returns {Promise<any>}
78-
*/
79-
async _preload (opts, preload, ns, name) {
80-
const isPublic = name === 'public'
81-
82-
// Get keyPair programatically from name
83-
const { from } = await preload(opts)
84-
85-
// public drive needs no encryption
86-
// No need currently to save a record about the public drive
87-
if (isPublic) {
88-
if (opts.name !== 'db') return { from }
89-
const session = this.corestore.get({ keyPair: this.keyPair })
90-
await session.ready()
91-
return { from: session }
92-
}
93-
94-
this._drives.ready().then(async () => {
95-
const saved = await this._drives.get(name)
96-
if (!saved) await this._drives.put(name, b4a.from(''))
97-
// TODO enable key rotation, where we overwrite keys, or use saved ones.
98-
// TODO block closing drivestore before this update is flushed
99-
})
100-
101-
// Add encryption keys for non public drives
102-
return { from, encryptionKey: ns._namespace }
103-
}
104-
}
105-
106-
/** @param {string} name */
107-
function validateName (name) {
108-
if (!/^[0-9a-zA-Z-._ ]*$/.test(name)) throw new Error('Invalid drive name')
10936
}
11037

111-
const emptyIterator = { async next () { return { done: true, value: null } } }
112-
11338
module.exports = Drivestore

packages/drive/package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@
2828
"types",
2929
"!**/*.tsbuildinfo"
3030
],
31-
"dependencies": {
32-
"b4a": "^1.6.0",
33-
"corestore": "^6.2.1",
34-
"hyperbee": "^2.0.1",
35-
"hyperdrive": "^11.0.0-alpha.5",
36-
"safety-catch": "^1.0.2"
37-
},
3831
"devDependencies": {
32+
"@synonymdev/slashtags-core-data": "^1.0.0-alpha.9",
33+
"b4a": "^1.6.4",
3934
"brittle": "^3.0.2",
4035
"depcheck": "^1.4.3",
4136
"hypercore-crypto": "^3.3.0",
42-
"random-access-memory": "^5.0.1",
4337
"standard": "^17.0.0",
4438
"typescript": "^4.8.2"
4539
}

0 commit comments

Comments
 (0)