@adaas/a-server 0.0.20 → 0.0.21
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/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +9 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/components/A-ServerProxy/A-ServerProxy.component.ts +8 -7
- package/src/components/A-StaticLoader/A-StaticLoader.component.types.ts +5 -107
- package/src/containers/A-Service/A-Service.container.ts +11 -7
- package/src/entities/A-Request/A-Request.entity.ts +1 -1
- package/src/entities/A-Request/A-Request.entity.types.ts +1 -1
- package/src/entities/A-Response/A-Response.entity.ts +1 -1
- package/src/entities/A-Response/A-Response.entity.types.ts +1 -1
- package/tsconfig.build.json +0 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "SDK to create a server with ease. Build your backend server with modular structure and use it within or outside the ADAAS ecosystem.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"homepage": "https://github.com/ADAAS-org/adaas-a-server#readme",
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@adaas/a-concept": "^0.1.24",
|
|
52
|
-
"@adaas/a-utils": "^0.1.
|
|
52
|
+
"@adaas/a-utils": "^0.1.11"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/chai": "^4.3.14",
|
|
@@ -4,9 +4,7 @@ import { A_ProxyConfig } from "@adaas/a-server/context/A-ProxyConfig/A_ProxyConf
|
|
|
4
4
|
import { A_Request } from "@adaas/a-server/entities/A-Request/A-Request.entity";
|
|
5
5
|
import { A_Response } from "@adaas/a-server/entities/A-Response/A-Response.entity";
|
|
6
6
|
import { A_Route } from "@adaas/a-server/entities/A-Route/A-Route.entity";
|
|
7
|
-
import { A_Logger } from "@adaas/a-utils";
|
|
8
|
-
import http from "http";
|
|
9
|
-
import https from "https";
|
|
7
|
+
import { A_Logger, A_Polyfill } from "@adaas/a-utils";
|
|
10
8
|
|
|
11
9
|
|
|
12
10
|
export class A_ServerProxy extends A_Component {
|
|
@@ -25,7 +23,7 @@ export class A_ServerProxy extends A_Component {
|
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
|
|
29
27
|
@A_Feature.Extend({
|
|
30
28
|
name: A_SERVER_TYPES__ServerFeature.onRequest,
|
|
31
29
|
})
|
|
@@ -33,9 +31,10 @@ export class A_ServerProxy extends A_Component {
|
|
|
33
31
|
@A_Inject(A_Request) req: A_Request,
|
|
34
32
|
@A_Inject(A_Response) res: A_Response,
|
|
35
33
|
@A_Inject(A_ProxyConfig) proxyConfig: A_ProxyConfig,
|
|
36
|
-
@A_Inject(A_Logger) logger: A_Logger
|
|
34
|
+
@A_Inject(A_Logger) logger: A_Logger,
|
|
35
|
+
@A_Inject(A_Polyfill) polyfill: A_Polyfill
|
|
37
36
|
) {
|
|
38
|
-
return new Promise<void>((resolve, reject) => {
|
|
37
|
+
return new Promise<void>(async (resolve, reject) => {
|
|
39
38
|
const { method, url } = req;
|
|
40
39
|
|
|
41
40
|
const route = new A_Route(url, method);
|
|
@@ -51,7 +50,9 @@ export class A_ServerProxy extends A_Component {
|
|
|
51
50
|
config
|
|
52
51
|
);
|
|
53
52
|
|
|
54
|
-
const client = config.protocol === "https:"
|
|
53
|
+
const client = await (config.protocol === "https:"
|
|
54
|
+
? polyfill.https()
|
|
55
|
+
: polyfill.http());
|
|
55
56
|
|
|
56
57
|
const proxyReq = client.request(
|
|
57
58
|
{
|
|
@@ -1,110 +1,8 @@
|
|
|
1
|
-
import { A_Component, A_Feature, A_Inject } from "@adaas/a-concept"
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { URL } from "url";
|
|
5
|
-
import { A_Request } from "@adaas/a-server/entities/A-Request/A-Request.entity";
|
|
6
|
-
import { A_Response } from "@adaas/a-server/entities/A-Response/A-Response.entity";
|
|
7
|
-
import { A_SERVER_TYPES__ServerFeature } from "@adaas/a-server/containers/A-Service/A-Service.container.types";
|
|
8
|
-
import { A_Logger } from "@adaas/a-utils";
|
|
9
1
|
|
|
10
2
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@A_Feature.Extend({
|
|
18
|
-
name: A_SERVER_TYPES__ServerFeature.onRequest,
|
|
19
|
-
})
|
|
20
|
-
async onRequest(
|
|
21
|
-
@A_Inject(A_Request) req: A_Request,
|
|
22
|
-
@A_Inject(A_Response) res: A_Response,
|
|
23
|
-
@A_Inject(A_Logger) logger: A_Logger
|
|
24
|
-
) {
|
|
25
|
-
|
|
26
|
-
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
27
|
-
return; // Only handle GET and HEAD requests
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const staticDir = path.resolve(process.cwd(), 'public');
|
|
31
|
-
|
|
32
|
-
if (!fs.existsSync(staticDir) || !fs.statSync(staticDir).isDirectory()) {
|
|
33
|
-
logger.log("red", `Static directory ${staticDir} does not exist or is not a directory.`);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const filePath = this.safeFilePath(staticDir, req.url || "/", req.headers.host);
|
|
38
|
-
|
|
39
|
-
await this.serveFile(filePath, res);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
protected getMimeType(ext: string): string {
|
|
45
|
-
const mimeTypes: Record<string, string> = {
|
|
46
|
-
".html": "text/html",
|
|
47
|
-
".js": "application/javascript",
|
|
48
|
-
".css": "text/css",
|
|
49
|
-
".json": "application/json",
|
|
50
|
-
".png": "image/png",
|
|
51
|
-
".jpg": "image/jpeg",
|
|
52
|
-
".jpeg": "image/jpeg",
|
|
53
|
-
".gif": "image/gif",
|
|
54
|
-
".svg": "image/svg+xml",
|
|
55
|
-
".ico": "image/x-icon",
|
|
56
|
-
".txt": "text/plain",
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
return mimeTypes[ext.toLowerCase()] || "application/octet-stream";
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
protected safeFilePath(staticDir: string, reqUrl: string, host?: string): string {
|
|
64
|
-
const parsedUrl = new URL(reqUrl || "/", `http://${host || "localhost"}`);
|
|
65
|
-
let pathname = decodeURIComponent(parsedUrl.pathname);
|
|
66
|
-
|
|
67
|
-
// Prevent path traversal attacks
|
|
68
|
-
pathname = pathname.replace(/\.\.[\/\\]/g, "");
|
|
69
|
-
|
|
70
|
-
let filePath = path.join(staticDir, pathname);
|
|
71
|
-
|
|
72
|
-
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
|
|
73
|
-
filePath = path.join(filePath, "index.html");
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return filePath;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
protected serveFile(filePath: string, res: A_Response): Promise<void> {
|
|
81
|
-
|
|
82
|
-
return new Promise<void>((resolve, reject) => {
|
|
83
|
-
|
|
84
|
-
if (fs.existsSync(filePath)) {
|
|
85
|
-
const ext = path.extname(filePath);
|
|
86
|
-
const contentType = this.getMimeType(ext);
|
|
87
|
-
|
|
88
|
-
res.writeHead(200, { "Content-Type": contentType });
|
|
89
|
-
const stream = fs.createReadStream(filePath);
|
|
90
|
-
stream.pipe(res.original);
|
|
91
|
-
|
|
92
|
-
stream.on('end', () => {
|
|
93
|
-
resolve();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
stream.on("error", (err) => {
|
|
97
|
-
reject(new Error(`File stream error: ${err.message}`));
|
|
98
|
-
});
|
|
99
|
-
} else {
|
|
100
|
-
reject(new Error(`File not found: ${filePath}`));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
resolve();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
3
|
+
export type A_SERVER_TYPES__StaticLoader_Init = {
|
|
4
|
+
/**
|
|
5
|
+
* Path to the static files directory
|
|
6
|
+
*/
|
|
7
|
+
staticFilesPath: string,
|
|
108
8
|
}
|
|
109
|
-
|
|
110
|
-
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { A_SERVER_TYPES__ServerFeature
|
|
1
|
+
import type { IncomingMessage, Server, ServerResponse } from "http";
|
|
2
|
+
import { A_SERVER_TYPES__ServerFeature } from "./A-Service.container.types";
|
|
3
3
|
import { A_Server } from "@adaas/a-server/context/A-Server/A_Server.context";
|
|
4
4
|
import { A_Request } from "@adaas/a-server/entities/A-Request/A-Request.entity";
|
|
5
5
|
import { A_Response } from "@adaas/a-server/entities/A-Response/A-Response.entity";
|
|
6
6
|
import crypto from 'crypto';
|
|
7
7
|
import { A_SERVER_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY, A_TYPES__ServerENVVariables } from "@adaas/a-server/constants/env.constants";
|
|
8
|
-
import { A_Concept, A_Container, A_Feature, A_IdentityHelper, A_Scope } from "@adaas/a-concept";
|
|
9
|
-
import { A_Config, A_Logger } from "@adaas/a-utils";
|
|
8
|
+
import { A_Concept, A_Container, A_Feature, A_IdentityHelper, A_Inject, A_Scope } from "@adaas/a-concept";
|
|
9
|
+
import { A_Config, A_Logger, A_Polyfill } from "@adaas/a-utils";
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
@@ -22,7 +22,9 @@ export class A_Service extends A_Container {
|
|
|
22
22
|
port!: number;
|
|
23
23
|
|
|
24
24
|
@A_Concept.Load()
|
|
25
|
-
async load(
|
|
25
|
+
async load(
|
|
26
|
+
@A_Inject(A_Polyfill) polyfill: A_Polyfill
|
|
27
|
+
) {
|
|
26
28
|
|
|
27
29
|
let config: A_Config<A_TYPES__ServerENVVariables>;
|
|
28
30
|
let aServer: A_Server;
|
|
@@ -52,8 +54,10 @@ export class A_Service extends A_Container {
|
|
|
52
54
|
// Set the server to listen on port 3000
|
|
53
55
|
this.port = config.get('A_SERVER_PORT');
|
|
54
56
|
|
|
57
|
+
const http = await polyfill.http();
|
|
58
|
+
|
|
55
59
|
// Create the HTTP server
|
|
56
|
-
this.server = createServer(this.onRequest.bind(this));
|
|
60
|
+
this.server = http.createServer(this.onRequest.bind(this));
|
|
57
61
|
|
|
58
62
|
}
|
|
59
63
|
|
|
@@ -147,7 +151,7 @@ export class A_Service extends A_Container {
|
|
|
147
151
|
await res.status(200).send();
|
|
148
152
|
|
|
149
153
|
} catch (error) {
|
|
150
|
-
|
|
154
|
+
|
|
151
155
|
const logger = this.scope.resolve(A_Logger);
|
|
152
156
|
|
|
153
157
|
logger.error(error);
|
package/tsconfig.build.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2018",
|
|
4
|
-
"module": "esnext",
|
|
5
|
-
"jsx": "react-jsx",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"resolveJsonModule": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
|
-
"experimentalDecorators": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"baseUrl": ".",
|
|
13
|
-
"paths": {
|
|
14
|
-
"@adaas/a-server/constants/*": ["src/constants/*"],
|
|
15
|
-
"@adaas/a-server/defaults/*": ["src/defaults/*"],
|
|
16
|
-
"@adaas/a-server/decorators/*": ["src/decorators/*"],
|
|
17
|
-
"@adaas/a-server/helpers/*": ["src/helpers/*"],
|
|
18
|
-
"@adaas/a-server/examples/*": ["src/examples/*"],
|
|
19
|
-
"@adaas/a-server/entities/*": ["src/entities/*"],
|
|
20
|
-
"@adaas/a-server/channels/*": ["src/channels/*"],
|
|
21
|
-
"@adaas/a-server/context/*": ["src/context/*"],
|
|
22
|
-
"@adaas/a-server/components/*": ["src/components/*"],
|
|
23
|
-
"@adaas/a-server/containers/*": ["src/containers/*"]
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"include": ["src/**/*", "index.ts"],
|
|
27
|
-
"exclude": ["node_modules", "dist"]
|
|
28
|
-
}
|