Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api): update hub to v0.6.2 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"keywords": [],
"author": "Vincent den Boer",
"dependencies": {
"@textile/hub": "^0.4.1",
"@textile/threads-core": "^0.1.33",
"@textile/hub": "^0.6.2",
"@worldbrain/storex": "^0.4.1",
"@worldbrain/storex-pattern-modules": "^0.4.0",
"@worldbrain/storex-middleware-change-watcher": "^0.1.1",
Expand Down
23 changes: 5 additions & 18 deletions ts/application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EventEmitter } from 'events'
import { Libp2pCryptoIdentity } from '@textile/threads-core'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Libp2pCryptoIdentity has been replaced with the more useful PrivateKey, PublicKey, and Identity classes

import { Client, Buckets, ThreadID, KeyInfo } from '@textile/hub'
import { Client, Buckets, ThreadID, KeyInfo, PrivateKey } from '@textile/hub'
import { StorexHubApi_v0, StorexHubCallbacks_v0, HandleRemoteCallResult_v0 } from '@worldbrain/storex-hub/lib/public-api'
import { SettingsStore, Settings } from './types'
import { APP_NAME } from './constants'
Expand Down Expand Up @@ -90,15 +89,15 @@ export class Application {

async createThreadsClient() {
const client = await Client.withKeyInfo(await this.getKeyInfo())
const identity = await Libp2pCryptoIdentity.fromRandom()
const identity = await PrivateKey.fromRandom()
await client.getToken(identity)
this.threadsClient = client
return client
}

async createBucketsClient() {
const client = await Buckets.withKeyInfo(await this.getKeyInfo())
const identity = await Libp2pCryptoIdentity.fromRandom()
const identity = await PrivateKey.fromRandom()
await client.getToken(identity)
this.bucketsClient = client
return client
Expand Down Expand Up @@ -160,21 +159,9 @@ export class Application {
async ensureBucket(args: { bucketName: string }) {
const client = await this.getBucketsClient()

// Version 1
// const roots = await client.list();
// const existing = roots.find((bucket) => bucket.name === args.bucketName)
// if (existing) {
// return existing.key;
// }

// const created = await client.init(args.bucketName);
// const bucketKey = created.root ? created.root.key : ''

// Version 2
const result = await client.open(args.bucketName)
const bucketKey = result?.key
const result = await client.getOrInit(args.bucketName)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new getOrInit function is designed for your ensure method. Unfortunately this name is changing in the next release to getOrCreate, I'll try to PR again when that happens.

const bucketKey = result.root?.key
if (!bucketKey) throw new Error('bucket not created')

return { bucketKey }
}

Expand Down
35 changes: 6 additions & 29 deletions ts/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
global['WebSocket'] = require('isomorphic-ws')
import io from 'socket.io-client'
import { createStorexHubSocketClient } from '@worldbrain/storex-hub/lib/client'
// import { Identity, ThreadID, Libp2pCryptoIdentity } from '@textile/threads-core'
// import { Database } from '@textile/threads-database'
// import { Client, KeyInfo } from '@textile/hub'
// import { Client, KeyInfo, ThreadID, Identity, PrivateKey } from '@textile/hub'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything should be imported from the single lib now

import { Application } from './application'
import { FileSettingsStore, StorexHubSettingsStore } from './settings'
import { join } from 'path'
Expand All @@ -19,33 +17,12 @@ export async function main(options?: {
// return ThreadID.fromString(threadIDString)
// }

// let database: Database
// if (options?.local) {
// const identity = await Database.randomIdentity()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We learned that the Database class wasn't the right method and was proving confusing to users. We are moving all the local methods directly into the Client as part of this, textileio/js-threads#414. So for now, just use Client and then it should pretty cleanly upgrade to just being local at the same time once that ticket is wrapped.

// database = new Database('demo.db')
// await database.start(identity)
// } else {
// const client = await Client.withKeyInfo(getKeyInfo())
// const identity = await Libp2pCryptoIdentity.fromRandom()
// const token = await client.getToken(identity)
// const threadId = ThreadID.fromRandom()
// await client.newDB(threadId)
// }

// const db = new Database('demo.db')
// await db.start(identity)

// const Thing = db.collections.get('Thing') ?? await db.newCollection('Thing', {})
// const thing1 = new Thing({ test: 5 })
// const thing2 = new Thing({ test: 8 })
// await thing1.save()
// await thing2.save()

// for await (const test of Thing.find({ test: { $gt: 6 } })) {
// console.log(await test)
// }
// const client = await Client.withKeyInfo(getKeyInfo())
// const identity = await PrivateKey.fromRandom()
// const token = await client.getToken(identity)
// const threadId = ThreadID.fromRandom()
// await client.newDB(threadId)

// const db = new Database()
const port = options?.port ?? (process.env.NODE_ENV === 'production' ? 50482 : 50483)
const socket = io(`http://localhost:${port}`)
console.log('Connecting to Storex Hub')
Expand Down