(params)
| 114 | export function createRuntimeNodeLocalFilesAdapter(): RuntimeNodeFilesAdapter { |
| 115 | return { |
| 116 | async describePath(params) { |
| 117 | const record = asRecord(params) |
| 118 | const targetPath = stringValue(record, "path") |
| 119 | const maxReadSize = positiveInt(record.maxReadSize, DEFAULT_MAX_READ_SIZE) |
| 120 | const readContents = boolValue(record.readContents) |
| 121 | const showHidden = boolValue(record.showHidden) |
| 122 | |
| 123 | try { |
| 124 | const stat = await fs.lstat(targetPath) |
| 125 | if (stat.isDirectory()) { |
| 126 | const names = await fs.readdir(targetPath) |
| 127 | const entries = (await Promise.all(names.filter((name) => showHidden || !name.startsWith(".")).map((name) => entryFor(targetPath, name)))) |
| 128 | .filter((entry): entry is PathEntry => entry !== null) |
| 129 | .sort((a, b) => Number(b.isDir) - Number(a.isDir) || a.name.localeCompare(b.name)) |
| 130 | return { type: "dir", path: targetPath, mode: stat.mode, entries } |
| 131 | } |
| 132 | |
| 133 | if (stat.isFile()) { |
| 134 | const tooLarge = stat.size > maxReadSize |
| 135 | let isReadable = true |
| 136 | let metadata: FileMetadata | null = null |
| 137 | if (readContents) { |
| 138 | try { |
| 139 | metadata = classifyFileMetadata(targetPath, await readFileSample(targetPath, stat.size)) |
| 140 | } catch { |
| 141 | isReadable = false |
| 142 | } |
| 143 | } |
| 144 | const content = readContents && isReadable && !tooLarge && metadata?.isBinary !== true ? await fs.readFile(targetPath, "utf8").catch(() => null) : null |
| 145 | if (readContents && isReadable && !tooLarge && metadata?.isBinary !== true && content === null) isReadable = false |
| 146 | return { |
| 147 | type: "file", |
| 148 | path: targetPath, |
| 149 | size: stat.size, |
| 150 | mode: stat.mode, |
| 151 | content, |
| 152 | tooLarge, |
| 153 | isReadable, |
| 154 | ...(metadata ? { isBinary: metadata.isBinary, mediaType: metadata.mediaType, previewKind: metadata.previewKind } : {}), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return { type: "error", path: targetPath, message: "Path is neither a file nor directory" } |
| 159 | } catch (error) { |
| 160 | if ((error as NodeJS.ErrnoException).code === "ENOENT") return { type: "not_found", path: targetPath } |
| 161 | return { type: "error", path: targetPath, message: error instanceof Error ? error.message : "Unable to read path" } |
| 162 | } |
| 163 | }, |
| 164 | |
| 165 | async readFile(params) { |
| 166 | const record = asRecord(params) |
nothing calls this directly
no test coverage detected