Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(desktop): drop app store response cap, unexport errors

Hampusfc37b9ccbb2f
diff --git a/fluxer_desktop/src/main/DesktopAppStorage.ts b/fluxer_desktop/src/main/DesktopAppStorage.ts
index 466e7aea1..e1e63082d 100644
--- a/fluxer_desktop/src/main/DesktopAppStorage.ts
+++ b/fluxer_desktop/src/main/DesktopAppStorage.ts
@@ -28,8 +28,8 @@ import {
DESKTOP_LEGACY_AUTHORITY_MARKER_VALUE,
} from '@fluxer/desktop_ipc/src/StorageContract';
-export const DESKTOP_APP_STORE_FILE_NAME = 'desktop-app-store.sqlite3';
-export const DESKTOP_APP_STORE_DERIVATION_VERSION = '1';
+const DESKTOP_APP_STORE_FILE_NAME = 'desktop-app-store.sqlite3';
+const DESKTOP_APP_STORE_DERIVATION_VERSION = '1';
const ENTRY_STORE = 'app';
const SCHEMA_VERSION_MARKER = 'schema.version';
@@ -37,8 +37,6 @@ const DESKTOP_AUTHORITY_SENTINEL_FILE_NAME = 'desktop-app-authority-v1';
const PENDING_OPERATIONS_MAX = 64;
const PENDING_INPUT_BYTES_MAX = 128 * 1024 * 1024;
-const PENDING_RESPONSE_BYTES_MAX = 128 * 1024 * 1024;
-const NATIVE_JSON_MAX_BYTES = 64 * 1024 * 1024;
const LOOKUP_KEY_MAX_BYTES = 1024;
const ENTRY_VALUE_MAX_BYTES = 8 * 1024 * 1024;
const MARKER_VALUE_MAX_BYTES = 64 * 1024;
@@ -64,35 +62,35 @@ interface DesktopAppStorageRecoveryResult {
readonly reseedRequired: boolean;
}
-export class DesktopAppStoreRequestError extends TypeError {
+class DesktopAppStoreRequestError extends TypeError {
public constructor(context: string, reason: string) {
super(`Desktop app store request is invalid: ${context} ${reason}`);
this.name = 'DesktopAppStoreRequestError';
}
}
-export class DesktopAppStoreUnavailableError extends Error {
+class DesktopAppStoreUnavailableError extends Error {
public constructor(reason: string) {
super(`Desktop app store is unavailable: ${reason}`);
this.name = 'DesktopAppStoreUnavailableError';
}
}
-export class DesktopAppStoreShutdownError extends Error {
+class DesktopAppStoreShutdownError extends Error {
public constructor() {
super('Desktop app store is shutting down');
this.name = 'DesktopAppStoreShutdownError';
}
}
-export class DesktopAppStoreCapacityError extends Error {
+class DesktopAppStoreCapacityError extends Error {
public constructor(resource: string, limit: number) {
super(`Desktop app store ${resource} is limited to ${limit}`);
this.name = 'DesktopAppStoreCapacityError';
}
}
-export class DesktopAppStoreWriteRefusedError extends Error {
+class DesktopAppStoreWriteRefusedError extends Error {
public constructor(refusals: ReadonlyArray<string>) {
super(`Desktop app store refused ${refusals.length} write(s): ${refusals.join('; ')}`);
this.name = 'DesktopAppStoreWriteRefusedError';
@@ -234,7 +232,6 @@ const UNOPENED_STATUS: DesktopStoreStatus = Object.freeze({
interface DesktopAppStoreOperation<T> {
readonly inputBytes: number;
- readonly maximumResponseBytes: number;
readonly operation: (boundary: AppStoreBoundary) => Promise<T>;
}
@@ -254,7 +251,6 @@ export class DesktopAppStorage {
private tail: Promise<unknown> = Promise.resolve();
private readonly pending = new Set<Promise<unknown>>();
private pendingInputBytes = 0;
- private pendingResponseBytes = 0;
private stamp = 0;
private authorityExpected: boolean;
private readonly authoritySentinelPath: string;
@@ -317,7 +313,6 @@ export class DesktopAppStorage {
try {
await this.runOperation({
inputBytes: byteLength(SCHEMA_VERSION_MARKER),
- maximumResponseBytes: MARKER_VALUE_MAX_BYTES,
operation: (boundary) => boundary.getMetadata({key: SCHEMA_VERSION_MARKER}),
});
} catch (error) {
@@ -343,7 +338,6 @@ export class DesktopAppStorage {
public getAllAccounts(): Promise<Array<DesktopAccountRecord>> {
return this.runOperation({
inputBytes: 0,
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: async (boundary) => {
const accounts = await boundary.getAllAccounts();
return accounts.flatMap((account) => {
@@ -358,7 +352,6 @@ export class DesktopAppStorage {
const key = requireLookupKey(storageKey, 'account storage key');
return this.runOperation({
inputBytes: byteLength(key),
- maximumResponseBytes: RECORD_MAX_BYTES,
operation: async (boundary) => {
const account = await boundary.getAccount({storageKey: key});
const record = account == null ? null : readAccountRecord(account.record);
@@ -371,7 +364,6 @@ export class DesktopAppStorage {
const entry = requireAccountRecord(account, 'account');
return this.runOperation({
inputBytes: this.recordBytes(entry.storageKey, entry.record),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.upsertAccount(entry),
});
}
@@ -391,7 +383,6 @@ export class DesktopAppStorage {
inputBytes:
this.recordBytes(expected.storageKey, expected.record) +
this.recordBytes(replacement.storageKey, replacement.record),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.compareAndSwapAccount(validated),
});
}
@@ -400,7 +391,6 @@ export class DesktopAppStorage {
const key = requireLookupKey(storageKey, 'account storage key');
return this.runOperation({
inputBytes: byteLength(key),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.deleteAccount({storageKey: key}),
});
}
@@ -417,7 +407,6 @@ export class DesktopAppStorage {
}
return this.runOperation({
inputBytes,
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: (boundary) => boundary.importAccounts({records, marker}),
});
}
@@ -435,7 +424,6 @@ export class DesktopAppStorage {
);
return this.runOperation({
inputBytes: knownStorageKeys.reduce((total, key) => total + byteLength(key), 0),
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: (boundary) => boundary.prune({knownStorageKeys, listIsAuthoritative}),
});
}
@@ -444,7 +432,6 @@ export class DesktopAppStorage {
const entryScope = requireLookupKey(scope, 'storage scope');
return this.runOperation({
inputBytes: byteLength(entryScope),
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: async (boundary) => {
const entries = await boundary.getEntries({store: ENTRY_STORE, scope: entryScope});
return entries.map((entry) => {
@@ -460,7 +447,6 @@ export class DesktopAppStorage {
const entryKey = requireLookupKey(key, 'storage key');
return this.runOperation({
inputBytes: byteLength(entryScope) + byteLength(entryKey),
- maximumResponseBytes: ENTRY_VALUE_MAX_BYTES,
operation: async (boundary) => {
const entry = await boundary.getEntry({store: ENTRY_STORE, scope: entryScope, key: entryKey});
if (entry == null) {
@@ -478,7 +464,6 @@ export class DesktopAppStorage {
const entryValue = requireBoundedString(value, 'storage value', ENTRY_VALUE_MAX_BYTES);
return this.runOperation({
inputBytes: byteLength(entryScope) + byteLength(entryKey) + byteLength(entryValue),
- maximumResponseBytes: 0,
operation: (boundary) =>
boundary.setEntry({
store: ENTRY_STORE,
@@ -495,7 +480,6 @@ export class DesktopAppStorage {
const entryKey = requireLookupKey(key, 'storage key');
return this.runOperation({
inputBytes: byteLength(entryScope) + byteLength(entryKey),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.deleteEntry({store: ENTRY_STORE, scope: entryScope, key: entryKey}),
});
}
@@ -504,7 +488,6 @@ export class DesktopAppStorage {
const entryScope = requireLookupKey(scope, 'storage scope');
return this.runOperation({
inputBytes: byteLength(entryScope),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.clearScope({scope: entryScope}),
});
}
@@ -515,7 +498,6 @@ export class DesktopAppStorage {
);
return this.runOperation({
inputBytes: keys.reduce((total, key) => total + byteLength(key), 0),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.clearStoreExcept({store: ENTRY_STORE, keysToKeep: keys}),
});
}
@@ -538,7 +520,6 @@ export class DesktopAppStorage {
}
return this.runOperation({
inputBytes,
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: async (boundary) => {
const present = await this.readPresentKeys(boundary, requested);
const entries = requested
@@ -583,7 +564,6 @@ export class DesktopAppStorage {
}
return this.runOperation({
inputBytes,
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: (boundary) => {
for (const entry of entries) {
this.observeStamp(entry.updatedAt);
@@ -606,7 +586,6 @@ export class DesktopAppStorage {
const markerKey = requireLookupKey(key, 'store marker key');
return this.runOperation({
inputBytes: byteLength(markerKey),
- maximumResponseBytes: MARKER_VALUE_MAX_BYTES,
operation: (boundary) => boundary.getMetadata({key: markerKey}),
});
}
@@ -630,7 +609,6 @@ export class DesktopAppStorage {
private writeMetadata(markerKey: string, markerValue: string): Promise<void> {
return this.runOperation({
inputBytes: byteLength(markerKey) + byteLength(markerValue),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.setMetadata({key: markerKey, value: markerValue}),
});
}
@@ -652,7 +630,6 @@ export class DesktopAppStorage {
public getAllKnownInstances(): Promise<Array<DesktopKnownInstanceRecord>> {
return this.runOperation({
inputBytes: 0,
- maximumResponseBytes: NATIVE_JSON_MAX_BYTES,
operation: async (boundary) => {
const stored = await boundary.getAllKnownInstances();
return stored.flatMap((instance) => {
@@ -667,7 +644,6 @@ export class DesktopAppStorage {
const record = requireKnownInstanceRecord(instance, 'known instance');
return this.runOperation({
inputBytes: this.recordBytes(record.instanceKey, record),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.upsertKnownInstance({key: record.instanceKey, record}),
});
}
@@ -676,7 +652,6 @@ export class DesktopAppStorage {
const key = requireLookupKey(instanceKey, 'known instance key');
return this.runOperation({
inputBytes: byteLength(key),
- maximumResponseBytes: 0,
operation: (boundary) => boundary.deleteKnownInstance({key}),
});
}
@@ -719,7 +694,7 @@ export class DesktopAppStorage {
}
}
- private runOperation<T>({inputBytes, maximumResponseBytes, operation}: DesktopAppStoreOperation<T>): Promise<T> {
+ private runOperation<T>({inputBytes, operation}: DesktopAppStoreOperation<T>): Promise<T> {
if (this.boundary == null) {
return Promise.reject(
new DesktopAppStoreUnavailableError(this.status.unavailableReason ?? 'the store did not open'),
@@ -734,11 +709,7 @@ export class DesktopAppStorage {
if (this.pendingInputBytes + inputBytes > PENDING_INPUT_BYTES_MAX) {
return Promise.reject(new DesktopAppStoreCapacityError('pending request bytes', PENDING_INPUT_BYTES_MAX));
}
- if (this.pendingResponseBytes + maximumResponseBytes > PENDING_RESPONSE_BYTES_MAX) {
- return Promise.reject(new DesktopAppStoreCapacityError('pending response bytes', PENDING_RESPONSE_BYTES_MAX));
- }
this.pendingInputBytes += inputBytes;
- this.pendingResponseBytes += maximumResponseBytes;
const result = this.tail.then(() => {
const boundary = this.boundary;
if (!this.accepting || boundary == null) {
@@ -749,7 +720,6 @@ export class DesktopAppStorage {
const tracked = result.finally(() => {
this.pending.delete(tracked);
this.pendingInputBytes -= inputBytes;
- this.pendingResponseBytes -= maximumResponseBytes;
});
this.pending.add(tracked);
this.tail = tracked.then(