Skip to content

Commit

Permalink
Plugin search by modified date (#409)
Browse files Browse the repository at this point in the history
* add function to check plugin uri match

* use `pluginMatchesUrl` to find plugin match

`hasNewPluginForUri` - finds newest plugin for cache invalidation

* export `hasNewPluginForUri`

* cache: better search plugin by uri for cache invalidation
  • Loading branch information
nleush authored May 19, 2022
1 parent a2c5c1e commit 639d04a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 47 deletions.
54 changes: 9 additions & 45 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,7 @@

} else {

var domain = uri.split('/')[2]
// Skip www. for domain search.
.replace(/^www\./i, "")
.toLowerCase();
const domain = pluginLoader.getDomainForPlugin(uri);

var pluginMatchesByDomains = {};

Expand All @@ -426,48 +423,15 @@
for(var i = 0; i < pluginsList.length; i++) {
var plugin = pluginsList[i];

if (plugin.domain) {
var match = plugin.pluginMatchesUrl(domain, uri);

// Match only by regexp. Used in specific cases where domain changes (like national domain).

var match = null, j = 0, res = plugin.re;
while (!match && j < res.length) {
match = uri.match(res[j]);
j++;
}
if (match) {
// Store match for plugin.
if (match) {
if (match === true) {
// Match without regexp.
registerDomainPlugin(plugin, null);
} else {
registerDomainPlugin(plugin, match);
pluginsUrlMatches[plugin.id] = match;
continue;
} else if (res.length) {
// Skip plugin with unmatched re.
continue;
}

// Straight match by domain.

// Positive match on plugin.domain="domain.com", domain="sub.domain.com"
// Positive match on plugin.domain="domain.com", domain="domain.com"
var idx = domain.indexOf(plugin.domain);

if (idx === -1 || ((idx > 0) && domain.charAt(idx - 1) !== '.')) {
// Break if not found, or not dot separation.
continue;
}

if (idx > 0) {
var subdomain = domain.substring(0, idx - 1);
if (subdomain === 'blog') {
// Skip "blog.*.*" blog page for domain plugin without re.
continue;
}
}

var match = (idx + plugin.domain.length) === domain.length;

if (match) {
registerDomainPlugin(plugin, null);
}
}
}
Expand All @@ -488,8 +452,8 @@
addAllGeneric();
}

for(var domain in pluginMatchesByDomains) {
var domainPlugins = pluginMatchesByDomains[domain];
for(var d in pluginMatchesByDomains) {
var domainPlugins = pluginMatchesByDomains[d];

var matchedPluginsNames = [];

Expand Down
96 changes: 96 additions & 0 deletions lib/loader/pluginLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
pluginsList = [],
postPluginsList = [],
pluginsByDomain = {};

var pluginsListSortedByModifiedTime;

export {
plugins as _plugins,
Expand Down Expand Up @@ -98,6 +100,19 @@
}
}

function fillModifiedDate() {
for(var i = 0; i < pluginsList.length; i++) {
var plugin = pluginsList[i];
plugin.modifiedWithMixins = plugin.getPluginLastModifiedDate();
}

pluginsListSortedByModifiedTime = [...pluginsList];
pluginsListSortedByModifiedTime.sort((p1, p2) => {
// Sort desc by modified date.
return p2.modifiedWithMixins - p1.modifiedWithMixins;
});
}

function getFileName(filenameWithExt) {
return filenameWithExt.replace(/\.(js|ejs)$/i, "");
}
Expand Down Expand Up @@ -304,6 +319,7 @@
var stat = fs.statSync(pluginPath);
pluginDeclaration.modified = new Date(stat.mtime);
pluginDeclaration.getPluginLastModifiedDate = getPluginLastModifiedDate;
pluginDeclaration.pluginMatchesUrl = pluginMatchesUrl;

// If no mixins - mark domain plugin 'asks to mixin all generic plugins'.
if (plugin.mixins) {
Expand Down Expand Up @@ -445,6 +461,84 @@
return record;
};

export function getDomainForPlugin(uri) {
return uri.split('/')[2]
// Skip www. for domain search.
.replace(/^www\./i, "")
.toLowerCase();
}

export function hasNewPluginForUri(timeAfter, uri) {
if (typeof timeAfter !== 'number') {
return;
}

var i = 0,
plugin, match;

const domain = getDomainForPlugin(uri);

while (i < pluginsListSortedByModifiedTime.length
&& (plugin = pluginsListSortedByModifiedTime[i])
// Dot not check next plugins if older, because they are all older (SortedByModifiedTime desc).
&& plugin.modifiedWithMixins > timeAfter
// Find first plugin match (newest).
&& !(match = plugin.pluginMatchesUrl(domain, uri))) {

i++;
}

// Return plugin if match found.
return match && plugin;
};

function pluginMatchesUrl(domain, uri) {
const plugin = this;
if (plugin.domain) {

// Match only by regexp. Used in specific cases where domain changes (like national domain).

var match = null, j = 0, res = plugin.re;
while (!match && j < res.length) {
match = uri.match(res[j]);
j++;
}
if (match) {
// Store match for plugin.
return match;
} else if (res.length) {
// Skip plugin with unmatched re.
return;
}

// Straight match by domain.

// Positive match on plugin.domain="domain.com", domain="sub.domain.com"
// Positive match on plugin.domain="domain.com", domain="domain.com"
var idx = domain.indexOf(plugin.domain);

if (idx === -1 || ((idx > 0) && domain.charAt(idx - 1) !== '.')) {
// Break if not found, or not dot separation.
return;
}

if (idx > 0) {
var subdomain = domain.substring(0, idx - 1);
if (subdomain === 'blog') {
// Skip "blog.*.*" blog page for domain plugin without re.
return;
}
}

var match = (idx + plugin.domain.length) === domain.length;

if (match) {
// TODO: use `true` as `null`.
return true;
}
}
}

async function scanModulesForPlugins() {

// Scan node_modules dir.
Expand Down Expand Up @@ -513,6 +607,8 @@
}
}

fillModifiedDate();

console.log('Iframely plugins loaded:')
console.log(' - custom domains:', pluginsList.filter(function(p) { return p.domain; }).length);
console.log(' - generic & meta:', pluginsList.filter(function(p) { return !p.domain && !p.custom; }).length);
Expand Down
5 changes: 3 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@
result += new Date(whitelistRecord.date).getTime();
}

var plugin = pluginLoader.findDomainPlugin(uri);
// `0` to search all plugins.
var plugin = pluginLoader.hasNewPluginForUri(0, uri);
if (plugin) {
result += plugin.getPluginLastModifiedDate().getTime();
result += plugin.modifiedWithMixins.getTime();
}

if (result) {
Expand Down

0 comments on commit 639d04a

Please sign in to comment.