release(v4.0.0): Shade GA — V3.x consolidation + audit prep
Some checks failed
Test / test (push) Has been cancelled
Cross-platform vectors / TypeScript vectors (bun) (push) Has been cancelled
Cross-platform vectors / Kotlin vectors (gradle) (push) Has been cancelled
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled

V3.1 → V3.12 consolidated and tagged for the first GA release. Wire
format unchanged from 0.4.x — 4.0 peers interoperate with 0.4.x peers
byte-for-byte. The version bump is semantic: audit-cycle complete,
opt-in surface fully exposed, threat model refreshed for every new
surface.

Highlights:
- All 24 @shade/* packages bumped to 4.0.0 in lockstep.
- CHANGELOG 4.0.0 section is the canonical manifest of what landed.
- THREAT-MODEL extended (§10 fingerprint gates, §11 WebRTC P2P, §12
  Web-Worker boundary) + residual-risks table refreshed.
- OpenAPI now covers all 27 routes: prekey, transfer, KT, inbox,
  bridge, observer, /metrics, /healthz, /ready.
- MIGRATION 0.3.x → 4.0 documented + smoke-tested against
  shade migrate-storage on a real SQLite DB.
- docs/audit/REVIEW-BUNDLE.md + SCOPE.md ready for external reviewer.
- scripts/soak.ts harness for the GA-stable 2-week soak window.
- All V*.md plans archived under docs/archive/ with Status: Done.
- Voice/Video carved out into V5.0; 4.0 audit focuses on the frozen
  non-realtime stack.

Tests: TS 1000/1000 + Kotlin 11/11 cross-platform vectors green.
Docker: gt.zyon.no/stian/shade-prekey:4.0.0 builds and reports
  version 4.0.0 on /health.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-03 18:35:35 +02:00
parent 8b055912b7
commit e6fdf31b49
298 changed files with 37909 additions and 256 deletions

View File

@@ -0,0 +1,205 @@
/**
* WebSocket bridge.
*
* Connects to `<base>/v1/bridge/ws?…` (where `base` uses the http(s):// URL
* — the bridge swaps to ws(s):// internally) and consumes JSON-encoded
* `BridgeWireMessage` frames pushed by the server.
*
* Auth uses the same signed query-string scheme as SSE/long-poll. The server
* runs the verification at upgrade time and rejects upgrades with a 4xx;
* once the WebSocket is open, frames are unauthenticated at the WS layer
* (the address-owner relationship is bound at upgrade and the connection is
* dedicated to that address).
*/
import type { BridgeConnectOptions, BridgeTransport, BridgeWireMessage } from './types.js';
import { decodeWireMessage } from './types.js';
import type { BridgeAuthInput } from './auth.js';
import { signBridgeQuery } from './auth.js';
import { BridgeError } from './errors.js';
export interface WsBridgeOptions {
baseUrl: string;
auth: Omit<BridgeAuthInput, 'kind' | 'since'>;
/** WebSocket constructor override (browsers vs Node vs Bun). */
WebSocketCtor?: typeof WebSocket;
/** Connect timeout (ms). Default 5_000. */
connectTimeoutMs?: number;
/** Initial cursor. */
startCursor?: number;
/** Disable auto-reconnect on drop. Default false. */
disableAutoReconnect?: boolean;
/** Initial reconnect backoff (ms). Default 250. */
initialBackoffMs?: number;
/** Max reconnect backoff (ms). Default 10_000. */
maxBackoffMs?: number;
}
const DEFAULT_CONNECT_TIMEOUT = 5_000;
const DEFAULT_INITIAL_BACKOFF = 250;
const DEFAULT_MAX_BACKOFF = 10_000;
export class WsBridge implements BridgeTransport {
readonly kind = 'ws';
private readonly Ctor: typeof WebSocket;
private cursor: number;
private connectStarted = false;
private disposed = false;
private current: WebSocket | null = null;
private onMessage: BridgeConnectOptions['onMessage'] | null = null;
private onError: NonNullable<BridgeConnectOptions['onError']> = (err) =>
console.warn('[shade-bridge:ws]', err.message);
private reconnectPromise: Promise<void> | null = null;
constructor(private readonly options: WsBridgeOptions) {
const ctor =
options.WebSocketCtor ??
((globalThis as unknown as { WebSocket?: typeof WebSocket }).WebSocket);
if (ctor === undefined) {
throw new BridgeError('WebSocket constructor not available; pass options.WebSocketCtor');
}
this.Ctor = ctor;
this.cursor = options.startCursor ?? 0;
}
async connect(opts: BridgeConnectOptions): Promise<void> {
if (this.connectStarted) throw new BridgeError('WsBridge.connect already called');
this.connectStarted = true;
this.onMessage = opts.onMessage;
if (opts.onError) this.onError = opts.onError;
await this.openOnce();
}
async disconnect(): Promise<void> {
this.disposed = true;
if (this.current) {
try {
this.current.close(1000, 'client disconnect');
} catch {
/* ignore */
}
this.current = null;
}
if (this.reconnectPromise) {
try {
await this.reconnectPromise;
} catch {
/* swallow */
}
}
}
getCursor(): number {
return this.cursor;
}
private async openOnce(): Promise<void> {
const qs = await signBridgeQuery({
crypto: this.options.auth.crypto,
signingPrivateKey: this.options.auth.signingPrivateKey,
address: this.options.auth.address,
kind: 'ws',
since: this.cursor,
});
const url = `${toWsUrl(this.options.baseUrl)}/v1/bridge/ws?${qs.toString()}`;
const ws = new this.Ctor(url);
ws.binaryType = 'arraybuffer';
const connectTimeoutMs = this.options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT;
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
try {
ws.close();
} catch {
/* ignore */
}
reject(new BridgeError(`WS connect timeout to ${url}`));
}, connectTimeoutMs);
ws.addEventListener('open', () => {
clearTimeout(timer);
resolve();
});
ws.addEventListener('error', () => {
clearTimeout(timer);
reject(new BridgeError(`WS connect error to ${url}`));
});
});
this.current = ws;
this.attachListeners(ws);
}
private attachListeners(ws: WebSocket): void {
ws.addEventListener('message', (ev) => {
const data = ev.data;
let text: string;
if (typeof data === 'string') {
text = data;
} else if (data instanceof ArrayBuffer) {
text = new TextDecoder().decode(data);
} else {
this.onError(new BridgeError('WS frame neither text nor ArrayBuffer'));
return;
}
let wire: BridgeWireMessage;
try {
wire = JSON.parse(text);
} catch (err) {
this.onError(new BridgeError(`malformed WS frame: ${(err as Error).message}`));
return;
}
if (typeof wire.ciphertext !== 'string' || typeof wire.receivedAt !== 'number') {
this.onError(new BridgeError('WS frame missing required fields'));
return;
}
if (wire.receivedAt > this.cursor) this.cursor = wire.receivedAt;
const msg = decodeWireMessage(wire);
if (this.onMessage) {
Promise.resolve(this.onMessage(msg)).catch((err) => this.onError(err as Error));
}
});
const onCloseOrError = (): void => {
if (this.disposed) return;
this.current = null;
if (this.options.disableAutoReconnect) return;
this.reconnectPromise = this.reconnectLoop();
};
ws.addEventListener('close', onCloseOrError);
ws.addEventListener('error', () => {
// Treat error as a close — the platform fires 'close' too in most
// implementations, but Node's `ws` library does not always.
if (this.current === ws) {
try {
ws.close();
} catch {
/* ignore */
}
}
});
}
private async reconnectLoop(): Promise<void> {
let backoff = this.options.initialBackoffMs ?? DEFAULT_INITIAL_BACKOFF;
const maxBackoff = this.options.maxBackoffMs ?? DEFAULT_MAX_BACKOFF;
while (!this.disposed) {
await sleep(backoff);
if (this.disposed) return;
try {
await this.openOnce();
return;
} catch (err) {
this.onError(err as Error);
backoff = Math.min(backoff * 2, maxBackoff);
}
}
}
}
function toWsUrl(httpUrl: string): string {
const trimmed = httpUrl.endsWith('/') ? httpUrl.slice(0, -1) : httpUrl;
if (trimmed.startsWith('https://')) return 'wss://' + trimmed.slice('https://'.length);
if (trimmed.startsWith('http://')) return 'ws://' + trimmed.slice('http://'.length);
return trimmed; // already ws(s)://
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}