Skip to content

Commit 0cf9209

Browse files
committed
fix: make tests pass again
1 parent 99f2577 commit 0cf9209

File tree

8 files changed

+186
-12
lines changed

8 files changed

+186
-12
lines changed

src/kernel.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ const handle = async (type: 'target' | 'service' | 'mount', name: string, Instan
3131
name
3232
)
3333
console.groupEnd()
34-
if (typeof Instance === 'object') return data
35-
else return instance
34+
return typeof Instance === 'object' ? data : instance
3635
} catch (e) {
3736
print.failed('Failed', `to start ${name}`, e)
3837
console.error(`${spaces}${e.stack.split('\n').join(`\n${spaces}`) as string}`)

src/structures/ProcessLib.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class ProcessLib {
1818
[key: string]: Package
1919
}
2020
config: any
21+
setConfig: (config: any) => any
2122
}
2223

2324
readonly permission: Permission
@@ -41,7 +42,10 @@ export default class ProcessLib {
4142
getExecutable: kernel.getExecutable.bind(kernel),
4243
processList: kernel.processList,
4344
packageList: kernel.packageList,
44-
config: kernel.config
45+
config: kernel.config,
46+
setConfig: (config) => {
47+
if (this.permission >= Permission.ELEVATED) kernel.config = config
48+
}
4549
}
4650
this.process = process
4751
this.data = data

src/system/BootLoader.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import HTML from '../HTML'
2+
import { AppClosedEvent, AppOpenedEvent, Process } from '../types'
3+
import { getTime } from '../utils'
4+
import nullIcon from '../assets/icons/application-default-icon.svg'
5+
import { parse } from 'js-ini'
6+
import { v4 as uuid } from 'uuid'
7+
8+
const BootLoader: Process = {
9+
config: {
10+
name: 'Bootloader',
11+
type: 'process',
12+
targetVer: '1.0.0-indev.0'
13+
},
14+
run: async (process) => {
15+
const splashScreen = await process.loadLibrary('lib/SplashScreen')
16+
const splashElement = splashScreen.getElement()
17+
splashElement.appendTo(document.body)
18+
19+
const { fs } = process
20+
const wm = await process.loadLibrary('lib/WindowManager')
21+
const launcher = await process.loadLibrary('lib/Launcher')
22+
23+
const config = Buffer.from(await fs.readFile('/etc/flow')).toString()
24+
process.kernel.setConfig(parse(config))
25+
26+
if ('serviceWorker' in navigator) {
27+
const registrations = await navigator.serviceWorker.getRegistrations()
28+
for (const registration of registrations) {
29+
await registration.unregister()
30+
}
31+
32+
try {
33+
await navigator.serviceWorker.register(`/uv-sw.js?url=${encodeURIComponent(btoa(process.kernel.config.SERVER))}&e=${uuid()}`, {
34+
scope: '/service/'
35+
})
36+
} catch (e) {
37+
console.error(e)
38+
}
39+
} else {
40+
console.warn('Service workers are not supported.')
41+
}
42+
43+
const input = new HTML('input').attr({
44+
type: 'text',
45+
placeholder: 'Search'
46+
}).on('keyup', () => {
47+
apps.elm.innerHTML = ''
48+
renderApps().catch(e => console.error(e))
49+
}).appendTo(launcher.element)
50+
const apps = new HTML('apps').appendTo(launcher.element)
51+
52+
const renderApps = async (): Promise<void> => {
53+
apps.html('')
54+
const files = await fs.readdir('/home/Applications/')
55+
files
56+
.filter((x: string) => x.endsWith('.app') && ((input.elm as HTMLInputElement) !== null ? x.toLowerCase().includes((input.elm as HTMLInputElement).value.toLowerCase()) : true))
57+
.forEach((file: string) => {
58+
fs.readFile(`/home/Applications/${file}`).then(async (data: Uint8Array) => {
59+
const path = Buffer.from(data).toString()
60+
const executable = await process.kernel.getExecutable(path) as Process
61+
62+
const appElement = new HTML('app').on('click', () => {
63+
process.launch(path).catch((e: any) => console.error(e))
64+
launcher.toggle()
65+
}).appendTo(apps)
66+
new HTML('img').attr({
67+
src: executable.config.icon ?? nullIcon,
68+
alt: `${executable.config.name} icon`
69+
}).appendTo(appElement)
70+
new HTML('div').text(executable.config.name).appendTo(appElement)
71+
}).catch((e: any) => console.error(e))
72+
})
73+
}
74+
75+
await renderApps()
76+
document.addEventListener('fs_update', () => {
77+
renderApps().catch(e => console.error(e))
78+
})
79+
80+
launcher.element.on('click', (e: Event) => {
81+
if (e.target !== e.currentTarget) return
82+
launcher.toggle()
83+
})
84+
85+
const statusBar = await process.loadLibrary('lib/StatusBar')
86+
87+
statusBar.element.html(`
88+
<div class="outlined" data-toolbar-id="start"><span class="material-symbols-rounded">space_dashboard</span></div>
89+
90+
<div data-toolbar-id="apps"></div>
91+
<flex></flex>
92+
<div class="outlined" data-toolbar-id="plugins"><span class="material-symbols-rounded">expand_less</span></div>
93+
<div class="outlined" data-toolbar-id="controls">
94+
<span class="material-symbols-rounded battery">battery_2_bar</span>
95+
<span class="material-symbols-rounded signal">signal_cellular_4_bar</span>
96+
</div>
97+
<div class="outlined" data-toolbar-id="calendar"></div>
98+
99+
`)
100+
101+
setInterval((): any => {
102+
getTime().then((time) => {
103+
statusBar.element.qs('div[data-toolbar-id="calendar"]')?.text(time)
104+
}).catch(e => console.error)
105+
}, 1000)
106+
107+
statusBar.element.qs('div[data-toolbar-id="start"]')?.on('click', () => {
108+
launcher.toggle()
109+
})
110+
111+
if ('getBattery' in navigator) {
112+
(navigator as any).getBattery().then((battery: any) => {
113+
statusBar.updateBatteryIcon(battery)
114+
115+
battery.addEventListener('levelchange', () => {
116+
statusBar.updateBatteryIcon(battery)
117+
})
118+
119+
battery.addEventListener('chargingchange', () => {
120+
statusBar.updateBatteryIcon(battery)
121+
})
122+
})
123+
} else {
124+
const batteryDiv = document.querySelector('div[data-toolbar-id="controls"] > .battery')
125+
if (batteryDiv != null) {
126+
batteryDiv.innerHTML = 'battery_unknown'
127+
}
128+
}
129+
130+
async function ping (startTime: number): Promise<void> {
131+
fetch(`${process.kernel.config.SERVER as string}/bare/`)
132+
.then(() => {
133+
const endTime = performance.now()
134+
const pingTime = endTime - startTime
135+
statusBar.updateIcon(pingTime)
136+
})
137+
.catch(() => {
138+
(document.querySelector('div[data-toolbar-id="controls"] > .signal') as HTMLElement).innerHTML = 'signal_cellular_connected_no_internet_4_bar'
139+
})
140+
}
141+
142+
setInterval((): any => ping(performance.now()), 10_000)
143+
144+
document.addEventListener('app_opened', (e: AppOpenedEvent): void => {
145+
new HTML('app').appendMany(
146+
new HTML('img').attr({
147+
alt: `${e.detail.proc.config.name} icon`,
148+
'data-id': e.detail.token,
149+
src: e.detail.proc.config.icon ?? nullIcon
150+
}).on('click', () => {
151+
e.detail.win.focus()
152+
e.detail.win.toggleMin()
153+
})
154+
).appendTo(statusBar.element.qs('div[data-toolbar-id="apps"]')?.elm as HTMLElement)
155+
})
156+
157+
document.addEventListener('app_closed', (e: AppClosedEvent): void => {
158+
statusBar.element.qs('div[data-toolbar-id="apps"]')?.qs(`img[data-id="${e.detail.token}"]`)?.elm.parentElement?.remove()
159+
})
160+
161+
document.body.style.flexDirection = 'column-reverse'
162+
163+
await statusBar.element.appendTo(document.body)
164+
await launcher.element.appendTo(document.body)
165+
await wm.windowArea.appendTo(document.body)
166+
167+
splashElement.cleanup()
168+
}
169+
}
170+
171+
export default BootLoader

src/system/VirtualFS.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class VirtualFS {
241241
const getRequest = store?.get('fs')
242242

243243
return await new Promise((resolve, reject) => {
244+
if (getRequest == null) return
244245
getRequest.onsuccess = () => {
245246
resolve(getRequest.result)
246247
}
@@ -262,6 +263,7 @@ class VirtualFS {
262263
const putRequest = store?.put(this.fileSystem, 'fs')
263264

264265
return await new Promise((resolve, reject) => {
266+
if (putRequest == null) return
265267
putRequest.onsuccess = () => {
266268
document.dispatchEvent(new CustomEvent('fs_update', {}))
267269
resolve()

src/system/apps/Browser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const BrowserApp: Process = {
7575
iframe: HTMLIFrameElement = document.createElement('iframe')
7676

7777
constructor (url: string) {
78-
this.iframe.src = `/service/${xor.encode(url)}`
78+
this.iframe.src = `/service/${xor.encode(url) as string}`
7979
this.iframe.style.display = 'none'
8080

8181
this.header.innerHTML = `
@@ -97,7 +97,7 @@ const BrowserApp: Process = {
9797
if (this === tabManager.activeTab) {
9898
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
9999
}
100-
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input')?.value ?? '')}`
100+
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input')?.value ?? '') as string}`
101101
}
102102
}
103103

@@ -167,7 +167,7 @@ const BrowserApp: Process = {
167167

168168
win.content.querySelector('.inp')?.addEventListener('keydown', (event: KeyboardEvent) => {
169169
if (event.key === 'Enter') {
170-
tabManager.activeTab.iframe.src = tabManager.activeTab.proxy ? `/service/${xor.encode((win.content.querySelector('.inp') as HTMLInputElement).value)}` : (win.content.querySelector('.inp') as HTMLInputElement).value
170+
tabManager.activeTab.iframe.src = tabManager.activeTab.proxy ? `/service/${xor.encode((win.content.querySelector('.inp') as HTMLInputElement).value) as string}` : (win.content.querySelector('.inp') as HTMLInputElement).value
171171
}
172172
});
173173

src/system/apps/Settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Settings: Process = {
2424
)
2525
})
2626

27-
const fs = process.fs
27+
const { fs } = process
2828
const HTML = await process.loadLibrary('lib/HTML')
2929

3030
const { Input, Button } = await process.loadLibrary('lib/Components')

src/system/apps/Store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Store: Process = {
1919
}, process)
2020
})
2121

22-
const fs = process.fs
22+
const { fs } = process
2323
const HTML = await process.loadLibrary('lib/HTML')
2424
const { Button, Icon } = await process.loadLibrary('lib/Components')
2525

src/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ export const getTime = async (): Promise<string> => {
1616
if (hours === 0) {
1717
hours = 12
1818
} else if (hours > 12) {
19-
hours = hours % 12
19+
hours %= 12
2020
}
2121
}
2222

2323
hours = (hours < 10) ? `0${hours}` : hours
2424
minutes = (minutes < 10) ? `0${minutes}` : minutes
2525

26-
const timeString = use24hrs
26+
return use24hrs
2727
? `${hours}:${minutes}`
2828
: `${hours}:${minutes} ${period}`
29-
30-
return timeString
3129
}
3230

3331
/**

0 commit comments

Comments
 (0)