var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/index.ts
var src_exports = {};
__export(src_exports, {
  default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_http = require("http");
var import_url = __toESM(require("url"));
var startRuntimeServer = ({ functions, api }) => {
  const listener = async (req, res) => {
    if (req.method === "POST") {
      const parts = import_url.default.parse(req.url);
      const { pathname } = parts;
      const normalisedPathname = pathname.replace(/\//, "");
      const buffers = [];
      for await (const chunk of req) {
        buffers.push(chunk);
      }
      const data = Buffer.concat(buffers).toString();
      const json = JSON.parse(data);
      const { call } = functions[normalisedPathname];
      try {
        const result = await call(json, api);
        if (!result) {
          res.write(JSON.stringify({
            errors: [
              {
                message: `No value returned from ${normalisedPathname}`
              }
            ]
          }));
          return;
        }
        res.write(JSON.stringify(result));
      } catch (e) {
        console.error(e);
        res.statusCode = 500;
        if (e instanceof Error) {
          const { message } = e;
          res.write(JSON.stringify({
            errors: [
              {
                message
              }
            ]
          }));
        } else {
          res.write(JSON.stringify({
            errors: [
              {
                message: "An unknown error occurred"
              }
            ]
          }));
        }
      }
    } else {
      res.statusCode = 400;
      res.write(JSON.stringify({
        errors: [
          {
            message: "Only POST requests are permitted"
          }
        ]
      }));
    }
    res.end();
  };
  const server = (0, import_http.createServer)(listener);
  const port = process.env.PORT && parseInt(process.env.PORT, 10) || 3001;
  server.listen(port);
};
var src_default = startRuntimeServer;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
