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

fix!: change credentials mode from "omit" to "same-origin" when set to false #27

Merged
merged 1 commit into from
Dec 15, 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
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>
29 changes: 24 additions & 5 deletions src/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export type EventSourceOptions = {
* Fetch implementation to use for connecting. Defaults to {@link globalThis.fetch}
*/
fetch?: typeof fetch;

/**
* 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`.
*/
omitCredentials?: boolean;
} & Omit<RequestInit, 'cache' | 'credentials' | 'signal'>;

/**
Expand All @@ -40,9 +46,9 @@ export type EventSourceExtraOptions = {
fetchInput?: typeof fetch;
};

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 Down Expand Up @@ -93,6 +99,12 @@ export class CustomEventSource extends EventTarget implements EventSource {
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,7 +143,11 @@ 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,
};

Expand All @@ -157,7 +173,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
Loading