Apply formatting and fix web request utilities
Add consistent indent rule to ESLint and normalize formatting/indentation across multiple files. Refactor RequestUtil: improve cookie extraction, redirect handling, and HttpGetJson/HttpGetText request flow; fix cookie string builder formatting. Fix uploadGroupNoticeImage promise/error handling and network request flow. Also apply minor style/refactor tweaks in bridge web APIs, onebot handlers, message-store SQL alignment, and Proton AST collector utilities to improve readability and correctness.
This commit is contained in:
1 parent
aea2676c31
commit
36c3ed031b
22 files changed
+2144
-2140
No files matched your search
+115
-115
@@ -8,11 +8,11 @@ import type { MessageRegistry } from './ast/types.js';
|
||||
import { buildDependencyRegistry } from './ast/dependency-graph.js';
|
||||
import { generateCode } from './codegen/generator.js';
|
||||
import {
|
||||
normalizeRuntimeMapPath,
|
||||
runtimeMapToRegistry,
|
||||
type ProtobufRuntimeMap,
|
||||
type RuntimeMapCallSite,
|
||||
type RuntimeMapFnName,
|
||||
normalizeRuntimeMapPath,
|
||||
runtimeMapToRegistry,
|
||||
type ProtobufRuntimeMap,
|
||||
type RuntimeMapCallSite,
|
||||
type RuntimeMapFnName,
|
||||
} from './runtime-map.js';
|
||||
|
||||
type DynamicEncode = (params: unknown) => Uint8Array;
|
||||
@@ -41,180 +41,180 @@ const dynamicTypeCache = new Map<string, DynamicTypeFns>();
|
||||
* This is disabled by default.
|
||||
*/
|
||||
export function protobuf_enableRuntimeMapFallback(map: ProtobufRuntimeMap): void {
|
||||
runtimeMapFallback = map;
|
||||
runtimeMapRegistry = runtimeMapToRegistry(map);
|
||||
runtimeMapCallSites = map.callSites.map(cs => ({
|
||||
...cs,
|
||||
file: normalizeRuntimeMapPath(cs.file),
|
||||
}));
|
||||
dynamicTypeCache.clear();
|
||||
runtimeMapFallback = map;
|
||||
runtimeMapRegistry = runtimeMapToRegistry(map);
|
||||
runtimeMapCallSites = map.callSites.map(cs => ({
|
||||
...cs,
|
||||
file: normalizeRuntimeMapPath(cs.file),
|
||||
}));
|
||||
dynamicTypeCache.clear();
|
||||
}
|
||||
|
||||
/** Disable dynamic runtime fallback and clear all generated function caches. */
|
||||
export function protobuf_disableRuntimeMapFallback(): void {
|
||||
runtimeMapFallback = null;
|
||||
runtimeMapRegistry = null;
|
||||
runtimeMapCallSites = [];
|
||||
dynamicTypeCache.clear();
|
||||
runtimeMapFallback = null;
|
||||
runtimeMapRegistry = null;
|
||||
runtimeMapCallSites = [];
|
||||
dynamicTypeCache.clear();
|
||||
}
|
||||
|
||||
export function protobuf_encode<T>(_params: T): Uint8Array {
|
||||
const dynamic = tryDynamicInvoke('protobuf_encode', _params);
|
||||
if (dynamic !== DYNAMIC_MISS) return dynamic as Uint8Array;
|
||||
const dynamic = tryDynamicInvoke('protobuf_encode', _params);
|
||||
if (dynamic !== DYNAMIC_MISS) return dynamic as Uint8Array;
|
||||
|
||||
throw new Error(
|
||||
'protobuf_encode<T>() was not transformed by the @snowluma/proton Vite plugin. ' +
|
||||
throw new Error(
|
||||
'protobuf_encode<T>() was not transformed by the @snowluma/proton Vite plugin. ' +
|
||||
'Make sure protobufVitePlugin() is added to your vite.config.ts plugins array.',
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
export function protobuf_decode<T>(_data: Uint8Array): T {
|
||||
const dynamic = tryDynamicInvoke('protobuf_decode', _data);
|
||||
if (dynamic !== DYNAMIC_MISS) return dynamic as T;
|
||||
const dynamic = tryDynamicInvoke('protobuf_decode', _data);
|
||||
if (dynamic !== DYNAMIC_MISS) return dynamic as T;
|
||||
|
||||
throw new Error(
|
||||
'protobuf_decode<T>() was not transformed by the @snowluma/proton Vite plugin. ' +
|
||||
throw new Error(
|
||||
'protobuf_decode<T>() was not transformed by the @snowluma/proton Vite plugin. ' +
|
||||
'Make sure protobufVitePlugin() is added to your vite.config.ts plugins array.',
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
function tryDynamicInvoke(fnName: RuntimeMapFnName, payload: unknown): unknown {
|
||||
if (!runtimeMapFallback || runtimeMapFallback.version !== 1 || !runtimeMapRegistry) {
|
||||
return DYNAMIC_MISS;
|
||||
}
|
||||
if (!runtimeMapFallback || runtimeMapFallback.version !== 1 || !runtimeMapRegistry) {
|
||||
return DYNAMIC_MISS;
|
||||
}
|
||||
|
||||
const caller = getFirstUserFrame();
|
||||
if (!caller) return DYNAMIC_MISS;
|
||||
const caller = getFirstUserFrame();
|
||||
if (!caller) return DYNAMIC_MISS;
|
||||
|
||||
const callSite = resolveCallSite(fnName, caller);
|
||||
if (!callSite) return DYNAMIC_MISS;
|
||||
const callSite = resolveCallSite(fnName, caller);
|
||||
if (!callSite) return DYNAMIC_MISS;
|
||||
|
||||
const typeFns = ensureTypeFunctions(callSite.typeName);
|
||||
if (!typeFns) return DYNAMIC_MISS;
|
||||
const typeFns = ensureTypeFunctions(callSite.typeName);
|
||||
if (!typeFns) return DYNAMIC_MISS;
|
||||
|
||||
const fn = fnName === 'protobuf_encode' ? typeFns.encode : typeFns.decode;
|
||||
if (typeof fn !== 'function') return DYNAMIC_MISS;
|
||||
const fn = fnName === 'protobuf_encode' ? typeFns.encode : typeFns.decode;
|
||||
if (typeof fn !== 'function') return DYNAMIC_MISS;
|
||||
|
||||
return fn(payload as never);
|
||||
return fn(payload as never);
|
||||
}
|
||||
|
||||
function ensureTypeFunctions(typeName: string): DynamicTypeFns | null {
|
||||
const cached = dynamicTypeCache.get(typeName);
|
||||
if (cached) return cached;
|
||||
if (!runtimeMapRegistry || !runtimeMapRegistry.has(typeName)) return null;
|
||||
const cached = dynamicTypeCache.get(typeName);
|
||||
if (cached) return cached;
|
||||
if (!runtimeMapRegistry || !runtimeMapRegistry.has(typeName)) return null;
|
||||
|
||||
const subset = buildDependencyRegistry(runtimeMapRegistry, [typeName]);
|
||||
if (subset.size === 0) return null;
|
||||
const subset = buildDependencyRegistry(runtimeMapRegistry, [typeName]);
|
||||
if (subset.size === 0) return null;
|
||||
|
||||
const generated = generateCode(subset);
|
||||
if (!generated) return null;
|
||||
const generated = generateCode(subset);
|
||||
if (!generated) return null;
|
||||
|
||||
const factory = new Function(
|
||||
`${generated}\nreturn {\n` +
|
||||
const factory = new Function(
|
||||
`${generated}\nreturn {\n` +
|
||||
` encode: typeof protobuf_encode_${typeName} === 'function' ? protobuf_encode_${typeName} : undefined,\n` +
|
||||
` decode: typeof protobuf_decode_${typeName} === 'function' ? protobuf_decode_${typeName} : undefined,\n` +
|
||||
`};`,
|
||||
) as () => DynamicTypeFns;
|
||||
) as () => DynamicTypeFns;
|
||||
|
||||
const compiled = factory();
|
||||
dynamicTypeCache.set(typeName, compiled);
|
||||
return compiled;
|
||||
const compiled = factory();
|
||||
dynamicTypeCache.set(typeName, compiled);
|
||||
return compiled;
|
||||
}
|
||||
|
||||
function resolveCallSite(fnName: RuntimeMapFnName, caller: StackFrame): RuntimeMapCallSite | null {
|
||||
const callerFile = normalizeRuntimeMapPath(caller.file);
|
||||
const callerFile = normalizeRuntimeMapPath(caller.file);
|
||||
|
||||
const candidates = runtimeMapCallSites.filter(cs =>
|
||||
cs.fnName === fnName &&
|
||||
const candidates = runtimeMapCallSites.filter(cs =>
|
||||
cs.fnName === fnName &&
|
||||
cs.line === caller.line &&
|
||||
pathsLikelySame(callerFile, cs.file),
|
||||
);
|
||||
);
|
||||
|
||||
if (candidates.length === 0) return null;
|
||||
if (candidates.length === 1) return candidates[0];
|
||||
if (candidates.length === 0) return null;
|
||||
if (candidates.length === 1) return candidates[0];
|
||||
|
||||
let best = candidates[0];
|
||||
let bestDistance = Math.abs(best.column - caller.column);
|
||||
for (let i = 1; i < candidates.length; i++) {
|
||||
const dist = Math.abs(candidates[i].column - caller.column);
|
||||
if (dist < bestDistance) {
|
||||
best = candidates[i];
|
||||
bestDistance = dist;
|
||||
}
|
||||
let best = candidates[0];
|
||||
let bestDistance = Math.abs(best.column - caller.column);
|
||||
for (let i = 1; i < candidates.length; i++) {
|
||||
const dist = Math.abs(candidates[i].column - caller.column);
|
||||
if (dist < bestDistance) {
|
||||
best = candidates[i];
|
||||
bestDistance = dist;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
function getFirstUserFrame(): StackFrame | null {
|
||||
const err = new Error();
|
||||
if (!err.stack) return null;
|
||||
const err = new Error();
|
||||
if (!err.stack) return null;
|
||||
|
||||
const lines = err.stack.split('\n').slice(1);
|
||||
for (const raw of lines) {
|
||||
const frame = parseStackLine(raw);
|
||||
if (!frame) continue;
|
||||
const lines = err.stack.split('\n').slice(1);
|
||||
for (const raw of lines) {
|
||||
const frame = parseStackLine(raw);
|
||||
if (!frame) continue;
|
||||
|
||||
const file = normalizeRuntimeMapPath(frame.file);
|
||||
if (isRuntimeInternalFile(file)) continue;
|
||||
const file = normalizeRuntimeMapPath(frame.file);
|
||||
if (isRuntimeInternalFile(file)) continue;
|
||||
|
||||
return {
|
||||
file,
|
||||
line: frame.line,
|
||||
column: frame.column,
|
||||
};
|
||||
}
|
||||
return {
|
||||
file,
|
||||
line: frame.line,
|
||||
column: frame.column,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseStackLine(line: string): StackFrame | null {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) return null;
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) return null;
|
||||
|
||||
let location = trimmed;
|
||||
const atPrefix = 'at ';
|
||||
if (location.startsWith(atPrefix)) {
|
||||
location = location.slice(atPrefix.length).trim();
|
||||
}
|
||||
let location = trimmed;
|
||||
const atPrefix = 'at ';
|
||||
if (location.startsWith(atPrefix)) {
|
||||
location = location.slice(atPrefix.length).trim();
|
||||
}
|
||||
|
||||
const openParen = location.lastIndexOf('(');
|
||||
if (openParen >= 0 && location.endsWith(')')) {
|
||||
location = location.slice(openParen + 1, -1);
|
||||
}
|
||||
const openParen = location.lastIndexOf('(');
|
||||
if (openParen >= 0 && location.endsWith(')')) {
|
||||
location = location.slice(openParen + 1, -1);
|
||||
}
|
||||
|
||||
const lastColon = location.lastIndexOf(':');
|
||||
if (lastColon < 0) return null;
|
||||
const secondLastColon = location.lastIndexOf(':', lastColon - 1);
|
||||
if (secondLastColon < 0) return null;
|
||||
const lastColon = location.lastIndexOf(':');
|
||||
if (lastColon < 0) return null;
|
||||
const secondLastColon = location.lastIndexOf(':', lastColon - 1);
|
||||
if (secondLastColon < 0) return null;
|
||||
|
||||
const file = location.slice(0, secondLastColon);
|
||||
const linePart = location.slice(secondLastColon + 1, lastColon);
|
||||
const colPart = location.slice(lastColon + 1);
|
||||
const file = location.slice(0, secondLastColon);
|
||||
const linePart = location.slice(secondLastColon + 1, lastColon);
|
||||
const colPart = location.slice(lastColon + 1);
|
||||
|
||||
const parsedLine = Number(linePart);
|
||||
const parsedCol = Number(colPart);
|
||||
if (!Number.isFinite(parsedLine) || !Number.isFinite(parsedCol)) return null;
|
||||
const parsedLine = Number(linePart);
|
||||
const parsedCol = Number(colPart);
|
||||
if (!Number.isFinite(parsedLine) || !Number.isFinite(parsedCol)) return null;
|
||||
|
||||
return {
|
||||
file,
|
||||
line: parsedLine,
|
||||
column: parsedCol,
|
||||
};
|
||||
return {
|
||||
file,
|
||||
line: parsedLine,
|
||||
column: parsedCol,
|
||||
};
|
||||
}
|
||||
|
||||
function isRuntimeInternalFile(file: string): boolean {
|
||||
return file.endsWith('/runtime.ts') || file.endsWith('/runtime.js') || file.includes('/dist/runtime.js');
|
||||
return file.endsWith('/runtime.ts') || file.endsWith('/runtime.js') || file.includes('/dist/runtime.js');
|
||||
}
|
||||
|
||||
function pathsLikelySame(a: string, b: string): boolean {
|
||||
if (a === b) return true;
|
||||
const aBase = basename(a);
|
||||
const bBase = basename(b);
|
||||
return aBase !== '' && aBase === bBase;
|
||||
if (a === b) return true;
|
||||
const aBase = basename(a);
|
||||
const bBase = basename(b);
|
||||
return aBase !== '' && aBase === bBase;
|
||||
}
|
||||
|
||||
function basename(file: string): string {
|
||||
const normalized = normalizeRuntimeMapPath(file);
|
||||
const idx = normalized.lastIndexOf('/');
|
||||
return idx >= 0 ? normalized.slice(idx + 1) : normalized;
|
||||
const normalized = normalizeRuntimeMapPath(file);
|
||||
const idx = normalized.lastIndexOf('/');
|
||||
return idx >= 0 ? normalized.slice(idx + 1) : normalized;
|
||||
}
|
||||
Reference in new issue
Block a user