refactor(server): serve raw filesystem content (#31911)
This commit is contained in:
parent
31b233e9db
commit
31c5454ee6
12 changed files with 67 additions and 93 deletions
|
|
@ -1,8 +1,7 @@
|
|||
export * as FileSystem from "./filesystem"
|
||||
|
||||
import path from "path"
|
||||
import { pathToFileURL } from "url"
|
||||
import { Context, Effect, Layer, Option, Schema } from "effect"
|
||||
import { Context, Effect, Layer, Schema } from "effect"
|
||||
import { EventV2 } from "./event"
|
||||
import { FSUtil } from "./fs-util"
|
||||
import { Location } from "./location"
|
||||
|
|
@ -59,7 +58,7 @@ export const Event = {
|
|||
}
|
||||
|
||||
export interface Interface {
|
||||
readonly read: (input: ReadInput) => Effect.Effect<Content>
|
||||
readonly read: (input: ReadInput) => Effect.Effect<{ readonly content: Uint8Array; readonly mime: string }>
|
||||
readonly list: (input?: ListInput) => Effect.Effect<Entry[]>
|
||||
readonly find: (input: FindInput) => Effect.Effect<Entry[]>
|
||||
readonly glob: (input: GlobInput) => Effect.Effect<readonly Entry[]>
|
||||
|
|
@ -91,27 +90,9 @@ const baseLayer = Layer.effect(
|
|||
const target = yield* resolve(input.path)
|
||||
const info = yield* fs.stat(target.real).pipe(Effect.orDie)
|
||||
if (info.type !== "File") return yield* Effect.die(new Error("Path is not a file"))
|
||||
const bytes = yield* fs.readFile(target.real).pipe(Effect.orDie)
|
||||
const mime = FSUtil.mimeType(target.real)
|
||||
if (!bytes.includes(0)) {
|
||||
const content = yield* Effect.sync(() => new TextDecoder("utf-8", { fatal: true }).decode(bytes)).pipe(
|
||||
Effect.option,
|
||||
)
|
||||
if (Option.isSome(content))
|
||||
return {
|
||||
uri: pathToFileURL(target.real).href,
|
||||
name: path.basename(target.real),
|
||||
content: content.value,
|
||||
encoding: "utf8" as const,
|
||||
mime,
|
||||
}
|
||||
}
|
||||
return {
|
||||
uri: pathToFileURL(target.real).href,
|
||||
name: path.basename(target.real),
|
||||
content: Buffer.from(bytes).toString("base64"),
|
||||
encoding: "base64" as const,
|
||||
mime,
|
||||
content: yield* fs.readFile(target.real).pipe(Effect.orDie),
|
||||
mime: FSUtil.mimeType(target.real),
|
||||
}
|
||||
}),
|
||||
list: Effect.fn("FileSystem.list")(function* (input = {}) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue