@stacksjs/sites 0.75.735 → 0.75.762
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/provision.js +1 -1
- package/dist/resolver.js +1 -1
- package/package.json +8 -8
package/dist/provision.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{db}from"@stacksjs/database/runtime";export async function provisionSite(input){const existing=input.siteId?await
|
|
1
|
+
import{randomUUIDv7}from"bun";import{db,sqliteWriteOptions}from"@stacksjs/database/runtime";import{clearSiteCache}from"./resolver";export async function provisionSite(input){const settings=JSON.stringify(input.settings??{});const{siteId,created}=await db.transaction(async(trx)=>{const existing=input.siteId?await trx.selectFrom("sites").selectAll().where("id","=",input.siteId).executeTakeFirst():await trx.selectFrom("sites").selectAll().where("subdomain","=",input.subdomain).executeTakeFirst();if(existing){const id=Number(existing.id);await trx.updateTable("sites").set({settings,...input.timezone?{timezone:input.timezone}:{}}).where("id","=",id).execute();return{siteId:id,created:false}}const uuid=randomUUIDv7();const inserted=await trx.insertInto("sites").values({uuid,name:input.name,subdomain:input.subdomain,status:"active",settings,...input.timezone?{timezone:input.timezone}:{}}).executeTakeFirst();let id=Number(inserted?.insertId??inserted?.id);if(!Number.isFinite(id)||id<=0){const row=await trx.selectFrom("sites").select("id").where("subdomain","=",input.subdomain).executeTakeFirst();id=Number(row?.id)}return{siteId:id,created:true}},sqliteWriteOptions());clearSiteCache();const pagesCreated=[];const pagesKept=[];if(input.pages?.length){const{createPageDocument,registerDefaultBlocks}=await import("@stacksjs/cms");registerDefaultBlocks();for(const page of input.pages){const path=page.slug==="/"?"/":`/${page.slug}`;const already=await db.selectFrom("pages").select("id").where("site_id","=",siteId).where("path","=",path).executeTakeFirst();if(already){pagesKept.push(path);continue}await createPageDocument(siteId,{title:page.title,slug:page.slug,status:page.status??"published",blocks:page.blocks??[],parentId:page.parentId});pagesCreated.push(path)}}return{siteId,subdomain:input.subdomain,created,pagesCreated,pagesKept}}
|
package/dist/resolver.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{config}from"@stacksjs/config";import{db}from"@stacksjs/database/runtime";export function normalizeHost(raw){if(!raw)return"";let host=raw.trim().toLowerCase();if(host.startsWith("[")){const close=host.indexOf("]");if(close!==-1)host=host.slice(0,close+1)}else{const colon=host.indexOf(":");if(colon!==-1)host=host.slice(0,colon)}return host.endsWith(".")?host.slice(0,-1):host}export function sitesOptions(){const raw=config.sites??{};return{enabled:raw.enabled??false,baseDomain:normalizeHost(raw.baseDomain??""),platformHosts:(raw.platformHosts??[]).map(normalizeHost),strict:raw.strict??false,trustProxyHost:raw.trustProxyHost??
|
|
1
|
+
import{config}from"@stacksjs/config";import{db}from"@stacksjs/database/runtime";export function normalizeHost(raw){if(!raw)return"";let host=raw.trim().toLowerCase();if(host.startsWith("[")){const close=host.indexOf("]");if(close!==-1)host=host.slice(0,close+1)}else{const colon=host.indexOf(":");if(colon!==-1)host=host.slice(0,colon)}return host.endsWith(".")?host.slice(0,-1):host}export function sitesOptions(){const raw=config.sites??{};return{enabled:raw.enabled??false,baseDomain:normalizeHost(raw.baseDomain??""),platformHosts:(raw.platformHosts??[]).map(normalizeHost),strict:raw.strict??false,trustProxyHost:raw.trustProxyHost??false,cacheTtlSeconds:raw.cacheTtlSeconds??60}}export function classifyHost(host,options){if(!host||options.platformHosts.includes(host))return{kind:"platform"};const base=options.baseDomain;if(base&&host===base)return{kind:"platform"};if(base&&host.endsWith(`.${base}`)){const sub=host.slice(0,-(base.length+1));if(sub&&!sub.includes(".")&&sub!=="www")return{kind:"subdomain",subdomain:sub};return{kind:"platform"}}return{kind:"custom",domain:host}}export function requestHost(headers,options){const forwarded=options.trustProxyHost?headers.get("x-forwarded-host"):null;const first=forwarded?.split(",")[0];return normalizeHost(first||headers.get("host"))}function rowToContext(row,host){let settings={};const rawSettings=row.settings;if(typeof rawSettings==="string"&&rawSettings){try{settings=JSON.parse(rawSettings)}catch{}}else if(rawSettings&&typeof rawSettings==="object"){settings=rawSettings}return{id:Number(row.id),uuid:String(row.uuid??""),name:String(row.name??""),subdomain:String(row.subdomain??""),host,teamId:row.team_id==null?null:Number(row.team_id),status:String(row.status??"active"),settings}}const SITE_COLUMNS=["id","uuid","name","subdomain","team_id","status","settings"];export const databaseSiteStore={async byDomain(domain){const link=await db.selectFrom("site_domains").where("domain","=",domain).where("verified_at","is not",null).select(["site_id"]).executeTakeFirst();if(!link)return null;const site=await db.selectFrom("sites").where("id","=",link.site_id).where("status","=","active").select([...SITE_COLUMNS]).executeTakeFirst();return site?rowToContext(site,domain):null},async bySubdomain(subdomain){const site=await db.selectFrom("sites").where("subdomain","=",subdomain).where("status","=","active").select([...SITE_COLUMNS]).executeTakeFirst();return site?rowToContext(site,`${subdomain}.${sitesOptions().baseDomain}`):null}};const CACHE_KEY=Symbol.for("stacks.sites.hostCache");const MAX_HOST_CACHE_ENTRIES=1000;const hostCache=globalThis[CACHE_KEY]??=new Map;export function clearSiteCache(){hostCache.clear()}function setHostCache(host,site,ttlSeconds){const now=Date.now();if(hostCache.size>=MAX_HOST_CACHE_ENTRIES){for(const[key,entry]of hostCache){if(entry.expiresAt<=now)hostCache.delete(key)}while(hostCache.size>=MAX_HOST_CACHE_ENTRIES){const oldest=hostCache.keys().next().value;if(oldest===undefined)break;hostCache.delete(oldest)}}hostCache.set(host,{site,expiresAt:now+Math.max(1,ttlSeconds)*1000})}export async function resolveSiteByHost(rawHost,store=databaseSiteStore,options=sitesOptions()){if(!options.enabled)return null;const host=normalizeHost(rawHost);if(!host)return null;const cached=hostCache.get(host);if(cached){if(cached.expiresAt>Date.now()){hostCache.delete(host);hostCache.set(host,cached);return cached.site}hostCache.delete(host)}const kind=classifyHost(host,options);let site=null;if(kind.kind==="custom")site=await store.byDomain(kind.domain);else if(kind.kind==="subdomain")site=await store.bySubdomain(kind.subdomain);setHostCache(host,site,options.cacheTtlSeconds);return site}export function isPlatformHost(rawHost,options=sitesOptions()){return classifyHost(normalizeHost(rawHost),options).kind==="platform"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/sites",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.75.
|
|
4
|
+
"version": "0.75.762",
|
|
5
5
|
"description": "The Stacks multi-site (request-level tenancy) functionality.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"contributors": [
|
|
@@ -56,18 +56,18 @@
|
|
|
56
56
|
"prepublishOnly": "bun run build"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@stacksjs/auth": "0.75.
|
|
60
|
-
"@stacksjs/config": "0.75.
|
|
61
|
-
"@stacksjs/database": "0.75.
|
|
62
|
-
"@stacksjs/router": "0.75.
|
|
59
|
+
"@stacksjs/auth": "0.75.762",
|
|
60
|
+
"@stacksjs/config": "0.75.762",
|
|
61
|
+
"@stacksjs/database": "0.75.762",
|
|
62
|
+
"@stacksjs/router": "0.75.762"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@stacksjs/error-handling": "0.75.
|
|
66
|
-
"better-dx": "^0.2.
|
|
65
|
+
"@stacksjs/error-handling": "0.75.762",
|
|
66
|
+
"better-dx": "^0.2.42"
|
|
67
67
|
},
|
|
68
68
|
"sideEffects": false,
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@stacksjs/cms": "0.75.
|
|
70
|
+
"@stacksjs/cms": "0.75.762"
|
|
71
71
|
},
|
|
72
72
|
"peerDependenciesMeta": {
|
|
73
73
|
"@stacksjs/cms": {
|