"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parse_exports = {};
__export(parse_exports, {
  isBuffer: () => isBuffer,
  isJsonPrimitive: () => isJsonPrimitive,
  parseJson: () => parseJson,
  parseJsonSafe: () => parseJsonSafe,
  prototypePollutionReviver: () => prototypePollutionReviver
});
module.exports = __toCommonJS(parse_exports);
var import_validate = require("../schema/validate");
var import_transform = require("../strings/transform");
var import_buffer = require("../primordials/buffer");
var import_error = require("../primordials/error");
var import_json = require("../primordials/json");
var import_map_set = require("../primordials/map-set");
// @__NO_SIDE_EFFECTS__
function isBuffer(x) {
  if (!x || typeof x !== "object") {
    return false;
  }
  const obj = x;
  if (typeof obj["length"] !== "number") {
    return false;
  }
  if (typeof obj["copy"] !== "function" || typeof obj["slice"] !== "function") {
    return false;
  }
  if (typeof obj["length"] === "number" && obj["length"] > 0 && typeof obj[0] !== "number") {
    return false;
  }
  const Ctor = x.constructor;
  return !!(typeof Ctor?.isBuffer === "function" && Ctor.isBuffer(x));
}
// @__NO_SIDE_EFFECTS__
function isJsonPrimitive(value) {
  return value === null || typeof value === "boolean" || typeof value === "number" || typeof value === "string";
}
// @__NO_SIDE_EFFECTS__
function parseJson(content, options) {
  const { filepath, reviver, throws } = {
    __proto__: null,
    ...options
  };
  const shouldThrow = throws === void 0 || !!throws;
  const jsonStr = /* @__PURE__ */ isBuffer(content) ? content.toString("utf8") : content;
  try {
    return (0, import_json.JSONParse)((0, import_transform.stripBom)(jsonStr), reviver);
  } catch (e) {
    if (shouldThrow) {
      const error = e;
      if (error && typeof filepath === "string") {
        error.message = `${filepath}: ${error.message}`;
      }
      throw error;
    }
  }
  return void 0;
}
const DANGEROUS_KEYS = new import_map_set.SetCtor(["__proto__", "constructor", "prototype"]);
const DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
// @__NO_SIDE_EFFECTS__
function parseJsonSafe(jsonString, schema, options = {}) {
  const { allowPrototype = false, maxSize = DEFAULT_MAX_SIZE } = options;
  const byteLength = (0, import_buffer.BufferByteLength)(jsonString, "utf8");
  if (byteLength > maxSize) {
    throw new import_error.ErrorCtor(
      `JSON string exceeds maximum size limit${maxSize !== DEFAULT_MAX_SIZE ? ` of ${maxSize} bytes` : ""}`
    );
  }
  let parsed;
  try {
    parsed = allowPrototype ? (0, import_json.JSONParse)(jsonString) : (0, import_json.JSONParse)(jsonString, prototypePollutionReviver);
  } catch (e) {
    throw new import_error.ErrorCtor(`Failed to parse JSON: ${e}`);
  }
  if (schema) {
    const result = (0, import_validate.validateSchema)(schema, parsed);
    if (!result.ok) {
      const summary = result.errors.map((e) => `${e.path.join(".") || "(root)"}: ${e.message}`).join(", ");
      throw new import_error.ErrorCtor(`Validation failed: ${summary}`);
    }
    return result.value;
  }
  return parsed;
}
function prototypePollutionReviver(key, value) {
  if (DANGEROUS_KEYS.has(key)) {
    throw new import_error.ErrorCtor(
      "JSON contains potentially malicious prototype pollution keys"
    );
  }
  return value;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isBuffer,
  isJsonPrimitive,
  parseJson,
  parseJsonSafe,
  prototypePollutionReviver
});
