@autonomys/auto-drive 1.1.4 → 1.2.0

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
@@ -25,43 +25,40 @@ yarn add @autonomys/auto-drive
25
25
  Here is an example of how to use the `uploadFileFromFilepath` method to upload a file with optional encryption and compression:
26
26
 
27
27
  ```typescript
28
- import { uploadFileFromFilepath } from '@autonomys/auto-drive'
28
+ import { uploadFileFromFilepath,createAutoDriveApi } from '@autonomys/auto-drive'
29
29
 
30
30
  const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
31
31
  const filePath = 'path/to/your/file.txt' // Specify the path to your file
32
32
  const options = {
33
33
  password: 'your-encryption-password', // Optional: specify a password for encryption
34
34
  compression: true,
35
+ // an optional callback useful for large file uploads
36
+ onProgress?: (progress: number) => {
37
+ console.log(`The upload is completed is ${progress}% completed`)
38
+ }
35
39
  }
36
40
 
37
- const uploadObservable = uploadFileFromFilepath(api, filePath, options)
38
- .then(() => {
39
- console.log('File uploaded successfully!')
40
- })
41
- .catch((error) => {
42
- console.error('Error uploading file:', error)
43
- })
41
+ const cid = await uploadFileFromFilepath(api, filePath, options)
42
+
43
+ console.log(`The file is uploaded and its cid is ${cid}`)
44
44
  ```
45
45
 
46
46
  ### How to upload [File](https://developer.mozilla.org/en-US/docs/Web/API/File) interface
47
47
 
48
48
  ```typescript
49
- import { uploadFileFromFilepath } from '@autonomys/auto-drive'
49
+ import { uploadFileFromInput, createAutoDriveApi } from '@autonomys/auto-drive'
50
50
 
51
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
52
+
53
+ // e.g Get File from object from HTML event
54
+ const file: File = e.target.value // Substitute with your file
53
55
  const options = {
54
56
  password: 'your-encryption-password', // Optional: specify a password for encryption
55
57
  compression: true,
56
58
  }
59
+ const cid = await uploadFileFromInput(api, file, options)
57
60
 
58
- const uploadObservable = uploadFile(api, filePath, options)
59
- .then(() => {
60
- console.log('File uploaded successfully!')
61
- })
62
- .catch((error) => {
63
- console.error('Error uploading file:', error)
64
- })
61
+ console.log(`The file is uploaded and its cid is ${cid}`)
65
62
  ```
66
63
 
67
64
  ### How to upload a file from a custom interface?
@@ -83,7 +80,7 @@ For more info about asynn generator visit [this website](https://developer.mozil
83
80
  You could upload any file that could be represented in that way. For example, uploading a file as a `Buffer`
84
81
 
85
82
  ```typescript
86
- import { uploadFile } from '@autonomys/auto-drive'
83
+ import { createAutoDriveApi, uploadFile } from '@autonomys/auto-drive'
87
84
 
88
85
  const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
89
86
  const buffer = Buffer.from(...);
@@ -100,21 +97,21 @@ const genericFile = {
100
97
  const options = {
101
98
  password: 'your-encryption-password', // Optional: specify a password for encryption
102
99
  compression: true,
100
+ // an optional callback useful for large file uploads
101
+ onProgress?: (progress: number) => {
102
+ console.log(`The upload is completed is ${progress}% completed`)
103
+ }
103
104
  }
104
105
 
105
- const uploadObservable = uploadFile(api, genericFile, options)
106
- .then(() => {
107
- console.log('File uploaded successfully!')
108
- })
109
- .catch((error) => {
110
- console.error('Error uploading file:', error)
111
- })
106
+ const cid = uploadFile(api, genericFile, options)
107
+
108
+ console.log(`The file is uploaded and its cid is ${cid}`)
112
109
  ```
113
110
 
114
111
  ### How to upload a folder from folder? (Not available in browser)
115
112
 
116
113
  ```ts
117
- import { uploadFolderFromFolderPath } from '@autonomys/auto-drive'
114
+ import { createAutoDriveApi, uploadFolderFromFolderPath } from '@autonomys/auto-drive'
118
115
 
119
116
  const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
120
117
  const folderPath = 'path/to/your/folder' // Specify the path to your folder
@@ -122,74 +119,31 @@ const folderPath = 'path/to/your/folder' // Specify the path to your folder
122
119
  const options = {
123
120
  uploadChunkSize: 1024 * 1024, // Optional: specify the chunk size for uploads
124
121
  password: 'your-encryption-password', // Optional: If folder is encrypted
122
+ // an optional callback useful for large file uploads
123
+ onProgress: (progress: number) => {
124
+ console.log(`The upload is completed is ${progress}% completed`)
125
+ },
125
126
  }
126
127
 
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)
128
+ const folderCID = await uploadFolderFromFolderPath(api, folderPath, options)
162
129
 
163
- useEffect(async () => {
164
- const finalStatus = await uploadFileFromInput(api, genericFile, options).forEach((status) => {
165
- setProgress(status.progress)
166
- })
167
- })
130
+ console.log(`The folder is uploaded and its cid is ${folderCID}`)
168
131
  ```
169
132
 
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
- ```
133
+ **Note: If a folder is tried to be encrypted a zip file would be generated and that file would be encrypted and uploaded.**
180
134
 
181
135
  ### Example Usage of Download
182
136
 
183
137
  Here is an example of how to use the `downloadFile` method to download a file from the server:
184
138
 
185
139
  ```typescript
186
- import { downloadObject } from '@autonomys/auto-drive'
140
+ import { createAutoDriveApi, downloadFile } from '@autonomys/auto-drive'
187
141
 
188
142
  const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
189
143
 
190
144
  try {
191
145
  const cid = '..'
192
- const stream = await downloadObject(api, { cid })
146
+ const stream = await downloadFile(api, cid)
193
147
  let file = Buffer.alloc(0)
194
148
  for await (const chunk of stream) {
195
149
  file = Buffer.concat([file, chunk])
@@ -205,16 +159,20 @@ try {
205
159
  Here is an example of how to use the `getRoots` method to retrieve the root directories:
206
160
 
207
161
  ```typescript
208
- import { createAutoDriveApi, downloadObject } from '@autonomys/auto-drive'
209
- import fs from 'fs'
162
+ import { createAutoDriveApi, apiCalls, Scope } from '@autonomys/auto-drive'
210
163
 
211
164
  const api = createAutoDriveApi({ apiKey: 'your-api-key' }) // Initialize your API instance with API key
212
165
 
213
166
  try {
214
- const stream = fs.createWriteStream('/path/to/file')
215
- const asyncBuffer = await downloadObject(api, { cid })
216
- for await (const buffer of asyncBuffer) {
217
- stream.write(buffer)
167
+ const myFiles = await apiCalls.getRoots(api, {
168
+ scope: Scope.User,
169
+ limit: 100,
170
+ offset: 0,
171
+ })
172
+
173
+ console.log(`Retrieved ${myFiles.rows.length} files of ${myFiles.totalCount} total`)
174
+ for (const file of myFiles.rows) {
175
+ console.log(`${file.name} - ${file.headCid}: ${file.size}`)
218
176
  }
219
177
  } catch (error) {
220
178
  console.error('Error downloading file:', error)
@@ -1,10 +1,9 @@
1
- import { PromisedObservable } from '../utils/observable';
2
1
  import { AutoDriveApi } from './connection';
3
2
  import { GenericFile, GenericFileWithinFolder } from './models/file';
4
- import { UploadChunksStatus, UploadFileStatus, UploadFolderStatus } from './models/uploads';
5
3
  export type UploadFileOptions = {
6
4
  password?: string;
7
5
  compression?: boolean;
6
+ onProgress?: (progress: number) => void;
8
7
  };
9
8
  /**
10
9
  * Uploads a file to the server with optional encryption and compression.
@@ -23,7 +22,7 @@ export type UploadFileOptions = {
23
22
  * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
24
23
  * @throws {Error} - Throws an error if the upload fails at any stage.
25
24
  */
26
- export declare const uploadFileFromInput: (api: AutoDriveApi, file: File, options?: UploadFileOptions, uploadChunkSize?: number) => PromisedObservable<UploadFileStatus>;
25
+ export declare const uploadFileFromInput: (api: AutoDriveApi, file: File, options?: UploadFileOptions, uploadChunkSize?: number) => Promise<string>;
27
26
  /**
28
27
  * Uploads a file to the server with optional encryption and compression.
29
28
  *
@@ -41,7 +40,7 @@ export declare const uploadFileFromInput: (api: AutoDriveApi, file: File, option
41
40
  * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
42
41
  * @throws {Error} - Throws an error if the upload fails at any stage.
43
42
  */
44
- export declare const uploadFile: (api: AutoDriveApi, file: GenericFile, options?: UploadFileOptions, uploadChunkSize?: number) => PromisedObservable<UploadFileStatus>;
43
+ export declare const uploadFile: (api: AutoDriveApi, file: GenericFile, options?: UploadFileOptions, uploadChunkSize?: number) => Promise<string>;
45
44
  /**
46
45
  * Uploads an entire folder to the server.
47
46
  *
@@ -59,10 +58,11 @@ export declare const uploadFile: (api: AutoDriveApi, file: GenericFile, options?
59
58
  * @returns {PromisedObservable<UploadFileStatus | UploadFolderStatus>} - An observable that emits the upload status.
60
59
  * @throws {Error} - Throws an error if the upload fails at any stage.
61
60
  */
62
- export declare const uploadFolderFromInput: (api: AutoDriveApi, fileList: FileList | File[], { uploadChunkSize, password }?: {
61
+ export declare const uploadFolderFromInput: (api: AutoDriveApi, fileList: FileList | File[], { uploadChunkSize, password, onProgress, }?: {
63
62
  uploadChunkSize?: number;
64
63
  password?: string;
65
- }) => Promise<PromisedObservable<UploadFileStatus | UploadFolderStatus>>;
64
+ onProgress?: (progress: number) => void;
65
+ }) => Promise<string>;
66
66
  /**
67
67
  * Uploads a file within an existing folder upload session.
68
68
  *
@@ -72,7 +72,7 @@ export declare const uploadFolderFromInput: (api: AutoDriveApi, fileList: FileLi
72
72
  *
73
73
  * @returns {Promise<void>} A promise that resolves when the file upload is complete.
74
74
  */
75
- export declare const uploadFileWithinFolderUpload: (api: AutoDriveApi, uploadId: string, file: GenericFileWithinFolder, uploadChunkSize?: number) => PromisedObservable<UploadChunksStatus>;
75
+ export declare const uploadFileWithinFolderUpload: (api: AutoDriveApi, uploadId: string, file: GenericFileWithinFolder, uploadChunkSize?: number, options?: Pick<UploadFileOptions, "onProgress">) => Promise<string>;
76
76
  /**
77
77
  * Downloads a file from the AutoDrive service.
78
78
  *
@@ -1 +1 @@
1
- {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/api/wrappers.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAExD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAEpE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAE3F,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAuBD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB,QACzB,YAAY,QACX,IAAI,YACD,iBAAiB,oBACR,MAAM,KACvB,kBAAkB,CAAC,gBAAgB,CAgDrC,CAAA;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,UAAU,QAChB,YAAY,QACX,WAAW,YACR,iBAAiB,oBACR,MAAM,KACvB,kBAAkB,CAAC,gBAAgB,CAiDrC,CAAA;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,qBAAqB,QAC3B,YAAY,YACP,QAAQ,GAAG,IAAI,EAAE,kCACI;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,KAC7E,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,CA8DnE,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,QAClC,YAAY,YACP,MAAM,QACV,uBAAuB,oBACX,MAAM,KACvB,kBAAkB,CAAC,kBAAkB,CAkBvC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,QAClB,YAAY,OACZ,MAAM,aACA,MAAM,KAChB,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAwB/B,CAAA"}
1
+ {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/api/wrappers.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAGpE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CACxC,CAAA;AA6BD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB,QACzB,YAAY,QACX,IAAI,YACD,iBAAiB,oBACR,MAAM,KACvB,OAAO,CAAC,MAAM,CAgDhB,CAAA;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,UAAU,QAChB,YAAY,QACX,WAAW,YACR,iBAAiB,oBACR,MAAM,KACvB,OAAO,CAAC,MAAM,CA+ChB,CAAA;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,qBAAqB,QAC3B,YAAY,YACP,QAAQ,GAAG,IAAI,EAAE,+CAKxB;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CAAE,KAC1F,OAAO,CAAC,MAAM,CA2DhB,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,QAClC,YAAY,YACP,MAAM,QACV,uBAAuB,oBACX,MAAM,YACf,IAAI,CAAC,iBAAiB,EAAE,YAAY,CAAC,KAC7C,OAAO,CAAC,MAAM,CAchB,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,QAClB,YAAY,OACZ,MAAM,aACA,MAAM,KAChB,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAwB/B,CAAA"}
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -56,34 +23,38 @@ exports.downloadFile = exports.uploadFileWithinFolderUpload = exports.uploadFold
56
23
  const mime_types_1 = __importDefault(require("mime-types"));
57
24
  const async_1 = require("../utils/async");
58
25
  const misc_1 = require("../utils/misc");
59
- const observable_1 = require("../utils/observable");
60
26
  const index_1 = require("./calls/index");
61
27
  const folderTree_1 = require("./models/folderTree");
62
28
  const UPLOAD_FILE_CHUNK_SIZE = 1024 * 1024;
63
- const uploadFileChunks = (api, fileUploadId, asyncIterable, uploadChunkSize = UPLOAD_FILE_CHUNK_SIZE) => {
64
- return new observable_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
29
+ const uploadFileChunks = (api, fileUploadId, asyncIterable, uploadChunkSize = UPLOAD_FILE_CHUNK_SIZE, onProgress) => {
30
+ return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
65
31
  var _a, e_1, _b, _c;
66
- let index = 0;
67
- let uploadBytes = 0;
68
32
  try {
69
- for (var _d = true, _e = __asyncValues((0, async_1.asyncByChunk)(asyncIterable, uploadChunkSize)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
70
- _c = _f.value;
71
- _d = false;
72
- const chunk = _c;
73
- yield index_1.apiCalls.uploadFileChunk(api, { uploadId: fileUploadId, chunk, index });
74
- uploadBytes += chunk.length;
75
- subscriber.next({ uploadBytes });
76
- index++;
77
- }
78
- }
79
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
80
- finally {
33
+ let index = 0;
34
+ let uploadBytes = 0;
81
35
  try {
82
- if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
36
+ for (var _d = true, _e = __asyncValues((0, async_1.asyncByChunk)(asyncIterable, uploadChunkSize)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
37
+ _c = _f.value;
38
+ _d = false;
39
+ const chunk = _c;
40
+ yield index_1.apiCalls.uploadFileChunk(api, { uploadId: fileUploadId, chunk, index });
41
+ uploadBytes += chunk.length;
42
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(uploadBytes);
43
+ index++;
44
+ }
83
45
  }
84
- finally { if (e_1) throw e_1.error; }
46
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
47
+ finally {
48
+ try {
49
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
50
+ }
51
+ finally { if (e_1) throw e_1.error; }
52
+ }
53
+ resolve();
54
+ }
55
+ catch (e) {
56
+ reject(e);
85
57
  }
86
- subscriber.complete();
87
58
  }));
88
59
  };
89
60
  /**
@@ -105,8 +76,8 @@ const uploadFileChunks = (api, fileUploadId, asyncIterable, uploadChunkSize = UP
105
76
  */
106
77
  const uploadFileFromInput = (api, file, options = {}, uploadChunkSize) => {
107
78
  const { password = undefined, compression = true } = options;
108
- return new observable_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
109
- const { stringToCid, compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } = yield Promise.resolve().then(() => __importStar(require('@autonomys/auto-dag-data')));
79
+ return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
80
+ const { compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } = yield import('@autonomys/auto-dag-data');
110
81
  let asyncIterable = (0, async_1.fileToIterable)(file);
111
82
  if (compression) {
112
83
  asyncIterable = compressFile(asyncIterable, {
@@ -137,10 +108,12 @@ const uploadFileFromInput = (api, file, options = {}, uploadChunkSize) => {
137
108
  filename: file.name,
138
109
  uploadOptions,
139
110
  });
140
- yield uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize).forEach((e) => subscriber.next({ type: 'file', progress: (0, misc_1.progressToPercentage)(e.uploadBytes, file.size) }));
111
+ yield uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize, (bytes) => {
112
+ var _a;
113
+ (_a = options.onProgress) === null || _a === void 0 ? void 0 : _a.call(options, (0, misc_1.progressToPercentage)(bytes, file.size));
114
+ });
141
115
  const result = yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
142
- subscriber.next({ type: 'file', progress: 100, cid: result.cid });
143
- subscriber.complete();
116
+ resolve(result.cid);
144
117
  }));
145
118
  };
146
119
  exports.uploadFileFromInput = uploadFileFromInput;
@@ -161,46 +134,46 @@ exports.uploadFileFromInput = uploadFileFromInput;
161
134
  * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
162
135
  * @throws {Error} - Throws an error if the upload fails at any stage.
163
136
  */
164
- const uploadFile = (api, file, options = {}, uploadChunkSize) => {
137
+ const uploadFile = (api_1, file_1, ...args_1) => __awaiter(void 0, [api_1, file_1, ...args_1], void 0, function* (api, file, options = {}, uploadChunkSize) {
165
138
  const { password = undefined, compression = true } = options;
166
- return new observable_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
167
- const { stringToCid, compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } = yield Promise.resolve().then(() => __importStar(require('@autonomys/auto-dag-data')));
168
- let asyncIterable = file.read();
169
- if (compression) {
170
- asyncIterable = compressFile(asyncIterable, {
139
+ const { compressFile, CompressionAlgorithm, encryptFile, EncryptionAlgorithm } = yield import('@autonomys/auto-dag-data');
140
+ let asyncIterable = file.read();
141
+ if (compression) {
142
+ asyncIterable = compressFile(asyncIterable, {
143
+ level: 9,
144
+ algorithm: CompressionAlgorithm.ZLIB,
145
+ });
146
+ }
147
+ if (password) {
148
+ asyncIterable = encryptFile(asyncIterable, password, {
149
+ algorithm: EncryptionAlgorithm.AES_256_GCM,
150
+ });
151
+ }
152
+ const uploadOptions = {
153
+ compression: compression
154
+ ? {
171
155
  level: 9,
172
156
  algorithm: CompressionAlgorithm.ZLIB,
173
- });
174
- }
175
- if (password) {
176
- asyncIterable = encryptFile(asyncIterable, password, {
157
+ }
158
+ : undefined,
159
+ encryption: password
160
+ ? {
177
161
  algorithm: EncryptionAlgorithm.AES_256_GCM,
178
- });
179
- }
180
- const uploadOptions = {
181
- compression: compression
182
- ? {
183
- level: 9,
184
- algorithm: CompressionAlgorithm.ZLIB,
185
- }
186
- : undefined,
187
- encryption: password
188
- ? {
189
- algorithm: EncryptionAlgorithm.AES_256_GCM,
190
- }
191
- : undefined,
192
- };
193
- const fileUpload = yield index_1.apiCalls.createFileUpload(api, {
194
- mimeType: mime_types_1.default.lookup(file.name) || undefined,
195
- filename: file.name,
196
- uploadOptions,
197
- });
198
- yield uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize).forEach((e) => subscriber.next({ type: 'file', progress: (0, misc_1.progressToPercentage)(e.uploadBytes, file.size) }));
199
- const result = yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
200
- subscriber.next({ type: 'file', progress: 100, cid: result.cid });
201
- subscriber.complete();
202
- }));
203
- };
162
+ }
163
+ : undefined,
164
+ };
165
+ const fileUpload = yield index_1.apiCalls.createFileUpload(api, {
166
+ mimeType: mime_types_1.default.lookup(file.name) || undefined,
167
+ filename: file.name,
168
+ uploadOptions,
169
+ });
170
+ yield uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize, (bytes) => {
171
+ var _a;
172
+ (_a = options.onProgress) === null || _a === void 0 ? void 0 : _a.call(options, (0, misc_1.progressToPercentage)(bytes, file.size));
173
+ });
174
+ const result = yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
175
+ return result.cid;
176
+ });
204
177
  exports.uploadFile = uploadFile;
205
178
  /**
206
179
  * Uploads an entire folder to the server.
@@ -219,7 +192,7 @@ exports.uploadFile = uploadFile;
219
192
  * @returns {PromisedObservable<UploadFileStatus | UploadFolderStatus>} - An observable that emits the upload status.
220
193
  * @throws {Error} - Throws an error if the upload fails at any stage.
221
194
  */
222
- const uploadFolderFromInput = (api_1, fileList_1, ...args_1) => __awaiter(void 0, [api_1, fileList_1, ...args_1], void 0, function* (api, fileList, { uploadChunkSize, password } = {}) {
195
+ const uploadFolderFromInput = (api_1, fileList_1, ...args_1) => __awaiter(void 0, [api_1, fileList_1, ...args_1], void 0, function* (api, fileList, { uploadChunkSize, password, onProgress, } = {}) {
223
196
  const files = fileList instanceof FileList ? Array.from(fileList) : fileList;
224
197
  const fileTree = (0, folderTree_1.constructFromInput)(files);
225
198
  // If password is provided, we zip the files and upload the zip file
@@ -235,34 +208,31 @@ const uploadFolderFromInput = (api_1, fileList_1, ...args_1) => __awaiter(void 0
235
208
  }, {
236
209
  password,
237
210
  compression: true,
211
+ onProgress,
238
212
  });
239
213
  }
240
- return new observable_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
241
- // Otherwise, we upload the files as a folder w/o compression or encryption
242
- const folderUpload = yield index_1.apiCalls.createFolderUpload(api, {
243
- fileTree,
214
+ // Otherwise, we upload the files as a folder w/o compression or encryption
215
+ const folderUpload = yield index_1.apiCalls.createFolderUpload(api, {
216
+ fileTree,
217
+ });
218
+ let currentBytesUploaded = 0;
219
+ const totalSize = files.reduce((acc, file) => acc + file.size, 0);
220
+ for (const file of files) {
221
+ yield (0, exports.uploadFileWithinFolderUpload)(api, folderUpload.id, {
222
+ read: () => (0, async_1.fileToIterable)(file),
223
+ name: file.name,
224
+ mimeType: mime_types_1.default.lookup(file.name) || undefined,
225
+ size: file.size,
226
+ path: file.webkitRelativePath,
227
+ }, uploadChunkSize, {
228
+ onProgress: (progress) => {
229
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress((0, misc_1.progressToPercentage)(currentBytesUploaded + progress, totalSize));
230
+ },
244
231
  });
245
- let currentBytesUploaded = 0;
246
- const totalSize = files.reduce((acc, file) => acc + file.size, 0);
247
- for (const file of files) {
248
- yield (0, exports.uploadFileWithinFolderUpload)(api, folderUpload.id, {
249
- read: () => (0, async_1.fileToIterable)(file),
250
- name: file.name,
251
- mimeType: mime_types_1.default.lookup(file.name) || undefined,
252
- size: file.size,
253
- path: file.webkitRelativePath,
254
- }, uploadChunkSize).forEach((e) => {
255
- subscriber.next({
256
- type: 'folder',
257
- progress: (0, misc_1.progressToPercentage)(currentBytesUploaded + e.uploadBytes, totalSize),
258
- });
259
- });
260
- currentBytesUploaded += file.size;
261
- }
262
- const result = yield index_1.apiCalls.completeUpload(api, { uploadId: folderUpload.id });
263
- subscriber.next({ type: 'folder', progress: 100, cid: result.cid });
264
- subscriber.complete();
265
- }));
232
+ currentBytesUploaded += file.size;
233
+ }
234
+ const result = yield index_1.apiCalls.completeUpload(api, { uploadId: folderUpload.id });
235
+ return result.cid;
266
236
  });
267
237
  exports.uploadFolderFromInput = uploadFolderFromInput;
268
238
  /**
@@ -274,20 +244,18 @@ exports.uploadFolderFromInput = uploadFolderFromInput;
274
244
  *
275
245
  * @returns {Promise<void>} A promise that resolves when the file upload is complete.
276
246
  */
277
- const uploadFileWithinFolderUpload = (api, uploadId, file, uploadChunkSize) => {
278
- return new observable_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
279
- const fileUpload = yield index_1.apiCalls.createFileUploadWithinFolderUpload(api, {
280
- uploadId,
281
- name: file.name,
282
- mimeType: file.mimeType,
283
- relativeId: file.path,
284
- uploadOptions: {},
285
- });
286
- yield uploadFileChunks(api, fileUpload.id, file.read(), uploadChunkSize).forEach((e) => subscriber.next({ uploadBytes: e.uploadBytes }));
287
- yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
288
- subscriber.complete();
289
- }));
290
- };
247
+ const uploadFileWithinFolderUpload = (api_1, uploadId_1, file_1, uploadChunkSize_1, ...args_1) => __awaiter(void 0, [api_1, uploadId_1, file_1, uploadChunkSize_1, ...args_1], void 0, function* (api, uploadId, file, uploadChunkSize, options = {}) {
248
+ const fileUpload = yield index_1.apiCalls.createFileUploadWithinFolderUpload(api, {
249
+ uploadId,
250
+ name: file.name,
251
+ mimeType: file.mimeType,
252
+ relativeId: file.path,
253
+ uploadOptions: {},
254
+ });
255
+ yield uploadFileChunks(api, fileUpload.id, file.read(), uploadChunkSize, options.onProgress);
256
+ const result = yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
257
+ return result.cid;
258
+ });
291
259
  exports.uploadFileWithinFolderUpload = uploadFileWithinFolderUpload;
292
260
  /**
293
261
  * Downloads a file from the AutoDrive service.
@@ -298,7 +266,7 @@ exports.uploadFileWithinFolderUpload = uploadFileWithinFolderUpload;
298
266
  */
299
267
  const downloadFile = (api, cid, password) => __awaiter(void 0, void 0, void 0, function* () {
300
268
  var _a, _b;
301
- const { decompressFile, CompressionAlgorithm, EncryptionAlgorithm, decryptFile } = yield Promise.resolve().then(() => __importStar(require('@autonomys/auto-dag-data')));
269
+ const { decompressFile, CompressionAlgorithm, EncryptionAlgorithm, decryptFile } = yield import('@autonomys/auto-dag-data');
302
270
  const metadata = yield index_1.apiCalls.getObjectMetadata(api, { cid });
303
271
  let iterable = (0, async_1.asyncFromStream)(yield index_1.apiCalls.downloadObject(api, { cid }));
304
272
  if ((_a = metadata.uploadOptions) === null || _a === void 0 ? void 0 : _a.encryption) {
@@ -1,7 +1,5 @@
1
1
  import { AutoDriveApi } from '../api/connection.js';
2
- import { UploadFileStatus, UploadFolderStatus } from '../api/models/uploads.js';
3
2
  import { UploadFileOptions } from '../api/wrappers.js';
4
- import { PromisedObservable } from '../utils/observable.js';
5
3
  /**
6
4
  * Uploads a file to the server with optional encryption and compression.
7
5
  *
@@ -19,7 +17,7 @@ import { PromisedObservable } from '../utils/observable.js';
19
17
  * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
20
18
  * @throws {Error} - Throws an error if the upload fails at any stage.
21
19
  */
22
- export declare const uploadFileFromFilepath: (api: AutoDriveApi, filePath: string, options?: UploadFileOptions, uploadChunkSize?: number) => PromisedObservable<UploadFileStatus>;
20
+ export declare const uploadFileFromFilepath: (api: AutoDriveApi, filePath: string, options?: UploadFileOptions, uploadChunkSize?: number) => Promise<string>;
23
21
  /**
24
22
  * Uploads an entire folder to the server.
25
23
  *
@@ -38,8 +36,9 @@ export declare const uploadFileFromFilepath: (api: AutoDriveApi, filePath: strin
38
36
  * @returns {Promise<PromisedObservable<UploadFileStatus | UploadFolderStatus>>} - A promise that resolves to an observable that tracks the upload progress.
39
37
  * @throws {Error} - Throws an error if the upload fails at any stage.
40
38
  */
41
- export declare const uploadFolderFromFolderPath: (api: AutoDriveApi, folderPath: string, { uploadChunkSize, password }?: {
39
+ export declare const uploadFolderFromFolderPath: (api: AutoDriveApi, folderPath: string, { uploadChunkSize, password, onProgress, }?: {
42
40
  uploadChunkSize?: number;
43
41
  password?: string;
44
- }) => Promise<PromisedObservable<UploadFileStatus | UploadFolderStatus>>;
42
+ onProgress?: (progressInPercentage: number) => void;
43
+ }) => Promise<Promise<string>>;
45
44
  //# sourceMappingURL=wrappers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/fs/wrappers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAInD,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC/E,OAAO,EAAc,iBAAiB,EAAgC,MAAM,oBAAoB,CAAA;AAGhG,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAG3D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,sBAAsB,QAC5B,YAAY,YACP,MAAM,YACP,iBAAiB,oBACR,MAAM,KACvB,kBAAkB,CAAC,gBAAgB,CAkBrC,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,0BAA0B,QAChC,YAAY,cACL,MAAM,kCACa;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,KAC7E,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,CA6DnE,CAAA"}
1
+ {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/fs/wrappers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAKnD,OAAO,EAAc,iBAAiB,EAAgC,MAAM,oBAAoB,CAAA;AAKhG;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,sBAAsB,QAC5B,YAAY,YACP,MAAM,YACP,iBAAiB,oBACR,MAAM,KACvB,OAAO,CAAC,MAAM,CAmBhB,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,0BAA0B,QAChC,YAAY,cACL,MAAM,+CAKf;IACD,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,CAAC,oBAAoB,EAAE,MAAM,KAAK,IAAI,CAAA;CACpD,KACA,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CA0DzB,CAAA"}