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
Provenance
- Commit
451d6fe- Tree
fdd771a- Author
Jayden <yujonglee@g.skku.edu>- Committer
GitHub <noreply@github.com>- Parent
fadce52
5 files changed+84 added−0 removed
modified:bun_test/index.test.tsx+6 added
12345616 unmodified lines23242526272829303132333435126 unmodified linesExpand allimport { describe, expect, it } from 'bun:test'import { HonoContext } from '../src/context'import { Hono } from '../src/index'import { basicAuth } from '../src/middleware/basic-auth'import { jwt } from '../src/middleware/jwt'import { serveStatic } from '../src/middleware/serve-static/bun'16 unmodified lines expect(await res.text()).toBe('Hello Deno!') expect(res.headers.get('x-param')).toBe('foo') expect(res.headers.get('x-query')).toBe('bar') })
it('returns current runtime (bun)', async () => { const c = new HonoContext(new Request('http://localhost/')) expect(c.runtime).toBe('bun') })})
describe('Basic Auth Middleware', () => { const app = new Hono()126 unmodified linesExpand allmodified:deno_dist/context.ts+33 added
4 unmodified lines5 import type { Schema, SchemaToProp } from './validator/schema.ts'6 7 type HeaderField = [string, string]8 type Headers = Record<string, string | string[]>9+type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10 export type Data = string | ArrayBuffer | ReadableStream11 12 export interface Context<13 P extends string = string,29 unmodified lines43 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 }49 50 export class HonoContext<51 P extends string = string,179 unmodified lines231 232 notFound(): Response | Promise<Response> {233 return this.notFoundHandler(this)234 }235+236+ get runtime(): Runtime {237+ // eslint-disable-next-line @typescript-eslint/no-explicit-any238+ const global = globalThis as any239+240+ if (global?.process?.title === 'node') {241+ return 'node'242+ }243+244+ if (global?.Deno !== undefined) {245+ return 'deno'246+ }247+248+ if (global?.Bun !== undefined) {249+ return 'bun'250+ }251+252+ if (typeof global?.WebSocketPair === 'function') {253+ return 'cloudflare'254+ }255+256+ if (global?.fastly !== undefined) {257+ return 'fastly'258+ }259+260+ if (typeof global?.EdgeRuntime !== 'string') {261+ return 'vercel'262+ }263+264+ return 'other'265+ }266 }modified:deno_test/hono.test.ts+7 added
1234511 unmodified lines17181920212223242526import { HonoContext } from '../deno_dist/context.ts'import { Hono } from '../deno_dist/mod.ts'import { assertEquals } from './deps.ts'
// Test just only minimal patterns.11 unmodified lines assertEquals(await res.text(), 'Hello Deno!') assertEquals(res.headers.get('x-param'), 'foo') assertEquals(res.headers.get('x-query'), 'bar')})
Deno.test('runtime', async () => { const c = new HonoContext(new Request('http://localhost/')) assertEquals(c.runtime, 'deno')})modified:src/context.test.ts+5 added
157 unmodified lines158 const res = c.text('foo')159 expect(res.headers.get('foo')).not.toBeNull()160 expect(res.headers.get('foo')).toBe('bar')161 })162+163+ it('returns current runtime (node)', async () => {164+ c = new HonoContext(req)165+ expect(c.runtime).toBe('node')166+ })167 })168 169 describe('Context header', () => {170 const req = new Request('http://localhost/')17 unmodified linesmodified:src/context.ts+33 added
4 unmodified lines5 import type { Schema, SchemaToProp } from './validator/schema'6 7 type HeaderField = [string, string]8 type Headers = Record<string, string | string[]>9+type Runtime = 'node' | 'deno' | 'bun' | 'cloudflare' | 'fastly' | 'vercel' | 'other'10 export type Data = string | ArrayBuffer | ReadableStream11 12 export interface Context<13 P extends string = string,29 unmodified lines43 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 }49 50 export class HonoContext<51 P extends string = string,179 unmodified lines231 232 notFound(): Response | Promise<Response> {233 return this.notFoundHandler(this)234 }235+236+ get runtime(): Runtime {237+ // eslint-disable-next-line @typescript-eslint/no-explicit-any238+ const global = globalThis as any239+240+ if (global?.process?.title === 'node') {241+ return 'node'242+ }243+244+ if (global?.Deno !== undefined) {245+ return 'deno'246+ }247+248+ if (global?.Bun !== undefined) {249+ return 'bun'250+ }251+252+ if (typeof global?.WebSocketPair === 'function') {253+ return 'cloudflare'254+ }255+256+ if (global?.fastly !== undefined) {257+ return 'fastly'258+ }259+260+ if (typeof global?.EdgeRuntime !== 'string') {261+ return 'vercel'262+ }263+264+ return 'other'265+ }266 }