Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature-change: Remove reserved keyname space in headers and metadata. #374

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@
* server-side validation. When reading the headers treat them like user
* input.
*
* The key prefix `ably-chat` is reserved and cannot be used. Ably may add
* headers prefixed with `ably-chat` in the future.
*/
export type Headers = Record<string, number | string | boolean | null | undefined>;
20 changes: 0 additions & 20 deletions src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ export interface SendMessageParams {
* Do not use metadata for authoritative information. There is no server-side
* validation. When reading the metadata treat it like user input.
*
* The key `ably-chat` is reserved and cannot be used. Ably may populate
* this with different values in the future.
*/
metadata?: MessageMetadata;

Expand All @@ -105,8 +103,6 @@ export interface SendMessageParams {
* server-side validation. When reading the headers treat them like user
* input.
*
* The key prefix `ably-chat` is reserved and cannot be used. Ably may add
* headers prefixed with `ably-chat` in the future.
*/
headers?: MessageHeaders;
}
Expand Down Expand Up @@ -445,22 +441,6 @@ export class DefaultMessages

const { text, metadata, headers } = params;

if (metadata && metadata['ably-chat'] !== undefined) {
throw new Ably.ErrorInfo("unable to send message; metadata cannot use reserved key 'ably-chat'", 40001, 400);
}

if (headers) {
for (const key of Object.keys(headers)) {
if (key.startsWith('ably-chat')) {
throw new Ably.ErrorInfo(
"unable to send message; headers cannot have any key starting with reserved prefix 'ably-chat'",
40001,
400,
);
}
}
}

const response = await this._chatApi.sendMessage(this._roomId, { text, headers, metadata });

return new DefaultMessage(
Expand Down
2 changes: 0 additions & 2 deletions src/core/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
* Do not use metadata for authoritative information. There is no server-side
* validation. When reading the metadata treat it like user input.
*
* The key `ably-chat` is reserved and cannot be used. Ably may populate
* this with different values in the future.
*/
export type Metadata = Record<string, unknown>;
24 changes: 0 additions & 24 deletions src/core/room-reactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export interface SendReactionParams {
* Do not use metadata for authoritative information. There is no server-side
* validation. When reading the metadata treat it like user input.
*
* The key `ably-chat` is reserved and cannot be used. Ably may populate this
* with different values in the future.
*/
metadata?: ReactionMetadata;

Expand All @@ -58,8 +56,6 @@ export interface SendReactionParams {
* server-side validation. When reading the headers treat them like user
* input.
*
* The key prefix `ably-chat` is reserved and cannot be used. Ably may add
* headers prefixed with `ably-chat` in the future.
*/
headers?: ReactionHeaders;
}
Expand Down Expand Up @@ -190,26 +186,6 @@ export class DefaultRoomReactions
return Promise.reject(new Ably.ErrorInfo('unable to send reaction; type not set and it is required', 40001, 400));
}

if (metadata && metadata['ably-chat'] !== undefined) {
return Promise.reject(
new Ably.ErrorInfo("unable to send reaction; metadata cannot use reserved key 'ably-chat'", 40001, 400),
);
}

if (headers) {
for (const key of Object.keys(headers)) {
if (key.startsWith('ably-chat')) {
return Promise.reject(
new Ably.ErrorInfo(
"unable to send reaction; headers cannot have any key starting with reserved prefix 'ably-chat'",
40001,
400,
),
);
}
}
}

const payload: ReactionPayload = {
type: type,
metadata: metadata ?? {},
Expand Down
54 changes: 0 additions & 54 deletions test/core/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,60 +115,6 @@ describe('Messages', () => {
}),
);
});

it<TestContext>('should be not be able to set reserved header prefix', (context) => {
return new Promise<void>((accept, reject) => {
const { chatApi, realtime } = context;
const timestamp = Date.now();
vi.spyOn(chatApi, 'sendMessage').mockResolvedValue({
timeserial: 'abcdefghij@1672531200000-123',
createdAt: timestamp,
});

const room = makeRandomRoom({ chatApi, realtime });
const messagePromise = room.messages.send({
text: 'hello there',
headers: { 'ably-chat.you': 'shall not pass' },
});

messagePromise
.then(() => {
reject(new Error('message should have not been sent successfully'));
})
.catch((error: unknown) => {
expect(error).toBeTruthy();
expect((error as Error).message).toMatch(/reserved prefix/);
accept();
});
});
});

it<TestContext>('should be not be able to set reserved metadata key', (context) => {
return new Promise<void>((accept, reject) => {
const { chatApi, realtime } = context;
const timestamp = Date.now();
vi.spyOn(chatApi, 'sendMessage').mockResolvedValue({
timeserial: 'abcdefghij@1672531200000-123',
createdAt: timestamp,
});

const room = makeRandomRoom({ chatApi, realtime });
const messagePromise = room.messages.send({
text: 'hello there',
metadata: { 'ably-chat': 'shall not pass' },
});

messagePromise
.then(() => {
reject(new Error('message should have not been sent successfully'));
})
.catch((error: unknown) => {
expect(error).toBeTruthy();
expect((error as Error).message).toMatch(/reserved key/);
accept();
});
});
});
});

describe('subscribing to updates', () => {
Expand Down
52 changes: 0 additions & 52 deletions test/core/room-reactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,58 +299,6 @@ describe('Reactions', () => {
headers: { action: 'strike back', number: 1980 },
});
}));

it<TestContext>('should not be able to use reserved prefix in reaction headers', (context) =>
new Promise<void>((done, reject) => {
const { room } = context;

room.reactions.subscribe(() => {
reject(new Error("should not receive reaction, sending must've failed"));
});

const sendPromise = room.reactions.send({
type: 'love',
headers: { 'ably-chat-hello': true }, // "ably-chat" prefix is the reserved
});

sendPromise
.then(() => {
reject(new Error('send should not succeed'));
})
.catch((error: unknown) => {
const errInfo = error as Ably.ErrorInfo;
expect(errInfo).toBeTruthy();
expect(errInfo.message).toMatch(/reserved prefix/);
expect(errInfo.code).toEqual(40001);
done();
});
}));

it<TestContext>('should not be able to use reserved key in reaction metadata', (context) =>
new Promise<void>((done, reject) => {
const { room } = context;

room.reactions.subscribe(() => {
reject(new Error("should not receive reaction, sending must've failed"));
});

const sendPromise = room.reactions.send({
type: 'love',
metadata: { 'ably-chat': { value: 1 } }, // "ably-chat" is reserved
});

sendPromise
.then(() => {
reject(new Error('send should not succeed'));
})
.catch((error: unknown) => {
const errInfo = error as Ably.ErrorInfo;
expect(errInfo).toBeTruthy();
expect(errInfo.message).toMatch(/reserved key/);
expect(errInfo.code).toEqual(40001);
done();
});
}));
});

it<TestContext>('has an attachment error code', (context) => {
Expand Down
Loading