Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: edge runtime support for crypto #184

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:

- uses: codecov/codecov-action@v3
with:
# token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
token: ${{ secrets.CODECOV_TOKEN }}
nadilas marked this conversation as resolved.
Show resolved Hide resolved
fail_ci_if_error: true # optional (default = false)
verbose: true # optional (default = false)
14 changes: 6 additions & 8 deletions packages/ogre/src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<string> {
export function digest(obj: any, algorithm = "SHA-256"): Promise<string> {
nadilas marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-line
const algorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
if (!algorithms.includes(algorithm)) {
Expand All @@ -38,12 +36,12 @@ export function digest(
const hashInput = encoder.encode(hashable(obj)).buffer;
let digest = "";

if (isBrowser) {
if (isBrowser()) {
nadilas marked this conversation as resolved.
Show resolved Hide resolved
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"))
Expand Down
3 changes: 3 additions & 0 deletions packages/ogre/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
Loading