parser

package
v3.0.0-...-caf1bf9 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
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
)
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")
	// ErrInvalidAttachmentCount matches the error exposed by the official
	// Socket.IO parser when a binary packet does not contain a valid
	// "<attachments>-" prefix. ErrIllegalAttachments remains reserved for a
	// placeholder that cannot be reconstructed from the received buffers.
	ErrInvalidAttachmentCount = errors.New("Illegal attachments") //nolint:staticcheck // Official parser compatibility requires this exact text.
	ErrTooManyAttachments     = errors.New("too many attachments")
)

Error definitions for decoder operations.

View Source
var ErrCircularReference = errors.New("circular reference")

ErrCircularReference is raised by Encoder.Encode when packet data contains a cycle that cannot be represented by JSON.

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.

func IsPacketValid

func IsPacketValid(packet *Packet) bool

IsPacketValid reports whether a packet has a payload valid for its type. Packet.Nsp and Packet.Id are already constrained to a string and an optional unsigned integer by Go's type system, which provides the namespace and ACK ID checks performed dynamically by the JavaScript implementation.

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 ...any) Decoder

NewDecoder creates a new Decoder instance. The optional constructor argument can be either DecoderOptionsInterface or JSONReviver, matching the object and legacy function forms accepted by the official parser.

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 (*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) GetRawReviver

func (d *DecoderOptions) GetRawReviver() types.Optional[JSONReviver]

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) Reviver

func (d *DecoderOptions) Reviver() JSONReviver

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.

func (*DecoderOptions) SetReviver

func (d *DecoderOptions) SetReviver(reviver JSONReviver)

SetReviver sets the custom JSON reviver used while decoding packet data.

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 DecoderReviverOptionsInterface

type DecoderReviverOptionsInterface interface {
	GetRawReviver() types.Optional[JSONReviver]
	Reviver() JSONReviver
}

DecoderReviverOptionsInterface is implemented by decoder options that expose a JSON reviver. It is kept separate from DecoderOptionsInterface so existing custom option implementations remain source-compatible.

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 JSONReviver

type JSONReviver func(key string, value any) any

JSONReviver transforms a decoded JSON value. It is called bottom-up for every array item and object property, followed by the root value with an empty key, matching the traversal order of JSON.parse(..., reviver).

type JSONTransformer

type JSONTransformer interface {
	ToJSON() any
}

JSONTransformer exposes the value that should be inspected before JSON encoding. It is the Go equivalent of an object's toJSON() method.

type Packet

type Packet struct {
	// Type is the packet type.
	Type PacketType `json:"type" msgpack:"type" bson:"type"`
	// Nsp is the namespace this packet belongs to.
	Nsp string `json:"nsp" msgpack:"nsp" bson:"nsp"`
	// Data is the payload of the packet.
	Data any `json:"data,omitempty" msgpack:"data,omitempty" bson:"data,omitempty"`
	// Id is the optional packet ID for acknowledgment.
	Id *uint64 `json:"id,omitempty" msgpack:"id,omitempty" bson:"id,omitempty"`
	// Attachments is the number of binary attachments for binary packets.
	Attachments *uint64 `json:"attachments,omitempty" msgpack:"attachments,omitempty" bson:"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