path-white-list.js 1.88 KB

const _ = require('lodash');
const logger = global.yoho.logger;
const cache = global.yoho.cache.master;
const WHITE_LIST_KEY = 'wap:limiter:whitelist:path';

const DEFAULT_PATH_WHITE_LIST = [
    '/3party/check',
    '/3party/check/submit',
    '/passport/captcha/get',
    '/passport/img-check.jpg',
    '/passport/img-check-risk.jpg',
    '/passport/geetest/register',
    '/activity/individuation',
    '/activity/individuation/coupon',
    '/activity/share',
    '/activity/wechat/share',
    '/activity/wechat/1111',
    '/api/switch',
    '/passport/login/user',
    '/sw.js',
    '/manifest.json',
    '/activity/sw.js',
    '/activity/manifest.json',
    '/hfxRaNY27L.txt',
    '/activity/hfxRaNY27L.txt',
    '/product/shop/hfxRaNY27L.txt',
    '/product/hfxRaNY27L.txt',
    '/.well-known/apple-app-site-association',
    '/service/sitemap.xml',
    '/node/status.html',
    '/product/seckill',
    '/product/seckill/list',
    '/product/seckill/remind',
    '/product/seckill/get-product-list'
];

const cacheWhiteList = {
    nowTime() {
        return Date.parse(new Date()) / 1000;
    },
    getValue() {
        if (this.updateTime || this.nowTime() - this.updateTime > 60 * 10) {
            this.syncRemoteConfig();
        }

        return _.uniq(_.concat([], DEFAULT_PATH_WHITE_LIST, _.toArray(this.whiteList)));
    },
    syncRemoteConfig() {
        if (this.syncing) {
            return;
        }

        this.syncing = true;

        cache.getAsync(WHITE_LIST_KEY).then(res => {
            this.updateTime = this.nowTime();

            if (!res) {
                return;
            }

            this.whiteList = JSON.parse(res);
            this.syncing = false;
        }).catch((e) => {
            this.syncing = false;
            logger.debug('whitelist path parse error. ' + JSON.stringfy(e));
        });
    }
};

module.exports = () => {
    return cacheWhiteList.getValue();
};