@coveo/push-api-client 1.1.28 → 1.2.1
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/definitions/help/file.d.ts +3 -0
- package/dist/definitions/help/file.spec.d.ts +1 -0
- package/dist/definitions/help/generator.d.ts +11 -0
- package/dist/definitions/help/generator.spec.d.ts +1 -0
- package/dist/definitions/source.d.ts +40 -5
- package/dist/definitions/validation/caseInsensitiveDocument.d.ts +4 -0
- package/dist/definitions/validation/knownKey.d.ts +9 -0
- package/dist/definitions/validation/parseFile.d.ts +3 -0
- package/dist/definitions/validation/parseFile.spec.d.ts +1 -0
- package/dist/definitions/validation/requiredKeyValidator.d.ts +17 -0
- package/dist/definitions/validation/validatorErrors.d.ts +22 -0
- package/dist/docs/assets/search.js +1 -1
- package/dist/docs/classes/AnySecurityIdentityBuilder.html +2 -2
- package/dist/docs/classes/DocumentBuilder.html +16 -16
- package/dist/docs/classes/GroupSecurityIdentityBuilder.html +2 -2
- package/dist/docs/classes/Source.html +23 -13
- package/dist/docs/classes/UserSecurityIdentityBuilder.html +2 -2
- package/dist/docs/classes/VirtualGroupSecurityIdentityBuilder.html +2 -2
- package/dist/docs/interfaces/BatchUpdateDocuments.html +1 -1
- package/dist/docs/interfaces/Document.html +13 -13
- package/dist/docs/interfaces/SecurityIdentity.html +3 -3
- package/dist/docs/interfaces/SecurityIdentityBuilder.html +1 -1
- package/dist/docs/modules.html +2 -2
- package/dist/help/file.js +23 -0
- package/dist/help/file.js.map +1 -0
- package/dist/help/file.spec.js +35 -0
- package/dist/help/file.spec.js.map +1 -0
- package/dist/help/generator.js +33 -0
- package/dist/help/generator.js.map +1 -0
- package/dist/help/generator.spec.js +48 -0
- package/dist/help/generator.spec.js.map +1 -0
- package/dist/source.js +83 -0
- package/dist/source.js.map +1 -1
- package/dist/source.spec.js +55 -2
- package/dist/source.spec.js.map +1 -1
- package/dist/validation/caseInsensitiveDocument.js +13 -0
- package/dist/validation/caseInsensitiveDocument.js.map +1 -0
- package/dist/validation/knownKey.js +29 -0
- package/dist/validation/knownKey.js.map +1 -0
- package/dist/validation/parseFile.js +144 -0
- package/dist/validation/parseFile.js.map +1 -0
- package/dist/validation/parseFile.spec.js +50 -0
- package/dist/validation/parseFile.spec.js.map +1 -0
- package/dist/validation/requiredKeyValidator.js +62 -0
- package/dist/validation/requiredKeyValidator.js.map +1 -0
- package/dist/validation/validatorErrors.js +39 -0
- package/dist/validation/validatorErrors.js.map +1 -0
- package/package.json +9 -8
package/dist/source.js
CHANGED
@@ -10,6 +10,10 @@ Object.defineProperty(exports, "SourceVisibility", { enumerable: true, get: func
|
|
10
10
|
const axios_1 = require("axios");
|
11
11
|
const dayjs = require("dayjs");
|
12
12
|
const url_1 = require("url");
|
13
|
+
const generator_1 = require("./help/generator");
|
14
|
+
const parseFile_1 = require("./validation/parseFile");
|
15
|
+
const path_1 = require("path");
|
16
|
+
const file_1 = require("./help/file");
|
13
17
|
/**
|
14
18
|
* Manage a push source.
|
15
19
|
*
|
@@ -109,6 +113,34 @@ class Source {
|
|
109
113
|
await this.uploadContentToFileContainer(fileContainer, batch);
|
110
114
|
return this.pushFileContainerContent(sourceID, fileContainer);
|
111
115
|
}
|
116
|
+
/**
|
117
|
+
*
|
118
|
+
* Manage batches of items in a push source from a list of JSON files. See [Manage Batches of Items in a Push Source](https://docs.coveo.com/en/90)
|
119
|
+
* @param {string} sourceID The unique identifier of the target Push source
|
120
|
+
* @param {string[]} filesOrDirectories A list of JSON files or directories (containing JSON files) from which to extract documents.
|
121
|
+
* @param {UploadBatchCallback} callback Callback executed when a batch of documents is either successfully uploaded or when an error occurs during the upload
|
122
|
+
* @param {BatchUpdateDocumentsFromFiles} [{maxConcurrent = 10}={}]
|
123
|
+
*/
|
124
|
+
async batchUpdateDocumentsFromFiles(sourceID, filesOrDirectories, callback, { maxConcurrent = 10 } = {}) {
|
125
|
+
const files = (0, file_1.getAllJsonFilesFromEntries)(filesOrDirectories);
|
126
|
+
const fileNames = files.map((path) => (0, path_1.basename)(path));
|
127
|
+
const { chunksToUpload, close } = this.splitByChunkAndUpload(sourceID, fileNames, callback);
|
128
|
+
// parallelize uploads within the same file
|
129
|
+
const docBuilderGenerator = function* (docBuilders) {
|
130
|
+
for (const upload of chunksToUpload(docBuilders)) {
|
131
|
+
yield upload();
|
132
|
+
}
|
133
|
+
};
|
134
|
+
// parallelize uploads across multiple files
|
135
|
+
const fileGenerator = function* () {
|
136
|
+
for (const filePath of files.values()) {
|
137
|
+
const docBuilders = (0, parseFile_1.parseAndGetDocumentBuilderFromJSONDocument)(filePath);
|
138
|
+
yield* docBuilderGenerator(docBuilders);
|
139
|
+
}
|
140
|
+
};
|
141
|
+
await (0, generator_1.consumeGenerator)(fileGenerator.bind(this), maxConcurrent);
|
142
|
+
await close();
|
143
|
+
}
|
112
144
|
/**
|
113
145
|
* Deletes a specific item from a Push source. Optionally, the child items of that item can also be deleted. See [Deleting an Item in a Push Source](https://docs.coveo.com/en/171).
|
114
146
|
* @param sourceID
|
@@ -186,6 +218,57 @@ class Source {
|
|
186
218
|
pushURL.searchParams.append('fileId', fileContainer.fileId);
|
187
219
|
return axios_1.default.put(pushURL.toString(), {}, this.documentsAxiosConfig);
|
188
220
|
}
|
221
|
+
splitByChunkAndUpload(sourceID, fileNames, callback, accumulator = this.accumulator) {
|
222
|
+
const chunksToUpload = (documentBuilders) => {
|
223
|
+
const batchesToUpload = [];
|
224
|
+
for (const docBuilder of documentBuilders) {
|
225
|
+
const sizeOfDoc = Buffer.byteLength(JSON.stringify(docBuilder.marshal()));
|
226
|
+
if (accumulator.size + sizeOfDoc >= Source.maxContentLength) {
|
227
|
+
const chunks = accumulator.chunks;
|
228
|
+
if (chunks.length > 0) {
|
229
|
+
batchesToUpload.push(() => this.uploadBatch(sourceID, chunks, fileNames, callback));
|
230
|
+
}
|
231
|
+
accumulator.chunks = [docBuilder];
|
232
|
+
accumulator.size = sizeOfDoc;
|
233
|
+
}
|
234
|
+
else {
|
235
|
+
accumulator.size += sizeOfDoc;
|
236
|
+
accumulator.chunks.push(docBuilder);
|
237
|
+
}
|
238
|
+
}
|
239
|
+
return batchesToUpload;
|
240
|
+
};
|
241
|
+
const close = async () => {
|
242
|
+
await this.uploadBatch(sourceID, accumulator.chunks, fileNames, callback);
|
243
|
+
};
|
244
|
+
return { chunksToUpload, close };
|
245
|
+
}
|
246
|
+
async uploadBatch(sourceID, batch, fileNames, callback) {
|
247
|
+
try {
|
248
|
+
const res = await this.batchUpdateDocuments(sourceID, {
|
249
|
+
addOrUpdate: batch,
|
250
|
+
delete: [],
|
251
|
+
});
|
252
|
+
callback(null, {
|
253
|
+
files: fileNames,
|
254
|
+
batch,
|
255
|
+
res,
|
256
|
+
});
|
257
|
+
}
|
258
|
+
catch (e) {
|
259
|
+
callback(e, {
|
260
|
+
files: fileNames,
|
261
|
+
batch,
|
262
|
+
});
|
263
|
+
}
|
264
|
+
}
|
265
|
+
get accumulator() {
|
266
|
+
return {
|
267
|
+
size: 0,
|
268
|
+
chunks: [],
|
269
|
+
};
|
270
|
+
}
|
189
271
|
}
|
190
272
|
exports.Source = Source;
|
273
|
+
Source.maxContentLength = 5 * 1024 * 1024;
|
191
274
|
//# sourceMappingURL=source.js.map
|
package/dist/source.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":";;;AAAA,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAC5B,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAEpC,8DAUkC;AAClC,4DAAuE;AAA/D,8GAAA,WAAW,OAAA;AAAE,mHAAA,gBAAgB,OAAA;AACrC,
|
1
|
+
{"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":";;;AAAA,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAC5B,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAEpC,8DAUkC;AAClC,4DAAuE;A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AgB,EAChB,KAAwB,EACxB,SAAmB,EACnB,QAA6B;QAE7B,IAAI;YACF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,QAAQ,EAAE;gBACpD,WAAW,EAAE,KAAK;gBAClB,MAAM,EAAE,EAAE;aACX,CAAC,CAAC;YACH,QAAQ,CAAC,IAAI,EAAE;gBACb,KAAK,EAAE,SAAS;gBAChB,KAAK;gBACL,GAAG;aACJ,CAAC,CAAC;SACJ;QAAC,OAAO,CAAU,EAAE;YACnB,QAAQ,CAAC,CAAC,EAAE;gBACV,KAAK,EAAE,SAAS;gBAChB,KAAK;aACN,CAAC,CAAC;SACJ;IACH,CAAC;IAED,IAAY,WAAW;QACrB,OAAO;YACL,IAAI,EAAE,CAAC;YACP,MAAM,EAAE,EAAE;SACX,CAAC;IACJ,CAAC;;AAjXH,wBAkXC;AAhXgB,uBAAgB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC"}
|
package/dist/source.spec.js
CHANGED
@@ -5,12 +5,14 @@ jest.mock('@coveord/platform-client');
|
|
5
5
|
jest.mock('axios');
|
6
6
|
const platform_client_1 = require("@coveord/platform-client");
|
7
7
|
const source_1 = require("./source");
|
8
|
-
const utils_1 = require("ts-jest/utils");
|
9
8
|
const documentBuilder_1 = require("./documentBuilder");
|
10
9
|
const axios_1 = require("axios");
|
10
|
+
const path_1 = require("path");
|
11
|
+
const process_1 = require("process");
|
11
12
|
const mockAxios = axios_1.default;
|
12
|
-
const mockedPlatformClient =
|
13
|
+
const mockedPlatformClient = jest.mocked(platform_client_1.default);
|
13
14
|
const mockCreate = jest.fn();
|
15
|
+
const pathToStub = (0, path_1.join)((0, process_1.cwd)(), 'src', '__stub__');
|
14
16
|
mockedPlatformClient.mockImplementation(() => ({
|
15
17
|
source: {
|
16
18
|
create: mockCreate,
|
@@ -116,5 +118,56 @@ describe('Source', () => {
|
|
116
118
|
expect(mockAxios.put).toHaveBeenCalledWith('https://api.cloud.coveo.com/push/v1/organizations/the_org/sources/the_id/documents/batch?fileId=file_id', {}, expectedDocumentsHeaders);
|
117
119
|
});
|
118
120
|
});
|
121
|
+
describe('when doing batch update from local files', () => {
|
122
|
+
const mockedCallback = jest.fn();
|
123
|
+
beforeEach(() => {
|
124
|
+
mockAxios.post.mockImplementationOnce((url) => {
|
125
|
+
if (url.match(/files/)) {
|
126
|
+
return Promise.resolve({
|
127
|
+
data: {
|
128
|
+
uploadUri: 'https://fake.upload.url',
|
129
|
+
fileId: 'file_id',
|
130
|
+
requiredHeaders: { foo: 'bar' },
|
131
|
+
},
|
132
|
+
});
|
133
|
+
}
|
134
|
+
return Promise.resolve();
|
135
|
+
});
|
136
|
+
});
|
137
|
+
it('should upload documents from local file', async () => {
|
138
|
+
await source.batchUpdateDocumentsFromFiles('the_id', [(0, path_1.join)(pathToStub, 'mixdocuments')], mockedCallback);
|
139
|
+
expect(mockAxios.put).toHaveBeenCalledWith('https://fake.upload.url/', expect.objectContaining({
|
140
|
+
addOrUpdate: expect.arrayContaining([
|
141
|
+
expect.objectContaining({
|
142
|
+
documentId: 'https://www.themoviedb.org/movie/268',
|
143
|
+
}),
|
144
|
+
expect.objectContaining({
|
145
|
+
documentId: 'https://www.themoviedb.org/movie/999',
|
146
|
+
}),
|
147
|
+
]),
|
148
|
+
delete: expect.arrayContaining([]),
|
149
|
+
}), {
|
150
|
+
headers: {
|
151
|
+
foo: 'bar',
|
152
|
+
},
|
153
|
+
});
|
154
|
+
});
|
155
|
+
it('should call the callback without error when uploading documents', async () => {
|
156
|
+
await source.batchUpdateDocumentsFromFiles('the_id', [(0, path_1.join)(pathToStub, 'mixdocuments')], mockedCallback);
|
157
|
+
expect(mockedCallback).toHaveBeenCalledWith(null, expect.anything());
|
158
|
+
});
|
159
|
+
it('should only push JSON files', async () => {
|
160
|
+
await source.batchUpdateDocumentsFromFiles('the_id', [(0, path_1.join)(pathToStub, 'mixdocuments')], mockedCallback);
|
161
|
+
expect(mockedCallback).toHaveBeenCalledWith(null, expect.objectContaining({ files: ['valid.json'] }));
|
162
|
+
});
|
163
|
+
it('should call the errorCallback on a failure from the API', async () => {
|
164
|
+
mockAxios.post.mockReset();
|
165
|
+
mockAxios.post.mockRejectedValue({ message: 'Error Message' });
|
166
|
+
await source.batchUpdateDocumentsFromFiles('the_id', [(0, path_1.join)(pathToStub, 'mixdocuments')], mockedCallback);
|
167
|
+
expect(mockedCallback).toHaveBeenCalledWith({
|
168
|
+
message: 'Error Message',
|
169
|
+
}, expect.anything());
|
170
|
+
});
|
171
|
+
});
|
119
172
|
});
|
120
173
|
//# sourceMappingURL=source.spec.js.map
|
package/dist/source.spec.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"source.spec.js","sourceRoot":"","sources":["../src/source.spec.ts"],"names":[],"mappings":";;AAAA,+CAA+C;AAC/C,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;AACtC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACnB,8DAA0E;AAC1E,qCAAsD;AACtD,
|
1
|
+
{"version":3,"file":"source.spec.js","sourceRoot":"","sources":["../src/source.spec.ts"],"names":[],"mappings":";;AAAA,+CAA+C;AAC/C,IAAI,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;AACtC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACnB,8DAA0E;AAC1E,qCAAsD;AACtD,uDAAkD;AAClD,iCAA0B;AAC1B,+BAA0B;AAC1B,qCAA4B;AAC5B,MAAM,SAAS,GAAG,eAAkC,CAAC;AAErD,MAAM,oBAAoB,GAAG,IAAI,CAAC,MAAM,CAAC,yBAAc,CAAC,CAAC;AACzD,MAAM,UAAU,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;AAC7B,MAAM,UAAU,GAAG,IAAA,WAAI,EAAC,IAAA,aAAG,GAAE,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;AAClD,oBAAoB,CAAC,kBAAkB,CACrC,GAAG,E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C;QACvE,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,6BAA6B,EAAE,KAAK,IAAI,EAAE;YAC3C,MAAM,MAAM,CAAC,6BAA6B,CACxC,QAAQ,EACR,CAAC,IAAA,WAAI,EAAC,UAAU,EAAE,cAAc,CAAC,CAAC,EAClC,cAAc,CACf,CAAC;YAEF,MAAM,CAAC,cAAc,CAAC,CAAC,oBAAoB,CACzC,IAAI,EACJ,MAAM,CAAC,gBAAgB,CAAC,EAAC,KAAK,EAAE,CAAC,YAAY,CAAC,EAAC,CAAC,CACjD,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,yDAAyD,EAAE,KAAK,IAAI,EAAE;YACvE,SAAS,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YAC3B,SAAS,CAAC,IAAI,CAAC,iBAAiB,CAAC,EAAC,OAAO,EAAE,eAAe,EAAC,CAAC,CAAC;YAE7D,MAAM,MAAM,CAAC,6BAA6B,CACxC,QAAQ,EACR,CAAC,IAAA,WAAI,EAAC,UAAU,EAAE,cAAc,CAAC,CAAC,EAClC,cAAc,CACf,CAAC;YACF,MAAM,CAAC,cAAc,CAAC,CAAC,oBAAoB,CACzC;gBACE,OAAO,EAAE,eAAe;aACzB,EACD,MAAM,CAAC,QAAQ,EAAE,CAClB,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.CaseInsensitiveDocument = void 0;
|
4
|
+
class CaseInsensitiveDocument {
|
5
|
+
constructor(doc) {
|
6
|
+
this.documentRecord = {};
|
7
|
+
Object.entries(doc).forEach(([k, v]) => {
|
8
|
+
this.documentRecord[k.toLowerCase()] = v;
|
9
|
+
});
|
10
|
+
}
|
11
|
+
}
|
12
|
+
exports.CaseInsensitiveDocument = CaseInsensitiveDocument;
|
13
|
+
//# sourceMappingURL=caseInsensitiveDocument.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caseInsensitiveDocument.js","sourceRoot":"","sources":["../../src/validation/caseInsensitiveDocument.ts"],"names":[],"mappings":";;;AAAA,MAAa,uBAAuB;IAElC,YAAmB,GAAsB;QADlC,mBAAc,GAAsB,EAAE,CAAC;QAE5C,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE;YACrC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAPD,0DAOC"}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.KnownKeys = void 0;
|
4
|
+
const bueno_1 = require("@coveo/bueno");
|
5
|
+
class KnownKeys {
|
6
|
+
constructor(k, doc) {
|
7
|
+
this.doc = doc;
|
8
|
+
if (Array.isArray(k)) {
|
9
|
+
this.keys = k;
|
10
|
+
}
|
11
|
+
else {
|
12
|
+
this.keys = [k];
|
13
|
+
}
|
14
|
+
}
|
15
|
+
get value() {
|
16
|
+
const found = this.keys.find((k) => !(0, bueno_1.isNullOrUndefined)(this.doc.documentRecord[k.toLowerCase()]));
|
17
|
+
if (found) {
|
18
|
+
return this.doc.documentRecord[found.toLowerCase()];
|
19
|
+
}
|
20
|
+
return null;
|
21
|
+
}
|
22
|
+
whenExists(cb) {
|
23
|
+
if (!(0, bueno_1.isNullOrUndefined)(this.value)) {
|
24
|
+
cb(this.value);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
exports.KnownKeys = KnownKeys;
|
29
|
+
//# sourceMappingURL=knownKey.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"knownKey.js","sourceRoot":"","sources":["../../src/validation/knownKey.ts"],"names":[],"mappings":";;;AAAA,wCAAiE;AAGjE,MAAa,SAAS;IAEpB,YACE,CAAoB,EACZ,GAA8C;QAA9C,QAAG,GAAH,GAAG,CAA2C;QAEtD,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACpB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SACf;aAAM;YACL,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;SACjB;IACH,CAAC;IAED,IAAW,KAAK;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAC1B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAA,yBAAiB,EAAC,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CACpE,CAAC;QACF,IAAI,KAAK,EAAE;YACT,OAAO,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;SACrD;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,UAAU,CAAC,EAAkB;QAClC,IAAI,CAAC,IAAA,yBAAiB,EAAC,IAAI,CAAC,KAAK,CAAC,EAAE;YAClC,EAAE,CAAC,IAAI,CAAC,KAAU,CAAC,CAAC;SACrB;IACH,CAAC;CACF;AA7BD,8BA6BC"}
|
@@ -0,0 +1,144 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.parseAndGetDocumentBuilderFromJSONDocument = void 0;
|
4
|
+
const bueno_1 = require("@coveo/bueno");
|
5
|
+
const __1 = require("..");
|
6
|
+
const fs_1 = require("fs");
|
7
|
+
const caseInsensitiveDocument_1 = require("./caseInsensitiveDocument");
|
8
|
+
const knownKey_1 = require("./knownKey");
|
9
|
+
const validatorErrors_1 = require("./validatorErrors");
|
10
|
+
const requiredKeyValidator_1 = require("./requiredKeyValidator");
|
11
|
+
const parseAndGetDocumentBuilderFromJSONDocument = (documentPath) => {
|
12
|
+
ensureFileIntegrity(documentPath);
|
13
|
+
const fileContent = safeJSONParse(documentPath);
|
14
|
+
if (Array.isArray(fileContent)) {
|
15
|
+
return fileContent.map((doc) => processDocument(doc, documentPath));
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
return [processDocument(fileContent, documentPath)];
|
19
|
+
}
|
20
|
+
};
|
21
|
+
exports.parseAndGetDocumentBuilderFromJSONDocument = parseAndGetDocumentBuilderFromJSONDocument;
|
22
|
+
const safeJSONParse = (documentPath) => {
|
23
|
+
const fileContent = (0, fs_1.readFileSync)(documentPath).toString();
|
24
|
+
try {
|
25
|
+
return JSON.parse(fileContent);
|
26
|
+
}
|
27
|
+
catch (error) {
|
28
|
+
throw new validatorErrors_1.NotAJsonFileError(documentPath);
|
29
|
+
}
|
30
|
+
};
|
31
|
+
const processDocument = (fileContent, documentPath) => {
|
32
|
+
const caseInsensitiveDoc = new caseInsensitiveDocument_1.CaseInsensitiveDocument(fileContent);
|
33
|
+
const documentBuilder = validateRequiredKeysAndGetDocumentBuilder(caseInsensitiveDoc, documentPath);
|
34
|
+
processKnownKeys(caseInsensitiveDoc, documentBuilder);
|
35
|
+
processSecurityIdentities(caseInsensitiveDoc, documentBuilder, documentPath);
|
36
|
+
processMetadata(caseInsensitiveDoc, documentBuilder);
|
37
|
+
return documentBuilder;
|
38
|
+
};
|
39
|
+
const validateRequiredKeysAndGetDocumentBuilder = (caseInsensitiveDoc, documentPath) => {
|
40
|
+
const requiredDocumentId = new requiredKeyValidator_1.RequiredKeyValidator(['documentid', 'uri'], caseInsensitiveDoc, new bueno_1.StringValue({ required: true, emptyAllowed: false }));
|
41
|
+
if (!requiredDocumentId.isValid) {
|
42
|
+
throw new validatorErrors_1.InvalidDocument(documentPath, requiredDocumentId.explanation);
|
43
|
+
}
|
44
|
+
const requiredDocumentTitle = new requiredKeyValidator_1.RequiredKeyValidator('title', caseInsensitiveDoc, new bueno_1.StringValue({ required: true, emptyAllowed: false }));
|
45
|
+
if (!requiredDocumentTitle.isValid) {
|
46
|
+
throw new validatorErrors_1.InvalidDocument(documentPath, requiredDocumentTitle.explanation);
|
47
|
+
}
|
48
|
+
delete caseInsensitiveDoc.documentRecord['documentid'];
|
49
|
+
delete caseInsensitiveDoc.documentRecord['uri'];
|
50
|
+
delete caseInsensitiveDoc.documentRecord['title'];
|
51
|
+
return new __1.DocumentBuilder(requiredDocumentId.value, requiredDocumentTitle.value);
|
52
|
+
};
|
53
|
+
const processKnownKeys = (caseInsensitiveDoc, documentBuilder) => {
|
54
|
+
new knownKey_1.KnownKeys('author', caseInsensitiveDoc).whenExists((author) => {
|
55
|
+
documentBuilder.withAuthor(author);
|
56
|
+
delete caseInsensitiveDoc.documentRecord['author'];
|
57
|
+
});
|
58
|
+
new knownKey_1.KnownKeys('clickableuri', caseInsensitiveDoc).whenExists((clickuri) => {
|
59
|
+
documentBuilder.withClickableUri(clickuri);
|
60
|
+
delete caseInsensitiveDoc.documentRecord['clickableuri'];
|
61
|
+
});
|
62
|
+
new knownKey_1.KnownKeys('data', caseInsensitiveDoc).whenExists((data) => {
|
63
|
+
documentBuilder.withData(data);
|
64
|
+
delete caseInsensitiveDoc.documentRecord['data'];
|
65
|
+
});
|
66
|
+
new knownKey_1.KnownKeys('date', caseInsensitiveDoc).whenExists((date) => {
|
67
|
+
documentBuilder.withDate(date);
|
68
|
+
delete caseInsensitiveDoc.documentRecord['date'];
|
69
|
+
});
|
70
|
+
new knownKey_1.KnownKeys('modifieddate', caseInsensitiveDoc).whenExists((modifiedDate) => {
|
71
|
+
documentBuilder.withModifiedDate(modifiedDate);
|
72
|
+
delete caseInsensitiveDoc.documentRecord['modifieddate'];
|
73
|
+
});
|
74
|
+
new knownKey_1.KnownKeys('fileextension', caseInsensitiveDoc).whenExists((fileExtension) => {
|
75
|
+
documentBuilder.withFileExtension(fileExtension);
|
76
|
+
delete caseInsensitiveDoc.documentRecord['fileextension'];
|
77
|
+
});
|
78
|
+
new knownKey_1.KnownKeys('permanentid', caseInsensitiveDoc).whenExists((permanentId) => {
|
79
|
+
documentBuilder.withPermanentId(permanentId);
|
80
|
+
delete caseInsensitiveDoc.documentRecord['permanentid'];
|
81
|
+
});
|
82
|
+
};
|
83
|
+
const processSecurityIdentities = (caseInsensitiveDoc, documentBuilder, documentPath) => {
|
84
|
+
new knownKey_1.KnownKeys('permissions', caseInsensitiveDoc).whenExists((permissions) => {
|
85
|
+
var _a, _b;
|
86
|
+
const caseInsensitivePermissions = new caseInsensitiveDocument_1.CaseInsensitiveDocument(permissions);
|
87
|
+
const requiredAllowAnonymous = new requiredKeyValidator_1.RequiredKeyValidator('allowanonymous', caseInsensitivePermissions, new bueno_1.BooleanValue({ required: true }));
|
88
|
+
if (!requiredAllowAnonymous.isValid) {
|
89
|
+
throw new validatorErrors_1.InvalidDocument(documentPath, requiredAllowAnonymous.explanation);
|
90
|
+
}
|
91
|
+
const requiredAllowedPermissions = new requiredKeyValidator_1.RequiredKeyValidator('allowedpermissions', caseInsensitivePermissions, getSecurityIdentitySchemaValidation());
|
92
|
+
if (!requiredAllowedPermissions.isValid) {
|
93
|
+
throw new validatorErrors_1.InvalidDocument(documentPath, requiredAllowedPermissions.explanation);
|
94
|
+
}
|
95
|
+
const requiredDeniedPermissions = new requiredKeyValidator_1.RequiredKeyValidator('deniedpermissions', caseInsensitivePermissions, getSecurityIdentitySchemaValidation());
|
96
|
+
if (!requiredDeniedPermissions.isValid) {
|
97
|
+
throw new validatorErrors_1.InvalidDocument(documentPath, requiredDeniedPermissions.explanation);
|
98
|
+
}
|
99
|
+
documentBuilder.withAllowAnonymousUsers(permissions.allowAnonymous);
|
100
|
+
(_a = permissions === null || permissions === void 0 ? void 0 : permissions.allowedPermissions) === null || _a === void 0 ? void 0 : _a.forEach((p) => {
|
101
|
+
documentBuilder.withAllowedPermissions(new __1.AnySecurityIdentityBuilder(p.identityType, p.identity, p.securityProvider));
|
102
|
+
});
|
103
|
+
(_b = permissions === null || permissions === void 0 ? void 0 : permissions.deniedPermissions) === null || _b === void 0 ? void 0 : _b.forEach((p) => {
|
104
|
+
documentBuilder.withDeniedPermissions(new __1.AnySecurityIdentityBuilder(p.identityType, p.identity, p.securityProvider));
|
105
|
+
});
|
106
|
+
delete caseInsensitiveDoc.documentRecord['permissions'];
|
107
|
+
});
|
108
|
+
};
|
109
|
+
const processMetadata = (caseInsensitiveDoc, documentBuilder) => {
|
110
|
+
Object.entries(caseInsensitiveDoc.documentRecord).forEach(([k, v]) => {
|
111
|
+
documentBuilder.withMetadataValue(k, v);
|
112
|
+
});
|
113
|
+
};
|
114
|
+
const ensureFileIntegrity = (documentPath) => {
|
115
|
+
if (!isFile(documentPath)) {
|
116
|
+
throw new validatorErrors_1.NotAFileError(documentPath);
|
117
|
+
}
|
118
|
+
};
|
119
|
+
const isFile = (p) => {
|
120
|
+
if (!(0, fs_1.existsSync)(p)) {
|
121
|
+
return false;
|
122
|
+
}
|
123
|
+
return (0, fs_1.lstatSync)(p).isFile();
|
124
|
+
};
|
125
|
+
const getSecurityIdentitySchemaValidation = () => {
|
126
|
+
return new bueno_1.ArrayValue({
|
127
|
+
required: true,
|
128
|
+
each: new bueno_1.RecordValue({
|
129
|
+
values: {
|
130
|
+
identity: new bueno_1.StringValue({ required: true, emptyAllowed: false }),
|
131
|
+
identityType: new bueno_1.StringValue({
|
132
|
+
constrainTo: ['UNKNOWN', 'USER', 'GROUP', 'VIRTUAL_GROUP'],
|
133
|
+
required: true,
|
134
|
+
emptyAllowed: false,
|
135
|
+
}),
|
136
|
+
securityProvider: new bueno_1.StringValue({
|
137
|
+
emptyAllowed: false,
|
138
|
+
required: true,
|
139
|
+
}),
|
140
|
+
},
|
141
|
+
}),
|
142
|
+
});
|
143
|
+
};
|
144
|
+
//# sourceMappingURL=parseFile.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"parseFile.js","sourceRoot":"","sources":["../../src/validation/parseFile.ts"],"names":[],"m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AAM,EAAE,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,mCAAmC,GACvC,GAAiC,EAAE;IACjC,OAAO,IAAI,kBAAU,CAAC;QACpB,QAAQ,EAAE,IAAI;QACd,IAAI,EAAE,IAAI,mBAAW,CAAC;YACpB,MAAM,EAAE;gBACN,QAAQ,EAAE,IAAI,mBAAW,CAAC,EAAC,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAC,CAAC;gBAChE,YAAY,EAAE,IAAI,mBAAW,CAAC;oBAC5B,WAAW,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,CAAC;oBAC1D,QAAQ,EAAE,IAAI;oBACd,YAAY,EAAE,KAAK;iBACpB,CAAC;gBACF,gBAAgB,EAAE,IAAI,mBAAW,CAAC;oBAChC,YAAY,EAAE,KAAK;oBACnB,QAAQ,EAAE,IAAI;iBACf,CAAC;aACH;SACF,CAAC;KACH,CAAC,CAAC;AACL,CAAC,CAAC"}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const path_1 = require("path");
|
4
|
+
const process_1 = require("process");
|
5
|
+
const parseFile_1 = require("./parseFile");
|
6
|
+
const validatorErrors_1 = require("./validatorErrors");
|
7
|
+
describe('parseFile', () => {
|
8
|
+
const pathToStub = (0, path_1.join)((0, process_1.cwd)(), 'src', '__stub__');
|
9
|
+
const parse = (file) => () => (0, parseFile_1.parseAndGetDocumentBuilderFromJSONDocument)(file);
|
10
|
+
it('should accept non-url documentIDs', () => {
|
11
|
+
const file = (0, path_1.join)(pathToStub, 'jsondocuments', 'notAnUrl.json');
|
12
|
+
expect(() => (0, parseFile_1.parseAndGetDocumentBuilderFromJSONDocument)(file)).not.toThrow();
|
13
|
+
});
|
14
|
+
it.each([
|
15
|
+
{
|
16
|
+
title: 'should fail on missing title',
|
17
|
+
fileName: 'noTitle.json',
|
18
|
+
error: 'title: value is required.',
|
19
|
+
},
|
20
|
+
{
|
21
|
+
title: 'should fail on missing documentID',
|
22
|
+
fileName: 'noID.json',
|
23
|
+
error: 'documentid: value is required.',
|
24
|
+
},
|
25
|
+
{
|
26
|
+
title: 'should fail on missing identity',
|
27
|
+
fileName: 'noIdentity.json',
|
28
|
+
error: 'allowedpermissions: value does not contain identity',
|
29
|
+
},
|
30
|
+
{
|
31
|
+
title: 'should fail on identity with an invalid string',
|
32
|
+
fileName: 'identityNotAString.json',
|
33
|
+
error: 'allowedpermissions: value is not a string.',
|
34
|
+
},
|
35
|
+
{
|
36
|
+
title: 'should fail on allow anonymous with an invalid boolean',
|
37
|
+
fileName: 'identityAllowAnonymousNotABoolean.json',
|
38
|
+
error: 'allowanonymous: value is not a boolean.',
|
39
|
+
},
|
40
|
+
{
|
41
|
+
title: 'should fail on identityType with an invalid value',
|
42
|
+
fileName: 'identityTypeInvalidValue.json',
|
43
|
+
error: 'allowedpermissions: value should be one of: UNKNOWN, USER, GROUP, VIRTUAL_GROUP.',
|
44
|
+
},
|
45
|
+
])('$title', ({ fileName, error }) => {
|
46
|
+
const file = (0, path_1.join)(pathToStub, 'jsondocuments', fileName);
|
47
|
+
expect(parse(file)).toThrow(new validatorErrors_1.InvalidDocument(file, `Document contains an invalid value for ${error}`));
|
48
|
+
});
|
49
|
+
});
|
50
|
+
//# sourceMappingURL=parseFile.spec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"parseFile.spec.js","sourceRoot":"","sources":["../../src/validation/parseFile.spec.ts"],"names":[],"mappings":";;AAAA,+BAA0B;AAC1B,qCAA4B;AAC5B,2CAAuE;AACvE,uDAAkD;AAElD,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,MAAM,UAAU,GAAG,IAAA,WAAI,EAAC,IAAA,aAAG,GAAE,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;IAClD,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,EAA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|
@@ -0,0 +1,62 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RequiredKeyValidator = void 0;
|
4
|
+
class RequiredKeyValidator {
|
5
|
+
constructor(k, doc, schema) {
|
6
|
+
this.doc = doc;
|
7
|
+
this.schema = schema;
|
8
|
+
this.validations = [];
|
9
|
+
if (Array.isArray(k)) {
|
10
|
+
this.keys = k;
|
11
|
+
}
|
12
|
+
else {
|
13
|
+
this.keys = [k];
|
14
|
+
}
|
15
|
+
this.doValidations();
|
16
|
+
}
|
17
|
+
doValidations() {
|
18
|
+
this.validations = this.keys.map((k) => {
|
19
|
+
const value = this.doc.documentRecord[k.toLowerCase()];
|
20
|
+
if (this.isValueCorrectType(value)) {
|
21
|
+
return { value, message: null };
|
22
|
+
}
|
23
|
+
return {
|
24
|
+
value: value,
|
25
|
+
message: `Document contains an invalid value for ${k}: ${this.getValidationMessage(value)}`,
|
26
|
+
};
|
27
|
+
});
|
28
|
+
}
|
29
|
+
get isValid() {
|
30
|
+
return this.validEntries.length > 0;
|
31
|
+
}
|
32
|
+
get value() {
|
33
|
+
if (this.isValid) {
|
34
|
+
return this.validEntries[0].value;
|
35
|
+
}
|
36
|
+
return null;
|
37
|
+
}
|
38
|
+
get explanation() {
|
39
|
+
if (this.isValid) {
|
40
|
+
return '';
|
41
|
+
}
|
42
|
+
return this.invalidEntries[0].message;
|
43
|
+
}
|
44
|
+
get validEntries() {
|
45
|
+
return this.validations.filter((validation) => validation.message === null);
|
46
|
+
}
|
47
|
+
get invalidEntries() {
|
48
|
+
return this.validations.filter((validation) => validation.message !== null);
|
49
|
+
}
|
50
|
+
getValidationMessage(value) {
|
51
|
+
return this.schema.validate(value);
|
52
|
+
}
|
53
|
+
isValueCorrectType(value) {
|
54
|
+
const validationMessage = this.getValidationMessage(value);
|
55
|
+
if (validationMessage) {
|
56
|
+
return false;
|
57
|
+
}
|
58
|
+
return true;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
exports.RequiredKeyValidator = RequiredKeyValidator;
|
62
|
+
//# sourceMappingURL=requiredKeyValidator.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"requiredKeyValidator.js","sourceRoot":"","sources":["../../src/validation/requiredKeyValidator.ts"],"names":[],"mappings":";;;AAGA,MAAa,oBAAoB;IAI/B,YACE,CAAoB,EACZ,GAA8C,EAC9C,MAAsB;QADtB,QAAG,GAAH,GAAG,CAA2C;QAC9C,WAAM,GAAN,MAAM,CAAgB;QALxB,gBAAW,GAAmD,EAAE,CAAC;QAOvE,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACpB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SACf;aAAM;YACL,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;SACjB;QACD,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC;IAEO,aAAa;QACnB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YACrC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;YACvD,IAAI,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE;gBAClC,OAAO,EAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;aAC/B;YACD,OAAO;gBACL,KAAK,EAAE,KAAgB;gBACvB,OAAO,EAAE,0CAA0C,CAAC,KAAK,IAAI,CAAC,oBAAoB,CAChF,KAAK,CACN,EAAE;aACJ,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC;IACtC,CAAC;IAED,IAAW,KAAK;QACd,IAAI,IAAI,CAAC,OAAO,EAAE;YAChB,OAAO,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,KAAU,CAAC;SACxC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAW,WAAW;QACpB,IAAI,IAAI,CAAC,OAAO,EAAE;YAChB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,OAAQ,CAAC;IACzC,CAAC;IAED,IAAY,YAAY;QACtB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC;IAC9E,CAAC;IAED,IAAY,cAAc;QACxB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC;IAC9E,CAAC;IAEO,oBAAoB,CAAC,KAAuB;QAClD,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAU,CAAC,CAAC;IAC1C,CAAC;IAEO,kBAAkB,CAAC,KAAuB;QAChD,MAAM,iBAAiB,GAAG,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC;QAC3D,IAAI,iBAAiB,EAAE;YACrB,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AArED,oDAqEC"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UnsupportedAttribute = exports.InvalidDocument = exports.NotAJsonFileError = exports.NotAFileError = exports.PushApiClientBaseError = void 0;
|
4
|
+
class PushApiClientBaseError extends Error {
|
5
|
+
constructor(message) {
|
6
|
+
super(message);
|
7
|
+
this.name = 'Push API Client Error';
|
8
|
+
}
|
9
|
+
}
|
10
|
+
exports.PushApiClientBaseError = PushApiClientBaseError;
|
11
|
+
class NotAFileError extends PushApiClientBaseError {
|
12
|
+
constructor(p) {
|
13
|
+
super(`${p} is not a valid file, or does not exists.`);
|
14
|
+
this.name = 'Not A File Error';
|
15
|
+
}
|
16
|
+
}
|
17
|
+
exports.NotAFileError = NotAFileError;
|
18
|
+
class NotAJsonFileError extends PushApiClientBaseError {
|
19
|
+
constructor(p) {
|
20
|
+
super(`${p} is not a valid JSON file.`);
|
21
|
+
this.name = 'Not A JSON File Error';
|
22
|
+
}
|
23
|
+
}
|
24
|
+
exports.NotAJsonFileError = NotAJsonFileError;
|
25
|
+
class InvalidDocument extends PushApiClientBaseError {
|
26
|
+
constructor(p, explanation) {
|
27
|
+
super(`${p} is not a valid JSON document: ${explanation}`);
|
28
|
+
this.name = 'Invalid JSON Document Error';
|
29
|
+
}
|
30
|
+
}
|
31
|
+
exports.InvalidDocument = InvalidDocument;
|
32
|
+
class UnsupportedAttribute extends PushApiClientBaseError {
|
33
|
+
constructor(p, unsupported) {
|
34
|
+
super(`${p} contains a currently unsupported document attribute: ${unsupported}`);
|
35
|
+
this.name = 'Unsupported Attribute Error';
|
36
|
+
}
|
37
|
+
}
|
38
|
+
exports.UnsupportedAttribute = UnsupportedAttribute;
|
39
|
+
//# sourceMappingURL=validatorErrors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"validatorErrors.js","sourceRoot":"","sources":["../../src/validation/validatorErrors.ts"],"names":[],"mappings":";;;AAEA,MAAsB,sBAAuB,SAAQ,KAAK;IAExD,YAAmB,OAAgB;QACjC,KAAK,CAAC,OAAO,CAAC,CAAC;QAFV,SAAI,GAAG,uBAAuB,CAAC;IAGtC,CAAC;CACF;AALD,wDAKC;AAED,MAAa,aAAc,SAAQ,sBAAsB;IAEvD,YAAmB,CAAW;QAC5B,KAAK,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;QAFlD,SAAI,GAAG,kBAAkB,CAAC;IAGjC,CAAC;CACF;AALD,sCAKC;AACD,MAAa,iBAAkB,SAAQ,sBAAsB;IAE3D,YAAmB,CAAW;QAC5B,KAAK,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;QAFnC,SAAI,GAAG,uBAAuB,CAAC;IAGtC,CAAC;CACF;AALD,8CAKC;AAED,MAAa,eAAgB,SAAQ,sBAAsB;IAEzD,YAAmB,CAAW,EAAE,WAAmB;QACjD,KAAK,CAAC,GAAG,CAAC,kCAAkC,WAAW,EAAE,CAAC,CAAC;QAFtD,SAAI,GAAG,6BAA6B,CAAC;IAG5C,CAAC;CACF;AALD,0CAKC;AAED,MAAa,oBAAqB,SAAQ,sBAAsB;IAE9D,YAAmB,CAAW,EAAE,WAAmB;QACjD,KAAK,CACH,GAAG,CAAC,yDAAyD,WAAW,EAAE,CAC3E,CAAC;QAJG,SAAI,GAAG,6BAA6B,CAAC;IAK5C,CAAC;CACF;AAPD,oDAOC"}
|