diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index ac8a509..ad6a5c4 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -17,6 +17,6 @@ jobs: - uses: codecov/codecov-action@v3 with: - # token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos + token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: true # optional (default = false) verbose: true # optional (default = false) diff --git a/packages/ogre/src/hash.ts b/packages/ogre/src/hash.ts index 8a9b0d4..e4eea0d 100644 --- a/packages/ogre/src/hash.ts +++ b/packages/ogre/src/hash.ts @@ -7,6 +7,8 @@ * @packageDocumentation */ +import { isBrowser } from "./utils.js"; + /** * Returns a string with a hexadecimal representation of the digest of the input object using a given hash algorithm. * It first creates an array of the object values ordered by the object keys (using hashable(obj)); @@ -21,11 +23,7 @@ * * @returns a promise that resolves to a string with hexadecimal content. */ -export function digest( - obj: any, - algorithm = "SHA-256", - isBrowser = false, -): Promise { +export function digest(obj: any, algorithm = "SHA-256"): Promise { // eslint-disable-line const algorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"]; if (!algorithms.includes(algorithm)) { @@ -38,12 +36,12 @@ export function digest( const hashInput = encoder.encode(hashable(obj)).buffer; let digest = ""; - if (isBrowser) { + if (isBrowser()) { const buf = await crypto.subtle.digest(algorithm, hashInput); const h = "0123456789abcdef"; - new Uint8Array(buf).forEach((v) => { + for (const v of new Uint8Array(buf)) { digest += h[v >> 4] + h[v & 15]; - }); + } } else { const nodeAlg = algorithm.toLowerCase().replace("-", ""); digest = (await import("crypto")) diff --git a/packages/ogre/src/utils.ts b/packages/ogre/src/utils.ts index 9aecd71..7e629d2 100644 --- a/packages/ogre/src/utils.ts +++ b/packages/ogre/src/utils.ts @@ -9,6 +9,9 @@ import { RepositoryObject } from "./repository.js"; const emailRegex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/; +export const isBrowser = () => + typeof window !== "undefined" && typeof window.document !== "undefined"; + export const cleanAuthor = (author: string): [name: string, email: string] => { if (author === "") { throw new Error(`author not provided`);