🧁 An object-oriented Gemini server for Deno!
gemini-protocol deno typescript gemini
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(laurali): refer to callbacks as hooks

Fuwn a5e10156 3dc5aaa6

+32 -37
+1 -6
README.md
··· 35 35 You can then begin to implement your very own Laurali server. 36 36 37 37 ```ts 38 - import { 39 - Callback, 40 - callback, 41 - route, 42 - Server, 43 - } from "https://deno.land/x/laurali/mod.ts"; 38 + import { route, Server } from "https://deno.land/x/laurali/mod.ts"; 44 39 45 40 class MyCoolServer extends Server { 46 41 /** Visit `/hi` */
+4 -4
examples/my_cool_server.ts
··· 16 16 // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 17 // SPDX-License-Identifier: GPL-3.0-only 18 18 19 - import { callback, route, Server } from "../mod.ts"; 19 + import { hook, route, Server } from "../mod.ts"; 20 20 import * as optic from "https://deno.land/x/optic@1.3.5/mod.ts"; 21 21 22 22 /** Implement a new Laurali server */ ··· 50 50 return MyCoolServer.clicks; 51 51 } 52 52 53 - @callback() 53 + @hook() 54 54 override onPreRoute(ctx: Deno.TlsConn) { 55 55 MyCoolServer.clicks += 1; 56 56 ··· 60 60 ); 61 61 } 62 62 63 - @callback() 63 + @hook() 64 64 override onPostRoute() { 65 65 MyCoolServer.logger.info("Closed connection."); 66 66 } 67 67 68 - @callback() 68 + @hook() 69 69 override onError() { 70 70 return "hi"; 71 71 }
+2 -2
laurali/callbacks.ts laurali/hooks.ts
··· 16 16 // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 17 // SPDX-License-Identifier: GPL-3.0-only 18 18 19 - /** One of few callbacks of a `Server` */ 20 - export const enum Callback { 19 + /** One of few hooks of a `Server` */ 20 + export const enum Hook { 21 21 /** Called before a connection to a client has been responded to */ 22 22 ON_PRE_ROUTE = 0, 23 23 /** Called after a connection to a client has concluded */
+12 -12
laurali/decorators.ts
··· 16 16 // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 17 // SPDX-License-Identifier: GPL-3.0-only 18 18 19 - import { Callback } from "./callbacks.ts"; 19 + import { Hook } from "./hooks.ts"; 20 20 21 21 /** 22 22 * Mark the function as a route and register it to the `Server`. ··· 36 36 }; 37 37 38 38 /** 39 - * Mark the function as a callback and register it to the `Server`. 40 - * @param callback The type of callback which the function will be called for. 39 + * Mark the function as a hook and register it to the `Server`. 40 + * @param hook The type of hook which the function will be called for. 41 41 */ 42 - export const callback = (callback?: Callback) => { 42 + export const hook = (hook?: Hook) => { 43 43 return ( 44 44 // deno-lint-ignore no-explicit-any 45 45 target: any, ··· 48 48 ) => { 49 49 let type; 50 50 51 - if (callback) { 52 - type = callback; 51 + if (hook) { 52 + type = hook; 53 53 } else { 54 54 switch (key) { 55 55 case "onPreRoute": 56 56 { 57 - type = Callback.ON_PRE_ROUTE; 57 + type = Hook.ON_PRE_ROUTE; 58 58 } 59 59 break; 60 60 case "onPostRoute": 61 61 { 62 - type = Callback.ON_POST_ROUTE; 62 + type = Hook.ON_POST_ROUTE; 63 63 } 64 64 break; 65 65 case "onError": 66 66 { 67 - type = Callback.ON_ERROR; 67 + type = Hook.ON_ERROR; 68 68 } 69 69 break; 70 70 default: { 71 71 throw new Error( 72 - `Unknown callback type: '${key.toString()}'. Did you forget to ` + 73 - "specify the callback type?`", 72 + `Unknown hook type: '${key.toString()}'. Did you forget to ` + 73 + "specify the hook type?`", 74 74 ); 75 75 } 76 76 } 77 77 } 78 78 79 - target.addCallback(type, descriptor.value); 79 + target.addHook(type, descriptor.value); 80 80 81 81 return descriptor; 82 82 };
+1 -1
laurali/mod.ts
··· 17 17 // SPDX-License-Identifier: GPL-3.0-only 18 18 19 19 export * from "./decorators.ts"; 20 - export * from "./callbacks.ts"; 20 + export * from "./hooks.ts"; 21 21 export * from "./server.ts";
+12 -12
laurali/server.ts
··· 16 16 // Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17 17 // SPDX-License-Identifier: GPL-3.0-only 18 18 19 - import { Callback } from "./callbacks.ts"; 19 + import { Hook } from "./hooks.ts"; 20 20 21 21 export interface ServerConfiguration { 22 22 port?: number; ··· 34 34 /** All registered route functions of the `Server` */ 35 35 // deno-lint-ignore no-explicit-any 36 36 static #routes: Map<string, (ctx: Deno.TlsConn) => any> = new Map(); 37 - /** All registered callback functions of the `Server` */ 38 - static #callbacks: Map<Callback, (ctx: Deno.TlsConn) => void> = new Map(); 37 + /** All registered hook functions of the `Server` */ 38 + static #hooks: Map<Hook, (ctx: Deno.TlsConn) => void> = new Map(); 39 39 /** The port of the `Server` */ 40 40 static #port: number; 41 41 /** The hostname of the `Server` */ ··· 65 65 addRoute(route: string, handler: () => any) { 66 66 Server.#routes.set(route, handler); 67 67 } 68 - /** Add a callback function to the `Server` */ 69 - addCallback(callback: Callback, handler: () => void) { 70 - Server.#callbacks.set(callback, handler); 68 + /** Add a hook function to the `Server` */ 69 + addHook(hook: Hook, handler: () => void) { 70 + Server.#hooks.set(hook, handler); 71 71 } 72 72 73 73 /** Get the `port` of the `Server` */ ··· 98 98 99 99 /** Start listening and responding to client connections */ 100 100 async listen() { 101 - // If the `Server` has an `onListen` callback, call it. 101 + // If the `Server` has an `onListen` hook, call it. 102 102 if (this.onListen) this.onListen(); 103 103 104 104 // Listen for connections and handle them. ··· 115 115 continue; 116 116 } 117 117 118 - const onPreRoute = Server.#callbacks.get(Callback.ON_PRE_ROUTE); 119 - const onPostRoute = Server.#callbacks.get(Callback.ON_POST_ROUTE); 120 - const onError = Server.#callbacks.get(Callback.ON_ERROR); 118 + const onPreRoute = Server.#hooks.get(Hook.ON_PRE_ROUTE); 119 + const onPostRoute = Server.#hooks.get(Hook.ON_POST_ROUTE); 120 + const onError = Server.#hooks.get(Hook.ON_ERROR); 121 121 122 122 // Make sure that the client has sent a request. 123 123 if (n === null) { ··· 132 132 "", 133 133 ).replace(/gemini:\/\//, ""); 134 134 135 - // If the `Server` has an `onPreRoute` callback, call it. 135 + // If the `Server` has an `onPreRoute` hook, call it. 136 136 if (onPreRoute) onPreRoute(r); 137 137 138 138 // Respond to index requests. ··· 178 178 r.close(); 179 179 } 180 180 181 - // If the `Server` has an `onPostRoute` callback, call it. 181 + // If the `Server` has an `onPostRoute` hook, call it. 182 182 if (onPostRoute) onPostRoute(r); 183 183 184 184 continue;