Skip to content

Commit

Permalink
feat: add validation of the prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Feb 6, 2025
1 parent 6bb2e37 commit 99145d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/mongodb-log-writer/src/mongo-log-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@ describe('MongoLogManager', function () {
await fs.rmdir(directory, { recursive: true });
});

it('constructor throws with invalid prefixes', function () {
expect(() => {
new MongoLogManager({
directory,
retentionDays,
prefix: '%asdabs/',
onwarn,
onerror,
});
}).to.throw();

expect(() => {
new MongoLogManager({
directory,
retentionDays,
prefix: '$$$$',
onwarn,
onerror,
});
}).to.throw();

expect(() => {
new MongoLogManager({
directory,
retentionDays,
prefix: 'abc_',
onwarn,
onerror,
});
}).not.to.throw();

expect(() => {
new MongoLogManager({
directory,
retentionDays,
prefix: 'something',
onwarn,
onerror,
});
}).not.to.throw();
});

it('allows creating and writing to log files', async function () {
const manager = new MongoLogManager({
directory,
Expand Down
8 changes: 7 additions & 1 deletion packages/mongodb-log-writer/src/mongo-log-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export class MongoLogManager {
_options: MongoLogOptions;

constructor(options: MongoLogOptions) {
if (options.prefix) {
if (!/^[a-z0-9_]+$/i.test(options.prefix)) {
throw new Error(
'Prefix must only contain letters, numbers, and underscores'
);
}
}
this._options = options;
}

Expand Down Expand Up @@ -69,7 +76,6 @@ export class MongoLogManager {
? this._options.logRetentionGB * 1024 * 1024 * 1024
: Infinity;
let usedStorageSize = this._options.logRetentionGB ? 0 : -Infinity;
// eslint-disable-next-line no-console

for await (const dirent of dirHandle) {
// Cap the overall time spent inside this function. Consider situations like
Expand Down

0 comments on commit 99145d1

Please sign in to comment.