{"version":3,"file":"setWacRule.mjs","names":[],"sources":["../../src/wac/setWacRule.ts"],"sourcesContent":["import { createLdoDataset } from \"@ldo/ldo\";\nimport type { AccessModeList, WacRule } from \"./WacRule\";\nimport { SetWacRuleSuccess } from \"./results/SetWacRuleSuccess\";\nimport type { Authorization } from \"../_ldo/wac.typings\";\nimport { AuthorizationShapeType } from \"../_ldo/wac.shapeTypes\";\nimport { v4 } from \"uuid\";\nimport { guaranteeFetch } from \"../util/guaranteeFetch\";\nimport type { SolidLeafUri } from \"../types\";\nimport type { SolidLeaf } from \"../resources/SolidLeaf\";\nimport type { SolidContainer } from \"../resources/SolidContainer\";\nimport {\n  HttpErrorResult,\n  type HttpErrorResultType,\n} from \"../requester/results/error/HttpErrorResult\";\nimport type { UnexpectedResourceError } from \"@ldo/connected\";\nimport type { BasicRequestOptions } from \"../requester/requests/requestOptions\";\nimport { isSolidContainerUri } from \"../util/isSolidUri\";\n\nexport type SetWacRuleError<ResourceType extends SolidContainer | SolidLeaf> =\n  | HttpErrorResultType<ResourceType>\n  | UnexpectedResourceError<ResourceType>;\nexport type SetWacRuleResult<ResourceType extends SolidContainer | SolidLeaf> =\n  | SetWacRuleSuccess<ResourceType>\n  | SetWacRuleError<ResourceType>;\n\n/**\n * Given the URI of an ACL document and some WAC rules, set the WAC rules of\n * that document\n * @param aclUri: The URI for the ACL document\n * @param newRule: A new WAC rule to set. This will overwrite old rules\n * @param accessTo: The document this rule refers to\n * @param options: Options object to include an authenticated fetch function\n * @returns SetWacRuleResult\n */\nexport async function setWacRuleForAclUri(\n  aclUri: SolidLeafUri,\n  newRule: WacRule,\n  resource: SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<SetWacRuleResult<SolidContainer>>;\nexport async function setWacRuleForAclUri(\n  aclUri: SolidLeafUri,\n  newRule: WacRule,\n  resource: SolidLeaf,\n  options?: BasicRequestOptions,\n): Promise<SetWacRuleResult<SolidLeaf>>;\nexport async function setWacRuleForAclUri(\n  aclUri: SolidLeafUri,\n  newRule: WacRule,\n  resource: SolidContainer | SolidLeaf,\n  options?: BasicRequestOptions,\n): Promise<SetWacRuleResult<SolidContainer | SolidLeaf>>;\nexport async function setWacRuleForAclUri(\n  aclUri: SolidLeafUri,\n  newRule: WacRule,\n  resource: SolidContainer | SolidLeaf,\n  options?: BasicRequestOptions,\n): Promise<SetWacRuleResult<SolidContainer | SolidLeaf>> {\n  const fetch = guaranteeFetch(options?.fetch);\n  // The rule map keeps track of all the rules that are currently being used\n  // so that similar rules can be grouped together\n  const ruleMap: Record<string, Authorization> = {};\n  // The dataset that will eventually be sent to the Pod\n  const dataset = createLdoDataset();\n\n  // Helper function to add rules to the dataset by grouping them in the ruleMap\n  function addRuleToDataset(\n    type: \"public\" | \"authenticated\" | \"agent\",\n    accessModeList: AccessModeList,\n    agentId?: string,\n  ) {\n    const accessModeListHash = hashAccessModeList(accessModeList);\n    // No need to add if all access is false\n    if (accessModeListHash === \"\") return;\n    if (!ruleMap[accessModeListHash]) {\n      const authorization = dataset\n        .usingType(AuthorizationShapeType)\n        .fromSubject(`${aclUri}#${v4()}`);\n      authorization.type = { \"@id\": \"Authorization\" };\n      if (accessModeList.read) authorization.mode?.add({ \"@id\": \"Read\" });\n      if (accessModeList.write) authorization.mode?.add({ \"@id\": \"Write\" });\n      if (accessModeList.append) authorization.mode?.add({ \"@id\": \"Append\" });\n      if (accessModeList.control) authorization.mode?.add({ \"@id\": \"Control\" });\n      authorization.accessTo = { \"@id\": resource.uri };\n      if (isSolidContainerUri(resource.uri)) {\n        authorization.default = { \"@id\": resource.uri };\n      }\n      ruleMap[accessModeListHash] = authorization;\n    }\n    const authorization = ruleMap[accessModeListHash];\n    // Add agents to the rule\n    if (type === \"public\") {\n      authorization.agentClass?.add({ \"@id\": \"Agent\" });\n    } else if (type === \"authenticated\") {\n      authorization.agentClass?.add({ \"@id\": \"AuthenticatedAgent\" });\n    } else if (type === \"agent\" && agentId) {\n      authorization.agent?.add({ \"@id\": agentId });\n    }\n  }\n\n  // Add each rule to the dataset\n  addRuleToDataset(\"public\", newRule.public);\n  addRuleToDataset(\"authenticated\", newRule.authenticated);\n  Object.entries(newRule.agent).forEach(([agentUri, accessModeList]) => {\n    addRuleToDataset(\"agent\", accessModeList, agentUri);\n  });\n\n  // Save to Pod\n  const response = await fetch(aclUri, {\n    method: \"PUT\",\n    headers: {\n      \"content-type\": \"text/turtle\",\n    },\n    body: dataset.toString(),\n  });\n  const errorResult = HttpErrorResult.checkResponse(resource, response);\n  if (errorResult) return errorResult;\n\n  return new SetWacRuleSuccess(resource, newRule);\n}\n\n// Hashes the access mode list for use in the rule map\nfunction hashAccessModeList(list: AccessModeList): string {\n  return Object.entries(list).reduce(\n    (agg, [key, isPresent]) => (isPresent ? agg + key : agg),\n    \"\",\n  );\n}\n"],"mappings":";;;;;;;;AAoDA,eAAsB,oBACpB,QACA,SACA,UACA,SACuD;CACvD,MAAM,QAAQ,eAAe,SAAS,MAAM;CAG5C,MAAM,UAAyC,EAAE;CAEjD,MAAM,UAAU,kBAAkB;CAGlC,SAAS,iBACP,MACA,gBACA,SACA;EACA,MAAM,qBAAqB,mBAAmB,eAAe;AAE7D,MAAI,uBAAuB,GAAI;AAC/B,MAAI,CAAC,QAAQ,qBAAqB;GAChC,MAAM,gBAAgB,QACnB,UAAU,uBAAuB,CACjC,YAAY,GAAG,OAAO,GAAG,IAAI,GAAG;AACnC,iBAAc,OAAO,EAAE,OAAO,iBAAiB;AAC/C,OAAI,eAAe,KAAM,eAAc,MAAM,IAAI,EAAE,OAAO,QAAQ,CAAC;AACnE,OAAI,eAAe,MAAO,eAAc,MAAM,IAAI,EAAE,OAAO,SAAS,CAAC;AACrE,OAAI,eAAe,OAAQ,eAAc,MAAM,IAAI,EAAE,OAAO,UAAU,CAAC;AACvE,OAAI,eAAe,QAAS,eAAc,MAAM,IAAI,EAAE,OAAO,WAAW,CAAC;AACzE,iBAAc,WAAW,EAAE,OAAO,SAAS,KAAK;AAChD,OAAI,oBAAoB,SAAS,I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