+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
168 lines
5.8 KiB
TypeScript
168 lines
5.8 KiB
TypeScript
export * as LocationMutation from "./location-mutation"
|
|
|
|
import { makeLocationNode } from "./effect/app-node"
|
|
import path from "path"
|
|
import { Context, Effect, Layer, Schema } from "effect"
|
|
import { FSUtil } from "./fs-util"
|
|
import { Location } from "./location"
|
|
import { Project } from "./project"
|
|
import { AbsolutePath } from "./schema"
|
|
|
|
export const Kind = Schema.Literals(["file", "directory"])
|
|
export type Kind = typeof Kind.Type
|
|
|
|
/**
|
|
* Mutation paths do not accept project references. Relative paths must stay
|
|
* inside the active Location. Absolute paths outside it require separate
|
|
* `external_directory` approval.
|
|
*/
|
|
export const ResolveInput = Schema.Struct({
|
|
path: Schema.String,
|
|
/** Selects the external approval boundary; it does not validate the target type. */
|
|
kind: Kind.pipe(Schema.optional),
|
|
})
|
|
export type ResolveInput = typeof ResolveInput.Type
|
|
|
|
export class PathError extends Schema.TaggedErrorClass<PathError>()("LocationMutation.PathError", {
|
|
path: Schema.String,
|
|
reason: Schema.Literals(["relative_escape", "location_escape", "non_directory_ancestor"]),
|
|
}) {}
|
|
|
|
export interface ExternalDirectoryAuthorization {
|
|
readonly action: "external_directory"
|
|
/** Canonical existing directory used as the external approval boundary. */
|
|
readonly directory: string
|
|
/** `external_directory` permission resource. */
|
|
readonly resource: string
|
|
readonly save: string
|
|
}
|
|
|
|
export const externalDirectoryPermission = (input: ExternalDirectoryAuthorization) => ({
|
|
action: input.action,
|
|
resources: [input.resource],
|
|
save: [input.save],
|
|
})
|
|
|
|
export interface Target {
|
|
/** Canonical existing path, or missing path below a canonical directory. */
|
|
readonly canonical: string
|
|
/** Permission resource: Location-relative for internal paths, canonical for external paths. */
|
|
readonly resource: string
|
|
readonly externalDirectory?: ExternalDirectoryAuthorization
|
|
}
|
|
|
|
export interface Interface {
|
|
/**
|
|
* Resolve a path and derive its permission resources. Relative paths must
|
|
* stay inside the Location. Absolute paths outside it require separate
|
|
* `external_directory` approval. This does not approve the mutation.
|
|
*/
|
|
readonly resolve: (input: ResolveInput) => Effect.Effect<Target, PathError | FSUtil.Error>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/LocationMutation") {}
|
|
|
|
interface ResolvedPath {
|
|
readonly canonical: string
|
|
readonly type?:
|
|
| "File"
|
|
| "Directory"
|
|
| "SymbolicLink"
|
|
| "BlockDevice"
|
|
| "CharacterDevice"
|
|
| "FIFO"
|
|
| "Socket"
|
|
| "Unknown"
|
|
readonly directory: string
|
|
}
|
|
|
|
const slash = (value: string) => value.replaceAll("\\", "/")
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
const location = yield* Location.Service
|
|
const locationRoot = yield* fs.realPath(location.directory)
|
|
|
|
function notFound<A>(effect: Effect.Effect<A, FSUtil.Error>) {
|
|
return effect.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)))
|
|
}
|
|
|
|
const resolvePath = Effect.fnUntraced(function* (absolute: string) {
|
|
const existing = yield* notFound(fs.realPath(absolute))
|
|
if (existing !== undefined) {
|
|
const info = yield* fs.stat(existing)
|
|
return {
|
|
canonical: existing,
|
|
type: info.type,
|
|
directory: info.type === "Directory" ? existing : path.dirname(existing),
|
|
} satisfies ResolvedPath
|
|
}
|
|
|
|
let anchor = path.dirname(absolute)
|
|
while (true) {
|
|
const canonical = yield* notFound(fs.realPath(anchor))
|
|
if (canonical !== undefined) {
|
|
const info = yield* fs.stat(canonical)
|
|
if (info.type !== "Directory") {
|
|
return yield* new PathError({ path: absolute, reason: "non_directory_ancestor" })
|
|
}
|
|
return {
|
|
canonical: path.resolve(canonical, path.relative(anchor, absolute)),
|
|
directory: canonical,
|
|
} satisfies ResolvedPath
|
|
}
|
|
const parent = path.dirname(anchor)
|
|
if (parent === anchor) return yield* new PathError({ path: absolute, reason: "non_directory_ancestor" })
|
|
anchor = parent
|
|
}
|
|
})
|
|
|
|
const resolve = Effect.fn("LocationMutation.resolve")(function* (input: ResolveInput) {
|
|
const relative = !path.isAbsolute(input.path)
|
|
const absolute = path.resolve(location.directory, input.path)
|
|
const lexicallyInternal = FSUtil.contains(location.directory, absolute)
|
|
if (relative && !lexicallyInternal) return yield* new PathError({ path: input.path, reason: "relative_escape" })
|
|
|
|
const resolved = yield* resolvePath(absolute)
|
|
if (lexicallyInternal && !FSUtil.contains(locationRoot, resolved.canonical)) {
|
|
return yield* new PathError({ path: input.path, reason: "location_escape" })
|
|
}
|
|
|
|
const external = !lexicallyInternal
|
|
const resource = external
|
|
? slash(resolved.canonical)
|
|
: slash(path.relative(locationRoot, resolved.canonical) || ".")
|
|
const externalDirectory =
|
|
input.kind === "directory" && resolved.type === "Directory" ? resolved.canonical : resolved.directory
|
|
const externalResource = slash(path.join(externalDirectory, "*"))
|
|
return {
|
|
canonical: resolved.canonical,
|
|
resource,
|
|
externalDirectory: external
|
|
? {
|
|
action: "external_directory",
|
|
directory: externalDirectory,
|
|
resource: externalResource,
|
|
save: slash(
|
|
path.join(
|
|
(yield* Project.root(fs, AbsolutePath.make(externalDirectory))) ?? externalDirectory,
|
|
"*",
|
|
),
|
|
),
|
|
}
|
|
: undefined,
|
|
} satisfies Target
|
|
})
|
|
|
|
return Service.of({ resolve })
|
|
}),
|
|
)
|
|
|
|
export const node = makeLocationNode({
|
|
service: Service,
|
|
layer: layer.pipe(Layer.orDie),
|
|
deps: [FSUtil.node, Location.node],
|
|
})
|