@autonomys/auto-drive 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cid/index.d.ts +1 -1
- package/dist/cid/index.d.ts.map +1 -1
- package/dist/cid/index.js +3 -3
- package/dist/ipld/blockstore/base.d.ts +9 -0
- package/dist/ipld/blockstore/base.d.ts.map +1 -0
- package/dist/ipld/blockstore/base.js +1 -0
- package/dist/ipld/blockstore/index.d.ts +3 -0
- package/dist/ipld/blockstore/index.d.ts.map +1 -0
- package/dist/ipld/blockstore/index.js +2 -0
- package/dist/ipld/blockstore/memory.d.ts +13 -0
- package/dist/ipld/blockstore/memory.d.ts.map +1 -0
- package/dist/ipld/blockstore/memory.js +57 -0
- package/dist/ipld/builders.d.ts +1 -1
- package/dist/ipld/builders.d.ts.map +1 -1
- package/dist/ipld/chunker.d.ts +23 -13
- package/dist/ipld/chunker.d.ts.map +1 -1
- package/dist/ipld/chunker.js +148 -45
- package/dist/ipld/index.d.ts +2 -1
- package/dist/ipld/index.d.ts.map +1 -1
- package/dist/ipld/index.js +2 -1
- package/dist/ipld/nodes.d.ts +1 -1
- package/dist/ipld/nodes.d.ts.map +1 -1
- package/dist/ipld/nodes.js +1 -1
- package/dist/ipld/utils.d.ts +7 -4
- package/dist/ipld/utils.d.ts.map +1 -1
- package/dist/ipld/utils.js +49 -9
- package/dist/metadata/offchain/file.d.ts +2 -2
- package/dist/metadata/offchain/file.d.ts.map +1 -1
- package/dist/metadata/offchain/file.js +6 -13
- package/dist/metadata/offchain/folder.d.ts +4 -1
- package/dist/metadata/offchain/folder.d.ts.map +1 -1
- package/dist/metadata/offchain/folder.js +17 -1
- package/dist/metadata/onchain/utils.js +2 -2
- package/dist/src/cid/index.d.ts +9 -0
- package/dist/src/cid/index.d.ts.map +1 -0
- package/dist/src/cid/index.js +20 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +3 -0
- package/dist/src/ipld/builders.d.ts +11 -0
- package/dist/src/ipld/builders.d.ts.map +1 -0
- package/dist/src/ipld/builders.js +13 -0
- package/dist/src/ipld/chunker.d.ts +22 -0
- package/dist/src/ipld/chunker.d.ts.map +1 -0
- package/dist/src/ipld/chunker.js +144 -0
- package/dist/src/ipld/index.d.ts +4 -0
- package/dist/src/ipld/index.d.ts.map +1 -0
- package/dist/src/ipld/index.js +3 -0
- package/dist/src/ipld/nodes.d.ts +15 -0
- package/dist/src/ipld/nodes.d.ts.map +1 -0
- package/dist/src/ipld/nodes.js +89 -0
- package/dist/src/ipld/utils.d.ts +5 -0
- package/dist/src/ipld/utils.d.ts.map +1 -0
- package/dist/src/ipld/utils.js +51 -0
- package/dist/src/metadata/index.d.ts +3 -0
- package/dist/src/metadata/index.d.ts.map +1 -0
- package/dist/src/metadata/index.js +2 -0
- package/dist/src/metadata/offchain/base.d.ts +4 -0
- package/dist/src/metadata/offchain/base.d.ts.map +1 -0
- package/dist/src/metadata/offchain/base.js +1 -0
- package/dist/src/metadata/offchain/file.d.ts +16 -0
- package/dist/src/metadata/offchain/file.d.ts.map +1 -0
- package/dist/src/metadata/offchain/file.js +19 -0
- package/dist/src/metadata/offchain/folder.d.ts +17 -0
- package/dist/src/metadata/offchain/folder.d.ts.map +1 -0
- package/dist/src/metadata/offchain/folder.js +10 -0
- package/dist/src/metadata/offchain/index.d.ts +4 -0
- package/dist/src/metadata/offchain/index.d.ts.map +1 -0
- package/dist/src/metadata/offchain/index.js +3 -0
- package/dist/src/metadata/onchain/index.d.ts +3 -0
- package/dist/src/metadata/onchain/index.d.ts.map +1 -0
- package/dist/src/metadata/onchain/index.js +2 -0
- package/dist/src/metadata/onchain/protobuf/OnchainMetadata.d.ts +28 -0
- package/dist/src/metadata/onchain/protobuf/OnchainMetadata.d.ts.map +1 -0
- package/dist/src/metadata/onchain/protobuf/OnchainMetadata.js +112 -0
- package/dist/src/metadata/onchain/utils.d.ts +4 -0
- package/dist/src/metadata/onchain/utils.d.ts.map +1 -0
- package/dist/src/metadata/onchain/utils.js +12 -0
- package/package.json +4 -2
- package/src/cid/index.ts +3 -3
- package/src/ipld/builders.ts +1 -1
- package/src/ipld/chunker.ts +114 -56
- package/src/ipld/index.ts +2 -1
- package/src/ipld/nodes.ts +1 -1
- package/src/ipld/utils.ts +17 -11
- package/src/metadata/offchain/file.ts +10 -15
- package/src/metadata/offchain/folder.ts +24 -3
- package/src/metadata/onchain/utils.ts +2 -2
- package/tests/chunker.spec.ts +148 -46
- package/tests/cid.spec.ts +1 -1
- package/tests/nodes.spec.ts +1 -1
package/tests/chunker.spec.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { cidOfNode } from '../src'
|
|
3
|
-
import {
|
|
1
|
+
import { BaseBlockstore, MemoryBlockstore } from 'blockstore-core'
|
|
2
|
+
import { cidOfNode, cidToString } from '../src'
|
|
3
|
+
import {
|
|
4
|
+
processFileToIPLDFormat,
|
|
5
|
+
processFolderToIPLDFormat,
|
|
6
|
+
processMetadataToIPLDFormat,
|
|
7
|
+
} from '../src/ipld/chunker'
|
|
8
|
+
import { createNode, decodeNode, PBNode } from '../src/ipld/utils'
|
|
4
9
|
import { IPLDNodeData, MetadataType, OffchainMetadata } from '../src/metadata'
|
|
5
10
|
|
|
6
11
|
describe('chunker', () => {
|
|
7
12
|
describe('file creation', () => {
|
|
8
|
-
it('create a file dag from a small buffer', () => {
|
|
13
|
+
it('create a file dag from a small buffer', async () => {
|
|
9
14
|
const text = 'hello world'
|
|
15
|
+
const size = text.length
|
|
10
16
|
const name = 'test.txt'
|
|
11
|
-
const
|
|
12
|
-
expect(dag.nodes.size).toBe(1)
|
|
17
|
+
const blockstore = new MemoryBlockstore()
|
|
13
18
|
|
|
14
|
-
|
|
19
|
+
await processFileToIPLDFormat(blockstore, bufferToIterable(Buffer.from(text)), size, name)
|
|
20
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
21
|
+
expect(nodes.length).toBe(1)
|
|
22
|
+
|
|
23
|
+
const node = nodes[0]
|
|
15
24
|
expect(node).toBeDefined()
|
|
16
25
|
expect(node?.Data).toBeDefined()
|
|
17
26
|
const decoded = IPLDNodeData.decode(node?.Data ?? new Uint8Array())
|
|
@@ -28,22 +37,33 @@ describe('chunker', () => {
|
|
|
28
37
|
expect(node?.Links.length).toBe(0)
|
|
29
38
|
})
|
|
30
39
|
|
|
31
|
-
it('create a file dag from a large buffer', () => {
|
|
32
|
-
const
|
|
40
|
+
it('create a file dag from a large buffer', async () => {
|
|
41
|
+
const maxChunkSize = 1000
|
|
33
42
|
const chunkNum = 10
|
|
34
|
-
const chunk = 'h'.repeat(
|
|
43
|
+
const chunk = 'h'.repeat(maxChunkSize)
|
|
35
44
|
const text = chunk.repeat(chunkNum)
|
|
45
|
+
const size = text.length
|
|
36
46
|
|
|
37
47
|
const name = 'test.txt'
|
|
48
|
+
/// 1 chunk + root
|
|
49
|
+
const EXPECTED_NODE_COUNT = 2
|
|
38
50
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
const blockstore = new MemoryBlockstore()
|
|
52
|
+
const headCID = await processFileToIPLDFormat(
|
|
53
|
+
blockstore,
|
|
54
|
+
bufferToIterable(Buffer.from(text)),
|
|
55
|
+
size,
|
|
56
|
+
name,
|
|
57
|
+
{
|
|
58
|
+
maxChunkSize,
|
|
59
|
+
maxLinkPerNode: maxChunkSize / 64,
|
|
60
|
+
},
|
|
61
|
+
)
|
|
43
62
|
|
|
44
|
-
|
|
63
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
64
|
+
expect(nodes.length).toBe(EXPECTED_NODE_COUNT)
|
|
45
65
|
|
|
46
|
-
const head =
|
|
66
|
+
const head = decodeNode(await blockstore.get(headCID))
|
|
47
67
|
expect(head?.Data).toBeDefined()
|
|
48
68
|
expect(head).toBeDefined()
|
|
49
69
|
expect(head?.Links.length).toBe(chunkNum)
|
|
@@ -54,33 +74,42 @@ describe('chunker', () => {
|
|
|
54
74
|
expect(decoded.linkDepth).toBe(1)
|
|
55
75
|
expect(decoded.size).toBe(text.length)
|
|
56
76
|
|
|
57
|
-
|
|
58
|
-
if (
|
|
77
|
+
nodes.forEach((node) => {
|
|
78
|
+
if (cidToString(cidOfNode(node)) !== cidToString(headCID)) {
|
|
59
79
|
expect(node?.Links.length).toBe(0)
|
|
60
80
|
}
|
|
61
81
|
})
|
|
62
82
|
})
|
|
63
83
|
|
|
64
|
-
it('create a file dag with inlinks', () => {
|
|
65
|
-
const
|
|
84
|
+
it('create a file dag with inlinks', async () => {
|
|
85
|
+
const maxChunkSize = 1000
|
|
66
86
|
const chunkNum = 10
|
|
67
|
-
const chunk = 'h'.repeat(
|
|
87
|
+
const chunk = 'h'.repeat(maxChunkSize)
|
|
68
88
|
const name = 'test.txt'
|
|
69
89
|
const text = chunk.repeat(chunkNum)
|
|
90
|
+
const size = text.length
|
|
70
91
|
|
|
71
|
-
///
|
|
72
|
-
const EXPECTED_NODE_COUNT =
|
|
92
|
+
/// 1 chunks + 2 inlinks + root
|
|
93
|
+
const EXPECTED_NODE_COUNT = 4
|
|
73
94
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
95
|
+
const blockstore = new MemoryBlockstore()
|
|
96
|
+
const headCID = await processFileToIPLDFormat(
|
|
97
|
+
blockstore,
|
|
98
|
+
bufferToIterable(Buffer.from(text)),
|
|
99
|
+
size,
|
|
100
|
+
name,
|
|
101
|
+
{
|
|
102
|
+
maxChunkSize,
|
|
103
|
+
maxLinkPerNode: 4,
|
|
104
|
+
},
|
|
105
|
+
)
|
|
78
106
|
|
|
79
|
-
|
|
107
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
108
|
+
expect(nodes.length).toBe(EXPECTED_NODE_COUNT)
|
|
80
109
|
|
|
81
110
|
let [rootCount, inlinkCount, chunkCount] = [0, 0, 0]
|
|
82
111
|
|
|
83
|
-
|
|
112
|
+
nodes.forEach((node) => {
|
|
84
113
|
const decoded = IPLDNodeData.decode(node?.Data ?? new Uint8Array())
|
|
85
114
|
if (decoded.type === MetadataType.File) {
|
|
86
115
|
rootCount++
|
|
@@ -94,24 +123,26 @@ describe('chunker', () => {
|
|
|
94
123
|
})
|
|
95
124
|
|
|
96
125
|
expect(rootCount).toBe(1)
|
|
97
|
-
expect(inlinkCount).toBe(
|
|
98
|
-
expect(chunkCount).toBe(
|
|
126
|
+
expect(inlinkCount).toBe(2)
|
|
127
|
+
expect(chunkCount).toBe(1)
|
|
99
128
|
})
|
|
100
129
|
})
|
|
101
130
|
|
|
102
131
|
describe('folder creation', () => {
|
|
103
|
-
it('create a folder dag from a small buffer', () => {
|
|
132
|
+
it('create a folder dag from a small buffer', async () => {
|
|
104
133
|
const links = Array.from({ length: 1 }, () =>
|
|
105
134
|
cidOfNode(createNode(Buffer.from(Math.random().toString()))),
|
|
106
135
|
)
|
|
107
136
|
const name = 'folder'
|
|
108
137
|
const size = 1000
|
|
109
|
-
const
|
|
138
|
+
const blockstore = new MemoryBlockstore()
|
|
139
|
+
const headCID = processFolderToIPLDFormat(blockstore, links, name, size, {
|
|
110
140
|
maxLinkPerNode: 4,
|
|
111
141
|
})
|
|
112
142
|
|
|
113
|
-
|
|
114
|
-
|
|
143
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
144
|
+
expect(nodes.length).toBe(1)
|
|
145
|
+
const node = nodes[0]
|
|
115
146
|
expect(node).toBeDefined()
|
|
116
147
|
expect(node?.Data).toBeDefined()
|
|
117
148
|
const decoded = IPLDNodeData.decode(node?.Data ?? new Uint8Array())
|
|
@@ -121,7 +152,7 @@ describe('chunker', () => {
|
|
|
121
152
|
expect(decoded.size).toBe(size)
|
|
122
153
|
})
|
|
123
154
|
|
|
124
|
-
it('create a folder dag with inlinks', () => {
|
|
155
|
+
it('create a folder dag with inlinks', async () => {
|
|
125
156
|
const links = Array.from({ length: 10 }, () =>
|
|
126
157
|
cidOfNode(createNode(Buffer.from(Math.random().toString()))),
|
|
127
158
|
)
|
|
@@ -131,15 +162,17 @@ describe('chunker', () => {
|
|
|
131
162
|
/// 3 inlinks + root
|
|
132
163
|
const EXPECTED_NODE_COUNT = 4
|
|
133
164
|
|
|
134
|
-
const
|
|
165
|
+
const blockstore = new MemoryBlockstore()
|
|
166
|
+
const headCID = processFolderToIPLDFormat(blockstore, links, name, size, {
|
|
135
167
|
maxLinkPerNode: 4,
|
|
136
168
|
})
|
|
137
169
|
|
|
138
|
-
|
|
170
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
171
|
+
expect(nodes.length).toBe(EXPECTED_NODE_COUNT)
|
|
139
172
|
|
|
140
173
|
let [rootCount, inlinkCount] = [0, 0, 0]
|
|
141
174
|
|
|
142
|
-
|
|
175
|
+
nodes.forEach((node) => {
|
|
143
176
|
const decoded = IPLDNodeData.decode(node?.Data ?? new Uint8Array())
|
|
144
177
|
if (decoded.type === MetadataType.Folder) {
|
|
145
178
|
rootCount++
|
|
@@ -156,7 +189,7 @@ describe('chunker', () => {
|
|
|
156
189
|
})
|
|
157
190
|
|
|
158
191
|
describe('metadata creation', () => {
|
|
159
|
-
it('create a metadata dag from a small buffer', () => {
|
|
192
|
+
it('create a metadata dag from a small buffer', async () => {
|
|
160
193
|
const metadata: OffchainMetadata = {
|
|
161
194
|
type: 'file',
|
|
162
195
|
dataCid: 'test',
|
|
@@ -167,11 +200,13 @@ describe('chunker', () => {
|
|
|
167
200
|
chunks: [],
|
|
168
201
|
}
|
|
169
202
|
|
|
170
|
-
const
|
|
171
|
-
|
|
203
|
+
const blockstore = new MemoryBlockstore()
|
|
204
|
+
const headCID = await processMetadataToIPLDFormat(blockstore, metadata)
|
|
205
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
206
|
+
expect(nodes.length).toBe(1)
|
|
172
207
|
})
|
|
173
208
|
|
|
174
|
-
it('large metadata dag represented into multiple nodes', () => {
|
|
209
|
+
it('large metadata dag represented into multiple nodes', async () => {
|
|
175
210
|
const metadata: OffchainMetadata = {
|
|
176
211
|
type: 'file',
|
|
177
212
|
dataCid: 'test',
|
|
@@ -182,11 +217,78 @@ describe('chunker', () => {
|
|
|
182
217
|
chunks: [],
|
|
183
218
|
}
|
|
184
219
|
|
|
185
|
-
const
|
|
186
|
-
|
|
220
|
+
const blockstore = new MemoryBlockstore()
|
|
221
|
+
const headCID = await processMetadataToIPLDFormat(blockstore, metadata, {
|
|
222
|
+
maxChunkSize: 200,
|
|
187
223
|
maxLinkPerNode: 2,
|
|
188
224
|
})
|
|
189
|
-
|
|
225
|
+
const nodes = await nodesFromBlockstore(blockstore)
|
|
226
|
+
expect(nodes.length).toBeGreaterThan(1)
|
|
227
|
+
})
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
describe('asyncronous chunking equivalence', () => {
|
|
231
|
+
it('chunk a small file buffer', async () => {
|
|
232
|
+
const buffer = Buffer.from('hello world')
|
|
233
|
+
const blockstore = new MemoryBlockstore()
|
|
234
|
+
const chunkedBlockstore = new MemoryBlockstore()
|
|
235
|
+
const singleBufferCID = await processFileToIPLDFormat(
|
|
236
|
+
blockstore,
|
|
237
|
+
bufferToIterable(buffer),
|
|
238
|
+
buffer.length,
|
|
239
|
+
'test.txt',
|
|
240
|
+
)
|
|
241
|
+
const chunkedBufferCID = await processFileToIPLDFormat(
|
|
242
|
+
chunkedBlockstore,
|
|
243
|
+
separateBufferToIterable(buffer, 5),
|
|
244
|
+
buffer.length,
|
|
245
|
+
'test.txt',
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
expect(singleBufferCID).toEqual(chunkedBufferCID)
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('chunk a large file buffer', async () => {
|
|
252
|
+
const buffer = Buffer.from('hello world')
|
|
253
|
+
const blockstore = new MemoryBlockstore()
|
|
254
|
+
const chunkedBlockstore = new MemoryBlockstore()
|
|
255
|
+
const singleBufferCID = await processFileToIPLDFormat(
|
|
256
|
+
blockstore,
|
|
257
|
+
bufferToIterable(buffer),
|
|
258
|
+
buffer.length,
|
|
259
|
+
'test.txt',
|
|
260
|
+
)
|
|
261
|
+
const chunkedBufferCID = await processFileToIPLDFormat(
|
|
262
|
+
chunkedBlockstore,
|
|
263
|
+
separateBufferToIterable(buffer, 5),
|
|
264
|
+
buffer.length,
|
|
265
|
+
'test.txt',
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
expect(singleBufferCID).toEqual(chunkedBufferCID)
|
|
190
269
|
})
|
|
191
270
|
})
|
|
192
271
|
})
|
|
272
|
+
|
|
273
|
+
const bufferToIterable = (buffer: Buffer): AsyncIterable<Buffer> => {
|
|
274
|
+
return (async function* () {
|
|
275
|
+
yield buffer
|
|
276
|
+
})()
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const separateBufferToIterable = (buffer: Buffer, chunkSize: number): AsyncIterable<Buffer> => {
|
|
280
|
+
return (async function* () {
|
|
281
|
+
while (buffer.length > 0) {
|
|
282
|
+
yield buffer.subarray(0, chunkSize)
|
|
283
|
+
buffer = buffer.subarray(chunkSize)
|
|
284
|
+
}
|
|
285
|
+
})()
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const nodesFromBlockstore = async (blockstore: BaseBlockstore): Promise<PBNode[]> => {
|
|
289
|
+
const nodes: PBNode[] = []
|
|
290
|
+
for await (const pair of blockstore.getAll()) {
|
|
291
|
+
nodes.push(decodeNode(pair.block))
|
|
292
|
+
}
|
|
293
|
+
return nodes
|
|
294
|
+
}
|
package/tests/cid.spec.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createNode } from '@ipld/dag-pb'
|
|
2
1
|
import { CID } from 'multiformats'
|
|
3
2
|
import { cidOfNode, cidToString, stringToCid } from '../src/cid/index.js'
|
|
3
|
+
import { createNode } from '../src/ipld/index.js'
|
|
4
4
|
|
|
5
5
|
const randomCIDString = cidOfNode(createNode(new Uint8Array([]), [])).toString()
|
|
6
6
|
|
package/tests/nodes.spec.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { createNode } from '@ipld/dag-pb'
|
|
2
1
|
import {
|
|
3
2
|
cidOfNode,
|
|
4
3
|
createChunkedFileIpldNode,
|
|
5
4
|
createFileChunkIpldNode,
|
|
6
5
|
createSingleFileIpldNode,
|
|
7
6
|
} from '../src/index.js'
|
|
7
|
+
import { createNode } from '../src/ipld/index.js'
|
|
8
8
|
import { IPLDNodeData, MetadataType } from '../src/metadata/onchain/protobuf/OnchainMetadata.js'
|
|
9
9
|
|
|
10
10
|
describe('node creation', () => {
|