File
Blob: src/worker/dav/collection-handler.ts
| 1 | import { parseCollectionObjectPath } from "@/worker/dav/paths"; |
| 2 | import type { ProppatchInstruction } from "@/worker/xml/dav-request"; |
| 3 | |
| 4 | export type { DavConditionalHeaders, DavReadResult } from "@/worker/dav/readers"; |
| 5 | export { |
| 6 | acceptedDavOrigins, |
| 7 | readCollectionPropfind, |
| 8 | readCollectionProppatch, |
| 9 | readDavConditionalHeaders, |
| 10 | readReportDepth, |
| 11 | } from "@/worker/dav/readers"; |
| 12 | export type { DavResultError } from "@/worker/dav/responses"; |
| 13 | export { |
| 14 | davObjectResponse, |
| 15 | davResultError, |
| 16 | multigetMultistatus, |
| 17 | protectedProppatchMultistatus, |
| 18 | proppatchMultistatus, |
| 19 | syncCollectionMultistatus, |
| 20 | } from "@/worker/dav/responses"; |
| 21 | |
| 22 | export type CollectionDavPath<TCollectionKind extends string> = |
| 23 | | { kind: "home" } |
| 24 | | { kind: TCollectionKind; collectionName: string } |
| 25 | | { kind: "object"; collectionName: string; objectName: string }; |
| 26 | |
| 27 | export type DavDeadPropInstruction = |
| 28 | | { kind: "set"; nsUri: string; localName: string; xmlValue: string } |
| 29 | | { kind: "remove"; nsUri: string; localName: string }; |
| 30 | |
| 31 | export function parseCollectionDavPath<TCollectionKind extends string>( |
| 32 | pathname: string, |
| 33 | basePath: string, |
| 34 | collectionKind: TCollectionKind, |
| 35 | ): CollectionDavPath<TCollectionKind> | null { |
| 36 | const path = parseCollectionObjectPath(pathname, basePath); |
| 37 | if (!path) return null; |
| 38 | if (path.kind === "home") return path; |
| 39 | if (path.kind === "collection") return { kind: collectionKind, collectionName: path.collectionName }; |
| 40 | return { kind: "object", collectionName: path.collectionName, objectName: path.objectName }; |
| 41 | } |
| 42 | |
| 43 | export function collectionDavHref<TCollectionKind extends string>( |
| 44 | path: CollectionDavPath<TCollectionKind>, |
| 45 | input: { |
| 46 | homeHref: string; |
| 47 | collectionHref: (collectionName: string) => string; |
| 48 | objectHref: (collectionName: string, objectName: string) => string; |
| 49 | }, |
| 50 | ): string { |
| 51 | if ("objectName" in path) return input.objectHref(path.collectionName, path.objectName); |
| 52 | if ("collectionName" in path) return input.collectionHref(path.collectionName); |
| 53 | return input.homeHref; |
| 54 | } |
| 55 | |
| 56 | export function deadPropInstructions(instructions: ProppatchInstruction[]): DavDeadPropInstruction[] { |
| 57 | return instructions.map((instruction) => |
| 58 | instruction.kind === "set" |
| 59 | ? { kind: "set", ...instruction.prop, xmlValue: instruction.xmlValue } |
| 60 | : { kind: "remove", ...instruction.prop }, |
| 61 | ); |
| 62 | } |