codex-multi-auth 2.0.1 → 2.0.2

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.
@@ -1,29 +1,29 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import {
4
- chmodSync,
5
- closeSync,
6
- fstatSync,
7
- ftruncateSync,
8
- mkdirSync,
9
- openSync,
10
- readFileSync,
11
- renameSync,
12
- rmSync,
13
- statSync,
14
- truncateSync,
15
- writeSync,
16
- writeFileSync,
17
- } from "node:fs";
3
+ import {
4
+ chmodSync,
5
+ closeSync,
6
+ fstatSync,
7
+ ftruncateSync,
8
+ mkdirSync,
9
+ openSync,
10
+ readFileSync,
11
+ renameSync,
12
+ rmSync,
13
+ statSync,
14
+ truncateSync,
15
+ writeSync,
16
+ writeFileSync,
17
+ } from "node:fs";
18
18
  import { basename, dirname, join } from "node:path";
19
19
  import process from "node:process";
20
20
 
21
- const DEFAULT_MAX_LOG_BYTES = 1024 * 1024;
22
- const LOG_SIZE_CHECK_INTERVAL_MS = 60_000;
23
-
24
- function parsePort(value) {
25
- if (typeof value !== "string" && typeof value !== "number") return Number.NaN;
26
- const text = String(value).trim();
21
+ const DEFAULT_MAX_LOG_BYTES = 1024 * 1024;
22
+ const LOG_SIZE_CHECK_INTERVAL_MS = 60_000;
23
+
24
+ function parsePort(value) {
25
+ if (typeof value !== "string" && typeof value !== "number") return Number.NaN;
26
+ const text = String(value).trim();
27
27
  if (!/^\d+$/.test(text)) return Number.NaN;
28
28
  const port = Number(text);
29
29
  return Number.isInteger(port) && port >= 0 && port <= 65535
@@ -33,13 +33,13 @@ function parsePort(value) {
33
33
 
34
34
  function parseArgs(argv) {
35
35
  const result = {
36
- host: "127.0.0.1",
37
- port: 0,
38
- statusPath: "",
39
- statePath: "",
40
- logPath: "",
41
- maxLogBytes: DEFAULT_MAX_LOG_BYTES,
42
- };
36
+ host: "127.0.0.1",
37
+ port: 0,
38
+ statusPath: "",
39
+ statePath: "",
40
+ logPath: "",
41
+ maxLogBytes: DEFAULT_MAX_LOG_BYTES,
42
+ };
43
43
  for (let index = 0; index < argv.length; index += 1) {
44
44
  const arg = argv[index];
45
45
  const next = argv[index + 1] ?? "";
@@ -58,26 +58,26 @@ function parseArgs(argv) {
58
58
  index += 1;
59
59
  continue;
60
60
  }
61
- if (arg === "--state") {
62
- result.statePath = next;
63
- index += 1;
64
- continue;
65
- }
66
- if (arg === "--max-log-bytes") {
67
- const parsed = Number.parseInt(next, 10);
68
- result.maxLogBytes =
69
- Number.isFinite(parsed) && parsed > 0
70
- ? parsed
71
- : DEFAULT_MAX_LOG_BYTES;
72
- index += 1;
73
- continue;
74
- }
75
- if (arg === "--log") {
76
- result.logPath = next;
77
- index += 1;
78
- }
79
- }
80
- return result;
61
+ if (arg === "--state") {
62
+ result.statePath = next;
63
+ index += 1;
64
+ continue;
65
+ }
66
+ if (arg === "--max-log-bytes") {
67
+ const parsed = Number.parseInt(next, 10);
68
+ result.maxLogBytes =
69
+ Number.isFinite(parsed) && parsed > 0
70
+ ? parsed
71
+ : DEFAULT_MAX_LOG_BYTES;
72
+ index += 1;
73
+ continue;
74
+ }
75
+ if (arg === "--log") {
76
+ result.logPath = next;
77
+ index += 1;
78
+ }
79
+ }
80
+ return result;
81
81
  }
82
82
 
83
83
  function readState(path) {
@@ -175,79 +175,79 @@ function isLoopbackHost(host) {
175
175
  unbracketed === "::1" ||
176
176
  unbracketed === "localhost"
177
177
  );
178
- }
179
-
180
- function truncateLogFdIfTooLarge(fd, maxBytes) {
181
- if (!Number.isFinite(maxBytes) || maxBytes <= 0) return;
182
- try {
183
- const stats = fstatSync(fd);
184
- if (!stats.isFile() || stats.size <= maxBytes) return;
185
- ftruncateSync(fd, 0);
186
- writeSync(
187
- fd,
188
- `codex-multi-auth app router log truncated after exceeding ${maxBytes} bytes\n`,
189
- );
190
- } catch {
191
- // stdout/stderr may be pipes or otherwise unavailable; logging must not fail startup.
192
- }
193
- }
194
-
195
- function truncateLogPathIfTooLarge(logPath, maxBytes) {
196
- if (!logPath || !Number.isFinite(maxBytes) || maxBytes <= 0) return false;
197
- try {
198
- const stats = statSync(logPath);
199
- if (!stats.isFile() || stats.size <= maxBytes) return false;
200
- truncateSync(logPath, 0);
201
- return true;
202
- } catch {
203
- // Log path may not exist yet or may be locked; fd-level checks can still work.
204
- return false;
205
- }
206
- }
207
-
208
- function writeLogTruncatedMarker(maxBytes) {
209
- try {
210
- writeSync(
211
- 1,
212
- `codex-multi-auth app router log truncated after exceeding ${maxBytes} bytes\n`,
213
- );
214
- } catch {
215
- // A closed stdout/stderr should not crash the router while enforcing log bounds.
216
- }
217
- }
218
-
219
- function installLogBounds(maxBytes, logPath) {
220
- if (truncateLogPathIfTooLarge(logPath, maxBytes)) {
221
- writeLogTruncatedMarker(maxBytes);
222
- }
223
- truncateLogFdIfTooLarge(1, maxBytes);
224
- truncateLogFdIfTooLarge(2, maxBytes);
225
- return setInterval(() => {
226
- if (truncateLogPathIfTooLarge(logPath, maxBytes)) {
227
- writeLogTruncatedMarker(maxBytes);
228
- }
229
- truncateLogFdIfTooLarge(1, maxBytes);
230
- truncateLogFdIfTooLarge(2, maxBytes);
231
- }, LOG_SIZE_CHECK_INTERVAL_MS);
232
- }
233
-
234
- async function main() {
235
- const args = parseArgs(process.argv.slice(2));
236
- installLogBounds(args.maxLogBytes, args.logPath).unref?.();
237
- const stateRecord = readState(args.statePath);
238
- if (args.statePath && stateRecord === null) {
239
- const error = new Error(
240
- "Codex app runtime router state is unreadable; refusing to bind an ephemeral port.",
241
- );
242
- writeStatus(
243
- args.statusPath,
244
- createStatusPayload({ state: "error", proxyServer: null, error, stateRecord: null }),
245
- );
246
- throw error;
247
- }
248
- const host =
249
- typeof stateRecord?.host === "string" && stateRecord.host.trim().length > 0
250
- ? stateRecord.host.trim()
178
+ }
179
+
180
+ function truncateLogFdIfTooLarge(fd, maxBytes) {
181
+ if (!Number.isFinite(maxBytes) || maxBytes <= 0) return;
182
+ try {
183
+ const stats = fstatSync(fd);
184
+ if (!stats.isFile() || stats.size <= maxBytes) return;
185
+ ftruncateSync(fd, 0);
186
+ writeSync(
187
+ fd,
188
+ `codex-multi-auth app router log truncated after exceeding ${maxBytes} bytes\n`,
189
+ );
190
+ } catch {
191
+ // stdout/stderr may be pipes or otherwise unavailable; logging must not fail startup.
192
+ }
193
+ }
194
+
195
+ function truncateLogPathIfTooLarge(logPath, maxBytes) {
196
+ if (!logPath || !Number.isFinite(maxBytes) || maxBytes <= 0) return false;
197
+ try {
198
+ const stats = statSync(logPath);
199
+ if (!stats.isFile() || stats.size <= maxBytes) return false;
200
+ truncateSync(logPath, 0);
201
+ return true;
202
+ } catch {
203
+ // Log path may not exist yet or may be locked; fd-level checks can still work.
204
+ return false;
205
+ }
206
+ }
207
+
208
+ function writeLogTruncatedMarker(maxBytes) {
209
+ try {
210
+ writeSync(
211
+ 1,
212
+ `codex-multi-auth app router log truncated after exceeding ${maxBytes} bytes\n`,
213
+ );
214
+ } catch {
215
+ // A closed stdout/stderr should not crash the router while enforcing log bounds.
216
+ }
217
+ }
218
+
219
+ function installLogBounds(maxBytes, logPath) {
220
+ if (truncateLogPathIfTooLarge(logPath, maxBytes)) {
221
+ writeLogTruncatedMarker(maxBytes);
222
+ }
223
+ truncateLogFdIfTooLarge(1, maxBytes);
224
+ truncateLogFdIfTooLarge(2, maxBytes);
225
+ return setInterval(() => {
226
+ if (truncateLogPathIfTooLarge(logPath, maxBytes)) {
227
+ writeLogTruncatedMarker(maxBytes);
228
+ }
229
+ truncateLogFdIfTooLarge(1, maxBytes);
230
+ truncateLogFdIfTooLarge(2, maxBytes);
231
+ }, LOG_SIZE_CHECK_INTERVAL_MS);
232
+ }
233
+
234
+ async function main() {
235
+ const args = parseArgs(process.argv.slice(2));
236
+ installLogBounds(args.maxLogBytes, args.logPath).unref?.();
237
+ const stateRecord = readState(args.statePath);
238
+ if (args.statePath && stateRecord === null) {
239
+ const error = new Error(
240
+ "Codex app runtime router state is unreadable; refusing to bind an ephemeral port.",
241
+ );
242
+ writeStatus(
243
+ args.statusPath,
244
+ createStatusPayload({ state: "error", proxyServer: null, error, stateRecord: null }),
245
+ );
246
+ throw error;
247
+ }
248
+ const host =
249
+ typeof stateRecord?.host === "string" && stateRecord.host.trim().length > 0
250
+ ? stateRecord.host.trim()
251
251
  : args.host;
252
252
  const statePort = parsePort(stateRecord?.port);
253
253
  const port = Number.isFinite(statePort) ? statePort : args.port;