Created
June 17, 2019 12:54
-
-
Save shamoons/4bf9e78cd92624bcb120644fb995454a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# freeCodeCamp/freeCodeCamp | |
# client/utils/blockNameify.js | |
const preFormattedBlockNames = { | |
'api-projects': 'API Projects', | |
'basic-css': 'Basic CSS', | |
'basic-html-and-html5': 'Basic HTML and HTML5', | |
'css-flexbox': 'CSS Flexbox', | |
'css-grid': 'CSS Grid', | |
devops: 'DevOps', | |
es6: 'ES6', | |
'information-security-with-helmetjs': 'Information Security with HelmetJS', | |
jquery: 'jQuery', | |
'json-apis-and-ajax': 'JSON APIs and Ajax', | |
'mongodb-and-mongoose': 'MongoDB and Mongoose', | |
'the-dom': 'The DOM', | |
'apis-and-microservices': 'APIs and Microservices', | |
'apis-and-microservices-projects': 'APIs and Microservices Projects' | |
}; | |
const noFormatting = ['and', 'for', 'of', 'the', 'up', 'with']; | |
exports.blockNameify = function blockNameify(phrase) { | |
const preFormatted = preFormattedBlockNames[phrase] || ''; | |
if (preFormatted) { | |
return preFormatted; | |
} | |
return phrase | |
.split('-') | |
.map(word => { | |
if (noFormatting.indexOf(word) !== -1) { | |
return word; | |
} | |
if (word === 'javascript') { | |
return 'JavaScript'; | |
} | |
return word.charAt(0).toUpperCase() + word.slice(1); | |
}) | |
.join(' '); | |
};<eos> | |
# freeCodeCamp/freeCodeCamp | |
# api-server/.babelrc.js | |
module.exports = { | |
plugins: [ | |
require.resolve('babel-plugin-transform-function-bind'), | |
require.resolve('@babel/plugin-proposal-class-properties'), | |
require.resolve('@babel/plugin-proposal-object-rest-spread'), | |
], | |
presets: [ | |
[ | |
require.resolve('@babel/preset-env'), { | |
targets: { | |
node: '10', | |
}, | |
}, | |
], | |
], | |
};<eos> | |
# freeCodeCamp/freeCodeCamp | |
# api-server/server/middlewares/error-reporter.js | |
import debug from 'debug'; | |
import Rollbar from 'rollbar'; | |
import { | |
isHandledError, | |
unwrapHandledError | |
} from '../utils/create-handled-error.js'; | |
import { | |
rollbar | |
} from '../../../config/secrets'; | |
const { | |
appId | |
} = rollbar; | |
const reporter = new Rollbar(appId); | |
const log = debug('fcc:middlewares:error-reporter'); | |
const errTemplate = (error, req) => { | |
const { | |
message, | |
stack | |
} = error; | |
return ` | |
Time: ${new Date(Date.now()).toISOString()} | |
Error: ${message} | |
Is authenticated user: ${!!req.user} | |
Route: ${JSON.stringify(req.route, null, 2)} | |
Stack: ${stack} | |
${JSON.stringify(error, null, 2)} | |
`; | |
}; | |
export function reportError(err) { | |
return process.env.NODE_ENV === 'production' ? | |
reporter.error(err.message, err) : | |
console.error(err); | |
} | |
export default function errrorReporter() { | |
if (process.env.NODE_ENV !== 'production' && process.env.ERROR_REPORTER) { | |
return (err, req, res, next) => { | |
console.error(errTemplate(err, req)); | |
if (isHandledError(err)) { | |
const handled = unwrapHandledError(err); | |
log(handled.message); | |
} | |
next(err); | |
}; | |
} | |
return (err, req, res, next) => { | |
if (isHandledError(err) || err.statusCode || err.status) { | |
return next(err); | |
} | |
console.error(errTemplate(err, req)); | |
reportError(err); | |
return next(err); | |
}; | |
}<eos> | |
# freeCodeCamp/freeCodeCamp | |
# api-server/server/models/donation.js | |
import { | |
Observable | |
} from 'rx'; | |
import debug from 'debug'; | |
import { | |
reportError | |
} from '../middlewares/error-reporter'; | |
import InMemoryCache from '../utils/in-memory-cache'; | |
const log = debug('fcc:boot:donate'); | |
const fiveMinutes = 1000 * 60 * 5; | |
export default function(Donation) { | |
let activeDonationUpdateInterval = null; | |
const activeDonationCountCacheTTL = fiveMinutes; | |
const activeDonationCountCache = InMemoryCache(0, reportError); | |
const activeDonationsQuery$ = () => | |
Donation.find$({ | |
where: { | |
endDate: undefined | |
} | |
}).map(instances => instances.length); | |
function cleanUp() { | |
if (activeDonationUpdateInterval) { | |
clearInterval(activeDonationUpdateInterval); | |
} | |
return; | |
} | |
process.on('exit', cleanUp); | |
Donation.on('dataSourceAttached', () => { | |
Donation.find$ = Observable.fromNodeCallback(Donation.find.bind(Donation)); | |
Donation.findOne$ = Observable.fromNodeCallback( | |
Donation.findOne.bind(Donation) | |
); | |
seedTheCache() | |
.then(setupCacheUpdateInterval) | |
.catch(err => { | |
const errMsg = `Error caught seeding the cache: ${err.message}`; | |
err.message = errMsg; | |
reportError(err); | |
}); | |
}); | |
function seedTheCache() { | |
return new Promise((resolve, reject) => | |
Observable.defer(activeDonationsQuery$).subscribe(count => { | |
log('activeDonator count: %d', count); | |
activeDonationCountCache.update(() => count); | |
return resolve(); | |
}, reject) | |
); | |
} | |
function setupCacheUpdateInterval() { | |
activeDonationUpdateInterval = setInterval( | |
() => | |
Observable.defer(activeDonationsQuery$).subscribe( | |
count => { | |
log('activeDonator count: %d', count); | |
return activeDonationCountCache.update(() => count); | |
}, | |
err => { | |
const errMsg = `Error caught updating the cache: ${err.message}`; | |
err.message = errMsg; | |
reportError(err); | |
} | |
), | |
activeDonationCountCacheTTL | |
); | |
return null; | |
} | |
function getCurrentActiveDonationCount$() { | |
return Observable.of(activeDonationCountCache.get()); | |
} | |
Donation.getCurrentActiveDonationCount$ = getCurrentActiveDonationCount$; | |
}<eos> | |
# freeCodeCamp/freeCodeCamp | |
# api-server/server/utils/date-utils.test.js | |
import moment from 'moment-timezone'; | |
import { | |
dayCount | |
} from './date-utils'; | |
const PST = 'America/Los_Angeles'; | |
describe('date utils', () => { | |
describe('dayCount', () => { | |
it('should return 1 day given epochs of the same day', () => { | |
expect( | |
dayCount([ | |
moment.utc('8/3/2015 3:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(1); | |
}); | |
it('should return 1 day given same epochs', () => { | |
expect( | |
dayCount([ | |
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(1); | |
}); | |
it('should return 2 days when there is a 24 hours difference', () => { | |
expect( | |
dayCount([ | |
moment.utc('8/4/2015 2:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(2); | |
}); | |
it( | |
'should return 2 days when the diff is less than 24h but ' + | |
'different in UTC', | |
() => { | |
expect( | |
dayCount([ | |
moment.utc('8/4/2015 1:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 23:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(2); | |
} | |
); | |
it( | |
'should return 1 day when the diff is less than 24h ' + | |
'and days are different in UTC, but given PST', | |
() => { | |
expect( | |
dayCount( | |
[ | |
moment.utc('8/4/2015 1:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 23:00', 'M/D/YYYY H:mm').valueOf() | |
], | |
PST | |
) | |
).toEqual(1); | |
} | |
); | |
it('should return correct count when there is very big period', () => { | |
expect( | |
dayCount([ | |
moment.utc('10/27/2015 1:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('5/12/1982 1:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(12222); | |
}); | |
it( | |
'should return 2 days when there is a 24 hours difference ' + | |
'between dates given `undefined` timezone', | |
() => { | |
expect( | |
dayCount([ | |
moment.utc('8/4/2015 2:00', 'M/D/YYYY H:mm').valueOf(), | |
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() | |
]) | |
).toEqual(2); | |
} | |
); | |
}); | |
});<eos> | |
# freeCodeCamp/freeCodeCamp | |
# api-server/server/utils/in-memory-cache.js | |
function isPromiseLike(thing) { | |
return !!thing && typeof thing.then === 'function'; | |
} | |
function InMemoryCache(initialValue, reportError) { | |
if (typeof reportError !== 'function') { | |
throw new Error( | |
'No reportError function specified for this in-memory-cache' | |
); | |
} | |
const cacheKey = Symbol('cacheKey'); | |
const cache = new Map(); | |
cache.set(cacheKey, initialValue); | |
return { | |
get() { | |
const value = cache.get(cacheKey); | |
return typeof value !== 'undefined' ? value : null; | |
}, | |
update(fn) { | |
try { | |
const value = fn(); | |
if (isPromiseLike(value)) { | |
return value.then(value => cache.set(cacheKey, value)); | |
} else { | |
cache.set(cacheKey, value); | |
} | |
} catch (e) { | |
const errMsg = `InMemoryCache > update > caught: ${e.message}`; | |
e.message = errMsg; | |
reportError(e); | |
} | |
return null; | |
}, | |
clear() { | |
return cache.delete(cacheKey); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment