|
| 1 | +export * as Mime from "./mime.js" |
| 2 | + |
| 3 | +import { Effect, FileSystem, Option } from "effect" |
| 4 | +import { fileURLToPath } from "url" |
| 5 | +import { FSUtil } from "./fs-util" |
| 6 | + |
| 7 | +const SAMPLE_BYTES = 8192 |
| 8 | + |
| 9 | +export const resolve = Effect.fn("Mime.resolve")(function* (uri: string) { |
| 10 | + const data = dataSample(uri) |
| 11 | + if (data) return detect(data) |
| 12 | + |
| 13 | + const target = yield* Effect.try({ |
| 14 | + try: () => localPath(uri), |
| 15 | + catch: () => new Error("Invalid file URI"), |
| 16 | + }).pipe(Effect.catch(() => Effect.succeed(undefined))) |
| 17 | + if (!target) return "application/octet-stream" |
| 18 | + |
| 19 | + const fs = yield* FSUtil.Service |
| 20 | + const local = yield* Effect.scoped( |
| 21 | + Effect.gen(function* () { |
| 22 | + const info = yield* fs.stat(target) |
| 23 | + if (info.type === "Directory") return { type: "directory" as const } |
| 24 | + if (info.type !== "File") return |
| 25 | + const file = yield* fs.open(target) |
| 26 | + return { |
| 27 | + type: "file" as const, |
| 28 | + sample: Option.getOrElse(yield* file.readAlloc(FileSystem.Size(SAMPLE_BYTES)), () => new Uint8Array()), |
| 29 | + } |
| 30 | + }), |
| 31 | + ).pipe(Effect.catch(() => Effect.succeed(undefined))) |
| 32 | + |
| 33 | + if (local?.type === "directory") return "application/x-directory" |
| 34 | + if (local?.type === "file") return detect(local.sample) |
| 35 | + return "application/octet-stream" |
| 36 | +}) |
| 37 | + |
| 38 | +function detect(bytes: Uint8Array) { |
| 39 | + if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png" |
| 40 | + if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg" |
| 41 | + if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38])) return "image/gif" |
| 42 | + if (startsWith(bytes, [0x42, 0x4d])) return "image/bmp" |
| 43 | + if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46, 0x2d])) return "application/pdf" |
| 44 | + if (startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) && startsWith(bytes.subarray(8), [0x57, 0x45, 0x42, 0x50])) |
| 45 | + return "image/webp" |
| 46 | + if ( |
| 47 | + startsWith(bytes.subarray(4), [0x66, 0x74, 0x79, 0x70]) && |
| 48 | + (startsWith(bytes.subarray(8), [0x61, 0x76, 0x69, 0x66]) || |
| 49 | + startsWith(bytes.subarray(8), [0x61, 0x76, 0x69, 0x73])) |
| 50 | + ) |
| 51 | + return "image/avif" |
| 52 | + return isText(bytes) ? "text/plain" : "application/octet-stream" |
| 53 | +} |
| 54 | + |
| 55 | +function dataSample(uri: string) { |
| 56 | + if (!uri.startsWith("data:")) return |
| 57 | + const comma = uri.indexOf(",") |
| 58 | + if (comma === -1) return new Uint8Array() |
| 59 | + const metadata = uri.slice(5, comma) |
| 60 | + const payload = uri.slice(comma + 1) |
| 61 | + if (metadata.split(";").some((part) => part.toLowerCase() === "base64")) { |
| 62 | + return Buffer.from(payload.slice(0, Math.ceil((SAMPLE_BYTES * 4) / 3) + 4), "base64").subarray(0, SAMPLE_BYTES) |
| 63 | + } |
| 64 | + return new TextEncoder().encode(payload.slice(0, SAMPLE_BYTES)) |
| 65 | +} |
| 66 | + |
| 67 | +function localPath(uri: string) { |
| 68 | + if (!URL.canParse(uri)) return |
| 69 | + const url = new URL(uri) |
| 70 | + if (url.protocol !== "file:") return |
| 71 | + return fileURLToPath(url) |
| 72 | +} |
| 73 | + |
| 74 | +function startsWith(bytes: Uint8Array, prefix: number[]) { |
| 75 | + return prefix.every((value, index) => bytes[index] === value) |
| 76 | +} |
| 77 | + |
| 78 | +function isText(bytes: Uint8Array) { |
| 79 | + if (bytes.length === 0) return true |
| 80 | + if (bytes.includes(0)) return false |
| 81 | + try { |
| 82 | + new TextDecoder("utf-8", { fatal: true }).decode(bytes, { stream: true }) |
| 83 | + } catch { |
| 84 | + return false |
| 85 | + } |
| 86 | + const controls = bytes.reduce((count, byte) => count + Number(byte < 9 || (byte > 13 && byte < 32)), 0) |
| 87 | + return controls / bytes.length <= 0.3 |
| 88 | +} |
0 commit comments