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: Add crypto-js for the case of missing crypto.subtle in the browser #3267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion clients/js/examples/browser/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useCallback, useEffect, useState } from "react";
import { ChromaClient } from "../../src/ChromaClient";
import { Collection } from "../../src/types";
import CryptoJS from "crypto-js";

const SAMPLE_DOCUMENTS = [
"apple",
Expand All @@ -14,7 +15,18 @@ const SAMPLE_DOCUMENTS = [
const hashString = async (message: string) => {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
let hashBuffer;
if (crypto.subtle) {
hashBuffer = await crypto.subtle.digest("SHA-256", data);
} else {
const hash = CryptoJS.SHA256(CryptoJS.enc.Utf8.parse(message));
hashBuffer = new Uint8Array(hash.words.map(word => [
(word >> 24) & 0xff,
(word >> 16) & 0xff,
(word >> 8) & 0xff,
word & 0xff
]).flat());
}
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
};
Expand Down
1 change: 1 addition & 0 deletions clients/js/examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"chromadb": "link:../..",
"crypto-js": "^4.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions clients/js/examples/browser/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
server: {
host: "0.0.0.0",
port: 3000,
},
// This manual remapping is only needed because we're loading a locally linked version of the JS client
Expand Down