552 lines
20 KiB
JavaScript
552 lines
20 KiB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
/*
|
|
Copyright 2022-2024 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import { logger } from "./logger.js";
|
|
import { TypedEventEmitter } from "./models/typed-event-emitter.js";
|
|
import { sleep } from "./utils.js";
|
|
// /sync requests allow you to set a timeout= but the request may continue
|
|
// beyond that and wedge forever, so we need to track how long we are willing
|
|
// to keep open the connection. This constant is *ADDED* to the timeout= value
|
|
// to determine the max time we're willing to wait.
|
|
const BUFFER_PERIOD_MS = 10 * 1000;
|
|
export const MSC3575_WILDCARD = "*";
|
|
export const MSC3575_STATE_KEY_ME = "$ME";
|
|
export const MSC3575_STATE_KEY_LAZY = "$LAZY";
|
|
|
|
/**
|
|
* Represents a subscription to a room or set of rooms. Controls which events are returned.
|
|
*/
|
|
|
|
/**
|
|
* Controls which rooms are returned in a given list.
|
|
*/
|
|
|
|
/**
|
|
* Represents a list subscription.
|
|
*/
|
|
|
|
/**
|
|
* A complete Sliding Sync request.
|
|
*/
|
|
|
|
/**
|
|
* New format of hero introduced in MSC4186 with display name and avatar URL
|
|
* in addition to just user_id (as it is on the wire, with underscores)
|
|
* as opposed to Hero in room-summary.ts which has fields in camelCase
|
|
* (and also a flag to note what format the hero came from).
|
|
*/
|
|
|
|
/**
|
|
* A complete Sliding Sync response
|
|
*/
|
|
|
|
export let SlidingSyncState = /*#__PURE__*/function (SlidingSyncState) {
|
|
/**
|
|
* Fired by SlidingSyncEvent.Lifecycle event immediately before processing the response.
|
|
*/
|
|
SlidingSyncState["RequestFinished"] = "FINISHED";
|
|
/**
|
|
* Fired by SlidingSyncEvent.Lifecycle event immediately after all room data listeners have been
|
|
* invoked, but before list listeners.
|
|
*/
|
|
SlidingSyncState["Complete"] = "COMPLETE";
|
|
return SlidingSyncState;
|
|
}({});
|
|
|
|
/**
|
|
* Internal Class. SlidingList represents a single list in sliding sync. The list can have filters,
|
|
* multiple sliding windows, and maintains the index-\>room_id mapping.
|
|
*/
|
|
class SlidingList {
|
|
/**
|
|
* Construct a new sliding list.
|
|
* @param list - The range, sort and filter values to use for this list.
|
|
*/
|
|
constructor(list) {
|
|
_defineProperty(this, "list", void 0);
|
|
_defineProperty(this, "isModified", void 0);
|
|
// returned data
|
|
_defineProperty(this, "joinedCount", 0);
|
|
this.replaceList(list);
|
|
}
|
|
|
|
/**
|
|
* Mark this list as modified or not. Modified lists will return sticky params with calls to getList.
|
|
* This is useful for the first time the list is sent, or if the list has changed in some way.
|
|
* @param modified - True to mark this list as modified so all sticky parameters will be re-sent.
|
|
*/
|
|
setModified(modified) {
|
|
this.isModified = modified;
|
|
}
|
|
|
|
/**
|
|
* Update the list range for this list. Does not affect modified status as list ranges are non-sticky.
|
|
* @param newRanges - The new ranges for the list
|
|
*/
|
|
updateListRange(newRanges) {
|
|
this.list.ranges = JSON.parse(JSON.stringify(newRanges));
|
|
}
|
|
|
|
/**
|
|
* Replace list parameters. All fields will be replaced with the new list parameters.
|
|
* @param list - The new list parameters
|
|
*/
|
|
replaceList(list) {
|
|
list.filters = list.filters ?? {};
|
|
list.ranges = list.ranges ?? [];
|
|
this.list = JSON.parse(JSON.stringify(list));
|
|
this.isModified = true;
|
|
|
|
// reset values as the join count may be very different (if filters changed) including the rooms
|
|
// (e.g. sort orders or sliding window ranges changed)
|
|
|
|
// the total number of joined rooms according to the server, always >= len(roomIndexToRoomId)
|
|
this.joinedCount = 0;
|
|
}
|
|
|
|
/**
|
|
* Return a copy of the list suitable for a request body.
|
|
* @param forceIncludeAllParams - True to forcibly include all params even if the list
|
|
* hasn't been modified. Callers may want to do this if they are modifying the list prior to calling
|
|
* updateList.
|
|
*/
|
|
getList(forceIncludeAllParams) {
|
|
let list = {
|
|
ranges: JSON.parse(JSON.stringify(this.list.ranges))
|
|
};
|
|
if (this.isModified || forceIncludeAllParams) {
|
|
list = JSON.parse(JSON.stringify(this.list));
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* When onResponse extensions should be invoked: before or after processing the main response.
|
|
*/
|
|
export let ExtensionState = /*#__PURE__*/function (ExtensionState) {
|
|
// Call onResponse before processing the response body. This is useful when your extension is
|
|
// preparing the ground for the response body e.g. processing to-device messages before the
|
|
// encrypted event arrives.
|
|
ExtensionState["PreProcess"] = "ExtState.PreProcess";
|
|
// Call onResponse after processing the response body. This is useful when your extension is
|
|
// decorating data from the client, and you rely on MatrixClient.getRoom returning the Room object
|
|
// e.g. room account data.
|
|
ExtensionState["PostProcess"] = "ExtState.PostProcess";
|
|
return ExtensionState;
|
|
}({});
|
|
|
|
/**
|
|
* An interface that must be satisfied to register extensions
|
|
*/
|
|
|
|
/**
|
|
* Events which can be fired by the SlidingSync class. These are designed to provide different levels
|
|
* of information when processing sync responses.
|
|
* - RoomData: concerns rooms, useful for SlidingSyncSdk to update its knowledge of rooms.
|
|
* - Lifecycle: concerns callbacks at various well-defined points in the sync process.
|
|
* Specifically, the order of event invocation is:
|
|
* - Lifecycle (state=RequestFinished)
|
|
* - RoomData (N times)
|
|
* - Lifecycle (state=Complete)
|
|
*/
|
|
export let SlidingSyncEvent = /*#__PURE__*/function (SlidingSyncEvent) {
|
|
/**
|
|
* This event fires when there are updates for a room. Fired as and when rooms are encountered
|
|
* in the response.
|
|
*/
|
|
SlidingSyncEvent["RoomData"] = "SlidingSync.RoomData";
|
|
/**
|
|
* This event fires at various points in the /sync loop lifecycle.
|
|
* - SlidingSyncState.RequestFinished: Fires after we receive a valid response but before the
|
|
* response has been processed. Perform any pre-process steps here. If there was a problem syncing,
|
|
* `err` will be set (e.g network errors).
|
|
* - SlidingSyncState.Complete: Fires after the response has been processed.
|
|
*/
|
|
SlidingSyncEvent["Lifecycle"] = "SlidingSync.Lifecycle";
|
|
return SlidingSyncEvent;
|
|
}({});
|
|
/**
|
|
* SlidingSync is a high-level data structure which controls the majority of sliding sync.
|
|
* It has no hooks into JS SDK except for needing a MatrixClient to perform the HTTP request.
|
|
* This means this class (and everything it uses) can be used in isolation from JS SDK if needed.
|
|
* To hook this up with the JS SDK, you need to use SlidingSyncSdk.
|
|
*/
|
|
export class SlidingSync extends TypedEventEmitter {
|
|
/**
|
|
* Create a new sliding sync instance
|
|
* @param proxyBaseUrl - The base URL of the sliding sync proxy
|
|
* @param lists - The lists to use for sliding sync.
|
|
* @param roomSubscriptionInfo - The params to use for room subscriptions.
|
|
* @param client - The client to use for /sync calls.
|
|
* @param timeoutMS - The number of milliseconds to wait for a response.
|
|
*/
|
|
constructor(proxyBaseUrl, lists, roomSubscriptionInfo, client, timeoutMS) {
|
|
super();
|
|
_defineProperty(this, "lists", void 0);
|
|
_defineProperty(this, "listModifiedCount", 0);
|
|
_defineProperty(this, "terminated", false);
|
|
// flag set when resend() is called because we cannot rely on detecting AbortError in JS SDK :(
|
|
_defineProperty(this, "needsResend", false);
|
|
// map of extension name to req/resp handler
|
|
_defineProperty(this, "extensions", {});
|
|
_defineProperty(this, "desiredRoomSubscriptions", new Set());
|
|
// the *desired* room subscriptions
|
|
_defineProperty(this, "confirmedRoomSubscriptions", new Set());
|
|
// map of custom subscription name to the subscription
|
|
_defineProperty(this, "customSubscriptions", new Map());
|
|
// map of room ID to custom subscription name
|
|
_defineProperty(this, "roomIdToCustomSubscription", new Map());
|
|
_defineProperty(this, "pendingReq", void 0);
|
|
_defineProperty(this, "abortController", void 0);
|
|
this.proxyBaseUrl = proxyBaseUrl;
|
|
this.roomSubscriptionInfo = roomSubscriptionInfo;
|
|
this.client = client;
|
|
this.timeoutMS = timeoutMS;
|
|
this.lists = new Map();
|
|
lists.forEach((list, key) => {
|
|
this.lists.set(key, new SlidingList(list));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Add a custom room subscription, referred to by an arbitrary name. If a subscription with this
|
|
* name already exists, it is replaced. No requests are sent by calling this method.
|
|
* @param name - The name of the subscription. Only used to reference this subscription in
|
|
* useCustomSubscription.
|
|
* @param sub - The subscription information.
|
|
*/
|
|
addCustomSubscription(name, sub) {
|
|
if (this.customSubscriptions.has(name)) {
|
|
logger.warn(`addCustomSubscription: ${name} already exists as a custom subscription, ignoring.`);
|
|
return;
|
|
}
|
|
this.customSubscriptions.set(name, sub);
|
|
}
|
|
|
|
/**
|
|
* Use a custom subscription previously added via addCustomSubscription. No requests are sent
|
|
* by calling this method. Use modifyRoomSubscriptions to resend subscription information.
|
|
* @param roomId - The room to use the subscription in.
|
|
* @param name - The name of the subscription. If this name is unknown, the default subscription
|
|
* will be used.
|
|
*/
|
|
useCustomSubscription(roomId, name) {
|
|
// We already know about this custom subscription, as it is immutable,
|
|
// we don't need to unconfirm the subscription.
|
|
if (this.roomIdToCustomSubscription.get(roomId) === name) {
|
|
return;
|
|
}
|
|
this.roomIdToCustomSubscription.set(roomId, name);
|
|
// unconfirm this subscription so a resend() will send it up afresh.
|
|
this.confirmedRoomSubscriptions.delete(roomId);
|
|
}
|
|
|
|
/**
|
|
* Get the room index data for a list.
|
|
* @param key - The list key
|
|
* @returns The list data which contains the rooms in this list
|
|
*/
|
|
getListData(key) {
|
|
const data = this.lists.get(key);
|
|
if (!data) {
|
|
return null;
|
|
}
|
|
return {
|
|
joinedCount: data.joinedCount
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Get the full request list parameters for a list index. This function is provided for callers to use
|
|
* in conjunction with setList to update fields on an existing list.
|
|
* @param key - The list key to get the params for.
|
|
* @returns A copy of the list params or undefined.
|
|
*/
|
|
getListParams(key) {
|
|
const params = this.lists.get(key);
|
|
if (!params) {
|
|
return null;
|
|
}
|
|
return params.getList(true);
|
|
}
|
|
|
|
/**
|
|
* Set new ranges for an existing list. Calling this function when _only_ the ranges have changed
|
|
* is more efficient than calling setList(index,list) as this function won't resend sticky params,
|
|
* whereas setList always will.
|
|
* @param key - The list key to modify
|
|
* @param ranges - The new ranges to apply.
|
|
* @returns A promise which resolves to the transaction ID when it has been received down sync
|
|
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
|
|
* immediately after sending, in which case the action will be applied in the subsequent request)
|
|
*/
|
|
setListRanges(key, ranges) {
|
|
const list = this.lists.get(key);
|
|
if (!list) {
|
|
throw new Error("no list with key " + key);
|
|
}
|
|
list.updateListRange(ranges);
|
|
this.resend();
|
|
}
|
|
|
|
/**
|
|
* Add or replace a list. Calling this function will interrupt the /sync request to resend new
|
|
* lists.
|
|
* @param key - The key to modify
|
|
* @param list - The new list parameters.
|
|
* @returns A promise which resolves to the transaction ID when it has been received down sync
|
|
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
|
|
* immediately after sending, in which case the action will be applied in the subsequent request)
|
|
*/
|
|
setList(key, list) {
|
|
const existingList = this.lists.get(key);
|
|
if (existingList) {
|
|
existingList.replaceList(list);
|
|
this.lists.set(key, existingList);
|
|
} else {
|
|
this.lists.set(key, new SlidingList(list));
|
|
}
|
|
this.listModifiedCount += 1;
|
|
this.resend();
|
|
}
|
|
|
|
/**
|
|
* Get the room subscriptions for the sync API.
|
|
* @returns A copy of the desired room subscriptions.
|
|
*/
|
|
getRoomSubscriptions() {
|
|
return new Set(Array.from(this.desiredRoomSubscriptions));
|
|
}
|
|
|
|
/**
|
|
* Modify the room subscriptions for the sync API. Calling this function will interrupt the
|
|
* /sync request to resend new subscriptions. If the /sync stream has not started, this will
|
|
* prepare the room subscriptions for when start() is called.
|
|
* @param s - The new desired room subscriptions.
|
|
*/
|
|
modifyRoomSubscriptions(s) {
|
|
this.desiredRoomSubscriptions = s;
|
|
this.resend();
|
|
}
|
|
|
|
/**
|
|
* Modify which events to retrieve for room subscriptions. Invalidates all room subscriptions
|
|
* such that they will be sent up afresh.
|
|
* @param rs - The new room subscription fields to fetch.
|
|
*/
|
|
modifyRoomSubscriptionInfo(rs) {
|
|
this.roomSubscriptionInfo = rs;
|
|
this.confirmedRoomSubscriptions = new Set();
|
|
this.resend();
|
|
}
|
|
|
|
/**
|
|
* Register an extension to send with the /sync request.
|
|
* @param ext - The extension to register.
|
|
*/
|
|
registerExtension(ext) {
|
|
if (this.extensions[ext.name()]) {
|
|
throw new Error(`registerExtension: ${ext.name()} already exists as an extension`);
|
|
}
|
|
this.extensions[ext.name()] = ext;
|
|
}
|
|
async getExtensionRequest(isInitial) {
|
|
const ext = {};
|
|
// oxlint-disable-next-line guard-for-in
|
|
for (const extName in this.extensions) {
|
|
ext[extName] = await this.extensions[extName].onRequest(isInitial);
|
|
}
|
|
return ext;
|
|
}
|
|
async onPreExtensionsResponse(ext) {
|
|
await Promise.all(Object.keys(ext).map(async extName => {
|
|
if (this.extensions[extName].when() == ExtensionState.PreProcess) {
|
|
await this.extensions[extName].onResponse(ext[extName]);
|
|
}
|
|
}));
|
|
}
|
|
async onPostExtensionsResponse(ext) {
|
|
await Promise.all(Object.keys(ext).map(async extName => {
|
|
if (this.extensions[extName].when() == ExtensionState.PostProcess) {
|
|
await this.extensions[extName].onResponse(ext[extName]);
|
|
}
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Invoke all attached room data listeners.
|
|
* @param roomId - The room which received some data.
|
|
* @param roomData - The raw sliding sync response JSON.
|
|
*/
|
|
async invokeRoomDataListeners(roomId, roomData) {
|
|
if (!roomData.required_state) {
|
|
roomData.required_state = [];
|
|
}
|
|
if (!roomData.timeline) {
|
|
roomData.timeline = [];
|
|
}
|
|
await this.emitPromised(SlidingSyncEvent.RoomData, roomId, roomData);
|
|
}
|
|
|
|
/**
|
|
* Invoke all attached lifecycle listeners.
|
|
* @param state - The Lifecycle state
|
|
* @param resp - The raw sync response JSON
|
|
* @param err - Any error that occurred when making the request e.g. network errors.
|
|
*/
|
|
invokeLifecycleListeners(state, resp, err) {
|
|
this.emit(SlidingSyncEvent.Lifecycle, state, resp, err);
|
|
}
|
|
|
|
/**
|
|
* Resend a Sliding Sync request. Used when something has changed in the request.
|
|
*/
|
|
resend() {
|
|
this.needsResend = true;
|
|
this.abortController?.abort();
|
|
this.abortController = new AbortController();
|
|
}
|
|
|
|
/**
|
|
* Stop syncing with the server.
|
|
*/
|
|
stop() {
|
|
this.terminated = true;
|
|
this.abortController?.abort();
|
|
// remove all listeners so things can be GC'd
|
|
this.removeAllListeners(SlidingSyncEvent.Lifecycle);
|
|
this.removeAllListeners(SlidingSyncEvent.RoomData);
|
|
}
|
|
|
|
/**
|
|
* Re-setup this connection e.g in the event of an expired session.
|
|
*/
|
|
resetup() {
|
|
logger.warn("SlidingSync: resetting connection info");
|
|
// resend sticky params and de-confirm all subscriptions
|
|
this.lists.forEach(l => {
|
|
l.setModified(true);
|
|
});
|
|
this.confirmedRoomSubscriptions = new Set(); // leave desired ones alone though!
|
|
// reset the connection as we might be wedged
|
|
this.resend();
|
|
}
|
|
|
|
/**
|
|
* Start syncing with the server. Blocks until stopped.
|
|
*/
|
|
async start() {
|
|
this.abortController = new AbortController();
|
|
let currentPos;
|
|
while (!this.terminated) {
|
|
this.needsResend = false;
|
|
let resp;
|
|
try {
|
|
const reqLists = {};
|
|
this.lists.forEach((l, key) => {
|
|
reqLists[key] = l.getList(true);
|
|
});
|
|
const reqBody = {
|
|
lists: reqLists,
|
|
pos: currentPos,
|
|
timeout: this.timeoutMS,
|
|
clientTimeout: this.timeoutMS + BUFFER_PERIOD_MS,
|
|
extensions: await this.getExtensionRequest(currentPos === undefined)
|
|
};
|
|
// check if we are (un)subscribing to a room and modify request this one time for it
|
|
const newSubscriptions = difference(this.desiredRoomSubscriptions, this.confirmedRoomSubscriptions);
|
|
const unsubscriptions = difference(this.confirmedRoomSubscriptions, this.desiredRoomSubscriptions);
|
|
if (unsubscriptions.size > 0) {
|
|
reqBody.unsubscribe_rooms = Array.from(unsubscriptions);
|
|
}
|
|
if (newSubscriptions.size > 0) {
|
|
reqBody.room_subscriptions = {};
|
|
for (const roomId of newSubscriptions) {
|
|
const customSubName = this.roomIdToCustomSubscription.get(roomId);
|
|
let sub = this.roomSubscriptionInfo;
|
|
if (customSubName && this.customSubscriptions.has(customSubName)) {
|
|
sub = this.customSubscriptions.get(customSubName);
|
|
}
|
|
reqBody.room_subscriptions[roomId] = sub;
|
|
}
|
|
}
|
|
this.pendingReq = this.client.slidingSync(reqBody, this.proxyBaseUrl, this.abortController.signal);
|
|
resp = await this.pendingReq;
|
|
currentPos = resp.pos;
|
|
// update what we think we're subscribed to.
|
|
for (const roomId of newSubscriptions) {
|
|
this.confirmedRoomSubscriptions.add(roomId);
|
|
}
|
|
for (const roomId of unsubscriptions) {
|
|
this.confirmedRoomSubscriptions.delete(roomId);
|
|
}
|
|
// mark all these lists as having been sent as sticky so we don't keep sending sticky params
|
|
this.lists.forEach(l => {
|
|
l.setModified(false);
|
|
});
|
|
// set default empty values so we don't need to null check
|
|
resp.lists = resp.lists ?? {};
|
|
resp.rooms = resp.rooms ?? {};
|
|
resp.extensions = resp.extensions ?? {};
|
|
Object.keys(resp.lists).forEach(key => {
|
|
const list = this.lists.get(key);
|
|
if (!list || !resp) {
|
|
return;
|
|
}
|
|
list.joinedCount = resp.lists[key].count;
|
|
});
|
|
this.invokeLifecycleListeners(SlidingSyncState.RequestFinished, resp);
|
|
} catch (err) {
|
|
if (err.httpStatus) {
|
|
this.invokeLifecycleListeners(SlidingSyncState.RequestFinished, null, err);
|
|
if (err.httpStatus === 400) {
|
|
// session probably expired TODO: assign an errcode
|
|
// so drop state and re-request
|
|
this.resetup();
|
|
currentPos = undefined;
|
|
await sleep(50); // in case the 400 was for something else; don't tightloop
|
|
continue;
|
|
} // else fallthrough to generic error handling
|
|
} else if (this.needsResend || err.name === "AbortError") {
|
|
continue; // don't sleep as we caused this error by abort()ing the request.
|
|
}
|
|
logger.error(err);
|
|
await sleep(5000);
|
|
}
|
|
if (!resp) {
|
|
continue;
|
|
}
|
|
await this.onPreExtensionsResponse(resp.extensions);
|
|
|
|
// oxlint-disable-next-line guard-for-in
|
|
for (const roomId in resp.rooms) {
|
|
await this.invokeRoomDataListeners(roomId, resp.rooms[roomId]);
|
|
}
|
|
this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
|
|
await this.onPostExtensionsResponse(resp.extensions);
|
|
}
|
|
}
|
|
}
|
|
const difference = (setA, setB) => {
|
|
const diff = new Set(setA);
|
|
for (const elem of setB) {
|
|
diff.delete(elem);
|
|
}
|
|
return diff;
|
|
};
|
|
//# sourceMappingURL=sliding-sync.js.map
|