Skip to content

Commit

Permalink
fix: edge runtime support for crypto (#184)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Improved environment detection to enhance functionality across different platforms.

- **Bug Fixes**
  - Updated `digest` function to simplify its usage and improve reliability.

- **Chores**
  - Adjusted code coverage workflow to ensure proper token usage for public repositories.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
<!-- GITHUB_RELEASE PR BODY: canary-version -->
<details>
  <summary>📦 Published PR as canary version: <code>Canary Versions</code></summary>
  <br />

  ✨ Test out this PR locally via:
  
  ```bash
  npm install @dotinc/[email protected]
  npm install @dotinc/[email protected]
  # or 
  yarn add @dotinc/[email protected]
  yarn add @dotinc/[email protected]
  ```
</details>
<!-- GITHUB_RELEASE PR BODY: canary-version -->
  • Loading branch information
nadilas authored May 31, 2024
2 parents fbc5fb7 + 30800c7 commit 4ac334d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
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 }}
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> {
// 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()) {
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

0 comments on commit 4ac334d

Please sign in to comment.