672067d513
makeReadFilteredView wraps a DeepSignalSet in a Proxy whose iteration/size/forEach yield only items the current user may read (canRead on the item's emulated grant), while add/delete and the underlying reactivity pass through. filterReadable is the pure core. useShape applies it only when a grantOf resolver is configured (else passthrough, so ungranted apps are unaffected). grantOf/setGrantOf added to the polyfill surface. Mirrors the broker delivering only authorized docs; removed at migration. 4 unit tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
/**
|
|
* The polyfill bootstrap — the ONLY non-SDK surface of the client.
|
|
*
|
|
* It injects the REAL SDK and the polyfill settings; afterwards the SDK-shaped
|
|
* exports (`ng`, `useShape`, `inbox`) behave as drop-ins. This is exposed at the
|
|
* subpath `@ng-eventually/client/polyfill` so the main entry
|
|
* (`@ng-eventually/client`) stays a **pure, SDK-identical** surface. Everything
|
|
* here is removed at migration.
|
|
*/
|
|
|
|
import type { NgLike, UseShapeLike, PrincipalId } from "./types";
|
|
import type { GrantResolver } from "./read-filter";
|
|
|
|
export interface EventuallyConfig {
|
|
/** The REAL `@ng-org/web` `ng` (injected to avoid a hard import / alias loop). */
|
|
ng: NgLike;
|
|
/** The REAL `@ng-org/orm` `useShape`. */
|
|
useShape: UseShapeLike;
|
|
/** Shared-wallet credentials — polyfill only (one wallet for everyone). */
|
|
sharedWallet?: { name: string; secret: string };
|
|
/** Initial current user; may also be set later via {@link setCurrentUser}. */
|
|
currentUser?: PrincipalId;
|
|
/** REAL `@ng-org/web` `init` (lifecycle) — forwarded by the lib's `init()`. */
|
|
init?: (...args: any[]) => any;
|
|
/** REAL `@ng-org/orm` `initNg` (ORM signals) — forwarded by the lib's `initNg()`. */
|
|
initNg?: (...args: any[]) => any;
|
|
/** How to read a document's emulated grant. When set, reads are filtered. */
|
|
grantOf?: GrantResolver;
|
|
}
|
|
|
|
let cfg: EventuallyConfig | null = null;
|
|
let currentUser: PrincipalId | null = null;
|
|
let grantOf: GrantResolver | null = null;
|
|
|
|
export function configure(c: EventuallyConfig): void {
|
|
cfg = c;
|
|
currentUser = c.currentUser ?? null;
|
|
grantOf = c.grantOf ?? null;
|
|
}
|
|
|
|
/** @internal — used by the SDK-shaped wrappers to reach the injected real SDK. */
|
|
export function getConfig(): EventuallyConfig {
|
|
if (!cfg) throw new Error("[ng-eventually] configure() must be called before use");
|
|
return cfg;
|
|
}
|
|
|
|
/** Set the current app-level user (the polyfill's notion of "who am I"). */
|
|
export function setCurrentUser(id: PrincipalId | null): void {
|
|
currentUser = id;
|
|
}
|
|
|
|
export function getCurrentUser(): PrincipalId | null {
|
|
return currentUser;
|
|
}
|
|
|
|
/** Set/clear how reads resolve a document's grant. null → reads pass through. */
|
|
export function setGrantOf(fn: GrantResolver | null): void {
|
|
grantOf = fn;
|
|
}
|
|
|
|
export function getGrantOf(): GrantResolver | null {
|
|
return grantOf;
|
|
}
|
|
|
|
// Capability helpers — polyfill-era surface (caps are emulated now; native at
|
|
// migration). Re-exported here so the whole polyfill API lives under /polyfill.
|
|
export { canRead, canWrite, defaultGrant, grantRead } from "./access";
|
|
export type { GrantResolver } from "./read-filter";
|