@bluestep-systems/b6p-core 0.8.1 → 1.0.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/README.md +310 -32
- package/dist/B6PCore.d.ts +7 -0
- package/dist/B6PCore.js +96 -1
- package/dist/B6PCore.js.map +1 -1
- package/dist/B6PUri.d.ts +6 -8
- package/dist/B6PUri.js +6 -8
- package/dist/B6PUri.js.map +1 -1
- package/dist/Err.d.ts +421 -0
- package/dist/Err.js +558 -0
- package/dist/Err.js.map +1 -1
- package/dist/auth/BearerAuthProvider.d.ts +32 -0
- package/dist/auth/BearerAuthProvider.js +49 -0
- package/dist/auth/BearerAuthProvider.js.map +1 -1
- package/dist/cache/OrgCache.js.map +1 -1
- package/dist/cache/ScriptMetaDataStore.js.map +1 -1
- package/dist/constants/ApiEndpoints.d.ts +1 -0
- package/dist/constants/ApiEndpoints.js +1 -0
- package/dist/constants/ApiEndpoints.js.map +1 -1
- package/dist/constants/AuthTypes.js.map +1 -1
- package/dist/constants/BlueHQ.js.map +1 -1
- package/dist/constants/CryptoAlgorithms.js.map +1 -1
- package/dist/constants/ExtensionConfig.js.map +1 -1
- package/dist/constants/FileExtensions.js.map +1 -1
- package/dist/constants/FolderNames.js.map +1 -1
- package/dist/constants/Gateway.d.ts +71 -0
- package/dist/constants/Gateway.js +79 -0
- package/dist/constants/Gateway.js.map +1 -0
- package/dist/constants/GitHubUrls.js.map +1 -1
- package/dist/constants/Http.js.map +1 -1
- package/dist/constants/MimeTypes.js.map +1 -1
- package/dist/constants/Numerical.js.map +1 -1
- package/dist/constants/OutputChannels.js.map +1 -1
- package/dist/constants/SettingsKeys.d.ts +5 -0
- package/dist/constants/SettingsKeys.js +5 -0
- package/dist/constants/SettingsKeys.js.map +1 -1
- package/dist/constants/SpecialFiles.js.map +1 -1
- package/dist/constants/WebDAVElements.js.map +1 -1
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +3 -1
- package/dist/constants/index.js.map +1 -1
- package/dist/data/DownstairsPathParser.js.map +1 -1
- package/dist/data/GlobMatcher.js.map +1 -1
- package/dist/data/IdUtility.js.map +1 -1
- package/dist/data/OrgWorker.js.map +1 -1
- package/dist/data/ScriptKey.d.ts +3 -27
- package/dist/data/ScriptKey.js +12 -24
- package/dist/data/ScriptKey.js.map +1 -1
- package/dist/data/ScriptUrlParser.js +3 -1
- package/dist/data/ScriptUrlParser.js.map +1 -1
- package/dist/data/ServerVersion.d.ts +76 -0
- package/dist/data/ServerVersion.js +111 -0
- package/dist/data/ServerVersion.js.map +1 -0
- package/dist/data/index.d.ts +2 -0
- package/dist/data/index.js +7 -1
- package/dist/data/index.js.map +1 -1
- package/dist/gateway/GatewayClient.d.ts +147 -0
- package/dist/gateway/GatewayClient.js +395 -0
- package/dist/gateway/GatewayClient.js.map +1 -0
- package/dist/gateway/GatewayContext.d.ts +43 -0
- package/dist/gateway/GatewayContext.js +3 -0
- package/dist/gateway/GatewayContext.js.map +1 -0
- package/dist/gateway/GatewayGit.d.ts +291 -0
- package/dist/gateway/GatewayGit.js +731 -0
- package/dist/gateway/GatewayGit.js.map +1 -0
- package/dist/gateway/GatewayKb.d.ts +173 -0
- package/dist/gateway/GatewayKb.js +354 -0
- package/dist/gateway/GatewayKb.js.map +1 -0
- package/dist/gateway/GatewayMarketplace.d.ts +116 -0
- package/dist/gateway/GatewayMarketplace.js +278 -0
- package/dist/gateway/GatewayMarketplace.js.map +1 -0
- package/dist/gateway/GatewayService.d.ts +47 -0
- package/dist/gateway/GatewayService.js +54 -0
- package/dist/gateway/GatewayService.js.map +1 -0
- package/dist/gateway/GatewayToken.d.ts +18 -0
- package/dist/gateway/GatewayToken.js +43 -0
- package/dist/gateway/GatewayToken.js.map +1 -0
- package/dist/gateway/GatewayTokenService.d.ts +101 -0
- package/dist/gateway/GatewayTokenService.js +465 -0
- package/dist/gateway/GatewayTokenService.js.map +1 -0
- package/dist/index.d.ts +29 -8
- package/dist/index.js +81 -9
- package/dist/index.js.map +1 -1
- package/dist/network/HttpClient.js.map +1 -1
- package/dist/network/HttpClientProvider.js.map +1 -1
- package/dist/network/StatusCodes.js.map +1 -1
- package/dist/persistence/PersistablePseudoMap.js.map +1 -1
- package/dist/persistence/PersistenceKeys.js.map +1 -1
- package/dist/persistence/PrivatePersistanceMap.js.map +1 -1
- package/dist/persistence/PseudoMap.js.map +1 -1
- package/dist/persistence/PublicPersistanceMap.js.map +1 -1
- package/dist/persistence/Serializable.js.map +1 -1
- package/dist/persistence/SharedFilePersistence.d.ts +22 -0
- package/dist/persistence/SharedFilePersistence.js +65 -14
- package/dist/persistence/SharedFilePersistence.js.map +1 -1
- package/dist/persistence/TypedMap.js.map +1 -1
- package/dist/persistence/TypedPersistable.js.map +1 -1
- package/dist/persistence/TypedPrivatePersistable.js.map +1 -1
- package/dist/persistence/dirLockdown.js.map +1 -1
- package/dist/providers.d.ts +46 -16
- package/dist/script/GitIdentity.d.ts +48 -0
- package/dist/script/GitIdentity.js +52 -0
- package/dist/script/GitIdentity.js.map +1 -0
- package/dist/script/GitSync.d.ts +388 -0
- package/dist/script/GitSync.js +1068 -0
- package/dist/script/GitSync.js.map +1 -0
- package/dist/script/ScriptContext.d.ts +20 -5
- package/dist/script/ScriptFactory.d.ts +1 -2
- package/dist/script/ScriptFactory.js +1 -2
- package/dist/script/ScriptFactory.js.map +1 -1
- package/dist/script/ScriptFile.d.ts +0 -94
- package/dist/script/ScriptFile.js +0 -245
- package/dist/script/ScriptFile.js.map +1 -1
- package/dist/script/ScriptFolder.d.ts +0 -9
- package/dist/script/ScriptFolder.js +4 -7
- package/dist/script/ScriptFolder.js.map +1 -1
- package/dist/script/ScriptNode.d.ts +0 -9
- package/dist/script/ScriptNode.js.map +1 -1
- package/dist/script/ScriptRoot.d.ts +0 -65
- package/dist/script/ScriptRoot.js +0 -199
- package/dist/script/ScriptRoot.js.map +1 -1
- package/dist/script/ScriptService.d.ts +113 -9
- package/dist/script/ScriptService.js +164 -52
- package/dist/script/ScriptService.js.map +1 -1
- package/dist/script/ServerCompile.d.ts +35 -0
- package/dist/script/ServerCompile.js +40 -0
- package/dist/script/ServerCompile.js.map +1 -0
- package/dist/script/TsConfig.d.ts +2 -2
- package/dist/script/TsConfig.js +3 -3
- package/dist/script/TsConfig.js.map +1 -1
- package/dist/script/push.d.ts +162 -188
- package/dist/script/push.js +216 -474
- package/dist/script/push.js.map +1 -1
- package/dist/session/SessionManager.js.map +1 -1
- package/dist/testing/FakeGateway.d.ts +280 -0
- package/dist/testing/FakeGateway.js +627 -0
- package/dist/testing/FakeGateway.js.map +1 -0
- package/dist/testing/MockFileSystem.js.map +1 -1
- package/dist/types.d.ts +332 -0
- package/dist/update/UpdateService.js.map +1 -1
- package/package.json +17 -12
- package/dist/script/ScriptTranspiler.d.ts +0 -121
- package/dist/script/ScriptTranspiler.js +0 -333
- package/dist/script/ScriptTranspiler.js.map +0 -1
- package/dist/script/SnapshotHistoryRecorder.d.ts +0 -47
- package/dist/script/SnapshotHistoryRecorder.js +0 -231
- package/dist/script/SnapshotHistoryRecorder.js.map +0 -1
- package/dist/script/TsLibResolver.d.ts +0 -60
- package/dist/script/TsLibResolver.js +0 -134
- package/dist/script/TsLibResolver.js.map +0 -1
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GatewayClient = void 0;
|
|
4
|
+
exports.parseGatewayOrigin = parseGatewayOrigin;
|
|
5
|
+
exports.gatewayErrorFor = gatewayErrorFor;
|
|
6
|
+
const AuthTypes_1 = require("../constants/AuthTypes");
|
|
7
|
+
const Gateway_1 = require("../constants/Gateway");
|
|
8
|
+
const Http_1 = require("../constants/Http");
|
|
9
|
+
const MimeTypes_1 = require("../constants/MimeTypes");
|
|
10
|
+
const Err_1 = require("../Err");
|
|
11
|
+
const GatewayToken_1 = require("./GatewayToken");
|
|
12
|
+
/**
|
|
13
|
+
* Hosts allowed to use plain `http:`; everything else must be `https:`, so the bearer token never
|
|
14
|
+
* crosses a network in the clear. Loopback is allowed for local fakes and port-forwards.
|
|
15
|
+
* @lastreviewed null
|
|
16
|
+
*/
|
|
17
|
+
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "[::1]"]);
|
|
18
|
+
/**
|
|
19
|
+
* Header names (lower case) a caller's {@link GatewayRequestOptions.headers} may not set: the ones
|
|
20
|
+
* the client sets itself, and every header that carries a credential.
|
|
21
|
+
* @lastreviewed null
|
|
22
|
+
*/
|
|
23
|
+
const RESERVED_HEADERS = new Set(["authorization", "proxy-authorization", "cookie", "accept", "content-type"]);
|
|
24
|
+
/**
|
|
25
|
+
* An HTTP header name (RFC 9110 token).
|
|
26
|
+
* @lastreviewed null
|
|
27
|
+
*/
|
|
28
|
+
const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
29
|
+
/**
|
|
30
|
+
* An HTTP header value: visible ASCII, spaces and tabs only.
|
|
31
|
+
* @lastreviewed null
|
|
32
|
+
*/
|
|
33
|
+
const HEADER_VALUE = /^[\t\x20-\x7e]*$/;
|
|
34
|
+
/**
|
|
35
|
+
* Check a caller's extra headers before anything is sent. Messages name the header, never its value.
|
|
36
|
+
* @lastreviewed null
|
|
37
|
+
*/
|
|
38
|
+
function checkExtraHeaders(headers) {
|
|
39
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
40
|
+
if (!HEADER_NAME.test(name)) {
|
|
41
|
+
throw new Err_1.Err.GatewayConfigError("A gateway request header name is not a valid HTTP header name; nothing was sent.");
|
|
42
|
+
}
|
|
43
|
+
if (RESERVED_HEADERS.has(name.toLowerCase())) {
|
|
44
|
+
throw new Err_1.Err.GatewayConfigError(`A gateway request may not carry a caller-set ${name} header (the client owns it); nothing was sent.`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof value !== "string" || !HEADER_VALUE.test(value)) {
|
|
47
|
+
throw new Err_1.Err.GatewayConfigError(`The gateway request header ${name} has a value that is not valid; nothing was sent.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Validate a configured gateway URL and reduce it to its origin. Throws
|
|
53
|
+
* {@link Err.GatewayConfigError} for anything the token must not be sent to.
|
|
54
|
+
* @lastreviewed null
|
|
55
|
+
*/
|
|
56
|
+
function parseGatewayOrigin(raw) {
|
|
57
|
+
// No message below quotes the value, its host or its path: a token pasted into the wrong setting
|
|
58
|
+
// would otherwise reach every error the client throws.
|
|
59
|
+
let url;
|
|
60
|
+
try {
|
|
61
|
+
url = new URL(raw);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new Err_1.Err.GatewayConfigError("The configured gateway URL is not a URL.");
|
|
65
|
+
}
|
|
66
|
+
if (url.username || url.password) {
|
|
67
|
+
throw new Err_1.Err.GatewayConfigError("The configured gateway URL carries credentials, which is not allowed.");
|
|
68
|
+
}
|
|
69
|
+
if (url.search || url.hash || (url.pathname !== "/" && url.pathname !== "")) {
|
|
70
|
+
throw new Err_1.Err.GatewayConfigError("The configured gateway URL is not a bare origin: it has a path, a query or a fragment.");
|
|
71
|
+
}
|
|
72
|
+
if (url.protocol === "https:") {
|
|
73
|
+
return url.origin;
|
|
74
|
+
}
|
|
75
|
+
if (url.protocol === "http:" && LOOPBACK_HOSTS.has(url.hostname)) {
|
|
76
|
+
return url.origin;
|
|
77
|
+
}
|
|
78
|
+
if (url.protocol === "http:") {
|
|
79
|
+
throw new Err_1.Err.GatewayConfigError("The configured gateway URL uses plain http on a host that is not loopback; https is required there.");
|
|
80
|
+
}
|
|
81
|
+
throw new Err_1.Err.GatewayConfigError("The configured gateway URL's scheme is neither https nor http.");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Parse a gateway error body. Best-effort: anything that is not a JSON object yields `{}`.
|
|
85
|
+
* @lastreviewed null
|
|
86
|
+
*/
|
|
87
|
+
function parseErrorBody(text) {
|
|
88
|
+
let parsed;
|
|
89
|
+
try {
|
|
90
|
+
parsed = JSON.parse(text);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return {};
|
|
94
|
+
}
|
|
95
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
96
|
+
return {};
|
|
97
|
+
}
|
|
98
|
+
const record = parsed;
|
|
99
|
+
const body = {};
|
|
100
|
+
if (typeof record.error === "string") {
|
|
101
|
+
body.error = record.error;
|
|
102
|
+
}
|
|
103
|
+
if (typeof record.message === "string") {
|
|
104
|
+
body.message = record.message;
|
|
105
|
+
}
|
|
106
|
+
if (typeof record.scope === "string") {
|
|
107
|
+
body.scope = record.scope;
|
|
108
|
+
}
|
|
109
|
+
if (typeof record.limit === "number" && Number.isInteger(record.limit) && record.limit > 0) {
|
|
110
|
+
body.limit = record.limit;
|
|
111
|
+
}
|
|
112
|
+
// A reason is a short code; anything else is ignored rather than quoted.
|
|
113
|
+
if (typeof record.reason === "string" && /^[a-z0-9-]{1,64}$/.test(record.reason)) {
|
|
114
|
+
body.reason = record.reason;
|
|
115
|
+
}
|
|
116
|
+
if (typeof record.presentingExpiresAt === "string" &&
|
|
117
|
+
record.presentingExpiresAt.length <= 40 &&
|
|
118
|
+
!Number.isNaN(Date.parse(record.presentingExpiresAt))) {
|
|
119
|
+
body.presentingExpiresAt = record.presentingExpiresAt;
|
|
120
|
+
}
|
|
121
|
+
return body;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Map a gateway error response to its typed error. Exported so the git layer, which sees the
|
|
125
|
+
* status (and a best-effort body) through its own HTTP client, maps failures exactly as REST does.
|
|
126
|
+
*
|
|
127
|
+
* @param status the HTTP status (non-2xx)
|
|
128
|
+
* @param bodyText the raw response body, `""` when there is none
|
|
129
|
+
* @param what what was being attempted, e.g. `GET /api/v1/knowledge-bases`; must not contain the token
|
|
130
|
+
* @param token the token that was sent, redacted from anything the server said
|
|
131
|
+
* @lastreviewed null
|
|
132
|
+
*/
|
|
133
|
+
function gatewayErrorFor(status, bodyText, what, token) {
|
|
134
|
+
const body = parseErrorBody(bodyText);
|
|
135
|
+
const code = body.error ?? null;
|
|
136
|
+
const said = body.message ? `: ${(0, GatewayToken_1.redactToken)(body.message, token)}` : "";
|
|
137
|
+
const tag = code ? `${status} ${code}` : `${status}`;
|
|
138
|
+
const msg = (happened) => `${what}: ${happened} (${tag})${said}`;
|
|
139
|
+
switch (status) {
|
|
140
|
+
case 401: {
|
|
141
|
+
const reason = code === "token-missing" ? "token-missing" : "token-invalid";
|
|
142
|
+
const happened = reason === "token-missing"
|
|
143
|
+
? "the gateway received no access token"
|
|
144
|
+
: "the gateway did not accept the access token";
|
|
145
|
+
return new Err_1.Err.GatewayAuthError(msg(happened), reason, status);
|
|
146
|
+
}
|
|
147
|
+
case 403:
|
|
148
|
+
if (code === "scope-missing") {
|
|
149
|
+
const scope = body.scope ?? null;
|
|
150
|
+
const lacks = scope ? `the access token lacks the ${scope} scope` : "the access token lacks a required scope";
|
|
151
|
+
return new Err_1.Err.GatewayScopeError(msg(lacks), scope);
|
|
152
|
+
}
|
|
153
|
+
if (code === "scope-escalation") {
|
|
154
|
+
return new Err_1.Err.GatewayScopeEscalationError(msg("a new token's scopes may not exceed those of the token making the request"));
|
|
155
|
+
}
|
|
156
|
+
return new Err_1.Err.GatewayNotPermittedError(msg("this user is not permitted to do this"), code, body.reason ?? null, body.presentingExpiresAt ?? null, body.scope !== undefined && /^[a-z0-9:_-]{1,64}$/.test(body.scope) ? body.scope : null);
|
|
157
|
+
case 400:
|
|
158
|
+
return new Err_1.Err.GatewayInvalidRequestError(msg("the gateway rejected the request as malformed"), code);
|
|
159
|
+
case 404:
|
|
160
|
+
return new Err_1.Err.GatewayNotFoundError(msg("not found, or not visible to this user"), code);
|
|
161
|
+
case 409:
|
|
162
|
+
if (code === "alias-taken") {
|
|
163
|
+
return new Err_1.Err.GatewayAliasTakenError(msg("another of this user's tokens already has that alias"));
|
|
164
|
+
}
|
|
165
|
+
if (code === "token-limit") {
|
|
166
|
+
const limit = body.limit ?? null;
|
|
167
|
+
const full = limit
|
|
168
|
+
? `this user already has the maximum of ${limit} live tokens`
|
|
169
|
+
: "this user already has the maximum number of live tokens";
|
|
170
|
+
return new Err_1.Err.GatewayTokenLimitError(msg(full), limit);
|
|
171
|
+
}
|
|
172
|
+
if (code === "live-branch-unprotected") {
|
|
173
|
+
return new Err_1.Err.GatewayLiveBranchUnprotectedError(msg("the gateway refused the push because the knowledge base's live branch is not protected on the forge; nothing was pushed"));
|
|
174
|
+
}
|
|
175
|
+
// Only the git routes' conflicts (a push that does not build on the head; a bodyless 409 from
|
|
176
|
+
// the git proxy) are about a branch head. Any other 409 code is some other conflict.
|
|
177
|
+
if (code === null || code === "non-fast-forward") {
|
|
178
|
+
return new Err_1.Err.GatewayConflictError(msg("the update does not build on the current branch head"), code);
|
|
179
|
+
}
|
|
180
|
+
return new Err_1.Err.GatewayConflictError(msg("the request conflicts with the current state"), code);
|
|
181
|
+
case 413:
|
|
182
|
+
return new Err_1.Err.GatewayTooLargeError(msg("the request is larger than the gateway accepts"), code);
|
|
183
|
+
case 503:
|
|
184
|
+
if (code === "registry-unavailable") {
|
|
185
|
+
return new Err_1.Err.GatewayUnavailableError(msg("web-global, which holds the registry, could not answer"), status, code);
|
|
186
|
+
}
|
|
187
|
+
return new Err_1.Err.GatewayUnavailableError(msg("the gateway or the forge behind it is unavailable"), status, code);
|
|
188
|
+
case 502:
|
|
189
|
+
case 504:
|
|
190
|
+
return new Err_1.Err.GatewayUnavailableError(msg("the gateway or the forge behind it is unavailable"), status, code);
|
|
191
|
+
default:
|
|
192
|
+
return new Err_1.Err.GatewayUnexpectedResponseError(msg("the gateway answered with an unexpected status"), status, code);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* HTTP client for web-global's gateway.
|
|
197
|
+
*
|
|
198
|
+
* Every request carries `Authorization: Bearer <b6pt_ token>`, asked of the injected
|
|
199
|
+
* {@link GatewayContext.gatewayToken} each time. There is no session, no login and no cookie: this
|
|
200
|
+
* is deliberately not {@link SessionManager}, which bootstraps the web origin's cookie session.
|
|
201
|
+
*
|
|
202
|
+
* REST requests made through this client ({@link request} / {@link getJson}) run **one at a time**,
|
|
203
|
+
* in call order, so a burst from a caller cannot fan out into parallel token reads or parallel
|
|
204
|
+
* connections (the same discipline `SessionManager` keeps). {@link sequence} puts a longer task, such
|
|
205
|
+
* as one of the git layer's operations, in the same queue. {@link authHeadersFor} is not queued: a
|
|
206
|
+
* request a caller sends itself with that header is outside it unless wrapped in `sequence`.
|
|
207
|
+
*
|
|
208
|
+
* Redirects are never followed; a 3xx is an {@link Err.GatewayUnexpectedResponseError} with code
|
|
209
|
+
* `redirect-refused`. A request path that resolves off the gateway's origin is refused unsent.
|
|
210
|
+
*
|
|
211
|
+
* The token never appears in a thrown message or a log line: the client never formats it, and
|
|
212
|
+
* anything the server says is passed through {@link redactToken} first.
|
|
213
|
+
* @lastreviewed null
|
|
214
|
+
*/
|
|
215
|
+
class GatewayClient {
|
|
216
|
+
ctx;
|
|
217
|
+
tail = Promise.resolve();
|
|
218
|
+
timeoutMs;
|
|
219
|
+
constructor(ctx, options = {}) {
|
|
220
|
+
this.ctx = ctx;
|
|
221
|
+
this.timeoutMs = options.timeoutMs ?? Gateway_1.Gateway.REQUEST_TIMEOUT_MS;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The validated gateway origin (e.g. `https://gateway.bluestep.net`), with no trailing slash.
|
|
225
|
+
* @lastreviewed null
|
|
226
|
+
*/
|
|
227
|
+
async origin() {
|
|
228
|
+
return parseGatewayOrigin(await this.ctx.gatewayUrl());
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The `Authorization` header for a request to `url`, for a caller that sends requests itself
|
|
232
|
+
* (the git layer hands this to its HTTP client). Throws {@link Err.GatewayConfigError} when
|
|
233
|
+
* `url` is not on the gateway's origin, so a server-supplied URL cannot redirect the token, and
|
|
234
|
+
* {@link Err.GatewayAuthError} when no usable token is available.
|
|
235
|
+
* @lastreviewed null
|
|
236
|
+
*/
|
|
237
|
+
async authHeadersFor(url) {
|
|
238
|
+
const origin = await this.origin();
|
|
239
|
+
let target;
|
|
240
|
+
try {
|
|
241
|
+
target = new URL(url);
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
throw new Err_1.Err.GatewayConfigError("A URL handed to the gateway client is not a URL.");
|
|
245
|
+
}
|
|
246
|
+
if (target.origin !== origin) {
|
|
247
|
+
throw new Err_1.Err.GatewayConfigError("A URL handed to the gateway client is not on the gateway's origin; no token is sent to it.");
|
|
248
|
+
}
|
|
249
|
+
const token = await this.requireToken();
|
|
250
|
+
return { [Http_1.Http.Headers.AUTHORIZATION]: `${AuthTypes_1.AuthTypes.BEARER_PREFIX}${token}` };
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* `GET` a JSON resource.
|
|
254
|
+
* @lastreviewed null
|
|
255
|
+
*/
|
|
256
|
+
async getJson(path, query) {
|
|
257
|
+
return this.request(Http_1.Http.Methods.GET, path, { query });
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Send one REST request and return its parsed JSON body (`undefined` for an empty body). Queued
|
|
261
|
+
* behind any request already in flight on this client.
|
|
262
|
+
*
|
|
263
|
+
* @param method HTTP method
|
|
264
|
+
* @param path absolute path on the gateway, e.g. `/api/v1/knowledge-bases`
|
|
265
|
+
* @throws {Err.GatewayError} a subclass naming what went wrong
|
|
266
|
+
* @lastreviewed null
|
|
267
|
+
*/
|
|
268
|
+
request(method, path, options = {}) {
|
|
269
|
+
return this.sequence((seq) => seq.request(method, path, options));
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Run `unit` as ONE entry in this client's one-at-a-time queue, behind any request or unit
|
|
273
|
+
* already in flight. Nothing else on this client runs until `unit` settles. The git layer wraps
|
|
274
|
+
* a whole operation (several HTTP requests) in this so git traffic and REST calls never overlap
|
|
275
|
+
* (it ignores `seq`); token rotation runs list + rotate + store in it, so nothing queued can
|
|
276
|
+
* present the just-revoked token.
|
|
277
|
+
*
|
|
278
|
+
* `seq.token()` reads the stored token once, on first use, and every later call and every
|
|
279
|
+
* `seq.request` of the unit presents that same value.
|
|
280
|
+
*
|
|
281
|
+
* Inside `unit`, send REST requests through `seq` only. Calling {@link request} or `sequence` on
|
|
282
|
+
* this client from inside it waits behind the unit itself and never runs.
|
|
283
|
+
* @lastreviewed null
|
|
284
|
+
*/
|
|
285
|
+
sequence(unit) {
|
|
286
|
+
const run = async () => {
|
|
287
|
+
let read;
|
|
288
|
+
let open = true;
|
|
289
|
+
const token = () => (read ??= this.requireToken());
|
|
290
|
+
const seq = {
|
|
291
|
+
token,
|
|
292
|
+
request: async (method, path, options = {}) => {
|
|
293
|
+
if (!open) {
|
|
294
|
+
throw new Err_1.Err.GatewayConfigError("A gateway request was sent through a sequence that has ended.");
|
|
295
|
+
}
|
|
296
|
+
return this.send(method, path, options, token);
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
try {
|
|
300
|
+
return await unit(seq);
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
open = false;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
const result = this.tail.then(run, run);
|
|
307
|
+
this.tail = result.catch(() => undefined);
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
async requireToken() {
|
|
311
|
+
const token = await this.ctx.gatewayToken.token();
|
|
312
|
+
if (token === undefined || token.length === 0) {
|
|
313
|
+
throw new Err_1.Err.GatewayAuthError("No gateway access token is available.", "token-missing", null);
|
|
314
|
+
}
|
|
315
|
+
const problem = (0, GatewayToken_1.gatewayTokenProblem)(token);
|
|
316
|
+
if (problem) {
|
|
317
|
+
throw new Err_1.Err.GatewayAuthError(`The gateway access token is not usable: ${problem}.`, "token-malformed", null);
|
|
318
|
+
}
|
|
319
|
+
return token;
|
|
320
|
+
}
|
|
321
|
+
async send(method, path, options, readToken) {
|
|
322
|
+
const origin = await this.origin();
|
|
323
|
+
// Neither message quotes the path: it may carry caller or server input.
|
|
324
|
+
if (!path.startsWith("/") || path.startsWith("//")) {
|
|
325
|
+
throw new Err_1.Err.GatewayConfigError("A gateway request path is not an absolute path.");
|
|
326
|
+
}
|
|
327
|
+
const url = new URL(path, origin);
|
|
328
|
+
// URL parsing reads `\` as `/` and drops tabs and newlines, so a path like `/\host/x` resolves to
|
|
329
|
+
// another host. Checked on the built URL, before any header exists, as in authHeadersFor.
|
|
330
|
+
if (url.origin !== origin) {
|
|
331
|
+
throw new Err_1.Err.GatewayConfigError("A gateway request path resolves off the gateway's origin; no request is sent.");
|
|
332
|
+
}
|
|
333
|
+
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
334
|
+
if (value !== undefined) {
|
|
335
|
+
url.searchParams.set(key, value);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const what = `${method} ${url.pathname}${url.search}`;
|
|
339
|
+
if (options.headers) {
|
|
340
|
+
checkExtraHeaders(options.headers);
|
|
341
|
+
}
|
|
342
|
+
const token = await readToken();
|
|
343
|
+
// The caller's headers go first, so the client's own (already refused above) could never be overridden.
|
|
344
|
+
const headers = {
|
|
345
|
+
...options.headers,
|
|
346
|
+
[Http_1.Http.Headers.AUTHORIZATION]: `${AuthTypes_1.AuthTypes.BEARER_PREFIX}${token}`,
|
|
347
|
+
[Http_1.Http.Headers.ACCEPT]: MimeTypes_1.MimeTypes.APPLICATION_JSON,
|
|
348
|
+
};
|
|
349
|
+
// "manual", not "error": the redirect then arrives as a 3xx this client can name, rather than
|
|
350
|
+
// a TypeError indistinguishable from the network being down. It is never followed either way.
|
|
351
|
+
const init = { method, headers, redirect: "manual" };
|
|
352
|
+
if (options.body !== undefined) {
|
|
353
|
+
headers[Http_1.Http.Headers.CONTENT_TYPE] = MimeTypes_1.MimeTypes.APPLICATION_JSON;
|
|
354
|
+
init.body = JSON.stringify(options.body);
|
|
355
|
+
}
|
|
356
|
+
const controller = new AbortController();
|
|
357
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
358
|
+
timer.unref?.();
|
|
359
|
+
init.signal = controller.signal;
|
|
360
|
+
let response;
|
|
361
|
+
let text;
|
|
362
|
+
try {
|
|
363
|
+
response = await this.ctx.fetchFn(url, init);
|
|
364
|
+
text = await response.text();
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
const why = controller.signal.aborted
|
|
368
|
+
? `no answer within ${this.timeoutMs} ms`
|
|
369
|
+
: `the request failed (${(0, GatewayToken_1.redactToken)(error instanceof Error ? error.message : String(error), token)})`;
|
|
370
|
+
throw new Err_1.Err.GatewayUnavailableError(`${what}: the gateway could not be reached: ${why}.`, null, null);
|
|
371
|
+
}
|
|
372
|
+
finally {
|
|
373
|
+
clearTimeout(timer);
|
|
374
|
+
}
|
|
375
|
+
this.ctx.logger.debug(`gateway ${what} -> ${response.status}`);
|
|
376
|
+
if (response.type === "opaqueredirect" || (response.status >= 300 && response.status < 400)) {
|
|
377
|
+
// Location is not quoted: it is server-supplied and names a place the token was not sent.
|
|
378
|
+
throw new Err_1.Err.GatewayUnexpectedResponseError(`${what}: the gateway answered with a redirect (${response.status}), which was not followed.`, response.status, "redirect-refused");
|
|
379
|
+
}
|
|
380
|
+
if (!response.ok) {
|
|
381
|
+
throw gatewayErrorFor(response.status, text, what, token);
|
|
382
|
+
}
|
|
383
|
+
if (text.length === 0) {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
return JSON.parse(text);
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
throw new Err_1.Err.GatewayUnexpectedResponseError(`${what}: the gateway answered ${response.status} with a body that is not JSON.`, response.status, null);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
exports.GatewayClient = GatewayClient;
|
|
395
|
+
//# sourceMappingURL=GatewayClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AAC;YACjB,MAAM,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QAC5D,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACtB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAc,CAAC;QACvC,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,SAAG,CAAC,8BAA8B,CAC1C,GAAG,IAAI,0BAA0B,QAAQ,CAAC,MAAM,gCAAgC,EAChF,QAAQ,CAAC,MAAM,EACf,IAAI,CACL,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PlatformContext } from "../PlatformContext";
|
|
2
|
+
import type { GatewayTokenSource, GatewayTokenStore } from "../providers";
|
|
3
|
+
/**
|
|
4
|
+
* Fetch signature the gateway client calls; matches `globalThis.fetch`.
|
|
5
|
+
* @lastreviewed null
|
|
6
|
+
*/
|
|
7
|
+
export type GatewayFetch = (url: string | URL, init?: RequestInit) => Promise<Response>;
|
|
8
|
+
/**
|
|
9
|
+
* The dependency bundle of the gateway subsystem ({@link GatewayService}): the shared
|
|
10
|
+
* {@link PlatformContext} plus what only the gateway reads. {@link B6PCore} spreads its one
|
|
11
|
+
* platform bundle into this, the same as it does for `ScriptContext`.
|
|
12
|
+
*
|
|
13
|
+
* It holds no `persistence` and no `auth`: the gateway is a second origin that sends the stored
|
|
14
|
+
* `b6pt_` token on every request, read through {@link gatewayToken}. `sessionManager` is present
|
|
15
|
+
* (it comes with {@link PlatformContext}) but the gateway must not use it: that is the web
|
|
16
|
+
* session's cookie bootstrap. Nor may it drive the credential's lifecycle (prompting, clearing).
|
|
17
|
+
* Its one write is {@link gatewayTokenStore}: putting a rotated replacement in place of the stored
|
|
18
|
+
* token, which the gateway has just revoked.
|
|
19
|
+
* @lastreviewed null
|
|
20
|
+
*/
|
|
21
|
+
export interface GatewayContext extends PlatformContext {
|
|
22
|
+
/**
|
|
23
|
+
* Where the `b6pt_` token comes from, asked once per request.
|
|
24
|
+
* @lastreviewed null
|
|
25
|
+
*/
|
|
26
|
+
readonly gatewayToken: GatewayTokenSource;
|
|
27
|
+
/**
|
|
28
|
+
* Where the replacement goes when the stored token is rotated; read only by
|
|
29
|
+
* {@link GatewayTokenService.rotate}.
|
|
30
|
+
* @lastreviewed null
|
|
31
|
+
*/
|
|
32
|
+
readonly gatewayTokenStore: GatewayTokenStore;
|
|
33
|
+
/**
|
|
34
|
+
* The configured gateway origin (unvalidated; the client checks it before every request).
|
|
35
|
+
* @lastreviewed null
|
|
36
|
+
*/
|
|
37
|
+
gatewayUrl(): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* The fetch the client sends requests through.
|
|
40
|
+
* @lastreviewed null
|
|
41
|
+
*/
|
|
42
|
+
readonly fetchFn: GatewayFetch;
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GatewayContext.js","sourceRoot":"","sources":["../../src/gateway/GatewayContext.ts"],"names":[],"mappings":""}
|