{"version":3,"sources":["../../../../src/shared/lib/server-reference-info.ts"],"sourcesContent":["export interface ServerReferenceInfo {\n  type: 'server-action' | 'use-cache'\n  usedArgs: [boolean, boolean, boolean, boolean, boolean, boolean]\n  hasRestArgs: boolean\n}\n\nexport const SERVER_REFERENCE_ID_LENGTH = 42\n\nexport function mightBeServerReferenceId(id: string): boolean {\n  return id.length === SERVER_REFERENCE_ID_LENGTH\n}\n\n/**\n * Extracts info about the server reference for the given server reference ID by\n * parsing the first byte of the hex-encoded ID.\n *\n * ```\n * Bit positions: [7]      [6] [5] [4] [3] [2] [1]  [0]\n * Bits:          typeBit  argMask                  restArgs\n * ```\n *\n * If the `typeBit` is `1` the server reference represents a `\"use cache\"`\n * function, otherwise a server action.\n *\n * The `argMask` encodes whether the function uses the argument at the\n * respective position.\n *\n * The `restArgs` bit indicates whether the function uses a rest parameter. It's\n * also set to 1 if the function has more than 6 args.\n *\n * @param id hex-encoded server reference ID\n */\nexport function extractInfoFromServerReferenceId(\n  id: string\n): ServerReferenceInfo {\n  const infoByte = parseInt(id.slice(0, 2), 16)\n  const typeBit = (infoByte >> 7) & 0x1\n  const argMask = (infoByte >> 1) & 0x3f\n  const restArgs = infoByte & 0x1\n  const usedArgs = Array(6)\n\n  for (let index = 0; index < 6; index++) {\n    const bitPosition = 5 - index\n    const bit = (argMask >> bitPosition) & 0x1\n    usedArgs[index] = bit === 1\n  }\n\n  return {\n    type: typeBit === 1 ? 'use-cache' : 'server-action',\n    usedArgs: usedArgs as [\n      boolean,\n      boolean,\n      boolean,\n      boolean,\n      boolean,\n      boolean,\n    ],\n    hasRestArgs: restArgs === 1,\n  }\n}\n\n/**\n * Creates a sparse array containing only the used arguments based on the\n * provided action info.\n */\nexport function omitUnusedArgs(\n  args: unknown[],\n  info: ServerReferenceInfo\n): unknown[] {\n  const filteredArgs = new Array(args.length)\n  let length = 0\n\n  for (let index = 0; index < args.length; index++) {\n    if (\n      (index < 6 && info.usedArgs[index]) ||\n      // This assumes that the server reference info byte has the restArgs bit\n      // set to 1 if there are more than 6 args.\n      (index >= 6 && info.hasRestArgs)\n    ) {\n      filteredArgs[index] = args[index]\n      length = index + 1\n    }\n  }\n\n  // Trim trailing unused args from the array.\n  filteredArgs.length = length\n\n  return filteredArgs\n}\n"],"names":["SERVER_REFERENCE_ID_LENGTH","mightBeServerReferenceId","id","length","extractInfoFromServerReferenceId","infoByte","parseInt","slice","typeBit","argMask","restArgs","usedArgs","Array","index","bitPosition","bit","type","hasRestArgs","omitUnusedArgs","args","info","filteredArgs"],"mappings":"AAMA,OAAO,MAAMA,6BAA6B,GAAE;AAE5C,OAAO,SAASC,yBAAyBC,EAAU;IACjD,OAAOA,GAAGC,MAAM,KAAKH;AACvB;AAEA;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,SAASI,iCACdF,EAAU;IAEV,MAAMG,WAAWC,SAASJ,GAAGK,KAAK,CAAC,GAAG,IAAI;IAC1C,MAAMC,UAAU,AAACH,YAAY,IAAK;IAClC,MAAMI,UAAU,AAACJ,YAAY,IAAK;IAClC,MAAMK,WAAWL,WAAW;IAC5B,MAAMM,WAAWC,MAAM;IAEvB,IAAK,IAAIC,QAAQ,GAAGA,QAAQ,GAAGA,QAAS;QACtC,MAAMC,cAAc,IAAID;QACxB,MAAME,MAAM,AAACN,WAAWK,cAAe;QACvCH,QAAQ,CAACE,MAAM,GAAGE,QAAQ;IAC5B;IAEA,OAAO;QACLC,MAAMR,YAAY,IAAI,cAAc;QACpCG,UAAUA;QAQVM,aAAaP,aAAa;IAC5B;AACF;AAEA;;;CAGC,GACD,OAAO,SAASQ,eACdC,IAAe,EACfC,IAAyB;IAEzB,MAAMC,eAAe,IAAIT,MAAMO,KAAKhB,MAAM;IAC1C,IAAIA,SAAS;IAEb,IAAK,IAAIU,QAAQ,GAAGA,QAAQM,KAAKhB,MAAM,EAAEU,QAAS;QAChD,IACE,AAACA,QAAQ,KAAKO,KAAKT,QAAQ,CAACE,MAAM,IAClC,wEAAwE;QACxE,0CAA0C;QACzCA,SAAS,KAAKO,KAAKH,WAAW,EAC/B;YACAI,YAAY,CAACR,MAAM,GAAGM,IAAI,CAACN,MAAM;YACjCV,SAASU,QAAQ;QACnB;IACF;IAEA,4CAA4C;IAC5CQ,aAAalB,MAAM,GAAGA;IAEtB,OAAOkB;AACT","ignoreList":[0]}