Spaces:
Running
Running
| # -*- coding: utf-8 -*- | |
| """ | |
| J1K OS v2.2 Final Master - Database Configuration | |
| ⚡ ASYNC Wrapper with HTTP/1.1 Enforcement (To fix Compression Error) | |
| """ | |
| import os | |
| import asyncio | |
| import httpx # 👈 إضافة مهمة | |
| from supabase import create_client | |
| from dotenv import load_dotenv | |
| from supabase.client import ClientOptions | |
| load_dotenv() | |
| url: str = os.environ.get("SUPABASE_URL") | |
| key: str = os.environ.get("SUPABASE_KEY") | |
| if not url or not key: | |
| raise ValueError("🚨 كارثة: مفاتيح قاعدة البيانات مش موجودة!") | |
| # 🛡️ إجبار العميل على استخدام HTTP/1.1 لقتل الـ COMPRESSION_ERROR | |
| # إحنا بنكريت Session يدوي ونقفل فيها الـ http2 | |
| sync_client = httpx.Client( | |
| http2=False, # الأمان أولاً | |
| timeout=60, | |
| limits=httpx.Limits(max_keepalive_connections=20, max_connections=100) # 💎 الحقنة الماسية: خلي الخط مفتوح دايماً | |
| ) | |
| opts = ClientOptions( | |
| postgrest_client_timeout=60, | |
| schema="public" | |
| ) | |
| # إنشاء العميل الـ Sync مع السيشن المخصصة | |
| _sync_supabase = create_client(url, key, options=opts) | |
| _sync_supabase.postgrest.session = sync_client # 👈 الحقنة الماسية هنا | |
| class _AsyncQuery: | |
| __slots__ = ('_q',) | |
| def __init__(self, sync_query): self._q = sync_query | |
| def __getattr__(self, name): | |
| attr = getattr(self._q, name) | |
| if callable(attr): | |
| def chain(*args, **kwargs): return _AsyncQuery(attr(*args, **kwargs)) | |
| return chain | |
| return attr | |
| async def execute(self): | |
| return await asyncio.to_thread(self._q.execute) | |
| class _AsyncTable: | |
| def __init__(self, table_name): self._table = table_name | |
| def __getattr__(self, name): | |
| sync_method = getattr(_sync_supabase.table(self._table), name) | |
| def chain(*args, **kwargs): return _AsyncQuery(sync_method(*args, **kwargs)) | |
| return chain | |
| class AsyncSupabaseClient: | |
| def table(self, name): return _AsyncTable(name) | |
| def __getattr__(self, name): | |
| attr = getattr(_sync_supabase, name) | |
| if callable(attr): | |
| async def async_wrapper(*args, **kwargs): | |
| return await asyncio.to_thread(attr, *args, **kwargs) | |
| return async_wrapper | |
| return attr | |
| supabase = AsyncSupabaseClient() | |
| print("✅ [DATABASE] HTTP/1.1 Enforced - Async Wrapper Ready.") |