@autonomys/auto-drive 0.6.3 → 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/README.md CHANGED
@@ -128,6 +128,27 @@ const cidString = cidToString(cid)
128
128
  console.log(`CID of the file DAG: ${cidString}`)
129
129
  ```
130
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
+
131
152
  ## License
132
153
 
133
154
  This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/metadata/offchain/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAEhE,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,SAAS,EAAE,CAAA;CACpB,CAAA;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,eAAO,MAAM,YAAY,QAClB,OAAO,aACD,MAAM,SACV,MAAM,aACF,MAAM,KAChB,oBAaF,CAAA"}
1
+ {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/metadata/offchain/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,OAAO,EAA8B,MAAM,gBAAgB,CAAA;AAE5F,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,SAAS,EAAE,CAAA;CACpB,CAAA;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,eAAO,MAAM,YAAY,QAClB,OAAO,aACD,MAAM,SACV,MAAM,aACF,MAAM,KAChB,oBAiBF,CAAA"}
@@ -1,13 +1,14 @@
1
- import { cidOfNode, cidToString } from '../../index.js';
1
+ import { cidOfNode, cidToString, IPLDNodeData } from '../../index.js';
2
2
  export const fileMetadata = (dag, totalSize, name, mimeType) => {
3
+ const chunks = Array.from(dag.nodes.values()).filter((n) => n.Data && IPLDNodeData.decode(n.Data).data);
3
4
  return {
4
5
  type: 'file',
5
6
  dataCid: cidToString(dag.headCID),
6
7
  name,
7
8
  mimeType,
8
9
  totalSize,
9
- totalChunks: dag.nodes.size,
10
- chunks: Array.from(dag.nodes.values()).map((chunk) => {
10
+ totalChunks: chunks.length,
11
+ chunks: chunks.map((chunk) => {
11
12
  var _a, _b;
12
13
  return ({
13
14
  cid: cidToString(cidOfNode(chunk)),
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.3",
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": "68a54c6f18e51cb1f32a5997c04230a6107ab631"
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)