@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,182 +0,0 @@
1
- import { Block, encode, decode } from 'multiformats/block'
2
- import { sha256 } from 'multiformats/hashes/sha2'
3
- import * as cbor from '@ipld/dag-cbor'
4
- // eslint-disable-next-line no-unused-vars
5
- import * as API from './api.js'
6
-
7
- /**
8
- * Advance the clock by adding an event.
9
- *
10
- * @template T
11
- * @param {API.BlockFetcher} blocks Block storage.
12
- * @param {API.EventLink<T>[]} head The head of the clock.
13
- * @param {API.EventLink<T>} event The event to add.
14
- */
15
- export const advance = async (blocks, head, event) => {
16
- const events = new EventFetcher(blocks)
17
- const headmap = new Map(head.map(cid => [cid.toString(), cid]))
18
- if (headmap.has(event.toString())) return head
19
-
20
- // does event contain the clock?
21
- let changed = false
22
- for (const cid of head) {
23
- if (await contains(events, event, cid)) {
24
- headmap.delete(cid.toString())
25
- headmap.set(event.toString(), event)
26
- changed = true
27
- }
28
- }
29
- if (changed) {
30
- return [...headmap.values()]
31
- }
32
-
33
- // does clock contain the event?
34
- for (const p of head) {
35
- if (await contains(events, p, event)) {
36
- return head
37
- }
38
- }
39
-
40
- return head.concat(event)
41
- }
42
-
43
- /**
44
- * @template T
45
- * @extends {Block<API.EventView<T>, typeof cbor.code, typeof sha256.code, 1>}
46
- * @implements {API.EventBlockView<T>}
47
- */
48
- export class EventBlock extends Block {
49
- /**
50
- * @param {object} config
51
- * @param {API.EventLink<T>} config.cid
52
- * @param {Event} config.value
53
- * @param {Uint8Array} config.bytes
54
- * @param {string} config.prefix
55
- */
56
- constructor ({ cid, value, bytes, prefix }) {
57
- // @ts-expect-error
58
- super({ cid, value, bytes })
59
- this.prefix = prefix
60
- }
61
-
62
- /**
63
- * @template T
64
- * @param {T} data
65
- * @param {API.EventLink<T>[]} [parents]
66
- */
67
- static create (data, parents) {
68
- return encodeEventBlock({ data, parents: parents ?? [] })
69
- }
70
- }
71
-
72
- /** @template T */
73
- export class EventFetcher {
74
- /** @param {API.BlockFetcher} blocks */
75
- constructor (blocks) {
76
- /** @private */
77
- this._blocks = blocks
78
- }
79
-
80
- /**
81
- * @param {API.EventLink<T>} link
82
- * @returns {Promise<API.EventBlockView<T>>}
83
- */
84
- async get (link) {
85
- const block = await this._blocks.get(link)
86
- if (!block) throw new Error(`missing block: ${link}`)
87
- return decodeEventBlock(block.bytes)
88
- }
89
- }
90
-
91
- /**
92
- * @template T
93
- * @param {API.EventView<T>} value
94
- * @returns {Promise<API.EventBlockView<T>>}
95
- */
96
- export const encodeEventBlock = async (value) => {
97
- // TODO: sort parents
98
- const { cid, bytes } = await encode({ value, codec: cbor, hasher: sha256 })
99
- // @ts-expect-error
100
- return new Block({ cid, value, bytes })
101
- }
102
-
103
- /**
104
- * @template T
105
- * @param {Uint8Array} bytes
106
- * @returns {Promise<API.EventBlockView<T>>}
107
- */
108
- export const decodeEventBlock = async (bytes) => {
109
- const { cid, value } = await decode({ bytes, codec: cbor, hasher: sha256 })
110
- // @ts-expect-error
111
- return new Block({ cid, value, bytes })
112
- }
113
-
114
- /**
115
- * Returns true if event "a" contains event "b". Breadth first search.
116
- * @template T
117
- * @param {EventFetcher<T>} events
118
- * @param {API.EventLink<T>} a
119
- * @param {API.EventLink<T>} b
120
- */
121
- const contains = async (events, a, b) => {
122
- if (a.toString() === b.toString()) return true
123
- const [{ value: aevent }, { value: bevent }] = await Promise.all([events.get(a), events.get(b)])
124
- const links = [...aevent.parents]
125
- const seen = new Set()
126
- while (links.length) {
127
- const link = links.shift()
128
- if (!link) break
129
- if (link.toString() === b.toString()) return true
130
- // if any of b's parents are this link, then b cannot exist in any of the
131
- // tree below, since that would create a cycle.
132
- if (bevent.parents.some(p => link.toString() === p.toString())) continue
133
- if (seen.has(link.toString())) continue
134
- seen.add(link.toString())
135
- const { value: event } = await events.get(link)
136
- links.push(...event.parents)
137
- }
138
- return false
139
- }
140
-
141
- /**
142
- * @template T
143
- * @param {API.BlockFetcher} blocks Block storage.
144
- * @param {API.EventLink<T>[]} head
145
- * @param {object} [options]
146
- * @param {(b: API.EventBlockView<T>) => string} [options.renderNodeLabel]
147
- */
148
- export const vis = async function * (blocks, head, options = {}) {
149
- const renderNodeLabel = options.renderNodeLabel ?? (b => shortLink(b.cid))
150
- const events = new EventFetcher(blocks)
151
- yield 'digraph clock {'
152
- yield ' node [shape=point fontname="Courier"]; head;'
153
- const hevents = await Promise.all(head.map(link => events.get(link)))
154
- /** @type {import('multiformats').Link<API.EventView<any>>[]} */
155
- const links = []
156
- const nodes = new Set()
157
- for (const e of hevents) {
158
- nodes.add(e.cid.toString())
159
- yield ` node [shape=oval fontname="Courier"]; ${e.cid} [label="${renderNodeLabel(e)}"];`
160
- yield ` head -> ${e.cid};`
161
- for (const p of e.value.parents) {
162
- yield ` ${e.cid} -> ${p};`
163
- }
164
- links.push(...e.value.parents)
165
- }
166
- while (links.length) {
167
- const link = links.shift()
168
- if (!link) break
169
- if (nodes.has(link.toString())) continue
170
- nodes.add(link.toString())
171
- const block = await events.get(link)
172
- yield ` node [shape=oval]; ${link} [label="${renderNodeLabel(block)}" fontname="Courier"];`
173
- for (const p of block.value.parents) {
174
- yield ` ${link} -> ${p};`
175
- }
176
- links.push(...block.value.parents)
177
- }
178
- yield '}'
179
- }
180
-
181
- /** @param {import('multiformats').UnknownLink} l */
182
- const shortLink = l => `${String(l).slice(0, 4)}..${String(l).slice(-4)}`
@@ -1 +0,0 @@
1
- export {}
@@ -1,33 +0,0 @@
1
- import { ShardDiff, ShardLink, UnknownLink } from '../api.js'
2
- import { EventLink, EventBlockView } from '../clock/api.js'
3
-
4
- export { BlockFetcher, UnknownLink, ShardBlockView, ShardDiff, ShardLink, EntriesOptions } from '../api.js'
5
- export { EventBlockView, EventLink } from '../clock/api.js'
6
-
7
- export interface Result extends ShardDiff {
8
- root: ShardLink
9
- head: EventLink<Operation>[]
10
- event?: EventBlockView<Operation>
11
- }
12
-
13
- export type Operation = (
14
- | PutOperation
15
- | DeleteOperation
16
- | BatchOperation
17
- ) & { root: ShardLink }
18
-
19
- export interface PutOperation {
20
- type: 'put',
21
- key: string
22
- value: UnknownLink
23
- }
24
-
25
- export interface DeleteOperation {
26
- type: 'del',
27
- key: string
28
- }
29
-
30
- export interface BatchOperation {
31
- type: 'batch',
32
- ops: Array<PutOperation|DeleteOperation>
33
- }
@@ -1 +0,0 @@
1
- export {}
@@ -1,30 +0,0 @@
1
- import {
2
- Batcher,
3
- BatcherShardEntry,
4
- ShardBlockView,
5
- BlockFetcher,
6
- ShardLink,
7
- UnknownLink
8
- } from '../../batch/api.js'
9
- import { Operation, BatchOperation, EventLink, Result } from '../api.js'
10
-
11
- export {
12
- Batcher,
13
- BatcherShardEntry,
14
- ShardBlockView,
15
- BlockFetcher,
16
- ShardLink,
17
- UnknownLink,
18
- Operation,
19
- BatchOperation,
20
- EventLink,
21
- Result
22
- }
23
-
24
- export interface CRDTBatcher extends Batcher {
25
- /**
26
- * Encode all altered shards in the batch and return the new root CID, new
27
- * clock head, the new clock event and the difference blocks.
28
- */
29
- commit (): Promise<Result>
30
- }
@@ -1,155 +0,0 @@
1
- // eslint-disable-next-line no-unused-vars
2
- import * as API from './api.js'
3
- import * as Shard from '../../shard.js'
4
- import { ShardFetcher, ShardBlock } from '../../shard.js'
5
- import * as Batch from '../../batch/index.js'
6
- import { BatchCommittedError } from '../../batch/index.js'
7
- import * as CRDT from '../index.js'
8
- import * as Clock from '../../clock/index.js'
9
- import { EventBlock } from '../../clock/index.js'
10
- import { MemoryBlockstore, MultiBlockFetcher } from '../../block.js'
11
-
12
- export { BatchCommittedError }
13
-
14
- /** @implements {API.CRDTBatcher} */
15
- class Batcher {
16
- #committed = false
17
-
18
- /**
19
- * @param {object} init
20
- * @param {API.BlockFetcher} init.blocks Block storage.
21
- * @param {API.EventLink<API.Operation>[]} init.head Merkle clock head.
22
- * @param {API.BatcherShardEntry[]} init.entries The entries in this shard.
23
- * @param {string} init.prefix Key prefix.
24
- * @param {number} init.version Shard compatibility version.
25
- * @param {string} init.keyChars Characters allowed in keys, referring to a known character set.
26
- * @param {number} init.maxKeySize Max key size in bytes.
27
- * @param {API.ShardBlockView} init.base Original shard this batcher is based on.
28
- * @param {API.ShardBlockView[]} init.additions Additions to include in the committed batch.
29
- * @param {API.ShardBlockView[]} init.removals Removals to include in the committed batch.
30
- */
31
- constructor ({ blocks, head, entries, prefix, version, keyChars, maxKeySize, base, additions, removals }) {
32
- this.blocks = blocks
33
- this.head = head
34
- this.prefix = prefix
35
- this.entries = [...entries]
36
- this.base = base
37
- this.version = version
38
- this.keyChars = keyChars
39
- this.maxKeySize = maxKeySize
40
- this.additions = additions
41
- this.removals = removals
42
- /** @type {API.BatchOperation['ops']} */
43
- this.ops = []
44
- }
45
-
46
- /**
47
- * @param {string} key The key of the value to put.
48
- * @param {API.UnknownLink} value The value to put.
49
- * @returns {Promise<void>}
50
- */
51
- async put (key, value) {
52
- if (this.#committed) throw new BatchCommittedError()
53
- await Batch.put(this.blocks, this, key, value)
54
- this.ops.push({ type: 'put', key, value })
55
- }
56
-
57
- async commit () {
58
- if (this.#committed) throw new BatchCommittedError()
59
- this.#committed = true
60
-
61
- const res = await Batch.commit(this)
62
-
63
- /** @type {API.Operation} */
64
- const data = { type: 'batch', ops: this.ops, root: res.root }
65
- const event = await EventBlock.create(data, this.head)
66
-
67
- const mblocks = new MemoryBlockstore()
68
- const blocks = new MultiBlockFetcher(mblocks, this.blocks)
69
- mblocks.putSync(event.cid, event.bytes)
70
-
71
- const head = await Clock.advance(blocks, this.head, event.cid)
72
-
73
- /** @type {Map<string, API.ShardBlockView>} */
74
- const additions = new Map()
75
- /** @type {Map<string, API.ShardBlockView>} */
76
- const removals = new Map()
77
-
78
- for (const a of this.additions) {
79
- additions.set(a.cid.toString(), a)
80
- }
81
- for (const r of this.removals) {
82
- removals.set(r.cid.toString(), r)
83
- }
84
-
85
- for (const a of res.additions) {
86
- if (removals.has(a.cid.toString())) {
87
- removals.delete(a.cid.toString())
88
- }
89
- additions.set(a.cid.toString(), a)
90
- }
91
- for (const r of res.removals) {
92
- if (additions.has(r.cid.toString())) {
93
- additions.delete(r.cid.toString())
94
- } else {
95
- removals.set(r.cid.toString(), r)
96
- }
97
- }
98
-
99
- return {
100
- head,
101
- event,
102
- root: res.root,
103
- additions: [...additions.values()],
104
- removals: [...removals.values()]
105
- }
106
- }
107
-
108
- /**
109
- * @param {object} init
110
- * @param {API.BlockFetcher} init.blocks Block storage.
111
- * @param {API.EventLink<API.Operation>[]} init.head Merkle clock head.
112
- */
113
- static async create ({ blocks, head }) {
114
- const mblocks = new MemoryBlockstore()
115
- blocks = new MultiBlockFetcher(mblocks, blocks)
116
-
117
- if (!head.length) {
118
- const base = await ShardBlock.create()
119
- mblocks.putSync(base.cid, base.bytes)
120
- return new Batcher({
121
- blocks,
122
- head,
123
- entries: [],
124
- base,
125
- additions: [base],
126
- removals: [],
127
- ...Shard.configure(base.value)
128
- })
129
- }
130
-
131
- const { root, additions, removals } = await CRDT.root(blocks, head)
132
- for (const a of additions) {
133
- mblocks.putSync(a.cid, a.bytes)
134
- }
135
-
136
- const shards = new ShardFetcher(blocks)
137
- const base = await shards.get(root)
138
- return new Batcher({
139
- blocks,
140
- head,
141
- entries: base.value.entries,
142
- base,
143
- additions,
144
- removals,
145
- ...Shard.configure(base.value)
146
- })
147
- }
148
- }
149
-
150
- /**
151
- * @param {API.BlockFetcher} blocks Bucket block storage.
152
- * @param {API.EventLink<API.Operation>[]} head Merkle clock head.
153
- * @returns {Promise<API.CRDTBatcher>}
154
- */
155
- export const create = (blocks, head) => Batcher.create({ blocks, head })