Files
Stella/Sources/stella/Decoders/MsgPack.swift
2025-05-23 13:26:13 -04:00

20 lines
887 B
Swift

import SwiftMsgpack
import Vapor
extension MsgPackEncoder: @retroactive ContentEncoder, @retroactive @unchecked Sendable {
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws where E : Encodable {
let data = try self.encode(encodable)
body.writeString(data.base64EncodedString())
headers.contentType = .init(type: "application", subType: "x-msgpack")
}
}
extension MsgPackDecoder: @retroactive ContentDecoder, @retroactive @unchecked Sendable {
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders) throws -> D where D : Decodable {
guard let base64String = body.peekString(length: body.capacity), let data = Data(base64Encoded: base64String) else {
throw Abort(.badRequest)
}
return try self.decode(decodable, from: data)
}
}