Refactor bridge modules: imports & cleanup

Standardize and reorganize imports, type usages and minor formatting across the bridge modules. This commit moves protobuf helpers, makeOidbEnvelope/runOidb, OidbBase and logger imports into consistent places, reorders and groups action/contacts exports in Bridge, removes or trims file-level comments, and fixes whitespace/logic ordering in several highway, element-builder and msg-push files. These are refactors and import/type adjustments across many files under packages/core/src (bridge, highway, msg-push, webui, utils, etc.) with no intended behavioral changes.
This commit is contained in:
手瓜一十雪 committed 2026-05-20 19:35:09 +08:00
1 parent c49ae1c8d9
commit b56868878c
83 files changed
+356 -919

No files matched your search

@@ -1,14 +1,7 @@
// FFmpeg native addon wrapper — port of NapCat's FFmpegAddonAdapter.
// No fallback: throws if the addon isn't available on this platform/arch.
//
// The addon is shipped under `native/ffmpeg/ffmpegAddon.<platform>.<arch>.node`
// to match the layout used by the upstream NapCat build so the binaries
// can be dropped in as-is.
import crypto from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';
import crypto from 'crypto';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -1,17 +1,14 @@
// Highway HTTP client: session fetch and chunked binary upload.
// Port of src/bridge/src/highway_client.cpp
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import net from 'net';
import type { Bridge } from '../bridge';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
import type {
HttpConn0x6FF501Request,
HttpConn0x6FF501Response,
NTV2RichMediaHighwayExt,
ReqDataHighwayHead,
RespDataHighwayHead,
NTV2RichMediaHighwayExt,
} from '../proto/proton/highway';
import { packHighwayFrame, unpackHighwayFrame, computeMd5 } from './utils';
import { computeMd5, packHighwayFrame, unpackHighwayFrame } from './utils';
const HIGHWAY_APP_ID = 1600001604;
const HIGHWAY_BLOCK_SIZE = 1024 * 1024;
@@ -1,13 +1,6 @@
// Image upload: load the source, run NTV2 upload + (optional) Highway PUT,
// return the encoded MsgInfo for the outgoing commonElem.
//
// Shared OIDB envelope + response handling + Highway PUT live in
// pipeline.ts; this file just supplies the image-specific bits (format
// detection, picFormat, bizType, command ids).
import { createLogger } from '../../utils/logger';
import type { Bridge } from '../bridge';
import type { MessageElement } from '../events';
import { createLogger } from '../../utils/logger';
import { GROUP_IMAGE_CMD_ID, PRIVATE_IMAGE_CMD_ID } from './highway-client';
import {
finalizeMediaMsgInfo,
+4 -21
View File
@@ -1,32 +1,15 @@
// Shared NTV2 rich-media upload pipeline.
//
// Image, PTT, and video all follow the same three-step shape:
// 1. send a NTV2UploadRichMediaReq OIDB packet describing the file(s),
// 2. for any sub-file the server signals it wants (uKey present in the
// response), do a Highway HTTP PUT of the bytes,
// 3. encode the returned msgInfo into the bytes that go inside a
// commonElem on the outgoing message.
//
// The per-format files (image-upload, ptt-upload, video-upload) used to
// each implement steps 1 / 2 / 3 inline with subtle drift between them.
// This module pulls the envelope, response decoding, error handling, and
// the Highway PUT loop into one place, leaving each format file with
// only: how to load the file, how to fill the type-specific fileInfo +
// extBizInfo, and which command ids to use.
import crypto from 'crypto';
import type { Bridge } from '../bridge';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import crypto from 'crypto';
import { createLogger } from '../../utils/logger';
import type { Bridge } from '../bridge';
import { makeOidbEnvelope } from '../bridge-oidb';
import type {
EncodableMediaMsgInfo,
NTV2UploadRichMediaReq,
NTV2UploadRichMediaResp,
} from '../proto/proton/highway';
import { buildHighwayExtend, fetchHighwaySession, uploadHighwayHttp } from './highway-client';
import { createLogger } from '../../utils/logger';
import { OidbBase } from '../proto/proton/oidb';
import { buildHighwayExtend, fetchHighwaySession, uploadHighwayHttp } from './highway-client';
const moduleLog = createLogger('Highway');
+3 -11
View File
@@ -1,26 +1,18 @@
// Voice (PTT) upload: stage the audio bytes (convert to NT silk if
// necessary), run NTV2 upload + (optional) Highway PUT, return the
// encoded MsgInfo for the outgoing commonElem.
//
// Shared OIDB envelope + response handling + Highway PUT live in
// pipeline.ts; this file owns the silk transcoding, the temp-file
// cleanup hooks, and the PTT-specific OIDB fields.
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import { createLogger } from '../../utils/logger';
import type { Bridge } from '../bridge';
import type { MessageElement } from '../events';
import { computeHashes, loadBinarySource, resolveLocalFilePath } from './utils';
import { defaultPttTempDir, encodeSilk } from './ffmpeg-addon';
import { createLogger } from '../../utils/logger';
import {
finalizeMediaMsgInfo,
hexToBytes,
runNtv2Upload,
type MediaSubFileUpload,
} from './pipeline';
import { computeHashes, loadBinarySource, resolveLocalFilePath } from './utils';
const moduleLog = createLogger('Highway.Ptt');
+6 -9
View File
@@ -1,6 +1,3 @@
// Highway utilities: binary source loading, hashing, image format detection, frame packing.
// Port of src/bridge/src/highway_utils.h/.cpp
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
@@ -193,7 +190,7 @@ export function detectImageFormat(bytes: Uint8Array): ImageFormat {
// PNG
if (bytes.length >= 24 &&
bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4E && bytes[3] === 0x47) {
bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4E && bytes[3] === 0x47) {
width = readBE32(bytes, 16);
height = readBE32(bytes, 20);
return { format: 1001, width, height };
@@ -201,8 +198,8 @@ export function detectImageFormat(bytes: Uint8Array): ImageFormat {
// GIF
if (bytes.length >= 10 &&
bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 &&
(bytes[4] === 0x37 || bytes[4] === 0x39) && bytes[5] === 0x61) {
bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 &&
(bytes[4] === 0x37 || bytes[4] === 0x39) && bytes[5] === 0x61) {
width = readLE16(bytes, 6);
height = readLE16(bytes, 8);
return { format: 2000, width, height };
@@ -217,8 +214,8 @@ export function detectImageFormat(bytes: Uint8Array): ImageFormat {
// WebP
if (bytes.length >= 30 &&
bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 &&
bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 &&
bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
if (bytes[12] === 0x56 && bytes[13] === 0x50 && bytes[14] === 0x38 && bytes[15] === 0x20) {
width = readLE16(bytes, 26);
height = readLE16(bytes, 28);
@@ -251,7 +248,7 @@ export function detectImageFormat(bytes: Uint8Array): ImageFormat {
// (DAC). The previous `(marker & 0xFC) === 0xC0` check accepted DHT
// and friends, then misread Huffman table bytes as image dimensions.
const isSof = marker >= 0xC0 && marker <= 0xCF
&& marker !== 0xC4 && marker !== 0xC8 && marker !== 0xCC;
&& marker !== 0xC4 && marker !== 0xC8 && marker !== 0xCC;
if (isSof && segLen >= 7 && offset + 9 <= bytes.length) {
height = readBE16(bytes, offset + 5);
width = readBE16(bytes, offset + 7);
@@ -1,33 +1,23 @@
// Video upload: stage the source video and a thumbnail (extracted via
// ffmpeg if not supplied), run NTV2 upload + (optional) Highway PUTs for
// both files, return the encoded MsgInfo for the outgoing commonElem.
//
// Shared OIDB envelope + response handling + Highway PUT loop live in
// pipeline.ts; this file owns the ffmpeg thumb extraction, the 1 MB
// streaming sha1 used by main-video Highway uploads, and the
// video-specific OIDB fields (uploadInfo has TWO entries — main + thumb).
import crypto from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';
import crypto from 'crypto';
import { createLogger } from '../../utils/logger';
import type { Bridge } from '../bridge';
import type { MessageElement } from '../events';
import {
computeHashes,
detectImageFormat,
loadBinarySource,
resolveLocalFilePath,
} from './utils';
import { getFFmpegAddon } from './ffmpeg-addon';
import { createLogger } from '../../utils/logger';
import {
finalizeMediaMsgInfo,
hexToBytes,
runNtv2Upload,
type MediaSubFileUpload,
} from './pipeline';
import {
computeHashes,
detectImageFormat,
loadBinarySource,
resolveLocalFilePath,
} from './utils';
const moduleLog = createLogger('Highway.Video');