Skip to content

Commit a0e21c1

Browse files
committed
chore: format with prettier
1 parent 0812e81 commit a0e21c1

31 files changed

+1179
-522
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"scripts": {
2323
"build": "unbuild",
2424
"dev": "vitest",
25-
"lint": "eslint --ext ts,mjs,cjs .",
25+
"lint": "eslint --ext ts,mjs,cjs . && prettier -c src test playground",
2626
"play": "jiti ./playground/index.ts",
2727
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs",
2828
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags",

playground/index.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
import { listen } from "listhen";
22
import { fetch } from "node-fetch-native";
3-
import { createApp, createRouter, eventHandler, toNodeListener, parseCookies, createError, proxyRequest } from "../src";
3+
import {
4+
createApp,
5+
createRouter,
6+
eventHandler,
7+
toNodeListener,
8+
parseCookies,
9+
createError,
10+
proxyRequest,
11+
} from "../src";
412

513
const app = createApp({ debug: true });
614
const router = createRouter()
7-
.get("/", eventHandler(event => proxyRequest(event, "http://icanhazip.com", {
8-
fetch
9-
})))
10-
.get("/error/:code", eventHandler((event) => {
11-
throw createError({ statusCode: Number.parseInt(event.context.params.code) });
12-
}))
13-
.get("/hello/:name", eventHandler((event) => {
14-
return `Hello ${parseCookies(event)}!`;
15-
}));
15+
.get(
16+
"/",
17+
eventHandler((event) =>
18+
proxyRequest(event, "http://icanhazip.com", {
19+
fetch,
20+
})
21+
)
22+
)
23+
.get(
24+
"/error/:code",
25+
eventHandler((event) => {
26+
throw createError({
27+
statusCode: Number.parseInt(event.context.params.code),
28+
});
29+
})
30+
)
31+
.get(
32+
"/hello/:name",
33+
eventHandler((event) => {
34+
return `Hello ${parseCookies(event)}!`;
35+
})
36+
);
1637

1738
app.use(router);
1839

src/app.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,102 @@
11
import { withoutTrailingSlash } from "ufo";
2-
import { lazyEventHandler, toEventHandler, isEventHandler, eventHandler, H3Event } from "./event";
2+
import {
3+
lazyEventHandler,
4+
toEventHandler,
5+
isEventHandler,
6+
eventHandler,
7+
H3Event,
8+
} from "./event";
39
import { createError } from "./error";
410
import { send, sendStream, isStream, MIMES } from "./utils";
511
import type { EventHandler, LazyEventHandler } from "./types";
612

713
export interface Layer {
8-
route: string
9-
match?: Matcher
10-
handler: EventHandler
14+
route: string;
15+
match?: Matcher;
16+
handler: EventHandler;
1117
}
1218

13-
export type Stack = Layer[]
19+
export type Stack = Layer[];
1420

1521
export interface InputLayer {
16-
route?: string
17-
match?: Matcher
18-
handler: EventHandler
19-
lazy?: boolean
22+
route?: string;
23+
match?: Matcher;
24+
handler: EventHandler;
25+
lazy?: boolean;
2026
}
2127

22-
export type InputStack = InputLayer[]
28+
export type InputStack = InputLayer[];
2329

24-
export type Matcher = (url: string, event?: H3Event) => boolean
30+
export type Matcher = (url: string, event?: H3Event) => boolean;
2531

2632
export interface AppUse {
27-
(route: string | string[], handler: EventHandler | EventHandler[], options?: Partial<InputLayer>): App
28-
(handler: EventHandler | EventHandler[], options?: Partial<InputLayer>): App
29-
(options: InputLayer): App
33+
(
34+
route: string | string[],
35+
handler: EventHandler | EventHandler[],
36+
options?: Partial<InputLayer>
37+
): App;
38+
(handler: EventHandler | EventHandler[], options?: Partial<InputLayer>): App;
39+
(options: InputLayer): App;
3040
}
3141

3242
export interface AppOptions {
33-
debug?: boolean
34-
onError?: (error: Error, event: H3Event) => any
43+
debug?: boolean;
44+
onError?: (error: Error, event: H3Event) => any;
3545
}
3646

3747
export interface App {
38-
stack: Stack
39-
handler: EventHandler
40-
options: AppOptions
41-
use: AppUse
48+
stack: Stack;
49+
handler: EventHandler;
50+
options: AppOptions;
51+
use: AppUse;
4252
}
4353

44-
export function createApp (options: AppOptions = {}): App {
54+
export function createApp(options: AppOptions = {}): App {
4555
const stack: Stack = [];
4656
const handler = createAppEventHandler(stack, options);
4757
const app: App = {
4858
// @ts-ignore
4959
use: (arg1, arg2, arg3) => use(app as App, arg1, arg2, arg3),
5060
handler,
5161
stack,
52-
options
62+
options,
5363
};
5464
return app;
5565
}
5666

57-
export function use (
67+
export function use(
5868
app: App,
5969
arg1: string | EventHandler | InputLayer | InputLayer[],
6070
arg2?: Partial<InputLayer> | EventHandler | EventHandler[],
6171
arg3?: Partial<InputLayer>
6272
) {
6373
if (Array.isArray(arg1)) {
64-
for (const i of arg1) { use(app, i, arg2, arg3); }
74+
for (const i of arg1) {
75+
use(app, i, arg2, arg3);
76+
}
6577
} else if (Array.isArray(arg2)) {
66-
for (const i of arg2) { use(app, arg1, i, arg3); }
78+
for (const i of arg2) {
79+
use(app, arg1, i, arg3);
80+
}
6781
} else if (typeof arg1 === "string") {
68-
app.stack.push(normalizeLayer({ ...arg3, route: arg1, handler: arg2 as EventHandler }));
82+
app.stack.push(
83+
normalizeLayer({ ...arg3, route: arg1, handler: arg2 as EventHandler })
84+
);
6985
} else if (typeof arg1 === "function") {
70-
app.stack.push(normalizeLayer({ ...arg2, route: "/", handler: arg1 as EventHandler }));
86+
app.stack.push(
87+
normalizeLayer({ ...arg2, route: "/", handler: arg1 as EventHandler })
88+
);
7189
} else {
7290
app.stack.push(normalizeLayer({ ...arg1 }));
7391
}
7492
return app;
7593
}
7694

77-
export function createAppEventHandler (stack: Stack, options: AppOptions) {
95+
export function createAppEventHandler(stack: Stack, options: AppOptions) {
7896
const spacing = options.debug ? 2 : undefined;
7997
return eventHandler(async (event) => {
80-
(event.node.req as any).originalUrl = (event.node.req as any).originalUrl || event.node.req.url || "/";
98+
(event.node.req as any).originalUrl =
99+
(event.node.req as any).originalUrl || event.node.req.url || "/";
81100
const reqUrl = event.node.req.url || "/";
82101
for (const layer of stack) {
83102
if (layer.route.length > 1) {
@@ -103,26 +122,36 @@ export function createAppEventHandler (stack: Stack, options: AppOptions) {
103122
} else if (val === null) {
104123
event.node.res.statusCode = 204;
105124
return send(event);
106-
} else if (type === "object" || type === "boolean" || type === "number" /* IS_JSON */) {
125+
} else if (
126+
type === "object" ||
127+
type === "boolean" ||
128+
type === "number" /* IS_JSON */
129+
) {
107130
if (val.buffer) {
108131
return send(event, val);
109132
} else if (val instanceof Error) {
110133
throw createError(val);
111134
} else {
112-
return send(event, JSON.stringify(val, undefined, spacing), MIMES.json);
135+
return send(
136+
event,
137+
JSON.stringify(val, undefined, spacing),
138+
MIMES.json
139+
);
113140
}
114141
}
115142
}
116143
if (!event.node.res.writableEnded) {
117144
throw createError({
118145
statusCode: 404,
119-
statusMessage: `Cannot find any route matching ${event.node.req.url || "/"}.`
146+
statusMessage: `Cannot find any route matching ${
147+
event.node.req.url || "/"
148+
}.`,
120149
});
121150
}
122151
});
123152
}
124153

125-
function normalizeLayer (input: InputLayer) {
154+
function normalizeLayer(input: InputLayer) {
126155
let handler = input.handler;
127156
// @ts-ignore
128157
if (handler.handler) {
@@ -139,6 +168,6 @@ function normalizeLayer (input: InputLayer) {
139168
return {
140169
route: withoutTrailingSlash(input.route),
141170
match: input.match,
142-
handler
171+
handler,
143172
} as Layer;
144173
}

src/error.ts

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ import { MIMES } from "./utils";
1515
*/
1616
export class H3Error extends Error {
1717
static __h3_error__ = true;
18-
toJSON () {
19-
const obj: Pick<H3Error, "message" | "statusCode" | "statusMessage" | "data"> = {
18+
toJSON() {
19+
const obj: Pick<
20+
H3Error,
21+
"message" | "statusCode" | "statusMessage" | "data"
22+
> = {
2023
message: this.message,
21-
statusCode: this.statusCode
24+
statusCode: this.statusCode,
2225
};
2326

24-
if (this.statusMessage) { obj.statusMessage = this.statusMessage; }
27+
if (this.statusMessage) {
28+
obj.statusMessage = this.statusMessage;
29+
}
2530
if (this.data !== undefined) {
2631
obj.data = this.data;
2732
}
@@ -42,7 +47,9 @@ export class H3Error extends Error {
4247
* @param input {Partial<H3Error>}
4348
* @return {H3Error} An instance of the H3Error
4449
*/
45-
export function createError (input: string | Partial<H3Error> & { status?: number, statusText?: string }): H3Error {
50+
export function createError(
51+
input: string | (Partial<H3Error> & { status?: number; statusText?: string })
52+
): H3Error {
4653
if (typeof input === "string") {
4754
return new H3Error(input);
4855
}
@@ -52,23 +59,46 @@ export function createError (input: string | Partial<H3Error> & { status?: numbe
5259
}
5360

5461
// @ts-ignore
55-
const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : undefined);
62+
const err = new H3Error(
63+
input.message ?? input.statusMessage,
64+
input.cause ? { cause: input.cause } : undefined
65+
);
5666

5767
if ("stack" in input) {
5868
try {
59-
Object.defineProperty(err, "stack", { get () { return input.stack; } });
69+
Object.defineProperty(err, "stack", {
70+
get() {
71+
return input.stack;
72+
},
73+
});
6074
} catch {
61-
try { err.stack = input.stack; } catch {}
75+
try {
76+
err.stack = input.stack;
77+
} catch {}
6278
}
6379
}
6480

65-
if (input.data) { err.data = input.data; }
81+
if (input.data) {
82+
err.data = input.data;
83+
}
6684

67-
if (input.statusCode) { err.statusCode = input.statusCode; } else if (input.status) { err.statusCode = input.status; }
68-
if (input.statusMessage) { err.statusMessage = input.statusMessage; } else if (input.statusText) { err.statusMessage = input.statusText; }
85+
if (input.statusCode) {
86+
err.statusCode = input.statusCode;
87+
} else if (input.status) {
88+
err.statusCode = input.status;
89+
}
90+
if (input.statusMessage) {
91+
err.statusMessage = input.statusMessage;
92+
} else if (input.statusText) {
93+
err.statusMessage = input.statusText;
94+
}
6995

70-
if (input.fatal !== undefined) { err.fatal = input.fatal; }
71-
if (input.unhandled !== undefined) { err.unhandled = input.unhandled; }
96+
if (input.fatal !== undefined) {
97+
err.fatal = input.fatal;
98+
}
99+
if (input.unhandled !== undefined) {
100+
err.unhandled = input.unhandled;
101+
}
72102

73103
return err;
74104
}
@@ -83,23 +113,31 @@ export function createError (input: string | Partial<H3Error> & { status?: numbe
83113
* @param debug {Boolean} Whether application is in debug mode.<br>
84114
* In the debug mode the stack trace of errors will be return in response.
85115
*/
86-
export function sendError (event: H3Event, error: Error | H3Error, debug?: boolean) {
87-
if (event.node.res.writableEnded) { return; }
116+
export function sendError(
117+
event: H3Event,
118+
error: Error | H3Error,
119+
debug?: boolean
120+
) {
121+
if (event.node.res.writableEnded) {
122+
return;
123+
}
88124

89125
const h3Error = isError(error) ? error : createError(error);
90126

91127
const responseBody = {
92128
statusCode: h3Error.statusCode,
93129
statusMessage: h3Error.statusMessage,
94130
stack: [] as string[],
95-
data: h3Error.data
131+
data: h3Error.data,
96132
};
97133

98134
if (debug) {
99-
responseBody.stack = (h3Error.stack || "").split("\n").map(l => l.trim());
135+
responseBody.stack = (h3Error.stack || "").split("\n").map((l) => l.trim());
100136
}
101137

102-
if (event.node.res.writableEnded) { return; }
138+
if (event.node.res.writableEnded) {
139+
return;
140+
}
103141
const _code = Number.parseInt(h3Error.statusCode as unknown as string);
104142
if (_code) {
105143
event.node.res.statusCode = _code;
@@ -111,6 +149,6 @@ export function sendError (event: H3Event, error: Error | H3Error, debug?: boole
111149
event.node.res.end(JSON.stringify(responseBody, undefined, 2));
112150
}
113151

114-
export function isError (input: any): input is H3Error {
152+
export function isError(input: any): input is H3Error {
115153
return input?.constructor?.__h3_error__ === true;
116154
}

0 commit comments

Comments
 (0)