Documentation
¶
Index ¶
- Constants
- Variables
- func HasBinary(data any) bool
- func IsBinary(data any) bool
- type Decoder
- type DecoderOptions
- func (d *DecoderOptions) Assign(data DecoderOptionsInterface) *DecoderOptions
- func (d *DecoderOptions) GetRawMaxAttachments() types.Optional[uint64]
- func (d *DecoderOptions) GetRawMaxNamespaceLength() types.Optional[int]
- func (d *DecoderOptions) GetRawMaxPacketIDLength() types.Optional[int]
- func (d *DecoderOptions) MaxAttachments() uint64
- func (d *DecoderOptions) MaxNamespaceLength() int
- func (d *DecoderOptions) MaxPacketIDLength() int
- func (d *DecoderOptions) SetMaxAttachments(maxAttachments uint64)
- func (d *DecoderOptions) SetMaxNamespaceLength(maxNamespaceLength int)
- func (d *DecoderOptions) SetMaxPacketIDLength(maxPacketIDLength int)
- type DecoderOptionsInterface
- type Encoder
- type Packet
- type PacketType
- type Parser
- type Placeholder
Constants ¶
const ( // DefaultMaxAttachments is the default maximum number of binary attachments allowed per packet. // This prevents resource exhaustion from malicious clients sending excessively large attachment counts. DefaultMaxAttachments uint64 = 10 // DefaultMaxNamespaceLength is the default maximum allowed length of a namespace name. // This prevents resource exhaustion from malicious clients sending excessively long namespace names. DefaultMaxNamespaceLength int = 512 // DefaultMaxPacketIDLength is the default maximum allowed length of a packet ID string. // uint64 max is 18446744073709551615 (20 digits). DefaultMaxPacketIDLength int = 20 )
const Protocol = 5
Protocol is the Socket.IO protocol version.
Variables ¶
var ( ErrPlaintextDuringReconstruction = errors.New("got plaintext data when reconstructing a packet") ErrBinaryWithoutReconstruction = errors.New("got binary data when not reconstructing a packet") ErrInvalidPayload = errors.New("invalid payload") ErrIllegalNamespace = errors.New("illegal namespace") ErrIllegalID = errors.New("illegal id") ErrTooManyAttachments = errors.New("too many attachments") )
Error definitions for decoder operations.
var ErrIllegalAttachments = errors.New("illegal attachments")
ErrIllegalAttachments is returned when a placeholder references an invalid buffer index.
var ( // ReservedEvents contains event names that have special meaning in Socket.IO // and cannot be used as custom event names. ReservedEvents = types.NewSet( "connect", "connect_error", "disconnect", "disconnecting", "newListener", "removeListener", ) )
Functions ¶
Types ¶
type Decoder ¶
type Decoder interface {
types.EventEmitter
// Add processes incoming data (string or binary).
// Emits "decoded" event when a complete packet is available.
Add(any) error
// Destroy releases resources and stops any ongoing operations.
Destroy()
}
Decoder defines the interface for Socket.IO packet decoding. Implementations parse wire format data into Packet structures.
func NewDecoder ¶
func NewDecoder(opts ...DecoderOptionsInterface) Decoder
NewDecoder creates a new Decoder instance. An optional DecoderOptions can be provided to configure the decoder.
type DecoderOptions ¶
type DecoderOptions struct {
// contains filtered or unexported fields
}
DecoderOptions holds configuration options for the Decoder.
func DefaultDecoderOptions ¶
func DefaultDecoderOptions() *DecoderOptions
func (*DecoderOptions) Assign ¶
func (d *DecoderOptions) Assign(data DecoderOptionsInterface) *DecoderOptions
func (*DecoderOptions) GetRawMaxAttachments ¶
func (d *DecoderOptions) GetRawMaxAttachments() types.Optional[uint64]
func (*DecoderOptions) GetRawMaxNamespaceLength ¶
func (d *DecoderOptions) GetRawMaxNamespaceLength() types.Optional[int]
func (*DecoderOptions) GetRawMaxPacketIDLength ¶
func (d *DecoderOptions) GetRawMaxPacketIDLength() types.Optional[int]
func (*DecoderOptions) MaxAttachments ¶
func (d *DecoderOptions) MaxAttachments() uint64
func (*DecoderOptions) MaxNamespaceLength ¶
func (d *DecoderOptions) MaxNamespaceLength() int
func (*DecoderOptions) MaxPacketIDLength ¶
func (d *DecoderOptions) MaxPacketIDLength() int
func (*DecoderOptions) SetMaxAttachments ¶
func (d *DecoderOptions) SetMaxAttachments(maxAttachments uint64)
MaxAttachments is the maximum number of binary attachments allowed per packet.
func (*DecoderOptions) SetMaxNamespaceLength ¶
func (d *DecoderOptions) SetMaxNamespaceLength(maxNamespaceLength int)
MaxNamespaceLength is the maximum allowed length of a namespace name.
func (*DecoderOptions) SetMaxPacketIDLength ¶
func (d *DecoderOptions) SetMaxPacketIDLength(maxPacketIDLength int)
MaxPacketIDLength is the maximum allowed length of a packet ID string.
type DecoderOptionsInterface ¶
type DecoderOptionsInterface interface {
SetMaxAttachments(uint64)
GetRawMaxAttachments() types.Optional[uint64]
MaxAttachments() uint64
SetMaxNamespaceLength(int)
GetRawMaxNamespaceLength() types.Optional[int]
MaxNamespaceLength() int
SetMaxPacketIDLength(int)
GetRawMaxPacketIDLength() types.Optional[int]
MaxPacketIDLength() int
}
DecoderOptions holds configuration options for the Decoder.
type Encoder ¶
type Encoder interface {
// Encode converts a packet into a sequence of buffers.
// For non-binary packets, returns a single string buffer.
// For binary packets, returns the header buffer followed by binary data buffers.
Encode(*Packet) []types.BufferInterface
}
Encoder defines the interface for Socket.IO packet encoding. Implementations convert Packet structures into wire format.
type Packet ¶
type Packet struct {
// Type is the packet type.
Type PacketType `json:"type" msgpack:"type"`
// Nsp is the namespace this packet belongs to.
Nsp string `json:"nsp" msgpack:"nsp"`
// Data is the payload of the packet.
Data any `json:"data,omitempty" msgpack:"data,omitempty"`
// Id is the optional packet ID for acknowledgment.
Id *uint64 `json:"id,omitempty" msgpack:"id,omitempty"`
// Attachments is the number of binary attachments for binary packets.
Attachments *uint64 `json:"attachments,omitempty" msgpack:"attachments,omitempty"`
}
Packet represents a Socket.IO packet. It contains the type, namespace, data, optional ID for acknowledgment, and the count of binary attachments for binary packets.
func DeconstructPacket ¶
func DeconstructPacket(packet *Packet) (*Packet, []types.BufferInterface)
DeconstructPacket extracts all binary data from a packet and replaces them with numbered placeholders. Returns the modified packet and a slice of buffers containing the extracted binary data.
func ReconstructPacket ¶
func ReconstructPacket(packet *Packet, buffers []types.BufferInterface) (*Packet, error)
ReconstructPacket reconstructs a binary packet from its placeholder packet and the corresponding buffers. It replaces all placeholders with their corresponding binary data from the buffers slice.
type PacketType ¶
type PacketType int
PacketType represents the type of a Socket.IO packet.
const ( // CONNECT is used to establish a connection with the server. CONNECT PacketType = iota // DISCONNECT is used to gracefully close a connection. DISCONNECT // EVENT is used to send an event with data. EVENT // ACK is used to acknowledge a received event. ACK // CONNECT_ERROR is used to report a connection error. CONNECT_ERROR // BINARY_EVENT is used to send an event containing binary data. BINARY_EVENT // BINARY_ACK is used to acknowledge a received binary event. BINARY_ACK )
Socket.IO packet types as defined in the protocol.
func (PacketType) String ¶
func (t PacketType) String() string
String returns the string representation of the packet type.
func (PacketType) Valid ¶
func (t PacketType) Valid() bool
Valid returns true if the packet type is a valid Socket.IO packet type.
type Parser ¶
type Parser interface {
// NewEncoder creates a new Encoder instance.
NewEncoder() Encoder
// NewDecoder creates a new Decoder instance.
NewDecoder() Decoder
}
Parser defines the interface for creating Encoder and Decoder instances. It serves as a factory for the Socket.IO parser components.
type Placeholder ¶
type Placeholder struct {
Placeholder bool `json:"_placeholder" msgpack:"_placeholder"`
Num int64 `json:"num" msgpack:"num"`
}
Placeholder represents a placeholder for binary data in JSON serialization. When a packet contains binary data, the binary is extracted and replaced with a placeholder containing the index into the buffers array.