WorkspaceModule · library/workspace/src/workspace-files-service.ts:132

WorkspaceFilesService

component

No JSDoc comment in source yet.

API

@Component()
export class WorkspaceFilesService implements WorkspaceFiles {
  async statExisting(workspace: string, rels: string[]): Promise<string[]>;
  async statExistingWithMtime(workspace: string, rels: string[]): Promise<WorkspaceFileStat[]>;
  async list(workspace: string, rel: string): Promise<WorkspaceFilesResponse>;
  async resolvePath(workspace: string, rel: string): Promise<string>;
  async read(workspace: string, rel: string, options?: WorkspaceFileReadOptions): Promise<WorkspaceFileContent>;
  async write(workspace: string, rel: string, data: Buffer, ifVersion?: string): Promise<string>;
  async create(workspace: string, rel: string, kind: "file" | "dir"): Promise<void>;
  async move(workspace: string, from: string, to: string, ifVersion?: string): Promise<void>;
  async remove(workspace: string, rel: string, ifVersion?: string): Promise<void>;
  async search(workspace: string, q: string): Promise<WorkspaceSearchResponse>;
}

Dependencies & Wiring

Extracted from class decorators
implementsinterfaces

Methods (10)

statExisting

async statExisting(workspace: string, rels: string[]): Promise<string[]>

Batch existence check (a message's file card lists only files that actually exist): each item goes through the same containment resolution as reading (resolveRead); out-of-bounds, resolution failure, missing Workspace, or an irregular file are all treated as non-existent — the card scenario only asks "can this be opened", and throwing a 4xx would only add frontend branches while leaking containment details. Returns the deduplicated existing items in input order.

@paramworkspacestring
@paramrelsstring[]
@returnsPromise<string[]>

statExistingWithMtime

async statExistingWithMtime(workspace: string, rels: string[]): Promise<WorkspaceFileStat[]>

The same check, with each file's modification time — for the caller that has to tell a file this run wrote from one that was already sitting in the Workspace (the messaging bridge sends only the former). Statting is what the check already does, so the time costs nothing extra.

@paramworkspacestring
@paramrelsstring[]
@returnsPromise<WorkspaceFileStat[]>

list

async list(workspace: string, rel: string): Promise<WorkspaceFilesResponse>

List a directory: dirs come first, each group sorted by name. Entries whose canonical target leaves the Workspace are omitted, so listing cannot expose metadata for an out-of-bounds symlink.

@paramworkspacestring
@paramrelstring
@returnsPromise<WorkspaceFilesResponse>

resolvePath

async resolvePath(workspace: string, rel: string): Promise<string>

The canonical absolute path of an existing entry, for the caller that hands the path to something other than this process's own IO (the desktop "show in folder", which passes it to the machine's file manager). Resolution is the read path's, so a .. and a symlink pointing out of the Workspace are refused here exactly as they are for a read, and a path that is not there at all is a 404 rather than a path the OS is asked to open.

@paramworkspacestring
@paramrelstring
@returnsPromise<string>

read

async read(workspace: string, rel: string, options?: WorkspaceFileReadOptions): Promise<WorkspaceFileContent>

Read a file (preview/download): IO on the canonical path (resolveRead has already eliminated symlink escapes).

The stat below runs before the bytes are read, and that order is load-bearing now that it also mints the version marker: a rewrite landing in between tags fresh bytes with the older marker, so the worst case is one needless conflict on the next write. Read-then-stat would fail the other way — old bytes carrying the new marker, which is exactly the write the precondition exists to refuse.

@paramworkspacestring
@paramrelstring
@paramoptions?WorkspaceFileReadOptions
@returnsPromise<WorkspaceFileContent>

write

async write(workspace: string, rel: string, data: Buffer, ifVersion?: string): Promise<string>

Write a file (upload, overwriting a same-named one). If the parent directory is missing, it's automatically created under sandbox checks (preserving directory structure for folder uploads); the final segment is opened with O_NOFOLLOW, refusing to follow a symlink to write outside the Workspace (together with resolveWriteParent's canonical-parent check, this blocks sandbox escapes).

ifVersion is the write precondition: the marker the caller got from its read, which the file must still carry. It is compared on the open handle — a handle names an inode, so what is stat'd is exactly what is about to be truncated, with no window in between. A caller that passes none read no version and gets the unconditional create-or-overwrite an upload wants: the field's absence, not some sentinel value, is what separates "first write of a file that isn't there" from "the file I read has changed". With a marker the file must already exist, so O_CREAT is left out — a missing file is a change like any other (ENOENT → the same 409), and a refused write must not leave an empty file behind.

Resolves to the version the write produced, read as soon as the bytes are down: the marker the caller's next conditional write of this file carries. Reading it back with a second request instead would adopt a rewrite landing between the two as the caller's own, and the next save would silently overwrite it.

@paramworkspacestring
@paramrelstring
@paramdataBuffer
@paramifVersion?string
@returnsPromise<string>

create

async create(workspace: string, rel: string, kind: "file" | "dir"): Promise<void>

Create one empty file or one folder at rel (the Files panel's New menu), refusing anything already there.

Missing parents are created the way a write creates them (resolveWriteParent), so a symlinked ancestor pointing out of the Workspace is caught by the write path's own checks. The entry itself is made by the primitive that fails rather than replaces: O_CREAT | O_EXCL for a file — which on POSIX also refuses a symlink at the final segment instead of following it (Windows gets an lstat first, below) — and a non-recursive mkdir for a folder. Either way an occupied path is 409 target_exists with nothing written, and on POSIX there is no check-then-act window to lose to the Agent writing the same path. A folder the server may not write in is 403 dir_write_denied.

@paramworkspacestring
@paramrelstring
@paramkind"file" | "dir"
@returnsPromise<void>

move

async move(workspace: string, from: string, to: string, ifVersion?: string): Promise<void>

Move or rename one Workspace file or folder.

A file is resolved as a mutable entry (canonical parent, O_NOFOLLOW on the final segment, version marker read off the open handle); the destination is resolved as a write, so its parent is created when missing under the same checks an upload runs. An occupied destination is a 409 rather than an overwrite — see targetExists — and a move onto the file's own path is a 400 rather than a success that did nothing.

A folder moves whole, and without a precondition: it has no single version marker, and one sent for it is a 400 rather than a check that silently never ran. See moveFolder for the two destinations it refuses.

@paramworkspacestring
@paramfromstring
@paramtostring
@paramifVersion?string
@returnsPromise<void>

remove

async remove(workspace: string, rel: string, ifVersion?: string): Promise<void>

Delete one Workspace file. Files only, for the same reason move is: a directory carries no version marker, so nothing can protect a recursive delete from removing work the Agent did after the panel last looked.

ifVersion is optional in the wire shape — the service will delete unconditionally without it — but the Files panel always sends the marker its read returned, so a file the Agent rewrote under the user's cursor is a 409 rather than a lost file.

@paramworkspacestring
@paramrelstring
@paramifVersion?string
@returnsPromise<void>