spooder 4.5.6 → 4.5.8

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 +15 -4
  2. package/package.json +1 -1
  3. package/src/api.ts +22 -2
package/README.md CHANGED
@@ -35,7 +35,7 @@ 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
41
  - [`server.unroute(path: string)`](#api-routing-server-unroute)
@@ -468,14 +468,15 @@ In addition to the information provided by the developer, `spooder` also include
468
468
  ## API > Serving
469
469
 
470
470
  <a id="api-serving-serve"></a>
471
- ### `serve(port: number): Server`
471
+ ### `serve(port: number, hostname?: string): Server`
472
472
 
473
- Bootstrap a server on the specified port.
473
+ Bootstrap a server on the specified port (and optional hostname).
474
474
 
475
475
  ```ts
476
476
  import { serve } from 'spooder';
477
477
 
478
- const server = serve(8080);
478
+ const server = serve(8080); // port only
479
+ const server = serve(3000, '0.0.0.0'); // optional hostname
479
480
  ```
480
481
 
481
482
  By default, the server responds with:
@@ -1214,6 +1215,16 @@ await parse_template('Hello {$world}', replacer);
1214
1215
  </html>
1215
1216
  ```
1216
1217
 
1218
+ `parse_template` supports optional scopes with the following syntax.
1219
+
1220
+ ```html
1221
+ {$if:foo}I love {$foo}{/if}
1222
+ ```
1223
+ Contents contained inside an `if` block will be rendered providing the given value, in this case `foo` is truthy in the substitution table.
1224
+
1225
+ An `if` block is only removed if `drop_missing` is `true`, allowing them to persist through multiple passes of a template.
1226
+
1227
+
1217
1228
  `parse_template` supports looping arrays with the following syntax.
1218
1229
 
1219
1230
  ```html
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.5.6",
4
+ "version": "4.5.8",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -173,6 +173,25 @@ export async function parse_template(template: string, replacements: Replacement
173
173
  }
174
174
  i += loop_content.length + 6;
175
175
  }
176
+ } else if (buffer.startsWith('if:')) {
177
+ const if_key = buffer.substring(3);
178
+ const if_content_start_index = i + 1;
179
+ const if_close_index = template.indexOf('{/if}', if_content_start_index);
180
+
181
+ if (if_close_index === -1) {
182
+ if (!drop_missing)
183
+ result += '{$' + buffer + '}';
184
+ } else {
185
+ const if_content = template.substring(if_content_start_index, if_close_index);
186
+ const condition_value = is_replacer_fn ? await replacements(if_key) : replacements[if_key];
187
+
188
+ if (!drop_missing) {
189
+ result += '{$' + buffer + '}' + if_content + '{/if}';
190
+ } else if (condition_value) {
191
+ result += await parse_template(if_content, replacements, drop_missing);
192
+ }
193
+ i += if_content.length + 5;
194
+ }
176
195
  } else {
177
196
  const replacement = is_replacer_fn ? await replacements(buffer) : replacements[buffer];
178
197
  if (replacement !== undefined)
@@ -616,7 +635,7 @@ function is_valid_method(method: HTTP_METHODS, req: Request): boolean {
616
635
  return req.method === method;
617
636
  }
618
637
 
619
- export function serve(port: number) {
638
+ export function serve(port: number, hostname?: string) {
620
639
  const routes = new Array<[string[], RequestHandler, HTTP_METHODS]>();
621
640
  const handlers = new Map<number, StatusCodeHandler>();
622
641
 
@@ -744,6 +763,7 @@ export function serve(port: number) {
744
763
 
745
764
  const server = Bun.serve({
746
765
  port,
766
+ hostname,
747
767
  development: false,
748
768
 
749
769
  async fetch(req: Request): Promise<Response> {
@@ -796,7 +816,7 @@ export function serve(port: number) {
796
816
  }
797
817
  });
798
818
 
799
- log('server started on port {%d}', port);
819
+ log('server started on port {%d} (host: {%s})', port, hostname ?? 'unspecified');
800
820
 
801
821
  return {
802
822
  /** Register a handler for a specific route. */