@human-protocol/sdk 5.2.0 → 6.0.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/dist/base.d.ts +4 -5
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +4 -5
- package/dist/constants.js +6 -6
- package/dist/encryption.d.ts +68 -203
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +66 -202
- package/dist/error.d.ts +0 -24
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -26
- package/dist/escrow.d.ts +427 -780
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +314 -684
- package/dist/graphql/queries/operator.d.ts.map +1 -1
- package/dist/graphql/queries/operator.js +3 -1
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/kvstore.d.ts +119 -181
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +119 -182
- package/dist/operator.d.ts +59 -30
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +59 -30
- package/dist/staking.d.ts +135 -134
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +135 -134
- package/dist/statistics.d.ts +104 -134
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +119 -144
- package/dist/transaction.d.ts +36 -15
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +36 -15
- package/dist/types.d.ts +0 -54
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +31 -17
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +31 -17
- package/dist/worker.d.ts +35 -14
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +35 -14
- package/package.json +7 -24
- package/src/base.ts +4 -5
- package/src/constants.ts +6 -6
- package/src/encryption.ts +69 -203
- package/src/error.ts +0 -36
- package/src/escrow.ts +425 -780
- package/src/graphql/queries/operator.ts +3 -1
- package/src/graphql/types.ts +4 -2
- package/src/index.ts +4 -5
- package/src/kvstore.ts +120 -183
- package/src/operator.ts +59 -30
- package/src/staking.ts +135 -134
- package/src/statistics.ts +125 -146
- package/src/transaction.ts +36 -15
- package/src/types.ts +0 -57
- package/src/utils.ts +31 -17
- package/src/worker.ts +35 -14
- package/dist/storage.d.ts +0 -186
- package/dist/storage.d.ts.map +0 -1
- package/dist/storage.js +0 -319
- package/src/storage.ts +0 -313
package/src/worker.ts
CHANGED
|
@@ -7,21 +7,40 @@ import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from './graphql/queries/worker';
|
|
|
7
7
|
import { IWorker, IWorkersFilter, SubgraphOptions } from './interfaces';
|
|
8
8
|
import { getSubgraphUrl, customGqlFetch } from './utils';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Utility class for worker-related operations.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { WorkerUtils, ChainId } from '@human-protocol/sdk';
|
|
16
|
+
*
|
|
17
|
+
* const worker = await WorkerUtils.getWorker(
|
|
18
|
+
* ChainId.POLYGON_AMOY,
|
|
19
|
+
* '0x1234567890abcdef1234567890abcdef12345678'
|
|
20
|
+
* );
|
|
21
|
+
* console.log('Worker:', worker);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
10
24
|
export class WorkerUtils {
|
|
11
25
|
/**
|
|
12
26
|
* This function returns the worker data for the given address.
|
|
13
27
|
*
|
|
14
|
-
* @param
|
|
15
|
-
* @param
|
|
16
|
-
* @param
|
|
17
|
-
* @returns
|
|
18
|
-
*
|
|
19
|
-
*
|
|
28
|
+
* @param chainId - The chain ID.
|
|
29
|
+
* @param address - The worker address.
|
|
30
|
+
* @param options - Optional configuration for subgraph requests.
|
|
31
|
+
* @returns Returns the worker details or null if not found.
|
|
32
|
+
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
33
|
+
* @throws ErrorInvalidAddress If the address is invalid
|
|
20
34
|
*
|
|
35
|
+
* @example
|
|
21
36
|
* ```ts
|
|
22
37
|
* import { WorkerUtils, ChainId } from '@human-protocol/sdk';
|
|
23
38
|
*
|
|
24
|
-
* const worker = await WorkerUtils.getWorker(
|
|
39
|
+
* const worker = await WorkerUtils.getWorker(
|
|
40
|
+
* ChainId.POLYGON_AMOY,
|
|
41
|
+
* '0x1234567890abcdef1234567890abcdef12345678'
|
|
42
|
+
* );
|
|
43
|
+
* console.log('Worker:', worker);
|
|
25
44
|
* ```
|
|
26
45
|
*/
|
|
27
46
|
public static async getWorker(
|
|
@@ -79,21 +98,23 @@ export class WorkerUtils {
|
|
|
79
98
|
* };
|
|
80
99
|
* ```
|
|
81
100
|
*
|
|
82
|
-
* @param
|
|
83
|
-
* @param
|
|
84
|
-
* @returns
|
|
85
|
-
*
|
|
86
|
-
*
|
|
101
|
+
* @param filter - Filter for the workers.
|
|
102
|
+
* @param options - Optional configuration for subgraph requests.
|
|
103
|
+
* @returns Returns an array with all the worker details.
|
|
104
|
+
* @throws ErrorUnsupportedChainID If the chain ID is not supported
|
|
105
|
+
* @throws ErrorInvalidAddress If the filter address is invalid
|
|
87
106
|
*
|
|
107
|
+
* @example
|
|
88
108
|
* ```ts
|
|
89
109
|
* import { WorkerUtils, ChainId } from '@human-protocol/sdk';
|
|
90
110
|
*
|
|
91
|
-
* const filter
|
|
92
|
-
* chainId: ChainId.
|
|
111
|
+
* const filter = {
|
|
112
|
+
* chainId: ChainId.POLYGON_AMOY,
|
|
93
113
|
* first: 10,
|
|
94
114
|
* skip: 0,
|
|
95
115
|
* };
|
|
96
116
|
* const workers = await WorkerUtils.getWorkers(filter);
|
|
117
|
+
* console.log('Workers:', workers.length);
|
|
97
118
|
* ```
|
|
98
119
|
*/
|
|
99
120
|
public static async getWorkers(
|
package/dist/storage.d.ts
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { UploadFile, StorageCredentials, StorageParams } from './types';
|
|
2
|
-
/**
|
|
3
|
-
*
|
|
4
|
-
* @deprecated StorageClient is deprecated. Use Minio.Client directly.
|
|
5
|
-
*
|
|
6
|
-
* ## Introduction
|
|
7
|
-
*
|
|
8
|
-
* This client enables interacting with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage, and others.
|
|
9
|
-
*
|
|
10
|
-
* The instance creation of `StorageClient` should be made using its constructor:
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* constructor(params: StorageParams, credentials?: StorageCredentials)
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* > If credentials are not provided, it uses anonymous access to the bucket for downloading files.
|
|
17
|
-
*
|
|
18
|
-
* ## Installation
|
|
19
|
-
*
|
|
20
|
-
* ### npm
|
|
21
|
-
* ```bash
|
|
22
|
-
* npm install @human-protocol/sdk
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* ### yarn
|
|
26
|
-
* ```bash
|
|
27
|
-
* yarn install @human-protocol/sdk
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* ## Code example
|
|
31
|
-
*
|
|
32
|
-
* ```ts
|
|
33
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
34
|
-
*
|
|
35
|
-
* const credentials: StorageCredentials = {
|
|
36
|
-
* accessKey: 'ACCESS_KEY',
|
|
37
|
-
* secretKey: 'SECRET_KEY',
|
|
38
|
-
* };
|
|
39
|
-
* const params: StorageParams = {
|
|
40
|
-
* endPoint: 'http://localhost',
|
|
41
|
-
* port: 9000,
|
|
42
|
-
* useSSL: false,
|
|
43
|
-
* region: 'us-east-1'
|
|
44
|
-
* };
|
|
45
|
-
*
|
|
46
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export declare class StorageClient {
|
|
50
|
-
private client;
|
|
51
|
-
private clientParams;
|
|
52
|
-
/**
|
|
53
|
-
* **Storage client constructor**
|
|
54
|
-
*
|
|
55
|
-
* @param {StorageParams} params - Cloud storage params
|
|
56
|
-
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials are not provided - use anonymous access to the bucket
|
|
57
|
-
*/
|
|
58
|
-
constructor(params: StorageParams, credentials?: StorageCredentials);
|
|
59
|
-
/**
|
|
60
|
-
* This function downloads files from a bucket.
|
|
61
|
-
*
|
|
62
|
-
* @param {string[]} keys Array of filenames to download.
|
|
63
|
-
* @param {string} bucket Bucket name.
|
|
64
|
-
* @returns {Promise<any[]>} Returns an array of JSON files downloaded and parsed into objects.
|
|
65
|
-
*
|
|
66
|
-
* **Code example**
|
|
67
|
-
*
|
|
68
|
-
* ```ts
|
|
69
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
70
|
-
*
|
|
71
|
-
* const params: StorageParams = {
|
|
72
|
-
* endPoint: 'http://localhost',
|
|
73
|
-
* port: 9000,
|
|
74
|
-
* useSSL: false,
|
|
75
|
-
* region: 'us-east-1'
|
|
76
|
-
* };
|
|
77
|
-
*
|
|
78
|
-
* const storageClient = new StorageClient(params);
|
|
79
|
-
*
|
|
80
|
-
* const keys = ['file1.json', 'file2.json'];
|
|
81
|
-
* const files = await storageClient.downloadFiles(keys, 'bucket-name');
|
|
82
|
-
* ```
|
|
83
|
-
*/
|
|
84
|
-
downloadFiles(keys: string[], bucket: string): Promise<any[]>;
|
|
85
|
-
/**
|
|
86
|
-
* This function downloads files from a URL.
|
|
87
|
-
*
|
|
88
|
-
* @param {string} url URL of the file to download.
|
|
89
|
-
* @returns {Promise<any>} Returns the JSON file downloaded and parsed into an object.
|
|
90
|
-
*
|
|
91
|
-
* **Code example**
|
|
92
|
-
*
|
|
93
|
-
* ```ts
|
|
94
|
-
* import { StorageClient } from '@human-protocol/sdk';
|
|
95
|
-
*
|
|
96
|
-
* const file = await StorageClient.downloadFileFromUrl('http://localhost/file.json');
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
static downloadFileFromUrl(url: string): Promise<any>;
|
|
100
|
-
/**
|
|
101
|
-
* This function uploads files to a bucket.
|
|
102
|
-
*
|
|
103
|
-
* @param {any[]} files Array of objects to upload serialized into JSON.
|
|
104
|
-
* @param {string} bucket Bucket name.
|
|
105
|
-
* @returns {Promise<UploadFile[]>} Returns an array of uploaded file metadata.
|
|
106
|
-
*
|
|
107
|
-
* **Code example**
|
|
108
|
-
*
|
|
109
|
-
* ```ts
|
|
110
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
111
|
-
*
|
|
112
|
-
* const credentials: StorageCredentials = {
|
|
113
|
-
* accessKey: 'ACCESS_KEY',
|
|
114
|
-
* secretKey: 'SECRET_KEY',
|
|
115
|
-
* };
|
|
116
|
-
* const params: StorageParams = {
|
|
117
|
-
* endPoint: 'http://localhost',
|
|
118
|
-
* port: 9000,
|
|
119
|
-
* useSSL: false,
|
|
120
|
-
* region: 'us-east-1'
|
|
121
|
-
* };
|
|
122
|
-
*
|
|
123
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
124
|
-
* const file1 = { name: 'file1', description: 'description of file1' };
|
|
125
|
-
* const file2 = { name: 'file2', description: 'description of file2' };
|
|
126
|
-
* const files = [file1, file2];
|
|
127
|
-
* const uploadedFiles = await storageClient.uploadFiles(files, 'bucket-name');
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
uploadFiles(files: any[], bucket: string): Promise<UploadFile[]>;
|
|
131
|
-
/**
|
|
132
|
-
* This function checks if a bucket exists.
|
|
133
|
-
*
|
|
134
|
-
* @param {string} bucket Bucket name.
|
|
135
|
-
* @returns {Promise<boolean>} Returns `true` if exists, `false` if it doesn't.
|
|
136
|
-
*
|
|
137
|
-
* **Code example**
|
|
138
|
-
*
|
|
139
|
-
* ```ts
|
|
140
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
141
|
-
*
|
|
142
|
-
* const credentials: StorageCredentials = {
|
|
143
|
-
* accessKey: 'ACCESS_KEY',
|
|
144
|
-
* secretKey: 'SECRET_KEY',
|
|
145
|
-
* };
|
|
146
|
-
* const params: StorageParams = {
|
|
147
|
-
* endPoint: 'http://localhost',
|
|
148
|
-
* port: 9000,
|
|
149
|
-
* useSSL: false,
|
|
150
|
-
* region: 'us-east-1'
|
|
151
|
-
* };
|
|
152
|
-
*
|
|
153
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
154
|
-
* const exists = await storageClient.bucketExists('bucket-name');
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
bucketExists(bucket: string): Promise<boolean>;
|
|
158
|
-
/**
|
|
159
|
-
* This function lists all file names contained in the bucket.
|
|
160
|
-
*
|
|
161
|
-
* @param {string} bucket Bucket name.
|
|
162
|
-
* @returns {Promise<string[]>} Returns the list of file names contained in the bucket.
|
|
163
|
-
*
|
|
164
|
-
* **Code example**
|
|
165
|
-
*
|
|
166
|
-
* ```ts
|
|
167
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
168
|
-
*
|
|
169
|
-
* const credentials: StorageCredentials = {
|
|
170
|
-
* accessKey: 'ACCESS_KEY',
|
|
171
|
-
* secretKey: 'SECRET_KEY',
|
|
172
|
-
* };
|
|
173
|
-
* const params: StorageParams = {
|
|
174
|
-
* endPoint: 'http://localhost',
|
|
175
|
-
* port: 9000,
|
|
176
|
-
* useSSL: false,
|
|
177
|
-
* region: 'us-east-1'
|
|
178
|
-
* };
|
|
179
|
-
*
|
|
180
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
181
|
-
* const fileNames = await storageClient.listObjects('bucket-name');
|
|
182
|
-
* ```
|
|
183
|
-
*/
|
|
184
|
-
listObjects(bucket: string): Promise<string[]>;
|
|
185
|
-
}
|
|
186
|
-
//# sourceMappingURL=storage.d.ts.map
|
package/dist/storage.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,YAAY,CAAgB;IAEpC;;;;;OAKG;gBACS,MAAM,EAAE,aAAa,EAAE,WAAW,CAAC,EAAE,kBAAkB;IAcnE;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAoB1E;;;;;;;;;;;;;OAaG;WACiB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAsBlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACU,WAAW,CACtB,KAAK,EAAE,GAAG,EAAE,EACZ,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,UAAU,EAAE,CAAC;IAmCxB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3D;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAqB5D"}
|
package/dist/storage.js
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.StorageClient = void 0;
|
|
40
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
41
|
-
const axios_1 = __importDefault(require("axios"));
|
|
42
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
43
|
-
const Minio = __importStar(require("minio"));
|
|
44
|
-
const error_1 = require("./error");
|
|
45
|
-
const utils_1 = require("./utils");
|
|
46
|
-
const constants_1 = require("./constants");
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
49
|
-
* @deprecated StorageClient is deprecated. Use Minio.Client directly.
|
|
50
|
-
*
|
|
51
|
-
* ## Introduction
|
|
52
|
-
*
|
|
53
|
-
* This client enables interacting with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage, and others.
|
|
54
|
-
*
|
|
55
|
-
* The instance creation of `StorageClient` should be made using its constructor:
|
|
56
|
-
*
|
|
57
|
-
* ```ts
|
|
58
|
-
* constructor(params: StorageParams, credentials?: StorageCredentials)
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* > If credentials are not provided, it uses anonymous access to the bucket for downloading files.
|
|
62
|
-
*
|
|
63
|
-
* ## Installation
|
|
64
|
-
*
|
|
65
|
-
* ### npm
|
|
66
|
-
* ```bash
|
|
67
|
-
* npm install @human-protocol/sdk
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* ### yarn
|
|
71
|
-
* ```bash
|
|
72
|
-
* yarn install @human-protocol/sdk
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* ## Code example
|
|
76
|
-
*
|
|
77
|
-
* ```ts
|
|
78
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
79
|
-
*
|
|
80
|
-
* const credentials: StorageCredentials = {
|
|
81
|
-
* accessKey: 'ACCESS_KEY',
|
|
82
|
-
* secretKey: 'SECRET_KEY',
|
|
83
|
-
* };
|
|
84
|
-
* const params: StorageParams = {
|
|
85
|
-
* endPoint: 'http://localhost',
|
|
86
|
-
* port: 9000,
|
|
87
|
-
* useSSL: false,
|
|
88
|
-
* region: 'us-east-1'
|
|
89
|
-
* };
|
|
90
|
-
*
|
|
91
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
class StorageClient {
|
|
95
|
-
/**
|
|
96
|
-
* **Storage client constructor**
|
|
97
|
-
*
|
|
98
|
-
* @param {StorageParams} params - Cloud storage params
|
|
99
|
-
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials are not provided - use anonymous access to the bucket
|
|
100
|
-
*/
|
|
101
|
-
constructor(params, credentials) {
|
|
102
|
-
try {
|
|
103
|
-
this.clientParams = params;
|
|
104
|
-
this.client = new Minio.Client({
|
|
105
|
-
...params,
|
|
106
|
-
accessKey: credentials?.accessKey ?? '',
|
|
107
|
-
secretKey: credentials?.secretKey ?? '',
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
throw error_1.ErrorStorageClientNotInitialized;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* This function downloads files from a bucket.
|
|
116
|
-
*
|
|
117
|
-
* @param {string[]} keys Array of filenames to download.
|
|
118
|
-
* @param {string} bucket Bucket name.
|
|
119
|
-
* @returns {Promise<any[]>} Returns an array of JSON files downloaded and parsed into objects.
|
|
120
|
-
*
|
|
121
|
-
* **Code example**
|
|
122
|
-
*
|
|
123
|
-
* ```ts
|
|
124
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
125
|
-
*
|
|
126
|
-
* const params: StorageParams = {
|
|
127
|
-
* endPoint: 'http://localhost',
|
|
128
|
-
* port: 9000,
|
|
129
|
-
* useSSL: false,
|
|
130
|
-
* region: 'us-east-1'
|
|
131
|
-
* };
|
|
132
|
-
*
|
|
133
|
-
* const storageClient = new StorageClient(params);
|
|
134
|
-
*
|
|
135
|
-
* const keys = ['file1.json', 'file2.json'];
|
|
136
|
-
* const files = await storageClient.downloadFiles(keys, 'bucket-name');
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
async downloadFiles(keys, bucket) {
|
|
140
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
141
|
-
if (!isBucketExists) {
|
|
142
|
-
throw error_1.ErrorStorageBucketNotFound;
|
|
143
|
-
}
|
|
144
|
-
return Promise.all(keys.map(async (key) => {
|
|
145
|
-
try {
|
|
146
|
-
const response = await this.client.getObject(bucket, key);
|
|
147
|
-
const content = response?.read();
|
|
148
|
-
return { key, content: JSON.parse(content?.toString('utf-8') || '') };
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
throw error_1.ErrorStorageFileNotFound;
|
|
152
|
-
}
|
|
153
|
-
}));
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* This function downloads files from a URL.
|
|
157
|
-
*
|
|
158
|
-
* @param {string} url URL of the file to download.
|
|
159
|
-
* @returns {Promise<any>} Returns the JSON file downloaded and parsed into an object.
|
|
160
|
-
*
|
|
161
|
-
* **Code example**
|
|
162
|
-
*
|
|
163
|
-
* ```ts
|
|
164
|
-
* import { StorageClient } from '@human-protocol/sdk';
|
|
165
|
-
*
|
|
166
|
-
* const file = await StorageClient.downloadFileFromUrl('http://localhost/file.json');
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
static async downloadFileFromUrl(url) {
|
|
170
|
-
if (!(0, utils_1.isValidUrl)(url)) {
|
|
171
|
-
throw error_1.ErrorInvalidUrl;
|
|
172
|
-
}
|
|
173
|
-
try {
|
|
174
|
-
const { data, status } = await axios_1.default.get(url, {
|
|
175
|
-
headers: {
|
|
176
|
-
'Content-Type': 'application/json',
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
if (status !== constants_1.HttpStatus.OK) {
|
|
180
|
-
throw error_1.ErrorStorageFileNotFound;
|
|
181
|
-
}
|
|
182
|
-
return data;
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
throw error_1.ErrorStorageFileNotFound;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* This function uploads files to a bucket.
|
|
190
|
-
*
|
|
191
|
-
* @param {any[]} files Array of objects to upload serialized into JSON.
|
|
192
|
-
* @param {string} bucket Bucket name.
|
|
193
|
-
* @returns {Promise<UploadFile[]>} Returns an array of uploaded file metadata.
|
|
194
|
-
*
|
|
195
|
-
* **Code example**
|
|
196
|
-
*
|
|
197
|
-
* ```ts
|
|
198
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
199
|
-
*
|
|
200
|
-
* const credentials: StorageCredentials = {
|
|
201
|
-
* accessKey: 'ACCESS_KEY',
|
|
202
|
-
* secretKey: 'SECRET_KEY',
|
|
203
|
-
* };
|
|
204
|
-
* const params: StorageParams = {
|
|
205
|
-
* endPoint: 'http://localhost',
|
|
206
|
-
* port: 9000,
|
|
207
|
-
* useSSL: false,
|
|
208
|
-
* region: 'us-east-1'
|
|
209
|
-
* };
|
|
210
|
-
*
|
|
211
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
212
|
-
* const file1 = { name: 'file1', description: 'description of file1' };
|
|
213
|
-
* const file2 = { name: 'file2', description: 'description of file2' };
|
|
214
|
-
* const files = [file1, file2];
|
|
215
|
-
* const uploadedFiles = await storageClient.uploadFiles(files, 'bucket-name');
|
|
216
|
-
* ```
|
|
217
|
-
*/
|
|
218
|
-
async uploadFiles(files, bucket) {
|
|
219
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
220
|
-
if (!isBucketExists) {
|
|
221
|
-
throw error_1.ErrorStorageBucketNotFound;
|
|
222
|
-
}
|
|
223
|
-
return Promise.all(files.map(async (file) => {
|
|
224
|
-
const content = JSON.stringify(file);
|
|
225
|
-
const hash = crypto_1.default.createHash('sha1').update(content).digest('hex');
|
|
226
|
-
const key = `s3${hash}.json`;
|
|
227
|
-
try {
|
|
228
|
-
await this.client.putObject(bucket, key, content, {
|
|
229
|
-
'Content-Type': 'application/json',
|
|
230
|
-
'Cache-Control': 'no-store',
|
|
231
|
-
});
|
|
232
|
-
return {
|
|
233
|
-
key,
|
|
234
|
-
url: `${this.clientParams.useSSL ? 'https' : 'http'}://${this.clientParams.endPoint}${this.clientParams.port ? `:${this.clientParams.port}` : ''}/${bucket}/${key}`,
|
|
235
|
-
hash,
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
catch {
|
|
239
|
-
throw error_1.ErrorStorageFileNotUploaded;
|
|
240
|
-
}
|
|
241
|
-
}));
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* This function checks if a bucket exists.
|
|
245
|
-
*
|
|
246
|
-
* @param {string} bucket Bucket name.
|
|
247
|
-
* @returns {Promise<boolean>} Returns `true` if exists, `false` if it doesn't.
|
|
248
|
-
*
|
|
249
|
-
* **Code example**
|
|
250
|
-
*
|
|
251
|
-
* ```ts
|
|
252
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
253
|
-
*
|
|
254
|
-
* const credentials: StorageCredentials = {
|
|
255
|
-
* accessKey: 'ACCESS_KEY',
|
|
256
|
-
* secretKey: 'SECRET_KEY',
|
|
257
|
-
* };
|
|
258
|
-
* const params: StorageParams = {
|
|
259
|
-
* endPoint: 'http://localhost',
|
|
260
|
-
* port: 9000,
|
|
261
|
-
* useSSL: false,
|
|
262
|
-
* region: 'us-east-1'
|
|
263
|
-
* };
|
|
264
|
-
*
|
|
265
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
266
|
-
* const exists = await storageClient.bucketExists('bucket-name');
|
|
267
|
-
* ```
|
|
268
|
-
*/
|
|
269
|
-
async bucketExists(bucket) {
|
|
270
|
-
return this.client.bucketExists(bucket);
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* This function lists all file names contained in the bucket.
|
|
274
|
-
*
|
|
275
|
-
* @param {string} bucket Bucket name.
|
|
276
|
-
* @returns {Promise<string[]>} Returns the list of file names contained in the bucket.
|
|
277
|
-
*
|
|
278
|
-
* **Code example**
|
|
279
|
-
*
|
|
280
|
-
* ```ts
|
|
281
|
-
* import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
|
|
282
|
-
*
|
|
283
|
-
* const credentials: StorageCredentials = {
|
|
284
|
-
* accessKey: 'ACCESS_KEY',
|
|
285
|
-
* secretKey: 'SECRET_KEY',
|
|
286
|
-
* };
|
|
287
|
-
* const params: StorageParams = {
|
|
288
|
-
* endPoint: 'http://localhost',
|
|
289
|
-
* port: 9000,
|
|
290
|
-
* useSSL: false,
|
|
291
|
-
* region: 'us-east-1'
|
|
292
|
-
* };
|
|
293
|
-
*
|
|
294
|
-
* const storageClient = new StorageClient(params, credentials);
|
|
295
|
-
* const fileNames = await storageClient.listObjects('bucket-name');
|
|
296
|
-
* ```
|
|
297
|
-
*/
|
|
298
|
-
async listObjects(bucket) {
|
|
299
|
-
const isBucketExists = await this.client.bucketExists(bucket);
|
|
300
|
-
if (!isBucketExists) {
|
|
301
|
-
throw error_1.ErrorStorageBucketNotFound;
|
|
302
|
-
}
|
|
303
|
-
try {
|
|
304
|
-
return new Promise((resolve, reject) => {
|
|
305
|
-
const keys = [];
|
|
306
|
-
const stream = this.client.listObjectsV2(bucket, '', true, '');
|
|
307
|
-
stream.on('data', (obj) => keys.push(obj.name));
|
|
308
|
-
stream.on('error', reject);
|
|
309
|
-
stream.on('end', () => {
|
|
310
|
-
resolve(keys);
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
catch (e) {
|
|
315
|
-
throw new Error(String(e));
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
exports.StorageClient = StorageClient;
|