From 5e0dec019099ed984b2351be913ec4b2f1fd5885 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:24:59 -0500 Subject: [PATCH 01/13] Update code-style-lint.yml enable style/lint --- .github/workflows/code-style-lint.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code-style-lint.yml b/.github/workflows/code-style-lint.yml index c9a117163..14143bfda 100644 --- a/.github/workflows/code-style-lint.yml +++ b/.github/workflows/code-style-lint.yml @@ -11,7 +11,7 @@ on: jobs: prettier: name: Style Check - if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -21,7 +21,7 @@ jobs: - run: npx --y prettier --config source/.prettierrc.yml --check 'source/**/*.ts' linter: name: Lint Check - if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -33,4 +33,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd source && npm i --only=dev - npx --y eslint . --ext .ts \ No newline at end of file + npx --y eslint . --ext .ts From c6e3c5fe74e88cbb8aa97f9de7e052aff30e4336 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:33:02 -0500 Subject: [PATCH 02/13] Update cdk-nag.yml enable cdk nag --- .github/workflows/cdk-nag.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cdk-nag.yml b/.github/workflows/cdk-nag.yml index 4072b8119..7ffcaef0b 100644 --- a/.github/workflows/cdk-nag.yml +++ b/.github/workflows/cdk-nag.yml @@ -11,7 +11,7 @@ on: jobs: cdk-nag: name: CDK Nag Check - if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -23,4 +23,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd source/constructs && npm i --only=dev - npx cdk synth \ No newline at end of file + npx cdk synth From d4c75193bea538bced5d6269caab51263921f630 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:33:42 -0500 Subject: [PATCH 03/13] Update codeql.yml enable ql --- .github/workflows/codeql.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1ecc1b03d..97a897d54 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -10,7 +10,7 @@ on: jobs: codeql: name: CodeQL Check - if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: fail-fast: false @@ -21,4 +21,4 @@ jobs: - uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} - - uses: github/codeql-action/analyze@v2 \ No newline at end of file + - uses: github/codeql-action/analyze@v2 From 8a4af203c68a66038f2cb938e4f890a07de678dd Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:34:31 -0500 Subject: [PATCH 04/13] Update run-unit-test.yml enable unit tests --- .github/workflows/run-unit-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-unit-test.yml b/.github/workflows/run-unit-test.yml index acb73c9b5..b2728ac3c 100644 --- a/.github/workflows/run-unit-test.yml +++ b/.github/workflows/run-unit-test.yml @@ -11,7 +11,7 @@ on: jobs: unittest: name: Unit Test Check - if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -23,4 +23,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd deployment - chmod +x ./run-unit-tests.sh && DEBUG=true ./run-unit-tests.sh \ No newline at end of file + chmod +x ./run-unit-tests.sh && DEBUG=true ./run-unit-tests.sh From 4cb412ec3daaf50dbc7dbec9194850549cd05046 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:36:18 -0500 Subject: [PATCH 05/13] Create .viperlightignore add file --- .viperlightignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .viperlightignore diff --git a/.viperlightignore b/.viperlightignore new file mode 100644 index 000000000..b41710277 --- /dev/null +++ b/.viperlightignore @@ -0,0 +1,5 @@ +node_modules/* +CONTRIBUTING.md:46 +CODE_OF_CONDUCT.md:4 +coverage/* +.zip From d0bebab0d5ec8613c9b6eedbb968454d7caf4327 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Wed, 21 Dec 2022 15:36:40 -0500 Subject: [PATCH 06/13] Create .viperlightrc --- .viperlightrc | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .viperlightrc diff --git a/.viperlightrc b/.viperlightrc new file mode 100644 index 000000000..34036e5ae --- /dev/null +++ b/.viperlightrc @@ -0,0 +1,4 @@ +{ + "all": true, + "failOn": "medium" +} From 1cc9014c2165adc2bd4de03b34fdb24265e79559 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Fri, 23 Dec 2022 09:24:07 -0500 Subject: [PATCH 07/13] Update code-style-lint.yml testing develop branch workflow --- .github/workflows/code-style-lint.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/code-style-lint.yml b/.github/workflows/code-style-lint.yml index 14143bfda..f14bea704 100644 --- a/.github/workflows/code-style-lint.yml +++ b/.github/workflows/code-style-lint.yml @@ -4,9 +4,14 @@ name: Code Style and Lint on: push: branches: - - '*' + - develop + - feature/eslint + pull_request: types: [opened, edited, reopened, synchronize] + branches: + - develop + - feature/eslint jobs: prettier: From 1667de7e867989d0cea1568008dc80b49ed35ade Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Fri, 23 Dec 2022 09:52:30 -0500 Subject: [PATCH 08/13] Update code-style-lint.yml quote branch names --- .github/workflows/code-style-lint.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/code-style-lint.yml b/.github/workflows/code-style-lint.yml index f14bea704..c87ffd6c9 100644 --- a/.github/workflows/code-style-lint.yml +++ b/.github/workflows/code-style-lint.yml @@ -4,14 +4,14 @@ name: Code Style and Lint on: push: branches: - - develop - - feature/eslint + - 'develop' + - 'feature/eslint' pull_request: types: [opened, edited, reopened, synchronize] branches: - - develop - - feature/eslint + - 'develop' + - 'feature/eslint' jobs: prettier: From 64fedd9bc71d4b0abc7a194843e345305fb6b173 Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Thu, 29 Dec 2022 15:44:47 -0500 Subject: [PATCH 09/13] Initial testing of PR 399 submission --- source/image-handler/image-request.ts | 36 ++++++++++++++----- .../image-request/parse-request-type.spec.ts | 18 ++++++++++ source/image-handler/thumbor-mapper.ts | 35 +++++++++++++----- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/source/image-handler/image-request.ts b/source/image-handler/image-request.ts index f17b25e44..90c6bf096 100644 --- a/source/image-handler/image-request.ts +++ b/source/image-handler/image-request.ts @@ -278,15 +278,18 @@ export class ImageRequest { if (requestType === RequestTypes.CUSTOM) { const { REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION } = process.env; - if (typeof REWRITE_MATCH_PATTERN === "string") { - const patternStrings = REWRITE_MATCH_PATTERN.split("/"); - const flags = patternStrings.pop(); - const parsedPatternString = REWRITE_MATCH_PATTERN.slice(1, REWRITE_MATCH_PATTERN.length - 1 - flags.length); - const regExp = new RegExp(parsedPatternString, flags); - - path = path.replace(regExp, REWRITE_SUBSTITUTION); - } else { - path = path.replace(REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION); + const REWRITE_MATCH_PATTERNS = this.parseJson(REWRITE_MATCH_PATTERN); + const REWRITE_SUBSTITUTIONS = this.parseJson(REWRITE_SUBSTITUTION); + + for (let k = 0; k < REWRITE_MATCH_PATTERNS.length; k++) { + const matchPattern = REWRITE_MATCH_PATTERNS[k]; + if (typeof matchPattern === "string") { + const regExp = this.generateRegExp(matchPattern); + path = path.replace(regExp, REWRITE_SUBSTITUTIONS[k]); + if (this.generateRegExp(matchPattern).test(path)) break; + } else { + path = path.replace(matchPattern, REWRITE_SUBSTITUTIONS[k]); + } } } @@ -303,6 +306,21 @@ export class ImageRequest { ); } + parseJson(str) { + try { + return JSON.parse(str); + } catch (e) { + return [str]; + } + } + + generateRegExp(matchPattern) { + const patternStrings = matchPattern.split("/"); + const flags = patternStrings.pop(); + const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length); + return new RegExp(parsedPatternString, flags); + } + /** * Determines how to handle the request being made based on the URL path prefix to the image request. * Categorizes a request as either "image" (uses the Sharp library), "thumbor" (uses Thumbor mapping), or "custom" (uses the rewrite function). diff --git a/source/image-handler/test/image-request/parse-request-type.spec.ts b/source/image-handler/test/image-request/parse-request-type.spec.ts index 0759bd8f7..c4dd604da 100644 --- a/source/image-handler/test/image-request/parse-request-type.spec.ts +++ b/source/image-handler/test/image-request/parse-request-type.spec.ts @@ -88,6 +88,24 @@ describe("parseRequestType", () => { expect(result).toEqual(expectedResult); }); + it("Should pass if the method detects a custom request with multiple rewrite patterns", () => { + // Arrange + const event = { path: "/additionalImageRequestParameters/image.jpg" }; + process.env = { + REWRITE_MATCH_PATTERN: '["//thumb/g","//small/g","//large/g"]', + REWRITE_SUBSTITUTION: + '["/300x300/filters:quality(80)","/fit-in/600x600/filters:quality(80)","/fit-in/1200x1200/filters:quality(80)"]', + }; + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.parseRequestType(event); + + // Assert + const expectedResult = RequestTypes.CUSTOM; + expect(result).toEqual(expectedResult); + }); + it("Should throw an error if the method cannot determine the request type based on the three groups given", () => { // Arrange const event = { path: "12x12e24d234r2ewxsad123d34r.bmp" }; diff --git a/source/image-handler/thumbor-mapper.ts b/source/image-handler/thumbor-mapper.ts index 5cc7b4c6a..07cd54f8e 100644 --- a/source/image-handler/thumbor-mapper.ts +++ b/source/image-handler/thumbor-mapper.ts @@ -54,20 +54,39 @@ export class ThumborMapper { } else { let parsedPath = ""; - if (typeof REWRITE_MATCH_PATTERN === "string") { - const patternStrings = REWRITE_MATCH_PATTERN.split("/"); - const flags = patternStrings.pop(); - const parsedPatternString = REWRITE_MATCH_PATTERN.slice(1, REWRITE_MATCH_PATTERN.length - 1 - flags.length); - const regExp = new RegExp(parsedPatternString, flags); - parsedPath = path.replace(regExp, REWRITE_SUBSTITUTION); - } else { - parsedPath = path.replace(REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION); + const REWRITE_MATCH_PATTERNS = this.parseJson(REWRITE_MATCH_PATTERN); + const REWRITE_SUBSTITUTIONS = this.parseJson(REWRITE_SUBSTITUTION); + + for (let k = 0; k < REWRITE_MATCH_PATTERNS.length; k++) { + const matchPattern = REWRITE_MATCH_PATTERNS[k]; + if (typeof matchPattern === "string") { + const regExp = this.generateRegExp(matchPattern); + parsedPath = path.replace(regExp, REWRITE_SUBSTITUTIONS[k]); + if (this.generateRegExp(matchPattern).test(path)) break; + } else { + parsedPath = path.replace(matchPattern, REWRITE_SUBSTITUTIONS[k]); + } } return parsedPath; } } + parseJson(str: string) { + try { + return JSON.parse(str); + } catch (e) { + return [str]; + } + } + + generateRegExp(matchPattern: string) { + const patternStrings = matchPattern.split("/"); + const flags = patternStrings.pop(); + const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length); + return new RegExp(parsedPatternString, flags); + } + /** * Maps background color the current edits object * @param filterValue The specified color value From 3444d627d476ac3fdfcf7e50b60f0f109c0a3b6e Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Mon, 2 Jan 2023 16:14:39 -0500 Subject: [PATCH 10/13] Add PR #399 multiple rewrite support and unit tests --- deployment/run-unit-tests.sh | 1 + source/image-handler/image-request.ts | 24 ++----- .../image-request/parse-image-edits.spec.ts | 48 ++++++++++++++ .../image-request/parse-image-key.spec.ts | 21 +++++++ source/image-handler/thumbor-mapper.ts | 24 ++----- source/image-handler/tsconfig.json | 2 +- source/solution-utils/helpers.ts | 25 ++++++++ .../solution-utils/test/get-options.test.ts | 2 + source/solution-utils/test/helpers.spec.ts | 62 +++++++++++++++++++ source/solution-utils/tsconfig.json | 2 +- 10 files changed, 171 insertions(+), 40 deletions(-) create mode 100644 source/solution-utils/test/helpers.spec.ts diff --git a/deployment/run-unit-tests.sh b/deployment/run-unit-tests.sh index 76d56a1af..43746bea4 100755 --- a/deployment/run-unit-tests.sh +++ b/deployment/run-unit-tests.sh @@ -53,6 +53,7 @@ declare -a lambda_packages=( "constructs" "image-handler" "custom-resource" + "solution-utils" ) export overrideWarningsEnabled=false diff --git a/source/image-handler/image-request.ts b/source/image-handler/image-request.ts index 90c6bf096..24c845689 100644 --- a/source/image-handler/image-request.ts +++ b/source/image-handler/image-request.ts @@ -18,6 +18,7 @@ import { } from "./lib"; import { SecretProvider } from "./secret-provider"; import { ThumborMapper } from "./thumbor-mapper"; +import { parseJson, generateRegExp } from "../solution-utils/helpers"; type OriginalImageInfo = Partial<{ contentType: string; @@ -278,15 +279,15 @@ export class ImageRequest { if (requestType === RequestTypes.CUSTOM) { const { REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION } = process.env; - const REWRITE_MATCH_PATTERNS = this.parseJson(REWRITE_MATCH_PATTERN); - const REWRITE_SUBSTITUTIONS = this.parseJson(REWRITE_SUBSTITUTION); + const REWRITE_MATCH_PATTERNS = parseJson(REWRITE_MATCH_PATTERN); + const REWRITE_SUBSTITUTIONS = parseJson(REWRITE_SUBSTITUTION); for (let k = 0; k < REWRITE_MATCH_PATTERNS.length; k++) { const matchPattern = REWRITE_MATCH_PATTERNS[k]; if (typeof matchPattern === "string") { - const regExp = this.generateRegExp(matchPattern); + const regExp = generateRegExp(matchPattern); path = path.replace(regExp, REWRITE_SUBSTITUTIONS[k]); - if (this.generateRegExp(matchPattern).test(path)) break; + if (generateRegExp(matchPattern).test(path)) break; } else { path = path.replace(matchPattern, REWRITE_SUBSTITUTIONS[k]); } @@ -306,21 +307,6 @@ export class ImageRequest { ); } - parseJson(str) { - try { - return JSON.parse(str); - } catch (e) { - return [str]; - } - } - - generateRegExp(matchPattern) { - const patternStrings = matchPattern.split("/"); - const flags = patternStrings.pop(); - const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length); - return new RegExp(parsedPatternString, flags); - } - /** * Determines how to handle the request being made based on the URL path prefix to the image request. * Categorizes a request as either "image" (uses the Sharp library), "thumbor" (uses Thumbor mapping), or "custom" (uses the rewrite function). diff --git a/source/image-handler/test/image-request/parse-image-edits.spec.ts b/source/image-handler/test/image-request/parse-image-edits.spec.ts index c8d4380c5..3fc837818 100644 --- a/source/image-handler/test/image-request/parse-image-edits.spec.ts +++ b/source/image-handler/test/image-request/parse-image-edits.spec.ts @@ -72,6 +72,54 @@ describe("parseImageEdits", () => { expect(result).toEqual(expectedResult); }); + it("Should pass if the proper result is returned for a sample custom-type image request - multipe image rewrite - confirm correct replacement - 1", () => { + // Arrange + const event = { + path: "/thumb/beach-100x100.jpg", + }; + + /** + * a custom request soecifying thumb should return a replacement using the first substitution entry + */ + process.env = { + REWRITE_MATCH_PATTERN: '["//thumb/g","//small/g","//large/g"]', + REWRITE_SUBSTITUTION: + '["/300x300/filters:quality(80)","/fit-in/600x600/filters:quality(80)","/fit-in/1200x1200/filters:quality(80)"]', + }; + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.parseImageEdits(event, RequestTypes.CUSTOM); + + // Assert + const expectedResult = { jpeg: { quality: 80 }, resize: { height: 300, width: 300 } }; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for a sample custom-type image request - multipe image rewrite - confirm correct replacement - 2", () => { + // Arrange + const event = { + path: "/large/test.jpg", + }; + + /** + * a custom request specifying large should return a replacement using the third substitution entry which includes a resize + */ + process.env = { + REWRITE_MATCH_PATTERN: '["//thumb/g","//small/g","//large/g"]', + REWRITE_SUBSTITUTION: + '["/300x300/filters:quality(80)","/fit-in/600x600/filters:quality(80)","/fit-in/1200x1200/filters:quality(100)"]', + }; + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.parseImageEdits(event, RequestTypes.CUSTOM); + + // Assert + const expectedResult = { jpeg: { quality: 100 }, resize: { height: 1200, width: 1200, fit: "inside" } }; + expect(result).toEqual(expectedResult); + }); + it("Should throw an error if a requestType is not specified and/or the image edits cannot be parsed", () => { // Arrange const event = { diff --git a/source/image-handler/test/image-request/parse-image-key.spec.ts b/source/image-handler/test/image-request/parse-image-key.spec.ts index 67914dba2..949101646 100644 --- a/source/image-handler/test/image-request/parse-image-key.spec.ts +++ b/source/image-handler/test/image-request/parse-image-key.spec.ts @@ -240,6 +240,27 @@ describe("parseImageKey", () => { expect(result).toEqual(expectedResult); }); + it("Should pass if an image key value is provided in the custom request format - confirm key retrieval when multiple image rewrite specified", () => { + // Arrange + const event = { + path: "/thumb/test.jpg", + }; + + process.env = { + REWRITE_MATCH_PATTERN: '["//thumb/g","//small/g","//large/g"]', + REWRITE_SUBSTITUTION: + '["/300x300/filters:quality(80)","/fit-in/600x600/filters:quality(80)","/fit-in/1200x1200/filters:quality(80)"]', + }; + + // Act + const imageRequest = new ImageRequest(s3Client, secretProvider); + const result = imageRequest.parseImageKey(event, RequestTypes.CUSTOM); + + // Assert + const expectedResult = "test.jpg"; + expect(result).toEqual(expectedResult); + }); + it("Should throw an error if an unrecognized requestType is passed into the function as a parameter", () => { // Arrange const event = { diff --git a/source/image-handler/thumbor-mapper.ts b/source/image-handler/thumbor-mapper.ts index 07cd54f8e..6fc305632 100644 --- a/source/image-handler/thumbor-mapper.ts +++ b/source/image-handler/thumbor-mapper.ts @@ -5,6 +5,7 @@ import Color from "color"; import ColorName from "color-name"; import { ImageEdits, ImageFitTypes, ImageFormatTypes } from "./lib"; +import { parseJson, generateRegExp } from "../solution-utils/helpers"; export class ThumborMapper { private static readonly EMPTY_IMAGE_EDITS: ImageEdits = {}; @@ -54,15 +55,15 @@ export class ThumborMapper { } else { let parsedPath = ""; - const REWRITE_MATCH_PATTERNS = this.parseJson(REWRITE_MATCH_PATTERN); - const REWRITE_SUBSTITUTIONS = this.parseJson(REWRITE_SUBSTITUTION); + const REWRITE_MATCH_PATTERNS = parseJson(REWRITE_MATCH_PATTERN); + const REWRITE_SUBSTITUTIONS = parseJson(REWRITE_SUBSTITUTION); for (let k = 0; k < REWRITE_MATCH_PATTERNS.length; k++) { const matchPattern = REWRITE_MATCH_PATTERNS[k]; if (typeof matchPattern === "string") { - const regExp = this.generateRegExp(matchPattern); + const regExp = generateRegExp(matchPattern); parsedPath = path.replace(regExp, REWRITE_SUBSTITUTIONS[k]); - if (this.generateRegExp(matchPattern).test(path)) break; + if (generateRegExp(matchPattern).test(path)) break; } else { parsedPath = path.replace(matchPattern, REWRITE_SUBSTITUTIONS[k]); } @@ -72,21 +73,6 @@ export class ThumborMapper { } } - parseJson(str: string) { - try { - return JSON.parse(str); - } catch (e) { - return [str]; - } - } - - generateRegExp(matchPattern: string) { - const patternStrings = matchPattern.split("/"); - const flags = patternStrings.pop(); - const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length); - return new RegExp(parsedPatternString, flags); - } - /** * Maps background color the current edits object * @param filterValue The specified color value diff --git a/source/image-handler/tsconfig.json b/source/image-handler/tsconfig.json index 56ebe595b..95ab2c31d 100644 --- a/source/image-handler/tsconfig.json +++ b/source/image-handler/tsconfig.json @@ -10,6 +10,6 @@ "sourceMap": true, "types": ["node", "@types/jest"] }, - "include": ["**/*.ts"], + "include": ["**/*.ts", "../solution-utils/test/helpers.spec.ts"], "exclude": ["package", "dist", "**/*.map"] } diff --git a/source/solution-utils/helpers.ts b/source/solution-utils/helpers.ts index 767f3472b..98c111071 100644 --- a/source/solution-utils/helpers.ts +++ b/source/solution-utils/helpers.ts @@ -9,3 +9,28 @@ export function isNullOrWhiteSpace(str: string): boolean { return !str || str.replace(/\s/g, "") === ""; } + +/** + * Determine if this appears to be json or a plain string + * @param str Input string to evaluate + * @returns Either a json object or a plain string + */ +export function parseJson(str) { + try { + return JSON.parse(str); + } catch (e) { + return [str]; + } +} + +/** + * Given a string that may describe actions in an image request, create a regex from the string + * @param matchPattern string to operate on + * @returns regex + */ +export function generateRegExp(matchPattern) { + const patternStrings = matchPattern.split("/"); + const flags = patternStrings.pop(); + const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length); + return new RegExp(parsedPatternString, flags); +} diff --git a/source/solution-utils/test/get-options.test.ts b/source/solution-utils/test/get-options.test.ts index b8da776f6..8ea83e17e 100644 --- a/source/solution-utils/test/get-options.test.ts +++ b/source/solution-utils/test/get-options.test.ts @@ -24,6 +24,7 @@ describe("getOptions", () => { }); it("will return an empty object when environment variables are missing", () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires const { getOptions } = require("../get-options"); expect.assertions(4); @@ -42,6 +43,7 @@ describe("getOptions", () => { }); it("will return an object with the custom user agent string", () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires const { getOptions } = require("../get-options"); expect.assertions(1); expect(getOptions()).toEqual({ diff --git a/source/solution-utils/test/helpers.spec.ts b/source/solution-utils/test/helpers.spec.ts new file mode 100644 index 000000000..234e24aa0 --- /dev/null +++ b/source/solution-utils/test/helpers.spec.ts @@ -0,0 +1,62 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { isNullOrWhiteSpace, parseJson, generateRegExp } from "../helpers"; + +describe("helpers", () => { + it("Should pass if the proper result is returned for a whitespace only string", () => { + const result = isNullOrWhiteSpace(" "); + + const expectedResult = true; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for a null string", () => { + const result = isNullOrWhiteSpace(""); + + const expectedResult = true; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for non-whitespace containing string", () => { + const result = isNullOrWhiteSpace("abc"); + + const expectedResult = false; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for tbd", () => { + const result = parseJson("filter:"); + + const expectedResult = ["filter:"]; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for a null string", () => { + const result = parseJson(""); + + const expectedResult = [""]; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for json with multiple objects", () => { + const result = parseJson('["//thumb/g","//small/g","//large/g"]'); + + const expectedResult = ["//thumb/g", "//small/g", "//large/g"]; + expect(result).toEqual(expectedResult); + }); + + it("Should pass if the proper result is returned for a simple regex", () => { + const result = generateRegExp("/thumb/g"); + + const expectedResult1 = /thumb/g; + expect(result).toEqual(expectedResult1); + }); + + it("Should pass if the proper result is returned for a simple rege with embedded slash that must be escaped", () => { + const result = generateRegExp("//thumb/g"); + + const expectedResult1 = /\/thumb/g; + expect(result).toEqual(expectedResult1); + }); +}); diff --git a/source/solution-utils/tsconfig.json b/source/solution-utils/tsconfig.json index 56ebe595b..f5ec83c87 100644 --- a/source/solution-utils/tsconfig.json +++ b/source/solution-utils/tsconfig.json @@ -10,6 +10,6 @@ "sourceMap": true, "types": ["node", "@types/jest"] }, - "include": ["**/*.ts"], + "include": ["**/*.ts", "test/helpers.spec.ts"], "exclude": ["package", "dist", "**/*.map"] } From 69b05da86f74a081d47adbcd40e656c18a38273d Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Tue, 3 Jan 2023 13:56:30 -0500 Subject: [PATCH 11/13] restore code-style-lint to previous version --- .github/workflows/code-style-lint.yml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/.github/workflows/code-style-lint.yml b/.github/workflows/code-style-lint.yml index c87ffd6c9..c9a117163 100644 --- a/.github/workflows/code-style-lint.yml +++ b/.github/workflows/code-style-lint.yml @@ -4,19 +4,14 @@ name: Code Style and Lint on: push: branches: - - 'develop' - - 'feature/eslint' - + - '*' pull_request: types: [opened, edited, reopened, synchronize] - branches: - - 'develop' - - 'feature/eslint' jobs: prettier: name: Style Check - #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -26,7 +21,7 @@ jobs: - run: npx --y prettier --config source/.prettierrc.yml --check 'source/**/*.ts' linter: name: Lint Check - #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -38,4 +33,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd source && npm i --only=dev - npx --y eslint . --ext .ts + npx --y eslint . --ext .ts \ No newline at end of file From 657cb25a50b11c7e2a1bdac5c3baf5beb79cbd6d Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Tue, 3 Jan 2023 14:00:14 -0500 Subject: [PATCH 12/13] restore to previous version --- .github/workflows/cdk-nag.yml | 4 ++-- .github/workflows/codeql.yml | 4 ++-- .github/workflows/run-unit-test.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cdk-nag.yml b/.github/workflows/cdk-nag.yml index 7ffcaef0b..4072b8119 100644 --- a/.github/workflows/cdk-nag.yml +++ b/.github/workflows/cdk-nag.yml @@ -11,7 +11,7 @@ on: jobs: cdk-nag: name: CDK Nag Check - #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -23,4 +23,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd source/constructs && npm i --only=dev - npx cdk synth + npx cdk synth \ No newline at end of file diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 97a897d54..1ecc1b03d 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -10,7 +10,7 @@ on: jobs: codeql: name: CodeQL Check - #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: fail-fast: false @@ -21,4 +21,4 @@ jobs: - uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} - - uses: github/codeql-action/analyze@v2 + - uses: github/codeql-action/analyze@v2 \ No newline at end of file diff --git a/.github/workflows/run-unit-test.yml b/.github/workflows/run-unit-test.yml index b2728ac3c..acb73c9b5 100644 --- a/.github/workflows/run-unit-test.yml +++ b/.github/workflows/run-unit-test.yml @@ -11,7 +11,7 @@ on: jobs: unittest: name: Unit Test Check - #if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} + if: ${{ (github.repository_owner != 'aws-solutions' && github.event_name == 'push') || (github.repository_owner == 'aws-solutions' && github.event_name == 'pull_request') }} runs-on: ubuntu-latest strategy: matrix: @@ -23,4 +23,4 @@ jobs: node-version: ${{ matrix.node-version }} - run: | cd deployment - chmod +x ./run-unit-tests.sh && DEBUG=true ./run-unit-tests.sh + chmod +x ./run-unit-tests.sh && DEBUG=true ./run-unit-tests.sh \ No newline at end of file From 860d9ee8d7c260262a4827555d9e04c933f7bb1b Mon Sep 17 00:00:00 2001 From: Doug Toppin Date: Tue, 3 Jan 2023 14:32:18 -0500 Subject: [PATCH 13/13] add detail to test description --- source/solution-utils/test/helpers.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/solution-utils/test/helpers.spec.ts b/source/solution-utils/test/helpers.spec.ts index 234e24aa0..254461662 100644 --- a/source/solution-utils/test/helpers.spec.ts +++ b/source/solution-utils/test/helpers.spec.ts @@ -25,7 +25,7 @@ describe("helpers", () => { expect(result).toEqual(expectedResult); }); - it("Should pass if the proper result is returned for tbd", () => { + it("Should pass if the proper result is returned for a singe entry", () => { const result = parseJson("filter:"); const expectedResult = ["filter:"];