Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(cli): refresh native bindings before startup

Yuting Zhong2ead32965de6
diff --git a/notfair/bin/cli.mjs b/notfair/bin/cli.mjs
index 11f3d2e7..dd9d1bd5 100755
--- a/notfair/bin/cli.mjs
+++ b/notfair/bin/cli.mjs
@@ -27,6 +27,8 @@ import { createServer } from "node:net";
import { Command } from "commander";
import open from "open";
+import { syncStandaloneNativeBindings } from "./native-bindings.mjs";
+
const CLI_PATH = fileURLToPath(import.meta.url);
const __dirname = dirname(CLI_PATH);
const PKG_ROOT = dirname(__dirname);
@@ -92,6 +94,7 @@ program
// Next.js standalone output omits .next/static and public by default; copy
// them in if they're missing so the server can serve CSS/JS chunks.
ensureStandaloneAssets();
+ syncStandaloneNativeBindings(PKG_ROOT);
const url = `http://127.0.0.1:${port}`;
const env = {
diff --git a/notfair/bin/native-bindings.mjs b/notfair/bin/native-bindings.mjs
new file mode 100644
index 00000000..353b2f60
--- /dev/null
+++ b/notfair/bin/native-bindings.mjs
@@ -0,0 +1,35 @@
+import { copyFileSync, existsSync, readdirSync } from "node:fs";
+import { join } from "node:path";
+
+/**
+ * npm builds the top-level better-sqlite3 dependency for the Node.js version
+ * that installed NotFair. Next.js also ships traced copies compiled on the
+ * release builder, so refresh those copies before every server start.
+ */
+export function syncStandaloneNativeBindings(packageRoot) {
+ const runtimeBinding = join(
+ packageRoot,
+ "node_modules",
+ "better-sqlite3",
+ "build",
+ "Release",
+ "better_sqlite3.node",
+ );
+ if (!existsSync(runtimeBinding)) return 0;
+
+ const targets = [];
+ const visit = (directory) => {
+ for (const entry of readdirSync(directory, { withFileTypes: true })) {
+ const path = join(directory, entry.name);
+ if (entry.isDirectory()) {
+ visit(path);
+ } else if (entry.isFile() && entry.name === "better_sqlite3.node") {
+ targets.push(path);
+ }
+ }
+ };
+ visit(join(packageRoot, ".next", "standalone"));
+
+ for (const target of targets) copyFileSync(runtimeBinding, target);
+ return targets.length;
+}
diff --git a/notfair/src/server/native-bindings.test.ts b/notfair/src/server/native-bindings.test.ts
new file mode 100644
index 00000000..da5fcd61
--- /dev/null
+++ b/notfair/src/server/native-bindings.test.ts
@@ -0,0 +1,48 @@
+import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { describe, expect, it } from "vitest";
+
+import { syncStandaloneNativeBindings } from "../../bin/native-bindings.mjs";
+
+describe("syncStandaloneNativeBindings", () => {
+ it("refreshes release-built bindings before a fresh CLI start", async () => {
+ const packageRoot = await mkdtemp(join(tmpdir(), "notfair-cli-bindings-"));
+ const runtimeDir = join(packageRoot, "node_modules", "better-sqlite3", "build", "Release");
+ const tracedDir = join(
+ packageRoot,
+ ".next",
+ "standalone",
+ ".next",
+ "node_modules",
+ "better-sqlite3-traced",
+ "build",
+ "Release",
+ );
+
+ try {
+ await Promise.all([
+ mkdir(runtimeDir, { recursive: true }),
+ mkdir(tracedDir, { recursive: true }),
+ ]);
+ await writeFile(join(runtimeDir, "better_sqlite3.node"), "local-node-binding");
+ await writeFile(join(tracedDir, "better_sqlite3.node"), "release-node-binding");
+
+ expect(syncStandaloneNativeBindings(packageRoot)).toBe(1);
+ await expect(readFile(join(tracedDir, "better_sqlite3.node"), "utf8")).resolves.toBe(
+ "local-node-binding",
+ );
+ } finally {
+ await rm(packageRoot, { recursive: true, force: true });
+ }
+ });
+
+ it("is a no-op when npm did not install a runtime binding", async () => {
+ const packageRoot = await mkdtemp(join(tmpdir(), "notfair-cli-bindings-"));
+ try {
+ expect(syncStandaloneNativeBindings(packageRoot)).toBe(0);
+ } finally {
+ await rm(packageRoot, { recursive: true, force: true });
+ }
+ });
+});