Fixed ESLint issues

This commit is contained in:
Peng-YM 2022-05-24 22:24:27 +08:00
parent 3b70cc0bd8
commit b56f17606e
10 changed files with 42 additions and 36 deletions

View File

@ -1,3 +1,4 @@
/* eslint-disable no-case-declarations */
import { safeLoad } from 'static-js-yaml'; import { safeLoad } from 'static-js-yaml';
import { Base64 } from 'js-base64'; import { Base64 } from 'js-base64';
@ -12,7 +13,7 @@ const PROXY_PREPROCESSORS = (function () {
const name = 'HTML'; const name = 'HTML';
const test = (raw) => /^<!DOCTYPE html>/.test(raw); const test = (raw) => /^<!DOCTYPE html>/.test(raw);
// simply discard HTML // simply discard HTML
const parse = (_) => ''; const parse = () => '';
return { name, test, parse }; return { name, test, parse };
} }
@ -61,12 +62,6 @@ const PROXY_PREPROCESSORS = (function () {
// preprocessing for SSD subscription format // preprocessing for SSD subscription format
const output = []; const output = [];
let ssdinfo = JSON.parse(Base64.decode(raw.split('ssd://')[1])); let ssdinfo = JSON.parse(Base64.decode(raw.split('ssd://')[1]));
// options (traffic_used, traffic_total, expiry, url)
const traffic_used = ssdinfo.traffic_used; // GB
const traffic_total = ssdinfo.traffic_total; // GB, -1 means unlimited
const expiry = ssdinfo.expiry; // YYYY-MM-DD HH:mm:ss
// default setting
let name = ssdinfo.airport; // name of the airport
let port = ssdinfo.port; let port = ssdinfo.port;
let method = ssdinfo.encryption; let method = ssdinfo.encryption;
let password = ssdinfo.password; let password = ssdinfo.password;
@ -124,7 +119,7 @@ const PROXY_PARSERS = (function () {
}; };
content = content.split('#')[0]; // strip proxy name content = content.split('#')[0]; // strip proxy name
// handle IPV4 and IPV6 // handle IPV4 and IPV6
const serverAndPort = content.match(/@([^\/]*)(\/|$)/)[1]; const serverAndPort = content.match(/@([^/]*)(\/|$)/)[1];
const portIdx = serverAndPort.lastIndexOf(':'); const portIdx = serverAndPort.lastIndexOf(':');
proxy.server = serverAndPort.substring(0, portIdx); proxy.server = serverAndPort.substring(0, portIdx);
proxy.port = serverAndPort.substring(portIdx + 1); proxy.port = serverAndPort.substring(portIdx + 1);
@ -1178,9 +1173,10 @@ const PROXY_PROCESSORS = (function () {
}; };
} }
// use base64 encoded script to rename /** Script Operator
/** Example script
function operator(proxies) { function operator(proxies) {
const {arg1} = $arguments;
// do something // do something
return proxies; return proxies;
} }
@ -1189,6 +1185,7 @@ const PROXY_PROCESSORS = (function () {
1. This function name should be `operator`! 1. This function name should be `operator`!
2. Always declare variables before using them! 2. Always declare variables before using them!
*/ */
// eslint-disable-next-line no-unused-vars
function ScriptOperator(script, targetPlatform, $arguments) { function ScriptOperator(script, targetPlatform, $arguments) {
return { return {
name: 'Script Operator', name: 'Script Operator',
@ -1196,12 +1193,18 @@ const PROXY_PROCESSORS = (function () {
let output = proxies; let output = proxies;
(function () { (function () {
// interface to get internal operators // interface to get internal operators
// eslint-disable-next-line no-unused-vars
const $get = (name, args) => { const $get = (name, args) => {
const item = PROXY_PROCESSORS[name]; const item = PROXY_PROCESSORS[name];
return item(args); return item(args);
}; };
// eslint-disable-next-line no-unused-vars
const $process = ApplyProcessor; const $process = ApplyProcessor;
eval(script); eval(script);
// eslint-disable-next-line no-undef
output = operator(proxies, targetPlatform); output = operator(proxies, targetPlatform);
})(); })();
return output; return output;
@ -1289,7 +1292,6 @@ const PROXY_PROCESSORS = (function () {
}; };
} }
// use base64 encoded script to filter proxies
/** /**
Script Example Script Example
function func(proxies) { function func(proxies) {
@ -1301,6 +1303,7 @@ const PROXY_PROCESSORS = (function () {
1. This function name should be `func`! 1. This function name should be `func`!
2. Always declare variables before using them! 2. Always declare variables before using them!
*/ */
// eslint-disable-next-line no-unused-vars
function ScriptFilter(script, targetPlatform, $arguments) { function ScriptFilter(script, targetPlatform, $arguments) {
return { return {
name: 'Script Filter', name: 'Script Filter',
@ -1308,6 +1311,7 @@ const PROXY_PROCESSORS = (function () {
let output = FULL(proxies.length, true); let output = FULL(proxies.length, true);
!(function () { !(function () {
eval(script); eval(script);
// eslint-disable-next-line no-undef
output = filter(proxies, targetPlatform); output = filter(proxies, targetPlatform);
})(); })();
return output; return output;

View File

@ -1,3 +1,5 @@
import { ApplyProcessor } from './proxy-utils';
import YAML from 'static-js-yaml';
import $ from './app'; import $ from './app';
const RULE_TYPES_MAPPING = [ const RULE_TYPES_MAPPING = [
@ -19,7 +21,7 @@ const RULE_PREPROCESSORS = (function () {
const name = 'HTML'; const name = 'HTML';
const test = (raw) => /^<!DOCTYPE html>/.test(raw); const test = (raw) => /^<!DOCTYPE html>/.test(raw);
// simply discard HTML // simply discard HTML
const parse = (_) => ''; const parse = () => '';
return { name, test, parse }; return { name, test, parse };
} }
@ -226,7 +228,7 @@ const RULE_PRODUCERS = (function () {
return output; return output;
}), }),
}; };
return YAML.stringify(conf); return YAML.dump(conf);
}; };
return { type, func }; return { type, func };
} }

View File

@ -13,7 +13,7 @@ import {
SETTINGS_KEY, SETTINGS_KEY,
} from './constants'; } from './constants';
function register($app) { export default function register($app) {
// Initialization // Initialization
if (!$.read(ARTIFACTS_KEY)) $.write({}, ARTIFACTS_KEY); if (!$.read(ARTIFACTS_KEY)) $.write({}, ARTIFACTS_KEY);
@ -66,7 +66,7 @@ async function getArtifact(req, res) {
artifact.updated = new Date().getTime(); artifact.updated = new Date().getTime();
const body = JSON.parse(resp.body); const body = JSON.parse(resp.body);
artifact.url = body.files[artifact.name].raw_url.replace( artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^\/]*\/(.*)/, /\/raw\/[^/]*\/(.*)/,
'/raw/$1', '/raw/$1',
); );
$.write(allArtifacts, ARTIFACTS_KEY); $.write(allArtifacts, ARTIFACTS_KEY);
@ -198,7 +198,7 @@ async function cronSyncArtifacts(_, res) {
artifact.updated = new Date().getTime(); artifact.updated = new Date().getTime();
// extract real url from gist // extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace( artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^\/]*\/(.*)/, /\/raw\/[^/]*\/(.*)/,
'/raw/$1', '/raw/$1',
); );
} }
@ -410,4 +410,4 @@ async function produceArtifact(
} }
} }
export { register, produceArtifact }; export { produceArtifact };

View File

@ -3,7 +3,7 @@ import { SUBS_KEY, COLLECTIONS_KEY } from './constants';
import { produceArtifact } from './artifacts'; import { produceArtifact } from './artifacts';
import $ from '../core/app'; import $ from '../core/app';
export function register($app) { export default function register($app) {
if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY); if (!$.read(COLLECTIONS_KEY)) $.write({}, COLLECTIONS_KEY);
$app.get('/download/collection/:name', downloadCollection); $app.get('/download/collection/:name', downloadCollection);

View File

@ -9,21 +9,19 @@ import { IP_API } from '../utils/geo';
import Gist from '../utils/gist'; import Gist from '../utils/gist';
import $ from '../core/app'; import $ from '../core/app';
import registerSubscriptionRoutes from './subscriptions';
import registerCollectionRoutes from './collections';
import registerArtifactRoutes from './artifacts';
import registerSettingRoutes from './settings';
export default function serve() { export default function serve() {
const $app = express(); const $app = express();
// register routes // register routes
const collections = require('./collections'); registerCollectionRoutes($app);
collections.register($app); registerSubscriptionRoutes($app);
registerSettingRoutes($app);
const subscriptions = require('./subscriptions'); registerArtifactRoutes($app);
subscriptions.register($app);
const settings = require('./settings');
settings.register($app);
const artifacts = require('./artifacts');
artifacts.register($app);
// utils // utils
$app.get('/api/utils/IP_API/:server', IP_API); // IP-API reverse proxy $app.get('/api/utils/IP_API/:server', IP_API); // IP-API reverse proxy
@ -78,10 +76,10 @@ async function gistBackup(req, res) {
}); });
try { try {
let content; let content;
const settings = $.read(SETTINGS_KEY);
switch (action) { switch (action) {
case 'upload': case 'upload':
// update syncTime. // update syncTime.
const settings = $.read(SETTINGS_KEY);
settings.syncTime = new Date().getTime(); settings.syncTime = new Date().getTime();
$.write(settings, SETTINGS_KEY); $.write(settings, SETTINGS_KEY);
content = $.read('#sub-store'); content = $.read('#sub-store');

View File

@ -1,7 +1,7 @@
import { SETTINGS_KEY } from './constants'; import { SETTINGS_KEY } from './constants';
import $ from '../core/app'; import $ from '../core/app';
export function register($app) { export default function register($app) {
if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY); if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY);
$app.route('/api/settings').get(getSettings).patch(updateSettings); $app.route('/api/settings').get(getSettings).patch(updateSettings);
} }

View File

@ -2,7 +2,7 @@ import { SUBS_KEY, COLLECTIONS_KEY } from './constants';
import { produceArtifact } from './artifacts'; import { produceArtifact } from './artifacts';
import $ from '../core/app'; import $ from '../core/app';
export function register($app) { export default function register($app) {
if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY); if (!$.read(SUBS_KEY)) $.write({}, SUBS_KEY);
$app.get('/download/:name', downloadSubscription); $app.get('/download/:name', downloadSubscription);

View File

@ -1,3 +1,4 @@
/* eslint-disable no-undef */
import { ENV } from './open-api'; import { ENV } from './open-api';
import $ from '../core/app'; import $ from '../core/app';
@ -238,7 +239,7 @@ export default function express({ port } = { port: 3000 }) {
function extractURL(url) { function extractURL(url) {
// extract path // extract path
const match = url.match(/https?:\/\/[^\/]+(\/[^?]*)/) || []; const match = url.match(/https?:\/\/[^/]+(\/[^?]*)/) || [];
const path = match[1] || '/'; const path = match[1] || '/';
// extract query string // extract query string

View File

@ -1,3 +1,4 @@
/* eslint-disable no-undef */
export function ENV() { export function ENV() {
const isQX = typeof $task !== 'undefined'; const isQX = typeof $task !== 'undefined';
const isLoon = typeof $loon !== 'undefined'; const isLoon = typeof $loon !== 'undefined';
@ -18,7 +19,7 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
'PATCH', 'PATCH',
]; ];
const URL_REGEX = const URL_REGEX =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/; /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
function send(method, options) { function send(method, options) {
options = typeof options === 'string' ? { url: options } : options; options = typeof options === 'string' ? { url: options } : options;

File diff suppressed because one or more lines are too long