@webtypen/webframez-core 0.2.68 → 0.3.0
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/Router/Response.d.ts +26 -0
- package/dist/Router/Response.js +83 -0
- package/package.json +1 -1
- package/src/Router/Response.ts +98 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { ServerResponse } from "http";
|
|
3
|
+
import { Request } from "./Request";
|
|
3
4
|
export declare class Response {
|
|
4
5
|
res?: ServerResponse;
|
|
5
6
|
statusCode: number;
|
|
@@ -42,6 +43,31 @@ export declare class Response {
|
|
|
42
43
|
* @returns Response
|
|
43
44
|
*/
|
|
44
45
|
send(content: any): this;
|
|
46
|
+
/**
|
|
47
|
+
* Sends a CSV file to the client
|
|
48
|
+
*
|
|
49
|
+
* @param content
|
|
50
|
+
* @param filename
|
|
51
|
+
* @param options
|
|
52
|
+
* @returns Response
|
|
53
|
+
*/
|
|
54
|
+
sendCsv(content: string | string[][], filename?: string, options?: {
|
|
55
|
+
seperator?: string;
|
|
56
|
+
eol: string;
|
|
57
|
+
skipUtf8BOM?: boolean;
|
|
58
|
+
contentType?: string;
|
|
59
|
+
contentDisposition?: string;
|
|
60
|
+
}): Promise<this>;
|
|
61
|
+
/**
|
|
62
|
+
* Streams a media file to the client
|
|
63
|
+
*
|
|
64
|
+
* @param req
|
|
65
|
+
* @param filepath
|
|
66
|
+
* @param filename
|
|
67
|
+
* @param mimeType
|
|
68
|
+
* @returns void
|
|
69
|
+
*/
|
|
70
|
+
stream(req: Request, filepath: string, filename: string, mimeType: string): Promise<unknown>;
|
|
45
71
|
download(filepath: string, options?: any): Promise<unknown>;
|
|
46
72
|
end(): this;
|
|
47
73
|
registerEvent(eventKey: string, func: any): Promise<void>;
|
package/dist/Router/Response.js
CHANGED
|
@@ -91,6 +91,89 @@ class Response {
|
|
|
91
91
|
}
|
|
92
92
|
return this;
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Sends a CSV file to the client
|
|
96
|
+
*
|
|
97
|
+
* @param content
|
|
98
|
+
* @param filename
|
|
99
|
+
* @param options
|
|
100
|
+
* @returns Response
|
|
101
|
+
*/
|
|
102
|
+
sendCsv(content, filename = "export.csv", options) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
this.header("Content-Type", options && options.contentType ? options.contentType : "text/csv");
|
|
105
|
+
this.header("Content-Disposition", (options && options.contentDisposition ? options.contentDisposition : "attachment") +
|
|
106
|
+
";filename=" +
|
|
107
|
+
(filename ? filename : "export.csv"));
|
|
108
|
+
let str = "";
|
|
109
|
+
if (content && typeof content === "string") {
|
|
110
|
+
str = content;
|
|
111
|
+
}
|
|
112
|
+
else if (content && Array.isArray(content) && content.length > 0) {
|
|
113
|
+
for (let row of content) {
|
|
114
|
+
str +=
|
|
115
|
+
(str.trim() !== "" ? (options && options.eol ? options.eol : "\n") : "") +
|
|
116
|
+
row.join(options && options.seperator ? options.seperator : ";");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return this.send((options && options.skipUtf8BOM ? "" : "\uFEFF") + str);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Streams a media file to the client
|
|
124
|
+
*
|
|
125
|
+
* @param req
|
|
126
|
+
* @param filepath
|
|
127
|
+
* @param filename
|
|
128
|
+
* @param mimeType
|
|
129
|
+
* @returns void
|
|
130
|
+
*/
|
|
131
|
+
stream(req, filepath, filename, mimeType) {
|
|
132
|
+
var _a;
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
const stats = fs_1.default.statSync(filepath);
|
|
135
|
+
const range = ((_a = req.req) === null || _a === void 0 ? void 0 : _a.headers.range) || "";
|
|
136
|
+
const total = stats.size;
|
|
137
|
+
let start = undefined;
|
|
138
|
+
let end = undefined;
|
|
139
|
+
if (range) {
|
|
140
|
+
var parts = range.replace(/bytes=/, "").split("-");
|
|
141
|
+
var partialstart = parts[0];
|
|
142
|
+
var partialend = parts[1];
|
|
143
|
+
start = parseInt(partialstart, 10);
|
|
144
|
+
end = partialend ? parseInt(partialend, 10) : total - 1;
|
|
145
|
+
var chunksize = end - start + 1;
|
|
146
|
+
this.status(206);
|
|
147
|
+
this.header("Content-Range", "bytes " + start + "-" + end + "/" + total);
|
|
148
|
+
this.header("Accept-Ranges", "bytes");
|
|
149
|
+
this.header("Content-Length", chunksize.toString());
|
|
150
|
+
this.header("Content-Type", mimeType);
|
|
151
|
+
this.header("Content-Disposition", `attachment; filename="${filename}"`);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
this.status(200);
|
|
155
|
+
this.header("Accept-Ranges", "bytes");
|
|
156
|
+
this.header("Content-Length", stats.size.toString());
|
|
157
|
+
this.header("Content-Type", mimeType);
|
|
158
|
+
this.header("Content-Disposition", `attachment; filename="${filename}"`);
|
|
159
|
+
}
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
var readStream = fs_1.default.createReadStream(filePath, { start: start, end: end });
|
|
162
|
+
readStream.on("error", (streamError) => {
|
|
163
|
+
console.error("WebframezStreamError:", streamError);
|
|
164
|
+
});
|
|
165
|
+
readStream.on("end", () => { });
|
|
166
|
+
// Prüfe ob res.res verfügbar ist
|
|
167
|
+
if (!this.res) {
|
|
168
|
+
throw new Error("Native Response-Objekt nicht verfügbar");
|
|
169
|
+
}
|
|
170
|
+
readStream.pipe(this.res);
|
|
171
|
+
return new Promise((resolve) => {
|
|
172
|
+
readStream.on("end", resolve);
|
|
173
|
+
readStream.on("error", resolve);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
94
177
|
download(filepath, options) {
|
|
95
178
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
179
|
return new Promise((resolve, reject) => {
|
package/package.json
CHANGED
package/src/Router/Response.ts
CHANGED
|
@@ -2,6 +2,7 @@ import fs from "fs";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import mime from "mime-types";
|
|
4
4
|
import { ServerResponse } from "http";
|
|
5
|
+
import { Request } from "./Request";
|
|
5
6
|
|
|
6
7
|
export class Response {
|
|
7
8
|
res?: ServerResponse;
|
|
@@ -89,6 +90,103 @@ export class Response {
|
|
|
89
90
|
return this;
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Sends a CSV file to the client
|
|
95
|
+
*
|
|
96
|
+
* @param content
|
|
97
|
+
* @param filename
|
|
98
|
+
* @param options
|
|
99
|
+
* @returns Response
|
|
100
|
+
*/
|
|
101
|
+
async sendCsv(
|
|
102
|
+
content: string | string[][],
|
|
103
|
+
filename = "export.csv",
|
|
104
|
+
options?: { seperator?: string; eol: string; skipUtf8BOM?: boolean; contentType?: string; contentDisposition?: string }
|
|
105
|
+
) {
|
|
106
|
+
this.header("Content-Type", options && options.contentType ? options.contentType : "text/csv");
|
|
107
|
+
this.header(
|
|
108
|
+
"Content-Disposition",
|
|
109
|
+
(options && options.contentDisposition ? options.contentDisposition : "attachment") +
|
|
110
|
+
";filename=" +
|
|
111
|
+
(filename ? filename : "export.csv")
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
let str = "";
|
|
115
|
+
if (content && typeof content === "string") {
|
|
116
|
+
str = content;
|
|
117
|
+
} else if (content && Array.isArray(content) && content.length > 0) {
|
|
118
|
+
for (let row of content) {
|
|
119
|
+
str +=
|
|
120
|
+
(str.trim() !== "" ? (options && options.eol ? options.eol : "\n") : "") +
|
|
121
|
+
row.join(options && options.seperator ? options.seperator : ";");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return this.send((options && options.skipUtf8BOM ? "" : "\uFEFF") + str);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Streams a media file to the client
|
|
130
|
+
*
|
|
131
|
+
* @param req
|
|
132
|
+
* @param filepath
|
|
133
|
+
* @param filename
|
|
134
|
+
* @param mimeType
|
|
135
|
+
* @returns void
|
|
136
|
+
*/
|
|
137
|
+
async stream(req: Request, filepath: string, filename: string, mimeType: string) {
|
|
138
|
+
const stats: any = fs.statSync(filepath);
|
|
139
|
+
const range = req.req?.headers.range || "";
|
|
140
|
+
const total = stats.size;
|
|
141
|
+
|
|
142
|
+
let start: any = undefined;
|
|
143
|
+
let end: any = undefined;
|
|
144
|
+
|
|
145
|
+
if (range) {
|
|
146
|
+
var parts = range.replace(/bytes=/, "").split("-");
|
|
147
|
+
var partialstart = parts[0];
|
|
148
|
+
var partialend = parts[1];
|
|
149
|
+
|
|
150
|
+
start = parseInt(partialstart, 10);
|
|
151
|
+
end = partialend ? parseInt(partialend, 10) : total - 1;
|
|
152
|
+
var chunksize = end - start + 1;
|
|
153
|
+
|
|
154
|
+
this.status(206);
|
|
155
|
+
this.header("Content-Range", "bytes " + start + "-" + end + "/" + total);
|
|
156
|
+
this.header("Accept-Ranges", "bytes");
|
|
157
|
+
this.header("Content-Length", chunksize.toString());
|
|
158
|
+
this.header("Content-Type", mimeType);
|
|
159
|
+
this.header("Content-Disposition", `attachment; filename="${filename}"`);
|
|
160
|
+
} else {
|
|
161
|
+
this.status(200);
|
|
162
|
+
this.header("Accept-Ranges", "bytes");
|
|
163
|
+
this.header("Content-Length", stats.size.toString());
|
|
164
|
+
this.header("Content-Type", mimeType);
|
|
165
|
+
this.header("Content-Disposition", `attachment; filename="${filename}"`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
var readStream = fs.createReadStream(filePath, { start: start, end: end });
|
|
170
|
+
|
|
171
|
+
readStream.on("error", (streamError) => {
|
|
172
|
+
console.error("WebframezStreamError:", streamError);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
readStream.on("end", () => {});
|
|
176
|
+
|
|
177
|
+
// Prüfe ob res.res verfügbar ist
|
|
178
|
+
if (!this.res) {
|
|
179
|
+
throw new Error("Native Response-Objekt nicht verfügbar");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
readStream.pipe(this.res);
|
|
183
|
+
|
|
184
|
+
return new Promise((resolve: any) => {
|
|
185
|
+
readStream.on("end", resolve);
|
|
186
|
+
readStream.on("error", resolve);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
92
190
|
async download(filepath: string, options?: any) {
|
|
93
191
|
return new Promise((resolve: Function, reject: Function) => {
|
|
94
192
|
if (!this.res) {
|