You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-31 15:24:23 +03:00
Improve types and their safety (#3290)
* Improve types and their safety * Iterate
This commit is contained in:
committed by
GitHub
parent
4f67e59692
commit
72d70bb929
@ -678,14 +678,14 @@ describe("utils", function () {
|
|||||||
|
|
||||||
describe("safeSet", () => {
|
describe("safeSet", () => {
|
||||||
it("should set a value", () => {
|
it("should set a value", () => {
|
||||||
const obj = {};
|
const obj: Record<string, string> = {};
|
||||||
safeSet(obj, "testProp", "test value");
|
safeSet(obj, "testProp", "test value");
|
||||||
expect(obj).toEqual({ testProp: "test value" });
|
expect(obj).toEqual({ testProp: "test value" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each(["__proto__", "prototype", "constructor"])("should raise an error when setting »%s«", (prop) => {
|
it.each(["__proto__", "prototype", "constructor"])("should raise an error when setting »%s«", (prop) => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
safeSet({}, prop, "teset value");
|
safeSet(<Record<string, string>>{}, prop, "teset value");
|
||||||
}).toThrow("Trying to modify prototype or constructor");
|
}).toThrow("Trying to modify prototype or constructor");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -38,7 +38,7 @@ import { Filter, IFilterDefinition, IRoomEventFilter } from "./filter";
|
|||||||
import { CallEventHandlerEvent, CallEventHandler, CallEventHandlerEventHandlerMap } from "./webrtc/callEventHandler";
|
import { CallEventHandlerEvent, CallEventHandler, CallEventHandlerEventHandlerMap } from "./webrtc/callEventHandler";
|
||||||
import { GroupCallEventHandlerEvent, GroupCallEventHandlerEventHandlerMap } from "./webrtc/groupCallEventHandler";
|
import { GroupCallEventHandlerEvent, GroupCallEventHandlerEventHandlerMap } from "./webrtc/groupCallEventHandler";
|
||||||
import * as utils from "./utils";
|
import * as utils from "./utils";
|
||||||
import { replaceParam, QueryDict, sleep, noUnsafeEventProps } from "./utils";
|
import { replaceParam, QueryDict, sleep, noUnsafeEventProps, safeSet } from "./utils";
|
||||||
import { Direction, EventTimeline } from "./models/event-timeline";
|
import { Direction, EventTimeline } from "./models/event-timeline";
|
||||||
import { IActionsObject, PushProcessor } from "./pushprocessor";
|
import { IActionsObject, PushProcessor } from "./pushprocessor";
|
||||||
import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery";
|
import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery";
|
||||||
@ -8809,8 +8809,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
|||||||
|
|
||||||
for (const [userId, deviceId] of devices) {
|
for (const [userId, deviceId] of devices) {
|
||||||
const query = queries[userId] || {};
|
const query = queries[userId] || {};
|
||||||
queries[userId] = query;
|
safeSet(queries, userId, query);
|
||||||
query[deviceId] = keyAlgorithm;
|
safeSet(query, deviceId, keyAlgorithm);
|
||||||
}
|
}
|
||||||
const content: IClaimKeysRequest = { one_time_keys: queries };
|
const content: IClaimKeysRequest = { one_time_keys: queries };
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
|
@ -338,7 +338,7 @@ export class MemoryCryptoStore implements CryptoStore {
|
|||||||
deviceSessions = {};
|
deviceSessions = {};
|
||||||
this.sessions[deviceKey] = deviceSessions;
|
this.sessions[deviceKey] = deviceSessions;
|
||||||
}
|
}
|
||||||
deviceSessions[sessionId] = sessionInfo;
|
safeSet(deviceSessions, sessionId, sessionInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async storeEndToEndSessionProblem(deviceKey: string, type: string, fixed: boolean): Promise<void> {
|
public async storeEndToEndSessionProblem(deviceKey: string, type: string, fixed: boolean): Promise<void> {
|
||||||
|
@ -720,7 +720,7 @@ function processMapToObjectValue(value: any): any {
|
|||||||
* Recursively converts Maps to plain objects.
|
* Recursively converts Maps to plain objects.
|
||||||
* Also supports sub-lists of Maps.
|
* Also supports sub-lists of Maps.
|
||||||
*/
|
*/
|
||||||
export function recursiveMapToObject(map: Map<any, any>): any {
|
export function recursiveMapToObject(map: Map<any, any>): Record<any, any> {
|
||||||
const targetMap = new Map();
|
const targetMap = new Map();
|
||||||
|
|
||||||
for (const [key, value] of map) {
|
for (const [key, value] of map) {
|
||||||
@ -734,7 +734,7 @@ export function unsafeProp<K extends keyof any | undefined>(prop: K): boolean {
|
|||||||
return prop === "__proto__" || prop === "prototype" || prop === "constructor";
|
return prop === "__proto__" || prop === "prototype" || prop === "constructor";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function safeSet<K extends keyof any>(obj: Record<any, any>, prop: K, value: any): void {
|
export function safeSet<O extends Record<any, any>, K extends keyof O>(obj: O, prop: K, value: O[K]): void {
|
||||||
if (unsafeProp(prop)) {
|
if (unsafeProp(prop)) {
|
||||||
throw new Error("Trying to modify prototype or constructor");
|
throw new Error("Trying to modify prototype or constructor");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user