@superdurable/connector-emq 0.1.0 → 0.1.1
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 +2 -0
- package/dex-connector.json +1 -1
- package/dist/index.js +76 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,3 +5,5 @@
|
|
|
5
5
|
In [EMQX Cloud Console](https://cloud.emqx.com/console/), open **Settings > Platform API Key**, create a key with Deployment Management and the required project scopes, then save its key and secret as Dex secret credentials. Copy the Platform API endpoint ending in `/public_api/v1` to the connection's `platformEndpoint`. Blank credentials or endpoint prevent requests; leave timeout and maxResponseBytes blank for their displayed defaults.
|
|
6
6
|
|
|
7
7
|
The [runnable Flow](examples/basic/) uses static connection name `emq`. `npm run check` replays the Go-recorded provider scenarios, including errors and retries. Live authorization and deployment data remain unverified without a user-controlled connection.
|
|
8
|
+
|
|
9
|
+
Connection and transaction capacities use JavaScript numbers. Values outside the safe integer range select `invalidResponse`.
|
package/dex-connector.json
CHANGED
package/dist/index.js
CHANGED
|
@@ -35,8 +35,16 @@ function newListDeploymentsStep(configuration) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// src/client.ts
|
|
38
|
-
import {
|
|
39
|
-
|
|
38
|
+
import {
|
|
39
|
+
SecretString,
|
|
40
|
+
newQueryBranch,
|
|
41
|
+
newQueryRetry
|
|
42
|
+
} from "@superdurable/connectors-sdk";
|
|
43
|
+
import {
|
|
44
|
+
BodyTooLargeError,
|
|
45
|
+
parseRetryAfter,
|
|
46
|
+
sendProviderRequest
|
|
47
|
+
} from "@superdurable/connectors-sdk/providerhttp";
|
|
40
48
|
var emptyOutput = { deployments: [] };
|
|
41
49
|
var EmqClient = class {
|
|
42
50
|
/** Validate local settings without contacting EMQX Cloud. */
|
|
@@ -50,11 +58,13 @@ var EmqClient = class {
|
|
|
50
58
|
} catch {
|
|
51
59
|
throw new TypeError("EMQX Cloud Platform endpoint is invalid");
|
|
52
60
|
}
|
|
53
|
-
if (!["https:", "http:"].includes(endpoint.protocol) || endpoint.protocol === "http:" && !["localhost", "127.0.0.1", "[::1]"].includes(endpoint.hostname) || !endpoint.hostname || endpoint.username || endpoint.password || endpoint.search || endpoint.hash || endpoint.pathname !== "/public_api/v1" || selectedEndpoint.endsWith("/"))
|
|
61
|
+
if (!["https:", "http:"].includes(endpoint.protocol) || endpoint.protocol === "http:" && !["localhost", "127.0.0.1", "[::1]"].includes(endpoint.hostname) || !endpoint.hostname || endpoint.username || endpoint.password || endpoint.search || endpoint.hash || endpoint.pathname !== "/public_api/v1" || !/^https?:\/\/[^/?#]+\/public_api\/v1$/iu.test(selectedEndpoint) || selectedEndpoint.endsWith("/"))
|
|
62
|
+
throw new TypeError("EMQX Cloud Platform endpoint is invalid");
|
|
54
63
|
this.endpoint = selectedEndpoint;
|
|
55
64
|
this.timeoutMs = config.timeoutMs || 1e4;
|
|
56
65
|
this.maxResponseBytes = config.maxResponseBytes || 1048576;
|
|
57
|
-
if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0 || this.maxResponseBytes > 16 << 20)
|
|
66
|
+
if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0 || this.maxResponseBytes > 16 << 20)
|
|
67
|
+
throw new TypeError("EMQX Cloud request limits are invalid");
|
|
58
68
|
this.send = options.send ?? sendProviderRequest;
|
|
59
69
|
}
|
|
60
70
|
credentials;
|
|
@@ -76,16 +86,37 @@ var EmqClient = class {
|
|
|
76
86
|
} catch {
|
|
77
87
|
return { error: "credential", retryAfterMs: 0 };
|
|
78
88
|
}
|
|
79
|
-
if (!validBasicCredential(key) || !validBasicCredential(secret) || key.includes(":"))
|
|
80
|
-
|
|
89
|
+
if (!validBasicCredential(key) || !validBasicCredential(secret) || key.includes(":"))
|
|
90
|
+
return { error: "credential", retryAfterMs: 0 };
|
|
91
|
+
const request = {
|
|
92
|
+
url: this.endpoint + "/deployments",
|
|
93
|
+
method: "GET",
|
|
94
|
+
headers: {
|
|
95
|
+
Authorization: "Basic " + Buffer.from(key + ":" + secret).toString("base64"),
|
|
96
|
+
Accept: "application/json"
|
|
97
|
+
},
|
|
98
|
+
timeoutMs: this.timeoutMs,
|
|
99
|
+
maxResponseBytes: this.maxResponseBytes,
|
|
100
|
+
addressPolicy: this.endpoint.startsWith("http://") ? "privateNetworkHTTP" : "publicHTTPS",
|
|
101
|
+
signal: call.context.cancellationSignal
|
|
102
|
+
};
|
|
81
103
|
let response;
|
|
82
104
|
try {
|
|
83
105
|
response = await this.send(request);
|
|
84
106
|
} catch (error) {
|
|
85
|
-
if (error instanceof BodyTooLargeError && error.status !== void 0)
|
|
107
|
+
if (error instanceof BodyTooLargeError && error.status !== void 0)
|
|
108
|
+
return {
|
|
109
|
+
response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() },
|
|
110
|
+
error: "tooLarge",
|
|
111
|
+
retryAfterMs: parseRetryAfter(header(error.headers ?? {}, "retry-after"))
|
|
112
|
+
};
|
|
86
113
|
return { error: "transport", retryAfterMs: 0 };
|
|
87
114
|
}
|
|
88
|
-
return {
|
|
115
|
+
return {
|
|
116
|
+
response,
|
|
117
|
+
error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0,
|
|
118
|
+
retryAfterMs: parseRetryAfter(header(response.headers, "retry-after"))
|
|
119
|
+
};
|
|
89
120
|
}
|
|
90
121
|
};
|
|
91
122
|
var ListDeploymentsOperation = class {
|
|
@@ -107,19 +138,42 @@ var ListDeploymentsOperation = class {
|
|
|
107
138
|
const { status, body } = exchange.response;
|
|
108
139
|
if (status === 429) return retry("RATE_LIMIT", exchange.retryAfterMs);
|
|
109
140
|
if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
|
|
110
|
-
if (status !== 200)
|
|
141
|
+
if (status !== 200)
|
|
142
|
+
return branch(
|
|
143
|
+
"providerRejected",
|
|
144
|
+
status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION"
|
|
145
|
+
);
|
|
111
146
|
if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
|
|
112
147
|
let wire;
|
|
113
148
|
try {
|
|
114
|
-
wire = JSON.parse(new TextDecoder("utf-8", {
|
|
149
|
+
wire = JSON.parse(new TextDecoder("utf-8", { ignoreBOM: true }).decode(body), capacityField);
|
|
115
150
|
} catch {
|
|
116
151
|
return branch("invalidResponse", "PROTOCOL");
|
|
117
152
|
}
|
|
118
153
|
if (!Array.isArray(wire)) return branch("invalidResponse", "PROTOCOL");
|
|
119
154
|
const deployments = [];
|
|
120
|
-
for (const
|
|
121
|
-
if (!isRecord(
|
|
122
|
-
|
|
155
|
+
for (const row of wire) {
|
|
156
|
+
if (!isRecord(row)) return branch("invalidResponse", "PROTOCOL");
|
|
157
|
+
const item = Object.fromEntries(Object.entries(row).map(([key, value]) => [key.toLowerCase(), value]));
|
|
158
|
+
if (!nonempty(item.deploymentid) || !nonempty(item.deploymentname) || !nonempty(item.deploymenttype) || !nonempty(item.status) || !Number.isSafeInteger(item.connections) || !Number.isSafeInteger(item.tps) || item.connections < 0 || item.tps < 0)
|
|
159
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
160
|
+
if (["projectname", "region", "version", "platform", "createat"].some(
|
|
161
|
+
(name) => item[name] != null && typeof item[name] !== "string"
|
|
162
|
+
))
|
|
163
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
164
|
+
deployments.push({
|
|
165
|
+
deploymentId: item.deploymentid,
|
|
166
|
+
deploymentName: item.deploymentname,
|
|
167
|
+
deploymentType: item.deploymenttype,
|
|
168
|
+
projectName: String(item.projectname ?? ""),
|
|
169
|
+
region: String(item.region ?? ""),
|
|
170
|
+
status: item.status,
|
|
171
|
+
version: String(item.version ?? ""),
|
|
172
|
+
platform: String(item.platform ?? ""),
|
|
173
|
+
connections: item.connections,
|
|
174
|
+
tps: item.tps,
|
|
175
|
+
createdAt: String(item.createat ?? "")
|
|
176
|
+
});
|
|
123
177
|
}
|
|
124
178
|
return newQueryBranch("listed", { deployments }, {});
|
|
125
179
|
}
|
|
@@ -138,7 +192,12 @@ function header(headers, name) {
|
|
|
138
192
|
return Array.isArray(value) ? value[0] ?? "" : value ?? "";
|
|
139
193
|
}
|
|
140
194
|
function failure(kind) {
|
|
141
|
-
return {
|
|
195
|
+
return {
|
|
196
|
+
provider: "emq",
|
|
197
|
+
operation: "listDeployments",
|
|
198
|
+
kind,
|
|
199
|
+
message: "EMQX Cloud deployment query failed"
|
|
200
|
+
};
|
|
142
201
|
}
|
|
143
202
|
function branch(name, kind) {
|
|
144
203
|
return newQueryBranch(name, emptyOutput, {}, failure(kind));
|
|
@@ -146,6 +205,9 @@ function branch(name, kind) {
|
|
|
146
205
|
function retry(kind, delay = 0) {
|
|
147
206
|
return newQueryRetry(failure(kind), delay);
|
|
148
207
|
}
|
|
208
|
+
function capacityField(name, value, context) {
|
|
209
|
+
return typeof value === "number" && ["connections", "tps"].includes(name.toLowerCase()) && !/^-?\d+$/u.test(context?.source ?? "") ? NaN : value;
|
|
210
|
+
}
|
|
149
211
|
|
|
150
212
|
// src/project.ts
|
|
151
213
|
function newProjectConnection2(project, connectionName) {
|