@cloud-cli/px 4.2.1 → 4.2.3

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
@@ -5,7 +5,7 @@ declare const _default: {
5
5
  remove(options: WithOptionalProps<DomainAndTarget>): Promise<boolean>;
6
6
  update(options: WithOptionalProps<Proxy>): Promise<Proxy>;
7
7
  list(filters: Partial<Proxy>): Promise<Proxy[]>;
8
- get(options: DomainName): Proxy[];
8
+ get(options: DomainName): Promise<Proxy[]>;
9
9
  domains(): Promise<string[]>;
10
10
  [init](): Promise<import("@cloud-cli/proxy").ProxyServer>;
11
11
  reload(): Promise<void>;
@@ -7,7 +7,7 @@ export declare class ProxyManager {
7
7
  removeProxy(options: WithOptionalProps<DomainAndTarget>): Promise<boolean>;
8
8
  getDomainList(): Promise<string[]>;
9
9
  getProxyList(filters?: Partial<Proxy>): Promise<Proxy[]>;
10
- getProxyListForDomain(options: WithOptionalProps<DomainName>): Proxy[];
10
+ getProxyListForDomain(options: WithOptionalProps<DomainName>): Promise<Proxy[]>;
11
11
  reload(): Promise<void>;
12
12
  getRunningContainers(): Promise<DockerContainer[]>;
13
13
  }
@@ -97,11 +97,13 @@ export class ProxyManager {
97
97
  return false;
98
98
  }
99
99
  async getDomainList() {
100
- const proxies = getAll();
100
+ const proxies = await this.getProxyList();
101
101
  return proxies.map((proxy) => proxy.domain);
102
102
  }
103
103
  async getProxyList(filters = {}) {
104
- const list = getAll();
104
+ const staticRoutes = getAll();
105
+ const containers = await this.getRunningContainers();
106
+ const list = [...staticRoutes, ...containers.map(readProxyFromContainer)];
105
107
  const keys = Object.keys(filters);
106
108
  if (!keys.length) {
107
109
  return list;
@@ -111,15 +113,17 @@ export class ProxyManager {
111
113
  return list.filter((p) => String(p[key]).toLowerCase().includes(filter));
112
114
  }, list);
113
115
  }
114
- getProxyListForDomain(options) {
116
+ async getProxyListForDomain(options) {
115
117
  readDomain(options);
116
- const all = getAll();
118
+ const all = await this.getProxyList();
117
119
  return all.filter((p) => p.domain === options.domain);
118
120
  }
119
121
  async reload() {
120
122
  const targets = await getAll();
123
+ const containers = await this.getRunningContainers();
124
+ const all = [...targets, ...containers.map(readProxyFromContainer)];
121
125
  px.reset();
122
- for (const t of targets) {
126
+ for (const t of all) {
123
127
  const [domain, path = ""] = t.domain.split("/");
124
128
  px.add(new ProxyEntry({
125
129
  domain: domain,
@@ -133,29 +137,30 @@ export class ProxyManager {
133
137
  preserveHost: t.preserveHost,
134
138
  }));
135
139
  }
136
- const dockerTargets = await this.getRunningContainers();
137
- for (const t of dockerTargets) {
138
- if (!t.ports.length || !t.labels.host)
139
- continue;
140
- px.add(new ProxyEntry({
141
- domain: t.labels.host,
142
- path: t.labels.path || '',
143
- target: `http://localhost:${t.ports[0].host}`,
144
- redirectToHttps: true,
145
- cors: true,
146
- }));
147
- }
148
140
  px.start();
149
141
  }
150
142
  async getRunningContainers() {
151
143
  const ps = await exec("docker", ["ps", "-aq"]);
152
144
  const ids = ps.stdout.trim().split("\n");
153
- const format = '--format="{{.Id}},{{.Name}},{{.State.Status}},{{.Image}}"';
154
- const state = await exec("docker", ["inspect", format, ...ids]);
145
+ const state = await exec("docker", ["inspect", ...ids]);
155
146
  const json = JSON.parse(state.stdout);
156
- return json.map(readDockerContainer);
147
+ return json
148
+ .map(readDockerContainer)
149
+ .filter(d => d.ports.length && !d.labels.host);
157
150
  }
158
151
  }
152
+ function readProxyFromContainer(c) {
153
+ return {
154
+ domain: [c.labels.host, c.labels.path].join("/"),
155
+ target: `http://localhost:${c.ports[0].host}`,
156
+ redirect: true,
157
+ redirectUrl: "",
158
+ cors: true,
159
+ authorization: "",
160
+ preserveHost: false,
161
+ headers: "",
162
+ };
163
+ }
159
164
  function readDockerContainer(d) {
160
165
  const labels = Object.fromEntries(Object.entries(d.Config.Labels)
161
166
  .filter(([key]) => key.startsWith("px:"))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/px",
3
- "version": "4.2.1",
3
+ "version": "4.2.3",
4
4
  "description": "Reverse Proxy",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",