UNPKG

@hippy/debug-server-next

Version:
78 lines (77 loc) 2.57 kB
"use strict"; /* * Tencent is pleased to support the open source community by making * Hippy available. * * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeHMRData = void 0; const enum_1 = require("@debug-server-next/@types/enum"); const log_1 = require("@debug-server-next/utils/log"); const logger = new log_1.Logger('buffer-decoder', enum_1.WinstonColor.White); const decodeHMRData = (buf) => { const { offset, emitJSON } = decodeEmitJSON(0, buf); const emitList = decodeEmitFiles(offset, buf); return { ...emitJSON, emitList, }; }; exports.decodeHMRData = decodeHMRData; const decodeEmitFiles = (offset, buf) => { const fileNum = buf.readUInt16BE(offset); offset += 2; const emitList = []; for (let i = 0; i < fileNum; i++) { const fnameLen = buf.readUInt8(offset); offset += 1; const name = buf.toString('utf8', offset, (offset += fnameLen)); const lenOfLen = buf.readUInt8(offset); offset += 1; const fn = { 1: 'readUInt8', 2: 'readUInt16BE', 4: 'readUInt32BE', }[lenOfLen]; const fileLen = buf[fn](offset); offset += lenOfLen; const content = buf.slice(offset, (offset += fileLen)); emitList.push({ name, content }); } return emitList; }; const decodeEmitJSON = (offset = 0, buf) => { const lenOfLen = buf.readUInt8(offset); offset += 1; const fn = { 1: 'readUInt8', 2: 'readUInt16BE', 4: 'readUInt32BE', }[lenOfLen]; const jsonLen = buf[fn](offset); offset += lenOfLen; const str = buf.toString('utf8', offset, offset + jsonLen); offset += jsonLen; try { const emitJSON = JSON.parse(str); return { emitJSON, offset }; } catch (e) { logger.error('decodeEmitJSON error: %j', e.stack || e); throw e; } };