@blocklet/pages-kit-inner-components 0.6.38 → 0.6.39
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/lib/cjs/add-component.js +1 -1
- package/lib/cjs/chunks/{components-CacZMc0_.js → components-D1oFQM3W.js} +3 -3
- package/lib/cjs/chunks/home-DeAzY6p9.js +39 -0
- package/lib/cjs/chunks/index-aDzdRTp1.js +475 -0
- package/lib/cjs/chunks/{publish-button-CxPaQCVl.js → publish-button-BuApNQWc.js} +1 -1
- package/lib/cjs/chunks/site-state-CIj4DvXS.js +1 -0
- package/lib/cjs/components.js +1 -1
- package/lib/cjs/home.js +1 -1
- package/lib/cjs/project-html.js +1 -1
- package/lib/cjs/resources.js +1 -1
- package/lib/cjs/site-state.js +1 -1
- package/lib/es/add-component.js +1 -1
- package/lib/es/chunks/{components-BWQM8y6L.js → components-6zI6vrkL.js} +32 -28
- package/lib/es/chunks/home-DaTNieOs.js +652 -0
- package/lib/es/chunks/{index-DBTbT9nm.js → index-Dm9c1YeI.js} +730 -666
- package/lib/es/chunks/{publish-button-Dd-8JzPl.js → publish-button-Tf6i5WAs.js} +2 -2
- package/lib/es/chunks/{site-state-Bx2ZDYuu.js → site-state-CxfQgHjO.js} +235 -231
- package/lib/es/components.js +3 -3
- package/lib/es/home.js +9 -8
- package/lib/es/project-html.js +1 -1
- package/lib/es/resources.js +2 -2
- package/lib/es/site-state.js +2 -2
- package/package.json +3 -3
- package/lib/cjs/chunks/home-DnAiAqUA.js +0 -39
- package/lib/cjs/chunks/index-DwXFWtCu.js +0 -475
- package/lib/cjs/chunks/site-state-8lUnv-C3.js +0 -1
- package/lib/es/chunks/home-1FD52b5s.js +0 -623
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const f=require("./components-CacZMc0_.js"),F=require("@syncedstore/core"),Je=require("yjs"),He=require("@blocklet/pages-kit/utils/data-source"),Q=require("@blocklet/pages-kit/utils/route"),We=require("lodash"),Qe=require("@blocklet/sdk/lib/config"),I=require("fs"),g=require("path"),De=require("@blocklet/pages-kit/utils/common"),Xe=require("@blocklet/pages-kit/utils/page-model"),Ze=require("@blocklet/pages-kit/utils/property"),Ie=require("@blocklet/sdk/lib/component"),et=require("@reactivedata/reactive"),X=require("glob"),tt=require("lib0/decoding"),st=require("lib0/encoding"),at=require("lodash/cloneDeep"),nt=require("lodash/debounce"),ot=require("lodash/get"),rt=require("lodash/isEmpty"),it=require("lodash/set"),ct=require("lodash/union"),be=require("lru-cache"),lt=require("p-limit"),C=require("sequelize"),pt=require("stream/promises"),ut=require("tar"),z=require("ufo"),dt=require("wait-on"),G=require("y-protocols/awareness"),le=require("y-protocols/sync"),ft=require("yaml"),Te=require("./html-JOhPskS4.js");require("sqlite3");require("@blocklet/pages-kit/types/state");const $=t=>t&&t.__esModule?t:{default:t};function ae(t){if(t&&t.__esModule)return t;const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const s in t)if(s!=="default"){const a=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,a.get?a:{enumerable:!0,get:()=>t[s]})}}return e.default=t,Object.freeze(e)}const U=ae(Je),w=$(Qe),pe=ae(tt),k=ae(st),gt=$(at),Le=$(nt),Ee=$(ot),we=$(rt),ve=$(it),mt=$(ct),ht=$(lt),yt=$(dt),x=ae(ft),St=C.DataTypes.sqlite.DATE.parse;C.DataTypes.sqlite.DATE.parse=(t,e)=>typeof t=="number"?new Date(t):St(t,e);const M=new C.Sequelize({dialect:"sqlite",storage:f.databaseUrl,benchmark:process.env.ENABLE_SEQUELIZE_BENCHMARK==="true",retry:{match:[/SQLITE_BUSY/],name:"query",max:10},logging:process.env.ENABLE_SEQUELIZE_LOGGING==="true"?console.log:!1});M.query("pragma journal_mode = WAL;");M.query("pragma synchronous = normal;");M.query("pragma journal_size_limit = 33554432;");M.query("pragma cache_size = -2000;");M.query("pragma mmap_size = 33554432;");process.on("SIGINT",async()=>{await M.close(),process.exit(0)});process.on("SIGTERM",async()=>{await M.close(),process.exit(0)});async function It(t,e){try{if(t.getDialect()!=="sqlite")return;const[s]=await t.query("SELECT 1");if(!s||s.length===0)return;await t.query("PRAGMA shrink_memory;")}catch(s){if(s.name==="SequelizeConnectionError"||s?.message&&/closed!/.test(s.message))return;console.error("Failed to cleanup SQLite memory",e,s)}}let fe=null;fe&&clearInterval(fe);fe=setInterval(async()=>{f.logger.info("Start cleanupSqliteMemory"),await It(M,f.databaseUrl),f.logger.info("End cleanupSqliteMemory")},60*1e3*10);class Ae extends C.Model{}Ae.init({id:{type:C.DataTypes.UUID,allowNull:!1,primaryKey:!0,defaultValue:C.DataTypes.UUIDV4},projectId:{type:C.DataTypes.UUID,allowNull:!1},componentId:{type:C.DataTypes.STRING,allowNull:!1}},{sequelize:M,tableName:"ProjectComponents",timestamps:!1});class L extends C.Model{static async getProjectByIdOrSlug(e){return L.findOne({where:{[C.Op.or]:[{id:e},{slug:e}]}})}}L.init({id:{type:C.DataTypes.UUID,defaultValue:C.DataTypes.UUIDV4,primaryKey:!0},name:{type:C.DataTypes.STRING,allowNull:!1},description:C.DataTypes.TEXT,createdAt:C.DataTypes.DATE,updatedAt:C.DataTypes.DATE,createdBy:{type:C.DataTypes.STRING,allowNull:!1},updatedBy:{type:C.DataTypes.STRING,allowNull:!1},slug:C.DataTypes.STRING,icon:C.DataTypes.STRING,pinnedAt:C.DataTypes.DATE,useAllResources:C.DataTypes.BOOLEAN,npmSecret:C.DataTypes.STRING,relatedBlocklets:{type:C.DataTypes.JSON,allowNull:!1,defaultValue:{},get(){const t=this.getDataValue("relatedBlocklets");if(typeof t=="object")return t??{};try{return t?JSON.parse(t):{}}catch(e){return f.logger.error("Failed to parse relatedBlocklets",{error:e,rawValue:t}),{}}},set(t){try{this.setDataValue("relatedBlocklets",t?JSON.stringify(t):"{}")}catch(e){f.logger.error("Failed to set relatedBlocklets",{error:e,value:t}),this.setDataValue("relatedBlocklets","{}")}}},productionState:{type:C.DataTypes.JSON,allowNull:!1,defaultValue:{},get(){const t=this.getDataValue("productionState");if(typeof t=="object")return t??{};try{return t?JSON.parse(t):{}}catch(e){return f.logger.error("Failed to parse productionState",{error:e,rawValue:t}),{}}},set(t){try{this.setDataValue("productionState",t?JSON.stringify(t):"{}")}catch(e){f.logger.error("Failed to set productionState",{error:e,value:t}),this.setDataValue("productionState","{}")}}}},{sequelize:M,paranoid:!0});L.hasMany(Ae,{foreignKey:"projectId",as:"components"});const bt="z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o",ge="page",me="trigger-reload-project-resource",Pe=bt,Et="z2qa7BQdkEb3TwYyEYC1psK6uvmGnHSUHt5RM";function Z(t){t.observeDeep(e=>{e.some(s=>s.changes.keys.has("updatedAt")||s.changes.keys.has("publishedAt"))||t.set("updatedAt",new Date().toISOString())})}function _e(){return I.mkdtempSync(g.join(w.default.env.dataDir,"tmp-"))}function ee(t,e,s=[]){return Array.isArray(t)?t.flatMap((a,n)=>ee(a,e,[...s,n])):typeof t=="object"?t===null?[]:Object.entries(t).flatMap(([a,n])=>ee(n,e,[...s,a])):e(t)?[s]:[]}function R(t){return t.filter(e=>e!=null)}function wt(t){t.pages&&Object.keys(t.pages).forEach(s=>{const a=F.getYjsValue(t.pages[s]);a&&a instanceof U.Map&&Z(a)});const e=F.getYjsValue(t.pages);e&&e instanceof U.Map&&e.observe(s=>{s.changes.keys.forEach((a,n)=>{if(a.action==="add"){const o=F.getYjsValue(t.pages[n]);o&&o instanceof U.Map&&Z(o)}})})}function At(t){t.routes&&Object.keys(t.routes).forEach(s=>{const a=F.getYjsValue(t.routes?.[s]);a&&a instanceof U.Map&&Z(a)});const e=F.getYjsValue(t.routes);e&&e instanceof U.Map&&e.observe(s=>{s.changes.keys.forEach((a,n)=>{if(a.action==="add"){const o=F.getYjsValue(t.routes?.[n]);o&&o instanceof U.Map&&Z(o)}})})}function Pt(t,e){for(const s of e||Object.keys(t.routes||{})){let a=s,n=[];if(s.includes("-")){const[o,...c]=s.split("-");a=o,n=c||[]}if(t.routes?.[a]!==void 0){t.routes[a].publishedAt=new Date().toISOString();const o=t.routes[a];if(!o||!o.params||o.params.length===0)continue;if(s.includes("-")&&n.length>0){const c=Q.getRouteMetaDataByOptionIds(n,o);c&&(c.publishedAt=new Date().toISOString())}if(!e){const c=Q.generateParamCombinations({basePath:o.path,params:o.params,routeId:o.id,paramsOptions:o.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]});for(const p of c)p.routeMetaData??={},p.routeMetaData.publishedAt=new Date().toISOString()}}}}function ue({page:t,route:e,state:s,routeId:a,routePathInfo:n}){f.logger.info(`Executing datasource data assembly, routeId: ${a}, routePathInfo: ${JSON.stringify(n)}`);const o={...We.cloneDeep(t),id:a,slug:n?.path??e.path,createdAt:e.createdAt,updatedAt:n?.routeMetaData?.updatedAt??e.updatedAt,publishedAt:n?.routeMetaData?.publishedAt??e.publishedAt,isPublic:(n?.routeMetaData?.isPublic??e.isPublic)&&e.isPublic};for(const c of s.supportedLocales){if(e.dataSource){let p=e.id;n&&(p=n.paramOptionIds.join("-"));const l=e.dataSource.pathDataMappings?.[p]?.dataCache?.[c.locale]??e.dataSource.pathDataMappings?.[p]?.dataCache?.[s.config.defaultLocale||"en"];if(!l)continue;He.setPageDataSource(o,s,c.locale,l)}n&&n.routeMetaData&&(n.routeMetaData.publishedAt=new Date().toISOString())}return o}const jt=30*60*1e3,Y=new be.LRUCache({max:100,ttl:jt});function Ot(t,e=[]){let s=0;const a=Array.from(Y.keys());for(const n of a)for(const o of t){if(Te.matchCacheKey(n,{currentPath:o})){Y.delete(n),s++,f.logger.info(`[Cache CLEAR] key: ${n}`);break}for(const c of e)if(Te.matchCacheKey(n,{currentPath:`/${c}${o}`})){Y.delete(n),s++,f.logger.info(`[Cache CLEAR] key: ${n}`);break}}return f.logger.info(`[Cache CLEAR] cleared ${s} entries for paths:`,t),s}function Ct(){const t=Y.size;return Y.clear(),f.logger.info(`[Cache CLEAR ALL] cleared ${t} entries`),t}w.default.events.on(w.default.Events.envUpdate,Ct);const{uploadToMediaKit:Dt}=require("@blocklet/uploader-server"),je=/^\w+(\w|-|\.)+\w+\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm)$/,K=/mediakit:\/\/([a-f0-9]{32}\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm))/i,ke=/mediakit:\/\/([a-f0-9]{32}\.(jpe?g|png|gif|svg|bmp|webp|mp4|m4v|webm))/gi,Tt=1e4,vt=3e4,H=0,de=1,kt=0,Rt=1,he=w.default,q=g.join(process.env.BLOCKLET_DATA_DIR,"site-state"),Lt=["production","draft"],_t=["production"];function te(t){return t?.replace(/\//g,"|")||""}function Ue(){const t=he.env.languages?.map(s=>({locale:s.code,name:s.name}))||[],e=t[0]?.locale||"en";return{pageIds:[],pages:{},routeIds:[],routes:{},dataSourceIds:[],dataSources:{},components:{},supportedLocales:t,config:{defaultLocale:e},resources:{}}}class O extends U.Doc{constructor(e){super(),this.options=e,I.existsSync(this.draftYjsFilePath)&&U.applyUpdate(this,I.readFileSync(this.draftYjsFilePath)),this.syncedStore=et.reactive(F.syncedStore({pages:{},pageIds:[],components:{},supportedLocales:[],config:{},resources:{},routeIds:[],routes:{},dataSourceIds:[],dataSources:{}},this)),this.initObserver(),this.on("update",this.updateHandler),this.awareness=new G.Awareness(this),this.awareness.on("update",this.awarenessChangeHandler),this.ensureDataStructure()}static RELEASE_DELAY=5*60*1e3;static PERIODIC_CHECK_INTERVAL=2*60*60*1e3;static sharedInstances={};static pageUrlMapCache=new be.LRUCache({max:100,ttl:1e3*60*60*24});static periodicCheckTimer;static safeDeleteProjectStateDir(e){if(!e)throw new Error("Should provide project context");try{const s=g.join(q,e),a=g.join(q,`@del-${e}`);I.renameSync(s,a)}catch(s){f.logger.error("Failed to safe delete project state dir:",s)}}static async getProjectIds(){return(await L.findAll({attributes:["id"],raw:!0}))?.map(e=>e.id)}static get projectIds(){return X.globSync("*/",{cwd:q,ignore:["@del-*","@tmp-*",".*","staging","production","@backup-*","undefined"]})}static get allShared(){return this.projectIds.map(e=>O.shared(e))}static shared(e){if(!e)throw new Error("Should provide project context");let s=O.sharedInstances[e];return s||(s=new O({path:g.join(q,e)}),O.sharedInstances[e]=s,s)}static async getProductionState(e){const s=await L.findByPk(e,{attributes:["productionState"]});if(we.default(s?.productionState)){const a=g.join(q,e,"production"),n=await Oe(a,{includeResources:!0})??Ue();if(!n?.config?.defaultLocale){n.config??={};const o=he.env.languages?.map(c=>({locale:c.code,name:c.name}))||[];n.config.defaultLocale=o[0]?.locale}return n}return s?.productionState}destroy(){this.cancelRelease(),this.save({flush:!0}),this.conns.forEach((s,a)=>this.closeConn(a)),this.awareness.destroy();const e=g.basename(this.options.path);delete O.sharedInstances[e],super.destroy()}initObserver(){wt(this.syncedStore),At(this.syncedStore)}get draftYjsFilePath(){return g.join(this.options.path,"draft.yjs")}static async getStateByProjectId(e,s){if(s==="draft"){const a=O.shared(e);return JSON.parse(JSON.stringify(a.syncedStore))}return O.getProductionState(e)}async getState(e){if(e==="draft")return JSON.parse(JSON.stringify(this.syncedStore));const s=g.basename(this.options.path);return O.getProductionState(s)}async setState(e,s){const a=await Fe(s,{exportAssets:!1,includeResources:!0}),n=this.getPublishDir(e);if(I.mkdirSync(g.dirname(n),{recursive:!0}),I.rmSync(n,{force:!0,recursive:!0}),I.renameSync(a,n),e==="production"){const o=g.basename(this.options.path);O.pageUrlMapCache.delete(o),await L.update({productionState:{...s}},{where:{id:o}})}}getPublishDir(e){return g.join(this.options.path,e)}syncedStore;conns=new Map;awareness;releaseTimer;awarenessChangeHandler=({added:e,updated:s,removed:a},n)=>{const o=e.concat(s,a);if(n!==null){const l=this.conns.get(n);l&&(e.forEach(S=>{l.add(S)}),a.forEach(S=>{l.delete(S)}))}const c=k.createEncoder();k.writeVarUint(c,de),k.writeVarUint8Array(c,G.encodeAwarenessUpdate(this.awareness,o));const p=k.toUint8Array(c);this.conns.forEach((l,S)=>this.send(S,p))};updateHandler=e=>{const s=k.createEncoder();k.writeVarUint(s,H),le.writeUpdate(s,e);const a=k.toUint8Array(s);this.conns.forEach((n,o)=>this.send(o,a))};ensureDataStructure=()=>{const{supportedLocales:e,pages:s,pageIds:a,config:n,routes:o,routeIds:c}=this.syncedStore;{const p=new Set(Object.keys(s));let l=0;for(;l<a.length;){const S=a[l];p.has(S)?(p.delete(S),l++):a.splice(l,1)}}{const p=new Set(Object.keys(o));let l=0;for(;l<c.length;){const S=c[l];p.has(S)?(p.delete(S),l++):c.splice(l,1)}}e.splice(0,e.length),e.push(...he.env.languages.map(p=>({locale:p.code,name:p.name}))),n.defaultLocale=e[0]?.locale;{let p=0;const l=new Set;for(;p<e.length;){const{locale:S}=e[p];l.has(S)?e.splice(p,1):(p++,l.add(S))}}};send=(e,s)=>{e.readyState!==kt&&e.readyState!==Rt&&this.closeConn(e);try{e.send(s,a=>{a&&this.closeConn(e)})}catch{this.closeConn(e)}};closeConn=e=>{if(e.removeAllListeners(),this.conns.has(e)){const s=this.conns.get(e);this.conns.delete(e),s&&G.removeAwarenessStates(this.awareness,Array.from(s),null)}e.close(),this.checkAndScheduleRelease()};checkAndScheduleRelease(){this.conns.size===0&&this.scheduleRelease()}scheduleRelease(){this.cancelRelease();const e=g.basename(this.options.path);this.releaseTimer=setTimeout(()=>{f.logger.info(`[SiteState] releasing instance due to no active connections: ${e}`),this.conns.size===0&&(this.releaseTimer=void 0,this.destroy())},O.RELEASE_DELAY),f.logger.info(`[SiteState] scheduled release for project ${e} in ${O.RELEASE_DELAY/1e3}s`)}cancelRelease(){if(this.releaseTimer){clearTimeout(this.releaseTimer),this.releaseTimer=void 0;const e=g.basename(this.options.path);f.logger.info(`[SiteState] cancelled scheduled release for project ${e}`)}}autoSave=Le.default(()=>{I.mkdirSync(g.dirname(this.draftYjsFilePath),{recursive:!0}),I.writeFileSync(this.draftYjsFilePath,U.encodeStateAsUpdate(this))},Tt);save=({flush:e=!1}={})=>{this.autoSave(),e&&this.autoSave.flush()};publish=async({mode:e,routes:s})=>{const a=await this.getState("draft"),n=await this.getState("production");await Se(a,n,{routes:s,mergeMode:"replace",deleteRoutes:!0,publishMode:e}),n.config.publishedAt=new Date().getTime(),Pt(this.syncedStore,s),await this.setState(e,n),await this.clearPageCacheForRoutes(s,n)};mergeState=async(e,s)=>{const a=JSON.parse(JSON.stringify(s));e.config.fontFamily??={};const n=a.config?.fontFamily,o=e.config?.fontFamily;e.config.fontFamily.title=n?.title||o?.title,e.config.fontFamily.description=n?.description||o?.description,await new Promise((c,p)=>{this.transact(async()=>{try{const l=await Se(e,s);c(l)}catch(l){p(l)}})})};clearPageCacheForRoutes=async(e,s)=>{const a=g.basename(this.options.path),o=(await L.findByPk(a))?.slug||a;let c=e;(!c||c.length===0)&&(c=s.pageIds??[]),f.logger.info(`[SiteState] clearing page cache for project ${a}, routes:`,c||[]);const p=s.supportedLocales.map(A=>A.locale),l=[],S=c.filter(A=>s.pageIds?.includes(A));for(const A of S){const E=s.pages[A].slug;o&&o!==a&&(o==="/"?l.push(E):l.push(`/${o}${E}`)),l.push(`/${a}${E}`)}if(f.logger.info(`[SiteState] clearing page cache for project ${a}, pathPatterns:`,l),l.length>0)try{const A=Ot(l,p);f.logger.info(`[SiteState] cleared ${A} page cache entries for project ${a}, routes:`,c)}catch{}};addConnection=e=>{if(this.conns.has(e))return;this.cancelRelease(),e.binaryType="arraybuffer",this.conns.set(e,new Set),e.on("message",n=>this.messageListener(e,new Uint8Array(n)));let s=!0;const a=setInterval(()=>{if(!s)this.conns.has(e)&&this.closeConn(e),clearInterval(a);else if(this.conns.has(e)){s=!1;try{e.ping()}catch{this.closeConn(e),clearInterval(a)}}},vt);e.on("close",()=>{this.closeConn(e),clearInterval(a)}),e.on("pong",()=>{s=!0});{const n=k.createEncoder();k.writeVarUint(n,H),le.writeSyncStep1(n,this),this.send(e,k.toUint8Array(n));const o=this.awareness.getStates();if(o.size>0){const c=k.createEncoder();k.writeVarUint(c,de),k.writeVarUint8Array(c,G.encodeAwarenessUpdate(this.awareness,Array.from(o.keys()))),this.send(e,k.toUint8Array(c))}}};messageListener=(e,s)=>{try{const a=k.createEncoder(),n=pe.createDecoder(s),o=pe.readVarUint(n);switch(o){case H:k.writeVarUint(a,H),le.readSyncMessage(n,a,this,null),k.length(a)>1&&(this.ensureDataStructure(),this.send(e,k.toUint8Array(a)));break;case de:{G.applyAwarenessUpdate(this.awareness,pe.readVarUint8Array(n),e);break}default:f.logger.warn(`Unsupported messageType ${o}`)}}catch(a){f.logger.error(a)}this.save()};static async pageUrlMap(e,s){let a=[];s?a=[s]:a=await this.getProjectIds();let n={};if(e==="production"&&a?.length){const o=new Map(a?.map(c=>[c,!0])||[]);for(const c of a){const p=O.pageUrlMapCache.get(c);p&&(n={...n,...p},o.delete(c))}a=Array.from(o.keys())}if(a?.length){const o=await L.findAll({where:{id:{[C.Op.in]:a}}});await Promise.all(o?.map(async c=>{const p=c.id,l=c.slug||p,S={},A=e==="production"&&c?.productionState?c.productionState:await O.getStateByProjectId(c.id,e),v=mt.default(w.default.env.languages?.map(u=>u.code)||[],A.supportedLocales?.map(u=>u.locale)||[]),E=(u,i)=>{l&&(S[z.joinURL("/",l,u)]={...i,shouldRedirect:!0,mainPage:!0}),S[z.joinURL("/",p,u)]={...i,shouldRedirect:!0,mainPage:!0};for(const h of v){const D={...i,locale:h};S[z.joinURL("/",h,p,u)]=D,l&&(S[z.joinURL("/",h,l,u)]=D)}};if(e==="draft")for(const u of A.routeIds||[]){const i=A?.routes?.[u];if(!i)continue;if(i.params&&i.params.length>0){const m=Q.generateParamCombinations({basePath:i.path,params:i.params,routeId:i.id,paramsOptions:i.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]});for(const d of m){const y=d.path,P={projectId:p,projectSlug:l,pageSlug:y,pageId:i.displayTemplateId||"",routeId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic&&d?.routeMetaData?.isPublic};E(y,P)}}const h=i.path,D={projectId:p,projectSlug:l,pageSlug:h,pageId:i.displayTemplateId||"",routeId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic};E(h,D)}for(const u of A.pageIds||[]){const i=A.pages[u];if(!i||e==="production"&&!i.isPublic)continue;const h=i.slug,D=c.slug||p,m={projectId:p,projectSlug:D,pageSlug:h,pageId:u,defaultLocale:v?.[0],locales:v,publishedAt:A.config.publishedAt,isPublic:i.isPublic,templateConfig:i.templateConfig};E(h,m)}e==="production"&&O.pageUrlMapCache.set(p,S),n={...n,...S}}))}return n}getDocumentSize(){return U.encodeStateAsUpdate(this).byteLength}static getInstancesSizeInfo(){const e=[];for(const[s,a]of Object.entries(O.sharedInstances)){const n=a.getDocumentSize();e.push({projectId:s,sizeInBytes:n,sizeInMB:`${(n/(1024*1024)).toFixed(2)} MB`,activeConnections:a.conns.size})}return e}static startPeriodicCheck(){this.periodicCheckTimer||(this.periodicCheckTimer=setInterval(()=>{this.performPeriodicCheck()},this.PERIODIC_CHECK_INTERVAL),f.logger.info(`[SiteState] periodic check started, interval: ${this.PERIODIC_CHECK_INTERVAL/(60*60*1e3)} hours`))}static stopPeriodicCheck(){this.periodicCheckTimer&&(clearInterval(this.periodicCheckTimer),this.periodicCheckTimer=void 0,f.logger.info("[SiteState] periodic check stopped"))}static performPeriodicCheck(){const e=Object.keys(O.sharedInstances).length,s=[],a=[];for(const[n,o]of Object.entries(O.sharedInstances))o.conns.size===0?s.push({projectId:n,instance:o}):a.push({projectId:n,connections:o.conns.size});if(f.logger.info(`[SiteState] periodic check summary: total instances: ${e}, with connections: ${a.length}, without connections: ${s.length}`),s.length>0){f.logger.info(`[SiteState] releasing ${s.length} instances without connections:`,s.map(o=>o.projectId));let n=0;for(const{projectId:o,instance:c}of s)try{f.logger.info(`[SiteState] releasing instance due to periodic check: ${o}`),c.destroy(),n++}catch(p){f.logger.error(`[SiteState] failed to release instance ${o} during periodic check:`,p)}f.logger.info(`[SiteState] periodic check completed: ${n}/${s.length} instances released successfully`)}else e>0?f.logger.debug("[SiteState] periodic check: all instances have active connections"):f.logger.debug("[SiteState] periodic check: no instances exist")}}async function Ut(t,e,s){if(!t||!I.existsSync(t)||!I.lstatSync(t).isFile())return null;let a=s[t];return a||(a=(async()=>{try{return(await Dt({filePath:t,fileName:e}))?.data?.filename}catch(n){return f.logger.error(`Failed to upload asset ${t}:`,n),null}})(),s[t]=a),a}const Me=async(t,e)=>{const s=g.basename(t),a=await Ie.call({name:Pe,path:z.joinURL("/uploads",s),responseType:"stream",method:"GET"});if(a.status>=200&&a.status<400){const n=I.createWriteStream(e);await pt.pipeline(a.data,n)}else throw new Error(`download asset failed ${a.status}`)},$e=async(t,e)=>{await Promise.all(t.map(async s=>{try{await Me(s,g.join(e,g.basename(s)))}catch(a){f.logger.error(`Failed to export assets: ${s}, ${a}`)}}))};function Ne(t){return je.test(t)?[t]:K.test(t)?(ke.lastIndex=0,Array.from(t.matchAll(ke)).map(s=>s[1]).filter(s=>!!s)):[]}async function W(t,e,s){const{getFilename:a,exportAssets:n}=s,o=g.join(e,a(t));if(I.mkdirSync(g.dirname(o),{recursive:!0}),I.writeFileSync(o,x.stringify(t)),n){const p=ee(t,l=>typeof l=="string"&&(je.test(l)||K.test(l))).map(l=>{const S=Ee.default(t,l);return Ne(S)}).flat().filter(Boolean);await $e(p,g.dirname(o))}}const ye=new be.LRUCache({max:100,ttl:1*60*1e3});async function Re(t,e,s){const a=ee(t,p=>typeof p=="string"&&(je.test(p)||K.test(p))),n=ht.default(2),o=a.map(p=>n(async()=>{try{const l=Ee.default(t,p),S=Ne(l);for(const A of S){const v=g.basename(A),E=s.getFilePath(A,p),u=E?`${E}:${v}`:v,i=ye.get(u);if(i){K.test(l)||ve.default(t,p,i);return}const h=await Ut(E,v,e);h&&(K.test(l)||ve.default(t,p,h),ye.set(u,h))}}catch(l){f.logger.error(`Failed to process upload for path ${p.join(".")}:`,l.message||l.reason)}})),c=await Promise.allSettled(o);s.onFinish?.(c)}async function Fe(t,{exportAssets:e,pageIds:s="all",componentIds:a="all",rawConfig:n,includeResources:o=!1,routeIds:c="all"}={}){const p=s==="all"?t.pageIds:s,l=Ze.getComponentDependencies({state:t,pageIds:p,componentIds:a==="all"?Object.keys(t.components):a}),S=c==="all"?t.routeIds:c,A=r=>({id:r.id,name:r.name,isTemplateSection:r.isTemplateSection??!1,templateDescription:r.templateDescription,llmConfig:r.llmConfig,component:r.component,config:r.config,visibility:r.visibility,sections:r?.sectionIds?R(r?.sectionIds?.map(b=>{const T=r.sections?.[b];return T&&A(T)})):void 0}),v=(r,b)=>({id:r.id,createdAt:r.createdAt,updatedAt:r.updatedAt,publishedAt:r.publishedAt,isPublic:r.isPublic??!0,templateConfig:r.templateConfig,meta:r.locales?.[b]??{},sections:R(r.sectionIds.map(T=>{const N=r.sections[T];return N&&A(N)})),dataSource:Object.fromEntries(Object.entries(r.dataSource||{}).map(([T,N])=>[T,N?.[b]??{}]))}),E=r=>({id:r.id,createdAt:r.createdAt,updatedAt:r.updatedAt,publishedAt:r.publishedAt,path:r.path,handler:r.handler,isPublic:r.isPublic??!0,params:r.params??[],enabledGenerate:r.enabledGenerate??!1,displayTemplateId:r.displayTemplateId,dataSource:r.dataSource}),u=R(S.map(r=>{const b=t.routes[r];return b&&E(b)})),i=R(t.supportedLocales.map(r=>r.locale).flatMap(r=>p.map(b=>{const T=t.pages[b];return T&&{locale:r,slug:T.slug,page:v(T,r)}}))),h=_e(),D=g.join(h,"pages");I.mkdirSync(D,{recursive:!0});const m=g.join(h,"components");I.mkdirSync(m,{recursive:!0});const d=g.join(h,"routes");I.mkdirSync(d,{recursive:!0});for(const{locale:r,slug:b,page:T}of i)await W(T,D,{getFilename:()=>`${te(b)||"index"}.${r}.yml`,exportAssets:e});for(const r of u)await W(r,d,{getFilename:()=>`${te(r.path)||"index"}.yml`,exportAssets:e});for(const r of l){const b=t.components[r]?.data;b&&await W(b,m,{getFilename:T=>`${T.name||"unnamed"}.${T.id}.yml`,exportAssets:e})}const y=g.join(h,".blocklet/pages/pages.config.yml");I.mkdirSync(g.dirname(y),{recursive:!0});const P={pages:R(p.map(r=>{const b=t.pages[r];return b&&{id:r,slug:b.slug}})),routes:R(S.map(r=>{const b=t.routes[r];return b&&{id:r,path:b.path}})),components:R(l.map(r=>{const b=t.components[r]?.data;return b&&{id:r,name:b.name}})),...o?{resources:{components:R(Object.keys(t.resources?.components||{}).filter(r=>l.includes(r)).map(r=>({id:r,name:t.resources?.components?.[r]?.component?.name})))}}:{},supportedLocales:t.supportedLocales,config:t.config};I.writeFileSync(y,x.stringify(P));const j=g.join(h,"config.source.json");if(n&&I.writeFileSync(j,JSON.stringify(n)),o){const r=g.join(h,"resources"),b=g.join(r,"components");I.mkdirSync(b,{recursive:!0});for(const B of Object.keys(t?.resources?.components??{}).filter(_=>l.includes(_))){const _=t.resources?.components?.[B]?.component;_&&await W(_,b,{getFilename:V=>`${V.name||"unnamed"}.${V.id}.yml`,exportAssets:e})}const T=g.join(h,"chunks");I.mkdirSync(T,{recursive:!0});const N=Mt();for(const B of Object.keys(t?.resources?.components??{}).filter(_=>l.includes(_))){const _=t.resources?.components?.[B]?.component;if(_&&_.renderer?.type==="react-component"){const V=_.renderer?.chunks??[];if(V?.length>0)for(const ie of V){const Ce=g.join(T,ie),ce=N?.[ie];try{ce&&I.existsSync(ce)&&!I.existsSync(Ce)&&I.copyFileSync(ce,Ce)}catch(Ke){f.logger.error(`copy chunk ${ie} error`,Ke.message)}}}}}return h}async function Oe(t,{importAssets:e,includeResources:s}={}){if(!I.existsSync(t))return null;let a,n=!1;try{I.lstatSync(t).isDirectory()?a=t:/\.(tgz|gz|tar)$/.test(t)&&(n=!0,a=_e(),await ut.x({file:t,C:a}));const o=X.globSync("**/.blocklet/pages/pages.config.yml",{cwd:a,absolute:!0}).at(0),c=o&&g.join(g.dirname(o),"../../pages"),p=o&&g.join(g.dirname(o),"../../components"),l=o&&g.join(g.dirname(o),"../../routes");if(!o)return null;const S=x.parse(I.readFileSync(o).toString()),A=(m,d,y)=>{let P=g.join(m,`${d}${y?`.${y}`:""}.yml`);return(!I.existsSync(P)||!I.lstatSync(P).isFile())&&(P=g.join(m,d,`index${y?`.${y}`:""}.yml`),!I.existsSync(P)||!I.lstatSync(P))?null:x.parse(I.readFileSync(P).toString())},v=(m,d)=>{try{const y=X.globSync(`*.${d}.yml`,{cwd:m,absolute:!0})[0];return y?x.parse(I.readFileSync(y).toString()):null}catch(y){f.logger.error("parse component error",y)}return null},E=(m,d)=>{let y=g.join(m,`${d}.yml`);return(!I.existsSync(y)||!I.lstatSync(y).isFile())&&(y=g.join(m,d,"index.yml"),!I.existsSync(y)||!I.lstatSync(y))?null:x.parse(I.readFileSync(y).toString())},u=R(S.pages.map(({slug:m})=>{const d=R(S.supportedLocales.map(({locale:j})=>{const r=c?A(c,te(m),j):void 0;if(r)return{locale:j,page:r};const b=c?A(c,m,j):void 0;return b&&{locale:j,page:b}})),y=d[0]?.page;if(!y)return null;const P=y.sections.map(Xe.unzipSection);return{id:y.id||De.nextId(),createdAt:y.createdAt,updatedAt:y.updatedAt,publishedAt:y.publishedAt,isPublic:y.isPublic??!0,templateConfig:y.templateConfig,slug:m,sections:Object.fromEntries(P.map(j=>[j.id,j])),sectionIds:P.map(j=>j.id),locales:Object.fromEntries(d.map(({locale:j,page:r})=>[j,r.meta])),dataSource:y.dataSource?Object.fromEntries([...new Set(d.flatMap(({page:j})=>Object.keys(j.dataSource??{})))].map(j=>[j,Object.fromEntries(d.map(({locale:r,page:b})=>{const T=b.dataSource?.[j];return[r,T||{}]}))])):Object.fromEntries([...new Set(d.flatMap(({page:j})=>j.sections.map(r=>r.id)))].map(j=>[j,Object.fromEntries(d.map(({locale:r,page:b})=>{const T=b.dataSource?.[j];if(T)return[r,T];const N=b.sections.find(B=>B.id===j);return[r,N?.properties||{}]}))]))}})),i=R(S?.routes?.map(({path:m})=>{const d=l?E(l,te(m)):void 0;return{...d,id:d?.id||De.nextId(),createdAt:d?.createdAt??new Date().toISOString(),updatedAt:d?.updatedAt??new Date().toISOString(),publishedAt:new Date(0).toISOString(),path:d?.path??`/${d?.id}`,params:d?.params,handler:d?.handler??"Pages Kit",isPublic:d?.isPublic??!0,enabledGenerate:d?.enabledGenerate??!1,displayTemplateId:d?.displayTemplateId??void 0,dataSource:d?.dataSource??{}}})??[]),h=p?R(S.components?.map(({id:m})=>v(p,m))??[]):[];if(e){const m=(...d)=>{f.logger.info(`[${n?g.basename(t):g.basename(g.join(t,"../../../../"))}] importAssets:`,...d)};try{m("wait image-bin api ready"),await yt.default({resources:[`${Ie.getComponentWebEndpoint(f.IMAGE_BIN_NAME)}/api/sdk/uploads`],validateStatus:P=>P>=200&&P<=500}),m("image-bin api is ready");const d={},y={};m("start to upload assets"),await Promise.allSettled([Re(h,d,{getFilePath:P=>p&&g.join(p,P),onFinish:P=>{m(`upload ${P.length} component assets`)}}),Re(u,y,{getFilePath:(P,j)=>{const r=Ee.default(u,j.slice(0,1));return c&&g.join(c,g.dirname(r.slug),P)},onFinish:P=>{m(`upload ${P.length} page assets`)}})]),m("upload assets done"),ye.clear(),global.gc&&global.gc()}catch(d){m("Error during asset import:",d)}}const D={};if(s){const m=o&&g.join(g.dirname(o),"../../resources/components"),d=R(S.resources?.components?.map(({id:y})=>v(m,y))??[]);d.length>0&&(D.components=Object.fromEntries(d.map((y,P)=>[y.id,{index:P,component:y}])))}return{supportedLocales:S.supportedLocales,pageIds:u.map(m=>m.id),components:Object.fromEntries(h.map((m,d)=>[m.id,{index:d,data:m}])),pages:Object.fromEntries(u.map(m=>[m.id,m])),config:S.config||{},resources:D,routeIds:i.map(m=>m.id),routes:Object.fromEntries(i.map(m=>[m.id,m])),dataSourceIds:[],dataSources:{}}}finally{n&&a&&I.rmSync(a,{force:!0,recursive:!0})}}async function Se(t,e,{routes:s,mergeMode:a="byUpdateTime",deleteRoutes:n=!1,publishMode:o=void 0}={}){try{o&&f.clearPreloadComponentsCacheByMode(o)}catch(E){f.logger.error("clear preload page cache error",{error:E})}const{pages:c,pageIds:p,routeIds:l,routes:S,supportedLocales:A}=t;if(o==="production"){let E=s??[],u=null;for(const i of l??[]){const h=S?.[i];if(h?.params&&h?.params.length>0&&h?.paramsOptions&&h?.paramsOptions.length>0){const D=Q.generateParamCombinations({basePath:h.path,params:h.params,routeId:h.id,paramsOptions:h.paramsOptions,currentIndex:0,currentParams:[],currentOptionIds:[],result:[]}),m=Object.fromEntries(D.map(d=>[`${i}-${d.paramOptionIds.join("-")}`,d]));u={...u||{},...m},s||(E=[...E,...D.map(d=>`${i}-${d.paramOptionIds.join("-")}`)])}else s||E.push(i)}f.logger.info("routeIds to be published: ",E);for(const i of E){let h=i;if(h.includes("-")){const[d]=h.split("-");h=d}const D=S?.[h];if(!D){const d=e.pageIds.indexOf(h);d!==-1&&n&&(e.pageIds.splice(d,1),delete e.pages[h]);for(const y of e.pageIds)y.includes(`${h}-`)&&(e.pageIds.splice(e.pageIds.indexOf(y),1),delete e.pages[y]);f.logger.info("delete main route page",h);continue}if(i.includes("-")&&!u?.[i]){const d=e.pageIds.indexOf(i);d!==-1&&n&&(e.pageIds.splice(d,1),delete e.pages[i]),f.logger.info("delete page",i);continue}if(!D.displayTemplateId){f.logger.info("no display template",i);continue}const m=c[D.displayTemplateId];if(!m){f.logger.info("no template page",i);continue}if(e.pageIds.includes(i)){if(f.logger.info("has need update page",i),a==="replace")e.pages[i]=ue({page:m,route:D,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("replace page",i);else if(a==="byUpdateTime"){const d=e.pages[D.id];(!d||D.updatedAt&&D.updatedAt>d.updatedAt)&&(e.pages[i]=ue({page:m,route:D,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("replace page by update time",i))}}else e.pageIds.push(i),e.pages[i]=ue({page:m,route:D,state:t,routeId:i,routePathInfo:u?.[i]}),f.logger.info("add page",i)}if(n&&!s)for(const i of e.pageIds)E?.includes(i)||(delete e.pages[i],f.logger.info("delete page",i)),e.pageIds=[...e.pageIds].filter(h=>E?.includes(h))}else{for(const E of p){const u=c[E];if(u)if(e.pageIds.includes(u.id)){if(a==="replace")e.pages[u.id]=u;else if(a==="byUpdateTime"){const i=e.pages[u.id];(!i||u.updatedAt&&u.updatedAt>i.updatedAt)&&(e.pages[u.id]=u)}}else e.pageIds.push(u.id),e.pages[u.id]=u}for(const E of l){const u=S[E];if(u)if(e.routeIds.includes(u.id)){if(a==="replace")e.routes[u.id]=u;else if(a==="byUpdateTime"){const i=e.routes[u.id];(!i||u.updatedAt&&u.updatedAt>i.updatedAt)&&(e.routes[u.id]=u)}}else e.routeIds.push(u.id),e.routes[u.id]=u}}if(e.supportedLocales.splice(0,e.supportedLocales.length),e.supportedLocales.push(...gt.default(A)),n)for(const E of Object.keys(e.components))delete e.components[E];let v=JSON.parse(JSON.stringify(t.components));v=Object.fromEntries(await Promise.all(Object.entries(v).map(async([E,u])=>{const i=await qe(u?.data);return[E,{...u,data:i}]}))),Object.assign(e.components,v),Object.assign(e.config,JSON.parse(JSON.stringify(t.config))),we.default(t.resources.components)||(e.resources.components=JSON.parse(JSON.stringify(t.resources.components||{})))}const qe=f.memoizeWithFs(async t=>{if(!we.default(t?.properties))return t;if(t?.renderer?.type==="react-component"){const{script:e,PROPERTIES_SCHEMA:s}=t?.renderer||{};if(s||e&&e.includes("PROPERTIES_SCHEMA"))try{const a=await f.getExportSchemaValueFromCode(e??"","PROPERTIES_SCHEMA",t.id,s);a&&a.length>0&&t&&(t.properties={},a.forEach((n,o)=>{t?.properties&&(t.properties[n.id]={index:o,data:n})}))}catch(a){f.logger.error("getPropertiesFromCode error",{componentId:t.id,name:t.name},{error:a})}}return t},{subdir:"getPropertiesFromCode"});let se,J,ne,oe;const xe=()=>Ie.getResources({types:[{did:Pe,type:ge},{did:Et,type:ge}]}),Mt=()=>{const t=xe(),e={};return t.forEach(s=>{const a=X.globSync("**/.blocklet/pages/pages.config.yml",{cwd:s.path,absolute:!0}).at(0),n=a&&g.join(g.dirname(a),"../../chunks");if(n&&I.existsSync(n)){const o=I.readdirSync(n);for(const c of o)e[c]=g.join(n,c)}}),e};function Be(){return se=(async()=>{const t=xe();J=(await Promise.all(t.map(async s=>{const a=s.path?await Oe(s.path,{importAssets:!1}):void 0;return a?{blockletId:s.did,state:a,blockletTitle:s.title}:void 0}))).filter(s=>!!s),ne=J.reduce((s,a)=>Object.assign(s,Object.fromEntries(Object.values(a.state.pages).map(n=>n?[n?.id,{page:n,blockletId:a.blockletId}]:[]))),{});const e=J.reduce((s,a)=>Object.assign(s,Object.fromEntries(Object.values(a.state.components).map(n=>[n.data.id,{blockletId:a.blockletId,component:n.data}]))),{});oe=Object.fromEntries(await Promise.all(Object.entries(e).map(async([s,a])=>{const n=await qe(a.component);return[s,{...a,component:n}]})))})(),se}function Ve(t){const e=Le.default(async()=>{await Be().catch(s=>{f.logger.error("load resource states error",{error:s})}),await t?.({states:J,pages:ne,components:oe})},3e3,{leading:!1,trailing:!0});return e(),w.default.events.on(w.default.Events.componentAdded,e),w.default.events.on(w.default.Events.componentRemoved,e),w.default.events.on(w.default.Events.componentStarted,e),w.default.events.on(w.default.Events.componentStopped,e),w.default.events.on(w.default.Events.componentUpdated,e),w.default.events.on(me,e),()=>{w.default.events.off(w.default.Events.componentAdded,e),w.default.events.off(w.default.Events.componentRemoved,e),w.default.events.off(w.default.Events.componentStarted,e),w.default.events.off(w.default.Events.componentStopped,e),w.default.events.off(w.default.Events.componentUpdated,e),w.default.events.off(me,e)}}const Ge=Symbol.for("GLOBAL_RESOURCE_STATES_LISTENER_KEY"),ze=Symbol.for("GLOBAL_ENV_UPDATE_LISTENER_KEY"),re=globalThis;re[Ge]?.();re[Ge]=Ve(async({pages:t,components:e})=>{const s=await O.getProjectIds();f.logger.info(`start update resource states projects(${s.length})`,s),await Promise.race([new Promise(a=>{setTimeout(()=>{a({})},30*1e3)}),Promise.all(s.map(async a=>{Ye({projectId:a,pages:t,components:e})}))]).catch(a=>{f.logger.error("update resource states failed:",a)})});re[ze]?.();re[ze]=()=>{const t=()=>{O.pageUrlMapCache.clear(),f.logger.info("[Cache CLEAR ALL] clear all page url map cache by env update")};return w.default.events.on(w.default.Events.envUpdate,t),()=>{w.default.events.off(w.default.Events.envUpdate,t)}};O.startPeriodicCheck();process.on("beforeExit",()=>{O.stopPeriodicCheck()});process.on("SIGINT",()=>{O.stopPeriodicCheck(),process.exit(0)});process.on("SIGTERM",()=>{O.stopPeriodicCheck(),process.exit(0)});async function Ye({projectId:t,pages:e,components:s}){const a=O.sharedInstances[t];if(!a){f.logger.info(`projectId: ${t} not found in sharedInstances`);return}if(a.syncedStore.resources.pages=e,(await L.findByPk(t))?.useAllResources)a.syncedStore.resources.components=s;else{const c=(await Ae.findAll({where:{projectId:t}})).map(l=>l.componentId),p=Object.fromEntries(Object.entries(s||{}).filter(([l])=>c.includes(l)));a.syncedStore.resources.components=p}f.logger.info(`update [${t}] resource states:`,{pages:Object.keys(a.syncedStore.resources.pages||{}).length,components:Object.keys(a.syncedStore.resources.components||{}).length})}async function $t(t){return Ye({projectId:t,pages:ne,components:oe})}async function Nt(){f.logger.info("trigger reload all project resource"),w.default.events.emit(me)}async function Ft({ensureLoaded:t=!0}={}){return t&&(se??=Be(),await se),{states:J,pages:ne,components:oe}}exports.COMPONENT_DID=Pe;exports.PUBLISH_MODES=_t;exports.Project=L;exports.RESOURCE_TYPE=ge;exports.SITE_STATE_PATH=q;exports.STATE_MODES=Lt;exports.SiteState=O;exports.downloadAsset=Me;exports.downloadAssets=$e;exports.fromPackage=Oe;exports.getDefaultState=Ue;exports.getResourceStates=Ft;exports.initPackResourceStates=Ve;exports.mergeState=Se;exports.toPackage=Fe;exports.triggerReloadAllProjectResource=Nt;exports.updateResourceStatesByProjectId=$t;
|