@fireproof/core 0.10.0-dev → 0.10.1-dev

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.
Files changed (90) hide show
  1. package/package.json +5 -1
  2. package/.eslintrc.cjs +0 -37
  3. package/.vscode/launch.json +0 -12
  4. package/dist/crdt-helpers.browser.js +0 -4961
  5. package/dist/crdt-helpers.browser.js.map +0 -7
  6. package/dist/crdt-helpers.cjs.js +0 -4823
  7. package/dist/crdt-helpers.cjs.js.map +0 -7
  8. package/dist/crdt-helpers.esm.js +0 -4788
  9. package/dist/crdt-helpers.esm.js.map +0 -7
  10. package/dist/crdt.browser.js +0 -18214
  11. package/dist/crdt.browser.js.map +0 -7
  12. package/dist/crdt.cjs.js +0 -8692
  13. package/dist/crdt.cjs.js.map +0 -7
  14. package/dist/crdt.esm.js +0 -8682
  15. package/dist/crdt.esm.js.map +0 -7
  16. package/dist/database.browser.js +0 -18867
  17. package/dist/database.browser.js.map +0 -7
  18. package/dist/database.cjs.js +0 -9296
  19. package/dist/database.cjs.js.map +0 -7
  20. package/dist/database.esm.js +0 -9288
  21. package/dist/database.esm.js.map +0 -7
  22. package/dist/loader-helpers.browser.js +0 -6943
  23. package/dist/loader-helpers.browser.js.map +0 -7
  24. package/dist/loader-helpers.cjs.js +0 -4419
  25. package/dist/loader-helpers.cjs.js.map +0 -7
  26. package/dist/loader-helpers.esm.js +0 -4408
  27. package/dist/loader-helpers.esm.js.map +0 -7
  28. package/dist/loader.browser.js +0 -15968
  29. package/dist/loader.browser.js.map +0 -7
  30. package/dist/loader.cjs.js +0 -6667
  31. package/dist/loader.cjs.js.map +0 -7
  32. package/dist/loader.esm.js +0 -6657
  33. package/dist/loader.esm.js.map +0 -7
  34. package/dist/store-browser.browser.js +0 -1414
  35. package/dist/store-browser.browser.js.map +0 -7
  36. package/dist/store-browser.cjs.js +0 -1387
  37. package/dist/store-browser.cjs.js.map +0 -7
  38. package/dist/store-browser.esm.js +0 -1358
  39. package/dist/store-browser.esm.js.map +0 -7
  40. package/dist/store-fs.browser.js +0 -16142
  41. package/dist/store-fs.browser.js.map +0 -7
  42. package/dist/store-fs.cjs.js +0 -1171
  43. package/dist/store-fs.cjs.js.map +0 -7
  44. package/dist/store-fs.esm.js +0 -1143
  45. package/dist/store-fs.esm.js.map +0 -7
  46. package/dist/store.browser.js +0 -1113
  47. package/dist/store.browser.js.map +0 -7
  48. package/dist/store.cjs.js +0 -1126
  49. package/dist/store.cjs.js.map +0 -7
  50. package/dist/store.esm.js +0 -1097
  51. package/dist/store.esm.js.map +0 -7
  52. package/dist/transaction.browser.js +0 -17241
  53. package/dist/transaction.browser.js.map +0 -7
  54. package/dist/transaction.cjs.js +0 -7842
  55. package/dist/transaction.cjs.js.map +0 -7
  56. package/dist/transaction.esm.js +0 -7831
  57. package/dist/transaction.esm.js.map +0 -7
  58. package/dist/types.d.browser.js +0 -4
  59. package/dist/types.d.browser.js.map +0 -7
  60. package/dist/types.d.cjs.js +0 -19
  61. package/dist/types.d.cjs.js.map +0 -7
  62. package/dist/types.d.esm.js +0 -1
  63. package/dist/types.d.esm.js.map +0 -7
  64. package/scripts/analyze.js +0 -31
  65. package/scripts/build.js +0 -20
  66. package/scripts/serve.js +0 -20
  67. package/scripts/settings.js +0 -65
  68. package/scripts/test.js +0 -14
  69. package/src/crdt-helpers.ts +0 -89
  70. package/src/crdt.ts +0 -45
  71. package/src/database.ts +0 -61
  72. package/src/fireproof.ts +0 -6
  73. package/src/loader-helpers.ts +0 -53
  74. package/src/loader.ts +0 -66
  75. package/src/store-browser.ts +0 -76
  76. package/src/store-fs.ts +0 -51
  77. package/src/store.ts +0 -32
  78. package/src/transaction.ts +0 -68
  79. package/src/types.d.ts +0 -38
  80. package/test/crdt.test.js +0 -142
  81. package/test/database.test.js +0 -144
  82. package/test/fireproof.test.js +0 -50
  83. package/test/globals.d.ts +0 -4
  84. package/test/hello.test.js +0 -9
  85. package/test/helpers.js +0 -34
  86. package/test/loader.test.js +0 -112
  87. package/test/store-fs.test.js +0 -105
  88. package/test/transaction.test.js +0 -90
  89. package/tsconfig.json +0 -18
  90. package/webpack.config.cjs +0 -17
package/src/store.ts DELETED
@@ -1,32 +0,0 @@
1
- import { parse } from 'multiformats/link'
2
- import { AnyLink } from './types'
3
-
4
- export class StoredHeader {
5
- car: AnyLink
6
- constructor(jsonHeader: { car: string }) {
7
- this.car = parse(jsonHeader.car)
8
- }
9
- }
10
-
11
- export class HeaderStore {
12
- name: string
13
- constructor(name: string) {
14
- this.name = name
15
- }
16
-
17
- makeHeader(car: AnyLink): string {
18
- return JSON.stringify({ car: car.toString() })
19
- }
20
-
21
- parseHeader(headerData: string) {
22
- const header = JSON.parse(headerData) as { car: string }
23
- return new StoredHeader(header)
24
- }
25
- }
26
-
27
- export class CarStore {
28
- name: string
29
- constructor(name: string) {
30
- this.name = name
31
- }
32
- }
@@ -1,68 +0,0 @@
1
- import { MemoryBlockstore } from '@alanshaw/pail/block'
2
- import { BlockFetcher, AnyBlock, AnyLink, BulkResult, ClockHead } from './types'
3
- import { Loader } from './loader'
4
- import { CID } from 'multiformats'
5
-
6
- /** forked from
7
- * https://github.com/alanshaw/pail/blob/main/src/block.js
8
- * thanks Alan
9
- **/
10
-
11
- export class Transaction extends MemoryBlockstore {
12
- constructor(private parent: BlockFetcher) {
13
- super()
14
- this.parent = parent
15
- }
16
-
17
- async get(cid: AnyLink): Promise<AnyBlock | undefined> {
18
- return this.parent.get(cid)
19
- }
20
-
21
- async superGet(cid: AnyLink): Promise<AnyBlock | undefined> {
22
- return super.get(cid)
23
- }
24
- }
25
-
26
- export class TransactionBlockstore implements BlockFetcher {
27
- name: string | null = null
28
- ready: Promise<{ head: ClockHead }> // todo this will be a map of headers by branch name
29
-
30
- private transactions: Set<Transaction> = new Set()
31
- private loader: Loader | null = null
32
-
33
- constructor(name?: string, loader?: Loader) {
34
- if (name) {
35
- this.name = name
36
- this.loader = loader || new Loader(name)
37
- this.ready = this.loader.ready
38
- } else {
39
- this.ready = Promise.resolve({ head: [] })
40
- }
41
- }
42
-
43
- // eslint-disable-next-line @typescript-eslint/require-await
44
- async put() {
45
- throw new Error('use a transaction to put')
46
- }
47
-
48
- async get(cid: AnyLink): Promise<AnyBlock | undefined> {
49
- for (const f of this.transactions) {
50
- const v = await f.superGet(cid)
51
- if (v) return v
52
- }
53
- if (!this.loader) return
54
- return await this.loader.getBlock(cid as CID)
55
- }
56
-
57
- async transaction(fn: (t: Transaction) => Promise<BulkResult>) {
58
- const t = new Transaction(this)
59
- this.transactions.add(t)
60
- const done: BulkResult = await fn(t)
61
- if (done) { return { ...done, car: await this.commit(t, done) } }
62
- return done
63
- }
64
-
65
- async commit(t: Transaction, done: BulkResult): Promise<AnyLink | undefined> {
66
- return await this.loader?.commit(t, done)
67
- }
68
- }
package/src/types.d.ts DELETED
@@ -1,38 +0,0 @@
1
- import { Link } from 'multiformats'
2
- import { EventLink } from '@alanshaw/pail/clock'
3
- import { EventData } from '@alanshaw/pail/crdt'
4
-
5
- export type ClockHead = EventLink<EventData>[]
6
-
7
- export type BulkResult = {
8
- head: ClockHead
9
- car?: AnyLink
10
- }
11
-
12
- type DocBody = {
13
- [key: string]: any
14
- }
15
-
16
- export type Doc = DocBody & {
17
- _id: string
18
- }
19
-
20
- export type DocUpdate = {
21
- key: string
22
- value?: DocBody
23
- del?: boolean
24
- }
25
-
26
- export type DocValue = {
27
- doc?: DocBody
28
- del?: boolean
29
- }
30
-
31
- export type AnyLink = Link<unknown, number, number, 1 | 0>
32
- export type AnyBlock = { cid: AnyLink; bytes: Uint8Array }
33
- export type BlockFetcher = { get: (link: AnyLink) => Promise<AnyBlock | undefined> }
34
-
35
- export type DbResponse = {
36
- id: string
37
- clock: ClockHead
38
- }
package/test/crdt.test.js DELETED
@@ -1,142 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-call */
2
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
3
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
- /* eslint-disable @typescript-eslint/require-await */
5
- /* eslint-disable mocha/max-top-level-suites */
6
- import { assert, equals, notEquals } from './helpers.js'
7
- import { CRDT } from '../dist/crdt.esm.js'
8
-
9
- describe('Fresh crdt', function () {
10
- /** @type {CRDT} */
11
- let crdt
12
- beforeEach(function () {
13
- crdt = new CRDT()
14
- })
15
- it('should have an empty head', async function () {
16
- const head = crdt._head
17
- equals(head.length, 0)
18
- })
19
- it('should accept put and return results', async function () {
20
- const didPut = await crdt.bulk([{ key: 'hello', value: { hello: 'world' } }])
21
- const head = didPut.head
22
- equals(head.length, 1)
23
- })
24
- it('should accept multi-put and return results', async function () {
25
- const didPut = await crdt.bulk([{ key: 'ace', value: { points: 11 } }, { key: 'king', value: { points: 10 } }])
26
- const head = didPut.head
27
- equals(head.length, 1)
28
- })
29
- })
30
-
31
- describe('CRDT with one record', function () {
32
- /** @type {CRDT} */
33
- let crdt, firstPut
34
- beforeEach(async function () {
35
- crdt = new CRDT()
36
- firstPut = await crdt.bulk([{ key: 'hello', value: { hello: 'world' } }])
37
- })
38
- it('should have a one-element head', async function () {
39
- const head = crdt._head
40
- equals(head.length, 1)
41
- })
42
- it('should return the head', async function () {
43
- equals(firstPut.head.length, 1)
44
- })
45
- it('return the record on get', async function () {
46
- const got = await crdt.get('hello')
47
- assert(got)
48
- const value = got.doc
49
- equals(value.hello, 'world')
50
- })
51
- it('should accept another put and return results', async function () {
52
- const didPut = await crdt.bulk([{ key: 'nice', value: { nice: 'data' } }])
53
- const head = didPut.head
54
- equals(head.length, 1)
55
- const { doc } = await crdt.get('nice')
56
- equals(doc.nice, 'data')
57
- })
58
- it('should allow for a delete', async function () {
59
- const didDel = await crdt.bulk([{ key: 'hello', del: true }])
60
- assert(didDel.head)
61
- const got = await crdt.get('hello')
62
- assert(!got)
63
- })
64
- it('should offer changes', async function () {
65
- const { result } = await crdt.changes([])
66
- equals(result.length, 1)
67
- equals(result[0].key, 'hello')
68
- equals(result[0].value.hello, 'world')
69
- })
70
- })
71
-
72
- describe('CRDT with a multi-write', function () {
73
- /** @type {CRDT} */
74
- let crdt, firstPut
75
- beforeEach(async function () {
76
- crdt = new CRDT()
77
- firstPut = await crdt.bulk([{ key: 'ace', value: { points: 11 } }, { key: 'king', value: { points: 10 } }])
78
- })
79
- it('should have a one-element head', async function () {
80
- const head = crdt._head
81
- equals(head.length, 1)
82
- equals(firstPut.head.length, 1)
83
- })
84
- it('return the records on get', async function () {
85
- const { doc } = await crdt.get('ace')
86
- equals(doc.points, 11)
87
-
88
- const got2 = await crdt.get('king')
89
- assert(got2)
90
- equals(got2.doc.points, 10)
91
- })
92
- it('should accept another put and return results', async function () {
93
- const didPut = await crdt.bulk([{ key: 'queen', value: { points: 10 } }])
94
- const head = didPut.head
95
- equals(head.length, 1)
96
- const got = await crdt.get('queen')
97
- assert(got)
98
- equals(got.doc.points, 10)
99
- })
100
- it('should offer changes', async function () {
101
- const { result } = await crdt.changes([])
102
- equals(result.length, 2)
103
- equals(result[0].key, 'ace')
104
- equals(result[0].value.points, 11)
105
- equals(result[1].key, 'king')
106
- })
107
- })
108
-
109
- describe('CRDT with two multi-writes', function () {
110
- /** @type {CRDT} */
111
- let crdt, firstPut, secondPut
112
- beforeEach(async function () {
113
- crdt = new CRDT()
114
- firstPut = await crdt.bulk([{ key: 'ace', value: { points: 11 } }, { key: 'king', value: { points: 10 } }])
115
- secondPut = await crdt.bulk([{ key: 'queen', value: { points: 10 } }, { key: 'jack', value: { points: 10 } }])
116
- })
117
- it('should have a one-element head', async function () {
118
- const head = crdt._head
119
- equals(head.length, 1)
120
- equals(firstPut.head.length, 1)
121
- equals(secondPut.head.length, 1)
122
- notEquals(firstPut.head[0], secondPut.head[0])
123
- })
124
- it('return the records on get', async function () {
125
- const { doc } = await crdt.get('ace')
126
- equals(doc.points, 11)
127
-
128
- for (const key of ['king', 'queen', 'jack']) {
129
- const { doc } = await crdt.get(key)
130
- equals(doc.points, 10)
131
- }
132
- })
133
- it('should offer changes', async function () {
134
- const { result } = await crdt.changes([])
135
- equals(result.length, 4)
136
- equals(result[0].key, 'ace')
137
- equals(result[0].value.points, 11)
138
- equals(result[1].key, 'king')
139
- equals(result[2].key, 'queen')
140
- equals(result[3].key, 'jack')
141
- })
142
- })
@@ -1,144 +0,0 @@
1
- /* eslint-disable mocha/max-top-level-suites */
2
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
3
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
- import { assert, equals, notEquals, matches } from './helpers.js'
6
- import { Database } from '../dist/database.esm.js'
7
- // import { Doc } from '../dist/types.d.esm.js'
8
-
9
- /**
10
- * @typedef {Object.<string, any>} DocBody
11
- */
12
-
13
- /**
14
- * @typedef {Object} Doc
15
- * @property {string} _id
16
- * @property {DocBody} [property] - an additional property
17
- */
18
-
19
- describe('basic Database', function () {
20
- /** @type {Database} */
21
- let db
22
- beforeEach(function () {
23
- db = new Database()
24
- })
25
- it('should put', async function () {
26
- /** @type {Doc} */
27
- const doc = { _id: 'hello', value: 'world' }
28
- const ok = await db.put(doc)
29
- equals(ok.id, 'hello')
30
- })
31
- it('get missing should throw', async function () {
32
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
33
- const e = await (db.get('missing')).catch(e => e)
34
- matches(e.message, /Not found/)
35
- })
36
- it('del missing should result in deleted state', async function () {
37
- await db.del('missing')
38
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
39
- const e = await (db.get('missing')).catch(e => e)
40
- matches(e.message, /Not found/)
41
- })
42
- it('has no changes', async function () {
43
- const { rows } = await db.changes([])
44
- equals(rows.length, 0)
45
- })
46
- })
47
-
48
- describe('basic Database with record', function () {
49
- /** @type {Database} */
50
- let db
51
- beforeEach(async function () {
52
- db = new Database()
53
- /** @type {Doc} */
54
- const doc = { _id: 'hello', value: 'world' }
55
- const ok = await db.put(doc)
56
- equals(ok.id, 'hello')
57
- })
58
- it('should get', async function () {
59
- const doc = await db.get('hello')
60
- assert(doc)
61
- equals(doc._id, 'hello')
62
- equals(doc.value, 'world')
63
- })
64
- it('should update', async function () {
65
- const ok = await db.put({ _id: 'hello', value: 'universe' })
66
- equals(ok.id, 'hello')
67
- const doc = await db.get('hello')
68
- assert(doc)
69
- equals(doc._id, 'hello')
70
- equals(doc.value, 'universe')
71
- })
72
- it('should del last record', async function () {
73
- const ok = await db.del('hello')
74
- equals(ok.id, 'hello')
75
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
76
- const e = await (db.get('hello')).catch(e => e)
77
- matches(e.message, /Not found/)
78
- })
79
- it('has changes', async function () {
80
- const { rows } = await db.changes([])
81
- equals(rows.length, 1)
82
- equals(rows[0].key, 'hello')
83
- equals(rows[0].value._id, 'hello')
84
- })
85
- })
86
-
87
- describe('basic Database parallel writes', function () {
88
- /** @type {Database} */
89
- let db
90
- const writes = []
91
- beforeEach(async function () {
92
- db = new Database()
93
- /** @type {Doc} */
94
- for (let i = 0; i < 10; i++) {
95
- const doc = { _id: `id-${i}`, hello: 'world' }
96
- writes.push(db.put(doc))
97
- }
98
- await Promise.all(writes)
99
- })
100
- it('should have one head', function () {
101
- const crdt = db._crdt
102
- equals(crdt._head.length, 1)
103
- })
104
- it('should write all', async function () {
105
- for (let i = 0; i < 10; i++) {
106
- const id = `id-${i}`
107
- const doc = await db.get(id)
108
- assert(doc)
109
- equals(doc._id, id)
110
- equals(doc.hello, 'world')
111
- }
112
- })
113
- it('should del all', async function () {
114
- for (let i = 0; i < 10; i++) {
115
- const id = `id-${i}`
116
- const ok = await db.del(id)
117
- equals(ok.id, id)
118
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
119
- const e = await (db.get(id)).catch(e => e)
120
- matches(e.message, /Not found/)
121
- }
122
- })
123
- it('should delete all in parallel', async function () {
124
- const deletes = []
125
- for (let i = 0; i < 10; i++) {
126
- const id = `id-${i}`
127
- deletes.push(db.del(id))
128
- }
129
- await Promise.all(deletes)
130
- for (let i = 0; i < 10; i++) {
131
- const id = `id-${i}`
132
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
133
- const e = await (db.get(id)).catch(e => e)
134
- matches(e.message, /Not found/)
135
- }
136
- })
137
- it('has changes', async function () {
138
- const { rows } = await db.changes([])
139
- equals(rows.length, 10)
140
- for (let i = 0; i < 10; i++) {
141
- equals(rows[i].key, 'id-' + i)
142
- }
143
- })
144
- })
@@ -1,50 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
3
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
4
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
- import { assert, equals, notEquals, matches, equalsJSON, resetDirectory } from './helpers.js'
6
-
7
- import { Fireproof } from '../dist/fireproof.esm.js'
8
- import { Database } from '../dist/database.esm.js'
9
- import { CarStoreFS, defaultConfig, HeaderStoreFS } from '../dist/store-fs.esm.js'
10
-
11
- describe('Reopening a database', function () {
12
- /** @type {Database} */
13
- let db
14
- beforeEach(async function () {
15
- // erase the existing test data
16
- await resetDirectory(defaultConfig.dataDir, 'test-reopen')
17
-
18
- db = Fireproof.storage('test-reopen')
19
- const ok = await db.put({ _id: 'test', foo: 'bar' })
20
- assert(ok)
21
- equals(ok.id, 'test')
22
-
23
- assert(db._crdt._head)
24
- equals(db._crdt._head.length, 1)
25
- })
26
-
27
- it('should persist data', async function () {
28
- const doc = await db.get('test')
29
- equals(doc.foo, 'bar')
30
- })
31
-
32
- it('should have the same data on reopen', async function () {
33
- const db2 = Fireproof.storage('test-reopen')
34
- const doc = await db2.get('test')
35
- equals(doc.foo, 'bar')
36
- assert(db2._crdt._head)
37
- equals(db2._crdt._head.length, 1)
38
- equalsJSON(db2._crdt._head, db._crdt._head)
39
- })
40
-
41
- it.skip('passing slow, should have the same data on reopen after reopen and update', async function () {
42
- for (let i = 0; i < 100; i++) {
43
- const db = Fireproof.storage('test-reopen')
44
- const ok = await db.put({ _id: `test${i}`, fire: 'proof'.repeat(50 * 1024) })
45
- assert(ok)
46
- const doc = await db.get(`test${i}`)
47
- equals(doc.fire, 'proof'.repeat(50 * 1024))
48
- }
49
- }).timeout(20000)
50
- })
package/test/globals.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare global {
2
- function describe(description: string, callback: () => void): void;
3
- function it(description: string, callback: () => void): void;
4
- }
@@ -1,9 +0,0 @@
1
- import assert from 'assert'
2
- import { Fireproof } from '../dist/fireproof.esm.js'
3
-
4
- describe('Hello World Test', function () {
5
- it('should pass the hello world test', function () {
6
- const result = Fireproof.storage('hello') // call to your library function
7
- assert(result.name === 'hello')
8
- })
9
- })
package/test/helpers.js DELETED
@@ -1,34 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
2
- import assert from 'assert'
3
- // import { join, dirname } from 'node:path'
4
- // import { mkdir, readdir, rm } from 'fs/promises'
5
-
6
- export { assert }
7
-
8
- export function equals(actual, expected) {
9
- assert(actual === expected, `Expected '${actual}' to equal '${expected}'`)
10
- }
11
-
12
- export function equalsJSON(actual, expected) {
13
- equals(JSON.stringify(actual), JSON.stringify(expected))
14
- }
15
-
16
- export function notEquals(actual, expected) {
17
- assert(actual !== expected, `Expected '${actual} 'to not equal '${expected}'`)
18
- }
19
-
20
- export function matches(actual, expected) {
21
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
22
- assert(actual.toString().match(expected), `Expected '${actual}' to match ${expected}`)
23
- }
24
-
25
- export async function resetDirectory(dir, name) {
26
- const path = join(dir, name)
27
- await mkdir(path, { recursive: true })
28
-
29
- const files = await readdir(path)
30
-
31
- for (const file of files) {
32
- await rm(join(path, file), { recursive: false, force: true })
33
- }
34
- }
@@ -1,112 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
- /* eslint-disable @typescript-eslint/no-unsafe-call */
3
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
- /* eslint-disable mocha/max-top-level-suites */
5
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
- import { assert, matches, equals, resetDirectory, notEquals } from './helpers.js'
7
-
8
- import { parseCarFile } from '../dist/loader-helpers.esm.js'
9
-
10
- import { Loader } from '../dist/loader.esm.js'
11
- import { CRDT } from '../dist/crdt.esm.js'
12
- import { TransactionBlockstore as Blockstore } from '../dist/transaction.esm.js'
13
-
14
- import { defaultConfig } from '../dist/store-fs.esm.js'
15
-
16
- describe('Loader with a committed transaction', function () {
17
- /** @type {Loader} */
18
- let loader, blockstore, crdt, done
19
- const dbname = 'test-loader'
20
- beforeEach(async function () {
21
- await resetDirectory(defaultConfig.dataDir, 'test-loader')
22
- loader = new Loader(dbname)
23
- blockstore = new Blockstore(dbname, loader)
24
- crdt = new CRDT(dbname, blockstore)
25
- done = await crdt.bulk([{ key: 'foo', value: { foo: 'bar' } }])
26
- })
27
- it('should have a name', function () {
28
- equals(loader.name, dbname)
29
- })
30
- it('should commit a transaction', function () {
31
- assert(done.head)
32
- assert(done.car)
33
- equals(blockstore.transactions.size, 1)
34
- equals(loader.carLog.length, 1)
35
- })
36
- it('can load the car', async function () {
37
- const reader = await loader.loadCar(done.car)
38
- assert(reader)
39
- const parsed = await parseCarFile(reader)
40
- assert(parsed.cars)
41
- equals(parsed.cars.length, 0)
42
- assert(parsed.head)
43
- })
44
- })
45
-
46
- describe('Loader with two committed transactions', function () {
47
- /** @type {Loader} */
48
- let loader, blockstore, crdt, done1, done2
49
- const dbname = 'test-loader'
50
- beforeEach(async function () {
51
- await resetDirectory(defaultConfig.dataDir, 'test-loader')
52
- loader = new Loader(dbname)
53
- blockstore = new Blockstore(dbname, loader)
54
- crdt = new CRDT(dbname, blockstore)
55
- done1 = await crdt.bulk([{ key: 'apple', value: { foo: 'bar' } }])
56
- done2 = await crdt.bulk([{ key: 'orange', value: { foo: 'bar' } }])
57
- })
58
- it('should commit two transactions', function () {
59
- assert(done1.head)
60
- assert(done1.car)
61
- assert(done2.head)
62
- assert(done2.car)
63
- notEquals(done1.head, done2.head)
64
- notEquals(done1.car, done2.car)
65
- equals(blockstore.transactions.size, 2)
66
- equals(loader.carLog.length, 2)
67
- equals(loader.carLog.indexOf(done1.car), 0)
68
- equals(loader.carLog.indexOf(done2.car), 1)
69
- })
70
- it('can load the car', async function () {
71
- const reader = await loader.loadCar(done2.car)
72
- assert(reader)
73
- const parsed = await parseCarFile(reader)
74
- assert(parsed.cars)
75
- equals(parsed.cars.length, 1)
76
- assert(parsed.head)
77
- })
78
- })
79
-
80
- describe('Loader with many committed transactions', function () {
81
- /** @type {Loader} */
82
- let loader, blockstore, crdt
83
- const dbname = 'test-loader'
84
- const dones = []
85
- const count = 10
86
- beforeEach(async function () {
87
- await resetDirectory(defaultConfig.dataDir, 'test-loader')
88
- loader = new Loader(dbname)
89
- blockstore = new Blockstore(dbname, loader)
90
- crdt = new CRDT(dbname, blockstore)
91
- for (let i = 0; i < count; i++) {
92
- const did = await crdt.bulk([{ key: `apple${i}`, value: { foo: 'bar' } }])
93
- dones.push(did)
94
- }
95
- })
96
- it('should commit many transactions', function () {
97
- for (const done of dones) {
98
- assert(done.head)
99
- assert(done.car)
100
- }
101
- equals(blockstore.transactions.size, count)
102
- equals(loader.carLog.length, count)
103
- })
104
- it('can load the car', async function () {
105
- const reader = await loader.loadCar(dones[5].car)
106
- assert(reader)
107
- const parsed = await parseCarFile(reader)
108
- assert(parsed.cars)
109
- equals(parsed.cars.length, 5)
110
- assert(parsed.head)
111
- })
112
- })