Skip to content

Commit e59696f

Browse files
committed
copy from github.com/sourcenetwork/defradb
0 parents  commit e59696f

28 files changed

+9895
-0
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DefraDB ACP Configuration
2+
VITE_ACP_API_URL=/api
3+
VITE_ACP_RPC_URL=/rpc
4+
VITE_ACP_GRPC_URL=/api
5+
VITE_ACP_CHAIN_ID=sourcehub-dev
6+
VITE_ACP_DENOM=uopen
7+
VITE_ACP_ALLOW_ZERO_FEES=true
8+
9+
# vite.config.ts targets for /api and /rpc
10+
VITE_API_TARGET=http://localhost:1317
11+
VITE_RPC_TARGET=http://localhost:26657

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
*.wasm

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enable legacy peer deps to handle React 19 compatibility with swagger-ui-react
2+
legacy-peer-deps=true

README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# DefraDB Playground
2+
3+
A web based playground for DefraDB.
4+
5+
## Table of Contents
6+
7+
<!--ts-->
8+
* [Getting Started](#getting-started)
9+
* [Remote mode](#remote-mode)
10+
* [Wasm mode](#wasm-mode)
11+
* [Local ACP (default)](#local-acp-default)
12+
* [SourceHub ACP (requires running SourceHub)](#sourcehub-acp-requires-running-sourcehub)
13+
* [Building](#building)
14+
<!--te-->
15+
16+
## Getting Started
17+
18+
The playground supports two modes, selectable via npm scripts.
19+
20+
## Remote mode
21+
Connects to a running DefraDB node.
22+
23+
**Steps:**
24+
- Start DefraDB with CORS allowed:
25+
```bash
26+
DEFRA_KEYRING_SECRET=your_secret defradb start --allowed-origins="*"
27+
```
28+
- Then run:
29+
```bash
30+
cd playground
31+
npm install
32+
npm run dev:remote
33+
```
34+
35+
## Wasm mode
36+
Runs DefraDB wasm in the browser.
37+
38+
**Steps:**
39+
- Build the **defradb.wasm** binary:
40+
```bash
41+
GOOS=js GOARCH=wasm go build -o playground/defradb.wasm ./cmd/defradb
42+
```
43+
- Then run one of the following commands:
44+
45+
### Local ACP (default)
46+
```bash
47+
cd playground
48+
npm install
49+
npm run dev:wasm
50+
```
51+
52+
### SourceHub ACP (requires running SourceHub)
53+
```bash
54+
cd playground
55+
cp .env.example .env
56+
npm install
57+
npm run dev:wasm:sourcehub
58+
```
59+
60+
The `npm run dev:wasm:sourcehub` command automatically attempts to connect to SourceHub. If SourceHub is not running or unreachable, the playground will automatically fall back to Local ACP mode.
61+
62+
**Note:** When SourceHub is available, the playground includes a keypair reset tab to manually generate a new keypair if the SourceHub state was reset.
63+
64+
**Running SourceHub with API endpoint at `http://localhost:1317`**
65+
```bash
66+
git clone https://github.com/sourcenetwork/sourcehub.git
67+
cd sourcehub && git checkout dev && git pull
68+
make build # make build-mac
69+
./scripts/dev-entrypoint.sh start
70+
```
71+
72+
**CORS Configuration**
73+
The playground uses Vite's proxy to route:
74+
- `/api/*``http://localhost:1317/*` (SourceHub API)
75+
- `/rpc/*``http://localhost:26657/*` (SourceHub RPC)
76+
77+
**Examples**
78+
When running the playground, the DefraDB client is available in the developer console via the `window.defradbClient` object.
79+
80+
```js
81+
// Add a schema
82+
const schema = `
83+
type User {
84+
name: String
85+
age: Int
86+
}
87+
`;
88+
89+
window.defradbClient.addSchema(schema);
90+
```
91+
92+
```js
93+
// Add a schema with a policy
94+
const schema = `
95+
type Users @policy(
96+
id: "policy_id",
97+
resource: "users"
98+
) {
99+
name: String
100+
age: Int
101+
}
102+
`;
103+
104+
window.defradbClient.addSchema(schema);
105+
```
106+
107+
```js
108+
// Add a document
109+
const query = `
110+
mutation {
111+
create_Users(input: {
112+
name: "John Doe 1",
113+
age: 33
114+
}) {
115+
_docID
116+
name
117+
age
118+
}
119+
}
120+
`;
121+
122+
window.defradbClient.execRequest(query, {}, {});
123+
```
124+
125+
```js
126+
// Add a policy with ACP configuration
127+
const policy = `
128+
description: A Valid DefraDB Policy Interface
129+
130+
actor:
131+
name: actor
132+
133+
resources:
134+
users:
135+
permissions:
136+
read:
137+
expr: owner + reader
138+
update:
139+
expr: owner
140+
delete:
141+
expr: owner
142+
143+
relations:
144+
owner:
145+
types:
146+
- actor
147+
reader:
148+
types:
149+
- actor
150+
`;
151+
152+
const context = {
153+
identity: "pub_key"
154+
};
155+
156+
window.defradbClient.addDACPolicy(policy, context);
157+
```
158+
159+
## Building
160+
161+
Create a static build and output files to `./dist`.
162+
163+
```bash
164+
npm install
165+
npm run build
166+
```

eslint.config.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import js from '@eslint/js';
2+
import typescript from '@typescript-eslint/eslint-plugin';
3+
import typescriptParser from '@typescript-eslint/parser';
4+
import reactHooks from 'eslint-plugin-react-hooks';
5+
import reactRefresh from 'eslint-plugin-react-refresh';
6+
7+
export default [
8+
js.configs.recommended,
9+
{
10+
files: ['**/*.{ts,tsx}'],
11+
languageOptions: {
12+
parser: typescriptParser,
13+
parserOptions: {
14+
ecmaVersion: 2020,
15+
sourceType: 'module',
16+
project: ['./tsconfig.json', './tsconfig.node.json'],
17+
},
18+
globals: {
19+
node: true,
20+
browser: true,
21+
es2020: true,
22+
jest: true,
23+
window: 'readonly',
24+
document: 'readonly',
25+
console: 'readonly',
26+
fetch: 'readonly',
27+
setTimeout: 'readonly',
28+
},
29+
},
30+
plugins: {
31+
'@typescript-eslint': typescript,
32+
'react-hooks': reactHooks,
33+
'react-refresh': reactRefresh,
34+
},
35+
rules: {
36+
// TypeScript specific rules
37+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
38+
'@typescript-eslint/no-explicit-any': 'warn',
39+
'@typescript-eslint/explicit-function-return-type': 'off',
40+
'@typescript-eslint/no-non-null-assertion': 'warn',
41+
'@typescript-eslint/prefer-optional-chain': 'error',
42+
'@typescript-eslint/prefer-nullish-coalescing': 'error',
43+
// General rules
44+
'no-console': 'off',
45+
'prefer-const': 'error',
46+
'no-var': 'error',
47+
'eqeqeq': 'error',
48+
'curly': 'error',
49+
// Code style
50+
'semi': ['error', 'always'],
51+
'quotes': ['error', 'single', { avoidEscape: true }],
52+
'comma-dangle': ['error', 'always-multiline'],
53+
},
54+
},
55+
{
56+
files: ['**/*.test.ts', '**/__tests__/**/*.ts'],
57+
rules: {
58+
'@typescript-eslint/no-explicit-any': 'off', // Allow any in tests
59+
'@typescript-eslint/no-non-null-assertion': 'off', // Allow ! in tests
60+
},
61+
},
62+
{
63+
files: ['**/*.tsx', '**/*.ts'],
64+
rules: {
65+
'no-unused-vars': 'off',
66+
'@typescript-eslint/no-unused-vars': ['error', {
67+
argsIgnorePattern: '^_',
68+
varsIgnorePattern: '^_',
69+
ignoreRestSiblings: true,
70+
}],
71+
},
72+
},
73+
{
74+
files: ['*.config.ts', '*.config.js', 'vite.config.ts'],
75+
languageOptions: {
76+
parser: typescriptParser,
77+
parserOptions: {
78+
ecmaVersion: 2020,
79+
sourceType: 'module',
80+
},
81+
globals: {
82+
process: 'readonly',
83+
console: 'readonly',
84+
},
85+
},
86+
plugins: {
87+
'@typescript-eslint': typescript,
88+
},
89+
rules: {
90+
'@typescript-eslint/no-explicit-any': 'warn',
91+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
92+
'@typescript-eslint/prefer-nullish-coalescing': 'off',
93+
},
94+
},
95+
{
96+
ignores: [
97+
'dist/',
98+
'node_modules/',
99+
'*.config.js',
100+
],
101+
},
102+
];

favicon.ico

14.7 KB
Binary file not shown.

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>DefraDB Playground</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)