From b7d6cc5b1c776d3ade409fc653edce0abebcd391 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Fri, 7 May 2021 09:59:45 -0400
Subject: [PATCH] FIX: `crypto.randomUUID` is only available in newer node
 releases (#12983)

Also, we should properly surface the error instead of saying it was
`bootstrap.json`
---
 .../discourse/lib/bootstrap-json/index.js      | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
index 9de67ea89e9..0e8b503a223 100644
--- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
+++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
@@ -6,7 +6,15 @@ const { encode } = require("html-entities");
 const cleanBaseURL = require("clean-base-url");
 const path = require("path");
 const fs = require("fs");
-const crypto = require("crypto");
+
+// via https://stackoverflow.com/a/6248722/165668
+function generateUID() {
+  let firstPart = (Math.random() * 46656) | 0; // eslint-disable-line no-bitwise
+  let secondPart = (Math.random() * 46656) | 0; // eslint-disable-line no-bitwise
+  firstPart = ("000" + firstPart.toString(36)).slice(-3);
+  secondPart = ("000" + secondPart.toString(36)).slice(-3);
+  return firstPart + secondPart;
+}
 
 const IGNORE_PATHS = [
   /\/ember-cli-live-reload\.js$/,
@@ -94,7 +102,7 @@ function bodyFooter(buffer, bootstrap, headers) {
   buffer.push(bootstrap.theme_html.body_tag);
   buffer.push(bootstrap.html.before_body_close);
 
-  let v = crypto.randomUUID();
+  let v = generateUID();
   buffer.push(`
 		<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=${v}" data-css-url="/mini-profiler-resources/includes.css?v=${v}" data-version="${v}" data-path="/mini-profiler-resources/" data-horizontal-position="left" data-vertical-position="top" data-trivial="false" data-children="false" data-max-traces="20" data-controls="false" data-total-sql-count="false" data-authorized="true" data-toggle-shortcut="alt+p" data-start-hidden="false" data-collapse-results="true" data-html-container="body" data-hidden-custom-fields="x" data-ids="${headers["x-miniprofiler-ids"]}"></script>
 	`);
@@ -158,8 +166,8 @@ function buildFromBootstrap(assetPath, proxy, req, headers) {
           .then((json) => {
             resolve(applyBootstrap(json.bootstrap, template, headers));
           })
-          .catch(() => {
-            reject(`Could not get ${proxy}/bootstrap.json`);
+          .catch((e) => {
+            reject(`Could not get ${proxy}/bootstrap.json\n\n${e.toString()}`);
           });
       }
     );
@@ -204,7 +212,7 @@ async function handleRequest(assetPath, proxy, req, res) {
       res.send(`
                 <html>
                   <h1>Discourse Build Error</h1>
-                  <p>${e.toString()}</p>
+                  <pre><code>${e.toString()}</code></pre>
                 </html>
               `);
     }