"use strict";(()=>{var e={};e.id=8893,e.ids=[8893],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},8325:(e,r,s)=>{s.r(r),s.d(r,{config:()=>p,default:()=>A,routeModule:()=>S});var a={};s.r(a),s.d(a,{default:()=>m});var t=s(89947),i=s(2706),o=s(96762),n=s(29021),u=s.n(n),l=s(48786),d=s(67619),c=s(69842),_=s(98e3),b=s(14160),E=s(76856);async function m(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let s=await (0,c.A)(e,r,!0);if(!s)return r.json({success:!1,msg:"Unauthorized"});let a=await (0,l.A)("SELECT * FROM users WHERE id = ?",[s.id]),t=a?.[0],{db_name:i,db_slug:o,db_description:n,db_image:m,duplicate:A,keepUpdated:p,keepDataUpdated:S}=e.body;if(o.match(/[^a-zA-Z0-9-_]/))return r.json({success:!1,msg:"Input Error"});let f=`datasquirel_user_${s.id}_${o}`,D=await (0,b.A)(`CREATE DATABASE IF NOT EXISTS \`${f}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!D?.affectedRows)return r.json({success:!1,msg:"Database Could Not be created!"});let h={user_id:s.id,db_name:i,db_slug:o,db_full_name:f,db_description:n,db_image:m};A&&p&&(h.active_clone="1",h.active_clone_parent_db=A),A&&S&&(h.active_data="1");let N=await (0,_.A)({dbFullName:"datasquirel",tableName:"user_databases",data:h});if(N.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${s.id}/main.json`,a=JSON.parse(u().readFileSync(e,"utf8")),l={dbName:i,dbSlug:o,dbFullName:f,dbDescription:n,dbImage:m,tables:[]};if(A)try{let e=a.filter(e=>e.dbFullName===A)[0];l.tables=e.tables,p&&(l.childDatabase=!0,l.childDatabaseDbFullName=A,S&&(l.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:f}):e.childrenDatabases=[{dbFullName:f}]);for(let e=0;e{s.d(r,{A:()=>u});var a=s(48786),t=s(95969),i=s(29021),o=s.n(i),n=s(89159);async function u(e,r,s){try{let{keyCookieName:i,csrfCookieName:u}=(0,n.A)();if(!e.cookies?.[i]?.match(/./))throw Error("Request Cookies not Found!");let l=(0,t.A)({encryptedString:e.cookies[i]});if(!l)throw Error("Couldn't decrypt auth cookie!");let d=JSON.parse(l);if(!d.csrf_k)throw Error("No `csrf_k` present in user Object!");if(s&&!Object.keys(e.headers).find(e=>e.includes(d.csrf_k)))throw console.log("req.headers",e.headers),console.log("userObject",d),Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(s&&!o().existsSync(`${c}/${d.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==d.verification_status&&!s){let e=await (0,a.A)(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(d?.date&&Date.now()-d.date>6048e5)throw Error("Key expired!");return d}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,s)=>{s.d(r,{A:()=>a});function a(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",s=process.env.DSQL_COOKIES_KEY_NAME||"key",a=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",t=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",i=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),i&&(o+=`${i}_`),o+=s;let n=r;e?.userId&&(n+=`user_${e.userId}_`),i&&(n+=`${i}_`),n+=a;let u=r;return e?.userId&&(u+=`user_${e.userId}_`),i&&(u+=`${i}_`),{keyCookieName:o,csrfCookieName:n,oneTimeCodeName:u+=t}}},14160:(e,r,s)=>{s.d(r,{A:()=>n});var a=s(29021),t=s.n(a),i=s(67619),o=s(47546);async function n(e){let r;"production".match(/dev/)&&t().appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{r=await (0,o.A)(e)}catch(e){(0,i.A)({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return r||null}},47546:(e,r,s)=>{s.d(r,{A:()=>a});function a(...e){let r=global.DSQL_DB_CONN;try{return new Promise((s,a)=>{r.query(...e).then(e=>{r.end(),s(JSON.parse(JSON.stringify(e)))}).catch(e=>{r.end(),s({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}}}};var r=require("../../webpack-api-runtime.js");r.C(e);var s=e=>r(r.s=e),a=r.X(0,[1251,2282],()=>s(8325));module.exports=a})();