DBD-libsql

 view release on metacpan or  search on metacpan

lib/DBD/libsql/Hrana.pm  view on Meta::CPAN

package DBD::libsql::Hrana;
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use JSON;
use Protocol::WebSocket;
use IO::Socket::SSL;
use Carp;

our $VERSION = "0.06";

# Hrana Protocol Client for libSQL
# Based on the Hrana protocol specification used by libsql-client-ts

sub new {
    my ($class, %options) = @_;
    
    my $self = bless {
        url => $options{url} || 'http://127.0.0.1:8080',
        auth_token => $options{auth_token},
        user_agent => LWP::UserAgent->new(
            timeout => $options{timeout} || 30,
            agent => "DBD::libsql/$VERSION",
        ),
        json => JSON->new->utf8,
        connection_id => undef,
        closed => 0,
    }, $class;
    
    # Detect protocol type from URL
    if ($self->{url} =~ /^ws/) {
        $self->{protocol} = 'websocket';
    } else {
        $self->{protocol} = 'http';
    }
    
    return $self;
}

# HTTP-based Hrana implementation
sub connect_http {
    my $self = shift;
    
    # For HTTP, we don't need a persistent connection
    # Each request is stateless
    return 1;
}

# WebSocket-based Hrana implementation  
sub connect_websocket {
    my $self = shift;
    
    # Convert HTTP URL to WebSocket URL for local dev server
    my $ws_url = $self->{url};
    $ws_url =~ s/^http/ws/;
    $ws_url .= '/v2' unless $ws_url =~ /\/v2$/;
    
    eval {
        # For now, we'll implement WebSocket later
        # Just mark as connected for HTTP fallback
        $self->{connection_id} = $self->_generate_connection_id();
        warn "WebSocket connection would connect to: $ws_url" if $ENV{DBD_LIBSQL_DEBUG};
    };
    
    if ($@) {
        croak "Failed to connect via WebSocket: $@";
    }



( run in 2.367 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )