@vcp-dev/site-edit-bridge 0.6.2 → 0.6.4

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.
@@ -1,6 +1,6 @@
1
- import ze from"node:http";import{existsSync as y,readFileSync as m}from"node:fs";import{createRequire as ae}from"node:module";import I from"node:path";import{basename as ue,dirname as x,join as b}from"node:path";import{fileURLToPath as ce}from"node:url";import Ie from"node:http";import{createHash as Z,randomBytes as xe,timingSafeEqual as be}from"node:crypto";import V from"node:path";import{WebSocket as X,WebSocketServer as ke}from"ws";import{createPreviewSiteEditEngineRuntime as Ce,openFileHistoryStore as ve}from"@vcp-dev/site-edit-engine";import{resolveNextAppRouteId as Ee,scanNextAppRouterRoutes as _e}from"@vcp-dev/site-edit-engine/next-app-router";import{SiteEditSourceWatcher as Pe}from"@vcp-dev/site-edit-engine/source-watcher";function B(t){let e=new Map,n=[],s=new Map,i=[],r=t.pageComposition.status==="supported"?t.pageComposition.placementUnitKeys:[],o=new Map(r.map((a,u)=>[a,u])),c=t.pageComposition.status==="supported"?{status:"supported",containerKey:t.pageComposition.containerKey,hasBusinessMembers:t.pageComposition.hasBusinessMembers,placementUnitKeys:t.pageComposition.placementUnitKeys,placements:t.pageComposition.placements.map(a=>({placementKey:a.placementKey,placementUnitKey:a.placementUnitKey,relation:a.relation}))}:{status:"unsupported",reason:t.pageComposition.reason,diagnostics:t.pageComposition.diagnostics},d=t.entries.flatMap((a,u)=>a.dynamicDataSummary?[[u,a.dynamicDataSummary]]:[]),h=a=>{let u=e.get(a);if(typeof u=="number")return u;let p=n.length;return n.push(a),e.set(a,p),p},f=a=>{let u=null;for(let p of ye(a)){let k=`${u??"root"}\0${p}`,S=s.get(k);if(typeof S=="number"){u=S;continue}let w=i.length;i.push([u,h(p)]),s.set(k,w),u=w}if(u===null)throw new Error("Document key must not be empty");return u};return{encoding:"segment-trie-v1",siteId:t.siteId,snapshotId:t.snapshotId,routeId:t.routeId,version:t.version,pageComposition:c,tokens:n,nodes:i,entries:t.entries.map(a=>{let u=a.pageCompositionUnitKey?o.get(a.pageCompositionUnitKey)??-1:-1;if(a.pageCompositionUnitKey&&u<0)throw new Error(`Render entry ${a.key} references unknown page composition unit ${a.pageCompositionUnitKey}`);return[f(a.key),me(a.operationSummary),...u>=0?[u]:[]]}),...d.length>0?{dynamicDataSummaries:d}:{}}}function me(t){return(t.canUpdateText?1:0)|(t.canInsertChild?2:0)|(t.canMove?4:0)|(t.canRemove?8:0)}function ye(t){if(!t)return[];let e=[],n=0,s=L(t,n);if(s<0)return e.push(t),e;for(e.push(t.slice(0,s)),n=s;n<t.length;){if(s=L(t,n+(t.startsWith("::",n)?2:1)),s<0){e.push(t.slice(n));break}e.push(t.slice(n,s)),n=s}return e}function L(t,e){let n=t.indexOf("/",e),s=t.indexOf("::",e);return n<0?s:s<0?n:Math.min(n,s)}var g=class t{constructor(){}static parseMessage(e,n={}){let s=JSON.parse(e);if(!t.isMessage(s,n))throw new Error("Invalid site edit bridge message");return s}static stringifyMessage(e){return JSON.stringify(e)}static extractRequestInfo(e){if(typeof e=="object"&&e!==null&&"payload"in e&&typeof e.payload=="object"&&e.payload!==null){let n=e.payload,s=typeof n.id=="string"?n.id:"unknown",i=t.isRequestKind(n.kind)?n.kind:"get-document";return{id:s,kind:i}}return{id:"unknown",kind:"get-document"}}static isRequestKind(e){return e==="initialize-session"||e==="get-document"||e==="get-history-status"||e==="get-style-context"||e==="get-object-edit-context"||e==="get-class-name-source"||e==="get-source-tree"||e==="get-source-file"||e==="resolve-source-location"||e==="execute"||e==="undo"||e==="redo"}static isMessage(e,n={}){return!l(e)||typeof e.type!="string"?!1:e.type==="request"?t.isRequestPayload(e.payload,n):e.type==="response"||e.type==="error"?l(e.payload)&&typeof e.payload.id=="string":e.type==="event"?l(e.payload)&&l(e.payload.event):e.type==="auth"?l(e.payload)&&typeof e.payload.key=="string":e.type==="auth-ok"?l(e.payload)&&e.payload.ok===!0:!1}static isRequestPayload(e,n={}){if(!l(e)||typeof e.id!="string"||!t.isRequestKind(e.kind))return!1;switch(e.kind){case"initialize-session":return typeof e.routeId=="string"&&(e.mutationContext===void 0||j(e.mutationContext));case"get-document":case"get-style-context":return typeof e.routeId=="string";case"get-object-edit-context":case"get-class-name-source":case"resolve-source-location":return typeof e.key=="string"&&(e.routeId===void 0||typeof e.routeId=="string");case"get-history-status":return e.mutationContext===void 0;case"get-source-tree":return!0;case"get-source-file":return typeof e.path=="string";case"execute":return Se(e.request)&&(e.request.mutationContext===void 0||j(e.request.mutationContext))&&(n.validateOperationRequest?.(e.request)??!0);case"undo":case"redo":return e.mutationContext===void 0||j(e.mutationContext)}}},v=class{constructor(e){this.options=e,this.messageQueue=new M,this.eventDispatcher=new D(n=>{this.sendJson(n)}),this.unsubscribe=e.engine.subscribe(n=>{this.eventDispatcher.push(n)}),e.socket.onmessage=n=>{try{this.messageQueue.enqueue(()=>this.handleRawMessage(n.data))}catch(s){this.sendJson({type:"error",payload:{id:"unknown",kind:"get-document",message:s instanceof Error?s.message:"Bridge queue overflow"}}),e.socket.close(1013,"bridge busy")}},e.socket.onclose=()=>{this.unsubscribe()}}async handleMessage(e){try{let n=e;if(n.type!=="request")return;let s=n.payload,{id:i}=s;switch(s.kind){case"initialize-session":{let r=await this.options.engine.initializeSession({routeId:s.routeId,...s.mutationContext?{mutationContext:s.mutationContext}:{}});this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:{ok:!0,data:{document:B(r.data.document),historyStatus:r.data.historyStatus}}}});return}case"get-document":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:{ok:!0,data:B((await this.options.engine.getDocument(s.routeId)).data)}}});return;case"get-history-status":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getHistoryStatus()}});return;case"get-style-context":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getStyleContext(s.routeId)}});return;case"get-object-edit-context":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getObjectEditContext(s.key,s.routeId)}});return;case"get-class-name-source":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getClassNameSource(s.key,s.routeId)}});return;case"get-source-tree":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getSourceTree()}});return;case"get-source-file":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.getSourceFile({path:s.path})}});return;case"resolve-source-location":this.sendJson({type:"response",payload:{id:i,kind:s.kind,result:await this.options.engine.resolveSourceLocation({key:s.key,routeId:s.routeId})}});return;case"execute":{await this.handleBufferedOperation(i,s.kind,()=>this.options.engine.execute(s.request));return}case"undo":await this.handleBufferedOperation(i,s.kind,()=>this.options.engine.undo());return;case"redo":await this.handleBufferedOperation(i,s.kind,()=>this.options.engine.redo());return}}catch(n){let s=g.extractRequestInfo(e);this.sendJson({type:"error",payload:{...s,message:J(n),code:$(n)}})}}async handleRawMessage(e){try{await this.handleMessage(g.parseMessage(e,{validateOperationRequest:this.options.validateOperationRequest}))}catch(n){let s=n instanceof Error?n.message:"Invalid websocket message",i=Re(e);this.sendJson({type:"error",payload:{...i,message:s}})}}sendJson(e){this.options.socket.send(g.stringifyMessage(e))}async handleBufferedOperation(e,n,s){this.eventDispatcher.beginBuffering();try{let i=await s();this.sendJson({type:"response",payload:{id:e,kind:n,result:i}}),this.eventDispatcher.flush()}catch(i){this.sendJson({type:"error",payload:{id:e,kind:n,message:J(i),code:$(i)}}),this.eventDispatcher.flush()}}},D=class{constructor(e){this.send=e,this.bufferedEvents=null}push(e){let n={type:"event",payload:{event:e}};if(this.bufferedEvents){this.bufferedEvents.push(n);return}this.send(n)}beginBuffering(){this.bufferedEvents=[]}flush(){let e=this.bufferedEvents??[];this.bufferedEvents=null,e.forEach(n=>{this.send(n)})}},M=class{constructor(e=64){this.maxQueued=e,this.queue=Promise.resolve(),this.queued=0}enqueue(e){if(this.queued>=this.maxQueued)throw new Error("Bridge request queue is full");this.queued+=1;let n=async()=>{this.queued-=1,await e()},s=this.queue.then(n,n);return this.queue=s.then(()=>{},()=>{}),s}};function l(t){return typeof t=="object"&&t!==null}function Se(t){return l(t)&&typeof t.id=="string"&&typeof t.siteId=="string"&&typeof t.baseSnapshotId=="string"&&(t.routeId===void 0||typeof t.routeId=="string")&&typeof t.kind=="string"&&l(t.target)&&typeof t.documentVersion=="number"&&Number.isInteger(t.documentVersion)&&t.documentVersion>=0&&l(t.params)}function j(t){return we(t)}function $(t){if(!t||typeof t!="object")return;let e=t.code;return typeof e=="string"&&e.length>0?e:void 0}function J(t){if(t instanceof Error)return t.message;if(!t||typeof t!="object")return"Unknown websocket operation failure";let e=t.message;return typeof e=="string"&&e.length>0?e:"Unknown websocket operation failure"}function we(t){return l(t)?typeof t.editingSessionId=="string"&&t.editingSessionId.length>0&&typeof t.lockId=="string"&&t.lockId.length>0&&typeof t.ownerId=="string"&&t.ownerId.length>0&&typeof t.leaseAssertion=="string"&&t.leaseAssertion.length>0:!1}function Re(t){try{return g.extractRequestInfo(JSON.parse(t))}catch{return{id:"unknown",kind:"get-document"}}}var E=class{constructor(){this.tail=Promise.resolve()}run(e){let n=this.tail.then(e,e);return this.tail=n.then(()=>{},()=>{}),n}};import{SiteEditOperationRequestSchema as Te}from"@vcp-dev/contracts";var He="127.0.0.1",Be=30218,je="/__vcp/site-edit/bridge",ee=6*1024*1024,De=ee*2,Me=3e3,W=50,z="__VCP_SITE_EDIT_BRIDGE_SERVERS__";function te(t,e,n={}){let s=new A(Ue(e),t,!1,n);return s.start({listen:!1}),s}var A=class{constructor(e,n=Ie.createServer((r,o)=>{o.writeHead(404),o.end("VCP site edit bridge server")}),s=!0,i={}){this.options=e,this.server=n,this.closeHttpServerOnStop=s,this.dependencies=i,this.runtime=null,this.activeHistorySessionId=null,this.historySessionTransition=Promise.resolve(),this.mutationQueue=new E,this.eventListeners=new Set,this.routes=[],this.stopWatching=null,this.stopWebSocketServer=null,this.pendingChangedFiles=new Set,this.fileChangeTimer=null,this.isStarted=!1,this.runtimeVersionFloor=0,this.activeHistoryStore=null,this.versionPersistence=Promise.resolve()}start(e={}){if(this.isStarted)return;this.refreshRoutes(),this.isStarted=!0,this.createRuntime();let n=Fe({server:this.server,path:this.options.path,sessionKey:this.options.sessionKey,host:this.options.host,allowedOrigins:this.options.allowedOrigins,engine:this.createBridgeEngine()});this.stopWebSocketServer=n.stop,this.stopWatching=new Pe({projectRoot:this.options.projectRoot,watch_dirs:this.options.runtime.watch_dirs,debounce_ms:this.options.runtime.watch_debounce_ms??W,max_dirs:this.options.runtime.watch_max_dirs,max_files:this.options.runtime.watch_max_files,max_depth:this.options.runtime.watch_max_depth,max_startup_ms:this.options.runtime.watch_max_startup_ms,on_change:s=>this.handleFileChanged(s),on_batch_change:s=>this.handleFilesChanged(s)}).start(),e.listen!==!1&&this.server.listen(this.options.port,this.options.host)}getHealth(){let e=!!this.runtime;return{ok:this.isStarted&&e,bridgePath:this.options.path,runtimeReady:e,routesIndexed:this.routes.length,sessionRequired:!!this.options.sessionKey}}stop(){this.stopWatching?.(),this.stopWatching=null,this.flushChangedFiles(),this.fileChangeTimer&&(clearTimeout(this.fileChangeTimer),this.fileChangeTimer=null),this.stopWebSocketServer?.(),this.stopWebSocketServer=null,this.runtime?.dispose(),this.runtime=null,this.closeHttpServerOnStop&&this.server.close();let e=Ve(),n=Ae(this.options);e.get(n)===this&&e.delete(n),this.isStarted=!1}createRuntime(e,n=null){let s=this.runtime;this.activeHistoryStore=e??null;let i=(this.dependencies.createRuntime??Ce)({...this.options.tenantId?{tenantId:this.options.tenantId}:{},siteId:this.options.siteId,snapshotId:this.options.snapshotId,projectRoot:this.options.projectRoot,routes:this.routes,onEvent:r=>{"version"in r&&typeof r.version=="number"&&this.recordRuntimeVersion(r.version);for(let o of this.eventListeners)o(r)},...e?{historyStore:e}:{},initialDocumentVersion:this.runtimeVersionFloor});this.runtime=i,this.activeHistorySessionId=n,s?.dispose()}createBridgeEngine(){return{initializeSession:e=>this.mutationQueue.run(async()=>{let n=await this.bindHistorySession(this.options.snapshotId);if(!n.ok)throw new Error(`${n.code}: ${n.message}`);let{runtime:s,resolvedRouteId:i}=await this.activateRoute(e.routeId),r=s.getDocument(i).data;await this.recordRuntimeVersion(r.version);let o=(await s.getHistoryStatus()).data;return{ok:!0,data:{document:r,historyStatus:o}}}),getDocument:async e=>{let{runtime:n,resolvedRouteId:s}=await this.activateRoute(e),i=n.getDocument(s);return await this.recordRuntimeVersion(i.data.version),i},getHistoryStatus:async()=>this.getRuntime().getHistoryStatus(),getStyleContext:async e=>{let{runtime:n,resolvedRouteId:s}=await this.activateRoute(e);return n.getStyleContext(s)},getObjectEditContext:async(e,n)=>this.getRuntime().getObjectEditContext(e,n?this.resolveRouteId(n):void 0),getClassNameSource:async(e,n)=>{let s=this.getRuntime(),i=n?this.resolveRouteId(n):void 0;return s.getClassNameSource(e,i)},getSourceTree:async()=>this.getRuntime().getSourceTree(),getSourceFile:async e=>this.getRuntime().getSourceFile(e),resolveSourceLocation:async e=>this.getRuntime().resolveSourceLocation({key:e.key,routeId:e.routeId?this.resolveRouteId(e.routeId):void 0}),execute:(e,n)=>this.mutationQueue.run(async()=>{let s=await this.bindHistorySession(this.options.snapshotId);if(!s.ok)return U(s,e);let i=await this.getRuntime().executeOperation(this.normalizeRequest(e));return i.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(i.data.resultVersion),i}),undo:()=>this.mutationQueue.run(async()=>{let e=await this.bindHistorySession(this.options.snapshotId);if(!e.ok)return U(e);let n=await this.getRuntime().undo();return n.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(n.data.resultVersion),n}),redo:()=>this.mutationQueue.run(async()=>{let e=await this.bindHistorySession(this.options.snapshotId);if(!e.ok)return U(e);let n=await this.getRuntime().redo();return n.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(n.data.resultVersion),n}),subscribe:e=>(this.eventListeners.add(e),()=>this.eventListeners.delete(e))}}async bindHistorySession(e){if(!this.options.historyRoot)return{ok:!1,code:"RECOVERY_PERSIST_FAILED",message:"Durable edit history is not configured."};if(this.activeHistorySessionId===e)return{ok:!0};let n=this.historySessionTransition.then(async()=>{if(this.activeHistorySessionId===e)return;let s=await(this.dependencies.openHistoryStore??ve)(Oe({root:this.options.historyRoot,siteId:this.options.siteId,snapshotId:this.options.snapshotId,editingSessionId:e}));if(!this.isStarted)return;let i=s.readDocumentVersion?await s.readDocumentVersion():0;this.runtimeVersionFloor=Math.max(this.runtimeVersionFloor,i),this.createRuntime(s,e)});this.historySessionTransition=n.catch(()=>{});try{return await n,{ok:!0}}catch{return{ok:!1,code:"RECOVERY_PERSIST_FAILED",message:"Durable edit history could not be opened."}}}recordRuntimeVersion(e){this.runtimeVersionFloor=Math.max(this.runtimeVersionFloor,e);let n=this.activeHistoryStore;return n?.writeDocumentVersion?(this.versionPersistence=this.versionPersistence.then(()=>n.writeDocumentVersion(this.runtimeVersionFloor)).catch(()=>{}),this.versionPersistence):Promise.resolve()}async activateRoute(e){let n=this.getRuntime(),s=this.resolveRouteId(e);return await n.setActiveRoute(s),{runtime:n,resolvedRouteId:s}}normalizeRequest(e){return e.target.kind!=="route"?e.routeId?{...e,routeId:this.resolveRouteId(e.routeId)}:e:{...e,target:{...e.target,routeId:this.resolveRouteId(e.target.routeId)}}}handleFileChanged(e){this.handleFilesChanged([e])}handleFilesChanged(e){for(let s of e)this.pendingChangedFiles.add(s);this.fileChangeTimer&&clearTimeout(this.fileChangeTimer);let n=this.options.runtime.watch_debounce_ms??W;this.fileChangeTimer=setTimeout(()=>{this.fileChangeTimer=null,this.flushChangedFiles()},n),this.fileChangeTimer?.unref?.()}flushChangedFiles(){if(this.fileChangeTimer&&(clearTimeout(this.fileChangeTimer),this.fileChangeTimer=null),this.pendingChangedFiles.size===0)return;let e=[...this.pendingChangedFiles].sort((n,s)=>n.localeCompare(s));this.pendingChangedFiles.clear(),this.refreshRoutes();for(let n of e)this.runtime?.notifyFileChanged(n)}refreshRoutes(){let e=this.options.runtime.routes?.length?this.options.runtime.routes:_e(this.options.projectRoot),n=new Map(this.routes.map(i=>[i.routeId,i.entryFile])),s=new Set(e.map(i=>i.routeId));this.routes=e;for(let i of e)n.get(i.routeId)!==i.entryFile&&this.runtime?.registerRoute(i.routeId,i.entryFile);for(let i of n.keys())s.has(i)||this.runtime?.unregisterRoute(i)}resolveRouteId(e){let n=Ee(e,this.routes,this.options.basePath);if(!n)throw new Error(`Route is not indexed by the preview bridge: ${e}`);return n}getRuntime(){if(!this.runtime)throw new Error("VCP site edit preview bridge runtime is not ready");return this.runtime}};function Ue(t){return{tenantId:t.tenantId,siteId:t.siteId,snapshotId:t.snapshotId,projectRoot:V.resolve(t.projectRoot??process.cwd()),basePath:t.basePath??"",host:t.host??He,port:t.port??Be,path:t.path??je,sessionKey:t.sessionKey??We(),historyRoot:t.historyRoot,allowedOrigins:t.allowedOrigins??[],runtime:t.runtime??{}}}function Oe(t){return V.join(V.resolve(t.root),G(t.siteId),G(t.snapshotId),Z("sha256").update(t.editingSessionId).digest("hex").slice(0,32))}function G(t){return/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(t)&&t!=="."&&t!==".."?t:Z("sha256").update(t).digest("hex").slice(0,32)}function Ve(){let t=globalThis;return t[z]??=new Map,t[z]}function Ae(t){return`${t.projectRoot}:${t.host}:${t.port}:${t.path}:${t.historyRoot??""}`}function U(t,e){return{ok:!1,error:{code:t.code,message:t.message},data:{requestId:e?.id??"mutation-context",kind:e?.kind??"update-text",target:e?.target??{kind:"node",key:""},resultVersion:e?.documentVersion??0}}}function Fe(t){let e=new ke({maxPayload:ee,noServer:!0}),n=new WeakSet,s=r=>{new v({socket:new F(r),engine:t.engine,validateOperationRequest:o=>Te.safeParse(o).success})},i=(r,o,c)=>{if(!Ne(r,t))return;let d=Ke(r,t);if(!d){o.write(`HTTP/1.1 401 Unauthorized\r
1
+ import st from"node:http";import{existsSync as w,readFileSync as S}from"node:fs";import{createRequire as pe}from"node:module";import R from"node:path";import{basename as ge,dirname as x,join as b}from"node:path";import{fileURLToPath as me}from"node:url";import Te from"node:http";import{createHash as ie,randomBytes as He,timingSafeEqual as Be}from"node:crypto";import L from"node:path";import{WebSocket as oe,WebSocketServer as je}from"ws";import{createPreviewSiteEditEngineRuntime as Ae,openFileHistoryStore as De}from"@vcp-dev/site-edit-engine";import{resolveNextAppRouteId as Me,scanNextAppRouterRoutes as Ue}from"@vcp-dev/site-edit-engine/next-app-router";import{SiteEditSourceWatcher as Ne}from"@vcp-dev/site-edit-engine/source-watcher";function U(t){let e=new Map,n=[],s=new Map,r=[],i=t.pageComposition.status==="supported"?t.pageComposition.placementUnitKeys:[],o=new Map(i.map((a,u)=>[a,u])),c=t.pageComposition.status==="supported"?{status:"supported",containerKey:t.pageComposition.containerKey,hasBusinessMembers:t.pageComposition.hasBusinessMembers,placementUnitKeys:t.pageComposition.placementUnitKeys,placements:t.pageComposition.placements.map(a=>({placementKey:a.placementKey,placementUnitKey:a.placementUnitKey,relation:a.relation}))}:{status:"unsupported",reason:t.pageComposition.reason,diagnostics:t.pageComposition.diagnostics},d=t.entries.flatMap((a,u)=>a.dynamicDataSummary?[[u,a.dynamicDataSummary]]:[]),h=a=>{let u=e.get(a);if(typeof u=="number")return u;let l=n.length;return n.push(a),e.set(a,l),l},g=a=>{let u=null;for(let l of Ce(a)){let k=`${u??"root"}\0${l}`,I=s.get(k);if(typeof I=="number"){u=I;continue}let m=r.length;r.push([u,h(l)]),s.set(k,m),u=m}if(u===null)throw new Error("Document key must not be empty");return u};return{encoding:"segment-trie-v1",siteId:t.siteId,snapshotId:t.snapshotId,routeId:t.routeId,version:t.version,pageComposition:c,tokens:n,nodes:r,entries:t.entries.map(a=>{let u=a.pageCompositionUnitKey?o.get(a.pageCompositionUnitKey)??-1:-1;if(a.pageCompositionUnitKey&&u<0)throw new Error(`Render entry ${a.key} references unknown page composition unit ${a.pageCompositionUnitKey}`);return[g(a.key),ve(a.operationSummary),...u>=0?[u]:[]]}),...d.length>0?{dynamicDataSummaries:d}:{}}}function ve(t){return(t.canUpdateText?1:0)|(t.canInsertChild?2:0)|(t.canMove?4:0)|(t.canRemove?8:0)}function Ce(t){if(!t)return[];let e=[],n=0,s=Y(t,n);if(s<0)return e.push(t),e;for(e.push(t.slice(0,s)),n=s;n<t.length;){if(s=Y(t,n+(t.startsWith("::",n)?2:1)),s<0){e.push(t.slice(n));break}e.push(t.slice(n,s)),n=s}return e}function Y(t,e){let n=t.indexOf("/",e),s=t.indexOf("::",e);return n<0?s:s<0?n:Math.min(n,s)}var p=class t{constructor(){}static parseMessage(e,n={}){let s=JSON.parse(e);if(!t.isMessage(s,n))throw new Error("Invalid site edit bridge message");return s}static stringifyMessage(e){return JSON.stringify(e)}static extractRequestInfo(e){if(typeof e=="object"&&e!==null&&"payload"in e&&typeof e.payload=="object"&&e.payload!==null){let n=e.payload,s=typeof n.id=="string"?n.id:"unknown",r=t.isRequestKind(n.kind)?n.kind:"get-document";return{id:s,kind:r}}return{id:"unknown",kind:"get-document"}}static isRequestKind(e){return e==="initialize-session"||e==="get-document"||e==="get-history-status"||e==="get-style-context"||e==="get-object-edit-context"||e==="get-class-name-source"||e==="get-source-tree"||e==="get-source-file"||e==="resolve-source-location"||e==="execute"||e==="undo"||e==="redo"}static isMessage(e,n={}){return!f(e)||typeof e.type!="string"?!1:e.type==="request"?t.isRequestPayload(e.payload,n):e.type==="response"||e.type==="error"?f(e.payload)&&typeof e.payload.id=="string":e.type==="event"?f(e.payload)&&f(e.payload.event):e.type==="auth"?f(e.payload)&&typeof e.payload.key=="string":e.type==="auth-ok"?f(e.payload)&&e.payload.ok===!0:!1}static isRequestPayload(e,n={}){if(!f(e)||typeof e.id!="string"||!t.isRequestKind(e.kind))return!1;switch(e.kind){case"initialize-session":return typeof e.routeId=="string"&&(e.mutationContext===void 0||N(e.mutationContext));case"get-document":case"get-style-context":return typeof e.routeId=="string";case"get-object-edit-context":case"get-class-name-source":case"resolve-source-location":return typeof e.key=="string"&&(e.routeId===void 0||typeof e.routeId=="string");case"get-history-status":return e.mutationContext===void 0;case"get-source-tree":return!0;case"get-source-file":return typeof e.path=="string";case"execute":return Ee(e.request)&&(e.request.mutationContext===void 0||N(e.request.mutationContext))&&(n.validateOperationRequest?.(e.request)??!0);case"undo":case"redo":return e.mutationContext===void 0||N(e.mutationContext)}}},P=class{constructor(e){this.options=e,this.messageQueue=new V,this.eventDispatcher=new O(n=>{this.sendJson(n)}),this.unsubscribe=e.engine.subscribe(n=>{this.eventDispatcher.push(n)}),e.socket.onmessage=n=>{try{this.messageQueue.enqueue(()=>this.handleRawMessage(n.data))}catch(s){this.sendJson({type:"error",payload:{id:"unknown",kind:"get-document",message:s instanceof Error?s.message:"Bridge queue overflow"}}),e.socket.close(1013,"bridge busy")}},e.socket.onclose=()=>{this.unsubscribe()}}async handleMessage(e){try{let n=e;if(n.type!=="request")return;let s=n.payload,{id:r}=s;switch(s.kind){case"initialize-session":{let i=await this.options.engine.initializeSession({routeId:s.routeId,...s.mutationContext?{mutationContext:s.mutationContext}:{}});this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:{ok:!0,data:{document:U(i.data.document),historyStatus:i.data.historyStatus}}}});return}case"get-document":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:{ok:!0,data:U((await this.options.engine.getDocument(s.routeId)).data)}}});return;case"get-history-status":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getHistoryStatus()}});return;case"get-style-context":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getStyleContext(s.routeId)}});return;case"get-object-edit-context":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getObjectEditContext(s.key,s.routeId)}});return;case"get-class-name-source":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getClassNameSource(s.key,s.routeId)}});return;case"get-source-tree":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getSourceTree()}});return;case"get-source-file":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.getSourceFile({path:s.path})}});return;case"resolve-source-location":this.sendJson({type:"response",payload:{id:r,kind:s.kind,result:await this.options.engine.resolveSourceLocation({key:s.key,routeId:s.routeId})}});return;case"execute":{await this.handleBufferedOperation(r,s.kind,()=>this.options.engine.execute(s.request));return}case"undo":await this.handleBufferedOperation(r,s.kind,()=>this.options.engine.undo());return;case"redo":await this.handleBufferedOperation(r,s.kind,()=>this.options.engine.redo());return}}catch(n){let s=p.extractRequestInfo(e);this.sendJson({type:"error",payload:{...s,message:X(n),code:Z(n)}})}}async handleRawMessage(e){try{await this.handleMessage(p.parseMessage(e,{validateOperationRequest:this.options.validateOperationRequest}))}catch(n){let s=n instanceof Error?n.message:"Invalid websocket message",r=Pe(e);this.sendJson({type:"error",payload:{...r,message:s}})}}sendJson(e){this.options.socket.send(p.stringifyMessage(e))}async handleBufferedOperation(e,n,s){this.eventDispatcher.beginBuffering();try{let r=await s();this.sendJson({type:"response",payload:{id:e,kind:n,result:r}}),this.eventDispatcher.flush()}catch(r){this.sendJson({type:"error",payload:{id:e,kind:n,message:X(r),code:Z(r)}}),this.eventDispatcher.flush()}}},O=class{constructor(e){this.send=e,this.bufferedEvents=null}push(e){let n={type:"event",payload:{event:e}};if(this.bufferedEvents){this.bufferedEvents.push(n);return}this.send(n)}beginBuffering(){this.bufferedEvents=[]}flush(){let e=this.bufferedEvents??[];this.bufferedEvents=null,e.forEach(n=>{this.send(n)})}},V=class{constructor(e=64){this.maxQueued=e,this.queue=Promise.resolve(),this.queued=0}enqueue(e){if(this.queued>=this.maxQueued)throw new Error("Bridge request queue is full");this.queued+=1;let n=async()=>{this.queued-=1,await e()},s=this.queue.then(n,n);return this.queue=s.then(()=>{},()=>{}),s}};function f(t){return typeof t=="object"&&t!==null}function Ee(t){return f(t)&&typeof t.id=="string"&&typeof t.siteId=="string"&&typeof t.baseSnapshotId=="string"&&(t.routeId===void 0||typeof t.routeId=="string")&&typeof t.kind=="string"&&f(t.target)&&typeof t.documentVersion=="number"&&Number.isInteger(t.documentVersion)&&t.documentVersion>=0&&f(t.params)}function N(t){return _e(t)}function Z(t){if(!t||typeof t!="object")return;let e=t.code;return typeof e=="string"&&e.length>0?e:void 0}function X(t){if(t instanceof Error)return t.message;if(!t||typeof t!="object")return"Unknown websocket operation failure";let e=t.message;return typeof e=="string"&&e.length>0?e:"Unknown websocket operation failure"}function _e(t){return f(t)?typeof t.editingSessionId=="string"&&t.editingSessionId.length>0&&typeof t.lockId=="string"&&t.lockId.length>0&&typeof t.ownerId=="string"&&t.ownerId.length>0&&typeof t.leaseAssertion=="string"&&t.leaseAssertion.length>0:!1}function Pe(t){try{return p.extractRequestInfo(JSON.parse(t))}catch{return{id:"unknown",kind:"get-document"}}}var T=class{constructor(){this.tail=Promise.resolve()}run(e){let n=this.tail.then(e,e);return this.tail=n.then(()=>{},()=>{}),n}};import{SiteEditOperationRequestSchema as Oe}from"@vcp-dev/contracts";var Ve="127.0.0.1",Fe=30218,Ke="/__vcp/site-edit/bridge",ae=6*1024*1024,Le=ae*2,qe=3e3,ee=50,te="__VCP_SITE_EDIT_BRIDGE_SERVERS__";function ue(t,e,n={}){let s=new q($e(e),t,!1,n);return s.start({listen:!1}),s}var q=class{constructor(e,n=Te.createServer((i,o)=>{o.writeHead(404),o.end("VCP site edit bridge server")}),s=!0,r={}){this.options=e,this.server=n,this.closeHttpServerOnStop=s,this.dependencies=r,this.runtime=null,this.activeHistorySessionId=null,this.historySessionTransition=Promise.resolve(),this.mutationQueue=new T,this.eventListeners=new Set,this.routes=[],this.stopWatching=null,this.stopWebSocketServer=null,this.pendingChangedFiles=new Set,this.fileChangeTimer=null,this.isStarted=!1,this.runtimeVersionFloor=0,this.activeHistoryStore=null,this.versionPersistence=Promise.resolve()}start(e={}){if(this.isStarted)return;this.refreshRoutes(),this.isStarted=!0,this.createRuntime();let n=Ge({server:this.server,path:this.options.path,sessionKey:this.options.sessionKey,host:this.options.host,allowedOrigins:this.options.allowedOrigins,engine:this.createBridgeEngine()});this.stopWebSocketServer=n.stop,this.stopWatching=new Ne({projectRoot:this.options.projectRoot,watch_dirs:this.options.runtime.watch_dirs,debounce_ms:this.options.runtime.watch_debounce_ms??ee,max_dirs:this.options.runtime.watch_max_dirs,max_files:this.options.runtime.watch_max_files,max_depth:this.options.runtime.watch_max_depth,max_startup_ms:this.options.runtime.watch_max_startup_ms,on_change:s=>this.handleFileChanged(s),on_batch_change:s=>this.handleFilesChanged(s)}).start(),e.listen!==!1&&this.server.listen(this.options.port,this.options.host)}getHealth(){let e=!!this.runtime;return{ok:this.isStarted&&e,bridgePath:this.options.path,runtimeReady:e,routesIndexed:this.routes.length,sessionRequired:!!this.options.sessionKey}}stop(){this.stopWatching?.(),this.stopWatching=null,this.flushChangedFiles(),this.fileChangeTimer&&(clearTimeout(this.fileChangeTimer),this.fileChangeTimer=null),this.stopWebSocketServer?.(),this.stopWebSocketServer=null,this.runtime?.dispose(),this.runtime=null,this.closeHttpServerOnStop&&this.server.close();let e=We(),n=ze(this.options);e.get(n)===this&&e.delete(n),this.isStarted=!1}createRuntime(e,n=null){let s=this.runtime;this.activeHistoryStore=e??null;let r=(this.dependencies.createRuntime??Ae)({...this.options.tenantId?{tenantId:this.options.tenantId}:{},siteId:this.options.siteId,snapshotId:this.options.snapshotId,projectRoot:this.options.projectRoot,routes:this.routes,onEvent:i=>{"version"in i&&typeof i.version=="number"&&this.recordRuntimeVersion(i.version);for(let o of this.eventListeners)o(i)},...e?{historyStore:e}:{},initialDocumentVersion:this.runtimeVersionFloor});this.runtime=r,this.activeHistorySessionId=n,s?.dispose()}createBridgeEngine(){return{initializeSession:e=>this.mutationQueue.run(async()=>{let n=await this.bindHistorySession(this.options.snapshotId);if(!n.ok)throw new Error(`${n.code}: ${n.message}`);let{runtime:s,resolvedRouteId:r}=await this.activateRoute(e.routeId),i=s.getDocument(r).data;await this.recordRuntimeVersion(i.version);let o=(await s.getHistoryStatus()).data;return{ok:!0,data:{document:i,historyStatus:o}}}),getDocument:async e=>{let{runtime:n,resolvedRouteId:s}=await this.activateRoute(e),r=n.getDocument(s);return await this.recordRuntimeVersion(r.data.version),r},getHistoryStatus:async()=>this.getRuntime().getHistoryStatus(),getStyleContext:async e=>{let{runtime:n,resolvedRouteId:s}=await this.activateRoute(e);return n.getStyleContext(s)},getObjectEditContext:async(e,n)=>this.getRuntime().getObjectEditContext(e,n?this.resolveRouteId(n):void 0),getClassNameSource:async(e,n)=>{let s=this.getRuntime(),r=n?this.resolveRouteId(n):void 0;return s.getClassNameSource(e,r)},getSourceTree:async()=>this.getRuntime().getSourceTree(),getSourceFile:async e=>this.getRuntime().getSourceFile(e),resolveSourceLocation:async e=>this.getRuntime().resolveSourceLocation({key:e.key,routeId:e.routeId?this.resolveRouteId(e.routeId):void 0}),execute:(e,n)=>this.mutationQueue.run(async()=>{let s=await this.bindHistorySession(this.options.snapshotId);if(!s.ok)return F(s,e);let r=await this.getRuntime().executeOperation(this.normalizeRequest(e));return r.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(r.data.resultVersion),r}),undo:()=>this.mutationQueue.run(async()=>{let e=await this.bindHistorySession(this.options.snapshotId);if(!e.ok)return F(e);let n=await this.getRuntime().undo();return n.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(n.data.resultVersion),n}),redo:()=>this.mutationQueue.run(async()=>{let e=await this.bindHistorySession(this.options.snapshotId);if(!e.ok)return F(e);let n=await this.getRuntime().redo();return n.data?.resultVersion!==void 0&&await this.recordRuntimeVersion(n.data.resultVersion),n}),subscribe:e=>(this.eventListeners.add(e),()=>this.eventListeners.delete(e))}}async bindHistorySession(e){if(!this.options.historyRoot)return{ok:!1,code:"RECOVERY_PERSIST_FAILED",message:"Durable edit history is not configured."};if(this.activeHistorySessionId===e)return{ok:!0};let n=this.historySessionTransition.then(async()=>{if(this.activeHistorySessionId===e)return;let s=await(this.dependencies.openHistoryStore??De)(Je({root:this.options.historyRoot,siteId:this.options.siteId,snapshotId:this.options.snapshotId,editingSessionId:e}));if(!this.isStarted)return;let r=s.readDocumentVersion?await s.readDocumentVersion():0;this.runtimeVersionFloor=Math.max(this.runtimeVersionFloor,r),this.createRuntime(s,e)});this.historySessionTransition=n.catch(()=>{});try{return await n,{ok:!0}}catch{return{ok:!1,code:"RECOVERY_PERSIST_FAILED",message:"Durable edit history could not be opened."}}}recordRuntimeVersion(e){this.runtimeVersionFloor=Math.max(this.runtimeVersionFloor,e);let n=this.activeHistoryStore;return n?.writeDocumentVersion?(this.versionPersistence=this.versionPersistence.then(()=>n.writeDocumentVersion(this.runtimeVersionFloor)).catch(()=>{}),this.versionPersistence):Promise.resolve()}async activateRoute(e){let n=this.getRuntime(),s=this.resolveRouteId(e);return await n.setActiveRoute(s),{runtime:n,resolvedRouteId:s}}normalizeRequest(e){return e.target.kind!=="route"?e.routeId?{...e,routeId:this.resolveRouteId(e.routeId)}:e:{...e,target:{...e.target,routeId:this.resolveRouteId(e.target.routeId)}}}handleFileChanged(e){this.handleFilesChanged([e])}handleFilesChanged(e){for(let s of e)this.pendingChangedFiles.add(s);this.fileChangeTimer&&clearTimeout(this.fileChangeTimer);let n=this.options.runtime.watch_debounce_ms??ee;this.fileChangeTimer=setTimeout(()=>{this.fileChangeTimer=null,this.flushChangedFiles()},n),this.fileChangeTimer?.unref?.()}flushChangedFiles(){if(this.fileChangeTimer&&(clearTimeout(this.fileChangeTimer),this.fileChangeTimer=null),this.pendingChangedFiles.size===0)return;let e=[...this.pendingChangedFiles].sort((n,s)=>n.localeCompare(s));this.pendingChangedFiles.clear(),this.refreshRoutes();for(let n of e)this.runtime?.notifyFileChanged(n)}refreshRoutes(){let e=this.options.runtime.routes?.length?this.options.runtime.routes:Ue(this.options.projectRoot),n=new Map(this.routes.map(r=>[r.routeId,r.entryFile])),s=new Set(e.map(r=>r.routeId));this.routes=e;for(let r of e)n.get(r.routeId)!==r.entryFile&&this.runtime?.registerRoute(r.routeId,r.entryFile);for(let r of n.keys())s.has(r)||this.runtime?.unregisterRoute(r)}resolveRouteId(e){let n=Me(e,this.routes,this.options.basePath);if(!n)throw new Error(`Route is not indexed by the preview bridge: ${e}`);return n}getRuntime(){if(!this.runtime)throw new Error("VCP site edit preview bridge runtime is not ready");return this.runtime}};function $e(t){return{tenantId:t.tenantId,siteId:t.siteId,snapshotId:t.snapshotId,projectRoot:L.resolve(t.projectRoot??process.cwd()),basePath:t.basePath??"",host:t.host??Ve,port:t.port??Fe,path:t.path??Ke,sessionKey:t.sessionKey??nt(),historyRoot:t.historyRoot,allowedOrigins:t.allowedOrigins??[],runtime:t.runtime??{}}}function Je(t){return L.join(L.resolve(t.root),ne(t.siteId),ne(t.snapshotId),ie("sha256").update(t.editingSessionId).digest("hex").slice(0,32))}function ne(t){return/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(t)&&t!=="."&&t!==".."?t:ie("sha256").update(t).digest("hex").slice(0,32)}function We(){let t=globalThis;return t[te]??=new Map,t[te]}function ze(t){return`${t.projectRoot}:${t.host}:${t.port}:${t.path}:${t.historyRoot??""}`}function F(t,e){return{ok:!1,error:{code:t.code,message:t.message},data:{requestId:e?.id??"mutation-context",kind:e?.kind??"update-text",target:e?.target??{kind:"node",key:""},resultVersion:e?.documentVersion??0}}}function Ge(t){let e=new je({maxPayload:ae,noServer:!0}),n=new WeakSet,s=i=>{new P({socket:new $(i),engine:t.engine,validateOperationRequest:o=>Oe.safeParse(o).success})},r=(i,o,c)=>{if(!Qe(i,t))return;let d=Ye(i,t);if(!d){o.write(`HTTP/1.1 401 Unauthorized\r
2
2
  Connection: close\r
3
3
  \r
4
- `),o.destroy();return}d.authenticatedByHeader&&n.add(r),e.handleUpgrade(r,o,c,h=>{e.emit("connection",h,r)})};return t.server.on("upgrade",i),e.on("connection",(r,o)=>{if(n.has(o)){s(r);return}qe(r,t.sessionKey,()=>{s(r)})}),e.stop=()=>{t.server.off("upgrade",i);for(let r of e.clients)r.close(1001,"bridge stopped");e.close()},e}var F=class{constructor(e){this.socket=e,this.onmessage=null,this.onclose=null,e.on("error",()=>{}),e.on("message",n=>{this.onmessage?.({data:n.toString()})}),e.on("close",()=>{this.onclose?.()})}send(e){if(this.socket.readyState===X.OPEN){if(this.socket.bufferedAmount+Buffer.byteLength(e,"utf8")>De){this.socket.close(1013,"bridge backpressure");return}this.socket.send(e)}}close(e,n){this.socket.close(e,n)}};function Ne(t,e){return new URL(t.url??"/","http://localhost").pathname===e.path}function Ke(t,e){if(new URL(t.url??"/","http://localhost").searchParams.has("vcp_bridge_key"))return null;let s=O(t.headers["x-vcp-site-edit-key"]),i=ne(s,e.sessionKey),r=O(t.headers.host),o=O(t.headers.origin);return r&&!Le(r,e.host,o)?null:o?Je(o,r,e.allowedOrigins)?{authenticatedByHeader:i}:null:i?{authenticatedByHeader:!0}:null}function qe(t,e,n){let s=setTimeout(()=>{t.close(1008,"bridge authentication timeout")},Me),i,r=()=>{clearTimeout(s),t.off("message",i),t.off("close",r)},o=()=>{r(),t.close(1008,"bridge authentication failed")};i=c=>{try{let d=g.parseMessage(c.toString());if(d.type!=="auth"||!ne(d.payload.key,e)){o();return}r(),t.readyState===X.OPEN&&t.send(g.stringifyMessage({type:"auth-ok",payload:{ok:!0}})),n()}catch{o()}},t.on("message",i),t.on("close",r)}function ne(t,e){if(!t)return!1;let n=Buffer.from(t),s=Buffer.from(e);return n.length===s.length&&be(n,s)}function Le(t,e,n){let s=Y(t),i=Y(e);return s===i||Q(i)&&Q(s)&&(!n||N(n,t))||$e(i)&&(!n||N(n,t))}function $e(t){return t==="0.0.0.0"||t==="::"}function Q(t){return t==="localhost"||t==="127.0.0.1"||t==="::1"}function Je(t,e,n){return n.includes(t)||(e?N(t,e):!1)}function N(t,e){try{return new URL(t).host===se(e)}catch{return!1}}function Y(t){let e=se(t);return e.startsWith("[")?e.slice(1,e.indexOf("]")):e.split(":").length-1===1?e.split(":",1)[0]??"":e}function se(t){return t.trim().toLowerCase()}function O(t){return Array.isArray(t)?t[0]:t}function We(){return xe(32).toString("base64url")}var Ge="next",Qe="127.0.0.1",Ye=3e3,_="/__vcp/site-edit/bridge",de="/__vcp/site-edit/health",P="/__vcp/site-edit/client/",Ze=`${P}auto-bootstrap.js`,he='data-vcp-site-edit-bootstrap="true"',Xe=["next.config.ts","next.config.mts","next.config.cts","next.config.js","next.config.mjs","next.config.cjs"],et="@vcp-dev/site-edit-engine/webpack-loader",tt=/@vcp\/site-edit-engine\/(?:webpack-loader|site-edit-instrumentation-loader)/,nt=[/\bconfig\.webpack\s*=/,/\bwebpack\s*\([^)]*\)\s*\{/,/\bwebpack\s*:\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)?\s*=>/,/\bwebpack\s*:\s*(?:async\s+)?function\b/],st=new Set(["auto-bootstrap.js","client.js","designer-trace.js","document-codec.js","protocol.js","window-message.js"]);async function on(t={}){let e=I.resolve(t.projectRoot??process.cwd()),n=t.host??Qe,s=t.port??Mt(process.env.PORT)??Ye,i=t.bridgePath??_,r=dt(i),o=vt(),c=t.basePath??process.env.VCP_SITE_PREVIEW_BASE_PATH??"/",d=(t.tenantId??process.env.VCP_TENANT_ID)?.trim()||void 0,h=await Ht(e),f=it(e),a=h({dev:!0,dir:e,hostname:n,port:s,...rt(f)}),u=a.getRequestHandler(),p=t.siteId??process.env.VCP_SITE_ID??"site_preview",k=t.snapshotId??process.env.VCP_SNAPSHOT_ID??Dt(e),S=t.historyRoot??process.env.VCP_SITE_EDIT_HISTORY_ROOT,w=S?.trim()?I.resolve(S):void 0;await a.prepare();let T=null,R=ze.createServer((fe,C)=>{yt({basePath:c,bridgePath:i,bridgeHealthPath:r,bridgeSessionKey:o,getBridgeHealth:()=>T?.getHealth()??wt(i),handle:u,request:fe,response:C}).catch(ge=>{console.error(ge),C.headersSent||(C.statusCode=500),C.end("site edit preview request failed")})});T=te(R,{...d?{tenantId:d}:{},siteId:p,snapshotId:k,projectRoot:e,basePath:c,host:n,port:s,path:i,sessionKey:o,...w?{historyRoot:w}:{}}),ut({server:R,bridgePath:i,handleUpgrade:a.getUpgradeHandler()});let H=()=>{T?.stop(),a.close().catch(()=>{}),R.close()};return process.on("SIGINT",H),process.on("SIGTERM",H),await Ut(R,s,n),{bundler:f,close:H,server:R}}function it(t){let e=ot(t);return e&&at(e)?"webpack":"turbopack"}function rt(t){return t==="webpack"?{webpack:!0}:{turbopack:!0}}function ot(t){for(let e of Xe){let n=b(t,e);if(y(n))return m(n,"utf8")}return null}function at(t){return t.includes(et)?!0:tt.test(t)&&nt.some(e=>e.test(t))}function ut(t){let e=(n,s,i)=>{ct(n.url,t.bridgePath)||t.handleUpgrade(n,s,i).catch(r=>{console.error(r),s.destroyed||s.destroy()})};return t.server.on("upgrade",e),()=>t.server.off("upgrade",e)}function ct(t,e){return new URL(t??"/","http://localhost").pathname===e}function dt(t=_){if(t===_)return de;let e=(t.startsWith("/")?t:`/${t}`).replace(/\/+$/,"");return e.endsWith("/bridge")?`${e.slice(0,-7)}/health`:e?`${e}/health`:"/health"}function ht(t){let e=JSON.stringify({basePath:t.basePath,wsPath:t.bridgePath,bridge_session_key:t.bridgeSessionKey,defer_initial_sync:!0,runtime_versions:t.runtimeVersions??lt()});return`<script type="module" ${he}>import { startSiteEditAutoBootstrap } from ${JSON.stringify(Ze)};void startSiteEditAutoBootstrap(window, ${e}).catch(() => undefined);</script>`}function lt(){return{bridge:K("@vcp-dev/site-edit-bridge"),contracts:K("@vcp-dev/contracts"),engine:K("@vcp-dev/site-edit-engine")}}function K(t){let e=ae(import.meta.url);try{let n=e.resolve(t),s=pt(n,t);if(!s)return;let i=JSON.parse(m(s,"utf8"));return typeof i.version=="string"?i.version:void 0}catch{return}}function pt(t,e){let n=I.parse(t).root,s=x(t);for(;s&&s!==n;){let i=b(s,"package.json");if(y(i))try{if(JSON.parse(m(i,"utf8")).name===e)return i}catch{return}s=x(s)}}function ft(t,e){if(t.includes(he))return t;let n=ht(e),s=/<\/body\s*>/i.exec(t);return s?`${t.slice(0,s.index)}${n}${t.slice(s.index)}`:`${t}${n}`}function an(t,e=x(ce(import.meta.url)),n=y){return le(t,e,n)?.path??null}function le(t,e=x(ce(import.meta.url)),n=y){let s=new URL(t??"/","http://localhost").pathname;if(!s.startsWith(P))return null;let i=s.slice(P.length);if(!st.has(i))return null;let r=q(Bt(e),i);if(ue(e)!=="src")return{path:r,requiresTranspile:!1};let o=q(e,i.replace(/\.js$/,".ts"));return n(o)?{path:o,requiresTranspile:!0}:{path:r,requiresTranspile:!1}}async function gt(t,e){let n=le(t.url);if(!n)return!1;if(!y(n.path))return e.writeHead(404),e.end("site edit client asset not found"),!0;let s=n.requiresTranspile?await mt(m(n.path,"utf8")):m(n.path);return e.writeHead(200,{"cache-control":"no-store","content-type":"text/javascript; charset=utf-8"}),e.end(s),!0}async function mt(t){let e=await import("typescript");return e.transpileModule(t,{compilerOptions:{module:e.ModuleKind.ES2022,target:e.ScriptTarget.ES2022}}).outputText}async function yt({basePath:t,bridgePath:e,bridgeHealthPath:n,bridgeSessionKey:s,getBridgeHealth:i,handle:r,request:o,response:c}){if(St(o,c,n,i)||await gt(o,c))return;let d=Rt(o),h=It(o),f=d||h?kt(c,{basePath:t,bridgePath:e,bridgeSessionKey:s,patchNextDevChunk:h}):()=>{};try{await r(o,c)}catch(a){throw f(),a}}function St(t,e,n,s){if(t.method!=="GET"||new URL(t.url??"/","http://localhost").pathname!==n)return!1;let r=s();return e.writeHead(r.ok?200:503,{"cache-control":"no-store","content-type":"application/json; charset=utf-8"}),e.end(JSON.stringify(r)),!0}function wt(t){return{ok:!1,bridgePath:t,runtimeReady:!1,routesIndexed:0,sessionRequired:!0}}function Rt(t){return xt(t)?(t.headers["accept-encoding"]="identity",!0):!1}function It(t){return bt(t)?(t.headers["accept-encoding"]="identity",!0):!1}function xt(t){if(t.method!=="GET")return!1;let e=new URL(t.url??"/","http://localhost").pathname;if(e.startsWith("/_next/")||e.startsWith(P)||e===_||e===de)return!1;let n=t.headers.accept??"";return n===""||n.includes("text/html")}function bt(t){if(t.method!=="GET"||!String(t.headers["user-agent"]??"").includes("Firefox"))return!1;let n=new URL(t.url??"/","http://localhost").pathname;return n.startsWith("/_next/static/chunks/")&&n.endsWith(".js")}function kt(t,e){let n=[],s=t,i=s.flushHeaders.bind(t),r=s.write.bind(t),o=s.end.bind(t);return s.flushHeaders=()=>{},s.write=(c,d,h)=>(c!==void 0&&n.push(ie(c,re(d))),oe(d),oe(h),!0),s.end=(c,d,h)=>{let f=typeof c=="function"?void 0:c;f!==void 0&&n.push(ie(f,re(d))),s.write=r,s.end=o,s.flushHeaders=i;let a=Buffer.concat(n),u=Ct(t,a,e);u!==a&&!t.headersSent&&t.removeHeader("content-length");let p=typeof c=="function"?c:typeof d=="function"?d:typeof h=="function"?h:void 0;return p?o(u,p):o(u)},()=>{s.write=r,s.end=o,s.flushHeaders=i}}function Ct(t,e,n){if(Et(t,e))return Buffer.from(ft(e.toString("utf8"),n));if(n.patchNextDevChunk&&_t(t)){let s=e.toString("utf8"),i=Pt(s);if(i!==s)return Buffer.from(i)}return e}function vt(){let t=new Uint8Array(32);return globalThis.crypto.getRandomValues(t),Buffer.from(t).toString("base64url")}function Et(t,e){if(t.statusCode<200||t.statusCode>=300)return!1;let n=t.getHeader("content-type"),s=Array.isArray(n)?n.join(";"):String(n??"");return e.toString("utf8",0,256).includes("<html")&&(s===""||s.includes("text/html"))}function _t(t){if(t.statusCode<200||t.statusCode>=300)return!1;let e=t.getHeader("content-type"),n=Array.isArray(e)?e.join(";"):String(e??"");return n===""||n.includes("javascript")||n.includes("ecmascript")}function Pt(t){return[["debugChannel = createDebugChannel(undefined);","debugChannel = undefined;"],["if (isStreamErrorOrUnfinished(ctr)) {","if (false) {"],["return ctr.desiredSize === null || ctr.desiredSize < 0;","return false;"],[["findSourceMapURL: _appfindsourcemapurl.findSourceMapURL,"," debugChannel,"," startTime: 0"].join(`
4
+ `),o.destroy();return}d.authenticatedByHeader&&n.add(i),e.handleUpgrade(i,o,c,h=>{e.emit("connection",h,i)})};return t.server.on("upgrade",r),e.on("connection",(i,o)=>{if(n.has(o)){s(i);return}Ze(i,t.sessionKey,()=>{s(i)})}),e.stop=()=>{t.server.off("upgrade",r);for(let i of e.clients)i.close(1001,"bridge stopped");e.close()},e}var $=class{constructor(e){this.socket=e,this.onmessage=null,this.onclose=null,e.on("error",()=>{}),e.on("message",n=>{this.onmessage?.({data:n.toString()})}),e.on("close",()=>{this.onclose?.()})}send(e){if(this.socket.readyState===oe.OPEN){if(this.socket.bufferedAmount+Buffer.byteLength(e,"utf8")>Le){this.socket.close(1013,"bridge backpressure");return}this.socket.send(e)}}close(e,n){this.socket.close(e,n)}};function Qe(t,e){return new URL(t.url??"/","http://localhost").pathname===e.path}function Ye(t,e){if(new URL(t.url??"/","http://localhost").searchParams.has("vcp_bridge_key"))return null;let s=K(t.headers["x-vcp-site-edit-key"]),r=ce(s,e.sessionKey),i=K(t.headers.host),o=K(t.headers.origin);return i&&!Xe(i,e.host,o)?null:o?tt(o,i,e.allowedOrigins)?{authenticatedByHeader:r}:null:r?{authenticatedByHeader:!0}:null}function Ze(t,e,n){let s=setTimeout(()=>{t.close(1008,"bridge authentication timeout")},qe),r,i=()=>{clearTimeout(s),t.off("message",r),t.off("close",i)},o=()=>{i(),t.close(1008,"bridge authentication failed")};r=c=>{try{let d=p.parseMessage(c.toString());if(d.type!=="auth"||!ce(d.payload.key,e)){o();return}i(),t.readyState===oe.OPEN&&t.send(p.stringifyMessage({type:"auth-ok",payload:{ok:!0}})),n()}catch{o()}},t.on("message",r),t.on("close",i)}function ce(t,e){if(!t)return!1;let n=Buffer.from(t),s=Buffer.from(e);return n.length===s.length&&Be(n,s)}function Xe(t,e,n){let s=re(t),r=re(e);return s===r||se(r)&&se(s)&&(!n||J(n,t))||et(r)&&(!n||J(n,t))}function et(t){return t==="0.0.0.0"||t==="::"}function se(t){return t==="localhost"||t==="127.0.0.1"||t==="::1"}function tt(t,e,n){return n.includes(t)||(e?J(t,e):!1)}function J(t,e){try{return new URL(t).host===de(e)}catch{return!1}}function re(t){let e=de(t);return e.startsWith("[")?e.slice(1,e.indexOf("]")):e.split(":").length-1===1?e.split(":",1)[0]??"":e}function de(t){return t.trim().toLowerCase()}function K(t){return Array.isArray(t)?t[0]:t}function nt(){return He(32).toString("base64url")}var rt="next",it="127.0.0.1",ot=3e3,B="/__vcp/site-edit/bridge",ye="/__vcp/site-edit/health",j="/__vcp/site-edit/client/",at=`${j}auto-bootstrap.js`,Se='data-vcp-site-edit-bootstrap="true"',ut=["next.config.ts","next.config.mts","next.config.cts","next.config.js","next.config.mjs","next.config.cjs"],ct="@vcp-dev/site-edit-engine/webpack-loader",dt=/@vcp\/site-edit-engine\/(?:webpack-loader|site-edit-instrumentation-loader)/,ht=[/\bconfig\.webpack\s*=/,/\bwebpack\s*\([^)]*\)\s*\{/,/\bwebpack\s*:\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)?\s*=>/,/\bwebpack\s*:\s*(?:async\s+)?function\b/],lt=new Set(["auto-bootstrap.js","client.js","designer-trace.js","document-codec.js","protocol.js","window-message.js"]);async function Sn(t={},e={}){let n=e.process??process,s=e.createServer??st.createServer,r=e.attachBridgeServer??ue,i=e.loadNext??Vt,o=R.resolve(t.projectRoot??process.cwd()),c=t.host??it,d=t.port??qt(n.env.PORT)??ot,h=t.bridgePath??B,g=wt(h),a=Dt(),u=t.basePath??n.env.VCP_SITE_PREVIEW_BASE_PATH??"/",l=(t.tenantId??n.env.VCP_TENANT_ID)?.trim()||void 0,k=await i(o),I=ft(o),m=k({dev:!0,dir:o,hostname:c,port:d,...pt(I)}),Re=m.getRequestHandler(),xe=t.siteId??n.env.VCP_SITE_ID??"site_preview",be=t.snapshotId??n.env.VCP_SNAPSHOT_ID??Lt(o),G=t.historyRoot??n.env.VCP_SITE_EDIT_HISTORY_ROOT,Q=G?.trim()?R.resolve(G):void 0,v=null,y=s((M,_)=>{Ct({basePath:u,bridgePath:h,bridgeHealthPath:g,bridgeSessionKey:a,getBridgeHealth:()=>v?.getHealth()??_t(h),handle:Re,request:M,response:_}).catch(ke=>{console.error(ke),_.headersSent||(_.statusCode=500),_.end("site edit preview request failed")})}),A=null,C=null,D=()=>C||(H(()=>n.off("SIGINT",E)),H(()=>n.off("SIGTERM",E)),H(()=>A?.()),A=null,H(()=>v?.stop()),v=null,C=Promise.allSettled([Jt(y),Wt(()=>m.close())]).then(()=>{}),C),E=()=>{D()};try{return await m.prepare(),v=r(y,{...l?{tenantId:l}:{},siteId:xe,snapshotId:be,projectRoot:o,basePath:u,host:c,port:d,path:h,sessionKey:a,...Q?{historyRoot:Q}:{}}),A=yt({server:y,bridgePath:h,handleUpgrade:m.getUpgradeHandler()}),n.on("SIGINT",E),n.on("SIGTERM",E),await $t(y,d,c),{bundler:I,close:D,host:c,port:zt(y,d),server:y}}catch(M){throw await D(),M}}function ft(t){let e=gt(t);return e&&mt(e)?"webpack":"turbopack"}function pt(t){return t==="webpack"?{webpack:!0}:{turbopack:!0}}function gt(t){for(let e of ut){let n=b(t,e);if(w(n))return S(n,"utf8")}return null}function mt(t){return t.includes(ct)?!0:dt.test(t)&&ht.some(e=>e.test(t))}function yt(t){let e=(n,s,r)=>{St(n.url,t.bridgePath)||t.handleUpgrade(n,s,r).catch(i=>{console.error(i),s.destroyed||s.destroy()})};return t.server.on("upgrade",e),()=>t.server.off("upgrade",e)}function St(t,e){return new URL(t??"/","http://localhost").pathname===e}function wt(t=B){if(t===B)return ye;let e=(t.startsWith("/")?t:`/${t}`).replace(/\/+$/,"");return e.endsWith("/bridge")?`${e.slice(0,-7)}/health`:e?`${e}/health`:"/health"}function It(t){let e=JSON.stringify({basePath:t.basePath,wsPath:t.bridgePath,bridge_session_key:t.bridgeSessionKey,defer_initial_sync:!0,runtime_versions:t.runtimeVersions??Rt()});return`<script type="module" ${Se}>import { startSiteEditAutoBootstrap } from ${JSON.stringify(at)};void startSiteEditAutoBootstrap(window, ${e}).catch(() => undefined);</script>`}function Rt(){return{bridge:W("@vcp-dev/site-edit-bridge"),contracts:W("@vcp-dev/contracts"),engine:W("@vcp-dev/site-edit-engine")}}function W(t){let e=pe(import.meta.url);try{let n=e.resolve(t),s=xt(n,t);if(!s)return;let r=JSON.parse(S(s,"utf8"));return typeof r.version=="string"?r.version:void 0}catch{return}}function xt(t,e){let n=R.parse(t).root,s=x(t);for(;s&&s!==n;){let r=b(s,"package.json");if(w(r))try{if(JSON.parse(S(r,"utf8")).name===e)return r}catch{return}s=x(s)}}function bt(t,e){if(t.includes(Se))return t;let n=It(e),s=/<\/body\s*>/i.exec(t);return s?`${t.slice(0,s.index)}${n}${t.slice(s.index)}`:`${t}${n}`}function wn(t,e=x(me(import.meta.url)),n=w){return we(t,e,n)?.path??null}function we(t,e=x(me(import.meta.url)),n=w){let s=new URL(t??"/","http://localhost").pathname;if(!s.startsWith(j))return null;let r=s.slice(j.length);if(!lt.has(r))return null;let i=z(Ft(e),r);if(ge(e)!=="src")return{path:i,requiresTranspile:!1};let o=z(e,r.replace(/\.js$/,".ts"));return n(o)?{path:o,requiresTranspile:!0}:{path:i,requiresTranspile:!1}}async function kt(t,e){let n=we(t.url);if(!n)return!1;if(!w(n.path))return e.writeHead(404),e.end("site edit client asset not found"),!0;let s=n.requiresTranspile?await vt(S(n.path,"utf8")):S(n.path);return e.writeHead(200,{"cache-control":"no-store","content-type":"text/javascript; charset=utf-8"}),e.end(s),!0}async function vt(t){let e=await import("typescript");return e.transpileModule(t,{compilerOptions:{module:e.ModuleKind.ES2022,target:e.ScriptTarget.ES2022}}).outputText}async function Ct({basePath:t,bridgePath:e,bridgeHealthPath:n,bridgeSessionKey:s,getBridgeHealth:r,handle:i,request:o,response:c}){if(Et(o,c,n,r)||await kt(o,c))return;let d=Pt(o),h=Tt(o),g=d||h?jt(c,{basePath:t,bridgePath:e,bridgeSessionKey:s,patchNextDevChunk:h}):()=>{};try{await i(o,c)}catch(a){throw g(),a}}function Et(t,e,n,s){if(t.method!=="GET"||new URL(t.url??"/","http://localhost").pathname!==n)return!1;let i=s();return e.writeHead(i.ok?200:503,{"cache-control":"no-store","content-type":"application/json; charset=utf-8"}),e.end(JSON.stringify(i)),!0}function _t(t){return{ok:!1,bridgePath:t,runtimeReady:!1,routesIndexed:0,sessionRequired:!0}}function Pt(t){return Ht(t)?(t.headers["accept-encoding"]="identity",!0):!1}function Tt(t){return Bt(t)?(t.headers["accept-encoding"]="identity",!0):!1}function Ht(t){if(t.method!=="GET")return!1;let e=new URL(t.url??"/","http://localhost").pathname;if(e.startsWith("/_next/")||e.startsWith(j)||e===B||e===ye)return!1;let n=t.headers.accept??"";return n===""||n.includes("text/html")}function Bt(t){if(t.method!=="GET"||!String(t.headers["user-agent"]??"").includes("Firefox"))return!1;let n=new URL(t.url??"/","http://localhost").pathname;return n.startsWith("/_next/static/chunks/")&&n.endsWith(".js")}function jt(t,e){let n=[],s=t,r=s.flushHeaders.bind(t),i=s.write.bind(t),o=s.end.bind(t);return s.flushHeaders=()=>{},s.write=(c,d,h)=>(c!==void 0&&n.push(he(c,le(d))),fe(d),fe(h),!0),s.end=(c,d,h)=>{let g=typeof c=="function"?void 0:c;g!==void 0&&n.push(he(g,le(d))),s.write=i,s.end=o,s.flushHeaders=r;let a=Buffer.concat(n),u=At(t,a,e);u!==a&&!t.headersSent&&t.removeHeader("content-length");let l=typeof c=="function"?c:typeof d=="function"?d:typeof h=="function"?h:void 0;return l?o(u,l):o(u)},()=>{s.write=i,s.end=o,s.flushHeaders=r}}function At(t,e,n){if(Mt(t,e))return Buffer.from(bt(e.toString("utf8"),n));if(n.patchNextDevChunk&&Ut(t)){let s=e.toString("utf8"),r=Nt(s);if(r!==s)return Buffer.from(r)}return e}function Dt(){let t=new Uint8Array(32);return globalThis.crypto.getRandomValues(t),Buffer.from(t).toString("base64url")}function Mt(t,e){if(t.statusCode<200||t.statusCode>=300)return!1;let n=t.getHeader("content-type"),s=Array.isArray(n)?n.join(";"):String(n??"");return e.toString("utf8",0,256).includes("<html")&&(s===""||s.includes("text/html"))}function Ut(t){if(t.statusCode<200||t.statusCode>=300)return!1;let e=t.getHeader("content-type"),n=Array.isArray(e)?e.join(";"):String(e??"");return n===""||n.includes("javascript")||n.includes("ecmascript")}function Nt(t){return[["debugChannel = createDebugChannel(undefined);","debugChannel = undefined;"],["if (isStreamErrorOrUnfinished(ctr)) {","if (false) {"],["return ctr.desiredSize === null || ctr.desiredSize < 0;","return false;"],[["findSourceMapURL: _appfindsourcemapurl.findSourceMapURL,"," debugChannel,"," startTime: 0"].join(`
5
5
  `),["unstable_allowPartialStream: true,"," debugChannel,"," startTime: 0"].join(`
6
- `)],[["findSourceMapURL: _appfindsourcemapurl.findSourceMapURL,"," debugChannel,"," startTime: 0"].join("\\n"),["unstable_allowPartialStream: true,"," debugChannel,"," startTime: 0"].join("\\n")],["debugChannel: createDebugChannel && createDebugChannel(requestHeaders)","debugChannel: undefined"],["debugChannel: createDebugChannel && createDebugChannel(headers)","debugChannel: undefined"]].reduce((n,[s,i])=>Tt(n,s,i),t)}function Tt(t,e,n){if(n.length>e.length)throw new Error("Replacement must not be longer than the search string.");return t.replaceAll(e,n.padEnd(e.length," "))}function ie(t,e){return Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t):Buffer.from(String(t),e)}function re(t){return typeof t=="string"?t:void 0}function oe(t){typeof t=="function"&&t()}async function Ht(t){let n=ae(b(t,"package.json"))(Ge);return n.default??n}function Bt(t){return ue(t)!=="src"?t:q(jt(t),"dist")}function jt(t){return pe(t)?I.posix.dirname(t):x(t)}function q(t,e){return pe(t)?I.posix.join(t,e):b(t,e)}function pe(t){return t.includes("/")&&!t.includes("\\")}function Dt(t){let e=b(t,".vcp/current-snapshot.json");if(!y(e))return"sandbox_worktree";try{let n=JSON.parse(m(e,"utf8"));return typeof n.snapshotId=="string"?n.snapshotId:"sandbox_worktree"}catch{return"sandbox_worktree"}}function Mt(t){if(!t)return null;let e=Number.parseInt(t,10);return Number.isInteger(e)&&e>0&&e<=65535?e:null}function Ut(t,e,n){return new Promise((s,i)=>{t.once("error",i),t.listen(e,n,()=>{t.off("error",i),s()})})}export{ut as attachNextDevUpgradeHandler,ht as createSiteEditAutoBootstrapHtmlSnippet,ft as injectSiteEditAutoBootstrap,Pt as patchNextFirefoxDevChunk,It as prepareFirefoxNextDevChunkPatchRequest,Rt as prepareSiteEditHtmlInjectionRequest,dt as resolveSiteEditBridgeHealthPath,le as resolveSiteEditClientAsset,an as resolveSiteEditClientAssetPath,it as resolveSiteEditNextDevBundler,on as startSiteEditNextDevServer,mt as transpileSiteEditClientSource};
6
+ `)],[["findSourceMapURL: _appfindsourcemapurl.findSourceMapURL,"," debugChannel,"," startTime: 0"].join("\\n"),["unstable_allowPartialStream: true,"," debugChannel,"," startTime: 0"].join("\\n")],["debugChannel: createDebugChannel && createDebugChannel(requestHeaders)","debugChannel: undefined"],["debugChannel: createDebugChannel && createDebugChannel(headers)","debugChannel: undefined"]].reduce((n,[s,r])=>Ot(n,s,r),t)}function Ot(t,e,n){if(n.length>e.length)throw new Error("Replacement must not be longer than the search string.");return t.replaceAll(e,n.padEnd(e.length," "))}function he(t,e){return Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t):Buffer.from(String(t),e)}function le(t){return typeof t=="string"?t:void 0}function fe(t){typeof t=="function"&&t()}async function Vt(t){let n=pe(b(t,"package.json"))(rt);return n.default??n}function Ft(t){return ge(t)!=="src"?t:z(Kt(t),"dist")}function Kt(t){return Ie(t)?R.posix.dirname(t):x(t)}function z(t,e){return Ie(t)?R.posix.join(t,e):b(t,e)}function Ie(t){return t.includes("/")&&!t.includes("\\")}function Lt(t){let e=b(t,".vcp/current-snapshot.json");if(!w(e))return"sandbox_worktree";try{let n=JSON.parse(S(e,"utf8"));return typeof n.snapshotId=="string"?n.snapshotId:"sandbox_worktree"}catch{return"sandbox_worktree"}}function qt(t){if(!t)return null;let e=Number.parseInt(t,10);return Number.isInteger(e)&&e>0&&e<=65535?e:null}function $t(t,e,n){return new Promise((s,r)=>{t.once("error",r),t.listen(e,n,()=>{t.off("error",r),s()})})}function Jt(t){return new Promise(e=>{t.close(()=>e())})}function H(t){try{t()}catch{}}function Wt(t){try{return t()}catch(e){return Promise.reject(e)}}function zt(t,e){let n=t.address();return typeof n=="object"&&n?n.port:e}export{yt as attachNextDevUpgradeHandler,It as createSiteEditAutoBootstrapHtmlSnippet,bt as injectSiteEditAutoBootstrap,Nt as patchNextFirefoxDevChunk,Tt as prepareFirefoxNextDevChunkPatchRequest,Pt as prepareSiteEditHtmlInjectionRequest,wt as resolveSiteEditBridgeHealthPath,we as resolveSiteEditClientAsset,wn as resolveSiteEditClientAssetPath,ft as resolveSiteEditNextDevBundler,Sn as startSiteEditNextDevServer,vt as transpileSiteEditClientSource};
@@ -0,0 +1,419 @@
1
+ import { encodeRenderDocument, } from "./document-codec.js";
2
+ export class SiteEditBridgeTransport {
3
+ constructor() { }
4
+ static parseMessage(raw, options = {}) {
5
+ const parsed = JSON.parse(raw);
6
+ if (!SiteEditBridgeTransport.isMessage(parsed, options)) {
7
+ throw new Error("Invalid site edit bridge message");
8
+ }
9
+ return parsed;
10
+ }
11
+ static stringifyMessage(message) {
12
+ return JSON.stringify(message);
13
+ }
14
+ static extractRequestInfo(message) {
15
+ if (typeof message === "object" &&
16
+ message !== null &&
17
+ "payload" in message &&
18
+ typeof message.payload === "object" &&
19
+ message.payload !== null) {
20
+ const payload = message.payload;
21
+ const id = typeof payload.id === "string" ? payload.id : "unknown";
22
+ const kind = SiteEditBridgeTransport.isRequestKind(payload.kind)
23
+ ? payload.kind
24
+ : "get-document";
25
+ return { id, kind };
26
+ }
27
+ return { id: "unknown", kind: "get-document" };
28
+ }
29
+ static isRequestKind(value) {
30
+ return (value === "initialize-session" ||
31
+ value === "get-document" ||
32
+ value === "get-history-status" ||
33
+ value === "get-style-context" ||
34
+ value === "get-object-edit-context" ||
35
+ value === "get-class-name-source" ||
36
+ value === "get-source-tree" ||
37
+ value === "get-source-file" ||
38
+ value === "resolve-source-location" ||
39
+ value === "execute" ||
40
+ value === "undo" ||
41
+ value === "redo");
42
+ }
43
+ static isMessage(value, options = {}) {
44
+ if (!isRecord(value) || typeof value.type !== "string") {
45
+ return false;
46
+ }
47
+ if (value.type === "request") {
48
+ return SiteEditBridgeTransport.isRequestPayload(value.payload, options);
49
+ }
50
+ if (value.type === "response" || value.type === "error") {
51
+ return isRecord(value.payload) && typeof value.payload.id === "string";
52
+ }
53
+ if (value.type === "event") {
54
+ return isRecord(value.payload) && isRecord(value.payload.event);
55
+ }
56
+ if (value.type === "auth") {
57
+ return isRecord(value.payload) && typeof value.payload.key === "string";
58
+ }
59
+ if (value.type === "auth-ok") {
60
+ return isRecord(value.payload) && value.payload.ok === true;
61
+ }
62
+ return false;
63
+ }
64
+ static isRequestPayload(payload, options = {}) {
65
+ if (!isRecord(payload) ||
66
+ typeof payload.id !== "string" ||
67
+ !SiteEditBridgeTransport.isRequestKind(payload.kind)) {
68
+ return false;
69
+ }
70
+ switch (payload.kind) {
71
+ case "initialize-session":
72
+ return (typeof payload.routeId === "string" &&
73
+ (payload.mutationContext === undefined ||
74
+ isValidMutationContext(payload.mutationContext)));
75
+ case "get-document":
76
+ case "get-style-context":
77
+ return typeof payload.routeId === "string";
78
+ case "get-object-edit-context":
79
+ case "get-class-name-source":
80
+ case "resolve-source-location":
81
+ return (typeof payload.key === "string" &&
82
+ (payload.routeId === undefined || typeof payload.routeId === "string"));
83
+ case "get-history-status":
84
+ return payload.mutationContext === undefined;
85
+ case "get-source-tree":
86
+ return true;
87
+ case "get-source-file":
88
+ return typeof payload.path === "string";
89
+ case "execute":
90
+ return (isOperationRequestLike(payload.request) &&
91
+ (payload.request.mutationContext === undefined ||
92
+ isValidMutationContext(payload.request.mutationContext)) &&
93
+ (options.validateOperationRequest?.(payload.request) ?? true));
94
+ case "undo":
95
+ case "redo":
96
+ return (payload.mutationContext === undefined ||
97
+ isValidMutationContext(payload.mutationContext));
98
+ }
99
+ }
100
+ }
101
+ export class SiteEditBridgeSocketServer {
102
+ constructor(options) {
103
+ this.options = options;
104
+ this.messageQueue = new SerializedSiteEditMessageQueue();
105
+ this.eventDispatcher = new BufferedSiteEditEventDispatcher((message) => {
106
+ this.sendJson(message);
107
+ });
108
+ this.unsubscribe = options.engine.subscribe((event) => {
109
+ this.eventDispatcher.push(event);
110
+ });
111
+ options.socket.onmessage = (event) => {
112
+ try {
113
+ void this.messageQueue.enqueue(() => this.handleRawMessage(event.data));
114
+ }
115
+ catch (error) {
116
+ this.sendJson({
117
+ type: "error",
118
+ payload: {
119
+ id: "unknown",
120
+ kind: "get-document",
121
+ message: error instanceof Error ? error.message : "Bridge queue overflow",
122
+ },
123
+ });
124
+ options.socket.close(1013, "bridge busy");
125
+ }
126
+ };
127
+ options.socket.onclose = () => {
128
+ this.unsubscribe();
129
+ };
130
+ }
131
+ async handleMessage(message) {
132
+ try {
133
+ const parsed = message;
134
+ if (parsed.type !== "request") {
135
+ return;
136
+ }
137
+ const payload = parsed.payload;
138
+ const { id } = payload;
139
+ switch (payload.kind) {
140
+ case "initialize-session": {
141
+ const result = await this.options.engine.initializeSession({
142
+ routeId: payload.routeId,
143
+ ...(payload.mutationContext
144
+ ? { mutationContext: payload.mutationContext }
145
+ : {}),
146
+ });
147
+ this.sendJson({
148
+ type: "response",
149
+ payload: {
150
+ id,
151
+ kind: payload.kind,
152
+ result: {
153
+ ok: true,
154
+ data: {
155
+ document: encodeRenderDocument(result.data.document),
156
+ historyStatus: result.data.historyStatus,
157
+ },
158
+ },
159
+ },
160
+ });
161
+ return;
162
+ }
163
+ case "get-document":
164
+ this.sendJson({
165
+ type: "response",
166
+ payload: {
167
+ id,
168
+ kind: payload.kind,
169
+ result: {
170
+ ok: true,
171
+ data: encodeRenderDocument((await this.options.engine.getDocument(payload.routeId)).data),
172
+ },
173
+ },
174
+ });
175
+ return;
176
+ case "get-history-status":
177
+ this.sendJson({
178
+ type: "response",
179
+ payload: {
180
+ id,
181
+ kind: payload.kind,
182
+ result: await this.options.engine.getHistoryStatus(),
183
+ },
184
+ });
185
+ return;
186
+ case "get-style-context":
187
+ this.sendJson({
188
+ type: "response",
189
+ payload: {
190
+ id,
191
+ kind: payload.kind,
192
+ result: await this.options.engine.getStyleContext(payload.routeId),
193
+ },
194
+ });
195
+ return;
196
+ case "get-object-edit-context":
197
+ this.sendJson({
198
+ type: "response",
199
+ payload: {
200
+ id,
201
+ kind: payload.kind,
202
+ result: await this.options.engine.getObjectEditContext(payload.key, payload.routeId),
203
+ },
204
+ });
205
+ return;
206
+ case "get-class-name-source":
207
+ this.sendJson({
208
+ type: "response",
209
+ payload: {
210
+ id,
211
+ kind: payload.kind,
212
+ result: await this.options.engine.getClassNameSource(payload.key, payload.routeId),
213
+ },
214
+ });
215
+ return;
216
+ case "get-source-tree":
217
+ this.sendJson({
218
+ type: "response",
219
+ payload: {
220
+ id,
221
+ kind: payload.kind,
222
+ result: await this.options.engine.getSourceTree(),
223
+ },
224
+ });
225
+ return;
226
+ case "get-source-file":
227
+ this.sendJson({
228
+ type: "response",
229
+ payload: {
230
+ id,
231
+ kind: payload.kind,
232
+ result: await this.options.engine.getSourceFile({
233
+ path: payload.path,
234
+ }),
235
+ },
236
+ });
237
+ return;
238
+ case "resolve-source-location":
239
+ this.sendJson({
240
+ type: "response",
241
+ payload: {
242
+ id,
243
+ kind: payload.kind,
244
+ result: await this.options.engine.resolveSourceLocation({
245
+ key: payload.key,
246
+ routeId: payload.routeId,
247
+ }),
248
+ },
249
+ });
250
+ return;
251
+ case "execute": {
252
+ await this.handleBufferedOperation(id, payload.kind, () => this.options.engine.execute(payload.request));
253
+ return;
254
+ }
255
+ case "undo":
256
+ await this.handleBufferedOperation(id, payload.kind, () => this.options.engine.undo());
257
+ return;
258
+ case "redo":
259
+ await this.handleBufferedOperation(id, payload.kind, () => this.options.engine.redo());
260
+ return;
261
+ }
262
+ }
263
+ catch (error) {
264
+ const requestInfo = SiteEditBridgeTransport.extractRequestInfo(message);
265
+ this.sendJson({
266
+ type: "error",
267
+ payload: {
268
+ ...requestInfo,
269
+ message: getBridgeErrorMessage(error),
270
+ code: getBridgeErrorCode(error),
271
+ },
272
+ });
273
+ }
274
+ }
275
+ async handleRawMessage(raw) {
276
+ try {
277
+ await this.handleMessage(SiteEditBridgeTransport.parseMessage(raw, {
278
+ validateOperationRequest: this.options.validateOperationRequest,
279
+ }));
280
+ }
281
+ catch (error) {
282
+ const message = error instanceof Error ? error.message : "Invalid websocket message";
283
+ const requestInfo = extractRawRequestInfo(raw);
284
+ this.sendJson({
285
+ type: "error",
286
+ payload: { ...requestInfo, message },
287
+ });
288
+ }
289
+ }
290
+ sendJson(message) {
291
+ this.options.socket.send(SiteEditBridgeTransport.stringifyMessage(message));
292
+ }
293
+ async handleBufferedOperation(id, kind, operation) {
294
+ this.eventDispatcher.beginBuffering();
295
+ try {
296
+ const result = await operation();
297
+ this.sendJson({ type: "response", payload: { id, kind, result } });
298
+ this.eventDispatcher.flush();
299
+ }
300
+ catch (error) {
301
+ this.sendJson({
302
+ type: "error",
303
+ payload: {
304
+ id,
305
+ kind,
306
+ message: getBridgeErrorMessage(error),
307
+ code: getBridgeErrorCode(error),
308
+ },
309
+ });
310
+ this.eventDispatcher.flush();
311
+ }
312
+ }
313
+ }
314
+ class BufferedSiteEditEventDispatcher {
315
+ constructor(send) {
316
+ this.send = send;
317
+ this.bufferedEvents = null;
318
+ }
319
+ push(event) {
320
+ const message = {
321
+ type: "event",
322
+ payload: { event },
323
+ };
324
+ if (this.bufferedEvents) {
325
+ this.bufferedEvents.push(message);
326
+ return;
327
+ }
328
+ this.send(message);
329
+ }
330
+ beginBuffering() {
331
+ this.bufferedEvents = [];
332
+ }
333
+ flush() {
334
+ const buffered = this.bufferedEvents ?? [];
335
+ this.bufferedEvents = null;
336
+ buffered.forEach((message) => {
337
+ this.send(message);
338
+ });
339
+ }
340
+ }
341
+ class SerializedSiteEditMessageQueue {
342
+ constructor(maxQueued = 64) {
343
+ this.maxQueued = maxQueued;
344
+ this.queue = Promise.resolve();
345
+ this.queued = 0;
346
+ }
347
+ enqueue(task) {
348
+ if (this.queued >= this.maxQueued) {
349
+ throw new Error("Bridge request queue is full");
350
+ }
351
+ this.queued += 1;
352
+ const run = async () => {
353
+ this.queued -= 1;
354
+ await task();
355
+ };
356
+ const next = this.queue.then(run, run);
357
+ this.queue = next.then(() => undefined, () => undefined);
358
+ return next;
359
+ }
360
+ }
361
+ function isRecord(value) {
362
+ return typeof value === "object" && value !== null;
363
+ }
364
+ function isOperationRequestLike(value) {
365
+ return (isRecord(value) &&
366
+ typeof value.id === "string" &&
367
+ typeof value.siteId === "string" &&
368
+ typeof value.baseSnapshotId === "string" &&
369
+ (value.routeId === undefined || typeof value.routeId === "string") &&
370
+ typeof value.kind === "string" &&
371
+ isRecord(value.target) &&
372
+ typeof value.documentVersion === "number" &&
373
+ Number.isInteger(value.documentVersion) &&
374
+ value.documentVersion >= 0 &&
375
+ isRecord(value.params));
376
+ }
377
+ function isValidMutationContext(value) {
378
+ return isMutationContextLike(value);
379
+ }
380
+ function getBridgeErrorCode(error) {
381
+ if (!error || typeof error !== "object") {
382
+ return undefined;
383
+ }
384
+ const code = error.code;
385
+ return typeof code === "string" && code.length > 0 ? code : undefined;
386
+ }
387
+ function getBridgeErrorMessage(error) {
388
+ if (error instanceof Error) {
389
+ return error.message;
390
+ }
391
+ if (!error || typeof error !== "object") {
392
+ return "Unknown websocket operation failure";
393
+ }
394
+ const message = error.message;
395
+ return typeof message === "string" && message.length > 0
396
+ ? message
397
+ : "Unknown websocket operation failure";
398
+ }
399
+ function isMutationContextLike(value) {
400
+ if (!isRecord(value)) {
401
+ return false;
402
+ }
403
+ return (typeof value.editingSessionId === "string" &&
404
+ value.editingSessionId.length > 0 &&
405
+ typeof value.lockId === "string" &&
406
+ value.lockId.length > 0 &&
407
+ typeof value.ownerId === "string" &&
408
+ value.ownerId.length > 0 &&
409
+ typeof value.leaseAssertion === "string" &&
410
+ value.leaseAssertion.length > 0);
411
+ }
412
+ function extractRawRequestInfo(raw) {
413
+ try {
414
+ return SiteEditBridgeTransport.extractRequestInfo(JSON.parse(raw));
415
+ }
416
+ catch {
417
+ return { id: "unknown", kind: "get-document" };
418
+ }
419
+ }