@bluestep-systems/b6p-core 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -48
- package/dist/B6PCore.d.ts +6 -0
- package/dist/B6PCore.js +7 -0
- package/dist/B6PCore.js.map +1 -1
- package/dist/Err.d.ts +44 -0
- package/dist/Err.js +67 -0
- package/dist/Err.js.map +1 -1
- package/dist/constants/Gateway.d.ts +1 -0
- package/dist/constants/Gateway.js +1 -0
- package/dist/constants/Gateway.js.map +1 -1
- package/dist/gateway/GatewayClient.d.ts +32 -0
- package/dist/gateway/GatewayClient.js +21 -2
- package/dist/gateway/GatewayClient.js.map +1 -1
- package/dist/gateway/GatewayKb.d.ts +56 -1
- package/dist/gateway/GatewayKb.js +205 -0
- package/dist/gateway/GatewayKb.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/schema/SchemaContext.d.ts +10 -0
- package/dist/schema/SchemaContext.js +3 -0
- package/dist/schema/SchemaContext.js.map +1 -0
- package/dist/schema/SchemaService.d.ts +99 -0
- package/dist/schema/SchemaService.js +225 -0
- package/dist/schema/SchemaService.js.map +1 -0
- package/dist/testing/FakeGateway.d.ts +24 -0
- package/dist/testing/FakeGateway.js +176 -0
- package/dist/testing/FakeGateway.js.map +1 -1
- package/dist/types.d.ts +297 -0
- package/package.json +2 -2
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { SchemaApplyResult, SchemaRecipeName, SchemaRecipeResult, SchemaTestResult } from "../types";
|
|
2
|
+
import type { SchemaContext } from "./SchemaContext";
|
|
3
|
+
/**
|
|
4
|
+
* Options of {@link SchemaService.generate}.
|
|
5
|
+
* @lastreviewed null
|
|
6
|
+
*/
|
|
7
|
+
export interface SchemaGenerateOptions {
|
|
8
|
+
/** Declare every object of a supported kind the named ones reference too; default `true` (the server's). @lastreviewed null */
|
|
9
|
+
closure?: boolean;
|
|
10
|
+
/** Generate a manifest holding no key, for applying in another tenant; default `false` (the server's). @lastreviewed null */
|
|
11
|
+
portable?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Options of {@link SchemaService.test}.
|
|
15
|
+
* @lastreviewed null
|
|
16
|
+
*/
|
|
17
|
+
export interface SchemaTestOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Also report same-display-name collisions within a kind as warnings. Sent only when `true`, so a server that predates
|
|
20
|
+
* the argument keeps working; a server that rejects it throws `Err.SchemaServerTooOldError`.
|
|
21
|
+
* @lastreviewed null
|
|
22
|
+
*/
|
|
23
|
+
verbose?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* What {@link SchemaService.testThenApply} did.
|
|
27
|
+
* @lastreviewed null
|
|
28
|
+
*/
|
|
29
|
+
export interface SchemaTestThenApplyResult {
|
|
30
|
+
/** The dry run. @lastreviewed null */
|
|
31
|
+
test: SchemaTestResult;
|
|
32
|
+
/** The caller was shown a passing test and answered no, so nothing was applied. @lastreviewed null */
|
|
33
|
+
declined: boolean;
|
|
34
|
+
/** The apply's answer; `null` when the test did not pass (no key) or the caller declined. @lastreviewed null */
|
|
35
|
+
apply: SchemaApplyResult | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The schema subsystem, `core.schema`: generate, test and apply declarative manifests against one org. Every operation
|
|
39
|
+
* is reserved to super users on the server (`Err.SchemaForbiddenError` otherwise). Requests go to the org's `/gql`
|
|
40
|
+
* through the session manager, the same CSRF-managed path script code uses.
|
|
41
|
+
*
|
|
42
|
+
* Each method takes `org`: any URL on the org (its origin is used), the way other per-org calls take one.
|
|
43
|
+
* @lastreviewed null
|
|
44
|
+
*/
|
|
45
|
+
export declare class SchemaService {
|
|
46
|
+
private readonly ctx;
|
|
47
|
+
constructor(ctx: SchemaContext);
|
|
48
|
+
/**
|
|
49
|
+
* The manifest describing the named objects as they stand now (GraphQL `generateSchema`).
|
|
50
|
+
* @param org Any URL on the org; its origin is used
|
|
51
|
+
* @param topIds Ids of the objects to describe
|
|
52
|
+
* @returns The manifest, a YAML stream
|
|
53
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
54
|
+
* @lastreviewed null
|
|
55
|
+
*/
|
|
56
|
+
generate(org: string | URL, topIds: readonly string[], options?: SchemaGenerateOptions): Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Runs a recipe: an audit of the org stated as a manifest, with what it met (GraphQL `schemaRecipe`). Writes nothing.
|
|
59
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
60
|
+
* @lastreviewed null
|
|
61
|
+
*/
|
|
62
|
+
recipe(org: string | URL, name: SchemaRecipeName): Promise<SchemaRecipeResult>;
|
|
63
|
+
/**
|
|
64
|
+
* Evaluates a manifest against the org as the caller, writing nothing (GraphQL `testSchema`). When every stage
|
|
65
|
+
* passes the result carries the key {@link apply} needs. `verbose` is sent only when asked for.
|
|
66
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
67
|
+
* @throws Err.SchemaServerTooOldError when `verbose` is asked for and the server does not know the argument
|
|
68
|
+
* @lastreviewed null
|
|
69
|
+
*/
|
|
70
|
+
test(org: string | URL, manifest: string, options?: SchemaTestOptions): Promise<SchemaTestResult>;
|
|
71
|
+
/**
|
|
72
|
+
* Applies the plan a {@link test} approved (GraphQL `applySchema`): the manifest string that was tested, byte for
|
|
73
|
+
* byte, and its key. The key is spent by the call whatever its outcome.
|
|
74
|
+
*
|
|
75
|
+
* The request is not retried: a retry after a lost answer would find the key spent and report `KEY_INVALID` for an
|
|
76
|
+
* apply that may have committed. A transport failure here means the outcome is unknown; a fresh {@link test} then
|
|
77
|
+
* plans to no change if the apply went through.
|
|
78
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
79
|
+
* @lastreviewed null
|
|
80
|
+
*/
|
|
81
|
+
apply(org: string | URL, manifest: string, schemaMutationKey: string): Promise<SchemaApplyResult>;
|
|
82
|
+
/**
|
|
83
|
+
* {@link test}, hand the result to `confirm`, and on a yes {@link apply} the same manifest string with the key the
|
|
84
|
+
* test minted. Keeps "same bytes, same key" in one place. `confirm` is asked only when the test passed.
|
|
85
|
+
* @lastreviewed null
|
|
86
|
+
*/
|
|
87
|
+
testThenApply(org: string | URL, manifest: string, confirm: (result: SchemaTestResult) => Promise<boolean>, options?: SchemaTestOptions): Promise<SchemaTestThenApplyResult>;
|
|
88
|
+
/**
|
|
89
|
+
* Posts one operation to the org's `/gql` and returns its `data`.
|
|
90
|
+
* @param operation The schema operation's name, for the error that names it
|
|
91
|
+
* @param retries Passed to the session manager's retrying fetch; `0` for a request that must not be repeated
|
|
92
|
+
* @throws Err.SchemaForbiddenError on `FORBIDDEN`
|
|
93
|
+
* @throws Err.SchemaServerTooOldError when the server's validation rejects a schema operation or `verbose`
|
|
94
|
+
* @throws Err.GraphQLError on any other GraphQL error
|
|
95
|
+
* @throws Err.GraphQLFetchError on a non-OK answer that is no GraphQL answer, or one with no data
|
|
96
|
+
* @lastreviewed null
|
|
97
|
+
*/
|
|
98
|
+
private post;
|
|
99
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaService = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const Err_1 = require("../Err");
|
|
6
|
+
/**
|
|
7
|
+
* The GraphQL selection of a {@link SchemaApplyRefusal}.
|
|
8
|
+
* @lastreviewed null
|
|
9
|
+
*/
|
|
10
|
+
const APPLY_REFUSAL_FIELDS = "code element detail";
|
|
11
|
+
/**
|
|
12
|
+
* The GraphQL selection of a {@link SchemaTestResult}: every field.
|
|
13
|
+
* @lastreviewed null
|
|
14
|
+
*/
|
|
15
|
+
const TEST_RESULT_FIELDS = `
|
|
16
|
+
passed
|
|
17
|
+
failedAt
|
|
18
|
+
actions {
|
|
19
|
+
element
|
|
20
|
+
action
|
|
21
|
+
objectId
|
|
22
|
+
spec
|
|
23
|
+
deltas { path kind from to livePositions }
|
|
24
|
+
reasons { code detail }
|
|
25
|
+
}
|
|
26
|
+
refusals {
|
|
27
|
+
stage
|
|
28
|
+
code
|
|
29
|
+
locus
|
|
30
|
+
detail
|
|
31
|
+
hits { objectId live }
|
|
32
|
+
}
|
|
33
|
+
schemaMutationKey
|
|
34
|
+
refusal { ${APPLY_REFUSAL_FIELDS} }
|
|
35
|
+
warnings { element detail }
|
|
36
|
+
`;
|
|
37
|
+
/**
|
|
38
|
+
* The GraphQL selection of a {@link SchemaApplyResult}: every field.
|
|
39
|
+
* @lastreviewed null
|
|
40
|
+
*/
|
|
41
|
+
const APPLY_RESULT_FIELDS = `
|
|
42
|
+
applied
|
|
43
|
+
objects { element objectId }
|
|
44
|
+
refusal { ${APPLY_REFUSAL_FIELDS} }
|
|
45
|
+
failedAt
|
|
46
|
+
historyCommit
|
|
47
|
+
`;
|
|
48
|
+
/**
|
|
49
|
+
* The GraphQL selection of a {@link SchemaRecipeResult}: every field.
|
|
50
|
+
* @lastreviewed null
|
|
51
|
+
*/
|
|
52
|
+
const RECIPE_RESULT_FIELDS = `
|
|
53
|
+
manifest
|
|
54
|
+
findings { code severity subjectId fieldId formId detail }
|
|
55
|
+
`;
|
|
56
|
+
/**
|
|
57
|
+
* The schema operation names, which a validation error naming one (or `verbose`) means the server does not know.
|
|
58
|
+
* @lastreviewed null
|
|
59
|
+
*/
|
|
60
|
+
const SCHEMA_FEATURE_NAME = /\b(generateSchema|schemaRecipe|testSchema|applySchema|verbose)\b/;
|
|
61
|
+
/**
|
|
62
|
+
* The schema subsystem, `core.schema`: generate, test and apply declarative manifests against one org. Every operation
|
|
63
|
+
* is reserved to super users on the server (`Err.SchemaForbiddenError` otherwise). Requests go to the org's `/gql`
|
|
64
|
+
* through the session manager, the same CSRF-managed path script code uses.
|
|
65
|
+
*
|
|
66
|
+
* Each method takes `org`: any URL on the org (its origin is used), the way other per-org calls take one.
|
|
67
|
+
* @lastreviewed null
|
|
68
|
+
*/
|
|
69
|
+
class SchemaService {
|
|
70
|
+
ctx;
|
|
71
|
+
constructor(ctx) {
|
|
72
|
+
this.ctx = ctx;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The manifest describing the named objects as they stand now (GraphQL `generateSchema`).
|
|
76
|
+
* @param org Any URL on the org; its origin is used
|
|
77
|
+
* @param topIds Ids of the objects to describe
|
|
78
|
+
* @returns The manifest, a YAML stream
|
|
79
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
80
|
+
* @lastreviewed null
|
|
81
|
+
*/
|
|
82
|
+
async generate(org, topIds, options = {}) {
|
|
83
|
+
const data = await this.post(org, "generateSchema", `query GenerateSchema($topIds: [ID!]!, $closure: Boolean!, $portable: Boolean!) {
|
|
84
|
+
generateSchema(topIds: $topIds, closure: $closure, portable: $portable)
|
|
85
|
+
}`, { topIds: [...topIds], closure: options.closure ?? true, portable: options.portable ?? false });
|
|
86
|
+
return data.generateSchema;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Runs a recipe: an audit of the org stated as a manifest, with what it met (GraphQL `schemaRecipe`). Writes nothing.
|
|
90
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
91
|
+
* @lastreviewed null
|
|
92
|
+
*/
|
|
93
|
+
async recipe(org, name) {
|
|
94
|
+
const data = await this.post(org, "schemaRecipe", `query SchemaRecipe($name: SchemaRecipeName!) { schemaRecipe(name: $name) { ${RECIPE_RESULT_FIELDS} } }`, { name });
|
|
95
|
+
return data.schemaRecipe;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Evaluates a manifest against the org as the caller, writing nothing (GraphQL `testSchema`). When every stage
|
|
99
|
+
* passes the result carries the key {@link apply} needs. `verbose` is sent only when asked for.
|
|
100
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
101
|
+
* @throws Err.SchemaServerTooOldError when `verbose` is asked for and the server does not know the argument
|
|
102
|
+
* @lastreviewed null
|
|
103
|
+
*/
|
|
104
|
+
async test(org, manifest, options = {}) {
|
|
105
|
+
const verbose = options.verbose === true;
|
|
106
|
+
const data = await this.post(org, "testSchema", verbose
|
|
107
|
+
? `mutation TestSchema($manifest: String!, $verbose: Boolean!) {
|
|
108
|
+
testSchema(manifest: $manifest, verbose: $verbose) { ${TEST_RESULT_FIELDS} }
|
|
109
|
+
}`
|
|
110
|
+
: `mutation TestSchema($manifest: String!) { testSchema(manifest: $manifest) { ${TEST_RESULT_FIELDS} } }`, verbose ? { manifest, verbose } : { manifest });
|
|
111
|
+
return data.testSchema;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Applies the plan a {@link test} approved (GraphQL `applySchema`): the manifest string that was tested, byte for
|
|
115
|
+
* byte, and its key. The key is spent by the call whatever its outcome.
|
|
116
|
+
*
|
|
117
|
+
* The request is not retried: a retry after a lost answer would find the key spent and report `KEY_INVALID` for an
|
|
118
|
+
* apply that may have committed. A transport failure here means the outcome is unknown; a fresh {@link test} then
|
|
119
|
+
* plans to no change if the apply went through.
|
|
120
|
+
* @throws Err.SchemaForbiddenError when the caller is not a super user
|
|
121
|
+
* @lastreviewed null
|
|
122
|
+
*/
|
|
123
|
+
async apply(org, manifest, schemaMutationKey) {
|
|
124
|
+
const data = await this.post(org, "applySchema", `mutation ApplySchema($manifest: String!, $key: ID!) {
|
|
125
|
+
applySchema(manifest: $manifest, schemaMutationKey: $key) { ${APPLY_RESULT_FIELDS} }
|
|
126
|
+
}`, { manifest, key: schemaMutationKey }, 0);
|
|
127
|
+
return data.applySchema;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* {@link test}, hand the result to `confirm`, and on a yes {@link apply} the same manifest string with the key the
|
|
131
|
+
* test minted. Keeps "same bytes, same key" in one place. `confirm` is asked only when the test passed.
|
|
132
|
+
* @lastreviewed null
|
|
133
|
+
*/
|
|
134
|
+
async testThenApply(org, manifest, confirm, options = {}) {
|
|
135
|
+
const test = await this.test(org, manifest, options);
|
|
136
|
+
if (!test.passed || test.schemaMutationKey === null) {
|
|
137
|
+
return { test, declined: false, apply: null };
|
|
138
|
+
}
|
|
139
|
+
if (!(await confirm(test))) {
|
|
140
|
+
return { test, declined: true, apply: null };
|
|
141
|
+
}
|
|
142
|
+
return { test, declined: false, apply: await this.apply(org, manifest, test.schemaMutationKey) };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Posts one operation to the org's `/gql` and returns its `data`.
|
|
146
|
+
* @param operation The schema operation's name, for the error that names it
|
|
147
|
+
* @param retries Passed to the session manager's retrying fetch; `0` for a request that must not be repeated
|
|
148
|
+
* @throws Err.SchemaForbiddenError on `FORBIDDEN`
|
|
149
|
+
* @throws Err.SchemaServerTooOldError when the server's validation rejects a schema operation or `verbose`
|
|
150
|
+
* @throws Err.GraphQLError on any other GraphQL error
|
|
151
|
+
* @throws Err.GraphQLFetchError on a non-OK answer that is no GraphQL answer, or one with no data
|
|
152
|
+
* @lastreviewed null
|
|
153
|
+
*/
|
|
154
|
+
async post(org, operation, query, variables, retries) {
|
|
155
|
+
const url = new URL(org);
|
|
156
|
+
url.pathname = constants_1.ApiEndpoints.GQL;
|
|
157
|
+
url.search = "";
|
|
158
|
+
url.hash = "";
|
|
159
|
+
if (retries === 0) {
|
|
160
|
+
// A request that must not be repeated cannot lean on csrfFetch's retry to recover a stale session: csrfFetch
|
|
161
|
+
// keeps an expired session object across its own login. Fetching the CSRF token first logs in when the session
|
|
162
|
+
// is expired, so csrfFetch then finds a live session and its one POST carries a matching token and cookie.
|
|
163
|
+
const warm = new URL(url);
|
|
164
|
+
warm.pathname = constants_1.ApiEndpoints.CSRF_TOKEN;
|
|
165
|
+
await this.ctx.sessionManager.fetch(warm);
|
|
166
|
+
}
|
|
167
|
+
const res = await this.ctx.sessionManager.csrfFetch(url, {
|
|
168
|
+
method: constants_1.Http.Methods.POST,
|
|
169
|
+
headers: { [constants_1.Http.Headers.CONTENT_TYPE]: constants_1.MimeTypes.APPLICATION_JSON },
|
|
170
|
+
body: JSON.stringify({ query, variables, operationName: operationNameOf(query) }),
|
|
171
|
+
}, retries);
|
|
172
|
+
const text = await res.text();
|
|
173
|
+
let body;
|
|
174
|
+
try {
|
|
175
|
+
const parsed = JSON.parse(text);
|
|
176
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
177
|
+
throw new Error("not a GraphQL response object");
|
|
178
|
+
}
|
|
179
|
+
body = parsed;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
throw new Err_1.Err.GraphQLFetchError(`${operation}: HTTP ${res.status} with no JSON body: ${text.substring(0, 500)}`);
|
|
183
|
+
}
|
|
184
|
+
if (Array.isArray(body.errors) && body.errors.length > 0) {
|
|
185
|
+
if (body.errors.some((e) => codeOf(e) === "FORBIDDEN")) {
|
|
186
|
+
throw new Err_1.Err.SchemaForbiddenError(operation);
|
|
187
|
+
}
|
|
188
|
+
const tooOld = body.errors.find((e) => codeOf(e) === "ValidationError" && SCHEMA_FEATURE_NAME.test(e.message ?? ""));
|
|
189
|
+
if (tooOld !== undefined) {
|
|
190
|
+
const feature = /\bverbose\b/.test(tooOld.message ?? "") ? "testSchema's verbose argument" : operation;
|
|
191
|
+
throw new Err_1.Err.SchemaServerTooOldError(feature, tooOld.message ?? "");
|
|
192
|
+
}
|
|
193
|
+
throw new Err_1.Err.GraphQLError(body.errors);
|
|
194
|
+
}
|
|
195
|
+
const data = body.data;
|
|
196
|
+
if (!res.ok ||
|
|
197
|
+
typeof data !== "object" ||
|
|
198
|
+
data === null ||
|
|
199
|
+
data[operation] === undefined ||
|
|
200
|
+
data[operation] === null) {
|
|
201
|
+
throw new Err_1.Err.GraphQLFetchError(`${operation}: HTTP ${res.status} ${res.statusText} with no ${operation} answer`);
|
|
202
|
+
}
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.SchemaService = SchemaService;
|
|
207
|
+
/**
|
|
208
|
+
* The code of a GraphQL error: the server's `extensions.code`, else graphql-java's `extensions.classification`.
|
|
209
|
+
* @lastreviewed null
|
|
210
|
+
*/
|
|
211
|
+
function codeOf(error) {
|
|
212
|
+
return error.extensions?.code ?? error.extensions?.classification;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The operation name a query document declares (`query Name`, `mutation Name`).
|
|
216
|
+
* @lastreviewed null
|
|
217
|
+
*/
|
|
218
|
+
function operationNameOf(query) {
|
|
219
|
+
const match = /^\s*(?:query|mutation)\s+(\w+)/.exec(query);
|
|
220
|
+
if (match === null) {
|
|
221
|
+
throw new Error("schema operation without a name");
|
|
222
|
+
}
|
|
223
|
+
return match[1];
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=SchemaService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaService.js","sourceRoot":"","sources":["../../src/schema/SchemaService.ts"],"names":[],"mappings":";;;AAAA,4CAA6D;AAC7D,gCAA6B;AAyC7B;;;GAGG;AACH,MAAM,oBAAoB,GAAG,qBAAqB,CAAC;AAEnD;;;GAGG;AACH,MAAM,kBAAkB,GAAG;;;;;;;;;;;;;;;;;;;cAmBb,oBAAoB;;CAEjC,CAAC;AAEF;;;GAGG;AACH,MAAM,mBAAmB,GAAG;;;cAGd,oBAAoB;;;CAGjC,CAAC;AAEF;;;GAGG;AACH,MAAM,oBAAoB,GAAG;;;CAG5B,CAAC;AAEF;;;GAGG;AACH,MAAM,mBAAmB,GAAG,kEAAkE,CAAC;AAoB/F;;;;;;;GAOG;AACH;IAC+B,GAAG;IAAhC,YAA6B,GAAkB;mBAAlB,GAAG;IAAkB,CAAC;IAEnD;;;;;;;OAOG;IACH,KAAK,CAAC,QAAQ,CAAC,GAAiB,EAAE,MAAyB,EAAE,OAAO,GAA0B,EAAE;QAC9F,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,CAC1B,GAAG,EACH,gBAAgB,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tC,OAAO,KAAK,CAAC,UAAU,EAAE,IAAI,IAAI,KAAK,CAAC,UAAU,EAAE,cAAc,CAAC;AACpE,CAAC;AAED;;;GAGG;AACH,SAAS,eAAe,CAAC,KAAa;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3D,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC"}
|
|
@@ -81,6 +81,11 @@ export interface FakeGatewayRequest {
|
|
|
81
81
|
* @lastreviewed null
|
|
82
82
|
*/
|
|
83
83
|
idempotencyKey: string | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* The raw `If-Match` header, if any.
|
|
86
|
+
* @lastreviewed null
|
|
87
|
+
*/
|
|
88
|
+
ifMatch: string | undefined;
|
|
84
89
|
}
|
|
85
90
|
/**
|
|
86
91
|
* A canned failure for the next request, served instead of the normal answer.
|
|
@@ -125,6 +130,10 @@ export interface FakeGatewayRoute {
|
|
|
125
130
|
* (via {@link route}) to a git client that brings its own HTTP stack.
|
|
126
131
|
*
|
|
127
132
|
* Built in: `GET /api/v1/knowledge-bases` (scope `kb:read`, `?scope=` and `?tenant=` filters),
|
|
133
|
+
* the global knowledge-base admin routes (`POST /api/v1/knowledge-bases` and `PATCH /api/v1/knowledge-bases/{id}`
|
|
134
|
+
* need `kb:manage`; `GET /api/v1/knowledge-bases/{id}` needs `kb:read`, as the content read does for a private KB),
|
|
135
|
+
* with an `ETag` and a mandatory `If-Match`, `428`/`412`, a strict
|
|
136
|
+
* `access` of `{"kind":"public"|"private"}`, `409 name-taken`; see {@link accessUnsupported}),
|
|
128
137
|
* `GET /api/v1/registry/repos` (scope `registry:read`), the self-service token routes
|
|
129
138
|
* (`GET`/`POST /api/v1/tokens`, `DELETE /api/v1/tokens/{id}`, `POST /api/v1/tokens/{id}/rotate`,
|
|
130
139
|
* scope `tokens:self`, with `scope-escalation` and `alias-taken`), and `404 not-found` for anything else.
|
|
@@ -160,6 +169,12 @@ export declare class FakeGateway {
|
|
|
160
169
|
* @lastreviewed null
|
|
161
170
|
*/
|
|
162
171
|
selfRotationRetryRefused: boolean;
|
|
172
|
+
/**
|
|
173
|
+
* When `true`, simulates a gateway that predates public knowledge bases: a create or change that names
|
|
174
|
+
* `access` is `400 invalid-request`, and no answer carries `access`.
|
|
175
|
+
* @lastreviewed null
|
|
176
|
+
*/
|
|
177
|
+
accessUnsupported: boolean;
|
|
163
178
|
/**
|
|
164
179
|
* How many rotations actually happened (a replay does not count).
|
|
165
180
|
* @lastreviewed null
|
|
@@ -197,6 +212,8 @@ export declare class FakeGateway {
|
|
|
197
212
|
*/
|
|
198
213
|
readonly issuedSecrets: string[];
|
|
199
214
|
private inFlight;
|
|
215
|
+
private readonly kbVersions;
|
|
216
|
+
private kbSerial;
|
|
200
217
|
private readonly tokens;
|
|
201
218
|
private tokenSerial;
|
|
202
219
|
private readonly failures;
|
|
@@ -253,6 +270,13 @@ export declare class FakeGateway {
|
|
|
253
270
|
*/
|
|
254
271
|
stop(): Promise<void>;
|
|
255
272
|
private handle;
|
|
273
|
+
private adminIdOf;
|
|
274
|
+
/**
|
|
275
|
+
* The global knowledge-base admin routes, for a request already authorised for its scope (`kb:read` for `GET`, `kb:manage` otherwise). The knowledge
|
|
276
|
+
* bases are the entries of {@link knowledgeBases}; a version starts at 1 and every change bumps it.
|
|
277
|
+
* @lastreviewed null
|
|
278
|
+
*/
|
|
279
|
+
private handleKbAdmin;
|
|
256
280
|
/**
|
|
257
281
|
* The contract's §Tokens routes, for a request already authorised with `tokens:self`.
|
|
258
282
|
* @lastreviewed null
|
|
@@ -71,6 +71,10 @@ function isExpired(expiresAt) {
|
|
|
71
71
|
* (via {@link route}) to a git client that brings its own HTTP stack.
|
|
72
72
|
*
|
|
73
73
|
* Built in: `GET /api/v1/knowledge-bases` (scope `kb:read`, `?scope=` and `?tenant=` filters),
|
|
74
|
+
* the global knowledge-base admin routes (`POST /api/v1/knowledge-bases` and `PATCH /api/v1/knowledge-bases/{id}`
|
|
75
|
+
* need `kb:manage`; `GET /api/v1/knowledge-bases/{id}` needs `kb:read`, as the content read does for a private KB),
|
|
76
|
+
* with an `ETag` and a mandatory `If-Match`, `428`/`412`, a strict
|
|
77
|
+
* `access` of `{"kind":"public"|"private"}`, `409 name-taken`; see {@link accessUnsupported}),
|
|
74
78
|
* `GET /api/v1/registry/repos` (scope `registry:read`), the self-service token routes
|
|
75
79
|
* (`GET`/`POST /api/v1/tokens`, `DELETE /api/v1/tokens/{id}`, `POST /api/v1/tokens/{id}/rotate`,
|
|
76
80
|
* scope `tokens:self`, with `scope-escalation` and `alias-taken`), and `404 not-found` for anything else.
|
|
@@ -102,6 +106,12 @@ class FakeGateway {
|
|
|
102
106
|
* @lastreviewed null
|
|
103
107
|
*/
|
|
104
108
|
selfRotationRetryRefused = false;
|
|
109
|
+
/**
|
|
110
|
+
* When `true`, simulates a gateway that predates public knowledge bases: a create or change that names
|
|
111
|
+
* `access` is `400 invalid-request`, and no answer carries `access`.
|
|
112
|
+
* @lastreviewed null
|
|
113
|
+
*/
|
|
114
|
+
accessUnsupported = false;
|
|
105
115
|
/**
|
|
106
116
|
* How many rotations actually happened (a replay does not count).
|
|
107
117
|
* @lastreviewed null
|
|
@@ -139,6 +149,8 @@ class FakeGateway {
|
|
|
139
149
|
*/
|
|
140
150
|
issuedSecrets = [];
|
|
141
151
|
inFlight = 0;
|
|
152
|
+
kbVersions = new Map();
|
|
153
|
+
kbSerial = 0;
|
|
142
154
|
tokens = new Map();
|
|
143
155
|
tokenSerial = 0;
|
|
144
156
|
failures = [];
|
|
@@ -270,6 +282,7 @@ class FakeGateway {
|
|
|
270
282
|
authorization: req.headers.authorization,
|
|
271
283
|
body,
|
|
272
284
|
idempotencyKey: headerOf(req, "idempotency-key"),
|
|
285
|
+
ifMatch: headerOf(req, "if-match"),
|
|
273
286
|
});
|
|
274
287
|
const at = this.failures.findIndex((f) => f.method === undefined || f.method === req.method);
|
|
275
288
|
const failure = at < 0 ? undefined : this.failures.splice(at, 1)[0];
|
|
@@ -298,6 +311,12 @@ class FakeGateway {
|
|
|
298
311
|
sendJson(res, 200, list);
|
|
299
312
|
return;
|
|
300
313
|
}
|
|
314
|
+
if (url.pathname === Gateway_1.Gateway.Paths.KNOWLEDGE_BASES ? method === "POST" : this.adminIdOf(url.pathname) !== null) {
|
|
315
|
+
if (this.authorize(req, res, method === "GET" ? Gateway_1.Gateway.Scopes.KB_READ : Gateway_1.Gateway.Scopes.KB_MANAGE)) {
|
|
316
|
+
this.handleKbAdmin(method, url.pathname, body, req, res);
|
|
317
|
+
}
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
301
320
|
if (method === "GET" && url.pathname === Gateway_1.Gateway.Paths.REGISTRY_REPOS) {
|
|
302
321
|
if (!this.authorize(req, res, Gateway_1.Gateway.Scopes.REGISTRY_READ)) {
|
|
303
322
|
return;
|
|
@@ -324,6 +343,163 @@ class FakeGateway {
|
|
|
324
343
|
this.inFlight--;
|
|
325
344
|
}
|
|
326
345
|
}
|
|
346
|
+
adminIdOf(path) {
|
|
347
|
+
const prefix = `${Gateway_1.Gateway.Paths.KNOWLEDGE_BASES}/`;
|
|
348
|
+
if (!path.startsWith(prefix)) {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
const rest = path.slice(prefix.length);
|
|
352
|
+
return rest.length > 0 && !rest.includes("/") ? decodeURIComponent(rest) : null;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The global knowledge-base admin routes, for a request already authorised for its scope (`kb:read` for `GET`, `kb:manage` otherwise). The knowledge
|
|
356
|
+
* bases are the entries of {@link knowledgeBases}; a version starts at 1 and every change bumps it.
|
|
357
|
+
* @lastreviewed null
|
|
358
|
+
*/
|
|
359
|
+
handleKbAdmin(method, path, body, req, res) {
|
|
360
|
+
const invalid = (message) => sendJson(res, 400, { error: "invalid-request", message });
|
|
361
|
+
const detail = (kb) => ({
|
|
362
|
+
id: kb.id,
|
|
363
|
+
ref: kb.ref,
|
|
364
|
+
scope: "global",
|
|
365
|
+
name: kb.name,
|
|
366
|
+
title: kb.title,
|
|
367
|
+
liveBranch: kb.liveBranch,
|
|
368
|
+
gitUrl: kb.gitUrl,
|
|
369
|
+
...(this.accessUnsupported || kb.access === undefined ? {} : { access: kb.access }),
|
|
370
|
+
});
|
|
371
|
+
const send = (status, kb) => {
|
|
372
|
+
const headers = { "Content-Type": "application/json" };
|
|
373
|
+
if (kb.id !== null) {
|
|
374
|
+
headers.ETag = `"${this.kbVersions.get(kb.id) ?? 1}"`;
|
|
375
|
+
}
|
|
376
|
+
res.writeHead(status, headers);
|
|
377
|
+
res.end(JSON.stringify(detail(kb)));
|
|
378
|
+
};
|
|
379
|
+
let parsed;
|
|
380
|
+
try {
|
|
381
|
+
parsed = body.length === 0 ? {} : JSON.parse(body);
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
invalid("The body is not JSON.");
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const fields = typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : undefined;
|
|
388
|
+
const record = (fields ?? {});
|
|
389
|
+
// access is strict: exactly {"kind":"public"|"private"}, and refused outright by an older gateway.
|
|
390
|
+
const checkAccess = () => {
|
|
391
|
+
if (record.access === undefined) {
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
const access = record.access;
|
|
395
|
+
if (this.accessUnsupported ||
|
|
396
|
+
typeof access !== "object" ||
|
|
397
|
+
access === null ||
|
|
398
|
+
Object.keys(access).length !== 1 ||
|
|
399
|
+
(access.kind !== "public" && access.kind !== "private")) {
|
|
400
|
+
invalid('access must be {"kind":"public"} or {"kind":"private"}.');
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
return { kind: access.kind };
|
|
404
|
+
};
|
|
405
|
+
const text = (field) => {
|
|
406
|
+
const value = record[field];
|
|
407
|
+
if (value === undefined) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
411
|
+
invalid(`${field} must be non-empty text.`);
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
return value;
|
|
415
|
+
};
|
|
416
|
+
if (fields === undefined) {
|
|
417
|
+
invalid("The body must be a JSON object.");
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (method === "POST" && path === Gateway_1.Gateway.Paths.KNOWLEDGE_BASES) {
|
|
421
|
+
const name = text("name");
|
|
422
|
+
const title = text("title");
|
|
423
|
+
const liveBranch = text("liveBranch");
|
|
424
|
+
const access = checkAccess();
|
|
425
|
+
if (name === null || title === null || liveBranch === null || access === null) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (name === undefined) {
|
|
429
|
+
invalid("name is required.");
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (this.knowledgeBases.some((kb) => kb.scope === "global" && kb.name.toLowerCase() === name.toLowerCase())) {
|
|
433
|
+
sendJson(res, 409, { error: "name-taken", message: "A knowledge base has that name." });
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const id = `00000000-0000-4000-8000-${String(++this.kbSerial).padStart(12, "0")}`;
|
|
437
|
+
const kb = {
|
|
438
|
+
id,
|
|
439
|
+
status: "ok",
|
|
440
|
+
ref: `global/${name}`,
|
|
441
|
+
scope: "global",
|
|
442
|
+
tenant: null,
|
|
443
|
+
name,
|
|
444
|
+
title: title ?? name,
|
|
445
|
+
liveBranch: liveBranch ?? "main",
|
|
446
|
+
canPush: true,
|
|
447
|
+
gitUrl: `${this.url}/git/global/${name}.git`,
|
|
448
|
+
access: access ?? { kind: "private" },
|
|
449
|
+
};
|
|
450
|
+
this.knowledgeBases.push(kb);
|
|
451
|
+
this.kbVersions.set(id, 1);
|
|
452
|
+
send(201, kb);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const id = this.adminIdOf(path);
|
|
456
|
+
const kb = this.knowledgeBases.find((candidate) => candidate.scope === "global" && candidate.id === id);
|
|
457
|
+
if (kb === undefined) {
|
|
458
|
+
sendJson(res, 404, { error: "not-found", message: "Not found." });
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
if (method === "GET") {
|
|
462
|
+
send(200, kb);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (method === "PATCH") {
|
|
466
|
+
const ifMatch = headerOf(req, "if-match");
|
|
467
|
+
if (ifMatch === undefined) {
|
|
468
|
+
sendJson(res, 428, { error: "precondition-required", message: "This change needs an If-Match header." });
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
if (kb.id !== null &&
|
|
472
|
+
ifMatch.replace(/^W\//, "").replace(/^"|"$/g, "") !== String(this.kbVersions.get(kb.id) ?? 1)) {
|
|
473
|
+
sendJson(res, 412, { error: "precondition-failed", message: "The knowledge base changed since it was read." });
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const title = text("title");
|
|
477
|
+
const liveBranch = text("liveBranch");
|
|
478
|
+
const access = checkAccess();
|
|
479
|
+
if (title === null || liveBranch === null || access === null) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (Object.keys(record).some((key) => key !== "title" && key !== "liveBranch" && key !== "access")) {
|
|
483
|
+
invalid("An unknown field was sent.");
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (title !== undefined) {
|
|
487
|
+
kb.title = title;
|
|
488
|
+
}
|
|
489
|
+
if (liveBranch !== undefined) {
|
|
490
|
+
kb.liveBranch = liveBranch;
|
|
491
|
+
}
|
|
492
|
+
if (access !== undefined) {
|
|
493
|
+
kb.access = access;
|
|
494
|
+
}
|
|
495
|
+
if (kb.id !== null) {
|
|
496
|
+
this.kbVersions.set(kb.id, (this.kbVersions.get(kb.id) ?? 1) + 1);
|
|
497
|
+
}
|
|
498
|
+
send(200, kb);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
sendJson(res, 404, { error: "not-found", message: "Not found." });
|
|
502
|
+
}
|
|
327
503
|
/**
|
|
328
504
|
* The contract's §Tokens routes, for a request already authorised with `tokens:self`.
|
|
329
505
|
* @lastreviewed null
|