Minor refactor

This commit is contained in:
Peng-YM 2022-06-16 11:19:00 +08:00
parent f76096f244
commit 56ada242d2
13 changed files with 38 additions and 45 deletions

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,7 @@ export function peggy() {
.pipe(tap(function (file) { .pipe(tap(function (file) {
const filename = path.basename(file.path).split(".")[0] + ".js"; const filename = path.basename(file.path).split(".")[0] + ".js";
const raw = fs.readFileSync(file.path, 'utf8'); const raw = fs.readFileSync(file.path, 'utf8');
const contents = `const grammars = String.raw\`\n${raw}\n\`;\nexport default grammars;`; const contents = `import * as peggy from 'peggy';\nconst grammars = String.raw\`\n${raw}\n\`;\nlet parser;\nexport default function getParser() { if(!parser) { parser = peggy.generate(grammars); } return parser; }`;
return newFile(filename, contents) return newFile(filename, contents)
.pipe(gulp.dest(path.dirname(file.path))) .pipe(gulp.dest(path.dirname(file.path)))
})); }));

View File

@ -1,31 +1,7 @@
import surge from './grammars/surge'; import getSurgeParser from './peggy/surge';
import loon from './grammars/loon'; import getLoonParser from './peggy/loon';
import getQXParser from './peggy/qx';
import { Base64 } from 'js-base64'; import { Base64 } from 'js-base64';
import qx from './grammars/qx';
import * as peggy from 'peggy';
let QXParser, LoonParser, SurgeParser;
function getQXParser() {
if (!QXParser) {
QXParser = peggy.generate(qx);
}
return QXParser;
}
function getLoonParser() {
if (!LoonParser) {
LoonParser = peggy.generate(loon);
}
return LoonParser;
}
function getSurgeParser() {
if (!SurgeParser) {
SurgeParser = peggy.generate(surge);
}
return SurgeParser;
}
// Parse SS URI format (only supports new SIP002, legacy format is depreciated). // Parse SS URI format (only supports new SIP002, legacy format is depreciated).
// reference: https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html // reference: https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html

View File

@ -1,3 +1,4 @@
import * as peggy from 'peggy';
const grammars = String.raw` const grammars = String.raw`
// global initializer // global initializer
{{ {{
@ -169,4 +170,10 @@ _ = [ \r\t]*
bool = b:("true"/"false") { return b === "true" } bool = b:("true"/"false") { return b === "true" }
others = comma [^=,]+ equals [^=,]+ others = comma [^=,]+ equals [^=,]+
`; `;
export default grammars; let parser;
export default function getParser() {
if (!parser) {
parser = peggy.generate(grammars);
}
return parser;
}

View File

@ -1,3 +1,4 @@
import * as peggy from 'peggy';
const grammars = String.raw` const grammars = String.raw`
// global initializer // global initializer
{{ {{
@ -168,4 +169,10 @@ equals = _ "=" _
_ = [ \r\t]* _ = [ \r\t]*
bool = b:("true"/"false") { return b === "true" } bool = b:("true"/"false") { return b === "true" }
`; `;
export default grammars; let parser;
export default function getParser() {
if (!parser) {
parser = peggy.generate(grammars);
}
return parser;
}

View File

@ -1,3 +1,4 @@
import * as peggy from 'peggy';
const grammars = String.raw` const grammars = String.raw`
// global initializer // global initializer
{{ {{
@ -169,4 +170,10 @@ _ = [ \r\t]*
bool = b:("true"/"false") { return b === "true" } bool = b:("true"/"false") { return b === "true" }
others = comma [^=,]+ equals [^=,]+ others = comma [^=,]+ equals [^=,]+
`; `;
export default grammars; let parser;
export default function getParser() {
if (!parser) {
parser = peggy.generate(grammars);
}
return parser;
}

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,9 @@
import { expect } from 'chai'; import { expect } from 'chai';
import * as peggy from 'peggy'; import getLoonParser from '../../src/core/proxy-utils/peggy/loon';
import loon from '../../src/core/proxy-utils/grammars/loon';
import testcases from './testcases'; import testcases from './testcases';
import { describe, it } from 'mocha'; import { describe, it } from 'mocha';
const parser = peggy.generate(loon); const parser = getLoonParser();
describe('Loon', function() { describe('Loon', function() {
describe('shadowsocks', function() { describe('shadowsocks', function() {

View File

@ -1,12 +1,11 @@
import * as peggy from 'peggy'; import getQXParser from '../../src/core/proxy-utils/peggy/qx';
import qx from '../../src/core/proxy-utils/grammars/qx';
import { expect } from 'chai'; import { expect } from 'chai';
import testcases from './testcases'; import testcases from './testcases';
import { describe, it } from 'mocha'; import { describe, it } from 'mocha';
const parser = peggy.generate(qx); const parser = getQXParser();
describe('QX', function() { describe('QX', function() {
describe('shadowsocks', function() { describe('shadowsocks', function() {

View File

@ -1,11 +1,9 @@
import * as peggy from 'peggy'; import getSurgeParser from '../../src/core/proxy-utils/peggy/surge';
import surge from '../../src/core/proxy-utils/grammars/surge';
import { expect } from 'chai'; import { expect } from 'chai';
import testcases from './testcases'; import testcases from './testcases';
import { describe, it } from 'mocha'; import { describe, it } from 'mocha';
const parser = getSurgeParser();
const parser = peggy.generate(surge);
describe('Surge', function() { describe('Surge', function() {
describe('shadowsocks', function() { describe('shadowsocks', function() {