Skip to content

Commit bf7e7bd

Browse files
committed
Add WASM build script, tests, and update SDK structure
Introduces a comprehensive WASM build script (build.sh) for the Rust link library, adds automated WASM/TypeScript test suites, and updates the README with detailed build and usage instructions. Removes outdated example and SDK files, updates Cargo.toml features, and marks related tasks as complete in the specs. This prepares the link package for robust WASM/TypeScript SDK distribution and testing.
1 parent 6e523ea commit bf7e7bd

12 files changed

Lines changed: 1081 additions & 179 deletions

File tree

docker/backend/Dockerfile

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# ============================================================================
66
# Stage 1: Builder
77
# ============================================================================
8-
FROM rust:1.90-slim AS builder
8+
FROM rust:1.90-bookworm AS builder
99

1010
# Install build dependencies
1111
RUN apt-get update && \
@@ -21,21 +21,24 @@ RUN apt-get update && \
2121
# Set working directory
2222
WORKDIR /build
2323

24-
# Copy workspace Cargo files
25-
COPY backend/Cargo.toml backend/Cargo.lock ./backend/
26-
COPY cli/Cargo.toml ./cli/
24+
# Copy workspace Cargo files (root level)
25+
COPY Cargo.toml Cargo.lock ./
2726

28-
# Copy source code
27+
# Copy backend Cargo file and source
28+
COPY backend/Cargo.toml ./backend/
29+
COPY backend/src ./backend/src
2930
COPY backend/crates ./backend/crates
30-
COPY cli/kalam-cli ./cli/kalam-cli
31-
COPY cli/kalam-link ./cli/kalam-link
3231

33-
# Build release binaries
34-
WORKDIR /build/backend
35-
RUN cargo build --release --bin kalamdb-server
32+
# Copy CLI Cargo file and source
33+
COPY cli/Cargo.toml ./cli/
34+
COPY cli/src ./cli/src
3635

37-
WORKDIR /build/cli
38-
RUN cargo build --release --bin kalam
36+
# Copy link (dependency for CLI)
37+
COPY link/Cargo.toml ./link/
38+
COPY link/src ./link/src
39+
40+
# Build release binaries
41+
RUN cargo build --release --bin kalamdb-server --bin kalam
3942

4043
# ============================================================================
4144
# Stage 2: Runtime
@@ -55,8 +58,8 @@ RUN useradd -m -u 1000 kalamdb && \
5558
chown -R kalamdb:kalamdb /data
5659

5760
# Copy binaries from builder
58-
COPY --from=builder /build/backend/target/release/kalamdb-server /usr/local/bin/kalamdb-server
59-
COPY --from=builder /build/cli/target/release/kalam /usr/local/bin/kalam-cli
61+
COPY --from=builder /build/target/release/kalamdb-server /usr/local/bin/kalamdb-server
62+
COPY --from=builder /build/target/release/kalam /usr/local/bin/kalam-cli
6063

6164
# Make binaries executable
6265
RUN chmod +x /usr/local/bin/kalamdb-server /usr/local/bin/kalam-cli

link/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ serde_json = { workspace = true }
2828
uuid = { workspace = true, features = ["js"] }
2929

3030
# Random number generation for WASM
31-
getrandom = { version = "0.2", features = ["js"], optional = true }
31+
getrandom = { version = "0.2", features = ["js"] }
3232

3333
# Futures utilities
3434
futures-util = { version = "0.3", default-features = false }
@@ -46,7 +46,7 @@ web-sys = { version = "0.3", features = ["console"], optional = true }
4646
[features]
4747
default = ["tokio-runtime"]
4848
tokio-runtime = ["tokio", "reqwest", "tokio-tungstenite"]
49-
wasm = ["wasm-bindgen", "wasm-bindgen-futures", "js-sys", "web-sys", "getrandom"]
49+
wasm = ["wasm-bindgen", "wasm-bindgen-futures", "js-sys", "web-sys"]
5050

5151
[dev-dependencies]
5252
tokio = { workspace = true, features = ["test-util", "macros"] }

link/README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
Rust client library for KalamDB with WebAssembly support.
44

5+
## Project Structure
6+
7+
```
8+
link/
9+
├── src/ # Rust source code
10+
│ ├── lib.rs # Library entry point
11+
│ ├── wasm.rs # WASM bindings (browser/Node.js)
12+
│ ├── client.rs # Native Rust client (for CLI)
13+
│ └── ...
14+
├── pkg/ # WASM build output (generated by build.sh)
15+
│ ├── kalam_link_bg.wasm
16+
│ ├── kalam_link.js
17+
│ ├── kalam_link.d.ts
18+
│ ├── package.json
19+
│ └── README.md
20+
├── tests-wasm/ # WASM/TypeScript tests
21+
│ ├── basic.test.mjs
22+
│ ├── connection.test.mjs
23+
│ ├── query.test.mjs
24+
│ └── subscription.test.mjs
25+
├── build.sh # WASM build script (use this!)
26+
└── Cargo.toml
27+
```
28+
529
## Features
630

731
- 🦀 **Dual-mode library**: Use natively in Rust or compile to WebAssembly for JavaScript/TypeScript
@@ -18,27 +42,39 @@ Add to your `Cargo.toml`:
1842

1943
```toml
2044
[dependencies]
21-
kalam-link = { path = "../kalam-link" }
45+
kalam-link = { path = "../link" }
2246
```
2347

2448
### WebAssembly (Browser/Node.js)
2549

26-
Build the WASM module:
50+
Build the WASM module using the provided script:
2751

2852
```bash
29-
# Install wasm-pack if not already installed
30-
cargo install wasm-pack
31-
32-
# Build for web target
33-
cd cli/kalam-link
34-
wasm-pack build --target web --out-dir pkg --features wasm --no-default-features
53+
cd link
54+
./build.sh
3555
```
3656

37-
This generates:
38-
- `pkg/kalam_link_bg.wasm` - WASM binary (36KB)
39-
- `pkg/kalam_link.js` - JavaScript bindings
40-
- `pkg/kalam_link.d.ts` - TypeScript definitions
41-
- `pkg/package.json` - NPM package metadata
57+
This generates everything in `pkg/`:
58+
- `kalam_link_bg.wasm` - WASM binary (~40KB)
59+
- `kalam_link.js` - JavaScript bindings
60+
- `kalam_link.d.ts` - TypeScript definitions
61+
- `package.json` - NPM package metadata with enhanced info
62+
- `README.md` - Detailed usage documentation
63+
64+
### Build Options
65+
66+
```bash
67+
# Development build (default)
68+
./build.sh
69+
70+
# Release build (optimized)
71+
./build.sh --release
72+
73+
# Different targets
74+
./build.sh --target web # For browsers (default)
75+
./build.sh --target nodejs # For Node.js
76+
./build.sh --target bundler # For webpack/rollup
77+
```
4278

4379
## Usage
4480

link/build.sh

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/bin/bash
2+
#
3+
# WASM Build Script for kalam-link
4+
#
5+
# This script compiles the Rust library to WebAssembly and generates
6+
# TypeScript bindings for browser and Node.js usage.
7+
#
8+
# Requirements:
9+
# - Rust 1.75+ with wasm32-unknown-unknown target
10+
# - wasm-pack 0.12+
11+
#
12+
# Usage:
13+
# ./build.sh [--release] [--target <web|nodejs|bundler>]
14+
#
15+
# Outputs:
16+
# pkg/ - WASM module with JavaScript/TypeScript bindings
17+
18+
set -e
19+
20+
# Colors for output
21+
RED='\033[0;31m'
22+
GREEN='\033[0;32m'
23+
YELLOW='\033[1;33m'
24+
BLUE='\033[0;34m'
25+
NC='\033[0m' # No Color
26+
27+
# Default values
28+
BUILD_TYPE="dev"
29+
TARGET="web"
30+
31+
# Parse arguments
32+
while [[ $# -gt 0 ]]; do
33+
case $1 in
34+
--release)
35+
BUILD_TYPE="release"
36+
shift
37+
;;
38+
--target)
39+
TARGET="$2"
40+
shift 2
41+
;;
42+
-h|--help)
43+
echo "Usage: $0 [--release] [--target <web|nodejs|bundler>]"
44+
echo ""
45+
echo "Options:"
46+
echo " --release Build in release mode (optimized)"
47+
echo " --target Target platform (web, nodejs, bundler)"
48+
echo " -h, --help Show this help message"
49+
exit 0
50+
;;
51+
*)
52+
echo -e "${RED}Error: Unknown option $1${NC}"
53+
exit 1
54+
;;
55+
esac
56+
done
57+
58+
echo -e "${BLUE}======================================${NC}"
59+
echo -e "${BLUE} KalamDB WASM Build Script${NC}"
60+
echo -e "${BLUE}======================================${NC}"
61+
echo ""
62+
63+
# Check prerequisites
64+
echo -e "${YELLOW}Checking prerequisites...${NC}"
65+
66+
if ! command -v rustc &> /dev/null; then
67+
echo -e "${RED}Error: Rust is not installed${NC}"
68+
echo "Install from: https://rustup.rs/"
69+
exit 1
70+
fi
71+
72+
if ! command -v wasm-pack &> /dev/null; then
73+
echo -e "${RED}Error: wasm-pack is not installed${NC}"
74+
echo "Install with: cargo install wasm-pack"
75+
exit 1
76+
fi
77+
78+
# Check wasm32-unknown-unknown target
79+
if ! rustup target list --installed | grep -q "wasm32-unknown-unknown"; then
80+
echo -e "${YELLOW}Installing wasm32-unknown-unknown target...${NC}"
81+
rustup target add wasm32-unknown-unknown
82+
fi
83+
84+
echo -e "${GREEN}✓ Prerequisites OK${NC}"
85+
echo ""
86+
87+
# Build WASM module
88+
echo -e "${YELLOW}Building WASM module...${NC}"
89+
echo " Build type: $BUILD_TYPE"
90+
echo " Target: $TARGET"
91+
echo ""
92+
93+
# Construct wasm-pack command
94+
WASM_PACK_CMD="wasm-pack build --target $TARGET --out-dir pkg --no-default-features --features wasm"
95+
96+
if [ "$BUILD_TYPE" = "release" ]; then
97+
WASM_PACK_CMD="$WASM_PACK_CMD --release"
98+
fi
99+
100+
# Run wasm-pack
101+
eval $WASM_PACK_CMD
102+
103+
if [ $? -eq 0 ]; then
104+
echo ""
105+
echo -e "${GREEN}✓ WASM build successful${NC}"
106+
else
107+
echo -e "${RED}Error: WASM build failed${NC}"
108+
exit 1
109+
fi
110+
111+
# Verify output
112+
echo ""
113+
echo -e "${YELLOW}Verifying output files...${NC}"
114+
115+
REQUIRED_FILES=(
116+
"pkg/kalam_link_bg.wasm"
117+
"pkg/kalam_link.js"
118+
"pkg/kalam_link.d.ts"
119+
"pkg/package.json"
120+
)
121+
122+
ALL_PRESENT=true
123+
for file in "${REQUIRED_FILES[@]}"; do
124+
if [ -f "$file" ]; then
125+
SIZE=$(du -h "$file" | cut -f1)
126+
echo -e " ${GREEN}${NC} $file ($SIZE)"
127+
else
128+
echo -e " ${RED}${NC} $file (missing)"
129+
ALL_PRESENT=false
130+
fi
131+
done
132+
133+
if [ "$ALL_PRESENT" = false ]; then
134+
echo -e "${RED}Error: Some output files are missing${NC}"
135+
exit 1
136+
fi
137+
138+
echo ""
139+
echo -e "${GREEN}✓ All output files present${NC}"
140+
141+
# Enhance package.json with better metadata
142+
echo ""
143+
echo -e "${YELLOW}Enhancing package.json...${NC}"
144+
145+
# Create enhanced package.json
146+
cat > pkg/package.json << 'EOF'
147+
{
148+
"name": "@kalamdb/sdk",
149+
"version": "0.1.0",
150+
"description": "TypeScript/JavaScript SDK for KalamDB - WebAssembly-powered database client",
151+
"main": "kalam_link.js",
152+
"types": "kalam_link.d.ts",
153+
"type": "module",
154+
"files": [
155+
"kalam_link_bg.wasm",
156+
"kalam_link.js",
157+
"kalam_link.d.ts",
158+
"kalam_link_bg.wasm.d.ts",
159+
"package.json",
160+
"README.md"
161+
],
162+
"keywords": [
163+
"kalamdb",
164+
"database",
165+
"client",
166+
"websocket",
167+
"wasm",
168+
"webassembly",
169+
"realtime",
170+
"typescript",
171+
"javascript"
172+
],
173+
"author": "KalamDB Team",
174+
"license": "MIT",
175+
"repository": {
176+
"type": "git",
177+
"url": "https://github.com/jamals86/KalamDB.git",
178+
"directory": "link"
179+
},
180+
"bugs": {
181+
"url": "https://github.com/jamals86/KalamDB/issues"
182+
},
183+
"homepage": "https://github.com/jamals86/KalamDB#readme",
184+
"engines": {
185+
"node": ">=18.0.0"
186+
}
187+
}
188+
EOF
189+
190+
echo -e "${GREEN}✓ package.json enhanced${NC}"
191+
192+
# Print usage instructions
193+
echo ""
194+
echo -e "${BLUE}======================================${NC}"
195+
echo -e "${BLUE} Build Complete!${NC}"
196+
echo -e "${BLUE}======================================${NC}"
197+
echo ""
198+
echo -e "Output location:"
199+
echo -e " - WASM SDK: ${GREEN}pkg/${NC}"
200+
echo ""
201+
echo -e "Usage in TypeScript/JavaScript:"
202+
echo -e "${YELLOW}import init, { KalamClient } from './pkg/kalam_link.js';${NC}"
203+
echo ""
204+
echo -e "${YELLOW}await init();${NC}"
205+
echo -e "${YELLOW}const client = new KalamClient('ws://localhost:8080', 'your-api-key');${NC}"
206+
echo -e "${YELLOW}await client.connect();${NC}"
207+
echo ""
208+
echo -e "Next steps:"
209+
echo -e " 1. Test the WASM module: ${GREEN}node test-wasm.mjs${NC}"
210+
echo -e " 2. Publish to npm: ${GREEN}cd pkg && npm publish${NC}"
211+
echo -e " 3. Use in your app: ${GREEN}npm install kalam-link${NC}"
212+
echo ""

0 commit comments

Comments
 (0)