@fireproof/core 0.7.3-dev.2 → 0.8.0-dev.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/blockstore.js +6 -0
- package/dist/crypto.js +5 -1
- package/dist/database.js +14 -7
- package/dist/fireproof.js +1 -0
- package/dist/loader.js +0 -4
- package/dist/prolly.js +1 -1
- package/dist/remote.js +102 -0
- package/dist/src/fireproof.d.ts +6 -5
- package/dist/src/fireproof.js +65878 -29050
- package/dist/src/fireproof.js.map +1 -1
- package/dist/src/fireproof.mjs +65878 -29050
- package/dist/src/fireproof.mjs.map +1 -1
- package/dist/storage/base.js +177 -262
- package/dist/storage/blocksToEncryptedCarBlock.js +144 -0
- package/dist/storage/browser.js +1 -1
- package/dist/storage/filesystem.js +2 -3
- package/dist/storage/rest.js +1 -2
- package/dist/storage/ucan.js +0 -40
- package/dist/storage/utils.js +144 -0
- package/dist/sync.js +1 -1
- package/dist/valet.js +9 -3
- package/package.json +3 -5
- package/src/blockstore.js +4 -0
- package/src/crypto.js +5 -1
- package/src/database.js +14 -9
- package/src/fireproof.js +1 -0
- package/src/loader.js +0 -5
- package/src/prolly.js +1 -1
- package/src/remote.js +113 -0
- package/src/storage/base.js +194 -275
- package/src/storage/browser.js +1 -1
- package/src/storage/filesystem.js +2 -3
- package/src/storage/rest.js +1 -2
- package/src/storage/ucan.js +0 -45
- package/src/storage/utils.js +152 -0
- package/src/sync.js +1 -1
- package/src/valet.js +9 -3
package/src/remote.js
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
// when you call database.connect(email)
|
2
|
+
// it will return a promise that resolves when the user is logged in
|
3
|
+
// and sends you an email
|
4
|
+
|
5
|
+
import { create } from '@web3-storage/w3up-client'
|
6
|
+
import * as w3clock from '@web3-storage/clock/client'
|
7
|
+
import { CID } from 'multiformats'
|
8
|
+
|
9
|
+
export class Remote {
|
10
|
+
client = null
|
11
|
+
name = 'unset'
|
12
|
+
config = {}
|
13
|
+
|
14
|
+
constructor (database, name, config) {
|
15
|
+
this.name = name
|
16
|
+
this.config = config
|
17
|
+
this.database = database
|
18
|
+
}
|
19
|
+
|
20
|
+
async clock (cid) {
|
21
|
+
// const did = this.client.currentSpace()
|
22
|
+
const agent = this.client.agent()
|
23
|
+
const head = await w3clock.head({ issuer: agent, with: agent.did(), proofs: [] })
|
24
|
+
console.log('head', head, JSON.stringify(head.root.data.ocm.out))
|
25
|
+
const headCids = head.root.data.ocm.out.ok.head
|
26
|
+
const blocks = await Promise.all([this.database.blocks.get(CID.parse(cid)),
|
27
|
+
...headCids.map(c => this.database.blocks.get(c))])
|
28
|
+
|
29
|
+
console.log('blocks', blocks)
|
30
|
+
const adv = await w3clock.advance({ issuer: agent, with: agent.did(), proofs: [] }, CID.parse(cid)
|
31
|
+
, { blocks }
|
32
|
+
)
|
33
|
+
console.log('adv', adv, JSON.stringify(adv.root.data.ocm.out))
|
34
|
+
return { head, adv }
|
35
|
+
}
|
36
|
+
|
37
|
+
async sync (cid) {
|
38
|
+
// fetch the remote clock headCids using w3clock.head
|
39
|
+
const agent = this.client.agent()
|
40
|
+
const head = await w3clock.head({ issuer: agent, with: agent.did(), proofs: [] })
|
41
|
+
console.log('head', head, JSON.stringify(head.root.data.ocm.out))
|
42
|
+
const headCids = head.root.data.ocm.out.ok.head
|
43
|
+
const lastSyncHead = await this.database.blocks.valet.primary.getLastSynced()
|
44
|
+
console.log('lastSyncHead', lastSyncHead)
|
45
|
+
const headSet = new Set(headCids.map(c => c.toString()))
|
46
|
+
const lastSyncSet = new Set(lastSyncHead.map(c => c.toString()))
|
47
|
+
|
48
|
+
// are they the same?
|
49
|
+
const same = headSet.size === lastSyncSet.size && [...headSet].every(value => lastSyncSet.has(value))
|
50
|
+
|
51
|
+
// if the headCids and the lastSyncHead are the same, we are in sync and can push
|
52
|
+
if (same) {
|
53
|
+
const currentHead = this.database.clock
|
54
|
+
const currentHeadSet = new Set(currentHead.map(c => c.toString()))
|
55
|
+
|
56
|
+
console.log('synced with cloud', headSet, lastSyncSet)
|
57
|
+
|
58
|
+
// are they the same?
|
59
|
+
const currentSame = headSet.size === currentHeadSet.size && [...headSet].every(value => currentHeadSet.has(value))
|
60
|
+
if (currentSame) {
|
61
|
+
// we are in sync, do nothing
|
62
|
+
return true
|
63
|
+
} else {
|
64
|
+
console.log('push to cloud', headSet, currentHeadSet)
|
65
|
+
// we are ahead of the remote, push our clock
|
66
|
+
// const lastCompact = this.database.blocks.valet.primary.getLastCompact()
|
67
|
+
// get a compact since the last sync
|
68
|
+
console.log('we are ahead of the remote, push our clock')
|
69
|
+
// const compact = this.database.blocks.valet.primary.getCompactSince(lastSyncHead)
|
70
|
+
}
|
71
|
+
} else {
|
72
|
+
// we are behind, fetch the remote
|
73
|
+
console.log('we are behind, fetch the remote')
|
74
|
+
}
|
75
|
+
|
76
|
+
// if it is the same as the local (current metadata carcid? `newValetCidCar` / sync clock), do nothing, we are in sync
|
77
|
+
// if it is the same as our previously pushed clock event, but our local clock is ahead of it, we need to push our clock
|
78
|
+
// - we can store the previous clock event cid in the metadata
|
79
|
+
// - sending our updates:
|
80
|
+
// - get the _last_sync and _last_compact values from our metadata
|
81
|
+
// - if last sync is after last compact
|
82
|
+
// - make a merged car file for the syncs
|
83
|
+
// - else
|
84
|
+
// - upload the car file for the last compact
|
85
|
+
// - make a merge car file for any uncompacted car files since the last compact, it should base its cidMap on the compact car file (as we go the sync stream will need to track it's own cidMap)
|
86
|
+
// - if there is only one car file, it is the merge car file (already based on last compact)
|
87
|
+
// - upload the merge car file
|
88
|
+
// - create a new clock block with the current w3clock.head as parent and the merge car file cid as the data
|
89
|
+
// - update the remote clock with the new clock block (it doesn't need to fetch the car file, and we dont need to store the clock blocks locally, just the most recent one)
|
90
|
+
//
|
91
|
+
// else if the remote head is not contained by our clock, it is is ahead of the local sync clock.
|
92
|
+
// - get the car file it points to from its data field
|
93
|
+
// - merge to the local clock (park that car so we have both carcid indexes)
|
94
|
+
// - calculate a new root from the merged head, and update the local clock
|
95
|
+
}
|
96
|
+
|
97
|
+
async connect (email) {
|
98
|
+
try {
|
99
|
+
const client = await create()
|
100
|
+
await client.authorize(email)
|
101
|
+
const claims = await client.capability.access.claim()
|
102
|
+
console.log('claims', claims)
|
103
|
+
const space = await client.createSpace('fp.' + this.name)
|
104
|
+
console.log('space', space)
|
105
|
+
await client.setCurrentSpace(space.did())
|
106
|
+
await client.registerSpace(email)
|
107
|
+
this.client = client
|
108
|
+
console.log('client', client)
|
109
|
+
} catch (err) {
|
110
|
+
console.error('registration failed: ', err)
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|