godprotocol 2.2.70 → 2.2.81
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/Godprotocol.js
CHANGED
|
@@ -5,7 +5,7 @@ class GodProtocol {
|
|
|
5
5
|
constructor(config) {
|
|
6
6
|
this.db_config = config.db_config;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
this.static_path = config.static_path;
|
|
9
9
|
this.api_key = config.api_key;
|
|
10
10
|
this.platform_uri = config.platform_uri;
|
|
11
11
|
this.route_table = new Route_table(this);
|
|
@@ -25,7 +25,7 @@ class GodProtocol {
|
|
|
25
25
|
|
|
26
26
|
add_router = async (version, routes, opts = {}) => {
|
|
27
27
|
if (opts.is_old) {
|
|
28
|
-
await this.route_table.init_version(
|
|
28
|
+
await this.route_table.init_version(version, { is_old: true });
|
|
29
29
|
this.route_table.versions[version].routes = routes;
|
|
30
30
|
return;
|
|
31
31
|
}
|
package/package.json
CHANGED
|
@@ -176,7 +176,7 @@ class Headers extends Services {
|
|
|
176
176
|
|
|
177
177
|
let third_party = xplatform === this.platform_uri;
|
|
178
178
|
let res = await fetch(
|
|
179
|
-
`${gp_services.profile}/${third_party ? "
|
|
179
|
+
`${gp_services.profile}/${third_party ? "third_party_me" : "me"}`,
|
|
180
180
|
{
|
|
181
181
|
method: "post",
|
|
182
182
|
headers: {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
const serveStaticFile = (req, res) => {
|
|
5
|
+
try {
|
|
6
|
+
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
7
|
+
|
|
8
|
+
let filePath = decodeURIComponent(parsedUrl.pathname);
|
|
9
|
+
|
|
10
|
+
// prevent path traversal attacks
|
|
11
|
+
filePath = filePath.replace(/\.\./g, "");
|
|
12
|
+
|
|
13
|
+
const fullPath = path.join(process.cwd(), filePath);
|
|
14
|
+
console.log(fullPath);
|
|
15
|
+
if (!fs.existsSync(fullPath)) {
|
|
16
|
+
res.statusCode = 404;
|
|
17
|
+
res.setHeader("Content-Type", "application/json");
|
|
18
|
+
return res.end(
|
|
19
|
+
JSON.stringify({
|
|
20
|
+
ok: false,
|
|
21
|
+
message: "File not found",
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const stream = fs.createReadStream(fullPath);
|
|
27
|
+
|
|
28
|
+
// basic content type handling
|
|
29
|
+
const ext = path.extname(fullPath);
|
|
30
|
+
|
|
31
|
+
const mimeTypes = {
|
|
32
|
+
".pdf": "application/pdf",
|
|
33
|
+
".docx":
|
|
34
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
35
|
+
".json": "application/json",
|
|
36
|
+
".txt": "text/plain",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
res.statusCode = 200;
|
|
40
|
+
res.setHeader("Content-Type", mimeTypes[ext] || "application/octet-stream");
|
|
41
|
+
|
|
42
|
+
stream.pipe(res);
|
|
43
|
+
return true;
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.error("Static file error:", err);
|
|
46
|
+
|
|
47
|
+
res.statusCode = 500;
|
|
48
|
+
res.end(
|
|
49
|
+
JSON.stringify({
|
|
50
|
+
ok: false,
|
|
51
|
+
message: "Failed to serve file",
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default serveStaticFile;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import serveStaticFile from "godprotocol/server/serve_static_file.js";
|
|
2
|
+
|
|
1
3
|
const getBody = (req) => {
|
|
2
4
|
return new Promise((resolve, reject) => {
|
|
3
5
|
let data = "";
|
|
@@ -94,8 +96,16 @@ const version_middleware = async (req, res, routers) => {
|
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
let version;
|
|
97
|
-
|
|
98
99
|
if (req.method === "GET") {
|
|
100
|
+
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
101
|
+
|
|
102
|
+
const pathname = parsedUrl.pathname;
|
|
103
|
+
|
|
104
|
+
// STATIC ROUTE DETECTION
|
|
105
|
+
if (routers.static_path && pathname.startsWith(routers.static_path)) {
|
|
106
|
+
return serveStaticFile(req, res);
|
|
107
|
+
}
|
|
108
|
+
|
|
99
109
|
version = "__GET__";
|
|
100
110
|
} else {
|
|
101
111
|
version = req.headers["x-api-version"] || "v1";
|