serverless-simple-middleware 0.0.60 → 0.0.61

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 (44) hide show
  1. package/.nvmrc +1 -0
  2. package/README.md +0 -1
  3. package/dist/aws/config.d.ts +2 -2
  4. package/dist/aws/config.js +6 -4
  5. package/dist/aws/define.d.ts +0 -12
  6. package/dist/aws/define.js +2 -1
  7. package/dist/aws/index.js +17 -6
  8. package/dist/aws/simple.d.ts +18 -15
  9. package/dist/aws/simple.js +317 -225
  10. package/dist/index.js +17 -6
  11. package/dist/internal/AwsError.d.ts +5 -0
  12. package/dist/internal/AwsError.js +33 -0
  13. package/dist/internal/s3.d.ts +38 -0
  14. package/dist/internal/s3.js +2 -0
  15. package/dist/middleware/aws.d.ts +2 -2
  16. package/dist/middleware/aws.js +12 -9
  17. package/dist/middleware/base.d.ts +8 -8
  18. package/dist/middleware/base.js +9 -8
  19. package/dist/middleware/build.d.ts +1 -1
  20. package/dist/middleware/build.js +68 -45
  21. package/dist/middleware/index.d.ts +2 -2
  22. package/dist/middleware/index.js +20 -8
  23. package/dist/middleware/logger.js +8 -4
  24. package/dist/middleware/mysql.d.ts +3 -3
  25. package/dist/middleware/mysql.js +17 -13
  26. package/dist/middleware/trace.d.ts +2 -2
  27. package/dist/middleware/trace.js +21 -21
  28. package/dist/utils/index.js +16 -5
  29. package/dist/utils/logger.d.ts +3 -3
  30. package/dist/utils/logger.js +9 -7
  31. package/dist/utils/misc.d.ts +1 -1
  32. package/dist/utils/misc.js +10 -3
  33. package/package.json +18 -10
  34. package/src/aws/config.ts +1 -1
  35. package/src/aws/define.ts +0 -19
  36. package/src/aws/simple.ts +306 -204
  37. package/src/internal/AwsError.ts +13 -0
  38. package/src/internal/s3.ts +75 -0
  39. package/src/middleware/aws.ts +1 -1
  40. package/src/middleware/base.ts +5 -4
  41. package/src/middleware/build.ts +31 -31
  42. package/src/middleware/mysql.ts +11 -7
  43. package/src/middleware/trace.ts +15 -19
  44. package/src/utils/misc.ts +11 -2
package/src/aws/simple.ts CHANGED
@@ -1,4 +1,8 @@
1
- import * as AWS from 'aws-sdk'; // tslint:disable-line
1
+ import {
2
+ CloudfrontSignedCookiesOutput,
3
+ getSignedCookies,
4
+ } from '@aws-sdk/cloudfront-signer';
5
+
2
6
  import * as fs from 'fs';
3
7
  import * as os from 'os';
4
8
  import { nanoid } from 'nanoid/non-secure';
@@ -6,22 +10,38 @@ import { nanoid } from 'nanoid/non-secure';
6
10
  import { getLogger, stringifyError } from '../utils';
7
11
  import { SimpleAWSConfig } from './config';
8
12
 
13
+ import { AWSComponent, SQSMessageBody } from './define';
14
+ import { DynamoDB, DynamoDBClient } from '@aws-sdk/client-dynamodb';
9
15
  import {
10
- AWSComponent,
11
- S3SignedUrlParams,
12
- S3SignedUrlResult,
13
- SQSMessageBody,
14
- } from './define';
16
+ AbortMultipartUploadCommand,
17
+ CompleteMultipartUploadCommand,
18
+ CopyObjectCommand,
19
+ CreateMultipartUploadCommand,
20
+ DeleteObjectCommand,
21
+ GetObjectCommand,
22
+ HeadObjectCommand,
23
+ ListObjectsV2Command,
24
+ ListPartsCommand,
25
+ PutObjectCommand,
26
+ S3,
27
+ UploadPartCommand,
28
+ UploadPartCopyCommand,
29
+ } from '@aws-sdk/client-s3';
30
+ import { SQS } from '@aws-sdk/client-sqs';
31
+ import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
32
+ import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
33
+ import { PresignerOptions } from '../internal/s3';
34
+ import { Upload } from '@aws-sdk/lib-storage';
15
35
 
16
36
  const logger = getLogger(__filename);
17
37
 
18
38
  export class SimpleAWS {
19
- private queueUrls: { [queueName: string]: string };
39
+ private queueUrls: { [queueName: string]: string } = {};
20
40
  private config: SimpleAWSConfig;
21
- private lazyS3: AWS.S3 | undefined;
22
- private lazySqs: AWS.SQS | undefined;
23
- private lazyDynamodb: AWS.DynamoDB.DocumentClient | undefined;
24
- private lazyDynamodbAdmin: AWS.DynamoDB | undefined;
41
+ private lazyS3: S3 | undefined;
42
+ private lazySqs: SQS | undefined;
43
+ private lazyDynamodb: DynamoDBDocument | undefined;
44
+ private lazyDynamodbAdmin: DynamoDB | undefined;
25
45
 
26
46
  constructor(config?: SimpleAWSConfig) {
27
47
  this.config = config || new SimpleAWSConfig();
@@ -35,28 +55,37 @@ export class SimpleAWS {
35
55
 
36
56
  get s3() {
37
57
  if (this.lazyS3 === undefined) {
38
- this.lazyS3 = new AWS.S3(this.config.get(AWSComponent.s3));
58
+ this.lazyS3 = new S3(this.config.get(AWSComponent.s3) || {});
39
59
  }
40
60
  return this.lazyS3;
41
61
  }
62
+
42
63
  get sqs() {
43
64
  if (this.lazySqs === undefined) {
44
- this.lazySqs = new AWS.SQS(this.config.get(AWSComponent.sqs));
65
+ this.lazySqs = new SQS(this.config.get(AWSComponent.sqs) || {});
45
66
  }
46
67
  return this.lazySqs;
47
68
  }
69
+
48
70
  get dynamodb() {
49
71
  if (this.lazyDynamodb === undefined) {
50
- this.lazyDynamodb = new AWS.DynamoDB.DocumentClient(
51
- this.config.get(AWSComponent.dynamodb),
72
+ this.lazyDynamodb = DynamoDBDocument.from(
73
+ new DynamoDBClient(this.config.get(AWSComponent.dynamodb) || {}),
74
+ {
75
+ marshallOptions: {
76
+ convertEmptyValues: true,
77
+ removeUndefinedValues: true,
78
+ },
79
+ },
52
80
  );
53
81
  }
54
82
  return this.lazyDynamodb;
55
83
  }
84
+
56
85
  get dynamodbAdmin() {
57
86
  if (this.lazyDynamodbAdmin === undefined) {
58
- this.lazyDynamodbAdmin = new AWS.DynamoDB(
59
- this.config.get(AWSComponent.dynamodb),
87
+ this.lazyDynamodbAdmin = new DynamoDB(
88
+ this.config.get(AWSComponent.dynamodb) || {},
60
89
  );
61
90
  }
62
91
  return this.lazyDynamodbAdmin;
@@ -66,11 +95,9 @@ export class SimpleAWS {
66
95
  if (this.queueUrls[queueName] !== undefined) {
67
96
  return this.queueUrls[queueName];
68
97
  }
69
- const urlResult = await this.sqs
70
- .getQueueUrl({
71
- QueueName: queueName,
72
- })
73
- .promise();
98
+ const urlResult = await this.sqs.getQueueUrl({
99
+ QueueName: queueName,
100
+ });
74
101
  logger.stupid(`urlResult`, urlResult);
75
102
  if (!urlResult.QueueUrl) {
76
103
  throw new Error(`No queue url with name[${queueName}]`);
@@ -82,26 +109,22 @@ export class SimpleAWS {
82
109
  logger.debug(`Send message[${data.key}] to queue.`);
83
110
  logger.stupid(`data`, data);
84
111
  const queueUrl = await this.getQueueUrl(queueName);
85
- const sendResult = await this.sqs
86
- .sendMessage({
87
- QueueUrl: queueUrl,
88
- MessageBody: JSON.stringify(data),
89
- DelaySeconds: 0,
90
- })
91
- .promise();
112
+ const sendResult = await this.sqs.sendMessage({
113
+ QueueUrl: queueUrl,
114
+ MessageBody: JSON.stringify(data),
115
+ DelaySeconds: 0,
116
+ });
92
117
  logger.stupid(`sendResult`, sendResult);
93
118
 
94
- const attrResult = await this.sqs
95
- .getQueueAttributes({
96
- QueueUrl: queueUrl,
97
- AttributeNames: ['ApproximateNumberOfMessages'],
98
- })
99
- .promise();
119
+ const attrResult = await this.sqs.getQueueAttributes({
120
+ QueueUrl: queueUrl,
121
+ AttributeNames: ['ApproximateNumberOfMessages'],
122
+ });
100
123
  logger.stupid(`attrResult`, attrResult);
101
124
  if (!attrResult.Attributes) {
102
125
  return 0;
103
126
  }
104
- return +attrResult.Attributes.ApproximateNumberOfMessages;
127
+ return +(attrResult.Attributes?.ApproximateNumberOfMessages || 0);
105
128
  };
106
129
 
107
130
  public dequeue = async <T>(
@@ -112,14 +135,12 @@ export class SimpleAWS {
112
135
  ): Promise<Array<SQSMessageBody<T>>> => {
113
136
  logger.debug(`Receive message from queue[${queueName}].`);
114
137
  const queueUrl = await this.getQueueUrl(queueName);
115
- const receiveResult = await this.sqs
116
- .receiveMessage({
117
- QueueUrl: queueUrl,
118
- MaxNumberOfMessages: fetchSize,
119
- WaitTimeSeconds: waitSeconds,
120
- VisibilityTimeout: visibilityTimeout,
121
- })
122
- .promise();
138
+ const receiveResult = await this.sqs.receiveMessage({
139
+ QueueUrl: queueUrl,
140
+ MaxNumberOfMessages: fetchSize,
141
+ WaitTimeSeconds: waitSeconds,
142
+ VisibilityTimeout: visibilityTimeout,
143
+ });
123
144
  logger.stupid(`receiveResult`, receiveResult);
124
145
  if (
125
146
  receiveResult.Messages === undefined ||
@@ -172,42 +193,29 @@ export class SimpleAWS {
172
193
  queueName: string,
173
194
  handle: string,
174
195
  seconds: number,
175
- ): Promise<string> =>
176
- new Promise<string>(async (resolve, reject) => {
177
- logger.debug(`Change visibilityTimeout of ${handle} to ${seconds}secs.`);
178
- this.getQueueUrl(queueName)
179
- .then(queueUrl => {
180
- this.sqs.changeMessageVisibility(
181
- {
182
- QueueUrl: queueUrl,
183
- ReceiptHandle: handle,
184
- VisibilityTimeout: seconds,
185
- },
186
- (err, changeResult) => {
187
- if (err) {
188
- reject(err);
189
- } else {
190
- logger.stupid(`changeResult`, changeResult);
191
- resolve(handle);
192
- }
193
- },
194
- );
195
- })
196
- .catch(reject);
196
+ ): Promise<string> => {
197
+ logger.debug(`Change visibilityTimeout of ${handle} to ${seconds}secs.`);
198
+ const queueUrl = await this.getQueueUrl(queueName);
199
+
200
+ await this.sqs.changeMessageVisibility({
201
+ QueueUrl: queueUrl,
202
+ ReceiptHandle: handle,
203
+ VisibilityTimeout: seconds,
197
204
  });
198
205
 
206
+ return handle;
207
+ };
208
+
199
209
  public completeMessage = async (
200
210
  queueName: string,
201
211
  handle: string,
202
212
  ): Promise<string> => {
203
213
  logger.debug(`Complete a message with handle[${handle}]`);
204
214
  const queueUrl = await this.getQueueUrl(queueName);
205
- const deleteResult = await this.sqs
206
- .deleteMessage({
207
- QueueUrl: queueUrl,
208
- ReceiptHandle: handle,
209
- })
210
- .promise();
215
+ const deleteResult = await this.sqs.deleteMessage({
216
+ QueueUrl: queueUrl,
217
+ ReceiptHandle: handle,
218
+ });
211
219
  logger.stupid(`deleteResult`, deleteResult);
212
220
  return handle;
213
221
  };
@@ -224,15 +232,13 @@ export class SimpleAWS {
224
232
  const end = Math.min(start + chunkSize, handles.length);
225
233
  const sublist = handles.slice(start, end);
226
234
  const queueUrl = await this.getQueueUrl(queueName);
227
- const deletesResult = await this.sqs
228
- .deleteMessageBatch({
229
- QueueUrl: queueUrl,
230
- Entries: sublist.map(handle => ({
231
- Id: (++index).toString(),
232
- ReceiptHandle: handle,
233
- })),
234
- })
235
- .promise();
235
+ const deletesResult = await this.sqs.deleteMessageBatch({
236
+ QueueUrl: queueUrl,
237
+ Entries: sublist.map((handle) => ({
238
+ Id: (++index).toString(),
239
+ ReceiptHandle: handle,
240
+ })),
241
+ });
236
242
  logger.stupid(`deleteResult`, deletesResult);
237
243
  }
238
244
  return handles;
@@ -244,15 +250,14 @@ export class SimpleAWS {
244
250
  localPath: string,
245
251
  ): Promise<string> => {
246
252
  logger.debug(`Get a stream of item[${key}] from bucket[${bucket}]`);
247
- const stream = this.s3
248
- .getObject({ Bucket: bucket, Key: key })
249
- .createReadStream();
253
+ const { Body } = await this.s3.getObject({ Bucket: bucket, Key: key });
254
+
250
255
  return new Promise<string>((resolve, reject) =>
251
- stream
252
- .on('error', error => reject(error))
256
+ (Body as NodeJS.ReadableStream)
257
+ .on('error', (error) => reject(error))
253
258
  .pipe(fs.createWriteStream(localPath))
254
259
  .on('finish', () => resolve(localPath))
255
- .on('error', error => reject(error)),
260
+ .on('error', (error) => reject(error)),
256
261
  );
257
262
  };
258
263
 
@@ -283,17 +288,13 @@ export class SimpleAWS {
283
288
  key: string,
284
289
  ): Promise<Buffer> => {
285
290
  logger.debug(`Read item[${key}] from bucket[${bucket}]`);
286
- const params = {
287
- Bucket: bucket,
288
- Key: key,
289
- };
291
+ const { Body } = await this.s3.getObject({ Bucket: bucket, Key: key });
290
292
 
291
- const data = await this.s3.getObject(params).promise();
292
- if (data.Body) {
293
- return data.Body as Buffer;
294
- } else {
293
+ const buffer = await Body?.transformToByteArray();
294
+ if (!buffer) {
295
295
  throw new Error(`Failed to read file ${key} from bucket ${bucket}`);
296
296
  }
297
+ return Buffer.from(buffer);
297
298
  };
298
299
 
299
300
  public upload = async (
@@ -302,14 +303,18 @@ export class SimpleAWS {
302
303
  key: string,
303
304
  ): Promise<string> => {
304
305
  logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
305
- const putResult = await this.s3
306
- .upload({
306
+ const upload = new Upload({
307
+ client: this.s3,
308
+ params: {
307
309
  Bucket: bucket,
308
310
  Key: key,
309
311
  Body: fs.createReadStream(localPath),
310
- })
311
- .promise();
312
- logger.stupid(`putResult`, putResult);
312
+ },
313
+ partSize: 5 * 1024 * 1024, // 5MB
314
+ queueSize: 4,
315
+ });
316
+
317
+ await upload.done();
313
318
  return key;
314
319
  };
315
320
 
@@ -319,14 +324,17 @@ export class SimpleAWS {
319
324
  buffer: Buffer,
320
325
  ): Promise<string> => {
321
326
  logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
322
- const putResult = await this.s3
323
- .upload({
327
+ const upload = new Upload({
328
+ client: this.s3,
329
+ params: {
324
330
  Bucket: bucket,
325
331
  Key: key,
326
332
  Body: buffer,
327
- })
328
- .promise();
329
- logger.stupid(`putResult`, putResult);
333
+ },
334
+ partSize: 5 * 1024 * 1024, // 5MB
335
+ queueSize: 4,
336
+ });
337
+ await upload.done();
330
338
  return key;
331
339
  };
332
340
 
@@ -344,43 +352,161 @@ export class SimpleAWS {
344
352
  if (!fs.existsSync(tempFile)) {
345
353
  return;
346
354
  }
347
- fs.unlink(tempFile, error => {
348
- if (!error) {
349
- return;
350
- }
355
+ try {
356
+ await fs.promises.unlink(tempFile);
357
+ } catch (error) {
351
358
  const msg = `Error during writeFile: unlink file ${tempFile}: ${stringifyError(
352
359
  error,
353
360
  )}`;
354
361
  logger.error(msg);
355
- });
362
+ }
356
363
  }
357
364
  };
358
365
 
359
- public getSignedUrl = (
360
- bucketName: string,
361
- key: string,
362
- operation: 'getObject' | 'putObject' = 'getObject',
363
- params?: S3SignedUrlParams,
364
- ): S3SignedUrlResult => {
365
- return {
366
- key,
367
- url: this.s3.getSignedUrl(operation, {
368
- Bucket: bucketName,
369
- Key: key,
370
- Expires: 60 * 10,
371
- ...(params || {}),
372
- }),
373
- };
374
- };
366
+ public async getSignedUrl(options: PresignerOptions): Promise<string> {
367
+ const { expiresIn = 600, unhoistableHeaders } = options;
368
+ switch (options.operation) {
369
+ case 'putObject': {
370
+ const cmd = new PutObjectCommand({
371
+ Bucket: options.bucket,
372
+ Key: options.key,
373
+ ...options.params,
374
+ });
375
+ return getSignedUrl(this.s3, cmd, {
376
+ expiresIn: expiresIn,
377
+ unhoistableHeaders,
378
+ });
379
+ }
380
+ case 'getObject': {
381
+ const cmd = new GetObjectCommand({
382
+ Bucket: options.bucket,
383
+ Key: options.key,
384
+ ...options.params,
385
+ });
386
+ return getSignedUrl(this.s3, cmd, {
387
+ expiresIn: expiresIn,
388
+ unhoistableHeaders,
389
+ });
390
+ }
391
+ case 'deleteObject': {
392
+ const cmd = new DeleteObjectCommand({
393
+ Bucket: options.bucket,
394
+ Key: options.key,
395
+ ...options.params,
396
+ });
397
+ return getSignedUrl(this.s3, cmd, {
398
+ expiresIn: expiresIn,
399
+ unhoistableHeaders,
400
+ });
401
+ }
402
+ case 'headObject': {
403
+ const cmd = new HeadObjectCommand({
404
+ Bucket: options.bucket,
405
+ Key: options.key,
406
+ ...options.params,
407
+ });
408
+ return getSignedUrl(this.s3, cmd, {
409
+ expiresIn: expiresIn,
410
+ unhoistableHeaders,
411
+ });
412
+ }
413
+ case 'copyObject': {
414
+ const cmd = new CopyObjectCommand({
415
+ Bucket: options.bucket,
416
+ Key: options.key,
417
+ ...options.params,
418
+ });
419
+ return getSignedUrl(this.s3, cmd, {
420
+ expiresIn: expiresIn,
421
+ unhoistableHeaders,
422
+ });
423
+ }
424
+ case 'uploadPart': {
425
+ const cmd = new UploadPartCommand({
426
+ Bucket: options.bucket,
427
+ Key: options.key,
428
+ ...options.params,
429
+ });
430
+ return getSignedUrl(this.s3, cmd, {
431
+ expiresIn: expiresIn,
432
+ unhoistableHeaders,
433
+ });
434
+ }
435
+ case 'uploadPartCopy': {
436
+ const cmd = new UploadPartCopyCommand({
437
+ Bucket: options.bucket,
438
+ Key: options.key,
439
+ ...options.params,
440
+ });
441
+ return getSignedUrl(this.s3, cmd, {
442
+ expiresIn: expiresIn,
443
+ unhoistableHeaders,
444
+ });
445
+ }
446
+ case 'listObjectsV2': {
447
+ const cmd = new ListObjectsV2Command({
448
+ Bucket: options.bucket,
449
+ ...options.params,
450
+ });
451
+ return getSignedUrl(this.s3, cmd, {
452
+ expiresIn: expiresIn,
453
+ unhoistableHeaders,
454
+ });
455
+ }
456
+ case 'createMultipartUpload': {
457
+ const cmd = new CreateMultipartUploadCommand({
458
+ Bucket: options.bucket,
459
+ Key: options.key,
460
+ ...options.params,
461
+ });
462
+ return getSignedUrl(this.s3, cmd, {
463
+ expiresIn: expiresIn,
464
+ unhoistableHeaders,
465
+ });
466
+ }
467
+ case 'completeMultipartUpload': {
468
+ const cmd = new CompleteMultipartUploadCommand({
469
+ Bucket: options.bucket,
470
+ Key: options.key,
471
+ ...options.params,
472
+ });
473
+ return getSignedUrl(this.s3, cmd, {
474
+ expiresIn: expiresIn,
475
+ unhoistableHeaders,
476
+ });
477
+ }
478
+ case 'abortMultipartUpload': {
479
+ const cmd = new AbortMultipartUploadCommand({
480
+ Bucket: options.bucket,
481
+ Key: options.key,
482
+ ...options.params,
483
+ });
484
+ return getSignedUrl(this.s3, cmd, {
485
+ expiresIn: expiresIn,
486
+ unhoistableHeaders,
487
+ });
488
+ }
489
+ case 'listParts': {
490
+ const cmd = new ListPartsCommand({
491
+ Bucket: options.bucket,
492
+ Key: options.key,
493
+ ...options.params,
494
+ });
495
+ return getSignedUrl(this.s3, cmd, {
496
+ expiresIn: expiresIn,
497
+ unhoistableHeaders,
498
+ });
499
+ }
500
+ }
501
+ }
375
502
 
376
503
  public getSignedCookie = (
377
504
  keyPairId: string,
378
505
  privateKey: string,
379
506
  url: string,
380
507
  expires: number,
381
- ): AWS.CloudFront.Signer.CustomPolicy => {
382
- const signer = new AWS.CloudFront.Signer(keyPairId, privateKey);
383
- const policy = {
508
+ ): CloudfrontSignedCookiesOutput => {
509
+ const policy = JSON.stringify({
384
510
  Statement: [
385
511
  {
386
512
  Resource: url,
@@ -389,20 +515,12 @@ export class SimpleAWS {
389
515
  },
390
516
  },
391
517
  ],
392
- };
393
- const ret = signer.getSignedCookie({ policy: JSON.stringify(policy) });
394
- return ret;
395
- };
518
+ });
396
519
 
397
- public getAttachmentUrl = (
398
- bucketName: string,
399
- key: string,
400
- fileName: string,
401
- params?: S3SignedUrlParams,
402
- ): S3SignedUrlResult => {
403
- return this.getSignedUrl(bucketName, key, 'getObject', {
404
- ...params,
405
- ResponseContentDisposition: `attachment; filename="${fileName}"`,
520
+ return getSignedCookies({
521
+ keyPairId,
522
+ privateKey,
523
+ policy,
406
524
  });
407
525
  };
408
526
 
@@ -414,16 +532,14 @@ export class SimpleAWS {
414
532
  logger.debug(
415
533
  `Read an item with key[${JSON.stringify(key)}] from ${tableName}.`,
416
534
  );
417
- const getResult = await this.dynamodb
418
- .get({
419
- TableName: tableName,
420
- Key: key,
421
- })
422
- .promise();
535
+ const getResult = await this.dynamodb.get({
536
+ TableName: tableName,
537
+ Key: key,
538
+ });
423
539
  logger.stupid(`getResult`, getResult);
424
540
  const item: T | undefined =
425
541
  getResult !== undefined && getResult.Item !== undefined
426
- ? ((getResult.Item as any) as T) // Casts forcefully.
542
+ ? (getResult.Item as any as T) // Casts forcefully.
427
543
  : defaultValue;
428
544
  logger.stupid(`item`, item);
429
545
  return item;
@@ -439,21 +555,19 @@ export class SimpleAWS {
439
555
  );
440
556
  logger.stupid(`keyValues`, columnValues);
441
557
  const expressions = Object.keys(columnValues)
442
- .map(column => `${column} = :${column}`)
558
+ .map((column) => `${column} = :${column}`)
443
559
  .join(', ');
444
560
  const attributeValues = Object.keys(columnValues)
445
- .map(column => [`:${column}`, columnValues[column]])
561
+ .map((column) => [`:${column}`, columnValues[column]])
446
562
  .reduce((obj, pair) => ({ ...obj, [pair[0]]: pair[1] }), {});
447
563
  logger.stupid(`expressions`, expressions);
448
564
  logger.stupid(`attributeValues`, attributeValues);
449
- const updateResult = await this.dynamodb
450
- .update({
451
- TableName: tableName,
452
- Key: key,
453
- UpdateExpression: `set ${expressions}`,
454
- ExpressionAttributeValues: attributeValues,
455
- })
456
- .promise();
565
+ const updateResult = await this.dynamodb.update({
566
+ TableName: tableName,
567
+ Key: key,
568
+ UpdateExpression: `set ${expressions}`,
569
+ ExpressionAttributeValues: attributeValues,
570
+ });
457
571
  logger.stupid(`updateResult`, updateResult);
458
572
  return updateResult;
459
573
  };
@@ -462,11 +576,9 @@ export class SimpleAWS {
462
576
 
463
577
  public setupQueue = async (queueName: string) => {
464
578
  try {
465
- const listResult = await this.sqs
466
- .listQueues({
467
- QueueNamePrefix: queueName,
468
- })
469
- .promise();
579
+ const listResult = await this.sqs.listQueues({
580
+ QueueNamePrefix: queueName,
581
+ });
470
582
  if (listResult.QueueUrls) {
471
583
  for (const queueUrl of listResult.QueueUrls) {
472
584
  if (queueUrl.endsWith(queueName)) {
@@ -479,11 +591,9 @@ export class SimpleAWS {
479
591
  logger.debug(`No Queue[${queueName}] exists due to ${error}`);
480
592
  }
481
593
  logger.debug(`Create a queue[${queueName}] newly.`);
482
- const createResult = await this.sqs
483
- .createQueue({
484
- QueueName: queueName,
485
- })
486
- .promise();
594
+ const createResult = await this.sqs.createQueue({
595
+ QueueName: queueName,
596
+ });
487
597
  logger.stupid(`createResult`, createResult);
488
598
  return true;
489
599
  };
@@ -496,10 +606,10 @@ export class SimpleAWS {
496
606
  },
497
607
  ) => {
498
608
  try {
499
- const listResult = await this.s3.listBuckets().promise();
609
+ const listResult = await this.s3.listBuckets();
500
610
  if (
501
611
  listResult.Buckets &&
502
- listResult.Buckets.map(each => each.Name).includes(bucketName)
612
+ listResult.Buckets.map((each) => each.Name).includes(bucketName)
503
613
  ) {
504
614
  logger.debug(`Bucket[${bucketName}] already exists.`);
505
615
  return true;
@@ -508,27 +618,23 @@ export class SimpleAWS {
508
618
  logger.debug(`No bucket[${bucketName}] exists due to ${error}`);
509
619
  }
510
620
  logger.debug(`Create a bucket[${bucketName}] newly.`);
511
- const createResult = await this.s3
512
- .createBucket({
513
- Bucket: bucketName,
514
- })
515
- .promise();
621
+ const createResult = await this.s3.createBucket({
622
+ Bucket: bucketName,
623
+ });
516
624
  logger.stupid(`createResult`, createResult);
517
625
  if (cors) {
518
- const corsResult = await this.s3
519
- .putBucketCors({
520
- Bucket: bucketName,
521
- CORSConfiguration: {
522
- CORSRules: [
523
- {
524
- AllowedHeaders: ['*'],
525
- AllowedMethods: cors.methods,
526
- AllowedOrigins: cors.origins,
527
- },
528
- ],
529
- },
530
- })
531
- .promise();
626
+ const corsResult = await this.s3.putBucketCors({
627
+ Bucket: bucketName,
628
+ CORSConfiguration: {
629
+ CORSRules: [
630
+ {
631
+ AllowedHeaders: ['*'],
632
+ AllowedMethods: cors.methods,
633
+ AllowedOrigins: cors.origins,
634
+ },
635
+ ],
636
+ },
637
+ });
532
638
  logger.stupid(`corsResult`, corsResult);
533
639
  }
534
640
  return true;
@@ -536,7 +642,7 @@ export class SimpleAWS {
536
642
 
537
643
  public setupDynamoDb = async (tableName: string, keyColumn: string) => {
538
644
  try {
539
- const listResult = await this.dynamodbAdmin.listTables().promise();
645
+ const listResult = await this.dynamodbAdmin.listTables();
540
646
  if (listResult.TableNames && listResult.TableNames.includes(tableName)) {
541
647
  logger.debug(`Table[${tableName}] already exists.`);
542
648
  return true;
@@ -545,19 +651,15 @@ export class SimpleAWS {
545
651
  logger.debug(`No table[${tableName}] exists due to ${error}`);
546
652
  }
547
653
  logger.debug(`Create a table[${tableName}] newly.`);
548
- const createResult = await this.dynamodbAdmin
549
- .createTable({
550
- TableName: tableName,
551
- KeySchema: [{ AttributeName: keyColumn, KeyType: 'HASH' }],
552
- AttributeDefinitions: [
553
- { AttributeName: keyColumn, AttributeType: 'S' },
554
- ],
555
- ProvisionedThroughput: {
556
- ReadCapacityUnits: 30,
557
- WriteCapacityUnits: 10,
558
- },
559
- })
560
- .promise();
654
+ const createResult = await this.dynamodbAdmin.createTable({
655
+ TableName: tableName,
656
+ KeySchema: [{ AttributeName: keyColumn, KeyType: 'HASH' }],
657
+ AttributeDefinitions: [{ AttributeName: keyColumn, AttributeType: 'S' }],
658
+ ProvisionedThroughput: {
659
+ ReadCapacityUnits: 30,
660
+ WriteCapacityUnits: 10,
661
+ },
662
+ });
561
663
  logger.stupid(`createResult`, createResult);
562
664
  return true;
563
665
  };