@letpeoplework/lighthouse-client 0.3.0 → 0.4.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.mjs → index.cjs} +207 -3
- package/dist/{index.d.mts → index.d.cts} +46 -1
- package/dist/index.d.ts +46 -1
- package/dist/index.js +174 -33
- package/package.json +2 -1
|
@@ -1,4 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
1
20
|
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createLighthouseAuthContext: () => createLighthouseAuthContext,
|
|
24
|
+
createLighthouseClient: () => createLighthouseClient,
|
|
25
|
+
getClientPackageContract: () => getClientPackageContract,
|
|
26
|
+
getDefaultMetricsDateRange: () => getDefaultMetricsDateRange,
|
|
27
|
+
parseStandaloneDiscoveryContract: () => parseStandaloneDiscoveryContract,
|
|
28
|
+
pollCliAuthSession: () => pollCliAuthSession,
|
|
29
|
+
queryServerAuthMode: () => queryServerAuthMode,
|
|
30
|
+
startCliAuthSession: () => startCliAuthSession,
|
|
31
|
+
validateLighthouseConnectivity: () => validateLighthouseConnectivity
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
2
34
|
var getClientPackageContract = () => ({
|
|
3
35
|
name: "@letpeoplework/lighthouse-client",
|
|
4
36
|
capabilities: [
|
|
@@ -12,6 +44,25 @@ var SUPPORTED_DISCOVERY_CONTRACT_VERSION = 1;
|
|
|
12
44
|
var isObjectRecord = (value) => {
|
|
13
45
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
46
|
};
|
|
47
|
+
var getErrorMessageWithCause = (error, fallback) => {
|
|
48
|
+
if (!(error instanceof Error)) {
|
|
49
|
+
return fallback;
|
|
50
|
+
}
|
|
51
|
+
const cause = error.cause;
|
|
52
|
+
if (cause instanceof Error && cause.message.trim().length > 0) {
|
|
53
|
+
if (cause.message === error.message) {
|
|
54
|
+
return error.message;
|
|
55
|
+
}
|
|
56
|
+
return `${error.message} (${cause.message})`;
|
|
57
|
+
}
|
|
58
|
+
if (typeof cause === "string" && cause.trim().length > 0) {
|
|
59
|
+
if (cause === error.message) {
|
|
60
|
+
return error.message;
|
|
61
|
+
}
|
|
62
|
+
return `${error.message} (${cause})`;
|
|
63
|
+
}
|
|
64
|
+
return error.message;
|
|
65
|
+
};
|
|
15
66
|
var getNormalizedLighthouseUrl = (value) => {
|
|
16
67
|
const trimmed = value.trim();
|
|
17
68
|
if (trimmed.length === 0) {
|
|
@@ -195,7 +246,10 @@ var validateLighthouseConnectivity = async (configuration, dependencies, request
|
|
|
195
246
|
endpoint
|
|
196
247
|
};
|
|
197
248
|
} catch (error) {
|
|
198
|
-
const message =
|
|
249
|
+
const message = getErrorMessageWithCause(
|
|
250
|
+
error,
|
|
251
|
+
"Connectivity request failed."
|
|
252
|
+
);
|
|
199
253
|
return {
|
|
200
254
|
category: "unreachable",
|
|
201
255
|
reason: message,
|
|
@@ -284,6 +338,152 @@ var createLighthouseAuthContext = (store) => ({
|
|
|
284
338
|
return getStatusFromAuth({ kind: "none" }, "none");
|
|
285
339
|
}
|
|
286
340
|
});
|
|
341
|
+
var mapAuthMode = (serverMode) => {
|
|
342
|
+
switch (serverMode.toLowerCase()) {
|
|
343
|
+
case "enabled":
|
|
344
|
+
return "required";
|
|
345
|
+
case "disabled":
|
|
346
|
+
return "disabled";
|
|
347
|
+
case "blocked":
|
|
348
|
+
return "blocked";
|
|
349
|
+
default:
|
|
350
|
+
return "misconfigured";
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
var getAuthStartFailureCategory = (statusCode) => {
|
|
354
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
355
|
+
return "unauthorized";
|
|
356
|
+
}
|
|
357
|
+
if (statusCode === 404) {
|
|
358
|
+
return "misconfigured";
|
|
359
|
+
}
|
|
360
|
+
if (statusCode >= 500 && statusCode <= 599) {
|
|
361
|
+
return "dependency-failure";
|
|
362
|
+
}
|
|
363
|
+
return "unexpected";
|
|
364
|
+
};
|
|
365
|
+
var getErrorReasonFromResponse = async (response, fallback) => {
|
|
366
|
+
try {
|
|
367
|
+
const body = (await response.text()).trim();
|
|
368
|
+
if (body.length > 0) {
|
|
369
|
+
return body;
|
|
370
|
+
}
|
|
371
|
+
} catch {
|
|
372
|
+
}
|
|
373
|
+
return fallback;
|
|
374
|
+
};
|
|
375
|
+
var queryServerAuthMode = async (endpointUrl, dependencies) => {
|
|
376
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
377
|
+
if (normalizedEndpointUrl === null) {
|
|
378
|
+
return {
|
|
379
|
+
mode: "misconfigured",
|
|
380
|
+
misconfigurationMessage: "Server URL is invalid."
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
384
|
+
try {
|
|
385
|
+
const response = await dependencies.fetch(`${apiBase}/v1/auth/mode`);
|
|
386
|
+
if (!response.ok) {
|
|
387
|
+
return {
|
|
388
|
+
mode: "misconfigured",
|
|
389
|
+
misconfigurationMessage: await getErrorReasonFromResponse(
|
|
390
|
+
response,
|
|
391
|
+
`HTTP ${response.status}`
|
|
392
|
+
)
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
const json = await response.json();
|
|
396
|
+
return {
|
|
397
|
+
mode: mapAuthMode(json.mode),
|
|
398
|
+
misconfigurationMessage: json.misconfigurationMessage
|
|
399
|
+
};
|
|
400
|
+
} catch (error) {
|
|
401
|
+
return {
|
|
402
|
+
mode: "misconfigured",
|
|
403
|
+
misconfigurationMessage: getErrorMessageWithCause(
|
|
404
|
+
error,
|
|
405
|
+
"Could not reach auth mode endpoint."
|
|
406
|
+
)
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
var startCliAuthSession = async (endpointUrl, dependencies) => {
|
|
411
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
412
|
+
if (normalizedEndpointUrl === null) {
|
|
413
|
+
return {
|
|
414
|
+
status: "error",
|
|
415
|
+
category: "misconfigured",
|
|
416
|
+
reason: "Server URL is invalid."
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
420
|
+
try {
|
|
421
|
+
const response = await dependencies.fetch(
|
|
422
|
+
`${apiBase}/v1/auth/cli/session`,
|
|
423
|
+
{
|
|
424
|
+
method: "POST"
|
|
425
|
+
}
|
|
426
|
+
);
|
|
427
|
+
if (!response.ok) {
|
|
428
|
+
return {
|
|
429
|
+
status: "error",
|
|
430
|
+
category: getAuthStartFailureCategory(response.status),
|
|
431
|
+
reason: await getErrorReasonFromResponse(
|
|
432
|
+
response,
|
|
433
|
+
`HTTP ${response.status}`
|
|
434
|
+
),
|
|
435
|
+
statusCode: response.status
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const json = await response.json();
|
|
439
|
+
return {
|
|
440
|
+
status: "started",
|
|
441
|
+
sessionId: json.sessionId,
|
|
442
|
+
verificationUrl: json.verificationUrl,
|
|
443
|
+
expiresAt: json.expiresAt
|
|
444
|
+
};
|
|
445
|
+
} catch (error) {
|
|
446
|
+
return {
|
|
447
|
+
status: "error",
|
|
448
|
+
category: "unreachable",
|
|
449
|
+
reason: getErrorMessageWithCause(
|
|
450
|
+
error,
|
|
451
|
+
"Could not reach auth session endpoint."
|
|
452
|
+
)
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
var pollCliAuthSession = async (endpointUrl, sessionId, dependencies) => {
|
|
457
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
458
|
+
if (normalizedEndpointUrl === null) {
|
|
459
|
+
return { status: "expired" };
|
|
460
|
+
}
|
|
461
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
462
|
+
try {
|
|
463
|
+
const response = await dependencies.fetch(
|
|
464
|
+
`${apiBase}/v1/auth/cli/poll/${encodeURIComponent(sessionId)}`
|
|
465
|
+
);
|
|
466
|
+
if (response.status === 404) {
|
|
467
|
+
return { status: "not-found" };
|
|
468
|
+
}
|
|
469
|
+
if (!response.ok) {
|
|
470
|
+
return { status: "expired" };
|
|
471
|
+
}
|
|
472
|
+
const json = await response.json();
|
|
473
|
+
if (json.status === "approved" && json.token && json.userName) {
|
|
474
|
+
return { status: "approved", token: json.token, userName: json.userName };
|
|
475
|
+
}
|
|
476
|
+
if (json.status === "expired") {
|
|
477
|
+
return { status: "expired" };
|
|
478
|
+
}
|
|
479
|
+
if (json.status === "denied") {
|
|
480
|
+
return { status: "denied" };
|
|
481
|
+
}
|
|
482
|
+
return { status: "pending" };
|
|
483
|
+
} catch {
|
|
484
|
+
return { status: "expired" };
|
|
485
|
+
}
|
|
486
|
+
};
|
|
287
487
|
var getDefaultMetricsDateRange = () => {
|
|
288
488
|
const end = /* @__PURE__ */ new Date();
|
|
289
489
|
const start = /* @__PURE__ */ new Date();
|
|
@@ -666,11 +866,15 @@ var createLighthouseClient = (configuration, dependencies = {}) => ({
|
|
|
666
866
|
{ method: "POST", body: payload }
|
|
667
867
|
)
|
|
668
868
|
});
|
|
669
|
-
export
|
|
869
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
870
|
+
0 && (module.exports = {
|
|
670
871
|
createLighthouseAuthContext,
|
|
671
872
|
createLighthouseClient,
|
|
672
873
|
getClientPackageContract,
|
|
673
874
|
getDefaultMetricsDateRange,
|
|
674
875
|
parseStandaloneDiscoveryContract,
|
|
876
|
+
pollCliAuthSession,
|
|
877
|
+
queryServerAuthMode,
|
|
878
|
+
startCliAuthSession,
|
|
675
879
|
validateLighthouseConnectivity
|
|
676
|
-
};
|
|
880
|
+
});
|
|
@@ -86,6 +86,51 @@ type LighthouseAuthContext = {
|
|
|
86
86
|
readonly getStatus: (overrides?: LighthouseAuthOverrides) => Promise<LighthouseAuthStatus>;
|
|
87
87
|
};
|
|
88
88
|
declare const createLighthouseAuthContext: (store: LighthouseAuthStore) => LighthouseAuthContext;
|
|
89
|
+
type CliServerAuthMode = "disabled" | "required" | "blocked" | "misconfigured";
|
|
90
|
+
type ServerAuthModeResult = {
|
|
91
|
+
readonly mode: CliServerAuthMode;
|
|
92
|
+
readonly misconfigurationMessage?: string;
|
|
93
|
+
};
|
|
94
|
+
type CliAuthSessionStartResult = {
|
|
95
|
+
readonly status: "started";
|
|
96
|
+
readonly sessionId: string;
|
|
97
|
+
readonly verificationUrl: string;
|
|
98
|
+
readonly expiresAt: string;
|
|
99
|
+
};
|
|
100
|
+
type CliAuthSessionStartError = {
|
|
101
|
+
readonly status: "error";
|
|
102
|
+
readonly category: Exclude<ConnectivityCategory, "success">;
|
|
103
|
+
readonly reason: string;
|
|
104
|
+
readonly statusCode?: number;
|
|
105
|
+
};
|
|
106
|
+
type CliAuthSessionStartOutcome = CliAuthSessionStartResult | CliAuthSessionStartError;
|
|
107
|
+
type CliAuthSessionPollResult = {
|
|
108
|
+
readonly status: "pending";
|
|
109
|
+
} | {
|
|
110
|
+
readonly status: "approved";
|
|
111
|
+
readonly token: string;
|
|
112
|
+
readonly userName: string;
|
|
113
|
+
} | {
|
|
114
|
+
readonly status: "expired";
|
|
115
|
+
} | {
|
|
116
|
+
readonly status: "denied";
|
|
117
|
+
} | {
|
|
118
|
+
readonly status: "not-found";
|
|
119
|
+
};
|
|
120
|
+
type CliServerConnection = {
|
|
121
|
+
readonly mode: "server";
|
|
122
|
+
readonly endpointUrl: string;
|
|
123
|
+
readonly authMode: "disabled" | "required";
|
|
124
|
+
readonly insecure?: boolean;
|
|
125
|
+
readonly auth?: StoredLighthouseAuth;
|
|
126
|
+
};
|
|
127
|
+
type CliConnection = CliServerConnection;
|
|
128
|
+
type CliAuthDependencies = {
|
|
129
|
+
readonly fetch: typeof globalThis.fetch;
|
|
130
|
+
};
|
|
131
|
+
declare const queryServerAuthMode: (endpointUrl: string, dependencies: CliAuthDependencies) => Promise<ServerAuthModeResult>;
|
|
132
|
+
declare const startCliAuthSession: (endpointUrl: string, dependencies: CliAuthDependencies) => Promise<CliAuthSessionStartOutcome>;
|
|
133
|
+
declare const pollCliAuthSession: (endpointUrl: string, sessionId: string, dependencies: CliAuthDependencies) => Promise<CliAuthSessionPollResult>;
|
|
89
134
|
type LighthouseClientConfiguration = {
|
|
90
135
|
readonly connection: LighthouseConnectionConfiguration;
|
|
91
136
|
readonly auth?: LighthouseClientAuth;
|
|
@@ -156,4 +201,4 @@ type LighthouseClientDependencies = {
|
|
|
156
201
|
};
|
|
157
202
|
declare const createLighthouseClient: (configuration: LighthouseClientConfiguration, dependencies?: LighthouseClientDependencies) => LighthouseClient;
|
|
158
203
|
|
|
159
|
-
export { type BacktestInput, type ClientCapability, type ClientPackageContract, type ConnectivityCategory, type ConnectivityDependencies, type ConnectivityFetchResponse, type ConnectivityValidationResult, type LighthouseApiError, type LighthouseApiResult, type LighthouseAuthContext, type LighthouseAuthOverrides, type LighthouseAuthStatus, type LighthouseAuthStore, type LighthouseClient, type LighthouseClientAuth, type LighthouseClientConfiguration, type LighthouseClientDependencies, type LighthouseConnectionConfiguration, type LighthouseWritePayload, type ManualForecastInput, type MetricsDateRange, type ResolvedLighthouseEndpoint, type StandaloneDiscoveryContract, type StandaloneDiscoveryContractParseResult, type StoredLighthouseAuth, createLighthouseAuthContext, createLighthouseClient, getClientPackageContract, getDefaultMetricsDateRange, parseStandaloneDiscoveryContract, validateLighthouseConnectivity };
|
|
204
|
+
export { type BacktestInput, type CliAuthSessionPollResult, type CliAuthSessionStartError, type CliAuthSessionStartOutcome, type CliAuthSessionStartResult, type CliConnection, type CliServerAuthMode, type CliServerConnection, type ClientCapability, type ClientPackageContract, type ConnectivityCategory, type ConnectivityDependencies, type ConnectivityFetchResponse, type ConnectivityValidationResult, type LighthouseApiError, type LighthouseApiResult, type LighthouseAuthContext, type LighthouseAuthOverrides, type LighthouseAuthStatus, type LighthouseAuthStore, type LighthouseClient, type LighthouseClientAuth, type LighthouseClientConfiguration, type LighthouseClientDependencies, type LighthouseConnectionConfiguration, type LighthouseWritePayload, type ManualForecastInput, type MetricsDateRange, type ResolvedLighthouseEndpoint, type ServerAuthModeResult, type StandaloneDiscoveryContract, type StandaloneDiscoveryContractParseResult, type StoredLighthouseAuth, createLighthouseAuthContext, createLighthouseClient, getClientPackageContract, getDefaultMetricsDateRange, parseStandaloneDiscoveryContract, pollCliAuthSession, queryServerAuthMode, startCliAuthSession, validateLighthouseConnectivity };
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,51 @@ type LighthouseAuthContext = {
|
|
|
86
86
|
readonly getStatus: (overrides?: LighthouseAuthOverrides) => Promise<LighthouseAuthStatus>;
|
|
87
87
|
};
|
|
88
88
|
declare const createLighthouseAuthContext: (store: LighthouseAuthStore) => LighthouseAuthContext;
|
|
89
|
+
type CliServerAuthMode = "disabled" | "required" | "blocked" | "misconfigured";
|
|
90
|
+
type ServerAuthModeResult = {
|
|
91
|
+
readonly mode: CliServerAuthMode;
|
|
92
|
+
readonly misconfigurationMessage?: string;
|
|
93
|
+
};
|
|
94
|
+
type CliAuthSessionStartResult = {
|
|
95
|
+
readonly status: "started";
|
|
96
|
+
readonly sessionId: string;
|
|
97
|
+
readonly verificationUrl: string;
|
|
98
|
+
readonly expiresAt: string;
|
|
99
|
+
};
|
|
100
|
+
type CliAuthSessionStartError = {
|
|
101
|
+
readonly status: "error";
|
|
102
|
+
readonly category: Exclude<ConnectivityCategory, "success">;
|
|
103
|
+
readonly reason: string;
|
|
104
|
+
readonly statusCode?: number;
|
|
105
|
+
};
|
|
106
|
+
type CliAuthSessionStartOutcome = CliAuthSessionStartResult | CliAuthSessionStartError;
|
|
107
|
+
type CliAuthSessionPollResult = {
|
|
108
|
+
readonly status: "pending";
|
|
109
|
+
} | {
|
|
110
|
+
readonly status: "approved";
|
|
111
|
+
readonly token: string;
|
|
112
|
+
readonly userName: string;
|
|
113
|
+
} | {
|
|
114
|
+
readonly status: "expired";
|
|
115
|
+
} | {
|
|
116
|
+
readonly status: "denied";
|
|
117
|
+
} | {
|
|
118
|
+
readonly status: "not-found";
|
|
119
|
+
};
|
|
120
|
+
type CliServerConnection = {
|
|
121
|
+
readonly mode: "server";
|
|
122
|
+
readonly endpointUrl: string;
|
|
123
|
+
readonly authMode: "disabled" | "required";
|
|
124
|
+
readonly insecure?: boolean;
|
|
125
|
+
readonly auth?: StoredLighthouseAuth;
|
|
126
|
+
};
|
|
127
|
+
type CliConnection = CliServerConnection;
|
|
128
|
+
type CliAuthDependencies = {
|
|
129
|
+
readonly fetch: typeof globalThis.fetch;
|
|
130
|
+
};
|
|
131
|
+
declare const queryServerAuthMode: (endpointUrl: string, dependencies: CliAuthDependencies) => Promise<ServerAuthModeResult>;
|
|
132
|
+
declare const startCliAuthSession: (endpointUrl: string, dependencies: CliAuthDependencies) => Promise<CliAuthSessionStartOutcome>;
|
|
133
|
+
declare const pollCliAuthSession: (endpointUrl: string, sessionId: string, dependencies: CliAuthDependencies) => Promise<CliAuthSessionPollResult>;
|
|
89
134
|
type LighthouseClientConfiguration = {
|
|
90
135
|
readonly connection: LighthouseConnectionConfiguration;
|
|
91
136
|
readonly auth?: LighthouseClientAuth;
|
|
@@ -156,4 +201,4 @@ type LighthouseClientDependencies = {
|
|
|
156
201
|
};
|
|
157
202
|
declare const createLighthouseClient: (configuration: LighthouseClientConfiguration, dependencies?: LighthouseClientDependencies) => LighthouseClient;
|
|
158
203
|
|
|
159
|
-
export { type BacktestInput, type ClientCapability, type ClientPackageContract, type ConnectivityCategory, type ConnectivityDependencies, type ConnectivityFetchResponse, type ConnectivityValidationResult, type LighthouseApiError, type LighthouseApiResult, type LighthouseAuthContext, type LighthouseAuthOverrides, type LighthouseAuthStatus, type LighthouseAuthStore, type LighthouseClient, type LighthouseClientAuth, type LighthouseClientConfiguration, type LighthouseClientDependencies, type LighthouseConnectionConfiguration, type LighthouseWritePayload, type ManualForecastInput, type MetricsDateRange, type ResolvedLighthouseEndpoint, type StandaloneDiscoveryContract, type StandaloneDiscoveryContractParseResult, type StoredLighthouseAuth, createLighthouseAuthContext, createLighthouseClient, getClientPackageContract, getDefaultMetricsDateRange, parseStandaloneDiscoveryContract, validateLighthouseConnectivity };
|
|
204
|
+
export { type BacktestInput, type CliAuthSessionPollResult, type CliAuthSessionStartError, type CliAuthSessionStartOutcome, type CliAuthSessionStartResult, type CliConnection, type CliServerAuthMode, type CliServerConnection, type ClientCapability, type ClientPackageContract, type ConnectivityCategory, type ConnectivityDependencies, type ConnectivityFetchResponse, type ConnectivityValidationResult, type LighthouseApiError, type LighthouseApiResult, type LighthouseAuthContext, type LighthouseAuthOverrides, type LighthouseAuthStatus, type LighthouseAuthStore, type LighthouseClient, type LighthouseClientAuth, type LighthouseClientConfiguration, type LighthouseClientDependencies, type LighthouseConnectionConfiguration, type LighthouseWritePayload, type ManualForecastInput, type MetricsDateRange, type ResolvedLighthouseEndpoint, type ServerAuthModeResult, type StandaloneDiscoveryContract, type StandaloneDiscoveryContractParseResult, type StoredLighthouseAuth, createLighthouseAuthContext, createLighthouseClient, getClientPackageContract, getDefaultMetricsDateRange, parseStandaloneDiscoveryContract, pollCliAuthSession, queryServerAuthMode, startCliAuthSession, validateLighthouseConnectivity };
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
1
|
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
createLighthouseAuthContext: () => createLighthouseAuthContext,
|
|
24
|
-
createLighthouseClient: () => createLighthouseClient,
|
|
25
|
-
getClientPackageContract: () => getClientPackageContract,
|
|
26
|
-
getDefaultMetricsDateRange: () => getDefaultMetricsDateRange,
|
|
27
|
-
parseStandaloneDiscoveryContract: () => parseStandaloneDiscoveryContract,
|
|
28
|
-
validateLighthouseConnectivity: () => validateLighthouseConnectivity
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(index_exports);
|
|
31
2
|
var getClientPackageContract = () => ({
|
|
32
3
|
name: "@letpeoplework/lighthouse-client",
|
|
33
4
|
capabilities: [
|
|
@@ -41,6 +12,25 @@ var SUPPORTED_DISCOVERY_CONTRACT_VERSION = 1;
|
|
|
41
12
|
var isObjectRecord = (value) => {
|
|
42
13
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
43
14
|
};
|
|
15
|
+
var getErrorMessageWithCause = (error, fallback) => {
|
|
16
|
+
if (!(error instanceof Error)) {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
const cause = error.cause;
|
|
20
|
+
if (cause instanceof Error && cause.message.trim().length > 0) {
|
|
21
|
+
if (cause.message === error.message) {
|
|
22
|
+
return error.message;
|
|
23
|
+
}
|
|
24
|
+
return `${error.message} (${cause.message})`;
|
|
25
|
+
}
|
|
26
|
+
if (typeof cause === "string" && cause.trim().length > 0) {
|
|
27
|
+
if (cause === error.message) {
|
|
28
|
+
return error.message;
|
|
29
|
+
}
|
|
30
|
+
return `${error.message} (${cause})`;
|
|
31
|
+
}
|
|
32
|
+
return error.message;
|
|
33
|
+
};
|
|
44
34
|
var getNormalizedLighthouseUrl = (value) => {
|
|
45
35
|
const trimmed = value.trim();
|
|
46
36
|
if (trimmed.length === 0) {
|
|
@@ -224,7 +214,10 @@ var validateLighthouseConnectivity = async (configuration, dependencies, request
|
|
|
224
214
|
endpoint
|
|
225
215
|
};
|
|
226
216
|
} catch (error) {
|
|
227
|
-
const message =
|
|
217
|
+
const message = getErrorMessageWithCause(
|
|
218
|
+
error,
|
|
219
|
+
"Connectivity request failed."
|
|
220
|
+
);
|
|
228
221
|
return {
|
|
229
222
|
category: "unreachable",
|
|
230
223
|
reason: message,
|
|
@@ -313,6 +306,152 @@ var createLighthouseAuthContext = (store) => ({
|
|
|
313
306
|
return getStatusFromAuth({ kind: "none" }, "none");
|
|
314
307
|
}
|
|
315
308
|
});
|
|
309
|
+
var mapAuthMode = (serverMode) => {
|
|
310
|
+
switch (serverMode.toLowerCase()) {
|
|
311
|
+
case "enabled":
|
|
312
|
+
return "required";
|
|
313
|
+
case "disabled":
|
|
314
|
+
return "disabled";
|
|
315
|
+
case "blocked":
|
|
316
|
+
return "blocked";
|
|
317
|
+
default:
|
|
318
|
+
return "misconfigured";
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var getAuthStartFailureCategory = (statusCode) => {
|
|
322
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
323
|
+
return "unauthorized";
|
|
324
|
+
}
|
|
325
|
+
if (statusCode === 404) {
|
|
326
|
+
return "misconfigured";
|
|
327
|
+
}
|
|
328
|
+
if (statusCode >= 500 && statusCode <= 599) {
|
|
329
|
+
return "dependency-failure";
|
|
330
|
+
}
|
|
331
|
+
return "unexpected";
|
|
332
|
+
};
|
|
333
|
+
var getErrorReasonFromResponse = async (response, fallback) => {
|
|
334
|
+
try {
|
|
335
|
+
const body = (await response.text()).trim();
|
|
336
|
+
if (body.length > 0) {
|
|
337
|
+
return body;
|
|
338
|
+
}
|
|
339
|
+
} catch {
|
|
340
|
+
}
|
|
341
|
+
return fallback;
|
|
342
|
+
};
|
|
343
|
+
var queryServerAuthMode = async (endpointUrl, dependencies) => {
|
|
344
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
345
|
+
if (normalizedEndpointUrl === null) {
|
|
346
|
+
return {
|
|
347
|
+
mode: "misconfigured",
|
|
348
|
+
misconfigurationMessage: "Server URL is invalid."
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
352
|
+
try {
|
|
353
|
+
const response = await dependencies.fetch(`${apiBase}/v1/auth/mode`);
|
|
354
|
+
if (!response.ok) {
|
|
355
|
+
return {
|
|
356
|
+
mode: "misconfigured",
|
|
357
|
+
misconfigurationMessage: await getErrorReasonFromResponse(
|
|
358
|
+
response,
|
|
359
|
+
`HTTP ${response.status}`
|
|
360
|
+
)
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
const json = await response.json();
|
|
364
|
+
return {
|
|
365
|
+
mode: mapAuthMode(json.mode),
|
|
366
|
+
misconfigurationMessage: json.misconfigurationMessage
|
|
367
|
+
};
|
|
368
|
+
} catch (error) {
|
|
369
|
+
return {
|
|
370
|
+
mode: "misconfigured",
|
|
371
|
+
misconfigurationMessage: getErrorMessageWithCause(
|
|
372
|
+
error,
|
|
373
|
+
"Could not reach auth mode endpoint."
|
|
374
|
+
)
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
var startCliAuthSession = async (endpointUrl, dependencies) => {
|
|
379
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
380
|
+
if (normalizedEndpointUrl === null) {
|
|
381
|
+
return {
|
|
382
|
+
status: "error",
|
|
383
|
+
category: "misconfigured",
|
|
384
|
+
reason: "Server URL is invalid."
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
388
|
+
try {
|
|
389
|
+
const response = await dependencies.fetch(
|
|
390
|
+
`${apiBase}/v1/auth/cli/session`,
|
|
391
|
+
{
|
|
392
|
+
method: "POST"
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
if (!response.ok) {
|
|
396
|
+
return {
|
|
397
|
+
status: "error",
|
|
398
|
+
category: getAuthStartFailureCategory(response.status),
|
|
399
|
+
reason: await getErrorReasonFromResponse(
|
|
400
|
+
response,
|
|
401
|
+
`HTTP ${response.status}`
|
|
402
|
+
),
|
|
403
|
+
statusCode: response.status
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
const json = await response.json();
|
|
407
|
+
return {
|
|
408
|
+
status: "started",
|
|
409
|
+
sessionId: json.sessionId,
|
|
410
|
+
verificationUrl: json.verificationUrl,
|
|
411
|
+
expiresAt: json.expiresAt
|
|
412
|
+
};
|
|
413
|
+
} catch (error) {
|
|
414
|
+
return {
|
|
415
|
+
status: "error",
|
|
416
|
+
category: "unreachable",
|
|
417
|
+
reason: getErrorMessageWithCause(
|
|
418
|
+
error,
|
|
419
|
+
"Could not reach auth session endpoint."
|
|
420
|
+
)
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
var pollCliAuthSession = async (endpointUrl, sessionId, dependencies) => {
|
|
425
|
+
const normalizedEndpointUrl = getNormalizedLighthouseUrl(endpointUrl);
|
|
426
|
+
if (normalizedEndpointUrl === null) {
|
|
427
|
+
return { status: "expired" };
|
|
428
|
+
}
|
|
429
|
+
const apiBase = getApiBaseUrl(normalizedEndpointUrl);
|
|
430
|
+
try {
|
|
431
|
+
const response = await dependencies.fetch(
|
|
432
|
+
`${apiBase}/v1/auth/cli/poll/${encodeURIComponent(sessionId)}`
|
|
433
|
+
);
|
|
434
|
+
if (response.status === 404) {
|
|
435
|
+
return { status: "not-found" };
|
|
436
|
+
}
|
|
437
|
+
if (!response.ok) {
|
|
438
|
+
return { status: "expired" };
|
|
439
|
+
}
|
|
440
|
+
const json = await response.json();
|
|
441
|
+
if (json.status === "approved" && json.token && json.userName) {
|
|
442
|
+
return { status: "approved", token: json.token, userName: json.userName };
|
|
443
|
+
}
|
|
444
|
+
if (json.status === "expired") {
|
|
445
|
+
return { status: "expired" };
|
|
446
|
+
}
|
|
447
|
+
if (json.status === "denied") {
|
|
448
|
+
return { status: "denied" };
|
|
449
|
+
}
|
|
450
|
+
return { status: "pending" };
|
|
451
|
+
} catch {
|
|
452
|
+
return { status: "expired" };
|
|
453
|
+
}
|
|
454
|
+
};
|
|
316
455
|
var getDefaultMetricsDateRange = () => {
|
|
317
456
|
const end = /* @__PURE__ */ new Date();
|
|
318
457
|
const start = /* @__PURE__ */ new Date();
|
|
@@ -695,12 +834,14 @@ var createLighthouseClient = (configuration, dependencies = {}) => ({
|
|
|
695
834
|
{ method: "POST", body: payload }
|
|
696
835
|
)
|
|
697
836
|
});
|
|
698
|
-
|
|
699
|
-
0 && (module.exports = {
|
|
837
|
+
export {
|
|
700
838
|
createLighthouseAuthContext,
|
|
701
839
|
createLighthouseClient,
|
|
702
840
|
getClientPackageContract,
|
|
703
841
|
getDefaultMetricsDateRange,
|
|
704
842
|
parseStandaloneDiscoveryContract,
|
|
843
|
+
pollCliAuthSession,
|
|
844
|
+
queryServerAuthMode,
|
|
845
|
+
startCliAuthSession,
|
|
705
846
|
validateLighthouseConnectivity
|
|
706
|
-
}
|
|
847
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@letpeoplework/lighthouse-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Shared Lighthouse API client contracts",
|
|
5
5
|
"repository": "https://github.com/LetPeopleWork/lighthouse-clients",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"main": "dist/index.cjs",
|
|
7
8
|
"module": "dist/index.js",
|
|
8
9
|
"types": "dist/index.d.ts",
|