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K,GACrB,KAAK,OAAO,KAAK,CAAE,OAAQ,MAAO,QAAS,GAC7C,CAEA,YAAc,KACd,MAAM,EAAU,KAAK,OAAO,UACtB,EAAO,EAAU,EAAQ,KAAO,UACtC,QAAQ,IAAI,OAAO,UAAY,KAAK,gBAAkB,MAAM,GAAQ,MAAM,SAAS,EAGnF,QAAU,KACR,QAAQ,IAAI,OAAO,UAAY,KAAK,mBAAmB,EAGzD,GAAA,CAAI,EAAM,GACR,KAAK,OAAO,KAAK,CAAE,OAAQ,MAAO,OAAM,WAC1C,CAEA,IAAA,CAAK,EAAM,GACT,KAAK,OAAO,KAAK,CAAE,OAAQ,OAAQ,OAAM,WAC3C,CAEA,OAAO,EAAM,GACX,KAAK,OAAO,KAAK,CAAE,OAAQ,SAAU,OAAM,WAC7C,CAEA,KAAA,CAAM,EAAM,GACV,KAAK,OAAO,KAAK,CAAE,OAAQ,QAAS,OAAM,WAC5C,CAEA,GAAA,CAAI,EAAM,GACR,KAAK,OAAO,KAAK,CAAE,OAAQ,MAAO,OAAM,WAC1C,CAEA,KAAA,CAAM,GACJ,KAAK,OAAO,MAAM,EACpB,SAGO,OAAQ,SAAU","file":"/npm/tryn@1.0.2/src/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/tryn@1.0.2/src/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import http from 'http'; \nimport url from 'url';\nimport { blue, yellow, white, bold, green } from 'colorette';\nimport util from 'util';\nimport Prettier from './plugins/esm/prettier.js'\nimport Logger from './plugins/esm/logger.js' \n\nconst routes = [];\n\nclass Server {\n  constructor(options) {\n    const { port, prettier } = options || {};\n    const pref = yellow(\"[TRYN] \");\n    this.routes = routes;\n    this.prettier = prettier;\n    this.middleware = [];\n    this.server = http.createServer(this.app);\n    this.server.on('listening', this.onListening);\n    this.server.on('close', this.onClose);\n    this.server.listen(port ? port : 80);\n    console.log(pref + blue(\"Web app started!\"));\n    console.log(pref + white(\"Preview: \") + bold(white(`http://localhost:${port ? port : 80}`)));\n  }\n\n  app = (req, res) => {\n    const parsedUrl = url.parse(req.url, true);\n    const { pathname } = parsedUrl;\n    const method = req.method.toLowerCase();\n\n    // Apply middleware\n    this.applyMiddleware(req, res);\n\n    // Find matching route\n    const route = this.routes.find(\n      r => r.path === pathname && r.method === method\n    );\n\n    if (route) {\n      res.json = (data) => {\n        res.setHeader('Content-Type', 'application/json');\n        if (this.prettier) {\n          res.end(this.prettier.pretty(data));\n        } else {\n          res.end(JSON.stringify(data));\n        }\n      };\n      req.close = (data) => {\n        this.server.close(() => {\n            return;\n        })\n      };\n      res.write = (data) => {\n        res.write(data);\n      };\n      route.handler(req, res);\n    } else {\n      res.statusCode = 404;\n      res.end('Not Found');\n    }\n  }\n\n  applyMiddleware(req, res) {\n    for (const middleware of this.middleware) {\n      middleware(req, res);\n    }\n  }\n\n  use(middleware) {\n    this.middleware.push(middleware);\n    this.routes.push({ method: 'use', handler: middleware });\n  }\n\n  onListening = () => {\n  const address = this.server.address();\n  const port = address ? address.port : 'unknown';\n  console.log(yellow(\"[TRYN]\") + blue(\" Running on \") + green(port) + white(\" port\"));\n  }\n\n  onClose = () => {\n    console.log(yellow(\"[TRYN]\") + blue(\" Server closed.\"));\n  }\n\n  get(path, handler) {\n    this.routes.push({ method: 'get', path, handler });\n  }\n\n  post(path, handler) {\n    this.routes.push({ method: 'post', path, handler });\n  }\n\n  delete(path, handler) {\n    this.routes.push({ method: 'delete', path, handler });\n  }\n\n  patch(path, handler) {\n    this.routes.push({ method: 'patch', path, handler });\n  }\n\n  put(path, handler) {\n    this.routes.push({ method: 'put', path, handler });\n  }\n\n  close(callback) {\n    this.server.close(callback);\n  }\n}\n\nexport { Server, Prettier, Logger };"]}