-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
env.ts
88 lines (76 loc) · 2.2 KB
/
env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Inlined from https://github.com/flexdinesh/browser-or-node
declare global {
const Deno:
| {
version: {
deno: string;
};
}
| undefined;
}
export const isBrowser = () =>
typeof window !== "undefined" && typeof window.document !== "undefined";
export const isWebWorker = () =>
typeof globalThis === "object" &&
globalThis.constructor &&
globalThis.constructor.name === "DedicatedWorkerGlobalScope";
export const isJsDom = () =>
(typeof window !== "undefined" && window.name === "nodejs") ||
(typeof navigator !== "undefined" &&
(navigator.userAgent.includes("Node.js") ||
navigator.userAgent.includes("jsdom")));
// Supabase Edge Function provides a `Deno` global object
// without `version` property
export const isDeno = () => typeof Deno !== "undefined";
// Mark not-as-node if in Supabase Edge Function
export const isNode = () =>
typeof process !== "undefined" &&
typeof process.versions !== "undefined" &&
typeof process.versions.node !== "undefined" &&
!isDeno();
export const getEnv = () => {
let env: string;
if (isBrowser()) {
env = "browser";
} else if (isNode()) {
env = "node";
} else if (isWebWorker()) {
env = "webworker";
} else if (isJsDom()) {
env = "jsdom";
} else if (isDeno()) {
env = "deno";
} else {
env = "other";
}
return env;
};
export type RuntimeEnvironment = {
library: string;
libraryVersion?: string;
runtime: string;
runtimeVersion?: string;
};
let runtimeEnvironment: RuntimeEnvironment | undefined;
export async function getRuntimeEnvironment(): Promise<RuntimeEnvironment> {
if (runtimeEnvironment === undefined) {
const env = getEnv();
runtimeEnvironment = {
library: "langchain-js",
runtime: env,
};
}
return runtimeEnvironment;
}
export function getEnvironmentVariable(name: string): string | undefined {
// Certain Deno setups will throw an error if you try to access environment variables
// https://github.com/langchain-ai/langchainjs/issues/1412
try {
return typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env?.[name]
: undefined;
} catch (e) {
return undefined;
}
}