Skip to content

Commit

Permalink
Require uuid header when consuming messages
Browse files Browse the repository at this point in the history
If the header does not exist, drop the message and log.
Update the tests.
  • Loading branch information
zknill committed Oct 18, 2023
1 parent f3f4caa commit f35897f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"keywords": ["ably", "models", "live", "realtime", "data", "sync", "collaboration"],
"devDependencies": {
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"@vitest/coverage-c8": "^0.28.4",
Expand All @@ -58,7 +57,6 @@
"ably": "^1.2.41",
"lodash": "^4.17.21",
"pino": "^8.14.1",
"rxjs": "^7.8.1",
"uuid": "^9.0.0"
"rxjs": "^7.8.1"
}
}
2 changes: 0 additions & 2 deletions src/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,6 @@ describe('Model', () => {
expect(optimisticSubscriptionSpy).toHaveBeenNthCalledWith(2, null, 'data_1');
expect(confirmedSubscriptionSpy).toHaveBeenCalledTimes(1);

// the event data payload does not match the expected event but the uuid does
// this passes because the mutation id passed to optimistic is empty.
events.e1.next(
customMessage('id_1', 'testEvent', 'confirmed_data', { 'x-ably-models-event-uuid': 'some-custom-id' }),
);
Expand Down
23 changes: 16 additions & 7 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import type {
OptimisticEvent,
ConfirmedEvent,
} from './types/model.js';
import type { OptimisticEventOptions } from './types/optimistic.js';
import {
MODELS_EVENT_REJECT_HEADER,
MODELS_EVENT_UUID_HEADER,
type OptimisticEventOptions,
} from './types/optimistic.js';
import EventEmitter from './utilities/EventEmitter.js';

/**
Expand Down Expand Up @@ -326,20 +330,25 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
throw err;
}
let rejected = false;
if (event?.extras?.headers && event?.extras?.headers['x-ably-models-reject'] === 'true') {
if (event?.extras?.headers && event?.extras?.headers[MODELS_EVENT_REJECT_HEADER] === 'true') {
rejected = true;
}
let uuid: string | undefined;
if (event?.extras?.headers && !!event?.extras?.headers['x-ably-models-event-uuid']) {
uuid = event?.extras?.headers['x-ably-models-event-uuid'];

const mutationId = event?.extras?.headers[MODELS_EVENT_UUID_HEADER];
if (!mutationId) {
this.logger.warn(
{ ...this.baseLogContext, action: 'streamEventCallback' },
`message does not have "${MODELS_EVENT_UUID_HEADER}" header, skipping message id`,
event?.id,
);
return;
}

const modelsEvent: ConfirmedEvent = {
...event!,
confirmed: true,
rejected,
mutationId: event!.id,
...(uuid && { mutationId: uuid }),
mutationId: mutationId,
};

await this.onStreamEvent(modelsEvent);
Expand Down
3 changes: 3 additions & 0 deletions src/types/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ export type OptimisticInvocationParams = {
*/
options?: Partial<OptimisticEventOptions>;
};

export const MODELS_EVENT_UUID_HEADER = 'x-ably-models-event-uuid';
export const MODELS_EVENT_REJECT_HEADER = 'x-ably-models-reject';
10 changes: 10 additions & 0 deletions src/utilities/test/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Types } from 'ably/promises';

import { MODELS_EVENT_UUID_HEADER } from '../../types/optimistic.js';

export const baseMessage: Types.Message = {
id: '1',
data: null,
Expand All @@ -14,15 +16,22 @@ export const baseMessage: Types.Message = {
};

export function createMessage(i: number): Types.Message {
const headers: { [key: string]: string } = {};
headers[MODELS_EVENT_UUID_HEADER] = `id_${i}`;

return {
...baseMessage,
id: `id_${i}`,
name: `name_${i}`,
data: `data_${i}`,
extras: { headers: headers },
};
}

export function customMessage(id: string, name: string, data: string, headers?: Record<string, string>): Types.Message {
const baseHeaders: { [key: string]: string } = {};
baseHeaders[MODELS_EVENT_UUID_HEADER] = id;

return {
...baseMessage,
id,
Expand All @@ -32,6 +41,7 @@ export function customMessage(id: string, name: string, data: string, headers?:
...baseMessage.extras,
headers: {
...baseMessage.extras.headers,
...baseHeaders,
...(headers || {}),
},
},
Expand Down

0 comments on commit f35897f

Please sign in to comment.