@human-protocol/sdk 0.0.10

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/src/logger.ts ADDED
@@ -0,0 +1,29 @@
1
+ import winston from 'winston';
2
+
3
+ /**
4
+ * Winston logger instance
5
+ */
6
+ export const createLogger = (level: string): winston.Logger => {
7
+ const logger = winston.createLogger({
8
+ level,
9
+ format: winston.format.json(),
10
+ defaultMeta: { service: 'human-protocol' },
11
+ transports: [
12
+ new winston.transports.File({
13
+ filename: 'logs/error.log',
14
+ level: 'error',
15
+ }),
16
+ new winston.transports.File({ filename: 'logs/all.log' }),
17
+ ],
18
+ });
19
+
20
+ if (process.env.NODE_ENV !== 'production') {
21
+ logger.add(
22
+ new winston.transports.Console({
23
+ format: winston.format.simple(),
24
+ })
25
+ );
26
+ }
27
+
28
+ return logger;
29
+ };
package/src/storage.ts ADDED
@@ -0,0 +1,135 @@
1
+ import AWS from 'aws-sdk';
2
+ import crypto from 'crypto';
3
+
4
+ import { UploadResult, StorageAccessData, Result } from './types';
5
+
6
+ /**
7
+ * **Get S3 instance**
8
+ *
9
+ * @param {StorageAccessData} storageAccessData - Cloud storage access data
10
+ * @returns {AWS.S3} - AWS S3 instance
11
+ */
12
+ const getS3Instance = (storageAccessData: StorageAccessData): AWS.S3 => {
13
+ AWS.config.update({
14
+ accessKeyId: storageAccessData.accessKeyId,
15
+ secretAccessKey: storageAccessData.secretAccessKey,
16
+ });
17
+
18
+ const s3 = new AWS.S3({
19
+ endpoint: storageAccessData.endpoint,
20
+ region: storageAccessData.region,
21
+ });
22
+
23
+ return s3;
24
+ };
25
+
26
+ /**
27
+ * **Get bucket name**
28
+ *
29
+ * @param {StorageAccessData} storageAccessData - Cloud storage access data
30
+ * @param {boolean} isPublic - Whether to return public bucket, or private bucket.
31
+ * @returns {string} - Bucket name
32
+ */
33
+ const getBucket = (
34
+ storageAccessData: StorageAccessData,
35
+ isPublic: boolean
36
+ ): string => {
37
+ return isPublic ? storageAccessData.publicBucket : storageAccessData.bucket;
38
+ };
39
+
40
+ /**
41
+ * **Get public URL of the object**
42
+ *
43
+ * @param {StorageAccessData} storageAccessData - Cloud storage access data
44
+ * @param {string} key - Key of the object
45
+ * @returns {string} - The public URL of the object
46
+ */
47
+ export const getPublicURL = (
48
+ storageAccessData: StorageAccessData,
49
+ key: string
50
+ ): string => {
51
+ return `https://${storageAccessData.publicBucket}.s3.amazonaws.com/${key}`;
52
+ };
53
+
54
+ /**
55
+ * **Parse object key from URL**
56
+ *
57
+ * @param {string} url - URL to parse
58
+ * @returns {string} - The key of the object
59
+ */
60
+ export const getKeyFromURL = (url: string): string => {
61
+ if (url.startsWith('https')) {
62
+ // URL is fully qualified URL. Let's split it and try to retrieve key from last part of it.
63
+ const keyParts = url.split('/');
64
+ const key = keyParts[keyParts.length - 1];
65
+
66
+ return key;
67
+ }
68
+
69
+ // If not fully qualified http URL, the key is the URL
70
+ return url;
71
+ };
72
+
73
+ /**
74
+ * **Download result from cloud storage**
75
+ *
76
+ * @param {StorageAccessData} storageAccessData - Cloud storage access data
77
+ * @param {string} key - Key of result object
78
+ * @param {string} privateKey - Private key to decode encrypted content
79
+ * @param {boolean} isPublic - Whether the objest is using public bucket, or private bucket
80
+ * @returns {Promise<Result>} - Downloaded result
81
+ */
82
+ export const download = async (
83
+ storageAccessData: StorageAccessData,
84
+ key: string,
85
+ privateKey: string,
86
+ isPublic = false
87
+ ): Promise<Result> => {
88
+ const s3 = getS3Instance(storageAccessData);
89
+ const params = {
90
+ Bucket: getBucket(storageAccessData, isPublic),
91
+ Key: key,
92
+ };
93
+
94
+ const { Body } = await s3.getObject(params).promise();
95
+
96
+ const data = JSON.parse(Body?.toString('utf-8') || '');
97
+
98
+ return data;
99
+ };
100
+
101
+ /**
102
+ * **Upload result to cloud storage**
103
+ *
104
+ * @param {StorageAccessData} storageAccessData - Cloud storage access data
105
+ * @param {Result} result - Result to upload
106
+ * @param {string} publicKey - Public key to encrypt data if necessary
107
+ * @param {boolean} _encrypt - Whether to encrypt the result, or not
108
+ * @param {boolean} isPublic - Whether to use public bucket, or private bucket
109
+ * @returns {Promise<UploadResult>} - Uploaded result with key/hash
110
+ */
111
+ export const upload = async (
112
+ storageAccessData: StorageAccessData,
113
+ result: Result,
114
+ publicKey: string,
115
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
+ encrypt = true,
117
+ isPublic = false
118
+ ): Promise<UploadResult> => {
119
+ const s3 = getS3Instance(storageAccessData);
120
+
121
+ const content = JSON.stringify(result);
122
+
123
+ const hash = crypto.createHash('sha1').update(content).digest('hex');
124
+ const key = `s3${hash}`;
125
+
126
+ const params = {
127
+ Bucket: getBucket(storageAccessData, isPublic),
128
+ Key: key,
129
+ Body: content,
130
+ };
131
+
132
+ await s3.putObject(params).promise();
133
+
134
+ return { key, hash };
135
+ };