Skip to content

Commit

Permalink
fix(media): edge runtime support (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp authored Nov 26, 2024
1 parent a904428 commit 4aec74e
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions langfuse-core/src/media/LangfuseMedia.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
let fs: any;
let crypto: any;

// Fail gracefully in environments without fs/crypto support
try {
fs = require("fs");
crypto = require("crypto");
} catch {
fs = undefined;
crypto = undefined;
let fs: any = null;
let cryptoModule: any = null;

if (typeof process !== "undefined" && process.versions?.node) {
// Node
try {
fs = require("fs");
cryptoModule = require("crypto");
} catch (error) {
console.error("Error loading crypto or fs module", error);
}
} else if (typeof crypto !== "undefined") {
// Edge Runtime, Cloudflare Workers, etc.
cryptoModule = crypto;
}

try {
crypto = require("crypto");
} catch {
// Fail gracefully in environments without fs/crypto support
crypto = undefined;
}
import { type MediaContentType } from "../types";

interface ParsedMediaReference {
Expand Down Expand Up @@ -130,12 +128,12 @@ class LangfuseMedia {
return undefined;
}

if (!crypto) {
if (!cryptoModule) {
console.error("Crypto support is not available in this environment");
return undefined;
}

const sha256Hash = crypto.createHash("sha256").update(this._contentBytes).digest("base64");
const sha256Hash = cryptoModule.createHash("sha256").update(this._contentBytes).digest("base64");
return sha256Hash;
}

Expand Down

0 comments on commit 4aec74e

Please sign in to comment.