mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
- moejs 迁到 github.com/MoeForge/moejs v0.1.0,Dockerfile、Electron 构建与发布流程的 PGO 路径同步更新;发布附带 THIRD-PARTY-LICENSES.md,并补充 moejs 附带的第三方声明。 - moego 升级到 v0.1.0-alpha.2:宿主 JSON 编解码经 common/kitutil 封装改用 moego/json,根模块和 relaykit 生产代码的 regexp 改用 moego/regexp。 - 内存中的请求体由校验器零拷贝解析;磁盘缓存的请求体经独立文件读取器解码,且只接受一个 JSON 值,尾随内容会被拒绝。 - 模型/分组提取与重复 model 键检测、渠道亲和 JSON 路径、任务协议 stream/background 标志、multipart 解析在磁盘模式下不再把整份请求体读进内存。图片透传直接转发已存储的请求体,并校验最终发往上游的 n。 - 出站请求体各自持有读取位置。此前透传重试共用存储游标,上一次尝试仍在发送时,下一次尝试可能发出残缺的请求体。收到响应头后不再提前关闭请求体,避免中断仍在发送的请求体。 - 表达式计费只在表达式使用 param() 时冻结请求体,其余情况不再读取并保留整份请求体。
114 lines
3.1 KiB
Go
114 lines
3.1 KiB
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
|
|
"github.com/MoeForge/moego/json"
|
|
kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
|
|
"github.com/gin-gonic/gin/binding"
|
|
)
|
|
|
|
type RawMessage = json.RawMessage
|
|
|
|
// hostJSONCodec is the single place where the host chooses its JSON engine.
|
|
// Swap the implementation here (for example to sonic.ConfigStd) and every
|
|
// common.* and kitutil.* JSON helper, including relaykit DTO (un)marshalling,
|
|
// follows. Injected from init() rather than main() so tests run on the same
|
|
// engine as production: common is imported by virtually every root package
|
|
// and test binary, while main() never executes under `go test`.
|
|
type hostJSONCodec struct{}
|
|
|
|
func (hostJSONCodec) Marshal(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (hostJSONCodec) Unmarshal(data []byte, v any) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
func (hostJSONCodec) UnmarshalNoCopy(data []byte, v any) error {
|
|
return json.UnmarshalNoCopy(data, v)
|
|
}
|
|
|
|
func (hostJSONCodec) Decode(r io.Reader, v any) error {
|
|
return json.NewDecoder(r).Decode(v)
|
|
}
|
|
|
|
func (hostJSONCodec) Valid(data []byte) bool {
|
|
return json.Valid(data)
|
|
}
|
|
|
|
func init() {
|
|
kitutil.SetCodec(hostJSONCodec{})
|
|
}
|
|
|
|
func Unmarshal(data []byte, v any) error {
|
|
return kitutil.Unmarshal(data, v)
|
|
}
|
|
|
|
// UnmarshalNoCopy borrows strings and raw JSON from data. Keep data and borrowed
|
|
// raw JSON immutable while decoded values are in use.
|
|
func UnmarshalNoCopy(data []byte, v any) error {
|
|
return kitutil.UnmarshalNoCopy(data, v)
|
|
}
|
|
|
|
func UnmarshalJsonStr(data string, v any) error {
|
|
return kitutil.UnmarshalJsonStr(data, v)
|
|
}
|
|
|
|
func DecodeJson(reader io.Reader, v any) error {
|
|
return kitutil.DecodeJson(reader, v)
|
|
}
|
|
|
|
// DecodeJsonBody reads one JSON value and requires EOF after trailing whitespace.
|
|
// Seekable readers let the decoder process large values through a bounded window.
|
|
func DecodeJsonBody(reader io.Reader, v any) error {
|
|
decoder := json.NewDecoder(reader)
|
|
if err := decoder.Decode(v); err != nil {
|
|
return err
|
|
}
|
|
var extra struct{}
|
|
if err := decoder.Decode(&extra); err != io.EOF {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return errors.New("multiple JSON values in request body")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DecodeJsonWithValidation decodes JSON and applies Gin's configured binding-tag
|
|
// validator, including binding:"required" and any registered custom validators.
|
|
func DecodeJsonWithValidation(reader io.Reader, v any) error {
|
|
if err := DecodeJson(reader, v); err != nil {
|
|
return err
|
|
}
|
|
if binding.Validator == nil {
|
|
return nil
|
|
}
|
|
return binding.Validator.ValidateStruct(v)
|
|
}
|
|
|
|
func Marshal(v any) ([]byte, error) {
|
|
return kitutil.Marshal(v)
|
|
}
|
|
|
|
func IndentJson(data []byte) ([]byte, error) {
|
|
var buffer bytes.Buffer
|
|
if err := json.Indent(&buffer, data, "", " "); err != nil {
|
|
return nil, err
|
|
}
|
|
return buffer.Bytes(), nil
|
|
}
|
|
|
|
func GetJsonType(data RawMessage) string {
|
|
return kitutil.GetJsonType(data)
|
|
}
|
|
|
|
// JsonRawMessageToString returns JSON strings as their decoded value and other JSON values as raw text.
|
|
func JsonRawMessageToString(data RawMessage) string {
|
|
return kitutil.JsonRawMessageToString(data)
|
|
}
|