@dataworks-technology/data 0.1.3 → 0.1.5
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 +295 -26
- package/dist/index.cjs +273 -64
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -4
- package/dist/index.d.ts +16 -4
- package/dist/index.js +273 -64
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -36,7 +36,49 @@ __export(index_exports, {
|
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(index_exports);
|
|
38
38
|
|
|
39
|
-
//
|
|
39
|
+
// node_modules/@dataworks/sdk/dist/client/subscription-manager.js
|
|
40
|
+
function createAutoRefreshingSubscription(options) {
|
|
41
|
+
const maxReconnectAttempts = options.maxReconnectAttempts ?? 1;
|
|
42
|
+
let reconnectAttempts = 0;
|
|
43
|
+
let reconnectInFlight = false;
|
|
44
|
+
let closedByUser = false;
|
|
45
|
+
let activeConnection = null;
|
|
46
|
+
const hooks = {
|
|
47
|
+
onUnexpectedClose: () => {
|
|
48
|
+
if (closedByUser || reconnectInFlight) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (reconnectAttempts >= maxReconnectAttempts) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
reconnectInFlight = true;
|
|
55
|
+
reconnectAttempts += 1;
|
|
56
|
+
void (async () => {
|
|
57
|
+
try {
|
|
58
|
+
await options.refreshAuth();
|
|
59
|
+
if (closedByUser) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
activeConnection = options.connect(hooks);
|
|
63
|
+
} catch {
|
|
64
|
+
options.onReconnectError?.(new Error("Subscription reconnect failed. Please retry or reauthenticate."));
|
|
65
|
+
} finally {
|
|
66
|
+
reconnectInFlight = false;
|
|
67
|
+
}
|
|
68
|
+
})();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
activeConnection = options.connect(hooks);
|
|
72
|
+
return {
|
|
73
|
+
close() {
|
|
74
|
+
closedByUser = true;
|
|
75
|
+
activeConnection?.close();
|
|
76
|
+
activeConnection = null;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// node_modules/@dataworks/sdk/dist/client/validation.js
|
|
40
82
|
function validateMetric(m) {
|
|
41
83
|
if (typeof m.metric !== "string" || m.metric.trim() === "")
|
|
42
84
|
return "metric must be a non-empty string";
|
|
@@ -55,28 +97,59 @@ function validateMetric(m) {
|
|
|
55
97
|
return null;
|
|
56
98
|
}
|
|
57
99
|
function filterValidMetrics(metrics, warn) {
|
|
100
|
+
const { valid, dropped } = getMetricValidationResult(metrics);
|
|
101
|
+
for (const item of dropped) {
|
|
102
|
+
let valueStr;
|
|
103
|
+
try {
|
|
104
|
+
valueStr = JSON.stringify(item.metric.value);
|
|
105
|
+
} catch {
|
|
106
|
+
valueStr = String(item.metric.value);
|
|
107
|
+
}
|
|
108
|
+
warn(`Dropping invalid metric [${String(item.metric.metric)}=${valueStr}]: ${item.reason}`);
|
|
109
|
+
}
|
|
110
|
+
return valid;
|
|
111
|
+
}
|
|
112
|
+
function getMetricValidationResult(metrics) {
|
|
58
113
|
const valid = [];
|
|
59
|
-
|
|
114
|
+
const dropped = [];
|
|
115
|
+
for (const [index, m] of metrics.entries()) {
|
|
60
116
|
const reason = validateMetric(m);
|
|
61
117
|
if (reason) {
|
|
62
|
-
|
|
118
|
+
dropped.push({
|
|
119
|
+
index,
|
|
120
|
+
reason,
|
|
121
|
+
metric: m
|
|
122
|
+
});
|
|
63
123
|
} else {
|
|
64
124
|
valid.push(m);
|
|
65
125
|
}
|
|
66
126
|
}
|
|
67
|
-
return valid;
|
|
127
|
+
return { valid, dropped };
|
|
68
128
|
}
|
|
69
129
|
|
|
70
|
-
//
|
|
130
|
+
// node_modules/@dataworks/sdk/dist/client/base64url.js
|
|
131
|
+
var toBase64Url = (input) => {
|
|
132
|
+
if (typeof Buffer !== "undefined") {
|
|
133
|
+
return Buffer.from(input).toString("base64url");
|
|
134
|
+
}
|
|
135
|
+
return btoa(input).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
136
|
+
};
|
|
137
|
+
var fromBase64Url = (b64url) => {
|
|
138
|
+
if (typeof Buffer !== "undefined") {
|
|
139
|
+
return Buffer.from(b64url, "base64url").toString("utf-8");
|
|
140
|
+
}
|
|
141
|
+
const padded = b64url.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((-b64url.length % 4 + 4) % 4);
|
|
142
|
+
return atob(padded);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// node_modules/@dataworks/sdk/dist/client/auth.js
|
|
71
146
|
async function loginWithCredentials(username, password, config) {
|
|
72
147
|
const authParams = {
|
|
73
148
|
USERNAME: username,
|
|
74
149
|
PASSWORD: password
|
|
75
150
|
};
|
|
76
151
|
if (config.clientSecret) {
|
|
77
|
-
|
|
78
|
-
const hash = crypto2.createHmac("sha256", config.clientSecret).update(username + config.clientId).digest("base64");
|
|
79
|
-
authParams.SECRET_HASH = hash;
|
|
152
|
+
authParams.SECRET_HASH = await computeSecretHashAsync(username, config.clientId, config.clientSecret);
|
|
80
153
|
}
|
|
81
154
|
const resp = await fetch(config.cognitoEndpoint, {
|
|
82
155
|
method: "POST",
|
|
@@ -107,15 +180,60 @@ async function loginWithCredentials(username, password, config) {
|
|
|
107
180
|
tenant
|
|
108
181
|
};
|
|
109
182
|
}
|
|
183
|
+
async function refreshWithToken(refreshToken, config) {
|
|
184
|
+
const authParams = {
|
|
185
|
+
REFRESH_TOKEN: refreshToken
|
|
186
|
+
};
|
|
187
|
+
if (config.clientSecret) {
|
|
188
|
+
if (!config.username) {
|
|
189
|
+
throw new Error("refreshWithToken: username is required when clientSecret is configured");
|
|
190
|
+
}
|
|
191
|
+
authParams.SECRET_HASH = await computeSecretHashAsync(config.username, config.clientId, config.clientSecret);
|
|
192
|
+
}
|
|
193
|
+
const resp = await fetch(config.cognitoEndpoint, {
|
|
194
|
+
method: "POST",
|
|
195
|
+
headers: {
|
|
196
|
+
"X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth",
|
|
197
|
+
"Content-Type": "application/x-amz-json-1.1"
|
|
198
|
+
},
|
|
199
|
+
body: JSON.stringify({
|
|
200
|
+
AuthFlow: "REFRESH_TOKEN_AUTH",
|
|
201
|
+
ClientId: config.clientId,
|
|
202
|
+
AuthParameters: authParams
|
|
203
|
+
})
|
|
204
|
+
});
|
|
205
|
+
if (!resp.ok) {
|
|
206
|
+
const body = await resp.text().catch(() => "");
|
|
207
|
+
throw new Error(`refreshWithToken: Cognito refresh failed: ${resp.status} \u2014 ${body}`);
|
|
208
|
+
}
|
|
209
|
+
const data = await resp.json();
|
|
210
|
+
const result = data.AuthenticationResult;
|
|
211
|
+
if (!result?.AccessToken) {
|
|
212
|
+
throw new Error("refreshWithToken: response missing AccessToken");
|
|
213
|
+
}
|
|
214
|
+
const idToken = result.IdToken ?? config.previousIdToken ?? "";
|
|
215
|
+
const extractedTenant = idToken ? extractTenantFromJwt(idToken) : "";
|
|
216
|
+
const tenant = extractedTenant || config.previousTenant || "";
|
|
217
|
+
return {
|
|
218
|
+
accessToken: result.AccessToken,
|
|
219
|
+
idToken,
|
|
220
|
+
refreshToken: result.RefreshToken ?? refreshToken,
|
|
221
|
+
tenant
|
|
222
|
+
};
|
|
223
|
+
}
|
|
110
224
|
function extractTenantFromJwt(idToken) {
|
|
111
225
|
try {
|
|
112
226
|
const payload = idToken.split(".")[1];
|
|
113
|
-
const decoded = JSON.parse(
|
|
114
|
-
return decoded["custom:tenant"] ?? decoded.tenant ?? decoded["cognito:groups"]?.[0] ?? "";
|
|
227
|
+
const decoded = JSON.parse(fromBase64Url(payload));
|
|
228
|
+
return decoded["custom:tenants"] ?? decoded["custom:tenant"] ?? decoded.tenant ?? decoded["cognito:groups"]?.[0] ?? "";
|
|
115
229
|
} catch {
|
|
116
230
|
return "";
|
|
117
231
|
}
|
|
118
232
|
}
|
|
233
|
+
async function computeSecretHashAsync(username, clientId, clientSecret) {
|
|
234
|
+
const crypto2 = await import("crypto");
|
|
235
|
+
return crypto2.createHmac("sha256", clientSecret).update(username + clientId).digest("base64");
|
|
236
|
+
}
|
|
119
237
|
|
|
120
238
|
// src/validation.ts
|
|
121
239
|
var validateMetric2 = validateMetric;
|
|
@@ -125,6 +243,8 @@ var filterValidMetrics2 = filterValidMetrics;
|
|
|
125
243
|
var DataClient = class {
|
|
126
244
|
constructor(config) {
|
|
127
245
|
this.credentials = null;
|
|
246
|
+
this.lastLoginUsername = null;
|
|
247
|
+
this.refreshInFlight = null;
|
|
128
248
|
this.config = config;
|
|
129
249
|
}
|
|
130
250
|
/**
|
|
@@ -134,11 +254,14 @@ var DataClient = class {
|
|
|
134
254
|
* @param username - Cognito username
|
|
135
255
|
* @param password - Cognito password
|
|
136
256
|
* @returns Login result containing tokens and tenant
|
|
257
|
+
* @see https://data-sdk-docs.dataworks.live/authentication
|
|
137
258
|
*/
|
|
138
259
|
async login(username, password) {
|
|
260
|
+
this.lastLoginUsername = username;
|
|
139
261
|
this.credentials = await loginWithCredentials(username, password, {
|
|
140
262
|
cognitoEndpoint: this.config.cognitoEndpoint,
|
|
141
|
-
clientId: this.config.clientId
|
|
263
|
+
clientId: this.config.clientId,
|
|
264
|
+
clientSecret: this.config.clientSecret
|
|
142
265
|
});
|
|
143
266
|
return this.credentials;
|
|
144
267
|
}
|
|
@@ -150,6 +273,7 @@ var DataClient = class {
|
|
|
150
273
|
* @param eventId - Event identifier
|
|
151
274
|
* @param datasetDatasourceId - Dataset-datasource identifier
|
|
152
275
|
* @throws Error if not authenticated or if the request fails
|
|
276
|
+
* @see https://data-sdk-docs.dataworks.live/ingesting-data
|
|
153
277
|
*/
|
|
154
278
|
async ingest(metrics, eventId, datasetDatasourceId) {
|
|
155
279
|
this.requireAuth();
|
|
@@ -163,14 +287,16 @@ var DataClient = class {
|
|
|
163
287
|
datasetDatasourceId,
|
|
164
288
|
metrics: valid
|
|
165
289
|
};
|
|
166
|
-
const resp = await
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
290
|
+
const resp = await this.fetchWithAutoRefresh(
|
|
291
|
+
(accessToken) => fetch(this.config.ingestUrl, {
|
|
292
|
+
method: "POST",
|
|
293
|
+
headers: {
|
|
294
|
+
"Content-Type": "application/json",
|
|
295
|
+
Authorization: `Bearer ${accessToken}`
|
|
296
|
+
},
|
|
297
|
+
body: JSON.stringify(payload)
|
|
298
|
+
})
|
|
299
|
+
);
|
|
174
300
|
if (!resp.ok) {
|
|
175
301
|
const body = await resp.text().catch(() => "");
|
|
176
302
|
throw new Error(
|
|
@@ -183,17 +309,26 @@ var DataClient = class {
|
|
|
183
309
|
*
|
|
184
310
|
* @param error - Error report details
|
|
185
311
|
* @throws Error if not authenticated or if the request fails
|
|
312
|
+
* @see https://data-sdk-docs.dataworks.live/error-reporting
|
|
186
313
|
*/
|
|
187
314
|
async reportError(error) {
|
|
188
315
|
this.requireAuth();
|
|
189
|
-
const resp = await
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
316
|
+
const resp = await this.fetchWithAutoRefresh(
|
|
317
|
+
(accessToken) => fetch(this.config.errorUrl, {
|
|
318
|
+
method: "POST",
|
|
319
|
+
headers: {
|
|
320
|
+
"Content-Type": "application/json",
|
|
321
|
+
Authorization: `Bearer ${accessToken}`
|
|
322
|
+
},
|
|
323
|
+
body: JSON.stringify({
|
|
324
|
+
client_id: String(error.clientId),
|
|
325
|
+
dataset_datasource_id: error.datasetDatasourceId,
|
|
326
|
+
athlete_id: error.athleteId,
|
|
327
|
+
error_title: error.errorTitle,
|
|
328
|
+
error_description: error.errorDescription
|
|
329
|
+
})
|
|
330
|
+
})
|
|
331
|
+
);
|
|
197
332
|
if (!resp.ok) {
|
|
198
333
|
const body = await resp.text().catch(() => "");
|
|
199
334
|
throw new Error(
|
|
@@ -205,55 +340,93 @@ var DataClient = class {
|
|
|
205
340
|
* Subscribe to a real-time data channel on the AppSync Events API.
|
|
206
341
|
* Uses the Cognito JWT for authentication.
|
|
207
342
|
*
|
|
208
|
-
*
|
|
343
|
+
* Channel format: dataworks/{datasetDatasourceId}/{eventId}/{metric}
|
|
344
|
+
* Use "*" for metric to receive all metrics for a dataset-datasource/event pair.
|
|
345
|
+
* Examples: "dataworks/1/1/heartrate", "dataworks/1/1/*".
|
|
209
346
|
*
|
|
210
347
|
* @param channel - Channel path to subscribe to
|
|
211
348
|
* @param onEvent - Callback invoked for each received event
|
|
212
349
|
* @returns Subscription handle with a close() method
|
|
213
350
|
* @throws Error if not authenticated
|
|
351
|
+
* @see https://data-sdk-docs.dataworks.live/real-time-subscriptions
|
|
214
352
|
*/
|
|
215
353
|
subscribe(channel, onEvent) {
|
|
216
354
|
this.requireAuth();
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
355
|
+
if (typeof WebSocket === "undefined") {
|
|
356
|
+
throw new Error(
|
|
357
|
+
"[@dataworks-technology/data] WebSocket is not available. Use Node >= 22, Bun, or a browser environment. For older Node versions, install a WebSocket polyfill (e.g. ws) and assign it to globalThis.WebSocket."
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
if (typeof crypto?.randomUUID !== "function") {
|
|
361
|
+
throw new Error(
|
|
362
|
+
"[@dataworks-technology/data] crypto.randomUUID() is not available. Use Node >= 19, Bun, or a browser with Crypto API support."
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
const channelPath = channel.startsWith("/") ? channel : `/${channel}`;
|
|
366
|
+
return createAutoRefreshingSubscription({
|
|
367
|
+
refreshAuth: async () => {
|
|
368
|
+
await this.refreshSession();
|
|
369
|
+
},
|
|
370
|
+
connect: ({ onUnexpectedClose }) => {
|
|
371
|
+
const url = new URL(this.config.realtimeUrl);
|
|
372
|
+
url.pathname = "/event/realtime";
|
|
373
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
374
|
+
const host = new URL(this.config.realtimeUrl).host;
|
|
375
|
+
const authPayload = JSON.stringify({
|
|
225
376
|
Authorization: this.credentials.accessToken,
|
|
226
|
-
host
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
377
|
+
host
|
|
378
|
+
});
|
|
379
|
+
const authHeader = toBase64Url(authPayload);
|
|
380
|
+
const ws = new WebSocket(url.toString(), [
|
|
381
|
+
"aws-appsync-event-ws",
|
|
382
|
+
`header-${authHeader}`
|
|
383
|
+
]);
|
|
384
|
+
let closedByUser = false;
|
|
385
|
+
let unexpectedCloseHandled = false;
|
|
386
|
+
const handleUnexpectedCloseOnce = () => {
|
|
387
|
+
if (unexpectedCloseHandled) return;
|
|
388
|
+
unexpectedCloseHandled = true;
|
|
389
|
+
onUnexpectedClose();
|
|
390
|
+
};
|
|
391
|
+
ws.addEventListener("open", () => {
|
|
392
|
+
ws.send(JSON.stringify({ type: "connection_init" }));
|
|
393
|
+
});
|
|
394
|
+
ws.addEventListener("message", (event) => {
|
|
395
|
+
try {
|
|
396
|
+
const msg = JSON.parse(String(event.data));
|
|
397
|
+
if (msg.type === "connection_ack") {
|
|
398
|
+
ws.send(
|
|
399
|
+
JSON.stringify({
|
|
400
|
+
type: "subscribe",
|
|
401
|
+
id: crypto.randomUUID(),
|
|
402
|
+
channel: channelPath,
|
|
403
|
+
authorization: {
|
|
404
|
+
Authorization: this.credentials.accessToken,
|
|
405
|
+
host
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
} else if (msg.type === "data") {
|
|
410
|
+
onEvent(JSON.parse(msg.event));
|
|
411
|
+
} else if (msg.type === "error" && JSON.stringify(msg).toLowerCase().includes("unauthor")) {
|
|
412
|
+
handleUnexpectedCloseOnce();
|
|
413
|
+
}
|
|
414
|
+
} catch {
|
|
239
415
|
}
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
416
|
+
});
|
|
417
|
+
ws.addEventListener("close", () => {
|
|
418
|
+
if (!closedByUser) {
|
|
419
|
+
handleUnexpectedCloseOnce();
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
return {
|
|
423
|
+
close() {
|
|
424
|
+
closedByUser = true;
|
|
425
|
+
ws.close();
|
|
426
|
+
}
|
|
427
|
+
};
|
|
250
428
|
}
|
|
251
429
|
});
|
|
252
|
-
return {
|
|
253
|
-
close() {
|
|
254
|
-
ws.close();
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
430
|
}
|
|
258
431
|
/** Returns true if the client has been authenticated via login(). */
|
|
259
432
|
get isAuthenticated() {
|
|
@@ -271,6 +444,42 @@ var DataClient = class {
|
|
|
271
444
|
);
|
|
272
445
|
}
|
|
273
446
|
}
|
|
447
|
+
async fetchWithAutoRefresh(makeRequest) {
|
|
448
|
+
this.requireAuth();
|
|
449
|
+
let response = await makeRequest(this.credentials.accessToken);
|
|
450
|
+
if (response.status !== 401) {
|
|
451
|
+
return response;
|
|
452
|
+
}
|
|
453
|
+
await this.refreshSession();
|
|
454
|
+
response = await makeRequest(this.credentials.accessToken);
|
|
455
|
+
return response;
|
|
456
|
+
}
|
|
457
|
+
async refreshSession() {
|
|
458
|
+
this.requireAuth();
|
|
459
|
+
if (!this.credentials.refreshToken) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"[@dataworks-technology/data] Session expired and no refresh token is available \u2014 call login() again"
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
if (!this.refreshInFlight) {
|
|
465
|
+
this.refreshInFlight = (async () => {
|
|
466
|
+
this.credentials = await refreshWithToken(
|
|
467
|
+
this.credentials.refreshToken,
|
|
468
|
+
{
|
|
469
|
+
cognitoEndpoint: this.config.cognitoEndpoint,
|
|
470
|
+
clientId: this.config.clientId,
|
|
471
|
+
clientSecret: this.config.clientSecret,
|
|
472
|
+
username: this.lastLoginUsername ?? void 0,
|
|
473
|
+
previousTenant: this.credentials.tenant,
|
|
474
|
+
previousIdToken: this.credentials.idToken
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
})().finally(() => {
|
|
478
|
+
this.refreshInFlight = null;
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
await this.refreshInFlight;
|
|
482
|
+
}
|
|
274
483
|
};
|
|
275
484
|
/**
|
|
276
485
|
* Check whether a metric is valid before ingesting.
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../../../node_modules/@dataworks/sdk/dist/client/validation.js","../../../node_modules/@dataworks/sdk/dist/client/auth.js","../src/validation.ts","../src/data-client.ts"],"sourcesContent":["/**\n * @dataworks-technology/data — Dataworks Data Engine SDK\n *\n * Public npm package for external developers to:\n * - Authenticate via Cognito\n * - Ingest live athlete metrics\n * - Subscribe to real-time data streams\n * - Report errors\n *\n * @example\n * ```ts\n * import { DataClient } from \"@dataworks-technology/data\";\n *\n * const client = new DataClient({ ... });\n * await client.login(\"username\", \"password\");\n * await client.ingest(metrics, eventId, dsId);\n * ```\n *\n * @module @dataworks-technology/data\n */\n\nexport { DataClient } from \"./data-client.js\";\n\nexport type {\n DataClientConfig,\n LoginResult,\n Metric,\n MetricItem,\n RawMetricItem,\n MetricsPayload,\n ErrorReport,\n SubscriptionHandler,\n Subscription,\n} from \"./types.js\";\n\n// Re-export validation utilities for standalone use\nexport { validateMetric, filterValidMetrics } from \"./validation.js\";\n","/**\n * Metric validation for Dataworks payloads.\n *\n * Ported from Dataworks-Data packages/adaptors/shared-ts/src/validation.ts\n * to serve as the single source of truth for all Dataworks engine SDKs.\n *\n * @module @dataworks/sdk/client\n */\n/**\n * Returns null if the metric is valid, or a human-readable reason string if invalid.\n */\nexport function validateMetric(m) {\n if (typeof m.metric !== \"string\" || m.metric.trim() === \"\")\n return \"metric must be a non-empty string\";\n if (typeof m.athleteId !== \"string\" || m.athleteId.trim() === \"\")\n return \"athleteId must be a non-empty string\";\n if (!Number.isInteger(m.timestamp) || m.timestamp <= 0)\n return `timestamp must be a positive integer, got ${m.timestamp}`;\n if (m.value === null || m.value === undefined)\n return \"value is null/undefined\";\n if (typeof m.value === \"number\" && !isFinite(m.value))\n return `value is non-finite number (${m.value})`;\n if (typeof m.value !== \"number\" && typeof m.value !== \"string\")\n return `value type ${typeof m.value} not allowed (must be number or string)`;\n if (typeof m.value === \"string\" && m.value === \"\")\n return \"value is empty string\";\n return null;\n}\n/**\n * Filters an array of metrics, returning only valid ones.\n * Logs a warning for each dropped item via the provided warn function.\n */\nexport function filterValidMetrics(metrics, warn) {\n const valid = [];\n for (const m of metrics) {\n const reason = validateMetric(m);\n if (reason) {\n warn(`Dropping invalid metric [${String(m.metric)}=${JSON.stringify(m.value)}]: ${reason}`);\n }\n else {\n valid.push(m);\n }\n }\n return valid;\n}\n","/**\n * Cognito authentication helpers.\n *\n * Provides M2M (client_credentials) and user (USER_PASSWORD_AUTH) flows.\n * Refactored from @dataworks/sdk e2e-utils.ts — uses native fetch instead\n * of axios so the client sub-path has zero heavy dependencies.\n *\n * @module @dataworks/sdk/client\n */\n/**\n * Fetch an M2M access token using the client_credentials grant.\n *\n * @param clientId - Cognito app client ID\n * @param clientSecret - Cognito app client secret\n * @param tokenUrl - Cognito token endpoint (e.g. https://<domain>.auth.<region>.amazoncognito.com/oauth2/token)\n * @returns Access token string\n */\nexport async function getClientToken(clientId, clientSecret, tokenUrl) {\n const resp = await fetch(tokenUrl, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/x-www-form-urlencoded\" },\n body: new URLSearchParams({\n grant_type: \"client_credentials\",\n client_id: clientId,\n client_secret: clientSecret,\n }),\n });\n if (!resp.ok) {\n throw new Error(`getClientToken: Cognito token request failed: ${resp.status} ${resp.statusText}`);\n }\n const data = (await resp.json());\n if (!data.access_token) {\n throw new Error(\"getClientToken: response missing access_token\");\n }\n return data.access_token;\n}\n/**\n * Authenticate a user via Cognito USER_PASSWORD_AUTH flow.\n * Returns access token, ID token, refresh token, and tenant (extracted from JWT).\n *\n * Used by external developer clients — no client secret required when the\n * Cognito app client is configured without one.\n *\n * @param username - Cognito username\n * @param password - Cognito password\n * @param config - Cognito endpoint and client configuration\n * @returns Login result with tokens and tenant\n */\nexport async function loginWithCredentials(username, password, config) {\n const authParams = {\n USERNAME: username,\n PASSWORD: password,\n };\n // If a client secret is configured, compute SECRET_HASH\n if (config.clientSecret) {\n // Dynamic import to keep this module usable in environments without crypto\n const crypto = await import(\"crypto\");\n const hash = crypto\n .createHmac(\"sha256\", config.clientSecret)\n .update(username + config.clientId)\n .digest(\"base64\");\n authParams.SECRET_HASH = hash;\n }\n const resp = await fetch(config.cognitoEndpoint, {\n method: \"POST\",\n headers: {\n \"X-Amz-Target\": \"AWSCognitoIdentityProviderService.InitiateAuth\",\n \"Content-Type\": \"application/x-amz-json-1.1\",\n },\n body: JSON.stringify({\n AuthFlow: \"USER_PASSWORD_AUTH\",\n ClientId: config.clientId,\n AuthParameters: authParams,\n }),\n });\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(`loginWithCredentials: Cognito auth failed: ${resp.status} — ${body}`);\n }\n const data = (await resp.json());\n const result = data.AuthenticationResult;\n if (!result?.AccessToken || !result?.IdToken) {\n throw new Error(\"loginWithCredentials: response missing AccessToken or IdToken\");\n }\n // Extract tenant from JWT claims (ID token payload)\n const tenant = extractTenantFromJwt(result.IdToken);\n return {\n accessToken: result.AccessToken,\n idToken: result.IdToken,\n refreshToken: result.RefreshToken ?? \"\",\n tenant,\n };\n}\n/**\n * Extracts the tenant claim from a JWT ID token without verifying the signature.\n * The token is validated server-side by AppSync/API Gateway — this is a client-side\n * convenience for reading the tenant value.\n */\nfunction extractTenantFromJwt(idToken) {\n try {\n const payload = idToken.split(\".\")[1];\n const decoded = JSON.parse(Buffer.from(payload, \"base64url\").toString(\"utf-8\"));\n // Cognito custom attributes use \"custom:tenant\" claim\n return (decoded[\"custom:tenant\"] ?? decoded.tenant ?? decoded[\"cognito:groups\"]?.[0] ?? \"\");\n }\n catch {\n return \"\";\n }\n}\n","/**\n * Validation re-exports — thin wrappers around @dataworks/sdk/client validation.\n * Declared locally so the published .d.ts is fully self-contained.\n *\n * @module @dataworks-technology/data\n */\n\nimport {\n validateMetric as _validateMetric,\n filterValidMetrics as _filterValidMetrics,\n} from \"@dataworks/sdk/client\";\nimport type { RawMetricItem, MetricItem } from \"./types.js\";\n\n/**\n * Check whether a metric is valid.\n * Returns null if valid, or a human-readable reason string if invalid.\n */\nexport const validateMetric: (m: RawMetricItem) => string | null =\n _validateMetric;\n\n/**\n * Filter an array of metrics, keeping only valid ones.\n * Logs a warning for each dropped item via the provided warn function.\n */\nexport const filterValidMetrics: (\n metrics: RawMetricItem[],\n warn: (msg: string) => void,\n) => MetricItem[] = _filterValidMetrics;\n","/**\n * DataClient — the public entry point for external developers using the Dataworks Data Engine.\n *\n * Handles authentication, metric ingestion, error reporting, and real-time subscriptions.\n * All SDK foundation code (@dataworks/sdk) is bundled at build time — consumers install\n * only @dataworks/data with zero transitive dependencies.\n *\n * @example\n * ```ts\n * import { DataClient } from \"@dataworks-technology/data\";\n *\n * const client = new DataClient({\n * cognitoEndpoint: \"https://cognito-idp.eu-west-1.amazonaws.com/\",\n * clientId: \"abc123\",\n * ingestUrl: \"https://dev-realtime.dataworks.live\",\n * errorUrl: \"https://dev-realtime-errors.dataworks.live\",\n * realtimeUrl: \"https://dev-event-api.dataworks.live\",\n * });\n *\n * await client.login(\"graeme\", \"password123\");\n * await client.ingest([{ athleteId: \"1\", metric: \"heartrate\", value: 172, timestamp: Date.now() / 1000 }], \"evt1\", \"ds1\");\n * ```\n *\n * @module @dataworks-technology/data\n */\n\nimport { loginWithCredentials } from \"@dataworks/sdk/client\";\nimport { validateMetric, filterValidMetrics } from \"./validation.js\";\nimport type {\n DataClientConfig,\n LoginResult,\n Metric,\n ErrorReport,\n SubscriptionHandler,\n Subscription,\n} from \"./types.js\";\n\nexport class DataClient {\n private readonly config: DataClientConfig;\n private credentials: LoginResult | null = null;\n\n constructor(config: DataClientConfig) {\n this.config = config;\n }\n\n /**\n * Authenticate with the Dataworks platform using Cognito USER_PASSWORD_AUTH.\n * Must be called before ingest(), reportError(), or subscribe().\n *\n * @param username - Cognito username\n * @param password - Cognito password\n * @returns Login result containing tokens and tenant\n */\n async login(username: string, password: string): Promise<LoginResult> {\n this.credentials = await loginWithCredentials(username, password, {\n cognitoEndpoint: this.config.cognitoEndpoint,\n clientId: this.config.clientId,\n });\n return this.credentials;\n }\n\n /**\n * Ingest metric data points into the Dataworks Data Engine.\n * Validates each metric before sending — invalid metrics are silently dropped.\n *\n * @param metrics - Array of metric data points\n * @param eventId - Event identifier\n * @param datasetDatasourceId - Dataset-datasource identifier\n * @throws Error if not authenticated or if the request fails\n */\n async ingest(\n metrics: Metric[],\n eventId: string,\n datasetDatasourceId: string,\n ): Promise<void> {\n this.requireAuth();\n\n const valid = filterValidMetrics(metrics, (msg) =>\n console.warn(`[@dataworks-technology/data] ${msg}`),\n );\n\n if (valid.length === 0) return;\n\n const payload = {\n eventId,\n datasetDatasourceId,\n metrics: valid,\n };\n\n const resp = await fetch(this.config.ingestUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.credentials!.accessToken}`,\n },\n body: JSON.stringify(payload),\n });\n\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(\n `[@dataworks-technology/data] ingest failed: ${resp.status} — ${body}`,\n );\n }\n }\n\n /**\n * Report an error to the Dataworks Data Engine.\n *\n * @param error - Error report details\n * @throws Error if not authenticated or if the request fails\n */\n async reportError(error: ErrorReport): Promise<void> {\n this.requireAuth();\n\n const resp = await fetch(this.config.errorUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.credentials!.accessToken}`,\n },\n body: JSON.stringify(error),\n });\n\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(\n `[@dataworks-technology/data] reportError failed: ${resp.status} — ${body}`,\n );\n }\n }\n\n /**\n * Subscribe to a real-time data channel on the AppSync Events API.\n * Uses the Cognito JWT for authentication.\n *\n * The channel name maps to the AppSync Events API namespace (e.g. \"dataworks/metrics\").\n *\n * @param channel - Channel path to subscribe to\n * @param onEvent - Callback invoked for each received event\n * @returns Subscription handle with a close() method\n * @throws Error if not authenticated\n */\n subscribe(channel: string, onEvent: SubscriptionHandler): Subscription {\n this.requireAuth();\n\n const url = new URL(this.config.realtimeUrl);\n // AppSync Events API uses the /event/realtime path for WebSocket connections\n url.pathname = \"/event/realtime\";\n url.protocol = url.protocol === \"https:\" ? \"wss:\" : \"ws:\";\n\n const ws = new WebSocket(url.toString(), [\n \"aws-appsync-event-ws\",\n // Encode auth as a base64 subprotocol header\n `header-${btoa(\n JSON.stringify({\n Authorization: this.credentials!.accessToken,\n host: new URL(this.config.realtimeUrl).host,\n }),\n )}`,\n ]);\n\n ws.addEventListener(\"open\", () => {\n // Subscribe to the channel after connection is established\n ws.send(\n JSON.stringify({\n type: \"subscribe\",\n id: crypto.randomUUID(),\n channel: `/${channel}`,\n authorization: {\n Authorization: this.credentials!.accessToken,\n host: new URL(this.config.realtimeUrl).host,\n },\n }),\n );\n });\n\n ws.addEventListener(\"message\", (event) => {\n try {\n const msg = JSON.parse(String(event.data));\n if (msg.type === \"data\") {\n onEvent(JSON.parse(msg.event));\n }\n } catch {\n // Ignore non-data messages (connection_ack, subscribe_success, etc.)\n }\n });\n\n return {\n close() {\n ws.close();\n },\n };\n }\n\n /**\n * Check whether a metric is valid before ingesting.\n * Returns null if valid, or a human-readable reason string if invalid.\n */\n static validateMetric = validateMetric;\n\n /**\n * Filter an array of metrics, keeping only valid ones.\n * Logs a warning for each dropped item.\n */\n static filterValidMetrics = filterValidMetrics;\n\n /** Returns true if the client has been authenticated via login(). */\n get isAuthenticated(): boolean {\n return this.credentials !== null;\n }\n\n /** Returns the tenant from the last successful login, or null. */\n get tenant(): string | null {\n return this.credentials?.tenant ?? null;\n }\n\n /** @internal Throws if not authenticated. */\n private requireAuth(): void {\n if (!this.credentials) {\n throw new Error(\n \"[@dataworks-technology/data] Not authenticated — call login() first\",\n );\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,4BAAAA;AAAA,EAAA,sBAAAC;AAAA;AAAA;;;ACWO,SAAS,eAAe,GAAG;AAC9B,MAAI,OAAO,EAAE,WAAW,YAAY,EAAE,OAAO,KAAK,MAAM;AACpD,WAAO;AACX,MAAI,OAAO,EAAE,cAAc,YAAY,EAAE,UAAU,KAAK,MAAM;AAC1D,WAAO;AACX,MAAI,CAAC,OAAO,UAAU,EAAE,SAAS,KAAK,EAAE,aAAa;AACjD,WAAO,6CAA6C,EAAE,SAAS;AACnE,MAAI,EAAE,UAAU,QAAQ,EAAE,UAAU;AAChC,WAAO;AACX,MAAI,OAAO,EAAE,UAAU,YAAY,CAAC,SAAS,EAAE,KAAK;AAChD,WAAO,+BAA+B,EAAE,KAAK;AACjD,MAAI,OAAO,EAAE,UAAU,YAAY,OAAO,EAAE,UAAU;AAClD,WAAO,cAAc,OAAO,EAAE,KAAK;AACvC,MAAI,OAAO,EAAE,UAAU,YAAY,EAAE,UAAU;AAC3C,WAAO;AACX,SAAO;AACX;AAKO,SAAS,mBAAmB,SAAS,MAAM;AAC9C,QAAM,QAAQ,CAAC;AACf,aAAW,KAAK,SAAS;AACrB,UAAM,SAAS,eAAe,CAAC;AAC/B,QAAI,QAAQ;AACR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OAAO;AAAA,UACpB,eAAe;AAAA,YACb,eAAe,KAAK,YAAa;AAAA,YACjC,MAAM,IAAI,IAAI,KAAK,OAAO,WAAW,EAAE;AAAA,UACzC;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAED,OAAG,iBAAiB,WAAW,CAAC,UAAU;AACxC,UAAI;AACF,cAAM,MAAM,KAAK,MAAM,OAAO,MAAM,IAAI,CAAC;AACzC,YAAI,IAAI,SAAS,QAAQ;AACvB,kBAAQ,KAAK,MAAM,IAAI,KAAK,CAAC;AAAA,QAC/B;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF,CAAC;AAED,WAAO;AAAA,MACL,QAAQ;AACN,WAAG,MAAM;AAAA,MACX;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAeA,IAAI,kBAA2B;AAC7B,WAAO,KAAK,gBAAgB;AAAA,EAC9B;AAAA;AAAA,EAGA,IAAI,SAAwB;AAC1B,WAAO,KAAK,aAAa,UAAU;AAAA,EACrC;AAAA;AAAA,EAGQ,cAAoB;AAC1B,QAAI,CAAC,KAAK,aAAa;AACrB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAAA;AAAA;AAAA;AAAA;AA5La,WAkKJ,iBAAiBC;AAAA;AAAA;AAAA;AAAA;AAlKb,WAwKJ,qBAAqBD;","names":["filterValidMetrics","validateMetric","crypto","validateMetric","filterValidMetrics","filterValidMetrics","validateMetric"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../node_modules/@dataworks/sdk/dist/client/subscription-manager.js","../node_modules/@dataworks/sdk/dist/client/validation.js","../node_modules/@dataworks/sdk/dist/client/base64url.js","../node_modules/@dataworks/sdk/dist/client/auth.js","../src/validation.ts","../src/data-client.ts"],"sourcesContent":["/**\n * @dataworks-technology/data — Dataworks Data Engine SDK\n *\n * Public npm package for external developers to:\n * - Authenticate via Cognito\n * - Ingest live athlete metrics\n * - Subscribe to real-time data streams\n * - Report errors\n *\n * @example\n * ```ts\n * import { DataClient } from \"@dataworks-technology/data\";\n *\n * const dataworks = new DataClient({ ... });\n * await dataworks.login(\"username\", \"password\");\n * await dataworks.ingest(metrics, eventId, dsId);\n * ```\n *\n * @module @dataworks-technology/data\n */\n\nexport { DataClient } from \"./data-client.js\";\n\nexport type {\n DataClientConfig,\n LoginResult,\n Metric,\n MetricItem,\n RawMetricItem,\n MetricsPayload,\n ErrorReport,\n SubscriptionHandler,\n Subscription,\n} from \"./types.js\";\n\n// Re-export validation utilities for standalone use\nexport { validateMetric, filterValidMetrics } from \"./validation.js\";\n","/**\n * Generic subscription lifecycle manager with auth-refresh reconnect support.\n *\n * The protocol-specific client provides a `connect` function and calls\n * `onUnexpectedClose` when auth expiry or unexpected socket termination occurs.\n */\nexport function createAutoRefreshingSubscription(options) {\n const maxReconnectAttempts = options.maxReconnectAttempts ?? 1;\n let reconnectAttempts = 0;\n let reconnectInFlight = false;\n let closedByUser = false;\n let activeConnection = null;\n const hooks = {\n onUnexpectedClose: () => {\n if (closedByUser || reconnectInFlight) {\n return;\n }\n if (reconnectAttempts >= maxReconnectAttempts) {\n return;\n }\n reconnectInFlight = true;\n reconnectAttempts += 1;\n void (async () => {\n try {\n await options.refreshAuth();\n if (closedByUser) {\n return;\n }\n activeConnection = options.connect(hooks);\n }\n catch {\n options.onReconnectError?.(new Error(\"Subscription reconnect failed. Please retry or reauthenticate.\"));\n }\n finally {\n reconnectInFlight = false;\n }\n })();\n },\n };\n activeConnection = options.connect(hooks);\n return {\n close() {\n closedByUser = true;\n activeConnection?.close();\n activeConnection = null;\n },\n };\n}\n","/**\n * Metric validation for Dataworks payloads.\n *\n * Ported from Dataworks-Data packages/adaptors/shared-ts/src/validation.ts\n * to serve as the single source of truth for all Dataworks engine SDKs.\n *\n * @module @dataworks/sdk/client\n */\n/**\n * Returns null if the metric is valid, or a human-readable reason string if invalid.\n */\nexport function validateMetric(m) {\n if (typeof m.metric !== \"string\" || m.metric.trim() === \"\")\n return \"metric must be a non-empty string\";\n if (typeof m.athleteId !== \"string\" || m.athleteId.trim() === \"\")\n return \"athleteId must be a non-empty string\";\n if (!Number.isInteger(m.timestamp) || m.timestamp <= 0)\n return `timestamp must be a positive integer, got ${m.timestamp}`;\n if (m.value === null || m.value === undefined)\n return \"value is null/undefined\";\n if (typeof m.value === \"number\" && !isFinite(m.value))\n return `value is non-finite number (${m.value})`;\n if (typeof m.value !== \"number\" && typeof m.value !== \"string\")\n return `value type ${typeof m.value} not allowed (must be number or string)`;\n if (typeof m.value === \"string\" && m.value === \"\")\n return \"value is empty string\";\n return null;\n}\n/**\n * Filters an array of metrics, returning only valid ones.\n * Logs a warning for each dropped item via the provided warn function.\n */\nexport function filterValidMetrics(metrics, warn) {\n const { valid, dropped } = getMetricValidationResult(metrics);\n for (const item of dropped) {\n let valueStr;\n try {\n valueStr = JSON.stringify(item.metric.value);\n }\n catch {\n valueStr = String(item.metric.value);\n }\n warn(`Dropping invalid metric [${String(item.metric.metric)}=${valueStr}]: ${item.reason}`);\n }\n return valid;\n}\n/**\n * Validates metrics and returns both accepted and dropped items (with reasons).\n * This is useful for consumers that need structured diagnostics.\n */\nexport function getMetricValidationResult(metrics) {\n const valid = [];\n const dropped = [];\n for (const [index, m] of metrics.entries()) {\n const reason = validateMetric(m);\n if (reason) {\n dropped.push({\n index,\n reason,\n metric: m,\n });\n }\n else {\n valid.push(m);\n }\n }\n return { valid, dropped };\n}\n","/**\n * Runtime-agnostic base64url encode/decode.\n *\n * Prefers Node.js Buffer when available, falls back to btoa/atob for\n * browser and edge runtimes. Used for AppSync auth subprotocol headers\n * and JWT payload parsing.\n *\n * @module @dataworks/sdk/client\n */\n/** Encode a UTF-8 string to base64url. */\nexport const toBase64Url = (input) => {\n if (typeof Buffer !== \"undefined\") {\n return Buffer.from(input).toString(\"base64url\");\n }\n return btoa(input).replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=+$/, \"\");\n};\n/** Decode a base64url string to UTF-8. */\nexport const fromBase64Url = (b64url) => {\n if (typeof Buffer !== \"undefined\") {\n return Buffer.from(b64url, \"base64url\").toString(\"utf-8\");\n }\n const padded = b64url.replace(/-/g, \"+\").replace(/_/g, \"/\") +\n \"=\".repeat(((-b64url.length % 4) + 4) % 4);\n return atob(padded);\n};\n","/**\n * Cognito authentication helpers.\n *\n * Provides M2M (client_credentials) and user (USER_PASSWORD_AUTH) flows.\n * Refactored from @dataworks/sdk e2e-utils.ts — uses native fetch instead\n * of axios so the client sub-path has zero heavy dependencies.\n *\n * @module @dataworks/sdk/client\n */\nimport { fromBase64Url } from \"./base64url.js\";\n/**\n * Fetch an M2M access token using the client_credentials grant.\n *\n * @param clientId - Cognito app client ID\n * @param clientSecret - Cognito app client secret\n * @param tokenUrl - Cognito token endpoint (e.g. https://<domain>.auth.<region>.amazoncognito.com/oauth2/token)\n * @returns Access token string\n */\nexport async function getClientToken(clientId, clientSecret, tokenUrl) {\n const resp = await fetch(tokenUrl, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/x-www-form-urlencoded\" },\n body: new URLSearchParams({\n grant_type: \"client_credentials\",\n client_id: clientId,\n client_secret: clientSecret,\n }),\n });\n if (!resp.ok) {\n throw new Error(`getClientToken: Cognito token request failed: ${resp.status} ${resp.statusText}`);\n }\n const data = (await resp.json());\n if (!data.access_token) {\n throw new Error(\"getClientToken: response missing access_token\");\n }\n return data.access_token;\n}\n/**\n * Authenticate a user via Cognito USER_PASSWORD_AUTH flow.\n * Returns access token, ID token, refresh token, and tenant (extracted from JWT).\n *\n * Used by external developer clients — no client secret required when the\n * Cognito app client is configured without one.\n *\n * @param username - Cognito username\n * @param password - Cognito password\n * @param config - Cognito endpoint and client configuration\n * @returns Login result with tokens and tenant\n */\nexport async function loginWithCredentials(username, password, config) {\n const authParams = {\n USERNAME: username,\n PASSWORD: password,\n };\n // If a client secret is configured, compute SECRET_HASH\n if (config.clientSecret) {\n authParams.SECRET_HASH = await computeSecretHashAsync(username, config.clientId, config.clientSecret);\n }\n const resp = await fetch(config.cognitoEndpoint, {\n method: \"POST\",\n headers: {\n \"X-Amz-Target\": \"AWSCognitoIdentityProviderService.InitiateAuth\",\n \"Content-Type\": \"application/x-amz-json-1.1\",\n },\n body: JSON.stringify({\n AuthFlow: \"USER_PASSWORD_AUTH\",\n ClientId: config.clientId,\n AuthParameters: authParams,\n }),\n });\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(`loginWithCredentials: Cognito auth failed: ${resp.status} — ${body}`);\n }\n const data = (await resp.json());\n const result = data.AuthenticationResult;\n if (!result?.AccessToken || !result?.IdToken) {\n throw new Error(\"loginWithCredentials: response missing AccessToken or IdToken\");\n }\n // Extract tenant from JWT claims (ID token payload)\n const tenant = extractTenantFromJwt(result.IdToken);\n return {\n accessToken: result.AccessToken,\n idToken: result.IdToken,\n refreshToken: result.RefreshToken ?? \"\",\n tenant,\n };\n}\n/**\n * Refreshes Cognito user tokens using REFRESH_TOKEN_AUTH.\n *\n * Returns updated tokens and tenant, preserving previous tenant/idToken when\n * Cognito omits fields in the refresh response.\n *\n * @param refreshToken - The refresh token string\n * @param config - Cognito endpoint and client configuration, plus optional username/previousTenant/previousIdToken\n */\nexport async function refreshWithToken(refreshToken, config) {\n const authParams = {\n REFRESH_TOKEN: refreshToken,\n };\n if (config.clientSecret) {\n if (!config.username) {\n throw new Error(\"refreshWithToken: username is required when clientSecret is configured\");\n }\n authParams.SECRET_HASH = await computeSecretHashAsync(config.username, config.clientId, config.clientSecret);\n }\n const resp = await fetch(config.cognitoEndpoint, {\n method: \"POST\",\n headers: {\n \"X-Amz-Target\": \"AWSCognitoIdentityProviderService.InitiateAuth\",\n \"Content-Type\": \"application/x-amz-json-1.1\",\n },\n body: JSON.stringify({\n AuthFlow: \"REFRESH_TOKEN_AUTH\",\n ClientId: config.clientId,\n AuthParameters: authParams,\n }),\n });\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(`refreshWithToken: Cognito refresh failed: ${resp.status} — ${body}`);\n }\n const data = (await resp.json());\n const result = data.AuthenticationResult;\n if (!result?.AccessToken) {\n throw new Error(\"refreshWithToken: response missing AccessToken\");\n }\n const idToken = result.IdToken ?? config.previousIdToken ?? \"\";\n const extractedTenant = idToken ? extractTenantFromJwt(idToken) : \"\";\n const tenant = extractedTenant || config.previousTenant || \"\";\n return {\n accessToken: result.AccessToken,\n idToken,\n refreshToken: result.RefreshToken ?? refreshToken,\n tenant,\n };\n}\n/**\n * Extracts the tenant claim from a JWT ID token without verifying the signature.\n * The token is validated server-side by AppSync/API Gateway — this is a client-side\n * convenience for reading the tenant value.\n */\nfunction extractTenantFromJwt(idToken) {\n try {\n const payload = idToken.split(\".\")[1];\n const decoded = JSON.parse(fromBase64Url(payload));\n // Cognito custom attributes use \"custom:tenant\" claim\n return (decoded[\"custom:tenants\"] ??\n decoded[\"custom:tenant\"] ??\n decoded.tenant ??\n decoded[\"cognito:groups\"]?.[0] ??\n \"\");\n }\n catch {\n return \"\";\n }\n}\nasync function computeSecretHashAsync(username, clientId, clientSecret) {\n // Dynamic import to keep this module usable in environments without crypto.\n const crypto = await import(\"crypto\");\n return crypto\n .createHmac(\"sha256\", clientSecret)\n .update(username + clientId)\n .digest(\"base64\");\n}\n","/**\n * Validation re-exports — thin wrappers around @dataworks/sdk/client validation.\n * Declared locally so the published .d.ts is fully self-contained.\n *\n * @module @dataworks-technology/data\n */\n\nimport {\n validateMetric as _validateMetric,\n filterValidMetrics as _filterValidMetrics,\n} from \"@dataworks/sdk/client\";\nimport type { RawMetricItem, MetricItem } from \"./types.js\";\n\n/**\n * Check whether a metric is valid.\n * Returns null if valid, or a human-readable reason string if invalid.\n */\nexport const validateMetric: (m: RawMetricItem) => string | null =\n _validateMetric;\n\n/**\n * Filter an array of metrics, keeping only valid ones.\n * Logs a warning for each dropped item via the provided warn function.\n */\nexport const filterValidMetrics: (\n metrics: RawMetricItem[],\n warn: (msg: string) => void,\n) => MetricItem[] = _filterValidMetrics;\n","/**\n * DataClient — the public entry point for external developers using the Dataworks Data Engine.\n *\n * Handles authentication, metric ingestion, error reporting, and real-time subscriptions.\n * All SDK foundation code (@dataworks/sdk) is bundled at build time — consumers install\n * only @dataworks-technology/data with zero transitive dependencies.\n *\n * @example\n * ```ts\n * import { DataClient } from \"@dataworks-technology/data\";\n *\n * const dataworks = new DataClient({\n * cognitoEndpoint: \"https://cognito-idp.eu-west-1.amazonaws.com/\",\n * clientId: \"abc123\",\n * ingestUrl: \"https://dev-realtime.dataworks.live\",\n * errorUrl: \"https://dev-realtime-errors.dataworks.live\",\n * realtimeUrl: \"https://dev-event-api.dataworks.live\",\n * });\n *\n * await dataworks.login(\"graeme\", \"password123\");\n * await dataworks.ingest([{ athleteId: \"1\", metric: \"heartrate_calculated\", value: 172, timestamp: Date.now() / 1000 }], \"evt1\", \"ds1\");\n * ```\n *\n * @module @dataworks-technology/data\n */\n\nimport {\n createAutoRefreshingSubscription,\n loginWithCredentials,\n refreshWithToken,\n toBase64Url,\n} from \"@dataworks/sdk/client\";\nimport { validateMetric, filterValidMetrics } from \"./validation.js\";\nimport type {\n DataClientConfig,\n LoginResult,\n Metric,\n ErrorReport,\n SubscriptionHandler,\n Subscription,\n} from \"./types.js\";\n\nexport class DataClient {\n private readonly config: DataClientConfig;\n private credentials: LoginResult | null = null;\n private lastLoginUsername: string | null = null;\n private refreshInFlight: Promise<void> | null = null;\n\n constructor(config: DataClientConfig) {\n this.config = config;\n }\n\n /**\n * Authenticate with the Dataworks platform using Cognito USER_PASSWORD_AUTH.\n * Must be called before ingest(), reportError(), or subscribe().\n *\n * @param username - Cognito username\n * @param password - Cognito password\n * @returns Login result containing tokens and tenant\n * @see https://data-sdk-docs.dataworks.live/authentication\n */\n async login(username: string, password: string): Promise<LoginResult> {\n this.lastLoginUsername = username;\n this.credentials = await loginWithCredentials(username, password, {\n cognitoEndpoint: this.config.cognitoEndpoint,\n clientId: this.config.clientId,\n clientSecret: this.config.clientSecret,\n });\n return this.credentials;\n }\n\n /**\n * Ingest metric data points into the Dataworks Data Engine.\n * Validates each metric before sending — invalid metrics are silently dropped.\n *\n * @param metrics - Array of metric data points\n * @param eventId - Event identifier\n * @param datasetDatasourceId - Dataset-datasource identifier\n * @throws Error if not authenticated or if the request fails\n * @see https://data-sdk-docs.dataworks.live/ingesting-data\n */\n async ingest(\n metrics: Metric[],\n eventId: string,\n datasetDatasourceId: string,\n ): Promise<void> {\n this.requireAuth();\n\n const valid = filterValidMetrics(metrics, (msg) =>\n console.warn(`[@dataworks-technology/data] ${msg}`),\n );\n\n if (valid.length === 0) return;\n\n const payload = {\n eventId,\n datasetDatasourceId,\n metrics: valid,\n };\n\n const resp = await this.fetchWithAutoRefresh((accessToken) =>\n fetch(this.config.ingestUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${accessToken}`,\n },\n body: JSON.stringify(payload),\n }),\n );\n\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(\n `[@dataworks-technology/data] ingest failed: ${resp.status} — ${body}`,\n );\n }\n }\n\n /**\n * Report an error to the Dataworks Data Engine.\n *\n * @param error - Error report details\n * @throws Error if not authenticated or if the request fails\n * @see https://data-sdk-docs.dataworks.live/error-reporting\n */\n async reportError(error: ErrorReport): Promise<void> {\n this.requireAuth();\n\n const resp = await this.fetchWithAutoRefresh((accessToken) =>\n fetch(this.config.errorUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${accessToken}`,\n },\n body: JSON.stringify({\n client_id: String(error.clientId),\n dataset_datasource_id: error.datasetDatasourceId,\n athlete_id: error.athleteId,\n error_title: error.errorTitle,\n error_description: error.errorDescription,\n }),\n }),\n );\n\n if (!resp.ok) {\n const body = await resp.text().catch(() => \"\");\n throw new Error(\n `[@dataworks-technology/data] reportError failed: ${resp.status} — ${body}`,\n );\n }\n }\n\n /**\n * Subscribe to a real-time data channel on the AppSync Events API.\n * Uses the Cognito JWT for authentication.\n *\n * Channel format: dataworks/{datasetDatasourceId}/{eventId}/{metric}\n * Use \"*\" for metric to receive all metrics for a dataset-datasource/event pair.\n * Examples: \"dataworks/1/1/heartrate\", \"dataworks/1/1/*\".\n *\n * @param channel - Channel path to subscribe to\n * @param onEvent - Callback invoked for each received event\n * @returns Subscription handle with a close() method\n * @throws Error if not authenticated\n * @see https://data-sdk-docs.dataworks.live/real-time-subscriptions\n */\n subscribe(channel: string, onEvent: SubscriptionHandler): Subscription {\n this.requireAuth();\n\n if (typeof WebSocket === \"undefined\") {\n throw new Error(\n \"[@dataworks-technology/data] WebSocket is not available. \" +\n \"Use Node >= 22, Bun, or a browser environment. \" +\n \"For older Node versions, install a WebSocket polyfill (e.g. ws) and assign it to globalThis.WebSocket.\",\n );\n }\n if (typeof crypto?.randomUUID !== \"function\") {\n throw new Error(\n \"[@dataworks-technology/data] crypto.randomUUID() is not available. \" +\n \"Use Node >= 19, Bun, or a browser with Crypto API support.\",\n );\n }\n\n const channelPath = channel.startsWith(\"/\") ? channel : `/${channel}`;\n\n return createAutoRefreshingSubscription({\n refreshAuth: async () => {\n await this.refreshSession();\n },\n connect: ({ onUnexpectedClose }) => {\n const url = new URL(this.config.realtimeUrl);\n // AppSync Events API uses the /event/realtime path for WebSocket connections\n url.pathname = \"/event/realtime\";\n url.protocol = url.protocol === \"https:\" ? \"wss:\" : \"ws:\";\n\n const host = new URL(this.config.realtimeUrl).host;\n // AWS AppSync Events API requires base64URL-encoded auth in the subprotocol.\n const authPayload = JSON.stringify({\n Authorization: this.credentials!.accessToken,\n host,\n });\n const authHeader = toBase64Url(authPayload);\n\n const ws = new WebSocket(url.toString(), [\n \"aws-appsync-event-ws\",\n `header-${authHeader}`,\n ]);\n\n let closedByUser = false;\n let unexpectedCloseHandled = false;\n\n const handleUnexpectedCloseOnce = () => {\n if (unexpectedCloseHandled) return;\n unexpectedCloseHandled = true;\n onUnexpectedClose();\n };\n\n ws.addEventListener(\"open\", () => {\n // Send connection_init — required for AppSync to send connection_ack\n ws.send(JSON.stringify({ type: \"connection_init\" }));\n });\n\n ws.addEventListener(\"message\", (event) => {\n try {\n const msg = JSON.parse(String(event.data));\n\n if (msg.type === \"connection_ack\") {\n ws.send(\n JSON.stringify({\n type: \"subscribe\",\n id: crypto.randomUUID(),\n channel: channelPath,\n authorization: {\n Authorization: this.credentials!.accessToken,\n host,\n },\n }),\n );\n } else if (msg.type === \"data\") {\n onEvent(JSON.parse(msg.event));\n } else if (\n msg.type === \"error\" &&\n JSON.stringify(msg).toLowerCase().includes(\"unauthor\")\n ) {\n handleUnexpectedCloseOnce();\n }\n } catch {\n // Ignore malformed or non-data messages (ka, subscribe_success, etc.)\n }\n });\n\n ws.addEventListener(\"close\", () => {\n if (!closedByUser) {\n handleUnexpectedCloseOnce();\n }\n });\n\n return {\n close() {\n closedByUser = true;\n ws.close();\n },\n };\n },\n });\n }\n\n /**\n * Check whether a metric is valid before ingesting.\n * Returns null if valid, or a human-readable reason string if invalid.\n */\n static validateMetric = validateMetric;\n\n /**\n * Filter an array of metrics, keeping only valid ones.\n * Logs a warning for each dropped item.\n */\n static filterValidMetrics = filterValidMetrics;\n\n /** Returns true if the client has been authenticated via login(). */\n get isAuthenticated(): boolean {\n return this.credentials !== null;\n }\n\n /** Returns the tenant from the last successful login, or null. */\n get tenant(): string | null {\n return this.credentials?.tenant ?? null;\n }\n\n /** @internal Throws if not authenticated. */\n private requireAuth(): void {\n if (!this.credentials) {\n throw new Error(\n \"[@dataworks-technology/data] Not authenticated — call login() first\",\n );\n }\n }\n\n private async fetchWithAutoRefresh(\n makeRequest: (accessToken: string) => Promise<Response>,\n ): Promise<Response> {\n this.requireAuth();\n\n let response = await makeRequest(this.credentials!.accessToken);\n if (response.status !== 401) {\n return response;\n }\n\n await this.refreshSession();\n response = await makeRequest(this.credentials!.accessToken);\n return response;\n }\n\n private async refreshSession(): Promise<void> {\n this.requireAuth();\n\n if (!this.credentials!.refreshToken) {\n throw new Error(\n \"[@dataworks-technology/data] Session expired and no refresh token is available — call login() again\",\n );\n }\n\n if (!this.refreshInFlight) {\n this.refreshInFlight = (async () => {\n this.credentials = await refreshWithToken(\n this.credentials!.refreshToken,\n {\n cognitoEndpoint: this.config.cognitoEndpoint,\n clientId: this.config.clientId,\n clientSecret: this.config.clientSecret,\n username: this.lastLoginUsername ?? undefined,\n previousTenant: this.credentials!.tenant,\n previousIdToken: this.credentials!.idToken,\n },\n );\n })().finally(() => {\n this.refreshInFlight = null;\n });\n }\n\n await this.refreshInFlight;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,4BAAAA;AAAA,EAAA,sBAAAC;AAAA;AAAA;;;ACMO,SAAS,iCAAiC,SAAS;AACtD,QAAM,uBAAuB,QAAQ,wBAAwB;AAC7D,MAAI,oBAAoB;AACxB,MAAI,oBAAoB;AACxB,MAAI,eAAe;AACnB,MAAI,mBAAmB;AACvB,QAAM,QAAQ;AAAA,IACV,mBAAmB,MAAM;AACrB,UAAI,gBAAgB,mBAAmB;AACnC;AAAA,MACJ;AACA,UAAI,qBAAqB,sBAAsB;AAC3C;AAAA,MACJ;AACA,0BAAoB;AACpB,2BAAqB;AACrB,YAAM,YAAY;AACd,YAAI;AACA,gBAAM,QAAQ,YAAY;AAC1B,cAAI,cAAc;AACd;AAAA,UACJ;AACA,6BAAmB,QAAQ,QAAQ,KAAK;AAAA,QAC5C,QACM;AACF,kBAAQ,mBAAmB,IAAI,MAAM,gEAAgE,CAAC;AAAA,QAC1G,UACA;AACI,8BAAoB;AAAA,QACxB;AAAA,MACJ,GAAG;AAAA,IACP;AAAA,EACJ;AACA,qBAAmB,QAAQ,QAAQ,KAAK;AACxC,SAAO;AAAA,IACH,QAAQ;AACJ,qBAAe;AACf,wBAAkB,MAAM;AACxB,yBAAmB;AAAA,IACvB;AAAA,EACJ;AACJ;;;ACpCO,SAAS,eAAe,GAAG;AAC9B,MAAI,OAAO,EAAE,WAAW,YAAY,EAAE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K,YAAa,cAAc;AACnC,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,QAAI,CAAC,KAAK,iBAAiB;AACzB,WAAK,mBAAmB,YAAY;AAClC,aAAK,cAAc,MAAM;AAAA,UACvB,KAAK,YAAa;AAAA,UAClB;AAAA,YACE,iBAAiB,KAAK,OAAO;AAAA,YAC7B,UAAU,KAAK,OAAO;AAAA,YACtB,cAAc,KAAK,OAAO;AAAA,YAC1B,UAAU,KAAK,qBAAqB;AAAA,YACpC,gBAAgB,KAAK,YAAa;AAAA,YAClC,iBAAiB,KAAK,YAAa;AAAA,UACrC;AAAA,QACF;AAAA,MACF,GAAG,EAAE,QAAQ,MAAM;AACjB,aAAK,kBAAkB;AAAA,MACzB,CAAC;AAAA,IACH;AAEA,UAAM,KAAK;AAAA,EACb;AACF;AAAA;AAAA;AAAA;AAAA;AA9Sa,WAuOJ,iBAAiBC;AAAA;AAAA;AAAA;AAAA;AAvOb,WA6OJ,qBAAqBD;","names":["filterValidMetrics","validateMetric","crypto","validateMetric","filterValidMetrics","filterValidMetrics","validateMetric"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Public types for @dataworks/data.
|
|
2
|
+
* Public types for @dataworks-technology/data.
|
|
3
3
|
*
|
|
4
4
|
* @module @dataworks-technology/data
|
|
5
5
|
*/
|
|
@@ -9,6 +9,8 @@ interface DataClientConfig {
|
|
|
9
9
|
cognitoEndpoint: string;
|
|
10
10
|
/** Cognito app client ID */
|
|
11
11
|
clientId: string;
|
|
12
|
+
/** Optional Cognito app client secret (if configured on the app client) */
|
|
13
|
+
clientSecret?: string;
|
|
12
14
|
/** Data Engine ingest URL (API Gateway endpoint) */
|
|
13
15
|
ingestUrl: string;
|
|
14
16
|
/** Data Engine error reporting URL (API Gateway endpoint) */
|
|
@@ -59,8 +61,8 @@ interface MetricsPayload {
|
|
|
59
61
|
interface ErrorReport {
|
|
60
62
|
/** Dataset-datasource identifier */
|
|
61
63
|
datasetDatasourceId: string;
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
+
/** Athlete identifier */
|
|
65
|
+
athleteId: string;
|
|
64
66
|
/** Client identifier */
|
|
65
67
|
clientId: number;
|
|
66
68
|
/** Short error title */
|
|
@@ -79,6 +81,8 @@ interface Subscription {
|
|
|
79
81
|
declare class DataClient {
|
|
80
82
|
private readonly config;
|
|
81
83
|
private credentials;
|
|
84
|
+
private lastLoginUsername;
|
|
85
|
+
private refreshInFlight;
|
|
82
86
|
constructor(config: DataClientConfig);
|
|
83
87
|
/**
|
|
84
88
|
* Authenticate with the Dataworks platform using Cognito USER_PASSWORD_AUTH.
|
|
@@ -87,6 +91,7 @@ declare class DataClient {
|
|
|
87
91
|
* @param username - Cognito username
|
|
88
92
|
* @param password - Cognito password
|
|
89
93
|
* @returns Login result containing tokens and tenant
|
|
94
|
+
* @see https://data-sdk-docs.dataworks.live/authentication
|
|
90
95
|
*/
|
|
91
96
|
login(username: string, password: string): Promise<LoginResult>;
|
|
92
97
|
/**
|
|
@@ -97,6 +102,7 @@ declare class DataClient {
|
|
|
97
102
|
* @param eventId - Event identifier
|
|
98
103
|
* @param datasetDatasourceId - Dataset-datasource identifier
|
|
99
104
|
* @throws Error if not authenticated or if the request fails
|
|
105
|
+
* @see https://data-sdk-docs.dataworks.live/ingesting-data
|
|
100
106
|
*/
|
|
101
107
|
ingest(metrics: Metric[], eventId: string, datasetDatasourceId: string): Promise<void>;
|
|
102
108
|
/**
|
|
@@ -104,18 +110,22 @@ declare class DataClient {
|
|
|
104
110
|
*
|
|
105
111
|
* @param error - Error report details
|
|
106
112
|
* @throws Error if not authenticated or if the request fails
|
|
113
|
+
* @see https://data-sdk-docs.dataworks.live/error-reporting
|
|
107
114
|
*/
|
|
108
115
|
reportError(error: ErrorReport): Promise<void>;
|
|
109
116
|
/**
|
|
110
117
|
* Subscribe to a real-time data channel on the AppSync Events API.
|
|
111
118
|
* Uses the Cognito JWT for authentication.
|
|
112
119
|
*
|
|
113
|
-
*
|
|
120
|
+
* Channel format: dataworks/{datasetDatasourceId}/{eventId}/{metric}
|
|
121
|
+
* Use "*" for metric to receive all metrics for a dataset-datasource/event pair.
|
|
122
|
+
* Examples: "dataworks/1/1/heartrate", "dataworks/1/1/*".
|
|
114
123
|
*
|
|
115
124
|
* @param channel - Channel path to subscribe to
|
|
116
125
|
* @param onEvent - Callback invoked for each received event
|
|
117
126
|
* @returns Subscription handle with a close() method
|
|
118
127
|
* @throws Error if not authenticated
|
|
128
|
+
* @see https://data-sdk-docs.dataworks.live/real-time-subscriptions
|
|
119
129
|
*/
|
|
120
130
|
subscribe(channel: string, onEvent: SubscriptionHandler): Subscription;
|
|
121
131
|
/**
|
|
@@ -134,6 +144,8 @@ declare class DataClient {
|
|
|
134
144
|
get tenant(): string | null;
|
|
135
145
|
/** @internal Throws if not authenticated. */
|
|
136
146
|
private requireAuth;
|
|
147
|
+
private fetchWithAutoRefresh;
|
|
148
|
+
private refreshSession;
|
|
137
149
|
}
|
|
138
150
|
|
|
139
151
|
/**
|