@miqro/core 1.7.2 → 1.7.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/dist/common/context.d.ts +2 -1
- package/dist/common/context.js +27 -11
- package/dist/proxy.js +28 -10
- package/package.json +4 -4
package/dist/common/context.d.ts
CHANGED
|
@@ -40,7 +40,8 @@ export declare class Context {
|
|
|
40
40
|
addVaryHeader(value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
41
41
|
setHeader(name: string, value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
42
42
|
setCookie(name: string, value: string, options?: CookieSerializeOptions): ServerResponse;
|
|
43
|
-
|
|
43
|
+
write(chunk: any): Promise<void>;
|
|
44
|
+
end(args?: {
|
|
44
45
|
body?: any;
|
|
45
46
|
status: number;
|
|
46
47
|
headers: OutgoingHttpHeaders;
|
package/dist/common/context.js
CHANGED
|
@@ -47,7 +47,19 @@ class Context {
|
|
|
47
47
|
setCookie(name, value, options) {
|
|
48
48
|
return this.setHeader('Set-Cookie', (0, cookie_1.serialize)(name, String(value), options));
|
|
49
49
|
}
|
|
50
|
-
async
|
|
50
|
+
async write(chunk) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
this.res.write(chunk, function (error) {
|
|
53
|
+
if (error) {
|
|
54
|
+
reject(error);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async end(args) {
|
|
51
63
|
if (this.res.headersSent) {
|
|
52
64
|
return Promise.reject(new Error("already ended"));
|
|
53
65
|
}
|
|
@@ -57,19 +69,23 @@ class Context {
|
|
|
57
69
|
reject(new Error("already ended"));
|
|
58
70
|
}
|
|
59
71
|
else {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
const body = args ? args.body : undefined;
|
|
73
|
+
if (args !== undefined) {
|
|
74
|
+
const { status, headers } = args;
|
|
75
|
+
this.res.statusCode = status;
|
|
76
|
+
const keys = Object.keys(headers);
|
|
77
|
+
for (const key of keys) {
|
|
78
|
+
if (exports.IGNORE_HEADER.indexOf(key.toLocaleLowerCase()) !== -1) {
|
|
79
|
+
this.logger.warn("ignoring " + key);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (headers[key] !== undefined) {
|
|
83
|
+
this.res.setHeader(key, headers[key]);
|
|
84
|
+
}
|
|
69
85
|
}
|
|
70
86
|
}
|
|
71
87
|
this.res.setHeader("connection", "close");
|
|
72
|
-
this.res.end(body !== undefined ? (body instanceof Buffer ? body : String(body)) :
|
|
88
|
+
this.res.end(body !== undefined ? (body instanceof Buffer ? body : String(body)) : null, () => {
|
|
73
89
|
resolve();
|
|
74
90
|
});
|
|
75
91
|
}
|
package/dist/proxy.js
CHANGED
|
@@ -26,6 +26,7 @@ class ProxyRouter extends router_1.Router {
|
|
|
26
26
|
};
|
|
27
27
|
delete headers["connection"];
|
|
28
28
|
delete headers["keep-alive"];
|
|
29
|
+
let responseHeaders;
|
|
29
30
|
const requestArgs = {
|
|
30
31
|
url: url.toString(),
|
|
31
32
|
query: ctx.query,
|
|
@@ -34,21 +35,38 @@ class ProxyRouter extends router_1.Router {
|
|
|
34
35
|
disableUserAgent: true,
|
|
35
36
|
method: ctx.method,
|
|
36
37
|
headers,
|
|
38
|
+
onChunk: async function (chunk, res) {
|
|
39
|
+
const responseHeaders = {
|
|
40
|
+
...res.headers
|
|
41
|
+
};
|
|
42
|
+
delete responseHeaders["connection"];
|
|
43
|
+
delete responseHeaders["keep-alive"];
|
|
44
|
+
if (!ctx.res.headersSent) {
|
|
45
|
+
const headerNames = Object.keys(responseHeaders);
|
|
46
|
+
for (const name of headerNames) {
|
|
47
|
+
ctx.setHeader(name, responseHeaders[name]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (res.statusCode !== undefined) {
|
|
51
|
+
ctx.res.statusCode = res.statusCode;
|
|
52
|
+
}
|
|
53
|
+
await ctx.write(chunk);
|
|
54
|
+
},
|
|
37
55
|
data: ctx.method !== "GET" ? ctx.buffer : undefined
|
|
38
56
|
};
|
|
39
57
|
ctx.logger.info("proxy to %s", url.toString());
|
|
40
58
|
ctx.logger.debug("proxy with %o", requestArgs);
|
|
41
59
|
const response = await (0, request_1.request)(requestArgs);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
if (!ctx.res.headersSent) {
|
|
61
|
+
const responseHeaders = {
|
|
62
|
+
...response.headers
|
|
63
|
+
};
|
|
64
|
+
const headerNames = Object.keys(responseHeaders);
|
|
65
|
+
for (const name of headerNames) {
|
|
66
|
+
ctx.setHeader(name, responseHeaders[name]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
await ctx.end();
|
|
52
70
|
}
|
|
53
71
|
return !ctx.res.headersSent;
|
|
54
72
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miqro/core",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"author": "claukers",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@miqro/test": "0.2.
|
|
19
|
-
"@miqro/test-http": "0.0.
|
|
18
|
+
"@miqro/test": "0.2.8",
|
|
19
|
+
"@miqro/test-http": "0.0.9",
|
|
20
20
|
"@types/node": "18.6.1",
|
|
21
21
|
"typescript": "4.7.4",
|
|
22
22
|
"@types/cookie": "0.5.1"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@miqro/parser": "^0.1.6",
|
|
30
|
-
"@miqro/request": "^0.1.
|
|
30
|
+
"@miqro/request": "^0.1.3",
|
|
31
31
|
"cookie": "0.5.0"
|
|
32
32
|
}
|
|
33
33
|
}
|