Skip to content
Myr

raw.ts

This file

Path

packages/project/src/code/raw.ts

163 lines · 4.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 1–10why, one step back
1import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";
2import { Rev } from "@repo/protocol/domain/code";3import { BlobSha, RepoPath } from "@repo/protocol/ids";4import type { ProjectName } from "@repo/protocol/ids";5import { Effect, Option, Schema } from "effect";67import { codeProjectOf } from "../code.ts";8import type { CodeProject } from "../code.ts";9import { Projects } from "../projects.ts";10import { isBinary, unlessTooLarge } from "./file.ts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 11why, one step back
11import { commitAt, isFullSha, resolveAt } from "./revision.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 12–13why, one step back
12import { entryAt, segmentsOf } from "./tree.ts";
13

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 14why, one step back
14/** Both origins hold a session cookie, so no raw file may run as script: every answer, a refusal too, carries `nosniff` and this CSP. */

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 15–75why, one step back
15const SANDBOX =
16 "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; sandbox";1718const IMAGES = new Map([19 ["avif", "image/avif"],20 ["gif", "image/gif"],21 ["ico", "image/x-icon"],22 ["jpeg", "image/jpeg"],23 ["jpg", "image/jpeg"],24 ["png", "image/png"],25 ["svg", "image/svg+xml"],26 ["webp", "image/webp"],27]);2829const TEXT = "text/plain; charset=utf-8";3031const SAFE = {32 "content-security-policy": SANDBOX,33 "x-content-type-options": "nosniff",34} as const;3536const extensionOf = (path: string) => {37 const name = path.slice(path.lastIndexOf("/") + 1);38 const dot = name.lastIndexOf(".");3940 return dot <= 0 ? "" : name.slice(dot + 1).toLowerCase();41};4243export const rawHeaders = (44 path: RepoPath,45 rev: "sha" | "ref",46 blob: BlobSha,47 binary = false48): Headers => {49 const headers = new Headers({50 ...SAFE,51 "content-type":52 IMAGES.get(extensionOf(path)) ??53 (binary ? "application/octet-stream" : TEXT),54 });5556 if (rev === "sha") {57 headers.set("cache-control", "public, max-age=31536000, immutable");58 headers.set("etag", `"${blob}"`);59 } else {60 headers.set("cache-control", "public, max-age=60");61 }6263 return headers;64};6566const notFound = () => new Response(null, { headers: SAFE, status: 404 });6768const tooLarge = () => new Response(null, { headers: SAFE, status: 413 });6970export const rawOf = (71 project: CodeProject,72 rev: Rev,73 path: RepoPath74): Effect.Effect<Response, never, RepoHost> =>75 Effect.gen(function* () {

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 76why, one step back
76 const { commit, repo } = yield* resolveAt(project, Option.some(rev));

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 77why, one step back
77

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 78why, one step back
78 const { tree } = yield* commitAt(repo, commit).pipe(

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 79–81why, one step back
79 Effect.flatMap(Effect.fromOption)
80 );81

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 82why, one step back
82 const entry = yield* entryAt(repo, tree, path);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 83–90why, one step back
83
84 if (Option.isNone(entry) || entry.value.type !== "blob") {85 return notFound();86 }8788 const blob = BlobSha.make(entry.value.sha);8990 const bytes = yield* RepoHost.use((host) =>

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 91why, one step back
91 host.readBlob(repo, ObjectId.make(blob))

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 92–139why, one step back
92 );
9394 return Option.match(bytes, {95 onNone: notFound,96 onSome: (read) =>97 new Response(new Uint8Array(read), {98 headers: rawHeaders(99 path,100 isFullSha(rev) ? "sha" : "ref",101 blob,102 isBinary(read)103 ),104 status: 200,105 }),106 });107 }).pipe(108 (read) => unlessTooLarge(read, tooLarge),109 Effect.catchTags({110 NoSuchElementError: () => Effect.succeed(notFound()),111 RepoNotFound: () => Effect.succeed(notFound()),112 UnknownRevision: () => Effect.succeed(notFound()),113 })114 );115116const decodeRev = Schema.decodeUnknownOption(Rev);117118const decodePath = Schema.decodeUnknownOption(RepoPath);119120const decoded = Option.liftThrowable(decodeURIComponent);121122export const rawRequestOf = (123 rest: string124): Option.Option<{ readonly rev: Rev; readonly path: RepoPath }> => {125 const slash = rest.indexOf("/");126127 if (slash === -1) {128 return Option.none();129 }130131 return Option.all({132 path: decoded(rest.slice(slash + 1)).pipe(133 Option.filter((path) => Option.isSome(segmentsOf(path))),134 Option.flatMap(decodePath)135 ),136 rev: Option.flatMap(decoded(rest.slice(0, slash)), decodeRev),137 });138};139

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 140why, one step back
140/** Read from the URL, not a router's parameter: a router decodes `%2F`, which would cut a tag's name in two. */

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 141–143why, one step back
141export const rawRestOf = (url: string, project: ProjectName): string => {
142 const segments = new URL(url, "http://raw.invalid").pathname.split("/");143 const named = project.split("/").length;

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 144why, one step back
144

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 145–151why, one step back
145 if (segments[named + 1] === "raw") {
146 return segments.slice(named + 2).join("/");147 }148149 return segments[2] === "raw" ? segments.slice(3).join("/") : "";150};151

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 152–155why, one step back
152export const rawFor = (
153 project: ProjectName,154 url: string155): Effect.Effect<Response, never, RepoHost | Projects> =>

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 156why, one step back
156 Option.match(rawRequestOf(rawRestOf(url, project)), {

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 157–163why, one step back
157 onNone: () => Effect.succeed(notFound()),
158 onSome: ({ path, rev }) =>159 Projects.use((projects) => projects.get(project)).pipe(160 Effect.flatMap((api) => rawOf(codeProjectOf(api, project), rev, path)),161 Effect.catchTag("UnknownProject", () => Effect.succeed(notFound()))162 ),163 });