diff --git a/src/content-repo.ts b/src/content-repo.ts index 257541296..d3130aa72 100644 --- a/src/content-repo.ts +++ b/src/content-repo.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import * as utils from "./utils"; +import { encodeParams } from "./utils"; /** * Get the HTTP URL for an MXC URI. @@ -74,6 +74,6 @@ export function getHttpUriForMxc( serverAndMediaId = serverAndMediaId.slice(0, fragmentOffset); } - const urlParams = Object.keys(params).length === 0 ? "" : "?" + utils.encodeParams(params); + const urlParams = Object.keys(params).length === 0 ? "" : "?" + encodeParams(params); return baseUrl + prefix + serverAndMediaId + urlParams + fragment; } diff --git a/src/crypto/store/indexeddb-crypto-store-backend.ts b/src/crypto/store/indexeddb-crypto-store-backend.ts index 7827697ec..228418091 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.ts +++ b/src/crypto/store/indexeddb-crypto-store-backend.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { logger, PrefixedLogger } from "../../logger"; -import * as utils from "../../utils"; +import { deepCompare } from "../../utils"; import { CryptoStore, IDeviceData, @@ -158,7 +158,7 @@ export class Backend implements CryptoStore { const existing = cursor.value; - if (utils.deepCompare(existing.requestBody, requestBody)) { + if (deepCompare(existing.requestBody, requestBody)) { // got a match callback(existing); return; diff --git a/src/crypto/store/memory-crypto-store.ts b/src/crypto/store/memory-crypto-store.ts index d4e154d39..a78574850 100644 --- a/src/crypto/store/memory-crypto-store.ts +++ b/src/crypto/store/memory-crypto-store.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { logger } from "../../logger"; -import * as utils from "../../utils"; +import { deepCompare, promiseTry } from "../../utils"; import { CryptoStore, IDeviceData, @@ -90,7 +90,7 @@ export class MemoryCryptoStore implements CryptoStore { public getOrAddOutgoingRoomKeyRequest(request: OutgoingRoomKeyRequest): Promise { const requestBody = request.requestBody; - return utils.promiseTry(() => { + return promiseTry(() => { // first see if we already have an entry for this request. const existing = this._getOutgoingRoomKeyRequest(requestBody); @@ -138,7 +138,7 @@ export class MemoryCryptoStore implements CryptoStore { // eslint-disable-next-line @typescript-eslint/naming-convention private _getOutgoingRoomKeyRequest(requestBody: IRoomKeyRequestBody): OutgoingRoomKeyRequest | null { for (const existing of this.outgoingRoomKeyRequests) { - if (utils.deepCompare(existing.requestBody, requestBody)) { + if (deepCompare(existing.requestBody, requestBody)) { return existing; } } diff --git a/src/http-api/fetch.ts b/src/http-api/fetch.ts index e1c266c64..5b0f0a1ba 100644 --- a/src/http-api/fetch.ts +++ b/src/http-api/fetch.ts @@ -18,7 +18,7 @@ limitations under the License. * This is an internal module. See {@link MatrixHttpApi} for the public class. */ -import * as utils from "../utils"; +import { checkObjectHasKeys, encodeParams } from "../utils"; import { TypedEventEmitter } from "../models/typed-event-emitter"; import { Method } from "./method"; import { ConnectionError, MatrixError } from "./errors"; @@ -45,7 +45,7 @@ export class FetchHttpApi { private eventEmitter: TypedEventEmitter, public readonly opts: O, ) { - utils.checkObjectHasKeys(opts, ["baseUrl", "prefix"]); + checkObjectHasKeys(opts, ["baseUrl", "prefix"]); opts.onlyData = !!opts.onlyData; opts.useAuthorizationHeader = opts.useAuthorizationHeader ?? true; } @@ -304,7 +304,7 @@ export class FetchHttpApi { public getUrl(path: string, queryParams?: QueryDict, prefix?: string, baseUrl?: string): URL { const url = new URL((baseUrl ?? this.opts.baseUrl) + (prefix ?? this.opts.prefix) + path); if (queryParams) { - utils.encodeParams(queryParams, url.searchParams); + encodeParams(queryParams, url.searchParams); } return url; } diff --git a/src/http-api/index.ts b/src/http-api/index.ts index c5e8e2a3a..1de1f847b 100644 --- a/src/http-api/index.ts +++ b/src/http-api/index.ts @@ -17,7 +17,7 @@ limitations under the License. import { FetchHttpApi } from "./fetch"; import { FileType, IContentUri, IHttpOpts, Upload, UploadOpts, UploadResponse } from "./interface"; import { MediaPrefix } from "./prefix"; -import * as utils from "../utils"; +import { defer, QueryDict, removeElement } from "../utils"; import * as callbacks from "../realtime-callbacks"; import { Method } from "./method"; import { ConnectionError } from "./errors"; @@ -58,14 +58,14 @@ export class MatrixHttpApi extends FetchHttpApi { total: 0, abortController, } as Upload; - const defer = utils.defer(); + const deferred = defer(); if (global.XMLHttpRequest) { const xhr = new global.XMLHttpRequest(); const timeoutFn = function (): void { xhr.abort(); - defer.reject(new Error("Timeout")); + deferred.reject(new Error("Timeout")); }; // set an initial timeout of 30s; we'll advance it each time we get a progress notification @@ -84,16 +84,16 @@ export class MatrixHttpApi extends FetchHttpApi { } if (xhr.status >= 400) { - defer.reject(parseErrorResponse(xhr, xhr.responseText)); + deferred.reject(parseErrorResponse(xhr, xhr.responseText)); } else { - defer.resolve(JSON.parse(xhr.responseText)); + deferred.resolve(JSON.parse(xhr.responseText)); } } catch (err) { if ((err).name === "AbortError") { - defer.reject(err); + deferred.reject(err); return; } - defer.reject(new ConnectionError("request failed", err)); + deferred.reject(new ConnectionError("request failed", err)); } break; } @@ -131,7 +131,7 @@ export class MatrixHttpApi extends FetchHttpApi { xhr.abort(); }); } else { - const queryParams: utils.QueryDict = {}; + const queryParams: QueryDict = {}; if (includeFilename && fileName) { queryParams.filename = fileName; } @@ -146,16 +146,16 @@ export class MatrixHttpApi extends FetchHttpApi { .then((response) => { return this.opts.onlyData ? response : response.json(); }) - .then(defer.resolve, defer.reject); + .then(deferred.resolve, deferred.reject); } // remove the upload from the list on completion - upload.promise = defer.promise.finally(() => { - utils.removeElement(this.uploads, (elem) => elem === upload); + upload.promise = deferred.promise.finally(() => { + removeElement(this.uploads, (elem) => elem === upload); }); abortController.signal.addEventListener("abort", () => { - utils.removeElement(this.uploads, (elem) => elem === upload); - defer.reject(new DOMException("Aborted", "AbortError")); + removeElement(this.uploads, (elem) => elem === upload); + deferred.reject(new DOMException("Aborted", "AbortError")); }); this.uploads.push(upload); return upload.promise; diff --git a/src/models/read-receipt.ts b/src/models/read-receipt.ts index 5858fe5bb..169977915 100644 --- a/src/models/read-receipt.ts +++ b/src/models/read-receipt.ts @@ -20,7 +20,7 @@ import { WrappedReceipt, } from "../@types/read_receipts"; import { ListenerMap, TypedEventEmitter } from "./typed-event-emitter"; -import * as utils from "../utils"; +import { isSupportedReceiptType } from "../utils"; import { MatrixEvent } from "./event"; import { EventType } from "../@types/event"; import { EventTimelineSet } from "./event-timeline-set"; @@ -267,7 +267,7 @@ export abstract class ReadReceipt< public getUsersReadUpTo(event: MatrixEvent): string[] { return this.getReceiptsForEvent(event) .filter(function (receipt) { - return utils.isSupportedReceiptType(receipt.type); + return isSupportedReceiptType(receipt.type); }) .map(function (receipt) { return receipt.userId; diff --git a/src/models/room-member.ts b/src/models/room-member.ts index 116a93b62..e8fcfa606 100644 --- a/src/models/room-member.ts +++ b/src/models/room-member.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { getHttpUriForMxc } from "../content-repo"; -import * as utils from "../utils"; +import { removeDirectionOverrideChars, removeHiddenChars } from "../utils"; import { User } from "./user"; import { MatrixEvent } from "./event"; import { RoomState } from "./room-state"; @@ -206,8 +206,8 @@ export class RoomMember extends TypedEventEmitter * @returns An array of user IDs or an empty array. */ public getUserIdsWithDisplayName(displayName: string): string[] { - return this.displayNameToUserIds.get(utils.removeHiddenChars(displayName)) ?? []; + return this.displayNameToUserIds.get(removeHiddenChars(displayName)) ?? []; } /** @@ -798,7 +798,7 @@ export class RoomState extends TypedEventEmitter } let requiredLevel = 50; - if (utils.isNumber(powerLevels[action])) { + if (isNumber(powerLevels[action])) { requiredLevel = powerLevels[action]!; } @@ -928,7 +928,7 @@ export class RoomState extends TypedEventEmitter powerLevelsEvent && powerLevelsEvent.getContent() && powerLevelsEvent.getContent().notifications && - utils.isNumber(powerLevelsEvent.getContent().notifications[notifLevelKey]) + isNumber(powerLevelsEvent.getContent().notifications[notifLevelKey]) ) { notifLevel = powerLevelsEvent.getContent().notifications[notifLevelKey]; } @@ -1058,7 +1058,7 @@ export class RoomState extends TypedEventEmitter // We clobber the user_id > name lookup but the name -> [user_id] lookup // means we need to remove that user ID from that array rather than nuking // the lot. - const strippedOldName = utils.removeHiddenChars(oldName); + const strippedOldName = removeHiddenChars(oldName); const existingUserIds = this.displayNameToUserIds.get(strippedOldName); if (existingUserIds) { @@ -1070,7 +1070,7 @@ export class RoomState extends TypedEventEmitter this.userIdsToDisplayNames[userId] = displayName; - const strippedDisplayname = displayName && utils.removeHiddenChars(displayName); + const strippedDisplayname = displayName && removeHiddenChars(displayName); // an empty stripped displayname (undefined/'') will be set to MXID in room-member.js if (strippedDisplayname) { const arr = this.displayNameToUserIds.get(strippedDisplayname) ?? []; diff --git a/src/models/room.ts b/src/models/room.ts index 439bd6819..e16375707 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -24,7 +24,7 @@ import { } from "./event-timeline-set"; import { Direction, EventTimeline } from "./event-timeline"; import { getHttpUriForMxc } from "../content-repo"; -import * as utils from "../utils"; +import { compare, removeElement } from "../utils"; import { normalize, noUnsafeEventProps } from "../utils"; import { IEvent, IThreadBundledRelationship, MatrixEvent, MatrixEventEvent, MatrixEventHandlerMap } from "./event"; import { EventStatus } from "./event-status"; @@ -733,7 +733,7 @@ export class Room extends ReadReceipt { ); } - const removed = utils.removeElement( + const removed = removeElement( this.pendingEventList, function (ev) { return ev.getId() == eventId; @@ -3267,7 +3267,7 @@ export class Room extends ReadReceipt { return true; }); // make sure members have stable order - otherMembers.sort((a, b) => utils.compare(a.userId, b.userId)); + otherMembers.sort((a, b) => compare(a.userId, b.userId)); // only 5 first members, immitate summaryHeroes otherMembers = otherMembers.slice(0, 5); otherNames = otherMembers.map((m) => m.name); diff --git a/src/scheduler.ts b/src/scheduler.ts index e41770249..41612f1c9 100644 --- a/src/scheduler.ts +++ b/src/scheduler.ts @@ -18,11 +18,10 @@ limitations under the License. * This is an internal module which manages queuing, scheduling and retrying * of requests. */ -import * as utils from "./utils"; import { logger } from "./logger"; import { MatrixEvent } from "./models/event"; import { EventType } from "./@types/event"; -import { IDeferred } from "./utils"; +import { defer, IDeferred, removeElement } from "./utils"; import { ConnectionError, MatrixError } from "./http-api"; import { ISendEventResponse } from "./@types/requests"; @@ -175,7 +174,7 @@ export class MatrixScheduler { return false; } let removed = false; - utils.removeElement(this.queues[name], (element) => { + removeElement(this.queues[name], (element) => { if (element.event.getId() === event.getId()) { // XXX we should probably reject the promise? // https://github.com/matrix-org/matrix-js-sdk/issues/496 @@ -214,15 +213,15 @@ export class MatrixScheduler { if (!this.queues[queueName]) { this.queues[queueName] = []; } - const defer = utils.defer(); + const deferred = defer(); this.queues[queueName].push({ event: event, - defer: defer, + defer: deferred, attempts: 0, }); debuglog("Queue algorithm dumped event %s into queue '%s'", event.getId(), queueName); this.startProcessingQueues(); - return defer.promise; + return deferred.promise; } private startProcessingQueues(): void { diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index e0aa627e2..b7d2223ff 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -17,7 +17,7 @@ limitations under the License. import type { SyncCryptoCallbacks } from "./common-crypto/CryptoBackend"; import { NotificationCountType, Room, RoomEvent } from "./models/room"; import { logger } from "./logger"; -import * as utils from "./utils"; +import { promiseMapSeries } from "./utils"; import { EventTimeline } from "./models/event-timeline"; import { ClientEvent, IStoredClientOpts, MatrixClient } from "./client"; import { @@ -726,8 +726,8 @@ export class SlidingSyncSdk { } }; - await utils.promiseMapSeries(stateEvents, processRoomEvent); - await utils.promiseMapSeries(timelineEvents, processRoomEvent); + await promiseMapSeries(stateEvents, processRoomEvent); + await promiseMapSeries(timelineEvents, processRoomEvent); ephemeralEvents.forEach(function (e) { client.emit(ClientEvent.Event, e); }); diff --git a/src/store/indexeddb-local-backend.ts b/src/store/indexeddb-local-backend.ts index 80fed44c5..a82de854e 100644 --- a/src/store/indexeddb-local-backend.ts +++ b/src/store/indexeddb-local-backend.ts @@ -15,8 +15,8 @@ limitations under the License. */ import { IMinimalEvent, ISyncData, ISyncResponse, SyncAccumulator } from "../sync-accumulator"; -import * as utils from "../utils"; -import * as IndexedDBHelpers from "../indexeddb-helpers"; +import { deepCopy, promiseTry } from "../utils"; +import { exists as idbExists } from "../indexeddb-helpers"; import { logger } from "../logger"; import { IStateEventWithRoomId, IStoredClientOpts } from "../matrix"; import { ISavedSync } from "./index"; @@ -122,7 +122,7 @@ function reqAsCursorPromise(req: IDBRequest): Promise { export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { public static exists(indexedDB: IDBFactory, dbName: string): Promise { dbName = "matrix-js-sdk:" + (dbName || "default"); - return IndexedDBHelpers.exists(indexedDB, dbName); + return idbExists(indexedDB, dbName); } private readonly dbName: string; @@ -380,7 +380,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { if (copy) { // We must deep copy the stored data so that the /sync processing code doesn't // corrupt the internal state of the sync accumulator (it adds non-clonable keys) - return Promise.resolve(utils.deepCopy(data)); + return Promise.resolve(deepCopy(data)); } else { return Promise.resolve(data); } @@ -435,7 +435,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { */ private persistSyncData(nextBatch: string, roomsData: ISyncResponse["rooms"]): Promise { logger.log("Persisting sync data up to", nextBatch); - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["sync"], "readwrite"); const store = txn.objectStore("sync"); store.put({ @@ -456,7 +456,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { * @returns Promise which resolves if the events were persisted. */ private persistAccountData(accountData: IMinimalEvent[]): Promise { - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["accountData"], "readwrite"); const store = txn.objectStore("accountData"); for (const event of accountData) { @@ -475,7 +475,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { * @returns Promise which resolves if the users were persisted. */ private persistUserPresenceEvents(tuples: UserTuple[]): Promise { - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["users"], "readwrite"); const store = txn.objectStore("users"); for (const tuple of tuples) { @@ -495,7 +495,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { * @returns A list of presence events in their raw form. */ public getUserPresenceEvents(): Promise { - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["users"], "readonly"); const store = txn.objectStore("users"); return selectQuery(store, undefined, (cursor) => { @@ -510,7 +510,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { */ private loadAccountData(): Promise { logger.log(`LocalIndexedDBStoreBackend: loading account data...`); - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["accountData"], "readonly"); const store = txn.objectStore("accountData"); return selectQuery(store, undefined, (cursor) => { @@ -528,7 +528,7 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend { */ private loadSyncData(): Promise { logger.log(`LocalIndexedDBStoreBackend: loading sync data...`); - return utils.promiseTry(() => { + return promiseTry(() => { const txn = this.db!.transaction(["sync"], "readonly"); const store = txn.objectStore("sync"); return selectQuery(store, undefined, (cursor) => { diff --git a/src/sync.ts b/src/sync.ts index 8bf0f8626..4800880bc 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -28,7 +28,7 @@ import { Optional } from "matrix-events-sdk"; import type { SyncCryptoCallbacks } from "./common-crypto/CryptoBackend"; import { User, UserEvent } from "./models/user"; import { NotificationCountType, Room, RoomEvent } from "./models/room"; -import * as utils from "./utils"; +import { promiseMapSeries, defer, deepCopy } from "./utils"; import { IDeferred, noUnsafeEventProps, unsafeProp } from "./utils"; import { Filter } from "./filter"; import { EventTimeline } from "./models/event-timeline"; @@ -414,7 +414,7 @@ export class SyncApi { // FIXME: Mostly duplicated from injectRoomEvents but not entirely // because "state" in this API is at the BEGINNING of the chunk - const oldStateEvents = utils.deepCopy(response.state).map(client.getEventMapper()); + const oldStateEvents = deepCopy(response.state).map(client.getEventMapper()); const stateEvents = response.state.map(client.getEventMapper()); const messages = response.messages.chunk.map(client.getEventMapper()); @@ -1247,7 +1247,7 @@ export class SyncApi { this.notifEvents = []; // Handle invites - await utils.promiseMapSeries(inviteRooms, async (inviteObj) => { + await promiseMapSeries(inviteRooms, async (inviteObj) => { const room = inviteObj.room; const stateEvents = this.mapSyncEventsFormat(inviteObj.invite_state, room); @@ -1288,7 +1288,7 @@ export class SyncApi { }); // Handle joins - await utils.promiseMapSeries(joinRooms, async (joinObj) => { + await promiseMapSeries(joinRooms, async (joinObj) => { const room = joinObj.room; const stateEvents = this.mapSyncEventsFormat(joinObj.state, room); // Prevent events from being decrypted ahead of time @@ -1471,7 +1471,7 @@ export class SyncApi { }); // Handle leaves (e.g. kicked rooms) - await utils.promiseMapSeries(leaveRooms, async (leaveObj) => { + await promiseMapSeries(leaveRooms, async (leaveObj) => { const room = leaveObj.room; const stateEvents = this.mapSyncEventsFormat(leaveObj.state, room); const events = this.mapSyncEventsFormat(leaveObj.timeline, room); @@ -1552,7 +1552,7 @@ export class SyncApi { this.pokeKeepAlive(); } if (!this.connectionReturnedDefer) { - this.connectionReturnedDefer = utils.defer(); + this.connectionReturnedDefer = defer(); } return this.connectionReturnedDefer.promise; } diff --git a/src/webrtc/call.ts b/src/webrtc/call.ts index df2a22587..1e42c3143 100644 --- a/src/webrtc/call.ts +++ b/src/webrtc/call.ts @@ -25,7 +25,7 @@ import { v4 as uuidv4 } from "uuid"; import { parse as parseSdp, write as writeSdp } from "sdp-transform"; import { logger } from "../logger"; -import * as utils from "../utils"; +import { checkObjectHasKeys, isNullOrUndefined, recursivelyAssign } from "../utils"; import { IContent, MatrixEvent } from "../models/event"; import { EventType, ToDeviceMessageId } from "../@types/event"; import { RoomMember } from "../models/room-member"; @@ -453,7 +453,7 @@ export class MatrixCall extends TypedEventEmitter