diff --git a/src/dataProvider/adminDataProvider.ts b/src/dataProvider/adminDataProvider.ts index 868550e0..e2e5d14c 100644 --- a/src/dataProvider/adminDataProvider.ts +++ b/src/dataProvider/adminDataProvider.ts @@ -16,7 +16,6 @@ export default ( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/hydra/dataProvider.ts b/src/hydra/dataProvider.ts index 385cc405..a2910fd9 100644 --- a/src/hydra/dataProvider.ts +++ b/src/hydra/dataProvider.ts @@ -201,7 +201,6 @@ function dataProvider( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/mercure/createSubscription.test.ts b/src/mercure/createSubscription.test.ts new file mode 100644 index 00000000..8bdab9a4 --- /dev/null +++ b/src/mercure/createSubscription.test.ts @@ -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(''); +}); diff --git a/src/mercure/createSubscription.ts b/src/mercure/createSubscription.ts index 2f8336a8..68d398e1 100644 --- a/src/mercure/createSubscription.ts +++ b/src/mercure/createSubscription.ts @@ -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)); diff --git a/src/openapi/dataProvider.ts b/src/openapi/dataProvider.ts index e409a88f..a028420d 100644 --- a/src/openapi/dataProvider.ts +++ b/src/openapi/dataProvider.ts @@ -57,7 +57,6 @@ function dataProvider( const mercure: MercureOptions | null = factoryParams.mercure ? { hub: null, - jwt: null, topicUrl: entrypointUrl, ...(factoryParams.mercure === true ? {} : factoryParams.mercure), } diff --git a/src/types.ts b/src/types.ts index 2a67f5e3..f7eb3ee3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 {