Skip to content

Commit

Permalink
Merge branch 'v4' into feat/disable-ssg-and-only-ssg
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Jan 29, 2024
2 parents f95453f + b24f9e3 commit ee4dff4
Show file tree
Hide file tree
Showing 55 changed files with 2,358 additions and 2,402 deletions.
2 changes: 2 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ RUN curl -fsSL https://gist.githubusercontent.com/LukeChannings/09d53f5c36439104
# Install Bun
ENV BUN_INSTALL=/usr/local
RUN curl -fsSL https://bun.sh/install | bash

WORKDIR /hono
18 changes: 18 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'

services:
hono:
build: .
container_name: hono
volumes:
- ../:/hono
networks:
- hono
command: bash
stdin_open: true
tty: true
restart: 'no'

networks:
hono:
driver: bridge
71 changes: 32 additions & 39 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- uses: oven-sh/setup-bun@v1
with:
node-version: 20.x
- run: yarn install --frozen-lockfile
- run: npm run format
- run: npm run lint
- run: npm run build
- run: NODE_OPTIONS="--no-warnings" npm test
bun-version: '1.0.25'
- run: bun install
- run: bun run format
- run: bun run lint
- run: bun run build
- run: bun run test

denoify:
name: "Checking if you've done denoify"
Expand All @@ -29,11 +29,11 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: npm run build
node-version: '18.x'
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: |
npm run denoify
bun run denoify
if [[ `git status --porcelain` ]]; then
exit 1
fi
Expand All @@ -57,20 +57,18 @@ jobs:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: '1.0.18'
bun-version: '1.0.25'
- run: bun run test:bun

fastly:
name: 'Fastly Compute'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: npm run build
- run: npm run test:fastly
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun run build
- run: bun run test:fastly

node:
name: 'Node.js v${{ matrix.node }}'
Expand All @@ -83,42 +81,37 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: yarn install --frozen-lockfile
- run: npm run build
- run: npm run test:node
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun run build
- run: bun run test:node

wrangler:
name: 'Cloudflare Workers'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: npm run build
- run: npm run test:wrangler
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun run build
- run: bun run test:wrangler

lambda:
name: 'AWS Lambda'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: npm run build
- run: npm run test:lambda
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun run build
- run: bun run test:lambda

lambda-edge:
name: 'Lambda@Edge'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- run: npm run build
- run: npm run test:lambda-edge
- uses: oven-sh/setup-bun@v1
- run: bun install --frozen-lockfile
- run: bun run build
- run: bun run test:lambda-edge
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ sandbox
worker
.wrangler

# Bun
bun.lockb

# Logs
logs
*.log
Expand Down
Binary file added benchmarks/query-param/bun.lockb
Binary file not shown.
Binary file added bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[install.lockfile]
print = "yarn"
4 changes: 3 additions & 1 deletion deno_dist/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const serveStatic = <E extends Env = Env>(
defaultDocument: DEFAULT_DOCUMENT,
})

if (!path) return await next()
if (!path) {
return await next()
}

path = `./${path}`

Expand Down
8 changes: 6 additions & 2 deletions deno_dist/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './util
const createProxy = (callback: Callback, path: string[]) => {
const proxy: unknown = new Proxy(() => {}, {
get(_obj, key) {
if (typeof key !== 'string' || key === 'then') return undefined
if (typeof key !== 'string' || key === 'then') {
return undefined
}
return createProxy(callback, [...path, key])
},
apply(_1, _2, args) {
Expand Down Expand Up @@ -91,7 +93,9 @@ class ClientRequestImpl {
headerValues['Cookie'] = cookies.join(',')
}

if (this.cType) headerValues['Content-Type'] = this.cType
if (this.cType) {
headerValues['Content-Type'] = this.cType
}

const headers = new Headers(headerValues ?? undefined)
let url = this.url
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Hono } from '../hono.ts'
import type { Schema } from '../types.ts'
import type { HasRequiredKeys } from '../utils/types.ts'

type HonoRequest = typeof Hono.prototype['request']
type HonoRequest = (typeof Hono.prototype)['request']

export type ClientRequestOptions<T = unknown> = keyof T extends never
? {
Expand Down
4 changes: 3 additions & 1 deletion deno_dist/helper/accepts/accepts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export const defaultMatch = (accepts: Accept[], config: acceptsConfig) => {
*/
export const accepts = (c: Context, options: acceptsOptions) => {
const acceptHeader = c.req.header(options.header)
if (!acceptHeader) return options.default
if (!acceptHeader) {
return options.default
}
const accepts = parseAccept(acceptHeader)
const match = options.match || defaultMatch

Expand Down
24 changes: 18 additions & 6 deletions deno_dist/helper/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ 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?.process?.release?.name === 'node') return 'node'
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?.process?.release?.name === 'node') {
return 'node'
}

return 'other'
}
16 changes: 12 additions & 4 deletions deno_dist/helper/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ interface GetSignedCookie {
export const getCookie: GetCookie = (c, key?) => {
const cookie = c.req.raw.headers.get('Cookie')
if (typeof key === 'string') {
if (!cookie) return undefined
if (!cookie) {
return undefined
}
const obj = parse(cookie, key)
return obj[key]
}
if (!cookie) return {}
if (!cookie) {
return {}
}
const obj = parse(cookie)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return obj as any
Expand All @@ -28,11 +32,15 @@ export const getCookie: GetCookie = (c, key?) => {
export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => {
const cookie = c.req.raw.headers.get('Cookie')
if (typeof key === 'string') {
if (!cookie) return undefined
if (!cookie) {
return undefined
}
const obj = await parseSigned(cookie, secret, key)
return obj[key]
}
if (!cookie) return {}
if (!cookie) {
return {}
}
const obj = await parseSigned(cookie, secret)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return obj as any
Expand Down
16 changes: 12 additions & 4 deletions deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export const fetchRoutesContent = async <
const baseURL = 'http://localhost'

for (const route of inspectRoutes(app)) {
if (route.isMiddleware) continue
if (route.isMiddleware) {
continue
}

// GET Route Info
const thisRouteBaseURL = new URL(route.path, baseURL).toString()
Expand All @@ -132,13 +134,17 @@ export const fetchRoutesContent = async <
forGetInfoURLRequest.headers.set(X_HONO_SSG_HEADER_KEY, 'true')
if (beforeRequestHook) {
const maybeRequest = beforeRequestHook(forGetInfoURLRequest)
if (!maybeRequest) continue
if (!maybeRequest) {
continue
}
forGetInfoURLRequest = maybeRequest as unknown as AddedSSGDataRequest
}
await app.fetch(forGetInfoURLRequest)

if (!forGetInfoURLRequest.ssgParams) {
if (isDynamicRoute(route.path)) continue
if (isDynamicRoute(route.path)) {
continue
}
forGetInfoURLRequest.ssgParams = [{}]
}

Expand All @@ -148,7 +154,9 @@ export const fetchRoutesContent = async <
if (response.headers.get(X_HONO_DISABLE_SSG_HEADER_KEY)) continue
if (afterResponseHook) {
const maybeResponse = afterResponseHook(response)
if (!maybeResponse) continue
if (!maybeResponse) {
continue
}
response = maybeResponse
}
const mimeType = response.headers.get('Content-Type')?.split(';')[0] || 'text/plain'
Expand Down
22 changes: 16 additions & 6 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/ur

export const COMPOSED_HANDLER = Symbol('composedHandler')

type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE
type Methods = (typeof METHODS)[number] | typeof METHOD_NAME_ALL_LOWERCASE

function defineDynamicClass(): {
new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {
Expand Down Expand Up @@ -130,7 +130,9 @@ class Hono<

// Implementation of app.on(method, path, ...handlers[])
this.on = (method: string | string[], path: string | string[], ...handlers: H[]) => {
if (!method) return this
if (!method) {
return this
}
for (const p of [path].flat()) {
this.#path = p
for (const m of [method].flat()) {
Expand Down Expand Up @@ -188,7 +190,9 @@ class Hono<
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {
const subApp = this.basePath(path)

if (!app) return subApp
if (!app) {
return subApp
}

app.routes.map((r) => {
let handler
Expand Down Expand Up @@ -274,7 +278,9 @@ class Hono<
...optionsArray
)

if (res) return res
if (res) {
return res
}

await next()
}
Expand All @@ -295,7 +301,9 @@ class Hono<
}

private handleError(err: unknown, c: Context<E>) {
if (err instanceof Error) return this.errorHandler(err, c)
if (err instanceof Error) {
return this.errorHandler(err, c)
}
throw err
}

Expand Down Expand Up @@ -325,7 +333,9 @@ class Hono<
let res: ReturnType<H>
try {
res = matchResult[0][0][0][0](c, async () => {})
if (!res) return this.notFoundHandler(c)
if (!res) {
return this.notFoundHandler(c)
}
} catch (err) {
return this.handleError(err, c)
}
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/middleware/compress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MiddlewareHandler } from '../../types.ts'
const ENCODING_TYPES = ['gzip', 'deflate'] as const

interface CompressionOptions {
encoding?: typeof ENCODING_TYPES[number]
encoding?: (typeof ENCODING_TYPES)[number]
}

export const compress = (options?: CompressionOptions): MiddlewareHandler => {
Expand Down
Loading

0 comments on commit ee4dff4

Please sign in to comment.