Skip to content

Commit 0df9b09

Browse files
authored
fix: fixes #1041: websocket URL ending with / wont work (#1046)
1 parent 2205308 commit 0df9b09

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

packages/provider/src/HocuspocusProvider.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export type HocuspocusProviderConfiguration = Required<
3636
> &
3737
Partial<CompleteHocuspocusProviderConfiguration> &
3838
(
39-
| Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">>
39+
| (Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">> &
40+
Partial<
41+
Pick<
42+
CompleteHocuspocusProviderWebsocketConfiguration,
43+
"preserveTrailingSlash"
44+
>
45+
>)
4046
| Required<
4147
Pick<CompleteHocuspocusProviderConfiguration, "websocketProvider">
4248
>
@@ -222,12 +228,10 @@ export class HocuspocusProvider extends EventEmitter {
222228
configuration: Partial<HocuspocusProviderConfiguration> = {},
223229
): void {
224230
if (!configuration.websocketProvider) {
225-
const websocketProviderConfig =
226-
configuration as CompleteHocuspocusProviderWebsocketConfiguration;
227231
this.manageSocket = true;
228-
this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
229-
url: websocketProviderConfig.url,
230-
});
232+
this.configuration.websocketProvider = new HocuspocusProviderWebsocket(
233+
configuration as CompleteHocuspocusProviderWebsocketConfiguration,
234+
);
231235
}
232236

233237
this.configuration = { ...this.configuration, ...configuration };
@@ -310,7 +314,9 @@ export class HocuspocusProvider extends EventEmitter {
310314
try {
311315
token = await this.getToken();
312316
} catch (error) {
313-
this.permissionDeniedHandler(`Failed to get token during sendToken(): ${error}`);
317+
this.permissionDeniedHandler(
318+
`Failed to get token during sendToken(): ${error}`,
319+
);
314320
return;
315321
}
316322

packages/provider/src/HocuspocusProviderWebsocket.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export interface CompleteHocuspocusProviderWebsocketConfiguration {
3737
*/
3838
url: string;
3939

40+
/**
41+
* By default, trailing slashes are removed from the URL. Set this to true
42+
* to preserve trailing slashes if your server configuration requires them.
43+
*/
44+
preserveTrailingSlash: boolean;
45+
4046
/**
4147
* An optional WebSocket polyfill, for example for Node.js
4248
*/
@@ -102,6 +108,7 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
102108
public configuration: CompleteHocuspocusProviderWebsocketConfiguration = {
103109
url: "",
104110
autoConnect: true,
111+
preserveTrailingSlash: false,
105112
// @ts-ignore
106113
document: undefined,
107114
WebSocketPolyfill: undefined,
@@ -434,13 +441,18 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
434441
}
435442
}
436443

437-
// Ensure that the URL never ends with /
438444
get serverUrl() {
439-
while (this.configuration.url[this.configuration.url.length - 1] === "/") {
440-
return this.configuration.url.slice(0, this.configuration.url.length - 1);
445+
if (this.configuration.preserveTrailingSlash) {
446+
return this.configuration.url;
447+
}
448+
449+
// By default, ensure that the URL never ends with /
450+
let url = this.configuration.url;
451+
while (url[url.length - 1] === "/") {
452+
url = url.slice(0, url.length - 1);
441453
}
442454

443-
return this.configuration.url;
455+
return url;
444456
}
445457

446458
get url() {

0 commit comments

Comments
 (0)