@autonomys/auto-drive 1.0.3 → 1.0.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.
Files changed (72) hide show
  1. package/dist/{cjs/api → api}/calls/download.d.ts +1 -0
  2. package/dist/api/calls/download.d.ts.map +1 -0
  3. package/dist/api/calls/download.js +21 -0
  4. package/dist/{esm/api → api}/calls/index.d.ts +1 -0
  5. package/dist/api/calls/index.d.ts.map +1 -0
  6. package/dist/{esm/api → api}/calls/read.d.ts +1 -0
  7. package/dist/api/calls/read.d.ts.map +1 -0
  8. package/dist/{cjs/api/calls/read.d.ts → api/calls/read.js} +72 -20
  9. package/dist/{esm/api → api}/calls/upload.d.ts +3 -2
  10. package/dist/api/calls/upload.d.ts.map +1 -0
  11. package/dist/{cjs/api/calls/upload.d.ts → api/calls/upload.js} +65 -29
  12. package/dist/{cjs/api → api}/calls/write.d.ts +1 -0
  13. package/dist/api/calls/write.d.ts.map +1 -0
  14. package/dist/{esm/api/calls/write.d.ts → api/calls/write.js} +38 -12
  15. package/dist/api/connection.d.ts +15 -0
  16. package/dist/api/connection.d.ts.map +1 -0
  17. package/dist/api/connection.js +24 -0
  18. package/dist/{esm/api → api}/index.d.ts +1 -0
  19. package/dist/api/index.d.ts.map +1 -0
  20. package/dist/api/models/folderTree.d.ts +45 -0
  21. package/dist/api/models/folderTree.d.ts.map +1 -0
  22. package/dist/api/models/folderTree.js +56 -0
  23. package/dist/{esm/api → api}/models/index.d.ts +1 -0
  24. package/dist/api/models/index.d.ts.map +1 -0
  25. package/dist/{cjs/api → api}/models/objects.d.ts +1 -0
  26. package/dist/api/models/objects.d.ts.map +1 -0
  27. package/dist/api/models/objects.js +10 -0
  28. package/dist/api/models/uploads.d.ts +117 -0
  29. package/dist/api/models/uploads.d.ts.map +1 -0
  30. package/dist/api/models/uploads.js +40 -0
  31. package/dist/{cjs/api → api}/wrappers.d.ts +5 -3
  32. package/dist/api/wrappers.d.ts.map +1 -0
  33. package/dist/api/wrappers.js +192 -0
  34. package/dist/{esm/index.d.ts → index.d.ts} +1 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/{cjs/utils → utils}/async.d.ts +1 -0
  37. package/dist/utils/async.d.ts.map +1 -0
  38. package/dist/utils/async.js +58 -0
  39. package/dist/{cjs/utils → utils}/folder.d.ts +1 -0
  40. package/dist/utils/folder.d.ts.map +1 -0
  41. package/dist/utils/folder.js +23 -0
  42. package/dist/{esm/utils → utils}/index.d.ts +1 -0
  43. package/dist/utils/index.d.ts.map +1 -0
  44. package/dist/{cjs/utils → utils}/stream.d.ts +1 -0
  45. package/dist/utils/stream.d.ts.map +1 -0
  46. package/dist/utils/stream.js +22 -0
  47. package/dist/{cjs/utils → utils}/types.d.ts +1 -0
  48. package/dist/utils/types.d.ts.map +1 -0
  49. package/dist/utils/types.js +1 -0
  50. package/package.json +3 -3
  51. package/dist/cjs/api/connection.d.ts +0 -7
  52. package/dist/cjs/api/models/folderTree.d.ts +0 -29
  53. package/dist/cjs/api/models/uploads.d.ts +0 -113
  54. package/dist/cjs/index.js +0 -28810
  55. package/dist/cjs/index.js.map +0 -1
  56. package/dist/esm/api/calls/download.d.ts +0 -5
  57. package/dist/esm/api/connection.d.ts +0 -7
  58. package/dist/esm/api/models/folderTree.d.ts +0 -29
  59. package/dist/esm/api/models/objects.d.ts +0 -40
  60. package/dist/esm/api/models/uploads.d.ts +0 -113
  61. package/dist/esm/api/wrappers.d.ts +0 -66
  62. package/dist/esm/index.js +0 -28762
  63. package/dist/esm/index.js.map +0 -1
  64. package/dist/esm/utils/async.d.ts +0 -2
  65. package/dist/esm/utils/folder.d.ts +0 -1
  66. package/dist/esm/utils/stream.d.ts +0 -2
  67. package/dist/esm/utils/types.d.ts +0 -5
  68. /package/dist/{cjs/api/calls/index.d.ts → api/calls/index.js} +0 -0
  69. /package/dist/{cjs/api/index.d.ts → api/index.js} +0 -0
  70. /package/dist/{cjs/api/models/index.d.ts → api/models/index.js} +0 -0
  71. /package/dist/{cjs/index.d.ts → index.js} +0 -0
  72. /package/dist/{cjs/utils/index.d.ts → utils/index.js} +0 -0
@@ -0,0 +1,117 @@
1
+ import { FileUploadOptions } from '@autonomys/auto-dag-data';
2
+ import { z } from 'zod';
3
+ export declare enum UploadType {
4
+ FILE = "file",
5
+ FOLDER = "folder"
6
+ }
7
+ export declare enum UploadStatus {
8
+ PENDING = "pending",
9
+ COMPLETED = "completed",
10
+ MIGRATING = "migrating",
11
+ CANCELLED = "cancelled",
12
+ FAILED = "failed"
13
+ }
14
+ export declare const fileUploadSchema: z.ZodObject<{
15
+ id: z.ZodString;
16
+ rootId: z.ZodString;
17
+ relativeId: z.ZodNullable<z.ZodString>;
18
+ type: z.ZodNativeEnum<typeof UploadType>;
19
+ status: z.ZodNativeEnum<typeof UploadStatus>;
20
+ fileTree: z.ZodNull;
21
+ name: z.ZodString;
22
+ mimeType: z.ZodNullable<z.ZodString>;
23
+ oauthProvider: z.ZodString;
24
+ oauthUserId: z.ZodString;
25
+ }, "strip", z.ZodTypeAny, {
26
+ name: string;
27
+ type: UploadType;
28
+ status: UploadStatus;
29
+ id: string;
30
+ rootId: string;
31
+ relativeId: string | null;
32
+ fileTree: null;
33
+ mimeType: string | null;
34
+ oauthProvider: string;
35
+ oauthUserId: string;
36
+ }, {
37
+ name: string;
38
+ type: UploadType;
39
+ status: UploadStatus;
40
+ id: string;
41
+ rootId: string;
42
+ relativeId: string | null;
43
+ fileTree: null;
44
+ mimeType: string | null;
45
+ oauthProvider: string;
46
+ oauthUserId: string;
47
+ }>;
48
+ export type FileUpload = z.infer<typeof fileUploadSchema> & {
49
+ uploadOptions: FileUploadOptions | null;
50
+ };
51
+ export declare const folderUploadSchema: z.ZodObject<{
52
+ id: z.ZodString;
53
+ rootId: z.ZodString;
54
+ relativeId: z.ZodNullable<z.ZodString>;
55
+ type: z.ZodNativeEnum<typeof UploadType>;
56
+ status: z.ZodNativeEnum<typeof UploadStatus>;
57
+ fileTree: z.ZodObject<{
58
+ name: z.ZodString;
59
+ type: z.ZodLiteral<"folder">;
60
+ children: z.ZodArray<z.ZodLazy<z.ZodType<import("./folderTree.js").FolderTree, z.ZodTypeDef, import("./folderTree.js").FolderTree>>, "many">;
61
+ id: z.ZodString;
62
+ }, "strip", z.ZodTypeAny, {
63
+ children: import("./folderTree.js").FolderTree[];
64
+ name: string;
65
+ type: "folder";
66
+ id: string;
67
+ }, {
68
+ children: import("./folderTree.js").FolderTree[];
69
+ name: string;
70
+ type: "folder";
71
+ id: string;
72
+ }>;
73
+ name: z.ZodString;
74
+ mimeType: z.ZodNull;
75
+ oauthProvider: z.ZodString;
76
+ oauthUserId: z.ZodString;
77
+ uploadOptions: z.ZodNull;
78
+ }, "strip", z.ZodTypeAny, {
79
+ name: string;
80
+ type: UploadType;
81
+ status: UploadStatus;
82
+ id: string;
83
+ rootId: string;
84
+ relativeId: string | null;
85
+ fileTree: {
86
+ children: import("./folderTree.js").FolderTree[];
87
+ name: string;
88
+ type: "folder";
89
+ id: string;
90
+ };
91
+ mimeType: null;
92
+ oauthProvider: string;
93
+ oauthUserId: string;
94
+ uploadOptions: null;
95
+ }, {
96
+ name: string;
97
+ type: UploadType;
98
+ status: UploadStatus;
99
+ id: string;
100
+ rootId: string;
101
+ relativeId: string | null;
102
+ fileTree: {
103
+ children: import("./folderTree.js").FolderTree[];
104
+ name: string;
105
+ type: "folder";
106
+ id: string;
107
+ };
108
+ mimeType: null;
109
+ oauthProvider: string;
110
+ oauthUserId: string;
111
+ uploadOptions: null;
112
+ }>;
113
+ export type FolderUpload = z.infer<typeof folderUploadSchema>;
114
+ export type CompleteUploadResponse = {
115
+ cid: string;
116
+ };
117
+ //# sourceMappingURL=uploads.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["../../../src/api/models/uploads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,oBAAY,UAAU;IACpB,IAAI,SAAS;IACb,MAAM,WAAW;CAClB;AAED,oBAAY,YAAY;IACtB,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,SAAS,cAAc;IACvB,SAAS,cAAc;IACvB,MAAM,WAAW;CAClB;AAED,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAW3B,CAAA;AAEF,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,GAAG;IAC1D,aAAa,EAAE,iBAAiB,GAAG,IAAI,CAAA;CACxC,CAAA;AAED,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAY7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAE7D,MAAM,MAAM,sBAAsB,GAAG;IACnC,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA"}
@@ -0,0 +1,40 @@
1
+ import { z } from 'zod';
2
+ import { FolderTreeFolderSchema } from './folderTree.js';
3
+ export var UploadType;
4
+ (function (UploadType) {
5
+ UploadType["FILE"] = "file";
6
+ UploadType["FOLDER"] = "folder";
7
+ })(UploadType || (UploadType = {}));
8
+ export var UploadStatus;
9
+ (function (UploadStatus) {
10
+ UploadStatus["PENDING"] = "pending";
11
+ UploadStatus["COMPLETED"] = "completed";
12
+ UploadStatus["MIGRATING"] = "migrating";
13
+ UploadStatus["CANCELLED"] = "cancelled";
14
+ UploadStatus["FAILED"] = "failed";
15
+ })(UploadStatus || (UploadStatus = {}));
16
+ export const fileUploadSchema = z.object({
17
+ id: z.string(),
18
+ rootId: z.string(),
19
+ relativeId: z.string().nullable(),
20
+ type: z.nativeEnum(UploadType),
21
+ status: z.nativeEnum(UploadStatus),
22
+ fileTree: z.null(),
23
+ name: z.string(),
24
+ mimeType: z.string().nullable(),
25
+ oauthProvider: z.string(),
26
+ oauthUserId: z.string(),
27
+ });
28
+ export const folderUploadSchema = z.object({
29
+ id: z.string(),
30
+ rootId: z.string(),
31
+ relativeId: z.string().nullable(),
32
+ type: z.nativeEnum(UploadType),
33
+ status: z.nativeEnum(UploadStatus),
34
+ fileTree: FolderTreeFolderSchema,
35
+ name: z.string(),
36
+ mimeType: z.null(),
37
+ oauthProvider: z.string(),
38
+ oauthUserId: z.string(),
39
+ uploadOptions: z.null(),
40
+ });
@@ -1,3 +1,4 @@
1
+ import { CID } from '@autonomys/auto-dag-data';
1
2
  import { AutoDriveApi } from './connection.js';
2
3
  type UploadFileOptions = {
3
4
  password?: string;
@@ -19,7 +20,7 @@ type UploadFileOptions = {
19
20
  * @returns {Promise<void>} - A promise that resolves when the upload is complete.
20
21
  * @throws {Error} - Throws an error if the upload fails at any stage.
21
22
  */
22
- export declare const uploadFile: (api: AutoDriveApi, filePath: string, { password, compression }: UploadFileOptions, uploadChunkSize?: number) => Promise<void>;
23
+ export declare const uploadFile: (api: AutoDriveApi, filePath: string, { password, compression }: UploadFileOptions, uploadChunkSize?: number) => Promise<CID>;
23
24
  /**
24
25
  * Uploads an entire folder to the server.
25
26
  *
@@ -33,7 +34,7 @@ export declare const uploadFile: (api: AutoDriveApi, filePath: string, { passwor
33
34
  * @returns {Promise<void>} - A promise that resolves when the folder upload is complete.
34
35
  * @throws {Error} - Throws an error if the upload fails at any stage.
35
36
  */
36
- export declare const uploadFolder: (api: AutoDriveApi, folderPath: string, uploadChunkSize?: number) => Promise<void>;
37
+ export declare const uploadFolder: (api: AutoDriveApi, folderPath: string, uploadChunkSize?: number) => Promise<CID>;
37
38
  /**
38
39
  * Uploads a file within an existing folder upload session.
39
40
  *
@@ -43,7 +44,7 @@ export declare const uploadFolder: (api: AutoDriveApi, folderPath: string, uploa
43
44
  *
44
45
  * @returns {Promise<void>} A promise that resolves when the file upload is complete.
45
46
  */
46
- export declare const uploadFileWithinFolderUpload: (api: AutoDriveApi, uploadId: string, filepath: string, uploadChunkSize?: number) => Promise<void>;
47
+ export declare const uploadFileWithinFolderUpload: (api: AutoDriveApi, uploadId: string, filepath: string, uploadChunkSize?: number) => Promise<CID>;
47
48
  /**
48
49
  * Downloads a file from the AutoDrive service.
49
50
  *
@@ -64,3 +65,4 @@ export declare const downloadFile: (api: AutoDriveApi, cid: string, password?: s
64
65
  */
65
66
  export declare const downloadFolderWithoutEncryption: (api: AutoDriveApi, cid: string) => Promise<ReadableStream<Uint8Array>>;
66
67
  export {};
68
+ //# sourceMappingURL=wrappers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrappers.d.ts","sourceRoot":"","sources":["../../src/api/wrappers.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAQJ,MAAM,0BAA0B,CAAA;AAcjC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAG9C,KAAK,iBAAiB,GAAG;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAiBD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,UAAU,QAChB,YAAY,YACP,MAAM,6BACkB,iBAAiB,oBACjC,MAAM,KACvB,OAAO,CAAC,GAAG,CAwCb,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,QAClB,YAAY,cACL,MAAM,oBACA,MAAM,KACvB,OAAO,CAAC,GAAG,CAsBb,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,QAClC,YAAY,YACP,MAAM,YACN,MAAM,oBACE,MAAM,KACvB,OAAO,CAAC,GAAG,CAeb,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,QAClB,YAAY,OACZ,MAAM,aACA,MAAM,KAChB,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAoB/B,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,+BAA+B,QACrC,YAAY,OACZ,MAAM,KACV,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CASpC,CAAA"}
@@ -0,0 +1,192 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
11
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12
+ var m = o[Symbol.asyncIterator], i;
13
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
14
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
15
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
16
+ };
17
+ import { compressFile, CompressionAlgorithm, decompressFile, decryptFile, encryptFile, EncryptionAlgorithm, stringToCid, } from '@autonomys/auto-dag-data';
18
+ import fs from 'fs';
19
+ import mime from 'mime-types';
20
+ import { asyncByChunk, asyncFromStream } from '../utils/async.js';
21
+ import { getFiles } from '../utils/folder.js';
22
+ import { completeUpload, createFileUpload, createFileUploadWithinFolderUpload, createFolderUpload, downloadObject, getObjectMetadata, uploadFileChunk, } from './calls/index.js';
23
+ import { constructFromFileSystemEntries } from './models/folderTree.js';
24
+ const UPLOAD_FILE_CHUNK_SIZE = 1024 * 1024;
25
+ const uploadFileChunks = (api_1, fileUploadId_1, asyncIterable_1, ...args_1) => __awaiter(void 0, [api_1, fileUploadId_1, asyncIterable_1, ...args_1], void 0, function* (api, fileUploadId, asyncIterable, uploadChunkSize = UPLOAD_FILE_CHUNK_SIZE) {
26
+ var _a, e_1, _b, _c;
27
+ let index = 0;
28
+ try {
29
+ for (var _d = true, _e = __asyncValues(asyncByChunk(asyncIterable, uploadChunkSize)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
30
+ _c = _f.value;
31
+ _d = false;
32
+ const chunk = _c;
33
+ yield uploadFileChunk(api, { uploadId: fileUploadId, chunk, index });
34
+ index++;
35
+ }
36
+ }
37
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
38
+ finally {
39
+ try {
40
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
41
+ }
42
+ finally { if (e_1) throw e_1.error; }
43
+ }
44
+ });
45
+ /**
46
+ * Uploads a file to the server with optional encryption and compression.
47
+ *
48
+ * This function reads a file from the specified file path, optionally encrypts it
49
+ * using the provided password, and compresses it using the ZLIB algorithm if specified.
50
+ * It then uploads the file in chunks to the server, creating an upload session and
51
+ * completing it once all chunks have been uploaded.
52
+ *
53
+ * @param {AutoDriveApi} api - The API instance used to send requests.
54
+ * @param {string} filePath - The path to the file to be uploaded.
55
+ * @param {UploadFileOptions} options - Options for the upload process.
56
+ * @param {string} [options.password] - The password for encryption (optional).
57
+ * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
58
+ * @returns {Promise<void>} - A promise that resolves when the upload is complete.
59
+ * @throws {Error} - Throws an error if the upload fails at any stage.
60
+ */
61
+ export const uploadFile = (api_1, filePath_1, _a, uploadChunkSize_1) => __awaiter(void 0, [api_1, filePath_1, _a, uploadChunkSize_1], void 0, function* (api, filePath, { password, compression = true }, uploadChunkSize) {
62
+ let asyncIterable = fs.createReadStream(filePath);
63
+ if (compression) {
64
+ asyncIterable = compressFile(asyncIterable, {
65
+ level: 9,
66
+ algorithm: CompressionAlgorithm.ZLIB,
67
+ });
68
+ }
69
+ if (password) {
70
+ asyncIterable = encryptFile(asyncIterable, password, {
71
+ algorithm: EncryptionAlgorithm.AES_256_GCM,
72
+ });
73
+ }
74
+ const uploadOptions = {
75
+ compression: compression
76
+ ? {
77
+ level: 9,
78
+ algorithm: CompressionAlgorithm.ZLIB,
79
+ }
80
+ : undefined,
81
+ encryption: password
82
+ ? {
83
+ algorithm: EncryptionAlgorithm.AES_256_GCM,
84
+ }
85
+ : undefined,
86
+ };
87
+ const fileUpload = yield createFileUpload(api, {
88
+ mimeType: mime.lookup(filePath) || undefined,
89
+ filename: filePath.split('/').pop(),
90
+ uploadOptions,
91
+ });
92
+ yield uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize);
93
+ const result = yield completeUpload(api, { uploadId: fileUpload.id });
94
+ return stringToCid(result.cid);
95
+ });
96
+ /**
97
+ * Uploads an entire folder to the server.
98
+ *
99
+ * This function retrieves all files within the specified folder,
100
+ * constructs a file tree representation, and initiates the upload
101
+ * process. It also handles optional compression of the files during
102
+ * the upload.
103
+ *
104
+ * @param {AutoDriveApi} api - The API instance used to send requests.
105
+ * @param {string} folderPath - The path of the folder to be uploaded.
106
+ * @returns {Promise<void>} - A promise that resolves when the folder upload is complete.
107
+ * @throws {Error} - Throws an error if the upload fails at any stage.
108
+ */
109
+ export const uploadFolder = (api, folderPath, uploadChunkSize) => __awaiter(void 0, void 0, void 0, function* () {
110
+ const files = yield getFiles(folderPath);
111
+ const fileTree = constructFromFileSystemEntries(files);
112
+ const folderUpload = yield createFolderUpload(api, {
113
+ fileTree,
114
+ uploadOptions: {
115
+ compression: {
116
+ algorithm: CompressionAlgorithm.ZLIB,
117
+ level: 9,
118
+ },
119
+ },
120
+ });
121
+ for (const file of files) {
122
+ yield uploadFileWithinFolderUpload(api, folderUpload.id, file, uploadChunkSize);
123
+ }
124
+ const result = yield completeUpload(api, { uploadId: folderUpload.id });
125
+ return stringToCid(result.cid);
126
+ });
127
+ /**
128
+ * Uploads a file within an existing folder upload session.
129
+ *
130
+ * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
131
+ * @param {string} uploadId - The ID of the folder upload session to which the file will be added.
132
+ * @param {string} filepath - The path of the file to be uploaded.
133
+ *
134
+ * @returns {Promise<void>} A promise that resolves when the file upload is complete.
135
+ */
136
+ export const uploadFileWithinFolderUpload = (api, uploadId, filepath, uploadChunkSize) => __awaiter(void 0, void 0, void 0, function* () {
137
+ const name = filepath.split('/').pop();
138
+ const fileUpload = yield createFileUploadWithinFolderUpload(api, {
139
+ uploadId,
140
+ name,
141
+ mimeType: mime.lookup(name) || undefined,
142
+ relativeId: filepath,
143
+ uploadOptions: {},
144
+ });
145
+ yield uploadFileChunks(api, fileUpload.id, fs.createReadStream(filepath), uploadChunkSize);
146
+ const result = yield completeUpload(api, { uploadId: fileUpload.id });
147
+ return stringToCid(result.cid);
148
+ });
149
+ /**
150
+ * Downloads a file from the AutoDrive service.
151
+ *
152
+ * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
153
+ * @param {string} cid - The CID of the file to be downloaded.
154
+ * @returns {Promise<ReadableStream<Uint8Array>>} A promise that resolves to a ReadableStream of the downloaded file.
155
+ */
156
+ export const downloadFile = (api, cid, password) => __awaiter(void 0, void 0, void 0, function* () {
157
+ var _a, _b;
158
+ const metadata = yield getObjectMetadata(api, { cid });
159
+ let iterable = asyncFromStream(yield downloadObject(api, { cid }));
160
+ if ((_a = metadata.uploadOptions) === null || _a === void 0 ? void 0 : _a.encryption) {
161
+ if (!password) {
162
+ throw new Error('Password is required to decrypt the file');
163
+ }
164
+ iterable = decryptFile(iterable, password, {
165
+ algorithm: EncryptionAlgorithm.AES_256_GCM,
166
+ });
167
+ }
168
+ if ((_b = metadata.uploadOptions) === null || _b === void 0 ? void 0 : _b.compression) {
169
+ iterable = decompressFile(iterable, {
170
+ algorithm: CompressionAlgorithm.ZLIB,
171
+ });
172
+ }
173
+ return iterable;
174
+ });
175
+ /**
176
+ * Downloads a folder from the AutoDrive service without encryption.
177
+ *
178
+ * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
179
+ * @param {string} cid - The CID of the folder to be downloaded.
180
+ *
181
+ * @returns {Promise<ReadableStream<Uint8Array>>} A promise that resolves to a ReadableStream of the downloaded folder.
182
+ *
183
+ * @warning If the folder is encrypted, a warning will be logged, but the download will proceed without decryption.
184
+ */
185
+ export const downloadFolderWithoutEncryption = (api, cid) => __awaiter(void 0, void 0, void 0, function* () {
186
+ var _a;
187
+ const metadata = yield getObjectMetadata(api, { cid });
188
+ if ((_a = metadata.uploadOptions) === null || _a === void 0 ? void 0 : _a.encryption) {
189
+ console.warn('Downloading encrypted folder. Folder support encryption but it is not recommended.');
190
+ }
191
+ return downloadObject(api, { cid });
192
+ });
@@ -1,2 +1,3 @@
1
1
  export * from './api/index.js';
2
2
  export * from './utils/index.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
@@ -1,2 +1,3 @@
1
1
  export declare const asyncByChunk: (asyncIterable: AsyncIterable<Buffer>, chunkSize: number) => AsyncGenerator<Buffer, void, unknown>;
2
2
  export declare const asyncFromStream: (stream: ReadableStream<Uint8Array>) => AsyncIterable<Buffer>;
3
+ //# sourceMappingURL=async.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/utils/async.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,kBACR,aAAa,CAAC,MAAM,CAAC,aACzB,MAAM,0CAelB,CAAA;AAED,eAAO,MAAM,eAAe,WAClB,cAAc,CAAC,UAAU,CAAC,KACjC,aAAa,CAAC,MAAM,CAOtB,CAAA"}
@@ -0,0 +1,58 @@
1
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
2
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
3
+ var m = o[Symbol.asyncIterator], i;
4
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
5
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
6
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
7
+ };
8
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
9
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
10
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
11
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
12
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
13
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
14
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
15
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
16
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
17
+ function fulfill(value) { resume("next", value); }
18
+ function reject(value) { resume("throw", value); }
19
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
20
+ };
21
+ export const asyncByChunk = function (asyncIterable, chunkSize) {
22
+ return __asyncGenerator(this, arguments, function* () {
23
+ var _a, e_1, _b, _c;
24
+ let buffer = Buffer.alloc(0);
25
+ try {
26
+ for (var _d = true, asyncIterable_1 = __asyncValues(asyncIterable), asyncIterable_1_1; asyncIterable_1_1 = yield __await(asyncIterable_1.next()), _a = asyncIterable_1_1.done, !_a; _d = true) {
27
+ _c = asyncIterable_1_1.value;
28
+ _d = false;
29
+ const chunk = _c;
30
+ buffer = Buffer.concat([buffer, chunk]);
31
+ while (buffer.length >= chunkSize) {
32
+ yield yield __await(buffer.subarray(0, chunkSize));
33
+ buffer = buffer.subarray(chunkSize);
34
+ }
35
+ }
36
+ }
37
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
38
+ finally {
39
+ try {
40
+ if (!_d && !_a && (_b = asyncIterable_1.return)) yield __await(_b.call(asyncIterable_1));
41
+ }
42
+ finally { if (e_1) throw e_1.error; }
43
+ }
44
+ if (buffer.length > 0) {
45
+ yield yield __await(buffer);
46
+ }
47
+ });
48
+ };
49
+ export const asyncFromStream = function (stream) {
50
+ return __asyncGenerator(this, arguments, function* () {
51
+ const reader = stream.getReader();
52
+ let result = yield __await(reader.read());
53
+ while (!result.done) {
54
+ yield yield __await(Buffer.from(result.value));
55
+ result = yield __await(reader.read());
56
+ }
57
+ });
58
+ };
@@ -1 +1,2 @@
1
1
  export declare const getFiles: (folderPath: string) => Promise<string[]>;
2
+ //# sourceMappingURL=folder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"folder.d.ts","sourceRoot":"","sources":["../../src/utils/folder.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,eAAsB,MAAM,KAAG,OAAO,CAAC,MAAM,EAAE,CAWnE,CAAA"}
@@ -0,0 +1,23 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import fs from 'fs/promises';
11
+ import path from 'path';
12
+ export const getFiles = (folderPath) => __awaiter(void 0, void 0, void 0, function* () {
13
+ const stat = yield fs.stat(folderPath);
14
+ if (stat.isDirectory()) {
15
+ const files = yield fs.readdir(folderPath);
16
+ const promises = files.map((file) => getFiles(path.join(folderPath, file)));
17
+ const allFiles = yield Promise.all(promises);
18
+ return allFiles.flat();
19
+ }
20
+ else {
21
+ return [folderPath];
22
+ }
23
+ });
@@ -2,3 +2,4 @@ export * from './async.js';
2
2
  export * from './folder.js';
3
3
  export * from './stream.js';
4
4
  export * from './types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
@@ -1,2 +1,3 @@
1
1
  import { WriteStream } from 'fs';
2
2
  export declare const createWriteStreamAdapter: (nodeWriteStream: WriteStream) => WritableStream<Uint8Array>;
3
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/utils/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,IAAI,CAAA;AAEhC,eAAO,MAAM,wBAAwB,oBAClB,WAAW,KAC3B,cAAc,CAAC,UAAU,CAoB3B,CAAA"}
@@ -0,0 +1,22 @@
1
+ export const createWriteStreamAdapter = (nodeWriteStream) => {
2
+ return new WritableStream({
3
+ write(chunk) {
4
+ return new Promise((resolve, reject) => {
5
+ nodeWriteStream.write(chunk, (err) => {
6
+ if (err) {
7
+ reject(err);
8
+ }
9
+ else {
10
+ resolve();
11
+ }
12
+ });
13
+ });
14
+ },
15
+ close() {
16
+ nodeWriteStream.end();
17
+ },
18
+ abort(err) {
19
+ nodeWriteStream.destroy(err);
20
+ },
21
+ });
22
+ };
@@ -3,3 +3,4 @@ export type ArgsWithPagination<T = {}> = T & {
3
3
  offset: number;
4
4
  };
5
5
  export type ArgsWithoutPagination<T = {}> = T;
6
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG;IAC3C,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,MAAM,MAAM,qBAAqB,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
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.3",
6
+ "version": "1.0.4",
7
7
  "author": {
8
8
  "name": "Autonomys",
9
9
  "url": "https://autonomys.net"
@@ -16,9 +16,9 @@
16
16
  "typescript": "^5.6.3"
17
17
  },
18
18
  "dependencies": {
19
- "@autonomys/auto-dag-data": "^1.0.3",
19
+ "@autonomys/auto-dag-data": "^1.0.4",
20
20
  "mime-types": "^2.1.35",
21
21
  "zod": "^3.23.8"
22
22
  },
23
- "gitHead": "15b7535a4e4f0ff7d66b20941b76dcf6a3e56005"
23
+ "gitHead": "7cf7c4940f48d88322bdfc2ab6e1e28d34dd396a"
24
24
  }
@@ -1,7 +0,0 @@
1
- export interface AutoDriveApi {
2
- sendRequest: (relativeUrl: string, request: Partial<Request>, body?: BodyInit) => Promise<Response>;
3
- }
4
- export declare const createAutoDriveApi: ({ apiKey, url, }: {
5
- apiKey: string;
6
- url?: string;
7
- }) => AutoDriveApi;
@@ -1,29 +0,0 @@
1
- import { z } from 'zod';
2
- export type FolderTreeFolder = {
3
- name: string;
4
- type: 'folder';
5
- children: FolderTree[];
6
- id: string;
7
- };
8
- export type FolderTreeFile = {
9
- name: string;
10
- type: 'file';
11
- id: string;
12
- };
13
- export type FolderTree = FolderTreeFolder | FolderTreeFile;
14
- export declare const FolderTreeFolderSchema: any;
15
- export declare const FolderTreeFileSchema: z.ZodObject<{
16
- name: z.ZodString;
17
- type: z.ZodLiteral<"file">;
18
- id: z.ZodString;
19
- }, "strip", z.ZodTypeAny, {
20
- name?: string;
21
- type?: "file";
22
- id?: string;
23
- }, {
24
- name?: string;
25
- type?: "file";
26
- id?: string;
27
- }>;
28
- export declare const FolderTreeSchema: any;
29
- export declare const constructFromFileSystemEntries: (entries: string[]) => FolderTree;