lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -8,89 +8,125 @@ export const MAX_RICH_CARD_OUTPUT_BYTES = 4 * 1024 * 1024;
|
||||
export const MAX_RICH_CARD_MESSAGE_OUTPUT_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
export type DecompressDataResult =
|
||||
| { ok: true; text: string; outputBytes: number }
|
||||
| { ok: false; reason: string };
|
||||
{ ok: true; text: string; outputBytes: number } | { ok: false; reason: string };
|
||||
|
||||
export function makeImageUrl(origUrl: string): string {
|
||||
if (!origUrl) return '';
|
||||
if (/^https?:\/\//i.test(origUrl)) return origUrl;
|
||||
// Compatibility faces sometimes carry a query fragment without a path.
|
||||
// Prefixing the host onto that produces an unusable URL.
|
||||
if (!origUrl.startsWith('/')) return '';
|
||||
if (origUrl.includes('rkey') || origUrl.includes('fileid')) {
|
||||
return 'https://multimedia.nt.qq.com.cn' + origUrl;
|
||||
}
|
||||
return 'http://gchat.qpic.cn' + origUrl;
|
||||
if (!origUrl) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (/^https?:\/\//i.test(origUrl)) {
|
||||
return origUrl;
|
||||
}
|
||||
|
||||
// Compatibility faces sometimes carry a query fragment without a path.
|
||||
// Prefixing the host onto that produces an unusable URL.
|
||||
if (!origUrl.startsWith('/')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (origUrl.includes('rkey') || origUrl.includes('fileid')) {
|
||||
return 'https://multimedia.nt.qq.com.cn' + origUrl;
|
||||
}
|
||||
|
||||
return 'http://gchat.qpic.cn' + origUrl;
|
||||
}
|
||||
|
||||
/** Classic group-pic URL used when the wire carries an MD5 and no download path. */
|
||||
export function imageUrlFromMd5(md5Hex: string): string {
|
||||
if (!/^[0-9a-fA-F]{32}$/.test(md5Hex)) return '';
|
||||
return `http://gchat.qpic.cn/gchatpic_new/0/0-0-${md5Hex.toUpperCase()}/0`;
|
||||
if (!/^[0-9a-fA-F]{32}$/.test(md5Hex)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `http://gchat.qpic.cn/gchatpic_new/0/0-0-${md5Hex.toUpperCase()}/0`;
|
||||
}
|
||||
|
||||
/** NT multimedia download URL reconstructed from a file id when the path is omitted. */
|
||||
export function ntImageUrlFromFileId(fileId: string, isGroup: boolean): string {
|
||||
if (!fileId) return '';
|
||||
const appid = isGroup ? 1407 : 1406;
|
||||
return `https://multimedia.nt.qq.com.cn/download?appid=${appid}&fileid=${encodeURIComponent(fileId)}`;
|
||||
if (!fileId) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const appid = isGroup ? 1407 : 1406;
|
||||
return `https://multimedia.nt.qq.com.cn/download?appid=${appid}&fileid=${encodeURIComponent(fileId)}`;
|
||||
}
|
||||
|
||||
export function decompressData(
|
||||
data: Uint8Array,
|
||||
maxOutputBytes = MAX_RICH_CARD_OUTPUT_BYTES,
|
||||
data: Uint8Array,
|
||||
maxOutputBytes = MAX_RICH_CARD_OUTPUT_BYTES,
|
||||
): DecompressDataResult {
|
||||
if (!data || data.length === 0) return { ok: false, reason: 'empty_payload' };
|
||||
if (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes <= 0) {
|
||||
throw new Error(`invalid decompression output limit: ${maxOutputBytes}`);
|
||||
}
|
||||
if ((data[0] === 0x00 || data[0] === 0x01) && data.length === 1) {
|
||||
return { ok: false, reason: 'marker_without_payload' };
|
||||
}
|
||||
if (data[0] === 0x01) {
|
||||
try {
|
||||
const inflated = inflateSync(Buffer.from(data.subarray(1)), {
|
||||
maxOutputLength: maxOutputBytes,
|
||||
});
|
||||
return inflated.length > 0
|
||||
? { ok: true, text: inflated.toString('utf8'), outputBytes: inflated.length }
|
||||
: { ok: false, reason: 'empty_output' };
|
||||
} catch (error) {
|
||||
const code = typeof error === 'object' && error !== null && 'code' in error
|
||||
? String(error.code)
|
||||
: '';
|
||||
return {
|
||||
ok: false,
|
||||
reason: code === 'ERR_BUFFER_TOO_LARGE'
|
||||
? 'output_limit_exceeded'
|
||||
: code
|
||||
? `inflate_failed:${code}`
|
||||
: 'inflate_failed',
|
||||
};
|
||||
if (!data || data.length === 0) {
|
||||
return { ok: false, reason: 'empty_payload' };
|
||||
}
|
||||
}
|
||||
if (data[0] === 0x00) {
|
||||
if (data.length - 1 > maxOutputBytes) return { ok: false, reason: 'output_limit_exceeded' };
|
||||
return {
|
||||
ok: true,
|
||||
text: Buffer.from(data.subarray(1)).toString('utf8'),
|
||||
outputBytes: data.length - 1,
|
||||
};
|
||||
}
|
||||
if (data.length > maxOutputBytes) return { ok: false, reason: 'output_limit_exceeded' };
|
||||
return { ok: true, text: Buffer.from(data).toString('utf8'), outputBytes: data.length };
|
||||
|
||||
if (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes <= 0) {
|
||||
throw new Error(`invalid decompression output limit: ${maxOutputBytes}`);
|
||||
}
|
||||
|
||||
if ((data[0] === 0x00 || data[0] === 0x01) && data.length === 1) {
|
||||
return { ok: false, reason: 'marker_without_payload' };
|
||||
}
|
||||
|
||||
if (data[0] === 0x01) {
|
||||
try {
|
||||
const inflated = inflateSync(Buffer.from(data.subarray(1)), {
|
||||
maxOutputLength: maxOutputBytes,
|
||||
});
|
||||
return inflated.length > 0
|
||||
? { ok: true, text: inflated.toString('utf8'), outputBytes: inflated.length }
|
||||
: { ok: false, reason: 'empty_output' };
|
||||
} catch (error) {
|
||||
const code =
|
||||
typeof error === 'object' && error !== null && 'code' in error
|
||||
? String(error.code)
|
||||
: '';
|
||||
return {
|
||||
ok: false,
|
||||
reason:
|
||||
code === 'ERR_BUFFER_TOO_LARGE'
|
||||
? 'output_limit_exceeded'
|
||||
: code
|
||||
? `inflate_failed:${code}`
|
||||
: 'inflate_failed',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (data[0] === 0x00) {
|
||||
if (data.length - 1 > maxOutputBytes) {
|
||||
return { ok: false, reason: 'output_limit_exceeded' };
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
text: Buffer.from(data.subarray(1)).toString('utf8'),
|
||||
outputBytes: data.length - 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (data.length > maxOutputBytes) {
|
||||
return { ok: false, reason: 'output_limit_exceeded' };
|
||||
}
|
||||
|
||||
return { ok: true, text: Buffer.from(data).toString('utf8'), outputBytes: data.length };
|
||||
}
|
||||
|
||||
export function parseU64OrZero(value: string): number {
|
||||
if (!value) return 0;
|
||||
const n = parseInt(value, 10);
|
||||
return isNaN(n) ? 0 : n;
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const n = parseInt(value, 10);
|
||||
return isNaN(n) ? 0 : n;
|
||||
}
|
||||
|
||||
/** Sync Identity lookup. Miss is unresolved (0). Decoder must not invent a UIN. */
|
||||
export function resolveUidToUin(identity: IdentityService, groupId: number, uid: string): number {
|
||||
if (!uid) return 0;
|
||||
return identity.findUinByUid(uid, groupId || undefined) ?? 0;
|
||||
if (!uid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return identity.findUinByUid(uid, groupId || undefined) ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,94 +135,144 @@ export function resolveUidToUin(identity: IdentityService, groupId: number, uid:
|
||||
* to a different UID.
|
||||
*/
|
||||
export function c2cRoutingUin(identity: IdentityService, uid: string, wireUin: number): number {
|
||||
const resolved = resolveUidToUin(identity, 0, uid);
|
||||
if (resolved > 0) return resolved;
|
||||
return wireUin > 0 ? wireUin : 0;
|
||||
const resolved = resolveUidToUin(identity, 0, uid);
|
||||
|
||||
if (resolved > 0) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
return wireUin > 0 ? wireUin : 0;
|
||||
}
|
||||
|
||||
const OPERATOR_BYTES_PREVIEW_LENGTH = 32;
|
||||
|
||||
function describeOperatorBytes(bytes: Uint8Array): string {
|
||||
return `bytes=${bytes.length} preview=${hexPreview(bytes, OPERATOR_BYTES_PREVIEW_LENGTH)}`;
|
||||
return `bytes=${bytes.length} preview=${hexPreview(bytes, OPERATOR_BYTES_PREVIEW_LENGTH)}`;
|
||||
}
|
||||
|
||||
function hasControlCharacter(value: string): boolean {
|
||||
for (const char of value) {
|
||||
const codePoint = char.codePointAt(0);
|
||||
if (codePoint !== undefined && (codePoint <= 0x1f || codePoint === 0x7f)) return true;
|
||||
}
|
||||
return false;
|
||||
for (const char of value) {
|
||||
const codePoint = char.codePointAt(0);
|
||||
|
||||
if (codePoint !== undefined && (codePoint <= 0x1f || codePoint === 0x7f)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function assertOperatorUid(uid: string, context: string, bytes: Uint8Array): string {
|
||||
if (hasControlCharacter(uid)) {
|
||||
throw new Error(`${context} operator UID contains control characters (${describeOperatorBytes(bytes)})`);
|
||||
}
|
||||
return uid;
|
||||
if (hasControlCharacter(uid)) {
|
||||
throw new Error(
|
||||
`${context} operator UID contains control characters (${describeOperatorBytes(bytes)})`,
|
||||
);
|
||||
}
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
export function decodeRawOperatorUid(bytes: Uint8Array, context: string): string {
|
||||
if (!bytes || bytes.length === 0) return '';
|
||||
let uid: string;
|
||||
try {
|
||||
uid = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${context} operator UID is not valid UTF-8 (${describeOperatorBytes(bytes)})`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
return assertOperatorUid(uid, context, bytes);
|
||||
if (!bytes || bytes.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let uid: string;
|
||||
|
||||
try {
|
||||
uid = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${context} operator UID is not valid UTF-8 (${describeOperatorBytes(bytes)})`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
|
||||
return assertOperatorUid(uid, context, bytes);
|
||||
}
|
||||
|
||||
export function decodeNestedOperatorUid(bytes: Uint8Array, context: string): string {
|
||||
if (!bytes || bytes.length === 0) return '';
|
||||
let info: OperatorInfo | null;
|
||||
try {
|
||||
info = protobuf_decode<OperatorInfo>(bytes);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${context} operator info decode failed (${describeOperatorBytes(bytes)})`,
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
const uid = info?.operatorField?.uid;
|
||||
if (!uid) {
|
||||
throw new Error(`${context} operator info is missing UID (${describeOperatorBytes(bytes)})`);
|
||||
}
|
||||
return assertOperatorUid(uid, context, bytes);
|
||||
if (!bytes || bytes.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let info: OperatorInfo | null;
|
||||
|
||||
try {
|
||||
info = protobuf_decode<OperatorInfo>(bytes);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`${context} operator info decode failed (${describeOperatorBytes(bytes)})`,
|
||||
{
|
||||
cause: error,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const uid = info?.operatorField?.uid;
|
||||
|
||||
if (!uid) {
|
||||
throw new Error(
|
||||
`${context} operator info is missing UID (${describeOperatorBytes(bytes)})`,
|
||||
);
|
||||
}
|
||||
|
||||
return assertOperatorUid(uid, context, bytes);
|
||||
}
|
||||
|
||||
const OPERATOR_INFO_FIELD_TAG = 0x0a;
|
||||
|
||||
export function decodeGroupChangeOperatorUid(bytes: Uint8Array, context: string): string {
|
||||
if (!bytes || bytes.length === 0) return '';
|
||||
return bytes[0] === OPERATOR_INFO_FIELD_TAG
|
||||
? decodeNestedOperatorUid(bytes, context)
|
||||
: decodeRawOperatorUid(bytes, context);
|
||||
if (!bytes || bytes.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return bytes[0] === OPERATOR_INFO_FIELD_TAG
|
||||
? decodeNestedOperatorUid(bytes, context)
|
||||
: decodeRawOperatorUid(bytes, context);
|
||||
}
|
||||
|
||||
export function buildTemplateMap(params: Array<{ name?: string; value?: string }>): Map<string, string> {
|
||||
const map = new Map<string, string>();
|
||||
for (const p of params) {
|
||||
if (p.name !== undefined) map.set(p.name, p.value ?? '');
|
||||
}
|
||||
return map;
|
||||
export function buildTemplateMap(
|
||||
params: Array<{ name?: string; value?: string }>,
|
||||
): Map<string, string> {
|
||||
const map = new Map<string, string>();
|
||||
|
||||
for (const p of params) {
|
||||
if (p.name !== undefined) {
|
||||
map.set(p.name, p.value ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
export function findTemplateValue(map: Map<string, string>, ...keys: string[]): string {
|
||||
for (const k of keys) {
|
||||
const v = map.get(k);
|
||||
if (v) return v;
|
||||
}
|
||||
return '';
|
||||
for (const k of keys) {
|
||||
const v = map.get(k);
|
||||
|
||||
if (v) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export function unwrapGroupNotifyPayload(content: Uint8Array): Uint8Array | null {
|
||||
if (content.length <= 7) return null;
|
||||
const lenBe = (content[5] << 8) | content[6];
|
||||
const lenLe = content[5] | (content[6] << 8);
|
||||
if (7 + lenBe <= content.length) return content.subarray(7, 7 + lenBe);
|
||||
if (7 + lenLe <= content.length) return content.subarray(7, 7 + lenLe);
|
||||
return content.subarray(7);
|
||||
if (content.length <= 7) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const lenBe = (content[5] << 8) | content[6];
|
||||
const lenLe = content[5] | (content[6] << 8);
|
||||
|
||||
if (7 + lenBe <= content.length) {
|
||||
return content.subarray(7, 7 + lenBe);
|
||||
}
|
||||
|
||||
if (7 + lenLe <= content.length) {
|
||||
return content.subarray(7, 7 + lenLe);
|
||||
}
|
||||
|
||||
return content.subarray(7);
|
||||
}
|
||||
Reference in new issue
Block a user