@adobe/helix-admin-support 2.9.2 → 2.9.3

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,10 @@
1
+ ## [2.9.3](https://github.com/adobe/helix-admin-support/compare/v2.9.2...v2.9.3) (2024-08-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add bucketName option for auditlog reader/writer ([#303](https://github.com/adobe/helix-admin-support/issues/303)) ([cac5f6f](https://github.com/adobe/helix-admin-support/commit/cac5f6ff406e8fcfc77c37cdcf1ad6f4fa93ba31))
7
+
1
8
  ## [2.9.2](https://github.com/adobe/helix-admin-support/compare/v2.9.1...v2.9.2) (2024-08-24)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-admin-support",
3
- "version": "2.9.2",
3
+ "version": "2.9.3",
4
4
  "description": "Helix Admin Support Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -9,19 +9,28 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
+
12
13
  import AuditLogReader from './Reader.js';
14
+ import { resolveOptions } from './utils.js';
13
15
  import AuditLogWriter from './Writer.js';
14
16
 
17
+ /**
18
+ * @typedef {{
19
+ * bucketName?: string;
20
+ * }} Options
21
+ */
22
+
15
23
  /**
16
24
  * Create an audit log reader of an org and site.
17
25
  *
18
26
  * @param {String} org org
19
27
  * @param {String|undefined} site site, optional
20
28
  * @param {Logger} log logger
29
+ * @param {Options} [opts] options
21
30
  * @returns {AuditLogReader} reader
22
31
  */
23
- function createReader(org, site, log) {
24
- return new AuditLogReader(org, site, log);
32
+ function createReader(org, site, log, opts) {
33
+ return new AuditLogReader(org, site, log, resolveOptions(opts));
25
34
  }
26
35
 
27
36
  /**
@@ -30,10 +39,11 @@ function createReader(org, site, log) {
30
39
  * @param {String} org org
31
40
  * @param {String|undefined} site site, optional
32
41
  * @param {Logger} log logger
42
+ * @param {Options} [opts] options
33
43
  * @returns {AuditLogWriter} writer
34
44
  */
35
- function createWriter(org, site, log) {
36
- return new AuditLogWriter(org, site, log);
45
+ function createWriter(org, site, log, opts) {
46
+ return new AuditLogWriter(org, site, log, resolveOptions(opts));
37
47
  }
38
48
 
39
49
  export const AuditLog = {
@@ -9,11 +9,12 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
+
12
13
  import { promisify } from 'util';
13
14
  import zlib from 'zlib';
14
15
  import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
15
16
  import { Response } from '@adobe/fetch';
16
- import { BUCKET_NAME, INDEX_FILE } from './utils.js';
17
+ import { INDEX_FILE } from './utils.js';
17
18
  import DateFormat from './DateFormat.js';
18
19
 
19
20
  const gunzip = promisify(zlib.gunzip);
@@ -35,15 +36,18 @@ const gunzip = promisify(zlib.gunzip);
35
36
 
36
37
  /**
37
38
  * Audit log reader implemented in S3.
38
- *
39
- * @param {String} org org
40
- * @param {String|undefined} site site, optional
41
- * @param {import('@adobe/helix-universal').Logger} log logger
42
39
  */
43
40
  export default class Reader {
44
- constructor(org, site, log) {
41
+ /**
42
+ * @param {String} org org
43
+ * @param {String|undefined} site site, optional
44
+ * @param {import('@adobe/helix-universal').Logger} log logger
45
+ * @param {Required<import('./AuditLog.js').Options>} opts options
46
+ */
47
+ constructor(org, site, log, opts) {
45
48
  this.prefix = site ? `${org}/${site}` : `${org}/.all`;
46
49
  this.log = log;
50
+ this.bucketName = opts.bucketName;
47
51
  }
48
52
 
49
53
  /**
@@ -56,7 +60,7 @@ export default class Reader {
56
60
 
57
61
  try {
58
62
  const res = await this.s3.send(new GetObjectCommand({
59
- Bucket: BUCKET_NAME,
63
+ Bucket: this.bucketName,
60
64
  Key: `${prefix}/${INDEX_FILE}`,
61
65
  }));
62
66
  const logContents = await new Response(res.Body, {}).text();
@@ -79,7 +83,7 @@ export default class Reader {
79
83
  *
80
84
  * @param {number} fileIndex file index
81
85
  *
82
- * @returns array of entries
86
+ * @returns {Promise<LogEntry[]>} array of entries
83
87
  */
84
88
  async #getLogContents(fileIndex) {
85
89
  const {
@@ -89,7 +93,7 @@ export default class Reader {
89
93
  const key = `${prefix}/${logFiles[fileIndex].name}.gz`;
90
94
  try {
91
95
  const res = await s3.send(new GetObjectCommand({
92
- Bucket: BUCKET_NAME,
96
+ Bucket: this.bucketName,
93
97
  Key: key,
94
98
  }));
95
99
  const buf = await new Response(res.Body, {}).buffer();
@@ -105,7 +109,7 @@ export default class Reader {
105
109
  *
106
110
  * @param {number} after timestamp
107
111
  *
108
- * @returns {LogEntries>} entries, fileIndex and entryIndex
112
+ * @returns {Promise<LogEntries>} entries, fileIndex and entryIndex
109
113
  */
110
114
  async #findEntries(after) {
111
115
  const { logFiles } = this;
@@ -146,7 +150,7 @@ export default class Reader {
146
150
  * Locate entries given a previously saved location
147
151
  *
148
152
  * @param {LogLocation} location location
149
- * @returns {LogEntries>} entries, fileIndex and entryIndex
153
+ * @returns {Promise<LogEntries>} entries, fileIndex and entryIndex
150
154
  */
151
155
  async #locateEntries(location) {
152
156
  const { log, logFiles } = this;
@@ -15,7 +15,7 @@ import { promisify } from 'util';
15
15
  import zlib from 'zlib';
16
16
  import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
17
17
  import { Response } from '@adobe/fetch';
18
- import { BUCKET_NAME, INDEX_FILE } from './utils.js';
18
+ import { INDEX_FILE } from './utils.js';
19
19
  import DateFormat from './DateFormat.js';
20
20
 
21
21
  const gzip = promisify(zlib.gzip);
@@ -45,9 +45,16 @@ function generateID() {
45
45
  * Audit log implemented in S3
46
46
  */
47
47
  export default class Writer {
48
- constructor(org, site, log) {
48
+ /**
49
+ * @param {string} org
50
+ * @param {string|undefined} site
51
+ * @param {Logger} log
52
+ * @param {Required<import('./AuditLog.js').Options>} opts
53
+ */
54
+ constructor(org, site, log, opts) {
49
55
  this.prefix = site ? `${org}/${site}` : `${org}/.all`;
50
56
  this.log = log;
57
+ this.bucketName = opts.bucketName;
51
58
  }
52
59
 
53
60
  /**
@@ -61,7 +68,7 @@ export default class Writer {
61
68
  /**
62
69
  * Fetch index file
63
70
  *
64
- * @returns {String[]} log entry file names array
71
+ * @returns {Promise<String[]>} log entry file names array
65
72
  */
66
73
  async #fetchIndex() {
67
74
  const { prefix, s3 } = this;
@@ -69,7 +76,7 @@ export default class Writer {
69
76
  // fetch name of last log object
70
77
  try {
71
78
  const res = await s3.send(new GetObjectCommand({
72
- Bucket: BUCKET_NAME,
79
+ Bucket: this.bucketName,
73
80
  Key: `${prefix}/${INDEX_FILE}`,
74
81
  }));
75
82
  const logContents = await new Response(res.Body, {}).text();
@@ -86,13 +93,15 @@ export default class Writer {
86
93
  /**
87
94
  * Fetch log file. Returns null if the log file is not found or too large to accomodate
88
95
  * another record.
96
+ *
97
+ * @param {string} key
89
98
  */
90
99
  async #fetchLogFile(key) {
91
100
  const { s3 } = this;
92
101
 
93
102
  try {
94
103
  const res = await s3.send(new GetObjectCommand({
95
- Bucket: BUCKET_NAME,
104
+ Bucket: this.bucketName,
96
105
  Key: `${key}.gz`,
97
106
  }));
98
107
  if (res.ContentLength < MAX_OBJECT_SIZE) {
@@ -132,7 +141,7 @@ export default class Writer {
132
141
  logFiles.push(generateID());
133
142
  await s3.send(new PutObjectCommand({
134
143
  Body: logFiles.join('\n'),
135
- Bucket: BUCKET_NAME,
144
+ Bucket: this.bucketName,
136
145
  ContentType: 'text/plain',
137
146
  Key: `${prefix}/${INDEX_FILE}`,
138
147
  }));
@@ -150,7 +159,7 @@ export default class Writer {
150
159
  const { s3 } = this;
151
160
  await s3.send(new PutObjectCommand({
152
161
  Body: await gzip(JSON.stringify(contents)),
153
- Bucket: BUCKET_NAME,
162
+ Bucket: this.bucketName,
154
163
  ContentEncoding: 'gzip',
155
164
  ContentType: 'application/json',
156
165
  Key: `${key}.gz`,
@@ -13,10 +13,23 @@
13
13
  /**
14
14
  * Bucket to use for logging.
15
15
  */
16
- export const BUCKET_NAME = 'helix-adobe-logs';
16
+ const DEFAULT_BUCKET_NAME = 'helix-adobe-logs';
17
17
 
18
18
  /**
19
19
  * Index file in project folder. This is a LF separated text file with
20
20
  * the list of all log files in ascending order.
21
21
  */
22
22
  export const INDEX_FILE = '.index';
23
+
24
+ /**
25
+ * Resolve options object to contain all properties.
26
+ *
27
+ * @param {import("./AuditLog").Options} [popts]
28
+ * @returns {Required<import("./AuditLog").Options>}
29
+ */
30
+ export function resolveOptions(opts = {}) {
31
+ return {
32
+ bucketName: DEFAULT_BUCKET_NAME,
33
+ ...opts,
34
+ };
35
+ }