fix(sdk,transport): bounded prekey requests and a real registration retry (4.13.2)
An unreachable prekey server (packets dropped, not refused) held createShade for ~2 min per call, and a failed registration was never retried despite the log saying so. Every request now has a deadline, registration retries with backoff until it lands, and registration: 'background' lets an app open its identity offline. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,9 @@ import {
|
||||
KTVerificationError,
|
||||
} from '@shade/key-transparency';
|
||||
|
||||
/** How long any prekey-server request may go unanswered. */
|
||||
export const DEFAULT_REQUEST_TIMEOUT_MS = 15_000;
|
||||
|
||||
/**
|
||||
* HTTP transport client for the Shade Prekey Server.
|
||||
*
|
||||
@@ -67,15 +70,31 @@ export class ShadeFetchTransport {
|
||||
private readonly crypto: CryptoProvider;
|
||||
private readonly signingPrivateKey?: Uint8Array;
|
||||
private readonly kt?: KTVerifierOptions;
|
||||
private readonly fetchImpl: (url: string, init: RequestInit) => Promise<Response>;
|
||||
private readonly requestTimeoutMs: number;
|
||||
|
||||
constructor(options: {
|
||||
baseUrl: string;
|
||||
crypto: CryptoProvider;
|
||||
signingPrivateKey?: Uint8Array;
|
||||
keyTransparency?: KTVerifierOptions;
|
||||
/**
|
||||
* Fetch for every request. Lets an app observe its server's
|
||||
* reachability, or tests stub the network. Default: global fetch.
|
||||
*/
|
||||
fetch?: typeof fetch;
|
||||
/**
|
||||
* A request with no response by then fails with a `NetworkError`.
|
||||
* Default 15 000. Without it an unreachable server (packets dropped,
|
||||
* not refused) held each call until the kernel gave up on the TCP
|
||||
* handshake — about two minutes on Linux.
|
||||
*/
|
||||
requestTimeoutMs?: number;
|
||||
}) {
|
||||
this.baseUrl = options.baseUrl;
|
||||
this.crypto = options.crypto;
|
||||
this.fetchImpl = options.fetch ?? ((url, init) => fetch(url, init));
|
||||
this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
||||
if (options.signingPrivateKey !== undefined) {
|
||||
this.signingPrivateKey = options.signingPrivateKey;
|
||||
}
|
||||
@@ -84,6 +103,22 @@ export class ShadeFetchTransport {
|
||||
}
|
||||
}
|
||||
|
||||
/** Every request goes through here, so every request has a deadline. */
|
||||
private async request(url: string, init: RequestInit = {}): Promise<Response> {
|
||||
const timeout = AbortSignal.timeout(this.requestTimeoutMs);
|
||||
const signal = init.signal ? AbortSignal.any([init.signal, timeout]) : timeout;
|
||||
try {
|
||||
return await this.fetchImpl(url, { ...init, signal });
|
||||
} catch (err) {
|
||||
if (timeout.aborted && !init.signal?.aborted) {
|
||||
throw new NetworkError(
|
||||
`No response from ${new URL(url).host} within ${this.requestTimeoutMs} ms`,
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private headers(): Record<string, string> {
|
||||
return { 'Content-Type': 'application/json' };
|
||||
}
|
||||
@@ -133,7 +168,7 @@ export class ShadeFetchTransport {
|
||||
|
||||
const signed = await this.sign(body);
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/v1/keys/register`, {
|
||||
const res = await this.request(`${this.baseUrl}/v1/keys/register`, {
|
||||
method: 'POST',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(signed),
|
||||
@@ -159,7 +194,7 @@ export class ShadeFetchTransport {
|
||||
* direct access to the proof side-channel.
|
||||
*/
|
||||
async fetchBundleVerified(address: string): Promise<FetchBundleResult> {
|
||||
const res = await fetch(`${this.baseUrl}/v1/keys/bundle/${encodeURIComponent(address)}`, {
|
||||
const res = await this.request(`${this.baseUrl}/v1/keys/bundle/${encodeURIComponent(address)}`, {
|
||||
headers: this.headers(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -283,7 +318,7 @@ export class ShadeFetchTransport {
|
||||
};
|
||||
const signed = await this.sign(body);
|
||||
|
||||
const res = await fetch(`${this.baseUrl}/v1/keys/replenish`, {
|
||||
const res = await this.request(`${this.baseUrl}/v1/keys/replenish`, {
|
||||
method: 'POST',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(signed),
|
||||
@@ -295,7 +330,7 @@ export class ShadeFetchTransport {
|
||||
|
||||
/** Get remaining one-time prekey count (anonymous) */
|
||||
async getKeyCount(address: string): Promise<number> {
|
||||
const res = await fetch(`${this.baseUrl}/v1/keys/count/${encodeURIComponent(address)}`, {
|
||||
const res = await this.request(`${this.baseUrl}/v1/keys/count/${encodeURIComponent(address)}`, {
|
||||
headers: this.headers(),
|
||||
});
|
||||
if (!res.ok) throw new NetworkError(`Count failed: ${res.status}`, res.status);
|
||||
@@ -306,7 +341,7 @@ export class ShadeFetchTransport {
|
||||
/** Delete all keys for this identity (signed) */
|
||||
async unregister(address: string): Promise<void> {
|
||||
const signed = await this.sign({ address });
|
||||
const res = await fetch(`${this.baseUrl}/v1/keys/${encodeURIComponent(address)}`, {
|
||||
const res = await this.request(`${this.baseUrl}/v1/keys/${encodeURIComponent(address)}`, {
|
||||
method: 'DELETE',
|
||||
headers: this.headers(),
|
||||
body: JSON.stringify(signed),
|
||||
|
||||
Reference in New Issue
Block a user