surrealdb-server 3.1.2

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fs, mem};

use anyhow::Result;
use surrealism_runtime::PrefixErr;
use surrealism_runtime::config::{AbiVersion, SurrealismConfig, Target};
use surrealism_runtime::exports::{ExportsManifest, FunctionExport};
use surrealism_runtime::package::SurrealismPackage;
use surrealism_runtime::runtime::Runtime;
use tempfile::TempDir;
use wasm_encoder::{ComponentSectionId, Encode, RawSection, Section};
use wasm_opt::OptimizationOptions;

/// Build a Surrealism WASM module from a Rust project, optimize the binary,
/// and pack it into a `.surrealism` package file.
pub async fn init(path: Option<PathBuf>, out: Option<PathBuf>, debug: bool) -> Result<()> {
	let path = match path {
		Some(p) => p,
		None => std::env::current_dir().prefix_err(|| "Failed to determine current directory")?,
	};
	let config = load_config(&path)?;

	if config.target != Target::Rust {
		anyhow::bail!(
			"Unsupported target '{}'. Only 'rust' is currently supported.",
			config.target
		);
	}

	let metadata = metadata(&path).prefix_err(|| "Failed to retrieve cargo metadata")?;
	validate_sdk_version(&metadata)?;
	let (package_name, source_wasm) = get_source_wasm(&metadata, &path, debug)?;

	build_wasm_module(&path, &package_name, debug)?;

	if !source_wasm.exists() {
		anyhow::bail!("Expected WASM file not found: {}", source_wasm.display());
	}
	let compiled = fs::read(&source_wasm).prefix_err(|| "Failed to read WASM file")?;

	let wasm = if debug {
		compiled
	} else {
		println!("Optimizing bundle...");
		optimize_component(&compiled)?
	};

	let fs_dir = resolve_attach_fs(&path, &config)?;
	let logo = resolve_logo(&path)?;

	println!("Extracting function signatures...");
	let exports = extract_exports(&wasm, &config).await?;

	let package = SurrealismPackage {
		config,
		wasm,
		exports,
		fs: None,
		logo,
	};
	let out = resolve_output_path(out, &package.config)?;
	package.pack(out, fs_dir.as_deref()).prefix_err(|| "Failed to pack Surrealism package")?;

	Ok(())
}

/// Instantiate the compiled WASM temporarily to extract function signatures
/// into an `ExportsManifest`.
async fn extract_exports(wasm: &[u8], config: &SurrealismConfig) -> Result<ExportsManifest> {
	use crate::cli::module::host::DemoHost;

	let temp_package = SurrealismPackage {
		config: config.clone(),
		wasm: wasm.to_vec(),
		exports: ExportsManifest::empty(),
		fs: None,
		logo: None,
	};

	let runtime = Runtime::new(temp_package, 1, None, None, None, None)?;
	let host = Box::new(DemoHost::new());
	let mut controller =
		runtime.new_controller(host).await.prefix_err(|| "Failed to load WASM module")?;

	let names = controller.list().await.prefix_err(|| "Failed to list functions")?;

	let mut functions = Vec::new();
	for name in names {
		let display = name.as_deref().unwrap_or("<default>");
		let args = controller
			.args(name.clone())
			.await
			.prefix_err(|| format!("Failed to get args for '{display}'"))?;
		let returns = controller
			.returns(name.clone())
			.await
			.prefix_err(|| format!("Failed to get return type for '{display}'"))?;
		let writeable = controller
			.writeable(name.clone())
			.await
			.prefix_err(|| format!("Failed to get writeable flag for '{display}'"))?;
		let comment = controller
			.comment(name.clone())
			.await
			.prefix_err(|| format!("Failed to get comment for '{display}'"))?;

		let args_text = Some(args.iter().map(|(n, k)| format!("{n}: {k}")).collect());
		let returns_text = Some(returns.to_string());

		functions.push(FunctionExport {
			name,
			args,
			returns,
			args_text,
			returns_text,
			writeable,
			comment,
		});
	}

	Ok(ExportsManifest {
		functions,
	})
}

/// Parse the `surrealism.toml` configuration from the project directory.
///
/// The ABI version is always stamped to `AbiVersion::CURRENT` because the
/// build toolchain targets the current SDK; user-supplied values are ignored.
fn load_config(path: &Path) -> Result<SurrealismConfig> {
	let surrealism_toml = path.join("surrealism.toml");
	if !surrealism_toml.exists() {
		anyhow::bail!("surrealism.toml not found in the current directory");
	}

	let mut config = SurrealismConfig::parse(
		&fs::read_to_string(&surrealism_toml).prefix_err(|| "Failed to read surrealism.toml")?,
	)?;
	config.abi = AbiVersion::CURRENT;
	Ok(config)
}

/// Verify that the project's resolved `surrealism` crate version is compatible
/// with the version this toolchain was compiled against. Compatibility follows
/// Cargo's caret (`^`) semantics: the SDK must be at least the toolchain
/// version but within the same semver-compatible range.
///
/// For pre-1.0 (`0.x.y`): minor must match, patch may be ahead.
/// For post-1.0: major must match, minor/patch may be ahead.
fn validate_sdk_version(metadata: &serde_json::Value) -> Result<()> {
	let expected = surrealism_runtime::SDK_VERSION;

	let packages = metadata["packages"]
		.as_array()
		.ok_or_else(|| anyhow::anyhow!("No packages found in cargo metadata"))?;

	let surrealism_pkg = packages.iter().find(|pkg| pkg["name"].as_str() == Some("surrealism"));

	let Some(pkg) = surrealism_pkg else {
		anyhow::bail!(
			"surrealism dependency not found in the project's dependency tree.\n\
			 Ensure your Cargo.toml depends on the `surrealism` crate (version {expected})."
		);
	};

	let resolved_str = pkg["version"]
		.as_str()
		.ok_or_else(|| anyhow::anyhow!("surrealism package in metadata has no version"))?;

	let resolved: semver::Version = resolved_str
		.parse()
		.map_err(|e| anyhow::anyhow!("invalid surrealism version '{resolved_str}': {e}"))?;

	let req: semver::VersionReq = format!("^{expected}")
		.parse()
		.map_err(|e| anyhow::anyhow!("invalid toolchain SDK version '{expected}': {e}"))?;

	if !req.matches(&resolved) {
		anyhow::bail!(
			"surrealism SDK version mismatch: project uses {resolved}, \
			 but this toolchain requires ^{expected} (>={expected}, compatible range).\n\
			 Update your surrealism dependency to version {expected}."
		);
	}

	Ok(())
}

const WASM_TARGET: &str = "wasm32-wasip2";

/// Invoke `cargo build` targeting `wasm32-wasip2`.
fn build_wasm_module(path: &Path, package_name: &str, debug: bool) -> Result<()> {
	let target = WASM_TARGET;
	let profile = if debug {
		"debug"
	} else {
		"release"
	};
	println!("Building WASM module (target: {target}, profile: {profile})...");

	let mut cmd = Command::new("cargo");
	cmd.args(["build", "-p", package_name, "--target", target]);
	if !debug {
		cmd.arg("--release");
	}

	let cargo_status =
		cmd.current_dir(path).status().prefix_err(|| "Failed to execute cargo build")?;

	if !cargo_status.success() {
		anyhow::bail!("Cargo build failed");
	}

	Ok(())
}

/// Returns `true` if a custom section with the given name should be removed
/// from a release build. Preserves `component-type:*` sections which are
/// required by the component model.
fn should_strip_section(name: &str) -> bool {
	if name.starts_with("component-type") {
		return false;
	}
	name.starts_with(".debug")
		|| name == "name"
		|| name == "sourceMappingURL"
		|| name.starts_with("reloc.")
		|| name.starts_with("linking")
		|| name == "target_features"
		|| name == "producers"
}

/// Optimize a P2 component binary by stripping unnecessary custom sections
/// at all nesting levels and applying wasm-opt to embedded core modules.
///
/// `walrus` and `wasm-opt` only understand core WASM modules, not the
/// component model binary format. This function uses `wasmparser` to walk
/// the component's nested structure, strips metadata sections, and runs
/// Binaryen on each core module before re-embedding it.
///
/// The parse-rewrite approach is taken from the Bytecode Alliance's
/// [`wasm-tools strip`](https://github.com/bytecodealliance/wasm-tools/blob/main/src/bin/wasm-tools/strip.rs).
fn optimize_component(wasm_bytes: &[u8]) -> Result<Vec<u8>> {
	let mut output = Vec::new();
	let mut stack = Vec::new();

	for payload in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
		let payload = payload.prefix_err(|| "Failed to parse WASM component")?;

		#[allow(clippy::collapsible_match)]
		match &payload {
			wasmparser::Payload::Version {
				encoding,
				..
			} => {
				output.extend_from_slice(match encoding {
					wasmparser::Encoding::Component => &wasm_encoder::Component::HEADER,
					wasmparser::Encoding::Module => &wasm_encoder::Module::HEADER,
				});
				continue;
			}
			wasmparser::Payload::ModuleSection {
				..
			}
			| wasmparser::Payload::ComponentSection {
				..
			} => {
				stack.push(mem::take(&mut output));
				continue;
			}
			wasmparser::Payload::End(_) => {
				let mut parent = match stack.pop() {
					Some(p) => p,
					None => break,
				};
				let is_component = output.starts_with(&wasm_encoder::Component::HEADER);
				if !is_component {
					output = apply_wasm_opt(&output)?;
				}
				if is_component {
					parent.push(ComponentSectionId::Component as u8);
				} else {
					parent.push(ComponentSectionId::CoreModule as u8);
				}
				output.encode(&mut parent);
				output = parent;
				continue;
			}
			wasmparser::Payload::CustomSection(c) => {
				if should_strip_section(c.name()) {
					continue;
				}
			}
			_ => {}
		}

		if let Some((id, range)) = payload.as_section() {
			RawSection {
				id,
				data: &wasm_bytes[range],
			}
			.append_to(&mut output);
		}
	}

	Ok(output)
}

/// Run Binaryen's `wasm-opt` on a core module with aggressive size optimization
/// and common post-MVP features enabled.
fn apply_wasm_opt(wasm_bytes: &[u8]) -> Result<Vec<u8>> {
	let mut opts = OptimizationOptions::new_optimize_for_size_aggressively();
	opts.enable_feature(wasm_opt::Feature::BulkMemory);
	opts.enable_feature(wasm_opt::Feature::Simd);
	opts.enable_feature(wasm_opt::Feature::Atomics);
	opts.enable_feature(wasm_opt::Feature::MutableGlobals);
	opts.enable_feature(wasm_opt::Feature::TruncSat);
	opts.enable_feature(wasm_opt::Feature::SignExt);
	opts.debug_info(false);

	let temp_dir = TempDir::new().prefix_err(|| "Failed to create temporary directory")?;
	let temp_wasm_input = temp_dir.path().join("input.wasm");
	let temp_wasm_output = temp_dir.path().join("output.wasm");

	fs::write(&temp_wasm_input, wasm_bytes).prefix_err(|| "Failed to write temporary WASM file")?;

	opts.run(&temp_wasm_input, &temp_wasm_output)
		.prefix_err(|| "Failed to optimize WASM with wasm-opt")?;

	Ok(fs::read(&temp_wasm_output).prefix_err(|| "Failed to read optimized WASM file")?)
}

/// Locate the `.wasm` artifact produced by `cargo build` using `cargo metadata`
/// to resolve the target directory and package name.
fn get_source_wasm(
	metadata: &serde_json::Value,
	path: &Path,
	debug: bool,
) -> Result<(String, PathBuf)> {
	let target_directory = metadata["target_directory"]
		.as_str()
		.ok_or_else(|| anyhow::anyhow!("No target_directory found in cargo metadata"))?;

	let packages = metadata["packages"]
		.as_array()
		.ok_or_else(|| anyhow::anyhow!("No packages found in cargo metadata"))?;

	let current_package = packages
		.iter()
		.find(|pkg| {
			let manifest_path = pkg["manifest_path"].as_str().unwrap_or("");
			let manifest_pathbuf = PathBuf::from(manifest_path);
			let manifest_dir = manifest_pathbuf.parent();

			match (manifest_dir, path.canonicalize()) {
				(Some(manifest_dir), Ok(canonical_path)) => {
					manifest_dir.canonicalize().ok() == Some(canonical_path)
				}
				_ => false,
			}
		})
		.ok_or_else(|| {
			anyhow::anyhow!(
				"Could not find current package in metadata for path: {}",
				path.display()
			)
		})?;

	let package_name = current_package["name"]
		.as_str()
		.ok_or_else(|| anyhow::anyhow!("No package name found in metadata"))?;

	let wasm_filename = format!("{}.wasm", package_name.replace("-", "_"));
	let target = WASM_TARGET;
	let profile_dir = if debug {
		"debug"
	} else {
		"release"
	};
	let target_dir = PathBuf::from(target_directory).join(format!("{target}/{profile_dir}"));
	Ok((package_name.to_string(), target_dir.join(&wasm_filename)))
}

/// Run `cargo metadata` and return the parsed JSON.
///
/// Uses the full resolve (no `--no-deps`) so that transitive dependency
/// versions are available for SDK compatibility checks.
fn metadata(path: &Path) -> Result<serde_json::Value> {
	let output = Command::new("cargo")
		.args(["metadata", "--format-version", "1"])
		.current_dir(path)
		.output()
		.prefix_err(|| "Failed to execute cargo metadata")?;

	if !output.status.success() {
		anyhow::bail!("Failed to get cargo metadata");
	}

	let metadata_str =
		String::from_utf8(output.stdout).prefix_err(|| "Invalid UTF-8 in cargo metadata output")?;

	Ok(serde_json::from_str(&metadata_str).prefix_err(|| "Failed to parse cargo metadata JSON")?)
}

/// Read `logo.png` from the project root if it exists, validating format and size.
fn resolve_logo(project_root: &Path) -> Result<Option<Vec<u8>>> {
	use surrealism_runtime::package::{MAX_LOGO_BYTES, verify_logo};

	let logo_path = project_root.join("logo.png");
	if !logo_path.is_file() {
		return Ok(None);
	}

	let bytes = fs::read(&logo_path).prefix_err(|| "Failed to read logo.png")?;
	verify_logo(&bytes).prefix_err(|| {
		format!("Invalid logo.png (must be a valid PNG, max {} KiB)", MAX_LOGO_BYTES / 1024)
	})?;

	println!("Including logo.png ({} bytes)", bytes.len());
	Ok(Some(bytes))
}

/// Resolve the `[attach] fs` directory path from the config.
///
/// Returns `Some(path)` when a filesystem directory should be bundled, or
/// `None` when no `[attach] fs` is configured.
fn resolve_attach_fs(project_root: &Path, config: &SurrealismConfig) -> Result<Option<PathBuf>> {
	let Some(ref fs_value) = config.attach.fs else {
		return Ok(None);
	};

	if fs_value.is_empty() {
		return Ok(None);
	}

	let fs_path = PathBuf::from(fs_value);
	let resolved = if fs_path.is_absolute() {
		fs_path
	} else {
		project_root.join(&fs_path)
	};

	if !resolved.exists() {
		anyhow::bail!(
			"Attached filesystem directory not found: {} (resolved to {})",
			fs_value,
			resolved.display()
		);
	}
	if !resolved.is_dir() {
		anyhow::bail!(
			"Attached filesystem path is not a directory: {} (resolved to {})",
			fs_value,
			resolved.display()
		);
	}

	Ok(Some(resolved))
}

/// Resolve the output path for the `.surrealism` package, defaulting to
/// `<package_name>.surrealism` in the current working directory.
fn resolve_output_path(out: Option<PathBuf>, config: &SurrealismConfig) -> Result<PathBuf> {
	let cwd = || std::env::current_dir().prefix_err(|| "Failed to determine current directory");
	match out {
		None => Ok(cwd()?.join(config.file_name())),
		Some(out_path) => {
			if out_path.is_absolute() {
				Ok(out_path)
			} else {
				Ok(cwd()?.join(out_path))
			}
		}
	}
}