Skip to content
Open
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
1 change: 0 additions & 1 deletion src/dataProvider/adminDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default (
const mercure: MercureOptions | null = factoryParams.mercure
? {
hub: null,
jwt: null,
topicUrl: entrypointUrl,
...(factoryParams.mercure === true ? {} : factoryParams.mercure),
}
Expand Down
1 change: 0 additions & 1 deletion src/hydra/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ function dataProvider(
const mercure: MercureOptions | null = factoryParams.mercure
? {
hub: null,
jwt: null,
topicUrl: entrypointUrl,
...(factoryParams.mercure === true ? {} : factoryParams.mercure),
}
Expand Down
51 changes: 51 additions & 0 deletions src/mercure/createSubscription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import createSubscription from './createSubscription.js';
import type { MercureOptions } from '../types.js';

let urls: string[] = [];
let inits: (EventSourceInit | undefined)[] = [];

function FakeEventSource(url: string, init?: EventSourceInit) {
urls.push(url);
inits.push(init);

return { addEventListener: () => {} };
}

const options = (protocolVersion?: MercureOptions['protocolVersion']) => ({
hub: 'https://example.com/.well-known/mercure',
topicUrl: new URL('https://example.com/api'),
...(protocolVersion ? { protocolVersion } : {}),
});

beforeEach(() => {
urls = [];
inits = [];
global.EventSource = FakeEventSource as unknown as typeof EventSource;
});

test('Subscribe with the Mercure 1.0 match parameter by default', () => {
createSubscription(options(), '/api/books/1', () => {});

expect(urls).toEqual([
'https://example.com/.well-known/mercure?match=https%3A%2F%2Fexample.com%2Fapi%2Fbooks%2F1',
]);
});

test('Subscribe with the topic parameter of the Mercure 0.x protocol', () => {
createSubscription(options('0.x'), '/api/books/1', () => {});

expect(urls).toEqual([
'https://example.com/.well-known/mercure?topic=https%3A%2F%2Fexample.com%2Fapi%2Fbooks%2F1',
]);
});

test('Leave the authorization cookie to the server', () => {
createSubscription(
{ ...options(), withCredentials: true },
'/api/books/1',
() => {},
);

expect(inits).toEqual([{ withCredentials: true }]);
expect(document.cookie).toBe('');
});
12 changes: 6 additions & 6 deletions src/mercure/createSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const createSubscription = (
};
}

const legacy = mercure.protocolVersion === '0.x';
const url = new URL(mercure.hub, window.origin);
url.searchParams.append('topic', new URL(topic, mercure.topicUrl).toString());

if (mercure.jwt !== null) {
document.cookie = `mercureAuthorization=${mercure.jwt}; Path=${mercure.hub}; Secure; SameSite=None`;
}
url.searchParams.append(
legacy ? 'topic' : 'match',
new URL(topic, mercure.topicUrl).toString(),
);

const eventSource = new EventSource(url.toString(), {
withCredentials: mercure.jwt !== null,
withCredentials: mercure.withCredentials ?? false,
});
const eventListener = (event: MessageEvent) => {
const document = transformData(JSON.parse(event.data));
Expand Down
1 change: 0 additions & 1 deletion src/openapi/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function dataProvider(
const mercure: MercureOptions | null = factoryParams.mercure
? {
hub: null,
jwt: null,
topicUrl: entrypointUrl,
...(factoryParams.mercure === true ? {} : factoryParams.mercure),
}
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ export interface SchemaAnalyzer {

export interface MercureOptions {
hub: string | null;
jwt: string | null;
/** Sends the hub's authorization cookie, which the server must set as HttpOnly. */
withCredentials?: boolean;
topicUrl: URL;
/** Protocol spoken by the hub: '1.0' (default) or '0.x' for hubs older than Mercure 1.0. */
protocolVersion?: '1.0' | '0.x';
}

export interface MercureSubscription {
Expand Down
Loading