Alex Dunmow b6d40ed8ac feat: bootstrap block/pluginsdk — the proto-first plugin SDK (P1)
Bootstrap the plugin-facing SDK module so the fleet migration (P2) becomes a
mechanical block/core/X -> block/pluginsdk/X import rewrite.

- abi/proto/v1: the single source-of-truth ABI proto tree, copied from the cms
  authoring source (cms/backend/abi/proto/v1) with go_package retargeted to
  git.dev.alexdunmow.com/block/pluginsdk/abi/v1;abiv1. Kills the hand-kept
  cms/core proto duplication (audit gap 4).
- abi/v1: generated Go bindings (buf generate); byte-identical to core's abi/v1
  save the embedded go_package path.
- plugin/ (registration + DI surface), plugin/wasmguest/** (transport shim,
  caps stubs, bnwasm db driver, testdata fixtures + golden .pb), and the
  guest-facing type packages: blocks (+builtin/shared/tags), templates
  (+pongo/bn), auth, settings, content, gating, crypto, rbac, video, ai,
  subscriptions, menus, datasources. Internal imports rewritten core ->
  pluginsdk; zero block/core references remain.
- README/AGENTS(+CLAUDE symlink)/Makefile: proto is the contract, Go is one
  binding; no replace directives; templates/bn is a synced copy authored in cms.

Spec: cms docs/superpowers/specs/2026-07-07-proto-first-plugin-sdk-design.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 10:51:00 +08:00

103 lines
3.5 KiB
Go

package bnwasm
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// errNestedTx is returned by Tx.Begin. pgx models a nested Begin as a SAVEPOINT;
// the db.* ABI has no savepoint verb (v1 scope), and a grep of the plugin fleet
// (symposium, messenger) found no nested-transaction/savepoint use, so this is
// rejected explicitly rather than silently degrading correctness.
var errNestedTx = errors.New("bnwasm: nested transactions (savepoints) are not supported")
// Tx is a pgx.Tx bound to a host-side transaction handle. Every Exec/Query/
// QueryRow carries the handle so the host runs it on the transaction's
// connection; Commit/Rollback release it. After either, the Tx is closed and
// further statements return pgx.ErrTxClosed. The host also drops the handle at
// the call chain's deadline (WO-WZ-007), so a guest that leaks a Tx without
// committing has it rolled back host-side — a guest can never pin a connection.
type Tx struct {
t Transport
handle uint64
closed bool
}
// Begin would start a savepoint-backed nested transaction: unsupported.
func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error) { return nil, errNestedTx }
func (tx *Tx) Commit(ctx context.Context) error {
if tx.closed {
return pgx.ErrTxClosed
}
tx.closed = true
return tx.t.txCommit(ctx, tx.handle)
}
func (tx *Tx) Rollback(ctx context.Context) error {
if tx.closed {
return pgx.ErrTxClosed
}
tx.closed = true
return tx.t.txRollback(ctx, tx.handle)
}
func (tx *Tx) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
if tx.closed {
return pgconn.CommandTag{}, pgx.ErrTxClosed
}
return tx.t.exec(ctx, sql, tx.handle, args)
}
func (tx *Tx) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
if tx.closed {
return errRows(pgx.ErrTxClosed), pgx.ErrTxClosed
}
resp, err := tx.t.query(ctx, sql, tx.handle, args)
if err != nil {
return errRows(err), err
}
return newPgxRows(resp), nil
}
func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
if tx.closed {
return &pgxRow{err: pgx.ErrTxClosed}
}
resp, err := tx.t.query(ctx, sql, tx.handle, args)
if err != nil {
return &pgxRow{err: err}
}
return &pgxRow{rows: newPgxRows(resp)}
}
// --- Unsupported pgx.Tx surface (not emitted by sqlc; explicit errors) ---
func (tx *Tx) CopyFrom(context.Context, pgx.Identifier, []string, pgx.CopyFromSource) (int64, error) {
return 0, errors.New("bnwasm: CopyFrom is not supported over the wasm DB ABI")
}
func (tx *Tx) SendBatch(context.Context, *pgx.Batch) pgx.BatchResults {
return errBatchResults{errors.New("bnwasm: SendBatch is not supported over the wasm DB ABI")}
}
func (tx *Tx) LargeObjects() pgx.LargeObjects { return pgx.LargeObjects{} }
func (tx *Tx) Prepare(context.Context, string, string) (*pgconn.StatementDescription, error) {
return nil, errors.New("bnwasm: Prepare is not supported over the wasm DB ABI")
}
func (tx *Tx) Conn() *pgx.Conn { return nil }
// errBatchResults is a pgx.BatchResults that reports the same error from every
// method, so an unsupported SendBatch surfaces cleanly instead of nil-panicking.
type errBatchResults struct{ err error }
func (e errBatchResults) Exec() (pgconn.CommandTag, error) { return pgconn.CommandTag{}, e.err }
func (e errBatchResults) Query() (pgx.Rows, error) { return errRows(e.err), e.err }
func (e errBatchResults) QueryRow() pgx.Row { return &pgxRow{err: e.err} }
func (e errBatchResults) Close() error { return e.err }