feat: add decoder `msgpack` #134
This commit is contained in:
parent
f94134faa0
commit
7a579d0d0b
|
@ -13,3 +13,4 @@ const DECODE_GZIP = "GZip"
|
|||
const DECODE_DEFLATE = "Deflate"
|
||||
const DECODE_ZSTD = "ZStd"
|
||||
const DECODE_BROTLI = "Brotli"
|
||||
const DECODE_MSGPACK = "Msgpack"
|
||||
|
|
|
@ -15,6 +15,9 @@ func containsBinary(str string) bool {
|
|||
//}
|
||||
rs := []rune(str)
|
||||
for _, r := range rs {
|
||||
if r == unicode.ReplacementChar {
|
||||
return true
|
||||
}
|
||||
if !unicode.IsPrint(r) && !unicode.IsSpace(r) {
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/klauspost/compress/flate"
|
||||
"github.com/klauspost/compress/gzip"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -137,6 +138,11 @@ func autoDecode(str string) (value, resultDecode string) {
|
|||
// resultDecode = types.DECODE_BROTLI
|
||||
// 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
|
||||
for i := 0; i < len(decodeStr); i += 2 {
|
||||
resultStr.WriteString("\\x")
|
||||
resultStr.WriteString(decodeStr[i : i+2])
|
||||
resultStr.WriteString(strings.ToUpper(decodeStr[i : i+2]))
|
||||
}
|
||||
return resultStr.String(), true
|
||||
}
|
||||
|
@ -285,6 +291,22 @@ func decodeBrotli(str string) (string, bool) {
|
|||
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) {
|
||||
value = str
|
||||
switch format {
|
||||
|
@ -352,6 +374,14 @@ func SaveAs(str, format, decode string) (value string, err error) {
|
|||
err = errors.New("fail to build brotli")
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -462,3 +492,14 @@ func encodeBrotli(str string) (string, bool) {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<script setup>
|
||||
import { computed, reactive, ref, watchEffect } from 'vue'
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import Copy from '@/components/icons/Copy.vue'
|
||||
import Save from '@/components/icons/Save.vue'
|
||||
|
@ -82,13 +82,13 @@ const showMemoryUsage = computed(() => {
|
|||
return !isNaN(props.size) && props.size > 0
|
||||
})
|
||||
|
||||
watchEffect(
|
||||
() => {
|
||||
if (props.value !== undefined) {
|
||||
watch(
|
||||
() => props.value,
|
||||
(val) => {
|
||||
if (!isEmpty(val)) {
|
||||
onFormatChanged(viewAs.decode, viewAs.format)
|
||||
}
|
||||
},
|
||||
{ flush: 'post' },
|
||||
)
|
||||
|
||||
const converting = ref(false)
|
||||
|
|
|
@ -58,7 +58,7 @@ const onSelectOptions = async (select) => {
|
|||
t-tooltip="interface.new_group"
|
||||
@click="dialogStore.openNewGroupDialog()" />
|
||||
<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>
|
||||
<n-icon :component="Filter" size="20" />
|
||||
</template>
|
||||
|
|
|
@ -22,6 +22,7 @@ export const decodeTypes = {
|
|||
DEFLATE: 'Deflate',
|
||||
ZSTD: 'ZStd',
|
||||
BROTLI: 'Brotli',
|
||||
MSGPACK: 'Msgpack',
|
||||
// PHP: 'PHP',
|
||||
// Java: 'Java',
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue