@forklaunch/hyper-express 0.2.0 → 0.2.1

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.
Files changed (32) hide show
  1. package/lib/index.d.mts +95 -0
  2. package/lib/index.d.ts +95 -0
  3. package/lib/{hyperExpressApplication.js → index.js} +124 -8
  4. package/lib/index.mjs +230 -0
  5. package/package.json +22 -22
  6. package/lib/hyperExpressApplication.d.mts +0 -30
  7. package/lib/hyperExpressApplication.d.ts +0 -30
  8. package/lib/hyperExpressApplication.mjs +0 -111
  9. package/lib/hyperExpressRouter.d.mts +0 -12
  10. package/lib/hyperExpressRouter.d.ts +0 -12
  11. package/lib/hyperExpressRouter.js +0 -130
  12. package/lib/hyperExpressRouter.mjs +0 -109
  13. package/lib/middleware/contentParse.middleware.d.mts +0 -10
  14. package/lib/middleware/contentParse.middleware.d.ts +0 -10
  15. package/lib/middleware/contentParse.middleware.js +0 -49
  16. package/lib/middleware/contentParse.middleware.mjs +0 -24
  17. package/lib/middleware/enrichResponseTransmission.middleware.d.mts +0 -17
  18. package/lib/middleware/enrichResponseTransmission.middleware.d.ts +0 -17
  19. package/lib/middleware/enrichResponseTransmission.middleware.js +0 -67
  20. package/lib/middleware/enrichResponseTransmission.middleware.mjs +0 -44
  21. package/lib/middleware/polyfillGetHeaders.middleware.d.mts +0 -5
  22. package/lib/middleware/polyfillGetHeaders.middleware.d.ts +0 -5
  23. package/lib/middleware/polyfillGetHeaders.middleware.js +0 -36
  24. package/lib/middleware/polyfillGetHeaders.middleware.mjs +0 -11
  25. package/lib/middleware/swagger.middleware.d.mts +0 -27
  26. package/lib/middleware/swagger.middleware.d.ts +0 -27
  27. package/lib/middleware/swagger.middleware.js +0 -97
  28. package/lib/middleware/swagger.middleware.mjs +0 -61
  29. package/lib/types/hyperExpress.types.d.mts +0 -44
  30. package/lib/types/hyperExpress.types.d.ts +0 -44
  31. package/lib/types/hyperExpress.types.js +0 -18
  32. package/lib/types/hyperExpress.types.mjs +0 -0
@@ -0,0 +1,95 @@
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, ForklaunchRouter, TypedMiddlewareDefinition, ParamsDictionary, ForklaunchRequest, ForklaunchResponse, ForklaunchStatusResponse, ForklaunchSendableData } from '@forklaunch/core/http';
3
+ import { Server, MiddlewareHandler, Router as Router$1, Request as Request$1, Response as Response$1 } from '@forklaunch/hyper-express-fork';
4
+ import * as uWebsockets from 'uWebSockets.js';
5
+ import { ParsedQs } from 'qs';
6
+
7
+ /**
8
+ * Represents an application built on top of Hyper-Express and Forklaunch.
9
+ *
10
+ * @template SV - A type that extends AnySchemaValidator.
11
+ */
12
+ declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Server, MiddlewareHandler> {
13
+ /**
14
+ * Creates an instance of the Application class.
15
+ *
16
+ * @param {SV} schemaValidator - The schema validator.
17
+ */
18
+ constructor(schemaValidator: SV);
19
+ /**
20
+ * Starts the server and sets up Swagger documentation.
21
+ *
22
+ * @param {string | number} arg0 - The port number or UNIX path to listen on.
23
+ * @param {...unknown[]} args - Additional arguments.
24
+ * @returns {Promise<uWebsockets.us_listen_socket>} - A promise that resolves with the listening socket.
25
+ */
26
+ listen(port: number, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
27
+ listen(port: number, host?: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
28
+ listen(unix_path: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
29
+ }
30
+
31
+ declare class Router<SV extends AnySchemaValidator, BasePath extends `/${string}`> extends ForklaunchExpressLikeRouter<SV, BasePath, MiddlewareHandler, Router$1> implements ForklaunchRouter<SV> {
32
+ basePath: BasePath;
33
+ constructor(basePath: BasePath, schemaValidator: SV);
34
+ route(path: string): this;
35
+ any: TypedMiddlewareDefinition<this, SV>;
36
+ }
37
+
38
+ /**
39
+ * Extends the Forklaunch request interface with properties from Hyper-Express's request interface.
40
+ *
41
+ * @template SV - A type that extends AnySchemaValidator.
42
+ * @template P - A type for request parameters, defaulting to ParamsDictionary.
43
+ * @template _ResBody - A type for the response body, defaulting to unknown.
44
+ * @template ReqBody - A type for the request body, defaulting to unknown.
45
+ * @template ReqQuery - A type for the request query, defaulting to ParsedQs.
46
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
47
+ */
48
+ interface Request<SV extends AnySchemaValidator, P extends ParamsDictionary, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, Omit<Request$1<LocalsObj>, 'method' | 'params' | 'query' | 'headers'> {
49
+ /** The request body */
50
+ body: ReqBody;
51
+ /** The request query parameters */
52
+ query: ReqQuery;
53
+ /** The request parameters */
54
+ params: P;
55
+ }
56
+ /**
57
+ * Extends the Forklaunch response interface with properties from Hyper-Express's response interface.
58
+ *
59
+ * @template ResBody - A type for the response body, defaulting to unknown.
60
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
61
+ * @template StatusCode - A type for the status code, defaulting to number.
62
+ */
63
+ interface Response<ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj>, Omit<Response$1<LocalsObj>, 'getHeaders' | 'setHeader' | 'headersSent' | 'send' | 'status' | 'statusCode' | 'json' | 'jsonp' | 'end'>, ForklaunchStatusResponse<ForklaunchSendableData> {
64
+ /** The body data of the response */
65
+ bodyData: unknown;
66
+ /** If cors are applied to the response */
67
+ cors: boolean;
68
+ /** The status code of the response */
69
+ _status_code: number;
70
+ /** Whether the response is corked */
71
+ _cork: boolean;
72
+ /** Whether the response is currently corked */
73
+ _corked: boolean;
74
+ }
75
+
76
+ type App<SV extends AnySchemaValidator> = Application<SV>;
77
+ /**
78
+ * Creates a new instance of Application with the given schema validator.
79
+ *
80
+ * @template SV - A type that extends AnySchemaValidator.
81
+ * @param {SV} schemaValidator - The schema validator.
82
+ * @returns {Application<SV>} - The new application instance.
83
+ */
84
+ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV): Application<SV>;
85
+ /**
86
+ * Creates a new instance of Router with the given base path and schema validator.
87
+ *
88
+ * @template SV - A type that extends AnySchemaValidator.
89
+ * @param {string} basePath - The base path for the router.
90
+ * @param {SV} schemaValidator - The schema validator.
91
+ * @returns {Router<SV>} - The new router instance.
92
+ */
93
+ declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV): Router<SV, BasePath>;
94
+
95
+ export { type App, Application, type Request, type Response, Router, forklaunchExpress, forklaunchRouter };
package/lib/index.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ import { AnySchemaValidator } from '@forklaunch/validator';
2
+ import { ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, ForklaunchRouter, TypedMiddlewareDefinition, ParamsDictionary, ForklaunchRequest, ForklaunchResponse, ForklaunchStatusResponse, ForklaunchSendableData } from '@forklaunch/core/http';
3
+ import { Server, MiddlewareHandler, Router as Router$1, Request as Request$1, Response as Response$1 } from '@forklaunch/hyper-express-fork';
4
+ import * as uWebsockets from 'uWebSockets.js';
5
+ import { ParsedQs } from 'qs';
6
+
7
+ /**
8
+ * Represents an application built on top of Hyper-Express and Forklaunch.
9
+ *
10
+ * @template SV - A type that extends AnySchemaValidator.
11
+ */
12
+ declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Server, MiddlewareHandler> {
13
+ /**
14
+ * Creates an instance of the Application class.
15
+ *
16
+ * @param {SV} schemaValidator - The schema validator.
17
+ */
18
+ constructor(schemaValidator: SV);
19
+ /**
20
+ * Starts the server and sets up Swagger documentation.
21
+ *
22
+ * @param {string | number} arg0 - The port number or UNIX path to listen on.
23
+ * @param {...unknown[]} args - Additional arguments.
24
+ * @returns {Promise<uWebsockets.us_listen_socket>} - A promise that resolves with the listening socket.
25
+ */
26
+ listen(port: number, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
27
+ listen(port: number, host?: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
28
+ listen(unix_path: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
29
+ }
30
+
31
+ declare class Router<SV extends AnySchemaValidator, BasePath extends `/${string}`> extends ForklaunchExpressLikeRouter<SV, BasePath, MiddlewareHandler, Router$1> implements ForklaunchRouter<SV> {
32
+ basePath: BasePath;
33
+ constructor(basePath: BasePath, schemaValidator: SV);
34
+ route(path: string): this;
35
+ any: TypedMiddlewareDefinition<this, SV>;
36
+ }
37
+
38
+ /**
39
+ * Extends the Forklaunch request interface with properties from Hyper-Express's request interface.
40
+ *
41
+ * @template SV - A type that extends AnySchemaValidator.
42
+ * @template P - A type for request parameters, defaulting to ParamsDictionary.
43
+ * @template _ResBody - A type for the response body, defaulting to unknown.
44
+ * @template ReqBody - A type for the request body, defaulting to unknown.
45
+ * @template ReqQuery - A type for the request query, defaulting to ParsedQs.
46
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
47
+ */
48
+ interface Request<SV extends AnySchemaValidator, P extends ParamsDictionary, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, Omit<Request$1<LocalsObj>, 'method' | 'params' | 'query' | 'headers'> {
49
+ /** The request body */
50
+ body: ReqBody;
51
+ /** The request query parameters */
52
+ query: ReqQuery;
53
+ /** The request parameters */
54
+ params: P;
55
+ }
56
+ /**
57
+ * Extends the Forklaunch response interface with properties from Hyper-Express's response interface.
58
+ *
59
+ * @template ResBody - A type for the response body, defaulting to unknown.
60
+ * @template LocalsObj - A type for local variables, defaulting to an empty object.
61
+ * @template StatusCode - A type for the status code, defaulting to number.
62
+ */
63
+ interface Response<ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> extends ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj>, Omit<Response$1<LocalsObj>, 'getHeaders' | 'setHeader' | 'headersSent' | 'send' | 'status' | 'statusCode' | 'json' | 'jsonp' | 'end'>, ForklaunchStatusResponse<ForklaunchSendableData> {
64
+ /** The body data of the response */
65
+ bodyData: unknown;
66
+ /** If cors are applied to the response */
67
+ cors: boolean;
68
+ /** The status code of the response */
69
+ _status_code: number;
70
+ /** Whether the response is corked */
71
+ _cork: boolean;
72
+ /** Whether the response is currently corked */
73
+ _corked: boolean;
74
+ }
75
+
76
+ type App<SV extends AnySchemaValidator> = Application<SV>;
77
+ /**
78
+ * Creates a new instance of Application with the given schema validator.
79
+ *
80
+ * @template SV - A type that extends AnySchemaValidator.
81
+ * @param {SV} schemaValidator - The schema validator.
82
+ * @returns {Application<SV>} - The new application instance.
83
+ */
84
+ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV): Application<SV>;
85
+ /**
86
+ * Creates a new instance of Router with the given base path and schema validator.
87
+ *
88
+ * @template SV - A type that extends AnySchemaValidator.
89
+ * @param {string} basePath - The base path for the router.
90
+ * @param {SV} schemaValidator - The schema validator.
91
+ * @returns {Router<SV>} - The new router instance.
92
+ */
93
+ declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV): Router<SV, BasePath>;
94
+
95
+ export { type App, Application, type Request, type Response, Router, forklaunchExpress, forklaunchRouter };
@@ -27,14 +27,17 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
- // src/hyperExpressApplication.ts
31
- var hyperExpressApplication_exports = {};
32
- __export(hyperExpressApplication_exports, {
33
- Application: () => Application
30
+ // index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ forklaunchExpress: () => forklaunchExpress,
34
+ forklaunchRouter: () => forklaunchRouter
34
35
  });
35
- module.exports = __toCommonJS(hyperExpressApplication_exports);
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/hyperExpressApplication.ts
36
39
  var import_http = require("@forklaunch/core/http");
37
- var import_hyper_express = require("hyper-express");
40
+ var import_hyper_express_fork = require("@forklaunch/hyper-express-fork");
38
41
 
39
42
  // src/middleware/swagger.middleware.ts
40
43
  var import_live_directory = __toESM(require("live-directory"));
@@ -102,7 +105,7 @@ var Application = class extends import_http.ForklaunchExpressLikeApplication {
102
105
  * @param {SV} schemaValidator - The schema validator.
103
106
  */
104
107
  constructor(schemaValidator) {
105
- super(schemaValidator, new import_hyper_express.Server());
108
+ super(schemaValidator, new import_hyper_express_fork.Server());
106
109
  }
107
110
  listen(arg0, arg1, arg2) {
108
111
  if (typeof arg0 === "number") {
@@ -137,7 +140,120 @@ ${err.message}`);
137
140
  );
138
141
  }
139
142
  };
143
+
144
+ // src/hyperExpressRouter.ts
145
+ var import_http3 = require("@forklaunch/core/http");
146
+ var import_hyper_express_fork2 = require("@forklaunch/hyper-express-fork");
147
+
148
+ // src/middleware/contentParse.middleware.ts
149
+ async function contentParse(req) {
150
+ console.debug("[MIDDLEWARE] contentParse started");
151
+ switch (req.headers["content-type"] && req.headers["content-type"].split(";")[0]) {
152
+ case "application/json":
153
+ req.body = await req.json();
154
+ break;
155
+ case "application/x-www-form-urlencoded":
156
+ req.body = await req.urlencoded();
157
+ break;
158
+ case "text/plain":
159
+ req.body = await req.text();
160
+ break;
161
+ case "application/octet-stream":
162
+ req.body = await req.buffer();
163
+ break;
164
+ default:
165
+ req.body = await req.json();
166
+ break;
167
+ }
168
+ }
169
+
170
+ // src/middleware/enrichResponseTransmission.middleware.ts
171
+ var import_http2 = require("@forklaunch/core/http");
172
+ function enrichResponseTransmission(req, res, next) {
173
+ console.debug("[MIDDLEWARE] enrichResponseTransmission");
174
+ const originalSend = res.send;
175
+ const originalJson = res.json;
176
+ const originalSetHeader = res.setHeader;
177
+ res.json = function(data) {
178
+ res.bodyData = data;
179
+ const result = originalJson.call(this, data);
180
+ return result;
181
+ };
182
+ res.send = function(data) {
183
+ if (!res.bodyData) {
184
+ res.bodyData = data;
185
+ res.statusCode = res._status_code;
186
+ }
187
+ return (0, import_http2.enrichExpressLikeSend)(
188
+ this,
189
+ req,
190
+ res,
191
+ originalSend,
192
+ data,
193
+ !res.cors && (res._cork && !res._corked || !res._cork)
194
+ );
195
+ };
196
+ res.setHeader = function(name, value) {
197
+ let stringifiedValue;
198
+ if (Array.isArray(value)) {
199
+ stringifiedValue = value.map(
200
+ (v) => typeof v !== "string" ? JSON.stringify(v) : v
201
+ );
202
+ } else {
203
+ stringifiedValue = typeof value !== "string" ? JSON.stringify(value) : value;
204
+ }
205
+ return originalSetHeader.call(this, name, stringifiedValue);
206
+ };
207
+ next();
208
+ }
209
+
210
+ // src/middleware/polyfillGetHeaders.middleware.ts
211
+ function polyfillGetHeaders(_req, res, next) {
212
+ console.debug("[MIDDLEWARE] polyfillGetHeaders started");
213
+ res.getHeaders = () => {
214
+ return res._headers;
215
+ };
216
+ next?.();
217
+ }
218
+
219
+ // src/hyperExpressRouter.ts
220
+ var Router = class extends import_http3.ForklaunchExpressLikeRouter {
221
+ constructor(basePath, schemaValidator) {
222
+ super(basePath, schemaValidator, new import_hyper_express_fork2.Router());
223
+ this.basePath = basePath;
224
+ this.internal.use(polyfillGetHeaders);
225
+ this.internal.use(contentParse);
226
+ this.internal.use(
227
+ enrichResponseTransmission
228
+ );
229
+ }
230
+ route(path) {
231
+ this.internal.route(path);
232
+ return this;
233
+ }
234
+ any = (pathOrContractDetailsOrMiddlewareOrTypedHandler, contractDetailsOrMiddlewareOrTypedHandler, ...middlewareOrMiddlewareWithTypedHandler) => {
235
+ return this.registerMiddlewareHandler(
236
+ this.internal.any,
237
+ pathOrContractDetailsOrMiddlewareOrTypedHandler,
238
+ contractDetailsOrMiddlewareOrTypedHandler,
239
+ ...middlewareOrMiddlewareWithTypedHandler
240
+ );
241
+ };
242
+ // TODO: Implement the rest of the methods
243
+ // upgrade
244
+ // ws
245
+ };
246
+
247
+ // index.ts
248
+ function forklaunchExpress(schemaValidator) {
249
+ return new Application(schemaValidator);
250
+ }
251
+ function forklaunchRouter(basePath, schemaValidator) {
252
+ const router = new Router(basePath, schemaValidator);
253
+ return router;
254
+ }
140
255
  // Annotate the CommonJS export names for ESM import in node:
141
256
  0 && (module.exports = {
142
- Application
257
+ forklaunchExpress,
258
+ forklaunchRouter
143
259
  });
package/lib/index.mjs ADDED
@@ -0,0 +1,230 @@
1
+ // src/hyperExpressApplication.ts
2
+ import {
3
+ ForklaunchExpressLikeApplication,
4
+ generateSwaggerDocument
5
+ } from "@forklaunch/core/http";
6
+ import { Server } from "@forklaunch/hyper-express-fork";
7
+
8
+ // src/middleware/swagger.middleware.ts
9
+ import LiveDirectory from "live-directory";
10
+ import getAbsoluteSwaggerFsPath from "swagger-ui-dist/absolute-path";
11
+ import swaggerUi from "swagger-ui-express";
12
+ function swaggerRedirect(path) {
13
+ return (req, res, next) => {
14
+ if (req.path === path) {
15
+ res.redirect(`${path}/`);
16
+ }
17
+ return next?.();
18
+ };
19
+ }
20
+ function swagger(path, document, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle) {
21
+ const LiveAssets = new LiveDirectory(getAbsoluteSwaggerFsPath(), {
22
+ filter: {
23
+ keep: {
24
+ names: [
25
+ "swagger-ui-bundle.js",
26
+ "swagger-ui-standalone-preset.js",
27
+ "swagger-ui-init.js",
28
+ "swagger-ui.css",
29
+ "favicon-32x32.png",
30
+ "favicon-16x16.png"
31
+ ]
32
+ }
33
+ },
34
+ cache: {
35
+ max_file_count: 10,
36
+ max_file_size: 1024 * 1024 * 1.5
37
+ }
38
+ });
39
+ const serve = swaggerUi.serve[0];
40
+ const staticAssets = (req, res, next) => {
41
+ const filePath = req.path.replace(path, "");
42
+ const file = LiveAssets.get(filePath);
43
+ if (file === void 0) {
44
+ if (next) {
45
+ return next();
46
+ }
47
+ return res.status(404).send();
48
+ }
49
+ const fileParts = file.path.split(".");
50
+ const extension = fileParts[fileParts.length - 1];
51
+ const content = file.content;
52
+ return res.type(extension).send(content);
53
+ };
54
+ const ui = swaggerUi.setup(
55
+ document,
56
+ opts,
57
+ options,
58
+ customCss,
59
+ customfavIcon,
60
+ swaggerUrl,
61
+ customSiteTitle
62
+ );
63
+ return [serve, staticAssets, ui];
64
+ }
65
+
66
+ // src/hyperExpressApplication.ts
67
+ var Application = class extends ForklaunchExpressLikeApplication {
68
+ /**
69
+ * Creates an instance of the Application class.
70
+ *
71
+ * @param {SV} schemaValidator - The schema validator.
72
+ */
73
+ constructor(schemaValidator) {
74
+ super(schemaValidator, new Server());
75
+ }
76
+ listen(arg0, arg1, arg2) {
77
+ if (typeof arg0 === "number") {
78
+ const port = arg0 || Number(process.env.PORT);
79
+ this.internal.set_error_handler((_req, res, err) => {
80
+ res.locals.errorMessage = err.message;
81
+ console.error(err);
82
+ res.status(
83
+ res.statusCode && res.statusCode >= 400 ? res.statusCode : 500
84
+ ).send(`Internal server error:
85
+
86
+ ${err.message}`);
87
+ });
88
+ const swaggerPath = `/api${process.env.VERSION ?? "/v1"}${process.env.SWAGGER_PATH ?? "/swagger"}`;
89
+ this.internal.use(swaggerPath, swaggerRedirect(swaggerPath));
90
+ this.internal.get(
91
+ `${swaggerPath}/*`,
92
+ swagger(
93
+ swaggerPath,
94
+ generateSwaggerDocument(this.schemaValidator, port, this.routers)
95
+ )
96
+ );
97
+ if (arg1 && typeof arg1 === "string") {
98
+ return this.internal.listen(port, arg1, arg2);
99
+ } else if (arg1 && typeof arg1 === "function") {
100
+ return this.internal.listen(port, arg1);
101
+ }
102
+ }
103
+ return this.internal.listen(
104
+ arg0,
105
+ arg1
106
+ );
107
+ }
108
+ };
109
+
110
+ // src/hyperExpressRouter.ts
111
+ import {
112
+ ForklaunchExpressLikeRouter
113
+ } from "@forklaunch/core/http";
114
+ import {
115
+ Router as ExpressRouter
116
+ } from "@forklaunch/hyper-express-fork";
117
+
118
+ // src/middleware/contentParse.middleware.ts
119
+ async function contentParse(req) {
120
+ console.debug("[MIDDLEWARE] contentParse started");
121
+ switch (req.headers["content-type"] && req.headers["content-type"].split(";")[0]) {
122
+ case "application/json":
123
+ req.body = await req.json();
124
+ break;
125
+ case "application/x-www-form-urlencoded":
126
+ req.body = await req.urlencoded();
127
+ break;
128
+ case "text/plain":
129
+ req.body = await req.text();
130
+ break;
131
+ case "application/octet-stream":
132
+ req.body = await req.buffer();
133
+ break;
134
+ default:
135
+ req.body = await req.json();
136
+ break;
137
+ }
138
+ }
139
+
140
+ // src/middleware/enrichResponseTransmission.middleware.ts
141
+ import {
142
+ enrichExpressLikeSend
143
+ } from "@forklaunch/core/http";
144
+ function enrichResponseTransmission(req, res, next) {
145
+ console.debug("[MIDDLEWARE] enrichResponseTransmission");
146
+ const originalSend = res.send;
147
+ const originalJson = res.json;
148
+ const originalSetHeader = res.setHeader;
149
+ res.json = function(data) {
150
+ res.bodyData = data;
151
+ const result = originalJson.call(this, data);
152
+ return result;
153
+ };
154
+ res.send = function(data) {
155
+ if (!res.bodyData) {
156
+ res.bodyData = data;
157
+ res.statusCode = res._status_code;
158
+ }
159
+ return enrichExpressLikeSend(
160
+ this,
161
+ req,
162
+ res,
163
+ originalSend,
164
+ data,
165
+ !res.cors && (res._cork && !res._corked || !res._cork)
166
+ );
167
+ };
168
+ res.setHeader = function(name, value) {
169
+ let stringifiedValue;
170
+ if (Array.isArray(value)) {
171
+ stringifiedValue = value.map(
172
+ (v) => typeof v !== "string" ? JSON.stringify(v) : v
173
+ );
174
+ } else {
175
+ stringifiedValue = typeof value !== "string" ? JSON.stringify(value) : value;
176
+ }
177
+ return originalSetHeader.call(this, name, stringifiedValue);
178
+ };
179
+ next();
180
+ }
181
+
182
+ // src/middleware/polyfillGetHeaders.middleware.ts
183
+ function polyfillGetHeaders(_req, res, next) {
184
+ console.debug("[MIDDLEWARE] polyfillGetHeaders started");
185
+ res.getHeaders = () => {
186
+ return res._headers;
187
+ };
188
+ next?.();
189
+ }
190
+
191
+ // src/hyperExpressRouter.ts
192
+ var Router = class extends ForklaunchExpressLikeRouter {
193
+ constructor(basePath, schemaValidator) {
194
+ super(basePath, schemaValidator, new ExpressRouter());
195
+ this.basePath = basePath;
196
+ this.internal.use(polyfillGetHeaders);
197
+ this.internal.use(contentParse);
198
+ this.internal.use(
199
+ enrichResponseTransmission
200
+ );
201
+ }
202
+ route(path) {
203
+ this.internal.route(path);
204
+ return this;
205
+ }
206
+ any = (pathOrContractDetailsOrMiddlewareOrTypedHandler, contractDetailsOrMiddlewareOrTypedHandler, ...middlewareOrMiddlewareWithTypedHandler) => {
207
+ return this.registerMiddlewareHandler(
208
+ this.internal.any,
209
+ pathOrContractDetailsOrMiddlewareOrTypedHandler,
210
+ contractDetailsOrMiddlewareOrTypedHandler,
211
+ ...middlewareOrMiddlewareWithTypedHandler
212
+ );
213
+ };
214
+ // TODO: Implement the rest of the methods
215
+ // upgrade
216
+ // ws
217
+ };
218
+
219
+ // index.ts
220
+ function forklaunchExpress(schemaValidator) {
221
+ return new Application(schemaValidator);
222
+ }
223
+ function forklaunchRouter(basePath, schemaValidator) {
224
+ const router = new Router(basePath, schemaValidator);
225
+ return router;
226
+ }
227
+ export {
228
+ forklaunchExpress,
229
+ forklaunchRouter
230
+ };
package/package.json CHANGED
@@ -1,24 +1,32 @@
1
1
  {
2
2
  "name": "@forklaunch/hyper-express",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Forklaunch framework for hyper-express.",
5
- "files": [
6
- "lib/**"
7
- ],
8
- "types": "lib/index.d.ts",
5
+ "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/forklaunch/forklaunch-js/issues"
8
+ },
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/forklaunch/forklaunch-js.git"
12
12
  },
13
- "author": "Rohin Bhargava",
14
13
  "license": "MIT",
15
- "bugs": {
16
- "url": "https://github.com/forklaunch/forklaunch-js/issues"
14
+ "author": "Rohin Bhargava",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./lib/index.d.ts",
18
+ "import": "./lib/index.mjs",
19
+ "require": "./lib/index.js",
20
+ "default": "./lib/index.js"
21
+ }
17
22
  },
18
- "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
23
+ "types": "lib/index.d.ts",
24
+ "files": [
25
+ "lib/**"
26
+ ],
19
27
  "dependencies": {
28
+ "@forklaunch/hyper-express-fork": "6.17.3",
20
29
  "cors": "^2.8.5",
21
- "hyper-express": "6.17.3",
22
30
  "live-directory": "^3.0.3",
23
31
  "openapi3-ts": "^4.4.0",
24
32
  "qs": "^6.13.1",
@@ -46,22 +54,14 @@
46
54
  "typescript": "^5.7.3",
47
55
  "typescript-eslint": "^8.19.1"
48
56
  },
49
- "exports": {
50
- ".": {
51
- "types": "./lib/index.d.ts",
52
- "import": "./lib/index.mjs",
53
- "require": "./lib/index.js",
54
- "default": "./lib/index.js"
55
- }
56
- },
57
57
  "scripts": {
58
- "test": "vitest --passWithNoTests",
59
- "build": "tsc --noEmit && tsup ./src --format cjs,esm --no-splitting --dts --tsconfig tsconfig.json --out-dir lib --clean",
58
+ "build": "tsc --noEmit && tsup index.ts --format cjs,esm --no-splitting --dts --tsconfig tsconfig.json --out-dir lib --clean",
60
59
  "clean": "rm -rf lib pnpm.lock.yaml node_modules",
61
60
  "docs": "typedoc --out docs *",
61
+ "format": "prettier --ignore-path=.prettierignore --config .prettierrc '**/*.{ts,json}' --write",
62
62
  "lint": "eslint . -c eslint.config.mjs",
63
63
  "lint:fix": "eslint . -c eslint.config.mjs --fix",
64
- "format": "prettier --ignore-path=.prettierignore --config .prettierrc '**/*.ts' --write",
65
- "publish:package": "./publish-package.bash"
64
+ "publish:package": "./publish-package.bash",
65
+ "test": "vitest --passWithNoTests"
66
66
  }
67
67
  }
@@ -1,30 +0,0 @@
1
- import { ForklaunchExpressLikeApplication } from '@forklaunch/core/http';
2
- import { AnySchemaValidator } from '@forklaunch/validator';
3
- import { Server, MiddlewareHandler } from 'hyper-express';
4
- import * as uWebsockets from 'uWebSockets.js';
5
-
6
- /**
7
- * Represents an application built on top of Hyper-Express and Forklaunch.
8
- *
9
- * @template SV - A type that extends AnySchemaValidator.
10
- */
11
- declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Server, MiddlewareHandler> {
12
- /**
13
- * Creates an instance of the Application class.
14
- *
15
- * @param {SV} schemaValidator - The schema validator.
16
- */
17
- constructor(schemaValidator: SV);
18
- /**
19
- * Starts the server and sets up Swagger documentation.
20
- *
21
- * @param {string | number} arg0 - The port number or UNIX path to listen on.
22
- * @param {...unknown[]} args - Additional arguments.
23
- * @returns {Promise<uWebsockets.us_listen_socket>} - A promise that resolves with the listening socket.
24
- */
25
- listen(port: number, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
26
- listen(port: number, host?: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
27
- listen(unix_path: string, callback?: (listen_socket: uWebsockets.us_listen_socket) => void): Promise<uWebsockets.us_listen_socket>;
28
- }
29
-
30
- export { Application };