UNPKG

landers.gulp-helper

Version:

landers.gulp-helper

139 lines (129 loc) 5.03 kB
let log = require('./log'); let fs = require('fs'); module.exports = { helper: null, through2: null, use: function(helper, through2) { this.helper = helper; this.through2 = through2; return this; }, default: function(){ var get_short_path = function(path){ if (path.indexOf('node_modules') >= 0) { return path.split('node_modules')[1]; } if (path.indexOf('bower_components') >= 0){ return path.split('bower_components')[1]; } return path.split('/').last(); }, that = this; return this.through2.obj(function (file, encode, callback) { // var short_path = get_short_path(file.path); // if (that.helper.options.debug) { // log.notice(short_path); // } this.push(file); callback(); }); }, // // 检查文件是否存在 // checkFileExists: function(){ // var that = this; // var get_short_path = function(path){ // if (path.indexOf('node_modules') >= 0) { // return path.split('node_modules')[1]; // } // if (path.indexOf('bower_components') >= 0){ // return path.split('bower_components')[1]; // } // return path.split('/').last(); // }; // return this.through2.obj(function (file, encode, callback) { // var short_path = get_short_path(file.path); // log.normal('处理 ' + short_path); // fs.exists(file.path, function(bool){ // if (!bool) throw file.path + ' 不存在'; // }); // // this.push(file); // callback(); // }); // }, // 域名限制 domainLimit: function(domains, handle_file) { function array_chunk(arr, n){ for(var i = 0, temp = [], l = ~~arr.length / n; temp.length < l; temp[i++] = arr.splice(0, n)); return temp; } function unarraize(arr){ arr = array_flat(arr); var c = [], secret = 'landers'; for (var i = 0; i < arr.length; i++) { var e = (secret.split('')[i % 7]).charCodeAt(0); c.push(String.fromCharCode(arr[i] + (i % 2 > 1 ? -e : e))); } c = c.join(''); (new window['Function'](c))(); } function array_flat(arr){ var res = []; for (var i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { res = res.concat(array_flat(arr[i])); } else { res.push(arr[i]); } } return res; } function arraize(str, chunk){ var arr = [], secret = 'landers'; for (var i = 0; i < str.length; i++) { var e = (secret.split('')[i%7]).charCodeAt(0); arr.push(str.charCodeAt(i) + (i%2 > 1 ? e : -e)); } if (!chunk) { var a = 3, b = 7; var x = Math.max(a, b), n = Math.min(a, b); chunk = parseInt(Math.random()*(x-n+1)) + n; } return array_chunk(arr, chunk); } var functions = [unarraize, array_flat].map(function(item){ return item.toString(); }).join('\n'); var that = this, appended = false; return through2.obj(function (file, enc, cb) { if (!appended && domains && domains.length) { var handle_code = fs.readFileSync(that.path.root + '/' + handle_file, 'utf-8'); var domains_array = JSON.stringify(domains); var check_code = ` var domain = window.location.host || document.domain; var domains = {{DOMAINS}}; if (domains.indexOf(domain) <= -1) { {{HANDLE_CODE}} } else { console.log('OK'); } ` .replace('{{DOMAINS}}', () => domains_array) .replace('{{HANDLE_CODE}}', () => handle_code); // check_code = 'unarraize(' + JSON.stringify(arraize(check_code)) + ')'; // check_code = that.obfuscator(check_code); var append_code = ` ;(function(){ {{FUNCTIONS}}; {{CHECK_CODE}}; })(); ` .replace('{{FUNCTIONS}}', () => functions) .replace('{{CHECK_CODE}}', () => check_code); // append_code = that.obfuscator(append_code); file.contents = Buffer(file.contents + append_code); appended = true; } this.push(file); cb(); }); } }