fix(memory): fail-open exists probe, drop dead readRegistry
This commit is contained in:
1 parent
0478061457
commit
929c5dc50c
2 files changed
+43
-10
No files matched your search
@@ -25,6 +25,8 @@ type World = {
|
|||||||
tools: string[] // every tool name registered
|
tools: string[] // every tool name registered
|
||||||
files: Map<string, string | null> // other files (null: a directory)
|
files: Map<string, string | null> // other files (null: a directory)
|
||||||
unreadable: Set<string> // paths whose fs.read throws
|
unreadable: Set<string> // paths whose fs.read throws
|
||||||
|
statFails: Set<string> // paths whose fs.stat throws though they exist
|
||||||
|
existsFails: Set<string> // paths whose fs.exists throws
|
||||||
}
|
}
|
||||||
|
|
||||||
type Opts = {
|
type Opts = {
|
||||||
@@ -34,6 +36,8 @@ type Opts = {
|
|||||||
storeFails?: boolean // store.set rejects (a hand-made store)
|
storeFails?: boolean // store.set rejects (a hand-made store)
|
||||||
files?: Record<string, string | null> // registries under the root
|
files?: Record<string, string | null> // registries under the root
|
||||||
unreadable?: string[] // paths whose fs.read throws
|
unreadable?: string[] // paths whose fs.read throws
|
||||||
|
statFails?: string[] // paths whose fs.stat throws though they exist
|
||||||
|
existsFails?: string[] // paths whose fs.exists throws
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,6 +68,8 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise<World> {
|
|||||||
sets: [], logs: [], tools: [],
|
sets: [], logs: [], tools: [],
|
||||||
files: new Map(Object.entries(o.files ?? {})),
|
files: new Map(Object.entries(o.files ?? {})),
|
||||||
unreadable: new Set(o.unreadable ?? []),
|
unreadable: new Set(o.unreadable ?? []),
|
||||||
|
statFails: new Set(o.statFails ?? []),
|
||||||
|
existsFails: new Set(o.existsFails ?? []),
|
||||||
}
|
}
|
||||||
handStore(on, w, o.storeFails ?? false)
|
handStore(on, w, o.storeFails ?? false)
|
||||||
on('tool.register', (_a, e) => {
|
on('tool.register', (_a, e) => {
|
||||||
@@ -71,10 +77,12 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise<World> {
|
|||||||
return { value: undefined } as never
|
return { value: undefined } as never
|
||||||
})
|
})
|
||||||
on('env.get', () => ({ value: HOME }))
|
on('env.get', () => ({ value: HOME }))
|
||||||
on('fs.exists', (_a, e) => ({
|
on('fs.exists', (_a, e) => {
|
||||||
value: w.files.has(e.path) || e.path === SWITCH,
|
if (w.existsFails.has(e.path)) throw new Error('EIO')
|
||||||
}))
|
return { value: w.files.has(e.path) || e.path === SWITCH }
|
||||||
|
})
|
||||||
on('fs.stat', (_a, e) => {
|
on('fs.stat', (_a, e) => {
|
||||||
|
if (w.statFails.has(e.path)) throw new Error('EIO')
|
||||||
if (w.files.has(e.path)) {
|
if (w.files.has(e.path)) {
|
||||||
const text = w.files.get(e.path)
|
const text = w.files.get(e.path)
|
||||||
return { value: {
|
return { value: {
|
||||||
@@ -809,3 +817,25 @@ test('a span of 12 chars is indexed, one of 11 is not', () => {
|
|||||||
expect(at12.size).toBe(1)
|
expect(at12.size).toBe(1)
|
||||||
expect(at11.size).toBe(0)
|
expect(at11.size).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('a rejected stat on an existing registry is unreadable', async (
|
||||||
|
$, on,
|
||||||
|
) => {
|
||||||
|
const w = await boot($, on, { files: REGS, statFails: [BLK] })
|
||||||
|
await failing($, w, EXIT1 + 'error: pathspec did not match anything')
|
||||||
|
expect(await pending($)).toContain(
|
||||||
|
'index: read learnings.md; skipped blockers.md (unreadable)')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a throwing exists probe names the registry, keeps the other', async (
|
||||||
|
$, on,
|
||||||
|
) => {
|
||||||
|
const files = { [BLK]: BLOCKERS, [LRN]: LEARNINGS.replace(
|
||||||
|
'superseded', 'open') }
|
||||||
|
const w = await boot($, on, { files, statFails: [BLK], existsFails: [BLK] })
|
||||||
|
await failing($, w, EXIT1 + 'error: pathspec did not match anything')
|
||||||
|
const text = await pending($)
|
||||||
|
expect(text).toContain(
|
||||||
|
'index: read learnings.md; skipped blockers.md (unreadable)')
|
||||||
|
expect(text).toContain('- known LRN-002 \u00d71')
|
||||||
|
})
|
||||||
@@ -294,6 +294,15 @@ type Entry = { id: string; body: string }
|
|||||||
|
|
||||||
type Read = { text: string } | { why: string }
|
type Read = { text: string } | { why: string }
|
||||||
|
|
||||||
|
/** True only when exists says no; a throwing probe cannot tell. */
|
||||||
|
async function isGone($: Api, path: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
return !(await $.fs.exists(path))
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** One stat, one read: the text, or why the registry is skipped. */
|
/** One stat, one read: the text, or why the registry is skipped. */
|
||||||
// a rejected stat means missing only when exists says so
|
// a rejected stat means missing only when exists says so
|
||||||
async function tryRead($: Api, path: string): Promise<Read> {
|
async function tryRead($: Api, path: string): Promise<Read> {
|
||||||
@@ -302,7 +311,7 @@ async function tryRead($: Api, path: string): Promise<Read> {
|
|||||||
if (info.kind !== 'file') return { why: 'not a file' }
|
if (info.kind !== 'file') return { why: 'not a file' }
|
||||||
if (info.size > MAX_REGISTRY) return { why: 'over 4 MiB' }
|
if (info.size > MAX_REGISTRY) return { why: 'over 4 MiB' }
|
||||||
} catch {
|
} catch {
|
||||||
return { why: (await $.fs.exists(path)) ? 'unreadable' : 'missing' }
|
return { why: (await isGone($, path)) ? 'missing' : 'unreadable' }
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return { text: await $.fs.read(path) }
|
return { text: await $.fs.read(path) }
|
||||||
@@ -311,12 +320,6 @@ async function tryRead($: Api, path: string): Promise<Read> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A registry's text; `''` when skipped (missing, not a file, too big). */
|
|
||||||
export async function readRegistry($: Api, path: string): Promise<string> {
|
|
||||||
const r = await tryRead($, path)
|
|
||||||
return 'text' in r ? r.text : ''
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The BLK/LRN entries; a body runs from its heading to the next one. */
|
/** The BLK/LRN entries; a body runs from its heading to the next one. */
|
||||||
export function entriesOf(text: string): Entry[] {
|
export function entriesOf(text: string): Entry[] {
|
||||||
const heads = [...text.matchAll(HEADING)]
|
const heads = [...text.matchAll(HEADING)]
|
||||||
|
|||||||
Reference in new issue
Block a user