function trough() {
	let e = [], n = {
		run: r,
		use: i
	};
	return n;
	function r(...n) {
		let r = -1, i = n.pop();
		if (typeof i != "function") throw TypeError("Expected function as last argument, not " + i);
		a(null, ...n);
		function a(o, ...s) {
			let c = e[++r], l = -1;
			if (o) {
				i(o);
				return;
			}
			for (; ++l < n.length;) (s[l] === null || s[l] === void 0) && (s[l] = n[l]);
			n = s, c ? wrap(c, a)(...s) : i(null, ...s);
		}
	}
	function i(t) {
		if (typeof t != "function") throw TypeError("Expected `middelware` to be a function, not " + t);
		return e.push(t), n;
	}
}
function wrap(e, t) {
	let n;
	return r;
	function r(...t) {
		let r = e.length > t.length, o;
		r && t.push(i);
		try {
			o = e.apply(this, t);
		} catch (e) {
			let t = e;
			if (r && n) throw t;
			return i(t);
		}
		r || (o && o.then && typeof o.then == "function" ? o.then(a, i) : o instanceof Error ? i(o) : a(o));
	}
	function i(e, ...r) {
		n || (n = !0, t(e, ...r));
	}
	function a(e) {
		i(null, e);
	}
}
export { trough };
