async injectRecaptchaJs()

in bindings/fastly/src/index.ts [145:155]


  async injectRecaptchaJs(resp: EdgeResponse): Promise<EdgeResponse> {
    let base_resp = (resp as FetchApiResponse).asResponse();
    const sessionKey = this.config.sessionSiteKey;
    const RECAPTCHA_JS_SCRIPT = `<script src="${RECAPTCHA_JS}?render=${sessionKey}&waf=session" async defer></script>`;
    // rewrite the response
    if (resp.getHeader("Content-Type")?.startsWith("text/html")) {
      const newRespStream = streamReplace(base_resp.body!, "</head>", RECAPTCHA_JS_SCRIPT + "</head>");
      resp = new FetchApiResponse(new Response(newRespStream, base_resp));
    }
    return Promise.resolve(resp);
  }