Fix indexeddb file system readdir to use a range

This commit is contained in:
Misode
2024-11-26 18:04:11 +01:00
parent 46ed105c34
commit 6badc9f06f
7 changed files with 20 additions and 19 deletions

View File

@@ -1,3 +1,4 @@
import type { ExternalFileSystem } from '@spyglassmc/core'
import * as zip from '@zip.js/zip.js'
import type { Identifier, NbtTag, Random } from 'deepslate'
import { Matrix3, Matrix4, NbtByte, NbtCompound, NbtDouble, NbtInt, NbtList, NbtString, Vector } from 'deepslate'
@@ -635,3 +636,12 @@ export function safeJsonParse(text: string): any {
return undefined
}
}
export async function clearFolder(fs: ExternalFileSystem, uri: string) {
const entries = await fs.readdir(uri)
return Promise.all(entries.map(async e => {
if (e.name !== uri) {
return await fs.unlink(e.name)
}
}))
}

View File

@@ -381,7 +381,7 @@ function formatUnionMember(type: SimplifiedMcdocTypeNoUnion, others: SimplifiedM
}
if (!others.some(o => o.kind === type.kind)) {
// No other member is of this kind
return formatIdentifier(type.kind)
return formatIdentifier(type.kind === 'struct' ? 'object' : type.kind)
}
if (type.kind === 'struct') {
// Show the first literal key
@@ -390,7 +390,7 @@ function formatUnionMember(type: SimplifiedMcdocTypeNoUnion, others: SimplifiedM
return formatUnionMember(firstKey, [])
}
}
return formatIdentifier(type.kind)
return formatIdentifier(type.kind === 'struct' ? 'object' : type.kind)
}
function UnionBody({ type, optional, node, ctx }: Props<UnionType<SimplifiedMcdocTypeNoUnion>>) {

View File

@@ -29,7 +29,7 @@ export function ProjectPanel() {
setEntries(undefined)
client.fs.readdir(projectRoot).then(entries => {
setEntries(entries.flatMap(e => {
return e.isFile() && e.name.startsWith(projectRoot) ? [e.name.slice(projectRoot.length)] : []
return e.isFile() ? [e.name.slice(projectRoot.length)] : []
}))
})
}, [projectRoot])

View File

@@ -12,7 +12,7 @@ import type { VersionId } from '../../services/index.js'
import { checkVersion, fetchDependencyMcdoc, fetchPreset, fetchRegistries, getSnippet, shareSnippet } from '../../services/index.js'
import { DEPENDENCY_URI, SpyglassClient } from '../../services/Spyglass.js'
import { Store } from '../../Store.js'
import { cleanUrl, genPath } from '../../Utils.js'
import { cleanUrl, clearFolder, genPath } from '../../Utils.js'
import { Ad, Btn, BtnMenu, ErrorPanel, FileCreation, FileView, Footer, HasPreview, Octicon, PreviewPanel, ProjectPanel, SearchList, SourcePanel, TextInput, VersionSwitcher } from '../index.js'
import { getRootDefault } from './McdocHelpers.js'
@@ -94,10 +94,7 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
if (!service || !uri) {
return AsyncCancel
}
const dependencies = await SpyglassClient.FS.readdir(DEPENDENCY_URI)
await Promise.all(dependencies.flatMap(async d => {
d.isFile() && d.name.startsWith(DEPENDENCY_URI) ? [await SpyglassClient.FS.unlink(d.name)] : []
}))
await clearFolder(SpyglassClient.FS, DEPENDENCY_URI)
if (gen.dependency) {
const dependency = await fetchDependencyMcdoc(gen.dependency)
const dependencyUri = `${DEPENDENCY_URI}${gen.dependency}.mcdoc`

View File

@@ -134,12 +134,7 @@ export function ProjectProvider({ children }: { children: ComponentChildren }) {
if (project) {
const projectRoot = getProjectRoot(project)
const entries = await SpyglassClient.FS.readdir(projectRoot)
await Promise.all(entries.flatMap(async e => {
if (e.name.startsWith(projectRoot)) {
return [await SpyglassClient.FS.unlink(e.name)]
}
return []
}))
await Promise.all(entries.map(async e => SpyglassClient.FS.unlink(e.name)))
}
changeProjects(projects.filter(p => p.name !== name))
setOpenProject(undefined)

View File

@@ -311,8 +311,7 @@ export class IndexedDbFileSystem implements core.ExternalFileSystem {
return new Promise((res, rej) => {
const transaction = db.transaction(IndexedDbFileSystem.storeName, 'readonly')
const store = transaction.objectStore(IndexedDbFileSystem.storeName)
// TODO: specify range
const request = store.openCursor()
const request = store.openCursor(IDBKeyRange.bound(location, location + '\uffff'))
const result: { name: string, isDirectory(): boolean, isFile(): boolean, isSymbolicLink(): boolean }[] = []
request.onsuccess = () => {
if (request.result) {

View File

@@ -14,7 +14,7 @@ import siteConfig from '../Config.js'
import { computeIfAbsent, genPath } from '../Utils.js'
import type { VersionMeta } from './DataFetcher.js'
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js'
import { IndexedDbFileSystem, MixedFileSystem } from './FileSystem.js'
import { IndexedDbFileSystem } from './FileSystem.js'
import type { VersionId } from './Versions.js'
export const CACHE_URI = 'file:///cache/'
@@ -52,7 +52,7 @@ interface ClientDocument {
}
export class SpyglassClient {
public static readonly FS = new MixedFileSystem(new IndexedDbFileSystem(), [])
public static readonly FS = new IndexedDbFileSystem()
public readonly fs = SpyglassClient.FS
public readonly externals: core.Externals = {
...BrowserExternals,
@@ -108,7 +108,7 @@ export class SpyglassService {
await Promise.all(this.treeWatchers.map(async ({ prefix, handler }) => {
const entries = await client.fs.readdir(prefix)
handler(entries.flatMap(e => {
return e.isFile() && e.name.startsWith(prefix) ? [e.name.slice(prefix.length)] : []
return e.isFile() ? [e.name.slice(prefix.length)] : []
}))
}))
})