-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
372 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,8 @@ | |
"iron-webcrypto": "^0.8.0", | ||
"radix3": "^1.0.1", | ||
"ufo": "^1.1.2", | ||
"uncrypto": "^0.1.3" | ||
"uncrypto": "^0.1.3", | ||
"unenv": "^1.6.1" | ||
}, | ||
"devDependencies": { | ||
"0x": "^5.5.0", | ||
|
@@ -64,4 +65,4 @@ | |
"zod": "^3.21.4" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export * from "./node"; | ||
|
||
export { type WebHandler, toWebHandler } from "./web"; | ||
|
||
export { | ||
type PlainHandler, | ||
type PlainRequest, | ||
type PlainResponse, | ||
toPlainHandler, | ||
} from "./plain"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { IncomingMessage as NodeIncomingMessage } from "unenv/runtime/node/http/_request"; | ||
import { ServerResponse as NodeServerResponse } from "unenv/runtime/node/http/_response"; | ||
import type { App } from "../app"; | ||
import type { HTTPMethod } from "../types"; | ||
import { createError, isError, sendError } from "../error"; | ||
import { H3Event, createEvent } from "../event"; | ||
import { splitCookiesString } from "../utils"; | ||
|
||
export interface PlainRequest { | ||
_eventOverrides?: Partial<H3Event>; | ||
context?: Record<string, unknown>; | ||
|
||
method: string; | ||
path: string; | ||
headers: HeadersInit; | ||
body?: null | BodyInit; | ||
} | ||
|
||
export interface PlainResponse { | ||
status: number; | ||
statusText: string; | ||
headers: [string, string][]; | ||
body?: unknown; | ||
} | ||
|
||
export type PlainHandler = (request: PlainRequest) => Promise<PlainResponse>; | ||
|
||
/** @experimental */ | ||
export function toPlainHandler(app: App) { | ||
const handler: PlainHandler = (request) => { | ||
return _handlePlainRequest(app, request); | ||
}; | ||
return handler; | ||
} | ||
|
||
// --- Internal --- | ||
|
||
export async function _handlePlainRequest(app: App, request: PlainRequest) { | ||
// Normalize request | ||
const path = request.path; | ||
const method = (request.method || "GET").toUpperCase() as HTTPMethod; | ||
const headers = new Headers(request.headers); | ||
|
||
// Shim for Node.js request and response objects | ||
// TODO: Remove in next major version | ||
const nodeReq = new NodeIncomingMessage(); | ||
const nodeRes = new NodeServerResponse(nodeReq); | ||
|
||
// Fill node request properties | ||
nodeReq.method = method; | ||
nodeReq.url = path; | ||
// TODO: Normalize with array merge and lazy getter | ||
nodeReq.headers = Object.fromEntries(headers.entries()); | ||
|
||
// Create new event | ||
const event = createEvent(nodeReq, nodeRes); | ||
|
||
// Fill internal event properties | ||
event._method = method; | ||
event._path = path; | ||
event._headers = headers; | ||
if (request.body) { | ||
event._body = request.body; | ||
} | ||
if (request._eventOverrides) { | ||
Object.assign(event, request._eventOverrides); | ||
} | ||
if (request.context) { | ||
Object.assign(event.context, request.context); | ||
} | ||
|
||
// Run app handler logic | ||
try { | ||
await app.handler(event); | ||
} catch (_error: any) { | ||
const error = createError(_error); | ||
if (!isError(_error)) { | ||
error.unhandled = true; | ||
} | ||
if (app.options.onError) { | ||
await app.options.onError(error, event); | ||
} | ||
if (!event.handled) { | ||
if (error.unhandled || error.fatal) { | ||
console.error("[h3]", error.fatal ? "[fatal]" : "[unhandled]", error); // eslint-disable-line no-console | ||
} | ||
await sendError(event, error, !!app.options.debug); | ||
} | ||
} | ||
|
||
return { | ||
status: nodeRes.statusCode, | ||
statusText: nodeRes.statusMessage, | ||
headers: _normalizeUnenvHeaders(nodeRes._headers), | ||
body: nodeRes._data, | ||
}; | ||
} | ||
|
||
function _normalizeUnenvHeaders( | ||
input: Record<string, undefined | string | number | string[]> | ||
) { | ||
const headers: [string, string][] = []; | ||
const cookies: string[] = []; | ||
|
||
for (const _key in input) { | ||
const key = _key.toLowerCase(); | ||
|
||
if (key === "set-cookie") { | ||
cookies.push( | ||
...splitCookiesString(input["set-cookie"] as string | string[]) | ||
); | ||
continue; | ||
} | ||
|
||
const value = input[key]; | ||
if (Array.isArray(value)) { | ||
for (const _value of value) { | ||
headers.push([key, _value]); | ||
} | ||
} else if (value !== undefined) { | ||
headers.push([key, String(value)]); | ||
} | ||
} | ||
|
||
if (cookies.length > 0) { | ||
for (const cookie of cookies) { | ||
headers.push(["set-cookie", cookie]); | ||
} | ||
} | ||
|
||
return headers; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { App } from "../app"; | ||
import { _handlePlainRequest } from "./plain"; | ||
|
||
export type WebHandler = ( | ||
request: Request, | ||
context?: Record<string, unknown> | ||
) => Promise<Response>; | ||
|
||
/** @experimental */ | ||
export function toWebHandler(app: App) { | ||
const webHandler: WebHandler = (request, context) => { | ||
return _handleWebRequest(app, request, context); | ||
}; | ||
|
||
return webHandler; | ||
} | ||
|
||
// --- Internal --- | ||
|
||
async function _handleWebRequest( | ||
app: App, | ||
request: Request, | ||
context?: Record<string, unknown> | ||
) { | ||
const url = new URL(request.url); | ||
const res = await _handlePlainRequest(app, { | ||
_eventOverrides: { | ||
_request: request, | ||
_url: url, | ||
}, | ||
context, | ||
method: request.method, | ||
path: url.pathname + url.search, | ||
headers: request.headers, | ||
body: request.body, | ||
}); | ||
|
||
return new Response(res.body as BodyInit, { | ||
status: res.status, | ||
statusText: res.statusText, | ||
headers: res.headers, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export * from "./app"; | ||
export * from "./error"; | ||
export * from "./event"; | ||
export * from "./node"; | ||
export * from "./utils"; | ||
export * from "./router"; | ||
export * from "./types"; | ||
export * from "./adapters"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.