@web3-storage/pail 0.6.2 → 0.6.3-rc.0

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 (86) hide show
  1. package/package.json +1 -1
  2. package/dist/cli.d.ts +0 -3
  3. package/dist/cli.d.ts.map +0 -1
  4. package/dist/cli.js +0 -208
  5. package/dist/scripts/randomcid.d.ts +0 -2
  6. package/dist/scripts/randomcid.d.ts.map +0 -1
  7. package/dist/scripts/randomcid.js +0 -10
  8. package/dist/scripts/words/gen.d.ts +0 -2
  9. package/dist/scripts/words/gen.d.ts.map +0 -1
  10. package/dist/scripts/words/gen.js +0 -51
  11. package/dist/src/api.d.ts +0 -61
  12. package/dist/src/api.d.ts.map +0 -1
  13. package/dist/src/api.js +0 -1
  14. package/dist/src/batch/api.d.ts +0 -31
  15. package/dist/src/batch/api.d.ts.map +0 -1
  16. package/dist/src/batch/api.js +0 -1
  17. package/dist/src/batch/index.d.ts +0 -23
  18. package/dist/src/batch/index.d.ts.map +0 -1
  19. package/dist/src/batch/index.js +0 -241
  20. package/dist/src/batch/shard.d.ts +0 -3
  21. package/dist/src/batch/shard.d.ts.map +0 -1
  22. package/dist/src/batch/shard.js +0 -12
  23. package/dist/src/block.d.ts +0 -35
  24. package/dist/src/block.d.ts.map +0 -1
  25. package/dist/src/block.js +0 -66
  26. package/dist/src/clock/api.d.ts +0 -10
  27. package/dist/src/clock/api.d.ts.map +0 -1
  28. package/dist/src/clock/api.js +0 -1
  29. package/dist/src/clock/index.d.ts +0 -48
  30. package/dist/src/clock/index.d.ts.map +0 -1
  31. package/dist/src/clock/index.js +0 -199
  32. package/dist/src/crdt/api.d.ts +0 -26
  33. package/dist/src/crdt/api.d.ts.map +0 -1
  34. package/dist/src/crdt/api.js +0 -1
  35. package/dist/src/crdt/batch/api.d.ts +0 -11
  36. package/dist/src/crdt/batch/api.d.ts.map +0 -1
  37. package/dist/src/crdt/batch/api.js +0 -1
  38. package/dist/src/crdt/batch/index.d.ts +0 -5
  39. package/dist/src/crdt/batch/index.d.ts.map +0 -1
  40. package/dist/src/crdt/batch/index.js +0 -140
  41. package/dist/src/crdt/index.d.ts +0 -9
  42. package/dist/src/crdt/index.d.ts.map +0 -1
  43. package/dist/src/crdt/index.js +0 -344
  44. package/dist/src/diff.d.ts +0 -13
  45. package/dist/src/diff.d.ts.map +0 -1
  46. package/dist/src/diff.js +0 -151
  47. package/dist/src/index.d.ts +0 -10
  48. package/dist/src/index.d.ts.map +0 -1
  49. package/dist/src/index.js +0 -356
  50. package/dist/src/merge.d.ts +0 -5
  51. package/dist/src/merge.d.ts.map +0 -1
  52. package/dist/src/merge.js +0 -42
  53. package/dist/src/shard.d.ts +0 -43
  54. package/dist/src/shard.d.ts.map +0 -1
  55. package/dist/src/shard.js +0 -166
  56. package/dist/test/batch.test.d.ts +0 -2
  57. package/dist/test/batch.test.d.ts.map +0 -1
  58. package/dist/test/batch.test.js +0 -126
  59. package/dist/test/clock.test.d.ts +0 -2
  60. package/dist/test/clock.test.d.ts.map +0 -1
  61. package/dist/test/clock.test.js +0 -244
  62. package/dist/test/crdt.test.d.ts +0 -2
  63. package/dist/test/crdt.test.d.ts.map +0 -1
  64. package/dist/test/crdt.test.js +0 -271
  65. package/dist/test/del.test.d.ts +0 -2
  66. package/dist/test/del.test.d.ts.map +0 -1
  67. package/dist/test/del.test.js +0 -21
  68. package/dist/test/diff.test.d.ts +0 -2
  69. package/dist/test/diff.test.d.ts.map +0 -1
  70. package/dist/test/diff.test.js +0 -38
  71. package/dist/test/entries.test.d.ts +0 -2
  72. package/dist/test/entries.test.d.ts.map +0 -1
  73. package/dist/test/entries.test.js +0 -210
  74. package/dist/test/get.test.d.ts +0 -2
  75. package/dist/test/get.test.d.ts.map +0 -1
  76. package/dist/test/get.test.js +0 -26
  77. package/dist/test/helpers.d.ts +0 -35
  78. package/dist/test/helpers.d.ts.map +0 -1
  79. package/dist/test/helpers.js +0 -168
  80. package/dist/test/put.test.d.ts +0 -2
  81. package/dist/test/put.test.d.ts.map +0 -1
  82. package/dist/test/put.test.js +0 -165
  83. package/dist/tsconfig.tsbuildinfo +0 -1
  84. package/dist/vitest.config.d.ts +0 -3
  85. package/dist/vitest.config.d.ts.map +0 -1
  86. package/dist/vitest.config.js +0 -13
@@ -1,271 +0,0 @@
1
- import { expect } from 'vitest';
2
- // eslint-disable-next-line no-unused-vars
3
- import * as API from '../src/crdt/api.js';
4
- import { advance, vis } from '../src/clock/index.js';
5
- import { put, get, root, entries } from '../src/crdt/index.js';
6
- import * as Batch from '../src/crdt/batch/index.js';
7
- import { Blockstore, clockVis, randomCID, randomString } from './helpers.js';
8
- describe('CRDT', () => {
9
- it('put a value to a new clock', async () => {
10
- const blocks = new Blockstore();
11
- const alice = new TestPail(blocks, []);
12
- const key = 'key';
13
- const value = await randomCID(32);
14
- const { event, head } = await alice.put(key, value);
15
- await alice.vis();
16
- assert(event);
17
- assert(event?.value.data.type === 'put');
18
- assert.equal(event?.value.data.key, key);
19
- assert.equal(event?.value.data.value.toString(), value.toString());
20
- assert.equal(head.length, 1);
21
- assert.equal(head[0].toString(), event.cid.toString());
22
- });
23
- it('linear put multiple values', async () => {
24
- const blocks = new Blockstore();
25
- const alice = new TestPail(blocks, []);
26
- const key0 = 'key0';
27
- const value0 = await randomCID(32);
28
- await alice.put(key0, value0);
29
- const key1 = 'key1';
30
- const value1 = await randomCID(32);
31
- const result = await alice.put(key1, value1);
32
- await alice.vis();
33
- assert(result.event);
34
- assert(result.event.value.data.type === 'put');
35
- assert.equal(result.event.value.data.key, key1);
36
- assert.equal(result.event.value.data.value.toString(), value1.toString());
37
- assert.equal(result.head.length, 1);
38
- assert.equal(result.head[0].toString(), result.event.cid.toString());
39
- });
40
- it('simple parallel put multiple values', async () => {
41
- const blocks = new Blockstore();
42
- const alice = new TestPail(blocks, []);
43
- await alice.put('apple', await randomCID(32));
44
- const bob = new TestPail(blocks, alice.head);
45
- /** @type {Array<[string, API.UnknownLink]>} */
46
- const data = [
47
- ['banana', await randomCID(32)],
48
- ['kiwi', await randomCID(32)],
49
- ['mango', await randomCID(32)],
50
- ['orange', await randomCID(32)],
51
- ['pear', await randomCID(32)]
52
- ];
53
- const { event: aevent0 } = await alice.put(data[0][0], data[0][1]);
54
- const { event: bevent0 } = await bob.put(data[1][0], data[1][1]);
55
- assert(aevent0);
56
- assert(bevent0);
57
- const carol = new TestPail(blocks, bob.head);
58
- const { event: bevent1 } = await bob.put(data[2][0], data[2][1]);
59
- const { event: cevent1 } = await carol.put(data[3][0], data[3][1]);
60
- assert(bevent1);
61
- assert(cevent1);
62
- await alice.advance(cevent1.cid);
63
- await alice.advance(bevent0.cid);
64
- await alice.advance(bevent1.cid);
65
- await bob.advance(aevent0.cid);
66
- const { event: aevent1 } = await alice.put(data[4][0], data[4][1]);
67
- await alice.vis();
68
- assert(aevent1);
69
- await bob.advance(aevent1.cid);
70
- await carol.advance(aevent1.cid);
71
- assert(alice.root);
72
- assert(bob.root);
73
- assert(carol.root);
74
- assert.equal(bob.root.toString(), alice.root.toString());
75
- assert.equal(carol.root.toString(), alice.root.toString());
76
- // get item put to bob from alice
77
- const avalue = await alice.get(data[1][0]);
78
- assert(avalue);
79
- assert.equal(avalue.toString(), data[1][1].toString());
80
- // get item put to alice from bob
81
- const bvalue = await bob.get(data[0][0]);
82
- assert(bvalue);
83
- assert.equal(bvalue.toString(), data[0][1].toString());
84
- // get item put to alice from carol
85
- const cvalue = await bob.get(data[4][0]);
86
- assert(cvalue);
87
- assert.equal(cvalue.toString(), data[4][1].toString());
88
- });
89
- it('get from multi event head', async () => {
90
- const blocks = new Blockstore();
91
- const alice = new TestPail(blocks, []);
92
- await alice.put('apple', await randomCID(32));
93
- const bob = new TestPail(blocks, alice.head);
94
- /** @type {Array<[string, API.UnknownLink]>} */
95
- const data = [
96
- ['banana', await randomCID(32)],
97
- ['kiwi', await randomCID(32)]
98
- ];
99
- await alice.put(data[0][0], data[0][1]);
100
- const { event } = await bob.put(data[1][0], data[1][1]);
101
- assert(event);
102
- await alice.advance(event.cid);
103
- const value = await alice.get(data[1][0]);
104
- assert(value);
105
- assert.equal(value.toString(), data[1][1].toString());
106
- });
107
- it('entries from multi event head', async () => {
108
- const blocks = new Blockstore();
109
- const alice = new TestPail(blocks, []);
110
- await alice.put('apple', await randomCID(32));
111
- const bob = new TestPail(blocks, alice.head);
112
- /** @type {Array<[string, API.UnknownLink]>} */
113
- const data = [
114
- ['banana', await randomCID(32)],
115
- ['kiwi', await randomCID(32)]
116
- ];
117
- await alice.put(data[0][0], data[0][1]);
118
- const { event } = await bob.put(data[1][0], data[1][1]);
119
- assert(event);
120
- await alice.advance(event.cid);
121
- for await (const [k, v] of alice.entries()) {
122
- assert(v.toString(), new Map(data).get(k)?.toString());
123
- }
124
- });
125
- it('put same value to existing key', async () => {
126
- const blocks = new Blockstore();
127
- const alice = new TestPail(blocks, []);
128
- const key0 = 'key0';
129
- const value0 = await randomCID(32);
130
- const r0 = await alice.put(key0, value0);
131
- assert(r0.additions.map(s => s.cid.toString()).includes(r0.root.toString()));
132
- assert(!r0.removals.map(s => s.cid.toString()).includes(r0.root.toString()));
133
- const r1 = await alice.put(key0, value0);
134
- // nothing was added or removed
135
- assert.equal(r1.root.toString(), r0.root.toString());
136
- assert.equal(r1.additions.length, 0);
137
- assert.equal(r1.removals.length, 0);
138
- // no event should have been added to the clock
139
- assert.deepEqual(r1.head.map(cid => cid.toString()), r0.head.map(cid => cid.toString()));
140
- assert.equal(r1.event, undefined);
141
- });
142
- });
143
- describe('CRDT batch', () => {
144
- it('error when put after commit', async () => {
145
- const blocks = new Blockstore();
146
- const ops = [];
147
- for (let i = 0; i < 5; i++) {
148
- ops.push({ type: 'put', key: `test${randomString(10)}`, value: await randomCID() });
149
- }
150
- const batch = await Batch.create(blocks, []);
151
- for (const op of ops) {
152
- await batch.put(op.key, op.value);
153
- }
154
- await batch.commit();
155
- await expect(async () => batch.put('test', await randomCID())).rejects.toThrow(/batch already committed/);
156
- });
157
- it('error when commit after commit', async () => {
158
- const blocks = new Blockstore();
159
- const ops = [];
160
- for (let i = 0; i < 5; i++) {
161
- ops.push({ type: 'put', key: `test${randomString(10)}`, value: await randomCID() });
162
- }
163
- const batch = await Batch.create(blocks, []);
164
- for (const op of ops) {
165
- await batch.put(op.key, op.value);
166
- }
167
- await batch.commit();
168
- await expect(async () => batch.commit()).rejects.toThrow(/batch already committed/);
169
- });
170
- it('linear put with batch', async () => {
171
- const blocks = new Blockstore();
172
- const alice = new TestPail(blocks, []);
173
- const key0 = 'test0';
174
- const value0 = await randomCID(32);
175
- await alice.put(key0, value0);
176
- const ops = [];
177
- for (let i = 0; i < 25; i++) {
178
- ops.push({ type: 'put', key: `test${randomString(10)}`, value: await randomCID() });
179
- }
180
- await alice.putBatch(ops);
181
- // put a new value for the first batch key
182
- const key1 = ops[0].key;
183
- const value1 = await randomCID(32);
184
- await alice.put(key1, value1);
185
- await alice.vis();
186
- const res0 = await alice.get(key0);
187
- assert(res0);
188
- assert.equal(res0.toString(), value0.toString());
189
- for (const op of ops.slice(1)) {
190
- const res = await alice.get(op.key);
191
- assert(res);
192
- assert.equal(res.toString(), op.value.toString());
193
- }
194
- const res1 = await alice.get(key1);
195
- assert(res1);
196
- assert.equal(res1.toString(), value1.toString());
197
- });
198
- });
199
- class TestPail {
200
- /**
201
- * @param {Blockstore} blocks
202
- * @param {API.EventLink<API.Operation>[]} head
203
- */
204
- constructor(blocks, head) {
205
- this.blocks = blocks;
206
- this.head = head;
207
- /** @type {API.ShardLink?} */
208
- this.root = null;
209
- }
210
- /** @param {API.EventLink<API.Operation>} event */
211
- async advance(event) {
212
- this.head = await advance(this.blocks, this.head, event);
213
- const result = await root(this.blocks, this.head);
214
- result.additions.forEach(a => this.blocks.putSync(a.cid, a.bytes));
215
- this.root = result.root;
216
- return this.head;
217
- }
218
- /**
219
- * @param {string} key
220
- * @param {API.UnknownLink} value
221
- */
222
- async put(key, value) {
223
- const result = await put(this.blocks, this.head, key, value);
224
- if (result.event)
225
- this.blocks.putSync(result.event.cid, result.event.bytes);
226
- result.additions.forEach(a => this.blocks.putSync(a.cid, a.bytes));
227
- this.head = result.head;
228
- this.root = (await root(this.blocks, this.head)).root;
229
- return result;
230
- }
231
- /**
232
- * @param {Array<{ key: string, value: API.UnknownLink }>} items
233
- */
234
- async putBatch(items) {
235
- const batch = await Batch.create(this.blocks, this.head);
236
- for (const { key, value } of items) {
237
- await batch.put(key, value);
238
- }
239
- const result = await batch.commit();
240
- if (result.event)
241
- this.blocks.putSync(result.event.cid, result.event.bytes);
242
- result.additions.forEach(a => this.blocks.putSync(a.cid, a.bytes));
243
- this.head = result.head;
244
- this.root = (await root(this.blocks, this.head)).root;
245
- return result;
246
- }
247
- async vis() {
248
- /** @param {API.UnknownLink} l */
249
- const shortLink = l => `${String(l).slice(0, 4)}..${String(l).slice(-4)}`;
250
- /**
251
- * @param {API.PutOperation|API.DeleteOperation|API.BatchOperation} o
252
- * @returns {string}
253
- **/
254
- const renderOp = o => o.type === 'batch'
255
- ? `${o.ops.slice(0, 10).map(renderOp).join('\\n')}${o.ops.length > 10 ? `\\n...${o.ops.length - 10} more` : ''}`
256
- : `${o.type}(${o.key}${o.type === 'put'
257
- ? `, ${shortLink(o.value)}`
258
- : ''})`;
259
- /** @type {(e: API.EventBlockView<API.Operation>) => string} */
260
- const renderNodeLabel = event => `${shortLink(event.cid)}\\n${renderOp(event.value.data)}`;
261
- await clockVis(this.blocks, this.head, { renderNodeLabel });
262
- }
263
- /** @param {string} key */
264
- async get(key) {
265
- return get(this.blocks, this.head, key);
266
- }
267
- /** @param {API.EntriesOptions} [options] */
268
- async *entries(options) {
269
- yield* entries(this.blocks, this.head, options);
270
- }
271
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=del.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"del.test.d.ts","sourceRoot":"","sources":["../../test/del.test.js"],"names":[],"mappings":""}
@@ -1,21 +0,0 @@
1
- import { put, get, del } from '../src/index.js';
2
- import { ShardBlock } from '../src/shard.js';
3
- import { Blockstore, randomCID } from './helpers.js';
4
- describe('del', () => {
5
- it('deletes a value', async () => {
6
- const empty = await ShardBlock.create();
7
- const blocks = new Blockstore();
8
- await blocks.put(empty.cid, empty.bytes);
9
- const dataCID = await randomCID(32);
10
- const { root: root0, additions: additions0 } = await put(blocks, empty.cid, 'test', dataCID);
11
- for (const b of additions0) {
12
- await blocks.put(b.cid, b.bytes);
13
- }
14
- const { root: root1, additions: additions1 } = await del(blocks, root0, 'test');
15
- for (const b of additions1) {
16
- await blocks.put(b.cid, b.bytes);
17
- }
18
- const res = await get(blocks, root1, 'test');
19
- assert.strictEqual(res, undefined);
20
- });
21
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=diff.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"diff.test.d.ts","sourceRoot":"","sources":["../../test/diff.test.js"],"names":[],"mappings":""}
@@ -1,38 +0,0 @@
1
- // eslint-disable-next-line no-unused-vars
2
- import * as API from '../src/api.js';
3
- import { put } from '../src/index.js';
4
- import { ShardBlock } from '../src/shard.js';
5
- import { difference } from '../src/diff.js';
6
- import { Blockstore, randomCID } from './helpers.js';
7
- describe('diff', () => {
8
- it('diffs a non-sharded addition', async () => {
9
- const empty = await ShardBlock.create();
10
- const blocks = new Blockstore();
11
- await blocks.put(empty.cid, empty.bytes);
12
- /** @type {Array<[string, API.UnknownLink]>} */
13
- const testdata = [
14
- ['a', await randomCID(32)]
15
- ];
16
- /** @type {API.ShardLink} */
17
- let root = empty.cid;
18
- for (const [k, v] of testdata) {
19
- const res = await put(blocks, root, k, v);
20
- for (const b of res.additions) {
21
- await blocks.put(b.cid, b.bytes);
22
- }
23
- root = res.root;
24
- }
25
- const diff = await difference(blocks, empty.cid, root);
26
- assert.equal(diff.shards.removals.length, 1);
27
- assert.equal(diff.shards.removals[0].cid.toString(), empty.cid.toString());
28
- assert.equal(diff.shards.additions.length, 1);
29
- assert.equal(diff.shards.additions[0].cid.toString(), root.toString());
30
- assert.equal(diff.keys.length, testdata.length);
31
- for (const [k, v] of testdata) {
32
- const d = diff.keys.find(p => p[0] === k);
33
- assert(d);
34
- assert.equal(d[1][0], null);
35
- assert.equal(d[1][1]?.toString(), v.toString());
36
- }
37
- });
38
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=entries.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"entries.test.d.ts","sourceRoot":"","sources":["../../test/entries.test.js"],"names":[],"mappings":""}
@@ -1,210 +0,0 @@
1
- // eslint-disable-next-line no-unused-vars
2
- import * as API from '../src/api.js';
3
- import { put, entries } from '../src/index.js';
4
- import { ShardBlock } from '../src/shard.js';
5
- import { Blockstore, randomCID } from './helpers.js';
6
- describe('entries', () => {
7
- it('lists entries in lexicographical order', async () => {
8
- const empty = await ShardBlock.create();
9
- const blocks = new Blockstore();
10
- await blocks.put(empty.cid, empty.bytes);
11
- /** @type {Array<[string, API.UnknownLink]>} */
12
- const testdata = [
13
- ['c', await randomCID(32)],
14
- ['d', await randomCID(32)],
15
- ['a', await randomCID(32)],
16
- ['b', await randomCID(32)]
17
- ];
18
- /** @type {API.ShardLink} */
19
- let root = empty.cid;
20
- for (const [k, v] of testdata) {
21
- const res = await put(blocks, root, k, v);
22
- for (const b of res.additions) {
23
- await blocks.put(b.cid, b.bytes);
24
- }
25
- root = res.root;
26
- }
27
- const results = [];
28
- for await (const entry of entries(blocks, root)) {
29
- results.push(entry);
30
- }
31
- for (const [i, key] of testdata.map(d => d[0]).sort().entries()) {
32
- assert.equal(results[i][0], key);
33
- }
34
- });
35
- it('lists entries by prefix', async () => {
36
- const empty = await ShardBlock.create();
37
- const blocks = new Blockstore();
38
- await blocks.put(empty.cid, empty.bytes);
39
- /** @type {Array<[string, API.UnknownLink]>} */
40
- const testdata = [
41
- ['cccc', await randomCID(32)],
42
- ['deee', await randomCID(32)],
43
- ['dooo', await randomCID(32)],
44
- ['beee', await randomCID(32)]
45
- ];
46
- /** @type {API.ShardLink} */
47
- let root = empty.cid;
48
- for (const [k, v] of testdata) {
49
- const res = await put(blocks, root, k, v);
50
- for (const b of res.additions) {
51
- await blocks.put(b.cid, b.bytes);
52
- }
53
- root = res.root;
54
- }
55
- const prefix = 'd';
56
- const results = [];
57
- for await (const entry of entries(blocks, root, { prefix })) {
58
- results.push(entry);
59
- }
60
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k.startsWith(prefix)).sort().entries()) {
61
- assert.equal(results[i][0], key);
62
- }
63
- });
64
- it('lists entries by key greater than string', async () => {
65
- const empty = await ShardBlock.create();
66
- const blocks = new Blockstore();
67
- await blocks.put(empty.cid, empty.bytes);
68
- /** @type {Array<[string, API.UnknownLink]>} */
69
- const testdata = [
70
- ['cccc', await randomCID(32)],
71
- ['deee', await randomCID(32)],
72
- ['dooo', await randomCID(32)],
73
- ['beee', await randomCID(32)]
74
- ];
75
- /** @type {API.ShardLink} */
76
- let root = empty.cid;
77
- for (const [k, v] of testdata) {
78
- const res = await put(blocks, root, k, v);
79
- for (const b of res.additions) {
80
- await blocks.put(b.cid, b.bytes);
81
- }
82
- root = res.root;
83
- }
84
- const gt = 'beee';
85
- const results = [];
86
- for await (const entry of entries(blocks, root, { gt })) {
87
- results.push(entry);
88
- }
89
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k > gt).sort().entries()) {
90
- assert.equal(results[i][0], key);
91
- }
92
- });
93
- it('lists entries by key greater than or equal to string', async () => {
94
- const empty = await ShardBlock.create();
95
- const blocks = new Blockstore();
96
- await blocks.put(empty.cid, empty.bytes);
97
- /** @type {Array<[string, API.UnknownLink]>} */
98
- const testdata = [
99
- ['cccc', await randomCID(32)],
100
- ['deee', await randomCID(32)],
101
- ['dooo', await randomCID(32)],
102
- ['beee', await randomCID(32)]
103
- ];
104
- /** @type {API.ShardLink} */
105
- let root = empty.cid;
106
- for (const [k, v] of testdata) {
107
- const res = await put(blocks, root, k, v);
108
- for (const b of res.additions) {
109
- await blocks.put(b.cid, b.bytes);
110
- }
111
- root = res.root;
112
- }
113
- const gte = 'beee';
114
- const results = [];
115
- for await (const entry of entries(blocks, root, { gte })) {
116
- results.push(entry);
117
- }
118
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k >= gte).sort().entries()) {
119
- assert.equal(results[i][0], key);
120
- }
121
- });
122
- it('lists entries by key less than string', async () => {
123
- const empty = await ShardBlock.create();
124
- const blocks = new Blockstore();
125
- await blocks.put(empty.cid, empty.bytes);
126
- /** @type {Array<[string, API.UnknownLink]>} */
127
- const testdata = [
128
- ['cccc', await randomCID(32)],
129
- ['deee', await randomCID(32)],
130
- ['dooo', await randomCID(32)],
131
- ['beee', await randomCID(32)]
132
- ];
133
- /** @type {API.ShardLink} */
134
- let root = empty.cid;
135
- for (const [k, v] of testdata) {
136
- const res = await put(blocks, root, k, v);
137
- for (const b of res.additions) {
138
- await blocks.put(b.cid, b.bytes);
139
- }
140
- root = res.root;
141
- }
142
- const lt = 'doo';
143
- const results = [];
144
- for await (const entry of entries(blocks, root, { lt })) {
145
- results.push(entry);
146
- }
147
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k < lt).sort().entries()) {
148
- assert.equal(results[i][0], key);
149
- }
150
- });
151
- it('lists entries by key less than or equal to string', async () => {
152
- const empty = await ShardBlock.create();
153
- const blocks = new Blockstore();
154
- await blocks.put(empty.cid, empty.bytes);
155
- /** @type {Array<[string, API.UnknownLink]>} */
156
- const testdata = [
157
- ['cccc', await randomCID(32)],
158
- ['deee', await randomCID(32)],
159
- ['dooo', await randomCID(32)],
160
- ['beee', await randomCID(32)]
161
- ];
162
- /** @type {API.ShardLink} */
163
- let root = empty.cid;
164
- for (const [k, v] of testdata) {
165
- const res = await put(blocks, root, k, v);
166
- for (const b of res.additions) {
167
- await blocks.put(b.cid, b.bytes);
168
- }
169
- root = res.root;
170
- }
171
- const lte = 'dooo';
172
- const results = [];
173
- for await (const entry of entries(blocks, root, { lte })) {
174
- results.push(entry);
175
- }
176
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k <= lte).sort().entries()) {
177
- assert.equal(results[i][0], key);
178
- }
179
- });
180
- it('lists entries by key greater than and less than or equal to string', async () => {
181
- const empty = await ShardBlock.create();
182
- const blocks = new Blockstore();
183
- await blocks.put(empty.cid, empty.bytes);
184
- /** @type {Array<[string, API.UnknownLink]>} */
185
- const testdata = [
186
- ['cccc', await randomCID(32)],
187
- ['deee', await randomCID(32)],
188
- ['dooo', await randomCID(32)],
189
- ['beee', await randomCID(32)]
190
- ];
191
- /** @type {API.ShardLink} */
192
- let root = empty.cid;
193
- for (const [k, v] of testdata) {
194
- const res = await put(blocks, root, k, v);
195
- for (const b of res.additions) {
196
- await blocks.put(b.cid, b.bytes);
197
- }
198
- root = res.root;
199
- }
200
- const gt = 'c';
201
- const lte = 'deee';
202
- const results = [];
203
- for await (const entry of entries(blocks, root, { gt, lte })) {
204
- results.push(entry);
205
- }
206
- for (const [i, key] of testdata.map(d => d[0]).filter(k => k > gt && k <= lte).sort().entries()) {
207
- assert.equal(results[i][0], key);
208
- }
209
- });
210
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=get.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get.test.d.ts","sourceRoot":"","sources":["../../test/get.test.js"],"names":[],"mappings":""}
@@ -1,26 +0,0 @@
1
- import { put, get } from '../src/index.js';
2
- import { ShardBlock } from '../src/shard.js';
3
- import { Blockstore, randomCID } from './helpers.js';
4
- describe('get', () => {
5
- it('get from root shard', async () => {
6
- const empty = await ShardBlock.create();
7
- const blocks = new Blockstore();
8
- await blocks.put(empty.cid, empty.bytes);
9
- const dataCID = await randomCID(32);
10
- const { root, additions } = await put(blocks, empty.cid, 'test', dataCID);
11
- for (const b of additions) {
12
- await blocks.put(b.cid, b.bytes);
13
- }
14
- const res = await get(blocks, root, 'test');
15
- assert(res);
16
- assert(res.toString(), dataCID.toString());
17
- });
18
- it('returns undefined when not found', async () => {
19
- const empty = await ShardBlock.create();
20
- const blocks = new Blockstore();
21
- await blocks.put(empty.cid, empty.bytes);
22
- const res = await get(blocks, empty.cid, 'test');
23
- assert.strictEqual(res, undefined);
24
- });
25
- // TODO: test get when key is also shard link
26
- });
@@ -1,35 +0,0 @@
1
- /**
2
- * @param {number} min
3
- * @param {number} max
4
- */
5
- export function randomInteger(min: number, max: number): number;
6
- /**
7
- * @param {number} size
8
- */
9
- export function randomString(size: number, alphabet?: string): string;
10
- /** @param {number} [size] Number of random bytes to hash. */
11
- export function randomCID(size?: number): Promise<Link.Link<any, 85, 18, 1>>;
12
- /**
13
- * @param {number} size
14
- */
15
- export function randomBytes(size: number): Promise<Uint8Array<ArrayBuffer>>;
16
- export class Blockstore extends MemoryBlockstore {
17
- /**
18
- * @param {import('../src/api.js').ShardLink} cid
19
- */
20
- getShardBlock(cid: import("../src/api.js").ShardLink): Promise<API.ShardBlockView>;
21
- }
22
- export function vis(blocks: API.BlockFetcher, root: API.ShardLink): Promise<void>;
23
- export function materialize(blocks: API.BlockFetcher, root: API.ShardLink): Promise<any[]>;
24
- export function putAll(blocks: Blockstore, root: API.ShardLink, items: Array<[string, API.UnknownLink]>): Promise<{
25
- root: API.ShardLink;
26
- }>;
27
- export function verify(blocks: MemoryBlockstore, root: API.ShardLink, data: Map<string, API.UnknownLink>): Promise<void>;
28
- export function clockVis<T>(blocks: API.BlockFetcher, head: ClockAPI.EventLink<T>[], options?: {
29
- renderNodeLabel?: ((b: ClockAPI.EventBlockView<T>) => string) | undefined;
30
- }): Promise<void>;
31
- import * as Link from 'multiformats/link';
32
- import { MemoryBlockstore } from '../src/block.js';
33
- import * as API from '../src/api.js';
34
- import * as ClockAPI from '../src/clock/api.js';
35
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../test/helpers.js"],"names":[],"mappings":"AAcA;;;GAGG;AACH,mCAHW,MAAM,OACN,MAAM,UAMhB;AAID;;GAEG;AACH,mCAFW,MAAM,6BAQhB;AAED,6DAA6D;AAC7D,iCADY,MAAM,sCAIjB;AAyGD;;GAEG;AACH,kCAFW,MAAM,oCAahB;AArHD;IACE;;OAEG;IACH,mBAFW,OAAO,eAAe,EAAE,SAAS,+BAM3C;CACF;AAMM,4BAHI,GAAG,CAAC,YAAY,QAChB,GAAG,CAAC,SAAS,iBA8BvB;AAMM,oCAHI,GAAG,CAAC,YAAY,QAChB,GAAG,CAAC,SAAS,kBAkBvB;AAOM,+BAJI,UAAU,QACV,GAAG,CAAC,SAAS,SACb,KAAK,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;;GAc1C;AAOM,+BAJI,gBAAgB,QAChB,GAAG,CAAC,SAAS,QACb,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,WAAW,CAAC,iBAYtC;AAyBM,yBANM,CAAC,UACH,GAAG,CAAC,YAAY,QAChB,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,YAE/B;IAA4D,eAAe,QAA/D,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,MAAM;CAA2B,iBAQ9E;sBA/KqB,mBAAmB;iCAUR,iBAAiB;qBAJ7B,eAAe;0BAEV,qBAAqB"}