Skip to content
Myr

feat(context): Introducing c.runtime (#644)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add context.platform getter * Remove assumption on fastly * Add Vercel support * denoify * Replace .platform with .runtime

authored by Jayden · committed by GitHub · parent fadce52 · Browse files · 5 files+84 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+84 added−0 removed

  • tests:+18 added−0 removed
  • other:+66 added−0 removed

5 files+84 added−0 removed

modified:bun_test/index.test.tsx+6 added
modified:deno_dist/context.ts+33 added
4 unmodified lines
5import type { Schema, SchemaToProp } from './validator/schema.ts'67type HeaderField = [string, string]8type Headers = Record<string, string | string[]>9type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10export type Data = string | ArrayBuffer | ReadableStream1112export interface Context<13 P extends string = string,
29 unmodified lines
43 html: (html: string, status?: StatusCode, headers?: Headers) => Response44 redirect: (location: string, status?: StatusCode) => Response45 cookie: (name: string, value: string, options?: CookieOptions) => void46 notFound: () => Response | Promise<Response>47 get runtime(): Runtime48}4950export class HonoContext<51 P extends string = string,
179 unmodified lines
231232 notFound(): Response | Promise<Response> {233 return this.notFoundHandler(this)234 }235236 get runtime(): Runtime {237 // eslint-disable-next-line @typescript-eslint/no-explicit-any238 const global = globalThis as any239240 if (global?.process?.title === 'node') {241 return 'node'242 }243244 if (global?.Deno !== undefined) {245 return 'deno'246 }247248 if (global?.Bun !== undefined) {249 return 'bun'250 }251252 if (typeof global?.WebSocketPair === 'function') {253 return 'cloudflare'254 }255256 if (global?.fastly !== undefined) {257 return 'fastly'258 }259260 if (typeof global?.EdgeRuntime !== 'string') {261 return 'vercel'262 }263264 return 'other'265 }266}
modified:deno_test/hono.test.ts+7 added
modified:src/context.test.ts+5 added
157 unmodified lines
158 const res = c.text('foo')159 expect(res.headers.get('foo')).not.toBeNull()160 expect(res.headers.get('foo')).toBe('bar')161 })162163 it('returns current runtime (node)', async () => {164 c = new HonoContext(req)165 expect(c.runtime).toBe('node')166 })167})168169describe('Context header', () => {170 const req = new Request('http://localhost/')
17 unmodified lines
modified:src/context.ts+33 added
4 unmodified lines
5import type { Schema, SchemaToProp } from './validator/schema'67type HeaderField = [string, string]8type Headers = Record<string, string | string[]>9type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10export type Data = string | ArrayBuffer | ReadableStream1112export interface Context<13 P extends string = string,
29 unmodified lines
43 html: (html: string, status?: StatusCode, headers?: Headers) => Response44 redirect: (location: string, status?: StatusCode) => Response45 cookie: (name: string, value: string, options?: CookieOptions) => void46 notFound: () => Response | Promise<Response>47 get runtime(): Runtime48}4950export class HonoContext<51 P extends string = string,
179 unmodified lines
231232 notFound(): Response | Promise<Response> {233 return this.notFoundHandler(this)234 }235236 get runtime(): Runtime {237 // eslint-disable-next-line @typescript-eslint/no-explicit-any238 const global = globalThis as any239240 if (global?.process?.title === 'node') {241 return 'node'242 }243244 if (global?.Deno !== undefined) {245 return 'deno'246 }247248 if (global?.Bun !== undefined) {249 return 'bun'250 }251252 if (typeof global?.WebSocketPair === 'function') {253 return 'cloudflare'254 }255256 if (global?.fastly !== undefined) {257 return 'fastly'258 }259260 if (typeof global?.EdgeRuntime !== 'string') {261 return 'vercel'262 }263264 return 'other'265 }266}