Moved utils constants to lib/constants

refs #9178
This commit is contained in:
kirrg001 2017-12-14 14:13:40 +01:00
parent c5169e23c4
commit a3091a3012
32 changed files with 120 additions and 113 deletions

View File

@ -10,7 +10,7 @@ var serveStatic = require('express').static,
moment = require('moment'),
config = require('../../config'),
common = require('../../lib/common'),
globalUtils = require('../../utils'),
constants = require('../../lib/constants'),
urlService = require('../../services/url'),
StorageBase = require('ghost-storage-base');
@ -85,7 +85,7 @@ class LocalFileStore extends StorageBase {
return serveStatic(
self.storagePath,
{
maxAge: globalUtils.ONE_YEAR_MS,
maxAge: constants.ONE_YEAR_MS,
fallthrough: false,
onEnd: function onEnd() {
common.logging.info('LocalFileStorage.serve', req.path, moment().diff(startedAtMoment, 'ms') + 'ms');

View File

@ -1,15 +1,15 @@
var Promise = require('bluebird'),
_ = require('lodash'),
validator = require('validator'),
pipeline = require('../lib/promise/pipeline'),
mail = require('../services/mail'),
globalUtils = require('../utils'),
urlService = require('../services/url'),
localUtils = require('./utils'),
models = require('../models'),
config = require('../config'),
common = require('../lib/common'),
security = require('../lib/security'),
constants = require('../lib/constants'),
pipeline = require('../lib/promise/pipeline'),
mail = require('../services/mail'),
urlService = require('../services/url'),
localUtils = require('./utils'),
models = require('../models'),
spamPrevention = require('../web/middleware/api/spam-prevention'),
mailAPI = require('./mail'),
settingsAPI = require('./settings'),
@ -169,7 +169,7 @@ authentication = {
}
token = security.tokens.resetToken.generateHash({
expires: Date.now() + globalUtils.ONE_DAY_MS,
expires: Date.now() + constants.ONE_DAY_MS,
email: email,
dbHash: dbHash,
password: user.get('password')

View File

@ -39,7 +39,7 @@ function getBaseConfig() {
*
* We need to load the client credentials dynamically.
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
configuration = {

View File

@ -18,7 +18,7 @@ var Promise = require('bluebird'),
/**
* ## DB API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
db = {
/**

View File

@ -44,7 +44,7 @@ function sendMail(object) {
/**
* ## Mail API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
* @typedef Mail
* @param mail
*/

View File

@ -16,7 +16,7 @@ var Promise = require('bluebird'),
/**
* ## Notification API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
notifications = {

View File

@ -16,7 +16,7 @@ var Promise = require('bluebird'),
/**
* ### Posts API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
posts = {

View File

@ -12,7 +12,7 @@ var Promise = require('bluebird'),
/**
* ## Roles API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
roles = {
/**

View File

@ -117,7 +117,7 @@ canEditAllSettings = function (settingsInfo, options) {
/**
* ## Settings API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
settings = {

View File

@ -7,7 +7,7 @@ var Promise = require('bluebird'),
/**
* ## Slack API Method
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
* @typedef Slack
* @param slack
*/

View File

@ -12,7 +12,7 @@ var Promise = require('bluebird'),
/**
* ## Slugs API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
slugs = {

View File

@ -14,7 +14,7 @@ var Promise = require('bluebird'),
/**
* ### Subscribers API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
subscribers = {
/**

View File

@ -13,7 +13,7 @@ var Promise = require('bluebird'),
/**
* ### Tags API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
tags = {
/**

View File

@ -14,7 +14,7 @@ var debug = require('ghost-ignition').debug('api:themes'),
/**
* ## Themes API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
themes = {
/**

View File

@ -6,7 +6,7 @@ var Promise = require('bluebird'),
/**
* ## Upload API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
upload = {

View File

@ -15,7 +15,7 @@ var Promise = require('bluebird'),
/**
* ### Users API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
users = {
/**

View File

@ -55,7 +55,7 @@ function makeRequests(webhooksCollection, payload, options) {
/**
* ## Webhook API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* **See:** [API Methods](constants.js.html#api%20methods)
*/
webhooks = {

View File

@ -4,7 +4,7 @@ var fs = require('fs-extra'),
path = require('path'),
config = require('../../../config'),
urlService = require('../../../services/url'),
globalUtils = require('../../../utils'),
constants = require('../../../lib/constants'),
common = require('../../../lib/common'),
settingsCache = require('../../../services/settings/cache'),
privateRoute = '/' + config.get('routeKeywords').private + '/',
@ -32,7 +32,7 @@ privateBlogging = {
res.isPrivateBlog = true;
return session({
maxAge: globalUtils.ONE_MONTH_MS,
maxAge: constants.ONE_MONTH_MS,
signed: false
})(req, res, next);
},

View File

@ -0,0 +1,14 @@
module.exports = {
ONE_HOUR_S: 3600,
ONE_DAY_S: 86400,
ONE_MONTH_S: 2628000,
SIX_MONTH_S: 15768000,
ONE_YEAR_S: 31536000,
FIVE_MINUTES_MS: 300000,
ONE_HOUR_MS: 3600000,
ONE_DAY_MS: 86400000,
ONE_WEEK_MS: 604800000,
ONE_MONTH_MS: 2628000000,
SIX_MONTH_MS: 15768000000,
ONE_YEAR_MS: 31536000000
};

View File

@ -1,8 +1,11 @@
var ghostBookshelf = require('./base'),
globalUtils = require('../utils'),
crypto = require('crypto'),
'use strict';
const crypto = require('crypto'),
_ = require('lodash'),
Invite,
constants = require('../lib/constants'),
ghostBookshelf = require('./base');
let Invite,
Invites;
Invite = ghostBookshelf.Model.extend({
@ -49,7 +52,7 @@ Invite = ghostBookshelf.Model.extend({
options = this.filterOptions(options, 'add');
options.withRelated = _.union(options.withRelated, options.include);
data.expires = Date.now() + globalUtils.ONE_WEEK_MS;
data.expires = Date.now() + constants.ONE_WEEK_MS;
data.status = 'pending';
// @TODO: call a util fn?

View File

@ -3,7 +3,7 @@ var Promise = require('bluebird'),
debug = require('ghost-ignition').debug('auth:utils'),
models = require('../../models'),
security = require('../../lib/security'),
globalUtils = require('../../utils'),
constants = require('../../lib/constants'),
knex = require('../../data/db').knex,
_private = {};
@ -27,7 +27,7 @@ _private.decreaseOldAccessTokenExpiry = function decreaseOldAccessTokenExpiry(da
}
return models.Accesstoken.edit({
expires: Date.now() + globalUtils.FIVE_MINUTES_MS
expires: Date.now() + constants.FIVE_MINUTES_MS
}, _.merge({id: oldAccessToken.id}, options));
});
};
@ -56,8 +56,8 @@ _private.handleTokenCreation = function handleTokenCreation(data, options) {
oldRefreshId = data.oldRefreshId,
newAccessToken = security.identifier.uid(191),
newRefreshToken = security.identifier.uid(191),
accessExpires = Date.now() + globalUtils.ONE_MONTH_MS,
refreshExpires = Date.now() + globalUtils.SIX_MONTH_MS,
accessExpires = Date.now() + constants.ONE_MONTH_MS,
refreshExpires = Date.now() + constants.SIX_MONTH_MS,
clientId = data.clientId,
userId = data.userId;
@ -85,7 +85,7 @@ _private.handleTokenCreation = function handleTokenCreation(data, options) {
return {
access_token: newAccessToken,
refresh_token: newRefreshToken,
expires_in: globalUtils.ONE_MONTH_S
expires_in: constants.ONE_MONTH_S
};
});
};

View File

@ -6,7 +6,7 @@ var fs = require('fs-extra'),
Promise = require('bluebird'),
config = require('../../config'),
security = require('../../lib/security'),
globalUtils = require('../../utils'),
zipFolder = require('../../utils/zip-folder'),
LocalFileStorage = require('../../adapters/storage/LocalFileStorage');
/**
@ -37,7 +37,7 @@ class ThemeStorage extends LocalFileStorage {
fs.ensureDir(zipBasePath)
.then(function () {
return Promise.promisify(globalUtils.zipFolder)(themePath, zipPath);
return Promise.promisify(zipFolder)(themePath, zipPath);
})
.then(function (length) {
res.set({

View File

@ -1,27 +1,2 @@
var utils;
utils = {
/**
* Timespans in seconds and milliseconds for better readability
*/
/* eslint-disable key-spacing */
ONE_HOUR_S: 3600,
ONE_DAY_S: 86400,
ONE_MONTH_S: 2628000,
SIX_MONTH_S: 15768000,
ONE_YEAR_S: 31536000,
FIVE_MINUTES_MS: 300000,
ONE_HOUR_MS: 3600000,
ONE_DAY_MS: 86400000,
ONE_WEEK_MS: 604800000,
ONE_MONTH_MS: 2628000000,
SIX_MONTH_MS: 15768000000,
ONE_YEAR_MS: 31536000000,
// eslint-enable key-spacing */
readCSV: require('./read-csv'),
zipFolder: require('./zip-folder'),
ghostVersion: require('./ghost-version')
};
module.exports = utils;
// here for mocking, will be moved soon
module.exports.readCSV = require('./read-csv');

View File

@ -3,7 +3,7 @@ var debug = require('ghost-ignition').debug('admin'),
// App requires
config = require('../../config'),
globalUtils = require('../../utils'),
constants = require('../../lib/constants'),
urlService = require('../../services/url'),
// Middleware
@ -35,7 +35,7 @@ module.exports = function setupAdminApp() {
configMaxAge = config.get('caching:admin:maxAge');
adminApp.use('/assets', serveStatic(
config.get('paths').clientAssets,
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : globalUtils.ONE_YEAR_MS, fallthrough: false}
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : constants.ONE_YEAR_MS, fallthrough: false}
));
// Service Worker for offline support

View File

@ -1,9 +1,9 @@
var _ = require('lodash'),
var _ = require('lodash'),
express = require('express'),
path = require('path'),
config = require('../../config'),
themeUtils = require('../../services/themes'),
utils = require('../../utils');
path = require('path'),
config = require('../../config'),
constants = require('../../lib/constants'),
themeUtils = require('../../services/themes');
function isBlackListedFileType(file) {
var blackListedFileTypes = ['.hbs', '.md', '.json'],
@ -25,7 +25,7 @@ function forwardToExpressStatic(req, res, next) {
var configMaxAge = config.get('caching:theme:maxAge');
express.static(themeUtils.getActive().path,
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : utils.ONE_YEAR_MS}
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : constants.ONE_YEAR_MS}
)(req, res, next);
}

View File

@ -4,7 +4,7 @@ var debug = require('ghost-ignition').debug('blog'),
// App requires
config = require('../../config'),
globalUtils = require('../../utils'),
constants = require('../../lib/constants'),
storage = require('../../adapters/storage'),
urlService = require('../../services/url'),
@ -52,18 +52,18 @@ module.exports = function setupSiteApp() {
// Favicon
siteApp.use(serveFavicon());
// /public/ghost-sdk.js
siteApp.use(servePublicFile('public/ghost-sdk.js', 'application/javascript', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost-sdk.min.js', 'application/javascript', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost-sdk.js', 'application/javascript', constants.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost-sdk.min.js', 'application/javascript', constants.ONE_HOUR_S));
// Serve sitemap.xsl file
siteApp.use(servePublicFile('sitemap.xsl', 'text/xsl', globalUtils.ONE_DAY_S));
siteApp.use(servePublicFile('sitemap.xsl', 'text/xsl', constants.ONE_DAY_S));
// Serve stylesheets for default templates
siteApp.use(servePublicFile('public/ghost.css', 'text/css', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost.min.css', 'text/css', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost.css', 'text/css', constants.ONE_HOUR_S));
siteApp.use(servePublicFile('public/ghost.min.css', 'text/css', constants.ONE_HOUR_S));
// Serve images for default templates
siteApp.use(servePublicFile('public/404-ghost@2x.png', 'png', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/404-ghost.png', 'png', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('public/404-ghost@2x.png', 'png', constants.ONE_HOUR_S));
siteApp.use(servePublicFile('public/404-ghost.png', 'png', constants.ONE_HOUR_S));
// Serve blog images using the storage adapter
siteApp.use('/' + urlService.utils.STATIC_IMAGE_URL_PREFIX, storage.getStorage().serve());
@ -87,7 +87,7 @@ module.exports = function setupSiteApp() {
debug('Static content done');
// Serve robots.txt if not found in theme
siteApp.use(servePublicFile('robots.txt', 'text/plain', globalUtils.ONE_HOUR_S));
siteApp.use(servePublicFile('robots.txt', 'text/plain', constants.ONE_HOUR_S));
// setup middleware for internal apps
// @TODO: refactor this to be a proper app middleware hook for internal & external apps

View File

@ -5,7 +5,7 @@ var should = require('should'),
// Stuff we are testing
exporter = require('../../server/data/export'),
globalUtils = require('../../server/utils'),
ghostVersion = require('../../server/utils/ghost-version'),
sandbox = sinon.sandbox.create();
@ -29,7 +29,7 @@ describe('Exporter', function () {
should.exist(exportData.meta);
should.exist(exportData.data);
exportData.meta.version.should.equal(globalUtils.ghostVersion.full);
exportData.meta.version.should.equal(ghostVersion.full);
_.each(tables, function (name) {
should.exist(exportData.data[name]);

View File

@ -2,7 +2,7 @@ var should = require('should'),
sinon = require('sinon'),
testUtils = require('../../utils'),
common = require('../../../server/lib/common'),
globalUtils = require('../../../server/utils'),
constants = require('../../../server/lib/constants'),
// Stuff we are testing
AccesstokenModel = require('../../../server/models/accesstoken').Accesstoken,
@ -30,7 +30,7 @@ describe('Accesstoken Model', function () {
token: 'foobartoken',
user_id: testUtils.DataGenerator.Content.users[0].id,
client_id: testUtils.DataGenerator.forKnex.clients[0].id,
expires: Date.now() + globalUtils.ONE_MONTH_MS
expires: Date.now() + constants.ONE_MONTH_MS
})
.then(function (token) {
should.exist(token);

View File

@ -7,8 +7,7 @@ var should = require('should'),
Models = require('../../../../server/models'),
common = require('../../../../server/lib/common'),
security = require('../../../../server/lib/security'),
urlService = require('../../../../server/services/url'),
globalUtils = require('../../../../server/utils'),
constants = require('../../../../server/lib/constants'),
sandbox = sinon.sandbox.create(),
@ -22,13 +21,13 @@ var should = require('should'),
user_id: 3,
token: 'valid-token',
client_id: 1,
expires: Date.now() + globalUtils.ONE_DAY_MS
expires: Date.now() + constants.ONE_DAY_MS
},
fakeInvalidToken = {
user_id: 3,
token: 'expired-token',
client_id: 1,
expires: Date.now() - globalUtils.ONE_DAY_MS
expires: Date.now() - constants.ONE_DAY_MS
};
describe('Auth Strategies', function () {

View File

@ -4,7 +4,7 @@ var should = require('should'),
_ = require('lodash'),
moment = require('moment-timezone'),
urlService = require('../../../server/services/url'),
globalUtils = require('../../../server/utils'),
constants = require('../../../server/lib/constants'),
settingsCache = require('../../../server/services/settings/cache'),
configUtils = require('../../utils/configUtils'),
testUtils = require('../../utils'),
@ -682,7 +682,7 @@ describe('Url', function () {
res.redirect = function (code, path) {
code.should.equal(301);
path.should.eql('my/awesome/path');
res.set.calledWith({'Cache-Control': 'public, max-age=' + globalUtils.ONE_YEAR_S}).should.be.true();
res.set.calledWith({'Cache-Control': 'public, max-age=' + constants.ONE_YEAR_S}).should.be.true();
done();
};
@ -698,7 +698,7 @@ describe('Url', function () {
res.redirect = function (code, path) {
code.should.equal(301);
path.should.eql('/ghost/#/my/awesome/path/');
res.set.calledWith({'Cache-Control': 'public, max-age=' + globalUtils.ONE_YEAR_S}).should.be.true();
res.set.calledWith({'Cache-Control': 'public, max-age=' + constants.ONE_YEAR_S}).should.be.true();
done();
};

View File

@ -2,7 +2,7 @@ var should = require('should'), // jshint ignore:line
path = require('path'),
fs = require('fs-extra'),
extract = require('extract-zip'),
utils = require('../../../server/utils');
zipFolder = require('../../../server/utils/zip-folder');
describe('Utils: zip-folder', function () {
const symlinkPath = path.join(__dirname, '..', '..', 'utils', 'fixtures', 'themes', 'theme-symlink'),
@ -25,7 +25,7 @@ describe('Utils: zip-folder', function () {
it('ensure symlinks work', function (done) {
fs.symlink(folderToSymlink, symlinkPath);
utils.zipFolder(symlinkPath, zipDestination, function (err) {
zipFolder(symlinkPath, zipDestination, function (err) {
if (err) {
return done(err);
}

View File

@ -1,8 +1,8 @@
var _ = require('lodash'),
uuid = require('uuid'),
ObjectId = require('bson-objectid'),
moment = require('moment'),
globalUtils = require('../../../server/utils'),
var _ = require('lodash'),
uuid = require('uuid'),
ObjectId = require('bson-objectid'),
moment = require('moment'),
constants = require('../../../server/lib/constants'),
DataGenerator = {};
DataGenerator.markdownToMobiledoc = function markdownToMobiledoc(content) {
@ -246,23 +246,23 @@ DataGenerator.Content = {
roles: [
{
id: ObjectId.generate(),
name: 'Administrator',
description: 'Administrators'
name: 'Administrator',
description: 'Administrators'
},
{
id: ObjectId.generate(),
name: 'Editor',
description: 'Editors'
name: 'Editor',
description: 'Editors'
},
{
id: ObjectId.generate(),
name: 'Author',
description: 'Authors'
name: 'Author',
description: 'Authors'
},
{
id: ObjectId.generate(),
name: 'Owner',
description: 'Blog Owner'
name: 'Owner',
description: 'Blog Owner'
}
],
@ -507,7 +507,7 @@ DataGenerator.forKnex = (function () {
id: ObjectId.generate(),
token: uuid.v4(),
client_id: clients[0].id,
expires: Date.now() + globalUtils.ONE_DAY_MS
expires: Date.now() + constants.ONE_DAY_MS
});
}
@ -591,10 +591,26 @@ DataGenerator.forKnex = (function () {
];
roles_users = [
{id: ObjectId.generate(), user_id: DataGenerator.Content.users[0].id, role_id: DataGenerator.Content.roles[3].id},
{id: ObjectId.generate(), user_id: DataGenerator.Content.users[1].id, role_id: DataGenerator.Content.roles[0].id},
{id: ObjectId.generate(), user_id: DataGenerator.Content.users[2].id, role_id: DataGenerator.Content.roles[1].id},
{id: ObjectId.generate(), user_id: DataGenerator.Content.users[3].id, role_id: DataGenerator.Content.roles[2].id}
{
id: ObjectId.generate(),
user_id: DataGenerator.Content.users[0].id,
role_id: DataGenerator.Content.roles[3].id
},
{
id: ObjectId.generate(),
user_id: DataGenerator.Content.users[1].id,
role_id: DataGenerator.Content.roles[0].id
},
{
id: ObjectId.generate(),
user_id: DataGenerator.Content.users[2].id,
role_id: DataGenerator.Content.roles[1].id
},
{
id: ObjectId.generate(),
user_id: DataGenerator.Content.users[3].id,
role_id: DataGenerator.Content.roles[2].id
}
];
// this is not pretty, but the fastest