@autonomys/auto-drive 1.0.6 → 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.
Files changed (3) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +190 -25
  3. package/package.json +3 -3
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,8 +1,14 @@
1
- # auto-drive
1
+ # Autonomys Auto Drive SDK
2
2
 
3
- ## auto-drive API Tools
3
+ ![Autonomys Banner](https://github.com/autonomys/auto-sdk/blob/main/.github/images/autonomys-banner.webp)
4
4
 
5
- The `auto-drive` package provides a set of tools to interact with the auto-drive API. Below are some key functionalities:
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 `auto-drive` package provides a set of tools to interact with the Autonomys Auto-Drive API. Below are some key functionalities:
6
12
 
7
13
  ### Installation
8
14
 
@@ -14,21 +20,21 @@ yarn add @autonomys/auto-drive
14
20
 
15
21
  ### How to use it?
16
22
 
17
- ### Example Usage
23
+ ### How to upload a file from filepath? (Not available in browser)
18
24
 
19
- Here is an example of how to use the `uploadFile` method to upload a file with optional encryption and compression:
25
+ Here is an example of how to use the `uploadFileFromFilepath` method to upload a file with optional encryption and compression:
20
26
 
21
27
  ```typescript
22
- import { uploadFile } from '@autonomys/auto-drive'
28
+ import { uploadFileFromFilepath } from '@autonomys/auto-drive'
23
29
 
24
- const api = new AutoDriveApi() // Initialize your API instance
30
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
25
31
  const filePath = 'path/to/your/file.txt' // Specify the path to your file
26
32
  const options = {
27
33
  password: 'your-encryption-password', // Optional: specify a password for encryption
28
34
  compression: true,
29
35
  }
30
36
 
31
- uploadFile(api, filePath, options)
37
+ const uploadObservable = uploadFile(api, filePath, options)
32
38
  .then(() => {
33
39
  console.log('File uploaded successfully!')
34
40
  })
@@ -37,38 +43,197 @@ uploadFile(api, filePath, options)
37
43
  })
38
44
  ```
39
45
 
40
- ### Example Usage of Download
41
-
42
- Here is an example of how to use the `downloadFile` method to download a file from the server:
46
+ ### How to upload [File](https://developer.mozilla.org/en-US/docs/Web/API/File) interface
43
47
 
44
48
  ```typescript
45
- import { getRoots } from '@autonomys/auto-drive'
49
+ import { uploadFileFromFilepath } from '@autonomys/auto-drive'
46
50
 
47
- const api = new AutoDriveApi() // Initialize your API instance
51
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
52
+ const filePath = 'path/to/your/file.txt' // Specify the path to your file
53
+ const options = {
54
+ password: 'your-encryption-password', // Optional: specify a password for encryption
55
+ compression: true,
56
+ }
48
57
 
49
- getRoots(api)
50
- .then((roots) => {
51
- console.log('Root directories:', roots)
58
+ const uploadObservable = uploadFile(api, filePath, options)
59
+ .then(() => {
60
+ console.log('File uploaded successfully!')
52
61
  })
53
62
  .catch((error) => {
54
- console.error('Error retrieving root directories:', error)
63
+ console.error('Error uploading file:', error)
55
64
  })
56
65
  ```
57
66
 
58
- ### Example Usage of getRoots
67
+ ### How to upload a file from a custom interface?
59
68
 
60
- Here is an example of how to use the `getRoots` method to retrieve the root directories:
69
+ Some times you might have a custom interface that doesn't fit either File or filepath. For those cases exists the interface GenericFile:
61
70
 
62
71
  ```typescript
63
- import { getRoots } from '@autonomys/auto-drive'
72
+ export interface GenericFile {
73
+ read(): AsyncIterable<Buffer> // A buffer generator function that will output the bytes of the file
74
+ name: string
75
+ mimeType?: string
76
+ size: number
77
+ path: string // Could be ignored in file upload
78
+ }
79
+ ```
64
80
 
65
- const api = new AutoDriveApi() // Initialize your API instance
81
+ For more info about asynn generator visit [this website](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator).
66
82
 
67
- getRoots(api)
68
- .then((roots) => {
69
- console.log('Root directories:', roots)
83
+ You could upload any file that could be represented in that way. For example, uploading a file as a `Buffer`
84
+
85
+ ```typescript
86
+ import { uploadFile } from '@autonomys/auto-drive'
87
+
88
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
89
+ const buffer = Buffer.from(...);
90
+ const genericFile = {
91
+ read: async function *() {
92
+ yield buffer
93
+ },
94
+ name: "autonomys-whitepaper.pdf",
95
+ mimeType: "application/pdf",
96
+ size: 1234556,
97
+ path: "autonomys-whitepaper.pdf"
98
+ }
99
+
100
+ const options = {
101
+ password: 'your-encryption-password', // Optional: specify a password for encryption
102
+ compression: true,
103
+ }
104
+
105
+ const uploadObservable = uploadFile(api, genericFile, options)
106
+ .then(() => {
107
+ console.log('File uploaded successfully!')
70
108
  })
71
109
  .catch((error) => {
72
- console.error('Error retrieving root directories:', error)
110
+ console.error('Error uploading file:', error)
73
111
  })
74
112
  ```
113
+
114
+ ### How to upload a folder from folder? (Not available in browser)
115
+
116
+ ```ts
117
+ import { uploadFolderFromFolderPath } from '@autonomys/auto-drive'
118
+
119
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
120
+ const folderPath = 'path/to/your/folder' // Specify the path to your folder
121
+
122
+ const options = {
123
+ uploadChunkSize: 1024 * 1024, // Optional: specify the chunk size for uploads
124
+ password: 'your-encryption-password', // Optional: If folder is encrypted
125
+ }
126
+
127
+ const uploadObservable = uploadFolderFromFolderPath(api, folderPath, options)
128
+ ```
129
+
130
+ **Note: If a folder is tried to be encrypted a zip file would be generated and that file would be encrypted and uploaded.**
131
+
132
+ ### Handle observables
133
+
134
+ Since uploads may take some time, specially in big-sized files. Uploads do implement `rxjs` observables so you could have feedback about the process or even show your users the progress of the upload.
135
+
136
+ For that reason when file upload functions return `PromisedObservable<UploadFileStatus>`:
137
+
138
+ ```typescript
139
+ export type UploadFileStatus = {
140
+ type: 'file'
141
+ progress: number
142
+ cid?: CID
143
+ }
144
+ ```
145
+
146
+ Being the cid only returned (and thus optional) when the upload is completed.
147
+
148
+ Similarly, for folder uploads the functions return `PromisedObservable<UploadFolderStatus>`
149
+
150
+ ```ts
151
+ export type UploadFolderStatus = {
152
+ type: 'folder'
153
+ progress: number
154
+ cid?: CID
155
+ }
156
+ ```
157
+
158
+ **e.g Show upload progress in React**
159
+
160
+ ```typescript
161
+ const [progress, setProgress] = useState<number>(0)
162
+
163
+ useEffect(async () => {
164
+ const finalStatus = await uploadFileFromInput(api, genericFile, options).forEach((status) => {
165
+ setProgress(status.progress)
166
+ })
167
+ })
168
+ ```
169
+
170
+ **e.g Ignore observables**
171
+
172
+ Other users may want to not use the progress observability. For having a promise instead the field `promise` is a Promise that resolves into `UploadFileStatus` and `UploadFolderStatus` for files and folders respectively.
173
+
174
+ e.g
175
+
176
+ ```ts
177
+ const status = await uploadFileFromInput(api, genericFile, options).promise
178
+ const cid = status.cid
179
+ ```
180
+
181
+ ### Example Usage of Download
182
+
183
+ Here is an example of how to use the `downloadFile` method to download a file from the server:
184
+
185
+ ```typescript
186
+ import { downloadObject } from '@autonomys/auto-drive'
187
+
188
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
189
+
190
+ try {
191
+ const cid = ".."
192
+ const stream = await downloadObject(api, { cid })
193
+ let file = Buffer.alloc(0);
194
+ for await (const chunk of stream) {
195
+ file = Buffer.concat([file, chunk])
196
+ }
197
+ console.log('File downloaded successfully:', stream)
198
+ } catch (error) {
199
+ console.error('Error downloading file:', error)
200
+ }
201
+
202
+ ```
203
+
204
+ ### Example Usage of getRoots
205
+
206
+ Here is an example of how to use the `getRoots` method to retrieve the root directories:
207
+
208
+ ```typescript
209
+ import { createAutoDriveApi, downloadObject } from '@autonomys/auto-drive'
210
+ import fs from 'fs'
211
+
212
+ const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
213
+
214
+ try {
215
+ const stream = fs.createWriteStream('/path/to/file')
216
+ const asyncBuffer = await downloadObject(api, { cid })
217
+ for await (const buffer of asyncBuffer) {
218
+ stream.write(buffer)
219
+ }
220
+ } catch (error) {
221
+ console.error('Error downloading file:', error)
222
+ }
223
+ ```
224
+
225
+ ## License
226
+
227
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
228
+
229
+ ## Additional Resources
230
+
231
+ - **Autonomys Academy**: Learn more at [Autonomys Academy](https://academy.autonomys.xyz).
232
+
233
+ ## Contact
234
+
235
+ If you have any questions or need support, feel free to reach out:
236
+
237
+ - **GitHub Issues**: [GitHub Issues Page](https://github.com/autonomys/auto-sdk/issues)
238
+
239
+ We appreciate your feedback and contributions!
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "packageManager": "yarn@4.2.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
- "version": "1.0.6",
6
+ "version": "1.0.8",
7
7
  "author": {
8
8
  "name": "Autonomys",
9
9
  "url": "https://autonomys.net"
@@ -19,10 +19,10 @@
19
19
  "typescript": "^5.6.3"
20
20
  },
21
21
  "dependencies": {
22
- "@autonomys/auto-dag-data": "^1.0.6",
22
+ "@autonomys/auto-dag-data": "^1.0.8",
23
23
  "jszip": "^3.10.1",
24
24
  "mime-types": "^2.1.35",
25
25
  "zod": "^3.23.8"
26
26
  },
27
- "gitHead": "2ee93dd0867c16e0a42f2f47b2fa66bc41ba29cf"
27
+ "gitHead": "88b1b90467db7dd1e301387451ed727189f9808c"
28
28
  }