mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-21 13:50:29 +08:00
161 lines
6.9 KiB
JavaScript
161 lines
6.9 KiB
JavaScript
'use strict';const require$$0=require('node:path'),fetchTextByLine=require('./fetch-text-by-line.YY5b5610.cjs'),index=require('./index.B8Uk9n3v.cjs'),base=require('./base.C13YY5P3.cjs'),require$$5=require('fdir'),fetchRetry=require('./fetch-retry.D06uBUaW.cjs'),domainset=require('./domainset.CSQfKAtf.cjs'),ruleset=require('./ruleset.iYH0XOC1.cjs');var buildCommon = {exports: {}};var hasRequiredBuildCommon;
|
|
|
|
function requireBuildCommon () {
|
|
if (hasRequiredBuildCommon) return buildCommon.exports;
|
|
hasRequiredBuildCommon = 1;
|
|
(function (module, exports) {
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "buildCommon", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return buildCommon;
|
|
}
|
|
});
|
|
const _nodepath = /*#__PURE__*/ _interop_require_wildcard(require$$0);
|
|
const _fetchtextbyline = /*@__PURE__*/ fetchTextByLine.a();
|
|
const _processline = /*@__PURE__*/ fetchTextByLine.r();
|
|
const _trace = /*@__PURE__*/ index.r();
|
|
const _description = /*@__PURE__*/ base.r();
|
|
const _fdir = require$$5;
|
|
const _appendarrayinplace = /*@__PURE__*/ base.a();
|
|
const _dir = /*@__PURE__*/ fetchRetry.a();
|
|
const _domainset = /*@__PURE__*/ domainset.r();
|
|
const _ruleset = /*@__PURE__*/ ruleset.r();
|
|
function _getRequireWildcardCache(nodeInterop) {
|
|
if (typeof WeakMap !== "function") return null;
|
|
var cacheBabelInterop = new WeakMap();
|
|
var cacheNodeInterop = new WeakMap();
|
|
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
})(nodeInterop);
|
|
}
|
|
function _interop_require_wildcard(obj, nodeInterop) {
|
|
if (obj && obj.__esModule) {
|
|
return obj;
|
|
}
|
|
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
return {
|
|
default: obj
|
|
};
|
|
}
|
|
var cache = _getRequireWildcardCache(nodeInterop);
|
|
if (cache && cache.has(obj)) {
|
|
return cache.get(obj);
|
|
}
|
|
var newObj = {
|
|
__proto__: null
|
|
};
|
|
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
for(var key in obj){
|
|
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
if (desc && (desc.get || desc.set)) {
|
|
Object.defineProperty(newObj, key, desc);
|
|
} else {
|
|
newObj[key] = obj[key];
|
|
}
|
|
}
|
|
}
|
|
newObj.default = obj;
|
|
if (cache) {
|
|
cache.set(obj, newObj);
|
|
}
|
|
return newObj;
|
|
}
|
|
const MAGIC_COMMAND_SKIP = '# $ custom_build_script';
|
|
const MAGIC_COMMAND_TITLE = '# $ meta_title ';
|
|
const MAGIC_COMMAND_DESCRIPTION = '# $ meta_description ';
|
|
const MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES = '# $ sgmodule_mitm_hostnames ';
|
|
const clawSourceDirPromise = new _fdir.fdir().withRelativePaths().filter((filepath, isDirectory)=>{
|
|
if (isDirectory) return true;
|
|
const extname = _nodepath.extname(filepath);
|
|
return !(extname === '.js' || extname === '.ts');
|
|
}).crawl(_dir.SOURCE_DIR).withPromise();
|
|
const buildCommon = (0, _trace.task)(require.main === module, __filename)(async (span)=>{
|
|
const promises = [];
|
|
const paths = await clawSourceDirPromise;
|
|
for(let i = 0, len = paths.length; i < len; i++){
|
|
const relativePath = paths[i];
|
|
const fullPath = _dir.SOURCE_DIR + _nodepath.sep + relativePath;
|
|
// if (
|
|
// relativePath.startsWith('ip/')
|
|
// || relativePath.startsWith('non_ip/')
|
|
// ) {
|
|
promises.push(transform(span, fullPath, relativePath));
|
|
// continue;
|
|
// }
|
|
// console.error(picocolors.red(`[build-comman] Unknown file: ${relativePath}`));
|
|
}
|
|
return Promise.all(promises);
|
|
});
|
|
const $skip = Symbol('skip');
|
|
function processFile(span, sourcePath) {
|
|
return span.traceChildAsync(`process file: ${sourcePath}`, async ()=>{
|
|
const lines = [];
|
|
let title = '';
|
|
const descriptions = [];
|
|
let sgmodulePathname = null;
|
|
try {
|
|
for await (const line of (0, _fetchtextbyline.readFileByLine)(sourcePath)){
|
|
if (line.startsWith(MAGIC_COMMAND_SKIP)) {
|
|
return $skip;
|
|
}
|
|
if (line.startsWith(MAGIC_COMMAND_TITLE)) {
|
|
title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
|
|
continue;
|
|
}
|
|
if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
|
|
descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
|
|
continue;
|
|
}
|
|
if (line.startsWith(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES)) {
|
|
sgmodulePathname = line.slice(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES.length).trim();
|
|
continue;
|
|
}
|
|
const l = (0, _processline.processLine)(line);
|
|
if (l) {
|
|
lines.push(l);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('Error processing', sourcePath);
|
|
console.trace(e);
|
|
}
|
|
return [
|
|
title,
|
|
descriptions,
|
|
lines,
|
|
sgmodulePathname
|
|
];
|
|
});
|
|
}
|
|
async function transform(parentSpan, sourcePath, relativePath) {
|
|
const extname = _nodepath.extname(sourcePath);
|
|
const id = _nodepath.basename(sourcePath, extname);
|
|
return parentSpan.traceChild(`transform ruleset: ${id}`).traceAsyncFn(async (span)=>{
|
|
const type = relativePath.split(_nodepath.sep)[0];
|
|
if (type !== 'ip' && type !== 'non_ip' && type !== 'domainset') {
|
|
throw new TypeError(`Invalid type: ${type}`);
|
|
}
|
|
const res = await processFile(span, sourcePath);
|
|
if (res === $skip) return;
|
|
const [title, descriptions, lines, sgmoduleName] = res;
|
|
let finalDescriptions;
|
|
if (descriptions.length) {
|
|
finalDescriptions = _description.SHARED_DESCRIPTION.slice();
|
|
finalDescriptions.push('');
|
|
(0, _appendarrayinplace.appendArrayInPlace)(finalDescriptions, descriptions);
|
|
} else {
|
|
finalDescriptions = _description.SHARED_DESCRIPTION;
|
|
}
|
|
if (type === 'domainset') {
|
|
return new _domainset.DomainsetOutput(span, id).withTitle(title).withDescription(finalDescriptions).addFromDomainset(lines).write();
|
|
}
|
|
return new _ruleset.RulesetOutput(span, id, type).withTitle(title).withDescription(finalDescriptions).withMitmSgmodulePath(sgmoduleName).addFromRuleset(lines).write();
|
|
});
|
|
}
|
|
} (buildCommon, buildCommon.exports));
|
|
return buildCommon.exports;
|
|
}exports.r=requireBuildCommon; |