@smartindustriesllc/ecosystem-client 0.1.1 → 0.2.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/package.json +1 -1
- package/src/hub.js +108 -1
package/package.json
CHANGED
package/src/hub.js
CHANGED
|
@@ -11,7 +11,7 @@ const os = require("os");
|
|
|
11
11
|
const path = require("path");
|
|
12
12
|
const { signRequest } = require("./_auth");
|
|
13
13
|
const { assertValidSchema, buildNotificationEnvelope } = require("./contracts");
|
|
14
|
-
const { resolveDeviceId } = require("./ulid");
|
|
14
|
+
const { generateUlid, resolveDeviceId } = require("./ulid");
|
|
15
15
|
|
|
16
16
|
function defaultJsOutboxPath(appName) {
|
|
17
17
|
const safe = String(appName || "js-app").replace(/[^A-Za-z0-9_-]/g, "_");
|
|
@@ -151,6 +151,113 @@ class HubClient {
|
|
|
151
151
|
return null;
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Ask the hub to run a capability and return its status document.
|
|
156
|
+
*
|
|
157
|
+
* The hub decides (policy, approval) and forwards to the provider; `status`
|
|
158
|
+
* is one of completed, accepted, pending_approval, denied, failed or
|
|
159
|
+
* provider_unavailable. Nothing is ever queued: if the hub cannot be
|
|
160
|
+
* reached the answer is provider_unavailable. To retry safely after an
|
|
161
|
+
* unknown outcome, pass the same `idempotencyKey`.
|
|
162
|
+
*
|
|
163
|
+
* opts: rationale, idempotencyKey, actorType, actorId, correlationId,
|
|
164
|
+
* context, stepUpToken, timeoutMs
|
|
165
|
+
*/
|
|
166
|
+
async invoke(capability, target, parameters = {}, opts = {}) {
|
|
167
|
+
if (!this.secret) {
|
|
168
|
+
return { status: "provider_unavailable", error: "not_configured", command_id: null };
|
|
169
|
+
}
|
|
170
|
+
const key = opts.idempotencyKey || generateUlid();
|
|
171
|
+
const body = {
|
|
172
|
+
capability,
|
|
173
|
+
target: Object.assign({}, target || {}),
|
|
174
|
+
parameters: Object.assign({}, parameters || {}),
|
|
175
|
+
actor: {
|
|
176
|
+
app: this.appName,
|
|
177
|
+
id: opts.actorId || this.appName,
|
|
178
|
+
type: opts.actorType || "service",
|
|
179
|
+
device_id: this.deviceId,
|
|
180
|
+
},
|
|
181
|
+
idempotency_key: key,
|
|
182
|
+
};
|
|
183
|
+
if (opts.rationale != null) body.rationale = opts.rationale;
|
|
184
|
+
if (opts.correlationId != null) body.correlation_id = opts.correlationId;
|
|
185
|
+
if (opts.context != null) body.context = opts.context;
|
|
186
|
+
if (opts.stepUpToken != null) body.step_up_token = opts.stepUpToken;
|
|
187
|
+
|
|
188
|
+
const url = `${this.registryUrl}/commands/dispatch`;
|
|
189
|
+
let resp;
|
|
190
|
+
try {
|
|
191
|
+
resp = await fetch(url, {
|
|
192
|
+
method: "POST",
|
|
193
|
+
headers: this._headers("POST", url, body),
|
|
194
|
+
body: JSON.stringify(body),
|
|
195
|
+
signal: AbortSignal.timeout(opts.timeoutMs || 60000),
|
|
196
|
+
});
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (HubClient._neverConnected(err)) {
|
|
199
|
+
return { status: "provider_unavailable", error: "hub_unreachable", command_id: null };
|
|
200
|
+
}
|
|
201
|
+
// Sent, but no answer: it may or may not have run.
|
|
202
|
+
return {
|
|
203
|
+
status: "failed",
|
|
204
|
+
error: `no answer from the hub (${(err && err.name) || "Error"})`,
|
|
205
|
+
outcome_unknown: true,
|
|
206
|
+
command_id: null,
|
|
207
|
+
idempotency_key: key,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let payload = null;
|
|
212
|
+
try {
|
|
213
|
+
payload = await resp.json();
|
|
214
|
+
} catch {
|
|
215
|
+
payload = null;
|
|
216
|
+
}
|
|
217
|
+
const isObject = payload !== null && typeof payload === "object" && !Array.isArray(payload);
|
|
218
|
+
if (resp.status === 200 && isObject) {
|
|
219
|
+
return payload;
|
|
220
|
+
}
|
|
221
|
+
const out = {
|
|
222
|
+
status: "failed",
|
|
223
|
+
error: isObject && payload.detail ? String(payload.detail) : `hub answered HTTP ${resp.status}`,
|
|
224
|
+
http_status: resp.status,
|
|
225
|
+
command_id: null,
|
|
226
|
+
idempotency_key: key,
|
|
227
|
+
};
|
|
228
|
+
if (resp.status >= 500 || resp.status === 200) {
|
|
229
|
+
out.outcome_unknown = true;
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** True when the request never reached the hub (as opposed to: sent, no answer). */
|
|
235
|
+
static _neverConnected(err) {
|
|
236
|
+
const codes = ["ECONNREFUSED", "ENOTFOUND", "EHOSTUNREACH", "ENETUNREACH", "EAI_AGAIN", "UND_ERR_CONNECT_TIMEOUT"];
|
|
237
|
+
const code = err && ((err.cause && err.cause.code) || err.code);
|
|
238
|
+
if (code && codes.includes(code)) return true;
|
|
239
|
+
const message = String((err && err.message) || "");
|
|
240
|
+
return codes.some((c) => message.includes(c));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The hub's record of a command (status, result, audit events), or null. */
|
|
244
|
+
async getCommand(commandId) {
|
|
245
|
+
try {
|
|
246
|
+
const url = `${this.registryUrl}/commands/${encodeURIComponent(commandId)}`;
|
|
247
|
+
const resp = await fetch(url, {
|
|
248
|
+
method: "GET",
|
|
249
|
+
headers: this._headers("GET", url, null),
|
|
250
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
251
|
+
});
|
|
252
|
+
if (resp && resp.ok) {
|
|
253
|
+
return await resp.json();
|
|
254
|
+
}
|
|
255
|
+
} catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
|
|
154
261
|
async registerCapability(partial) {
|
|
155
262
|
const manifest = this._storeLocalCapability(partial);
|
|
156
263
|
if (this._joined) {
|