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:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@shade/transport",
|
||||
"version": "4.13.1",
|
||||
"version": "4.13.2",
|
||||
"type": "module",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts",
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { ShadeFetchTransport } from './fetch-transport.js';
|
||||
export { ShadeFetchTransport, DEFAULT_REQUEST_TIMEOUT_MS } from './fetch-transport.js';
|
||||
export type { FetchBundleResult, KTVerifierOptions } from './fetch-transport.js';
|
||||
export { ShadeWebSocket } from './ws-adapter.js';
|
||||
|
||||
@@ -82,3 +82,44 @@ describe('ShadeFetchTransport', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* A server that drops packets instead of refusing them used to hold every
|
||||
* call until the kernel gave up on the TCP handshake — two minutes on
|
||||
* Linux. Every request now has a deadline.
|
||||
*/
|
||||
describe('ShadeFetchTransport request deadline', () => {
|
||||
/** A fetch that never answers, but honours abort like the real one. */
|
||||
const blackHole = ((_url: unknown, init?: RequestInit) =>
|
||||
new Promise<Response>((_resolve, reject) => {
|
||||
init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
|
||||
})) as unknown as typeof fetch;
|
||||
|
||||
test('a request nobody answers fails with a NetworkError at the deadline', async () => {
|
||||
const transport = new ShadeFetchTransport({
|
||||
baseUrl: 'https://prekey.invalid',
|
||||
crypto,
|
||||
fetch: blackHole,
|
||||
requestTimeoutMs: 50,
|
||||
});
|
||||
const started = Date.now();
|
||||
const err = await transport.getKeyCount('bob').catch((e) => e);
|
||||
expect(err.name).toBe('NetworkError');
|
||||
expect(err.message).toContain('No response from prekey.invalid within 50 ms');
|
||||
expect(Date.now() - started).toBeLessThan(2_000);
|
||||
});
|
||||
|
||||
test('every request goes through the supplied fetch', async () => {
|
||||
const urls: string[] = [];
|
||||
const transport = new ShadeFetchTransport({
|
||||
baseUrl: 'https://prekey.invalid',
|
||||
crypto,
|
||||
fetch: (async (url: string) => {
|
||||
urls.push(url);
|
||||
return new Response(JSON.stringify({ count: 7 }), { status: 200 });
|
||||
}) as unknown as typeof fetch,
|
||||
});
|
||||
expect(await transport.getKeyCount('bob')).toBe(7);
|
||||
expect(urls).toEqual(['https://prekey.invalid/v1/keys/count/bob']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user