@autonomys/auto-drive 1.1.4 → 1.2.1

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.
@@ -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) {
@@ -52,38 +19,42 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
52
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
53
20
  };
54
21
  Object.defineProperty(exports, "__esModule", { value: true });
55
- exports.downloadFile = exports.uploadFileWithinFolderUpload = exports.uploadFolderFromInput = exports.uploadFile = exports.uploadFileFromInput = void 0;
22
+ exports.getSubscriptionInfo = exports.getPendingCredits = exports.downloadFile = exports.uploadFileWithinFolderUpload = exports.uploadFolderFromInput = exports.uploadObjectAsJSON = exports.uploadFile = exports.uploadFileFromInput = void 0;
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;
@@ -158,50 +131,80 @@ exports.uploadFileFromInput = uploadFileFromInput;
158
131
  * @param {string} [options.password] - The password for encryption (optional).
159
132
  * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
160
133
  * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
161
- * @returns {PromisedObservable<UploadFileStatus>} - An observable that emits the upload status.
134
+ * @returns {Promise<string>} - The CID of the uploaded file.
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;
178
+ /**
179
+ * Uploads an object as a JSON file to the server.
180
+ *
181
+ * This function serializes the provided object to a JSON string,
182
+ * and then uploads the JSON string as a file to the server.
183
+ *
184
+ * @param {AutoDriveApi} api - The API instance used to send requests.
185
+ * @param {File | GenericFile} file - The file to be uploaded, which can be a File or a GenericFile.
186
+ * @param {UploadFileOptions} options - Options for the upload process.
187
+ * @param {string} [options.password] - The password for encryption (optional).
188
+ * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
189
+ * @param {number} [uploadChunkSize] - The size of each chunk to upload (optional).
190
+ * @returns {Promise<string>} - The CID of the uploaded file.
191
+ * @throws {Error} - Throws an error if the upload fails at any stage.
192
+ */
193
+ const uploadObjectAsJSON = (api_1, object_1, name_1, ...args_1) => __awaiter(void 0, [api_1, object_1, name_1, ...args_1], void 0, function* (api, object, name, options = {}, uploadChunkSize) {
194
+ try {
195
+ const json = Buffer.from(JSON.stringify(object));
196
+ return (0, exports.uploadFile)(api, {
197
+ read: () => (0, async_1.bufferToIterable)(json),
198
+ name: name || 'object.json',
199
+ mimeType: 'application/json',
200
+ size: json.length,
201
+ }, options, uploadChunkSize);
202
+ }
203
+ catch (e) {
204
+ throw new Error('Failed to serialize object to JSON');
205
+ }
206
+ });
207
+ exports.uploadObjectAsJSON = uploadObjectAsJSON;
205
208
  /**
206
209
  * Uploads an entire folder to the server.
207
210
  *
@@ -219,7 +222,7 @@ exports.uploadFile = uploadFile;
219
222
  * @returns {PromisedObservable<UploadFileStatus | UploadFolderStatus>} - An observable that emits the upload status.
220
223
  * @throws {Error} - Throws an error if the upload fails at any stage.
221
224
  */
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 } = {}) {
225
+ 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
226
  const files = fileList instanceof FileList ? Array.from(fileList) : fileList;
224
227
  const fileTree = (0, folderTree_1.constructFromInput)(files);
225
228
  // If password is provided, we zip the files and upload the zip file
@@ -235,34 +238,31 @@ const uploadFolderFromInput = (api_1, fileList_1, ...args_1) => __awaiter(void 0
235
238
  }, {
236
239
  password,
237
240
  compression: true,
241
+ onProgress,
238
242
  });
239
243
  }
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,
244
+ // Otherwise, we upload the files as a folder w/o compression or encryption
245
+ const folderUpload = yield index_1.apiCalls.createFolderUpload(api, {
246
+ fileTree,
247
+ });
248
+ let currentBytesUploaded = 0;
249
+ const totalSize = files.reduce((acc, file) => acc + file.size, 0);
250
+ for (const file of files) {
251
+ yield (0, exports.uploadFileWithinFolderUpload)(api, folderUpload.id, {
252
+ read: () => (0, async_1.fileToIterable)(file),
253
+ name: file.name,
254
+ mimeType: mime_types_1.default.lookup(file.name) || undefined,
255
+ size: file.size,
256
+ path: file.webkitRelativePath,
257
+ }, uploadChunkSize, {
258
+ onProgress: (progress) => {
259
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress((0, misc_1.progressToPercentage)(currentBytesUploaded + progress, totalSize));
260
+ },
244
261
  });
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
- }));
262
+ currentBytesUploaded += file.size;
263
+ }
264
+ const result = yield index_1.apiCalls.completeUpload(api, { uploadId: folderUpload.id });
265
+ return result.cid;
266
266
  });
267
267
  exports.uploadFolderFromInput = uploadFolderFromInput;
268
268
  /**
@@ -274,20 +274,18 @@ exports.uploadFolderFromInput = uploadFolderFromInput;
274
274
  *
275
275
  * @returns {Promise<void>} A promise that resolves when the file upload is complete.
276
276
  */
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
- };
277
+ 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 = {}) {
278
+ const fileUpload = yield index_1.apiCalls.createFileUploadWithinFolderUpload(api, {
279
+ uploadId,
280
+ name: file.name,
281
+ mimeType: file.mimeType,
282
+ relativeId: file.path,
283
+ uploadOptions: {},
284
+ });
285
+ yield uploadFileChunks(api, fileUpload.id, file.read(), uploadChunkSize, options.onProgress);
286
+ const result = yield index_1.apiCalls.completeUpload(api, { uploadId: fileUpload.id });
287
+ return result.cid;
288
+ });
291
289
  exports.uploadFileWithinFolderUpload = uploadFileWithinFolderUpload;
292
290
  /**
293
291
  * Downloads a file from the AutoDrive service.
@@ -298,7 +296,7 @@ exports.uploadFileWithinFolderUpload = uploadFileWithinFolderUpload;
298
296
  */
299
297
  const downloadFile = (api, cid, password) => __awaiter(void 0, void 0, void 0, function* () {
300
298
  var _a, _b;
301
- const { decompressFile, CompressionAlgorithm, EncryptionAlgorithm, decryptFile } = yield Promise.resolve().then(() => __importStar(require('@autonomys/auto-dag-data')));
299
+ const { decompressFile, CompressionAlgorithm, EncryptionAlgorithm, decryptFile } = yield import('@autonomys/auto-dag-data');
302
300
  const metadata = yield index_1.apiCalls.getObjectMetadata(api, { cid });
303
301
  let iterable = (0, async_1.asyncFromStream)(yield index_1.apiCalls.downloadObject(api, { cid }));
304
302
  if ((_a = metadata.uploadOptions) === null || _a === void 0 ? void 0 : _a.encryption) {
@@ -317,3 +315,16 @@ const downloadFile = (api, cid, password) => __awaiter(void 0, void 0, void 0, f
317
315
  return iterable;
318
316
  });
319
317
  exports.downloadFile = downloadFile;
318
+ const getPendingCredits = (api) => __awaiter(void 0, void 0, void 0, function* () {
319
+ const me = yield index_1.apiCalls.getMe(api);
320
+ return {
321
+ upload: me.subscription.pendingUploadCredits,
322
+ download: me.subscription.pendingDownloadCredits,
323
+ };
324
+ });
325
+ exports.getPendingCredits = getPendingCredits;
326
+ const getSubscriptionInfo = (api) => __awaiter(void 0, void 0, void 0, function* () {
327
+ const me = yield index_1.apiCalls.getMe(api);
328
+ return me.subscription;
329
+ });
330
+ exports.getSubscriptionInfo = getSubscriptionInfo;
@@ -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"}
@@ -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) {
@@ -50,10 +17,10 @@ const fs_1 = __importDefault(require("fs"));
50
17
  const mime_types_1 = __importDefault(require("mime-types"));
51
18
  const index_js_1 = require("../api/index.js");
52
19
  const folderTree_js_1 = require("../api/models/folderTree.js");
20
+ const uploads_js_1 = require("../api/models/uploads.js");
53
21
  const wrappers_js_1 = require("../api/wrappers.js");
54
22
  const index_js_2 = require("../utils/index.js");
55
23
  const misc_js_1 = require("../utils/misc.js");
56
- const observable_js_1 = require("../utils/observable.js");
57
24
  const utils_js_1 = require("./utils.js");
58
25
  /**
59
26
  * Uploads a file to the server with optional encryption and compression.
@@ -73,7 +40,7 @@ const utils_js_1 = require("./utils.js");
73
40
  * @throws {Error} - Throws an error if the upload fails at any stage.
74
41
  */
75
42
  const uploadFileFromFilepath = (api, filePath, options = {}, uploadChunkSize) => {
76
- const { password = undefined, compression = true } = options;
43
+ const { password = undefined, compression = true, onProgress } = options;
77
44
  const name = filePath.split('/').pop();
78
45
  return (0, wrappers_js_1.uploadFile)(api, {
79
46
  read: () => fs_1.default.createReadStream(filePath),
@@ -83,6 +50,7 @@ const uploadFileFromFilepath = (api, filePath, options = {}, uploadChunkSize) =>
83
50
  }, {
84
51
  password,
85
52
  compression,
53
+ onProgress,
86
54
  }, uploadChunkSize);
87
55
  };
88
56
  exports.uploadFileFromFilepath = uploadFileFromFilepath;
@@ -104,7 +72,7 @@ exports.uploadFileFromFilepath = uploadFileFromFilepath;
104
72
  * @returns {Promise<PromisedObservable<UploadFileStatus | UploadFolderStatus>>} - A promise that resolves to an observable that tracks the upload progress.
105
73
  * @throws {Error} - Throws an error if the upload fails at any stage.
106
74
  */
107
- const uploadFolderFromFolderPath = (api_1, folderPath_1, ...args_1) => __awaiter(void 0, [api_1, folderPath_1, ...args_1], void 0, function* (api, folderPath, { uploadChunkSize, password } = {}) {
75
+ const uploadFolderFromFolderPath = (api_1, folderPath_1, ...args_1) => __awaiter(void 0, [api_1, folderPath_1, ...args_1], void 0, function* (api, folderPath, { uploadChunkSize, password, onProgress, } = {}) {
108
76
  const files = yield (0, utils_js_1.getFiles)(folderPath);
109
77
  const fileTree = (0, folderTree_js_1.constructFromFileSystemEntries)(files);
110
78
  if (password) {
@@ -119,38 +87,38 @@ const uploadFolderFromFolderPath = (api_1, folderPath_1, ...args_1) => __awaiter
119
87
  }, {
120
88
  password,
121
89
  compression: true,
90
+ onProgress: (progressInPercentage) => {
91
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress((0, misc_js_1.progressToPercentage)(progressInPercentage, zipBlob.size));
92
+ },
122
93
  });
123
94
  }
124
- return new observable_js_1.PromisedObservable((subscriber) => __awaiter(void 0, void 0, void 0, function* () {
125
- const { CompressionAlgorithm } = yield Promise.resolve().then(() => __importStar(require('@autonomys/auto-dag-data')));
126
- const folderUpload = yield index_js_1.apiCalls.createFolderUpload(api, {
127
- fileTree,
128
- uploadOptions: {
129
- compression: {
130
- algorithm: CompressionAlgorithm.ZLIB,
131
- level: 9,
132
- },
95
+ const folderUpload = yield index_js_1.apiCalls.createFolderUpload(api, {
96
+ fileTree,
97
+ uploadOptions: {
98
+ compression: {
99
+ algorithm: uploads_js_1.CompressionAlgorithm.ZLIB,
100
+ level: 9,
133
101
  },
134
- });
135
- const genericFiles = files.map((file) => ({
136
- read: () => fs_1.default.createReadStream(file),
137
- name: file.split('/').pop(),
138
- mimeType: mime_types_1.default.lookup(file.split('/').pop()) || undefined,
139
- size: fs_1.default.statSync(file).size,
140
- path: file,
141
- }));
142
- const totalSize = genericFiles.reduce((acc, file) => acc + file.size, 0);
143
- let progress = 0;
144
- for (const file of genericFiles) {
145
- yield (0, wrappers_js_1.uploadFileWithinFolderUpload)(api, folderUpload.id, file, uploadChunkSize).forEach((e) => subscriber.next({
146
- type: 'folder',
147
- progress: (0, misc_js_1.progressToPercentage)(progress + e.uploadBytes, totalSize),
148
- }));
149
- progress += file.size;
150
- }
151
- const result = yield index_js_1.apiCalls.completeUpload(api, { uploadId: folderUpload.id });
152
- subscriber.next({ type: 'folder', progress: 100, cid: result.cid });
153
- subscriber.complete();
102
+ },
103
+ });
104
+ const genericFiles = files.map((file) => ({
105
+ read: () => fs_1.default.createReadStream(file),
106
+ name: file.split('/').pop(),
107
+ mimeType: mime_types_1.default.lookup(file.split('/').pop()) || undefined,
108
+ size: fs_1.default.statSync(file).size,
109
+ path: file,
154
110
  }));
111
+ const totalSize = genericFiles.reduce((acc, file) => acc + file.size, 0);
112
+ let progress = 0;
113
+ for (const file of genericFiles) {
114
+ yield (0, wrappers_js_1.uploadFileWithinFolderUpload)(api, folderUpload.id, file, uploadChunkSize, {
115
+ onProgress: (uploadedBytes) => {
116
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress((0, misc_js_1.progressToPercentage)(progress + uploadedBytes, totalSize));
117
+ },
118
+ });
119
+ progress += file.size;
120
+ }
121
+ const result = yield index_js_1.apiCalls.completeUpload(api, { uploadId: folderUpload.id });
122
+ return result.cid;
155
123
  });
156
124
  exports.uploadFolderFromFolderPath = uploadFolderFromFolderPath;
@@ -2,4 +2,5 @@ import { Buffer } from 'buffer';
2
2
  export declare const asyncByChunk: (asyncIterable: AsyncIterable<Buffer>, chunkSize: number) => AsyncGenerator<Buffer<ArrayBuffer>, void, unknown>;
3
3
  export declare const asyncFromStream: (stream: ReadableStream<Uint8Array>) => AsyncIterable<Buffer>;
4
4
  export declare const fileToIterable: (file: File | Blob) => AsyncIterable<Buffer>;
5
+ export declare const bufferToIterable: (buffer: Buffer) => AsyncIterable<Buffer>;
5
6
  //# sourceMappingURL=async.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/utils/async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,eAAO,MAAM,YAAY,kBACR,aAAa,CAAC,MAAM,CAAC,aACzB,MAAM,uDAelB,CAAA;AAED,eAAO,MAAM,eAAe,WAClB,cAAc,CAAC,UAAU,CAAC,KACjC,aAAa,CAAC,MAAM,CAOtB,CAAA;AAED,eAAO,MAAM,cAAc,SAA0B,IAAI,GAAG,IAAI,KAAG,aAAa,CAAC,MAAM,CAKtF,CAAA"}
1
+ {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/utils/async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,eAAO,MAAM,YAAY,kBACR,aAAa,CAAC,MAAM,CAAC,aACzB,MAAM,uDAelB,CAAA;AAED,eAAO,MAAM,eAAe,WAClB,cAAc,CAAC,UAAU,CAAC,KACjC,aAAa,CAAC,MAAM,CAOtB,CAAA;AAED,eAAO,MAAM,cAAc,SAA0B,IAAI,GAAG,IAAI,KAAG,aAAa,CAAC,MAAM,CAKtF,CAAA;AAED,eAAO,MAAM,gBAAgB,WAA4B,MAAM,KAAG,aAAa,CAAC,MAAM,CAErF,CAAA"}
@@ -20,7 +20,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
20
20
  function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.fileToIterable = exports.asyncFromStream = exports.asyncByChunk = void 0;
23
+ exports.bufferToIterable = exports.fileToIterable = exports.asyncFromStream = exports.asyncByChunk = void 0;
24
24
  const buffer_1 = require("buffer");
25
25
  const asyncByChunk = function (asyncIterable, chunkSize) {
26
26
  return __asyncGenerator(this, arguments, function* () {
@@ -71,3 +71,9 @@ const fileToIterable = function (file) {
71
71
  });
72
72
  };
73
73
  exports.fileToIterable = fileToIterable;
74
+ const bufferToIterable = function (buffer) {
75
+ return __asyncGenerator(this, arguments, function* () {
76
+ yield yield __await(buffer);
77
+ });
78
+ };
79
+ exports.bufferToIterable = bufferToIterable;
@@ -0,0 +1,17 @@
1
+ import type { CID } from 'multiformats/cid';
2
+ interface AutoCIDTransformers {
3
+ stringToCid: (cid: string) => CID;
4
+ blake3HashFromCid: (cid: CID) => Uint8Array;
5
+ }
6
+ export declare class AutoCID {
7
+ private readonly cid;
8
+ private readonly tools;
9
+ constructor(cid: string, tools: AutoCIDTransformers);
10
+ static create(cid: string): Promise<AutoCID>;
11
+ get asString(): string;
12
+ get asCID(): CID;
13
+ get asBlake3Hash(): Buffer;
14
+ }
15
+ export declare const getToolsFromAutoDagData: Promise<AutoCIDTransformers>;
16
+ export {};
17
+ //# sourceMappingURL=autohash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"autohash.d.ts","sourceRoot":"","sources":["../../src/utils/autohash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,UAAU,mBAAmB;IAC3B,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,GAAG,CAAA;IACjC,iBAAiB,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,UAAU,CAAA;CAC5C;AAED,qBAAa,OAAO;IAEhB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,KAAK;gBADL,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,mBAAmB;WAGhC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKlD,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,IAAI,KAAK,IAAI,GAAG,CAEf;IAED,IAAI,YAAY,IAAI,MAAM,CAEzB;CACF;AAED,eAAO,MAAM,uBAAuB,EAAE,OAAO,CAAC,mBAAmB,CAO/D,CAAA"}