blink

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exists = func(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}

Exists checks if the given path exists, using os.Stat

View Source
var FatalExit = func(err error) {
	logger.Fatal(err)
}

FatalExit ends the program after logging a message

View Source
var LogError = func(err error) {
	logger.Error(err)
}

LogError logs a message as an error, but does not end the program

View Source
var LogInfo = func(msg string) {
	logger.Info(msg)
}

LogInfo logs a message as information

Functions

func ApplyDevFilter added in v0.7.0

func ApplyDevFilter(filter *EventFilter, path string)

ApplyDevFilter applies development-related exclusion patterns to the given filter

func ApplyGitFilter added in v0.7.0

func ApplyGitFilter(filter *EventFilter, path string)

ApplyGitFilter applies Git-related exclusion patterns to the given filter Kept for backward compatibility

func ApplyPythonFilter added in v0.7.0

func ApplyPythonFilter(filter *EventFilter, path string)

ApplyPythonFilter applies Python-related exclusion patterns to the given filter Kept for backward compatibility

func CollectFileChangeEvents

func CollectFileChangeEvents(ctx context.Context, watcher *Watcher, mut *sync.Mutex, events TimeEventMap, maxAge time.Duration, filter *EventFilter, webhookManager *WebhookManager)

CollectFileChangeEvents collects file change events from the watcher

func EventServer

func EventServer(path, allowed, eventAddr, eventPath string, refreshDuration time.Duration, options ...Option)

EventServer starts a server that serves events over SSE and/or WebSockets

func Flush

func Flush(w http.ResponseWriter) bool

Flush can flush the given ResponseWriter. Returns false if it wasn't an http.Flusher.

func GenFileChangeEvents

func GenFileChangeEvents(events TimeEventMap, mut *sync.Mutex, maxAge time.Duration, allowed string) http.HandlerFunc

func GetDevExcludePatterns added in v0.7.0

func GetDevExcludePatterns() string

GetDevExcludePatterns returns a comma-separated string of patterns to exclude for development projects

func GetGitExcludePatterns added in v0.7.0

func GetGitExcludePatterns() string

GetGitExcludePatterns returns a comma-separated string of patterns to exclude for Git repositories

func GetPythonExcludePatterns added in v0.7.0

func GetPythonExcludePatterns() string

GetPythonExcludePatterns returns a comma-separated string of patterns to exclude for Python projects

func IsDevProject added in v0.7.0

func IsDevProject(path string) bool

IsDevProject checks if the given path is a development project

func IsGitRepository added in v0.7.0

func IsGitRepository(path string) bool

IsGitRepository checks if the given path is a Git repository

func IsPythonProject added in v0.7.0

func IsPythonProject(path string) bool

IsPythonProject checks if the given path contains Python files

func RemoveOldEvents

func RemoveOldEvents(events *TimeEventMap, maxAge time.Duration)

RemoveOldEvents can remove old filesystem events, after a certain duration. Needs to be called within a mutex!

func SetVerbose

func SetVerbose(enabled bool)

SetVerbose can be used to enable or disable logging of incoming events

func ShouldExcludePath added in v0.7.0

func ShouldExcludePath(path string) bool

ShouldExcludePath checks if a path should be excluded based on full path exclusions

func ShouldIgnoreFile

func ShouldIgnoreFile(name string) bool

ShouldIgnoreFile determines if a file or directory should be ignored based on its name. This is a performance-critical function as it's called for every file and directory. Performance improvement: Using a pre-defined list of prefixes to check against.

func Subfolders

func Subfolders(path string) (paths []string)

Subfolders returns a list of all subdirectories in the given path. It follows symbolic links and ignores directories that match the ignore criteria. Performance improvements: 1. Pre-allocates the paths slice with a reasonable capacity 2. Uses a concurrent approach with worker pools for large directory structures 3. Implements early termination for ignored directories

func WriteEvent

func WriteEvent(w http.ResponseWriter, id *uint64, message string, flush bool)

WriteEvent writes SSE events to the given ResponseWriter. id can be nil.

Types

type CustomFilterFunc added in v0.7.0

type CustomFilterFunc func(path string, isDir bool) bool

CustomFilterFunc is a function type for custom path filtering

type Event

type Event fsnotify.Event

func (Event) String

func (e Event) String() string

type EventBatcher added in v0.4.0

type EventBatcher struct {
	// contains filtered or unexported fields
}

EventBatcher batches file system events to reduce redundant processing

func NewEventBatcher added in v0.4.0

func NewEventBatcher(handlerDelay time.Duration) *EventBatcher

NewEventBatcher creates a new event batcher

func (*EventBatcher) Add added in v0.4.0

func (b *EventBatcher) Add(event fsnotify.Event)

Add adds an event to the batch

func (*EventBatcher) AddBatch added in v0.4.0

func (b *EventBatcher) AddBatch(events []fsnotify.Event)

AddBatch adds multiple events to the batch

func (*EventBatcher) Close added in v0.4.0

func (b *EventBatcher) Close()

Close closes the event batcher

func (*EventBatcher) Events added in v0.4.0

func (b *EventBatcher) Events() <-chan []fsnotify.Event

Events returns the channel that receives batched events

type EventFilter added in v0.3.0

type EventFilter struct {
	// contains filtered or unexported fields
}

EventFilter provides filtering capabilities for file system events

func NewEventFilter added in v0.3.0

func NewEventFilter() *EventFilter

NewEventFilter creates a new event filter

func (*EventFilter) AddCustomFilter added in v0.7.0

func (f *EventFilter) AddCustomFilter(filter CustomFilterFunc)

AddCustomFilter adds a custom filter function

func (*EventFilter) GetExcludePatterns added in v0.7.0

func (f *EventFilter) GetExcludePatterns() string

GetExcludePatterns returns the current exclude patterns as a comma-separated string

func (*EventFilter) SetExcludePatterns added in v0.3.0

func (f *EventFilter) SetExcludePatterns(patterns string)

SetExcludePatterns sets the patterns for files to exclude

func (*EventFilter) SetIgnoreEvents added in v0.3.0

func (f *EventFilter) SetIgnoreEvents(events string)

SetIgnoreEvents sets the event types to ignore

func (*EventFilter) SetIncludeEvents added in v0.3.0

func (f *EventFilter) SetIncludeEvents(events string)

SetIncludeEvents sets the event types to include

func (*EventFilter) SetIncludePatterns added in v0.3.0

func (f *EventFilter) SetIncludePatterns(patterns string)

SetIncludePatterns sets the patterns for files to include

func (*EventFilter) ShouldIncludePath added in v0.4.0

func (f *EventFilter) ShouldIncludePath(path string) bool

ShouldIncludePath checks if a path should be included based on patterns

func (*EventFilter) ShouldProcessEvent added in v0.4.0

func (f *EventFilter) ShouldProcessEvent(event fsnotify.Event) bool

ShouldProcessEvent checks if an event should be processed based on event type and path

type EventStreamer added in v0.4.0

type EventStreamer interface {
	// Start initializes and starts the streamer
	Start(ctx context.Context) error

	// Stop gracefully shuts down the streamer
	Stop() error

	// Send delivers an event to all connected clients
	Send(event fsnotify.Event) error
}

EventStreamer is an interface for streaming events to clients

type FilterOption added in v0.3.0

type FilterOption func(*EventFilter)

FilterOption is a function that configures an EventFilter

func WithExcludePatterns added in v0.3.0

func WithExcludePatterns(patterns string) FilterOption

WithExcludePatterns creates a FilterOption that sets the exclude patterns

func WithIgnoreEvents added in v0.3.0

func WithIgnoreEvents(events string) FilterOption

WithIgnoreEvents creates a FilterOption that sets the ignore event types

func WithIncludeEvents added in v0.3.0

func WithIncludeEvents(events string) FilterOption

WithIncludeEvents creates a FilterOption that sets the include event types

func WithIncludePatterns added in v0.3.0

func WithIncludePatterns(patterns string) FilterOption

WithIncludePatterns creates a FilterOption that sets the include patterns

type MultiStreamer added in v0.4.0

type MultiStreamer struct {
	// contains filtered or unexported fields
}

MultiStreamer combines multiple EventStreamers

func NewMultiStreamer added in v0.4.0

func NewMultiStreamer(streamers ...EventStreamer) *MultiStreamer

NewMultiStreamer creates a new multi-streamer

func (*MultiStreamer) Send added in v0.4.0

func (m *MultiStreamer) Send(event fsnotify.Event) error

Send delivers an event to all streamers

func (*MultiStreamer) Start added in v0.4.0

func (m *MultiStreamer) Start(ctx context.Context) error

Start initializes and starts all streamers

func (*MultiStreamer) Stop added in v0.4.0

func (m *MultiStreamer) Stop() error

Stop gracefully shuts down all streamers

type Option added in v0.3.0

type Option func(*Options)

Option is a function that configures Options

func WithFilter added in v0.3.0

func WithFilter(filter *EventFilter) Option

WithFilter creates an Option that sets the event filter

func WithShowEvents added in v0.6.0

func WithShowEvents(show bool) Option

WithShowEvents creates an Option that sets whether to show events in the console

func WithStreamMethod added in v0.4.0

func WithStreamMethod(method StreamMethod) Option

WithStreamMethod creates an Option that sets the stream method

func WithWebhook added in v0.3.0

func WithWebhook(url, method string, headers map[string]string, timeout, debounceDuration time.Duration, maxRetries int) Option

WithWebhook creates an Option that sets the webhook URL and method

func WithWebhookDebounce added in v0.3.0

func WithWebhookDebounce(duration time.Duration) Option

WithWebhookDebounce creates an Option that sets the webhook debounce duration

func WithWebhookHeaders added in v0.3.0

func WithWebhookHeaders(headers map[string]string) Option

WithWebhookHeaders creates an Option that sets the webhook headers

func WithWebhookRetries added in v0.3.0

func WithWebhookRetries(retries int) Option

WithWebhookRetries creates an Option that sets the webhook max retries

func WithWebhookTimeout added in v0.3.0

func WithWebhookTimeout(timeout time.Duration) Option

WithWebhookTimeout creates an Option that sets the webhook timeout

type Options added in v0.3.0

type Options struct {
	// Filter to apply to events
	Filter *EventFilter
	// Webhook URL to send events to
	WebhookURL string
	// HTTP method to use for webhooks
	WebhookMethod string
	// Headers to include in webhook requests
	WebhookHeaders map[string]string
	// Timeout for webhook requests
	WebhookTimeout time.Duration
	// Debounce duration for webhooks
	WebhookDebounceDuration time.Duration
	// Maximum number of retries for webhook requests
	WebhookMaxRetries int
	// Stream method to use
	StreamMethod StreamMethod
	// Show events in the console
	ShowEvents bool
}

Options contains all options for the EventServer

type RecursiveWatcher

type RecursiveWatcher struct {
	*fsnotify.Watcher
	Files   chan string // Channel for file events
	Folders chan string // Channel for folder events
	// contains filtered or unexported fields
}

RecursiveWatcher keeps the data for watching files and directories. It embeds fsnotify.Watcher and adds channels for tracking files and folders.

func NewRecursiveWatcher

func NewRecursiveWatcher(path string) (*RecursiveWatcher, error)

NewRecursiveWatcher creates a new RecursiveWatcher. Takes a path to a directory to watch recursively. Performance improvements: 1. Uses a worker pool for adding folders in parallel 2. Pre-allocates the paths slice with a reasonable capacity 3. Uses a mutex for thread-safe operations

func (*RecursiveWatcher) AddFolder

func (watcher *RecursiveWatcher) AddFolder(folder string) error

AddFolder adds a directory to watch, non-recursively. It's thread-safe due to the mutex lock.

func (*RecursiveWatcher) Close

func (watcher *RecursiveWatcher) Close() error

Close properly closes the watcher and its channels. This method should be called when the watcher is no longer needed to prevent resource leaks.

type SSEStreamer added in v0.4.0

type SSEStreamer struct {
	// contains filtered or unexported fields
}

SSEStreamer implements EventStreamer using Server-Sent Events

func NewSSEStreamer added in v0.4.0

func NewSSEStreamer(opts StreamerOptions) *SSEStreamer

NewSSEStreamer creates a new SSE streamer

func (*SSEStreamer) Send added in v0.4.0

func (s *SSEStreamer) Send(event fsnotify.Event) error

Send delivers an event to all connected clients

func (*SSEStreamer) Start added in v0.4.0

func (s *SSEStreamer) Start(ctx context.Context) error

Start initializes and starts the SSE streamer

func (*SSEStreamer) Stop added in v0.4.0

func (s *SSEStreamer) Stop() error

Stop gracefully shuts down the SSE streamer

type StreamMethod added in v0.4.0

type StreamMethod string

StreamMethod defines the method used for streaming events

const (
	// StreamMethodSSE uses Server-Sent Events for streaming
	StreamMethodSSE StreamMethod = "sse"
	// StreamMethodWebSocket uses WebSockets for streaming
	StreamMethodWebSocket StreamMethod = "websocket"
	// StreamMethodBoth uses both SSE and WebSockets for streaming
	StreamMethodBoth StreamMethod = "both"
)

type StreamerOptions added in v0.4.0

type StreamerOptions struct {
	// Address to listen on ([host][:port])
	Address string

	// Path for the event stream
	Path string

	// AllowedOrigin for CORS (Access-Control-Allow-Origin)
	AllowedOrigin string

	// RefreshDuration for SSE events
	RefreshDuration time.Duration

	// Filter for events
	Filter *EventFilter
}

StreamerOptions contains configuration for event streamers

type TimeEventMap

type TimeEventMap map[time.Time]Event

TimeEventMap stores filesystem events

type Watcher added in v0.4.0

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher provides improved file watching capabilities

func NewWatcher added in v0.4.0

func NewWatcher(ctx context.Context, config WatcherConfig) (*Watcher, error)

NewWatcher creates a new file watcher.

func (*Watcher) Close added in v0.4.0

func (w *Watcher) Close() error

Close stops the watcher and cleans up resources gracefully.

func (*Watcher) Errors added in v0.4.0

func (w *Watcher) Errors() <-chan error

Errors returns a channel that receives errors.

func (*Watcher) Events added in v0.4.0

func (w *Watcher) Events() <-chan []fsnotify.Event

Events returns a channel that receives batched file events.

func (*Watcher) Start added in v0.4.0

func (w *Watcher) Start()

Start begins watching for file changes.

type WatcherConfig added in v0.4.0

type WatcherConfig struct {
	RootPath               string
	IncludePatterns        []string
	ExcludePatterns        []string
	IncludeEvents          []string // e.g., ["create", "write"]
	IgnoreEvents           []string // e.g., ["chmod"]
	Recursive              bool
	HandlerDelay           time.Duration
	PollInterval           time.Duration
	DisableDefaultExcludes bool // New flag to disable default excludes
}

WatcherConfig holds configuration for the watcher

type WebSocketClient added in v0.4.0

type WebSocketClient struct {
	ID         string
	Connection *websocket.Conn
	SendChan   chan []byte
}

WebSocketClient represents a connected WebSocket client

type WebSocketStreamer added in v0.4.0

type WebSocketStreamer struct {
	// contains filtered or unexported fields
}

WebSocketStreamer implements EventStreamer using WebSockets

func NewWebSocketStreamer added in v0.4.0

func NewWebSocketStreamer(opts StreamerOptions) *WebSocketStreamer

NewWebSocketStreamer creates a new WebSocket streamer

func (*WebSocketStreamer) Send added in v0.4.0

func (ws *WebSocketStreamer) Send(event fsnotify.Event) error

Send delivers an event to all connected clients

func (*WebSocketStreamer) Start added in v0.4.0

func (ws *WebSocketStreamer) Start(ctx context.Context) error

Start initializes and starts the WebSocket streamer

func (*WebSocketStreamer) Stop added in v0.4.0

func (ws *WebSocketStreamer) Stop() error

Stop gracefully shuts down the WebSocket streamer

type WebhookConfig added in v0.3.0

type WebhookConfig struct {
	// URL to send the webhook to
	URL string
	// HTTP method to use (GET, POST, PUT, etc.)
	Method string
	// Headers to include in the request
	Headers map[string]string
	// Timeout for the HTTP request
	Timeout time.Duration
	// Debounce duration to avoid sending too many webhooks
	DebounceDuration time.Duration
	// Maximum number of retries for failed requests
	MaxRetries int
}

WebhookConfig defines the configuration for a webhook

type WebhookManager added in v0.3.0

type WebhookManager struct {
	// Configuration for the webhook
	Config WebhookConfig
	// contains filtered or unexported fields
}

WebhookManager manages webhooks for file system events

func NewWebhookManager added in v0.3.0

func NewWebhookManager(config WebhookConfig) *WebhookManager

NewWebhookManager creates a new webhook manager

func (*WebhookManager) HandleEvent added in v0.3.0

func (m *WebhookManager) HandleEvent(event fsnotify.Event)

HandleEvent processes a file system event and sends it to the webhook

type WebhookPayload added in v0.3.0

type WebhookPayload struct {
	// Path of the file that changed
	Path string `json:"path"`
	// Type of event (create, write, remove, rename, chmod)
	EventType string `json:"event_type"`
	// Time the event occurred (RFC3339 format)
	Timestamp string `json:"timestamp"`
}

WebhookPayload is the JSON payload sent to the webhook URL

Jump to

Keyboard shortcuts

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