@darthcav/ts-http-server 0.2.0 → 0.3.0

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @darthcav/ts-http-server
2
2
 
3
- ![Node][node-version] ![Version][version-image] ![CI][ci-badge] ![Coverage][coverage-badge]
3
+ [![Node][node-version]][node-url] ![Version][version-image] ![CI][ci-badge] [![Coverage][coverage-badge]][coverage-url]
4
4
 
5
5
  A TypeScript wrapper HTTP server for Node.js >= 25 based upon [Fastify](https://fastify.dev/).
6
6
 
@@ -112,7 +112,9 @@ public/ # Documentation output (generated)
112
112
  [Apache-2.0](LICENSE)
113
113
 
114
114
  [node-version]: https://img.shields.io/badge/node-%3E%3D25-orange.svg?style=flat-square
115
- [version-image]: https://img.shields.io/badge/version-0.2.0-blue.svg?style=flat-square
115
+ [node-url]: https://nodejs.org
116
+ [version-image]: https://img.shields.io/badge/version-0.3.0-blue.svg?style=flat-square
116
117
  [ci-badge]: https://github.com/darthcav/ts-http-server/actions/workflows/tests.yml/badge.svg
117
- [coverage-badge]: https://img.shields.io/badge/coverage-check%20CI-yellow.svg?style=flat-square
118
+ [coverage-badge]: https://codecov.io/github/darthcav/ts-http-server/branch/dev/graph/badge.svgK8Q4T4N9SG
119
+ [coverage-url]: https://codecov.io/github/darthcav/ts-http-server
118
120
  [pages-url]: https://darthcav.github.io/ts-http-server/
package/dist/index.d.ts CHANGED
@@ -11,5 +11,5 @@ import onResponse from "./hooks/onResponse.ts";
11
11
  import preHandler from "./hooks/preHandler.ts";
12
12
  import launcher from "./launcher.ts";
13
13
  export type { FSTPlugin, LauncherLocals, LauncherOptions, } from "./types.ts";
14
- export { defaultErrorHandler, defaultFastifyOptions, defaultPlugins, defaultRoutes, launcher, preHandler, onResponse, };
14
+ export { defaultErrorHandler, defaultFastifyOptions, defaultPlugins, defaultRoutes, launcher, onResponse, preHandler, };
15
15
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -10,5 +10,5 @@ import defaultRoutes from "./defaults/defaultRoutes.js";
10
10
  import onResponse from "./hooks/onResponse.js";
11
11
  import preHandler from "./hooks/preHandler.js";
12
12
  import launcher from "./launcher.js";
13
- export { defaultErrorHandler, defaultFastifyOptions, defaultPlugins, defaultRoutes, launcher, preHandler, onResponse, };
13
+ export { defaultErrorHandler, defaultFastifyOptions, defaultPlugins, defaultRoutes, launcher, onResponse, preHandler, };
14
14
  //# sourceMappingURL=index.js.map
package/dist/start.js CHANGED
@@ -3,7 +3,7 @@ import { getConsoleLogger, main } from "@darthcav/ts-utils";
3
3
  import pkg from "../package.json" with { type: "json" };
4
4
  import { defaultPlugins, defaultRoutes, launcher } from "./index.js";
5
5
  const logger = await getConsoleLogger(pkg.name, "info");
6
- main(pkg.name, logger, false, () => {
6
+ main(pkg.name, logger, () => {
7
7
  const locals = { pkg };
8
8
  const plugins = defaultPlugins({ locals });
9
9
  const routes = defaultRoutes();
package/dist/start.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"start.js","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,GAAG,MAAM,iBAAiB,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAEpE,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AAEvD,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE;IAC/B,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAA;IACtB,MAAM,OAAO,GAAG,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;IAC1C,MAAM,MAAM,GAAG,aAAa,EAAE,CAAA;IAE9B,MAAM,OAAO,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAA;IAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAU,EAAE,CAAC;QAClD,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,CAChC,OAAO;aACF,KAAK,EAAE;aACP,IAAI,CAAC,GAAG,EAAE;YACP,MAAM,CAAC,KAAK,CACR,2DAA2D,MAAM,EAAE,CACtE,CAAA;YACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACb,MAAM,CAAC,KAAK,CAAC,0BAA0B,KAAK,EAAE,CAAC,CAAA;YAC/C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC,CAAC,CACT,CAAA;IACL,CAAC;AACL,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"start.js","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAC3D,OAAO,GAAG,MAAM,iBAAiB,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAEpE,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AAEvD,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE;IACxB,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,CAAA;IACtB,MAAM,OAAO,GAAG,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;IAC1C,MAAM,MAAM,GAAG,aAAa,EAAE,CAAA;IAE9B,MAAM,OAAO,GAAG,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAA;IAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAU,EAAE,CAAC;QAClD,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,CAChC,OAAO;aACF,KAAK,EAAE;aACP,IAAI,CAAC,GAAG,EAAE;YACP,MAAM,CAAC,KAAK,CACR,2DAA2D,MAAM,EAAE,CACtE,CAAA;YACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACb,MAAM,CAAC,KAAK,CAAC,0BAA0B,KAAK,EAAE,CAAC,CAAA;YAC/C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACnB,CAAC,CAAC,CACT,CAAA;IACL,CAAC;AACL,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@darthcav/ts-http-server",
3
3
  "description": "A TypeScript HTTP server for Node.js >= 25",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "author": {
6
- "name": "darthcav"
6
+ "name": "darthcav",
7
+ "url": "https://github.com/darthcav"
7
8
  },
8
9
  "license": "Apache-2.0",
9
10
  "type": "module",
@@ -17,18 +18,13 @@
17
18
  }
18
19
  },
19
20
  "files": [
20
- "package.json",
21
- "README.md",
22
- "LICENSE",
23
- "src/**/*.ts",
24
- "src/**/*.tsx",
25
- "dist/**/*.js",
26
- "dist/**/*.js.map",
27
- "dist/**/*.d.ts",
28
- "dist/**/*.d.ts.map"
21
+ "src",
22
+ "!src/**/*.test.ts",
23
+ "!src/__tests__",
24
+ "dist"
29
25
  ],
30
26
  "dependencies": {
31
- "@darthcav/ts-utils": "0.8.0",
27
+ "@darthcav/ts-utils": "0.8.2",
32
28
  "@fastify/accepts": "5.0.4",
33
29
  "@fastify/compress": "8.3.1",
34
30
  "@fastify/cors": "11.2.0",
@@ -43,9 +39,9 @@
43
39
  "fastify": "5.8.2"
44
40
  },
45
41
  "devDependencies": {
46
- "@biomejs/biome": "2.4.6",
42
+ "@biomejs/biome": "2.4.8",
47
43
  "@types/ejs": "3.1.5",
48
- "@types/node": "25.4.0",
44
+ "@types/node": "25.5.0",
49
45
  "asserttt": "1.0.1",
50
46
  "shx": "0.4.0",
51
47
  "typedoc": "0.28.17"
@@ -64,15 +60,16 @@
64
60
  "prepare": "npm run build",
65
61
  "\n===== Typecheck =====": "",
66
62
  "typecheck": "tsc --noEmit",
63
+ "\n===== Start =====": "",
64
+ "start": "node --env-file=.env.local src/start.ts",
67
65
  "\n===== Test =====": "",
68
66
  "test": "node --env-file=.env.local --test --test-reporter=spec 'src/**/*.test.ts'",
69
67
  "test:coverage": "node --env-file=.env.local --experimental-test-coverage --test 'src/**/*.test.ts'",
68
+ "test:coverage:lcov": "shx mkdir -p coverage && node --env-file=.env.local --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout --test 'src/**/*.test.ts'",
70
69
  "\n===== Linter =====": "",
71
70
  "lint": "biome check .",
72
71
  "lint:fix": "biome check --write .",
73
72
  "\n===== TypeDoc =====": "",
74
- "doc": "shx rm -rf public/* && typedoc --tsconfig tsconfig.json",
75
- "\n===== Start =====": "",
76
- "start": "node --env-file=.env.local src/start.ts"
73
+ "doc": "shx rm -rf public/* && typedoc --tsconfig tsconfig.json"
77
74
  }
78
75
  }
package/src/index.ts CHANGED
@@ -23,6 +23,6 @@ export {
23
23
  defaultPlugins,
24
24
  defaultRoutes,
25
25
  launcher,
26
- preHandler,
27
26
  onResponse,
27
+ preHandler,
28
28
  }
@@ -0,0 +1,11 @@
1
+ /* ===== import extra fonts ===== */
2
+ @import url("https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i");
3
+
4
+ /* ===== custom styles ===== */
5
+ body {
6
+ padding-top: 6rem;
7
+ font-style: normal;
8
+ --bs-body-font-family: Roboto, sans-serif;
9
+ --bs-body-font-weight: 300;
10
+ --bs-body-font-size: 1.25rem;
11
+ }
Binary file
package/src/start.ts CHANGED
@@ -5,7 +5,7 @@ import { defaultPlugins, defaultRoutes, launcher } from "./index.ts"
5
5
 
6
6
  const logger = await getConsoleLogger(pkg.name, "info")
7
7
 
8
- main(pkg.name, logger, false, () => {
8
+ main(pkg.name, logger, () => {
9
9
  const locals = { pkg }
10
10
  const plugins = defaultPlugins({ locals })
11
11
  const routes = defaultRoutes()
@@ -0,0 +1,30 @@
1
+ <%- include ("./include/header.ejs") -%>
2
+
3
+ <table class="table table-striped">
4
+ <tr>
5
+ <th>
6
+ HTTP status
7
+ </th>
8
+ <td>
9
+ <%= status %>
10
+ </td>
11
+ </tr>
12
+ <tr>
13
+ <th>
14
+ Request path
15
+ </th>
16
+ <td>
17
+ <%= uri %>
18
+ </td>
19
+ </tr>
20
+ <tr>
21
+ <th>
22
+ Message
23
+ </th>
24
+ <td>
25
+ <%= message %>
26
+ </td>
27
+ </tr>
28
+ </table>
29
+
30
+ <%- include ("./include/footer.ejs") -%>
@@ -0,0 +1,12 @@
1
+ </main>
2
+ <footer class="container-fluid p-3 m-0 text-center text-light bg-secondary" role="contentinfo">
3
+ <address class="font-weight-bold">
4
+ Copyright &copy; <%= new Date().getFullYear() %> Darth CAV. All rights reserved.
5
+ </address>
6
+ </footer>
7
+ </div>
8
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
9
+ integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
10
+ crossorigin="anonymous"></script>
11
+ </body>
12
+ </html>
@@ -0,0 +1,40 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <title><%= pkg.name %> :: <%= header %></title>
5
+ <meta charset="utf-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1">
7
+ <link href="/images/favicon.ico" type="image/x-icon" rel="shortcut icon">
8
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css"
9
+ type="text/css" rel="stylesheet" crossorigin="anonymous"
10
+ integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB">
11
+ <link href="/css/main.css" type="text/css" rel="stylesheet">
12
+ </head>
13
+ <body class="d-flex h-100">
14
+ <% const menu = [ { name : "index", title : "Home" } ]; %>
15
+ <div class="container-fluid d-flex w-100 h-100 p-0 m-0 flex-column">
16
+ <nav class="navbar navbar-expand-lg navbar-dark bg-primary m-0 p-3 fixed-top">
17
+ <span class="navbar-brand px-4 d-block"><%= pkg.name %></span>
18
+ <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse"
19
+ aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
20
+ <span class="text-primary navbar-toggler-icon"></span>
21
+ </button>
22
+ <div class="collapse navbar-collapse justify-content-end" id="navbarCollapse">
23
+ <ul class="navbar-nav">
24
+ <% for (let i = 0; i < menu.length; i++) { %>
25
+ <% if (menu[i].name === menu_name) { %>
26
+ <li class="nav-item active">
27
+ <a class="nav-link" href="#"><%= menu[i].title %><span class="visually-hidden-focusable">(current)</span></a>
28
+ </li>
29
+ <% } else { %>
30
+ <li class="nav-item">
31
+ <a class="nav-link" href="/<%= menu[i].name %>.html"><%= menu[i].title %></a>
32
+ </li>
33
+ <% } } %>
34
+ </ul>
35
+ </div>
36
+ </nav>
37
+ <main id="main" class="container mb-5" role="main">
38
+ <header role="banner" class="border border-info border-3 rounded p-3 mb-3">
39
+ <h1 class="display-3 text-info"><%= header %><br><%= pkg.name %></h1>
40
+ </header>
@@ -0,0 +1,30 @@
1
+ <%- include ("./include/header.ejs") -%>
2
+
3
+ <p><%= pkg.description %></p>
4
+
5
+ <table class="table table-striped">
6
+ <tr>
7
+ <th>Software version</th>
8
+ <td><%= pkg.version %></td>
9
+ </tr>
10
+ <tr>
11
+ <th>Node binary</th>
12
+ <td><code><%= process.execPath %></code></td>
13
+ </tr>
14
+ <tr>
15
+ <th>Node version</th>
16
+ <td><code><%= process.version %></code></td>
17
+ </tr>
18
+ <tr>
19
+ <th>Working directory</th>
20
+ <td><code><%= process.cwd() %></code></td>
21
+ </tr>
22
+ <tr>
23
+ <th>Service process ID</th>
24
+ <td>
25
+ <code><%= process.pid %></code>
26
+ </td>
27
+ </tr>
28
+ </table>
29
+
30
+ <%- include ("./include/footer.ejs") -%>
@@ -1,25 +0,0 @@
1
- import { equal, ok } from "node:assert/strict"
2
- import { cwd } from "node:process"
3
- import { suite, test } from "node:test"
4
- import defaultPlugins from "../defaults/defaultPlugins.ts"
5
-
6
- suite("defaultPlugins", () => {
7
- const locals = {
8
- pkg: { name: "ts-http-server", version: "0.0.0", description: "Test" },
9
- }
10
-
11
- test("returns all plugins using default baseDir", () => {
12
- const plugins = defaultPlugins({ locals })
13
- equal(plugins.size, 7)
14
- ok(plugins.has("@fastify/accepts"))
15
- ok(plugins.has("@fastify/view"))
16
- ok(plugins.has("@fastify/static"))
17
- })
18
-
19
- test("returns all plugins using explicit baseDir", () => {
20
- const plugins = defaultPlugins({ locals, baseDir: cwd() })
21
- equal(plugins.size, 7)
22
- ok(plugins.has("@fastify/view"))
23
- ok(plugins.has("@fastify/static"))
24
- })
25
- })
@@ -1,204 +0,0 @@
1
- import { equal, match, ok } from "node:assert/strict"
2
- import { request as httpRequest } from "node:http"
3
- import { after, before, suite, test } from "node:test"
4
- import { setTimeout } from "node:timers/promises"
5
- import type { Logger } from "@logtape/logtape"
6
- import type { FastifyInstance, RouteOptions } from "fastify"
7
- import defaultPlugins from "../defaults/defaultPlugins.ts"
8
- import defaultRoutes from "../defaults/defaultRoutes.ts"
9
- import launcher from "../launcher.ts"
10
-
11
- // ---------------------------------------------------------------------------
12
- // HTTP server suite
13
- // ---------------------------------------------------------------------------
14
-
15
- suite("defaultRoutes [HTTP]", () => {
16
- // ---------------------------------------------------------------------------
17
- // Minimal test logger (no real I/O)
18
- // ---------------------------------------------------------------------------
19
- const noop = (): void => {}
20
- const testLogger = {
21
- category: ["test"],
22
- info: noop,
23
- error: noop,
24
- warn: noop,
25
- debug: noop,
26
- getChild: () => testLogger,
27
- } as unknown as Logger
28
-
29
- // ---------------------------------------------------------------------------
30
- // Error route for 500 testing
31
- // ---------------------------------------------------------------------------
32
- function errorRoute(): Map<string, RouteOptions> {
33
- const routes = new Map<string, RouteOptions>()
34
- routes.set("ERROR_ROUTE", {
35
- method: "GET",
36
- url: "/error",
37
- handler: async () => {
38
- throw new Error("test server error")
39
- },
40
- })
41
- return routes
42
- }
43
-
44
- // ---------------------------------------------------------------------------
45
- // Full plugin + route setup (EJS views, static files, accepts negotiation)
46
- // ---------------------------------------------------------------------------
47
- const locals = {
48
- pkg: { name: "ts-http-server", version: "0.0.0", description: "Test" },
49
- }
50
- const plugins = defaultPlugins({ locals })
51
- const routes = new Map([...defaultRoutes(), ...errorRoute()])
52
-
53
- const port = 19002
54
- const base = `http://localhost:${port}`
55
- let server: FastifyInstance
56
-
57
- before(async () => {
58
- server = launcher({
59
- logger: testLogger,
60
- locals: { ...locals, port },
61
- plugins,
62
- routes,
63
- opts: { disableRequestLogging: true },
64
- })
65
- await setTimeout(1000)
66
- })
67
-
68
- after(async () => {
69
- await setTimeout(500)
70
- await server.close()
71
- })
72
-
73
- test("GET / → 200 text/html", async () => {
74
- const res = await fetch(`${base}/`)
75
- const body = await res.text()
76
- equal(res.status, 200)
77
- equal(res.statusText, "OK")
78
- match(res.headers.get("content-type") ?? "", /text\/html/)
79
- ok(body.includes("<!doctype html>"))
80
- })
81
-
82
- test("HEAD / → 200 text/html", async () => {
83
- const res = await fetch(`${base}/`, { method: "HEAD" })
84
- equal(res.status, 200)
85
- equal(res.statusText, "OK")
86
- match(res.headers.get("content-type") ?? "", /text\/html/)
87
- })
88
-
89
- test("GET / with application/json → 406 Not Acceptable (json)", async () => {
90
- const res = await fetch(`${base}/`, {
91
- headers: { accept: "application/json" },
92
- })
93
- const body = (await res.json()) as { statusCode: number; error: string }
94
- equal(res.status, 406)
95
- equal(res.statusText, "Not Acceptable")
96
- match(res.headers.get("content-type") ?? "", /application\/json/)
97
- equal(body.statusCode, 406)
98
- equal(body.error, "Not Acceptable")
99
- })
100
-
101
- test("POST / → 405 Method Not Allowed (html)", async () => {
102
- const res = await fetch(`${base}/`, { method: "POST" })
103
- const body = await res.text()
104
- equal(res.status, 405)
105
- equal(res.statusText, "Method Not Allowed")
106
- equal(res.headers.get("allow"), "GET, HEAD")
107
- match(res.headers.get("content-type") ?? "", /text\/html/)
108
- ok(body.includes("405"))
109
- })
110
-
111
- test("POST / with application/json → 405 Method Not Allowed (json)", async () => {
112
- const res = await fetch(`${base}/`, {
113
- method: "POST",
114
- headers: { accept: "application/json" },
115
- })
116
- const body = (await res.json()) as { statusCode: number; error: string }
117
- equal(res.status, 405)
118
- equal(res.statusText, "Method Not Allowed")
119
- equal(res.headers.get("allow"), "GET, HEAD")
120
- match(res.headers.get("content-type") ?? "", /application\/json/)
121
- equal(body.statusCode, 405)
122
- equal(body.error, "Method Not Allowed")
123
- })
124
-
125
- test("GET /missing → 404 Not Found (html)", async () => {
126
- const res = await fetch(`${base}/missing`)
127
- const body = await res.text()
128
- equal(res.status, 404)
129
- equal(res.statusText, "Not Found")
130
- match(res.headers.get("content-type") ?? "", /text\/html/)
131
- ok(body.includes("404"))
132
- })
133
-
134
- test("GET /missing with application/json → 404 Not Found (json)", async () => {
135
- const res = await fetch(`${base}/missing`, {
136
- headers: { accept: "application/json" },
137
- })
138
- const body = (await res.json()) as { statusCode: number; error: string }
139
- equal(res.status, 404)
140
- equal(res.statusText, "Not Found")
141
- match(res.headers.get("content-type") ?? "", /application\/json/)
142
- equal(body.statusCode, 404)
143
- equal(body.error, "Not Found")
144
- })
145
-
146
- test("GET /error → 500 Internal Server Error (html)", async () => {
147
- const res = await fetch(`${base}/error`)
148
- const body = await res.text()
149
- equal(res.status, 500)
150
- equal(res.statusText, "Internal Server Error")
151
- match(res.headers.get("content-type") ?? "", /text\/html/)
152
- ok(body.includes("500"))
153
- })
154
-
155
- test("GET /error with application/json → 500 Internal Server Error (json)", async () => {
156
- const res = await fetch(`${base}/error`, {
157
- headers: { accept: "application/json" },
158
- })
159
- const body = await res.json()
160
- equal(res.status, 500)
161
- equal(res.statusText, "Internal Server Error")
162
- match(res.headers.get("content-type") ?? "", /application\/json/)
163
- ok(body)
164
- })
165
-
166
- test("GET / with Referer header → 200 text/html", async () => {
167
- const res = await fetch(`${base}/`, {
168
- headers: { accept: "text/html", referer: `${base}/` },
169
- })
170
- equal(res.status, 200)
171
- match(res.headers.get("content-type") ?? "", /text\/html/)
172
- })
173
-
174
- test("GET / without User-Agent → 200 (covers onResponse user-agent fallback)", async () => {
175
- const status = await new Promise<number>((resolve, reject) => {
176
- const req = httpRequest(
177
- {
178
- hostname: "localhost",
179
- port,
180
- path: "/",
181
- method: "GET",
182
- headers: { accept: "text/html" },
183
- },
184
- (res) => {
185
- res.resume()
186
- resolve(res.statusCode ?? 0)
187
- },
188
- )
189
- req.on("error", reject)
190
- req.end()
191
- })
192
- equal(status, 200)
193
- })
194
-
195
- test("GET /missing with text/plain → 404 plain text (Boom)", async () => {
196
- const res = await fetch(`${base}/missing`, {
197
- headers: { accept: "text/plain" },
198
- })
199
- const body = await res.text()
200
- equal(res.status, 404)
201
- match(res.headers.get("content-type") ?? "", /text\/plain/)
202
- ok(body.includes("Not Found"))
203
- })
204
- })
@@ -1,80 +0,0 @@
1
- import { equal, ok } from "node:assert/strict"
2
- import { after, before, suite, test } from "node:test"
3
- import { setTimeout } from "node:timers/promises"
4
- import FastifyAccepts from "@fastify/accepts"
5
- import type { Logger } from "@logtape/logtape"
6
- import type { FastifyInstance, RouteOptions } from "fastify"
7
- import launcher from "../launcher.ts"
8
- import type { FSTPlugin } from "../types.ts"
9
-
10
- // ---------------------------------------------------------------------------
11
- // Suite covering decorators map and done callback
12
- // ---------------------------------------------------------------------------
13
-
14
- suite("launcher [HTTP] with decorators and done callback", () => {
15
- // ---------------------------------------------------------------------------
16
- // Minimal test logger (no real I/O)
17
- // ---------------------------------------------------------------------------
18
- const noop = (): void => {}
19
- const testLogger = {
20
- category: ["test"],
21
- info: noop,
22
- error: noop,
23
- warn: noop,
24
- debug: noop,
25
- getChild: () => testLogger,
26
- } as unknown as Logger
27
-
28
- // ---------------------------------------------------------------------------
29
- // Minimal plugins and routes (no EJS/static required)
30
- // ---------------------------------------------------------------------------
31
- const plugins = new Map<string, FSTPlugin>([
32
- ["@fastify/accepts", { plugin: FastifyAccepts }],
33
- ])
34
- const routes = new Map<string, RouteOptions>([
35
- [
36
- "JSON_OK",
37
- {
38
- method: "GET",
39
- url: "/",
40
- handler: async (_request, reply) => reply.send({ ok: true }),
41
- },
42
- ],
43
- ])
44
-
45
- const port = 19003
46
- const base = `http://localhost:${port}`
47
- let server: FastifyInstance
48
- let doneWasCalled = false
49
-
50
- before(async () => {
51
- server = launcher({
52
- logger: testLogger,
53
- locals: { port },
54
- plugins,
55
- routes,
56
- decorators: new Map([["myDecorator", true]]),
57
- opts: { disableRequestLogging: true },
58
- done: () => {
59
- doneWasCalled = true
60
- },
61
- })
62
- await setTimeout(500)
63
- })
64
-
65
- after(async () => {
66
- await setTimeout(200)
67
- await server.close()
68
- })
69
-
70
- test("done callback is invoked on successful listen", () => {
71
- ok(doneWasCalled)
72
- })
73
-
74
- test("GET / → 200 JSON with decorators registered", async () => {
75
- const res = await fetch(`${base}/`)
76
- const body = (await res.json()) as { ok: boolean }
77
- equal(res.status, 200)
78
- equal(body.ok, true)
79
- })
80
- })
@@ -1,136 +0,0 @@
1
- import { equal, ok } from "node:assert/strict"
2
- import { after, before, suite, test } from "node:test"
3
- import { setTimeout } from "node:timers/promises"
4
- import FastifyAccepts from "@fastify/accepts"
5
- import type { Logger } from "@logtape/logtape"
6
- import type { RouteOptions } from "fastify"
7
- import launcher from "../launcher.ts"
8
- import type { FSTPlugin } from "../types.ts"
9
-
10
- // ---------------------------------------------------------------------------
11
- // Minimal test logger (no real I/O)
12
- // ---------------------------------------------------------------------------
13
-
14
- const noop = (): void => {}
15
- const testLogger = {
16
- category: ["test"],
17
- info: noop,
18
- error: noop,
19
- warn: noop,
20
- debug: noop,
21
- getChild: () => testLogger,
22
- } as unknown as Logger
23
-
24
- // ---------------------------------------------------------------------------
25
- // Minimal plugins and routes (no EJS/static required)
26
- // ---------------------------------------------------------------------------
27
-
28
- const plugins = new Map<string, FSTPlugin>([
29
- ["@fastify/accepts", { plugin: FastifyAccepts }],
30
- ])
31
-
32
- const routes = new Map<string, RouteOptions>([
33
- [
34
- "JSON_OK",
35
- {
36
- method: "GET",
37
- url: "/",
38
- handler: async (_request, reply) => reply.send({ ok: true }),
39
- },
40
- ],
41
- [
42
- "JSON_405",
43
- {
44
- method: ["POST", "PUT", "DELETE", "PATCH"],
45
- url: "/",
46
- handler: async (_request, reply) => {
47
- reply.header("allow", "GET, HEAD")
48
- return reply.status(405).send({ statusCode: 405 })
49
- },
50
- },
51
- ],
52
- [
53
- "ERROR_ROUTE",
54
- {
55
- method: "GET",
56
- url: "/error",
57
- handler: async () => {
58
- throw new Error("test server error")
59
- },
60
- },
61
- ],
62
- ])
63
-
64
- // ---------------------------------------------------------------------------
65
- // HTTP server suite
66
- // ---------------------------------------------------------------------------
67
-
68
- suite("launcher [HTTP]", () => {
69
- const port = 19001
70
- const base = `http://localhost:${port}`
71
- let server: import("fastify").FastifyInstance
72
-
73
- before(async () => {
74
- server = launcher({
75
- logger: testLogger,
76
- locals: { port },
77
- plugins,
78
- routes,
79
- opts: { disableRequestLogging: true },
80
- })
81
- await setTimeout(500)
82
- })
83
-
84
- after(async () => {
85
- await setTimeout(200)
86
- await server.close()
87
- })
88
-
89
- test("GET / → 200 JSON", async () => {
90
- const res = await fetch(`${base}/`)
91
- equal(res.status, 200)
92
- const body = (await res.json()) as { ok: boolean }
93
- equal(body.ok, true)
94
- })
95
-
96
- test("POST / → 405", async () => {
97
- const res = await fetch(`${base}/`, { method: "POST" })
98
- equal(res.status, 405)
99
- equal(res.headers.get("allow"), "GET, HEAD")
100
- })
101
-
102
- test("GET /missing → 404 JSON", async () => {
103
- const res = await fetch(`${base}/missing`, {
104
- headers: { accept: "application/json" },
105
- })
106
- equal(res.status, 404)
107
- equal(
108
- res.headers.get("content-type"),
109
- "application/json; charset=utf-8",
110
- )
111
- const body = (await res.json()) as { statusCode: number }
112
- ok(body.statusCode)
113
- equal(body.statusCode, 404)
114
- })
115
-
116
- test("GET /missing → 404 plain text", async () => {
117
- const res = await fetch(`${base}/missing`, {
118
- headers: { accept: "text/plain" },
119
- })
120
- equal(res.status, 404)
121
- })
122
-
123
- test("GET /error → 500 JSON", async () => {
124
- const res = await fetch(`${base}/error`, {
125
- headers: { accept: "application/json" },
126
- })
127
- equal(res.status, 500)
128
- })
129
-
130
- test("GET /error → 500 plain text", async () => {
131
- const res = await fetch(`${base}/error`, {
132
- headers: { accept: "text/plain" },
133
- })
134
- equal(res.status, 500)
135
- })
136
- })