yxorp 0.1.2 → 0.1.4

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/.editorconfig CHANGED
@@ -1,16 +1,16 @@
1
- # Editor configuration, see http://editorconfig.org
2
- root = true
3
-
4
- [*]
5
- charset = utf-8
6
- indent_style = space
7
- indent_size = 2
8
- insert_final_newline = true
9
- trim_trailing_whitespace = true
10
-
11
- [*.ts]
12
- quote_type = single
13
-
14
- [*.md]
15
- max_line_length = off
1
+ # Editor configuration, see http://editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ charset = utf-8
6
+ indent_style = space
7
+ indent_size = 2
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
10
+
11
+ [*.ts]
12
+ quote_type = single
13
+
14
+ [*.md]
15
+ max_line_length = off
16
16
  trim_trailing_whitespace = false
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Kirill Suntsov
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Kirill Suntsov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,81 +1,81 @@
1
- # Yxorp
2
- This is a simple CLI reverse proxy utility for rewriting or mocking API data. It is in early development and may contain many bugs 😊
3
-
4
- ## Installation
5
- ```
6
- npm i -g yxopr
7
- ```
8
- ## Usage
9
- Just run yxopr in folder which contains yxopr.json
10
-
11
- ## Config
12
- Simple yxopr.json example:
13
- ```json
14
- {
15
- "#": "The site that will be proxied"
16
- "target": "http://example.com/",
17
- "port": 3002,
18
- "#": "Makes static path with files in directory",
19
- "staticRules": [
20
- {
21
- "path": "/some-path-on-server",
22
- "directory": "./some-path-with-static-files",
23
- "disable": false
24
- }
25
- ],
26
- "#": "Scripts may be using for set some data for mock scripts",
27
- "scripts": [
28
- "./scripts/some-data.js"
29
- ],
30
- "#": "For matching paths in mockRules and rewriteRules uses https://www.npmjs.com/package/path-to-regex",
31
- "#": "This rules uses to modify API data",
32
- "mockRules": [
33
- {
34
- "method": "GET",
35
- "path": "/api/example-one(.*)",
36
- "script": "./mock/api/example-one.js",
37
- "disable": false
38
- }, {
39
- "method": "GET",
40
- "path": "/api/example-two(.*)",
41
- "file": "./mock/api/example-two.json",
42
- "statusCode": 200,
43
- "disable": false
44
- }
45
- ]
46
- "rewriteRules": [
47
- {
48
- "method": "GET",
49
- "path": "/api/example-one(.*)",
50
- "script": "./rewrite/api/example-one.js",
51
- "disable": false
52
- }, {
53
- "method": "GET",
54
- "path": "/api/example-two(.*)",
55
- "file": "./rewrite/api/example-two.json",
56
- "statusCode": 200,
57
- "disable": false
58
- }
59
- ]
60
- }
61
- ```
62
-
63
- ## Mock script example
64
- ```javascript
65
- var data = {
66
- error: "some error",
67
- };
68
-
69
- res.statusCode = 500;
70
- res.end(Buffer.from(JSON.stringify(data)));
71
- ```
72
-
73
- ## Rewrite script example
74
- ```javascript
75
- const data = JSON.parse(body.toString());
76
-
77
- data.modify = 'from js 123';
78
-
79
- result = Buffer.from(JSON.stringify(data));
80
-
81
- ```
1
+ # Yxorp
2
+ This is a simple CLI reverse proxy utility for rewriting or mocking API data. It is in early development and may contain many bugs 😊
3
+
4
+ ## Installation
5
+ ```
6
+ npm i -g yxopr
7
+ ```
8
+ ## Usage
9
+ Just run yxopr in folder which contains yxopr.json
10
+
11
+ ## Config
12
+ Simple yxopr.json example:
13
+ ```json
14
+ {
15
+ "#": "The site that will be proxied"
16
+ "target": "http://example.com/",
17
+ "proxyPort": 3002,
18
+ "#": "Makes static path with files in directory",
19
+ "staticRules": [
20
+ {
21
+ "path": "/some-path-on-server",
22
+ "directory": "./some-path-with-static-files",
23
+ "disable": false
24
+ }
25
+ ],
26
+ "#": "Scripts may be using for set some data for mock scripts",
27
+ "scripts": [
28
+ "./scripts/some-data.js"
29
+ ],
30
+ "#": "For matching paths in mockRules and rewriteRules uses https://www.npmjs.com/package/path-to-regex",
31
+ "#": "This rules uses to modify API data",
32
+ "mockRules": [
33
+ {
34
+ "method": "GET",
35
+ "path": "/api/example-one(.*)",
36
+ "script": "./mock/api/example-one.js",
37
+ "disable": false
38
+ }, {
39
+ "method": "GET",
40
+ "path": "/api/example-two(.*)",
41
+ "file": "./mock/api/example-two.json",
42
+ "statusCode": 200,
43
+ "disable": false
44
+ }
45
+ ]
46
+ "rewriteRules": [
47
+ {
48
+ "method": "GET",
49
+ "path": "/api/example-one(.*)",
50
+ "script": "./rewrite/api/example-one.js",
51
+ "disable": false
52
+ }, {
53
+ "method": "GET",
54
+ "path": "/api/example-two(.*)",
55
+ "file": "./rewrite/api/example-two.json",
56
+ "statusCode": 200,
57
+ "disable": false
58
+ }
59
+ ]
60
+ }
61
+ ```
62
+
63
+ ## Mock script example
64
+ ```javascript
65
+ var data = {
66
+ error: "some error",
67
+ };
68
+
69
+ res.statusCode = 500;
70
+ res.end(Buffer.from(JSON.stringify(data)));
71
+ ```
72
+
73
+ ## Rewrite script example
74
+ ```javascript
75
+ const data = JSON.parse(body.toString());
76
+
77
+ data.modify = 'from js 123';
78
+
79
+ result = Buffer.from(JSON.stringify(data));
80
+
81
+ ```
package/bin/yxorp.js CHANGED
@@ -1,22 +1,22 @@
1
1
  #!/usr/bin/env node
2
-
3
- var path = require('path');
4
- var nodemon = require('nodemon');
5
- var nodemonDefaults = require('nodemon/lib/config/defaults');
6
-
7
- var pathToTsNode = path.normalize(__dirname + './../node_modules/.bin/ts-node');
8
- var filepath = path.normalize(__dirname + './../src/index.ts')
9
-
10
- nodemonDefaults.execMap.ts = pathToTsNode;
11
-
12
- nodemon({
13
- script: filepath,
14
- watch: ['yxopr.json']
15
- });
16
-
17
- nodemon.on('quit', function () {
18
- console.log('Yxopr has quit');
19
- process.exit();
20
- }).on('restart', function (files) {
21
- console.log('Yxopr restarts...');
22
- });
2
+
3
+ var path = require('path');
4
+ var nodemon = require('nodemon');
5
+ var nodemonDefaults = require('nodemon/lib/config/defaults');
6
+
7
+ var pathToTsNode = path.normalize(__dirname + './../node_modules/.bin/ts-node');
8
+ var filepath = path.normalize(__dirname + './../src/index.ts')
9
+
10
+ nodemonDefaults.execMap.ts = pathToTsNode;
11
+
12
+ nodemon({
13
+ script: filepath,
14
+ watch: ['yxorp.json']
15
+ });
16
+
17
+ nodemon.on('quit', function () {
18
+ console.log('Yxopr has quit');
19
+ process.exit();
20
+ }).on('restart', function (files) {
21
+ console.log('Yxopr restarts...');
22
+ });
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "yxorp",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Yxorp is a reverse proxy for rewriting or mocking API data.",
5
5
  "main": "src/index.ts",
6
6
  "bin": {
7
- "yxorp": "./bin/yxorp.js"
7
+ "yxorp": "bin/yxorp.js"
8
8
  },
9
9
  "scripts": {
10
10
  "start": "nodemon src/index.ts"
@@ -20,17 +20,17 @@
20
20
  "qs": "^6.11.2",
21
21
  "reflect-metadata": "^0.1.13",
22
22
  "ts-node": "^10.9.1",
23
+ "tslib": "^2.6.2",
23
24
  "typedi": "^0.10.0",
25
+ "typescript": "^5.2.2",
26
+ "winston": "^3.11.0",
24
27
  "yargs": "^17.7.2"
25
28
  },
26
29
  "devDependencies": {
27
30
  "@types/http-proxy": "^1.17.11",
28
31
  "@types/mime": "^3.0.1",
29
32
  "@types/node": "^20.4.9",
30
- "@types/qs": "^6.9.8",
31
- "tslib": "^2.6.1",
32
- "typescript": "^5.1.6",
33
- "winston": "^3.10.0"
33
+ "@types/qs": "^6.9.8"
34
34
  },
35
35
  "engines": {
36
36
  "node": ">= 12"
package/src/index.ts CHANGED
@@ -1,48 +1,48 @@
1
- import 'reflect-metadata';
2
-
3
- import fs from 'fs';
4
- import path from 'path';
5
- import { Container } from 'typedi';
6
- import { ServerOptions } from 'http-proxy';
7
- import { YxorpServer } from './services/yxorp-server.service';
8
- import { ConfigFile } from './types/yxorp-config';
9
- import { ProxyConfigToken } from './services/config.service';
10
-
11
-
12
- let config: ConfigFile;
13
-
14
- try {
15
- config = JSON.parse(fs.readFileSync('./yxopr.json').toString()) as ConfigFile;
16
- } catch(e) {
17
- console.error(e);
18
- throw 'Can\'t read yxopr.json'
19
- }
20
-
21
- globalThis.require = require;
22
-
23
- const proxyOptions: ServerOptions = {
24
- target: config.target,
25
- changeOrigin: true,
26
- followRedirects: true,
27
- secure: false,
28
- localAddress: '0.0.0.0',
29
- ws: true,
30
- selfHandleResponse: true,
31
- };
32
-
33
- const proxyConfig = {
34
- ...config,
35
- proxyOptions,
36
- }
37
-
38
- config?.scripts?.forEach(script => {
39
- require(path.join(process.cwd(), script));
40
- });
41
-
42
- Container.set(ProxyConfigToken, proxyConfig);
43
-
44
- const server = Container.get(YxorpServer);
45
-
46
- server.listen(config.proxyPort, () => {
47
- console.log(`Yxorp server started successfully on http://localhost:${config.proxyPort}`);
48
- });
1
+ import 'reflect-metadata';
2
+
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { Container } from 'typedi';
6
+ import { ServerOptions } from 'http-proxy';
7
+ import { YxorpServer } from './services/yxorp-server.service';
8
+ import { ConfigFile } from './types/yxorp-config';
9
+ import { ProxyConfigToken } from './services/config.service';
10
+
11
+
12
+ let config: ConfigFile;
13
+
14
+ try {
15
+ config = JSON.parse(fs.readFileSync('./yxorp.json').toString()) as ConfigFile;
16
+ } catch(e) {
17
+ console.error(e);
18
+ throw 'Can\'t read yxorp.json'
19
+ }
20
+
21
+ globalThis.require = require;
22
+
23
+ const proxyOptions: ServerOptions = {
24
+ target: config.target,
25
+ changeOrigin: true,
26
+ followRedirects: true,
27
+ secure: false,
28
+ localAddress: '0.0.0.0',
29
+ ws: true,
30
+ selfHandleResponse: true,
31
+ };
32
+
33
+ const proxyConfig = {
34
+ ...config,
35
+ proxyOptions,
36
+ }
37
+
38
+ config?.scripts?.forEach(script => {
39
+ require(path.join(process.cwd(), script));
40
+ });
41
+
42
+ Container.set(ProxyConfigToken, proxyConfig);
43
+
44
+ const server = Container.get(YxorpServer);
45
+
46
+ server.listen(config.proxyPort, () => {
47
+ console.log(`Yxorp server started successfully on http://localhost:${config.proxyPort}`);
48
+ });
@@ -1,87 +1,87 @@
1
- import { IncomingMessage } from 'http';
2
- import { Service } from 'typedi';
3
- import qs from 'qs';
4
- import { Middleware } from '../services/pipeline.service';
5
- import { RewriteRulesMatcher } from '../services/rules-matchers/rewrite-rules-matcher.service';
6
- import { MockRulesMatcher } from '../services/rules-matchers/mock-rules-matcher.service';
7
- import { LoggerService } from '../services/logger.service';
8
-
9
-
10
- @Service({
11
- global: true
12
- })
13
- export class BootstrapMiddleware<T extends any[]> implements Middleware<T> {
14
- constructor(
15
- private rewriteRulesMatcher: RewriteRulesMatcher,
16
- private mockRulesMatcher: MockRulesMatcher,
17
- private logger: LoggerService,
18
- ) {
19
- }
20
-
21
- public use(...args: [...T, () => void]): void {
22
- const req: IncomingMessage = args[0] as IncomingMessage;
23
- const next: () => void = args[args.length - 1];
24
-
25
- try {
26
- this.setRewriteRule(req);
27
- this.setMockRule(req);
28
- this.setQueryParams(req);
29
-
30
- next();
31
- } catch (e) {
32
- this.logger.error(e);
33
- next();
34
- }
35
- }
36
-
37
- private setRewriteRule(req: IncomingMessage): void {
38
- if (!req.url || !req.method) {
39
- return;
40
- }
41
-
42
- const rewriteRule = this.rewriteRulesMatcher.match(req.url, req.method);
43
-
44
- if (rewriteRule) {
45
- req.rewriteRule = rewriteRule;
46
-
47
- const rewriteRuleParams = this.rewriteRulesMatcher.params(req.url, rewriteRule);
48
-
49
- if (rewriteRuleParams) {
50
- req.rewriteRuleParams = rewriteRuleParams || {};
51
- }
52
- }
53
- }
54
-
55
- private setMockRule(req: IncomingMessage): void {
56
- if (!req.url || !req.method) {
57
- return;
58
- }
59
-
60
- const mockRule = this.mockRulesMatcher.match(req.url, req.method);
61
-
62
- if (mockRule) {
63
- req.mockRule = mockRule;
64
-
65
- const mockRuleParams = this.mockRulesMatcher.params(req.url, mockRule);
66
-
67
- if (mockRuleParams) {
68
- req.mockRuleParams = mockRuleParams || {};
69
- }
70
- }
71
- }
72
-
73
- private setQueryParams(req: IncomingMessage): void {
74
- if (!req.url) {
75
- return;
76
- }
77
-
78
- const url = new URL(req.url, 'http://fake.com');
79
- const query = qs.parse(url.search, {
80
- ignoreQueryPrefix: true,
81
- });
82
-
83
- req.query = query;
84
- }
85
-
86
- }
87
-
1
+ import { IncomingMessage } from 'http';
2
+ import { Service } from 'typedi';
3
+ import qs from 'qs';
4
+ import { Middleware } from '../services/pipeline.service';
5
+ import { RewriteRulesMatcher } from '../services/rules-matchers/rewrite-rules-matcher.service';
6
+ import { MockRulesMatcher } from '../services/rules-matchers/mock-rules-matcher.service';
7
+ import { LoggerService } from '../services/logger.service';
8
+
9
+
10
+ @Service({
11
+ global: true
12
+ })
13
+ export class BootstrapMiddleware<T extends any[]> implements Middleware<T> {
14
+ constructor(
15
+ private rewriteRulesMatcher: RewriteRulesMatcher,
16
+ private mockRulesMatcher: MockRulesMatcher,
17
+ private logger: LoggerService,
18
+ ) {
19
+ }
20
+
21
+ public use(...args: [...T, () => void]): void {
22
+ const req: IncomingMessage = args[0] as IncomingMessage;
23
+ const next: () => void = args[args.length - 1];
24
+
25
+ try {
26
+ this.setRewriteRule(req);
27
+ this.setMockRule(req);
28
+ this.setQueryParams(req);
29
+
30
+ next();
31
+ } catch (e) {
32
+ this.logger.error(e);
33
+ next();
34
+ }
35
+ }
36
+
37
+ private setRewriteRule(req: IncomingMessage): void {
38
+ if (!req.url || !req.method) {
39
+ return;
40
+ }
41
+
42
+ const rewriteRule = this.rewriteRulesMatcher.match(req.url, req.method);
43
+
44
+ if (rewriteRule) {
45
+ req.rewriteRule = rewriteRule;
46
+
47
+ const rewriteRuleParams = this.rewriteRulesMatcher.params(req.url, rewriteRule);
48
+
49
+ if (rewriteRuleParams) {
50
+ req.rewriteRuleParams = rewriteRuleParams || {};
51
+ }
52
+ }
53
+ }
54
+
55
+ private setMockRule(req: IncomingMessage): void {
56
+ if (!req.url || !req.method) {
57
+ return;
58
+ }
59
+
60
+ const mockRule = this.mockRulesMatcher.match(req.url, req.method);
61
+
62
+ if (mockRule) {
63
+ req.mockRule = mockRule;
64
+
65
+ const mockRuleParams = this.mockRulesMatcher.params(req.url, mockRule);
66
+
67
+ if (mockRuleParams) {
68
+ req.mockRuleParams = mockRuleParams || {};
69
+ }
70
+ }
71
+ }
72
+
73
+ private setQueryParams(req: IncomingMessage): void {
74
+ if (!req.url) {
75
+ return;
76
+ }
77
+
78
+ const url = new URL(req.url, 'http://fake.com');
79
+ const query = qs.parse(url.search, {
80
+ ignoreQueryPrefix: true,
81
+ });
82
+
83
+ req.query = query;
84
+ }
85
+
86
+ }
87
+