@autonomys/auto-dag-data 1.0.7 → 1.0.9
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/README.md +26 -16
- package/dist/compression/index.d.ts.map +1 -1
- package/dist/compression/index.js +1 -1
- package/dist/encryption/index.d.ts +3 -2
- package/dist/encryption/index.d.ts.map +1 -1
- package/dist/ipld/chunker.d.ts +4 -2
- package/dist/ipld/chunker.d.ts.map +1 -1
- package/dist/ipld/chunker.js +23 -21
- package/dist/ipld/nodes.d.ts.map +1 -1
- package/dist/ipld/nodes.js +12 -11
- package/jest.config.ts +1 -0
- package/package.json +2 -2
- package/src/compression/index.ts +1 -1
- package/src/encryption/index.ts +3 -2
- package/src/ipld/chunker.ts +17 -15
- package/src/ipld/nodes.ts +12 -11
- package/tests/chunker.spec.ts +133 -4
- package/tests/compression.spec.ts +81 -0
- package/tests/encryption.spec.ts +21 -0
- package/tests/fileRetrievability.spec.ts +181 -0
- package/tests/nodes.spec.ts +25 -2
- package/tests/offchainMetadata.spec.ts +149 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { MemoryBlockstore } from 'blockstore-core'
|
|
2
|
+
import {
|
|
3
|
+
childrenMetadataFromNode,
|
|
4
|
+
cidOfNode,
|
|
5
|
+
cidToString,
|
|
6
|
+
CompressionAlgorithm,
|
|
7
|
+
createFolderIpldNode,
|
|
8
|
+
createNode,
|
|
9
|
+
createSingleFileIpldNode,
|
|
10
|
+
EncryptionAlgorithm,
|
|
11
|
+
fileMetadata,
|
|
12
|
+
folderMetadata,
|
|
13
|
+
processFolderToIPLDFormat,
|
|
14
|
+
} from '../src'
|
|
15
|
+
|
|
16
|
+
export const stringifyWithBigInt = (obj: any) => {
|
|
17
|
+
return JSON.stringify(obj, (key, value) => (typeof value === 'bigint' ? value.toString() : value))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('offchain metadata', () => {
|
|
21
|
+
describe('file metadata', () => {
|
|
22
|
+
it('matches expected structure', () => {
|
|
23
|
+
const buffer = Buffer.from('hello world')
|
|
24
|
+
const cid = cidOfNode(createSingleFileIpldNode(buffer, 'test.txt'))
|
|
25
|
+
const chunks = [{ size: BigInt(11).valueOf(), cid: cidToString(cid) }]
|
|
26
|
+
const metadata = fileMetadata(cid, chunks, BigInt(buffer.length), 'test.txt')
|
|
27
|
+
expect(metadata.name).toBe('test.txt')
|
|
28
|
+
expect(metadata.totalSize === BigInt(buffer.length)).toBe(true)
|
|
29
|
+
expect(metadata.type).toBe('file')
|
|
30
|
+
expect(metadata.dataCid).toBe(cidToString(cid))
|
|
31
|
+
expect(stringifyWithBigInt(metadata.chunks)).toEqual(stringifyWithBigInt(chunks))
|
|
32
|
+
expect(metadata.uploadOptions).toEqual({
|
|
33
|
+
compression: undefined,
|
|
34
|
+
encryption: undefined,
|
|
35
|
+
})
|
|
36
|
+
expect(metadata.totalChunks).toBe(1)
|
|
37
|
+
expect(metadata.mimeType).toBeUndefined()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('matches expected structure with mime type', () => {
|
|
41
|
+
const buffer = Buffer.from('hello world')
|
|
42
|
+
const cid = cidOfNode(createSingleFileIpldNode(buffer, 'test.txt'))
|
|
43
|
+
const chunks = [{ size: BigInt(11).valueOf(), cid: cidToString(cid) }]
|
|
44
|
+
const metadata = fileMetadata(cid, chunks, BigInt(buffer.length), 'test.txt', 'text/plain')
|
|
45
|
+
expect(metadata.name).toBe('test.txt')
|
|
46
|
+
expect(metadata.totalSize === BigInt(buffer.length)).toBe(true)
|
|
47
|
+
expect(metadata.type).toBe('file')
|
|
48
|
+
expect(metadata.dataCid).toBe(cidToString(cid))
|
|
49
|
+
expect(stringifyWithBigInt(metadata.chunks)).toEqual(stringifyWithBigInt(chunks))
|
|
50
|
+
expect(metadata.uploadOptions).toEqual({
|
|
51
|
+
compression: undefined,
|
|
52
|
+
encryption: undefined,
|
|
53
|
+
})
|
|
54
|
+
expect(metadata.mimeType).toBe('text/plain')
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('folder metadata', () => {
|
|
59
|
+
it('matches expected structure', async () => {
|
|
60
|
+
const CIDs = Array.from({ length: 10 }, () =>
|
|
61
|
+
cidOfNode(createNode(Buffer.from(Math.random().toString()))),
|
|
62
|
+
)
|
|
63
|
+
const name = 'test'
|
|
64
|
+
const childSize = BigInt(1000)
|
|
65
|
+
const blockstore = new MemoryBlockstore()
|
|
66
|
+
const folder = await processFolderToIPLDFormat(blockstore, CIDs, 'test', childSize)
|
|
67
|
+
const children = CIDs.map((cid) => ({
|
|
68
|
+
cid: cidToString(cid),
|
|
69
|
+
totalSize: childSize,
|
|
70
|
+
type: 'file' as const,
|
|
71
|
+
}))
|
|
72
|
+
|
|
73
|
+
const metadata = folderMetadata(cidToString(folder), children, name)
|
|
74
|
+
|
|
75
|
+
const totalSize = childSize * BigInt(CIDs.length)
|
|
76
|
+
expect(metadata.name).toBe(name)
|
|
77
|
+
expect(metadata.totalSize.toString()).toBe(totalSize.toString())
|
|
78
|
+
expect(metadata.type).toBe('folder')
|
|
79
|
+
expect(metadata.dataCid).toBe(cidToString(folder))
|
|
80
|
+
expect(stringifyWithBigInt(metadata.children)).toEqual(stringifyWithBigInt(children))
|
|
81
|
+
expect(metadata.uploadOptions).toEqual({
|
|
82
|
+
compression: undefined,
|
|
83
|
+
encryption: undefined,
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('matches expected structure with upload options', async () => {
|
|
88
|
+
const CIDs = Array.from({ length: 10 }, () =>
|
|
89
|
+
cidOfNode(createNode(Buffer.from(Math.random().toString()))),
|
|
90
|
+
)
|
|
91
|
+
const childSize = BigInt(1000)
|
|
92
|
+
const name = 'test'
|
|
93
|
+
const blockstore = new MemoryBlockstore()
|
|
94
|
+
const folder = await processFolderToIPLDFormat(blockstore, CIDs, name, childSize)
|
|
95
|
+
const children = CIDs.map((cid) => ({
|
|
96
|
+
cid: cidToString(cid),
|
|
97
|
+
totalSize: childSize,
|
|
98
|
+
type: 'file' as const,
|
|
99
|
+
}))
|
|
100
|
+
|
|
101
|
+
const metadata = folderMetadata(cidToString(folder), children, name, {
|
|
102
|
+
compression: {
|
|
103
|
+
algorithm: CompressionAlgorithm.ZLIB,
|
|
104
|
+
level: 1,
|
|
105
|
+
},
|
|
106
|
+
encryption: {
|
|
107
|
+
algorithm: EncryptionAlgorithm.AES_256_GCM,
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const totalSize = childSize * BigInt(CIDs.length)
|
|
112
|
+
expect(metadata.name).toBe(name)
|
|
113
|
+
expect(metadata.totalSize.toString()).toBe(totalSize.toString())
|
|
114
|
+
expect(metadata.type).toBe('folder')
|
|
115
|
+
expect(metadata.dataCid).toBe(cidToString(folder))
|
|
116
|
+
expect(stringifyWithBigInt(metadata.children)).toEqual(stringifyWithBigInt(children))
|
|
117
|
+
expect(metadata.uploadOptions).toEqual({
|
|
118
|
+
compression: {
|
|
119
|
+
algorithm: CompressionAlgorithm.ZLIB,
|
|
120
|
+
level: 1,
|
|
121
|
+
},
|
|
122
|
+
encryption: {
|
|
123
|
+
algorithm: EncryptionAlgorithm.AES_256_GCM,
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('file children metadata from node', () => {
|
|
129
|
+
const node = createSingleFileIpldNode(Buffer.from('hello world'), 'test.txt')
|
|
130
|
+
const metadata = childrenMetadataFromNode(node)
|
|
131
|
+
expect(metadata.cid).toBe(cidToString(cidOfNode(node)))
|
|
132
|
+
expect(metadata.totalSize.toString()).toBe('11')
|
|
133
|
+
expect(metadata.type).toBe('file')
|
|
134
|
+
expect(metadata.name).toBe('test.txt')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('folder children metadata from node', () => {
|
|
138
|
+
const name = 'test'
|
|
139
|
+
const size = BigInt(1000)
|
|
140
|
+
const cid = cidOfNode(createFolderIpldNode([], name, 0, size))
|
|
141
|
+
const node = createFolderIpldNode([cid], name, 0, size)
|
|
142
|
+
const metadata = childrenMetadataFromNode(node)
|
|
143
|
+
expect(metadata.cid).toBe(cidToString(cidOfNode(node)))
|
|
144
|
+
expect(metadata.totalSize.toString()).toBe(size.toString())
|
|
145
|
+
expect(metadata.type).toBe('folder')
|
|
146
|
+
expect(metadata.name).toBe(name)
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
})
|