export async function applyPostResponseActions()

in src/policy.ts [239:278]


export async function applyPostResponseActions(
  context: RecaptchaContext,
  resp: EdgeResponse,
  actions: action.Action[],
): Promise<EdgeResponse> {
  const respNonterminalActions: action.ResponseNonTerminalAction[] = [];
  for (const action of actions) {
    if (isTerminalAction(action) || isRequestNonTerminalAction(action)) {
      context.log("debug", "Applying response actions, ignoring request action");
    } else if (isResponseNonTerminalAction(action)) {
      respNonterminalActions.push(action);
    } else {
      /* v8 ignore next */
      throw new Error("Unsupported action: " + action);
    }
  }

  // Handle Post-Response actions.
  let modifiedResp = resp;
  const once = new Set<string>();
  for (const action of respNonterminalActions) {
    if (isInjectJsAction(action)) {
      if (once.has("injectjs")) {
        continue; // TODO: should this throw an error?
      }
      once.add("injectjs");
      context.log("debug", "respNonterminal action: injectjs");
      context.log_performance_debug("[func] injectJS - start");
      modifiedResp = await context.injectRecaptchaJs(resp);
      // If 'debug' is enabled, await the response to get reasonable performance metrics.
      if (context.config.debug) {
        modifiedResp = await Promise.resolve(resp);
      }
      context.log_performance_debug("[func] injectJS - end");
    } else {
      throw new Error("Unsupported post-response action: " + action);
    }
  }
  return modifiedResp;
}