spooder 4.5.5 → 4.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +16 -4
  2. package/package.json +1 -1
  3. package/src/api.ts +18 -1
package/README.md CHANGED
@@ -35,9 +35,10 @@ The `CLI` component of `spooder` is a global command-line tool for running serve
35
35
  `spooder` exposes a simple yet powerful API for developing servers. The API is designed to be minimal to leave control in the hands of the developer and not add overhead for features you may not need.
36
36
 
37
37
  - [API > Serving](#api-serving)
38
- - [`serve(port: number): Server`](#api-serving-serve)
38
+ - [`serve(port: number, hostname?: string): Server`](#api-serving-serve)
39
39
  - [API > Routing](#api-routing)
40
40
  - [`server.route(path: string, handler: RequestHandler, method: HTTP_METHODS)`](#api-routing-server-route)
41
+ - [`server.unroute(path: string)`](#api-routing-server-unroute)
41
42
  - [HTTP Methods](#api-routing-methods)
42
43
  - [Redirection Routes](#api-routing-redirection-routes)
43
44
  - [Status Code Text](#api-routing-status-code-text)
@@ -467,14 +468,15 @@ In addition to the information provided by the developer, `spooder` also include
467
468
  ## API > Serving
468
469
 
469
470
  <a id="api-serving-serve"></a>
470
- ### `serve(port: number): Server`
471
+ ### `serve(port: number, hostname?: string): Server`
471
472
 
472
- Bootstrap a server on the specified port.
473
+ Bootstrap a server on the specified port (and optional hostname).
473
474
 
474
475
  ```ts
475
476
  import { serve } from 'spooder';
476
477
 
477
- const server = serve(8080);
478
+ const server = serve(8080); // port only
479
+ const server = serve(3000, '0.0.0.0'); // optional hostname
478
480
  ```
479
481
 
480
482
  By default, the server responds with:
@@ -501,6 +503,16 @@ server.route('/test/route', (req, url) => {
501
503
  });
502
504
  ```
503
505
 
506
+ <a id="api-routing-server-unrouote"></a>
507
+ ### 🔧 `server.unroute(path: string)`
508
+
509
+ Unregister a specific route.
510
+
511
+ ```ts
512
+ server.route('/test/route', () => {});
513
+ server.unroute('/test/route');
514
+ ```
515
+
504
516
  <a id="api-routing-methods"></a>
505
517
  ### HTTP Methods
506
518
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.5.5",
4
+ "version": "4.5.7",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -616,7 +616,7 @@ function is_valid_method(method: HTTP_METHODS, req: Request): boolean {
616
616
  return req.method === method;
617
617
  }
618
618
 
619
- export function serve(port: number) {
619
+ export function serve(port: number, hostname?: string) {
620
620
  const routes = new Array<[string[], RequestHandler, HTTP_METHODS]>();
621
621
  const handlers = new Map<number, StatusCodeHandler>();
622
622
 
@@ -744,6 +744,7 @@ export function serve(port: number) {
744
744
 
745
745
  const server = Bun.serve({
746
746
  port,
747
+ hostname,
747
748
  development: false,
748
749
 
749
750
  async fetch(req: Request): Promise<Response> {
@@ -804,6 +805,22 @@ export function serve(port: number) {
804
805
  routes.push([path.split('/'), handler, method]);
805
806
  },
806
807
 
808
+ /** Unregister a specific route */
809
+ unroute: (path: string): void => {
810
+ const path_parts = path.split('/');
811
+ routes.splice(routes.findIndex(([route_parts]) => {
812
+ if (route_parts.length !== path_parts.length)
813
+ return false;
814
+
815
+ for (let i = 0; i < route_parts.length; i++) {
816
+ if (route_parts[i] !== path_parts[i])
817
+ return false;
818
+ }
819
+
820
+ return true;
821
+ }, 1));
822
+ },
823
+
807
824
  /** Serve a directory for a specific route. */
808
825
  dir: (path: string, dir: string, handler?: DirHandler, method: HTTP_METHODS = 'GET'): void => {
809
826
  if (path.endsWith('/'))