{"version":3,"file":"checkRootContainer.mjs","names":[],"sources":["../../../src/requester/requests/checkRootContainer.ts"],"sourcesContent":["import type { BasicRequestOptions } from \"./requestOptions\";\nimport LinkHeader from \"http-link-header\";\nimport { CheckRootContainerSuccess } from \"../results/success/CheckRootContainerSuccess\";\nimport type {\n  HttpErrorResultType,\n  UnexpectedHttpError,\n} from \"../results/error/HttpErrorResult\";\nimport { HttpErrorResult } from \"../results/error/HttpErrorResult\";\nimport { UnexpectedResourceError } from \"@ldo/connected\";\nimport type { SolidContainer } from \"../../resources/SolidContainer\";\nimport { guaranteeFetch } from \"../../util/guaranteeFetch\";\n\n/**\n * checkRootContainer result\n */\nexport type CheckRootResult = CheckRootContainerSuccess | CheckRootResultError;\n\n/**\n * All possible errors checkRootResult can return\n */\nexport type CheckRootResultError =\n  | HttpErrorResultType<SolidContainer>\n  | UnexpectedHttpError<SolidContainer>\n  | UnexpectedResourceError<SolidContainer>;\n\n/**\n * @internal\n * Checks provided headers to see if a given URI is a root container as defined\n * in the [solid specification section 4.1](https://solidproject.org/TR/protocol#storage-resource)\n *\n * @param uri - the URI of the container resource\n * @param headers - headers returned when making a GET request to the resource\n * @returns CheckRootContainerSuccess if there is not error\n */\nexport function checkHeadersForRootContainer(\n  resource: SolidContainer,\n  headers: Headers,\n): CheckRootContainerSuccess {\n  const linkHeader = headers.get(\"link\");\n  if (!linkHeader) {\n    return new CheckRootContainerSuccess(resource, false);\n  }\n  const parsedLinkHeader = LinkHeader.parse(linkHeader);\n  const types = parsedLinkHeader.get(\"rel\", \"type\");\n  const isRootContainer = types.some(\n    (type) => type.uri === \"http://www.w3.org/ns/pim/space#Storage\",\n  );\n  return new CheckRootContainerSuccess(resource, isRootContainer);\n}\n\n/**\n * @internal\n *\n * Performs a request to the Pod to check if the given URI is a root container\n * as defined in the [solid specification section 4.1](https://solidproject.org/TR/protocol#storage-resource)\n *\n * @param resource - the the container resource\n * @param options - options variable to pass a fetch function\n * @returns CheckResourceSuccess if there is no error\n */\nexport async function checkRootContainer(\n  resource: SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<CheckRootResult> {\n  try {\n    const fetch = guaranteeFetch(options?.fetch);\n    // Fetch options to determine the document type\n    // Note cache: \"no-store\": we don't want to depend on cached results because\n    // web browsers do not cache link headers\n    // https://github.com/CommunitySolidServer/CommunitySolidServer/issues/1959\n    const response = await fetch(resource.uri, {\n      method: \"HEAD\",\n      cache: \"no-store\",\n    });\n    const httpErrorResult = HttpErrorResult.checkResponse(resource, response);\n    if (httpErrorResult) return httpErrorResult;\n\n    return checkHeadersForRootContainer(resource, response.headers);\n  } catch (err) {\n    return UnexpectedResourceError.fromThrown(resource, err);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkCA,SAAgB,6BACd,UACA,SAC2B;CAC3B,MAAM,aAAa,QAAQ,IAAI,OAAO;AACtC,KAAI,CAAC,WACH,QAAO,IAAI,0BAA0B,UAAU,MAAM;AAOvD,QAAO,IAAI,0BAA0B,UALZ,WAAW,MAAM,WACZ,CAAC,IAAI,OAAO,OACb,CAAC,MAC3B,SAAS,KAAK,QAAQ,yCAEqC,CAAC;;;;;;;;;;;;AAajE,eAAsB,mBACpB,UACA,SAC0B;AAC1B,KAAI;EAMF,MAAM,WAAW,MALH,eAAe,SAAS,MAKV,CAAC,SAAS,KAAK;GACzC,QAAQ;GACR,OAAO;GACR,CAAC;EACF,MAAM,kBAAkB,gBAAgB,cAAc,UAAU,SAAS;AACzE,MAAI,gBAAiB,QAAO;AAE5B,SAAO,6BAA6B,UAAU,SAAS,QAAQ;UACxD,KAAK;AACZ,SAAO,wBAAwB,WAAW,UAAU,IAAI"}