Skip to content

Commit 82a955e

Browse files
authored
BC-9005 Change regex to more strict defintion. (#60)
1 parent 35a1c01 commit 82a955e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/infra/y-redis/helper.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,30 @@ describe('helper', () => {
5757
it('returns the expected value', () => {
5858
const room = 'room';
5959
const docid = 'docid';
60-
const prefix = 'prefix';
60+
const prefix = 'y';
6161

6262
const result = computeRedisRoomStreamName(room, docid, prefix);
6363

64-
expect(result).toBe('prefix:room:room:docid');
64+
expect(result).toBe('y:room:room:docid');
6565
});
6666
});
6767

6868
describe('decodeRedisRoomStreamName', () => {
6969
describe('when the rediskey is malformed', () => {
7070
it('throws an error', () => {
7171
const rediskey = 'invalid';
72-
const expectedPrefix = 'prefix';
72+
const expectedPrefix = 'y';
7373

7474
expect(() => decodeRedisRoomStreamName(rediskey, expectedPrefix)).toThrow(
75-
`Malformed stream name! prefix="undefined" expectedPrefix="prefix", rediskey="invalid"`,
75+
`Malformed stream name! expectedRedisPrefix="y", rediskey="invalid"`,
7676
);
7777
});
7878
});
7979

8080
describe('when the rediskey is well formed', () => {
8181
it('returns the expected values', () => {
82-
const rediskey = 'prefix:room:room:docid';
83-
const expectedPrefix = 'prefix';
82+
const rediskey = 'y:room:room:docid';
83+
const expectedPrefix = 'y';
8484

8585
const result = decodeRedisRoomStreamName(rediskey, expectedPrefix);
8686

@@ -91,10 +91,10 @@ describe('helper', () => {
9191
describe('when the prefix does not match', () => {
9292
it('throws an error', () => {
9393
const rediskey = 'invalid:room:room:docid';
94-
const expectedPrefix = 'prefix';
94+
const expectedPrefix = 'y';
9595

9696
expect(() => decodeRedisRoomStreamName(rediskey, expectedPrefix)).toThrow(
97-
`Malformed stream name! prefix="invalid" expectedPrefix="prefix", rediskey="invalid:room:room:docid"`,
97+
`Malformed stream name! expectedRedisPrefix="y", rediskey="invalid:room:room:docid"`,
9898
);
9999
});
100100
});

src/infra/y-redis/helper.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ export interface RoomStreamInfos {
2626
room: string;
2727
docid: string;
2828
}
29-
export const decodeRedisRoomStreamName = (rediskey: string, expectedPrefix: string): RoomStreamInfos => {
30-
const match = /^(.*):room:(.*):(.*)$/.exec(rediskey);
31-
if (match == null || match[1] !== expectedPrefix) {
32-
throw new Error(
33-
`Malformed stream name! prefix="${match?.[1]}" expectedPrefix="${expectedPrefix}", rediskey="${rediskey}"`,
34-
);
29+
30+
export const decodeRedisRoomStreamName = (rediskey: string, expectedRedisPrefix: string): RoomStreamInfos => {
31+
const match = new RegExp(`^${expectedRedisPrefix}:room:([^:]+):([^:]+)$`).exec(rediskey);
32+
33+
if (match == null) {
34+
throw new Error(`Malformed stream name! expectedRedisPrefix="${expectedRedisPrefix}", rediskey="${rediskey}"`);
3535
}
3636

37-
return { room: decodeURIComponent(match[2]), docid: decodeURIComponent(match[3]) };
37+
return { room: decodeURIComponent(match[1]), docid: decodeURIComponent(match[2]) };
3838
};
3939

4040
const getIdFromLastStreamMessageReply = (docStreamReplay: StreamMessagesReply): RedisKey | undefined => {

0 commit comments

Comments
 (0)