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MAAM,EAAW,QAAU,YAAc,aACzC,EAAI,KAAO,EACN,QAAQ,cAAc,EAAI,cAAc,SAAS,OAAO,EAAI,UAC5D,QAAQ,aAAa,EAAI,cAAc,SAAS,EAAI,QACzD,EAAI,KAAO,MACf,CACA,SAAS,mBACL,GAAyB,iBAAd,UACP,OAAO,UAIX,MAAM,GAAa,EAAI,WAAW,eAAe,6CACjD,OAAO,YAAY,QAAQ,QAAQ,EACvC","file":"/npm/koa-onerror@5.0.1/dist/commonjs/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/koa-onerror@5.0.1/dist/commonjs/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","\"use strict\";\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n    return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.onerror = onerror;\nconst node_http_1 = __importDefault(require(\"node:http\"));\nconst node_path_1 = __importDefault(require(\"node:path\"));\nconst node_fs_1 = __importDefault(require(\"node:fs\"));\nconst node_url_1 = require(\"node:url\");\nconst node_util_1 = require(\"node:util\");\nconst escape_html_1 = __importDefault(require(\"escape-html\"));\nconst stream_wormhole_1 = require(\"stream-wormhole\");\nconst debug = (0, node_util_1.debuglog)('koa-onerror');\nconst defaultOptions = {\n    text,\n    json,\n    html,\n};\nfunction onerror(app, options) {\n    options = { ...defaultOptions, ...options };\n    app.context.onerror = function (err) {\n        debug('onerror: %s', err);\n        // don't do anything if there is no error.\n        // this allows you to pass `this.onerror`\n        // to node-style callbacks.\n        if (err == null) {\n            return;\n        }\n        // ignore all padding request stream\n        if (this.req) {\n            (0, stream_wormhole_1.sendToWormhole)(this.req);\n            debug('send the req to wormhole');\n        }\n        // wrap non-error object\n        if (!(err instanceof Error)) {\n            debug('err is not an instance of Error');\n            let errMsg = err;\n            if (typeof err === 'object') {\n                try {\n                    errMsg = JSON.stringify(err);\n                }\n                catch (e) {\n                    debug('stringify error: %s', e);\n                    errMsg = (0, node_util_1.format)('%s', e);\n                }\n            }\n            const newError = new Error('non-error thrown: ' + errMsg);\n            // err maybe an object, try to copy the name, message and stack to the new error instance\n            if (err) {\n                if (err.name)\n                    newError.name = err.name;\n                if (err.message)\n                    newError.message = err.message;\n                if (err.stack)\n                    newError.stack = err.stack;\n                if (err.status) {\n                    Reflect.set(newError, 'status', err.status);\n                }\n                if (err.headers) {\n                    Reflect.set(newError, 'headers', err.headers);\n                }\n            }\n            err = newError;\n            debug('wrap err: %s', err);\n        }\n        const headerSent = this.headerSent || !this.writable;\n        if (headerSent) {\n            debug('headerSent is true');\n            err.headerSent = true;\n        }\n        // delegate\n        this.app.emit('error', err, this);\n        // nothing we can do here other\n        // than delegate to the app-level\n        // handler and log.\n        if (headerSent)\n            return;\n        // ENOENT support\n        if (err.code === 'ENOENT') {\n            err.status = 404;\n        }\n        if (typeof err.status !== 'number' || !node_http_1.default.STATUS_CODES[err.status]) {\n            err.status = 500;\n        }\n        this.status = err.status;\n        this.set(err.headers);\n        let type = 'text';\n        if (options.accepts) {\n            type = options.accepts.call(this, 'html', 'text', 'json');\n        }\n        else {\n            type = this.accepts('html', 'text', 'json');\n        }\n        debug('accepts type: %s', type);\n        type = type || 'text';\n        if (options.all) {\n            options.all.call(this, err, this);\n        }\n        else {\n            if (options.redirect && type !== 'json') {\n                this.redirect(options.redirect);\n            }\n            else {\n                options[type].call(this, err, this);\n                this.type = type;\n            }\n        }\n        if (type === 'json') {\n            this.body = JSON.stringify(this.body);\n        }\n        debug('end the response, body: %s', this.body);\n        this.res.end(this.body);\n    };\n    return app;\n}\nconst devTemplate = node_fs_1.default.readFileSync(node_path_1.default.join(getSourceDirname(), 'templates/dev_error.html'), 'utf8');\nconst prodTemplate = node_fs_1.default.readFileSync(node_path_1.default.join(getSourceDirname(), 'templates/prod_error.html'), 'utf8');\nfunction isDev() {\n    return !process.env.NODE_ENV || process.env.NODE_ENV === 'development';\n}\n/**\n * default text error handler\n */\nfunction text(err, ctx) {\n    // unset all headers, and set those specified\n    ctx.res._headers = {};\n    ctx.set(err.headers);\n    ctx.body = (isDev() || err.expose) && err.message\n        ? err.message\n        : node_http_1.default.STATUS_CODES[ctx.status];\n}\n/**\n * default json error handler\n */\nfunction json(err, ctx) {\n    const message = (isDev() || err.expose) && err.message\n        ? err.message\n        : node_http_1.default.STATUS_CODES[ctx.status];\n    ctx.body = { error: message };\n}\n/**\n * default html error handler\n */\nfunction html(err, ctx) {\n    const template = isDev() ? devTemplate : prodTemplate;\n    ctx.body = template\n        .replace('{{status}}', (0, escape_html_1.default)(String(err.status)))\n        .replace('{{stack}}', (0, escape_html_1.default)(err.stack));\n    ctx.type = 'html';\n}\nfunction getSourceDirname() {\n    if (typeof __dirname === 'string') {\n        return __dirname;\n    }\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-ignore\n    const __filename = (0, node_url_1.fileURLToPath)('import_meta_url_placeholder_by_tshy_after');\n    return node_path_1.default.dirname(__filename);\n}\n"]}