Initial commit

This commit is contained in:
2024-11-22 23:29:59 -07:00
commit f6379c144b
3594 changed files with 309643 additions and 0 deletions

49
device/browser.js Normal file
View File

@@ -0,0 +1,49 @@
import Store from 'store'
const noWorkingDirWarning = "Please open a directory to allow file saving."
export let cwd
export let store
export async function Start() {
store = await Store.Open('device', 'handles')
cwd = await store.Get('directory')
}
export async function cd() {
cwd = await window.showDirectoryPicker()
await store.Set('directory', cwd)
}
export async function Entries(handle = cwd) {
let pool = new Map()
for await (let [ , e ] of handle.entries()) {
pool.set(e.name, e)
}
return pool
}
export async function Create(sPath) {
return cwd.getFileHandle(sPath, { create: true })
}
export async function Open(hFile) {
if(!cwd) {
return new Blob([ noWorkingDirWarning ], { type: "text/plain" })
}
return hFile.getFile()
}
export async function Write(hFile, sContent) {
if(!cwd) {
throw new Error("Unable to save file: no working directory")
}
let s = await hFile.createWritable()
.catch(console.error)
await s.write(sContent)
await s.close()
}

96
device/kaios.js Normal file
View File

@@ -0,0 +1,96 @@
export const sdcard = navigator.getDeviceStorage('sdcard')
export function promisify(request) {
return new Promise((resolve, reject) => {
request.onsuccess = (event) => {
resolve(request.result)
}
request.onerror = (event) => {
reject(request.error)
console.error(request.error)
}
})
}
export function joinPath(left, right) {
return (left + right).replaceAll(/\/{2,}/g, '/')
}
function handle(sPath, sName) {
return {
name: sName,
path: sPath,
kind: 'directory'
}
}
function resolveHandle(handle) {
let cs = handle.path.split('/')
let f = filesystemIndex
for(let c of cs) {
f = f.entries[c]
if(f == undefined) {
throw new Error('Invalid path:', handle.path)
}
}
return f
}
export let filesystemIndex
export function Start() {
}
export const createIndex = () => {
return new Promise(filesystemIndexHandler)
}
const filesystemIndexHandler = (resolve, reject) => {
let cursor = sdcard.enumerate();
filesystemIndex = {}
cursor.onerror = function() {
reject(cursor.error)
}
cursor.onsuccess = function() {
if(!this.result) {
return resolve(filesystemIndex)
}
let cs = this.result.name.split('/').slice(1)
let f = filesystemIndex
let c
for(let i = 0; i < cs.length; i++) {
c = cs[i]
if(!f.entries) {
f.kind = 'directory'
f.entries = {}
}
f = f.entries[c] ??= handle('/' + cs.slice(0, i + 1).join('/'), c)
}
f.kind = 'file'
// f.file = this.result
this.continue()
}
}
export async function Entries(handle) {
if(!filesystemIndex)
await createIndex()
let pool = new Map()
let t = handle == null ? filesystemIndex : handle
for(let n in t.entries ?? {}) {
let h = t.entries[n]
pool.set(h.name, h)
}
return pool
}
export function Open(hFile) {
// return hFile.file
return promisify(sdcard.get(hFile.path))
}

45
device/nw.js Normal file
View File

@@ -0,0 +1,45 @@
const FS = require('fs/promises')
const Path = require('path')
export const sdcard = navigator.getDeviceStorage("sdcard")
export const music = navigator.getDeviceStorage("music")
function handle(sPath) {
return {
name: sPath,
kind: sPath,
path: sPath
}
}
export async function Init() {
// await loadTheme()
}
export async function Entries() {
let pool = new Map()
for(let d of await FS.readdir(window.cwd, { withFileTypes: true })) {
if(d.isFile()) {
let h = handle(d.name)
pool.set(d.name, h)
}
}
return pool
}
export async function Create(sPath) {
return handle(sPath)
}
export async function Open(hFile) {
let b = await FS.readFile(hFile.path)
return new File([ b ], hFile.name)
}
export async function Write(hFile, sContent) {
return await FS.writeFile(hFile.path, sContent, { encoding: 'utf-8' })
}