@fireproof/vendor 2.0.2 → 3.0.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 (73) hide show
  1. package/package.json +6 -60
  2. package/src/p-limit/index.d.ts +81 -0
  3. package/src/p-limit/index.js +104 -0
  4. package/src/p-limit/license +9 -0
  5. package/src/p-limit/readme.md +129 -0
  6. package/src/@web3-storage/pail/LICENSE.md +0 -232
  7. package/src/@web3-storage/pail/README.md +0 -84
  8. package/src/@web3-storage/pail/cli.js +0 -218
  9. package/src/@web3-storage/pail/dist/src/api.d.ts +0 -61
  10. package/src/@web3-storage/pail/dist/src/api.d.ts.map +0 -1
  11. package/src/@web3-storage/pail/dist/src/api.js +0 -1
  12. package/src/@web3-storage/pail/dist/src/batch/api.d.ts +0 -31
  13. package/src/@web3-storage/pail/dist/src/batch/api.d.ts.map +0 -1
  14. package/src/@web3-storage/pail/dist/src/batch/api.js +0 -1
  15. package/src/@web3-storage/pail/dist/src/batch/index.d.ts +0 -23
  16. package/src/@web3-storage/pail/dist/src/batch/index.d.ts.map +0 -1
  17. package/src/@web3-storage/pail/dist/src/batch/index.js +0 -241
  18. package/src/@web3-storage/pail/dist/src/batch/shard.d.ts +0 -3
  19. package/src/@web3-storage/pail/dist/src/batch/shard.d.ts.map +0 -1
  20. package/src/@web3-storage/pail/dist/src/batch/shard.js +0 -12
  21. package/src/@web3-storage/pail/dist/src/block.d.ts +0 -35
  22. package/src/@web3-storage/pail/dist/src/block.d.ts.map +0 -1
  23. package/src/@web3-storage/pail/dist/src/block.js +0 -66
  24. package/src/@web3-storage/pail/dist/src/clock/api.d.ts +0 -10
  25. package/src/@web3-storage/pail/dist/src/clock/api.d.ts.map +0 -1
  26. package/src/@web3-storage/pail/dist/src/clock/api.js +0 -1
  27. package/src/@web3-storage/pail/dist/src/clock/index.d.ts +0 -48
  28. package/src/@web3-storage/pail/dist/src/clock/index.d.ts.map +0 -1
  29. package/src/@web3-storage/pail/dist/src/clock/index.js +0 -178
  30. package/src/@web3-storage/pail/dist/src/crdt/api.d.ts +0 -26
  31. package/src/@web3-storage/pail/dist/src/crdt/api.d.ts.map +0 -1
  32. package/src/@web3-storage/pail/dist/src/crdt/api.js +0 -1
  33. package/src/@web3-storage/pail/dist/src/crdt/batch/api.d.ts +0 -11
  34. package/src/@web3-storage/pail/dist/src/crdt/batch/api.d.ts.map +0 -1
  35. package/src/@web3-storage/pail/dist/src/crdt/batch/api.js +0 -1
  36. package/src/@web3-storage/pail/dist/src/crdt/batch/index.d.ts +0 -5
  37. package/src/@web3-storage/pail/dist/src/crdt/batch/index.d.ts.map +0 -1
  38. package/src/@web3-storage/pail/dist/src/crdt/batch/index.js +0 -140
  39. package/src/@web3-storage/pail/dist/src/crdt/index.d.ts +0 -9
  40. package/src/@web3-storage/pail/dist/src/crdt/index.d.ts.map +0 -1
  41. package/src/@web3-storage/pail/dist/src/crdt/index.js +0 -344
  42. package/src/@web3-storage/pail/dist/src/diff.d.ts +0 -13
  43. package/src/@web3-storage/pail/dist/src/diff.d.ts.map +0 -1
  44. package/src/@web3-storage/pail/dist/src/diff.js +0 -151
  45. package/src/@web3-storage/pail/dist/src/index.d.ts +0 -10
  46. package/src/@web3-storage/pail/dist/src/index.d.ts.map +0 -1
  47. package/src/@web3-storage/pail/dist/src/index.js +0 -356
  48. package/src/@web3-storage/pail/dist/src/merge.d.ts +0 -5
  49. package/src/@web3-storage/pail/dist/src/merge.d.ts.map +0 -1
  50. package/src/@web3-storage/pail/dist/src/merge.js +0 -42
  51. package/src/@web3-storage/pail/dist/src/shard.d.ts +0 -43
  52. package/src/@web3-storage/pail/dist/src/shard.d.ts.map +0 -1
  53. package/src/@web3-storage/pail/dist/src/shard.js +0 -166
  54. package/src/@web3-storage/pail/dist/tsconfig.tsbuildinfo +0 -1
  55. package/src/@web3-storage/pail/src/api.ts +0 -92
  56. package/src/@web3-storage/pail/src/batch/api.js +0 -1
  57. package/src/@web3-storage/pail/src/batch/api.ts +0 -59
  58. package/src/@web3-storage/pail/src/batch/index.js +0 -258
  59. package/src/@web3-storage/pail/src/batch/shard.js +0 -13
  60. package/src/@web3-storage/pail/src/block.js +0 -75
  61. package/src/@web3-storage/pail/src/clock/api.js +0 -1
  62. package/src/@web3-storage/pail/src/clock/api.ts +0 -12
  63. package/src/@web3-storage/pail/src/clock/index.js +0 -182
  64. package/src/@web3-storage/pail/src/crdt/api.js +0 -1
  65. package/src/@web3-storage/pail/src/crdt/api.ts +0 -33
  66. package/src/@web3-storage/pail/src/crdt/batch/api.js +0 -1
  67. package/src/@web3-storage/pail/src/crdt/batch/api.ts +0 -30
  68. package/src/@web3-storage/pail/src/crdt/batch/index.js +0 -155
  69. package/src/@web3-storage/pail/src/crdt/index.js +0 -354
  70. package/src/@web3-storage/pail/src/diff.js +0 -151
  71. package/src/@web3-storage/pail/src/index.js +0 -406
  72. package/src/@web3-storage/pail/src/merge.js +0 -43
  73. package/src/@web3-storage/pail/src/shard.js +0 -180
@@ -1,84 +0,0 @@
1
- # pail
2
-
3
- [![Test](https://github.com/web3-storage/pail/actions/workflows/ci.yml/badge.svg)](https://github.com/web3-storage/pail/actions/workflows/ci.yml)
4
- [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
5
-
6
- DAG based key value store. Sharded DAG that minimises traversals and work to build shards.
7
-
8
- * 📖 [Read the SPEC](https://github.com/web3-storage/specs/blob/4163e28d7e6a7c44cff68db9d9bffb9b37707dc6/pail.md).
9
- * 🎬 [Watch the Presentation](https://youtu.be/f-BrtpYKZfg).
10
-
11
- ## Install
12
-
13
- ```
14
- npm install @web3-storage/pail
15
- ```
16
-
17
- ## Usage
18
-
19
- ```js
20
- import { put, get, del } from '@web3-storage/pail'
21
- import { ShardBlock } from '@web3-storage/pail/shard'
22
- import { MemoryBlockstore } from '@web3-storage/pail/block'
23
-
24
- // Initialize a new bucket
25
- const blocks = new MemoryBlockstore()
26
- const init = await ShardBlock.create() // empty root shard
27
- await blocks.put(init.cid, init.bytes)
28
-
29
- // Add a key and value to the bucket
30
- const { root, additions, removals } = await put(blocks, init.cid, 'path/to/data0', dataCID0)
31
-
32
- console.log(`new root: ${root}`)
33
-
34
- // Process the diff
35
- for (const block of additions) {
36
- await blocks.put(block.cid, block.bytes)
37
- }
38
- for (const block of removals) {
39
- await blocks.delete(block.cid)
40
- }
41
- ```
42
-
43
- ### Batch operations
44
-
45
- If adding many multiple items to the pail together, it is faster to batch them together.
46
-
47
- ```js
48
- import { put, get, del } from '@web3-storage/pail'
49
- import { ShardBlock } from '@web3-storage/pail/shard'
50
- import { MemoryBlockstore } from '@web3-storage/pail/block'
51
- import * as Batch from '@web3-storage/pail/batch'
52
-
53
- // Initialize a new bucket
54
- const blocks = new MemoryBlockstore()
55
- const init = await ShardBlock.create() // empty root shard
56
- await blocks.put(init.cid, init.bytes)
57
-
58
- const batch = await Batch.create(blocks, init.cid)
59
-
60
- // items is an array of `{ key: string, value: CID }` - the items to add to the pail
61
- for (const item of items) {
62
- await batch.put(item.key, item.value)
63
- }
64
-
65
- const { root, additions, removals } = await batch.commit()
66
-
67
- console.log(`new root: ${root}`)
68
-
69
- // Process the diff
70
- for (const block of additions) {
71
- await blocks.put(block.cid, block.bytes)
72
- }
73
- for (const block of removals) {
74
- await blocks.delete(block.cid)
75
- }
76
- ```
77
-
78
- ## Contributing
79
-
80
- Feel free to join in. All welcome. [Open an issue](https://github.com/web3-storage/pail/issues)!
81
-
82
- ## License
83
-
84
- Dual-licensed under [MIT or Apache 2.0](https://github.com/web3-storage/pail/blob/main/LICENSE.md)
@@ -1,218 +0,0 @@
1
- #!/usr/bin/env node
2
- import fs from 'fs'
3
- import os from 'os'
4
- import { join } from 'path'
5
- import { Readable, Writable } from 'stream'
6
- import sade from 'sade'
7
- import { CID } from 'multiformats/cid'
8
- import { CARReaderStream, CARWriterStream } from 'carstream'
9
- import clc from 'cli-color'
10
- import archy from 'archy'
11
- // eslint-disable-next-line no-unused-vars
12
- import * as API from './src/api.js'
13
- import { put, get, del, entries } from './src/index.js'
14
- import { ShardFetcher, ShardBlock, isShardLink } from './src/shard.js'
15
- import { difference } from './src/diff.js'
16
- import { merge } from './src/merge.js'
17
- import { MemoryBlockstore, MultiBlockFetcher } from './src/block.js'
18
-
19
- const cli = sade('pail')
20
- .option('--path', 'Path to data store.', './pail.car')
21
-
22
- cli.command('put <key> <value>')
23
- .describe('Put a value (a CID) for the given key. If the key exists it\'s value is overwritten.')
24
- .alias('set')
25
- .action(async (key, value, opts) => {
26
- const { root: prevRoot, blocks } = await openPail(opts.path)
27
- const { root, additions, removals } = await put(blocks, prevRoot, key, CID.parse(value))
28
- await updatePail(opts.path, blocks, root, { additions, removals })
29
-
30
- console.log(clc.red(`--- ${prevRoot}`))
31
- console.log(clc.green(`+++ ${root}`))
32
- console.log(clc.magenta('@@ -1 +1 @@'))
33
- additions.forEach(b => console.log(clc.green(`+${b.cid}`)))
34
- removals.forEach(b => console.log(clc.red(`-${b.cid}`)))
35
- })
36
-
37
- cli.command('get <key>')
38
- .describe('Get the stored value for the given key from the pail. If the key is not found, `undefined` is returned.')
39
- .action(async (key, opts) => {
40
- const { root, blocks } = await openPail(opts.path)
41
- const value = await get(blocks, root, key)
42
- if (value) console.log(value.toString())
43
- })
44
-
45
- cli.command('del <key>')
46
- .describe('Delete the value for the given key from the pail. If the key is not found no operation occurs.')
47
- .alias('delete', 'rm', 'remove')
48
- .action(async (key, opts) => {
49
- const { root: prevRoot, blocks } = await openPail(opts.path)
50
- const { root, additions, removals } = await del(blocks, prevRoot, key)
51
- await updatePail(opts.path, blocks, root, { additions, removals })
52
-
53
- console.log(clc.red(`--- ${prevRoot}`))
54
- console.log(clc.green(`+++ ${root}`))
55
- console.log(clc.magenta('@@ -1 +1 @@'))
56
- additions.forEach(b => console.log(clc.green(`+ ${b.cid}`)))
57
- removals.forEach(b => console.log(clc.red(`- ${b.cid}`)))
58
- })
59
-
60
- cli.command('ls')
61
- .describe('List entries in the pail.')
62
- .alias('list')
63
- .option('-p, --prefix', 'Key prefix to filter by.')
64
- .option('--gt', 'Filter results by keys greater than this string.')
65
- .option('--lt', 'Filter results by keys less than this string.')
66
- .option('--json', 'Format output as newline delimted JSON.')
67
- .action(async (opts) => {
68
- const { root, blocks } = await openPail(opts.path)
69
- let n = 0
70
- for await (const [k, v] of entries(blocks, root, { prefix: opts.prefix, gt: opts.gt, lt: opts.lt })) {
71
- console.log(opts.json ? JSON.stringify({ key: k, value: v.toString() }) : `${k}\t${v}`)
72
- n++
73
- }
74
- if (!opts.json) console.log(`total ${n}`)
75
- })
76
-
77
- cli.command('tree')
78
- .describe('Visualise the pail.')
79
- .alias('vis')
80
- .action(async (opts) => {
81
- const { root, blocks } = await openPail(opts.path)
82
- const shards = new ShardFetcher(blocks)
83
- const rshard = await shards.get(root)
84
-
85
- /** @type {archy.Data} */
86
- const archyRoot = { label: `Shard(${clc.yellow(rshard.cid.toString())}) ${rshard.bytes.length + 'b'}`, nodes: [] }
87
-
88
- /** @param {API.ShardEntry} entry */
89
- const getData = async ([k, v]) => {
90
- if (!Array.isArray(v)) {
91
- return { label: `Key(${clc.magenta(k)})`, nodes: [{ label: `Value(${clc.cyan(v)})` }] }
92
- }
93
- /** @type {archy.Data} */
94
- const data = { label: `Key(${clc.magenta(k)})`, nodes: [] }
95
- if (v[1]) data.nodes?.push({ label: `Value(${clc.cyan(v[1])})` })
96
- const blk = await shards.get(v[0])
97
- data.nodes?.push({
98
- label: `Shard(${clc.yellow(v[0])}) ${blk.bytes.length + 'b'}`,
99
- nodes: await Promise.all(blk.value.entries.map(e => getData(e)))
100
- })
101
- return data
102
- }
103
-
104
- for (const entry of rshard.value.entries) {
105
- archyRoot.nodes?.push(await getData(entry))
106
- }
107
-
108
- console.log(archy(archyRoot))
109
- })
110
-
111
- cli.command('diff <path>')
112
- .describe('Find the differences between this pail and the passed pail.')
113
- .option('-k, --keys', 'Output key/value diff.')
114
- .action(async (path, opts) => {
115
- const [
116
- { root: aroot, blocks: ablocks },
117
- { root: broot, blocks: bblocks }
118
- ] = await Promise.all([openPail(opts.path), openPail(path)])
119
- if (aroot.toString() === broot.toString()) return
120
-
121
- const fetcher = new MultiBlockFetcher(ablocks, bblocks)
122
- const { shards: { additions, removals }, keys } = await difference(fetcher, aroot, broot)
123
-
124
- console.log(clc.red(`--- ${aroot}`))
125
- console.log(clc.green(`+++ ${broot}`))
126
- console.log(clc.magenta('@@ -1 +1 @@'))
127
-
128
- if (opts.keys) {
129
- keys.forEach(([k, v]) => {
130
- if (v[0] != null) console.log(clc.red(`- ${k}\t${v[0]}`))
131
- if (v[1] != null) console.log(clc.green(`+ ${k}\t${v[1]}`))
132
- })
133
- } else {
134
- additions.forEach(b => console.log(clc.green(`+ ${b.cid}`)))
135
- removals.forEach(b => console.log(clc.red(`- ${b.cid}`)))
136
- }
137
- })
138
-
139
- cli.command('merge <path>')
140
- .describe('Merge the passed pail into this pail.')
141
- .action(async (path, opts) => {
142
- const [
143
- { root: aroot, blocks: ablocks },
144
- { root: broot, blocks: bblocks }
145
- ] = await Promise.all([openPail(opts.path), openPail(path)])
146
- if (aroot.toString() === broot.toString()) return
147
-
148
- const fetcher = new MultiBlockFetcher(ablocks, bblocks)
149
- const { root, additions, removals } = await merge(fetcher, aroot, [broot])
150
-
151
- await updatePail(opts.path, ablocks, root, { additions, removals })
152
-
153
- console.log(clc.red(`--- ${aroot}`))
154
- console.log(clc.green(`+++ ${root}`))
155
- console.log(clc.magenta('@@ -1 +1 @@'))
156
- additions.forEach(b => console.log(clc.green(`+ ${b.cid}`)))
157
- removals.forEach(b => console.log(clc.red(`- ${b.cid}`)))
158
- })
159
-
160
- cli.parse(process.argv)
161
-
162
- /**
163
- * @param {string} path
164
- * @returns {Promise<{ root: API.ShardLink, blocks: MemoryBlockstore }>}
165
- */
166
- async function openPail (path) {
167
- const blocks = new MemoryBlockstore()
168
- try {
169
- const carReader = new CARReaderStream()
170
- const readable = /** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(fs.createReadStream(path)))
171
- await readable.pipeThrough(carReader).pipeTo(new WritableStream({ write: b => blocks.put(b.cid, b.bytes) }))
172
- const header = await carReader.getHeader()
173
- if (!isShardLink(header.roots[0])) throw new Error(`not a shard: ${header.roots[0]}`)
174
- return { root: header.roots[0], blocks }
175
- } catch (err) {
176
- if (err.code !== 'ENOENT') throw new Error('failed to open bucket', { cause: err })
177
- const rootblk = await ShardBlock.create()
178
- blocks.put(rootblk.cid, rootblk.bytes)
179
- return { root: rootblk.cid, blocks }
180
- }
181
- }
182
-
183
- /**
184
- * @param {string} path
185
- * @param {MemoryBlockstore} blocks
186
- * @param {API.ShardLink} root
187
- * @param {API.ShardDiff} diff
188
- */
189
- async function updatePail (path, blocks, root, { additions, removals }) {
190
- const tmp = join(os.tmpdir(), `pail${Date.now()}.car`)
191
- const iterator = blocks.entries()
192
- const readable = new ReadableStream({
193
- start (controller) {
194
- for (const b of additions) controller.enqueue(b)
195
- },
196
- pull (controller) {
197
- for (const b of iterator) {
198
- if (removals.some(r => b.cid.toString() === r.cid.toString())) continue
199
- return controller.enqueue(b)
200
- }
201
- controller.close()
202
- }
203
- })
204
- await readable.pipeThrough(new CARWriterStream([root])).pipeTo(Writable.toWeb(fs.createWriteStream(tmp)))
205
-
206
- const old = `${path}-${new Date().toISOString()}`
207
- try {
208
- await fs.promises.rename(path, old)
209
- } catch (err) {
210
- if (err.code !== 'ENOENT') throw err
211
- }
212
- await fs.promises.rename(tmp, path)
213
- try {
214
- await fs.promises.rm(old)
215
- } catch (err) {
216
- if (err.code !== 'ENOENT') throw err
217
- }
218
- }
@@ -1,61 +0,0 @@
1
- import type { Version } from 'multiformats';
2
- import type { Link, UnknownLink } from 'multiformats/link/interface';
3
- import type { Block, BlockView } from 'multiformats/block/interface';
4
- import { sha256 } from 'multiformats/hashes/sha2';
5
- import * as dagCBOR from '@ipld/dag-cbor';
6
- export { Link, UnknownLink, BlockView, Block, Version };
7
- export type ShardEntryValueValue = UnknownLink;
8
- export type ShardEntryLinkValue = [ShardLink];
9
- export type ShardEntryLinkAndValueValue = [ShardLink, UnknownLink];
10
- export type ShardValueEntry = [key: string, value: ShardEntryValueValue];
11
- export type ShardLinkEntry = [key: string, value: ShardEntryLinkValue | ShardEntryLinkAndValueValue];
12
- /** Single key/value entry within a shard. */
13
- export type ShardEntry = [key: string, value: ShardEntryValueValue | ShardEntryLinkValue | ShardEntryLinkAndValueValue];
14
- export interface Shard extends ShardConfig {
15
- entries: ShardEntry[];
16
- }
17
- export type ShardLink = Link<Shard, typeof dagCBOR.code, typeof sha256.code, 1>;
18
- export interface ShardBlockView extends BlockView<Shard, typeof dagCBOR.code, typeof sha256.code, 1> {
19
- }
20
- export interface ShardDiff {
21
- additions: ShardBlockView[];
22
- removals: ShardBlockView[];
23
- }
24
- export interface BlockFetcher {
25
- get<T = unknown, C extends number = number, A extends number = number, V extends Version = 1>(link: Link<T, C, A, V>): Promise<Block<T, C, A, V> | undefined>;
26
- }
27
- export interface ShardConfig {
28
- /** Shard compatibility version. */
29
- version: number;
30
- /**
31
- * Characters allowed in keys, referring to a known character set.
32
- * e.g. "ascii" refers to the printable ASCII characters in the code range 32-126.
33
- */
34
- keyChars: string;
35
- /** Max key size in bytes - default 4096 bytes. */
36
- maxKeySize: number;
37
- /** The key prefix from the root to this shard. */
38
- prefix: string;
39
- }
40
- export type ShardOptions = Partial<ShardConfig>;
41
- export interface KeyPrefixOption {
42
- /** Filter results to entries with keys prefixed with this string. */
43
- prefix: string;
44
- }
45
- export type KeyRangeOption = KeyLowerBoundRangeOption | KeyUpperBoundRangeOption | (KeyLowerBoundRangeOption & KeyUpperBoundRangeOption);
46
- export type KeyLowerBoundRangeOption = KeyLowerBoundRangeExclusiveOption | KeyLowerBoundRangeInclusiveOption;
47
- export interface KeyLowerBoundRangeExclusiveOption {
48
- gt: string;
49
- }
50
- export interface KeyLowerBoundRangeInclusiveOption {
51
- gte: string;
52
- }
53
- export type KeyUpperBoundRangeOption = KeyUpperBoundRangeExclusiveOption | KeyUpperBoundRangeInclusiveOption;
54
- export interface KeyUpperBoundRangeExclusiveOption {
55
- lt: string;
56
- }
57
- export interface KeyUpperBoundRangeInclusiveOption {
58
- lte: string;
59
- }
60
- export type EntriesOptions = KeyPrefixOption | KeyRangeOption;
61
- //# sourceMappingURL=api.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"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"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,31 +0,0 @@
1
- import { UnknownLink, ShardLink, ShardDiff, ShardEntry, ShardEntryValueValue, ShardEntryLinkValue, ShardEntryLinkAndValueValue, ShardConfig, ShardOptions, ShardBlockView, BlockFetcher } from '../api.js';
2
- export { UnknownLink, ShardLink, ShardDiff, ShardEntry, ShardEntryValueValue, ShardEntryLinkValue, ShardEntryLinkAndValueValue, ShardConfig, ShardOptions, ShardBlockView, BlockFetcher };
3
- export interface BatcherShard extends ShardConfig {
4
- base?: ShardBlockView;
5
- entries: BatcherShardEntry[];
6
- }
7
- export interface BatcherShardInit extends ShardOptions {
8
- base?: ShardBlockView;
9
- entries?: BatcherShardEntry[];
10
- }
11
- export type BatcherShardEntry = [
12
- key: string,
13
- value: ShardEntryValueValue | ShardEntryLinkValue | ShardEntryLinkAndValueValue | ShardEntryShardValue | ShardEntryShardAndValueValue
14
- ];
15
- export type ShardEntryShardValue = [BatcherShard];
16
- export type ShardEntryShardAndValueValue = [BatcherShard, UnknownLink];
17
- export interface Batcher {
18
- /**
19
- * Put a value (a CID) for the given key. If the key exists it's value is
20
- * overwritten.
21
- */
22
- put(key: string, value: UnknownLink): Promise<void>;
23
- /**
24
- * Encode all altered shards in the batch and return the new root CID and
25
- * difference blocks.
26
- */
27
- commit(): Promise<{
28
- root: ShardLink;
29
- } & ShardDiff>;
30
- }
31
- //# sourceMappingURL=api.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/batch/api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,SAAS,EACT,SAAS,EACT,UAAU,EACV,oBAAoB,EACpB,mBAAmB,EACnB,2BAA2B,EAC3B,WAAW,EACX,YAAY,EACZ,cAAc,EACd,YAAY,EACb,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,WAAW,EACX,SAAS,EACT,SAAS,EACT,UAAU,EACV,oBAAoB,EACpB,mBAAmB,EACnB,2BAA2B,EAC3B,WAAW,EACX,YAAY,EACZ,cAAc,EACd,YAAY,EACb,CAAA;AAED,MAAM,WAAW,YAAa,SAAQ,WAAW;IAC/C,IAAI,CAAC,EAAE,cAAc,CAAA;IACrB,OAAO,EAAE,iBAAiB,EAAE,CAAA;CAC7B;AAED,MAAM,WAAW,gBAAiB,SAAQ,YAAY;IACpD,IAAI,CAAC,EAAE,cAAc,CAAA;IACrB,OAAO,CAAC,EAAE,iBAAiB,EAAE,CAAA;CAC9B;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM;IACX,KAAK,EAAE,oBAAoB,GAAG,mBAAmB,GAAG,2BAA2B,GAAG,oBAAoB,GAAG,4BAA4B;CACtI,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG,CAAC,YAAY,CAAC,CAAA;AAEjD,MAAM,MAAM,4BAA4B,GAAG,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;AAEtE,MAAM,WAAW,OAAO;IACtB;;;OAGG;IACH,GAAG,CAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACpD;;;OAGG;IACH,MAAM,IAAK,OAAO,CAAC;QAAE,IAAI,EAAE,SAAS,CAAA;KAAE,GAAG,SAAS,CAAC,CAAA;CACpD"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,23 +0,0 @@
1
- export function put(blocks: API.BlockFetcher, shard: API.BatcherShard, key: string, value: API.UnknownLink): Promise<void>;
2
- export function traverse(shards: ShardFetcher, shard: API.BatcherShard, key: string): Promise<{
3
- shard: API.BatcherShard;
4
- key: string;
5
- }>;
6
- export function commit(shard: API.BatcherShard): Promise<{
7
- root: import("multiformats").CID<import("../api.js").Shard, 113, 18, 1>;
8
- additions: API.ShardBlockView[];
9
- removals: API.ShardBlockView[];
10
- }>;
11
- export function create(blocks: API.BlockFetcher, root: API.ShardLink): Promise<API.Batcher>;
12
- export class BatchCommittedError extends Error {
13
- static code: string;
14
- /**
15
- * @param {string} [message]
16
- * @param {ErrorOptions} [options]
17
- */
18
- constructor(message?: string, options?: ErrorOptions);
19
- code: string;
20
- }
21
- import * as API from './api.js';
22
- import { ShardFetcher } from '../shard.js';
23
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/batch/index.js"],"names":[],"mappings":"AAiEO,4BANI,GAAG,CAAC,YAAY,SAChB,GAAG,CAAC,YAAY,OAChB,MAAM,SACN,GAAG,CAAC,WAAW,GACb,OAAO,CAAC,IAAI,CAAC,CAsGzB;AAWM,iCALI,YAAY,SACZ,GAAG,CAAC,YAAY,OAChB,MAAM,GACJ,OAAO,CAAC;IAAE,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAiB7D;AAQM,8BAFI,GAAG,CAAC,YAAY;;;;GAkC1B;AAaM,+BAJI,GAAG,CAAC,YAAY,QAChB,GAAG,CAAC,SAAS,GACX,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAE6C;AAE9E;IAUE,oBAAmC;IATnC;;;OAGG;IACH,sBAHW,MAAM,YACN,YAAY,EAKtB;IADC,aAAoC;CAIvC;qBAhQoB,UAAU;6BACgB,aAAa"}
@@ -1,241 +0,0 @@
1
- // eslint-disable-next-line no-unused-vars
2
- import * as API from './api.js';
3
- import { ShardFetcher, isPrintableASCII } from '../shard.js';
4
- import * as Shard from '../shard.js';
5
- import * as BatcherShard from './shard.js';
6
- /** @implements {API.Batcher} */
7
- class Batcher {
8
- #committed = false;
9
- /**
10
- * @param {object} init
11
- * @param {API.BlockFetcher} init.blocks Block storage.
12
- * @param {API.BatcherShardEntry[]} init.entries The entries in this shard.
13
- * @param {string} init.prefix Key prefix.
14
- * @param {number} init.version Shard compatibility version.
15
- * @param {string} init.keyChars Characters allowed in keys, referring to a known character set.
16
- * @param {number} init.maxKeySize Max key size in bytes.
17
- * @param {API.ShardBlockView} init.base Original shard this batcher is based on.
18
- */
19
- constructor({ blocks, entries, prefix, version, keyChars, maxKeySize, base }) {
20
- this.blocks = blocks;
21
- this.prefix = prefix;
22
- this.entries = [...entries];
23
- this.base = base;
24
- this.version = version;
25
- this.keyChars = keyChars;
26
- this.maxKeySize = maxKeySize;
27
- }
28
- /**
29
- * @param {string} key The key of the value to put.
30
- * @param {API.UnknownLink} value The value to put.
31
- * @returns {Promise<void>}
32
- */
33
- async put(key, value) {
34
- if (this.#committed)
35
- throw new BatchCommittedError();
36
- return put(this.blocks, this, key, value);
37
- }
38
- async commit() {
39
- if (this.#committed)
40
- throw new BatchCommittedError();
41
- this.#committed = true;
42
- return commit(this);
43
- }
44
- /**
45
- * @param {object} init
46
- * @param {API.BlockFetcher} init.blocks Block storage.
47
- * @param {API.ShardLink} init.link CID of the shard block.
48
- */
49
- static async create({ blocks, link }) {
50
- const shards = new ShardFetcher(blocks);
51
- const base = await shards.get(link);
52
- return new Batcher({ blocks, base, ...base.value });
53
- }
54
- }
55
- /**
56
- * @param {API.BlockFetcher} blocks
57
- * @param {API.BatcherShard} shard
58
- * @param {string} key The key of the value to put.
59
- * @param {API.UnknownLink} value The value to put.
60
- * @returns {Promise<void>}
61
- */
62
- export const put = async (blocks, shard, key, value) => {
63
- if (shard.keyChars !== Shard.KeyCharsASCII) {
64
- throw new Error(`unsupported key character set: ${shard.keyChars}`);
65
- }
66
- if (!isPrintableASCII(key)) {
67
- throw new Error('key contains non-ASCII characters');
68
- }
69
- // ensure utf8 encoded key is smaller than max
70
- if (new TextEncoder().encode(key).length > shard.maxKeySize) {
71
- throw new Error(`UTF-8 encoded key exceeds max size of ${shard.maxKeySize} bytes`);
72
- }
73
- const shards = new ShardFetcher(blocks);
74
- const dest = await traverse(shards, shard, key);
75
- if (dest.shard !== shard) {
76
- shard = dest.shard;
77
- key = dest.key;
78
- }
79
- /** @type {API.BatcherShardEntry} */
80
- let entry = [dest.key, value];
81
- let targetEntries = [...dest.shard.entries];
82
- for (const [i, e] of targetEntries.entries()) {
83
- const [k, v] = e;
84
- // is this just a replace?
85
- if (k === dest.key)
86
- break;
87
- // do we need to shard this entry?
88
- const shortest = k.length < dest.key.length ? k : dest.key;
89
- const other = shortest === k ? dest.key : k;
90
- let common = '';
91
- for (const char of shortest) {
92
- const next = common + char;
93
- if (!other.startsWith(next))
94
- break;
95
- common = next;
96
- }
97
- if (common.length) {
98
- /** @type {API.ShardEntry[]} */
99
- let entries = [];
100
- // if the existing entry key or new key is equal to the common prefix,
101
- // then the existing value / new value needs to persist in the parent
102
- // shard. Otherwise they persist in this new shard.
103
- if (common !== dest.key) {
104
- entries = Shard.putEntry(entries, [dest.key.slice(common.length), value]);
105
- }
106
- if (common !== k) {
107
- entries = Shard.putEntry(entries, asShardEntry([k.slice(common.length), v]));
108
- }
109
- let child = BatcherShard.create({
110
- ...Shard.configure(dest.shard),
111
- prefix: dest.shard.prefix + common,
112
- entries
113
- });
114
- // need to spread as access by index does not consider utf-16 surrogates
115
- const commonChars = [...common];
116
- // create parent shards for each character of the common prefix
117
- for (let i = commonChars.length - 1; i > 0; i--) {
118
- /** @type {API.ShardEntryShardValue | API.ShardEntryShardAndValueValue} */
119
- let parentValue;
120
- // if the first iteration and the existing entry key is equal to the
121
- // common prefix, then existing value needs to persist in this parent
122
- if (i === commonChars.length - 1 && common === k) {
123
- if (Array.isArray(v))
124
- throw new Error('found a shard link when expecting a value');
125
- parentValue = [child, v];
126
- }
127
- else if (i === commonChars.length - 1 && common === dest.key) {
128
- parentValue = [child, value];
129
- }
130
- else {
131
- parentValue = [child];
132
- }
133
- const parent = BatcherShard.create({
134
- ...Shard.configure(dest.shard),
135
- prefix: dest.shard.prefix + commonChars.slice(0, i).join(''),
136
- entries: [[commonChars[i], parentValue]]
137
- });
138
- child = parent;
139
- }
140
- // remove the sharded entry
141
- targetEntries.splice(i, 1);
142
- // create the entry that will be added to target
143
- if (commonChars.length === 1 && common === k) {
144
- if (Array.isArray(v))
145
- throw new Error('found a shard link when expecting a value');
146
- entry = [commonChars[0], [child, v]];
147
- }
148
- else if (commonChars.length === 1 && common === dest.key) {
149
- entry = [commonChars[0], [child, value]];
150
- }
151
- else {
152
- entry = [commonChars[0], [child]];
153
- }
154
- break;
155
- }
156
- }
157
- shard.entries = Shard.putEntry(asShardEntries(targetEntries), asShardEntry(entry));
158
- };
159
- /**
160
- * Traverse from the passed shard through to the correct shard for the passed
161
- * key.
162
- *
163
- * @param {ShardFetcher} shards
164
- * @param {API.BatcherShard} shard
165
- * @param {string} key
166
- * @returns {Promise<{ shard: API.BatcherShard, key: string }>}
167
- */
168
- export const traverse = async (shards, shard, key) => {
169
- for (let i = 0; i < shard.entries.length; i++) {
170
- const [k, v] = shard.entries[i];
171
- if (key <= k)
172
- break;
173
- if (key.startsWith(k) && Array.isArray(v)) {
174
- if (Shard.isShardLink(v[0])) {
175
- const blk = await shards.get(v[0]);
176
- const batcher = BatcherShard.create({ base: blk, ...blk.value });
177
- shard.entries[i] = [k, v[1] == null ? [batcher] : [batcher, v[1]]];
178
- return traverse(shards, batcher, key.slice(k.length));
179
- }
180
- return traverse(shards, v[0], key.slice(k.length));
181
- }
182
- }
183
- return { shard, key };
184
- };
185
- /**
186
- * Encode all altered shards in the batch and return the new root CID and
187
- * difference blocks.
188
- *
189
- * @param {API.BatcherShard} shard
190
- */
191
- export const commit = async (shard) => {
192
- /** @type {API.ShardBlockView[]} */
193
- const additions = [];
194
- /** @type {API.ShardBlockView[]} */
195
- const removals = [];
196
- /** @type {API.ShardEntry[]} */
197
- const entries = [];
198
- for (const entry of shard.entries) {
199
- if (Array.isArray(entry[1]) && !Shard.isShardLink(entry[1][0])) {
200
- const result = await commit(entry[1][0]);
201
- entries.push([
202
- entry[0],
203
- entry[1][1] == null ? [result.root] : [result.root, entry[1][1]]
204
- ]);
205
- additions.push(...result.additions);
206
- removals.push(...result.removals);
207
- }
208
- else {
209
- entries.push(asShardEntry(entry));
210
- }
211
- }
212
- const block = await Shard.encodeBlock(Shard.withEntries(entries, shard));
213
- additions.push(block);
214
- if (shard.base && shard.base.cid.toString() === block.cid.toString()) {
215
- return { root: block.cid, additions: [], removals: [] };
216
- }
217
- if (shard.base)
218
- removals.push(shard.base);
219
- return { root: block.cid, additions, removals };
220
- };
221
- /** @param {API.BatcherShardEntry[]} entries */
222
- const asShardEntries = entries => /** @type {API.ShardEntry[]} */ (entries);
223
- /** @param {API.BatcherShardEntry} entry */
224
- const asShardEntry = entry => /** @type {API.ShardEntry} */ (entry);
225
- /**
226
- * @param {API.BlockFetcher} blocks Bucket block storage.
227
- * @param {API.ShardLink} root CID of the root shard block.
228
- * @returns {Promise<API.Batcher>}
229
- */
230
- export const create = (blocks, root) => Batcher.create({ blocks, link: root });
231
- export class BatchCommittedError extends Error {
232
- /**
233
- * @param {string} [message]
234
- * @param {ErrorOptions} [options]
235
- */
236
- constructor(message, options) {
237
- super(message ?? 'batch already committed', options);
238
- this.code = BatchCommittedError.code;
239
- }
240
- static code = 'ERR_BATCH_COMMITTED';
241
- }