feat(sdk): shareMedia / onMediaGrant — media grants over a Shade session

A grant for a sealed file or a live channel rides the Double Ratchet like
any message. With an onMediaGrant handler registered, grants are routed
there instead of onMessage; with none, they arrive as text as before.
Bench results for @shade/media (1 GiB, up to 200 live viewers, 100 %
delivered) committed with their chart spec.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 21:17:59 +02:00
parent e9ae5bd28e
commit 8fd1b19902
7 changed files with 347 additions and 0 deletions

View File

@@ -166,3 +166,4 @@ export type {
CreateEncryptStreamOptions,
CreateDecryptStreamOptions,
} from '@shade/crypto-web';
export type { Grant, MediaGrant, LiveGrant } from '@shade/media';

View File

@@ -1,4 +1,6 @@
import type { ShadeEnvelope, StorageProvider, RatchetMessage } from '@shade/core';
import { encodeGrant, isGrantMessage, parseGrant } from '@shade/media';
import type { Grant } from '@shade/media';
import {
ShadeSessionManager,
ShadeEventEmitter,
@@ -181,6 +183,8 @@ export class Shade {
// Message handlers — may be sync or async; receive() awaits each. The
// optional third arg distinguishes direct vs broadcast plaintexts;
// handlers registered without it work unchanged (V4.6 back-compat).
/** V4.15 — handlers for media grants; while any is registered, grants skip `onMessage`. */
private mediaGrantHandlers: Array<(from: string, grant: Grant) => void | Promise<void>> = [];
private messageHandlers: Array<
(from: string, plaintext: string, meta?: MessageMeta) => void | Promise<void>
> = [];
@@ -490,6 +494,7 @@ export class Shade {
plaintext,
);
if (consumed) return '';
if (await this.maybeDispatchMediaGrant(from, plaintext)) return '';
const meta: MessageMeta = { kind: 'direct' };
for (const handler of this.messageHandlers) {
try {
@@ -518,6 +523,53 @@ export class Shade {
};
}
// ─── V4.15 Media grants ────────────────────────────────────
/**
* Give a peer the keys to a sealed file or a live channel
* (`@shade/media`). The grant rides this Double-Ratchet session, so it is
* authenticated to us and forward-secret; the relay holding the media
* never sees it.
*/
async shareMedia(address: string, grant: Grant): Promise<ShadeEnvelope> {
return this.send(address, encodeGrant(grant));
}
/**
* Receive media grants. While at least one handler is registered, grant
* messages go here and not to `onMessage`; with none, they arrive as
* ordinary text, as before. A grant that does not parse is dropped with a
* warning rather than handed on — it claimed to be a grant.
*
* `from` is the authenticated sender: check it (or its verified
* fingerprint) before playing what it sent.
*/
onMediaGrant(handler: (from: string, grant: Grant) => void | Promise<void>): () => void {
this.mediaGrantHandlers.push(handler);
return () => {
this.mediaGrantHandlers = this.mediaGrantHandlers.filter((h) => h !== handler);
};
}
private async maybeDispatchMediaGrant(from: string, plaintext: string): Promise<boolean> {
if (this.mediaGrantHandlers.length === 0 || !isGrantMessage(plaintext)) return false;
let grant: Grant;
try {
grant = parseGrant(plaintext);
} catch (err) {
console.warn(`[Shade] Dropped a malformed media grant from ${from}:`, (err as Error).message);
return true;
}
for (const handler of this.mediaGrantHandlers) {
try {
await handler(from, grant);
} catch (err) {
console.error('[Shade] Media grant handler threw:', err);
}
}
return true;
}
// ─── V4.6 Broadcast channels ───────────────────────────────
/**