-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
183 lines (158 loc) · 6.55 KB
/
index.ts
File metadata and controls
183 lines (158 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { ErrorContext, FluxStack, PluginContext, RequestContext, ResponseContext } from "@/core/plugins/types"
// ✅ Plugin imports its own configuration
import { FluxStackDesktopConfig } from './config'
// Import the desktop functionality
import { open } from './src'
// Browser types (copied from src for type checking)
type BrowserName =
| 'chrome'
| 'chrome_canary'
| 'chromium'
| 'chromium_snapshot'
| 'edge'
| 'firefox'
| 'firefox_nightly'
// Type guard for valid browser names
function isBrowserName(value: string): value is BrowserName {
const validBrowsers: BrowserName[] = [
'chrome',
'chrome_canary',
'chromium',
'chromium_snapshot',
'edge',
'firefox',
'firefox_nightly'
]
return validBrowsers.includes(value as BrowserName)
}
/**
* FluxStack Desktop Plugin
* Create native desktop applications from web apps using system browsers and Bun runtime.
*/
export class FluxStackDesktopPlugin implements FluxStack.Plugin {
name = 'fluxstack-desktop'
version = '1.0.0'
description = 'Create native desktop applications from web apps using system browsers and Bun runtime.'
author = 'FluxStack Team'
category = 'desktop'
tags = ['desktop', 'browser', 'electron-alternative']
/**
* Setup hook - called when plugin is loaded
*/
async setup(context: PluginContext): Promise<void> {
// Check if plugin is enabled
if (!FluxStackDesktopConfig.enabled) {
context.logger.info(`[FluxStack Desktop] Plugin disabled by configuration`)
return
}
context.logger.info(`[FluxStack Desktop] Plugin initialized`)
context.logger.info(`[FluxStack Desktop] Auto-open: ${FluxStackDesktopConfig.autoOpen}`)
context.logger.info(`[FluxStack Desktop] Auto-shutdown: ${FluxStackDesktopConfig.autoShutdown}`)
context.logger.info(`[FluxStack Desktop] Window size: ${FluxStackDesktopConfig.windowWidth}x${FluxStackDesktopConfig.windowHeight}`)
// Store plugin info for display
if (!(global as any).__fluxstackPlugins) {
(global as any).__fluxstackPlugins = []
}
(global as any).__fluxstackPlugins.push({
name: 'FluxStack Desktop',
status: 'Active',
details: `Auto-open: ${FluxStackDesktopConfig.autoOpen ? 'Yes' : 'No'}, Auto-shutdown: ${FluxStackDesktopConfig.autoShutdown ? 'Yes' : 'No'}`
})
}
/**
* Server start hook - called when server starts
* Opens desktop window if auto-open is enabled
*/
async onServerStart?(context: PluginContext): Promise<void> {
if (!FluxStackDesktopConfig.enabled) return
if (FluxStackDesktopConfig.autoOpen) {
const maxRetries = 3
const retryDelay = 2000 // 2 seconds
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
context.logger.info(`[FluxStack Desktop] Opening desktop window at ${FluxStackDesktopConfig.targetUrl}${attempt > 1 ? ` (attempt ${attempt}/${maxRetries})` : ''}`)
const windowSize: [number, number] = [
FluxStackDesktopConfig.windowWidth,
FluxStackDesktopConfig.windowHeight
]
const forceBrowserConfig = FluxStackDesktopConfig.forceBrowser
const forceBrowser = forceBrowserConfig && isBrowserName(forceBrowserConfig)
? forceBrowserConfig
: undefined
// Setup auto-shutdown callback if enabled
const onBrowserExit = FluxStackDesktopConfig.autoShutdown
? () => {
context.logger.info(`[FluxStack Desktop] Browser window closed, shutting down server...`)
// Add shutdown delay for graceful cleanup
setTimeout(() => {
context.logger.info(`[FluxStack Desktop] Initiating graceful server shutdown`)
process.exit(0)
}, FluxStackDesktopConfig.shutdownDelay)
}
: undefined
const browser = await open(FluxStackDesktopConfig.targetUrl, {
windowSize,
...(forceBrowser && { forceBrowser }),
...(onBrowserExit && { onBrowserExit })
})
if (browser) {
context.logger.info(`[FluxStack Desktop] Desktop window opened successfully`)
if (FluxStackDesktopConfig.autoShutdown) {
context.logger.info(`[FluxStack Desktop] Auto-shutdown enabled - server will close when browser window closes`)
}
return // Success, exit the retry loop
}
} catch (error) {
context.logger.error(`[FluxStack Desktop] Failed to open desktop window (attempt ${attempt}/${maxRetries}):`, error)
if (attempt < maxRetries) {
context.logger.info(`[FluxStack Desktop] Retrying in ${retryDelay / 1000} seconds...`)
await new Promise(resolve => setTimeout(resolve, retryDelay))
} else {
context.logger.error(`[FluxStack Desktop] All ${maxRetries} attempts failed. Desktop window could not be opened.`)
}
}
}
}
}
/**
* Request hook - called on each request
*/
async onRequest?(context: RequestContext): Promise<void> {
if (!FluxStackDesktopConfig.enabled) return
// Optional: Log desktop-related requests if debug is enabled
if (FluxStackDesktopConfig.debug) {
const userAgent = (context as any).headers?.['user-agent'] || ''
if (userAgent.includes('Chrome') || userAgent.includes('Firefox') || userAgent.includes('Edge')) {
console.debug(`[FluxStack Desktop] Request from desktop browser: ${context.method} ${context.path}`)
}
}
}
/**
* Response hook - called on each response
*/
async onResponse?(context: ResponseContext): Promise<void> {
if (!FluxStackDesktopConfig.enabled || !FluxStackDesktopConfig.debug) return
// Optional: Log desktop-related responses if debug is enabled
const userAgent = (context as any).headers?.['user-agent'] || ''
if (userAgent.includes('Chrome') || userAgent.includes('Firefox') || userAgent.includes('Edge')) {
console.debug(`[FluxStack Desktop] Response to desktop browser: ${context.statusCode} ${context.path}`)
}
}
/**
* Error hook - called when errors occur
*/
async onError?(context: ErrorContext): Promise<void> {
console.error(`[FluxStack Desktop] Error:`, context.error)
// Optionally handle desktop-specific errors here
}
}
// Export plugin instance
export default new FluxStackDesktopPlugin()
// Re-export the open function and utilities for external use
export { open, createDesktopUtils, type DesktopUtils } from './src'
export type {
ScreenshotOptions,
WindowState,
NotificationOptions,
FileDialogOptions
} from './src'