11/**
2- * MSW Browser Worker Setup via ObjectStack Service
3- *
4- * This creates a complete ObjectStack environment in the browser using the In-Memory Driver
5- * and the MSW Plugin which automatically exposes the API.
2+ * MSW Browser Worker Setup via ObjectStack Runtime
3+ *
4+ * Uses the standard @objectstack/plugin-msw to create a complete ObjectStack
5+ * environment in the browser with In-Memory Driver and MSW-intercepted API.
66 */
77
88import { ObjectKernel , DriverPlugin , AppPlugin } from '@objectstack/runtime' ;
99import { ObjectQLPlugin } from '@objectstack/objectql' ;
1010import { InMemoryDriver } from '@objectstack/driver-memory' ;
1111import { MSWPlugin } from '@objectstack/plugin-msw' ;
12- import { setupWorker } from 'msw/browser' ;
1312import appConfig from '../../objectstack.shared' ;
1413
1514let kernel : ObjectKernel | null = null ;
1615let driver : InMemoryDriver | null = null ;
1716
1817export async function startMockServer ( ) {
1918 // Polyfill process.on for ObjectKernel in browser environment
20- // This prevents the "process.on is not a function" error when ObjectKernel initializes
2119 try {
2220 if ( typeof process !== 'undefined' && ! ( process as any ) . on ) {
2321 ( process as any ) . on = ( ) => { } ;
@@ -35,53 +33,23 @@ export async function startMockServer() {
3533
3634 driver = new InMemoryDriver ( ) ;
3735
38- // Create kernel with MiniKernel architecture
3936 kernel = new ObjectKernel ( {
4037 skipSystemValidation : true
4138 } ) ;
42-
43- // Register ObjectQL engine
44- await kernel . use ( new ObjectQLPlugin ( ) )
45-
46- // Register the driver
47- await kernel . use ( new DriverPlugin ( driver , 'memory' ) )
48-
49- // Load app config as a plugin
50- await kernel . use ( new AppPlugin ( appConfig ) )
51-
52- // MSW Plugin (intercepts network requests)
53- // Disable auto-start to manually control worker registration with correct path
54- const mswPlugin = new MSWPlugin ( {
55- enableBrowser : true ,
39+
40+ await kernel . use ( new ObjectQLPlugin ( ) ) ;
41+ await kernel . use ( new DriverPlugin ( driver , 'memory' ) ) ;
42+ await kernel . use ( new AppPlugin ( appConfig ) ) ;
43+
44+ // MSW Plugin handles worker setup automatically with enableBrowser: true
45+ await kernel . use ( new MSWPlugin ( {
46+ enableBrowser : true ,
5647 baseUrl : '/api/v1' ,
5748 logRequests : true
58- } ) ;
49+ } ) ) ;
5950
60- await kernel . use ( mswPlugin ) ;
61-
6251 await kernel . bootstrap ( ) ;
6352
64- // Manually start MSW worker with correct service worker path
65- if ( typeof window !== 'undefined' ) {
66- const handlers = mswPlugin . getHandlers ( ) ;
67- const worker = setupWorker ( ...handlers ) ;
68-
69- // unless vite base is handled strangely. But typically public assets follow base.
70- const swUrl = '/mockServiceWorker.js' ;
71-
72- if ( import . meta. env . DEV ) console . log ( `[MSW] Starting worker with script at: ${ swUrl } ` ) ;
73-
74- await worker . start ( {
75- onUnhandledRequest : 'bypass' ,
76- serviceWorker : {
77- url : swUrl
78- }
79- } ) ;
80- }
81-
82- // Note: AppPlugin already loads manifest data during bootstrap via the Seeder,
83- // so we don't need to manually seed it again here. Doing so would create duplicates.
84-
8553 if ( import . meta. env . DEV ) console . log ( '[MSW] ObjectStack Runtime ready' ) ;
8654 return kernel ;
8755}
0 commit comments