Skip to content
Merged
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
723 changes: 723 additions & 0 deletions docs/coredeno/RFC.md

Large diffs are not rendered by default.

316 changes: 316 additions & 0 deletions docs/coredeno/architecture.md

Large diffs are not rendered by default.

211 changes: 211 additions & 0 deletions docs/coredeno/development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
---
title: Development
description: How to build, test, and contribute to CoreTS.
---

# Development

## Prerequisites

- **Go 1.26+** (uses Go workspaces)
- **Deno 2.x** (required for integration tests)
- **protoc** + Go/gRPC plugins (only if regenerating protobuf stubs)

This module is part of a Go workspace at `~/Code/go.work`. After cloning, ensure the workspace includes it:

```bash
go work use ./core/ts
```

## Building

CoreTS is a library package with no standalone binary. It compiles as part of applications that import it:

```bash
go build ./...
```

## Running Tests

### Unit Tests

Unit tests cover the Go side without requiring Deno:

```bash
core go test
# or
go test ./...
```

Tests use the `_Good`, `_Bad`, `_Ugly` suffix convention:
- `_Good` -- happy path
- `_Bad` -- expected error conditions
- `_Ugly` -- panics and edge cases

### Integration Tests

Integration tests require a working Deno installation and are gated behind the `integration` build tag:

```bash
go test -tags integration -timeout 60s ./...
```

These tests boot the full CoreTS stack (Go gRPC server + Deno sidecar + Workers) and verify end-to-end communication. They are organised in tiers:

| Tier | Test | What it proves |
|------|------|----------------|
| 1 | `TestIntegration_FullBoot_Good` | Go gRPC server starts, Deno sidecar launches, store round-trip works |
| 2 | `TestIntegration_Tier2_Bidirectional_Good` | Go can call Deno (LoadModule/UnloadModule/ModuleStatus), bidirectional communication |
| 3 | `TestIntegration_Tier3_WorkerIsolation_Good` | Module Workers can call back to Go via the I/O bridge (store write from inside a Worker) |
| 4 | `TestIntegration_Tier4_MarketplaceInstall_Good` | Full marketplace flow: install from Git, load module, verify I/O bridge, unload, remove |

If Deno is not installed, integration tests are automatically skipped.

### Single Test

```bash
core go test --run TestCheckPath_Good_Allowed
# or
go test -run TestCheckPath_Good_Allowed ./...
```

### Test Coverage

```bash
core go cov
core go cov --open # Opens HTML report
```

## Code Quality

```bash
core go fmt # Format
core go lint # Lint
core go vet # Vet
core go qa # All of the above + tests
core go qa full # + race detector, vulnerability scan
```

## Regenerating Protobuf Stubs

If you modify `proto/coredeno.proto`, regenerate the Go stubs:

```bash
protoc --go_out=. --go-grpc_out=. proto/coredeno.proto
```

Ensure you have the `protoc-gen-go` and `protoc-gen-go-grpc` plugins installed:

```bash
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
```

The Deno side loads the `.proto` file dynamically at runtime via `@grpc/proto-loader`, so no TypeScript code generation is needed.

## Project Structure

```
dappco.re/go/core/ts/
├── coredeno.go # Options, Permissions, Sidecar types
├── coredeno_test.go # Unit tests for options and sidecar creation
├── lifecycle.go # Sidecar Start/Stop/IsRunning
├── lifecycle_test.go # Unit tests for process lifecycle
├── listener.go # ListenGRPC -- Unix socket gRPC server
├── listener_test.go # Unit tests for gRPC listener
├── server.go # CoreService gRPC implementation
├── server_test.go # Unit tests for gRPC handlers + permissions
├── denoclient.go # DenoClient JSON-RPC client
├── permissions.go # CheckPath/CheckNet/CheckRun helpers
├── permissions_test.go # Unit tests for permission checks
├── service.go # Framework Service integration
├── service_test.go # Unit tests for service lifecycle
├── integration_test.go # End-to-end tests (build tag: integration)
├── go.mod
├── go.sum
├── proto/
│ ├── coredeno.proto # Service + message definitions
│ ├── coredeno.pb.go # Generated protobuf code
│ └── coredeno_grpc.pb.go # Generated gRPC stubs
└── runtime/
├── main.ts # Deno entry point
├── client.ts # CoreService gRPC client (Deno calls Go)
├── server.ts # DenoService JSON-RPC server (Go calls Deno)
├── modules.ts # Module registry + Worker isolation
├── worker-entry.ts # Worker bootstrap script
├── polyfill.ts # Deno 2.x http2/grpc-js patches
├── deno.json # Deno configuration + npm imports
├── deno.lock # Lock file
└── testdata/
└── test-module.ts # Test fixture for integration tests
src/
├── mod.ts # Browser/runtime entry point
├── storage.ts # Storage polyfills and bridge contracts
├── electron.ts # Electron compatibility shim
├── router.ts # Hash router with core:// handling
├── i18n.ts # Shared i18n API
├── components.ts # Web Component base classes
├── layout.ts # core-layout HLCRF Web Component
├── wasm.ts # go-html WASM loader
├── events.ts # Event bus
├── result.ts # Result helpers
└── options.ts # Shared option types
```

## Writing a TypeScript Module

A module is a TypeScript file that exports an `init` function:

```typescript
export async function init(core: any) {
// Use the I/O bridge to interact with Go-managed resources
await core.storeSet("my-module", "status", "running");

const data = await core.fileRead("./data/config.json");
console.log("Config loaded:", data);
}
```

The `core` object provides:

| Method | Description |
|--------|-------------|
| `core.storeGet(group, key)` | Read from the key-value store |
| `core.storeSet(group, key, value)` | Write to the key-value store |
| `core.fileRead(path)` | Read a file (permission-gated) |
| `core.fileWrite(path, content)` | Write a file (permission-gated) |
| `core.processStart(command, args)` | Start a subprocess (permission-gated) |
| `core.processStop(processId)` | Stop a subprocess |

All operations are relayed through the Go gRPC server and checked against the module's declared permissions.

## Adding a New gRPC Method

1. Add the RPC and message definitions to `proto/coredeno.proto`
2. Regenerate Go stubs: `protoc --go_out=. --go-grpc_out=. proto/coredeno.proto`
3. Implement the handler in `server.go` with appropriate permission checks
4. Add the method to `runtime/client.ts` (CoreService calls) or `runtime/server.ts` (DenoService calls)
5. If the method should be available to Workers, add it to `runtime/worker-entry.ts` and `runtime/modules.ts` (`dispatchRPC`)
6. Write unit tests (`server_test.go`) and integration tests (`integration_test.go`)

## Coding Standards

- **UK English** in documentation and user-facing strings (colour, organisation, centre)
- **Strict typing** -- all Go function parameters and return types must be declared
- **Test naming** -- use `_Good`, `_Bad`, `_Ugly` suffixes
- **Error context** -- wrap errors with the subsystem prefix: `fmt.Errorf("coredeno: <context>: %w", err)`
- **Thread safety** -- use `sync.RWMutex` for shared state; the Sidecar and DenoClient are both thread-safe
- **Secure by default** -- empty permission lists deny all access; reserved store namespaces are blocked

## Dependency Graph

```
dappco.re/go/core/ts
├── dappco.re/go/core (DI container, ServiceRuntime)
├── dappco.re/go/core/io (Sandboxed Medium, MockMedium, Store)
├── dappco.re/go/core/scm (Manifest loading, Marketplace installer)
├── google.golang.org/grpc (gRPC server + client)
└── google.golang.org/protobuf (Protocol buffer runtime)
```

CoreTS has no circular dependencies. It depends on the core framework but the framework does not depend on it.
119 changes: 119 additions & 0 deletions docs/coredeno/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
title: CoreTS
description: Go service that manages a Deno TypeScript runtime as a sandboxed sidecar process, providing permission-gated I/O over gRPC and Unix sockets.
---

# CoreTS

CoreTS (`dappco.re/go/core/ts`) is a Go package that embeds a **Deno TypeScript runtime** as a managed sidecar process. It provides a bidirectional communication bridge between Go and Deno over Unix sockets, with fine-grained permission gating for filesystem, key-value store, and process operations.

The Go side exposes a **CoreService** gRPC server that Deno calls for I/O. The Deno side exposes a **DenoService** JSON-RPC server that Go calls for module lifecycle management. TypeScript modules run in isolated Deno Workers with per-module permission sandboxing.

**Module path:** `dappco.re/go/core/ts`

**Licence:** EUPL-1.2

## Quick Start

Register CoreTS as a service in a Core application:

```go
import (
"context"
core "dappco.re/go/core"
ts "dappco.re/go/core/ts"
)

opts := ts.Options{
DenoPath: "deno",
SocketPath: "/tmp/core/core.sock",
AppRoot: "/app",
SidecarArgs: []string{"run", "-A", "--unstable-worker-options", "runtime/main.ts"},
}

app, err := core.New(core.WithService(ts.NewServiceFactory(opts)))
if err != nil {
panic(err)
}
```

On startup, the service will:

1. Create a sandboxed I/O medium scoped to `AppRoot`
2. Open a SQLite-backed key-value store
3. Start a gRPC server on a Unix socket
4. Load and optionally verify the application manifest
5. Launch the Deno sidecar process
6. Wait for Deno's JSON-RPC server and connect as a client
7. Auto-load any previously installed marketplace modules

## Package Layout

| Path | Language | Purpose |
|------|----------|---------|
| `*.go` | Go | Sidecar management, gRPC server, permission checks, service integration |
| `proto/coredeno.proto` | Protobuf | Service definitions for CoreService and DenoService |
| `proto/*.pb.go` | Go | Generated protobuf and gRPC stubs |
| `runtime/main.ts` | TypeScript | Deno entry point -- boots the runtime, connects to Go |
| `runtime/client.ts` | TypeScript | gRPC client that calls CoreService on the Go side |
| `runtime/server.ts` | TypeScript | JSON-RPC server that implements DenoService for Go to call |
| `runtime/modules.ts` | TypeScript | Module registry with Worker isolation and I/O bridge |
| `runtime/worker-entry.ts` | TypeScript | Worker bootstrap -- loaded as entry point for every module Worker |
| `runtime/polyfill.ts` | TypeScript | Patches for Deno 2.x http2/grpc-js compatibility issues |
| `runtime/testdata/` | TypeScript | Test fixtures for integration tests |
| `src/mod.ts` | TypeScript | Browser/runtime entry point that re-exports the client-side helpers |
| `src/*.ts` | TypeScript | Browser runtime modules: i18n, components, HLCRF layout, events, router, storage, Electron shim, WASM loader, and shared result/options types |

## Go Source Files

| File | Purpose |
|------|---------|
| `coredeno.go` | `Options`, `Permissions`, `Sidecar` types and `NewSidecar()` constructor |
| `lifecycle.go` | `Sidecar.Start()`, `Stop()`, `IsRunning()` -- process lifecycle |
| `listener.go` | `ListenGRPC()` -- Unix socket gRPC listener with graceful shutdown |
| `server.go` | `Server` -- CoreService gRPC implementation with permission gating |
| `denoclient.go` | `DenoClient` -- JSON-RPC client for calling the Deno sidecar |
| `permissions.go` | `CheckPath()`, `CheckNet()`, `CheckRun()` -- permission helpers |
| `service.go` | `Service` -- framework integration (Startable/Stoppable lifecycle) |

## Dependencies

| Module | Purpose |
|--------|---------|
| `dappco.re/go/core` | Core framework (DI container, `ServiceRuntime`, lifecycle interfaces) |
| `dappco.re/go/core/io` | Sandboxed filesystem I/O (`Medium` interface, `MockMedium`) |
| `dappco.re/go/core/io/store` | SQLite-backed key-value store |
| `dappco.re/go/core/scm/manifest` | Module manifest loading and ed25519 verification |
| `dappco.re/go/core/scm/marketplace` | Module installation from Git repositories |
| `google.golang.org/grpc` | gRPC server and client |
| `google.golang.org/protobuf` | Protocol buffer runtime |
| `github.com/stretchr/testify` | Test assertions (dev only) |

The Deno runtime uses npm packages managed via `runtime/deno.json`:

| Package | Purpose |
|---------|---------|
| `@grpc/grpc-js` | gRPC client for calling CoreService |
| `@grpc/proto-loader` | Dynamic protobuf loading |

## Configuration

The `Options` struct controls all behaviour:

```go
type Options struct {
DenoPath string // Path to deno binary (default: "deno")
SocketPath string // Unix socket for Go's gRPC server
DenoSocketPath string // Unix socket for Deno's JSON-RPC server
AppRoot string // Application root directory (sandboxed I/O boundary)
StoreDBPath string // SQLite path (default: AppRoot/.core/store.db)
PublicKey ed25519.PublicKey // Ed25519 key for manifest verification (optional)
SidecarArgs []string // Arguments passed to the Deno process
}
```

If `SocketPath` is not set, it defaults to `$XDG_RUNTIME_DIR/core/core.sock` (or `/tmp/core/core.sock` on macOS).

If `DenoSocketPath` is not set, it defaults to the same directory as `SocketPath` with filename `deno.sock`.

If `StoreDBPath` is not set and `AppRoot` is provided, it defaults to `AppRoot/.core/store.db`.
2 changes: 1 addition & 1 deletion go/display/http/framework/angular/server_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
httpdisplay "dappco.re/go/render/display/http"
"dappco.re/go/render/display/http/framework/angular"
tsengine "dappco.re/go/render/engine/ts"
corets "dappco.re/go/ts"
corets "dappco.re/go/render/engine/ts/deno"
)

func ExampleNew() {
Expand Down
2 changes: 1 addition & 1 deletion go/engine/ts/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package ts

import (
core "dappco.re/go"
corets "dappco.re/go/ts"
corets "dappco.re/go/render/engine/ts/deno"
)

const contextConnectTimeout = 10 * core.Second
Expand Down
Loading
Loading