@autonomys/auto-drive 0.8.2 → 1.0.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.
Files changed (56) hide show
  1. package/LICENSE +18 -0
  2. package/package.json +3 -3
  3. package/dist/api/calls/download.d.ts +0 -6
  4. package/dist/api/calls/download.d.ts.map +0 -1
  5. package/dist/api/calls/download.js +0 -12
  6. package/dist/api/calls/index.d.ts +0 -5
  7. package/dist/api/calls/index.d.ts.map +0 -1
  8. package/dist/api/calls/index.js +0 -4
  9. package/dist/api/calls/read.d.ts +0 -98
  10. package/dist/api/calls/read.d.ts.map +0 -1
  11. package/dist/api/calls/read.js +0 -140
  12. package/dist/api/calls/upload.d.ts +0 -94
  13. package/dist/api/calls/upload.d.ts.map +0 -1
  14. package/dist/api/calls/upload.js +0 -120
  15. package/dist/api/calls/write.d.ts +0 -49
  16. package/dist/api/calls/write.d.ts.map +0 -1
  17. package/dist/api/calls/write.js +0 -65
  18. package/dist/api/connection.d.ts +0 -8
  19. package/dist/api/connection.d.ts.map +0 -1
  20. package/dist/api/connection.js +0 -17
  21. package/dist/api/index.d.ts +0 -5
  22. package/dist/api/index.d.ts.map +0 -1
  23. package/dist/api/index.js +0 -4
  24. package/dist/api/models/folderTree.d.ts +0 -45
  25. package/dist/api/models/folderTree.d.ts.map +0 -1
  26. package/dist/api/models/folderTree.js +0 -56
  27. package/dist/api/models/index.d.ts +0 -3
  28. package/dist/api/models/index.d.ts.map +0 -1
  29. package/dist/api/models/index.js +0 -2
  30. package/dist/api/models/objects.d.ts +0 -41
  31. package/dist/api/models/objects.d.ts.map +0 -1
  32. package/dist/api/models/objects.js +0 -10
  33. package/dist/api/models/uploads.d.ts +0 -114
  34. package/dist/api/models/uploads.d.ts.map +0 -1
  35. package/dist/api/models/uploads.js +0 -40
  36. package/dist/api/wrappers.d.ts +0 -67
  37. package/dist/api/wrappers.d.ts.map +0 -1
  38. package/dist/api/wrappers.js +0 -158
  39. package/dist/index.d.ts +0 -3
  40. package/dist/index.d.ts.map +0 -1
  41. package/dist/index.js +0 -2
  42. package/dist/utils/async.d.ts +0 -3
  43. package/dist/utils/async.d.ts.map +0 -1
  44. package/dist/utils/async.js +0 -21
  45. package/dist/utils/folder.d.ts +0 -2
  46. package/dist/utils/folder.d.ts.map +0 -1
  47. package/dist/utils/folder.js +0 -14
  48. package/dist/utils/index.d.ts +0 -5
  49. package/dist/utils/index.d.ts.map +0 -1
  50. package/dist/utils/index.js +0 -4
  51. package/dist/utils/stream.d.ts +0 -3
  52. package/dist/utils/stream.d.ts.map +0 -1
  53. package/dist/utils/stream.js +0 -22
  54. package/dist/utils/types.d.ts +0 -6
  55. package/dist/utils/types.d.ts.map +0 -1
  56. package/dist/utils/types.js +0 -1
@@ -1,158 +0,0 @@
1
- import { compressFile, CompressionAlgorithm, decompressFile, decryptFile, encryptFile, EncryptionAlgorithm, } from '@autonomys/auto-dag-data';
2
- import fs from 'fs';
3
- import mime from 'mime-types';
4
- import { asyncByChunk, asyncFromStream } from '../utils/async.js';
5
- import { getFiles } from '../utils/folder.js';
6
- import { completeUpload, createFileUpload, createFileUploadWithinFolderUpload, createFolderUpload, downloadObject, getObjectMetadata, uploadFileChunk, } from './calls/index.js';
7
- import { constructFromFileSystemEntries } from './models/folderTree.js';
8
- const UPLOAD_FILE_CHUNK_SIZE = 1024 * 1024;
9
- const uploadFileChunks = async (api, fileUploadId, asyncIterable, uploadChunkSize = UPLOAD_FILE_CHUNK_SIZE) => {
10
- let index = 0;
11
- for await (const chunk of asyncByChunk(asyncIterable, uploadChunkSize)) {
12
- await uploadFileChunk(api, { uploadId: fileUploadId, chunk, index });
13
- index++;
14
- }
15
- };
16
- /**
17
- * Uploads a file to the server with optional encryption and compression.
18
- *
19
- * This function reads a file from the specified file path, optionally encrypts it
20
- * using the provided password, and compresses it using the ZLIB algorithm if specified.
21
- * It then uploads the file in chunks to the server, creating an upload session and
22
- * completing it once all chunks have been uploaded.
23
- *
24
- * @param {AutoDriveApi} api - The API instance used to send requests.
25
- * @param {string} filePath - The path to the file to be uploaded.
26
- * @param {UploadFileOptions} options - Options for the upload process.
27
- * @param {string} [options.password] - The password for encryption (optional).
28
- * @param {boolean} [options.compression=true] - Whether to compress the file (optional).
29
- * @returns {Promise<void>} - A promise that resolves when the upload is complete.
30
- * @throws {Error} - Throws an error if the upload fails at any stage.
31
- */
32
- export const uploadFile = async (api, filePath, { password, compression = true }, uploadChunkSize) => {
33
- let asyncIterable = fs.createReadStream(filePath);
34
- if (compression) {
35
- asyncIterable = compressFile(asyncIterable, {
36
- level: 9,
37
- algorithm: CompressionAlgorithm.ZLIB,
38
- });
39
- }
40
- if (password) {
41
- asyncIterable = encryptFile(asyncIterable, password, {
42
- algorithm: EncryptionAlgorithm.AES_256_GCM,
43
- });
44
- }
45
- const uploadOptions = {
46
- compression: compression
47
- ? {
48
- level: 9,
49
- algorithm: CompressionAlgorithm.ZLIB,
50
- }
51
- : undefined,
52
- encryption: password
53
- ? {
54
- algorithm: EncryptionAlgorithm.AES_256_GCM,
55
- }
56
- : undefined,
57
- };
58
- const fileUpload = await createFileUpload(api, {
59
- mimeType: mime.lookup(filePath) || undefined,
60
- filename: filePath.split('/').pop(),
61
- uploadOptions,
62
- });
63
- await uploadFileChunks(api, fileUpload.id, asyncIterable, uploadChunkSize);
64
- await completeUpload(api, { uploadId: fileUpload.id });
65
- };
66
- /**
67
- * Uploads an entire folder to the server.
68
- *
69
- * This function retrieves all files within the specified folder,
70
- * constructs a file tree representation, and initiates the upload
71
- * process. It also handles optional compression of the files during
72
- * the upload.
73
- *
74
- * @param {AutoDriveApi} api - The API instance used to send requests.
75
- * @param {string} folderPath - The path of the folder to be uploaded.
76
- * @returns {Promise<void>} - A promise that resolves when the folder upload is complete.
77
- * @throws {Error} - Throws an error if the upload fails at any stage.
78
- */
79
- export const uploadFolder = async (api, folderPath, uploadChunkSize) => {
80
- const files = await getFiles(folderPath);
81
- const fileTree = constructFromFileSystemEntries(files);
82
- const folderUpload = await createFolderUpload(api, {
83
- fileTree,
84
- uploadOptions: {
85
- compression: {
86
- algorithm: CompressionAlgorithm.ZLIB,
87
- level: 9,
88
- },
89
- },
90
- });
91
- for (const file of files) {
92
- await uploadFileWithinFolderUpload(api, folderUpload.id, file, uploadChunkSize);
93
- }
94
- await completeUpload(api, { uploadId: folderUpload.id });
95
- };
96
- /**
97
- * Uploads a file within an existing folder upload session.
98
- *
99
- * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
100
- * @param {string} uploadId - The ID of the folder upload session to which the file will be added.
101
- * @param {string} filepath - The path of the file to be uploaded.
102
- *
103
- * @returns {Promise<void>} A promise that resolves when the file upload is complete.
104
- */
105
- export const uploadFileWithinFolderUpload = async (api, uploadId, filepath, uploadChunkSize) => {
106
- const name = filepath.split('/').pop();
107
- const fileUpload = await createFileUploadWithinFolderUpload(api, {
108
- uploadId,
109
- name,
110
- mimeType: mime.lookup(name) || undefined,
111
- relativeId: filepath,
112
- uploadOptions: {},
113
- });
114
- await uploadFileChunks(api, fileUpload.id, fs.createReadStream(filepath), uploadChunkSize);
115
- await completeUpload(api, { uploadId: fileUpload.id });
116
- };
117
- /**
118
- * Downloads a file from the AutoDrive service.
119
- *
120
- * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
121
- * @param {string} cid - The CID of the file to be downloaded.
122
- * @returns {Promise<ReadableStream<Uint8Array>>} A promise that resolves to a ReadableStream of the downloaded file.
123
- */
124
- export const downloadFile = async (api, cid, password) => {
125
- const metadata = await getObjectMetadata(api, { cid });
126
- let iterable = asyncFromStream(await downloadObject(api, { cid }));
127
- if (metadata.uploadOptions?.encryption) {
128
- if (!password) {
129
- throw new Error('Password is required to decrypt the file');
130
- }
131
- iterable = decryptFile(iterable, password, {
132
- algorithm: EncryptionAlgorithm.AES_256_GCM,
133
- });
134
- }
135
- if (metadata.uploadOptions?.compression) {
136
- iterable = decompressFile(iterable, {
137
- algorithm: CompressionAlgorithm.ZLIB,
138
- });
139
- }
140
- return iterable;
141
- };
142
- /**
143
- * Downloads a folder from the AutoDrive service without encryption.
144
- *
145
- * @param {AutoDriveApi} api - The API instance to interact with the AutoDrive service.
146
- * @param {string} cid - The CID of the folder to be downloaded.
147
- *
148
- * @returns {Promise<ReadableStream<Uint8Array>>} A promise that resolves to a ReadableStream of the downloaded folder.
149
- *
150
- * @warning If the folder is encrypted, a warning will be logged, but the download will proceed without decryption.
151
- */
152
- export const downloadFolderWithoutEncryption = async (api, cid) => {
153
- const metadata = await getObjectMetadata(api, { cid });
154
- if (metadata.uploadOptions?.encryption) {
155
- console.warn('Downloading encrypted folder. Folder support encryption but it is not recommended.');
156
- }
157
- return downloadObject(api, { cid });
158
- };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './api/index.js';
2
- export * from './utils/index.js';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export * from './api/index.js';
2
- export * from './utils/index.js';
@@ -1,3 +0,0 @@
1
- export declare const asyncByChunk: (asyncIterable: AsyncIterable<Buffer>, chunkSize: number) => AsyncGenerator<Buffer, void, unknown>;
2
- export declare const asyncFromStream: (stream: ReadableStream<Uint8Array>) => AsyncIterable<Buffer>;
3
- //# sourceMappingURL=async.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,21 +0,0 @@
1
- export const asyncByChunk = async function* (asyncIterable, chunkSize) {
2
- let buffer = Buffer.alloc(0);
3
- for await (const chunk of asyncIterable) {
4
- buffer = Buffer.concat([buffer, chunk]);
5
- while (buffer.length >= chunkSize) {
6
- yield buffer.subarray(0, chunkSize);
7
- buffer = buffer.subarray(chunkSize);
8
- }
9
- }
10
- if (buffer.length > 0) {
11
- yield buffer;
12
- }
13
- };
14
- export const asyncFromStream = async function* (stream) {
15
- const reader = stream.getReader();
16
- let result = await reader.read();
17
- while (!result.done) {
18
- yield Buffer.from(result.value);
19
- result = await reader.read();
20
- }
21
- };
@@ -1,2 +0,0 @@
1
- export declare const getFiles: (folderPath: string) => Promise<string[]>;
2
- //# sourceMappingURL=folder.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,14 +0,0 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- export const getFiles = async (folderPath) => {
4
- const stat = await fs.stat(folderPath);
5
- if (stat.isDirectory()) {
6
- const files = await fs.readdir(folderPath);
7
- const promises = files.map((file) => getFiles(path.join(folderPath, file)));
8
- const allFiles = await Promise.all(promises);
9
- return allFiles.flat();
10
- }
11
- else {
12
- return [folderPath];
13
- }
14
- };
@@ -1,5 +0,0 @@
1
- export * from './async.js';
2
- export * from './folder.js';
3
- export * from './stream.js';
4
- export * from './types.js';
5
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
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,4 +0,0 @@
1
- export * from './async.js';
2
- export * from './folder.js';
3
- export * from './stream.js';
4
- export * from './types.js';
@@ -1,3 +0,0 @@
1
- import { WriteStream } from 'fs';
2
- export declare const createWriteStreamAdapter: (nodeWriteStream: WriteStream) => WritableStream<Uint8Array>;
3
- //# sourceMappingURL=stream.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,22 +0,0 @@
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
- };
@@ -1,6 +0,0 @@
1
- export type ArgsWithPagination<T = {}> = T & {
2
- limit: number;
3
- offset: number;
4
- };
5
- export type ArgsWithoutPagination<T = {}> = T;
6
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
1
- export {};