Skip to content

Commit

Permalink
chore: remove deprecated extraOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Reining <lukas.reining@codecentric.de>

# Conflicts:
#	src/eventsource.ts
  • Loading branch information
lukas-reining committed Dec 15, 2024
1 parent 6e35837 commit f900b8d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
1 change: 1 addition & 0 deletions eventsource.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="@zxing" level="application" />
</component>
</module>
26 changes: 9 additions & 17 deletions src/eventsource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { http, HttpResponse as MswHttpResponse } from 'msw';
import { server } from '../mocks/node';
import { CustomEventSource as EventSource, CustomEvent } from './eventsource';
import { CustomEvent, CustomEventSource as EventSource } from './eventsource';
import DoneCallback = jest.DoneCallback;

describe('EventSource', () => {
Expand Down Expand Up @@ -52,15 +52,10 @@ describe('EventSource', () => {
return globalThis.fetch(input, init);
}) as typeof fetch;

const ev = new EventSource(
'http://localhost/sse',
{
disableRetry: true,
},
{
fetchInput: fetchFn,
},
);
const ev = new EventSource('http://localhost/sse', {
disableRetry: true,
fetch: fetchFn,
});

ev.onopen = (event) => {
expect(event).toBeInstanceOf(Event);
Expand All @@ -76,13 +71,10 @@ describe('EventSource', () => {
return globalThis.fetch(input, init);
}) as typeof fetch;

const ev = new EventSource(
'http://localhost/sse',
{
disableRetry: true,
fetch: fetchFn,
},
);
const ev = new EventSource('http://localhost/sse', {
disableRetry: true,
fetch: fetchFn,
});

ev.onopen = (event) => {
expect(event).toBeInstanceOf(Event);
Expand Down
43 changes: 22 additions & 21 deletions src/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ export type EventSourceOptions = {
* Fetch implementation to use for connecting. Defaults to {@link globalThis.fetch}
*/
fetch?: typeof fetch;
} & Omit<RequestInit, 'cache' | 'credentials' | 'signal'>;

/**
* @deprecated
*/
export type EventSourceExtraOptions = {
/**
* @deprecated Use {@link EventSourceOptions#fetch} instead
* Sets the fetch credential mode to `omit` instead of `same-site`.
* If {@link EventSourceInit.withCredentials} is set, {@link omitCredentials} will take precedence and the `credential` will be set to `omit`.
*/
fetchInput?: typeof fetch;
};
omitCredentials?: boolean;
} & Omit<RequestInit, 'cache' | 'credentials' | 'signal'>;

export type CustomEvent = Event & {
response?: Response;
};
export type CustomEvent = Event & {
response?: Response;
};

export class CustomEventSource extends EventTarget implements EventSource {
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url
Expand All @@ -67,7 +63,6 @@ export class CustomEventSource extends EventTarget implements EventSource {
| null = null;

public readonly options: EventSourceInit & EventSourceOptions;
private readonly extraOptions?: EventSourceExtraOptions;
private abortController?: AbortController;
private timeoutId: ReturnType<typeof setTimeout> | undefined = undefined;
private retry: number;
Expand All @@ -78,21 +73,22 @@ export class CustomEventSource extends EventTarget implements EventSource {
constructor(
url: string | URL,
initDict?: EventSourceInit & EventSourceOptions,
/**
* @deprecated Use the related options in initDict
*/
extraOptions?: EventSourceExtraOptions,
) {
super();
this.options = initDict ?? {};
this.extraOptions = extraOptions;
this.url = url instanceof URL ? url.toString() : url;
this.retry = initDict?.retry ?? 5000;

if (!this.options.disableLogger) {
this.logger = this.options.logger ?? new ConsoleLogger();
}

if (this.options.omitCredentials && this.options.withCredentials) {
this.logger?.warn(
'omitCredentials and withCredentials have been set to true. withCredentials will be ignored and credentials will not be sent!',
);
}

this.connect();
}

Expand Down Expand Up @@ -131,14 +127,16 @@ export class CustomEventSource extends EventTarget implements EventSource {
Accept: ContentTypeEventStream,
},
cache: 'no-store',
credentials: this.withCredentials ? 'include' : 'omit',
credentials: this.options.omitCredentials
? 'omit'
: this.withCredentials
? 'include'
: 'same-origin',
signal: this.abortController?.signal,
};

const response = this.options.fetch
? await this.options.fetch(this.url, fetchOptions)
: this.extraOptions?.fetchInput
? await this.extraOptions.fetchInput(this.url, fetchOptions)
: await globalThis.fetch(this.url, fetchOptions);

// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource (Step 15)
Expand All @@ -157,7 +155,10 @@ export class CustomEventSource extends EventTarget implements EventSource {
response,
);
} else if (!response?.body) {
return this.failConnection(`Request failed with empty response body'`, response);
return this.failConnection(
`Request failed with empty response body'`,
response,
);
}

this.announceConnection(response);
Expand Down

0 comments on commit f900b8d

Please sign in to comment.