@miqro/core 1.5.4 → 1.5.6
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/proxy.d.ts +15 -6
- package/dist/proxy.js +37 -31
- package/dist/proxy.js.map +1 -1
- package/package.json +1 -1
package/dist/proxy.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import { Context
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Context } from "./common";
|
|
2
|
+
import { Router, RouterOptions } from "./router";
|
|
3
|
+
import { PathPartToken } from "./common/tokenize-match";
|
|
4
|
+
export interface ProxyRouterOptions extends RouterOptions {
|
|
5
|
+
url: string;
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
declare class ProxyRouter extends Router {
|
|
8
|
+
private proxyURL;
|
|
9
|
+
constructor(options: ProxyRouterOptions);
|
|
10
|
+
run(ctx: Context, pathTokens?: {
|
|
11
|
+
value: string;
|
|
12
|
+
lower: string;
|
|
13
|
+
}[], prePathTokens?: PathPartToken[]): Promise<boolean>;
|
|
14
|
+
}
|
|
15
|
+
export declare const Proxy: (options: ProxyRouterOptions) => ProxyRouter;
|
|
16
|
+
export {};
|
package/dist/proxy.js
CHANGED
|
@@ -1,39 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Proxy = void 0;
|
|
4
4
|
const request_1 = require("@miqro/request");
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
const router_1 = require("./router");
|
|
6
|
+
const tokenize_match_1 = require("./common/tokenize-match");
|
|
7
|
+
const url_1 = require("url");
|
|
8
|
+
class ProxyRouter extends router_1.Router {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super(options);
|
|
11
|
+
const proxyURL = new url_1.URL(options.url);
|
|
12
|
+
proxyURL.pathname = (0, tokenize_match_1.normalizePath)(proxyURL.pathname);
|
|
13
|
+
this.proxyURL = proxyURL;
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
async run(ctx, pathTokens, prePathTokens = []) {
|
|
16
|
+
const level = prePathTokens.length;
|
|
17
|
+
const shouldContinue = await super.run(ctx, pathTokens, prePathTokens);
|
|
18
|
+
if (shouldContinue) {
|
|
19
|
+
const relative = ctx.path.split("/").map(s => s.trim()).filter(s => s).slice(level).join("/");
|
|
20
|
+
const nUrl = new url_1.URL(this.proxyURL.toString());
|
|
21
|
+
nUrl.pathname = nUrl.pathname + relative;
|
|
22
|
+
const url = nUrl.toString();
|
|
23
|
+
ctx.logger.info("proxy to %s", url.toString());
|
|
24
|
+
const response = await (0, request_1.request)({
|
|
25
|
+
url: url.toString(),
|
|
26
|
+
disableThrow: true,
|
|
27
|
+
disableUserAgent: true,
|
|
28
|
+
method: ctx.method,
|
|
29
|
+
headers: ctx.headers,
|
|
30
|
+
data: ctx.method !== "GET" ? ctx.buffer : undefined
|
|
31
|
+
});
|
|
32
|
+
await ctx.end({
|
|
33
|
+
headers: response.headers,
|
|
34
|
+
body: response.data,
|
|
35
|
+
status: response.status
|
|
36
|
+
});
|
|
17
37
|
}
|
|
18
|
-
|
|
19
|
-
throw e;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
exports.ProxyHandler = ProxyHandler;
|
|
24
|
-
const ProxyResponseHandler = () => async function ProxyResponseHandler(ctx) {
|
|
25
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
26
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
const lastResult = ctx.results[ctx.results.length - 1];
|
|
30
|
-
ctx.logger.debug("response %o", lastResult);
|
|
31
|
-
await ctx.end({
|
|
32
|
-
headers: lastResult.headers,
|
|
33
|
-
body: lastResult.data,
|
|
34
|
-
status: lastResult.status
|
|
35
|
-
});
|
|
38
|
+
return !ctx.res.headersSent;
|
|
36
39
|
}
|
|
40
|
+
}
|
|
41
|
+
const Proxy = (options) => {
|
|
42
|
+
return new ProxyRouter(options);
|
|
37
43
|
};
|
|
38
|
-
exports.
|
|
44
|
+
exports.Proxy = Proxy;
|
|
39
45
|
//# sourceMappingURL=proxy.js.map
|
package/dist/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":";;;AACA,4CAAuC;AACvC,qCAA+C;AAC/C,4DAAqE;AACrE,6BAAwB;AAMxB,MAAM,WAAY,SAAQ,eAAM;IAG9B,YAAY,OAA2B;QACrC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,QAAQ,GAAG,IAAI,SAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACtC,QAAQ,CAAC,QAAQ,GAAG,IAAA,8BAAa,EAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACrD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAY,EAAE,UAA+C,EAAE,gBAAiC,EAAE;QAE1G,MAAM,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC;QACnC,MAAM,cAAc,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC;QAEvE,IAAI,cAAc,EAAE;YAClB,MAAM,QAAQ,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAA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