feeles-ide
Version:
The hackable and serializable IDE to make learning material
67 lines (62 loc) โข 1.99 kB
JavaScript
const redis = require('redis');
const promisify = require('es6-promisify');
// RedisClient instance and Promised API
const client = redis.createClient({
url: process.env.REDIS_URL,
retry_strategy(options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
} else {
console.log('๐จ', options.error);
}
}
});
const get = client && promisify(client.get, client);
const set = client && promisify(client.set, client);
// currentVersion ใฎๅๆๅคใฏ redis ใใๅๅพใใ
// ๅๆๅคใๅๅพใงใใ๏ผใใใใฏๅคฑๆใใ๏ผใพใงใฏ <pending>
const VERSION = 'version';
let currentVersion = get(VERSION).catch(err => '');
// version ใ 1 ใใใใ
// e.g. 'v1001' => 'v1002'
const advance = version => {
if (!version) return 'v1001';
const n = version.substr(1) >> 0;
return `v${n + 1}`;
};
// CDN ใฎใใฌใใฃใใฏใน
const endpoint = 'https://assets.feeles.com/public';
// Interface
module.exports = {
// ็พๅจใฎใใผใธใงใณ
async currentVersion() {
return (await currentVersion) || '';
},
// ็พๅจใใ 1 ใใใใ ใใผใธใงใณ
async nextVersion() {
return advance(await currentVersion);
},
// ็พๅจใฎใใผใธใงใณใๆไพใใ CDN URL
async currentUrl(pathname = '') {
return (
endpoint +
require('path').join('/', await this.currentVersion(), pathname)
);
},
// ็พๅจใใ 1 ใใใใ ใใผใธใงใณใๆไพใใ CDN URL
async nextUrl(pathname = '') {
return (
endpoint + require('path').join('/', await this.nextVersion(), pathname)
);
},
// ็พๅจใฎใใผใธใงใณใ 1 ใใใใ
async advance() {
// currentVersion ใซใฏใใชใใใฃใใชๅคใๅ
ฅใ
currentVersion = advance(await currentVersion);
await set(VERSION, currentVersion);
return currentVersion;
},
quit() {
client.quit();
}
};