feat: add decoder `msgpack` #134

This commit is contained in:
Lykin 2024-01-27 01:57:29 +08:00
parent f94134faa0
commit 7a579d0d0b
6 changed files with 53 additions and 7 deletions

View File

@ -13,3 +13,4 @@ const DECODE_GZIP = "GZip"
const DECODE_DEFLATE = "Deflate" const DECODE_DEFLATE = "Deflate"
const DECODE_ZSTD = "ZStd" const DECODE_ZSTD = "ZStd"
const DECODE_BROTLI = "Brotli" const DECODE_BROTLI = "Brotli"
const DECODE_MSGPACK = "Msgpack"

View File

@ -15,6 +15,9 @@ func containsBinary(str string) bool {
//} //}
rs := []rune(str) rs := []rune(str)
for _, r := range rs { for _, r := range rs {
if r == unicode.ReplacementChar {
return true
}
if !unicode.IsPrint(r) && !unicode.IsSpace(r) { if !unicode.IsPrint(r) && !unicode.IsSpace(r) {
return true return true
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/klauspost/compress/flate" "github.com/klauspost/compress/flate"
"github.com/klauspost/compress/gzip" "github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/vmihailenco/msgpack/v5"
"io" "io"
"regexp" "regexp"
"strconv" "strconv"
@ -137,6 +138,11 @@ func autoDecode(str string) (value, resultDecode string) {
// resultDecode = types.DECODE_BROTLI // resultDecode = types.DECODE_BROTLI
// return // return
//} //}
if value, ok = decodeMsgpack(str); ok {
resultDecode = types.DECODE_MSGPACK
return
}
} }
} }
@ -242,7 +248,7 @@ func decodeToHex(str string) (string, bool) {
var resultStr strings.Builder var resultStr strings.Builder
for i := 0; i < len(decodeStr); i += 2 { for i := 0; i < len(decodeStr); i += 2 {
resultStr.WriteString("\\x") resultStr.WriteString("\\x")
resultStr.WriteString(decodeStr[i : i+2]) resultStr.WriteString(strings.ToUpper(decodeStr[i : i+2]))
} }
return resultStr.String(), true return resultStr.String(), true
} }
@ -285,6 +291,22 @@ func decodeBrotli(str string) (string, bool) {
return str, false return str, false
} }
func decodeMsgpack(str string) (string, bool) {
var decodedStr string
if err := msgpack.Unmarshal([]byte(str), &decodedStr); err == nil {
return decodedStr, true
}
var decodedObj map[string]any
if err := msgpack.Unmarshal([]byte(str), &decodedObj); err == nil {
if b, err := json.Marshal(decodedObj); err == nil {
return string(b), true
}
}
return str, false
}
func SaveAs(str, format, decode string) (value string, err error) { func SaveAs(str, format, decode string) (value string, err error) {
value = str value = str
switch format { switch format {
@ -352,6 +374,14 @@ func SaveAs(str, format, decode string) (value string, err error) {
err = errors.New("fail to build brotli") err = errors.New("fail to build brotli")
} }
return return
case types.DECODE_MSGPACK:
if msgpackStr, ok := encodeMsgpack(str); ok {
value = msgpackStr
} else {
err = errors.New("fail to build msgpack")
}
return
} }
return str, nil return str, nil
} }
@ -462,3 +492,14 @@ func encodeBrotli(str string) (string, bool) {
} }
return str, false return str, false
} }
func encodeMsgpack(str string) (string, bool) {
var err error
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
if err = enc.EncodeString(str); err == nil {
return buf.String(), true
}
return str, false
}

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { computed, reactive, ref, watchEffect } from 'vue' import { computed, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import Copy from '@/components/icons/Copy.vue' import Copy from '@/components/icons/Copy.vue'
import Save from '@/components/icons/Save.vue' import Save from '@/components/icons/Save.vue'
@ -82,13 +82,13 @@ const showMemoryUsage = computed(() => {
return !isNaN(props.size) && props.size > 0 return !isNaN(props.size) && props.size > 0
}) })
watchEffect( watch(
() => { () => props.value,
if (props.value !== undefined) { (val) => {
if (!isEmpty(val)) {
onFormatChanged(viewAs.decode, viewAs.format) onFormatChanged(viewAs.decode, viewAs.format)
} }
}, },
{ flush: 'post' },
) )
const converting = ref(false) const converting = ref(false)

View File

@ -58,7 +58,7 @@ const onSelectOptions = async (select) => {
t-tooltip="interface.new_group" t-tooltip="interface.new_group"
@click="dialogStore.openNewGroupDialog()" /> @click="dialogStore.openNewGroupDialog()" />
<n-divider vertical /> <n-divider vertical />
<n-input v-model:value="filterPattern" :placeholder="$t('interface.filter')" clearable> <n-input v-model:value="filterPattern" :autofocus="false" :placeholder="$t('interface.filter')" clearable>
<template #prefix> <template #prefix>
<n-icon :component="Filter" size="20" /> <n-icon :component="Filter" size="20" />
</template> </template>

View File

@ -22,6 +22,7 @@ export const decodeTypes = {
DEFLATE: 'Deflate', DEFLATE: 'Deflate',
ZSTD: 'ZStd', ZSTD: 'ZStd',
BROTLI: 'Brotli', BROTLI: 'Brotli',
MSGPACK: 'Msgpack',
// PHP: 'PHP', // PHP: 'PHP',
// Java: 'Java', // Java: 'Java',
} }