UNPKG

abi.js

Version:

[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]

129 lines (128 loc) 6.11 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Application_instances, _Application_config, _Application_router, _Application_server, _Application_send; import 'buno.js'; import { defaultConfig, defineConfig, } from './config.js'; import { Context } from './context.js'; import { DELETE, GET, HEAD, PATCH, POST, PUT } from './method.js'; import { Router } from './router.js'; import { joinPath } from './runtime.js'; import { sendFile } from './send.js'; import { Server } from './server.js'; export class Application { constructor(config = defaultConfig) { _Application_instances.add(this); _Application_config.set(this, void 0); _Application_router.set(this, void 0); _Application_server.set(this, void 0); this.fetch = this.fetch.bind(this); __classPrivateFieldSet(this, _Application_config, defineConfig(config), "f"); __classPrivateFieldSet(this, _Application_router, new Router(), "f"); __classPrivateFieldSet(this, _Application_server, new Server(__classPrivateFieldGet(this, _Application_config, "f").root, __classPrivateFieldGet(this, _Application_config, "f").assets), "f"); } context(request) { return new Context(request); } use(handler, ...handlers) { __classPrivateFieldGet(this, _Application_server, "f").pipe(handler, ...handlers); return this; } get(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(GET, pattern, resolver, ...resolvers); return this; } head(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(HEAD, pattern, resolver, ...resolvers); return this; } post(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(POST, pattern, resolver, ...resolvers); return this; } put(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(PUT, pattern, resolver, ...resolvers); return this; } patch(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(PATCH, pattern, resolver, ...resolvers); return this; } delete(pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(DELETE, pattern, resolver, ...resolvers); return this; } on(method, pattern, resolver, ...resolvers) { __classPrivateFieldGet(this, _Application_router, "f").add(method, pattern, resolver, ...resolvers); return this; } serve(path, base) { return this.use((request) => { const context = this.context(request); const urlPathname = context.pathname; if (base && !urlPathname.startsWith(base)) { return context.abort(400); } const filename = joinPath(__classPrivateFieldGet(this, _Application_config, "f").root, path); return __classPrivateFieldGet(this, _Application_instances, "m", _Application_send).call(this, filename, request); }); } handle(root, base) { return this.use((request) => { const context = this.context(request); const urlPathname = context.pathname; if (base && !urlPathname.startsWith(base)) { return context.abort(400); } const filename = base ? urlPathname.replace(base, root) : joinPath(root, urlPathname); return __classPrivateFieldGet(this, _Application_instances, "m", _Application_send).call(this, filename, request); }); } routes() { return (request) => { const context = this.context(request); const pathname = context.pathname; const route = __classPrivateFieldGet(this, _Application_router, "f").get(context.method, pathname); if (route) { context.log(`Handle ${context.method} ${pathname}`); return route.resolve(request); } return context.abort(404, `Route ${context.method} ${pathname} not found.`); }; } useRoutes() { this.use(this.routes()); return this; } run() { this.start(); } start() { this.useRoutes(); __classPrivateFieldGet(this, _Application_server, "f").start(); } fetch(request) { this.useRoutes(); return __classPrivateFieldGet(this, _Application_server, "f").fetch(request); } listen(arg1, arg2) { this.useRoutes(); __classPrivateFieldGet(this, _Application_server, "f").listen(arg1, arg2); } } _Application_config = new WeakMap(), _Application_router = new WeakMap(), _Application_server = new WeakMap(), _Application_instances = new WeakSet(), _Application_send = function _Application_send(filename, request) { return sendFile(filename, request); }; export function app(config) { return new Application(config); }