@kyro-cms/core 0.13.2 → 0.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api-handler-graphql.cjs +1 -1
- package/dist/api-handler-graphql.js +1 -1
- package/dist/api-handler-trpc.cjs +1 -1
- package/dist/api-handler-trpc.js +1 -1
- package/dist/api-handler.cjs +1 -1
- package/dist/api-handler.js +1 -1
- package/dist/base-A9AJPCG5.d.ts +219 -0
- package/dist/base-C6wyX2nC.d.cts +219 -0
- package/dist/bootstrap-PG2LGDLB.js +1 -0
- package/dist/bootstrap-YD3GLTWX.cjs +1 -0
- package/dist/chunk-22RXJ442.js +57 -0
- package/dist/chunk-2SPWRQ5O.js +122 -0
- package/dist/chunk-32QY56XF.js +122 -0
- package/dist/chunk-4NTIK6DF.cjs +2 -0
- package/dist/chunk-6E44MQQ5.js +115 -0
- package/dist/chunk-6LWF5MPL.js +20 -0
- package/dist/chunk-7KLEANTO.cjs +1 -0
- package/dist/chunk-A7LZ6T42.cjs +1 -0
- package/dist/chunk-DSHKQL3R.cjs +121 -0
- package/dist/chunk-ELFQKQLL.js +2 -0
- package/dist/chunk-EQN3HDES.cjs +123 -0
- package/dist/chunk-EW424IBO.cjs +1 -0
- package/dist/chunk-GTR7JAKP.cjs +3 -0
- package/dist/chunk-HPIJI7TM.js +3 -0
- package/dist/chunk-J6V573PE.cjs +1 -0
- package/dist/chunk-JB4WG2YJ.cjs +121 -0
- package/dist/chunk-K73HOMQY.cjs +56 -0
- package/dist/chunk-LBLFU6KI.js +57 -0
- package/dist/chunk-LP3HRZGK.js +57 -0
- package/dist/chunk-MLCBSEBU.js +2 -0
- package/dist/chunk-NRE6VKL6.cjs +121 -0
- package/dist/chunk-O7CBGP4N.cjs +1 -0
- package/dist/chunk-OJ3H4AQ2.cjs +56 -0
- package/dist/chunk-OOVNTOZC.cjs +56 -0
- package/dist/chunk-PYUWSKKP.js +57 -0
- package/dist/chunk-QYN6C3FZ.cjs +114 -0
- package/dist/chunk-RYXLEFS5.js +2 -0
- package/dist/chunk-SRIXRJHL.js +4 -0
- package/dist/chunk-TEY5LZ26.cjs +56 -0
- package/dist/chunk-UWVBFRCW.js +2 -0
- package/dist/chunk-UZWYFEUN.js +122 -0
- package/dist/chunk-V4FAL7AG.js +2 -0
- package/dist/chunk-VOYPKTID.cjs +1 -0
- package/dist/chunk-WAVGRSE6.js +2 -0
- package/dist/chunk-WNU76M2C.js +124 -0
- package/dist/chunk-XDQVYCIR.cjs +1 -0
- package/dist/chunk-XOR64WUQ.cjs +19 -0
- package/dist/chunk-XP7BXFXT.cjs +121 -0
- package/dist/chunk-YGI5W4RS.js +2 -0
- package/dist/chunk-Z2SN2MYP.js +122 -0
- package/dist/cli/index.cjs +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/client.d.cts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/drizzle/index.cjs +1 -1
- package/dist/drizzle/index.d.cts +1 -1
- package/dist/drizzle/index.d.ts +1 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -4
- package/dist/local-6UUGBZ4M.js +1 -0
- package/dist/local-ZYHV3J2K.cjs +1 -0
- package/dist/mongo-auth-adapter-2H2AXDRH.cjs +1 -0
- package/dist/mongo-auth-adapter-YWMEOC42.js +1 -0
- package/dist/mongodb/index.cjs +1 -1
- package/dist/mongodb/index.d.cts +1 -1
- package/dist/mongodb/index.d.ts +1 -1
- package/dist/mongodb/index.js +1 -1
- package/dist/postgres-auth-adapter-QKBNXVPT.js +1 -0
- package/dist/postgres-auth-adapter-WMLM4UUC.cjs +1 -0
- package/dist/redis-adapter-EVK2SMKH.cjs +1 -0
- package/dist/redis-adapter-LY2NBSAG.js +1 -0
- package/dist/rest/index.cjs +1 -1
- package/dist/rest/index.js +1 -1
- package/dist/sqlite-adapter-KPSISY6E.cjs +1 -0
- package/dist/sqlite-adapter-PI6SIY7O.js +1 -0
- package/dist/templates/index.cjs +1 -1
- package/dist/templates/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var chunkF4ITQ6XQ_cjs=require('./chunk-F4ITQ6XQ.cjs'),chunkHTYVI2HK_cjs=require('./chunk-HTYVI2HK.cjs'),chunkBUNLC3II_cjs=require('./chunk-BUNLC3II.cjs'),crypto=require('crypto');chunkBUNLC3II_cjs.h();var g=chunkBUNLC3II_cjs.e(chunkHTYVI2HK_cjs.a(),1);var f="kyro:auth:",A=86400,x=604800,w=class{_redis=null;prefix;tokenExpiration;refreshExpiration;options;constructor(e={}){this.options=e,this.prefix=e.keyPrefix||f,this.tokenExpiration=e.tokenExpiration||A,this.refreshExpiration=e.refreshTokenExpiration||x;}async getRedis(){if(!this._redis){let e,t="ioredis";try{e=(await import(t)).Redis;}catch{chunkF4ITQ6XQ_cjs.a([t]),e=(await import(t)).Redis;}let s=this.options.url||`redis://${this.options.host||"localhost"}:${this.options.port||6379}`;this._redis=new e(s,{password:this.options.password,db:this.options.db,lazyConnect:true,tls:this.options.tls?{}:void 0});}return this._redis}async connect(){await this.getRedis();}async disconnect(){await(await this.getRedis()).quit();}userKey(e){return `${this.prefix}users:${e}`}sessionKey(e){return `${this.prefix}sessions:${e}`}refreshKey(e){return `${this.prefix}refresh:${e}`}userByEmailKey(e){return `${this.prefix}users:email:${e.toLowerCase()}`}passwordHistoryKey(e){return `${this.prefix}users:${e}:password_history`}async createUser(e){let t=crypto.randomBytes(16).toString("hex"),s=new Date().toISOString(),i=await this.hashPassword(e.password),r={id:t,email:e.email.toLowerCase(),passwordHash:i,role:e.role||"customer",tenantId:e.tenantId,createdAt:s,updatedAt:s},n=(await this.getRedis()).pipeline();return n.hset(this.userKey(t),this.userToHash(r)),n.set(this.userByEmailKey(e.email),t),await n.exec(),r}async findUserByEmail(e){let t=await(await this.getRedis()).get(this.userByEmailKey(e.toLowerCase()));return t?this.findUserById(t):null}async findUserById(e){let t=await(await this.getRedis()).hgetall(this.userKey(e));return !t||Object.keys(t).length===0?null:this.hashToUser(t)}async updateUser(e,t){let s=await this.findUserById(e);if(!s)return null;let i={...s,...t,id:e,updatedAt:new Date().toISOString()};if(t.email&&t.email!==s.email){let r=(await this.getRedis()).pipeline();r.del(this.userByEmailKey(s.email)),r.set(this.userByEmailKey(t.email),e),await r.exec();}return await(await this.getRedis()).hset(this.userKey(e),this.userToHash(i)),i}async deleteUser(e){let t=await this.findUserById(e);if(!t)return false;let s=(await this.getRedis()).pipeline();return s.del(this.userKey(e)),s.del(this.userByEmailKey(t.email)),s.del(this.passwordHistoryKey(e)),await s.exec(),true}async hashPassword(e){return g.default.hash(e,12)}async verifyPassword(e,t){let s=await(await this.getRedis()).get(this.userByEmailKey(e));if(!s)return null;let i=await this.findUserById(s);return !i||!i.passwordHash?null:await g.default.compare(t,i.passwordHash)?i:null}async createSession(e,t={}){let s=crypto.randomBytes(32).toString("hex"),i=crypto.randomBytes(32).toString("base64url"),r=crypto.randomBytes(32).toString("base64url"),n=new Date,a={id:s,userId:e,token:i,refreshToken:r,expiresAt:new Date(n.getTime()+this.tokenExpiration*1e3).toISOString(),createdAt:n.toISOString(),ipAddress:t.ipAddress,userAgent:t.userAgent},o=(await this.getRedis()).pipeline();return o.hset(this.sessionKey(s),this.sessionToHash(a)),o.setex(this.refreshKey(r),this.refreshExpiration,s),await o.exec(),a}async findSessionByToken(e){let t=await(await this.getRedis()).hgetall(this.sessionKey(e));return !t||Object.keys(t).length===0?null:this.hashToSession(t)}async findSessionByRefreshToken(e){let t=await(await this.getRedis()).get(this.refreshKey(e));if(!t)return null;let s=await(await this.getRedis()).hgetall(this.sessionKey(t));return !s||Object.keys(s).length===0?null:this.hashToSession(s)}async deleteSession(e){let t=await(await this.getRedis()).hgetall(this.sessionKey(e));if(!t||Object.keys(t).length===0)return false;let s=(await this.getRedis()).pipeline();return s.del(this.sessionKey(e)),t.refreshToken&&s.del(this.refreshKey(t.refreshToken)),await s.exec(),true}async deleteUserSessions(e){let t=`${this.prefix}sessions:*`,s="0",i=0;do{let[r,n]=await(await this.getRedis()).scan(s,"MATCH",t,"COUNT",100);s=r;for(let a of n)if((await(await this.getRedis()).hgetall(a)).userId===e){let l=a.replace(`${this.prefix}sessions:`,"");await this.deleteSession(l),i++;}}while(s!=="0");return i}async addPasswordToHistory(e,t){await(await this.getRedis()).lpush(this.passwordHistoryKey(e),t),await(await this.getRedis()).ltrim(this.passwordHistoryKey(e),0,4);}async getPasswordHistory(e,t=5){return (await this.getRedis()).lrange(this.passwordHistoryKey(e),0,t-1)}async isPasswordInHistory(e,t,s=5){let i=await this.getPasswordHistory(t,s);for(let r of i)if(await g.default.compare(e,r))return true;return false}userToHash(e){let t={id:e.id,email:e.email,passwordHash:e.passwordHash||"",role:e.role,createdAt:e.createdAt,updatedAt:e.updatedAt};return e.avatar&&(t.avatar=e.avatar),e.tenantId&&(t.tenantId=e.tenantId),e.metadata&&(t.metadata=JSON.stringify(e.metadata)),e.emailVerified!==void 0&&(t.emailVerified=String(e.emailVerified)),e.locked!==void 0&&(t.locked=String(e.locked)),e.lastLogin&&(t.lastLogin=e.lastLogin),e.failedLoginAttempts!==void 0&&(t.failedLoginAttempts=String(e.failedLoginAttempts)),t}hashToUser(e){return {id:e.id,email:e.email,passwordHash:e.passwordHash,role:e.role,tenantId:e.tenantId,avatar:e.avatar,metadata:e.metadata?JSON.parse(e.metadata):void 0,createdAt:e.createdAt,updatedAt:e.updatedAt,emailVerified:e.emailVerified==="true",locked:e.locked==="true",lastLogin:e.lastLogin,failedLoginAttempts:e.failedLoginAttempts?parseInt(e.failedLoginAttempts,10):0}}sessionToHash(e){let t={id:e.id,userId:e.userId,token:e.token,expiresAt:e.expiresAt,createdAt:e.createdAt};return e.refreshToken&&(t.refreshToken=e.refreshToken),e.ipAddress&&(t.ipAddress=e.ipAddress),e.userAgent&&(t.userAgent=e.userAgent),t}hashToSession(e){return {id:e.id,userId:e.userId,token:e.token,refreshToken:e.refreshToken,expiresAt:e.expiresAt,createdAt:e.createdAt,ipAddress:e.ipAddress,userAgent:e.userAgent}}tokenKey(e,t){return `${this.prefix}tokens:${e}:${t}`}async createEmailVerificationToken(e){let t=crypto.randomBytes(32).toString("hex"),s=new Date(Date.now()+1440*60*1e3);return await(await this.getRedis()).setex(this.tokenKey("email-verify",t),86400,e),{token:t,expiresAt:s}}async verifyEmailToken(e){let t=await this.getRedis(),s=await t.get(this.tokenKey("email-verify",e));return s?(await t.del(this.tokenKey("email-verify",e)),{success:true,userId:s}):{success:false,error:"Invalid or expired token"}}async createPasswordResetToken(e){let t=await this.findUserByEmail(e);if(!t)return {token:"",expiresAt:new Date,error:"User not found"};let s=crypto.randomBytes(32).toString("hex"),i=new Date(Date.now()+3600*1e3);return await(await this.getRedis()).setex(this.tokenKey("password-reset",s),3600,t.id),{token:s,expiresAt:i}}async resetPasswordWithToken(e,t){let s=await this.getRedis(),i=await s.get(this.tokenKey("password-reset",e));if(!i)return {success:false,error:"Invalid or expired token"};let r=await this.hashPassword(t);return await this.updateUser(i,{passwordHash:r}),await s.del(this.tokenKey("password-reset",e)),{success:true}}auditLogKey(e){return `${this.prefix}audit:logs:${e}`}auditLogIndexKey(){return `${this.prefix}audit:index`}async findAuditLogs(e){let{limit:t=50,offset:s=0}=e,i=this.auditLogIndexKey(),r=await(await this.getRedis()).zrevrange(i,0,-1),n=r.length,a=r.slice(s,s+t),o=[];for(let l of a){let u=await(await this.getRedis()).get(this.auditLogKey(l));if(u){let d=JSON.parse(u);(!e.userId||d.userId===e.userId)&&(!e.action||(Array.isArray(e.action)?e.action.includes(d.action):d.action===e.action))&&(!e.resource||d.resource===e.resource)&&(e.success===void 0||d.success===e.success)&&o.push({...d,timestamp:new Date(d.timestamp)});}}return {logs:o,total:n}}async createAuditLog(e){let t=crypto.randomBytes(16).toString("hex"),i={...e,id:t,timestamp:new Date};await(await this.getRedis()).set(this.auditLogKey(t),JSON.stringify(i)),await(await this.getRedis()).zadd(this.auditLogIndexKey(),Date.now(),t);let r=await(await this.getRedis()).zcard(this.auditLogIndexKey());if(r>1e4){let n=await(await this.getRedis()).zrange(this.auditLogIndexKey(),0,r-10001);for(let a of n)await(await this.getRedis()).del(this.auditLogKey(a)),await(await this.getRedis()).zrem(this.auditLogIndexKey(),a);}return i}};
|
|
2
|
+
exports.a=w;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {a as a$1,d,j,k,e as e$1,h,i}from'./chunk-47CXBWMA.js';import {n,M,p,s}from'./chunk-HRAY65XH.js';import {J}from'./chunk-3NSZZCOJ.js';import {a}from'./chunk-7NH7H3WA.js';import {g as g$1,e}from'./chunk-Y2YAKDEQ.js';import {randomBytes}from'crypto';if (typeof window === "undefined") { const { createRequire } = await import(/* @vite-ignore */ 'module'); createRequire(import.meta.url); }
|
|
2
|
+
g$1();var D=e(a(),1);var g=false,_=class{db;prefix;sessionTTL;refreshTokenTTL;constructor(e){this.db=e.db,this.prefix=e.prefix||"kyro:",this.sessionTTL=e.sessionTTL||86400,this.refreshTokenTTL=e.refreshTokenTTL||604800;}async connect(){if(!g){await this.db.execute(J`
|
|
3
|
+
CREATE TABLE IF NOT EXISTS "users" (
|
|
4
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
5
|
+
"name" VARCHAR(255),
|
|
6
|
+
"email" VARCHAR(255) NOT NULL,
|
|
7
|
+
"password_hash" VARCHAR(255),
|
|
8
|
+
"role" VARCHAR(50) NOT NULL DEFAULT 'customer',
|
|
9
|
+
"tenant_id" UUID,
|
|
10
|
+
"email_verified" BOOLEAN DEFAULT false,
|
|
11
|
+
"locked" BOOLEAN DEFAULT false,
|
|
12
|
+
"last_login" TIMESTAMP,
|
|
13
|
+
"failed_login_attempts" INTEGER DEFAULT 0,
|
|
14
|
+
"metadata" JSONB,
|
|
15
|
+
"avatar" VARCHAR(255),
|
|
16
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
17
|
+
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
18
|
+
)
|
|
19
|
+
`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "avatar" VARCHAR(255)`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "metadata" JSONB`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "tenant_id" UUID`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "email_verified" BOOLEAN DEFAULT false`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "locked" BOOLEAN DEFAULT false`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "last_login" TIMESTAMP`),await this.db.execute(J`ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "failed_login_attempts" INTEGER DEFAULT 0`),await this.db.execute(J`CREATE UNIQUE INDEX IF NOT EXISTS "users_email_idx" ON "users" ("email")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "users_tenant_idx" ON "users" ("tenant_id")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "users_role_idx" ON "users" ("role")`),await this.db.execute(J`
|
|
20
|
+
CREATE TABLE IF NOT EXISTS "sessions" (
|
|
21
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
22
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
23
|
+
"token" VARCHAR(512) NOT NULL UNIQUE,
|
|
24
|
+
"refresh_token" VARCHAR(512),
|
|
25
|
+
"ip_address" VARCHAR(45),
|
|
26
|
+
"user_agent" TEXT,
|
|
27
|
+
"expires_at" TIMESTAMP NOT NULL,
|
|
28
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
29
|
+
)
|
|
30
|
+
`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "sessions_user_idx" ON "sessions" ("user_id")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "sessions_token_idx" ON "sessions" ("token")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "sessions_expires_idx" ON "sessions" ("expires_at")`),await this.db.execute(J`
|
|
31
|
+
CREATE TABLE IF NOT EXISTS "audit_logs" (
|
|
32
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
33
|
+
"action" VARCHAR(100) NOT NULL,
|
|
34
|
+
"user_id" UUID REFERENCES "users"("id") ON DELETE SET NULL,
|
|
35
|
+
"user_email" VARCHAR(255),
|
|
36
|
+
"role" VARCHAR(50),
|
|
37
|
+
"resource" VARCHAR(100) NOT NULL,
|
|
38
|
+
"resource_id" VARCHAR(255),
|
|
39
|
+
"changes" JSONB,
|
|
40
|
+
"ip_address" VARCHAR(45),
|
|
41
|
+
"user_agent" TEXT,
|
|
42
|
+
"success" BOOLEAN NOT NULL DEFAULT true,
|
|
43
|
+
"error" TEXT,
|
|
44
|
+
"metadata" JSONB,
|
|
45
|
+
"timestamp" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
46
|
+
)
|
|
47
|
+
`),await this.db.execute(J`ALTER TABLE "audit_logs" ADD COLUMN IF NOT EXISTS "resource_id" VARCHAR(255)`),await this.db.execute(J`ALTER TABLE "audit_logs" ADD COLUMN IF NOT EXISTS "changes" JSONB`),await this.db.execute(J`ALTER TABLE "audit_logs" ADD COLUMN IF NOT EXISTS "user_agent" TEXT`),await this.db.execute(J`ALTER TABLE "audit_logs" ADD COLUMN IF NOT EXISTS "error" TEXT`),await this.db.execute(J`ALTER TABLE "audit_logs" ADD COLUMN IF NOT EXISTS "metadata" JSONB`);try{await this.db.execute(J`ALTER TABLE "audit_logs" ALTER COLUMN "resource_id" TYPE VARCHAR(255)`);}catch{}await this.db.execute(J`CREATE INDEX IF NOT EXISTS "audit_logs_user_idx" ON "audit_logs" ("user_id")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "audit_logs_action_idx" ON "audit_logs" ("action")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "audit_logs_resource_idx" ON "audit_logs" ("resource")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "audit_logs_timestamp_idx" ON "audit_logs" ("timestamp")`),await this.db.execute(J`
|
|
48
|
+
CREATE TABLE IF NOT EXISTS "password_history" (
|
|
49
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
50
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
51
|
+
"password_hash" VARCHAR(255) NOT NULL,
|
|
52
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
53
|
+
)
|
|
54
|
+
`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "password_history_user_idx" ON "password_history" ("user_id")`),await this.db.execute(J`
|
|
55
|
+
CREATE TABLE IF NOT EXISTS "lockouts" (
|
|
56
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
57
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
58
|
+
"ip_address" VARCHAR(45),
|
|
59
|
+
"reason" VARCHAR(255),
|
|
60
|
+
"locked_until" TIMESTAMP NOT NULL,
|
|
61
|
+
"released_at" TIMESTAMP,
|
|
62
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
63
|
+
)
|
|
64
|
+
`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "lockouts_user_idx" ON "lockouts" ("user_id")`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "lockouts_locked_until_idx" ON "lockouts" ("locked_until")`),await this.db.execute(J`
|
|
65
|
+
CREATE TABLE IF NOT EXISTS "roles" (
|
|
66
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
67
|
+
"name" VARCHAR(100) NOT NULL UNIQUE,
|
|
68
|
+
"level" INTEGER NOT NULL DEFAULT 0,
|
|
69
|
+
"inherits" TEXT[],
|
|
70
|
+
"description" TEXT,
|
|
71
|
+
"permissions" JSONB DEFAULT '[]',
|
|
72
|
+
"is_system" BOOLEAN DEFAULT false,
|
|
73
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
74
|
+
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
75
|
+
)
|
|
76
|
+
`),await this.db.execute(J`CREATE INDEX IF NOT EXISTS "roles_level_idx" ON "roles" ("level")`),await this.db.execute(J`
|
|
77
|
+
CREATE TABLE IF NOT EXISTS "tenants" (
|
|
78
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
79
|
+
"name" VARCHAR(255) NOT NULL,
|
|
80
|
+
"slug" VARCHAR(100) NOT NULL UNIQUE,
|
|
81
|
+
"settings" JSONB DEFAULT '{}',
|
|
82
|
+
"is_active" BOOLEAN DEFAULT true,
|
|
83
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
84
|
+
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
85
|
+
)
|
|
86
|
+
`),await this.db.execute(J`
|
|
87
|
+
CREATE TABLE IF NOT EXISTS "api_keys" (
|
|
88
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
89
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
90
|
+
"name" VARCHAR(255) NOT NULL,
|
|
91
|
+
"key" VARCHAR(64) NOT NULL UNIQUE,
|
|
92
|
+
"key_prefix" VARCHAR(8) NOT NULL,
|
|
93
|
+
"permissions" JSONB DEFAULT '[]',
|
|
94
|
+
"last_used_at" TIMESTAMP,
|
|
95
|
+
"expires_at" TIMESTAMP,
|
|
96
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
97
|
+
)
|
|
98
|
+
`),await this.db.execute(J`
|
|
99
|
+
CREATE TABLE IF NOT EXISTS "email_verifications" (
|
|
100
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
101
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
102
|
+
"token" VARCHAR(64) NOT NULL UNIQUE,
|
|
103
|
+
"expires_at" TIMESTAMP NOT NULL,
|
|
104
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
105
|
+
)
|
|
106
|
+
`),await this.db.execute(J`
|
|
107
|
+
CREATE TABLE IF NOT EXISTS "password_resets" (
|
|
108
|
+
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
109
|
+
"user_id" UUID NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
|
|
110
|
+
"token" VARCHAR(64) NOT NULL UNIQUE,
|
|
111
|
+
"expires_at" TIMESTAMP NOT NULL,
|
|
112
|
+
"used_at" TIMESTAMP,
|
|
113
|
+
"created_at" TIMESTAMP NOT NULL DEFAULT NOW()
|
|
114
|
+
)
|
|
115
|
+
`),g=true;}}async disconnect(){}async createUser(e){let s=await this.hashPassword(e.password),[i]=await this.db.insert(a$1).values({email:e.email.toLowerCase(),name:e.name,passwordHash:s,role:e.role||"customer",avatar:e.avatar,tenantId:e.tenantId,emailVerified:e.emailVerified??true}).returning();return this.userToAuthUser(i)}async findUserByEmail(e){let[s]=await this.db.select().from(a$1).where(n(a$1.email,e.toLowerCase())).limit(1);return s?this.userToAuthUser(s):null}async findUserById(e){let[s]=await this.db.select().from(a$1).where(n(a$1.id,e)).limit(1);return s?this.userToAuthUser(s):null}async updateUser(e,s){let i={updatedAt:new Date};s.name!==void 0&&(i.name=s.name),s.email!==void 0&&(i.email=s.email),s.passwordHash!==void 0&&(i.passwordHash=s.passwordHash),s.role!==void 0&&(i.role=s.role),s.tenantId!==void 0&&(i.tenantId=s.tenantId),s.avatar!==void 0&&(i.avatar=s.avatar),s.emailVerified!==void 0&&(i.emailVerified=s.emailVerified),s.locked!==void 0&&(i.locked=s.locked),s.lastLogin!==void 0&&(i.lastLogin=s.lastLogin?new Date(s.lastLogin):null),s.failedLoginAttempts!==void 0&&(i.failedLoginAttempts=s.failedLoginAttempts);let[r]=await this.db.update(a$1).set(i).where(n(a$1.id,e)).returning();return r?this.userToAuthUser(r):null}async deleteUser(e){return await this.db.delete(a$1).where(n(a$1.id,e)),true}async findUsers(e={}){let s=e.page??1,i=e.limit??10,r=(s-1)*i,u=e.search;if(u){let L=`%${u}%`,[w,[{count:O}]]=await Promise.all([this.db.select().from(a$1).where(J`${a$1.email} ILIKE ${L}`).orderBy(M(a$1.createdAt)).limit(i).offset(r),this.db.select({count:J`count(*)`}).from(a$1).where(J`${a$1.email} ILIKE ${L}`)]);return {users:w.map(A=>this.userToAuthUser(A)),total:Number(O)}}let[E,[{count:c}]]=await Promise.all([this.db.select().from(a$1).orderBy(M(a$1.createdAt)).limit(i).offset(r),this.db.select({count:J`count(*)`}).from(a$1)]);return {users:E.map(L=>this.userToAuthUser(L)),total:Number(c)}}async verifyPassword(e,s){let i=await this.findUserByEmail(e);if(!i)return null;let[r]=await this.db.select().from(a$1).where(n(a$1.email,e.toLowerCase())).limit(1);return r?.passwordHash&&await D.default.compare(s,r.passwordHash)?i:null}async hashPassword(e){return D.default.hash(e,12)}async createSession(e,s){let i=randomBytes(32).toString("base64url"),r=randomBytes(32).toString("base64url"),u=new Date(Date.now()+this.sessionTTL*1e3);new Date(Date.now()+this.refreshTokenTTL*1e3);let [c]=await this.db.insert(d).values({userId:e,token:i,refreshToken:r,ipAddress:s?.ipAddress,userAgent:s?.userAgent,expiresAt:u}).returning();return this.sessionToSession(c)}async findSessionByToken(e){let[s$1]=await this.db.select().from(d).where(p(n(d.token,e),s(d.expiresAt,new Date))).limit(1);return s$1?this.sessionToSession(s$1):null}async findSessionByRefreshToken(e){let[s$1]=await this.db.select().from(d).where(p(n(d.refreshToken,e),s(d.expiresAt,new Date))).limit(1);return s$1?this.sessionToSession(s$1):null}async deleteSession(e){return await this.db.delete(d).where(n(d.id,e)),true}async deleteUserSessions(e){return await this.db.delete(d).where(n(d.userId,e)),1}async addPasswordToHistory(e,s){await this.db.insert(j).values({userId:e,passwordHash:s});}async getPasswordHistory(e,s=5){return (await this.db.select({passwordHash:j.passwordHash}).from(j).where(n(j.userId,e)).orderBy(M(j.createdAt)).limit(s)).map(r=>r.passwordHash)}async isPasswordInHistory(e,s,i=5){let r=await this.getPasswordHistory(s,i);for(let u of r)if(await this.verifyPassword(e,u))return true;return false}async isLocked(e){let[s$1]=await this.db.select().from(k).where(p(n(k.userId,e),s(k.lockedUntil,new Date))).limit(1);return !!s$1}async getLockout(e){let[s$1]=await this.db.select().from(k).where(p(n(k.userId,e),s(k.lockedUntil,new Date))).limit(1);return s$1?{lockedUntil:s$1.lockedUntil}:null}async recordFailedAttempt(e,s){let r=((await this.findUserById(e))?.failedLoginAttempts||0)+1;await this.updateUser(e,{failedLoginAttempts:r});let E=r>=5;return E&&await this.db.insert(k).values({userId:e,ipAddress:s,reason:"Too many failed login attempts",lockedUntil:new Date(Date.now()+9e5)}),{attempts:r,locked:E}}async resetAttempts(e){await this.updateUser(e,{failedLoginAttempts:0});}async findAuditLogs(e){let{limit:s=50,offset:i=0,userId:r,action:u,resource:E,resourceId:c,success:L,startDate:w,endDate:O}=e,A=[];r&&A.push(n(e$1.userId,r)),u&&(Array.isArray(u)?A.push(J`${e$1.action} = ANY(${u})`):A.push(n(e$1.action,u))),E&&A.push(n(e$1.resource,E)),c&&A.push(n(e$1.resourceId,c)),L!==void 0&&A.push(n(e$1.success,L)),w&&A.push(J`${e$1.timestamp} >= ${w}`),O&&A.push(J`${e$1.timestamp} <= ${O}`);let S=A.length>0?p(...A):void 0,b=await this.db.select({count:J`count(*)`}).from(e$1).where(S);return {logs:(await this.db.select().from(e$1).where(S).orderBy(M(e$1.timestamp)).limit(s).offset(i)).map(o=>({id:o.id,timestamp:o.timestamp,action:o.action,userId:o.userId||void 0,userEmail:o.userEmail||void 0,role:o.role||void 0,resource:o.resource,resourceId:o.resourceId||void 0,changes:o.changes||void 0,ipAddress:o.ipAddress||void 0,userAgent:o.userAgent||void 0,success:o.success,error:o.error||void 0,metadata:o.metadata||void 0})),total:Number(b[0]?.count||0)}}async createAuditLog(e){let s=crypto.randomUUID(),i=new Date;return await this.db.insert(e$1).values({id:s,action:e.action,userId:e.userId??null,userEmail:e.userEmail??null,role:e.role??null,resource:e.resource,resourceId:e.resourceId??null,changes:e.changes??null,ipAddress:e.ipAddress??null,userAgent:e.userAgent??null,success:e.success,error:e.error??null,metadata:e.metadata??null,timestamp:i}),{...e,id:s,timestamp:i}}userToAuthUser(e){return {id:e.id,name:e.name||void 0,email:e.email,passwordHash:e.passwordHash||void 0,role:e.role,avatar:e.avatar&&typeof e.avatar=="object"?e.avatar.id||void 0:e.avatar||void 0,tenantId:e.tenantId||void 0,emailVerified:e.emailVerified||false,locked:e.locked||false,lastLogin:e.lastLogin?new Date(e.lastLogin).toISOString():void 0,failedLoginAttempts:e.failedLoginAttempts||0,createdAt:new Date(e.createdAt).toISOString(),updatedAt:new Date(e.updatedAt).toISOString()}}sessionToSession(e){return {id:e.id,userId:e.userId,token:e.token,refreshToken:e.refreshToken||void 0,expiresAt:new Date(e.expiresAt).toISOString(),createdAt:new Date(e.createdAt).toISOString(),ipAddress:e.ipAddress||void 0,userAgent:e.userAgent||void 0}}async createEmailVerificationToken(e){let s=randomBytes(32).toString("hex"),i=new Date(Date.now()+1440*60*1e3);return await this.db.insert(h).values({userId:e,token:s,expiresAt:i}),{token:s,expiresAt:i}}async verifyEmailToken(e){let[s]=await this.db.select().from(h).where(n(h.token,e)).limit(1);return s?s.expiresAt<new Date?{success:false,error:"Verification token has expired"}:(await this.db.update(a$1).set({emailVerified:true}).where(n(a$1.id,s.userId)),await this.db.delete(h).where(n(h.id,s.id)),{success:true,userId:s.userId}):{success:false,error:"Invalid verification token"}}async createPasswordResetToken(e){let s=await this.findUserByEmail(e);if(!s)return {token:"",expiresAt:new Date,error:"User not found"};let i$1=randomBytes(32).toString("hex"),r=new Date(Date.now()+3600*1e3);return await this.db.insert(i).values({userId:s.id,token:i$1,expiresAt:r}),{token:i$1,expiresAt:r}}async resetPasswordWithToken(e,s){let[i$1]=await this.db.select().from(i).where(n(i.token,e)).limit(1);if(!i$1)return {success:false,error:"Invalid reset token"};if(i$1.expiresAt<new Date)return {success:false,error:"Reset token has expired"};if(i$1.usedAt)return {success:false,error:"Reset token has already been used"};let r=await this.hashPassword(s);return await this.db.update(a$1).set({passwordHash:r,updatedAt:new Date}).where(n(a$1.id,i$1.userId)),await this.db.update(i).set({usedAt:new Date}).where(n(i.id,i$1.id)),await this.db.delete(d).where(n(d.userId,i$1.userId)),{success:true}}};export{_ as a};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {a,c,b,d}from'./chunk-SLMIYECU.js';import {a as a$1}from'./chunk-JA44K75G.js';import {g}from'./chunk-Y2YAKDEQ.js';import {createRequire}from'module';import {randomBytes}from'crypto';if (typeof window === "undefined") { const { createRequire } = await import(/* @vite-ignore */ 'module'); createRequire(import.meta.url); }
|
|
2
|
+
g();g();g();function E(m){let t=[];for(let a of m)if(a.type==="tabs"&&"tabs"in a)for(let n of a.tabs)t.push(...E(n.fields));else a.type==="row"&&"fields"in a?t.push(...E(a.fields)):a.type==="collapsible"&&"fields"in a?t.push(...E(a.fields)):t.push(a);return t}function P(m,t){let a=t,n=m[a.name];if(a.type==="json"||a.type==="richtext"||a.type==="array"||a.type==="group"||a.type==="blocks"||a.type==="list"||a.type==="relationship-block")try{n=n?JSON.parse(n):null;}catch{n=null;}if(a.type==="checkbox"&&(n=!!n),a.type==="date"&&n)try{let i=new Date(n);isNaN(i.getTime())?n=null:n=i.toISOString();}catch{n=null;}if((a.type==="upload"||a.type==="image")&&n)try{let i=JSON.parse(n);Array.isArray(i)?n=i.map(o=>typeof o=="object"&&o!==null?o:{id:o}):n=typeof i=="object"?i:{id:i};}catch{n=typeof n=="string"?{id:n}:n;}return n}var R;function U(){return R||(R=createRequire("file:///")("node:sqlite").DatabaseSync,R)}function j(m,t){return Array.isArray(m)?m.map(a=>{if(!a||typeof a!="object")return a;let n=a.data?{...a.data}:void 0;if(!n)return a;let i=t.find(o=>o.slug===a.type||o.slug===a.slug);if(!i||!Array.isArray(i.fields))return a;for(let o of i.fields){if(o.name&&(o.type==="upload"||o.type==="image")&&n[o.name]){let e=n[o.name];Array.isArray(e)?n[o.name]=e.map(s=>typeof s=="string"?{id:s}:s):typeof e=="string"&&(n[o.name]={id:e});}if(o.type==="blocks"&&o.name&&n[o.name]){let e=o.blocks||[];n[o.name]=j(n[o.name],e);}}return {...a,data:n}}):m}function I(m,t){try{return m.prepare(`PRAGMA table_info(${t})`).all().map(n=>n.name)}catch{return []}}var L=class extends a{db;path;migrations=new Map;versionsTableName="kyro_versions";constructor(t){super(),this.path=t.path,t.db?this.db=t.db:this.db=null;}async connect(){if(!this.db)this.db=new(U())(this.path||":memory:");else try{this.db.exec("SELECT 1");}catch{this.db=new(U())(this.path||":memory:");}if(this.db&&typeof this.db.prepare=="function"&&!this.db.prepare.__wrapped){let t=this.db.prepare.bind(this.db),a=n=>{let i=t(n),o=e=>typeof e=="boolean"?e?1:0:e===void 0?null:e;return new Proxy(i,{get(e,s,r){if(s==="all")return (...c)=>e.all(...c.map(o));if(s==="get")return (...c)=>e.get(...c.map(o));if(s==="run")return (...c)=>e.run(...c.map(o));let l=Reflect.get(e,s,r);return typeof l=="function"?l.bind(e):l}})};a.__wrapped=true,this.db.prepare=a;}this.db.exec("PRAGMA journal_mode = WAL"),this.db.exec("PRAGMA foreign_keys = ON"),this.connected=true;}async disconnect(){if(this.db){try{this.db.close();}catch{}this.db=null;}this.connected=false;}ensureTable(t,a){let n=a||this.getTableNameFor(t.slug),i=["id TEXT PRIMARY KEY"];for(let s of E(t.fields)){if(!s.name||s.name==="id")continue;let r=this.fieldToSQL(s);r&&i.push(r);}let o=E(t.fields);o.some(s=>s.name==="createdAt")||i.push(`${this.col("createdAt")} TEXT DEFAULT (datetime('now'))`),o.some(s=>s.name==="updatedAt")||i.push(`${this.col("updatedAt")} TEXT DEFAULT (datetime('now'))`),o.some(s=>s.name==="status")||i.push("status TEXT DEFAULT 'draft'"),o.some(s=>s.name==="hasDraft")||i.push("hasDraft INTEGER DEFAULT 0"),t.tenantScoped&&i.push("tenant_id TEXT NOT NULL");let e=I(this.db,n);if(e.length===0){let s=`CREATE TABLE IF NOT EXISTS ${n} (${i.join(", ")})`;this.db.exec(s),this.db.exec(`CREATE INDEX IF NOT EXISTS idx_${n}_status ON ${n}(status)`);for(let r of E(t.fields))r.name&&r.indexed&&this.db.exec(`CREATE INDEX IF NOT EXISTS idx_${n}_${r.name} ON ${n}(${this.col(r.name)})`),r.name&&r.unique&&this.db.exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_${n}_${r.name}_unique ON ${n}(${this.col(r.name)})`);}else {let s=new Set(e);for(let r of i){let l=r.split(" ")[0].replace(/^"/,"").replace(/"$/,"");if(!s.has(l)&&l!=="id")try{l==="status"?this.db.exec(`ALTER TABLE ${n} ADD COLUMN ${this.col(l)} TEXT DEFAULT 'published'`):l==="hasDraft"?this.db.exec(`ALTER TABLE ${n} ADD COLUMN ${this.col(l)} INTEGER DEFAULT 0`):this.db.exec(`ALTER TABLE ${n} ADD COLUMN ${this.col(l)} TEXT`);}catch{}}}this.migrations.set(n,true);}ensureVersionsTable(){this.db.exec(`
|
|
3
|
+
CREATE TABLE IF NOT EXISTS ${this.versionsTableName} (
|
|
4
|
+
id TEXT PRIMARY KEY,
|
|
5
|
+
collection_slug TEXT NOT NULL,
|
|
6
|
+
document_id TEXT NOT NULL,
|
|
7
|
+
tenant_id TEXT,
|
|
8
|
+
version INTEGER NOT NULL,
|
|
9
|
+
status TEXT NOT NULL DEFAULT 'draft',
|
|
10
|
+
autosave INTEGER NOT NULL DEFAULT 0,
|
|
11
|
+
data TEXT NOT NULL,
|
|
12
|
+
created_by TEXT,
|
|
13
|
+
change_description TEXT,
|
|
14
|
+
published_at TEXT,
|
|
15
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
16
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
17
|
+
)
|
|
18
|
+
`);try{this.db.exec(`ALTER TABLE ${this.versionsTableName} ADD COLUMN autosave INTEGER NOT NULL DEFAULT 0`);}catch{}this.db.exec(`CREATE INDEX IF NOT EXISTS idx_${this.versionsTableName}_doc ON ${this.versionsTableName}(collection_slug, document_id)`),this.db.exec(`CREATE INDEX IF NOT EXISTS idx_${this.versionsTableName}_status ON ${this.versionsTableName}(status)`),this.db.exec(`CREATE INDEX IF NOT EXISTS idx_${this.versionsTableName}_autosave ON ${this.versionsTableName}(autosave)`);}resolveCol(t,a){let n=a.replace(/^-/,""),i=I(this.db,t);if(!i.includes(n)){if(n==="created_at"&&i.includes("createdAt"))return '"createdAt"';if(n==="createdAt"&&i.includes("created_at"))return '"created_at"';if(n==="updated_at"&&i.includes("updatedAt"))return '"updatedAt"';if(n==="updatedAt"&&i.includes("updated_at"))return '"updated_at"'}return this.col(n)}col(t){return `"${t}"`}fieldToSQL(t){switch(t.type){case "text":case "email":case "password":case "textarea":case "color":case "icon":case "code":case "markdown":case "url":return this.col(t.name)+" TEXT";case "number":return this.col(t.name)+" REAL";case "checkbox":return this.col(t.name)+" INTEGER DEFAULT 0";case "date":return this.col(t.name)+" TEXT";case "select":case "radio":return this.col(t.name)+" TEXT";case "relationship":case "upload":case "image":return this.col(t.name)+" TEXT";case "json":case "richtext":case "array":case "group":case "blocks":return this.col(t.name)+" TEXT";default:return null}}parseGlobalsSlug(t){if(t.startsWith("_globals_")){let a=t.replace("_globals_","");return {isGlobal:true,globalSlug:a,tableName:`global_${a.replace(/-/g,"_")}`}}return {isGlobal:false,globalSlug:"",tableName:this.getTableNameFor(t)}}buildWhere(t,a){let n=[],i=[];for(let[o,e]of Object.entries(t)){let s=o.toUpperCase();if((s==="AND"||s==="OR")&&Array.isArray(e)){let l=[];for(let c of e)if(typeof c=="object"&&c!==null){let d=this.buildWhere(c,a);d.clause&&(l.push(`(${d.clause})`),i.push(...d.params));}l.length>0&&n.push(`(${l.join(` ${s} `)})`);continue}let r=this.resolveCol(a,o);typeof e=="object"&&e!==null&&!Array.isArray(e)?(e.equals!==void 0&&(n.push(`${r} = ?`),i.push(e.equals)),e.not_equals!==void 0&&(n.push(`${r} != ?`),i.push(e.not_equals)),e.in!==void 0&&Array.isArray(e.in)&&(e.in.length===0?n.push("1 = 0"):(n.push(`${r} IN (${e.in.map(()=>"?").join(", ")})`),i.push(...e.in))),e.contains!==void 0&&(n.push(`LOWER(${r}) LIKE LOWER(?)`),i.push(`%${e.contains}%`)),e.like!==void 0&&(n.push(`LOWER(${r}) LIKE LOWER(?)`),i.push(e.like)),e.greater_than!==void 0&&(n.push(`${r} > ?`),i.push(e.greater_than)),e.greater_than_equal!==void 0&&(n.push(`${r} >= ?`),i.push(e.greater_than_equal)),e.less_than!==void 0&&(n.push(`${r} < ?`),i.push(e.less_than)),e.less_than_equal!==void 0&&(n.push(`${r} <= ?`),i.push(e.less_than_equal))):e!==void 0&&(n.push(`${r} = ?`),i.push(e));}return {clause:n.join(" AND "),params:i}}async find(t){let{collection:a,where:n={},sort:i,limit:o=10,page:e=1,tenantId:s,select:r,draft:l=false}=t,c$1=this.parseGlobalsSlug(a),d$1=c$1.isGlobal?this.globals.get(c$1.globalSlug):this.getCollection(a);this.ensureTable(d$1,c$1.tableName);let y=c$1.tableName,h=`SELECT * FROM ${y}`,f=[],u=[],g={...n};this.tenantContext&&d$1.tenantScoped&&(g=c({where:g},a,this.tenantContext,b).where||{});let b$1=d$1.fields.find(p=>p.name==="status"),T=b$1?.type==="select"&&Array.isArray(b$1.options)&&b$1.options.some(p=>p.value==="published");if(!l&&T&&(u.push("status = ?"),f.push("published")),s&&d$1.tenantScoped&&(u.push("tenant_id = ?"),f.push(s)),g&&Object.keys(g).length>0){let p=this.buildWhere(g,y);p.clause&&(u.push(`(${p.clause})`),f.push(...p.params));}u.length>0&&(h+=` WHERE ${u.join(" AND ")}`);let $=i||"-createdAt",M=this.resolveCol(y,$),V=$.startsWith("-")?"DESC":"ASC";h+=` ORDER BY ${M} ${V}`;let X=h.replace("SELECT *","SELECT COUNT(*) as count"),S=this.db.prepare(X).get(...f)?.count||0;h+=" LIMIT ? OFFSET ?",f.push(o,(e-1)*o);let N=this.db.prepare(h).all(...f).map(p=>this.rowToDoc(p,d$1));return this.tenantContext&&!this.tenantContext.isSuperAdmin&&(N=N.filter(p=>d(p,a,this.tenantContext,b))),l&&(N=await Promise.all(N.map(async p=>{let D=this.db.prepare(`SELECT * FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? AND tenant_id IS NULL ORDER BY version DESC LIMIT 1`).get(a,p.id);if(D){let G=D.data?JSON.parse(D.data):{};return a$1({...p,...G,status:p.status,_hasUnpublishedChanges:D.status==="draft"})}return p}))),N=N.map(p=>a$1(p)),{docs:N,totalDocs:S,limit:o,totalPages:Math.ceil(S/o),page:e,pagingCounter:(e-1)*o+1,hasPrevPage:e>1,hasNextPage:e<Math.ceil(S/o),prevPage:e>1?e-1:null,nextPage:e<Math.ceil(S/o)?e+1:null}}async findByID(t){let{collection:a,id:n,tenantId:i,draft:o=false}=t,e=this.parseGlobalsSlug(a),s=e.isGlobal?this.globals.get(e.globalSlug):this.getCollection(a);this.ensureTable(s,e.tableName);let l=`SELECT * FROM ${e.tableName} WHERE id = ?`,c=[n];if(this.tenantContext&&s.tenantScoped){let u={id:n,tenant_id:this.tenantContext.tenantId};if(!d(u,a,this.tenantContext,b))return null}let d$1=s.fields.find(u=>u.name==="status"),y=d$1?.type==="select"&&Array.isArray(d$1.options)&&d$1.options.some(u=>u.value==="published");!o&&y&&(l+=" AND status = ?",c.push("published")),i&&s.tenantScoped&&(l+=" AND tenant_id = ?",c.push(i));let h=this.db.prepare(l).get(...c);if(!h)return null;let f=this.rowToDoc(h,s);if(o){let u=this.db.prepare(`SELECT * FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? AND tenant_id IS NULL ORDER BY version DESC LIMIT 1`).get(a,f.id);if(u){let g=u.data?JSON.parse(u.data):{};f={...f,...g,status:f.status,_hasUnpublishedChanges:u.status==="draft"};}}return a$1(f)}async create(t){let{collection:a,data:n,tenantId:i}=t,o=this.parseGlobalsSlug(a),e=o.isGlobal?this.globals.get(o.globalSlug):this.getCollection(a);this.ensureTable(e,o.tableName);let s=o.tableName,r=o.isGlobal?o.globalSlug:n.id||this.generateId(),l=this.prepareData(n,e);l.id=r;let c=new Date().toISOString();l.created_at=c,l.updated_at=c,i&&e.tenantScoped&&(l.tenant_id=i);let d=Object.keys(l),y=I(this.db,s),h={};for(let T of d)y.includes(T)&&(h[T]=l[T]);let f=Object.keys(h),u=f.map(T=>this.col(T)),g=f.map(()=>"?").join(", "),b=Object.values(h).map(T=>T!==null&&typeof T=="object"?JSON.stringify(T):T);return this.db.prepare(`INSERT OR REPLACE INTO ${s} (${u.join(", ")}) VALUES (${g})`).run(...b),this.ensureVersionsTable(),this.findByID({collection:a,id:r,tenantId:i,draft:true})}async update(t){let{collection:a,id:n,data:i,tenantId:o}=t,e=this.parseGlobalsSlug(a),s=e.isGlobal?this.globals.get(e.globalSlug):this.getCollection(a);this.ensureTable(s,e.tableName);let r=e.tableName,l=this.prepareData(i,s);l.updated_at=new Date().toISOString();let c=I(this.db,r),d={};for(let b of Object.keys(l))c.includes(b)&&(d[b]=l[b]);let h=Object.keys(d).map(b=>`${this.col(b)} = ?`).join(", "),f=Object.values(d).map(b=>b!==null&&typeof b=="object"?JSON.stringify(b):b),u=`UPDATE ${r} SET ${h} WHERE id = ?`,g=[...f,n];return o&&s.tenantScoped&&(u+=" AND tenant_id = ?",g.push(o)),this.db.prepare(u).run(...g),this.ensureVersionsTable(),this.findByID({collection:a,id:n,tenantId:o,draft:true})}async delete(t){let{collection:a,id:n,tenantId:i}=t,o=this.parseGlobalsSlug(a),e=o.isGlobal?this.globals.get(o.globalSlug):this.getCollection(a);this.ensureTable(e,o.tableName),this.ensureVersionsTable();let s=await this.findByID({collection:a,id:n,tenantId:i,draft:true});if(!s)throw new Error(`Document not found: ${a}/${n}`);let l=`DELETE FROM ${o.tableName} WHERE id = ?`,c=[n];return i&&e.tenantScoped&&(l+=" AND tenant_id = ?",c.push(i)),this.db.prepare(l).run(...c),s}async count(t){let{collection:a,tenantId:n}=t,i=this.parseGlobalsSlug(a),o=i.isGlobal?this.globals.get(i.globalSlug):this.getCollection(a);this.ensureTable(o,i.tableName);let e=i.tableName,s=`SELECT COUNT(*) as count FROM ${e}`,r=[],l=[];if(n&&o.tenantScoped&&(r.push("tenant_id = ?"),l.push(n)),t.where&&Object.keys(t.where).length>0){let d=this.buildWhere(t.where,e);d.clause&&(r.push(`(${d.clause})`),l.push(...d.params));}return r.length>0&&(s+=` WHERE ${r.join(" AND ")}`),this.db.prepare(s).get(...l)?.count||0}async findOne(t){let a=this.parseGlobalsSlug(t.collection);if(a.isGlobal){let i=this.globals.get(a.globalSlug);if(!i)throw new Error(`Global "${a.globalSlug}" not found in adapter`);this.ensureTable(i,a.tableName);let o=`SELECT * FROM ${a.tableName}`,e=[],s=[],r=i.fields.find(d=>d.name==="status"),l=r?.type==="select"&&Array.isArray(r.options)&&r.options.some(d=>d.value==="published");!t.draft&&i.versions&&l&&e.push("status = 'published'"),e.length>0&&(o+=` WHERE ${e.join(" AND ")}`),o+=" LIMIT 1";let c=this.db.prepare(o).get(...s);if(c){let d=this.rowToDoc(c,i);if(t.draft){let y=this.db.prepare(`SELECT * FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? AND tenant_id IS NULL ORDER BY version DESC LIMIT 1`).get(t.collection,a.globalSlug);if(y){let h=y.data?JSON.parse(y.data):{};d={...d,...h,status:d.status,_hasUnpublishedChanges:y.status==="draft"};}}return d}return null}return (await this.find({...t,limit:1})).docs[0]||null}async findVersions(t){this.ensureVersionsTable();let{collection:a,documentId:n,tenantId:i,limit:o=20,page:e=1}=t,s=["collection_slug = ?","document_id = ?","autosave = 0"],r=[a,n];i?(s.push("tenant_id = ?"),r.push(i)):s.push("tenant_id IS NULL");let l=`WHERE ${s.join(" AND ")}`,d=this.db.prepare(`SELECT COUNT(*) as count FROM ${this.versionsTableName} ${l}`).get(...r)?.count||0,y=(e-1)*o;return {docs:this.db.prepare(`SELECT * FROM ${this.versionsTableName} ${l} ORDER BY version DESC LIMIT ? OFFSET ?`).all(...r,o,y).map(u=>this.rowToVersion(u)),totalDocs:d,limit:o,totalPages:Math.ceil(d/o),page:e,pagingCounter:(e-1)*o+1,hasPrevPage:e>1,hasNextPage:e<Math.ceil(d/o),prevPage:e>1?e-1:null,nextPage:e<Math.ceil(d/o)?e+1:null}}async findVersionByID(t){this.ensureVersionsTable();let a=this.db.prepare(`SELECT * FROM ${this.versionsTableName} WHERE id = ? AND collection_slug = ? LIMIT 1`).get(t.versionId,t.collection);return a?this.rowToVersion(a):null}async createVersion(t){this.ensureVersionsTable();let a=new Date().toISOString();if(t.autosave){let s=`SELECT * FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? AND autosave = 1`,r=[t.collection,t.documentId];t.tenantId?(s+=" AND tenant_id = ?",r.push(t.tenantId)):s+=" AND tenant_id IS NULL",s+=" LIMIT 1";let l=this.db.prepare(s).get(...r);if(l){this.db.prepare(`UPDATE ${this.versionsTableName} SET data = ?, status = ?, updated_at = ? WHERE id = ?`).run(JSON.stringify(t.data),t.status,a,l.id);let c=await this.findVersionByID({collection:t.collection,versionId:l.id});if(c)return c}}let n=this.generateId(),o=(this.db.prepare(`SELECT version FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? AND autosave = 0 ORDER BY version DESC LIMIT 1`).get(t.collection,t.documentId)?.version??0)+1;if(this.db.prepare(`INSERT INTO ${this.versionsTableName} (
|
|
19
|
+
id, collection_slug, document_id, tenant_id, version, status, autosave, data, created_by, change_description, published_at, created_at, updated_at
|
|
20
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(n,t.collection,t.documentId,t.tenantId??null,o,t.status,t.autosave?1:0,JSON.stringify(t.data),t.createdBy??null,t.changeDescription??null,t.status==="published"?a:null,a,a),!t.autosave){let r=this.collections.get(t.collection)?.versions?.maxPerDoc;r&&r>0&&await this.deleteVersions({collection:t.collection,documentId:t.documentId,keepLatest:r,tenantId:t.tenantId});}return await this.findVersionByID({collection:t.collection,versionId:n})}async updateLatestVersion(t){return this.createVersion({...t,autosave:true})}async deleteVersions(t){this.ensureVersionsTable();let{collection:a,documentId:n,keepLatest:i,tenantId:o}=t;if(i&&i>0){let e=this.db.prepare(`SELECT id, status, autosave FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ? ORDER BY version DESC`).all(a,n),s=0,r=[];for(let l of e)l.status==="published"||l.autosave===1||(s++,s>i&&r.push(l.id));for(let l of r)this.db.prepare(`DELETE FROM ${this.versionsTableName} WHERE id = ?`).run(l);}else {let e=`DELETE FROM ${this.versionsTableName} WHERE collection_slug = ? AND document_id = ?`,s=[a,n];o&&(e+=" AND tenant_id = ?",s.push(o)),this.db.prepare(e).run(...s);}}rowToVersion(t){return {id:String(t.id),collection:t.collection_slug,documentId:t.document_id,version:t.version,status:t.status,data:t.data?JSON.parse(t.data):{},createdBy:t.created_by??void 0,changeDescription:t.change_description??void 0,publishedAt:t.published_at??null,createdAt:t.created_at,updatedAt:t.updated_at}}prepareData(t,a){let n={},i=E(a.fields),o=(e,s)=>{let r=e;if(r.type==="json"||r.type==="richtext"||r.type==="array"||r.type==="group"||r.type==="blocks"||r.type==="list"||r.type==="relationship-block")return s!=null?JSON.stringify(s):null;if(r.type==="checkbox")return s?1:0;if(r.type==="number")return s!==null&&s!==""?Number(s):null;if(r.type==="upload"||r.type==="image"){if(s==null)return null;if(Array.isArray(s)){let l=s.map(c=>typeof c=="string"?c:typeof c=="object"?c.id||c._id||c:String(c));return JSON.stringify(l)}return typeof s=="string"?s:typeof s=="object"?JSON.stringify(s):String(s)}else if(e.type==="relationship"){if(s==null)return null;if(Array.isArray(s)){let l=s.map(c=>typeof c=="string"?c:typeof c=="object"?JSON.stringify({relationTo:e.relationTo,value:c.id||c}):String(c));return JSON.stringify(l)}return typeof s=="string"?s:typeof s=="object"?JSON.stringify({relationTo:e.relationTo,value:s.id||s}):String(s)}return s};for(let e of i){if(!e.name||e.name==="id"||a.fields.some(l=>l.type==="tabs"&&l.name&&"tabs"in l&&l.tabs.some(c=>c.fields.some(d=>d.name===e.name))))continue;let r=t[e.name];r!==void 0&&(n[e.name]=o(e,r));}for(let e of a.fields)if(e.type==="tabs"&&"tabs"in e&&e.name){let s=t[e.name];if(s&&typeof s=="object"){for(let r of e.tabs)for(let l of r.fields)if(l.name&&l.name!=="id"){let c=s[l.name];c!==void 0&&(n[l.name]=o(l,c));}}}return n}rowToDoc(t,a){let n={id:t.id};for(let i of E(a.fields)){if(!i.name||i.name==="id")continue;let o=i,e=t[o.name];if(o.type==="json"||o.type==="richtext"||o.type==="array"||o.type==="group"||o.type==="blocks"||o.type==="list"||o.type==="relationship-block")try{e=e?JSON.parse(e):null;}catch{e=null;}if(o.type==="blocks"&&e&&Array.isArray(e)){let s=o.blocks||[];e=j(e,s);}if(i.type==="checkbox"&&(e=!!e),i.type==="date"&&e)try{let s=new Date(e);isNaN(s.getTime())?(console.warn(`[LocalAdapter] Invalid date value for field "${i.name}":`,e),e=null):e=s.toISOString();}catch{e=null;}if((i.type==="upload"||i.type==="image")&&e)try{let s=JSON.parse(e);Array.isArray(s)?e=s.map(r=>typeof r=="object"&&r!==null?r:{id:r}):e=typeof s=="object"?s:{id:s};}catch{e={id:e};}if(i.type==="relationship"&&e)try{let s=JSON.parse(e);Array.isArray(s),e=s;}catch{e={relationTo:Array.isArray(i.relationTo)?i.relationTo[0]:i.relationTo,value:e};}n[i.name]=e;}for(let i of a.fields)if(!(!i.name||i.name==="id"||i.name==="row"||i.name==="collapsible")&&i.type==="tabs"&&"tabs"in i){let o={};for(let e of i.tabs)for(let s of e.fields)s.name&&s.name!=="id"&&(o[s.name]=P(t,s));n[i.name]=o;}if(a.timestamps){let i=t.createdAt||t.created_at,o=t.updatedAt||t.updated_at;if(i)try{let e=new Date(i);isNaN(e.getTime())||(n.createdAt=e.toISOString());}catch{}if(o)try{let e=new Date(o);isNaN(e.getTime())||(n.updatedAt=e.toISOString());}catch{}}a.tenantScoped&&(n.tenantId=t.tenant_id),n.status=t.status??"published",n.hasDraft=t.hasDraft?!!t.hasDraft:false;for(let i of Object.keys(n))/^\d+$/.test(i)&&delete n[i];return n}generateId(){let t=Date.now().toString(16).padStart(12,"0"),a=randomBytes(6).toString("hex");return t+a}getMediaById(t){try{let a=this.getTableNameFor("media"),n=this.db.prepare(`SELECT id, url, thumbnail_url FROM ${a} WHERE id = ?`).get(t);if(n)return {id:n.id,url:n.url,thumbnailUrl:n.thumbnail_url||n.url}}catch{}return null}getTableNameFor(t){return t.replace(/-/g,"_")}async migrate(){for(let t of this.collections.values())this.ensureTable(t);}async rollback(){}async transaction(t){return new Promise((a,n)=>{let i=this.db.transaction(async()=>t({db:this.db}));try{let o=i();a(o);}catch(o){n(o);}})}getDatabase(){return this.db}exec(t){this.db.exec(t);}prepare(t){return this.db.prepare(t)}};function W(m){return new L(m||{})}export{L as a,W as b};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunkOOVNTOZC_cjs=require('./chunk-OOVNTOZC.cjs'),chunkEW424IBO_cjs=require('./chunk-EW424IBO.cjs'),chunkNRE6VKL6_cjs=require('./chunk-NRE6VKL6.cjs'),chunkEQN3HDES_cjs=require('./chunk-EQN3HDES.cjs'),chunkN4FXLQE3_cjs=require('./chunk-N4FXLQE3.cjs'),chunkQYN6C3FZ_cjs=require('./chunk-QYN6C3FZ.cjs'),chunkJ6V573PE_cjs=require('./chunk-J6V573PE.cjs'),chunkBUNLC3II_cjs=require('./chunk-BUNLC3II.cjs'),w=require('kyro:config');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var w__default=/*#__PURE__*/_interopDefault(w);chunkBUNLC3II_cjs.h();exports.a=null;var l=null;async function b(){if(!exports.a){if(globalThis.__KYRO_INSTANCE__)try{await globalThis.__KYRO_INSTANCE__.shutdown();}catch{}try{let e=w__default.default.default||w__default.default,t=e.adapter,o=t&&!t.dialect&&typeof t.connect=="function";if(globalThis.DB&&(!t||o)){let s=globalThis.DB;e.adapter=new chunkN4FXLQE3_cjs.c({type:"sqlite",client:s}),chunkOOVNTOZC_cjs.K.info("Hot-swapped to D1 DrizzleAdapter");}exports.a=chunkOOVNTOZC_cjs.H(e),await exports.a.init();let a=e.adapter,p=a&&a.dialect&&(a.dialect==="postgres"||a.dialect==="sqlite"),c=a&&a.dialect==="mongodb",n;if(p)a.dialect==="postgres"?n=new chunkQYN6C3FZ_cjs.a({db:a.client}):a.dialect==="sqlite"&&(n=new chunkNRE6VKL6_cjs.hb({db:a.rawClient||a.client}));else if(o&&!globalThis.DB){let s=process.env.KYRO_AUTH_DB_PATH||"./data/auth.db";n=new chunkEQN3HDES_cjs.a({path:s});}else c&&(n=new chunkJ6V573PE_cjs.a({adapter:a}));if(n?.connect)try{await n.connect();}catch(s){chunkOOVNTOZC_cjs.K.warn("Auth connect warning:",s);}try{await exports.a.loadSettings();}catch(s){chunkOOVNTOZC_cjs.K.warn("loadSettings warning:",s);}try{await chunkEW424IBO_cjs.e(n);}catch(s){chunkOOVNTOZC_cjs.K.warn("autoBootstrap warning:",s);}globalThis.__KYRO_INSTANCE__=exports.a;}catch(e){throw exports.a=null,l=null,chunkOOVNTOZC_cjs.K.error("Init failed, will retry:",e),e}}}async function E(e){let t=globalThis.DB?globalThis:null;try{let{env:o}=await import('cloudflare:workers');o&&(t=o);}catch{}if(e)try{t=e.locals?.cfContext?.env||e?.env||t;}catch{}if(!t)try{t=process.env;}catch{}t?.DB&&(globalThis.DB=t.DB),t?.STORAGE_BUCKET&&(globalThis.STORAGE_BUCKET=t.STORAGE_BUCKET),l||(l=b()),await l;}var _={graphqlEnabled:false,trpcEnabled:false,wsEnabled:false,requireAuth:false};function T(e){return exports.a?exports.a.settings?.access?.apiAccess?.[e]??_[e]??true:_[e]??true}var R=async e=>{if(!exports.a)try{await Promise.race([E(e),new Promise((t,o)=>setTimeout(()=>o(new Error("Initialization timeout")),3e4))]);}catch(t){return console.error("[Kyro API] Warm error:",t?.stack||t?.message||t),new Response(JSON.stringify({error:"Service Unavailable",message:"Kyro CMS is still starting up. Please try again in a moment."}),{status:503,headers:{"Content-Type":"application/json","Retry-After":"5"}})}try{let o=new URL(e.request.url).pathname,a=__KYRO_API_PATH__;if(o===`${a}/graphql`&&typeof exports.a.getGraphQL=="function"){if(!T("graphqlEnabled"))return new Response(JSON.stringify({error:"GraphQL is disabled"}),{status:503,headers:{"Content-Type":"application/json"}});let n=await exports.a.getGraphQL().fetch(e.request,e.locals);return new Response(n.body,n)}if(o.startsWith(`${a}/trpc/`)&&typeof exports.a.getTRPC=="function"){if(!T("trpcEnabled"))return new Response(JSON.stringify({error:"tRPC is disabled"}),{status:503,headers:{"Content-Type":"application/json"}});let n=await exports.a.getTRPC().fetch(e.request,e.locals);return new Response(n.body,n)}return await(await exports.a.getREST()).fetch(e.request,e.locals)}catch(t){return console.error("[Kyro API Execution Error]:",t?.stack||t?.message||t),new Response(JSON.stringify({error:"Internal Server Error",message:t?.message||String(t),stack:t?.stack}),{status:500,headers:{"Content-Type":"application/json"}})}},L=R;exports.b=E;exports.c=R;exports.d=L;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunkTEY5LZ26_cjs=require('./chunk-TEY5LZ26.cjs'),chunk3O3AVMWM_cjs=require('./chunk-3O3AVMWM.cjs'),chunkJB4WG2YJ_cjs=require('./chunk-JB4WG2YJ.cjs'),chunkOPTVCCQQ_cjs=require('./chunk-OPTVCCQQ.cjs'),chunkN4FXLQE3_cjs=require('./chunk-N4FXLQE3.cjs'),chunkXBQ6HV3T_cjs=require('./chunk-XBQ6HV3T.cjs'),chunkEYHD7HZE_cjs=require('./chunk-EYHD7HZE.cjs'),chunkBUNLC3II_cjs=require('./chunk-BUNLC3II.cjs'),w=require('kyro:config');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var w__default=/*#__PURE__*/_interopDefault(w);chunkBUNLC3II_cjs.h();exports.a=null;var l=null;async function b(){if(!exports.a){if(globalThis.__KYRO_INSTANCE__)try{await globalThis.__KYRO_INSTANCE__.shutdown();}catch{}try{let e=w__default.default.default||w__default.default,t=e.adapter,o=t&&!t.dialect&&typeof t.connect=="function";if(globalThis.DB&&(!t||o)){let s=globalThis.DB;e.adapter=new chunkN4FXLQE3_cjs.c({type:"sqlite",client:s}),chunkTEY5LZ26_cjs.K.info("Hot-swapped to D1 DrizzleAdapter");}exports.a=chunkTEY5LZ26_cjs.H(e),await exports.a.init();let a=e.adapter,p=a&&a.dialect&&(a.dialect==="postgres"||a.dialect==="sqlite"),c=a&&a.dialect==="mongodb",n;if(p)a.dialect==="postgres"?n=new chunkXBQ6HV3T_cjs.a({db:a.client}):a.dialect==="sqlite"&&(n=new chunkJB4WG2YJ_cjs.hb({db:a.rawClient||a.client}));else if(o&&!globalThis.DB){let s=process.env.KYRO_AUTH_DB_PATH||"./data/auth.db";n=new chunkOPTVCCQQ_cjs.a({path:s});}else c&&(n=new chunkEYHD7HZE_cjs.a({adapter:a}));if(n?.connect)try{await n.connect();}catch(s){chunkTEY5LZ26_cjs.K.warn("Auth connect warning:",s);}try{await exports.a.loadSettings();}catch(s){chunkTEY5LZ26_cjs.K.warn("loadSettings warning:",s);}try{await chunk3O3AVMWM_cjs.e(n);}catch(s){chunkTEY5LZ26_cjs.K.warn("autoBootstrap warning:",s);}globalThis.__KYRO_INSTANCE__=exports.a;}catch(e){throw exports.a=null,l=null,chunkTEY5LZ26_cjs.K.error("Init failed, will retry:",e),e}}}async function E(e){let t=globalThis.DB?globalThis:null;try{let{env:o}=await import('cloudflare:workers');o&&(t=o);}catch{}if(e)try{t=e.locals?.cfContext?.env||e?.env||t;}catch{}if(!t)try{t=process.env;}catch{}t?.DB&&(globalThis.DB=t.DB),t?.STORAGE_BUCKET&&(globalThis.STORAGE_BUCKET=t.STORAGE_BUCKET),l||(l=b()),await l;}var _={graphqlEnabled:false,trpcEnabled:false,wsEnabled:false,requireAuth:false};function T(e){return exports.a?exports.a.settings?.access?.apiAccess?.[e]??_[e]??true:_[e]??true}var R=async e=>{if(!exports.a)try{await Promise.race([E(e),new Promise((t,o)=>setTimeout(()=>o(new Error("Initialization timeout")),3e4))]);}catch(t){return console.error("[Kyro API] Warm error:",t?.stack||t?.message||t),new Response(JSON.stringify({error:"Service Unavailable",message:"Kyro CMS is still starting up. Please try again in a moment."}),{status:503,headers:{"Content-Type":"application/json","Retry-After":"5"}})}try{let o=new URL(e.request.url).pathname,a=__KYRO_API_PATH__;if(o===`${a}/graphql`&&typeof exports.a.getGraphQL=="function"){if(!T("graphqlEnabled"))return new Response(JSON.stringify({error:"GraphQL is disabled"}),{status:503,headers:{"Content-Type":"application/json"}});let n=await exports.a.getGraphQL().fetch(e.request,e.locals);return new Response(n.body,n)}if(o.startsWith(`${a}/trpc/`)&&typeof exports.a.getTRPC=="function"){if(!T("trpcEnabled"))return new Response(JSON.stringify({error:"tRPC is disabled"}),{status:503,headers:{"Content-Type":"application/json"}});let n=await exports.a.getTRPC().fetch(e.request,e.locals);return new Response(n.body,n)}return await(await exports.a.getREST()).fetch(e.request,e.locals)}catch(t){return console.error("[Kyro API Execution Error]:",t?.stack||t?.message||t),new Response(JSON.stringify({error:"Internal Server Error",message:t?.message||String(t),stack:t?.stack}),{status:500,headers:{"Content-Type":"application/json"}})}},L=R;exports.b=E;exports.c=R;exports.d=L;
|