samurai-net 0.0.0-stage → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # Temporary Holding Version
1
+ # `samurai-net` — Zero-Build CLI, Benchmark & MCP Launcher for Samurai
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `samurai-net` provides a single `npx` / `npm` entrypoint for the entire **Samurai (C++17)** network resilience platform:
4
+
5
+ - **`samurai`** CLI (`--worker cpu|afpacket|afxdp|dpdk|gpu|doca|l7|scenario|telco|defense`)
6
+ - **`samurai-mcp`** native C++17 MCP `2024-11-05` server (`22` tools, `5` skill resources, `5` prompts, plus `doctor` / `setup`)
7
+ - **`samurai-master` / `samurai-slave`** distributed ZMQ cluster daemons
8
+ - **Benchmarks:** `npx -y samurai-net bench 1m-tcp`, `bench 100k-tcp`, `bench l7`
9
+
10
+ ## Quick Usage
11
+
12
+ ```bash
13
+ # 1. Run the 1M stateful TCP flow benchmark (605 B/flow)
14
+ npx -y samurai-net bench 1m-tcp
15
+
16
+ # 2. Diagnose binaries, workers, and AI client registrations
17
+ npx -y samurai-net mcp doctor
18
+
19
+ # 3. Auto-register Samurai MCP into Claude Code / Cursor / Codex / Antigravity / VS Code
20
+ npx -y samurai-net mcp setup --client cursor --yes
21
+
22
+ # 4. Run a stateful HTTP/2 load test
23
+ npx -y samurai-net --worker l7 --target 127.0.0.1 --port 8080 --l7-protocol http2 --count 10000 --confirm
24
+ ```
25
+
26
+ On **Linux x86_64**, `samurai-net` caches and SHA-256 verifies the pre-built native GitHub Release tarball in `~/.cache/samurai/` (or uses your local `build/` binaries if present). On **macOS, Windows, and ARM64**, it seamlessly routes commands through `ghcr.io/mehmet8-dev/samurai:latest` via Docker.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { runCli } = require("../lib/launcher");
5
+
6
+ runCli("samurai_master", process.argv.slice(2)).then((code) => process.exit(code));
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { runCli } = require("../lib/launcher");
5
+
6
+ runCli("samurai_mcp", process.argv.slice(2)).then((code) => process.exit(code));
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { runCli } = require("../lib/launcher");
5
+
6
+ runCli("samurai_slave", process.argv.slice(2)).then((code) => process.exit(code));
package/bin/samurai.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { runCli } = require("../lib/launcher");
5
+
6
+ runCli("samurai", process.argv.slice(2)).then((code) => process.exit(code));
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+
3
+ const crypto = require("node:crypto");
4
+ const fs = require("node:fs");
5
+ const os = require("node:os");
6
+ const path = require("node:path");
7
+ const { spawnSync } = require("node:child_process");
8
+
9
+ const PKG = require("../package.json");
10
+
11
+ const DEFAULT_REPO = "Mehmet8-Dev/Samurai";
12
+ const DEFAULT_IMAGE = "ghcr.io/mehmet8-dev/samurai:latest";
13
+
14
+ const LOCAL_SUBDIRS = [
15
+ "build/cli",
16
+ "build/mcp",
17
+ "build/control/daemon",
18
+ "build/zmq",
19
+ "build/l4",
20
+ ];
21
+
22
+ function isExecutable(filePath) {
23
+ try {
24
+ fs.accessSync(filePath, fs.constants.X_OK);
25
+ return true;
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ function findInPath(cmd) {
32
+ const pathEnv = process.env.PATH || "";
33
+ const extList = process.platform === "win32" ? [".exe", ".cmd", ".bat", ""] : [""];
34
+ for (const dir of pathEnv.split(path.delimiter)) {
35
+ if (!dir) continue;
36
+ for (const ext of extList) {
37
+ const candidate = path.join(dir, cmd + ext);
38
+ if (isExecutable(candidate)) return candidate;
39
+ }
40
+ }
41
+ return null;
42
+ }
43
+
44
+ /**
45
+ * Resolve friendly wrapper commands like `samurai-net bench 1m-tcp` or
46
+ * `samurai-net mcp` onto the underlying native binary + arguments.
47
+ */
48
+ function resolveTargetCommand(defaultBinary, argv) {
49
+ if (defaultBinary === "samurai" && argv.length > 0) {
50
+ if (argv[0] === "bench" || argv[0] === "benchmark") {
51
+ const sub = argv[1] || "";
52
+ const benchMap = {
53
+ "1m-tcp": "samurai_bench_1m_tcp",
54
+ "100k-tcp": "samurai_bench_100k_tcp",
55
+ "l7": "samurai_bench_l7",
56
+ };
57
+ if (benchMap[sub]) {
58
+ return { binary: benchMap[sub], args: argv.slice(2) };
59
+ }
60
+ }
61
+ if (argv[0] === "mcp") {
62
+ const sub = argv[1] || "";
63
+ // `samurai` handles `mcp doctor` and `mcp setup` directly; stdio MCP server
64
+ // (`samurai-net mcp` or `samurai-net mcp --help`) routes to `samurai_mcp`.
65
+ if (sub !== "doctor" && sub !== "setup") {
66
+ return { binary: "samurai_mcp", args: argv.slice(1) };
67
+ }
68
+ }
69
+ }
70
+ return { binary: defaultBinary, args: argv };
71
+ }
72
+
73
+ function findLocalBinary(binaryName, cwd = process.cwd(), env = process.env) {
74
+ const altNames = [binaryName];
75
+ if (binaryName === "samurai_bench_1m_tcp") altNames.push("test_1m_tcp_flows");
76
+ if (binaryName === "samurai_bench_100k_tcp") altNames.push("test_100k_tcp_flows");
77
+ if (binaryName === "samurai_bench_l7") altNames.push("bench_l7_stateful_userspace");
78
+
79
+ const searchDirs = [];
80
+ if (env.SAMURAI_BIN_DIR) searchDirs.push(env.SAMURAI_BIN_DIR);
81
+ for (const sub of LOCAL_SUBDIRS) {
82
+ searchDirs.push(path.join(cwd, sub));
83
+ searchDirs.push(path.resolve(__dirname, "..", "..", sub));
84
+ }
85
+ searchDirs.push("/usr/local/bin");
86
+
87
+ for (const name of altNames) {
88
+ for (const dir of searchDirs) {
89
+ const cand = path.join(dir, name);
90
+ if (isExecutable(cand)) return cand;
91
+ }
92
+ }
93
+ return null;
94
+ }
95
+
96
+ function getCacheRoot(env = process.env) {
97
+ if (env.SAMURAI_CACHE_DIR) return env.SAMURAI_CACHE_DIR;
98
+ return path.join(os.homedir(), ".cache", "samurai");
99
+ }
100
+
101
+ function sha256File(filePath) {
102
+ const hash = crypto.createHash("sha256");
103
+ hash.update(fs.readFileSync(filePath));
104
+ return hash.digest("hex");
105
+ }
106
+
107
+ async function downloadToFile(url, destPath) {
108
+ const res = await fetch(url, { redirect: "follow" });
109
+ if (!res.ok) {
110
+ throw new Error(`HTTP ${res.status} fetching ${url}`);
111
+ }
112
+ const buf = Buffer.from(await res.arrayBuffer());
113
+ fs.writeFileSync(destPath, buf);
114
+ }
115
+
116
+ /**
117
+ * Ensure the Linux x86_64 GitHub Release bundle is cached and verified by SHA-256.
118
+ * Returns the extracted `bin/` directory on success, or null if unavailable.
119
+ */
120
+ async function ensureCachedLinuxRelease(version, env = process.env) {
121
+ const tag = version.startsWith("v") ? version : `v${version}`;
122
+ const repo = env.SAMURAI_REPO || DEFAULT_REPO;
123
+ const cacheDir = path.join(getCacheRoot(env), tag);
124
+ const binDir = path.join(cacheDir, "bin");
125
+ const marker = path.join(cacheDir, ".verified");
126
+
127
+ if (fs.existsSync(marker) && isExecutable(path.join(binDir, "samurai"))) {
128
+ return binDir;
129
+ }
130
+
131
+ const tarName = `samurai-${tag}-linux-x86_64.tar.gz`;
132
+ const baseUrl = `https://github.com/${repo}/releases/download/${tag}`;
133
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "samurai-net-"));
134
+
135
+ try {
136
+ const tarPath = path.join(tmpDir, tarName);
137
+ const shaPath = path.join(tmpDir, `${tarName}.sha256`);
138
+
139
+ process.stderr.write(`[samurai-net] Downloading ${tarName} from GitHub Releases...\n`);
140
+ await downloadToFile(`${baseUrl}/${tarName}`, tarPath);
141
+
142
+ // Verify checksum when .sha256 asset is published alongside the tarball.
143
+ try {
144
+ await downloadToFile(`${baseUrl}/${tarName}.sha256`, shaPath);
145
+ const expected = fs.readFileSync(shaPath, "utf8").trim().split(/\s+/)[0].toLowerCase();
146
+ const actual = sha256File(tarPath).toLowerCase();
147
+ if (!expected || expected !== actual) {
148
+ throw new Error(`SHA-256 mismatch for ${tarName}: expected ${expected}, got ${actual}`);
149
+ }
150
+ } catch (err) {
151
+ if (String(err.message || err).includes("SHA-256 mismatch")) throw err;
152
+ }
153
+
154
+ fs.mkdirSync(cacheDir, { recursive: true });
155
+ const extract = spawnSync("tar", ["-xzf", tarPath, "--strip-components=1", "-C", cacheDir], {
156
+ stdio: "inherit",
157
+ });
158
+ if (extract.status !== 0) {
159
+ throw new Error(`tar extraction exited with status ${extract.status}`);
160
+ }
161
+ fs.writeFileSync(marker, `${tag}\n`);
162
+ return binDir;
163
+ } finally {
164
+ fs.rmSync(tmpDir, { recursive: true, force: true });
165
+ }
166
+ }
167
+
168
+ function buildDockerArgs(binary, args, opts = {}) {
169
+ const platform = opts.platform || process.platform;
170
+ const image = opts.image || process.env.SAMURAI_IMAGE || DEFAULT_IMAGE;
171
+ const cwd = opts.cwd || process.cwd();
172
+ const home = opts.home || os.homedir();
173
+ const isMcpConfigCmd =
174
+ (binary === "samurai_mcp" && (args[0] === "setup" || args[0] === "doctor")) ||
175
+ (binary === "samurai" && args[0] === "mcp" && (args[1] === "setup" || args[1] === "doctor"));
176
+
177
+ const dockerArgs = ["run", "--rm", "-i"];
178
+ if (isMcpConfigCmd) {
179
+ if (typeof process.getuid === "function" && typeof process.getgid === "function") {
180
+ dockerArgs.push("-u", `${process.getuid()}:${process.getgid()}`);
181
+ }
182
+ dockerArgs.push("-v", `${home}:${home}`, "-v", `${cwd}:${cwd}`, "-w", cwd, "-e", `HOME=${home}`);
183
+ } else if (platform === "linux") {
184
+ dockerArgs.push("--network", "host");
185
+ } else {
186
+ dockerArgs.push("--add-host", "host.docker.internal:host-gateway");
187
+ }
188
+
189
+ dockerArgs.push(image, binary, ...args);
190
+ return dockerArgs;
191
+ }
192
+
193
+ function execBinary(binPath, args, extraBinDir) {
194
+ const env = { ...process.env };
195
+ if (extraBinDir) {
196
+ env.SAMURAI_BIN_DIR = extraBinDir;
197
+ env.PATH = `${extraBinDir}${path.delimiter}${env.PATH || ""}`;
198
+ }
199
+ const res = spawnSync(binPath, args, { stdio: "inherit", env });
200
+ if (res.error) {
201
+ process.stderr.write(`[samurai-net] Failed to execute ${binPath}: ${res.error.message}\n`);
202
+ return 1;
203
+ }
204
+ if (res.signal) {
205
+ process.kill(process.pid, res.signal);
206
+ return 1;
207
+ }
208
+ return typeof res.status === "number" ? res.status : 1;
209
+ }
210
+
211
+ async function runCli(defaultBinary, rawArgv) {
212
+ const { binary, args } = resolveTargetCommand(defaultBinary, rawArgv);
213
+ const forceDocker = process.env.SAMURAI_DOCKER === "1";
214
+
215
+ if (!forceDocker) {
216
+ const localBin = findLocalBinary(binary);
217
+ if (localBin) {
218
+ return execBinary(localBin, args, path.dirname(localBin));
219
+ }
220
+
221
+ if (process.platform === "linux" && process.arch === "x64") {
222
+ try {
223
+ const cachedBinDir = await ensureCachedLinuxRelease(PKG.version);
224
+ if (cachedBinDir) {
225
+ const cachedBin = path.join(cachedBinDir, binary);
226
+ if (isExecutable(cachedBin)) {
227
+ return execBinary(cachedBin, args, cachedBinDir);
228
+ }
229
+ }
230
+ } catch (err) {
231
+ process.stderr.write(`[samurai-net] Native release unavailable (${err.message}); falling back to Docker...\n`);
232
+ }
233
+ }
234
+ }
235
+
236
+ const dockerBin = findInPath("docker");
237
+ if (!dockerBin) {
238
+ process.stderr.write(
239
+ `[samurai-net] Could not locate native '${binary}' binary and 'docker' was not found in PATH.\n` +
240
+ `Install Docker (https://docs.docker.com/get-docker/) or build locally with:\n` +
241
+ ` cmake -B build -S samurai -DCMAKE_BUILD_TYPE=Release && cmake --build build -j\n`
242
+ );
243
+ return 1;
244
+ }
245
+
246
+ const dockerArgs = buildDockerArgs(binary, args);
247
+ return execBinary(dockerBin, dockerArgs, null);
248
+ }
249
+
250
+ module.exports = {
251
+ buildDockerArgs,
252
+ ensureCachedLinuxRelease,
253
+ findLocalBinary,
254
+ resolveTargetCommand,
255
+ runCli,
256
+ sha256File,
257
+ };
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "samurai-net",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "3.3.0",
4
+ "description": "Pure C++17 line-rate load, kernel-bypass & AI resilience engine — 1M TCP flows in 577 MiB (605 B/flow), HTTP/2-QUIC, GeForce BAR1 CUDA, 5G CUPS & native MCP.",
5
+ "license": "AGPL-3.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Mehmet8-Dev/Samurai.git"
9
+ },
10
+ "homepage": "https://github.com/Mehmet8-Dev/Samurai#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/Mehmet8-Dev/Samurai/issues"
13
+ },
14
+ "keywords": [
15
+ "networking",
16
+ "load-testing",
17
+ "stress-testing",
18
+ "tcp",
19
+ "http2",
20
+ "http3",
21
+ "quic",
22
+ "af-xdp",
23
+ "dpdk",
24
+ "cuda",
25
+ "5g",
26
+ "telecom",
27
+ "mcp",
28
+ "benchmark",
29
+ "cpp17"
30
+ ],
31
+ "engines": {
32
+ "node": ">=18.0.0"
33
+ },
34
+ "bin": {
35
+ "samurai-net": "bin/samurai.js",
36
+ "samurai": "bin/samurai.js",
37
+ "samurai-mcp": "bin/samurai-mcp.js",
38
+ "samurai-master": "bin/samurai-master.js",
39
+ "samurai-slave": "bin/samurai-slave.js"
40
+ },
41
+ "files": [
42
+ "bin/",
43
+ "lib/",
44
+ "README.md"
45
+ ],
46
+ "scripts": {
47
+ "test": "node --test test/*.test.js"
48
+ }
49
+ }