From ee1d091132e47cbe80f1b67d258ab986cf82c724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 31 May 2024 13:12:48 +0200 Subject: [PATCH 1/5] fix: browser detection for edge runtime support for crypto package --- packages/ogre/src/hash.ts | 10 ++++------ packages/ogre/src/utils.ts | 2 ++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/ogre/src/hash.ts b/packages/ogre/src/hash.ts index 8a9b0d4..4caa984 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,7 +36,7 @@ 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) => { diff --git a/packages/ogre/src/utils.ts b/packages/ogre/src/utils.ts index 9aecd71..a5c618c 100644 --- a/packages/ogre/src/utils.ts +++ b/packages/ogre/src/utils.ts @@ -9,6 +9,8 @@ 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`); From 5174cb49cc0ed3dfdecaafaca08401b1df9b8d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 31 May 2024 13:24:09 +0200 Subject: [PATCH 2/5] fix: typeof string literal in isBrowser check --- packages/ogre/src/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ogre/src/utils.ts b/packages/ogre/src/utils.ts index a5c618c..7e629d2 100644 --- a/packages/ogre/src/utils.ts +++ b/packages/ogre/src/utils.ts @@ -10,7 +10,8 @@ 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; + typeof window !== "undefined" && typeof window.document !== "undefined"; + export const cleanAuthor = (author: string): [name: string, email: string] => { if (author === "") { throw new Error(`author not provided`); From 1132c9f1ccaf940a4ce7a5aefe83f1f4e683469a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 31 May 2024 13:24:17 +0200 Subject: [PATCH 3/5] refactor: code clarity --- packages/ogre/src/hash.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ogre/src/hash.ts b/packages/ogre/src/hash.ts index 4caa984..ebe2fe0 100644 --- a/packages/ogre/src/hash.ts +++ b/packages/ogre/src/hash.ts @@ -39,12 +39,12 @@ export function digest(obj: any, algorithm = "SHA-256"): Promise { 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")) + digest = (await import("node:crypto")) .createHash(nodeAlg) .update(Buffer.from(hashInput)) .digest("hex"); // eslint-disable-line From 23a317cf5f036a246a19d384aaad778f6b627d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 31 May 2024 13:24:33 +0200 Subject: [PATCH 4/5] chore: add codecov token even though it shouldn't be required --- .github/workflows/coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 30800c7cb7372bc0661e3281a81e61dbbddf4a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Veres?= Date: Fri, 31 May 2024 13:33:08 +0200 Subject: [PATCH 5/5] fix: Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins --- packages/ogre/src/hash.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ogre/src/hash.ts b/packages/ogre/src/hash.ts index ebe2fe0..e4eea0d 100644 --- a/packages/ogre/src/hash.ts +++ b/packages/ogre/src/hash.ts @@ -44,7 +44,7 @@ export function digest(obj: any, algorithm = "SHA-256"): Promise { } } else { const nodeAlg = algorithm.toLowerCase().replace("-", ""); - digest = (await import("node:crypto")) + digest = (await import("crypto")) .createHash(nodeAlg) .update(Buffer.from(hashInput)) .digest("hex"); // eslint-disable-line