@lexho111/plainblog 0.8.0 → 0.8.2

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/Blog.js CHANGED
@@ -384,8 +384,12 @@ export default class Blog {
384
384
  if (startBothByDefault || hasHttp) {
385
385
  listenPromises.push(listen(this.#http_server, httpPort, "http"));
386
386
  }
387
- if (startBothByDefault || hasHttps) {
387
+ if ((startBothByDefault || hasHttps) && this.#server) {
388
388
  listenPromises.push(listen(this.#server, httpsPort, "https"));
389
+ } else if (hasHttps && !this.#server) {
390
+ console.warn(
391
+ "HTTPS server was requested but could not be started. SSL certificate/key files might be missing.",
392
+ );
389
393
  }
390
394
 
391
395
  await Promise.all(listenPromises);
package/README.md CHANGED
@@ -22,6 +22,19 @@ await blog.init();
22
22
  await blog.startServer();
23
23
  ```
24
24
 
25
+ ```
26
+ const blog = new BlogBuilder()
27
+ .withTitle("Mein Blog")
28
+ .withStyle("body { font-family: Arial, sans-serif; } h1 { color: #333; }")
29
+ .withPassword("mypassword")
30
+ .withDatabase(adapter)
31
+ .build();
32
+
33
+ await blog.init();
34
+
35
+ await blog.startServer({ httpPort: 38080 });
36
+ ```
37
+
25
38
  Now you can open your blog in your webbrowser on `http://localhost:8080`. Login via the login link in the navbar to begin with adding new articles, the password is '**_admin_**'.
26
39
 
27
40
  ## More Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lexho111/plainblog",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "A tool for creating and serving a minimalist, single-page blog.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/postinstall.js CHANGED
@@ -80,6 +80,28 @@ async function main() {
80
80
  }
81
81
  }
82
82
  }
83
+
84
+ // Copy SSL certificates to project root
85
+ const certFiles = ["localhost.cert", "localhost.key"];
86
+ for (const file of certFiles) {
87
+ const sourceFile = path.join(__dirname, file);
88
+ const destFile = path.join(projectRoot, file);
89
+
90
+ try {
91
+ await fs.promises.access(destFile);
92
+ } catch (error) {
93
+ if (error.code === "ENOENT" || error.message.includes("ENOENT")) {
94
+ try {
95
+ await fs.promises.copyFile(sourceFile, destFile);
96
+ console.log(
97
+ `[plainblog] copied default certificate '${file}' to project root.`,
98
+ );
99
+ } catch (copyError) {
100
+ // Silent fail if source certs are missing in the package
101
+ }
102
+ }
103
+ }
104
+ }
83
105
  } catch (error) {
84
106
  // We log errors but don't fail the entire installation.
85
107
  console.error("[plainblog] Postinstall error:", error);
package/server.js CHANGED
@@ -6,7 +6,7 @@ import { Buffer } from "node:buffer";
6
6
  import Article from "./Article.js";
7
7
  import { createDebug, debug_perf, debug_perf1 } from "./debug-loader.js";
8
8
  import { readFile, writeFile, mkdir } from "node:fs/promises";
9
- import { readFileSync } from "fs";
9
+ import { readFileSync, existsSync } from "fs";
10
10
  import Auth from "./Auth.js";
11
11
  import { getLoginTemplate } from "./render.js";
12
12
 
@@ -177,11 +177,6 @@ export async function createServer(config) {
177
177
  const publicDir = path.join(process.cwd(), "public");
178
178
  const auth = new Auth(secret, password);
179
179
 
180
- const options = {
181
- key: readFileSync(ssl?.key || "./localhost.key"),
182
- cert: readFileSync(ssl?.cert || "./localhost.cert"),
183
- };
184
-
185
180
  // --- Route Handlers ---
186
181
 
187
182
  async function apiHandler(req, res) {
@@ -387,8 +382,22 @@ export async function createServer(config) {
387
382
 
388
383
  const requestHandler = withRequestTimeout(handleRequests, REQUEST_TIMEOUT);
389
384
  const server_http = http.createServer(requestHandler);
390
- const server_https = https.createServer(options, requestHandler);
391
385
 
386
+ let server_https = null;
387
+ const keyPath = ssl?.key || "./localhost.key";
388
+ const certPath = ssl?.cert || "./localhost.cert";
389
+
390
+ if (existsSync(keyPath) && existsSync(certPath)) {
391
+ try {
392
+ const options = {
393
+ key: readFileSync(keyPath),
394
+ cert: readFileSync(certPath),
395
+ };
396
+ server_https = https.createServer(options, requestHandler);
397
+ } catch (e) {
398
+ console.error(`Error creating HTTPS server: ${e.message}.`);
399
+ }
400
+ }
392
401
  // Attach socket tracking to the server instance
393
402
  const setupSocketTracking = (serverInstance) => {
394
403
  serverInstance.sockets = new Set();
@@ -415,7 +424,9 @@ export async function createServer(config) {
415
424
  };
416
425
 
417
426
  setupSocketTracking(server_http);
418
- setupSocketTracking(server_https);
427
+ if (server_https) {
428
+ setupSocketTracking(server_https);
429
+ }
419
430
 
420
431
  return { http: server_http, https: server_https };
421
432
  }