Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🔨] (#133) conditional types for loadLibrary added #134

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/structures/ProcessLib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import semver from 'semver'
import Kernel from '../kernel'
import { Process, Executable, LibraryData, Package, Library, Permission } from '../types'
import { Process, Executable, Package, Library, Permission, LoadedLibrary, LibraryPath } from '../types'
import FlowWindow from './FlowWindow'
import LibraryLib from './LibraryLib'
import ProcLib from './ProcLib'
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class ProcessLib {
})
}

async loadLibrary (url: string): Promise<LibraryData> {
async loadLibrary <T extends LibraryPath>(url: T): Promise<LoadedLibrary<T>> {
let executable: Executable
try {
const comps = import.meta.glob('../system/**/*.ts')
Expand Down
12 changes: 6 additions & 6 deletions src/system/BootLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const BootLoader: Process = {
const files = await fs.readdir('/home/Applications/')
files
.filter((x: string) => x.endsWith('.app') && ((input.elm as HTMLInputElement) !== null ? x.toLowerCase().includes((input.elm as HTMLInputElement).value.toLowerCase()) : true))
.forEach(async (file: string) => {
.forEach((file: string) => {
fs.readFile(`/home/Applications/${file}`).then(async (data: Uint8Array) => {
const path = Buffer.from(data).toString()
const executable = await process.kernel.getExecutable(path) as Process
Expand All @@ -97,7 +97,7 @@ const BootLoader: Process = {
alt: `${executable.config.name} icon`
}).appendTo(appElement)
new HTML('div').text(executable.config.name).appendTo(appElement)
})
}).catch((e: any) => console.error(e))
})
}

Expand Down Expand Up @@ -129,11 +129,11 @@ const BootLoader: Process = {

setInterval((): any => {
getTime().then((time) => {
statusBar.element.qs('div[data-toolbar-id="calendar"]').text(time)
statusBar.element.qs('div[data-toolbar-id="calendar"]')?.text(time)
}).catch(e => console.error)
}, 1000)

statusBar.element.qs('div[data-toolbar-id="start"]').on('click', () => {
statusBar.element.qs('div[data-toolbar-id="start"]')?.on('click', () => {
launcher.toggle()
})

Expand Down Expand Up @@ -180,11 +180,11 @@ const BootLoader: Process = {
e.detail.win.focus()
e.detail.win.toggleMin()
})
).appendTo(statusBar.element.qs('div[data-toolbar-id="apps"]'))
).appendTo(statusBar.element.qs('div[data-toolbar-id="apps"]')?.elm as HTMLElement)
})

document.addEventListener('app_closed', (e: AppClosedEvent): void => {
statusBar.element.qs('div[data-toolbar-id="apps"]').qs(`img[data-id="${e.detail.token}"]`).elm.parentElement.remove()
statusBar.element.qs('div[data-toolbar-id="apps"]')?.qs(`img[data-id="${e.detail.token}"]`)?.elm.parentElement?.remove()
})

document.body.style.flexDirection = 'column-reverse'
Expand Down
4 changes: 2 additions & 2 deletions src/system/UserAccessControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const UserAccessControl: Process = {
const target = process.data.executable
return await new Promise((resolve) => {
process.loadLibrary('lib/WindowManager').then(async wm => {
let message
let message = 'Unknown action'
switch (process.data.type) {
case 'launch': {
message = `${initiator.config.name as string} wants to launch ${target.config.name as string}`
Expand All @@ -42,7 +42,7 @@ const UserAccessControl: Process = {
} else {
resolve(false)
}
})
}).catch((e) => console.error(e))
}).catch((e) => console.error(e))
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/system/apps/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
targetVer: '1.0.0-indev.0'
},
run: async (process) => {
const win = await process.loadLibrary('lib/WindowManager').then((wm: any) => {
const win = await process.loadLibrary('lib/WindowManager').then(wm => {
return wm.createWindow({
title: 'Browser',
icon,
Expand Down Expand Up @@ -75,7 +75,7 @@
iframe: HTMLIFrameElement = document.createElement('iframe')

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

this.header.innerHTML = `
Expand All @@ -97,7 +97,7 @@
if (this === tabManager.activeTab) {
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
}
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input').value) as string}`
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input')?.value ?? '')}`
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@

win.content.querySelector('.inp')?.addEventListener('keydown', (event: KeyboardEvent) => {
if (event.key === 'Enter') {
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
tabManager.activeTab.iframe.src = tabManager.activeTab.proxy ? `/service/${xor.encode((win.content.querySelector('.inp') as HTMLInputElement).value)}` : (win.content.querySelector('.inp') as HTMLInputElement).value

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/system/apps/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Files: Process = {
(win.content.querySelector('.folder') as HTMLElement).onclick = async () => {
const title = prompt('Enter folder name')
if (title != null) {
await fs.mkdir(`${dir}/${title}`, '')
await fs.mkdir(`${dir}/${title}`)
}
}

Expand All @@ -69,7 +69,7 @@ const Files: Process = {
const genIcon = (): string => {
return `<span class="material-symbols-rounded">${(MIMETypes[file.split('.')[1]] === undefined ? 'draft' : MIMETypes[file.split('.')[1]].icon) as string}</span>`
}
const icon = fileStat.isDirectory() as boolean ? '<span class="material-symbols-rounded">folder</span>' : genIcon()
const icon = fileStat.isDirectory() ? '<span class="material-symbols-rounded">folder</span>' : genIcon()

element.innerHTML += `${icon} <span style="flex:1;">${file}</span><span class="material-symbols-rounded delete">delete_forever</span><span class="material-symbols-rounded rename">edit</span>`;
(element.querySelector('.rename') as HTMLElement).onclick = async () => {
Expand All @@ -79,7 +79,7 @@ const Files: Process = {
}
}
(element.querySelector('.delete') as HTMLElement).onclick = async () => {
if (fileStat.isDirectory() as boolean) {
if (fileStat.isDirectory()) {
await fs.rmdir(dir + seperator + file)
} else {
await fs.unlink(dir + seperator + file)
Expand All @@ -101,7 +101,7 @@ const Files: Process = {
}

element.ondblclick = async () => {
if (fileStat.isDirectory() as boolean) {
if (fileStat.isDirectory()) {
await setDir(dir + seperator + file)
} else {
await run(dir + seperator + file)
Expand Down
2 changes: 1 addition & 1 deletion src/system/apps/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const Store: Process = {
install(app.url)
}
}
})
}).catch((e: any) => console.error(e))
}).catch((e: any) => console.error(e))
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/system/lib/MIMETypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Library } from '../../types'

const MIME: Library = {
const MIMETypes: Library = {
config: {
name: 'MIMETypes',
type: 'library',
Expand Down Expand Up @@ -83,4 +83,4 @@ const MIME: Library = {
}
}

export default MIME
export default MIMETypes
4 changes: 2 additions & 2 deletions src/system/lib/VirtualFS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Kernel from '../../kernel'
import ProcessLib from '../../structures/ProcessLib'
import { Directory, Errors, File, Library, Permission } from '../../types'
import { Directory, Errors, File, Library, Permission, Stats } from '../../types'

console.debug = (...args: any[]) => {
console.log('[VirtualFS]', ...args)
Expand Down Expand Up @@ -421,7 +421,7 @@ const VirtualFS: Library = {
console.debug(`readdir ${path}`)
return result
},
stat: async (path: string): Promise<{ isDirectory: () => boolean, isFile: () => boolean }> => {
stat: async (path: string): Promise<Stats> => {
const { current } = await navigatePath(path)

console.debug(`stat ${path}`)
Expand Down
3 changes: 0 additions & 3 deletions src/system/lib/XOR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const XOR: Library = {
},
init: (l, k, p) => {},
data: {
randomMax: 100,
randomMin: -100,

encode: (str: string): string => {
return encodeURIComponent(
str
Expand Down
61 changes: 61 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import HTML from './HTML'
import Kernel from './kernel'
import FlowWindow from './structures/FlowWindow'
import LibraryLib from './structures/LibraryLib'
import ProcessLib from './structures/ProcessLib'
import Components from './system/lib/Components'
import MIMETypes from './system/lib/MIMETypes'

export interface AppClosedEvent extends CustomEvent {
detail: {
Expand Down Expand Up @@ -124,3 +127,61 @@ export interface KernelConfig {
SERVER: string
[key: string]: any
}

export interface Stats {
isDirectory: () => boolean
isFile: () => boolean
}

export interface FileSystem {
unlink: (path: string) => Promise<void>
readFile: (path: string) => Promise<Buffer>
writeFile: (path: string, content: string | Buffer) => Promise<void>
mkdir: (path: string) => Promise<void>
rmdir: (path: string) => Promise<void>
readdir: (path: string) => Promise<string[]>
stat: (path: string) => Promise<Stats>
rename: (oldPath: string, newPath: string) => Promise<void>
exists: (path: string) => Promise<boolean>
}

export interface ModalData {
value: boolean
win: FlowWindow
}
export interface WindowManager {
windowArea: HTML
windows: FlowWindow[]
getHighestZIndex: () => number
createWindow: (config: FlowWindowConfig, process: ProcessLib) => FlowWindow
createModal: (title: string, text: string, process: ProcessLib) => Promise<ModalData>
}

export interface Launcher {
element: HTML
toggle: () => void
}

export interface XOR {
encode: (str: string) => string
decode: (str: string) => string
}

export interface StatusBar {
element: HTML
updateBatteryIcon: (battery: any) => void
updateIcon: (ms: number) => void
}

export type LoadedLibrary<T> =
T extends 'lib/VirtualFS' ? FileSystem :
T extends 'lib/WindowManager' ? WindowManager :
T extends 'lib/HTML' ? typeof HTML :
T extends 'lib/Launcher' ? Launcher :
T extends 'lib/XOR' ? XOR :
T extends 'lib/StatusBar' ? StatusBar :
T extends 'lib/MIMETypes' ? typeof MIMETypes.data :
T extends 'lib/Components' ? typeof Components.data :
any

export type LibraryPath = 'lib/VirtualFS' | 'lib/WindowManager' | string
Loading