@fireproof/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +5 -0
- package/package.json +70 -0
- package/scripts/propernames/gen.sh +3 -0
- package/scripts/randomcid.js +12 -0
- package/scripts/words/gen.js +55 -0
- package/src/block.js +75 -0
- package/src/blockstore.js +246 -0
- package/src/clock.js +352 -0
- package/src/db-index.js +196 -0
- package/src/fireproof.js +209 -0
- package/src/link.d.ts +3 -0
- package/src/listener.js +111 -0
- package/src/prolly.js +235 -0
- package/src/valet.js +142 -0
- package/test/clock.test.js +725 -0
- package/test/db-index.test.js +214 -0
- package/test/fireproof.test.js +287 -0
- package/test/helpers.js +45 -0
- package/test/listener.test.js +102 -0
- package/test/prolly.test.js +203 -0
package/index.js
ADDED
package/package.json
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
{
|
2
|
+
"name": "@fireproof/core",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"description": "Realtime database for IPFS",
|
5
|
+
"main": "index.js",
|
6
|
+
"type": "module",
|
7
|
+
"scripts": {
|
8
|
+
"test": "standard && mocha test/*.test.js",
|
9
|
+
"coverage": "c8 -r html -r text npm test",
|
10
|
+
"lint": "standard",
|
11
|
+
"lint:fix": "standard --fix"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"database",
|
15
|
+
"JSON",
|
16
|
+
"document",
|
17
|
+
"IPLD",
|
18
|
+
"CID",
|
19
|
+
"IPFS"
|
20
|
+
],
|
21
|
+
"contributors": [
|
22
|
+
"J Chris Anderson",
|
23
|
+
"Alan Shaw",
|
24
|
+
"Travis Vachon",
|
25
|
+
"Mikeal Rogers"
|
26
|
+
],
|
27
|
+
"license": "Apache-2.0 OR MIT",
|
28
|
+
"dependencies": {
|
29
|
+
"@ipld/car": "^5.1.0",
|
30
|
+
"@ipld/dag-cbor": "^9.0.0",
|
31
|
+
"archy": "^1.0.0",
|
32
|
+
"car-transaction": "^1.0.1",
|
33
|
+
"charwise": "^3.0.1",
|
34
|
+
"cli-color": "^2.0.3",
|
35
|
+
"idb": "^7.1.1",
|
36
|
+
"multiformats": "^11.0.1",
|
37
|
+
"prolly-trees": "^0.2.2",
|
38
|
+
"sade": "^1.8.1"
|
39
|
+
},
|
40
|
+
"devDependencies": {
|
41
|
+
"c8": "^7.12.0",
|
42
|
+
"fake-indexeddb": "^4.0.1",
|
43
|
+
"mocha": "^10.2.0",
|
44
|
+
"nanoid": "^4.0.0",
|
45
|
+
"standard": "^17.0.0"
|
46
|
+
},
|
47
|
+
"mocha": {
|
48
|
+
"require": [
|
49
|
+
"fake-indexeddb/auto"
|
50
|
+
]
|
51
|
+
},
|
52
|
+
"standard": {
|
53
|
+
"ignore": [
|
54
|
+
"examples/**/*.tsx",
|
55
|
+
"examples/**/dist"
|
56
|
+
]
|
57
|
+
},
|
58
|
+
"repository": {
|
59
|
+
"type": "git",
|
60
|
+
"url": "git+https://github.com/jchris/fireproof.git"
|
61
|
+
},
|
62
|
+
"bugs": {
|
63
|
+
"url": "https://github.com/jchris/fireproof/issues"
|
64
|
+
},
|
65
|
+
"homepage": "https://github.com/jchris/fireproof#readme",
|
66
|
+
"workspaces": [
|
67
|
+
"examples/todomvc"
|
68
|
+
]
|
69
|
+
|
70
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import crypto from 'node:crypto'
|
2
|
+
import { CID } from 'multiformats/cid'
|
3
|
+
import * as raw from 'multiformats/codecs/raw'
|
4
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
5
|
+
|
6
|
+
async function main () {
|
7
|
+
const bytes = crypto.webcrypto.getRandomValues(new Uint8Array(32))
|
8
|
+
const hash = await sha256.digest(bytes)
|
9
|
+
process.stdout.write(CID.create(1, raw.code, hash).toString())
|
10
|
+
}
|
11
|
+
|
12
|
+
main()
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import fs from 'node:fs'
|
2
|
+
import { Readable } from 'node:stream'
|
3
|
+
import { CarWriter } from '@ipld/car'
|
4
|
+
import { CID } from 'multiformats/cid'
|
5
|
+
import * as raw from 'multiformats/codecs/raw'
|
6
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
7
|
+
import { ShardBlock, put } from '../../index.js'
|
8
|
+
import { MemoryBlockstore } from '../../block.js'
|
9
|
+
|
10
|
+
/** @param {string} str */
|
11
|
+
async function stringToCID (str) {
|
12
|
+
const hash = await sha256.digest(new TextEncoder().encode(str))
|
13
|
+
return CID.create(1, raw.code, hash)
|
14
|
+
}
|
15
|
+
|
16
|
+
async function main () {
|
17
|
+
const data = await fs.promises.readFile('/usr/share/dict/words', 'utf8')
|
18
|
+
const words = data.split(/\n/)
|
19
|
+
const cids = await Promise.all(words.map(stringToCID))
|
20
|
+
const blocks = new MemoryBlockstore()
|
21
|
+
const rootblk = await ShardBlock.create()
|
22
|
+
blocks.putSync(rootblk.cid, rootblk.bytes)
|
23
|
+
|
24
|
+
console.time(`put x${words.length}`)
|
25
|
+
/** @type {import('../../shard').ShardLink} */
|
26
|
+
let root = rootblk.cid
|
27
|
+
for (const [i, word] of words.entries()) {
|
28
|
+
const res = await put(blocks, root, word, cids[i])
|
29
|
+
root = res.root
|
30
|
+
for (const b of res.additions) {
|
31
|
+
blocks.putSync(b.cid, b.bytes)
|
32
|
+
}
|
33
|
+
for (const b of res.removals) {
|
34
|
+
blocks.deleteSync(b.cid)
|
35
|
+
}
|
36
|
+
if (i % 1000 === 0) {
|
37
|
+
console.log(`${Math.floor(i / words.length * 100)}%`)
|
38
|
+
}
|
39
|
+
}
|
40
|
+
console.timeEnd(`put x${words.length}`)
|
41
|
+
|
42
|
+
// @ts-expect-error
|
43
|
+
const { writer, out } = CarWriter.create(root)
|
44
|
+
const finishPromise = new Promise(resolve => {
|
45
|
+
Readable.from(out).pipe(fs.createWriteStream('./pail.car')).on('finish', resolve)
|
46
|
+
})
|
47
|
+
|
48
|
+
for (const b of blocks.entries()) {
|
49
|
+
await writer.put(b)
|
50
|
+
}
|
51
|
+
await writer.close()
|
52
|
+
await finishPromise
|
53
|
+
}
|
54
|
+
|
55
|
+
main()
|
package/src/block.js
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
import { parse } from 'multiformats/link'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @typedef {{ cid: import('./link').AnyLink, bytes: Uint8Array }} AnyBlock
|
5
|
+
* @typedef {{ get: (link: import('./link').AnyLink) => Promise<AnyBlock | undefined> }} BlockFetcher
|
6
|
+
*/
|
7
|
+
|
8
|
+
/** @implements {BlockFetcher} */
|
9
|
+
export class MemoryBlockstore {
|
10
|
+
/** @type {Map<string, Uint8Array>} */
|
11
|
+
#blocks = new Map()
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @param {import('./link').AnyLink} cid
|
15
|
+
* @returns {Promise<AnyBlock | undefined>}
|
16
|
+
*/
|
17
|
+
async get (cid) {
|
18
|
+
const bytes = this.#blocks.get(cid.toString())
|
19
|
+
if (!bytes) return
|
20
|
+
return { cid, bytes }
|
21
|
+
}
|
22
|
+
|
23
|
+
/**
|
24
|
+
* @param {import('./link').AnyLink} cid
|
25
|
+
* @param {Uint8Array} bytes
|
26
|
+
*/
|
27
|
+
async put (cid, bytes) {
|
28
|
+
// console.log('put', cid)
|
29
|
+
this.#blocks.set(cid.toString(), bytes)
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* @param {import('./link').AnyLink} cid
|
34
|
+
* @param {Uint8Array} bytes
|
35
|
+
*/
|
36
|
+
putSync (cid, bytes) {
|
37
|
+
this.#blocks.set(cid.toString(), bytes)
|
38
|
+
}
|
39
|
+
|
40
|
+
/** @param {import('./link').AnyLink} cid */
|
41
|
+
async delete (cid) {
|
42
|
+
this.#blocks.delete(cid.toString())
|
43
|
+
}
|
44
|
+
|
45
|
+
/** @param {import('./link').AnyLink} cid */
|
46
|
+
deleteSync (cid) {
|
47
|
+
this.#blocks.delete(cid.toString())
|
48
|
+
}
|
49
|
+
|
50
|
+
* entries () {
|
51
|
+
for (const [str, bytes] of this.#blocks) {
|
52
|
+
yield { cid: parse(str), bytes }
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
export class MultiBlockFetcher {
|
58
|
+
/** @type {BlockFetcher[]} */
|
59
|
+
#fetchers
|
60
|
+
|
61
|
+
/** @param {BlockFetcher[]} fetchers */
|
62
|
+
constructor (...fetchers) {
|
63
|
+
this.#fetchers = fetchers
|
64
|
+
}
|
65
|
+
|
66
|
+
/** @param {import('./link').AnyLink} link */
|
67
|
+
async get (link) {
|
68
|
+
for (const f of this.#fetchers) {
|
69
|
+
const v = await f.get(link)
|
70
|
+
if (v) {
|
71
|
+
return v
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
@@ -0,0 +1,246 @@
|
|
1
|
+
import { parse } from 'multiformats/link'
|
2
|
+
import * as raw from 'multiformats/codecs/raw'
|
3
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
4
|
+
import * as Block from 'multiformats/block'
|
5
|
+
import * as CBW from '@ipld/car/buffer-writer'
|
6
|
+
|
7
|
+
import Valet from './valet.js'
|
8
|
+
|
9
|
+
// const sleep = ms => new Promise(r => setTimeout(r, ms))
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @typedef {Object} AnyBlock
|
13
|
+
* @property {import('./link').AnyLink} cid - The CID of the block
|
14
|
+
* @property {Uint8Array} bytes - The block's data
|
15
|
+
*
|
16
|
+
* @typedef {Object} Blockstore
|
17
|
+
* @property {function(import('./link').AnyLink): Promise<AnyBlock|undefined>} get - A function to retrieve a block by CID
|
18
|
+
* @property {function(import('./link').AnyLink, Uint8Array): Promise<void>} put - A function to store a block's data and CID
|
19
|
+
*
|
20
|
+
* A blockstore that caches writes to a transaction and only persists them when committed.
|
21
|
+
* @implements {Blockstore}
|
22
|
+
*/
|
23
|
+
export default class TransactionBlockstore {
|
24
|
+
/** @type {Map<string, Uint8Array>} */
|
25
|
+
#oldBlocks = new Map()
|
26
|
+
|
27
|
+
#valet = new Valet() // cars by cid
|
28
|
+
|
29
|
+
#instanceId = 'blkz.' + Math.random().toString(36).substring(2, 4)
|
30
|
+
#inflightTransactions = new Set()
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Get a block from the store.
|
34
|
+
*
|
35
|
+
* @param {import('./link').AnyLink} cid
|
36
|
+
* @returns {Promise<AnyBlock | undefined>}
|
37
|
+
*/
|
38
|
+
async get (cid) {
|
39
|
+
const key = cid.toString()
|
40
|
+
// it is safe to read from the in-flight transactions becauase they are immutable
|
41
|
+
// const bytes = this.#oldBlocks.get(key) || await this.#valet.getBlock(key)
|
42
|
+
const bytes = await this.#transactionsGet(key) || await this.commitedGet(key)
|
43
|
+
// const bytes = this.#blocks.get(key) || await this.#valet.getBlock(key)
|
44
|
+
// console.log('bytes', typeof bytes)
|
45
|
+
if (!bytes) throw new Error('Missing block: ' + key)
|
46
|
+
return { cid, bytes }
|
47
|
+
}
|
48
|
+
|
49
|
+
// this iterates over the in-flight transactions
|
50
|
+
// and returns the first matching block it finds
|
51
|
+
async #transactionsGet (key) {
|
52
|
+
for (const transaction of this.#inflightTransactions) {
|
53
|
+
const got = await transaction.get(key)
|
54
|
+
if (got && got.bytes) return got.bytes
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
async commitedGet (key) {
|
59
|
+
return this.#oldBlocks.get(key) || await this.#valet.getBlock(key)
|
60
|
+
// return await this.#valet.getBlock(key) // todo this is just for testing
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* Add a block to the store. Usually bound to a transaction by a closure.
|
65
|
+
* It sets the lastCid property to the CID of the block that was put.
|
66
|
+
* This is used by the transaction as the head of the car when written to the valet.
|
67
|
+
* We don't have to worry about which transaction we are when we are here because
|
68
|
+
* we are the transactionBlockstore.
|
69
|
+
*
|
70
|
+
* @param {import('./link').AnyLink} cid
|
71
|
+
* @param {Uint8Array} bytes
|
72
|
+
*/
|
73
|
+
put (cid, bytes) {
|
74
|
+
throw new Error('use a transaction to put')
|
75
|
+
}
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Iterate over all blocks in the store.
|
79
|
+
*
|
80
|
+
* @yields {AnyBlock}
|
81
|
+
* @returns {AsyncGenerator<AnyBlock>}
|
82
|
+
*/
|
83
|
+
* entries () {
|
84
|
+
// todo needs transaction blocks?
|
85
|
+
// for (const [str, bytes] of this.#blocks) {
|
86
|
+
// yield { cid: parse(str), bytes }
|
87
|
+
// }
|
88
|
+
for (const [str, bytes] of this.#oldBlocks) {
|
89
|
+
yield { cid: parse(str), bytes }
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Begin a transaction. Ensures the uncommited blocks are empty at the begining.
|
95
|
+
* Returns the blocks to read and write during the transaction.
|
96
|
+
* @returns {InnerBlockstore}
|
97
|
+
* @memberof TransactionBlockstore
|
98
|
+
*/
|
99
|
+
begin (label = '') {
|
100
|
+
const innerTransactionBlockstore = new InnerBlockstore(label, this)
|
101
|
+
this.#inflightTransactions.add(innerTransactionBlockstore)
|
102
|
+
return innerTransactionBlockstore
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Commit the transaction. Writes the blocks to the store.
|
107
|
+
* @returns {Promise<void>}
|
108
|
+
* @memberof TransactionBlockstore
|
109
|
+
*/
|
110
|
+
async commit (innerBlockstore) {
|
111
|
+
await this.#doCommit(innerBlockstore)
|
112
|
+
}
|
113
|
+
|
114
|
+
// first get the transaction blockstore from the map of transaction blockstores
|
115
|
+
// then copy it to oldBlocks
|
116
|
+
// then write the transaction blockstore to a car
|
117
|
+
// then write the car to the valet
|
118
|
+
// then remove the transaction blockstore from the map of transaction blockstores
|
119
|
+
#doCommit = async (innerBlockstore) => {
|
120
|
+
const cids = new Set()
|
121
|
+
for (const { cid, bytes } of innerBlockstore.entries()) {
|
122
|
+
const stringCid = cid.toString() // unnecessary string conversion, can we fix upstream?
|
123
|
+
if (this.#oldBlocks.has(stringCid)) {
|
124
|
+
// console.log('Duplicate block: ' + stringCid)
|
125
|
+
} else {
|
126
|
+
this.#oldBlocks.set(stringCid, bytes)
|
127
|
+
cids.add(stringCid)
|
128
|
+
}
|
129
|
+
}
|
130
|
+
if (cids.size > 0) {
|
131
|
+
console.log(innerBlockstore.label, 'committing', cids.size, 'blocks')
|
132
|
+
await this.#valetWriteTransaction(innerBlockstore, cids)
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Group the blocks into a car and write it to the valet.
|
138
|
+
* @param {InnerBlockstore} innerBlockstore
|
139
|
+
* @param {Set<string>} cids
|
140
|
+
* @returns {Promise<void>}
|
141
|
+
* @memberof TransactionBlockstore
|
142
|
+
* @private
|
143
|
+
*/
|
144
|
+
#valetWriteTransaction = async (innerBlockstore, cids) => {
|
145
|
+
if (innerBlockstore.lastCid) {
|
146
|
+
const newCar = await blocksToCarBlock(innerBlockstore.lastCid, innerBlockstore)
|
147
|
+
await this.#valet.parkCar(newCar.cid.toString(), newCar.bytes, cids)
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* Retire the transaction. Clears the uncommited blocks.
|
153
|
+
* @returns {void}
|
154
|
+
* @memberof TransactionBlockstore
|
155
|
+
*/
|
156
|
+
retire (innerBlockstore) {
|
157
|
+
this.#inflightTransactions.delete(innerBlockstore)
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
/**
|
162
|
+
* Runs a function on an inner blockstore, then persists the change to a car writer
|
163
|
+
* or other outer blockstore.
|
164
|
+
* @param {string} label
|
165
|
+
* @param {TransactionBlockstore} blockstore
|
166
|
+
* @param {(innerBlockstore: Blockstore) => Promise<any>} doFun
|
167
|
+
* @returns {Promise<any>}
|
168
|
+
* @memberof TransactionBlockstore
|
169
|
+
*/
|
170
|
+
export const doTransaction = async (label, blockstore, doFun) => {
|
171
|
+
if (!blockstore.commit) return await doFun(blockstore)
|
172
|
+
const innerBlockstore = blockstore.begin(label)
|
173
|
+
try {
|
174
|
+
const result = await doFun(innerBlockstore)
|
175
|
+
await blockstore.commit(innerBlockstore)
|
176
|
+
return result
|
177
|
+
} catch (e) {
|
178
|
+
console.error(`Transaction ${label} failed`, e, e.stack)
|
179
|
+
throw e
|
180
|
+
} finally {
|
181
|
+
blockstore.retire(innerBlockstore)
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
const blocksToCarBlock = async (lastCid, blocks) => {
|
186
|
+
let size = 0
|
187
|
+
const headerSize = CBW.headerLength({ roots: [lastCid] })
|
188
|
+
size += headerSize
|
189
|
+
for (const { cid, bytes } of blocks.entries()) {
|
190
|
+
size += CBW.blockLength({ cid, bytes })
|
191
|
+
}
|
192
|
+
const buffer = new Uint8Array(size)
|
193
|
+
const writer = await CBW.createWriter(buffer, { headerSize })
|
194
|
+
|
195
|
+
writer.addRoot(lastCid)
|
196
|
+
|
197
|
+
for (const { cid, bytes } of blocks.entries()) {
|
198
|
+
writer.write({ cid, bytes })
|
199
|
+
}
|
200
|
+
await writer.close()
|
201
|
+
return await Block.encode({ value: writer.bytes, hasher: sha256, codec: raw })
|
202
|
+
}
|
203
|
+
|
204
|
+
/** @implements {BlockFetcher} */
|
205
|
+
export class InnerBlockstore {
|
206
|
+
/** @type {Map<string, Uint8Array>} */
|
207
|
+
#blocks = new Map()
|
208
|
+
lastCid = null
|
209
|
+
label = ''
|
210
|
+
parentBlockstore = null
|
211
|
+
|
212
|
+
constructor (label, parentBlockstore) {
|
213
|
+
this.label = label
|
214
|
+
this.parentBlockstore = parentBlockstore
|
215
|
+
}
|
216
|
+
|
217
|
+
/**
|
218
|
+
* @param {import('./link').AnyLink} cid
|
219
|
+
* @returns {Promise<AnyBlock | undefined>}
|
220
|
+
*/
|
221
|
+
async get (cid) {
|
222
|
+
const key = cid.toString()
|
223
|
+
let bytes = this.#blocks.get(key)
|
224
|
+
if (bytes) { return { cid, bytes } }
|
225
|
+
bytes = await this.parentBlockstore.commitedGet(key)
|
226
|
+
if (bytes) {
|
227
|
+
return { cid, bytes }
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
/**
|
232
|
+
* @param {import('./link').AnyLink} cid
|
233
|
+
* @param {Uint8Array} bytes
|
234
|
+
*/
|
235
|
+
put (cid, bytes) {
|
236
|
+
// console.log('put', cid)
|
237
|
+
this.#blocks.set(cid.toString(), bytes)
|
238
|
+
this.lastCid = cid
|
239
|
+
}
|
240
|
+
|
241
|
+
* entries () {
|
242
|
+
for (const [str, bytes] of this.#blocks) {
|
243
|
+
yield { cid: parse(str), bytes }
|
244
|
+
}
|
245
|
+
}
|
246
|
+
}
|