@cloud-cli/px 1.1.0 → 1.1.2

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/README.md CHANGED
@@ -1,75 +1,63 @@
1
1
  ## PX
2
2
 
3
- Reverse proxy with Nginx
3
+ Reverse proxy to map local services to http(s) subdomains
4
4
 
5
5
  #### Usage
6
6
 
7
- As a module:
8
-
9
7
  ```ts
10
- import px from '@cloud-cli/px';
11
-
12
- px.start({ port: 4567 });
8
+ // cloudy.conf.mjs
13
9
 
14
- px.removeProxy({ domain: 'example.com' });
15
- px.addProxy({ domain: 'example.com', target: 'localhost:1234' });
10
+ import proxy from '@cloud-cli/px';
16
11
 
17
- px.removeCertificate({ domain: 'example.com' });
18
- px.addCertificate({ domain: 'example.com', certificate: '...', key: '...' });
19
-
20
- console.log(px.getDomainList());
21
- console.log(px.getProxyForDomain('example.com'));
12
+ export default { proxy };
22
13
  ```
23
14
 
24
- With Cloudy CLI:
15
+ #### API
25
16
 
26
- ```ts
27
- import px from '@cloud-cli/px';
28
- import { cli } from '@cloud-cli/cy';
17
+ **Add a certificate**
29
18
 
30
- cli.add('px', px);
19
+ ```
20
+ cy proxy.addCertificate --domain example.com --certificate @path/to/cert --key @path/to/key
31
21
  ```
32
22
 
33
- **start() options**
23
+ **Remove a certificate**
34
24
 
35
- | Property | Type | Default |
36
- | -------- | ------ | ----------- |
37
- | `host` | String | '127.0.0.1' |
38
- | `port` | Number | |
25
+ ```
26
+ cy proxy.removeCertificate --domain example.com
27
+ ```
39
28
 
40
- #### HTTP API
29
+ **Add a proxy to a local service**
41
30
 
42
- **Add a certificate**
31
+ ```
32
+ cy proxy.addProxy --domain "foo.example.com" --target "localhost:1234"
33
+ ```
34
+
35
+ **Remove a proxy**
43
36
 
44
37
  ```
45
- POST /certificates
38
+ cy proxy.removeProxy --domain "foo.example.com"
39
+ ```
46
40
 
47
- {
48
- "domain": "example.com",
49
- "certificate": "...",
50
- "key": "...",
51
- }
41
+ **Get details of a proxy**
52
42
 
43
+ ```
44
+ cy proxy.getProxy --domain "foo.example.com"
53
45
  ```
54
46
 
55
- **Add a certificate**
47
+ **List proxies**
56
48
 
57
49
  ```
58
- DELETE /certificates/example.com
50
+ cy proxy.getProxyList
59
51
  ```
60
52
 
61
- **Add a service**
53
+ **List registered domains**
62
54
 
63
55
  ```
64
- POST /services
65
- {
66
- "domain": "example.com",
67
- "target": "localhost:1234"
68
- }
56
+ cy proxy.getDomainList
69
57
  ```
70
58
 
71
- **Remove a service**
59
+ **Reload all configurations**
72
60
 
73
61
  ```
74
- DELETE /services/example.com
62
+ cy proxy.reload
75
63
  ```
package/dist/index.d.ts CHANGED
@@ -2,11 +2,11 @@ import { Proxy, DomainOption, Certificate } from './proxy-manager.js';
2
2
  declare const _default: {
3
3
  addProxy(options: Proxy): void;
4
4
  removeProxy(options: DomainOption): void;
5
+ getProxy(options: DomainOption): Proxy;
5
6
  addCertificate(options: Certificate): void;
6
7
  removeCertificate(options: DomainOption): void;
7
8
  reload(): void;
8
9
  getDomainList(): string[];
9
10
  getProxyList(): [string, Proxy][];
10
- getProxyForDomain(o: DomainOption): Proxy;
11
11
  };
12
12
  export default _default;
package/dist/index.js CHANGED
@@ -7,6 +7,9 @@ export default {
7
7
  removeProxy(options) {
8
8
  return manager.removeProxy(options);
9
9
  },
10
+ getProxy(options) {
11
+ return manager.getProxyForDomain(options);
12
+ },
10
13
  addCertificate(options) {
11
14
  return manager.addCertificate(options);
12
15
  },
@@ -21,8 +24,5 @@ export default {
21
24
  },
22
25
  getProxyList() {
23
26
  return manager.getProxyList();
24
- },
25
- getProxyForDomain(o) {
26
- return manager.getProxyForDomain(o);
27
- },
27
+ }
28
28
  };
@@ -24,7 +24,6 @@ export declare class ProxyManager {
24
24
  reloadProxies(): void;
25
25
  getProxyList(): [string, Proxy][];
26
26
  private connectDomainToTarget;
27
- private mkdir;
28
27
  private writeFile;
29
28
  private saveProxies;
30
29
  private closeProxy;
@@ -1,14 +1,16 @@
1
- import { existsSync, mkdirSync, readFileSync, rmdirSync, writeFileSync } from 'fs';
2
- import { join } from 'path';
3
- import redbird from 'redbird';
4
- const certificatesFolder = join(process.cwd(), 'certs');
5
- const configurationFile = join(process.cwd(), 'data', 'px.json');
6
- const keyFileName = 'key';
7
- const certificateFileName = 'cert';
1
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
2
+ import { join } from "path";
3
+ import redbird from "redbird";
4
+ const certificatesFolder = join(process.cwd(), "certs");
5
+ const configurationFile = join(process.cwd(), "data", "px.json");
6
+ const keyExtension = ".key";
7
+ const certificateExtension = ".cert";
8
8
  export class ProxyManager {
9
9
  constructor() {
10
10
  this.proxyConfiguration = new Map();
11
- this.mkdir(certificatesFolder);
11
+ if (!existsSync(certificatesFolder)) {
12
+ mkdirSync(certificatesFolder, { recursive: true });
13
+ }
12
14
  }
13
15
  get proxy() {
14
16
  if (!this._proxy) {
@@ -39,16 +41,13 @@ export class ProxyManager {
39
41
  }
40
42
  addCertificate({ domain, certificate, key }) {
41
43
  const basePath = join(certificatesFolder, domain);
42
- this.mkdir(basePath);
43
- this.writeFile(join(basePath, certificateFileName), certificate);
44
- this.writeFile(join(basePath, keyFileName), key);
44
+ this.writeFile(basePath + certificateExtension, certificate);
45
+ this.writeFile(basePath + keyExtension, key);
45
46
  }
46
47
  removeCertificate(options) {
47
- const { domain } = options;
48
- const path = join(certificatesFolder, domain);
49
- if (!existsSync(path)) {
50
- rmdirSync(path, { recursive: true });
51
- }
48
+ const basePath = join(certificatesFolder, options.domain);
49
+ rmSync(basePath + certificateExtension, { force: true });
50
+ rmSync(basePath + keyExtension, { force: true });
52
51
  }
53
52
  getDomainList() {
54
53
  return Array.from(this.proxyConfiguration.keys() || []);
@@ -69,23 +68,29 @@ export class ProxyManager {
69
68
  getProxyList() {
70
69
  if (!existsSync(configurationFile))
71
70
  return [];
72
- const json = readFileSync(configurationFile, 'utf8') || '[]';
71
+ const json = readFileSync(configurationFile, "utf8") || "[]";
73
72
  const entries = JSON.parse(json);
74
73
  return entries;
75
74
  }
76
75
  connectDomainToTarget(domain, target) {
76
+ const parts = domain.split(".");
77
+ let rootDomain = "";
78
+ while (parts.length) {
79
+ rootDomain = parts.join(".");
80
+ if (existsSync(join(certificatesFolder, rootDomain) + keyExtension))
81
+ break;
82
+ parts.shift();
83
+ }
84
+ if (!rootDomain) {
85
+ throw new Error("Certificate not found for " + domain);
86
+ }
77
87
  this.proxy.register(domain, target, {
78
88
  ssl: {
79
- cert: join(certificatesFolder, domain, certificateFileName),
80
- key: join(certificatesFolder, domain, keyFileName),
89
+ cert: join(certificatesFolder, rootDomain) + certificateExtension,
90
+ key: join(certificatesFolder, rootDomain) + keyExtension,
81
91
  },
82
92
  });
83
93
  }
84
- mkdir(path) {
85
- if (!existsSync(path)) {
86
- mkdirSync(path, { recursive: true });
87
- }
88
- }
89
94
  writeFile(path, content) {
90
95
  writeFileSync(path, content);
91
96
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "1.1.0",
4
- "description": "Reverse Proxy API/module",
3
+ "version": "1.1.2",
4
+ "description": "Reverse Proxy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "types": "./dist/index.d.ts",
@@ -9,8 +9,7 @@
9
9
  "test": "echo true",
10
10
  "xtest": "jest",
11
11
  "tdd": "jest --watchAll",
12
- "build": "rm -rf ./dist/* && tsc --project ./tsconfig.json",
13
- "start": "node dist/server.js"
12
+ "build": "rm -rf ./dist/* && tsc --project ./tsconfig.json"
14
13
  },
15
14
  "keywords": [
16
15
  "cloudy"
@@ -37,7 +36,6 @@
37
36
  "access": "public"
38
37
  },
39
38
  "dependencies": {
40
- "@cloud-cli/gw": "^1.1.2",
41
39
  "redbird": "^0.10.0"
42
40
  }
43
41
  }