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EAAG,MACnC,CACA,MAAO,GACH,EAAK,aAAa,MAAQ,EAAI,MAAM,GAAG,IAAK,CAAE,MAAO,IAAM,EAAS,GACxE,CACA,OAAI,aAAc,SACP,EACY,iBAAP,EACL,IAAI,SAAS,KAAM,CAAE,OAAQ,IAC/B,aAAc,MACZ,SAAS,KAAK,CACjB,KAAM,EAAG,KACT,QAAS,EAAG,QACZ,MAAO,EAAG,MACV,MAAO,EAAG,OACX,CAAE,OAAQ,GAAU,MAGhB,IAAI,SAAS,EAAI,CAAE,OAAQ,GAAU,KAAM,GAGlE,CAEA,KAAA,CAAM,EAAI,GACN,MAAO,CAAE,EAAQ,GAAQ,gBAAgB,KAAK,IAAO,GACrD,IAAK,KAAK,KAAK,GACX,MAAM,MAAM,kBAAkB,KAAK,UAAU,MACjD,IAAI,EAAO,KAAK,KAChB,IAAK,MAAO,EAAM,KAAS,EAAK,SAAS,oBAChC,EAEI,IAAS,EAAK,MAAM,KACzB,EAAO,EAAK,KAAK,KAEjB,EAAK,KAAO,CAAE,OAAM,KAAM,EAAO,IAAI,MAJrC,EAAO,EAAK,KAAK,mBAAmB,MAAW,IAAI,KAM3D,IAAK,MAAM,KAAK,EAAO,MAAM,KACzB,EAAK,KAAK,GAAK,EACnB,OAAO,IACX,EAEJ,QAAQ,OAAS","file":"/npm/@libn/router@0.5.0/script/router/mod.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@libn/router@0.5.0/script/router/mod.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.Router = void 0;\nrequire(\"../_dnt.polyfills.js\");\nconst PATH = /^(?:\\/(?:[A-Za-z\\d-_~!$&'()*+,;=:@]|%[\\dA-Fa-f]{2})+|\\/#(?!__proto__)[^/#]+)+$|^\\/$/;\nclass Node {\n    constructor() {\n        Object.defineProperty(this, \"self\", {\n            enumerable: true,\n            configurable: true,\n            writable: true,\n            value: Object.create(null)\n        });\n        Object.defineProperty(this, \"next\", {\n            enumerable: true,\n            configurable: true,\n            writable: true,\n            value: void 0\n        });\n        Object.defineProperty(this, \"rest\", {\n            enumerable: true,\n            configurable: true,\n            writable: true,\n            value: Object.create(null)\n        });\n    }\n}\n/** HTTP router. */\nclass Router {\n    constructor() {\n        Object.defineProperty(this, \"tree\", {\n            enumerable: true,\n            configurable: true,\n            writable: true,\n            value: new Node()\n        });\n        /** Handles a request (as an unbound arrow function). */\n        Object.defineProperty(this, \"fetch\", {\n            enumerable: true,\n            configurable: true,\n            writable: true,\n            value: async (request, ...rest) => {\n                const on = { url: new URL(request.url), path: {}, request };\n                let node = this.tree, to, status;\n                for (let part of on.url.pathname.match(/(?<=\\/)[^/]+/g) ?? []) {\n                    if (node.rest[part = decodeURIComponent(part)])\n                        node = node.rest[part];\n                    else if (node.next)\n                        on.path[node.next.name] = part, node = node.next.node;\n                    else\n                        return new Response(null, { status: 404 });\n                }\n                const handler = node.self[request.method];\n                if (!handler)\n                    return new Response(null, { status: 405 });\n                try {\n                    to = await handler(on, ...rest);\n                    if (Object.hasOwn(to ?? {}, \"error\"))\n                        status = to.error, to = to.value;\n                }\n                catch ($) {\n                    to = $ instanceof Error ? $ : Error(`${$}`, { cause: $ }), status = 500;\n                }\n                if (to instanceof Response)\n                    return to;\n                else if (typeof to === \"number\")\n                    return new Response(null, { status: to });\n                else if (to instanceof Error) {\n                    return Response.json({\n                        name: to.name,\n                        message: to.message,\n                        cause: to.cause,\n                        stack: to.stack,\n                    }, { status: status ?? 400 });\n                }\n                else\n                    return new Response(to, { status: status ?? 200 });\n            }\n        });\n    }\n    /** Adds a route. */\n    route(on, handler) {\n        const [, method, path] = /^(\\S+) (\\S+)$/.exec(on) ?? [];\n        if (!PATH.test(path))\n            throw Error(`Invalid route: ${JSON.stringify(on)}`);\n        let node = this.tree;\n        for (const [part, name] of path.matchAll(/#([^/#]+)|[^/]+/g)) {\n            if (!name)\n                node = node.rest[decodeURIComponent(part)] ??= new Node();\n            else if (name === node.next?.name)\n                node = node.next.node;\n            else\n                node.next = { name, node: node = new Node() };\n        }\n        for (const $ of method.split(\",\"))\n            node.self[$] = handler;\n        return this;\n    }\n}\nexports.Router = Router;\n"]}