@cloud-cli/px 3.7.0 → 4.0.0

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/index.d.ts CHANGED
@@ -1,15 +1,16 @@
1
1
  import { init } from '@cloud-cli/cli';
2
- import { DomainAndTarget, Domain } from './proxy-manager.js';
3
- import { Proxy } from './proxy.js';
4
- import { ProxyServer } from '@cloud-cli/proxy';
5
- declare function reload(): Promise<void>;
6
- declare function add(options: Proxy): Promise<unknown>;
2
+ import { DomainAndTarget, Domain, Proxy } from './proxy-manager.js';
3
+ type ExtraProps = {
4
+ _: string[];
5
+ };
6
+ declare function add(options: Proxy & ExtraProps): Promise<Proxy>;
7
7
  declare function remove(options: DomainAndTarget): Promise<unknown>;
8
- declare function update(options: Proxy): Promise<boolean>;
8
+ declare function update(options: Proxy & ExtraProps): Promise<boolean>;
9
9
  declare function list(): Promise<Proxy[]>;
10
10
  declare function get(options: Domain): Promise<Proxy[]>;
11
11
  declare function domains(): Promise<string[]>;
12
- declare function initServer(): Promise<ProxyServer>;
12
+ declare function initServer(): Promise<import("@cloud-cli/proxy").ProxyServer>;
13
+ declare function reload(): Promise<void>;
13
14
  declare const _default: {
14
15
  add: typeof add;
15
16
  remove: typeof remove;
package/dist/index.js CHANGED
@@ -1,45 +1,19 @@
1
- import { init } from '@cloud-cli/cli';
2
- import { Resource, SQLiteDriver } from '@cloud-cli/store';
1
+ import { init, logInfo } from '@cloud-cli/cli';
3
2
  import { ProxyManager } from './proxy-manager.js';
4
- import { loadTargets, Proxy } from './proxy.js';
5
- import { ProxyServer, ProxySettings, ProxyEntry } from '@cloud-cli/proxy';
6
- const settings = new ProxySettings({
7
- certificatesFolder: process.env.PX_CERTS_FOLDER || '/etc/letsencrypt/live',
8
- certificateFile: 'fullchain.pem',
9
- keyFile: 'privkey.pem',
10
- });
11
- const px = new ProxyServer(settings);
12
3
  const manager = new ProxyManager();
13
- async function reload() {
14
- const targets = await loadTargets();
15
- px.reset();
16
- targets.forEach((t) => {
17
- px.add(new ProxyEntry({
18
- domain: t.domain,
19
- target: t.target,
20
- path: t.path,
21
- redirectToHttps: t.redirect,
22
- redirectToUrl: t.redirectUrl,
23
- cors: t.cors,
24
- headers: t.headers,
25
- authorization: t.authorization,
26
- }));
27
- });
28
- px.start();
29
- }
30
4
  async function add(options) {
31
5
  const proxy = await manager.addProxy(options);
32
- await reload();
6
+ await manager.reload();
33
7
  return proxy;
34
8
  }
35
9
  async function remove(options) {
36
10
  const removed = await manager.removeProxy(options);
37
- await reload();
11
+ await manager.reload();
38
12
  return removed;
39
13
  }
40
14
  async function update(options) {
41
15
  const output = await manager.updateProxy(options);
42
- await reload();
16
+ await manager.reload();
43
17
  return output;
44
18
  }
45
19
  function list() {
@@ -52,9 +26,11 @@ function domains() {
52
26
  return manager.getDomainList();
53
27
  }
54
28
  async function initServer() {
55
- Resource.use(new SQLiteDriver());
56
- await Resource.create(Proxy);
57
- await reload();
58
- return px;
29
+ await manager.reload();
30
+ return manager.server;
31
+ }
32
+ async function reload() {
33
+ logInfo('Reloading proxy server');
34
+ return await manager.reload();
59
35
  }
60
36
  export default { add, remove, list, get, update, domains, reload, [init]: initServer };
@@ -1,11 +1,23 @@
1
- import { Proxy } from './proxy.js';
2
- export interface Domain {
3
- domain: string;
1
+ import { ProxyServer } from '@cloud-cli/proxy';
2
+ interface OptionalProps {
4
3
  host?: string;
4
+ _: string[];
5
+ }
6
+ export interface Domain extends OptionalProps {
7
+ domain: string;
5
8
  }
6
9
  export interface DomainAndTarget extends Domain {
7
10
  target: string;
8
11
  }
12
+ export interface Proxy {
13
+ domain: string;
14
+ target: string;
15
+ redirect: boolean;
16
+ redirectUrl: string;
17
+ headers: string;
18
+ authorization: string;
19
+ cors: boolean;
20
+ }
9
21
  type KeysOf<T> = {
10
22
  [K in keyof T]: T[K] extends () => any ? never : K;
11
23
  }[keyof T];
@@ -13,16 +25,14 @@ type ClassProperties<T> = {
13
25
  [K in KeysOf<T>]: T[K] extends never ? never : T[K];
14
26
  };
15
27
  export declare class ProxyManager {
16
- addProxy(proxy: ClassProperties<Proxy> & {
17
- host?: string;
18
- }): Promise<unknown>;
19
- updateProxy(options: ClassProperties<Proxy> & {
20
- host?: string;
21
- }): Promise<boolean>;
28
+ server: ProxyServer;
29
+ addProxy(proxy: ClassProperties<Proxy> & OptionalProps): Promise<Proxy>;
30
+ updateProxy(options: ClassProperties<Proxy> & OptionalProps): Promise<boolean>;
22
31
  removeProxy(options: DomainAndTarget): Promise<unknown>;
23
32
  getDomainList(): Promise<string[]>;
24
33
  getProxyList(): Promise<Proxy[]>;
25
34
  getProxyListForDomain(options: Domain): Promise<Proxy[]>;
35
+ reload(): Promise<void>;
26
36
  private findByDomainAndPath;
27
37
  }
28
38
  export {};
@@ -1,48 +1,57 @@
1
- import { Query, Resource } from '@cloud-cli/store';
2
- import { Proxy } from './proxy.js';
1
+ import { getStorage } from '@cloud-cli/cli';
2
+ import { ProxyEntry, ProxyServer, ProxySettings } from '@cloud-cli/proxy';
3
3
  const domainNotSpecifiedError = new Error('Domain not specified');
4
4
  const targetNotSpecifiedError = new Error('Target not specified');
5
+ const { set, remove, getAll } = getStorage('px');
6
+ const settings = new ProxySettings({
7
+ certificatesFolder: process.env.PX_CERTS_FOLDER || '/etc/letsencrypt/live',
8
+ certificateFile: 'fullchain.pem',
9
+ keyFile: 'privkey.pem',
10
+ });
11
+ const px = new ProxyServer(settings);
12
+ const emptyProxy = {
13
+ domain: '',
14
+ target: '',
15
+ cors: false,
16
+ redirect: false,
17
+ redirectUrl: '',
18
+ headers: '',
19
+ authorization: '',
20
+ };
5
21
  export class ProxyManager {
6
- addProxy(proxy) {
7
- return new Promise(async (resolve, reject) => {
8
- if (!proxy.domain && !proxy.host) {
9
- return reject(domainNotSpecifiedError);
10
- }
11
- if (!proxy.target && !proxy.redirectUrl) {
12
- return reject(targetNotSpecifiedError);
13
- }
14
- const [domain, path = ''] = (proxy.domain || proxy.host).split('/');
15
- const entry = new Proxy({
16
- domain: domain,
17
- path: path,
18
- target: proxy.target,
19
- cors: !!proxy.cors,
20
- redirect: !!proxy.redirect,
21
- redirectUrl: proxy.redirectUrl || '',
22
- headers: proxy.headers || '',
23
- authorization: proxy.authorization,
24
- });
25
- try {
26
- const id = await entry.save();
27
- entry.id = id;
28
- resolve(entry);
29
- }
30
- catch (error) {
31
- reject(error);
32
- }
33
- });
22
+ constructor() {
23
+ this.server = px;
24
+ }
25
+ async addProxy(proxy) {
26
+ if (!proxy.domain && !proxy.host) {
27
+ throw domainNotSpecifiedError;
28
+ }
29
+ if (!proxy.target && !proxy.redirectUrl) {
30
+ throw targetNotSpecifiedError;
31
+ }
32
+ const domainAndPath = proxy.domain || proxy.host || proxy._[0];
33
+ const entry = {
34
+ domain: domainAndPath,
35
+ target: proxy.target,
36
+ cors: !!proxy.cors,
37
+ redirect: !!proxy.redirect,
38
+ redirectUrl: proxy.redirectUrl || '',
39
+ headers: proxy.headers || '',
40
+ authorization: proxy.authorization,
41
+ };
42
+ await set(domainAndPath, entry);
43
+ return entry;
34
44
  }
35
45
  async updateProxy(options) {
36
- const host = options.domain || options.host;
46
+ const host = options.domain || options.host || options._[0];
37
47
  const proxies = await this.findByDomainAndPath(host);
38
48
  if (!proxies.length) {
39
- const [domain, path = ''] = host.split('/');
40
- proxies.push(new Proxy({ domain, path }));
49
+ proxies.push({ ...emptyProxy, domain: host });
41
50
  }
42
51
  const properties = ['target', 'cors', 'redirect', 'redirectUrl', 'headers', 'authorization'];
43
52
  for (const proxy of proxies) {
44
53
  properties.forEach((p) => p in options && (proxy[p] = options[p]));
45
- await proxy.save();
54
+ set(proxy.domain, proxy);
46
55
  }
47
56
  return true;
48
57
  }
@@ -55,7 +64,7 @@ export class ProxyManager {
55
64
  const proxies = await this.findByDomainAndPath(host);
56
65
  try {
57
66
  for (const p of proxies) {
58
- await p.remove();
67
+ await remove(p.domain);
59
68
  }
60
69
  resolve({ found: proxies.length });
61
70
  }
@@ -65,23 +74,36 @@ export class ProxyManager {
65
74
  });
66
75
  }
67
76
  async getDomainList() {
68
- const proxies = await Resource.find(Proxy, new Query());
69
- const list = proxies.map((proxy) => proxy.domain);
70
- return [...new Set(list)];
77
+ const proxies = await getAll();
78
+ return proxies.map((proxy) => proxy.domain);
71
79
  }
72
80
  async getProxyList() {
73
- return Resource.find(Proxy, new Query());
81
+ return await getAll();
74
82
  }
75
83
  getProxyListForDomain(options) {
76
84
  return this.findByDomainAndPath(options.domain || options.host);
77
85
  }
86
+ async reload() {
87
+ const targets = await getAll();
88
+ px.reset();
89
+ targets.forEach((t) => {
90
+ const [domain, path = ''] = t.domain.split('/');
91
+ px.add(new ProxyEntry({
92
+ domain: domain,
93
+ path: path,
94
+ target: t.target,
95
+ redirectToHttps: t.redirect,
96
+ redirectToUrl: t.redirectUrl,
97
+ cors: t.cors,
98
+ headers: t.headers,
99
+ authorization: t.authorization,
100
+ }));
101
+ });
102
+ px.start();
103
+ }
78
104
  async findByDomainAndPath(string) {
79
105
  const [domain, path = ''] = string.split('/');
80
- const query = new Query().where('domain').is(domain);
81
- if (path) {
82
- query.where('path').is(path);
83
- }
84
- const proxies = await Resource.find(Proxy, query);
85
- return proxies;
106
+ const proxies = await getAll();
107
+ return proxies.filter(p => p.domain === domain && (!path || domain === path));
86
108
  }
87
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "3.7.0",
3
+ "version": "4.0.0",
4
4
  "description": "Reverse Proxy",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -29,7 +29,6 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@cloud-cli/cli": "^1.2.2",
32
- "@cloud-cli/proxy": "^1.2.0",
33
- "@cloud-cli/store": "^1.1.0"
32
+ "@cloud-cli/proxy": "^1.2.0"
34
33
  }
35
34
  }
package/dist/proxy.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import { Resource } from '@cloud-cli/store';
2
- export declare class Proxy extends Resource {
3
- id: number;
4
- domain: string;
5
- path: string;
6
- target: string;
7
- redirect: boolean;
8
- redirectUrl: string;
9
- headers: string;
10
- authorization: string;
11
- cors: boolean;
12
- constructor(p: Partial<Proxy>);
13
- }
14
- export declare function loadTargets(): Promise<Proxy[]>;
package/dist/proxy.js DELETED
@@ -1,47 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Model, Primary, Property, Query, Resource } from '@cloud-cli/store';
8
- let Proxy = class Proxy extends Resource {
9
- constructor(p) {
10
- super(p);
11
- }
12
- };
13
- __decorate([
14
- Primary(),
15
- Property(Number)
16
- ], Proxy.prototype, "id", void 0);
17
- __decorate([
18
- Property(String)
19
- ], Proxy.prototype, "domain", void 0);
20
- __decorate([
21
- Property(String)
22
- ], Proxy.prototype, "path", void 0);
23
- __decorate([
24
- Property(String)
25
- ], Proxy.prototype, "target", void 0);
26
- __decorate([
27
- Property(Number)
28
- ], Proxy.prototype, "redirect", void 0);
29
- __decorate([
30
- Property(String)
31
- ], Proxy.prototype, "redirectUrl", void 0);
32
- __decorate([
33
- Property(String)
34
- ], Proxy.prototype, "headers", void 0);
35
- __decorate([
36
- Property(String)
37
- ], Proxy.prototype, "authorization", void 0);
38
- __decorate([
39
- Property(Number)
40
- ], Proxy.prototype, "cors", void 0);
41
- Proxy = __decorate([
42
- Model('proxyentry')
43
- ], Proxy);
44
- export { Proxy };
45
- export async function loadTargets() {
46
- return await Resource.find(Proxy, new Query());
47
- }
package/jest.config.mjs DELETED
@@ -1,3 +0,0 @@
1
- import config from '@cloud-cli/jest-config';
2
-
3
- export default { ...config };