Skip to content

Commit

Permalink
Fix WS connection getting closed
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Mar 27, 2024
1 parent 680d11e commit f3d90fc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
60 changes: 44 additions & 16 deletions app/common/UserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export class UserAPIImpl extends BaseAPI implements UserAPI {
method: 'GET',
credentials: 'include'
});
return getDocWorkerUrl(this._homeUrl, json);
return getPublicDocWorkerUrl(this._homeUrl, json);
}

public async getWorkerAPI(key: string): Promise<DocWorkerAPI> {
Expand Down Expand Up @@ -1112,6 +1112,22 @@ export class DocAPIImpl extends BaseAPI implements DocAPI {
}
}

interface DocWorkerInfo {
docWorkerUrl: string|null;
internalDocWorkerUrl: string|null;
selfPrefix?: string;
}

function getUrlFromPrefix(homeUrl: string, prefix?: string) {
if (!prefix) {
// This should never happen.
throw new Error('missing selfPrefix for docWorkerUrl');
}
const url = new URL(homeUrl);
url.pathname = prefix + url.pathname;
return url.href;
}

/**
* Get a docWorkerUrl from information returned from backend. When the backend
* is fully configured, and there is a pool of workers, this is straightforward,
Expand All @@ -1120,20 +1136,32 @@ export class DocAPIImpl extends BaseAPI implements DocAPI {
* use the homeUrl of the backend, with extra path prefix information
* given by selfPrefix. At the time of writing, the selfPrefix contains a
* doc-worker id, and a tag for the codebase (used in consistency checks).
*
* @param {string} homeUrl
* @param {object} docWorkerInfo
* @param {string} docWorkerInfo.docWorkerUrl The public doc Worker URL
* @param {string} docWorkerInfo.internalDocWorkerUrl The internal doc Worker URL
* @param {string|undefined} docWorkerInfo.selfPrefix
* @param {string} urlType The type of doc worker url to extract from the docWorkerInfo
*/
export function getDocWorkerUrl(homeUrl: string, docWorkerInfo: {
docWorkerUrl: string|null,
internalDocWorkerUrl: string|null,
selfPrefix?: string,
}): string {
if (!docWorkerInfo.docWorkerUrl) {
if (!docWorkerInfo.selfPrefix) {
// This should never happen.
throw new Error('missing selfPrefix for docWorkerUrl');
}
const url = new URL(homeUrl);
url.pathname = docWorkerInfo.selfPrefix + url.pathname;
return url.href;
}
return docWorkerInfo.internalDocWorkerUrl || docWorkerInfo.docWorkerUrl;
export function getPublicDocWorkerUrl(homeUrl: string, docWorkerInfo: DocWorkerInfo) {
const publicUrl = docWorkerInfo.docWorkerUrl;
return publicUrl || getUrlFromPrefix(homeUrl, docWorkerInfo.selfPrefix);
}

/**
* @see getPublicDocWorkerUrl
* Like getPublicDocWorkerUrl but returns the URL resolvable internally (behind a Reverse Proxy).
* Especially useful when and where the public url cannot be resolved.
*
* @param {string} homeUrl
* @param {object} docWorkerInfo
* @param {string} docWorkerInfo.docWorkerUrl The public doc Worker URL
* @param {string} docWorkerInfo.internalDocWorkerUrl The internal doc Worker URL
* @param {string|undefined} docWorkerInfo.selfPrefix
* @param {string} urlType The type of doc worker url to extract from the docWorkerInfo
*/
export function getInternalDocWorkerUrl(homeUrl: string, docWorkerInfo: DocWorkerInfo) {
const internalUrl = docWorkerInfo.internalDocWorkerUrl;
return internalUrl || getUrlFromPrefix(homeUrl, docWorkerInfo.selfPrefix);
}
4 changes: 2 additions & 2 deletions app/server/lib/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ApiError} from 'app/common/ApiError';
import {InactivityTimer} from 'app/common/InactivityTimer';
import {FetchUrlOptions, FileUploadResult, UPLOAD_URL_PATH, UploadResult} from 'app/common/uploads';
import {getDocWorkerUrl} from 'app/common/UserAPI';
import {getInternalDocWorkerUrl} from 'app/common/UserAPI';
import {getAuthorizedUserId, getTransitiveHeaders, getUserId, isSingleUserMode,
RequestWithLogin} from 'app/server/lib/Authorizer';
import {expressWrap} from 'app/server/lib/expressWrap';
Expand Down Expand Up @@ -415,7 +415,7 @@ export async function fetchDoc(server: GristServer, docId: string, req: Request,
const fetchUrl = new URL(`/api/worker/${docId}`, homeUrl);
const response: FetchResponse = await Deps.fetch(fetchUrl.href, {headers});
await _checkForError(response);
const docWorkerUrl = getDocWorkerUrl(server.getOwnUrl(), await response.json());
const docWorkerUrl = getInternalDocWorkerUrl(server.getOwnUrl(), await response.json());
// Download the document, in full or as a template.
const url = new URL(`api/docs/${docId}/download?template=${Number(template)}`,
docWorkerUrl.replace(/\/*$/, '/'));
Expand Down

0 comments on commit f3d90fc

Please sign in to comment.