'use strict';Object.defineProperty(exports,Symbol.toStringTag,{value:'Module'});const buildCommon=require('../_virtual/build-common.cjs'),require$$0=require('node:path'),fetchTextByLine=require('./lib/fetch-text-by-line.cjs'),processLine=require('./lib/process-line.cjs'),index=require('./trace/index.cjs'),description=require('./constants/description.cjs'),require$$5=require('fdir'),appendArrayInPlace=require('./lib/append-array-in-place.cjs'),dir=require('./constants/dir.cjs'),domainset=require('./lib/rules/domainset.cjs'),ruleset=require('./lib/rules/ruleset.cjs');var hasRequiredBuildCommon; function requireBuildCommon () { if (hasRequiredBuildCommon) return buildCommon.__module.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.__require(); const _processline = /*@__PURE__*/ processLine.__require(); const _trace = /*@__PURE__*/ index.__require(); const _description = /*@__PURE__*/ description.__require(); const _fdir = require$$5; const _appendarrayinplace = /*@__PURE__*/ appendArrayInPlace.__require(); const _dir = /*@__PURE__*/ dir.__require(); const _domainset = /*@__PURE__*/ domainset.__require(); const _ruleset = /*@__PURE__*/ ruleset.__require(); 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.__module, buildCommon.__module.exports)); return buildCommon.__module.exports; }exports.__require=requireBuildCommon;