@autonomys/auto-drive 0.6.2 → 0.6.4

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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Autonomys Network (autonomys.xyz)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ 1. **Attribution**: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+
14
+ 2. **No Warranty**: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15
+
16
+ 3. **Limitation of Liability**: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+
18
+ ---
package/README.md CHANGED
@@ -1 +1,167 @@
1
- # auto-drive
1
+ # Auto-Drive
2
+
3
+ ![Autonomys Banner](https://github.com/autonomys/auto-sdk/blob/main/.github/images/autonomys-banner.webp)
4
+
5
+ [![Latest Github release](https://img.shields.io/github/v/tag/autonomys/auto-sdk.svg)](https://github.com/autonomys/auto-sdk/tags)
6
+ [![Build status of the main branch on Linux/OSX](https://img.shields.io/github/actions/workflow/status/autonomys/auto-sdk/build.yaml?branch=main&label=Linux%2FOSX%20build)](https://github.com/autonomys/auto-sdk/actions/workflows/build.yaml)
7
+ [![npm version](https://badge.fury.io/js/@autonomys%2Fauto-drive.svg)](https://badge.fury.io/js/@autonomys/auto-drive)
8
+
9
+ ## Overview
10
+
11
+ The **Autonomys Auto Drive SDK** (`@autonomys/auto-drive`) 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
+
13
+ ## Features
14
+
15
+ - **File Chunking and DAG Creation**: Efficiently split large files into smaller chunks and create IPLD DAGs.
16
+ - **Folder Structure Creation**: Generate IPLD DAGs for directory structures.
17
+ - **Metadata Handling**: Add and manage metadata for files and folders.
18
+ - **CID Management**: Utilities for working with Content Identifiers (CIDs).
19
+ - **TypeScript Support**: Fully typed for enhanced developer experience.
20
+
21
+ ## Installation
22
+
23
+ You can install Auto-Drive using npm or yarn:
24
+
25
+ ```bash
26
+ npm install @autonomys/auto-drive
27
+ ```
28
+
29
+ or
30
+
31
+ ```bash
32
+ yarn add @autonomys/auto-drive
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ### Creating an IPLD DAG from a File
38
+
39
+ To create an IPLD DAG from a file, you can use the `createFileIPLDDag` function:
40
+
41
+ ```typescript
42
+ import { createFileIPLDDag } from '@autonomys/auto-drive'
43
+ import fs from 'fs'
44
+
45
+ const fileBuffer = fs.readFileSync('path/to/your/file.txt')
46
+
47
+ const dag = createFileIPLDDag(fileBuffer, 'file.txt')
48
+ ```
49
+
50
+ ### Creating an IPLD DAG from a Folder
51
+
52
+ To create an IPLD DAG from a folder, you can use the `createFolderIPLDDag` function:
53
+
54
+ ```typescript
55
+ import { createFolderIPLDDag } from '@autonomys/auto-drive'
56
+ import { CID } from 'multiformats'
57
+
58
+ // Example child CIDs and folder information
59
+ const childCIDs: CID[] = [
60
+ /* array of CIDs */
61
+ ]
62
+ const folderName = 'my-folder'
63
+ const folderSize = 1024 // size in bytes
64
+
65
+ const folderDag = createFolderIPLDDag(childCIDs, folderName, folderSize)
66
+ ```
67
+
68
+ ### Working with CIDs
69
+
70
+ You can use functions from the `cid` module to work with CIDs:
71
+
72
+ ```typescript
73
+ import { cidOfNode, cidToString, stringToCid } from '@autonomys/auto-drive'
74
+
75
+ // Create a CID from a node
76
+ const cid = cidOfNode(dag.head)
77
+
78
+ // Convert the CID to a string
79
+ const cidString = cidToString(cid)
80
+
81
+ // Parse a string back into a CID
82
+ const parsedCID = stringToCid(cidString)
83
+ ```
84
+
85
+ ### Encoding and Decoding Nodes
86
+
87
+ You can encode and decode IPLD nodes:
88
+
89
+ ```typescript
90
+ import { encodeNode, decodeNode } from '@autonomys/auto-drive'
91
+
92
+ // Encode a node
93
+ const encodedNode = encodeNode(dag.head)
94
+
95
+ // Decode a node
96
+ const decodedNode = decodeNode(encodedNode)
97
+ ```
98
+
99
+ ### Handling Metadata
100
+
101
+ To add metadata to a node, you can create a metadata node:
102
+
103
+ ```typescript
104
+ import { createMetadataNode } from '@autonomys/auto-drive'
105
+
106
+ const metadata = {
107
+ name: 'My File',
108
+ description: 'This is a sample file',
109
+ // ... other metadata fields
110
+ }
111
+
112
+ const metadataNode = createMetadataNode(metadata)
113
+ ```
114
+
115
+ ### Example: Creating a File DAG and Converting to CID
116
+
117
+ ```typescript
118
+ import { createFileIPLDDag, cidOfNode, cidToString } from '@autonomys/auto-drive'
119
+ import fs from 'fs'
120
+
121
+ const fileBuffer = fs.readFileSync('path/to/your/file.txt')
122
+
123
+ const dag = createFileIPLDDag(fileBuffer, 'file.txt')
124
+
125
+ const cid = cidOfNode(dag.headCID)
126
+ const cidString = cidToString(cid)
127
+
128
+ console.log(`CID of the file DAG: ${cidString}`)
129
+ ```
130
+
131
+ ### Example: Converting Metadata To DAG
132
+
133
+ ```typescript
134
+ import {
135
+ createMetadataIPLDDag,
136
+ cidOfNode,
137
+ cidToString,
138
+ type OffchainMetadata,
139
+ } from '@autonomys/auto-drive'
140
+ import fs from 'fs'
141
+
142
+ const metadata: OffchainMetadata = fs.readFileSync('path/to/your/metadata.json')
143
+
144
+ const dag = createMetadataIPLDDag(metadata)
145
+
146
+ const cid = cidOfNode(dag.headCID)
147
+ const cidString = cidToString(cid)
148
+
149
+ console.log(`CID of the metadata DAG: ${cidString}`)
150
+ ```
151
+
152
+ ## License
153
+
154
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
155
+
156
+ ## Additional Resources
157
+
158
+ - **Autonomys Academy**: Learn more at [Autonomys Academy](https://academy.autonomys.xyz).
159
+ - **Auto-Utils Package**: Utility functions used alongside `auto-drive` can be found in [`@autonomys/auto-utils`](../Auto-Utils/README.md).
160
+
161
+ ## Contact
162
+
163
+ If you have any questions or need support, feel free to reach out:
164
+
165
+ - **GitHub Issues**: [GitHub Issues Page](https://github.com/autonomys/auto-sdk/issues)
166
+
167
+ We appreciate your feedback and contributions!
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@autonomys/auto-drive",
3
3
  "packageManager": "yarn@4.1.1",
4
- "version": "0.6.2",
4
+ "version": "0.6.4",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "repository": {
@@ -39,5 +39,5 @@
39
39
  "protons": "^7.6.0",
40
40
  "protons-runtime": "^5.5.0"
41
41
  },
42
- "gitHead": "c49eb0479d010f277abe0fde54665a7a06dd00b8"
42
+ "gitHead": "cbeaa151644f75d43e7e36b5a817d580bff09ebd"
43
43
  }
@@ -0,0 +1,38 @@
1
+ import { PBNode } from '@ipld/dag-pb'
2
+ import { CID } from 'multiformats/cid'
3
+ import {
4
+ createChunkedFileIpldNode,
5
+ createChunkedMetadataIpldNode,
6
+ createFileChunkIpldNode,
7
+ createFileInlinkIpldNode,
8
+ createMetadataChunkIpldNode,
9
+ createMetadataInlinkIpldNode,
10
+ createSingleFileIpldNode,
11
+ createSingleMetadataIpldNode,
12
+ } from './nodes.js'
13
+
14
+ export interface Builders {
15
+ inlink: (links: CID[], size: number, linkDepth: number, chunkSize: number) => PBNode
16
+ chunk: (data: Buffer) => PBNode
17
+ root: (
18
+ links: CID[],
19
+ size: number,
20
+ linkDepth: number,
21
+ name?: string,
22
+ maxNodeSize?: number,
23
+ ) => PBNode
24
+ single: (data: Buffer, filename?: string) => PBNode
25
+ }
26
+ export const metadataBuilders: Builders = {
27
+ inlink: createMetadataInlinkIpldNode,
28
+ chunk: createMetadataChunkIpldNode,
29
+ root: createChunkedMetadataIpldNode,
30
+ single: createSingleMetadataIpldNode,
31
+ }
32
+
33
+ export const fileBuilders: Builders = {
34
+ inlink: createFileInlinkIpldNode,
35
+ chunk: createFileChunkIpldNode,
36
+ root: createChunkedFileIpldNode,
37
+ single: createSingleFileIpldNode,
38
+ }
@@ -1,14 +1,9 @@
1
1
  import { PBNode } from '@ipld/dag-pb'
2
2
  import { CID } from 'multiformats'
3
3
  import { cidOfNode } from '../cid/index.js'
4
- import {
5
- createChunkedFileIpldNode,
6
- createChunkIpldNode,
7
- createFileInlinkIpldNode,
8
- createFolderInlinkIpldNode,
9
- createFolderIpldNode,
10
- createSingleFileIpldNode,
11
- } from './nodes.js'
4
+ import { OffchainMetadata } from '../metadata/index.js'
5
+ import { Builders, fileBuilders, metadataBuilders } from './builders.js'
6
+ import { createFolderInlinkIpldNode, createFolderIpldNode } from './nodes.js'
12
7
  import { chunkBuffer, encodeNode } from './utils.js'
13
8
 
14
9
  export const DEFAULT_MAX_CHUNK_SIZE = 1024 * 64
@@ -27,8 +22,32 @@ export const createFileIPLDDag = (
27
22
  maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
28
23
  },
29
24
  ): IPLDDag => {
30
- if (file.length <= chunkSize) {
31
- const head = createSingleFileIpldNode(file, filename)
25
+ return createBufferIPLDDag(file, filename, fileBuilders, { chunkSize, maxLinkPerNode })
26
+ }
27
+
28
+ export const createMetadataIPLDDag = (
29
+ metadata: OffchainMetadata,
30
+ limits: { chunkSize: number; maxLinkPerNode: number } = {
31
+ chunkSize: DEFAULT_MAX_CHUNK_SIZE,
32
+ maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
33
+ },
34
+ ): IPLDDag => {
35
+ const buffer = Buffer.from(JSON.stringify(metadata))
36
+ const name = `${metadata.name}.metadata.json`
37
+ return createBufferIPLDDag(buffer, name, metadataBuilders, limits)
38
+ }
39
+
40
+ const createBufferIPLDDag = (
41
+ buffer: Buffer,
42
+ filename: string | undefined,
43
+ builders: Builders,
44
+ { chunkSize, maxLinkPerNode }: { chunkSize: number; maxLinkPerNode: number } = {
45
+ chunkSize: DEFAULT_MAX_CHUNK_SIZE,
46
+ maxLinkPerNode: DEFAULT_MAX_LINK_PER_NODE,
47
+ },
48
+ ): IPLDDag => {
49
+ if (buffer.length <= chunkSize) {
50
+ const head = builders.single(buffer, filename)
32
51
  const headCID = cidOfNode(head)
33
52
  return {
34
53
  headCID,
@@ -36,12 +55,12 @@ export const createFileIPLDDag = (
36
55
  }
37
56
  }
38
57
 
39
- const bufferChunks = chunkBuffer(file, chunkSize)
58
+ const bufferChunks = chunkBuffer(buffer, chunkSize)
40
59
 
41
60
  const nodes = new Map<CID, PBNode>()
42
61
 
43
62
  let CIDs: CID[] = bufferChunks.map((chunk) => {
44
- const node = createChunkIpldNode(chunk)
63
+ const node = builders.chunk(chunk)
45
64
  const cid = cidOfNode(node)
46
65
  nodes.set(cid, node)
47
66
 
@@ -54,7 +73,7 @@ export const createFileIPLDDag = (
54
73
  for (let i = 0; i < CIDs.length; i += maxLinkPerNode) {
55
74
  const chunk = CIDs.slice(i, i + maxLinkPerNode)
56
75
 
57
- const node = createFileInlinkIpldNode(chunk, chunk.length, depth, chunkSize)
76
+ const node = builders.inlink(chunk, chunk.length, depth, chunkSize)
58
77
  const cid = cidOfNode(node)
59
78
  nodes.set(cid, node)
60
79
  newCIDs.push(cid)
@@ -62,7 +81,7 @@ export const createFileIPLDDag = (
62
81
  depth++
63
82
  CIDs = newCIDs
64
83
  }
65
- const head = createChunkedFileIpldNode(CIDs, file.length, depth, filename, chunkSize)
84
+ const head = builders.root(CIDs, buffer.length, depth, filename, chunkSize)
66
85
  const headCID = cidOfNode(head)
67
86
  nodes.set(headCID, head)
68
87
 
package/src/ipld/nodes.ts CHANGED
@@ -4,8 +4,8 @@ import { OffchainMetadata } from '../metadata/index.js'
4
4
  import { encodeIPLDNodeData, MetadataType } from '../metadata/onchain/index.js'
5
5
  import { DEFAULT_MAX_CHUNK_SIZE, ensureNodeMaxSize } from './chunker.js'
6
6
 
7
- /// Creates a chunk ipld node
8
- export const createChunkIpldNode = (data: Buffer): PBNode =>
7
+ /// Creates a file chunk ipld node
8
+ export const createFileChunkIpldNode = (data: Buffer): PBNode =>
9
9
  createNode(
10
10
  encodeIPLDNodeData({
11
11
  type: MetadataType.FileChunk,
@@ -40,7 +40,6 @@ export const createChunkedFileIpldNode = (
40
40
  )
41
41
  // Creates a file ipld node
42
42
  // links: the CIDs of the file's contents
43
- // @todo: add the file's metadata
44
43
  export const createFileInlinkIpldNode = (
45
44
  links: CID[],
46
45
  size: number,
@@ -74,6 +73,72 @@ export const createSingleFileIpldNode = (data: Buffer, name?: string): PBNode =>
74
73
  [],
75
74
  )
76
75
 
76
+ // Creates a file ipld node
77
+ // links: the CIDs of the file's contents
78
+ // @todo: add the file's metadata
79
+ export const createMetadataInlinkIpldNode = (
80
+ links: CID[],
81
+ size: number,
82
+ linkDepth: number,
83
+ maxNodeSize: number = DEFAULT_MAX_CHUNK_SIZE,
84
+ ): PBNode =>
85
+ ensureNodeMaxSize(
86
+ createNode(
87
+ encodeIPLDNodeData({
88
+ type: MetadataType.FileInlink,
89
+ size,
90
+ linkDepth,
91
+ }),
92
+ links.map((cid) => ({ Hash: cid })),
93
+ ),
94
+ maxNodeSize,
95
+ )
96
+
97
+ // Creates a file ipld node
98
+ // links: the CIDs of the file's contents
99
+ // @todo: add the file's metadata
100
+ export const createSingleMetadataIpldNode = (data: Buffer, name?: string): PBNode =>
101
+ createNode(
102
+ encodeIPLDNodeData({
103
+ type: MetadataType.Metadata,
104
+ name,
105
+ size: data.length,
106
+ linkDepth: 0,
107
+ data,
108
+ }),
109
+ [],
110
+ )
111
+
112
+ export const createMetadataChunkIpldNode = (data: Buffer): PBNode =>
113
+ createNode(
114
+ encodeIPLDNodeData({
115
+ type: MetadataType.MetadataChunk,
116
+ size: data.length,
117
+ linkDepth: 0,
118
+ data,
119
+ }),
120
+ )
121
+
122
+ export const createChunkedMetadataIpldNode = (
123
+ links: CID[],
124
+ size: number,
125
+ linkDepth: number,
126
+ name?: string,
127
+ maxNodeSize: number = DEFAULT_MAX_CHUNK_SIZE,
128
+ ): PBNode =>
129
+ ensureNodeMaxSize(
130
+ createNode(
131
+ encodeIPLDNodeData({
132
+ type: MetadataType.Metadata,
133
+ name,
134
+ size,
135
+ linkDepth,
136
+ }),
137
+ links.map((cid) => ({ Hash: cid })),
138
+ ),
139
+ maxNodeSize,
140
+ )
141
+
77
142
  // Creates a folder ipld node
78
143
  // links: the CIDs of the folder's contents
79
144
  // @todo: add the folder's metadata
@@ -16,4 +16,6 @@ enum MetadataType {
16
16
  Folder = 3;
17
17
  FolderInlink = 4;
18
18
  Metadata = 5;
19
+ MetadataInlink = 6;
20
+ MetadataChunk = 7;
19
21
  }
@@ -114,7 +114,9 @@ export enum MetadataType {
114
114
  FileChunk = 'FileChunk',
115
115
  Folder = 'Folder',
116
116
  FolderInlink = 'FolderInlink',
117
- Metadata = 'Metadata'
117
+ Metadata = 'Metadata',
118
+ MetadataInlink = 'MetadataInlink',
119
+ MetadataChunk = 'MetadataChunk'
118
120
  }
119
121
 
120
122
  enum __MetadataTypeValues {
@@ -123,7 +125,9 @@ enum __MetadataTypeValues {
123
125
  FileChunk = 2,
124
126
  Folder = 3,
125
127
  FolderInlink = 4,
126
- Metadata = 5
128
+ Metadata = 5,
129
+ MetadataInlink = 6,
130
+ MetadataChunk = 7
127
131
  }
128
132
 
129
133
  export namespace MetadataType {
@@ -1,7 +1,7 @@
1
1
  import { createNode } from '@ipld/dag-pb'
2
2
  import { cidOfNode } from '../src'
3
- import { createFileIPLDDag, createFolderIPLDDag } from '../src/ipld/chunker'
4
- import { IPLDNodeData, MetadataType } from '../src/metadata'
3
+ import { createFileIPLDDag, createFolderIPLDDag, createMetadataIPLDDag } from '../src/ipld/chunker'
4
+ import { IPLDNodeData, MetadataType, OffchainMetadata } from '../src/metadata'
5
5
 
6
6
  describe('chunker', () => {
7
7
  describe('file creation', () => {
@@ -154,4 +154,39 @@ describe('chunker', () => {
154
154
  expect(inlinkCount).toBe(3)
155
155
  })
156
156
  })
157
+
158
+ describe('metadata creation', () => {
159
+ it('create a metadata dag from a small buffer', () => {
160
+ const metadata: OffchainMetadata = {
161
+ type: 'file',
162
+ dataCid: 'test',
163
+ name: 'test',
164
+ mimeType: 'text/plain',
165
+ totalSize: 1000,
166
+ totalChunks: 10,
167
+ chunks: [],
168
+ }
169
+
170
+ const dag = createMetadataIPLDDag(metadata)
171
+ expect(dag.nodes.size).toBe(1)
172
+ })
173
+
174
+ it('large metadata dag represented into multiple nodes', () => {
175
+ const metadata: OffchainMetadata = {
176
+ type: 'file',
177
+ dataCid: 'test',
178
+ name: 'test',
179
+ mimeType: 'text/plain'.repeat(100),
180
+ totalSize: 1000,
181
+ totalChunks: 10,
182
+ chunks: [],
183
+ }
184
+
185
+ const dag = createMetadataIPLDDag(metadata, {
186
+ chunkSize: 200,
187
+ maxLinkPerNode: 2,
188
+ })
189
+ expect(dag.nodes.size).toBeGreaterThan(1)
190
+ })
191
+ })
157
192
  })
@@ -1,6 +1,10 @@
1
1
  import { createNode } from '@ipld/dag-pb'
2
- import { cidOfNode, createChunkedFileIpldNode, createSingleFileIpldNode } from '../src/index.js'
3
- import { createChunkIpldNode } from '../src/ipld/nodes.js'
2
+ import {
3
+ cidOfNode,
4
+ createChunkedFileIpldNode,
5
+ createFileChunkIpldNode,
6
+ createSingleFileIpldNode,
7
+ } from '../src/index.js'
4
8
  import { IPLDNodeData, MetadataType } from '../src/metadata/onchain/protobuf/OnchainMetadata.js'
5
9
 
6
10
  describe('node creation', () => {
@@ -58,7 +62,7 @@ describe('node creation', () => {
58
62
 
59
63
  it('file chunk node | correctly params setup', () => {
60
64
  const buffer = Buffer.from('hello world')
61
- const node = createChunkIpldNode(buffer)
65
+ const node = createFileChunkIpldNode(buffer)
62
66
 
63
67
  const decoded = IPLDNodeData.decode(node.Data ?? new Uint8Array())
64
68
  expect(decoded.type).toBe(MetadataType.FileChunk)