aws-v3-sdk 1.0.5 → 1.0.7

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/clients/all.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ Lambda: require('./lambda'),
3
+ DynamoDB: require('./dynamoDB'),
4
+ EnvironmentCredentials: require('./credential'),
5
+ S3: require('./s3'),
6
+ STS: require('./sts'),
7
+ CognitoIdentityServiceProvider: require('./cognito')
8
+ }
@@ -0,0 +1,18 @@
1
+ const { CognitoIdentityProviderClient, ListUsersCommand } = require('@aws-sdk/client-cognito-identity-provider')
2
+
3
+ class CognitoIdentityServiceProvider {
4
+ constructor (region) {
5
+ this.client = new CognitoIdentityProviderClient({ region }) // Replace with your desired region
6
+ }
7
+
8
+ async listUsers (params, callback) {
9
+ const command = new ListUsersCommand(params)
10
+ this.client.send(command).then(data => {
11
+ callback(null, data)
12
+ }).catch(err => {
13
+ callback(err)
14
+ })
15
+ }
16
+ }
17
+
18
+ module.exports = CognitoIdentityServiceProvider
@@ -0,0 +1,118 @@
1
+ const { DynamoDB, DynamoDBClient } = require('@aws-sdk/client-dynamodb')
2
+ const { DynamoDBDocument, DynamoDBDocumentClient, GetCommand, BatchWriteCommand, PutCommand, QueryCommand, ScanCommand, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
3
+ const ddb = new DynamoDBClient()
4
+ const documentClient = DynamoDBDocumentClient.from(ddb)
5
+
6
+ class DocumentClient {
7
+ get (params, callback) {
8
+ const getCommand = new GetCommand(params)
9
+ documentClient.send(getCommand, (err, response) => {
10
+ if (err || !response) {
11
+ return callback(err)
12
+ }
13
+ if (response.Item) {
14
+ return callback(null, response)
15
+ } else if (!response.Item) {
16
+ return callback(null, {})
17
+ }
18
+ })
19
+ }
20
+
21
+ query (params, callback) {
22
+ const getCommand = new QueryCommand(params)
23
+ documentClient.send(getCommand, (err, response) => {
24
+ if (err || !response) {
25
+ return callback(err)
26
+ }
27
+ return callback(null, response)
28
+ })
29
+ }
30
+
31
+ scan (params, callback) {
32
+ const getCommand = new ScanCommand(params)
33
+ documentClient.send(getCommand, (err, response) => {
34
+ if (err || !response) {
35
+ return callback(err)
36
+ }
37
+ if (response.Items) {
38
+ return callback(null, response)
39
+ } else if (!response.Items) {
40
+ return callback(null)
41
+ }
42
+ })
43
+ }
44
+
45
+ put (params, callback) {
46
+ let item = params.Item
47
+ try {
48
+ item = Object.fromEntries(Object.entries(item).filter(([key, value]) => value !== undefined))
49
+ } catch (error) {
50
+
51
+ }
52
+ params.Item = item
53
+ const putCommand = new PutCommand(params)
54
+ documentClient.send(putCommand, (err, response) => {
55
+ if (err || !response) {
56
+ return callback(err)
57
+ }
58
+ return callback(null, response)
59
+ })
60
+ }
61
+
62
+ delete (params, callback) {
63
+ const docClient = DynamoDBDocument.from(new DynamoDB())
64
+ docClient.delete(params, (err, response) => {
65
+ if (err) {
66
+ return callback(err)
67
+ } else {
68
+ return callback(null, response)
69
+ }
70
+ })
71
+ }
72
+
73
+ deletev3 (params, callback) {
74
+ const putCommand = new DeleteCommand(params)
75
+ documentClient.send(putCommand, (err, response) => {
76
+ if (err) {
77
+ return callback(err)
78
+ }
79
+ return callback(null, response)
80
+ })
81
+ }
82
+
83
+ update (params, callback) {
84
+ const docClient = DynamoDBDocument.from(new DynamoDB())
85
+ docClient.update(params, (err, response) => {
86
+ if (err) {
87
+ return callback(err)
88
+ } else {
89
+ return callback(null, response)
90
+ }
91
+ })
92
+ }
93
+
94
+ batchWrite (params, callback) {
95
+ const command = new BatchWriteCommand(params)
96
+ documentClient.send(command, (err, response) => {
97
+ if (err || !response) {
98
+ return callback(err)
99
+ }
100
+ if (response) {
101
+ return callback(null, response)
102
+ }
103
+ })
104
+ }
105
+
106
+ batchGet (params, callback) {
107
+ const docClient = DynamoDBDocument.from(new DynamoDB())
108
+
109
+ docClient.batchGet(params, function (err, data) {
110
+ if (err) callback(err)
111
+ else {
112
+ callback(null, data)
113
+ }
114
+ })
115
+ }
116
+ }
117
+
118
+ module.exports = DocumentClient
@@ -1,4 +1,3 @@
1
-
2
1
  const DocumentClient = require('./dynamo/DocumentClient')
3
2
 
4
3
  class DynamoDB {
@@ -2,10 +2,22 @@ const { LambdaClient, InvokeCommand, InvokeAsyncCommand } = require('@aws-sdk/cl
2
2
  const { toUtf8, fromUtf8 } = require('@aws-sdk/util-utf8-node')
3
3
 
4
4
  class Lambda {
5
+ constructor (region, session) {
6
+ if (region && session) {
7
+ this.client = new LambdaClient({ region, credentials: session })
8
+ } else {
9
+ this.client = new LambdaClient()
10
+ }
11
+ }
12
+
5
13
  async invoke (params, callback) {
6
- const client = new LambdaClient()
14
+ if (params.Payload) {
15
+ params.InvokeArgs = fromUtf8(params.Payload)
16
+ delete params.Payload
17
+ }
18
+
7
19
  const command = new InvokeCommand(params)
8
- client.send(command).then(response => {
20
+ this.client.send(command).then(response => {
9
21
  response.Payload = toUtf8(response.Payload)
10
22
  callback(null, response)
11
23
  }).catch(err => {
package/clients/s3.js ADDED
@@ -0,0 +1,135 @@
1
+ const { S3Client, GetObjectCommand, PutObjectCommand, ListObjectsCommand, DeleteObjectsCommand, CreateMultipartUploadCommand, CompleteMultipartUploadCommand } = require('@aws-sdk/client-s3')
2
+ const { getSignedUrl } = require('@aws-sdk/s3-request-presigner')
3
+ const client = new S3Client({})
4
+
5
+ class S3 {
6
+ getSignedUrl (type, params, callback) {
7
+ return new Promise((resolve, reject) => {
8
+ if (type === 'getObject') {
9
+ const Expires = params.Expires
10
+ delete params.Expires
11
+ const command = new GetObjectCommand(params)
12
+ getSignedUrl(client, command, { expiresIn: Expires }).then(url => {
13
+ resolve(url)
14
+ }).catch(err => {
15
+ resolve(err)
16
+ })
17
+ } else if (type === 'putObject') {
18
+ const Expires = params.Expires
19
+ delete params.Expires
20
+ const command = new PutObjectCommand(params)
21
+ getSignedUrl(client, command, { expiresIn: Expires }).then(url => {
22
+ resolve(url)
23
+ }).catch(err => {
24
+ resolve(err)
25
+ })
26
+ }
27
+ }).then(result => {
28
+ if (callback && typeof callback === 'function') {
29
+ callback(null, result)
30
+ }
31
+ return result
32
+ }).catch(error => {
33
+ if (callback && typeof callback === 'function') {
34
+ callback(error, null)
35
+ }
36
+ return Promise.reject(error)
37
+ })
38
+ }
39
+
40
+ createMultipartUpload (type, params, callback) {
41
+ return new Promise((resolve, reject) => {
42
+ const command = new CreateMultipartUploadCommand(params)
43
+ client.send(command).then(response => {
44
+ resolve(response)
45
+ }).catch(err => {
46
+ resolve(err)
47
+ })
48
+ }).then(result => {
49
+ if (callback && typeof callback === 'function') {
50
+ callback(null, result)
51
+ }
52
+ return result
53
+ }).catch(error => {
54
+ if (callback && typeof callback === 'function') {
55
+ callback(error, null)
56
+ }
57
+ return Promise.reject(error)
58
+ })
59
+ }
60
+
61
+ completeMultipartUpload (type, params, callback) {
62
+ return new Promise((resolve, reject) => {
63
+ const command = new CompleteMultipartUploadCommand(params)
64
+ client.send(command).then(response => {
65
+ resolve(response)
66
+ }).catch(err => {
67
+ resolve(err)
68
+ })
69
+ }).then(result => {
70
+ if (callback && typeof callback === 'function') {
71
+ callback(null, result)
72
+ }
73
+ return result
74
+ }).catch(error => {
75
+ if (callback && typeof callback === 'function') {
76
+ callback(error, null)
77
+ }
78
+ return Promise.reject(error)
79
+ })
80
+ }
81
+
82
+ getObject (params, callback) {
83
+ return new Promise((resolve, reject) => {
84
+ const getObject = new GetObjectCommand(params)
85
+ client.send(getObject).then(response => {
86
+ const chunks = []
87
+ const stream = response.Body
88
+ stream.on('data', chunk => chunks.push(chunk))
89
+ stream.on('end', () => {
90
+ const imageBuffer = Buffer.concat(chunks)
91
+ response.Body = imageBuffer
92
+ resolve(response)
93
+ })
94
+ }).catch(err => {
95
+ resolve(err)
96
+ })
97
+ }).then(result => {
98
+ if (callback && typeof callback === 'function') {
99
+ callback(null, result)
100
+ }
101
+ return result
102
+ }).catch(error => {
103
+ if (callback && typeof callback === 'function') {
104
+ callback(error, null)
105
+ }
106
+ return Promise.reject(error)
107
+ })
108
+ }
109
+
110
+ putObject (params, callback) {
111
+ return new Promise((resolve, reject) => {
112
+ const putObject = new PutObjectCommand(params)
113
+ client.send(putObject, (err, data) => {
114
+ if (err || !data) {
115
+ resolve(err)
116
+ }
117
+ if (data) {
118
+ resolve(data)
119
+ }
120
+ })
121
+ }).then(result => {
122
+ if (callback && typeof callback === 'function') {
123
+ callback(null, result)
124
+ }
125
+ return result
126
+ }).catch(error => {
127
+ if (callback && typeof callback === 'function') {
128
+ callback(error, null)
129
+ }
130
+ return Promise.reject(error)
131
+ })
132
+ }
133
+ }
134
+
135
+ module.exports = S3
package/clients/sts.js ADDED
@@ -0,0 +1,19 @@
1
+ const { STSClient, AssumeRoleCommand } = require('@aws-sdk/client-sts')
2
+
3
+ class STS {
4
+ constructor (AWS_REGION) {
5
+ this.client = new STSClient({ region: AWS_REGION })
6
+ }
7
+
8
+ assumeRole (params, callback) {
9
+ console.log('params', params)
10
+ const command = new AssumeRoleCommand(params)
11
+ this.client.send(command).then(role => {
12
+ return callback(null, role)
13
+ }).catch(err => {
14
+ return callback(err)
15
+ })
16
+ }
17
+ }
18
+
19
+ module.exports = STS
@@ -0,0 +1,53 @@
1
+ const { Client, Connection } = require('@opensearch-project/opensearch')
2
+ const { defaultProvider } = require('@aws-sdk/credential-provider-node')
3
+ const aws4 = require('aws4')
4
+
5
+ const { AWS_REGION, ELASTICSEARCH_AUDITS_ENDPOINT } = process.env
6
+
7
+ const host = 'https://' + ELASTICSEARCH_AUDITS_ENDPOINT
8
+
9
+ const createAwsConnector = (credentials, region) => {
10
+ class AmazonConnection extends Connection {
11
+ buildRequestObject (params) {
12
+ const request = super.buildRequestObject(params)
13
+ request.service = 'es'
14
+ request.region = region
15
+ request.headers = request.headers || {}
16
+ request.headers.host = request.hostname
17
+ return aws4.sign(request, credentials)
18
+ }
19
+ }
20
+ return {
21
+ Connection: AmazonConnection
22
+ }
23
+ }
24
+
25
+ const getClient = async () => {
26
+ const credentials = await defaultProvider()()
27
+ const cred = createAwsConnector(credentials, AWS_REGION)
28
+ return new Client({
29
+ ...cred,
30
+ node: host
31
+ })
32
+ }
33
+
34
+ exports.sendAuditsReqToES = async (method, index, type, id, data) => {
35
+ const client = await getClient()
36
+ if (method === 'DELETE') {
37
+ const response = await client.delete({
38
+ index,
39
+ type,
40
+ id
41
+ })
42
+ return response.body
43
+ } else {
44
+ const response = await client.index({
45
+ id,
46
+ index,
47
+ body: data,
48
+ refresh: true
49
+
50
+ })
51
+ return response.body
52
+ }
53
+ }
@@ -0,0 +1,53 @@
1
+ const { Client, Connection } = require('@opensearch-project/opensearch')
2
+ const { defaultProvider } = require('@aws-sdk/credential-provider-node')
3
+ const aws4 = require('aws4')
4
+
5
+ const { AWS_REGION, ELASTICSEARCH_ENDPOINT } = process.env
6
+
7
+ const host = 'https://' + ELASTICSEARCH_ENDPOINT
8
+
9
+ const createAwsConnector = (credentials, region) => {
10
+ class AmazonConnection extends Connection {
11
+ buildRequestObject (params) {
12
+ const request = super.buildRequestObject(params)
13
+ request.service = 'es'
14
+ request.region = region
15
+ request.headers = request.headers || {}
16
+ request.headers.host = request.hostname
17
+ return aws4.sign(request, credentials)
18
+ }
19
+ }
20
+ return {
21
+ Connection: AmazonConnection
22
+ }
23
+ }
24
+
25
+ const getClient = async () => {
26
+ const credentials = await defaultProvider()()
27
+ const cred = createAwsConnector(credentials, AWS_REGION)
28
+ return new Client({
29
+ ...cred,
30
+ node: host
31
+ })
32
+ }
33
+
34
+ exports.sendReqToES = async (method, index, type, id, data) => {
35
+ const client = await getClient()
36
+ if (method === 'DELETE') {
37
+ const response = await client.delete({
38
+ index,
39
+ type,
40
+ id
41
+ })
42
+ return response.body
43
+ } else {
44
+ const response = await client.index({
45
+ id,
46
+ index,
47
+ body: data,
48
+ refresh: true
49
+
50
+ })
51
+ return response.body
52
+ }
53
+ }
@@ -0,0 +1,9 @@
1
+ const { sendGetRequest } = require('./sendGetRequest')
2
+ const { sendAuditsReqToES } = require('./es-audits-wrapper')
3
+ const { sendReqToES } = require('./es-wrapper')
4
+
5
+ module.exports = {
6
+ sendGetRequest,
7
+ sendReqToES,
8
+ sendAuditsReqToES
9
+ }
@@ -0,0 +1,50 @@
1
+ const { Client, Connection } = require('@opensearch-project/opensearch')
2
+ const { defaultProvider } = require('@aws-sdk/credential-provider-node')
3
+ const aws4 = require('aws4')
4
+
5
+ const { AWS_REGION, ELASTICSEARCH_ENDPOINT } = process.env
6
+ const host = 'https://' + ELASTICSEARCH_ENDPOINT
7
+
8
+ const createAwsConnector = (credentials, region) => {
9
+ class AmazonConnection extends Connection {
10
+ buildRequestObject (params) {
11
+ const request = super.buildRequestObject(params)
12
+ request.service = 'es'
13
+ request.region = region
14
+ request.headers = request.headers || {}
15
+ request.headers.host = request.hostname
16
+ return aws4.sign(request, credentials)
17
+ }
18
+ }
19
+ return {
20
+ Connection: AmazonConnection
21
+ }
22
+ }
23
+
24
+ const getClient = async () => {
25
+ const credentials = await defaultProvider()()
26
+ const cred = createAwsConnector(credentials, AWS_REGION)
27
+ return new Client({
28
+ ...cred,
29
+ node: host
30
+ })
31
+ }
32
+
33
+ exports.sendGetRequest = async (index, query, from, size, sort) => {
34
+ const client = await getClient()
35
+ return new Promise((resolve, reject) => {
36
+ client.search({
37
+ index,
38
+ body: { query }
39
+ }).then(response => {
40
+ const data = response.body.hits.hits
41
+ const totalCount = response.body.hits.total
42
+ const mappedData = data.map(item => {
43
+ return item._source
44
+ })
45
+ resolve({ items: mappedData, data: mappedData, totalCount })
46
+ }).catch(e => {
47
+ resolve({ items: [], data: [], totalCount: 0 })
48
+ })
49
+ })
50
+ }
package/lib/aws.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Load all service classes
2
- const AWS = require('./clients/all')
2
+ const AWS = require('../clients/all')
3
3
 
4
4
  /**
5
5
  * @api private
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aws-v3-sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "AWS v3 SDK Utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,5 +11,8 @@
11
11
  "url": "git@tomymathew:tomymathew/aws-v3-sdk.git"
12
12
  },
13
13
  "author": "tomy mathew",
14
- "license": "ISC"
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "@opensearch-project/opensearch": "^2.3.1"
17
+ }
15
18
  }
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- Lambda: require('./lambda'),
3
- DynamoDB: require('./dynamoDB'),
4
- EnvironmentCredentials: require('./credential')
5
- }
@@ -1,23 +0,0 @@
1
- const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
2
- const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
3
- const ddb = new DynamoDBClient()
4
- const documentClient = DynamoDBDocumentClient.from(ddb)
5
-
6
- class DocumentClient {
7
- get (params, callback) {
8
- const getCommand = new GetCommand(params)
9
- documentClient.send(getCommand, (err, response) => {
10
- console.log(response)
11
- if (err || !response) {
12
- return callback(err)
13
- }
14
- if (response.Item) {
15
- return callback(null, response)
16
- } else if (!response.Item) {
17
- return callback(response)
18
- }
19
- })
20
- }
21
- }
22
-
23
- module.exports = DocumentClient
File without changes