mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 09:03:37 +01:00
Merge remote-tracking branch 'origin/main' into feat/repo-list-sync
# Conflicts: # deploy/logging/sanitize.vrl # public/asset-manifest.json # public/script/vendor.min.js
This commit is contained in:
commit
07bdae9252
22 files changed
+394
-66
No files matched your search
@@ -1,6 +1,18 @@
|
||||
// Generate finite diagnostic allowlists from repository code, never runtime input.
|
||||
const fs = require('fs'), path = require('path'), ts = require('typescript');
|
||||
const files = {};
|
||||
// Error codes the code base throws or relays, so the logger and the collector
|
||||
// keep them as errorCode instead of dropping them as arbitrary strings.
|
||||
const codePattern = /^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)+$/;
|
||||
const errorFactories = new Set(['AnonymousError','billingError','appError']);
|
||||
const thrownCodes = new Set();
|
||||
function codeLiterals(node) {
|
||||
if (!node) return;
|
||||
if (ts.isStringLiteralLike(node)) { if (codePattern.test(node.text)) thrownCodes.add(node.text); }
|
||||
else if (ts.isConditionalExpression(node)) { codeLiterals(node.whenTrue); codeLiterals(node.whenFalse); }
|
||||
else if (ts.isBinaryExpression(node) && [ts.SyntaxKind.BarBarToken, ts.SyntaxKind.QuestionQuestionToken].includes(node.operatorToken.kind)) { codeLiterals(node.left); codeLiterals(node.right); }
|
||||
else if (ts.isParenthesizedExpression(node)) codeLiterals(node.expression);
|
||||
}
|
||||
function scan(dir) {
|
||||
for (const entry of fs.readdirSync(dir, {withFileTypes:true})) {
|
||||
const file = path.join(dir,entry.name);
|
||||
@@ -11,6 +23,11 @@ function scan(dir) {
|
||||
function visit(node) {
|
||||
if ((ts.isFunctionDeclaration(node)||ts.isMethodDeclaration(node)||ts.isFunctionExpression(node)) && node.name && ts.isIdentifier(node.name)) names.add(node.name.text);
|
||||
if(ts.isVariableDeclaration(node)&&ts.isIdentifier(node.name)&&node.initializer&&(ts.isArrowFunction(node.initializer)||ts.isFunctionExpression(node.initializer))) names.add(node.name.text);
|
||||
// new AnonymousError("code"), billingError("code"), appError("code")
|
||||
if((ts.isNewExpression(node)||ts.isCallExpression(node))&&ts.isIdentifier(node.expression)&&errorFactories.has(node.expression.text)&&node.arguments?.length) codeLiterals(node.arguments[0]);
|
||||
// Codes picked into a variable first: errCode = "streamer_unreachable"
|
||||
if(ts.isVariableDeclaration(node)&&ts.isIdentifier(node.name)&&/^(errCode|code)$/.test(node.name.text)) codeLiterals(node.initializer);
|
||||
if(ts.isBinaryExpression(node)&&node.operatorToken.kind===ts.SyntaxKind.EqualsToken&&ts.isIdentifier(node.left)&&/^(errCode|code)$/.test(node.left.text)) codeLiterals(node.right);
|
||||
ts.forEachChild(node,visit);
|
||||
}
|
||||
visit(source);
|
||||
@@ -23,7 +40,12 @@ scan('src');
|
||||
const types=['Error','TypeError','RangeError','ReferenceError','SyntaxError','URIError','EvalError','AggregateError','AnonymousError','HTTPError','RequestError','MongoServerError','MongoNetworkError','MongoServerSelectionError','ValidationError','TimeoutError','AbortError','UnknownError'];
|
||||
const vrl=fs.readFileSync('deploy/logging/sanitize.vrl','utf8');
|
||||
const routes=JSON.parse(vrl.match(/if includes\((\[.*?\]), record.route\)/)[1]);
|
||||
fs.writeFileSync('src/core/log-diagnostic-schema.ts','// Generated by scripts/generate-log-diagnostics.js; do not edit.\nexport const LOG_SOURCE_FUNCTIONS: Record<string, readonly string[]> = '+JSON.stringify(files,null,2)+';\nexport const LOG_ERROR_TYPES = '+JSON.stringify(types)+';\nexport const LOG_ROUTES = '+JSON.stringify(routes)+';\n');
|
||||
const codes=[...thrownCodes].sort();
|
||||
fs.writeFileSync('src/core/log-diagnostic-schema.ts','// Generated by scripts/generate-log-diagnostics.js; do not edit.\nexport const LOG_SOURCE_FUNCTIONS: Record<string, readonly string[]> = '+JSON.stringify(files,null,2)+';\nexport const LOG_ERROR_TYPES = '+JSON.stringify(types)+';\nexport const LOG_ROUTES = '+JSON.stringify(routes)+';\nexport const LOG_THROWN_ERROR_CODES = '+JSON.stringify(codes)+';\n');
|
||||
// The collector keeps reviewed codes too (system errnos, worker codes); only add.
|
||||
const codeList=/(if includes\()(\[.*?\])(, record\.errorCode\))/;
|
||||
const collectorCodes=JSON.parse(vrl.match(codeList)[2]);
|
||||
const mergedCodes=[...collectorCodes,...codes.filter(code=>!collectorCodes.includes(code))];
|
||||
const block=`# BEGIN GENERATED DIAGNOSTICS
|
||||
source_functions = ${JSON.stringify(files)}
|
||||
for_each(["errorType", "causeType"]) -> |_index, key| {
|
||||
@@ -43,5 +65,5 @@ for_each(["errorStack", "causeStack"]) -> |_index, key| {
|
||||
}
|
||||
}
|
||||
# END GENERATED DIAGNOSTICS`;
|
||||
const clean=vrl.replace(/# BEGIN GENERATED DIAGNOSTICS[\s\S]*?# END GENERATED DIAGNOSTICS\n?/,'');
|
||||
const clean=vrl.replace(/# BEGIN GENERATED DIAGNOSTICS[\s\S]*?# END GENERATED DIAGNOSTICS\n?/,'').replace(codeList,(_,start,_list,end)=>start+JSON.stringify(mergedCodes).replace(/","/g,'", "')+end);
|
||||
fs.writeFileSync('deploy/logging/sanitize.vrl',clean.replace('out._msg = out.event',()=>block+'\nout._msg = out.event'));
|
||||
Reference in new issue
Block a user