Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #69 from Jason-Abbott/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Jason-Abbott committed Apr 12, 2017
2 parents 0777ce2 + df9afe2 commit 26c4087
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 129 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"cspotcode.vscode-mocha-latte",
"codezombiech.gitignore",
"pranaygp.vscode-css-peek",
"joelday.docthis",
"ziyasal.vscode-open-in-github",
"donjayamanne.githistory",
"anseki.vscode-color",
Expand Down
7 changes: 1 addition & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
"files.associations": {
"procfile": "yaml"
},
"files.exclude": {
".idea/": true
},

"mocha.env": {
"FLICKR_API_KEY": "${env.FLICKR_API_KEY}",
Expand All @@ -24,7 +21,5 @@
"mocha.files.glob": "test/**/*.test.js",
"mocha.options": {
"ui": "bdd"
},

"npm-intellisense.scanDevDependencies": true
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.3
- Update dependencies
- Use memory instead of Redis to cache views

## 2.2.2
- Fix missing JSON-LD @type for blog entries

Expand Down
75 changes: 74 additions & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const compress = require('zlib');
const prefix = 'api:';
const viewKey = 'view';
const mapKey = 'map';
/**
* @type {object.<ViewCacheItem>}
*/
const memory = {};

/**
* Whether key with prefix exists
Expand Down Expand Up @@ -104,9 +108,78 @@ module.exports = {
),

/**
* Cache rendered views
* Cache rendered views in memory
*/
view: {
/**
* @param {string} key Page slug
* @returns {Promise.<ViewCacheItem>}
*/
getItem: key => Promise.resolve(memory[key]),

/**
* @returns {Promise.<string[]>}
*/
keys: ()=> Promise.resolve(Object.keys(memory)),

/**
* Add or replace value at key
* @param {string} key Page slug
* @param {Buffer|string} text HTML or JSON
* @returns {Promise.<ViewCacheItem>}
*/
add: (key, text) => createItem(key, text).then(item => {
if (config.cache.views) { memory[key] = item; }
return Promise.resolve(item);
}),

create: createItem,

/**
* Whether cache view exists
* @param {string} key
* @returns {Promise.<boolean>}
*/
exists: key => Promise.resolve(is.defined(memory, key)),

/**
* Add value only if it doesn't already exist (mainly for testing)
* @param {string} key Page slug
* @param {Buffer|string} buffer Zipped view content
* @returns {Promise}
*/
addIfMissing(key, buffer) {
return (config.cache.views)
? this.exists(key).then(exists => exists ? Promise.resolve() : this.add(key, buffer))
: Promise.resolve();
},

/**
* Remove cached page views
* @param {string|string[]} keys
* @returns {Promise}
*/
remove: keys => {
if (is.array(keys)) {
keys.forEach(k => delete memory[k]);
} else {
delete memory[keys];
}
return Promise.resolve();
},

/**
* In-memory cache doesn't need to serialize the page buffer
* @param {ViewCacheItem} item
* @returns {object}
*/
serialize: item => item
},

/**
* Cache rendered views in Redis
*/
redisView: {
/**
* @param {string} key Page slug
* @returns {Promise.<ViewCacheItem>}
Expand Down
8 changes: 6 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ module.exports = {
UNAVAILABLE: 503
},
alphabet: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
// provider names used internally by Winston
/**
* Provider names used internally by Winston
*/
logTo: {
REDIS: 'redis',
CONSOLE: 'console',
FILE: 'file'
},
// route placeholders that become req.params values
/**
* Route placeholders that become req.params values
*/
route: {
CATEGORY: 'category',
MONTH: 'month',
Expand Down
19 changes: 16 additions & 3 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const menuKeys = [
template.page.SITEMAP
];

//= Map =======================================================================

// can be replaced with injection
let google = require('./google');

Expand Down Expand Up @@ -82,6 +84,8 @@ function mapGPX(req, res) {
}
}

//= Post ======================================================================

/**
* @param {BlogResponse} res
* @param {string} key Post key
Expand Down Expand Up @@ -135,8 +139,7 @@ function postWithID(req, res) {
*/
function latestPost(req, res) { postView(res, library.posts[0].key); }

//endregion
//region Photos
//= Photo =====================================================================

/**
* Small HTML table of EXIF values for given photo
Expand Down Expand Up @@ -227,6 +230,8 @@ function normalizeTag(slug) {
return (is.defined(config.photoTagChanges, slug)) ? config.photoTagChanges[slug] : slug;
}

//= Category ==================================================================

/**
* @param res
* @param {String} path
Expand Down Expand Up @@ -333,6 +338,8 @@ function renderCategory(render, template, category, linkData, options, childCoun
}));
}

//= Menu ======================================================================

// https://npmjs.org/package/uglify-js
const uglify = require('uglify-js');

Expand All @@ -359,6 +366,8 @@ function mobileMenu(req, res) {
});
}

//= Auth ======================================================================

/**
* @see https://github.com/google/google-api-nodejs-client/#generating-an-authentication-url
*/
Expand Down Expand Up @@ -433,6 +442,8 @@ function googleAuth(req, res) {
}
}

//= Admin =====================================================================

/**
* @param res
* @param {string[]} viewKeys
Expand Down Expand Up @@ -682,6 +693,8 @@ function deleteJsonCache(req, res) {
// return p;
// }

//= RSS =======================================================================

const MAX_RSS_RETRIES = 10;
let rssRetries = 0;

Expand Down Expand Up @@ -725,7 +738,7 @@ function rssFeed(req, res) {
});
}
res.set('Content-Type', C.mimeType.XML);
res.send(feed.render('rss-2.0'));
res.send(feed.rss2());
}

module.exports = {
Expand Down
38 changes: 15 additions & 23 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const library = require('./library');
let flickr = require('./flickr');
let google = require('./google');

//region Library
//= Library ===================================================================

/**
* @param {Boolean} [emptyIfLoaded]
* @return {Promise.<Library>} Resolve with list of changed post keys
* @param {boolean} [emptyIfLoaded]
* @returns {Promise.<Library>} Resolve with list of changed post keys
*/
function buildLibrary(emptyIfLoaded = true) {
// store existing post keys to compute changes
Expand Down Expand Up @@ -76,7 +76,7 @@ function buildLibrary(emptyIfLoaded = true) {

/**
* @this {Library}
* @param {Photo|String} photo
* @param {Photo|string} photo
* @returns {Promise}
*/
function getPostWithPhoto(photo) {
Expand Down Expand Up @@ -140,14 +140,13 @@ function correlatePosts() {
}
}

//endregion
//region Categories
//= Category ==================================================================

/**
* Add Flickr collection to library singleton as category
* @param {Flickr.Collection} collection
* @param {Boolean} root Whether a root level collection
* @returns {Category|Object}
* @param {boolean} root Whether a root level collection
* @returns {Category|object}
*/
function buildCategory(collection, root = false) {
let exclude = config.flickr.excludeSets;
Expand Down Expand Up @@ -207,14 +206,14 @@ function buildCategory(collection, root = false) {
}

/**
* @param {String} key
* @param {string} key
* @this {Category} category
* @returns {Category}
*/
function getSubcategory(key) { return this.subcategories.find(c => c.title === key || c.key === key); }

/**
* @param {String} key
* @param {string} key
* @this {Category} category
* @returns {Boolean}
*/
Expand Down Expand Up @@ -262,8 +261,7 @@ function ensureCategoryLoaded() {
return Promise.all(this.posts.map(p => p.getInfo().then(p => p.getPhotos())));
}

//endregion
//region Posts
//= Posts =====================================================================

/**
* Create post from Flickr photo set
Expand Down Expand Up @@ -456,8 +454,7 @@ function postName() {
return p.title + ((p.isPartial) ? config.library.subtitleSeparator + ' ' + p.subTitle : '');
}

//endregion
//region Photos
//= Photos ====================================================================

/**
* Load photos for post and calculate summaries
Expand Down Expand Up @@ -675,8 +672,7 @@ function serializePhotoCoordinates() {
this.photoCoordinates = (is.empty(map)) ? null : encodeURIComponent('size:tiny' + map);
}

// endregion
// region EXIF
//= EXIF ======================================================================

/**
* @param {String} photoID
Expand Down Expand Up @@ -765,8 +761,7 @@ function sanitizeExif(exif) {
return exif;
}

// endregion
// region Tracks and Waypoints
//= Tracks and Waypoints ======================================================

const cache = require('./cache');

Expand Down Expand Up @@ -835,13 +830,12 @@ function addPhotoFeature(post, geo, resolve) {
});
}

//endregion
//region Video
//= Video =====================================================================

/**
* Get video ID and dimensions
* @param {Flickr.SetInfo} setInfo
* @returns {Object}
* @returns {object}
*/
function buildVideoInfo(setInfo) {
const d = setInfo.description._content;
Expand All @@ -861,8 +855,6 @@ function buildVideoInfo(setInfo) {
}
}

//endregion

module.exports = {
buildLibrary,
map: {
Expand Down
13 changes: 6 additions & 7 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const config = require('./config');
const C = require('./constants');
const c = require('./controller');
const library = require('./library');
// route placeholders
/**
* Route placeholders
* @type {object}
*/
const ph = C.route;

//region Sub-routes

/**
* Need to capture top-level route parameters
* @see http://expressjs.com/en/4x/api.html#express.router
Expand All @@ -25,7 +26,7 @@ function adminRoutes() {
}

/**
* @param {String} photoID Pattern
* @param {string} photoID Pattern
* @returns {core.Router}
*/
function postRoutes(photoID) {
Expand All @@ -41,7 +42,7 @@ function postRoutes(photoID) {

/**
* Series should load the PDF, GPX and GeoJSON for the main post
* @param {String} photoID Pattern
* @param {string} photoID Pattern
* @returns {core.Router}
*/
function seriesRoutes(photoID) {
Expand All @@ -67,8 +68,6 @@ function categoryRoutes() {
return r;
}

//endregion

/**
* @param app Express instance
* @see http://expressjs.com/en/4x/api.html
Expand Down
Loading

0 comments on commit 26c4087

Please sign in to comment.