@adobe/helix-admin-support 3.0.3 → 3.1.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,17 @@
1
+ # [3.1.0](https://github.com/adobe/helix-admin-support/compare/v3.0.4...v3.1.0) (2024-11-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * introduce UpdatesPartitioner ([#335](https://github.com/adobe/helix-admin-support/issues/335)) ([c44ebb3](https://github.com/adobe/helix-admin-support/commit/c44ebb3885dc43843f6eb6e80dadb40e2c843363))
7
+
8
+ ## [3.0.4](https://github.com/adobe/helix-admin-support/compare/v3.0.3...v3.0.4) (2024-11-16)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update external fixes ([#333](https://github.com/adobe/helix-admin-support/issues/333)) ([49493c4](https://github.com/adobe/helix-admin-support/commit/49493c40cbe144e5b44c47af634c2680fcdbebf5))
14
+
1
15
  ## [3.0.3](https://github.com/adobe/helix-admin-support/compare/v3.0.2...v3.0.3) (2024-11-14)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-admin-support",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "description": "Helix Admin Support Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -39,9 +39,9 @@
39
39
  "@adobe/fetch": "4.1.9",
40
40
  "@adobe/helix-shared-config": "11.0.9",
41
41
  "@adobe/helix-shared-utils": "3.0.2",
42
- "@aws-sdk/client-s3": "3.688.0",
43
- "@aws-sdk/client-sqs": "3.687.0",
44
- "@smithy/node-http-handler": "3.2.5"
42
+ "@aws-sdk/client-s3": "3.693.0",
43
+ "@aws-sdk/client-sqs": "3.693.0",
44
+ "@smithy/node-http-handler": "3.3.1"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@adobe/eslint-config-helix": "2.0.8",
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /**
14
+ * Default maximum number of indices to collect in one chunk.
15
+ */
16
+ const MAX_INDICES = 10;
17
+
18
+ /**
19
+ * Default maximum number of updates to collect in one chunk.
20
+ */
21
+ const MAX_UPDATES = 100;
22
+
23
+ /**
24
+ * @typedef ProjectUpdates
25
+ * @property {string} key key
26
+ * @property {string} owner owner
27
+ * @property {string} repo repo
28
+ * @property {Object[]} updates updates
29
+ */
30
+
31
+ /**
32
+ * Group updates by index name.
33
+ *
34
+ * @param {Object[]} updates updates containing an index value
35
+ * @returns {Map} updates keyed by index name
36
+ */
37
+ function groupUpdatesByIndex(updates) {
38
+ return updates.reduce((map, update) => {
39
+ if (!map.has(update.index)) {
40
+ map.set(update.index, []);
41
+ }
42
+ map.get(update.index).push(update);
43
+ return map;
44
+ }, new Map());
45
+ }
46
+
47
+ /**
48
+ * Partitions updates into chunks that will not contain more than a configurable
49
+ * amount of indices and updates.
50
+ */
51
+ export class UpdatesPartitioner {
52
+ /** @private */
53
+ #key;
54
+
55
+ /** @private */
56
+ #owner;
57
+
58
+ /** @private */
59
+ #repo;
60
+
61
+ /** @private */
62
+ #chunks;
63
+
64
+ /** @private */
65
+ #current;
66
+
67
+ /** @private */
68
+ #maxIndices;
69
+
70
+ /** @private */
71
+ #maxUpdates;
72
+
73
+ constructor(key, owner, repo, maxIndices = MAX_INDICES, maxUpdates = MAX_UPDATES) {
74
+ this.#key = key;
75
+ this.#owner = owner;
76
+ this.#repo = repo;
77
+ this.#maxIndices = maxIndices;
78
+ this.#maxUpdates = maxUpdates;
79
+
80
+ this.#chunks = [];
81
+
82
+ this.#pushChunk();
83
+ }
84
+
85
+ /**
86
+ * Partition a collection of project updates into chunks so every chunk can be processed
87
+ * by the indexer completely before the service times out.
88
+ *
89
+ * @param {ProjectUpdates} project project updates
90
+ * @returns {ProjectUpdates[]} updates to send
91
+ */
92
+ static partition(project) {
93
+ if (project.updates.length === 0) {
94
+ return [project];
95
+ }
96
+ const [{ type }] = project.updates;
97
+ if (type !== 'onedrive') {
98
+ return [project];
99
+ }
100
+ const partitioner = new UpdatesPartitioner(project.key, project.owner, project.repo);
101
+ for (const updates of groupUpdatesByIndex(project.updates).values()) {
102
+ partitioner.feed(updates);
103
+ }
104
+ return partitioner.chunks;
105
+ }
106
+
107
+ /**
108
+ * Feed another set of updates for a new index.
109
+ *
110
+ * @param {Object[]} updates updates
111
+ */
112
+ feed(updates) {
113
+ let chunk = this.#chunks[this.#current];
114
+ if (chunk.indices >= this.#maxIndices || chunk.updates.length >= this.#maxUpdates) {
115
+ chunk = this.#pushChunk();
116
+ }
117
+
118
+ const avail = this.#maxUpdates - chunk.updates.length;
119
+ const count = Math.min(avail, updates.length);
120
+ chunk.updates.push(...updates.slice(0, count));
121
+ chunk.indices += 1;
122
+
123
+ const remaining = updates.slice(count);
124
+ for (let i = 0; i < remaining.length; i += this.#maxUpdates) {
125
+ this.#pushChunk(remaining.slice(i, i + this.#maxUpdates));
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Push another chunk to the list of chunks.
131
+ *
132
+ * @param {Object[]} updates updates
133
+ * @returns new chunk
134
+ */
135
+ #pushChunk(updates = []) {
136
+ this.#chunks.push({
137
+ key: this.#key,
138
+ owner: this.#owner,
139
+ repo: this.#repo,
140
+ updates,
141
+ indices: updates.length ? 1 : 0,
142
+ });
143
+ this.#current = this.#chunks.length - 1;
144
+ return this.#chunks[this.#current];
145
+ }
146
+
147
+ /**
148
+ * Return array of chunks that satisfy conditions
149
+ *
150
+ * @returns chunks of updates
151
+ */
152
+ get chunks() {
153
+ return this.#chunks.map(({
154
+ key, owner, repo, updates,
155
+ }) => ({
156
+ key, owner, repo, updates,
157
+ }));
158
+ }
159
+ }
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ export declare interface ProjectUpdates {
14
+ key: string;
15
+ owner: string;
16
+ repo: string;
17
+ updates: Object[];
18
+ }
19
+
20
+ export declare class UpdatesPartitioner {
21
+ /**
22
+ * Creates a new updates partitioner
23
+ *
24
+ * @param {string} key project key
25
+ * @param {string} owner owner
26
+ * @param {string} repo repo
27
+ * @param {number|undefined} maxIndices maximum number of indices to allow in one chunk
28
+ * @param {number|undefined} maxUpdates maximum number of updates to allow in one chunk
29
+ */
30
+ constructor(
31
+ key: string,
32
+ owner: string,
33
+ repo: string,
34
+ maxIndices?: number,
35
+ maxUpdates?: number
36
+ );
37
+
38
+ /**
39
+ * Feed another set of updates for a new index.
40
+ *
41
+ * @param {Object[]} updates updates
42
+ */
43
+ feed(updates: Object[]): void;
44
+
45
+ /**
46
+ * Partition a collection of project updates into chunks so every chunk can be processed
47
+ * by the indexer completely before the service times out.
48
+ *
49
+ * @param {ProjectUpdates} project project updates
50
+ * @returns {ProjectUpdates[]} updates to send
51
+ */
52
+ static partition(project: ProjectUpdates): ProjectUpdates[];
53
+ }
package/src/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export * from './fetch-sitemap';
34
34
  export * from './fetch-feature-flags';
35
35
  export * from './flatJson2Object';
36
36
  export * from './BatchedQueueClient';
37
+ export * from './UpdatesPartitioner';
37
38
  export * from '../types/helix-config';
38
39
  export * from '../types/project-config';
39
40
  export * from '../types/project-config-all';
package/src/index.js CHANGED
@@ -23,4 +23,5 @@ export * from './fetch-sitemap.js';
23
23
  export * from './fetch-feature-flags.js';
24
24
  export * from './flatJson2Object.js';
25
25
  export * from './BatchedQueueClient.js';
26
+ export * from './UpdatesPartitioner.js';
26
27
  export * from './auditlog/AuditLog.js';