@adobe/helix-admin-support 2.9.22 → 3.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/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [3.0.0](https://github.com/adobe/helix-admin-support/compare/v2.9.23...v3.0.0) (2024-11-06)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * support limiting size of entries returned ([#328](https://github.com/adobe/helix-admin-support/issues/328)) ([5c3e6c7](https://github.com/adobe/helix-admin-support/commit/5c3e6c7d81282493d1124b0702562d5daaeb206e))
7
+
8
+
9
+ ### BREAKING CHANGES
10
+
11
+ * AuditLog.getRecords has changed semantics
12
+
13
+ * chore(test): add a larger example to check duration
14
+
15
+ ## [2.9.23](https://github.com/adobe/helix-admin-support/compare/v2.9.22...v2.9.23) (2024-11-02)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **deps:** update dependency @adobe/helix-shared-config to v11.0.8 ([6a0f67b](https://github.com/adobe/helix-admin-support/commit/6a0f67b4f4ca23c70613f10b78490596668ee78c))
21
+
1
22
  ## [2.9.22](https://github.com/adobe/helix-admin-support/compare/v2.9.21...v2.9.22) (2024-11-02)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-admin-support",
3
- "version": "2.9.22",
3
+ "version": "3.0.0",
4
4
  "description": "Helix Admin Support Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@adobe/fetch": "4.1.9",
40
- "@adobe/helix-shared-config": "11.0.7",
40
+ "@adobe/helix-shared-config": "11.0.8",
41
41
  "@adobe/helix-shared-utils": "3.0.2",
42
42
  "@aws-sdk/client-s3": "3.685.0",
43
43
  "@aws-sdk/client-sqs": "3.682.0",
@@ -34,6 +34,36 @@ const gunzip = promisify(zlib.gunzip);
34
34
  * @property {string} nextToken optional next token
35
35
  */
36
36
 
37
+ /**
38
+ * Return a slice of entries that don't exceed a given size
39
+ * @param {number|undefined} maxSize maximum size
40
+ * @param {LogEntry[]} obj.entries entries
41
+ * @param {number} obj.fileIndex file index
42
+ * @param {number} obj.entryIndex entry index
43
+ * @param {object} next next iterator to return unless we exceed the maximum size
44
+ * @returns {LogEntry[]} slice of entries
45
+ */
46
+ function checkSize(maxSize, {
47
+ entries, fileIndex, entryIndex, next,
48
+ }) {
49
+ if (maxSize && JSON.stringify(entries).length >= maxSize) {
50
+ let totalSize = 2;
51
+ for (const [index, entry] of entries.entries()) {
52
+ totalSize += JSON.stringify(entry).length + 1;
53
+ if (totalSize > maxSize) {
54
+ // return at least one item
55
+ const count = Math.max(1, index);
56
+ return {
57
+ entries: entries.slice(0, count),
58
+ next: { fileIndex, entryIndex: entryIndex + count },
59
+ };
60
+ }
61
+ }
62
+ /* c8 ignore next */
63
+ }
64
+ return next ? { entries, next } : { entries };
65
+ }
66
+
37
67
  /**
38
68
  * Audit log reader implemented in S3.
39
69
  */
@@ -178,11 +208,12 @@ export default class Reader {
178
208
  * @param {number} after from timestamp
179
209
  * @param {number} before to timestamp
180
210
  * @param {number} limit number of entries to return at most
211
+ * @param {number} maxSize maximum total size of entries when stringified
181
212
  * @param {LogLocation} location optional location with `fileIndex` and `entryIndex`
182
213
  *
183
214
  * @returns {Promise<LogResult>} log result
184
215
  */
185
- async getEntries(after, before, limit, location) {
216
+ async getEntries(after, before, { limit, maxSize }, location) {
186
217
  const { logFiles } = this;
187
218
  const { entries, fileIndex, entryIndex } = location
188
219
  ? await this.#locateEntries(location)
@@ -203,26 +234,38 @@ export default class Reader {
203
234
  const count = Math.min(limit, remaining);
204
235
  if (count < remaining) {
205
236
  // `limit` was reached first, so continue after that
206
- return {
237
+ return checkSize(maxSize, {
207
238
  entries: entries.slice(0, count),
239
+ fileIndex,
240
+ entryIndex,
208
241
  next: { fileIndex, entryIndex: entryIndex + count },
209
- };
242
+ });
210
243
  }
211
244
  if (remaining < entries.length) {
212
245
  // `before` timestamp was reached first, so stop
213
- return { entries: entries.slice(0, count) };
246
+ return checkSize(maxSize, {
247
+ entries: entries.slice(0, count),
248
+ fileIndex,
249
+ entryIndex,
250
+ });
214
251
  }
215
252
 
216
253
  // we return all items in this file, and need to determine whether to continue paging
217
254
  if (fileIndex === logFiles.length - 1 || logFiles[fileIndex + 1].timestamp > before) {
218
255
  // there's no next page or it starts after `before` date, so stop
219
- return { entries };
256
+ return checkSize(maxSize, {
257
+ entries,
258
+ fileIndex,
259
+ entryIndex,
260
+ });
220
261
  }
221
- return {
222
- // next location will start at the beginning of the next file
262
+ // next location will start at the beginning of the next file
263
+ return checkSize(maxSize, {
223
264
  entries,
265
+ fileIndex,
266
+ entryIndex,
224
267
  next: { fileIndex: fileIndex + 1, entryIndex: 0 },
225
- };
268
+ });
226
269
  }
227
270
 
228
271
  close() {