Skip to content

Commit

Permalink
fixed leaked emitters waits on undefined font subset, rm error throw …
Browse files Browse the repository at this point in the history
…on undefined variants 404 instead as specified
  • Loading branch information
majodev committed Feb 3, 2023
1 parent 6aff3dd commit 2758ced
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
71 changes: 49 additions & 22 deletions server/logic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ var subsetStore = {}; // every item in here holds a urlStore Object + a unique s
// fileStore holds arrays of local paths to font files, id = fontItem.id + "-" + fontItem.storeID
var fileStore = {};

if (debug.enabled) {
setInterval(function () {
var events = emitter.eventNames();
var listenersCount = _.reduce(_.map(events, function (event) {
return emitter.listenerCount(event);
}), function (sum, n) {
return sum + n;
}, 0);
// console.log("tracked events: ", events);
debug("core emitter ops tracked_events=" + events.length + " listeners=" + listenersCount + " eventNames=" + events);
}, 10000)
}

// -----------------------------------------------------------------------------
// Private
Expand Down Expand Up @@ -64,14 +76,16 @@ function getUrlStoreKey(font, subsetArr) {
fontSubsetKey = _.findKey(fontSubsetStore, {
subsetMap: getFilterObject(font, subsetArr)
});
debug(fontSubsetKey);
debug("fontSubsetKey for " + font.id, fontSubsetKey);
if (_.isUndefined(fontSubsetKey) === false) {
return fontSubsetKey;
} else {
throw new Error("fontSubsetKey for " + font.id + " subset " + subsetArr + " not found!");
debug("fontSubsetKey for " + font.id + " subset " + subsetArr + " not found!");
return null;
}
} else {
throw new Error("fontSubsetStore for " + font.id + " not found!");
debug("fontSubsetStore for " + font.id + " not found!");
return null;
}
}

Expand All @@ -80,20 +94,33 @@ function getFontItem(font, subsetArr, callback) {

// find the relevant subsetStore Object that holds the needed unique urlStore to fetch
var subsetStoreKey = getUrlStoreKey(font, subsetArr);

if (subsetStoreKey === null) {
callback(null); // bailout.
return;
}

var urlStore = subsetStore[font.id][subsetStoreKey];

debug(urlStore);
debug("urlStore for font.id=" + font.id + " subsetStoreKey=" + subsetStoreKey, urlStore);

if (_.isUndefined(urlStore.variants) === false) {
debug(urlStore);
if (urlStore.isDirty !== true) {
// already cached, return instantly
debug("already cached!");
debug("urlStore for font.id=" + font.id + " subsetStoreKey=" + subsetStoreKey, "already cached!");
callback(_.merge(_.cloneDeep(font), urlStore));
} else {
// process to cache has already begun, wait until it has finished...
debug("waiting until cache...");
emitter.once(font.id + "-pathFetched-" + urlStore.storeID, function (fontItem) {
debug("urlStore for font.id=" + font.id + " subsetStoreKey=" + subsetStoreKey, "waiting until cache...");

// // DEBUG ONLY exit now
// // TODO delete me
// // !!!!!!!!!!!!!!!!!!!!!!!!
// if (!urlStore.storeID) {
// console.error("urlStore.storeID is undefined for font " + font.id + " subset " + subsetStoreKey);
// process.exit(1);
// }
emitter.once(font.id + "-pathFetched-" + subsetStoreKey, function (fontItem) {
callback(fontItem);
});
}
Expand All @@ -106,7 +133,7 @@ function getFontItem(font, subsetArr, callback) {
urlStore.variants = [];
urlStore.isDirty = true;

debug(subsetStore);
// debug(subsetStore);

// Fetch fontItem for the first time...
urlFetcher(font, subsetStoreKey, function (urlStoreObject) {
Expand All @@ -115,11 +142,11 @@ function getFontItem(font, subsetArr, callback) {
console.error('urlStoreObject resolved null for font ' + font.id + ' subset ' + subsetStoreKey);
urlStore.variants = undefined;
callback(null);
emitter.emit(font.id + "-pathFetched-" + urlStore.storeID, null);
emitter.emit(font.id + "-pathFetched-" + subsetStoreKey, null);
return;
}

debug("fetched fontItem", urlStoreObject);
debug("fetched fontItem for font.id=" + font.id + " subsetStoreKey=" + subsetStoreKey, urlStoreObject);

var fontItem;

Expand All @@ -133,18 +160,18 @@ function getFontItem(font, subsetArr, callback) {
// set and build up a proper fontItem
fontItem = _.merge(_.cloneDeep(font), subsetStore[font.id][subsetStoreKey]);

debug("saveable fontimte processed", fontItem);
debug("saveable fontItem processed for font.id=" + font.id + " subsetStoreKey=" + subsetStoreKey, "fontItem=", fontItem);

// fullfill the original request
callback(fontItem);

// fullfill still pending requests awaiting process completion
emitter.emit(font.id + "-pathFetched-" + urlStoreObject.storeID, fontItem);
emitter.emit(font.id + "-pathFetched-" + subsetStoreKey, fontItem);

// trigger obviating downloading of font files (even tough it's might not needed!)
getFontFiles(fontItem, null);

debug(urlStore);
// debug(urlStore);

});
}
Expand All @@ -166,13 +193,13 @@ function getFontFiles(fontItem, cb) {
} else {
// process has already begun, wait until it has finished...
emitter.once(fontItem.id + "-filesFetched-" + fontItem.storeID, function (fileStoreItem) {
debug("Download: fulfilling pending download request...");
debug(fileStoreID + " download: fulfilling pending download request...");
// callback (if null, it's only obviating)
if (_.isFunction(cb) === true) {
// fullfill the original request
cb(fileStoreItem);
} else {
debug("fulfill fail no callback!");
debug(fileStoreID + " fulfill fail no callback!");
// nothing needs to be done, no callback (obviating)!
}
});
Expand All @@ -198,10 +225,10 @@ function getFontFiles(fontItem, cb) {
// callback (if null, it's only obviating)
if (_.isFunction(cb) === true) {
// fullfill the original request
debug("Download: fulfill original request...");
debug(fileStoreID + " download: fulfill original request...");
cb(fileStore[fileStoreID]);
} else {
debug("obsiation, no callback!");
debug(fileStoreID + " obsiation, no callback!");
}

// fullfill still pending requests awaiting process completion
Expand Down Expand Up @@ -290,7 +317,7 @@ module.exports.get = function get(id, subsetArr, callback) {
getFontItem(font, subsetArr, function (fontItem) {

if (fontItem === null) {
console.error("font loading failed for id: " + id + " subsetArr: " + subsetArr);
debug("font loading failed for id: " + id + " subsetArr: " + subsetArr);
callback(null);
return;
}
Expand All @@ -299,7 +326,7 @@ module.exports.get = function get(id, subsetArr, callback) {
});
} else {
// font not found!
console.error("font not found: " + id);
debug("font not found: " + id);
callback(null);
}

Expand All @@ -315,7 +342,7 @@ module.exports.getDownload = function getDownload(id, subsetArr, variantsArr, fo
getFontItem(font, subsetArr, function (fontItem) {

if (fontItem === null) {
console.error("font loading failed for id: " + id + " subsetArr: " + subsetArr + " variantsArr " + variantsArr + " formatsArr" + formatsArr);
debug("font loading failed for id: " + id + " subsetArr: " + subsetArr + " variantsArr " + variantsArr + " formatsArr" + formatsArr);
callback(null);
return;
}
Expand Down Expand Up @@ -347,7 +374,7 @@ module.exports.getDownload = function getDownload(id, subsetArr, variantsArr, fo
});
} else {
// font not found!
console.error("font not found: " + id);
debug("font not found: " + id);
callback(null);
}

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ camelcase@^2.0.0:
integrity sha512-DLIsRzJVBQu72meAKPkWQOLcujdXT32hwdfnkI1frSiSRMK1MofjKHf+MEx0SB6fjEFXL8fBDv1dKymBlOp4Qw==

"caniuse-db@1.0.20140618 - 2":
version "1.0.30001449"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001449.tgz#d07cc14cac52de0945562f563c2a1fae7fd78b63"
integrity sha512-K7XIAmo8tCt2W7LFSrTjbrXZqer/4af8cfediBT+iDk4b8eljuMo6gBzJlW7e4RTz5+wYEGKg8gXk1Vm+Jh+YA==
version "1.0.30001450"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001450.tgz#51ea1e1eb6688d391189a46b7d962854035d3ff5"
integrity sha512-HqXvh7yrdHTNLVaXu9t/8lkG49xXe97jD6zWvbnT6aQBGPl1I5ukZbSAlOLjJ6/hV8BduRMl1eR6ggNi/O0hTA==

capture-stack-trace@^1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -836,9 +836,9 @@ content-disposition@0.5.4:
safe-buffer "5.2.1"

content-type@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"
integrity sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==
version "1.0.5"
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.5.tgz#8b773162656d1d1086784c8f23a54ce6d73d7918"
integrity sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==

convert-source-map@^1.1.1:
version "1.9.0"
Expand Down

0 comments on commit 2758ced

Please sign in to comment.