Skip to content

Commit eb80ed4

Browse files
authored
broker: fix direct-run detection for symlinked release path (#105)
1 parent 94d03e1 commit eb80ed4

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

bin/broker-register.mjs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import os from "node:os";
1717
import path from "node:path";
1818
import readline from "node:readline";
1919
import { webcrypto } from "node:crypto";
20+
import { pathToFileURL } from "node:url";
2021

2122
const { subtle } = webcrypto;
2223

@@ -602,6 +603,27 @@ export async function runRegistration({
602603
};
603604
}
604605

606+
export function isMainModule(moduleUrl = import.meta.url, argv1 = process.argv[1]) {
607+
if (!argv1) return false;
608+
609+
let argvUrl = "";
610+
let argvRealUrl = "";
611+
612+
try {
613+
argvUrl = pathToFileURL(path.resolve(argv1)).href;
614+
} catch {
615+
argvUrl = "";
616+
}
617+
618+
try {
619+
argvRealUrl = pathToFileURL(fs.realpathSync(argv1)).href;
620+
} catch {
621+
argvRealUrl = "";
622+
}
623+
624+
return moduleUrl === argvUrl || (argvRealUrl !== "" && moduleUrl === argvRealUrl);
625+
}
626+
605627
export async function main(argv = process.argv.slice(2)) {
606628
const parsed = parseArgs(argv);
607629

@@ -650,7 +672,7 @@ export async function main(argv = process.argv.slice(2)) {
650672
return 0;
651673
}
652674

653-
if (import.meta.url === `file://${process.argv[1]}`) {
675+
if (isMainModule()) {
654676
main().catch((err) => {
655677
const message = err instanceof Error ? err.message : String(err);
656678
console.error(`❌ ${message}`);

bin/broker-register.test.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import test from "node:test";
22
import assert from "node:assert/strict";
3+
import fs from "node:fs";
4+
import os from "node:os";
5+
import path from "node:path";
36
import { createServer } from "node:http";
7+
import { pathToFileURL } from "node:url";
48
import {
59
parseArgs,
610
normalizeBrokerUrl,
@@ -9,6 +13,7 @@ import {
913
registerWithBroker,
1014
upsertEnvContent,
1115
runRegistration,
16+
isMainModule,
1217
} from "./broker-register.mjs";
1318

1419
const FIXTURE_SERVER_KEYS = {
@@ -53,6 +58,24 @@ test("parseArgs sets verbose=true for -v and --verbose", () => {
5358
assert.equal(long.verbose, true);
5459
});
5560

61+
test("isMainModule handles symlink argv path", () => {
62+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "broker-register-main-"));
63+
const realFile = path.join(tempDir, "real.mjs");
64+
const symlinkFile = path.join(tempDir, "link.mjs");
65+
66+
try {
67+
fs.writeFileSync(realFile, "export default 1;\n", "utf8");
68+
fs.symlinkSync(realFile, symlinkFile);
69+
70+
const moduleUrl = pathToFileURL(fs.realpathSync(realFile)).href;
71+
assert.equal(isMainModule(moduleUrl, symlinkFile), true);
72+
} finally {
73+
try { fs.unlinkSync(symlinkFile); } catch {}
74+
try { fs.unlinkSync(realFile); } catch {}
75+
try { fs.rmdirSync(tempDir); } catch {}
76+
}
77+
});
78+
5679
test("parseArgs accepts registration token", () => {
5780
const parsed = parseArgs(["--registration-token", "token-123"]);
5881
assert.equal(parsed.registrationToken, "token-123");

0 commit comments

Comments
 (0)