yxorp 0.1.3 → 0.1.5

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.
@@ -1,49 +1,49 @@
1
- import { Service } from 'typedi';
2
- import { Config } from '../config.service';
3
- import { match, compile } from 'path-to-regexp';
4
- import { RemoteRule } from '../../types/yxorp-config';
5
-
6
-
7
- @Service({
8
- global: true
9
- })
10
- export class RemoteRulesMatcher {
11
- constructor(
12
- private config: Config
13
- ) {
14
- }
15
-
16
- public match(url: string, ws: boolean = false): RemoteRule | undefined {
17
- const rules = this.config.get().remoteRules || [];
18
-
19
- for (let rule of rules) {
20
- if (rule.disable) {
21
- continue;
22
- }
23
-
24
- if (!!rule.ws !== ws) {
25
- continue;
26
- }
27
-
28
- const matchResult = match(rule.path, {
29
- decode: decodeURIComponent
30
- })(url);
31
-
32
- if (matchResult) {
33
- return rule;
34
- }
35
- }
36
- }
37
-
38
- public toPath(url: string, remoteRule: RemoteRule): string | undefined {
39
- const matchResult = match(remoteRule.path, {
40
- decode: decodeURIComponent
41
- })(url);
42
-
43
- if (matchResult) {
44
- const toPath = compile(remoteRule.target, { validate: false });
45
- return toPath(matchResult.params);
46
- }
47
- }
48
-
49
- }
1
+ import { Service } from 'typedi';
2
+ import { Config } from '../config.service';
3
+ import { match, compile } from 'path-to-regexp';
4
+ import { RemoteRule } from '../../types/yxorp-config';
5
+
6
+
7
+ @Service({
8
+ global: true
9
+ })
10
+ export class RemoteRulesMatcher {
11
+ constructor(
12
+ private config: Config
13
+ ) {
14
+ }
15
+
16
+ public match(url: string, ws: boolean = false): RemoteRule | undefined {
17
+ const rules = this.config.get().remoteRules || [];
18
+
19
+ for (let rule of rules) {
20
+ if (rule.disable) {
21
+ continue;
22
+ }
23
+
24
+ if (!!rule.ws !== ws) {
25
+ continue;
26
+ }
27
+
28
+ const matchResult = match(rule.path, {
29
+ decode: decodeURIComponent
30
+ })(url);
31
+
32
+ if (matchResult) {
33
+ return rule;
34
+ }
35
+ }
36
+ }
37
+
38
+ public toPath(url: string, remoteRule: RemoteRule): string | undefined {
39
+ const matchResult = match(remoteRule.path, {
40
+ decode: decodeURIComponent
41
+ })(url);
42
+
43
+ if (matchResult) {
44
+ const toPath = compile(remoteRule.target, { validate: false });
45
+ return toPath(matchResult.params);
46
+ }
47
+ }
48
+
49
+ }
@@ -1,48 +1,48 @@
1
- import { Service } from 'typedi';
2
- import { Config } from '../config.service';
3
- import { match } from 'path-to-regexp';
4
- import { RewriteRule } from '../../types/yxorp-config';
5
-
6
-
7
- @Service({
8
- global: true
9
- })
10
- export class RewriteRulesMatcher {
11
- constructor(
12
- private config: Config
13
- ) {
14
- }
15
-
16
- public match(url: string, method: string): RewriteRule | undefined {
17
- const rewriteRules = this.config.get().rewriteRules || [];
18
-
19
- for (let rewriteRule of rewriteRules) {
20
- if (rewriteRule.disable) {
21
- continue;
22
- }
23
-
24
- if (rewriteRule.method.toLowerCase() !== method.toLowerCase()) {
25
- continue;
26
- }
27
-
28
- const matchResult = match<Record<string, string>>(rewriteRule.path, {
29
- decode: decodeURIComponent,
30
- })(url);
31
-
32
- if (matchResult) {
33
- return rewriteRule;
34
- }
35
- }
36
- }
37
-
38
- public params(url: string, rewriteRule: RewriteRule): Object | undefined {
39
- const matchResult = match(rewriteRule.path, {
40
- decode: decodeURIComponent,
41
- })(url);
42
-
43
- if (matchResult) {
44
- return matchResult.params;
45
- }
46
- }
47
-
48
- }
1
+ import { Service } from 'typedi';
2
+ import { Config } from '../config.service';
3
+ import { match } from 'path-to-regexp';
4
+ import { RewriteRule } from '../../types/yxorp-config';
5
+
6
+
7
+ @Service({
8
+ global: true
9
+ })
10
+ export class RewriteRulesMatcher {
11
+ constructor(
12
+ private config: Config
13
+ ) {
14
+ }
15
+
16
+ public match(url: string, method: string): RewriteRule | undefined {
17
+ const rewriteRules = this.config.get().rewriteRules || [];
18
+
19
+ for (let rewriteRule of rewriteRules) {
20
+ if (rewriteRule.disable) {
21
+ continue;
22
+ }
23
+
24
+ if (rewriteRule.method.toLowerCase() !== method.toLowerCase()) {
25
+ continue;
26
+ }
27
+
28
+ const matchResult = match<Record<string, string>>(rewriteRule.path, {
29
+ decode: decodeURIComponent,
30
+ })(url);
31
+
32
+ if (matchResult) {
33
+ return rewriteRule;
34
+ }
35
+ }
36
+ }
37
+
38
+ public params(url: string, rewriteRule: RewriteRule): Object | undefined {
39
+ const matchResult = match(rewriteRule.path, {
40
+ decode: decodeURIComponent,
41
+ })(url);
42
+
43
+ if (matchResult) {
44
+ return matchResult.params;
45
+ }
46
+ }
47
+
48
+ }
@@ -1,73 +1,73 @@
1
- import { Service } from 'typedi';
2
- import { IncomingMessage, ServerResponse } from 'http';
3
- import { Duplex } from 'stream';
4
- import { BootstrapMiddleware } from '../middleware/bootstrap.middleware';
5
- import { ProxyMiddleware } from '../middleware/proxy.middleware';
6
- import { RawBodyMiddleware } from '../middleware/rawBody.middleware';
7
- import { ProxyResMiddleware } from '../middleware/proxyRes.middleware';
8
- import { RewriteMiddleware } from '../middleware/rewrite.middleware';
9
- import { MockMiddleware } from '../middleware/mock.middleware';
10
- import { StaticMiddleware } from '../middleware/static.middleware';
11
- import { HttpServer } from './http-server.service';
12
- import { HttpProxy } from './http-proxy.service';
13
- import { RemoteRulesMatcher } from './rules-matchers/remote-rules-matcher.service';
14
- import { Config } from './config.service';
15
- import { LoggerService } from './logger.service';
16
-
17
-
18
- @Service({
19
- global: true
20
- })
21
- export class YxorpServer {
22
- constructor(
23
- private httpServer: HttpServer,
24
- private httpProxy: HttpProxy,
25
- private remoteRulesMatcher: RemoteRulesMatcher,
26
- private config: Config,
27
- private logger: LoggerService,
28
- ) {
29
- this.httpServer.use(
30
- StaticMiddleware,
31
- BootstrapMiddleware,
32
- MockMiddleware,
33
- ProxyMiddleware
34
- );
35
- this.httpProxy.use(
36
- BootstrapMiddleware,
37
- RawBodyMiddleware,
38
- RewriteMiddleware,
39
- ProxyResMiddleware,
40
- );
41
-
42
- this.httpProxy.on('proxyRes', this.onProxyRes);
43
- this.httpServer.addListener('upgrade', this.onServerUpgrade);
44
- }
45
-
46
- private onProxyRes = (proxyRes: IncomingMessage, req: IncomingMessage, res: ServerResponse) => {
47
- this.httpProxy.execute(proxyRes, req, res);
48
- }
49
-
50
- private onServerUpgrade = (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
51
- const url = req.url || '';
52
-
53
- const proxyOptions = this.config.get().proxyOptions;
54
- const remoteRule = this.remoteRulesMatcher.match(url, true);
55
- const target = remoteRule
56
- ? this.remoteRulesMatcher.toPath(url, remoteRule)
57
- : undefined;
58
-
59
- const options = {
60
- ...proxyOptions,
61
- prependPath: !target,
62
- target: target || proxyOptions.target,
63
- }
64
-
65
- this.httpProxy.ws(
66
- req, socket, head, options, (error) => this.logger.info(error)
67
- );
68
- }
69
-
70
- public listen = this.httpServer.listen.bind(this.httpServer) as typeof this.httpServer.listen;
71
-
72
- }
73
-
1
+ import { Service } from 'typedi';
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ import { Duplex } from 'stream';
4
+ import { BootstrapMiddleware } from '../middleware/bootstrap.middleware';
5
+ import { ProxyMiddleware } from '../middleware/proxy.middleware';
6
+ import { RawBodyMiddleware } from '../middleware/rawBody.middleware';
7
+ import { ProxyResMiddleware } from '../middleware/proxyRes.middleware';
8
+ import { RewriteMiddleware } from '../middleware/rewrite.middleware';
9
+ import { MockMiddleware } from '../middleware/mock.middleware';
10
+ import { StaticMiddleware } from '../middleware/static.middleware';
11
+ import { HttpServer } from './http-server.service';
12
+ import { HttpProxy } from './http-proxy.service';
13
+ import { RemoteRulesMatcher } from './rules-matchers/remote-rules-matcher.service';
14
+ import { Config } from './config.service';
15
+ import { LoggerService } from './logger.service';
16
+
17
+
18
+ @Service({
19
+ global: true
20
+ })
21
+ export class YxorpServer {
22
+ constructor(
23
+ private httpServer: HttpServer,
24
+ private httpProxy: HttpProxy,
25
+ private remoteRulesMatcher: RemoteRulesMatcher,
26
+ private config: Config,
27
+ private logger: LoggerService,
28
+ ) {
29
+ this.httpServer.use(
30
+ StaticMiddleware,
31
+ BootstrapMiddleware,
32
+ MockMiddleware,
33
+ ProxyMiddleware
34
+ );
35
+ this.httpProxy.use(
36
+ BootstrapMiddleware,
37
+ RawBodyMiddleware,
38
+ RewriteMiddleware,
39
+ ProxyResMiddleware,
40
+ );
41
+
42
+ this.httpProxy.on('proxyRes', this.onProxyRes);
43
+ this.httpServer.addListener('upgrade', this.onServerUpgrade);
44
+ }
45
+
46
+ private onProxyRes = (proxyRes: IncomingMessage, req: IncomingMessage, res: ServerResponse) => {
47
+ this.httpProxy.execute(proxyRes, req, res);
48
+ }
49
+
50
+ private onServerUpgrade = (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
51
+ const url = req.url || '';
52
+
53
+ const proxyOptions = this.config.get().proxyOptions;
54
+ const remoteRule = this.remoteRulesMatcher.match(url, true);
55
+ const target = remoteRule
56
+ ? this.remoteRulesMatcher.toPath(url, remoteRule)
57
+ : undefined;
58
+
59
+ const options = {
60
+ ...proxyOptions,
61
+ prependPath: !target,
62
+ target: target || proxyOptions.target,
63
+ }
64
+
65
+ this.httpProxy.ws(
66
+ req, socket, head, options, (error) => this.logger.info(error)
67
+ );
68
+ }
69
+
70
+ public listen = this.httpServer.listen.bind(this.httpServer) as typeof this.httpServer.listen;
71
+
72
+ }
73
+
@@ -1,10 +1,10 @@
1
- declare module 'http' {
2
- export interface IncomingMessage {
3
- rawBody?: Buffer;
4
- rewriteRule?: import("./yxorp-config").RewriteRule;
5
- rewriteRuleParams?: Object;
6
- mockRule?: import("./yxorp-config").MockRule;
7
- mockRuleParams?: Object;
8
- query?: Record<string, any>;
9
- }
10
- }
1
+ declare module 'http' {
2
+ export interface IncomingMessage {
3
+ rawBody?: Buffer;
4
+ rewriteRule?: import("./yxorp-config").RewriteRule;
5
+ rewriteRuleParams?: Object;
6
+ mockRule?: import("./yxorp-config").MockRule;
7
+ mockRuleParams?: Object;
8
+ query?: Record<string, any>;
9
+ }
10
+ }
@@ -1,67 +1,67 @@
1
- import { ServerOptions } from 'http-proxy';
2
-
3
- export interface YxorpConfig extends ConfigFile {
4
- proxyOptions: ServerOptions;
5
- }
6
-
7
- export interface ConfigFile {
8
- target: string;
9
- proxyPort: string | number;
10
- scripts?: string[];
11
- remoteRules?: RemoteRule[];
12
- staticRules?: StaticRule[];
13
- mockRules?: MockRule[];
14
- rewriteRules?: RewriteRule[];
15
- }
16
-
17
- export interface RemoteRule {
18
- path: string;
19
- target: string;
20
- ws?: boolean;
21
- disable?: boolean;
22
- }
23
-
24
- export type StaticRule = {
25
- path: string;
26
- directory: string;
27
- caseInsensitive?: boolean;
28
- directoryIndex?: string;
29
- disable?: boolean;
30
- }
31
-
32
- export type MockFileRule = {
33
- method: string;
34
- path: string;
35
- file: string;
36
- statusCode?: number;
37
- disable?: boolean;
38
- }
39
-
40
- export type MockScriptRule = {
41
- method: string;
42
- path: string;
43
- script: string;
44
- disable?: boolean;
45
- }
46
-
47
- export type MockRule = MockFileRule | MockScriptRule;
48
-
49
-
50
- export type RewriteFileRule = {
51
- method: string;
52
- path: string;
53
- file: string;
54
- statusCode?: number;
55
- disable?: boolean;
56
- }
57
-
58
- export type RewriteScriptRule = {
59
- method: string;
60
- path: string;
61
- script: string;
62
- disable?: boolean;
63
- }
64
-
65
- export type RewriteRule = RewriteFileRule | RewriteScriptRule;
66
-
67
-
1
+ import { ServerOptions } from 'http-proxy';
2
+
3
+ export interface YxorpConfig extends ConfigFile {
4
+ proxyOptions: ServerOptions;
5
+ }
6
+
7
+ export interface ConfigFile {
8
+ target: string;
9
+ proxyPort: string | number;
10
+ scripts?: string[];
11
+ remoteRules?: RemoteRule[];
12
+ staticRules?: StaticRule[];
13
+ mockRules?: MockRule[];
14
+ rewriteRules?: RewriteRule[];
15
+ }
16
+
17
+ export interface RemoteRule {
18
+ path: string;
19
+ target: string;
20
+ ws?: boolean;
21
+ disable?: boolean;
22
+ }
23
+
24
+ export type StaticRule = {
25
+ path: string;
26
+ directory: string;
27
+ caseInsensitive?: boolean;
28
+ directoryIndex?: string;
29
+ disable?: boolean;
30
+ }
31
+
32
+ export type MockFileRule = {
33
+ method: string;
34
+ path: string;
35
+ file: string;
36
+ statusCode?: number;
37
+ disable?: boolean;
38
+ }
39
+
40
+ export type MockScriptRule = {
41
+ method: string;
42
+ path: string;
43
+ script: string;
44
+ disable?: boolean;
45
+ }
46
+
47
+ export type MockRule = MockFileRule | MockScriptRule;
48
+
49
+
50
+ export type RewriteFileRule = {
51
+ method: string;
52
+ path: string;
53
+ file: string;
54
+ statusCode?: number;
55
+ disable?: boolean;
56
+ }
57
+
58
+ export type RewriteScriptRule = {
59
+ method: string;
60
+ path: string;
61
+ script: string;
62
+ disable?: boolean;
63
+ }
64
+
65
+ export type RewriteRule = RewriteFileRule | RewriteScriptRule;
66
+
67
+
package/tsconfig.json CHANGED
@@ -1,21 +1,21 @@
1
- {
2
- // This is an alias to @tsconfig/node16: https://github.com/tsconfig/bases
3
- "extends": "ts-node/node12/tsconfig.json",
4
- "ts-node": {
5
- // It is faster to skip typechecking.
6
- // Remove if you want ts-node to do typechecking.
7
- "transpileOnly": true,
8
- "files": true,
9
- "compilerOptions": {
10
- // compilerOptions specified here will override those declared below,
11
- // but *only* in ts-node. Useful if you want ts-node and tsc to use
12
- // different options with a single tsconfig.json.
13
- }
14
- },
15
- "compilerOptions": {
16
- // typescript options here
17
- "resolveJsonModule": true,
18
- "emitDecoratorMetadata": true,
19
- "experimentalDecorators": true
20
- },
21
- }
1
+ {
2
+ // This is an alias to @tsconfig/node16: https://github.com/tsconfig/bases
3
+ "extends": "ts-node/node12/tsconfig.json",
4
+ "ts-node": {
5
+ // It is faster to skip typechecking.
6
+ // Remove if you want ts-node to do typechecking.
7
+ "transpileOnly": true,
8
+ "files": true,
9
+ "compilerOptions": {
10
+ // compilerOptions specified here will override those declared below,
11
+ // but *only* in ts-node. Useful if you want ts-node and tsc to use
12
+ // different options with a single tsconfig.json.
13
+ }
14
+ },
15
+ "compilerOptions": {
16
+ // typescript options here
17
+ "resolveJsonModule": true,
18
+ "emitDecoratorMetadata": true,
19
+ "experimentalDecorators": true
20
+ },
21
+ }