11
2+ console . log ( "DEBUG: objectstack.config.ts LOADED" ) ;
23import { createRequire } from 'module' ;
34const require = createRequire ( import . meta. url ) ;
45// @ts -ignore
@@ -16,236 +17,97 @@ import * as HonoServerPluginPkg from '@objectstack/plugin-hono-server';
1617
1718const MSWPlugin = MSWPluginPkg . MSWPlugin || ( MSWPluginPkg as any ) . default ?. MSWPlugin || ( MSWPluginPkg as any ) . default ;
1819const ObjectQLPlugin = ObjectQLPluginPkg . ObjectQLPlugin || ( ObjectQLPluginPkg as any ) . default ?. ObjectQLPlugin || ( ObjectQLPluginPkg as any ) . default ;
19- const HonoServerPlugin = HonoServerPluginPkg . HonoServerPlugin || ( HonoServerPluginPkg as any ) . default ?. HonoServerPlugin || ( HonoServerPluginPkg as any ) . default ;
20+ // const HonoServerPlugin = HonoServerPluginPkg.HonoServerPlugin || (HonoServerPluginPkg as any).default?.HonoServerPlugin || (HonoServerPluginPkg as any).default;
2021
2122import ConsolePluginConfig from './plugin.js' ;
2223
23- // FIX: Ensure init is own property for runtime compatibility
24- class PatchedMSWPlugin extends MSWPlugin {
25- constructor ( ...args : any [ ] ) {
26- super ( ...args ) ;
27- // @ts -ignore
28- this . init = this . init . bind ( this ) ;
29- // @ts -ignore
30- this . start = this . start ?. bind ( this ) ;
31- }
32- }
24+ const FixedConsolePlugin = {
25+ ...ConsolePluginConfig ,
26+ init : ( ) => { } ,
27+ start : async ( ctx : any ) => {
28+ console . log ( 'DEBUG: FixedConsolePlugin start called' ) ;
3329
34- class PatchedHonoServerPlugin extends HonoServerPlugin {
35- constructor ( ...args : any [ ] ) {
36- super ( ...args ) ;
37- console . log ( 'DEBUG: PatchedHonoServerPlugin instantiated' ) ;
38- // @ts -ignore
39- this . init = this . init . bind ( this ) ;
30+ let app = null ;
31+ const staticRoot = './dist' ;
4032
41- // Capture original start method (which is an arrow function property)
42- // @ts -ignore
43- const originalStart = this . start ;
44-
45- // Override start with custom logic
46- // @ts -ignore
47- this . start = async ( ctx : any ) => {
48- // Call original start first to ensure server is created and listening
49- if ( originalStart ) {
50- await originalStart ( ctx ) ;
51- }
52-
53- console . log ( 'DEBUG: Attempting to register SPA routes...' ) ;
54- console . log ( 'DEBUG: Plugin keys:' , Object . keys ( this ) ) ;
55- // @ts -ignore
56- if ( this . app ) console . log ( 'DEBUG: Found this.app' ) ;
57- // @ts -ignore
58- if ( this . server ) console . log ( 'DEBUG: Found this.server' ) ;
59-
60- // Try to find the app from various sources
61- // @ts -ignore
62- let app = this . server ?. getRawApp ?.( ) ; // If this.server exists
63-
64- // If not found, try to get from service
65- if ( ! app && ctx . getService ) {
33+ // Wait for app to be available (in case HonoServerPlugin initializes it in start)
34+ const maxRetries = 50 ;
35+ for ( let i = 0 ; i < maxRetries ; i ++ ) { // 10 seconds timeout
36+ if ( ctx . getService ) {
6637 const httpService = ctx . getService ( 'http-server' ) || ctx . getService ( 'http.server' ) ;
6738 if ( httpService ) {
68- // console.log('DEBUG: Found http-server service keys:', Object.keys(httpService));
69- if ( httpService . getRawApp ) app = httpService . getRawApp ( ) ;
70- else if ( httpService . app ) app = httpService . app ;
39+ if ( httpService . getRawApp ) app = httpService . getRawApp ( ) ;
40+ else if ( httpService . app ) app = httpService . app ;
7141 }
7242 }
43+ if ( app ) break ;
44+ await new Promise ( r => setTimeout ( r , 200 ) ) ;
45+ }
7346
74- // @ts -ignore
75- const staticRoot = this . options ?. staticRoot || './dist' ;
76-
77- if ( app && staticRoot ) {
78- const fs = require ( 'fs' ) ;
79- const path = require ( 'path' ) ;
80-
81- console . log ( 'DEBUG: Registering SPA routes for ' + staticRoot ) ;
82-
83- // Manual Asset Handler to ensure assets are served
84- app . get ( '/console/assets/*' , async ( c : any ) => {
85- const filePath = path . join ( path . resolve ( staticRoot ) , c . req . path . replace ( '/console' , '' ) ) ;
86- // console.log('DEBUG: Asset Request:', c.req.path, '->', filePath);
87- if ( fs . existsSync ( filePath ) ) {
88- const ext = path . extname ( filePath ) ;
89- let contentType = 'application/octet-stream' ;
90- if ( ext === '.css' ) contentType = 'text/css' ;
91- else if ( ext === '.js' ) contentType = 'application/javascript' ;
92- else if ( ext === '.json' ) contentType = 'application/json' ;
93- else if ( ext === '.png' ) contentType = 'image/png' ;
94- else if ( ext === '.svg' ) contentType = 'image/svg+xml' ;
95-
96- const content = fs . readFileSync ( filePath ) ;
97- return c . body ( content , 200 , { 'Content-Type' : contentType } ) ;
98- }
99- return c . text ( 'Asset Not Found' , 404 ) ;
100- } ) ;
101-
102- // Server-side Redirect: Root -> Console
103- app . get ( '/' , ( c : any ) => {
104- return c . redirect ( '/console/' ) ;
105- } ) ;
106-
107- // Register fallback
108- app . get ( '/console/*' , async ( c : any ) => {
109- // Ignore API calls -> let them 404
110- if ( c . req . path . startsWith ( '/api' ) ) {
111- return c . text ( 'Not Found' , 404 ) ;
112- }
47+ const path = require ( 'path' ) ;
48+ const fs = require ( 'fs' ) ;
49+ const absoluteStaticRoot = path . resolve ( process . cwd ( ) , staticRoot ) ;
50+
51+ if ( app ) {
52+ console . log ( 'DEBUG: Registering SPA routes for ' + absoluteStaticRoot ) ;
53+
54+ // Manual Asset Handler
55+ app . get ( '/console/assets/*' , async ( c : any ) => {
56+ const filePath = path . join ( absoluteStaticRoot , c . req . path . replace ( '/console' , '' ) ) ;
57+ if ( fs . existsSync ( filePath ) ) {
58+ const ext = path . extname ( filePath ) ;
59+ let contentType = 'application/octet-stream' ;
60+ if ( ext === '.css' ) contentType = 'text/css' ;
61+ else if ( ext === '.js' ) contentType = 'application/javascript' ;
62+ else if ( ext === '.json' ) contentType = 'application/json' ;
63+ else if ( ext === '.png' ) contentType = 'image/png' ;
64+ else if ( ext === '.svg' ) contentType = 'image/svg+xml' ;
11365
114- try {
115- // Debugging path resolution
116- const indexPath = path . resolve ( staticRoot , 'index.html' ) ;
117- console . log ( 'DEBUG: Request Path:' , c . req . path ) ;
118- console . log ( 'DEBUG: Serving index from:' , indexPath ) ;
119- console . log ( 'DEBUG: File exists?' , fs . existsSync ( indexPath ) ) ;
120-
121- if ( fs . existsSync ( indexPath ) ) {
122- const indexContent = fs . readFileSync ( indexPath , 'utf-8' ) ;
123- return c . html ( indexContent ) ;
124- }
125- return c . text ( 'SPA Index Not Found at ' + indexPath , 404 ) ;
126- } catch ( e : any ) {
127- console . error ( 'DEBUG: Error serving index:' , e ) ;
128- return c . text ( 'Server Error: ' + e . message , 500 ) ;
129- }
130- } ) ;
131- console . log ( 'SPA Fallback route registered for ' + staticRoot ) ;
132- } else {
133- console . log ( "DEBUG: failed to register routes. app found:" , ! ! app , "staticRoot:" , staticRoot ) ;
134- }
135- } ;
136- }
137- }
138-
139- const FixedConsolePlugin = {
140- ...ConsolePluginConfig ,
141- init : ( ) => { } ,
142- start : async ( ctx : any ) => {
143- const httpService = ctx . getService ( 'http-server' ) || ctx . getService ( 'http.server' ) ;
144- if ( httpService ) {
145- const app = httpService . app || httpService . getRawApp ?.( ) ;
146-
147- if ( app ) {
148- const fs = require ( 'fs' ) ;
149- const path = require ( 'path' ) ;
150- const staticRoot = './dist' ;
151-
152- // Manual Asset Handler
153- app . get ( '/console/assets/*' , async ( c : any ) => {
154- const filePath = path . join ( path . resolve ( staticRoot ) , c . req . path . replace ( '/console' , '' ) ) ;
155- if ( fs . existsSync ( filePath ) ) {
156- const ext = path . extname ( filePath ) ;
157- let contentType = 'application/octet-stream' ;
158- if ( ext === '.css' ) contentType = 'text/css' ;
159- else if ( ext === '.js' ) contentType = 'application/javascript' ;
160- else if ( ext === '.json' ) contentType = 'application/json' ;
161- else if ( ext === '.png' ) contentType = 'image/png' ;
162- else if ( ext === '.svg' ) contentType = 'image/svg+xml' ;
163-
164- const content = fs . readFileSync ( filePath ) ;
165- return c . body ( content , 200 , { 'Content-Type' : contentType } ) ;
66+ const content = fs . readFileSync ( filePath ) ;
67+ return c . body ( content , 200 , { 'Content-Type' : contentType } ) ;
68+ }
69+ return c . text ( 'Asset Not Found' , 404 ) ;
70+ } ) ;
71+
72+ // Redirect Root
73+ app . get ( '/' , ( c : any ) => {
74+ return c . redirect ( '/console/' ) ;
75+ } ) ;
76+
77+ // SPA Fallback
78+ app . get ( '/console/*' , async ( c : any ) => {
79+ if ( c . req . path . startsWith ( '/api' ) ) {
80+ return c . text ( 'Not Found' , 404 ) ;
81+ }
82+ try {
83+ const indexPath = path . join ( absoluteStaticRoot , 'index.html' ) ;
84+ if ( fs . existsSync ( indexPath ) ) {
85+ const indexContent = fs . readFileSync ( indexPath , 'utf-8' ) ;
86+ return c . html ( indexContent ) ;
16687 }
167- return c . text ( 'Asset Not Found' , 404 ) ;
168- } ) ;
169-
170- // Server-side Redirect
171- app . get ( '/' , ( c : any ) => c . redirect ( '/console/' ) ) ;
172- app . get ( '/console' , ( c : any ) => c . redirect ( '/console/' ) ) ;
173-
174- // SPA Fallback
175- app . get ( '/console/*' , async ( c : any ) => {
176- if ( c . req . path . startsWith ( '/api' ) ) return c . text ( 'Not Found' , 404 ) ;
177- const indexPath = path . resolve ( staticRoot , 'index.html' ) ;
178- if ( fs . existsSync ( indexPath ) ) {
179- return c . html ( fs . readFileSync ( indexPath , 'utf-8' ) ) ;
180- }
181- return c . text ( 'SPA Index Not Found' , 404 ) ;
182- } ) ;
183- console . log ( 'SPA routes registered for Console' ) ;
184- }
88+ return c . text ( 'SPA Index Not Found at ' + indexPath , 404 ) ;
89+ } catch ( e : any ) {
90+ console . error ( 'DEBUG: Error serving index:' , e ) ;
91+ return c . text ( 'Server Error: ' + e . message , 500 ) ;
92+ }
93+ } ) ;
94+ console . log ( 'SPA Fallback route registered' ) ;
95+ } else {
96+ console . log ( "DEBUG: Failed to find Hono app service. Routes not registered." ) ;
18597 }
18698 }
18799} ;
188100
189- // Workaround: Override the built-in api-registry plugin which fails due to async service issue
190- const DummyApiRegistryPlugin = {
191- name : 'com.objectstack.runtime.api-registry' ,
192- version : '1.0.0' ,
193- init : ( ctx : any ) => {
194- // Polyfill missing critical services to pass the Runtime health check
195-
196- // ctx.registerService('metadata', {
197- // getApp: () => null,
198- // getObject: () => null,
199- // getObjects: () => []
200- // });
201-
202- // ctx.registerService('data', {
203- // find: async () => [],
204- // findOne: async () => null,
205- // insert: async () => {},
206- // update: async () => {},
207- // delete: async () => {},
208- // count: async () => 0
209- // });
210-
211- // ctx.registerService('auth', {
212- // validate: async () => true,
213- // getSession: async () => ({ userId: 'mock-user', username: 'mock' })
214- // });
215-
216- // Mock API Registry Service
217- const apiEndpoints : any [ ] = [ ] ;
218- ctx . registerService ( 'api-registry' , {
219- registerApi : ( entry : any ) => {
220- apiEndpoints . push ( entry ) ;
221- } ,
222- getRegistry : ( ) => ( {
223- apis : apiEndpoints ,
224- totalApis : apiEndpoints . length ,
225- totalEndpoints : apiEndpoints . reduce ( ( acc , api ) => acc + ( api . endpoints ?. length || 0 ) , 0 )
226- } ) ,
227- registerRoute : ( ) => { } ,
228- getRoutes : ( ) => [ ]
229- } ) ;
230- } ,
231- start : ( ) => {
232- console . log ( 'Skipping com.objectstack.runtime.api-registry (Disabled via config override)' ) ;
233- }
234- } ;
235-
236101const plugins : any [ ] = [
237102 new ObjectQLPlugin ( ) ,
238- // new PatchedMSWPlugin(), // Disabled in production mode
239- new PatchedHonoServerPlugin ( {
240- staticRoot : './dist'
241- } ) ,
242- FixedConsolePlugin ,
243- DummyApiRegistryPlugin
103+ // new MSWPlugin(), // Disabled in production mode
104+ // HonoServerPlugin is auto-detected
105+ FixedConsolePlugin
244106] ;
245107
246108// Re-enable MSW only if explicitly needed
247109if ( process . env . ENABLE_MSW_PLUGIN === 'true' ) {
248- plugins . push ( new PatchedMSWPlugin ( ) ) ;
110+ plugins . push ( new MSWPlugin ( ) ) ;
249111}
250112
251113export default defineConfig ( {
0 commit comments