serverless-simple-middleware 0.0.61 → 0.0.62

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.
@@ -2,7 +2,7 @@ import { CloudfrontSignedCookiesOutput } from '@aws-sdk/cloudfront-signer';
2
2
  import { SimpleAWSConfig } from './config';
3
3
  import { SQSMessageBody } from './define';
4
4
  import { DynamoDB } from '@aws-sdk/client-dynamodb';
5
- import { S3 } from '@aws-sdk/client-s3';
5
+ import { S3, Tag } from '@aws-sdk/client-s3';
6
6
  import { SQS } from '@aws-sdk/client-sqs';
7
7
  import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
8
8
  import { PresignerOptions } from '../internal/s3';
@@ -13,6 +13,8 @@ export declare class SimpleAWS {
13
13
  private lazySqs;
14
14
  private lazyDynamodb;
15
15
  private lazyDynamodbAdmin;
16
+ private static readonly stageTag;
17
+ private static readonly stringifiedStageTag;
16
18
  constructor(config?: SimpleAWSConfig);
17
19
  get s3(): S3;
18
20
  get sqs(): SQS;
@@ -28,8 +30,8 @@ export declare class SimpleAWS {
28
30
  download: (bucket: string, key: string, localPath: string) => Promise<string>;
29
31
  readFile: (bucket: string, key: string) => Promise<string>;
30
32
  readFileBuffer: (bucket: string, key: string) => Promise<Buffer>;
31
- upload: (bucket: string, localPath: string, key: string) => Promise<string>;
32
- uploadFromBuffer: (bucket: string, key: string, buffer: Buffer) => Promise<string>;
33
+ upload: (bucket: string, localPath: string, key: string, tags?: Tag[]) => Promise<string>;
34
+ uploadFromBuffer: (bucket: string, key: string, buffer: Buffer, tags?: Tag[]) => Promise<string>;
33
35
  writeFile: (bucket: string, key: string, content: string) => Promise<void>;
34
36
  getSignedUrl(options: PresignerOptions): Promise<string>;
35
37
  getSignedCookie: (keyPairId: string, privateKey: string, url: string, expires: number) => CloudfrontSignedCookiesOutput;
@@ -58,6 +58,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
58
58
  Object.defineProperty(exports, "__esModule", { value: true });
59
59
  exports.SimpleAWS = void 0;
60
60
  var cloudfront_signer_1 = require("@aws-sdk/cloudfront-signer");
61
+ var simple_staging_1 = require("simple-staging");
61
62
  var fs = require("fs");
62
63
  var os = require("os");
63
64
  var non_secure_1 = require("nanoid/non-secure");
@@ -366,7 +367,7 @@ var SimpleAWS = /** @class */ (function () {
366
367
  }
367
368
  });
368
369
  }); };
369
- this.upload = function (bucket, localPath, key) { return __awaiter(_this, void 0, void 0, function () {
370
+ this.upload = function (bucket, localPath, key, tags) { return __awaiter(_this, void 0, void 0, function () {
370
371
  var upload;
371
372
  return __generator(this, function (_a) {
372
373
  switch (_a.label) {
@@ -381,6 +382,7 @@ var SimpleAWS = /** @class */ (function () {
381
382
  },
382
383
  partSize: 5 * 1024 * 1024, // 5MB
383
384
  queueSize: 4,
385
+ tags: __spreadArray([SimpleAWS.stageTag], (tags || []), true),
384
386
  });
385
387
  return [4 /*yield*/, upload.done()];
386
388
  case 1:
@@ -389,7 +391,7 @@ var SimpleAWS = /** @class */ (function () {
389
391
  }
390
392
  });
391
393
  }); };
392
- this.uploadFromBuffer = function (bucket, key, buffer) { return __awaiter(_this, void 0, void 0, function () {
394
+ this.uploadFromBuffer = function (bucket, key, buffer, tags) { return __awaiter(_this, void 0, void 0, function () {
393
395
  var upload;
394
396
  return __generator(this, function (_a) {
395
397
  switch (_a.label) {
@@ -404,6 +406,7 @@ var SimpleAWS = /** @class */ (function () {
404
406
  },
405
407
  partSize: 5 * 1024 * 1024, // 5MB
406
408
  queueSize: 4,
409
+ tags: __spreadArray([SimpleAWS.stageTag], (tags || []), true),
407
410
  });
408
411
  return [4 /*yield*/, upload.done()];
409
412
  case 1:
@@ -696,12 +699,16 @@ var SimpleAWS = /** @class */ (function () {
696
699
  });
697
700
  SimpleAWS.prototype.getSignedUrl = function (options) {
698
701
  return __awaiter(this, void 0, void 0, function () {
699
- var _a, expiresIn, unhoistableHeaders, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd;
700
- return __generator(this, function (_b) {
702
+ var _a, expiresIn, unhoistableHeaders, tagging, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, tagging, cmd, cmd, cmd, cmd;
703
+ var _b, _c;
704
+ return __generator(this, function (_d) {
701
705
  _a = options.expiresIn, expiresIn = _a === void 0 ? 600 : _a, unhoistableHeaders = options.unhoistableHeaders;
702
706
  switch (options.operation) {
703
707
  case 'putObject': {
704
- cmd = new client_s3_1.PutObjectCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
708
+ tagging = ((_b = options.params) === null || _b === void 0 ? void 0 : _b.Tagging)
709
+ ? SimpleAWS.stringifiedStageTag + '&' + options.params.Tagging
710
+ : SimpleAWS.stringifiedStageTag;
711
+ cmd = new client_s3_1.PutObjectCommand(__assign(__assign({ Bucket: options.bucket, Key: options.key }, options.params), { Tagging: tagging }));
705
712
  return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
706
713
  expiresIn: expiresIn,
707
714
  unhoistableHeaders: unhoistableHeaders,
@@ -757,7 +764,10 @@ var SimpleAWS = /** @class */ (function () {
757
764
  })];
758
765
  }
759
766
  case 'createMultipartUpload': {
760
- cmd = new client_s3_1.CreateMultipartUploadCommand(__assign({ Bucket: options.bucket, Key: options.key }, options.params));
767
+ tagging = ((_c = options.params) === null || _c === void 0 ? void 0 : _c.Tagging)
768
+ ? SimpleAWS.stringifiedStageTag + '&' + options.params.Tagging
769
+ : SimpleAWS.stringifiedStageTag;
770
+ cmd = new client_s3_1.CreateMultipartUploadCommand(__assign(__assign({ Bucket: options.bucket, Key: options.key }, options.params), { Tagging: tagging }));
761
771
  return [2 /*return*/, (0, s3_request_presigner_1.getSignedUrl)(this.s3, cmd, {
762
772
  expiresIn: expiresIn,
763
773
  unhoistableHeaders: unhoistableHeaders,
@@ -789,6 +799,11 @@ var SimpleAWS = /** @class */ (function () {
789
799
  });
790
800
  });
791
801
  };
802
+ SimpleAWS.stageTag = {
803
+ Key: 'STAGE',
804
+ Value: simple_staging_1.envDefault.level,
805
+ };
806
+ SimpleAWS.stringifiedStageTag = "STAGE=".concat(simple_staging_1.envDefault.level);
792
807
  return SimpleAWS;
793
808
  }());
794
809
  exports.SimpleAWS = SimpleAWS;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "serverless-simple-middleware",
3
3
  "description": "Simple middleware to translate the interface of lambda's handler to request => response",
4
- "version": "0.0.61",
4
+ "version": "0.0.62",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "VoyagerX",
package/src/aws/simple.ts CHANGED
@@ -3,6 +3,8 @@ import {
3
3
  getSignedCookies,
4
4
  } from '@aws-sdk/cloudfront-signer';
5
5
 
6
+ import { envDefault as currentStage } from 'simple-staging';
7
+
6
8
  import * as fs from 'fs';
7
9
  import * as os from 'os';
8
10
  import { nanoid } from 'nanoid/non-secure';
@@ -24,6 +26,7 @@ import {
24
26
  ListPartsCommand,
25
27
  PutObjectCommand,
26
28
  S3,
29
+ Tag,
27
30
  UploadPartCommand,
28
31
  UploadPartCopyCommand,
29
32
  } from '@aws-sdk/client-s3';
@@ -42,6 +45,11 @@ export class SimpleAWS {
42
45
  private lazySqs: SQS | undefined;
43
46
  private lazyDynamodb: DynamoDBDocument | undefined;
44
47
  private lazyDynamodbAdmin: DynamoDB | undefined;
48
+ private static readonly stageTag: Tag = {
49
+ Key: 'STAGE',
50
+ Value: currentStage.level,
51
+ };
52
+ private static readonly stringifiedStageTag: string = `STAGE=${currentStage.level}`;
45
53
 
46
54
  constructor(config?: SimpleAWSConfig) {
47
55
  this.config = config || new SimpleAWSConfig();
@@ -301,6 +309,7 @@ export class SimpleAWS {
301
309
  bucket: string,
302
310
  localPath: string,
303
311
  key: string,
312
+ tags?: Tag[],
304
313
  ): Promise<string> => {
305
314
  logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
306
315
  const upload = new Upload({
@@ -312,6 +321,7 @@ export class SimpleAWS {
312
321
  },
313
322
  partSize: 5 * 1024 * 1024, // 5MB
314
323
  queueSize: 4,
324
+ tags: [SimpleAWS.stageTag, ...(tags || [])],
315
325
  });
316
326
 
317
327
  await upload.done();
@@ -322,6 +332,7 @@ export class SimpleAWS {
322
332
  bucket: string,
323
333
  key: string,
324
334
  buffer: Buffer,
335
+ tags?: Tag[],
325
336
  ): Promise<string> => {
326
337
  logger.debug(`Upload item[${key}] into bucket[${bucket}]`);
327
338
  const upload = new Upload({
@@ -333,6 +344,7 @@ export class SimpleAWS {
333
344
  },
334
345
  partSize: 5 * 1024 * 1024, // 5MB
335
346
  queueSize: 4,
347
+ tags: [SimpleAWS.stageTag, ...(tags || [])],
336
348
  });
337
349
  await upload.done();
338
350
  return key;
@@ -367,10 +379,14 @@ export class SimpleAWS {
367
379
  const { expiresIn = 600, unhoistableHeaders } = options;
368
380
  switch (options.operation) {
369
381
  case 'putObject': {
382
+ const tagging = options.params?.Tagging
383
+ ? SimpleAWS.stringifiedStageTag + '&' + options.params.Tagging
384
+ : SimpleAWS.stringifiedStageTag;
370
385
  const cmd = new PutObjectCommand({
371
386
  Bucket: options.bucket,
372
387
  Key: options.key,
373
388
  ...options.params,
389
+ Tagging: tagging,
374
390
  });
375
391
  return getSignedUrl(this.s3, cmd, {
376
392
  expiresIn: expiresIn,
@@ -454,10 +470,14 @@ export class SimpleAWS {
454
470
  });
455
471
  }
456
472
  case 'createMultipartUpload': {
473
+ const tagging = options.params?.Tagging
474
+ ? SimpleAWS.stringifiedStageTag + '&' + options.params.Tagging
475
+ : SimpleAWS.stringifiedStageTag;
457
476
  const cmd = new CreateMultipartUploadCommand({
458
477
  Bucket: options.bucket,
459
478
  Key: options.key,
460
479
  ...options.params,
480
+ Tagging: tagging,
461
481
  });
462
482
  return getSignedUrl(this.s3, cmd, {
463
483
  expiresIn: expiresIn,