mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-19 16:08:34 +01:00
Rename files
This commit is contained in:
77
ts/textsecure/cds/CDSI.node.ts
Normal file
77
ts/textsecure/cds/CDSI.node.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type {
|
||||
RateLimitedError as NetRateLimitedError,
|
||||
Net,
|
||||
} from '@signalapp/libsignal-client';
|
||||
import {
|
||||
ErrorCode as LibSignalErrorCode,
|
||||
LibSignalErrorBase,
|
||||
} from '@signalapp/libsignal-client';
|
||||
import pTimeout from 'p-timeout';
|
||||
import type { CDSBaseOptionsType } from './CDSBase.node.js';
|
||||
import { CDSBase } from './CDSBase.node.js';
|
||||
import type { CDSRequestOptionsType, CDSResponseType } from './Types.d.ts';
|
||||
import { sleep } from '../../util/sleep.std.js';
|
||||
import * as durations from '../../util/durations/index.std.js';
|
||||
|
||||
export type CDSIOptionsType = CDSBaseOptionsType;
|
||||
|
||||
const REQUEST_TIMEOUT = 10 * durations.SECOND;
|
||||
|
||||
export class CDSI extends CDSBase<CDSIOptionsType> {
|
||||
#retryAfter?: number;
|
||||
|
||||
constructor(
|
||||
private readonly libsignalNet: Net.Net,
|
||||
options: CDSIOptionsType
|
||||
) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
public async request(
|
||||
options: CDSRequestOptionsType
|
||||
): Promise<CDSResponseType> {
|
||||
const log = this.logger;
|
||||
|
||||
if (this.#retryAfter !== undefined) {
|
||||
const delay = Math.max(0, this.#retryAfter - Date.now());
|
||||
|
||||
log.info(`CDSSocketManager: waiting ${delay}ms before retrying`);
|
||||
await sleep(delay);
|
||||
}
|
||||
|
||||
const { acisAndAccessKeys, e164s } = options;
|
||||
const auth = await this.getAuth();
|
||||
|
||||
log.info('CDSSocketManager: making request via libsignal');
|
||||
try {
|
||||
log.info('CDSSocketManager: starting lookup request');
|
||||
|
||||
const { timeout = REQUEST_TIMEOUT } = options;
|
||||
const response = await pTimeout(
|
||||
this.libsignalNet.cdsiLookup(auth, {
|
||||
acisAndAccessKeys,
|
||||
e164s,
|
||||
}),
|
||||
timeout
|
||||
);
|
||||
|
||||
log.info('CDSSocketManager: lookup request finished');
|
||||
return response as CDSResponseType;
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof LibSignalErrorBase &&
|
||||
error.code === LibSignalErrorCode.RateLimitedError
|
||||
) {
|
||||
const retryError = error as NetRateLimitedError;
|
||||
this.#retryAfter = Math.max(
|
||||
this.#retryAfter ?? Date.now(),
|
||||
Date.now() + retryError.retryAfterSecs * durations.SECOND
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user