Files
orca/src/shared/node-markdown-document-discovery.ts
T
NeilandOrca 879aad7dd6 oom(foundation): bound shared readers/limits + add BoundedMap primitive (#10299)
* oom(01): A1-shared-readers — reintroduce #10179 subset

Files: 18 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(02): A2-shared-image-media — reintroduce #10179 subset

Files: 7 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(03): A3-shared-fs-listing — reintroduce #10179 subset

Files: 21 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(04): A4-shared-remote-relay — reintroduce #10179 subset

Files: 8 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(05): A5-shared-misc — reintroduce #10179 subset

Files: 28 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(06): B-shared-wiring — reintroduce #10179 subset

Files: 81 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-24 21:36:57 -07:00

87 lines
2.8 KiB
TypeScript

import { opendir } from 'node:fs/promises'
import type { Dirent, Dir } from 'node:fs'
import { join } from 'node:path'
import {
assertMarkdownDocumentPathWithinLimit,
createMarkdownDocumentListingBudget,
MarkdownDocumentListingCapacityError,
retainMarkdownRelativePath,
visitMarkdownDocumentListingEntry,
type MarkdownDocumentListingLimits
} from './markdown-document-listing-limits'
type MarkdownDirectoryReader = (path: string) => Promise<Dir | AsyncIterable<Dirent>>
export type MarkdownDocumentDiscoveryOptions = {
shouldDescend: (relativePath: string, name: string) => boolean
ignoreNestedDirectoryErrors?: boolean
limits?: Partial<MarkdownDocumentListingLimits>
readDirectory?: MarkdownDirectoryReader
signal?: AbortSignal
}
export function isMarkdownDocumentPath(path: string): boolean {
const lowerPath = path.toLowerCase()
return lowerPath.endsWith('.md') || lowerPath.endsWith('.mdx') || lowerPath.endsWith('.markdown')
}
export async function discoverMarkdownRelativePaths(
rootPath: string,
options: MarkdownDocumentDiscoveryOptions
): Promise<string[]> {
const budget = createMarkdownDocumentListingBudget(options.limits)
const documents: string[] = []
const readDirectory = options.readDirectory ?? opendir
assertMarkdownDocumentPathWithinLimit(rootPath, budget.limits.maxPathBytes)
const visitDirectory = async (
absoluteDirectoryPath: string,
relativeDirectoryPath: string,
depth: number
): Promise<void> => {
throwIfAborted(options.signal)
let directory: Dir | AsyncIterable<Dirent>
try {
directory = await readDirectory(absoluteDirectoryPath)
} catch (error) {
if (depth > 0 && options.ignoreNestedDirectoryErrors) {
return
}
throw error
}
for await (const entry of directory) {
throwIfAborted(options.signal)
const relativePath = relativeDirectoryPath
? `${relativeDirectoryPath}/${entry.name}`
: entry.name
const nextDepth = depth + 1
const shouldDescend = entry.isDirectory() && options.shouldDescend(relativePath, entry.name)
visitMarkdownDocumentListingEntry(budget, relativePath, shouldDescend ? nextDepth : depth)
if (entry.isSymbolicLink()) {
continue
}
if (entry.isDirectory()) {
if (shouldDescend) {
await visitDirectory(join(absoluteDirectoryPath, entry.name), relativePath, nextDepth)
}
continue
}
if (entry.isFile() && isMarkdownDocumentPath(entry.name)) {
retainMarkdownRelativePath(budget, rootPath, relativePath)
documents.push(relativePath)
}
}
}
await visitDirectory(rootPath, '', 0)
return documents
}
function throwIfAborted(signal: AbortSignal | undefined): void {
if (!signal?.aborted) {
return
}
throw signal.reason instanceof Error ? signal.reason : new MarkdownDocumentListingCapacityError()
}