greend-server 1.0.1 → 1.0.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +59 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greend-server",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "GreenD ESG backend — self-hosted Express.js API server",
5
5
  "type": "module",
6
6
  "main": "server.js",
package/server.js CHANGED
@@ -2,6 +2,7 @@ import 'dotenv/config';
2
2
  import { randomBytes } from 'crypto';
3
3
  import express from 'express';
4
4
  import cors from 'cors';
5
+ import { readFileSync } from 'fs';
5
6
  import { readFile, writeFile, mkdir, readdir } from 'fs/promises';
6
7
  import { fileURLToPath } from 'url';
7
8
  import { dirname, join } from 'path';
@@ -32,6 +33,63 @@ const DATA_DIR = process.env.DATA_DIR
32
33
  ? path.resolve(process.env.DATA_DIR)
33
34
  : join(__dirname, 'data');
34
35
 
36
+ // ---------------------------------------------------------------------------
37
+ // File-backed session store — persists sessions to DATA_DIR/sessions.json so
38
+ // that sessions survive server restarts (MemoryStore loses them on every restart).
39
+ // ---------------------------------------------------------------------------
40
+ class FileSessionStore extends session.Store {
41
+ #filepath;
42
+ #sessions;
43
+
44
+ constructor(filepath) {
45
+ super();
46
+ this.#filepath = filepath;
47
+ this.#sessions = {};
48
+ // Load synchronously at startup so sessions are available immediately.
49
+ try {
50
+ const raw = readFileSync(filepath, 'utf8');
51
+ const loaded = JSON.parse(raw);
52
+ const now = Date.now();
53
+ // Discard any already-expired sessions while loading.
54
+ for (const [sid, s] of Object.entries(loaded)) {
55
+ if (s?.cookie?.expires && new Date(s.cookie.expires).getTime() < now) continue;
56
+ this.#sessions[sid] = s;
57
+ }
58
+ } catch {
59
+ // File doesn't exist yet — start with empty sessions.
60
+ }
61
+ }
62
+
63
+ #save() {
64
+ mkdir(dirname(this.#filepath), { recursive: true })
65
+ .then(() => writeFile(this.#filepath, JSON.stringify(this.#sessions)))
66
+ .catch(e => console.error('[sessions] write error:', e.message));
67
+ }
68
+
69
+ get(sid, cb) {
70
+ const s = this.#sessions[sid];
71
+ if (!s) return cb(null, null);
72
+ if (s.cookie?.expires && new Date(s.cookie.expires).getTime() < Date.now()) {
73
+ delete this.#sessions[sid];
74
+ this.#save();
75
+ return cb(null, null);
76
+ }
77
+ cb(null, s);
78
+ }
79
+
80
+ set(sid, s, cb) {
81
+ this.#sessions[sid] = s;
82
+ this.#save();
83
+ cb(null);
84
+ }
85
+
86
+ destroy(sid, cb) {
87
+ delete this.#sessions[sid];
88
+ this.#save();
89
+ cb(null);
90
+ }
91
+ }
92
+
35
93
  const app = express();
36
94
  app.use(express.json({ limit: '50mb' }));
37
95
 
@@ -48,6 +106,7 @@ app.use(cors({
48
106
  }));
49
107
 
50
108
  app.use(session({
109
+ store: new FileSessionStore(join(DATA_DIR, 'sessions.json')),
51
110
  secret: process.env.SESSION_SECRET || 'greend-dev-secret-change-in-production',
52
111
  resave: false,
53
112
  saveUninitialized: false,