parser

package
v3.0.0-rc.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 10 Imported by: 6

Documentation

Index

Constants

View Source
const Protocol = 5

Protocol is the Socket.IO protocol version.

Variables

View Source
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")
)

Error definitions for decoder operations.

View Source
var ErrIllegalAttachments = errors.New("illegal attachments")

ErrIllegalAttachments is returned when a placeholder references an invalid buffer index.

View Source
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

func HasBinary

func HasBinary(data any) bool

HasBinary recursively checks if the data contains any binary content. It traverses slices and maps to detect nested binary data.

func IsBinary

func IsBinary(data any) bool

IsBinary determines if the given data is a binary type. Returns true for []byte and io.Reader types (excluding StringBuffer and strings.Reader), which are treated as binary data in the Socket.IO protocol.

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() Decoder

NewDecoder creates a new Decoder instance.

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.

func NewEncoder

func NewEncoder() Encoder

NewEncoder creates a new Encoder instance.

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.

func NewParser

func NewParser() Parser

NewParser creates a new Parser instance that can create Encoder and Decoder instances for Socket.IO packet handling.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL