blink

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 16 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) {
	log.Fatalln(err)
}

FatalExit ends the program after logging a message

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

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

View Source
var LogInfo = func(msg string) {
	log.Println(msg)
}

LogInfo logs a message as information

Functions

func CollectFileChangeEvents

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

CollectFileChangeEvents gathers filesystem events in a way that web handle functions can use. Performance improvements: 1. Uses a buffered channel for event processing to handle bursts of events 2. Implements event debouncing to reduce duplicate events 3. Uses a separate goroutine for event processing to avoid blocking the main thread 4. Implements periodic cleanup of old events to prevent memory leaks

func EventServer

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

EventServer serves events on a dedicated port. addr is the host address ([host][:port]) The filesystem events are gathered independently of that. Allowed can be "*" or a hostname and sets a header in the SSE stream.

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

GenFileChangeEvents creates an SSE event whenever a file in the server directory changes.

Uses the following HTTP headers:

Content-Type: text/event-stream;charset=utf-8
Cache-Control: no-cache
Connection: keep-alive
Access-Control-Allow-Origin: (custom value)

The "Access-Control-Allow-Origin" header uses the value that is passed in the "allowed" argument.

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 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 Event

type Event fsnotify.Event

func (Event) String

func (e Event) String() string

type EventFilter added in v0.3.0

type EventFilter struct {
	// Include patterns (e.g., "*.js,*.css")
	IncludePatterns []string
	// Exclude patterns (e.g., "node_modules,*.tmp")
	ExcludePatterns []string
	// Include event types (e.g., "write,create")
	IncludeEvents []fsnotify.Op
	// Ignore event types (e.g., "chmod")
	IgnoreEvents []fsnotify.Op
}

EventFilter defines a filter for file system events

func NewEventFilter added in v0.3.0

func NewEventFilter() *EventFilter

NewEventFilter creates a new event filter

func (*EventFilter) SetExcludePatterns added in v0.3.0

func (f *EventFilter) SetExcludePatterns(patterns string)

SetExcludePatterns sets the exclude patterns

func (*EventFilter) SetIgnoreEvents added in v0.3.0

func (f *EventFilter) SetIgnoreEvents(events string)

SetIgnoreEvents sets the ignore event types

func (*EventFilter) SetIncludeEvents added in v0.3.0

func (f *EventFilter) SetIncludeEvents(events string)

SetIncludeEvents sets the include event types

func (*EventFilter) SetIncludePatterns added in v0.3.0

func (f *EventFilter) SetIncludePatterns(patterns string)

SetIncludePatterns sets the include patterns

func (*EventFilter) ShouldInclude added in v0.3.0

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

ShouldInclude checks if an event should be included based on the filter

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 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 WithWebhook added in v0.3.0

func WithWebhook(url string, method string) Option

WithWebhook creates an Option that configures a webhook

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 webhook headers

func WithWebhookRetries added in v0.3.0

func WithWebhookRetries(maxRetries int) Option

WithWebhookRetries creates an Option that sets the maximum number of webhook 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
}

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 TimeEventMap

type TimeEventMap map[time.Time]Event

TimeEventMap stores filesystem events

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
	// Filter to apply to events before sending webhooks
	Filter *EventFilter
}

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 handles a file system event

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
	Time time.Time `json:"time"`
}

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