@flow-state-dev/engine/folder
Interfaces
Entry
A path's entry kind, plus the failure behind an unreadable one.
Properties
error?
error?: Error;
Set only for unreadable, so the report can say what went wrong.
kind
kind: EntryKind;
OpenedDirectory
What openStructuralDirectory found at a structural folder.
Three outcomes, and the two that are not the happy path stay apart: entries
is set when the folder listed, refusal when it is there and the walk will
not go through it, and neither when it is simply absent — the one silent
outcome.
Properties
entries?
entries?: string[];
The folder's entries. Set only when it listed.
refusal?
refusal?: object;
Why the walk stopped here, when that is worth reporting.
error
error: Error;
reason
reason: "symlink" | "unreadable";
symlink when the folder is a link; unreadable when it is there and readdir failed.
PathReport
One path that should have produced something and did not, keyed by its slash-separated path relative to the tree root — deliberately not by an identity, because a folder that breaks the naming rules has no identity to be reported under.
Kind is the reader's own closed union of the conditions it can report, and
every entry carries one. Required rather than optional on purpose: a flat
array of reports is otherwise only tellable apart by matching on
error.message, and requiring the tag is what stops a condition added later
landing untagged.
Type Parameters
Kind
Kind extends string
Properties
error
error: Error;
kind
kind: Kind;
Which of the reader's conditions this entry is.
path
path: string;
Type Aliases
EntryKind
type EntryKind = "directory" | "file" | "symlink" | "absent" | "unreadable";
What a path is, without following symlinks.
Variables
IGNORED_ENTRIES
const IGNORED_ENTRIES: ReadonlySet<string>;
Names that never denote a thing in the tree — editor and OS droppings, which appear at every enumerated level and are skipped before they are read as a name. One list rather than one per reader: a name that is not a team to one reader is not a worker or document to another. It is also the only pass the claim pass gives: every other entry in a kit's folder is used or refused.
Published as a view that cannot be written to, not as the set the readers
consult. One list shared by every reader in the process is exactly the thing
a consumer must not be able to edit: IGNORED_ENTRIES.add("engineering")
would make every later read silently skip that team, with nothing thrown and
nothing reported. A ReadonlySet annotation alone does not stop that — types
erase, and .add is still there at runtime — so the mutators are absent
rather than merely untyped.
Functions
classify()
function classify(target): Promise<Entry>;
Classify a path without following symlinks. Symlinks are never followed: a folder can come from anywhere, and one could escape the root or point at something sensitive.
The answer is the one the path as written produces, with exactly one exception: when removing the trailing separators reveals a symlink, that wins. Nothing else about the spelling changes any answer.
The exception exists because lstat resolves the final symlink when a path
ends in a separator: <link> is a link, <link>/ is the directory behind it.
The trailing form is the ordinary way a directory gets written down — it falls
out of config, environment variables and hand-joined paths — so classifying
the string exactly as given would leave the no-follow answer one character
from being wrong, here and in everything built on it.
The exception is kept that narrow because a trailing separator is otherwise a
real assertion about the path, and the filesystem enforces it: <file>/ fails
with ENOTDIR, and answering file for it would have this helper accept a
spelling that nothing underneath it can open. So the strip decides
symlink-ness and the original decides everything else — <file>/ stays
unreadable, <missing>/ stays absent.
What gets stripped is guarded just as narrowly. A trailing separator is the
only piece of a path's spelling that cannot change which entry the path
denotes; every other normalization can. path.resolve and path.normalize
both collapse .. lexically, while the kernel applies it after traversing
a symlinked component, so the two disagree exactly when a link is involved:
spelled <base>/a/../c with `a` a link to <base>/sub/b
collapsed <base>/c an ordinary directory
what lstat sees <base>/sub/c a symlink to somewhere else entirely
Classifying the collapsed form would answer directory about a path whose
readdir follows a link — the caller is told the coast is clear and then
opens the linked tree. So the classification has to be about the entry the
caller actually named. Whoever tidies this later: that is why this is a
hand-rolled suffix strip and not a one-liner from node:path.
Only the final entry is answered for. A symlink in an ancestor component is
followed by lstat, here as everywhere, and is not this function's question.
Only a missing path is absent. Every other failure is unreadable and
stays distinct, for the same reason openStructuralDirectory keeps
them apart: a directory that is readable but not searchable (r-- rather
than r-x) lists its children and then fails to stat any of them, so folding
that into absent would drop everything under it while leaving the reports
empty — the one thing a caller told to treat them as fatal cannot see.
Parameters
target
string
Returns
Promise<Entry>
openRoot()
function openRoot(root, what): Promise<void>;
Open a configured folder's root, or throw. what names the folder in every
message ("workforce directory", "folder of kit \"notes\""), so the
refusal says which configuration is wrong.
The root is the one level whose failure is a wiring mistake rather than a missing worker or document, so it throws where everything below it is collected: a reader that returned an empty result for a root that is not there would hand an app a silent zero-worker roster.
It is classified before it is listed, for the reason every nested structural
folder is: a bare readdir follows a symlink, and a symlinked root would
load the whole tree from somewhere the caller never configured.
The refusal holds however the caller spelled the path — <root> and
<root>/ alike — because classify normalizes the spelling before it
answers. That matters most here: a root is the one path in the tree a user
writes by hand, into config or an environment variable, where the trailing
form is the ordinary way to write a directory down.
One spelling is refused before it is classified at all: a root carrying an
interior ... Everything below the root is addressed as path.join(root, …), which collapses .. lexically, while the check here goes through the
kernel, which collapses it only after walking what precedes it. With a link
in front of the .. those are two different directories, so classifying the
root would answer for a tree the walk never reads — the check would pass and
the content would come from somewhere else. Refused rather than resolved,
because resolving it would pick one of the two meanings on the caller's
behalf; the message asks for the path the caller means.
Returns nothing. Every reader goes on to open the levels it wants by name.
Parameters
root
string
what
string
Returns
Promise<void>
openStructuralDirectory()
function openStructuralDirectory(target, reportAs): Promise<OpenedDirectory>;
List one of a walk's structural directories — teams, a team's workers, a
level's skills. Returns its entries, or the reason the walk cannot go that
way, or neither when the folder is simply absent.
Absence is the only silent outcome, and separating it from the rest is why
this helper exists. A missing folder is a tree that does not go that way; a
folder that exists and cannot be read is a set of things the app has lost,
and reading it as empty would drop them while leaving errors empty. The
symlink check is what stops readdir following teams -> /outside and
loading files from outside the configured root.
Hands the reason back rather than filing a report itself, because which condition a refusal counts as belongs to the reader: this helper serves more than one, and their conditions are not the same.
Parameters
target
string
reportAs
string
Returns
Promise<OpenedDirectory>
refusedSymlink()
function refusedSymlink(what, name): Error;
The one wording for a refused symlink, wherever a walk meets one.
Parameters
what
string
name
string
Returns
Error
unreadable()
function unreadable(
what,
name,
cause
): Error;
The one wording for a path that is there and could not be read.
Parameters
what
string
name
string
cause
Error | undefined
Returns
Error