Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ jobs:
- name: Run server tests
run: npm run test:server

firebase:
name: Firebase transport (emulators)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
# The Firestore emulator is a Java program.
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- name: Install dependencies
run: npm ci
- name: Install Playwright browser
run: npx playwright install --with-deps chromium
- name: Security rules and wire-protocol conformance
run: npm run test:firebase
- name: Live sync in two browsers
run: npm run test:firebase:e2e
- uses: actions/upload-artifact@v4
if: failure()
with:
name: firebase-test-results
path: |
test-results/
firebase-debug.log
firestore-debug.log
retention-days: 14

test:
name: Playwright (${{ matrix.project }})
runs-on: ubuntu-latest
Expand Down
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,59 @@ Annotate.init({
`getReviewer` is optional. Without it the viewer is unnamed and never gets
the review author's controls.

### Live sync with Firebase

Skip the server: comments sync through **your own Firebase project** (Cloud
Firestore) and reach the review's authors live, without a reload. Needs a
bundler (or an import map) and the `firebase` SDK (12 or newer, tested with 13.4; an optional
peer dependency); `annotate.js` itself stays dependency-free.

```js
import { initializeApp } from "firebase/app";
import Annotate from "@reviewjs/annotate";
import { firebaseTransport } from "@reviewjs/annotate/firebase";

const app = initializeApp({ /* your Firebase web config */ });
Annotate.init({
reviewId: "launch-review",
transport: firebaseTransport({ app, reviewId: "launch-review" }),
});
```

One-time setup:

1. Enable **Cloud Firestore** and **Authentication → Anonymous** (or sign
reviewers in however you like and pass `signIn`, or sign them in before `init`).
2. Deploy the rules: copy `examples/firebase/firestore.rules` and run
`firebase deploy --only firestore:rules`. They are what limits who may edit
whose comments, so do not skip them.
3. Create the review in the console or with the Admin SDK:
`annotateReviews/launch-review` with `{ name, note, open: true }`
(`open` lets any signed-in visitor join as a reviewer, and **joining means
reading every comment in the review**; leave it `false` and add members
yourself for an invite-only review; `closed: true` stops new feedback).
4. Make yourself an author: add `annotateReviews/launch-review/members/<your uid>`
with `{ role: "author", name: "You" }`. Authors see every comment, reply,
assign and close them.
5. Optional: add a Firestore TTL policy on `comments.purgeAt` (see
`examples/firebase/firestore.indexes.json`) so deleted comments are purged.

**Everyone on a review sees everyone's comments and can reply to any of them.**
Reviewers can create comments and edit, resolve or delete only their own;
authors can change anyone's and are the only ones who assign. (This is a
behaviour change from the first version of the adapter, where reviewers saw
only their own comments, and it differs from the HTTP backend, which keeps
reviewers apart. A reply can be removed only by its own author or by an
author, and nobody can write words in another member's name.) Offline edits are queued and sent when the connection returns.
`Annotate.syncState().live` is `true` while updates are pushed. The emulator
suites (`npm run test:firebase`, `npm run test:firebase:e2e`) show it end to end. The design and its
limits (for example, there are no invite links) are in
`docs/superpowers/specs/2026-11-19-firebase-live-sync-design.md`.

Other databases can plug in the same way: `transport` takes any object that
answers the [wire protocol](docs/backlog/wire-protocol.md) routes
(`request(method, path, { body, headers })`, plus optional `subscribe` and `destroy`).

## Framework integration

For a bundler, install `@reviewjs/annotate`, call `init()` after mount, and
Expand Down Expand Up @@ -394,6 +447,7 @@ Annotate.export({ scope: "site" }); // one file with every page's comments
Annotate.import(); // open the file picker
await Annotate.submit(); // POST the JSON export to postUrl
Annotate.refresh(); // manually re-read the current page when spa is off
Annotate.syncState(); // → { state, pending, live } for a synced review
Annotate.destroy(); // remove UI, observers, and event listeners
Annotate.init({ spa: true }); // start again with programmatic settings
Annotate.clear(); // delete all comments on this page (local)
Expand Down
22 changes: 22 additions & 0 deletions annotate.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ export interface AnnotateConfig {
credentials?: 'omit' | 'same-origin' | 'include';
/** Integrator-managed auth. Replaces the built-in invite-link exchange. */
auth?: AnnotateAuth;
/**
* A database adapter (see `@reviewjs/annotate/firebase`) that answers the
* wire-protocol routes in-process instead of over HTTP. Requires reviewId;
* `api` is not needed. Object-only: it cannot come from data-* attributes.
*/
transport?: AnnotateTransport;
}

/** Same routes, statuses and bodies as docs/backlog/wire-protocol.md, minus the `/v1/annotate/reviews/{id}` prefix. */
export interface AnnotateTransport {
/** `path` is relative to the review, e.g. "/session" or "/comments?page=%2Fpricing". Resolve with a fetch Response. */
request(method: string, path: string, init: { body?: unknown; headers: Record<string, string> }): Promise<Response>;
/**
* Optional live updates. Call `onComments` with the page's full visible list
* on every change; call `onError` if the subscription is refused. Return the
* function that stops it.
*/
subscribe?(page: string, onComments: (comments: AnnotateComment[]) => void, onError: (error: unknown) => void): () => void;
/** Called once from `Annotate.destroy()`. */
destroy?(): void;
}

export interface AnnotateAuth {
Expand Down Expand Up @@ -123,6 +143,8 @@ export interface AnnotateComment {
export interface AnnotateSyncState {
state: 'local' | 'idle' | 'pending' | 'offline' | 'error';
pending: number;
/** True while a transport pushes updates (no polling needed). */
live?: boolean;
}

export interface FeedbackExport {
Expand Down
22 changes: 22 additions & 0 deletions annotate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ export interface AnnotateConfig {
credentials?: 'omit' | 'same-origin' | 'include';
/** Integrator-managed auth. Replaces the built-in invite-link exchange. */
auth?: AnnotateAuth;
/**
* A database adapter (see `@reviewjs/annotate/firebase`) that answers the
* wire-protocol routes in-process instead of over HTTP. Requires reviewId;
* `api` is not needed. Object-only: it cannot come from data-* attributes.
*/
transport?: AnnotateTransport;
}

/** Same routes, statuses and bodies as docs/backlog/wire-protocol.md, minus the `/v1/annotate/reviews/{id}` prefix. */
export interface AnnotateTransport {
/** `path` is relative to the review, e.g. "/session" or "/comments?page=%2Fpricing". Resolve with a fetch Response. */
request(method: string, path: string, init: { body?: unknown; headers: Record<string, string> }): Promise<Response>;
/**
* Optional live updates. Call `onComments` with the page's full visible list
* on every change; call `onError` if the subscription is refused. Return the
* function that stops it.
*/
subscribe?(page: string, onComments: (comments: AnnotateComment[]) => void, onError: (error: unknown) => void): () => void;
/** Called once from `Annotate.destroy()`. */
destroy?(): void;
}

export interface AnnotateAuth {
Expand Down Expand Up @@ -122,6 +142,8 @@ export interface AnnotateComment {
export interface AnnotateSyncState {
state: 'local' | 'idle' | 'pending' | 'offline' | 'error';
pending: number;
/** True while a transport pushes updates (no polling needed). */
live?: boolean;
}

export interface FeedbackExport {
Expand Down
Loading