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(media): no require in mjs modules #473

Merged
merged 9 commits into from
Nov 28, 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
28 changes: 14 additions & 14 deletions examples/example-node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.6.4:
version "1.6.5"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.5.tgz#2c090da14aeeab3770ad30c3a1461bc970fb0cd8"
integrity sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==
axios@^1.7.4:
version "1.7.8"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.8.tgz#1997b1496b394c21953e68c14aaa51b7b5de3d6e"
integrity sha512-Uu0wb7KNqK2t5K+YQyVCLM76prD5sRFjKHbJYCP1J7JFGEQ6nN7HWn9+04LAeiJ3ji54lgS/gZCH1oxyrf1SPw==
dependencies:
follow-redirects "^1.15.4"
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

Expand Down Expand Up @@ -608,10 +608,10 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.6.tgz#022e9218c637f9f3fc9c35ab9c9193f05add60b2"
integrity sha512-0sQoMh9s0BYsm+12Huy/rkKxVu4R1+r96YX5cG44rHV0pQ6iC3Q+mkoMFaGWObMFYQxCVT+ssG1ksneA2MI9KQ==

follow-redirects@^1.15.4:
version "1.15.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf"
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==
follow-redirects@^1.15.6:
version "1.15.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==

form-data@^4.0.0:
version "4.0.0"
Expand Down Expand Up @@ -835,16 +835,16 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

langfuse-core@^2.6.0, "langfuse-core@file:.yalc/langfuse-core":
version "2.6.0"
langfuse-core@^3.31.0, "langfuse-core@file:.yalc/langfuse-core":
version "3.31.0"
dependencies:
mustache "^4.2.0"

"langfuse-node@file:.yalc/langfuse-node":
version "2.6.0"
version "3.31.0"
dependencies:
axios "^1.6.4"
langfuse-core "^2.6.0"
axios "^1.7.4"
langfuse-core "^3.31.0"

levn@^0.4.1:
version "0.4.1"
Expand Down
38 changes: 19 additions & 19 deletions examples/example-web-nextjs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ axe-core@^4.6.2:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.7.2.tgz#040a7342b20765cb18bb50b628394c21bccc17a0"
integrity sha512-zIURGIS1E1Q4pcrMjp+nnEh+16G56eG/MUllJH8yEvw7asDo7Ac9uhC9KIH5jzpITueEZolfYglnCGIuSBz39g==

axios@^1.6.4:
version "1.6.5"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.5.tgz#2c090da14aeeab3770ad30c3a1461bc970fb0cd8"
integrity sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==
axios@^1.7.4:
version "1.7.8"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.8.tgz#1997b1496b394c21953e68c14aaa51b7b5de3d6e"
integrity sha512-Uu0wb7KNqK2t5K+YQyVCLM76prD5sRFjKHbJYCP1J7JFGEQ6nN7HWn9+04LAeiJ3ji54lgS/gZCH1oxyrf1SPw==
dependencies:
follow-redirects "^1.15.4"
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

Expand Down Expand Up @@ -1071,10 +1071,10 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.15.4:
version "1.15.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf"
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==
follow-redirects@^1.15.6:
version "1.15.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==

for-each@^0.3.3:
version "0.3.3"
Expand Down Expand Up @@ -1550,28 +1550,28 @@ jsonfile@^4.0.0:
object.assign "^4.1.4"
object.values "^1.1.6"

langfuse-core@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/langfuse-core/-/langfuse-core-2.6.0.tgz#acdf4233f89cb8206a028e477dddf6a3b6571ffd"
integrity sha512-HGesAV0bnZ10g1F109i0VYCqacCGMPIO3ijoPHSsKyZC83YL2Gqhn4HzOXDZI+gQUNQC1qu3bKl8DLbqOhmyOA==
langfuse-core@^3.31.0:
version "3.31.0"
resolved "https://registry.yarnpkg.com/langfuse-core/-/langfuse-core-3.31.0.tgz#2bffd0182c110fa80597d3bdc2b14d599d827946"
integrity sha512-aJ8dNp1Oh8e+XLJb5M+1lzluqi4oLBFxuSh56e8852WOifPs4Q3HPVblvf2YtyhpYqj0x+0cwhVP+iO2AAsh6Q==
dependencies:
mustache "^4.2.0"

"langfuse-core@file:.yalc/langfuse-core":
version "2.6.0"
version "3.31.0"
dependencies:
mustache "^4.2.0"

"langfuse-node@file:.yalc/langfuse-node":
version "2.6.0"
version "3.31.0"
dependencies:
axios "^1.6.4"
langfuse-core "^2.6.0"
axios "^1.7.4"
langfuse-core "^3.31.0"

"langfuse@file:.yalc/langfuse":
version "2.6.0"
version "3.31.0"
dependencies:
langfuse-core "^2.6.0"
langfuse-core "^3.31.0"

language-subtag-registry@~0.3.2:
version "0.3.22"
Expand Down
14 changes: 7 additions & 7 deletions examples/example-web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5789,22 +5789,22 @@ klona@^2.0.4, klona@^2.0.5:
resolved "https://registry.yarnpkg.com/klona/-/klona-2.0.5.tgz#d166574d90076395d9963aa7a928fabb8d76afbc"
integrity sha512-pJiBpiXMbt7dkzXe8Ghj/u4FfXOOa98fPW+bihOJ4SjnoijweJrNThJfd3ifXpXhREjpoF2mZVH1GfS9LV3kHQ==

langfuse-core@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/langfuse-core/-/langfuse-core-2.6.0.tgz#acdf4233f89cb8206a028e477dddf6a3b6571ffd"
integrity sha512-HGesAV0bnZ10g1F109i0VYCqacCGMPIO3ijoPHSsKyZC83YL2Gqhn4HzOXDZI+gQUNQC1qu3bKl8DLbqOhmyOA==
langfuse-core@^3.31.0:
version "3.31.0"
resolved "https://registry.yarnpkg.com/langfuse-core/-/langfuse-core-3.31.0.tgz#2bffd0182c110fa80597d3bdc2b14d599d827946"
integrity sha512-aJ8dNp1Oh8e+XLJb5M+1lzluqi4oLBFxuSh56e8852WOifPs4Q3HPVblvf2YtyhpYqj0x+0cwhVP+iO2AAsh6Q==
dependencies:
mustache "^4.2.0"

"langfuse-core@file:.yalc/langfuse-core":
version "2.6.0"
version "3.31.0"
dependencies:
mustache "^4.2.0"

"langfuse@file:.yalc/langfuse":
version "2.6.0"
version "3.31.0"
dependencies:
langfuse-core "^2.6.0"
langfuse-core "^3.31.0"

language-subtag-registry@~0.3.2:
version "0.3.22"
Expand Down
28 changes: 16 additions & 12 deletions langfuse-core/src/media/LangfuseMedia.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
let fs: any = null;
let cryptoModule: any = null;
let crypto: any = null;

if (typeof process !== "undefined" && process.versions?.node) {
// Use wrapper to prevent bundlers from trying to resolve the dynamic import
// Otherwise, the import will be incorrectly resolved as a static import even though it's dynamic
// Test for browser environment would fail because the import will be incorrectly resolved as a static import and fs and crypto will be unavailable
const dynamicImport = (module: string): Promise<any> => {
return import(/* webpackIgnore: true */ module);
};

// 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;
Promise.all([dynamicImport("fs"), dynamicImport("crypto")])
.then(([fsModule, cryptoModule]) => {
fs = fsModule;
crypto = cryptoModule;
})
.catch(); // Errors are handled on runtime
hassiebp marked this conversation as resolved.
Show resolved Hide resolved
}

import { type MediaContentType } from "../types";
Expand Down Expand Up @@ -128,12 +132,12 @@ class LangfuseMedia {
return undefined;
}

if (!cryptoModule) {
if (!crypto) {
console.error("Crypto support is not available in this environment");
return undefined;
}
hassiebp marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
6 changes: 3 additions & 3 deletions langfuse-node/test/langfuse-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ describe("Langfuse Node.js", () => {

// 10 capture calls to debug log
// 6 flush calls to debug log
expect(logSpy).toHaveBeenCalledTimes(16);
expect(logSpy.mock.calls.length).toBeGreaterThanOrEqual(15);
hassiebp marked this conversation as resolved.
Show resolved Hide resolved
expect(10).toEqual(logSpy.mock.calls.filter((call) => call[1].includes("trace-create")).length);
expect(6).toEqual(logSpy.mock.calls.filter((call) => call[1].includes("flush")).length);
expect(logSpy.mock.calls.filter((call) => call[1].includes("flush")).length).toBeGreaterThanOrEqual(5);

logSpy.mockClear();

await langfuse.shutdownAsync();
// remaining 4 flush calls to debug log
// happen during shutdown
expect(4).toEqual(logSpy.mock.calls.filter((call) => call[1].includes("flush")).length);
expect(logSpy.mock.calls.filter((call) => call[1].includes("flush")).length).toBeGreaterThanOrEqual(4);
jest.useFakeTimers();
logSpy.mockRestore();
});
Expand Down
Loading