@autonomys/auto-dag-data 1.0.7 → 1.0.8
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/ipld/chunker.d.ts +3 -2
- package/dist/ipld/chunker.d.ts.map +1 -1
- package/dist/ipld/chunker.js +22 -20
- package/dist/ipld/nodes.js +11 -11
- package/package.json +2 -2
- package/src/ipld/chunker.ts +16 -14
- package/src/ipld/nodes.ts +11 -11
- package/tests/nodes.spec.ts +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
## Overview
|
|
10
10
|
|
|
11
|
-
The **Autonomys Auto
|
|
11
|
+
The **Autonomys Auto Dag Data SDK** (`@autonomys/auto-dag-data`) provides utilities for creating and managing IPLD DAGs (InterPlanetary Linked Data Directed Acyclic Graphs) for files and folders. It facilitates chunking large files, handling metadata, and creating folder structures suitable for distributed storage systems like IPFS.
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
@@ -20,7 +20,7 @@ The **Autonomys Auto DAG Data SDK** (`@autonomys/auto-dag-data`) provides utilit
|
|
|
20
20
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
23
|
-
You can install Auto-
|
|
23
|
+
You can install Auto-Dag-Data using npm or yarn:
|
|
24
24
|
|
|
25
25
|
```bash
|
|
26
26
|
npm install @autonomys/auto-dag-data
|
|
@@ -36,23 +36,27 @@ yarn add @autonomys/auto-dag-data
|
|
|
36
36
|
|
|
37
37
|
### Creating an IPLD DAG from a File
|
|
38
38
|
|
|
39
|
-
To create an IPLD DAG from a file, you can use the `
|
|
39
|
+
To create an IPLD DAG from a file, you can use the `processFileToIPLDFormat` function:
|
|
40
40
|
|
|
41
41
|
```typescript
|
|
42
|
-
import {
|
|
42
|
+
import { processFileToIPLDFormat } from '@autonomys/auto-dag-data'
|
|
43
|
+
import { MemoryBlockstore } from 'blockstore-core/memory'
|
|
43
44
|
import fs from 'fs'
|
|
44
45
|
|
|
45
|
-
const
|
|
46
|
+
const fileStream = fs.createReadStream('path/to/your/file.txt')
|
|
47
|
+
const fileSize = fs.statSync('path/to/your/file.txt').size
|
|
46
48
|
|
|
47
|
-
const
|
|
49
|
+
const blockstore = new MemoryBlockstore()
|
|
50
|
+
const fileCID = processFileToIPLDFormat(blockstore, fileStream, totalSize, 'file.txt')
|
|
48
51
|
```
|
|
49
52
|
|
|
50
53
|
### Creating an IPLD DAG from a Folder
|
|
51
54
|
|
|
52
|
-
To
|
|
55
|
+
To generate an IPLD DAG from a folder, you can use the `processFolderToIPLDFormat` function:
|
|
53
56
|
|
|
54
57
|
```typescript
|
|
55
|
-
import {
|
|
58
|
+
import { processFolderToIPLDFormat, decodeNode } from '@autonomys/auto-dag-data'
|
|
59
|
+
import { MemoryBlockstore } from 'blockstore-core/memory'
|
|
56
60
|
import { CID } from 'multiformats'
|
|
57
61
|
|
|
58
62
|
// Example child CIDs and folder information
|
|
@@ -60,9 +64,12 @@ const childCIDs: CID[] = [
|
|
|
60
64
|
/* array of CIDs */
|
|
61
65
|
]
|
|
62
66
|
const folderName = 'my-folder'
|
|
63
|
-
const folderSize = 1024 // size in bytes
|
|
67
|
+
const folderSize = 1024 // size in bytes (the sum of their children size)
|
|
64
68
|
|
|
65
|
-
const
|
|
69
|
+
const blockstore = new MemoryBlockstore()
|
|
70
|
+
const folderCID = processFolderToIPLDFormat(blockstore, childCIDs, folderName, folderSize)
|
|
71
|
+
|
|
72
|
+
const node = decodeNode(blockstore.get(folderCID))
|
|
66
73
|
```
|
|
67
74
|
|
|
68
75
|
### Working with CIDs
|
|
@@ -115,14 +122,16 @@ const metadataNode = createMetadataNode(metadata)
|
|
|
115
122
|
### Example: Creating a File DAG and Converting to CID
|
|
116
123
|
|
|
117
124
|
```typescript
|
|
118
|
-
import {
|
|
125
|
+
import { processFileToIPLDFormat } from '@autonomys/auto-dag-data'
|
|
126
|
+
import { MemoryBlockstore } from 'blockstore-core/memory'
|
|
119
127
|
import fs from 'fs'
|
|
120
128
|
|
|
121
|
-
const
|
|
129
|
+
const fileStream = fs.createReadStream('path/to/your/file.txt')
|
|
130
|
+
const fileSize = fs.statSync('path/to/your/file.txt').size
|
|
122
131
|
|
|
123
|
-
const
|
|
132
|
+
const blockstore = new MemoryBlockstore()
|
|
133
|
+
const cid = processFileToIPLDFormat(blockstore, fileStream, totalSize, 'file.txt')
|
|
124
134
|
|
|
125
|
-
const cid = cidOfNode(dag.headCID)
|
|
126
135
|
const cidString = cidToString(cid)
|
|
127
136
|
|
|
128
137
|
console.log(`CID of the file DAG: ${cidString}`)
|
|
@@ -137,13 +146,14 @@ import {
|
|
|
137
146
|
cidToString,
|
|
138
147
|
type OffchainMetadata,
|
|
139
148
|
} from '@autonomys/auto-dag-data'
|
|
149
|
+
import { MemoryBlockstore } from 'blockstore-core/memory'
|
|
140
150
|
import fs from 'fs'
|
|
141
151
|
|
|
142
152
|
const metadata: OffchainMetadata = fs.readFileSync('path/to/your/metadata.json')
|
|
143
153
|
|
|
144
|
-
const
|
|
154
|
+
const blockstore = new MemoryBlockstore()
|
|
155
|
+
const cid = processMetadataToIPLDFormat(blockstore, metadata)
|
|
145
156
|
|
|
146
|
-
const cid = cidOfNode(dag.headCID)
|
|
147
157
|
const cidString = cidToString(cid)
|
|
148
158
|
|
|
149
159
|
console.log(`CID of the metadata DAG: ${cidString}`)
|
package/dist/ipld/chunker.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ type ChunkerLimits = {
|
|
|
9
9
|
maxLinkPerNode: number;
|
|
10
10
|
};
|
|
11
11
|
type ChunkerOptions = ChunkerLimits & FileUploadOptions;
|
|
12
|
+
export declare const DEFAULT_NODE_MAX_SIZE = 65535;
|
|
12
13
|
export declare const NODE_METADATA_SIZE: number;
|
|
13
14
|
export declare const DEFAULT_MAX_CHUNK_SIZE: number;
|
|
14
15
|
export declare const LINK_SIZE_IN_BYTES = 40;
|
|
@@ -24,8 +25,8 @@ export declare const processFolderToIPLDFormat: (blockstore: BaseBlockstore, chi
|
|
|
24
25
|
* Process chunks to IPLD format, return the last chunk if it's not full
|
|
25
26
|
* @returns the last chunk if it's not full, otherwise an empty buffer
|
|
26
27
|
*/
|
|
27
|
-
export declare const processChunksToIPLDFormat: (blockstore: BaseBlockstore, chunks: AwaitIterable<Buffer>, builders: Builders, {
|
|
28
|
-
|
|
28
|
+
export declare const processChunksToIPLDFormat: (blockstore: BaseBlockstore, chunks: AwaitIterable<Buffer>, builders: Builders, { maxChunkSize }?: {
|
|
29
|
+
maxChunkSize?: number;
|
|
29
30
|
}) => Promise<Buffer>;
|
|
30
31
|
export declare const ensureNodeMaxSize: (node: PBNode, maxSize?: number) => PBNode;
|
|
31
32
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chunker.d.ts","sourceRoot":"","sources":["../../src/ipld/chunker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAElC,OAAO,EAAsB,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE9F,OAAO,EAAE,QAAQ,EAAkC,MAAM,eAAe,CAAA;AAExE,OAAO,EAA2B,MAAM,EAAE,MAAM,YAAY,CAAA;AAE5D,KAAK,aAAa,GAAG;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,KAAK,cAAc,GAAG,aAAa,GAAG,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"chunker.d.ts","sourceRoot":"","sources":["../../src/ipld/chunker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAElC,OAAO,EAAsB,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE9F,OAAO,EAAE,QAAQ,EAAkC,MAAM,eAAe,CAAA;AAExE,OAAO,EAA2B,MAAM,EAAE,MAAM,YAAY,CAAA;AAE5D,KAAK,aAAa,GAAG;IACnB,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,KAAK,cAAc,GAAG,aAAa,GAAG,iBAAiB,CAAA;AAEvD,eAAO,MAAM,qBAAqB,QAAQ,CAAA;AAiB1C,eAAO,MAAM,kBAAkB,QAML,CAAA;AAE1B,eAAO,MAAM,sBAAsB,QAA6C,CAAA;AAEhF,eAAO,MAAM,kBAAkB,KAAK,CAAA;AACpC,eAAO,MAAM,yBAAyB,QAA0D,CAAA;AAEhG,eAAO,MAAM,uBAAuB,eACtB,cAAc,QACpB,aAAa,CAAC,MAAM,CAAC,aAChB,MAAM,aACN,MAAM,8DAMd,OAAO,CAAC,cAAc,CAAC,KAMzB,OAAO,CAAC,GAAG,CAWb,CAAA;AAED,eAAO,MAAM,2BAA2B,eAC1B,cAAc,YAChB,gBAAgB,WAClB;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,KAItD,OAAO,CAAC,GAAG,CAiBb,CAAA;AA0CD,eAAO,MAAM,mCAAmC,eAClC,cAAc,UAClB,aAAa,CAAC,GAAG,CAAC,YAChB,MAAM,GAAG,SAAS,aACjB,MAAM,YACP,QAAQ,2EAMf,OAAO,CAAC,cAAc,CAAC,KAMzB,OAAO,CAAC,GAAG,CAgDb,CAAA;AAED,eAAO,MAAM,yBAAyB,eACxB,cAAc,YAChB,GAAG,EAAE,QACT,MAAM,QACN,MAAM,2EAMT,OAAO,CAAC,cAAc,CAAC,KAMzB,OAAO,CAAC,GAAG,CA4Bb,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,yBAAyB,eACxB,cAAc,UAClB,aAAa,CAAC,MAAM,CAAC,YACnB,QAAQ,qBACyB;IAAE,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,KAGnE,OAAO,CAAC,MAAM,CAiBhB,CAAA;AAED,eAAO,MAAM,iBAAiB,SACtB,MAAM,YACH,MAAM,KACd,MAOF,CAAA"}
|
package/dist/ipld/chunker.js
CHANGED
|
@@ -33,7 +33,7 @@ import { stringifyMetadata } from '../utils/metadata.js';
|
|
|
33
33
|
import { fileBuilders, metadataBuilders } from './builders.js';
|
|
34
34
|
import { createFolderInlinkIpldNode, createFolderIpldNode } from './nodes.js';
|
|
35
35
|
import { chunkBuffer, encodeNode } from './utils.js';
|
|
36
|
-
const DEFAULT_NODE_MAX_SIZE = 65535;
|
|
36
|
+
export const DEFAULT_NODE_MAX_SIZE = 65535;
|
|
37
37
|
// u8 -> 1 byte (may grow in the future but unlikely further than 255)
|
|
38
38
|
const NODE_TYPE_SIZE = 1;
|
|
39
39
|
// u32 -> 4 bytes
|
|
@@ -57,8 +57,8 @@ export const NODE_METADATA_SIZE = NODE_TYPE_SIZE +
|
|
|
57
57
|
export const DEFAULT_MAX_CHUNK_SIZE = DEFAULT_NODE_MAX_SIZE - NODE_METADATA_SIZE;
|
|
58
58
|
export const LINK_SIZE_IN_BYTES = 40;
|
|
59
59
|
export const DEFAULT_MAX_LINK_PER_NODE = Math.floor(DEFAULT_MAX_CHUNK_SIZE / LINK_SIZE_IN_BYTES);
|
|
60
|
-
export const processFileToIPLDFormat = (blockstore, file, totalSize, filename, { maxNodeSize =
|
|
61
|
-
maxNodeSize:
|
|
60
|
+
export const processFileToIPLDFormat = (blockstore, file, totalSize, filename, { maxNodeSize = DEFAULT_NODE_MAX_SIZE, maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE, encryption = undefined, compression = undefined, } = {
|
|
61
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
62
62
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
63
63
|
encryption: undefined,
|
|
64
64
|
compression: undefined,
|
|
@@ -74,7 +74,7 @@ export const processFileToIPLDFormat = (blockstore, file, totalSize, filename, {
|
|
|
74
74
|
});
|
|
75
75
|
};
|
|
76
76
|
export const processMetadataToIPLDFormat = (blockstore_1, metadata_1, ...args_1) => __awaiter(void 0, [blockstore_1, metadata_1, ...args_1], void 0, function* (blockstore, metadata, limits = {
|
|
77
|
-
maxNodeSize:
|
|
77
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
78
78
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
79
79
|
}) {
|
|
80
80
|
if (metadata.name && metadata.name.length > MAX_NAME_SIZE) {
|
|
@@ -87,8 +87,8 @@ export const processMetadataToIPLDFormat = (blockstore_1, metadata_1, ...args_1)
|
|
|
87
87
|
});
|
|
88
88
|
})(), metadata.name, BigInt(buffer.byteLength), metadataBuilders, limits);
|
|
89
89
|
});
|
|
90
|
-
const processBufferToIPLDFormat = (blockstore_1, buffer_1, filename_1, totalSize_1, builders_1, ...args_1) => __awaiter(void 0, [blockstore_1, buffer_1, filename_1, totalSize_1, builders_1, ...args_1], void 0, function* (blockstore, buffer, filename, totalSize, builders, { maxNodeSize: maxNodeSize =
|
|
91
|
-
maxNodeSize:
|
|
90
|
+
const processBufferToIPLDFormat = (blockstore_1, buffer_1, filename_1, totalSize_1, builders_1, ...args_1) => __awaiter(void 0, [blockstore_1, buffer_1, filename_1, totalSize_1, builders_1, ...args_1], void 0, function* (blockstore, buffer, filename, totalSize, builders, { maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE, maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE, encryption = undefined, compression = undefined, } = {
|
|
91
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
92
92
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
93
93
|
encryption: undefined,
|
|
94
94
|
compression: undefined,
|
|
@@ -124,8 +124,8 @@ const processBufferToIPLDFormat = (blockstore_1, buffer_1, filename_1, totalSize
|
|
|
124
124
|
compression,
|
|
125
125
|
});
|
|
126
126
|
});
|
|
127
|
-
export const processBufferToIPLDFormatFromChunks = (blockstore_1, chunks_1, filename_1, totalSize_1, builders_1, ...args_1) => __awaiter(void 0, [blockstore_1, chunks_1, filename_1, totalSize_1, builders_1, ...args_1], void 0, function* (blockstore, chunks, filename, totalSize, builders, { maxNodeSize: maxNodeSize =
|
|
128
|
-
maxNodeSize:
|
|
127
|
+
export const processBufferToIPLDFormatFromChunks = (blockstore_1, chunks_1, filename_1, totalSize_1, builders_1, ...args_1) => __awaiter(void 0, [blockstore_1, chunks_1, filename_1, totalSize_1, builders_1, ...args_1], void 0, function* (blockstore, chunks, filename, totalSize, builders, { maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE, maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE, encryption = undefined, compression = undefined, } = {
|
|
128
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
129
129
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
130
130
|
encryption: undefined,
|
|
131
131
|
compression: undefined,
|
|
@@ -186,9 +186,9 @@ export const processBufferToIPLDFormatFromChunks = (blockstore_1, chunks_1, file
|
|
|
186
186
|
yield blockstore.put(headCID, encodeNode(head));
|
|
187
187
|
return headCID;
|
|
188
188
|
});
|
|
189
|
-
export const processFolderToIPLDFormat = (blockstore_1, children_1, name_1, size_1, ...args_1) => __awaiter(void 0, [blockstore_1, children_1, name_1, size_1, ...args_1], void 0, function* (blockstore, children, name, size, { maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE, maxNodeSize: maxNodeSize =
|
|
189
|
+
export const processFolderToIPLDFormat = (blockstore_1, children_1, name_1, size_1, ...args_1) => __awaiter(void 0, [blockstore_1, children_1, name_1, size_1, ...args_1], void 0, function* (blockstore, children, name, size, { maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE, maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE, compression = undefined, encryption = undefined, } = {
|
|
190
190
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
191
|
-
maxNodeSize:
|
|
191
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
192
192
|
compression: undefined,
|
|
193
193
|
encryption: undefined,
|
|
194
194
|
}) {
|
|
@@ -221,18 +221,20 @@ export const processFolderToIPLDFormat = (blockstore_1, children_1, name_1, size
|
|
|
221
221
|
* Process chunks to IPLD format, return the last chunk if it's not full
|
|
222
222
|
* @returns the last chunk if it's not full, otherwise an empty buffer
|
|
223
223
|
*/
|
|
224
|
-
export const processChunksToIPLDFormat = (blockstore_1, chunks_1, builders_1,
|
|
225
|
-
|
|
224
|
+
export const processChunksToIPLDFormat = (blockstore_1, chunks_1, builders_1, ...args_1) => __awaiter(void 0, [blockstore_1, chunks_1, builders_1, ...args_1], void 0, function* (blockstore, chunks, builders, { maxChunkSize = DEFAULT_MAX_CHUNK_SIZE } = {
|
|
225
|
+
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
|
|
226
|
+
}) {
|
|
227
|
+
var _a, e_3, _b, _c;
|
|
226
228
|
const bufferChunks = chunkBuffer(chunks, {
|
|
227
|
-
maxChunkSize
|
|
229
|
+
maxChunkSize,
|
|
228
230
|
ignoreLastChunk: false,
|
|
229
231
|
});
|
|
230
232
|
try {
|
|
231
|
-
for (var
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const chunk =
|
|
235
|
-
if (chunk.byteLength <
|
|
233
|
+
for (var _d = true, bufferChunks_2 = __asyncValues(bufferChunks), bufferChunks_2_1; bufferChunks_2_1 = yield bufferChunks_2.next(), _a = bufferChunks_2_1.done, !_a; _d = true) {
|
|
234
|
+
_c = bufferChunks_2_1.value;
|
|
235
|
+
_d = false;
|
|
236
|
+
const chunk = _c;
|
|
237
|
+
if (chunk.byteLength < maxChunkSize) {
|
|
236
238
|
return chunk;
|
|
237
239
|
}
|
|
238
240
|
const node = builders.chunk(chunk);
|
|
@@ -243,13 +245,13 @@ export const processChunksToIPLDFormat = (blockstore_1, chunks_1, builders_1, _a
|
|
|
243
245
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
244
246
|
finally {
|
|
245
247
|
try {
|
|
246
|
-
if (!
|
|
248
|
+
if (!_d && !_a && (_b = bufferChunks_2.return)) yield _b.call(bufferChunks_2);
|
|
247
249
|
}
|
|
248
250
|
finally { if (e_3) throw e_3.error; }
|
|
249
251
|
}
|
|
250
252
|
return Buffer.alloc(0);
|
|
251
253
|
});
|
|
252
|
-
export const ensureNodeMaxSize = (node, maxSize =
|
|
254
|
+
export const ensureNodeMaxSize = (node, maxSize = DEFAULT_NODE_MAX_SIZE) => {
|
|
253
255
|
const nodeSize = encodeNode(node).byteLength;
|
|
254
256
|
if (nodeSize > maxSize) {
|
|
255
257
|
throw new Error(`Node is too large to fit in a single chunk: ${nodeSize} > ${maxSize}`);
|
package/dist/ipld/nodes.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { encodeIPLDNodeData, MetadataType } from '../metadata/onchain/index.js';
|
|
2
2
|
import { stringifyMetadata } from '../utils/metadata.js';
|
|
3
|
-
import {
|
|
3
|
+
import { DEFAULT_NODE_MAX_SIZE, ensureNodeMaxSize } from './chunker.js';
|
|
4
4
|
import { createNode } from './index.js';
|
|
5
5
|
/// Creates a file chunk ipld node
|
|
6
|
-
export const createFileChunkIpldNode = (data, maxNodeSize =
|
|
6
|
+
export const createFileChunkIpldNode = (data, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
7
7
|
type: MetadataType.FileChunk,
|
|
8
8
|
size: BigInt(data.length).valueOf(),
|
|
9
9
|
linkDepth: 0,
|
|
@@ -12,7 +12,7 @@ export const createFileChunkIpldNode = (data, maxNodeSize = DEFAULT_MAX_CHUNK_SI
|
|
|
12
12
|
// Creates a file ipld node
|
|
13
13
|
// links: the CIDs of the file's contents
|
|
14
14
|
// @todo: add the file's metadata
|
|
15
|
-
export const createChunkedFileIpldNode = (links, size, linkDepth, name, maxNodeSize =
|
|
15
|
+
export const createChunkedFileIpldNode = (links, size, linkDepth, name, maxNodeSize = DEFAULT_NODE_MAX_SIZE, uploadOptions) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
16
16
|
type: MetadataType.File,
|
|
17
17
|
name,
|
|
18
18
|
size,
|
|
@@ -21,7 +21,7 @@ export const createChunkedFileIpldNode = (links, size, linkDepth, name, maxNodeS
|
|
|
21
21
|
}), links.map((cid) => ({ Hash: cid }))), maxNodeSize);
|
|
22
22
|
// Creates a file ipld node
|
|
23
23
|
// links: the CIDs of the file's contents
|
|
24
|
-
export const createFileInlinkIpldNode = (links, size, linkDepth, maxNodeSize =
|
|
24
|
+
export const createFileInlinkIpldNode = (links, size, linkDepth, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
25
25
|
type: MetadataType.FileInlink,
|
|
26
26
|
size: BigInt(size).valueOf(),
|
|
27
27
|
linkDepth,
|
|
@@ -29,7 +29,7 @@ export const createFileInlinkIpldNode = (links, size, linkDepth, maxNodeSize = D
|
|
|
29
29
|
// Creates a file ipld node
|
|
30
30
|
// links: the CIDs of the file's contents
|
|
31
31
|
// @todo: add the file's metadata
|
|
32
|
-
export const createSingleFileIpldNode = (data, name, uploadOptions, maxNodeSize =
|
|
32
|
+
export const createSingleFileIpldNode = (data, name, uploadOptions, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
33
33
|
type: MetadataType.File,
|
|
34
34
|
name,
|
|
35
35
|
size: BigInt(data.length).valueOf(),
|
|
@@ -40,7 +40,7 @@ export const createSingleFileIpldNode = (data, name, uploadOptions, maxNodeSize
|
|
|
40
40
|
// Creates a file ipld node
|
|
41
41
|
// links: the CIDs of the file's contents
|
|
42
42
|
// @todo: add the file's metadata
|
|
43
|
-
export const createMetadataInlinkIpldNode = (links, size, linkDepth, maxNodeSize =
|
|
43
|
+
export const createMetadataInlinkIpldNode = (links, size, linkDepth, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
44
44
|
type: MetadataType.FileInlink,
|
|
45
45
|
size: BigInt(size).valueOf(),
|
|
46
46
|
linkDepth,
|
|
@@ -55,13 +55,13 @@ export const createSingleMetadataIpldNode = (data, name) => createNode(encodeIPL
|
|
|
55
55
|
linkDepth: 0,
|
|
56
56
|
data,
|
|
57
57
|
}), []);
|
|
58
|
-
export const createMetadataChunkIpldNode = (data, maxNodeSize =
|
|
58
|
+
export const createMetadataChunkIpldNode = (data, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
59
59
|
type: MetadataType.MetadataChunk,
|
|
60
60
|
size: BigInt(data.length).valueOf(),
|
|
61
61
|
linkDepth: 0,
|
|
62
62
|
data,
|
|
63
63
|
})), maxNodeSize);
|
|
64
|
-
export const createChunkedMetadataIpldNode = (links, size, linkDepth, name, maxNodeSize =
|
|
64
|
+
export const createChunkedMetadataIpldNode = (links, size, linkDepth, name, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
65
65
|
type: MetadataType.Metadata,
|
|
66
66
|
name,
|
|
67
67
|
size,
|
|
@@ -70,19 +70,19 @@ export const createChunkedMetadataIpldNode = (links, size, linkDepth, name, maxN
|
|
|
70
70
|
// Creates a folder ipld node
|
|
71
71
|
// links: the CIDs of the folder's contents
|
|
72
72
|
// @todo: add the folder's metadata
|
|
73
|
-
export const createFolderIpldNode = (links, name, linkDepth, size, maxNodeSize =
|
|
73
|
+
export const createFolderIpldNode = (links, name, linkDepth, size, maxNodeSize = DEFAULT_NODE_MAX_SIZE, uploadOptions) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
74
74
|
type: MetadataType.Folder,
|
|
75
75
|
name,
|
|
76
76
|
size,
|
|
77
77
|
linkDepth,
|
|
78
78
|
uploadOptions,
|
|
79
79
|
}), links.map((cid) => ({ Hash: cid }))), maxNodeSize);
|
|
80
|
-
export const createFolderInlinkIpldNode = (links, linkDepth, maxNodeSize =
|
|
80
|
+
export const createFolderInlinkIpldNode = (links, linkDepth, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
81
81
|
type: MetadataType.FolderInlink,
|
|
82
82
|
linkDepth,
|
|
83
83
|
}), links.map((cid) => ({ Hash: cid }))), maxNodeSize);
|
|
84
84
|
/// Creates a metadata ipld node
|
|
85
|
-
export const createMetadataNode = (metadata, maxNodeSize =
|
|
85
|
+
export const createMetadataNode = (metadata, maxNodeSize = DEFAULT_NODE_MAX_SIZE) => {
|
|
86
86
|
const data = Buffer.from(stringifyMetadata(metadata));
|
|
87
87
|
return ensureNodeMaxSize(createNode(encodeIPLDNodeData({
|
|
88
88
|
type: MetadataType.Metadata,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autonomys/auto-dag-data",
|
|
3
3
|
"packageManager": "yarn@4.1.1",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"repository": {
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"protons": "^7.6.0",
|
|
49
49
|
"protons-runtime": "^5.5.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "88b1b90467db7dd1e301387451ed727189f9808c"
|
|
52
52
|
}
|
package/src/ipld/chunker.ts
CHANGED
|
@@ -15,7 +15,7 @@ type ChunkerLimits = {
|
|
|
15
15
|
|
|
16
16
|
type ChunkerOptions = ChunkerLimits & FileUploadOptions
|
|
17
17
|
|
|
18
|
-
const DEFAULT_NODE_MAX_SIZE = 65535
|
|
18
|
+
export const DEFAULT_NODE_MAX_SIZE = 65535
|
|
19
19
|
|
|
20
20
|
// u8 -> 1 byte (may grow in the future but unlikely further than 255)
|
|
21
21
|
const NODE_TYPE_SIZE = 1
|
|
@@ -51,12 +51,12 @@ export const processFileToIPLDFormat = (
|
|
|
51
51
|
totalSize: bigint,
|
|
52
52
|
filename?: string,
|
|
53
53
|
{
|
|
54
|
-
maxNodeSize =
|
|
54
|
+
maxNodeSize = DEFAULT_NODE_MAX_SIZE,
|
|
55
55
|
maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE,
|
|
56
56
|
encryption = undefined,
|
|
57
57
|
compression = undefined,
|
|
58
58
|
}: Partial<ChunkerOptions> = {
|
|
59
|
-
maxNodeSize:
|
|
59
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
60
60
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
61
61
|
encryption: undefined,
|
|
62
62
|
compression: undefined,
|
|
@@ -78,7 +78,7 @@ export const processMetadataToIPLDFormat = async (
|
|
|
78
78
|
blockstore: BaseBlockstore,
|
|
79
79
|
metadata: OffchainMetadata,
|
|
80
80
|
limits: { maxNodeSize: number; maxLinkPerNode: number } = {
|
|
81
|
-
maxNodeSize:
|
|
81
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
82
82
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
83
83
|
},
|
|
84
84
|
): Promise<CID> => {
|
|
@@ -107,12 +107,12 @@ const processBufferToIPLDFormat = async (
|
|
|
107
107
|
totalSize: bigint,
|
|
108
108
|
builders: Builders,
|
|
109
109
|
{
|
|
110
|
-
maxNodeSize: maxNodeSize =
|
|
110
|
+
maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE,
|
|
111
111
|
maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE,
|
|
112
112
|
encryption = undefined,
|
|
113
113
|
compression = undefined,
|
|
114
114
|
}: ChunkerOptions = {
|
|
115
|
-
maxNodeSize:
|
|
115
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
116
116
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
117
117
|
encryption: undefined,
|
|
118
118
|
compression: undefined,
|
|
@@ -147,12 +147,12 @@ export const processBufferToIPLDFormatFromChunks = async (
|
|
|
147
147
|
totalSize: bigint,
|
|
148
148
|
builders: Builders,
|
|
149
149
|
{
|
|
150
|
-
maxNodeSize: maxNodeSize =
|
|
150
|
+
maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE,
|
|
151
151
|
maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE,
|
|
152
152
|
encryption = undefined,
|
|
153
153
|
compression = undefined,
|
|
154
154
|
}: Partial<ChunkerOptions> = {
|
|
155
|
-
maxNodeSize:
|
|
155
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
156
156
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
157
157
|
encryption: undefined,
|
|
158
158
|
compression: undefined,
|
|
@@ -214,12 +214,12 @@ export const processFolderToIPLDFormat = async (
|
|
|
214
214
|
size: bigint,
|
|
215
215
|
{
|
|
216
216
|
maxLinkPerNode = DEFAULT_MAX_LINK_PER_NODE,
|
|
217
|
-
maxNodeSize: maxNodeSize =
|
|
217
|
+
maxNodeSize: maxNodeSize = DEFAULT_NODE_MAX_SIZE,
|
|
218
218
|
compression = undefined,
|
|
219
219
|
encryption = undefined,
|
|
220
220
|
}: Partial<ChunkerOptions> = {
|
|
221
221
|
maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
|
|
222
|
-
maxNodeSize:
|
|
222
|
+
maxNodeSize: DEFAULT_NODE_MAX_SIZE,
|
|
223
223
|
compression: undefined,
|
|
224
224
|
encryption: undefined,
|
|
225
225
|
},
|
|
@@ -261,15 +261,17 @@ export const processChunksToIPLDFormat = async (
|
|
|
261
261
|
blockstore: BaseBlockstore,
|
|
262
262
|
chunks: AwaitIterable<Buffer>,
|
|
263
263
|
builders: Builders,
|
|
264
|
-
{
|
|
264
|
+
{ maxChunkSize = DEFAULT_MAX_CHUNK_SIZE }: { maxChunkSize?: number } = {
|
|
265
|
+
maxChunkSize: DEFAULT_MAX_CHUNK_SIZE,
|
|
266
|
+
},
|
|
265
267
|
): Promise<Buffer> => {
|
|
266
268
|
const bufferChunks = chunkBuffer(chunks, {
|
|
267
|
-
maxChunkSize
|
|
269
|
+
maxChunkSize,
|
|
268
270
|
ignoreLastChunk: false,
|
|
269
271
|
})
|
|
270
272
|
|
|
271
273
|
for await (const chunk of bufferChunks) {
|
|
272
|
-
if (chunk.byteLength <
|
|
274
|
+
if (chunk.byteLength < maxChunkSize) {
|
|
273
275
|
return chunk
|
|
274
276
|
}
|
|
275
277
|
|
|
@@ -283,7 +285,7 @@ export const processChunksToIPLDFormat = async (
|
|
|
283
285
|
|
|
284
286
|
export const ensureNodeMaxSize = (
|
|
285
287
|
node: PBNode,
|
|
286
|
-
maxSize: number =
|
|
288
|
+
maxSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
287
289
|
): PBNode => {
|
|
288
290
|
const nodeSize = encodeNode(node).byteLength
|
|
289
291
|
if (nodeSize > maxSize) {
|
package/src/ipld/nodes.ts
CHANGED
|
@@ -2,13 +2,13 @@ import { CID } from 'multiformats/cid'
|
|
|
2
2
|
import { FileUploadOptions, OffchainMetadata } from '../metadata/index.js'
|
|
3
3
|
import { encodeIPLDNodeData, MetadataType } from '../metadata/onchain/index.js'
|
|
4
4
|
import { stringifyMetadata } from '../utils/metadata.js'
|
|
5
|
-
import {
|
|
5
|
+
import { DEFAULT_NODE_MAX_SIZE, ensureNodeMaxSize } from './chunker.js'
|
|
6
6
|
import { createNode, PBNode } from './index.js'
|
|
7
7
|
|
|
8
8
|
/// Creates a file chunk ipld node
|
|
9
9
|
export const createFileChunkIpldNode = (
|
|
10
10
|
data: Buffer,
|
|
11
|
-
maxNodeSize: number =
|
|
11
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
12
12
|
): PBNode =>
|
|
13
13
|
ensureNodeMaxSize(
|
|
14
14
|
createNode(
|
|
@@ -31,7 +31,7 @@ export const createChunkedFileIpldNode = (
|
|
|
31
31
|
size: bigint,
|
|
32
32
|
linkDepth: number,
|
|
33
33
|
name?: string,
|
|
34
|
-
maxNodeSize: number =
|
|
34
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
35
35
|
uploadOptions?: FileUploadOptions,
|
|
36
36
|
): PBNode =>
|
|
37
37
|
ensureNodeMaxSize(
|
|
@@ -53,7 +53,7 @@ export const createFileInlinkIpldNode = (
|
|
|
53
53
|
links: CID[],
|
|
54
54
|
size: number,
|
|
55
55
|
linkDepth: number,
|
|
56
|
-
maxNodeSize: number =
|
|
56
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
57
57
|
): PBNode =>
|
|
58
58
|
ensureNodeMaxSize(
|
|
59
59
|
createNode(
|
|
@@ -74,7 +74,7 @@ export const createSingleFileIpldNode = (
|
|
|
74
74
|
data: Buffer,
|
|
75
75
|
name?: string,
|
|
76
76
|
uploadOptions?: FileUploadOptions,
|
|
77
|
-
maxNodeSize: number =
|
|
77
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
78
78
|
): PBNode =>
|
|
79
79
|
ensureNodeMaxSize(
|
|
80
80
|
createNode(
|
|
@@ -98,7 +98,7 @@ export const createMetadataInlinkIpldNode = (
|
|
|
98
98
|
links: CID[],
|
|
99
99
|
size: number,
|
|
100
100
|
linkDepth: number,
|
|
101
|
-
maxNodeSize: number =
|
|
101
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
102
102
|
): PBNode =>
|
|
103
103
|
ensureNodeMaxSize(
|
|
104
104
|
createNode(
|
|
@@ -129,7 +129,7 @@ export const createSingleMetadataIpldNode = (data: Buffer, name?: string): PBNod
|
|
|
129
129
|
|
|
130
130
|
export const createMetadataChunkIpldNode = (
|
|
131
131
|
data: Buffer,
|
|
132
|
-
maxNodeSize: number =
|
|
132
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
133
133
|
): PBNode =>
|
|
134
134
|
ensureNodeMaxSize(
|
|
135
135
|
createNode(
|
|
@@ -148,7 +148,7 @@ export const createChunkedMetadataIpldNode = (
|
|
|
148
148
|
size: bigint,
|
|
149
149
|
linkDepth: number,
|
|
150
150
|
name?: string,
|
|
151
|
-
maxNodeSize: number =
|
|
151
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
152
152
|
): PBNode =>
|
|
153
153
|
ensureNodeMaxSize(
|
|
154
154
|
createNode(
|
|
@@ -171,7 +171,7 @@ export const createFolderIpldNode = (
|
|
|
171
171
|
name: string,
|
|
172
172
|
linkDepth: number,
|
|
173
173
|
size: bigint,
|
|
174
|
-
maxNodeSize: number =
|
|
174
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
175
175
|
uploadOptions?: FileUploadOptions,
|
|
176
176
|
): PBNode =>
|
|
177
177
|
ensureNodeMaxSize(
|
|
@@ -191,7 +191,7 @@ export const createFolderIpldNode = (
|
|
|
191
191
|
export const createFolderInlinkIpldNode = (
|
|
192
192
|
links: CID[],
|
|
193
193
|
linkDepth: number,
|
|
194
|
-
maxNodeSize: number =
|
|
194
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
195
195
|
): PBNode =>
|
|
196
196
|
ensureNodeMaxSize(
|
|
197
197
|
createNode(
|
|
@@ -207,7 +207,7 @@ export const createFolderInlinkIpldNode = (
|
|
|
207
207
|
/// Creates a metadata ipld node
|
|
208
208
|
export const createMetadataNode = (
|
|
209
209
|
metadata: OffchainMetadata,
|
|
210
|
-
maxNodeSize: number =
|
|
210
|
+
maxNodeSize: number = DEFAULT_NODE_MAX_SIZE,
|
|
211
211
|
): PBNode => {
|
|
212
212
|
const data = Buffer.from(stringifyMetadata(metadata))
|
|
213
213
|
|
package/tests/nodes.spec.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
createFileChunkIpldNode,
|
|
5
5
|
createSingleFileIpldNode,
|
|
6
6
|
} from '../src/index.js'
|
|
7
|
-
import { createNode, DEFAULT_MAX_CHUNK_SIZE } from '../src/ipld/index.js'
|
|
7
|
+
import { createNode, DEFAULT_MAX_CHUNK_SIZE, DEFAULT_NODE_MAX_SIZE } from '../src/ipld/index.js'
|
|
8
8
|
import { IPLDNodeData, MetadataType } from '../src/metadata/onchain/protobuf/OnchainMetadata.js'
|
|
9
9
|
|
|
10
10
|
describe('node creation', () => {
|
|
@@ -30,7 +30,7 @@ describe('node creation', () => {
|
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('single file root node | buffer too large', () => {
|
|
33
|
-
const maxNodeSize =
|
|
33
|
+
const maxNodeSize = DEFAULT_NODE_MAX_SIZE
|
|
34
34
|
const buffer = Buffer.from('h'.repeat(maxNodeSize))
|
|
35
35
|
expect(() => createSingleFileIpldNode(buffer, 'test.txt')).toThrow()
|
|
36
36
|
})
|