fix: use more withs for onion.ts file

pull/3281/head
Audric Ackermann 4 months ago
parent a8372f1b8c
commit 3e34b0f77f
No known key found for this signature in database

@ -212,7 +212,7 @@ export function useIsKickedFromGroup(convoId?: string) {
export function useIsGroupDestroyed(convoId?: string) {
const libIsDestroyed = useLibGroupDestroyed(convoId);
if (convoId && PubKey.is03Pubkey(convoId)) {
return libIsDestroyed || false;
return !!libIsDestroyed;
}
return false;
}

@ -2,7 +2,6 @@ import { isArray } from 'lodash';
import { AbortController } from 'abort-controller';
import { MessageSender } from '../../sending';
import { Snode } from '../../../data/types';
import { SnodeResponseError } from '../../utils/errors';
import { processOnionRequestErrorAtDestination, SnodeResponse } from './onions';
import { SessionRpc } from './sessionRpc';
@ -15,15 +14,12 @@ import {
} from './SnodeRequestTypes';
import { NotEmptyArrayOfBatchResults } from './BatchResultEntry';
import { MergedAbortSignal, WithTimeoutMs } from './requestWith';
import { WithAllow401s } from '../../types/with';
import { WithAllow401s, WithAssociatedWith, WithTargetNode } from '../../types/with';
function logSubRequests(requests: Array<BuiltSnodeSubRequests>) {
return `[${requests.map(builtRequestToLoggingId).join(', ')}]`;
}
type WithTargetNode = { targetNode: Snode };
type WithAssociatedWith = { associatedWith: string | null };
/**
* This is the equivalent to the batch send on sogs. The target node runs each sub request and returns a list of all the sub status and bodies.
* If the global status code is not 200, an exception is thrown.

@ -21,7 +21,13 @@ import { fileServerHost } from '../file_server_api/FileServerApi';
import { hrefPnServerProd } from '../push_notification_api/PnServer';
import { ERROR_CODE_NO_CONNECT } from './SNodeAPI';
import { MergedAbortSignal, WithAbortSignal, WithTimeoutMs } from './requestWith';
import { WithAllow401s } from '../../types/with';
import {
WithAllow401s,
WithAssociatedWith,
WithDestinationEd25519,
WithGuardNode,
WithSymmetricKey,
} from '../../types/with';
// hold the ed25519 key of a snode against the time it fails. Used to remove a snode only after a few failures (snodeFailureThreshold failures)
let snodeFailureCount: Record<string, number> = {};
@ -311,12 +317,11 @@ export async function processOnionRequestErrorAtDestination({
destinationSnodeEd25519,
associatedWith,
allow401s,
}: WithAllow401s & {
statusCode: number;
body: string;
destinationSnodeEd25519?: string;
associatedWith?: string;
}) {
}: WithAllow401s &
Partial<WithDestinationEd25519 & WithAssociatedWith> & {
statusCode: number;
body: string;
}) {
if (statusCode === 200) {
return;
}
@ -328,13 +333,13 @@ export async function processOnionRequestErrorAtDestination({
process401Error(statusCode);
}
processOxenServerError(statusCode, body);
await process421Error(statusCode, body, associatedWith, destinationSnodeEd25519);
await process421Error(statusCode, body, associatedWith || undefined, destinationSnodeEd25519);
if (destinationSnodeEd25519) {
await processAnyOtherErrorAtDestination(
statusCode,
body,
destinationSnodeEd25519,
associatedWith
associatedWith || undefined
);
}
}
@ -342,9 +347,8 @@ export async function processOnionRequestErrorAtDestination({
async function handleNodeNotFound({
ed25519NotFound,
associatedWith,
}: {
}: Partial<WithAssociatedWith> & {
ed25519NotFound: string;
associatedWith?: string;
}) {
const shortNodeNotFound = ed25519Str(ed25519NotFound);
window?.log?.warn('Handling NODE NOT FOUND with: ', shortNodeNotFound);
@ -526,13 +530,10 @@ async function processOnionResponse({
associatedWith,
destinationSnodeEd25519,
allow401s,
}: Partial<WithAbortSignal> &
WithAllow401s & {
}: Partial<WithAbortSignal & WithDestinationEd25519 & WithAssociatedWith & WithSymmetricKey> &
WithAllow401s &
WithGuardNode & {
response?: { text: () => Promise<string>; status: number };
symmetricKey?: ArrayBuffer;
guardNode: Snode;
destinationSnodeEd25519?: string;
associatedWith?: string;
}): Promise<SnodeResponse> {
let ciphertext = '';
@ -549,7 +550,7 @@ async function processOnionResponse({
ciphertext,
guardNode.pubkey_ed25519,
destinationSnodeEd25519,
associatedWith
associatedWith || undefined
);
if (!ciphertext) {
@ -598,7 +599,7 @@ async function processOnionResponse({
}
return value;
}) as Record<string, any>;
// TODO: type those status
const status = jsonRes.status_code || jsonRes.status;
await processOnionRequestErrorAtDestination({
@ -642,13 +643,10 @@ async function processOnionResponseV4({
guardNode,
destinationSnodeEd25519,
associatedWith,
}: Partial<WithAbortSignal> & {
response?: Response;
symmetricKey?: ArrayBuffer;
guardNode: Snode;
destinationSnodeEd25519?: string;
associatedWith?: string;
}): Promise<SnodeResponseV4 | undefined> {
}: Partial<WithAbortSignal & WithDestinationEd25519 & WithAssociatedWith & WithSymmetricKey> &
WithGuardNode & {
response?: Response;
}): Promise<SnodeResponseV4 | undefined> {
processAbortedRequest(abortSignal);
const validSymmetricKey = await processNoSymmetricKeyError(guardNode, symmetricKey);
@ -669,7 +667,7 @@ async function processOnionResponseV4({
cipherText,
guardNode.pubkey_ed25519,
destinationSnodeEd25519,
associatedWith
associatedWith || undefined
);
const plaintextBuffer = await callUtilsWorker(
@ -705,9 +703,8 @@ export type FinalRelayOptions = {
port?: number; // default to 443
};
export type DestinationContext = {
export type DestinationContext = WithSymmetricKey & {
ciphertext: Uint8Array;
symmetricKey: ArrayBuffer;
ephemeralKey: ArrayBuffer;
};
@ -721,10 +718,8 @@ async function handle421InvalidSwarm({
body,
destinationSnodeEd25519,
associatedWith,
}: {
}: Partial<WithDestinationEd25519 & WithAssociatedWith> & {
body: string;
destinationSnodeEd25519?: string;
associatedWith?: string;
}) {
if (!destinationSnodeEd25519 || !associatedWith) {
// The snode isn't associated with the given public key anymore
@ -784,9 +779,8 @@ async function handle421InvalidSwarm({
async function incrementBadSnodeCountOrDrop({
snodeEd25519,
associatedWith,
}: {
}: Partial<WithAssociatedWith> & {
snodeEd25519: string;
associatedWith?: string;
}) {
const oldFailureCount = snodeFailureCount[snodeEd25519] || 0;
const newFailureCount = oldFailureCount + 1;
@ -829,12 +823,12 @@ async function sendOnionRequestHandlingSnodeEjectNoRetries({
timeoutMs,
}: WithAbortSignal &
WithTimeoutMs &
WithAllow401s & {
WithAllow401s &
Partial<WithAssociatedWith> & {
nodePath: Array<Snode>;
destSnodeX25519: string;
finalDestOptions: FinalDestOptions;
finalRelayOptions?: FinalRelayOptions;
associatedWith?: string;
useV4: boolean;
throwErrors: boolean;
}): Promise<SnodeResponse | SnodeResponseV4 | undefined> {
@ -1119,12 +1113,12 @@ async function sendOnionRequestSnodeDestNoRetries({
associatedWith,
}: WithTimeoutMs &
WithAbortSignal &
WithAllow401s & {
WithAllow401s &
Partial<WithAssociatedWith> & {
onionPath: Array<Snode>;
targetNode: Snode;
headers: Record<string, any>;
plaintext: string | null;
associatedWith?: string;
}) {
return Onions.sendOnionRequestHandlingSnodeEjectNoRetries({
nodePath: onionPath,
@ -1156,11 +1150,11 @@ async function lokiOnionFetchNoRetries({
timeoutMs,
}: WithTimeoutMs &
WithAbortSignal &
WithAllow401s & {
WithAllow401s &
Partial<WithAssociatedWith> & {
targetNode: Snode;
headers: Record<string, any>;
body: string | null;
associatedWith?: string;
}): Promise<SnodeResponse | undefined> {
try {
// Get a path excluding `targetNode`:

@ -1,4 +1,5 @@
import { PubkeyType } from 'libsession_util_nodejs';
import { Snode } from '../../data/types';
export type WithMessageHash = { messageHash: string };
export type WithTimestamp = { timestamp: number };
@ -26,3 +27,10 @@ export type ShortenOrExtend = 'extend' | 'shorten' | '';
export type WithShortenOrExtend = { shortenOrExtend: ShortenOrExtend };
export type WithMessagesHashes = { messagesHashes: Array<string> };
export type WithAllow401s = { allow401s: boolean };
export type WithDestinationEd25519 = { destinationSnodeEd25519: string };
export type WithAssociatedWith = { associatedWith: string | null };
export type WithTargetNode = { targetNode: Snode };
export type WithGuardNode = { guardNode: Snode };
export type WithSymmetricKey = { symmetricKey: ArrayBuffer };

Loading…
Cancel
Save