* findRoutingRule - find applicable routing rule from bucket metadata * @param {RoutingRule []} routingRules - array of routingRule objects * @param {string} key - object key * @param {number} [errCode] - error code to match if applicable * @return {object | undefined} redirectInfo -- comprised
(routingRules, key, errCode)
| 8 | * a key of prefixFromRule and a value of routingRule.condition.keyPrefixEquals |
| 9 | */ |
| 10 | function findRoutingRule(routingRules, key, errCode) { |
| 11 | if (!routingRules || routingRules.length === 0) { |
| 12 | return undefined; |
| 13 | } |
| 14 | // For AWS compat: |
| 15 | // 1) use first routing rules whose conditions are satisfied |
| 16 | // 2) for matching prefix no need to check closest match. first |
| 17 | // match wins |
| 18 | // 3) there can be a match for a key condition with and without |
| 19 | // error code condition but first one that matches will be the rule |
| 20 | // used. So, if prefix foo without error and first rule has error condition, |
| 21 | // will fall through to next foo rule. But if first foo rule has |
| 22 | // no error condition, will have match on first rule even if later |
| 23 | // there is more specific rule with error condition. |
| 24 | for (let i = 0; i < routingRules.length; i++) { |
| 25 | const prefixFromRule = |
| 26 | routingRules[i].getCondition().keyPrefixEquals; |
| 27 | const errorCodeFromRule = |
| 28 | routingRules[i].getCondition().httpErrorCodeReturnedEquals; |
| 29 | if (prefixFromRule !== undefined) { |
| 30 | if (!key.startsWith(prefixFromRule)) { |
| 31 | // no key match, move on |
| 32 | continue; |
| 33 | } |
| 34 | // add the prefixFromRule to the redirect info |
| 35 | // so we can replaceKeyPrefixWith if that is part of redirect |
| 36 | // rule |
| 37 | const redirectInfo = Object.assign({ prefixFromRule }, |
| 38 | routingRules[i].getRedirect()); |
| 39 | // have key match so check error code match |
| 40 | if (errorCodeFromRule !== undefined) { |
| 41 | if (errCode === errorCodeFromRule) { |
| 42 | return redirectInfo; |
| 43 | } |
| 44 | // if don't match on both conditions, this is not the rule |
| 45 | // for us |
| 46 | continue; |
| 47 | } |
| 48 | // if no error code condition at all, we have found our match |
| 49 | return redirectInfo; |
| 50 | } |
| 51 | // we have an error code condition but no key condition |
| 52 | if (errorCodeFromRule !== undefined) { |
| 53 | if (errCode === errorCodeFromRule) { |
| 54 | const redirectInfo = Object.assign({}, |
| 55 | routingRules[i].getRedirect()); |
| 56 | return redirectInfo; |
| 57 | } |
| 58 | continue; |
| 59 | } |
| 60 | return undefined; |
| 61 | } |
| 62 | return undefined; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * extractRedirectInfo - convert location saved from x-amz-website header to |
no outgoing calls
no test coverage detected