2023-08-21 06:22:37 +00:00
|
|
|
import type { Context } from '../../context.ts'
|
2023-03-06 12:12:22 +00:00
|
|
|
|
2023-03-31 09:17:45 +00:00
|
|
|
export const env = <T extends Record<string, string>, C extends Context = Context<{}>>(
|
|
|
|
c: C
|
|
|
|
): T & C['env'] => {
|
2023-03-06 12:12:22 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const global = globalThis as any
|
|
|
|
|
|
|
|
if (
|
|
|
|
c.runtime === 'bun' ||
|
|
|
|
c.runtime === 'node' ||
|
|
|
|
c.runtime === 'edge-light' ||
|
|
|
|
c.runtime === 'lagon'
|
|
|
|
) {
|
|
|
|
return global?.process?.env as T
|
|
|
|
}
|
|
|
|
if (c.runtime === 'deno') {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
return Deno.env.toObject()
|
|
|
|
}
|
|
|
|
if (c.runtime === 'workerd') {
|
|
|
|
return c.env
|
|
|
|
}
|
2023-05-02 07:26:39 +00:00
|
|
|
if (c.runtime === 'fastly') {
|
|
|
|
// On Fastly Compute@Edge, you can use the ConfigStore to manage user-defined data.
|
|
|
|
return {} as T
|
|
|
|
}
|
2023-03-06 12:12:22 +00:00
|
|
|
return {} as T
|
|
|
|
}
|
2023-08-21 06:22:37 +00:00
|
|
|
|
|
|
|
export const getRuntimeKey = () => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const global = globalThis as any
|
|
|
|
|
|
|
|
if (global?.Deno !== undefined) {
|
|
|
|
return 'deno'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global?.Bun !== undefined) {
|
|
|
|
return 'bun'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof global?.WebSocketPair === 'function') {
|
|
|
|
return 'workerd'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof global?.EdgeRuntime === 'string') {
|
|
|
|
return 'edge-light'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global?.fastly !== undefined) {
|
|
|
|
return 'fastly'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global?.__lagon__ !== undefined) {
|
|
|
|
return 'lagon'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global?.process?.release?.name === 'node') {
|
|
|
|
return 'node'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'other'
|
|
|
|
}
|