refactor(helper): Use getRuntimeKey() in env function (#1454)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor * lint&denoify * marker * type assersion * revert fastly message
authored by watany · committed by GitHub · parent 9cb6b37 · Browse files · 3 files·+51 added−92 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+51 added−92 removed
- tests:+1 added−0 removed
- other:+50 added−92 removed
Provenance
- Commit
d8b0e7e- Tree
1d2149f- Author
watany <76135106+watany-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9cb6b37
3 files changed+51 added−92 removed
modified:deno_dist/helper/adapter/index.ts+25 added-46 removed
3 unmodified lines456789910111213141516171819202122232425101112131415161718192021222327242526292728293031323334363738353637383940414240414243444546474849505152535455565758596061626343443 unmodified lines c: C): T & C['env'] => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any const globalEnv = global?.process?.env as T
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 } if (c.runtime === 'fastly') { const runtime = getRuntimeKey()
const runtimeEnvHandlers: Record<string, () => T> = { bun: () => globalEnv, node: () => globalEnv, 'edge-light': () => globalEnv, lagon: () => globalEnv, deno: () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return Deno.env.toObject() as T }, workerd: () => c.env, // On Fastly Compute@Edge, you can use the ConfigStore to manage user-defined data. return {} as T fastly: () => ({} as T), other: () => ({} as T), } return {} as T
return runtimeEnvHandlers[runtime]()}
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?.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'
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'}modified:src/helper/adapter/index.ts+25 added-46 removed
3 unmodified lines456789910111213141516171819202122232425101112131415161718192021222327242526292728293031323334363738353637383940414240414243444546474849505152535455565758596061626343443 unmodified lines c: C): T & C['env'] => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any const globalEnv = global?.process?.env as T
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 } if (c.runtime === 'fastly') { const runtime = getRuntimeKey()
const runtimeEnvHandlers: Record<string, () => T> = { bun: () => globalEnv, node: () => globalEnv, 'edge-light': () => globalEnv, lagon: () => globalEnv, deno: () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return Deno.env.toObject() as T }, workerd: () => c.env, // On Fastly Compute@Edge, you can use the ConfigStore to manage user-defined data. return {} as T fastly: () => ({} as T), other: () => ({} as T), } return {} as T
return runtimeEnvHandlers[runtime]()}
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?.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'
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'}modified:src/context.test.ts+1 added
172 unmodified lines173 expect(res.headers.get('foo')).not.toBeNull()174 expect(res.headers.get('foo')).toBe('bar')175 })176 177+ // The `c.runtime()` will be removed in v4.178 it('returns current runtime (workerd)', async () => {179 c = new Context(req)180 expect(c.runtime).toBe('workerd')181 })114 unmodified lines