config.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. export const USAGE = "Usage: codex-app-server-bridge app-server";
  2. export class ConfigurationError extends Error {
  3. constructor(message) {
  4. super(message);
  5. this.name = "ConfigurationError";
  6. }
  7. }
  8. export function validateInvocation(arguments_) {
  9. if (arguments_.length !== 1 || arguments_[0] !== "app-server") {
  10. throw new ConfigurationError(USAGE);
  11. }
  12. }
  13. export function parseEndpoint(value) {
  14. if (value === undefined || value.length === 0) {
  15. throw new ConfigurationError("CODEX_APP_SERVER_URL is required");
  16. }
  17. const match = /^ws:\/\/(127\.0\.0\.1|\[::1\]):([0-9]+)\/?$/.exec(value);
  18. if (match === null) {
  19. throw new ConfigurationError("CODEX_APP_SERVER_URL must be ws://127.0.0.1:<port> or ws://[::1]:<port>");
  20. }
  21. const host = match[1];
  22. const portText = match[2];
  23. if (host === undefined || portText === undefined) {
  24. throw new ConfigurationError("CODEX_APP_SERVER_URL is invalid");
  25. }
  26. const port = Number(portText);
  27. if (!Number.isSafeInteger(port) || port < 1 || port > 65_535) {
  28. throw new ConfigurationError("CODEX_APP_SERVER_URL must contain a valid explicit port");
  29. }
  30. const display = `ws://${host}:${port}`;
  31. return { url: display, display };
  32. }