apigeelint
Version:
Node module and tool to lint a bundle for an Apigee API Proxy or sharedflow.
95 lines (81 loc) • 2.85 kB
JavaScript
/*
Copyright © 2019-2020,2026 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//BN003 Cache Coherence A bundle that includes cache reads should include cache writes with the same keys.
const xpath = require("xpath"),
ruleId = require("../lintUtil.js").getRuleId();
const plugin = {
ruleId,
name: "Cache Coherence",
message:
"Cache strategies that rely on external cache population can be problematic and lead to run time errors.",
fatal: false,
severity: 2, //2=error
nodeType: "Cache",
enabled: true,
};
// TODO: fix this global var
const store = { LookupCache: {}, PopulateCache: {} };
const onBundle = function (bundle, cb) {
//is it a cache policy?
//is it attached?
// TODO: fix this global var
let flagged = false;
bundle.getPolicies().forEach(function (policy) {
if (
(policy.getType() === "PopulateCache" ||
policy.getType() === "LookupCache") &&
policy.getSteps().length > 0
) {
let prefix = xpath.select(
"//CacheKey/Prefix/text()",
policy.getElement(),
),
keyFragmentRef = xpath.select(
"//CacheKey/KeyFragment/@ref/text()",
policy.getElement(),
),
keyFragment = xpath.select(
"//CacheKey/KeyFragment/text()",
policy.getElement(),
);
prefix = (prefix[0] && prefix[0].data) || "";
keyFragmentRef = (keyFragmentRef[0] && keyFragmentRef[0].data) || "";
keyFragment = (keyFragment[0] && keyFragment[0].data) || "";
// Ok now we have the key values for the cache policy; stow them if
// they don't already exist. Use a composite key.
store[policy.getType()][
prefix + "-" + keyFragmentRef + "-" + keyFragment
] = policy;
}
});
//now inspect the store object
//for every lookup key we expect a populate key
for (const lookupKey in store["LookupCache"]) {
if (!store["PopulateCache"][lookupKey]) {
//report an error
//the object in the store is the policy so we can report directly against it
store["LookupCache"][lookupKey].addMessage({
plugin,
message: "LookupCache may not have a corresponding PopulateCache.",
});
flagged = true;
}
}
if (typeof cb == "function") {
cb(null, flagged);
}
};
module.exports = {
plugin,
onBundle,
};