import {
  OAuthStateError
} from "./chunk-BPARRK6F.js";

// runtime/subsystems/auth/backends/state-store.memory-backend.ts
import { randomBytes } from "crypto";
var MemoryOAuthStateStore = class {
  store = /* @__PURE__ */ new Map();
  ttlMs;
  now;
  generateToken;
  constructor(opts = {}) {
    this.ttlMs = opts.ttlMs ?? 10 * 60 * 1e3;
    this.now = opts.now ?? (() => Date.now());
    this.generateToken = opts.generateToken ?? (() => randomBytes(32).toString("base64url"));
  }
  async generate(record) {
    const state = this.generateToken();
    this.store.set(state, {
      record: { ...record },
      expiresAt: this.now() + this.ttlMs
    });
    return state;
  }
  async consume(state) {
    const slot = this.store.get(state);
    if (!slot) {
      throw new OAuthStateError(
        `OAuth state token unknown or already consumed`,
        "missing"
      );
    }
    this.store.delete(state);
    if (slot.expiresAt <= this.now()) {
      throw new OAuthStateError(`OAuth state token expired`, "expired");
    }
    return slot.record;
  }
};

export {
  MemoryOAuthStateStore
};
//# sourceMappingURL=chunk-QLTJSCE6.js.map