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