"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onHMRServerConnection = exports.onHMRClientConnection = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
// import { throttle } from '@/utils/throttle';
const enum_1 = require("@/@types/enum");
const url_1 = require("@/utils/url");
const log_1 = require("@/utils/log");
const pub_sub_channel_1 = require("@/utils/pub-sub-channel");
const db_1 = require("@/db");
const config_1 = require("@/config");
const buffer_1 = require("@/utils/buffer");
const cos_1 = require("@/utils/cos");
const aegis_1 = require("@/utils/aegis");
const log = new log_1.Logger('hmr-controller', enum_1.WinstonColor.Blue);
const hmrCloseEvent = 'HMR_SERVER_CLOSED';
const onHMRClientConnection = async (ws, wsUrlParams) => {
    const { hash } = wsUrlParams;
    log.verbose('HMR client connected, hash: %s', hash);
    const { Subscriber, DB } = db_1.getDBOperator();
    const bundle = await new DB(config_1.config.redis.bundleTable).get(hash);
    if (!bundle) {
        const reason = 'Hippy dev server not started, not support HMR!';
        ws.close(enum_1.WSCode.InvalidRequestParams, reason);
        return log.warn(reason);
    }
    aegis_1.aegis.reportEvent({
        name: enum_1.ReportEvent.RemoteHMR,
        ext1: hash,
        ext2: enum_1.HMRReportExt2.Client,
    });
    const subscriber = new Subscriber(pub_sub_channel_1.createHMRChannel(hash));
    const hmrHandler = (msg) => {
        if (msg === hmrCloseEvent) {
            subscriber.unsubscribe(hmrHandler);
            subscriber.disconnect();
            const reason = 'Hippy dev server closed, stop HMR!';
            ws.close(enum_1.WSCode.HMRServerClosed, reason);
            log.warn(reason);
        }
        else {
            log.verbose('receive HMR msg from redis: %s', msg);
            const msgObj = JSON.parse(msg.toString());
            if (msgObj.performance) {
                msgObj.performance.serverToApp = Date.now();
            }
            ws.send(JSON.stringify(msgObj));
        }
    };
    subscriber.subscribe(hmrHandler);
    ws.on('close', (code, reason) => {
        log.warn('HMR client ws closed, code: %s, reason: %s', code, reason);
        subscriber.unsubscribe(hmrHandler);
        subscriber.disconnect();
    });
    ws.on('error', (e) => log.error('HMR client ws error: %s', e.stack || e));
};
exports.onHMRClientConnection = onHMRClientConnection;
const onHMRServerConnection = (ws, wsUrlParams) => {
    const { hash } = wsUrlParams;
    log.verbose('HMR server connected, hash: %s', hash);
    const { Publisher, DB } = db_1.getDBOperator();
    const model = new DB(config_1.config.redis.bundleTable);
    model.upsert(hash, { hash });
    const publisher = new Publisher(pub_sub_channel_1.createHMRChannel(hash));
    aegis_1.aegis.reportEvent({
        name: enum_1.ReportEvent.RemoteHMR,
        ext1: hash,
        ext2: enum_1.HMRReportExt2.Server,
    });
    // store all synced dist file, and clean when hmr server disconnect
    const distFiles = new Set();
    ws.on('message', async (msg) => {
        var _a;
        try {
            const serverReceive = Date.now();
            const { emitList, publicPath, ...emitJSON } = buffer_1.decodeHMRData(msg);
            if (emitJSON.performance) {
                const { hadSyncBundleResource } = emitJSON;
                const { pcToServer } = emitJSON.performance;
                const hmrSize = `${Math.ceil(msg.length / 1024)}KB`;
                const reportData = {
                    name: enum_1.ReportEvent.HMRPCToServer,
                    duration: serverReceive - pcToServer,
                    ext1: hmrSize,
                    ext2: '',
                };
                if ('hadSyncBundleResource' in emitJSON)
                    reportData.ext2 = hadSyncBundleResource ? enum_1.HMRSyncType.Patch : enum_1.HMRSyncType.FirstTime;
                aegis_1.aegis.reportTime(reportData);
            }
            const folder = url_1.getBaseFolderOfPublicPath(publicPath);
            emitList.forEach((file) => {
                distFiles.add(path_1.default.join(folder, file.name));
            });
            if (emitList === null || emitList === void 0 ? void 0 : emitList.length) {
                await saveHMRFiles(folder, emitList);
            }
            const msgStr = JSON.stringify(emitJSON);
            log.verbose('receive HMR msg from PC: %s', msgStr);
            setTimeout(() => {
                var _a;
                if ((_a = emitJSON.messages) === null || _a === void 0 ? void 0 : _a.length)
                    publisher.publish(msgStr);
            }, 1000);
        }
        catch (e) {
            log.error('decodeHMRData failed: ', ((_a = e) === null || _a === void 0 ? void 0 : _a.stack) || e);
        }
    });
    ws.on('close', async (code, reason) => {
        log.warn('HMR server ws closed, code: %s, reason: %s', code, reason);
        publisher.publish(hmrCloseEvent);
        process.nextTick(() => {
            publisher.disconnect();
        });
        model.delete(hash);
        cleanHMRFiles(Array.from(distFiles));
    });
    ws.on('error', (e) => log.error('HMR server ws error: %s', e.stack || e));
};
exports.onHMRServerConnection = onHMRServerConnection;
async function saveHMRFiles(folder, emitList) {
    return Promise.all(emitList.map(async ({ name, content }) => {
        const saveFn = {
            [enum_1.StaticFileStorage.COS]: saveHMRFileToCOS,
            [enum_1.StaticFileStorage.Local]: saveHMRFileToLocal,
        }[config_1.config.staticFileStorage];
        return saveFn(folder, name, content);
    }));
}
async function saveHMRFileToLocal(folder, name, content) {
    const fullFname = path_1.default.resolve(config_1.config.hmrStaticPath, folder, name);
    const cacheFolder = path_1.default.dirname(fullFname);
    await fs_1.default.promises.mkdir(cacheFolder, { recursive: true });
    return fs_1.default.promises.writeFile(fullFname, content);
}
async function saveHMRFileToCOS(folder, name, content) {
    const key = path_1.default.join(folder, name);
    return cos_1.cosUpload(key, content);
}
async function cleanHMRFiles(files) {
    try {
        if (files.length === 0)
            return;
        log.warn('clean cached static resources! \n %j', files);
        if (config_1.config.staticFileStorage === enum_1.StaticFileStorage.COS) {
            await cos_1.deleteObjects(files);
        }
        else {
            await Promise.all(files.map((file) => {
                const fullFname = path_1.default.join(config_1.config.hmrStaticPath, file);
                return fs_1.default.promises.rm(fullFname, { force: true, recursive: true });
            }));
        }
    }
    catch (e) {
        log.error('clean cached static resources failed! %j', e.stack || e);
    }
}
