diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/misc.js | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/common/misc.js b/common/misc.js index e609fe0..8096f27 100644 --- a/common/misc.js +++ b/common/misc.js @@ -42,9 +42,13 @@ * proprietary program, I am not going to enforce this in court. */ -/* uint8_to_hex is a separate function used in cryptographic functions. */ +/* + * uint8_to_hex is a separate function used in cryptographic functions and when + * dealing with binary data. + */ const uint8_to_hex = array => [...array].map(b => ("0" + b.toString(16)).slice(-2)).join(""); +#EXPORT uint8_to_hex /* * Asynchronously compute hex string representation of a sha256 digest of a @@ -61,8 +65,7 @@ async function sha256_async(string) { * Generate a unique value that can be computed synchronously and is impossible * to guess for a malicious website. */ -function gen_nonce(length=16) -{ +function gen_nonce(length=16) { const random_data = new Uint8Array(length); crypto.getRandomValues(random_data); return uint8_to_hex(random_data); @@ -71,16 +74,10 @@ function gen_nonce(length=16) /* Check if some HTTP header might define CSP rules. */ const csp_header_regex = - /^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)/i; + /^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)\s*$/i; #EXPORT csp_header_regex /* - * Print item together with type, e.g. - * nice_name("s", "hello") → "hello (script)" - */ -#EXPORT (prefix, name) => `${name} (${TYPE_NAME[prefix]})` AS nice_name - -/* * Check if url corresponds to a browser's special page (or a directory index in * case of `file://' protocol). */ @@ -90,3 +87,13 @@ const priv_reg = /^moz-extension:\/\/|^about:|^view-source:|^file:\/\/[^?#]*\/([ const priv_reg = /^chrome(-extension)?:\/\/|^about:|^view-source:|^file:\/\/[^?#]*\/([?#]|$)/; #ENDIF #EXPORT url => priv_reg.test(url) AS is_privileged_url + +/* Make it possible to serialize en Error object. */ +function error_data_jsonifiable(error) { + const jsonifiable = {}; + for (const property of ["name", "message", "fileName", "lineNumber"]) + jsonifiable[property] = error[property]; + + return jsonifiable; +} +#EXPORT error_data_jsonifiable |