@arkstack/driver-h3 0.2.4 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.js +37 -4
- package/dist/index.js.map +1 -1
- package/dist/middlewares/index.d.ts +11 -0
- package/dist/middlewares/index.js +35 -0
- package/dist/middlewares/index.js.map +1 -0
- package/package.json +5 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Toneflix Technologies Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArkstackKitDriver, PromiseOrValue } from "@arkstack/contract";
|
|
1
|
+
import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from "@arkstack/contract";
|
|
2
2
|
import { H3 } from "h3";
|
|
3
3
|
import { Middleware } from "clear-router/types/h3";
|
|
4
4
|
|
|
@@ -9,6 +9,13 @@ interface H3DriverOptions {
|
|
|
9
9
|
mountPublicAssets: (app: H3, publicPath: string) => PromiseOrValue<void>;
|
|
10
10
|
createApp?: () => H3;
|
|
11
11
|
}
|
|
12
|
+
declare class H3EventResponse {
|
|
13
|
+
response: Response;
|
|
14
|
+
status: number;
|
|
15
|
+
statusText?: string;
|
|
16
|
+
constructor(response: Response);
|
|
17
|
+
get headers(): Headers;
|
|
18
|
+
}
|
|
12
19
|
declare class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {
|
|
13
20
|
readonly name = "h3";
|
|
14
21
|
private readonly options;
|
|
@@ -16,9 +23,9 @@ declare class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {
|
|
|
16
23
|
createApp(): H3;
|
|
17
24
|
mountPublicAssets(app: H3, publicPath: string): PromiseOrValue<void>;
|
|
18
25
|
bindRouter(app: H3): PromiseOrValue<void>;
|
|
19
|
-
applyMiddleware(app: H3, middleware: H3Middleware): void;
|
|
26
|
+
applyMiddleware(app: H3, middleware: H3Middleware | ArkstackMiddlewareConfig<H3Middleware>): void;
|
|
20
27
|
start(app: H3, port: number): void;
|
|
21
28
|
}
|
|
22
29
|
//#endregion
|
|
23
|
-
export { H3Driver, H3DriverOptions, H3Middleware };
|
|
30
|
+
export { H3Driver, H3DriverOptions, H3EventResponse, H3Middleware };
|
|
24
31
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { ArkstackKitDriver } from "@arkstack/contract";
|
|
2
|
-
import { H3, serve } from "h3";
|
|
2
|
+
import { H3, serve, toResponse } from "h3";
|
|
3
|
+
import { Logger } from "@arkstack/common";
|
|
3
4
|
|
|
4
5
|
//#region src/index.ts
|
|
6
|
+
var H3EventResponse = class {
|
|
7
|
+
status = 200;
|
|
8
|
+
statusText;
|
|
9
|
+
constructor(response) {
|
|
10
|
+
this.response = response;
|
|
11
|
+
this.status = response.status;
|
|
12
|
+
this.statusText = response.statusText;
|
|
13
|
+
}
|
|
14
|
+
get headers() {
|
|
15
|
+
return this.response.headers;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
5
18
|
/**
|
|
6
19
|
* The H3Driver class implements the ArkstackKitDriver contract for the H3 framework.
|
|
7
20
|
*/
|
|
@@ -49,7 +62,22 @@ var H3Driver = class extends ArkstackKitDriver {
|
|
|
49
62
|
* @param middleware
|
|
50
63
|
*/
|
|
51
64
|
applyMiddleware(app, middleware) {
|
|
52
|
-
|
|
65
|
+
const mw = Array.isArray(middleware) ? middleware[0] : middleware;
|
|
66
|
+
const conf = Array.isArray(middleware) && middleware[1] ? middleware[1] : {};
|
|
67
|
+
if (typeof mw === "function") {
|
|
68
|
+
app.use(mw, conf);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const [pos, entries] of Object.entries(middleware)) for (const entry of entries) {
|
|
72
|
+
const mw = Array.isArray(entry) ? entry[0] : entry;
|
|
73
|
+
const conf = Array.isArray(entry) && entry[1] ? entry[1] : {};
|
|
74
|
+
if (pos === "after") app.use(async (evt, next) => {
|
|
75
|
+
evt[Symbol.for("h3.internal.event.res")] = new H3EventResponse(await toResponse(await next(), evt));
|
|
76
|
+
await mw(evt, next);
|
|
77
|
+
next();
|
|
78
|
+
}, conf);
|
|
79
|
+
else app.use(mw, conf);
|
|
80
|
+
}
|
|
53
81
|
}
|
|
54
82
|
/**
|
|
55
83
|
* Starts the H3 server on the specified port.
|
|
@@ -58,10 +86,15 @@ var H3Driver = class extends ArkstackKitDriver {
|
|
|
58
86
|
* @param port
|
|
59
87
|
*/
|
|
60
88
|
start(app, port) {
|
|
61
|
-
serve(app, {
|
|
89
|
+
serve(app, {
|
|
90
|
+
port,
|
|
91
|
+
silent: true
|
|
92
|
+
}).ready().then(() => {
|
|
93
|
+
Logger.log([["Server is running on", "white"], [`http://localhost:${port}`, "cyan"]], " ");
|
|
94
|
+
});
|
|
62
95
|
}
|
|
63
96
|
};
|
|
64
97
|
|
|
65
98
|
//#endregion
|
|
66
|
-
export { H3Driver };
|
|
99
|
+
export { H3Driver, H3EventResponse };
|
|
67
100
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { ArkstackKitDriver, PromiseOrValue } from '@arkstack/contract'\nimport { H3, serve } from 'h3'\n\nimport { Middleware as H3BaseMiddleware } from 'clear-router/types/h3'\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport type H3Middleware = H3BaseMiddleware | [H3BaseMiddleware, Record<string, any>];\n\nexport interface H3DriverOptions {\n bindRouter: (app: H3) => PromiseOrValue<void>;\n mountPublicAssets: (app: H3, publicPath: string) => PromiseOrValue<void>;\n createApp?: () => H3;\n}\n\n/**\n * The H3Driver class implements the ArkstackKitDriver contract for the H3 framework.\n */\nexport class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {\n readonly name = 'h3'\n private readonly options: H3DriverOptions\n\n /**\n * Creates an instance of H3Driver.\n * \n * @param options \n */\n constructor(options: H3DriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an H3 application instance.\n * \n * @returns \n */\n createApp (): H3 {\n return this.options.createApp?.() ?? new H3()\n }\n\n /**\n * Mounts static assets from the specified public path to the H3 application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: H3, publicPath: string): PromiseOrValue<void> {\n return this.options.mountPublicAssets(app, publicPath)\n }\n\n /**\n * Binds the router to the H3 application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: H3): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the H3 application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (app: H3
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { ArkstackKitDriver, ArkstackMiddlewareConfig, PromiseOrValue } from '@arkstack/contract'\nimport { H3, serve, toResponse } from 'h3'\n\nimport { Middleware as H3BaseMiddleware } from 'clear-router/types/h3'\nimport { Logger } from '@arkstack/common'\n\n// oxlint-disable-next-line typescript/no-explicit-any\nexport type H3Middleware = H3BaseMiddleware | [H3BaseMiddleware, Record<string, any>];\n\nexport interface H3DriverOptions {\n bindRouter: (app: H3) => PromiseOrValue<void>;\n mountPublicAssets: (app: H3, publicPath: string) => PromiseOrValue<void>;\n createApp?: () => H3;\n}\n\nexport class H3EventResponse {\n status: number = 200\n statusText?: string\n\n constructor(public response: Response) {\n this.status = response.status\n this.statusText = response.statusText\n }\n\n get headers (): Headers {\n return this.response.headers\n }\n}\n\n/**\n * The H3Driver class implements the ArkstackKitDriver contract for the H3 framework.\n */\nexport class H3Driver extends ArkstackKitDriver<H3, H3Middleware> {\n readonly name = 'h3'\n private readonly options: H3DriverOptions\n\n /**\n * Creates an instance of H3Driver.\n * \n * @param options \n */\n constructor(options: H3DriverOptions) {\n super()\n this.options = options\n }\n\n /**\n * Creates an H3 application instance.\n * \n * @returns \n */\n createApp (): H3 {\n return this.options.createApp?.() ?? new H3()\n }\n\n /**\n * Mounts static assets from the specified public path to the H3 application.\n * \n * @param app \n * @param publicPath \n */\n mountPublicAssets (app: H3, publicPath: string): PromiseOrValue<void> {\n return this.options.mountPublicAssets(app, publicPath)\n }\n\n /**\n * Binds the router to the H3 application using the provided bindRouter function.\n * \n * @param app \n */\n bindRouter (app: H3): PromiseOrValue<void> {\n return this.options.bindRouter(app)\n }\n\n /**\n * Applies middleware to the H3 application.\n * \n * @param app \n * @param middleware \n */\n applyMiddleware (\n app: H3,\n middleware: H3Middleware | ArkstackMiddlewareConfig<H3Middleware>,\n ): void {\n const mw = Array.isArray(middleware) ? middleware[0] : middleware\n const conf = Array.isArray(middleware) && middleware[1] ? middleware[1] : {}\n\n if (typeof mw === 'function') {\n app.use(mw, conf)\n\n return\n }\n\n for (const [pos, entries] of Object.entries(middleware) as [string, H3Middleware[]][]) {\n for (const entry of entries) {\n const mw = Array.isArray(entry) ? entry[0] : entry\n const conf = Array.isArray(entry) && entry[1] ? entry[1] : {}\n\n if (pos === 'after') {\n app.use(async (evt, next) => {\n const response = await toResponse(await next(), evt)\n // evt.res.status = response.status\n evt[Symbol.for('h3.internal.event.res') as never] = new H3EventResponse(response) as never\n await mw(evt, next)\n next()\n }, conf)\n } else {\n app.use(mw, conf)\n }\n }\n }\n }\n\n /**\n * Starts the H3 server on the specified port.\n * \n * @param app \n * @param port \n */\n start (app: H3, port: number): void {\n serve(app, { port, silent: true }).ready().then(() => {\n Logger.log([\n ['Server is running on', 'white'],\n [`http://localhost:${port}`, 'cyan']\n ], ' ')\n })\n }\n}\n"],"mappings":";;;;;AAeA,IAAa,kBAAb,MAA6B;CACzB,SAAiB;CACjB;CAEA,YAAY,AAAO,UAAoB;EAApB;AACf,OAAK,SAAS,SAAS;AACvB,OAAK,aAAa,SAAS;;CAG/B,IAAI,UAAoB;AACpB,SAAO,KAAK,SAAS;;;;;;AAO7B,IAAa,WAAb,cAA8B,kBAAoC;CAC9D,AAAS,OAAO;CAChB,AAAiB;;;;;;CAOjB,YAAY,SAA0B;AAClC,SAAO;AACP,OAAK,UAAU;;;;;;;CAQnB,YAAiB;AACb,SAAO,KAAK,QAAQ,aAAa,IAAI,IAAI,IAAI;;;;;;;;CASjD,kBAAmB,KAAS,YAA0C;AAClE,SAAO,KAAK,QAAQ,kBAAkB,KAAK,WAAW;;;;;;;CAQ1D,WAAY,KAA+B;AACvC,SAAO,KAAK,QAAQ,WAAW,IAAI;;;;;;;;CASvC,gBACI,KACA,YACI;EACJ,MAAM,KAAK,MAAM,QAAQ,WAAW,GAAG,WAAW,KAAK;EACvD,MAAM,OAAO,MAAM,QAAQ,WAAW,IAAI,WAAW,KAAK,WAAW,KAAK,EAAE;AAE5E,MAAI,OAAO,OAAO,YAAY;AAC1B,OAAI,IAAI,IAAI,KAAK;AAEjB;;AAGJ,OAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,WAAW,CACnD,MAAK,MAAM,SAAS,SAAS;GACzB,MAAM,KAAK,MAAM,QAAQ,MAAM,GAAG,MAAM,KAAK;GAC7C,MAAM,OAAO,MAAM,QAAQ,MAAM,IAAI,MAAM,KAAK,MAAM,KAAK,EAAE;AAE7D,OAAI,QAAQ,QACR,KAAI,IAAI,OAAO,KAAK,SAAS;AAGzB,QAAI,OAAO,IAAI,wBAAwB,IAAa,IAAI,gBAFvC,MAAM,WAAW,MAAM,MAAM,EAAE,IAAI,CAE6B;AACjF,UAAM,GAAG,KAAK,KAAK;AACnB,UAAM;MACP,KAAK;OAER,KAAI,IAAI,IAAI,KAAK;;;;;;;;;CAYjC,MAAO,KAAS,MAAoB;AAChC,QAAM,KAAK;GAAE;GAAM,QAAQ;GAAM,CAAC,CAAC,OAAO,CAAC,WAAW;AAClD,UAAO,IAAI,CACP,CAAC,wBAAwB,QAAQ,EACjC,CAAC,oBAAoB,QAAQ,OAAO,CACvC,EAAE,IAAI;IACT"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { H3Middleware } from "../index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/middlewares/request-logger.d.ts
|
|
4
|
+
declare const requestLogger: ({
|
|
5
|
+
allowInProduction
|
|
6
|
+
}?: {
|
|
7
|
+
allowInProduction?: boolean;
|
|
8
|
+
}) => H3Middleware;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { requestLogger };
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Logger, nodeEnv } from "@arkstack/common";
|
|
2
|
+
|
|
3
|
+
//#region src/middlewares/request-logger.ts
|
|
4
|
+
const colors = {
|
|
5
|
+
GET: "green",
|
|
6
|
+
POST: "blue",
|
|
7
|
+
PUT: "yellow",
|
|
8
|
+
DELETE: "red",
|
|
9
|
+
PATCH: "cyan"
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Middleware to log incoming requests and their response times.
|
|
13
|
+
*
|
|
14
|
+
* @param config Configuration options for the request logger middleware.
|
|
15
|
+
* @param config.allowInProduction If true, the logger will also log requests in production environment. Default is false.
|
|
16
|
+
* @returns H3Middleware function
|
|
17
|
+
*/
|
|
18
|
+
const requestLogger = ({ allowInProduction = false } = {}) => async (event, next) => {
|
|
19
|
+
if (nodeEnv() === "prod" && !allowInProduction) return next();
|
|
20
|
+
await next();
|
|
21
|
+
const start = Date.now();
|
|
22
|
+
const req = event.req;
|
|
23
|
+
const status = event.res.status || 200;
|
|
24
|
+
const duration = Date.now() - start;
|
|
25
|
+
Logger.log([
|
|
26
|
+
[`[${req.method}]`, colors[req.method] || "green"],
|
|
27
|
+
[req.url, "cyan"],
|
|
28
|
+
[status.toString(), status >= 500 ? "red" : status >= 400 ? "yellow" : "green"],
|
|
29
|
+
[`- ${duration}ms`, "dim"]
|
|
30
|
+
], " ");
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { requestLogger };
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/middlewares/request-logger.ts"],"sourcesContent":["import { Logger, nodeEnv } from '@arkstack/common'\n\nimport { H3Middleware } from '..'\n\nconst colors: Record<string, 'green' | 'blue' | 'yellow' | 'red' | 'cyan'> = {\n GET: 'green',\n POST: 'blue',\n PUT: 'yellow',\n DELETE: 'red',\n PATCH: 'cyan',\n}\n\n/**\n * Middleware to log incoming requests and their response times.\n * \n * @param config Configuration options for the request logger middleware.\n * @param config.allowInProduction If true, the logger will also log requests in production environment. Default is false. \n * @returns H3Middleware function\n */\nexport const requestLogger = ({\n allowInProduction = false,\n}: {\n allowInProduction?: boolean\n} = {}): H3Middleware => async (event, next) => {\n if (nodeEnv() === 'prod' && !allowInProduction) return next()\n\n await next()\n\n const start = Date.now()\n const req = event.req\n const status = event.res.status || 200\n const duration = Date.now() - start\n\n Logger.log([\n [`[${req.method}]`, colors[req.method] || 'green'],\n [req.url, 'cyan'],\n [status.toString(), status >= 500 ? 'red' : status >= 400 ? 'yellow' : 'green'],\n [`- ${duration}ms`, 'dim']\n ], ' ')\n}"],"mappings":";;;AAIA,MAAM,SAAuE;CACzE,KAAK;CACL,MAAM;CACN,KAAK;CACL,QAAQ;CACR,OAAO;CACV;;;;;;;;AASD,MAAa,iBAAiB,EAC1B,oBAAoB,UAGpB,EAAE,KAAmB,OAAO,OAAO,SAAS;AAC5C,KAAI,SAAS,KAAK,UAAU,CAAC,kBAAmB,QAAO,MAAM;AAE7D,OAAM,MAAM;CAEZ,MAAM,QAAQ,KAAK,KAAK;CACxB,MAAM,MAAM,MAAM;CAClB,MAAM,SAAS,MAAM,IAAI,UAAU;CACnC,MAAM,WAAW,KAAK,KAAK,GAAG;AAE9B,QAAO,IAAI;EACP,CAAC,IAAI,IAAI,OAAO,IAAI,OAAO,IAAI,WAAW,QAAQ;EAClD,CAAC,IAAI,KAAK,OAAO;EACjB,CAAC,OAAO,UAAU,EAAE,UAAU,MAAM,QAAQ,UAAU,MAAM,WAAW,QAAQ;EAC/E,CAAC,KAAK,SAAS,KAAK,MAAM;EAC7B,EAAE,IAAI"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkstack/driver-h3",
|
|
3
|
-
"version": "0.2
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "H3 driver package for Arkstack providing H3-specific implementations of core Arkstack features such as routing, middleware, and database integration.",
|
|
6
6
|
"homepage": "https://arkstack.toneflix.net",
|
|
@@ -32,14 +32,16 @@
|
|
|
32
32
|
},
|
|
33
33
|
"exports": {
|
|
34
34
|
".": "./dist/index.js",
|
|
35
|
+
"./middlewares": "./dist/middlewares/index.js",
|
|
35
36
|
"./package.json": "./package.json"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@arkstack/contract": "^0.2
|
|
39
|
+
"@arkstack/contract": "^0.3.2"
|
|
39
40
|
},
|
|
40
41
|
"peerDependencies": {
|
|
41
42
|
"clear-router": "^2.3.3",
|
|
42
|
-
"h3": "2.0.1-rc.16"
|
|
43
|
+
"h3": "2.0.1-rc.16",
|
|
44
|
+
"@arkstack/common": "^0.3.2"
|
|
43
45
|
},
|
|
44
46
|
"scripts": {
|
|
45
47
|
"build": "tsdown",
|