Files
ssw-mpc/tools/pg-dev/server.mjs
T
admin 54781b3f83
ci / gate (push) Waiting to run
refactor: 项目改名 ssw-mpc,crate 统一到 mpc-* 前缀
- 项目与文档标题:nax-rs → ssw-mpc(尚数网-多方安全计算),二进制 nax → mpc
- 18 个 crate 及目录 nax-* → mpc-*,Rust 引用 nax_* → mpc_*
- 环境变量、C 符号、构建/部署脚本:NAX_* → MPC_*、libnax_ray.so → libmpc_ray.so
- 保留协议域分隔常量与 refs/nax 参考实现路径,维持 golden 向量与历史数据兼容
- 新增 [profile.dev] incremental=false
2026-11-05 15:06:12 +08:00

60 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// 本地开发用的 PostgreSQL —— PGlite(Postgres 编成 WASM)+ PG-wire TCP 外壳。
//
// 为什么要它:`mpc sql-join --pg-url` 走的是手写的 PG v3 前端协议,正确性
// 只能拿**真的服务端**对拍(假服务端是我们自己写的,等于自证)。
// 但为了一个测试去 brew install 一个 200MB 的 PG 服务、还得管 data 目录、
// 端口、启动脚本,成本不成比例。PGlite 是一个 `npm i` 就位的 Postgres,
// 复用的是同一份 PG 源码(编译目标从 native 换成 wasm),协议行为一致。
//
// 用法:
// node server.mjs --port 5433 --init seed-bank.sql
// node server.mjs --port 5434 --init seed-shop.sql --data-dir .data/shop
//
// 退出:Ctrl-C / SIGTERM,会关掉 socket 与 db。
import { readFileSync } from 'node:fs'
import { PGlite } from '@electric-sql/pglite'
import { PGLiteSocketServer } from '@electric-sql/pglite-socket'
function arg(name, fallback) {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 && i + 1 < process.argv.length ? process.argv[i + 1] : fallback
}
const port = Number(arg('port', '5433'))
const host = arg('host', '127.0.0.1')
// 默认内存库:跑完即弃,不留垃圾。要反复调试才给 --data-dir。
const dataDir = arg('data-dir', 'memory://')
const initFile = arg('init', null)
const db = new PGlite(dataDir)
if (initFile) {
const sql = readFileSync(initFile, 'utf8')
// exec 支持多语句;PGlite 的 exec 与 psql 的 -f 语义接近
await db.exec(sql)
const r = await db.query(
`select count(*)::int as n from pg_catalog.pg_tables where schemaname = 'public'`,
)
console.log(`[pg-dev] 初始化 ${initFile}:public 下 ${r.rows[0].n} 张表`)
}
const server = new PGLiteSocketServer({ db, port, host })
await server.start()
// 这行是给脚本 grep 的就绪信号,别改格式
console.log(`[pg-dev] ready ${host}:${port} data=${dataDir}`)
const shutdown = async (sig) => {
console.log(`[pg-dev] ${sig},关闭中…`)
try {
await server.stop()
} catch {
/* 已经停了 */
}
await db.close()
process.exit(0)
}
process.on('SIGINT', () => void shutdown('SIGINT'))
process.on('SIGTERM', () => void shutdown('SIGTERM'))