@webtypen/webframez-core 0.1.1 → 0.1.3
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/dev/app.ts +5 -0
- package/dist/Router/Response.d.ts +1 -0
- package/dist/Router/Response.js +40 -0
- package/dist/Router/Router.js +4 -1
- package/package.json +5 -2
- package/src/Router/Response.ts +45 -0
- package/src/Router/Router.ts +5 -1
package/dev/app.ts
CHANGED
|
@@ -32,6 +32,11 @@ app.boot({
|
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
Route.get("/download", async (req: Request, res: Response) => {
|
|
36
|
+
const file = path.join(__dirname, "uploads", "2024-06-09_test.zip");
|
|
37
|
+
return await res.download(file);
|
|
38
|
+
});
|
|
39
|
+
|
|
35
40
|
Route.post("/fileupload", (req: Request, res: Response) => {
|
|
36
41
|
if (!req.message) {
|
|
37
42
|
throw Error("Missing IncomingMessage ...");
|
package/dist/Router/Response.js
CHANGED
|
@@ -8,8 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.Response = void 0;
|
|
16
|
+
const fs_1 = __importDefault(require("fs"));
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
const mime_types_1 = __importDefault(require("mime-types"));
|
|
13
19
|
class Response {
|
|
14
20
|
constructor(options) {
|
|
15
21
|
this.statusCode = 200;
|
|
@@ -85,6 +91,40 @@ class Response {
|
|
|
85
91
|
}
|
|
86
92
|
return this;
|
|
87
93
|
}
|
|
94
|
+
download(filepath, options) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
if (!this.res) {
|
|
98
|
+
return reject("Cannot download: Original response object is missing");
|
|
99
|
+
}
|
|
100
|
+
if (!fs_1.default.existsSync(filepath)) {
|
|
101
|
+
return reject("Cannot download: File '" + filepath + "' does not exist");
|
|
102
|
+
}
|
|
103
|
+
let mimeType = options && options.contentType ? options.contentType : "application/octet-stream";
|
|
104
|
+
if (options && options.inline) {
|
|
105
|
+
let tempMime = mime_types_1.default.lookup(filepath);
|
|
106
|
+
if (tempMime) {
|
|
107
|
+
mimeType = tempMime;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const stats = fs_1.default.lstatSync(filepath);
|
|
111
|
+
this.header("Content-Disposition", (options && options.inline ? "inline" : "attachment") +
|
|
112
|
+
"; filename=" +
|
|
113
|
+
(options && options.filename ? options.filename : path_1.default.basename(filepath)));
|
|
114
|
+
this.header("Content-Type", mimeType);
|
|
115
|
+
this.header("Content-Length", stats.size.toString());
|
|
116
|
+
const readStream = fs_1.default.createReadStream(filepath);
|
|
117
|
+
readStream.pipe(this.res);
|
|
118
|
+
readStream.on("close", () => {
|
|
119
|
+
resolve();
|
|
120
|
+
});
|
|
121
|
+
readStream.on("error", (streamErr) => {
|
|
122
|
+
console.error(streamErr);
|
|
123
|
+
reject(streamErr);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
88
128
|
end() {
|
|
89
129
|
var _a;
|
|
90
130
|
(_a = this.res) === null || _a === void 0 ? void 0 : _a.end();
|
package/dist/Router/Router.js
CHANGED
|
@@ -17,6 +17,7 @@ const http_1 = require("http");
|
|
|
17
17
|
const Response_1 = require("./Response");
|
|
18
18
|
const Request_1 = require("./Request");
|
|
19
19
|
const querystring_1 = __importDefault(require("querystring"));
|
|
20
|
+
const url_1 = __importDefault(require("url"));
|
|
20
21
|
class RouterFacade {
|
|
21
22
|
constructor() {
|
|
22
23
|
/**
|
|
@@ -185,7 +186,6 @@ class RouterFacade {
|
|
|
185
186
|
*/
|
|
186
187
|
dissolveRoute(routes, url, request) {
|
|
187
188
|
if (routes[url]) {
|
|
188
|
-
// If url matches directly -> instant return
|
|
189
189
|
return Object.assign(Object.assign({}, routes[url]), { params: {} });
|
|
190
190
|
}
|
|
191
191
|
else {
|
|
@@ -284,6 +284,7 @@ class RouterFacade {
|
|
|
284
284
|
}
|
|
285
285
|
else if (req && req instanceof http_1.IncomingMessage) {
|
|
286
286
|
const parsedBody = yield this.parseRequestBody(req);
|
|
287
|
+
const parsedUrl = req.url ? url_1.default.parse(req.url) : null;
|
|
287
288
|
request.message = req;
|
|
288
289
|
request.bodyPlain = parsedBody ? parsedBody.plain : "";
|
|
289
290
|
request.body = parsedBody ? parsedBody.parsed : {};
|
|
@@ -292,6 +293,8 @@ class RouterFacade {
|
|
|
292
293
|
request.url = req.url ? req.url : "";
|
|
293
294
|
request.method = req.method ? req.method : "";
|
|
294
295
|
request.socket = req.socket;
|
|
296
|
+
request.query = parsedUrl ? parsedUrl.query : {};
|
|
297
|
+
request.pathname = parsedUrl ? parsedUrl.pathname : "";
|
|
295
298
|
}
|
|
296
299
|
return request;
|
|
297
300
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webtypen/webframez-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
|
|
5
5
|
"homepage": "https://webtypen.de/",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,11 +19,13 @@
|
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsc",
|
|
22
|
-
"build:check": "tsc --noEmit"
|
|
22
|
+
"build:check": "tsc --noEmit",
|
|
23
|
+
"dev": "PORT=3210 ts-node ./dev/app.ts"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@types/busboy": "^1.5.4",
|
|
26
27
|
"@types/lodash": "^4.14.190",
|
|
28
|
+
"@types/mime-types": "^2.1.4",
|
|
27
29
|
"@types/node": "^18.11.9",
|
|
28
30
|
"@types/qs": "^6.9.7",
|
|
29
31
|
"ts-node": "^10.9.1",
|
|
@@ -34,6 +36,7 @@
|
|
|
34
36
|
"commander": "^9.4.1",
|
|
35
37
|
"dotenv": "^8.2.0",
|
|
36
38
|
"lodash": "^4.17.20",
|
|
39
|
+
"mime-types": "^2.1.35",
|
|
37
40
|
"qs": "^6.11.0",
|
|
38
41
|
"querystring": "^0.2.1"
|
|
39
42
|
}
|
package/src/Router/Response.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import mime from "mime-types";
|
|
1
4
|
import { ServerResponse } from "http";
|
|
2
5
|
|
|
3
6
|
export class Response {
|
|
@@ -86,6 +89,48 @@ export class Response {
|
|
|
86
89
|
return this;
|
|
87
90
|
}
|
|
88
91
|
|
|
92
|
+
async download(filepath: string, options?: any) {
|
|
93
|
+
return new Promise((resolve: Function, reject: Function) => {
|
|
94
|
+
if (!this.res) {
|
|
95
|
+
return reject("Cannot download: Original response object is missing");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!fs.existsSync(filepath)) {
|
|
99
|
+
return reject("Cannot download: File '" + filepath + "' does not exist");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let mimeType = options && options.contentType ? options.contentType : "application/octet-stream";
|
|
103
|
+
if (options && options.inline) {
|
|
104
|
+
let tempMime = mime.lookup(filepath);
|
|
105
|
+
if (tempMime) {
|
|
106
|
+
mimeType = tempMime;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const stats = fs.lstatSync(filepath);
|
|
111
|
+
this.header(
|
|
112
|
+
"Content-Disposition",
|
|
113
|
+
(options && options.inline ? "inline" : "attachment") +
|
|
114
|
+
"; filename=" +
|
|
115
|
+
(options && options.filename ? options.filename : path.basename(filepath))
|
|
116
|
+
);
|
|
117
|
+
this.header("Content-Type", mimeType);
|
|
118
|
+
this.header("Content-Length", stats.size.toString());
|
|
119
|
+
|
|
120
|
+
const readStream = fs.createReadStream(filepath);
|
|
121
|
+
readStream.pipe(this.res);
|
|
122
|
+
|
|
123
|
+
readStream.on("close", () => {
|
|
124
|
+
resolve();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
readStream.on("error", (streamErr) => {
|
|
128
|
+
console.error(streamErr);
|
|
129
|
+
reject(streamErr);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
89
134
|
end() {
|
|
90
135
|
this.res?.end();
|
|
91
136
|
return this;
|
package/src/Router/Router.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from "http";
|
|
|
2
2
|
import { Response } from "./Response";
|
|
3
3
|
import { Request } from "./Request";
|
|
4
4
|
import querystring from "querystring";
|
|
5
|
+
import url from "url";
|
|
5
6
|
|
|
6
7
|
type RouteObject = {
|
|
7
8
|
path: string;
|
|
@@ -218,7 +219,6 @@ class RouterFacade {
|
|
|
218
219
|
*/
|
|
219
220
|
dissolveRoute(routes: any, url: string, request?: Request) {
|
|
220
221
|
if (routes[url]) {
|
|
221
|
-
// If url matches directly -> instant return
|
|
222
222
|
return { ...routes[url], params: {} };
|
|
223
223
|
} else {
|
|
224
224
|
for (const route in routes) {
|
|
@@ -321,6 +321,8 @@ class RouterFacade {
|
|
|
321
321
|
request.bodyPlain = options && options.event && options.event.body ? options.event.body : "";
|
|
322
322
|
} else if (req && req instanceof IncomingMessage) {
|
|
323
323
|
const parsedBody: any = await this.parseRequestBody(req);
|
|
324
|
+
const parsedUrl = req.url ? url.parse(req.url) : null;
|
|
325
|
+
|
|
324
326
|
request.message = req;
|
|
325
327
|
request.bodyPlain = parsedBody ? parsedBody.plain : "";
|
|
326
328
|
request.body = parsedBody ? parsedBody.parsed : {};
|
|
@@ -329,6 +331,8 @@ class RouterFacade {
|
|
|
329
331
|
request.url = req.url ? req.url : "";
|
|
330
332
|
request.method = req.method ? req.method : "";
|
|
331
333
|
request.socket = req.socket;
|
|
334
|
+
request.query = parsedUrl ? parsedUrl.query : {};
|
|
335
|
+
request.pathname = parsedUrl ? parsedUrl.pathname : "";
|
|
332
336
|
}
|
|
333
337
|
return request;
|
|
334
338
|
}
|