import { timingSafeEqual } from "node:crypto";
import { logOptions } from "../../../otel/src/log-options.js";
import { flagToEnv, parseOptions } from "../../../shared/src/options.js";
import { literalUnion } from "../../../shared/src/valita.js";
import packageJson from "../../../zero/package.json.js";
import { runtimeDebugFlags } from "../../../zql/src/builder/debug-delegate.js";
import { singleProcessMode } from "../types/processes.js";
import { ALLOWED_APP_ID_CHARACTERS, INVALID_APP_ID_MESSAGE } from "../types/shards.js";
import { DEFAULT_PREFERRED_PREFIXES } from "./network.js";
import { assertNormalized, isDevelopmentMode } from "./normalize.js";
import { array, string, number, boolean } from "@badrap/valita";
const ZERO_ENV_VAR_PREFIX = "ZERO_";
const appOptions = {
  id: {
    type: string().default("zero").assert((id) => ALLOWED_APP_ID_CHARACTERS.test(id), INVALID_APP_ID_MESSAGE),
    desc: [
      "Unique identifier for the app.",
      "",
      "Multiple zero-cache apps can run on a single upstream database, each of which",
      "is isolated from the others, with its own permissions, sharding (future feature),",
      "and change/cvr databases.",
      "",
      "The metadata of an app is stored in an upstream schema with the same name,",
      'e.g. "zero", and the metadata for each app shard, e.g. client and mutation',
      'ids, is stored in the "\\{app-id\\}_\\{#\\}" schema. (Currently there is only a single',
      '"0" shard, but this will change with sharding).',
      "",
      'The CVR and Change data are managed in schemas named "\\{app-id\\}_\\{shard-num\\}/cvr"',
      'and "\\{app-id\\}_\\{shard-num\\}/cdc", respectively, allowing multiple apps and shards',
      'to share the same database instance (e.g. a Postgres "cluster") for CVR and Change management.',
      "",
      "Due to constraints on replication slot names, an App ID may only consist of",
      "lower-case letters, numbers, and the underscore character.",
      "",
      "Note that this option is used by both {bold zero-cache} and {bold zero-deploy-permissions}."
    ]
  },
  publications: {
    type: array(string()).optional(() => []),
    desc: [
      `Postgres {bold PUBLICATION}s that define the tables and columns to`,
      `replicate. Publication names may not begin with an underscore,`,
      `as zero reserves that prefix for internal use.`,
      ``,
      `If unspecified, zero-cache will create and use an internal publication that`,
      `publishes all tables in the {bold public} schema, i.e.:`,
      ``,
      `CREATE PUBLICATION _\\{app-id\\}_public_0 FOR TABLES IN SCHEMA public;`,
      ``,
      `Note that changing the set of publications will result in resyncing the replica,`,
      `which may involve downtime (replication lag) while the new replica is initializing.`,
      `To change the set of publications without disrupting an existing app, a new app`,
      `should be created.`
    ]
  }
};
const shardOptions = {
  id: {
    type: string().assert(() => {
      throw new Error(
        `ZERO_SHARD_ID is no longer an option. Please use ZERO_APP_ID instead.`
        // TODO: Link to release / migration notes?
      );
    }).optional(),
    hidden: true
  },
  num: {
    type: number().default(0),
    desc: [
      `The shard number (from 0 to NUM_SHARDS) of the App. zero will eventually`,
      `support data sharding as a first-class primitive; until then, deploying`,
      `multiple shard-nums creates functionally identical shards. Until sharding is`,
      `actually meaningful, this flag is hidden but available for testing.`
    ],
    hidden: true
  }
};
const replicaOptions = {
  file: {
    type: string().default("zero.db"),
    desc: [
      `File path to the SQLite replica that zero-cache maintains.`,
      `This can be lost, but if it is, zero-cache will have to re-replicate next`,
      `time it starts up.`
    ]
  },
  vacuumIntervalHours: {
    type: number().optional(),
    desc: [
      `Performs a VACUUM at server startup if the specified number of hours has elapsed`,
      `since the last VACUUM (or initial-sync). The VACUUM operation is heavyweight`,
      `and requires double the size of the db in disk space. If unspecified, VACUUM`,
      `operations are not performed.`
    ]
  }
};
const perUserMutationLimit = {
  max: {
    type: number().optional(),
    desc: [
      `The maximum mutations per user within the specified {bold windowMs}.`,
      `If unset, no rate limiting is enforced.`
    ]
  },
  windowMs: {
    type: number().default(6e4),
    desc: [
      `The sliding window over which the {bold perUserMutationLimitMax} is enforced.`
    ]
  }
};
const authOptions = {
  jwk: {
    type: string().optional(),
    desc: [
      `A public key in JWK format used to verify JWTs. Only one of {bold jwk}, {bold jwksUrl} and {bold secret} may be set.`
    ],
    deprecated: [
      `Use cookie-based authentication or an auth token instead - see https://zero.rocicorp.dev/docs/auth.`
    ]
  },
  jwksUrl: {
    type: string().optional(),
    desc: [
      `A URL that returns a JWK set used to verify JWTs. Only one of {bold jwk}, {bold jwksUrl} and {bold secret} may be set.`
    ],
    deprecated: [
      `Use cookie-based authentication or an auth token instead - see https://zero.rocicorp.dev/docs/auth.`
    ]
  },
  secret: {
    type: string().optional(),
    desc: [
      `A symmetric key used to verify JWTs. Only one of {bold jwk}, {bold jwksUrl} and {bold secret} may be set.`
    ],
    deprecated: [
      `Use cookie-based authentication or an auth token instead - see https://zero.rocicorp.dev/docs/auth.`
    ]
  },
  issuer: {
    type: string().optional(),
    desc: [
      `Expected issuer ({bold iss} claim) for JWT validation.`,
      `If set, tokens with a different or missing issuer will be rejected.`
    ],
    deprecated: [
      `Use cookie-based authentication or an auth token instead - see https://zero.rocicorp.dev/docs/auth.`
    ]
  },
  audience: {
    type: string().optional(),
    desc: [
      `Expected audience ({bold aud} claim) for JWT validation.`,
      `If set, tokens with a different or missing audience will be rejected.`
    ],
    deprecated: [
      `Use cookie-based authentication or an auth token instead - see https://zero.rocicorp.dev/docs/auth.`
    ]
  }
};
const makeDeprecationMessage = (flag) => `Use {bold ${flagToEnv(ZERO_ENV_VAR_PREFIX, flag)}} (or {bold --${flag}}) instead.`;
const makeMutatorQueryOptions = (replacement, suffix) => ({
  url: {
    type: array(string()).optional(),
    // optional until we remove CRUD mutations
    desc: [
      `The URL of the API server to which zero-cache will ${suffix}.`,
      ``,
      `{bold IMPORTANT:} URLs are matched using {bold URLPattern}, a standard Web API.`,
      ``,
      `{bold Pattern Syntax:}`,
      `  URLPattern uses a simple and intuitive syntax similar to Express routes.`,
      `  Wildcards and named parameters make it easy to match multiple URLs.`,
      ``,
      `{bold Basic Examples:}`,
      `  Exact URL match:`,
      `    "https://api.example.com/mutate"`,
      `  `,
      `  Any subdomain using wildcard:`,
      `    "https://*.example.com/mutate"`,
      `  `,
      `  Multiple subdomain levels:`,
      `    "https://*.*.example.com/mutate"`,
      `  `,
      `  Any path under a domain:`,
      `    "https://api.example.com/*"`,
      `  `,
      `  Named path parameters:`,
      `    "https://api.example.com/:version/mutate"`,
      `    ↳ Matches "https://api.example.com/v1/mutate", "https://api.example.com/v2/mutate", etc.`,
      ``,
      `{bold Advanced Patterns:}`,
      `  Optional path segments:`,
      `    "https://api.example.com/:path?"`,
      `  `,
      `  Regex in segments (for specific patterns):`,
      `    "https://api.example.com/:version(v\\\\d+)/mutate"`,
      `    ↳ Matches only "v" followed by digits`,
      ``,
      `{bold Multiple patterns:}`,
      `  ["https://api1.example.com/mutate", "https://api2.example.com/mutate"]`,
      ``,
      `{bold Note:} Query parameters and URL fragments (#) are automatically ignored during matching.`,
      ``,
      `For full URLPattern syntax, see: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern`
    ],
    ...replacement ? { deprecated: [makeDeprecationMessage(`${replacement}-url`)] } : {}
  },
  apiKey: {
    type: string().optional(),
    desc: [
      `An optional secret used to authorize zero-cache to call the API server handling writes.`
    ],
    ...replacement ? { deprecated: [makeDeprecationMessage(`${replacement}-api-key`)] } : {}
  },
  forwardCookies: {
    type: boolean().default(false),
    desc: [
      `If true, zero-cache will forward cookies from the request.`,
      `This is useful for passing authentication cookies to the API server.`,
      `If false, cookies are not forwarded.`
    ],
    ...replacement ? { deprecated: [makeDeprecationMessage(`${replacement}-forward-cookies`)] } : {}
  },
  allowedClientHeaders: {
    type: array(string()).optional(),
    desc: [
      `A list of header names that clients are allowed to set via custom headers.`,
      `If specified, only headers in this list will be forwarded to the ${suffix === "push mutations" ? "push" : "query"} URL.`,
      `Header names are case-insensitive.`,
      `If not specified, no client-provided headers are forwarded (secure by default).`,
      `Example: ZERO_${replacement ? replacement.toUpperCase() : suffix === "push mutations" ? "MUTATE" : "QUERY"}_ALLOWED_CLIENT_HEADERS=x-request-id,x-correlation-id`
    ],
    ...replacement ? {
      deprecated: [
        makeDeprecationMessage(`${replacement}-allowed-client-headers`)
      ]
    } : {}
  }
});
const mutateOptions = makeMutatorQueryOptions(void 0, "push mutations");
const pushOptions = makeMutatorQueryOptions("mutate", "push mutations");
const queryOptions = makeMutatorQueryOptions(void 0, "send synced queries");
const getQueriesOptions = makeMutatorQueryOptions(
  "query",
  "send synced queries"
);
const zeroOptions = {
  upstream: {
    db: {
      type: string(),
      desc: [
        `The "upstream" authoritative postgres database.`,
        `In the future we will support other types of upstream besides PG.`
      ]
    },
    type: {
      type: literalUnion("pg", "custom").default("pg"),
      desc: [
        `The meaning of the {bold upstream-db} depends on the upstream type:`,
        `* {bold pg}: The connection database string, e.g. "postgres://..."`,
        `* {bold custom}: The base URI of the change source "endpoint, e.g.`,
        `          "https://my-change-source.dev/changes/v0/stream?apiKey=..."`
      ],
      hidden: true
      // TODO: Unhide when ready to officially support.
    },
    maxConns: {
      type: number().default(20),
      desc: [
        `The maximum number of connections to open to the upstream database`,
        `for committing mutations. This is divided evenly amongst sync workers.`,
        `In addition to this number, zero-cache uses one connection for the`,
        `replication stream.`,
        ``,
        `Note that this number must allow for at least one connection per`,
        `sync worker, or zero-cache will fail to start. See {bold num-sync-workers}`
      ]
    },
    maxConnsPerWorker: {
      type: number().optional(),
      hidden: true
      // Passed from main thread to sync workers
    }
  },
  /** @deprecated */
  push: pushOptions,
  mutate: mutateOptions,
  /** @deprecated */
  getQueries: getQueriesOptions,
  query: queryOptions,
  cvr: {
    db: {
      type: string().optional(),
      desc: [
        `The Postgres database used to store CVRs. CVRs (client view records) keep track`,
        `of the data synced to clients in order to determine the diff to send on reconnect.`,
        `If unspecified, the {bold upstream-db} will be used.`
      ]
    },
    maxConns: {
      type: number().default(30),
      desc: [
        `The maximum number of connections to open to the CVR database.`,
        `This is divided evenly amongst sync workers.`,
        ``,
        `Note that this number must allow for at least one connection per`,
        `sync worker, or zero-cache will fail to start. See {bold num-sync-workers}`
      ]
    },
    maxConnsPerWorker: {
      type: number().optional(),
      hidden: true
      // Passed from main thread to sync workers
    },
    garbageCollectionInactivityThresholdHours: {
      type: number().default(48),
      desc: [
        `The duration after which an inactive CVR is eligible for garbage collection.`,
        `Note that garbage collection is an incremental, periodic process which does not`,
        `necessarily purge all eligible CVRs immediately.`
      ]
    },
    garbageCollectionInitialIntervalSeconds: {
      type: number().default(60),
      desc: [
        `The initial interval at which to check and garbage collect inactive CVRs.`,
        `This interval is increased exponentially (up to 16 minutes) when there is`,
        `nothing to purge.`
      ]
    },
    garbageCollectionInitialBatchSize: {
      type: number().default(25),
      desc: [
        `The initial number of CVRs to purge per garbage collection interval.`,
        `This number is increased linearly if the rate of new CVRs exceeds the rate of`,
        `purged CVRs, in order to reach a steady state.`,
        ``,
        `Setting this to 0 effectively disables CVR garbage collection.`
      ]
    }
  },
  queryHydrationStats: {
    type: boolean().optional(),
    desc: [
      `Track and log the number of rows considered by query hydrations which`,
      `take longer than {bold log-slow-hydrate-threshold} milliseconds.`,
      `This is useful for debugging and performance tuning.`
    ]
  },
  enableQueryPlanner: {
    type: boolean().default(true),
    desc: [
      `Enable the query planner for optimizing ZQL queries.`,
      ``,
      `The query planner analyzes and optimizes query execution by determining`,
      `the most efficient join strategies.`,
      ``,
      `You can disable the planner if it is picking bad strategies.`
    ]
  },
  yieldThresholdMs: {
    type: number().default(10),
    desc: [
      `The maximum amount of time in milliseconds that a sync worker will`,
      `spend in IVM (processing query hydration and advancement) before yielding`,
      `to the event loop. Lower values increase responsiveness and fairness at`,
      `the cost of reduced throughput.`
    ]
  },
  change: {
    db: {
      type: string().optional(),
      desc: [
        `The Postgres database used to store recent replication log entries, in order`,
        `to sync multiple view-syncers without requiring multiple replication slots on`,
        `the upstream database. If unspecified, the {bold upstream-db} will be used.`
      ]
    },
    maxConns: {
      type: number().default(5),
      desc: [
        `The maximum number of connections to open to the change database.`,
        `This is used by the {bold change-streamer} for catching up`,
        `{bold zero-cache} replication subscriptions.`
      ]
    }
  },
  replica: replicaOptions,
  log: logOptions,
  app: appOptions,
  shard: shardOptions,
  /** @deprecated */
  auth: authOptions,
  port: {
    type: number().default(4848),
    desc: [`The port for sync connections.`]
  },
  changeStreamer: {
    uri: {
      type: string().optional(),
      desc: [
        `When set, connects to the {bold change-streamer} at the given URI.`,
        `In a multi-node setup, this should be specified in {bold view-syncer} options,`,
        `pointing to the {bold replication-manager} URI, which runs a {bold change-streamer}`,
        `on port 4849.`
      ]
    },
    mode: {
      type: literalUnion("dedicated", "discover").default("dedicated"),
      desc: [
        `As an alternative to {bold ZERO_CHANGE_STREAMER_URI}, the {bold ZERO_CHANGE_STREAMER_MODE}`,
        `can be set to "{bold discover}" to instruct the {bold view-syncer} to connect to the `,
        `ip address registered by the {bold replication-manager} upon startup.`,
        ``,
        `This may not work in all networking configurations, e.g. certain private `,
        `networking or port forwarding configurations. Using the {bold ZERO_CHANGE_STREAMER_URI}`,
        `with an explicit routable hostname is recommended instead.`,
        ``,
        `Note: This option is ignored if the {bold ZERO_CHANGE_STREAMER_URI} is set.`
      ]
    },
    port: {
      type: number().optional(),
      desc: [
        `The port on which the {bold change-streamer} runs. This is an internal`,
        `protocol between the {bold replication-manager} and {bold view-syncers}, which`,
        `runs in the same process tree in local development or a single-node configuration.`,
        ``,
        `If unspecified, defaults to {bold --port} + 1.`
      ]
    },
    /** @deprecated */
    address: {
      type: string().optional(),
      deprecated: [
        `Set the {bold ZERO_CHANGE_STREAMER_URI} on view-syncers instead.`
      ],
      hidden: true
    },
    /** @deprecated */
    protocol: {
      type: literalUnion("ws", "wss").default("ws"),
      deprecated: [
        `Set the {bold ZERO_CHANGE_STREAMER_URI} on view-syncers instead.`
      ],
      hidden: true
    },
    discoveryInterfacePreferences: {
      type: array(string()).default([...DEFAULT_PREFERRED_PREFIXES]),
      desc: [
        `The name prefixes to prefer when introspecting the network interfaces to determine`,
        `the externally reachable IP address for change-streamer discovery. This defaults`,
        `to commonly used names for standard ethernet interfaces in order to prevent selecting`,
        `special interfaces such as those for VPNs.`
      ],
      // More confusing than it's worth to advertise this. The default list should be
      // adjusted to make things work for all environments; it is controlled as a
      // hidden flag as an emergency to unblock people with outlier network configs.
      hidden: true
    },
    startupDelayMs: {
      type: number().default(15e3),
      desc: [
        `The delay to wait before the change-streamer takes over the replication stream`,
        `(i.e. the handoff during replication-manager updates), to allow loadbalancers to register`,
        `the task as healthy based on healthcheck parameters. Note that if a change stream request`,
        `is received during this interval, the delay will be canceled and the takeover will happen`,
        `immediately, since the incoming request indicates that the task is registered as a target.`
      ]
    },
    backPressureLimitHeapProportion: {
      type: number().default(0.04),
      desc: [
        `The percentage of {bold --max-old-space-size} to use as a buffer for absorbing replication`,
        `stream spikes. When the estimated amount of queued data exceeds this threshold, back pressure`,
        `is applied to the replication stream, delaying downstream sync as a result.`,
        ``,
        `The threshold was determined empirically with load testing. Higher thresholds have resulted`,
        `in OOMs. Note also that the byte-counting logic in the queue is strictly an underestimate of`,
        `actual memory usage (but importantly, proportionally correct), so the queue is actually`,
        `using more than what this proportion suggests.`,
        ``,
        `This parameter is exported as an emergency knob to reduce the size of the buffer in the`,
        `event that the server OOMs from back pressure. Resist the urge to {italic increase} this`,
        `proportion, as it is mainly useful for absorbing periodic spikes and does not meaningfully`,
        `affect steady-state replication throughput; the latter is determined by other factors such`,
        `as object serialization and PG throughput`,
        ``,
        `In other words, the back pressure limit does not constrain replication throughput;`,
        `rather, it protects the system when the upstream throughput exceeds the downstream`,
        `throughput.`
      ]
    }
  },
  taskID: {
    type: string().optional(),
    desc: [
      `Globally unique identifier for the zero-cache instance.`,
      ``,
      `Setting this to a platform specific task identifier can be useful for debugging.`,
      `If unspecified, zero-cache will attempt to extract the TaskARN if run from within`,
      `an AWS ECS container, and otherwise use a random string.`
    ]
  },
  perUserMutationLimit,
  numSyncWorkers: {
    type: number().optional(),
    desc: [
      `The number of processes to use for view syncing.`,
      `Leave this unset to use the maximum available parallelism.`,
      `If set to 0, the server runs without sync workers, which is the`,
      `configuration for running the {bold replication-manager}.`
    ]
  },
  autoReset: {
    type: boolean().default(true),
    desc: [
      `Automatically wipe and resync the replica when replication is halted.`,
      `This situation can occur for configurations in which the upstream database`,
      `provider prohibits event trigger creation, preventing the zero-cache from`,
      `being able to correctly replicate schema changes. For such configurations,`,
      `an upstream schema change will instead result in halting replication with an`,
      `error indicating that the replica needs to be reset.`,
      ``,
      `When {bold auto-reset} is enabled, zero-cache will respond to such situations`,
      `by shutting down, and when restarted, resetting the replica and all synced `,
      `clients. This is a heavy-weight operation and can result in user-visible`,
      `slowness or downtime if compute resources are scarce.`
    ]
  },
  adminPassword: {
    type: string().optional(),
    desc: [
      `A password used to administer zero-cache server, for example to access the`,
      `/statz endpoint.`,
      "",
      "A password is optional in development mode but {bold required in production} mode."
    ]
  },
  websocketCompression: {
    type: boolean().default(false),
    desc: [
      "Enable WebSocket per-message deflate compression.",
      "",
      "Compression can reduce bandwidth usage for sync traffic but",
      "increases CPU usage on both client and server. Disabled by default.",
      "",
      "See: https://github.com/websockets/ws#websocket-compression"
    ]
  },
  websocketCompressionOptions: {
    type: string().optional(),
    desc: [
      "JSON string containing WebSocket compression options.",
      "",
      "Only used if websocketCompression is enabled.",
      "",
      'Example: \\{"zlibDeflateOptions":\\{"level":3\\},"threshold":1024\\}',
      "",
      "See https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback for available options."
    ]
  },
  websocketMaxPayloadBytes: {
    type: number().default(10 * 1024 * 1024),
    desc: [
      "Maximum size of incoming WebSocket messages in bytes.",
      "",
      "Messages exceeding this limit are rejected before parsing.",
      "Default: 10MB (10 * 1024 * 1024 = 10485760)"
    ]
  },
  litestream: {
    executable: {
      type: string().optional(),
      desc: [`Path to the {bold litestream} executable.`]
    },
    configPath: {
      type: string().default("./src/services/litestream/config.yml"),
      desc: [
        `Path to the litestream yaml config file. zero-cache will run this with its`,
        `environment variables, which can be referenced in the file via $\\{ENV\\}`,
        `substitution, for example:`,
        `* {bold ZERO_REPLICA_FILE} for the db path`,
        `* {bold ZERO_LITESTREAM_BACKUP_LOCATION} for the db replica url`,
        `* {bold ZERO_LITESTREAM_LOG_LEVEL} for the log level`,
        `* {bold ZERO_LOG_FORMAT} for the log type`
      ]
    },
    logLevel: {
      type: literalUnion("debug", "info", "warn", "error").default("warn")
    },
    backupURL: {
      type: string().optional(),
      desc: [
        `The location of the litestream backup, usually an {bold s3://} URL.`,
        `This is only consulted by the {bold replication-manager}.`,
        `{bold view-syncers} receive this information from the {bold replication-manager}.`
      ]
    },
    endpoint: {
      type: string().optional(),
      desc: [
        `The S3-compatible endpoint URL to use for the litestream backup. Only required for non-AWS services.`,
        `The {bold replication-manager} and {bold view-syncers} must have the same endpoint.`
      ]
    },
    port: {
      type: number().optional(),
      desc: [
        `Port on which litestream exports metrics, used to determine the replication`,
        `watermark up to which it is safe to purge change log records.`,
        ``,
        `If unspecified, defaults to {bold --port} + 2.`
      ]
    },
    checkpointThresholdMB: {
      type: number().default(40),
      desc: [
        `The size of the WAL file at which to perform an SQlite checkpoint to apply`,
        `the writes in the WAL to the main database file. Each checkpoint creates`,
        `a new WAL segment file that will be backed up by litestream. Smaller thresholds`,
        `may improve read performance, at the expense of creating more files to download`,
        `when restoring the replica from the backup.`
      ]
    },
    minCheckpointPageCount: {
      type: number().optional(),
      desc: [
        `The WAL page count at which SQLite attempts a PASSIVE checkpoint, which`,
        `transfers pages to the main database file without blocking writers.`,
        `Defaults to {bold checkpointThresholdMB * 250} (since SQLite page size is 4KB).`
      ]
    },
    maxCheckpointPageCount: {
      type: number().optional(),
      desc: [
        `The WAL page count at which SQLite performs a RESTART checkpoint, which`,
        `blocks writers until complete. Defaults to {bold minCheckpointPageCount * 10}.`,
        `Set to {bold 0} to disable RESTART checkpoints entirely.`
      ]
    },
    incrementalBackupIntervalMinutes: {
      type: number().default(15),
      desc: [
        `The interval between incremental backups of the replica. Shorter intervals`,
        `reduce the amount of change history that needs to be replayed when catching`,
        `up a new view-syncer, at the expense of increasing the number of files needed`,
        `to download for the initial litestream restore.`
      ]
    },
    snapshotBackupIntervalHours: {
      type: number().default(12),
      desc: [
        `The interval between snapshot backups of the replica. Snapshot backups`,
        `make a full copy of the database to a new litestream generation. This`,
        `improves restore time at the expense of bandwidth. Applications with a`,
        `large database and low write rate can increase this interval to reduce`,
        `network usage for backups (litestream defaults to 24 hours).`
      ]
    },
    restoreParallelism: {
      type: number().default(48),
      desc: [
        `The number of WAL files to download in parallel when performing the`,
        `initial restore of the replica from the backup.`
      ]
    },
    multipartConcurrency: {
      type: number().default(48),
      desc: [
        `The number of parts (of size {bold --litestream-multipart-size} bytes)`,
        `to upload or download in parallel when backing up or restoring the snapshot.`
      ]
    },
    multipartSize: {
      type: number().default(16 * 1024 * 1024),
      desc: [
        `The size of each part when uploading or downloading the snapshot with`,
        `{bold --multipart-concurrency}. Note that up to {bold concurrency * size}`,
        `bytes of memory are used when backing up or restoring the snapshot.`
      ]
    }
  },
  storageDBTmpDir: {
    type: string().optional(),
    desc: [
      `tmp directory for IVM operator storage. Leave unset to use os.tmpdir()`
    ]
  },
  initialSync: {
    tableCopyWorkers: {
      type: number().default(5),
      desc: [
        `The number of parallel workers used to copy tables during initial sync.`,
        `Each worker uses a database connection and will buffer up to (approximately)`,
        `10 MB of table data in memory during initial sync. Increasing the number of`,
        `workers may improve initial sync speed; however, note that local disk throughput`,
        `(i.e. IOPS), upstream CPU, and network bandwidth may also be bottlenecks.`
      ]
    },
    profileCopy: {
      type: boolean().optional(),
      hidden: true,
      desc: [
        `Takes a cpu profile during the copy phase initial-sync, storing it as a JSON file`,
        `initial-copy.cpuprofile in the tmp directory.`
      ]
    }
  },
  /** @deprecated */
  targetClientRowCount: {
    type: number().default(2e4),
    deprecated: [
      "This option is no longer used and will be removed in a future version.",
      "The client-side cache no longer enforces a row limit. Instead, TTL-based expiration",
      "automatically manages cache size to prevent unbounded growth."
    ],
    hidden: true
  },
  lazyStartup: {
    type: boolean().default(false),
    desc: [
      "Delay starting the majority of zero-cache until first request.",
      "",
      "This is mainly intended to avoid connecting to Postgres replication stream",
      "until the first request is received, which can be useful i.e., for preview instances.",
      "",
      "Currently only supported in single-node mode."
    ]
  },
  serverVersion: {
    type: string().optional(),
    desc: [`The version string outputted to logs when the server starts up.`]
  },
  enableTelemetry: {
    type: boolean().default(true),
    desc: [
      `Set to false to opt out of telemetry collection.`,
      ``,
      `This helps us improve Zero by collecting anonymous usage data.`,
      `Setting the DO_NOT_TRACK environment variable also disables telemetry.`
    ]
  },
  cloudEvent: {
    sinkEnv: {
      type: string().optional(),
      desc: [
        `ENV variable containing a URI to a CloudEvents sink. When set, ZeroEvents`,
        `will be published to the sink as the {bold data} field of CloudEvents.`,
        `The {bold source} field of the CloudEvents will be set to the {bold ZERO_TASK_ID},`,
        `along with any extension attributes specified by the {bold ZERO_CLOUD_EVENT_EXTENSION_OVERRIDES_ENV}.`,
        ``,
        `This configuration is modeled to easily integrate with a knative K_SINK binding,`,
        `(i.e. https://github.com/knative/eventing/blob/main/docs/spec/sources.md#sinkbinding).`,
        `However, any CloudEvents sink can be used.`
      ]
    },
    extensionOverridesEnv: {
      type: string().optional(),
      desc: [
        `ENV variable containing a JSON stringified object with an {bold extensions} field`,
        `containing attributes that should be added or overridden on outbound CloudEvents.`,
        ``,
        `This configuration is modeled to easily integrate with a knative K_CE_OVERRIDES binding,`,
        `(i.e. https://github.com/knative/eventing/blob/main/docs/spec/sources.md#sinkbinding).`
      ]
    }
  }
};
let loadedConfig;
function getZeroConfig(opts = {}) {
  if (!loadedConfig || singleProcessMode()) {
    loadedConfig = parseOptions(zeroOptions, {
      envNamePrefix: ZERO_ENV_VAR_PREFIX,
      emitDeprecationWarnings: false,
      // overridden at the top level parse
      ...opts
    });
    if (loadedConfig.queryHydrationStats) {
      runtimeDebugFlags.trackRowCountsVended = true;
    }
  }
  return loadedConfig;
}
function getNormalizedZeroConfig(opts = {}) {
  const config = getZeroConfig(opts);
  assertNormalized(config);
  return config;
}
function getServerVersion(config) {
  return config?.serverVersion ?? packageJson.version;
}
function isAdminPasswordValid(lc, config, password) {
  if (!password && !config.adminPassword && isDevelopmentMode()) {
    warnOnce(
      lc,
      "No admin password set; allowing access in development mode only"
    );
    return true;
  }
  if (!config.adminPassword) {
    lc.warn?.("No admin password set; denying access");
    return false;
  }
  const passwordBuffer = Buffer.from(password ?? "");
  const configBuffer = Buffer.from(config.adminPassword);
  if (passwordBuffer.length !== configBuffer.length) {
    timingSafeEqual(configBuffer, configBuffer);
    lc.warn?.("Invalid admin password");
    return false;
  }
  if (!timingSafeEqual(passwordBuffer, configBuffer)) {
    lc.warn?.("Invalid admin password");
    return false;
  }
  lc.debug?.("Admin password accepted");
  return true;
}
let hasWarned = false;
function warnOnce(lc, msg) {
  if (!hasWarned) {
    lc.warn?.(msg);
    hasWarned = true;
  }
}
export {
  ZERO_ENV_VAR_PREFIX,
  appOptions,
  getNormalizedZeroConfig,
  getServerVersion,
  getZeroConfig,
  isAdminPasswordValid,
  shardOptions,
  zeroOptions
};
//# sourceMappingURL=zero-config.js.map
