Skip to content

Commit

Permalink
fix: browser detection for edge runtime support for crypto package
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed May 31, 2024
1 parent fbc5fb7 commit ee1d091
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 4 additions & 6 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> {
// eslint-disable-line
const algorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
if (!algorithms.includes(algorithm)) {
Expand All @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/ogre/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down

0 comments on commit ee1d091

Please sign in to comment.