Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rewrite-pattern): rewrite pattern substitution support with unit tests #423

Closed
wants to merge 2 commits into from
Closed

feat(rewrite-pattern): rewrite pattern substitution support with unit tests #423

wants to merge 2 commits into from

Conversation

dougtoppin
Copy link
Contributor

@dougtoppin dougtoppin commented Jan 14, 2023

Description of changes:
Add unit tests to rewrite pattern support from original PR #399

All changes, including original:

  • image-request and thumbor-mapper changes for multiple rewrite/substitution entries
  • unit tests added for rewrite/substitution entries
  • common functions in PR moved to solution-utils/helpers
  • unit test support added for solution-utils/helpers

Serverless Image Handler provides support for remapping requests to reduce the effort required to migrate an existing image request model to the solution. This is done by configuring the REWRITE_MATCH_PATTERN and REWRITE_SUBSTITUTION regular expression environment variables for the backend Lambda. REWRITE_MATCH_PATTERN describes the incoming request that is to be changed. REWRITE_SUBSTITUTION contains the pattern to be mapped to.

The initial implementation of this facility supported a single rewrite/substitution pair.
This PR provides the ability to remap/substitute multiple patterns.
The env vars are set to a matching set of entries.

REWRITE_MATCH_PATTERN and REWRITE_SUBSTITUTION values can be written like JSON Arrays so multiple rewrite rules can be applied.

For example, for the following env var in the. backend Lambda, 3 request patterns can be specified which will be mapped to the rewrite substitution values

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)"]

Checklist

  • 👋 I have added unit tests for all code changes.
  • 👋 I have run the unit tests, and all unit tests have passed.
  • ⚠️ This pull request might incur a breaking change.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dougtoppin dougtoppin marked this pull request as ready for review January 16, 2023 19:56
* @param str Input string to evaluate
* @returns Either a json object or a plain string
*/
export function parseJson(str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this function is a little misleading, I would change it to "getParamsFromString" or something. It being in a generic utils/helpers file puts it at real risk of being reused by someone.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok with your suggestion. Maybe we can say "convertPatterns"

} else {
path = path.replace(REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION);
const REWRITE_MATCH_PATTERNS = parseJson(REWRITE_MATCH_PATTERN);
const REWRITE_SUBSTITUTIONS = parseJson(REWRITE_SUBSTITUTION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever check that these are the same length?

* @param matchPattern string to operate on
* @returns regex
*/
export function generateRegExp(matchPattern) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests for invalid regexes? Invalid flags?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment is important but I think it is not relevant to this pull request. Only the location of the code has changed, the logic has not changed.

path = path.replace(regExp, REWRITE_SUBSTITUTIONS[k]);
if (generateRegExp(matchPattern).test(path)) break;
} else {
path = path.replace(matchPattern, REWRITE_SUBSTITUTIONS[k]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If matchPattern isn't a string, what is it? Should we check? I don't see any relevant tests

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this PR converts "REWRITE_MATCH_PATTERNS" variable into an array and loop each item as "matchPattern" variable. There is no change in the logic.

Change is like below

- if (typeof REWRITE_MATCH_PATTERN === "string") {
+ if (typeof matchPattern === "string") {

If a test is going to be written on this subject, a different pull request may be requested.

const patternStrings = matchPattern.split("/");
const flags = patternStrings.pop();
const parsedPatternString = matchPattern.slice(1, matchPattern.length - 1 - flags.length);
return new RegExp(parsedPatternString, flags);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this feature is already out there, but do we really need regex to solve this problem?
It looks like these values are controlled in the lambda env, but are we worried about customers redos-ing themselves?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment is important but I think it is not relevant to this pull request. Only the location of the code has changed, the logic has not changed.

@github-actions
Copy link

This pr has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the Stale label Apr 18, 2023
@dougtoppin dougtoppin removed the Stale label May 4, 2023
@dougtoppin dougtoppin closed this by deleting the head repository Dec 22, 2023
@tasarsu
Copy link

tasarsu commented Dec 22, 2023

Hi @dougtoppin,

I received an email regarding the closure of this pull request. Are you not considering implementing this feature?

I have checked the Implementation Guide, and it mentions that the instructions for the rewrite feature have been clarified. However, it is still not possible to create more than one rewrite rule with the current code. In the old Thumbor version, it was possible to make multiple rewrites. This pull request makes it possible to do so.

@dougtoppin
Copy link
Contributor Author

dougtoppin commented Dec 22, 2023

@tasarsu I deleted an old personal fork of mine where I had made some changes.
Sorry about the confusion.
Can you resubmit your PR using the current v6.2.4 and we will evaluate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants