@seip/bluebird-js 0.1.1 → 0.1.4
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/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +10 -0
- package/AGENTS.md +124 -64
- package/README.md +236 -103
- package/app/routes/web.js +47 -55
- package/app/views/about.jsx +98 -0
- package/app/views/components/Header.jsx +42 -0
- package/app/views/components/LiveCounter.jsx +67 -0
- package/app/views/dashboard.jsx +300 -0
- package/app/views/index.jsx +123 -0
- package/app/views/layouts/main/view.jsx +201 -0
- package/app/views/legal/terms.html +29 -19
- package/app/views/login.jsx +141 -0
- package/core/actions.d.ts +4 -0
- package/core/actions.js +329 -0
- package/core/app.js +129 -22
- package/core/auth.js +1 -1
- package/core/cli/doctor.js +3 -4
- package/core/cli/init.js +153 -6
- package/core/cli/migrate.js +418 -145
- package/core/cli/view.js +76 -11
- package/core/config.js +35 -5
- package/core/csrf.js +1 -1
- package/core/database.js +52 -17
- package/core/debug.js +1 -1
- package/core/html.d.ts +54 -42
- package/core/html.js +473 -217
- package/core/index.d.ts +187 -85
- package/core/jsx.d.ts +38 -0
- package/core/jsx.js +1180 -0
- package/core/model.js +282 -61
- package/core/queue.js +7 -4
- package/core/render-cache.js +212 -212
- package/core/render.d.ts +20 -2
- package/core/render.js +320 -43
- package/core/upload.js +7 -1
- package/core/validate.js +1 -1
- package/core/view-loader.js +341 -19
- package/core/vite.js +1 -1
- package/package.json +9 -3
- package/public/js/bluebird-client.js +2313 -1049
- package/public/js/tailwind.js +8 -0
- package/app/views/about.js +0 -63
- package/app/views/dashboard.js +0 -147
- package/app/views/index.js +0 -80
- package/app/views/layouts/main/view.js +0 -81
- package/app/views/login.js +0 -80
- package/public/css/bluebird.min.css +0 -2
- package/public/js/bluebird.min.js +0 -1073
package/core/actions.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import Csrf from "./csrf.js";
|
|
2
|
+
import Validator from "./validate.js";
|
|
3
|
+
import Render from "./render.js";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MAX_BYTES = 64 * 1024;
|
|
6
|
+
const DEFAULT_MAX_DEPTH = 6;
|
|
7
|
+
const DEFAULT_MAX_KEYS = 100;
|
|
8
|
+
const BANNED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Recursively sanitizes and validates an incoming action payload object to prevent
|
|
12
|
+
* prototype pollution (`__proto__`, `constructor`, `prototype`), excessive nesting depth,
|
|
13
|
+
* and key-flooding memory attacks.
|
|
14
|
+
*
|
|
15
|
+
* @param {any} input
|
|
16
|
+
* @param {number} [maxDepth=DEFAULT_MAX_DEPTH]
|
|
17
|
+
* @param {number} [maxKeys=DEFAULT_MAX_KEYS]
|
|
18
|
+
* @param {number} [currentDepth=0]
|
|
19
|
+
* @param {{ count: number }} [keyCounter={ count: 0 }]
|
|
20
|
+
* @returns {any}
|
|
21
|
+
*/
|
|
22
|
+
export function sanitizeActionPayload(
|
|
23
|
+
input,
|
|
24
|
+
maxDepth = DEFAULT_MAX_DEPTH,
|
|
25
|
+
maxKeys = DEFAULT_MAX_KEYS,
|
|
26
|
+
currentDepth = 0,
|
|
27
|
+
keyCounter = { count: 0 }
|
|
28
|
+
) {
|
|
29
|
+
if (input === null || input === undefined) return input;
|
|
30
|
+
if (typeof input !== "object") return input;
|
|
31
|
+
|
|
32
|
+
if (currentDepth > maxDepth) {
|
|
33
|
+
const err = new Error("Payload nesting depth limit exceeded.");
|
|
34
|
+
err.statusCode = 413;
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (Array.isArray(input)) {
|
|
39
|
+
if (input.length > maxKeys) {
|
|
40
|
+
const err = new Error(`Payload array length exceeds maximum allowed (${maxKeys}).`);
|
|
41
|
+
err.statusCode = 413;
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
return input.map((item) =>
|
|
45
|
+
sanitizeActionPayload(item, maxDepth, maxKeys, currentDepth + 1, keyCounter)
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const clean = Object.create(null);
|
|
50
|
+
const entries = Object.entries(input);
|
|
51
|
+
for (const [k, v] of entries) {
|
|
52
|
+
if (BANNED_KEYS.has(k)) continue;
|
|
53
|
+
keyCounter.count++;
|
|
54
|
+
if (keyCounter.count > maxKeys) {
|
|
55
|
+
const err = new Error(`Payload key count exceeds maximum allowed (${maxKeys}).`);
|
|
56
|
+
err.statusCode = 413;
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
clean[k] = sanitizeActionPayload(v, maxDepth, maxKeys, currentDepth + 1, keyCounter);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ...clean };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Parses byte size strings like "64kb", "1mb", or numbers into integer bytes.
|
|
67
|
+
* @param {number|string} [limit]
|
|
68
|
+
* @returns {number}
|
|
69
|
+
*/
|
|
70
|
+
export function parseByteLimit(limit) {
|
|
71
|
+
if (typeof limit === "number" && limit > 0) return limit;
|
|
72
|
+
if (typeof limit === "string") {
|
|
73
|
+
const match = limit.trim().match(/^(\d+(?:\.\d+)?)\s*(b|kb|mb)?$/i);
|
|
74
|
+
if (match) {
|
|
75
|
+
const num = parseFloat(match[1]);
|
|
76
|
+
const unit = (match[2] || "b").toLowerCase();
|
|
77
|
+
if (unit === "mb") return Math.floor(num * 1024 * 1024);
|
|
78
|
+
if (unit === "kb") return Math.floor(num * 1024);
|
|
79
|
+
return Math.floor(num);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return DEFAULT_MAX_BYTES;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Native Server Actions manager for Bluebird.js.
|
|
87
|
+
* Wraps server mutation handlers with automatic CSRF verification, strict payload size/depth/key
|
|
88
|
+
* guards, optional schema validation, and reactive partial SSR re-rendering.
|
|
89
|
+
*/
|
|
90
|
+
export class Actions {
|
|
91
|
+
/**
|
|
92
|
+
* Defines a single server action or a map of server actions with security and validation options.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* // Single action with schema validation and 32KB max payload
|
|
96
|
+
* export const actions = {
|
|
97
|
+
* updateProfile: Actions.define(
|
|
98
|
+
* {
|
|
99
|
+
* schema: { name: { required: true, min: 2 } },
|
|
100
|
+
* maxBytes: "32kb",
|
|
101
|
+
* },
|
|
102
|
+
* async ({ body, req, user }) => {
|
|
103
|
+
* await User.update(user.id, body, { only: ["name"] });
|
|
104
|
+
* return { updated: true };
|
|
105
|
+
* }
|
|
106
|
+
* ),
|
|
107
|
+
* };
|
|
108
|
+
*
|
|
109
|
+
* @param {Object|Function} optionsOrMap - Options object, action map, or handler function.
|
|
110
|
+
* @param {Function} [handlerFn] - Action handler function when first argument is options.
|
|
111
|
+
* @returns {Object}
|
|
112
|
+
*/
|
|
113
|
+
static define(optionsOrMap, handlerFn) {
|
|
114
|
+
if (typeof optionsOrMap === "function") {
|
|
115
|
+
return {
|
|
116
|
+
__bb_action__: true,
|
|
117
|
+
options: {},
|
|
118
|
+
handler: optionsOrMap,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (typeof handlerFn === "function") {
|
|
123
|
+
return {
|
|
124
|
+
__bb_action__: true,
|
|
125
|
+
options: optionsOrMap || {},
|
|
126
|
+
handler: handlerFn,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (optionsOrMap && typeof optionsOrMap === "object") {
|
|
131
|
+
const out = {};
|
|
132
|
+
for (const [key, val] of Object.entries(optionsOrMap)) {
|
|
133
|
+
if (typeof val === "function") {
|
|
134
|
+
out[key] = { __bb_action__: true, options: {}, handler: val };
|
|
135
|
+
} else if (val && typeof val === "object" && typeof val.handler === "function") {
|
|
136
|
+
out[key] = {
|
|
137
|
+
__bb_action__: true,
|
|
138
|
+
options: val.options || val,
|
|
139
|
+
handler: val.handler,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Express route handler that dispatches incoming POST requests to the matching Server Action
|
|
151
|
+
* exported by a view module (`export const actions = { ... }`).
|
|
152
|
+
*
|
|
153
|
+
* Security checks performed automatically before invoking the action:
|
|
154
|
+
* 1. CSRF verification (header `x-csrf-token` or body `_csrf` matched against signed `_csrf` cookie).
|
|
155
|
+
* 2. Payload byte size guard (`Content-Length` and serialized body byte length, default 64KB).
|
|
156
|
+
* 3. Prototype pollution & deep-nesting guard (`__proto__`, `constructor`, `prototype`, maxDepth, maxKeys).
|
|
157
|
+
* 4. Optional `Validator` schema check if `schema` is configured on the action.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} viewName - View name associated with the route.
|
|
160
|
+
* @param {Record<string, any>} actionsMap - Exported `actions` object from the view module.
|
|
161
|
+
* @returns {Function} Express route handler.
|
|
162
|
+
*/
|
|
163
|
+
static createDispatcher(viewName, actionsMap) {
|
|
164
|
+
return async (req, res) => {
|
|
165
|
+
const actionName =
|
|
166
|
+
req.headers["x-bluebird-action"] ||
|
|
167
|
+
req.body?._action ||
|
|
168
|
+
req.query?._action ||
|
|
169
|
+
"default";
|
|
170
|
+
|
|
171
|
+
const rawAction =
|
|
172
|
+
actionsMap[actionName] ||
|
|
173
|
+
(actionName === "default" && typeof actionsMap === "function" ? actionsMap : null);
|
|
174
|
+
|
|
175
|
+
if (!rawAction) {
|
|
176
|
+
return res.status(404).json({
|
|
177
|
+
status: "error",
|
|
178
|
+
message: `Action '${String(actionName)}' not found on view '${viewName}'.`,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const actionDef =
|
|
183
|
+
typeof rawAction === "function"
|
|
184
|
+
? { options: {}, handler: rawAction }
|
|
185
|
+
: rawAction;
|
|
186
|
+
const opts = actionDef.options || {};
|
|
187
|
+
const handler = actionDef.handler;
|
|
188
|
+
|
|
189
|
+
if (typeof handler !== "function") {
|
|
190
|
+
return res.status(404).json({
|
|
191
|
+
status: "error",
|
|
192
|
+
message: `Invalid handler for action '${String(actionName)}'.`,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const maxBytes = parseByteLimit(opts.maxBytes ?? opts.limit ?? DEFAULT_MAX_BYTES);
|
|
197
|
+
const maxDepth = opts.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
198
|
+
const maxKeys = opts.maxKeys ?? DEFAULT_MAX_KEYS;
|
|
199
|
+
|
|
200
|
+
const contentLength = parseInt(req.headers["content-length"] || "0", 10);
|
|
201
|
+
if (contentLength > maxBytes) {
|
|
202
|
+
return res.status(413).json({
|
|
203
|
+
status: "error",
|
|
204
|
+
message: `Action payload too large (exceeds ${maxBytes} bytes).`,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const rawBodyStr = JSON.stringify(req.body || {});
|
|
209
|
+
if (Buffer.byteLength(rawBodyStr, "utf-8") > maxBytes) {
|
|
210
|
+
return res.status(413).json({
|
|
211
|
+
status: "error",
|
|
212
|
+
message: `Action payload size exceeds maximum allowed (${maxBytes} bytes).`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (opts.csrf !== false) {
|
|
217
|
+
const cookieToken = req.cookies?.["_csrf"];
|
|
218
|
+
const headerToken =
|
|
219
|
+
req.headers["x-csrf-token"] ||
|
|
220
|
+
req.body?._csrf ||
|
|
221
|
+
req.query?._csrf;
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
!cookieToken ||
|
|
225
|
+
!headerToken ||
|
|
226
|
+
cookieToken !== headerToken ||
|
|
227
|
+
!Csrf.verifyToken(headerToken)
|
|
228
|
+
) {
|
|
229
|
+
return res.status(403).json({
|
|
230
|
+
status: "error",
|
|
231
|
+
message: "Invalid or missing CSRF token for server action.",
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let safeBody = {};
|
|
237
|
+
try {
|
|
238
|
+
safeBody = sanitizeActionPayload(req.body || {}, maxDepth, maxKeys);
|
|
239
|
+
delete safeBody._csrf;
|
|
240
|
+
delete safeBody._action;
|
|
241
|
+
} catch (err) {
|
|
242
|
+
return res.status(err.statusCode || 400).json({
|
|
243
|
+
status: "error",
|
|
244
|
+
message: err.message || "Invalid action payload.",
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (opts.schema) {
|
|
249
|
+
const validator =
|
|
250
|
+
opts.schema instanceof Validator
|
|
251
|
+
? opts.schema
|
|
252
|
+
: new Validator(opts.schema, req.lang || "en");
|
|
253
|
+
const validation = await validator.validate(safeBody);
|
|
254
|
+
if (!validation.success) {
|
|
255
|
+
return res.status(400).json({
|
|
256
|
+
status: "error",
|
|
257
|
+
message: validation.message?.[0] || "Validation failed.",
|
|
258
|
+
errors: validation.errors,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const ctx = {
|
|
264
|
+
req,
|
|
265
|
+
res,
|
|
266
|
+
user: req.user,
|
|
267
|
+
body: safeBody,
|
|
268
|
+
payload: safeBody,
|
|
269
|
+
params: req.params || {},
|
|
270
|
+
query: req.query || {},
|
|
271
|
+
action: String(actionName),
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const result = await handler(ctx, safeBody);
|
|
275
|
+
if (res.headersSent) return;
|
|
276
|
+
|
|
277
|
+
if (result && typeof result === "object" && typeof result.redirect === "string") {
|
|
278
|
+
return Render.redirect(req, res, result.redirect);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const wantsPartial =
|
|
282
|
+
req.headers["x-bluebird-partial"] === "1" ||
|
|
283
|
+
req.query?._partial === "1" ||
|
|
284
|
+
(result && typeof result === "object" && result.reRender === true);
|
|
285
|
+
|
|
286
|
+
if (wantsPartial) {
|
|
287
|
+
const extraProps =
|
|
288
|
+
result && typeof result === "object" && !Array.isArray(result)
|
|
289
|
+
? result.props || result
|
|
290
|
+
: {};
|
|
291
|
+
return Render.send(res, viewName, {
|
|
292
|
+
user: req.user,
|
|
293
|
+
params: req.params,
|
|
294
|
+
query: req.query,
|
|
295
|
+
actionResult: result,
|
|
296
|
+
...extraProps,
|
|
297
|
+
_layout: false,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const expectsJson =
|
|
302
|
+
req.headers["x-bluebird-action"] ||
|
|
303
|
+
req.headers["x-requested-with"] === "XMLHttpRequest" ||
|
|
304
|
+
req.headers.accept?.includes("application/json");
|
|
305
|
+
|
|
306
|
+
if (expectsJson) {
|
|
307
|
+
return res.status(200).json({
|
|
308
|
+
status: "success",
|
|
309
|
+
action: String(actionName),
|
|
310
|
+
data: result ?? null,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const extraProps =
|
|
315
|
+
result && typeof result === "object" && !Array.isArray(result)
|
|
316
|
+
? result.props || result
|
|
317
|
+
: {};
|
|
318
|
+
return Render.send(res, viewName, {
|
|
319
|
+
user: req.user,
|
|
320
|
+
params: req.params,
|
|
321
|
+
query: req.query,
|
|
322
|
+
actionResult: result,
|
|
323
|
+
...extraProps,
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export default Actions;
|
package/core/app.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import http from "node:http";
|
|
2
2
|
import express from "express";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
4
6
|
import colors from "./util/colors.js";
|
|
5
7
|
import Config from "./config.js";
|
|
6
8
|
import Logger from "./logger.js";
|
|
@@ -9,10 +11,25 @@ import WebSocketManager from "./ws.js";
|
|
|
9
11
|
import RateLimit from "./ratelimit.js";
|
|
10
12
|
import Seo from "./seo.js";
|
|
11
13
|
|
|
12
|
-
const __dirname = Config.dirname();
|
|
13
14
|
const props = Config.props();
|
|
14
15
|
const SERVER_BOOT_ID = Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Detects the primary local network IPv4 address.
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
function getLanIp() {
|
|
22
|
+
const interfaces = os.networkInterfaces();
|
|
23
|
+
for (const name of Object.keys(interfaces)) {
|
|
24
|
+
for (const iface of interfaces[name] || []) {
|
|
25
|
+
if (iface.family === "IPv4" && !iface.internal) {
|
|
26
|
+
return iface.address;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return "127.0.0.1";
|
|
31
|
+
}
|
|
32
|
+
|
|
16
33
|
/**
|
|
17
34
|
* Lightweight native cookie parser middleware.
|
|
18
35
|
* @param {import('express').Request} req
|
|
@@ -171,8 +188,13 @@ class App {
|
|
|
171
188
|
* @param {boolean} [options.trustProxy=true] - Enable Express trust proxy for accurate IP behind reverse proxies.
|
|
172
189
|
* @param {boolean} [options.desktop=false] - Enable Electron/Desktop mode (binds 127.0.0.1 and uses writable userDataDir).
|
|
173
190
|
* @param {string} [options.userDataPath] - Custom writable directory path for SQLite, logs, and file cache in Electron.
|
|
191
|
+
* @param {string} [options.appPath] - Custom application root directory for views and static assets (ideal for Electron app.getAppPath()).
|
|
192
|
+
* @param {boolean} [options.allowLan=false] - Whether to allow local network / mobile access (binds 0.0.0.0 in desktop mode).
|
|
174
193
|
*/
|
|
175
194
|
constructor(options = {}) {
|
|
195
|
+
if (options.appPath) {
|
|
196
|
+
Config.setAppDir(options.appPath);
|
|
197
|
+
}
|
|
176
198
|
if (options.userDataPath) {
|
|
177
199
|
Config.setUserDataDir(options.userDataPath);
|
|
178
200
|
} else if (options.desktop) {
|
|
@@ -188,8 +210,9 @@ class App {
|
|
|
188
210
|
this.cors = options.cors || {};
|
|
189
211
|
this.middlewares = options.middlewares || [];
|
|
190
212
|
this.desktop = options.desktop ?? Config.isDesktop();
|
|
213
|
+
this.allowLan = Boolean(options.allowLan);
|
|
191
214
|
this.port = options.port !== undefined ? options.port : (this.desktop && !process.env.PORT ? 0 : props.port);
|
|
192
|
-
this.host = options.host || (this.desktop ? "http://127.0.0.1" : props.host);
|
|
215
|
+
this.host = options.host || (this.desktop ? (this.allowLan ? "http://0.0.0.0" : "http://127.0.0.1") : props.host);
|
|
193
216
|
this.appUrl = options.appUrl || props.appUrl;
|
|
194
217
|
this.logger = options.logger ?? false;
|
|
195
218
|
this.notFound = options.notFound ?? true;
|
|
@@ -201,6 +224,8 @@ class App {
|
|
|
201
224
|
this.compression = options.compression ?? false;
|
|
202
225
|
this.security = options.security ?? true;
|
|
203
226
|
this.rateLimit = options.rateLimit ?? false;
|
|
227
|
+
this.noAutoDiscoverRoutes = Boolean(options.noAutoDiscoverRoutes);
|
|
228
|
+
this.autoRoutes = this.noAutoDiscoverRoutes ? false : (options.autoRoutes ?? true);
|
|
204
229
|
this.trustProxy = options.trustProxy ?? !this.desktop;
|
|
205
230
|
this.debug = options.debug !== undefined ? Boolean(options.debug) : (props.debug || process.env.DEBUG === "true");
|
|
206
231
|
this._loggerInstance = null;
|
|
@@ -273,16 +298,30 @@ class App {
|
|
|
273
298
|
*/
|
|
274
299
|
_setupCoreMiddlewares() {
|
|
275
300
|
if (this.compression) {
|
|
301
|
+
let compressionMiddleware = null;
|
|
276
302
|
import("compression")
|
|
277
303
|
.then((mod) => {
|
|
278
304
|
const comp = mod.default || mod;
|
|
279
|
-
|
|
305
|
+
compressionMiddleware = comp();
|
|
280
306
|
})
|
|
281
307
|
.catch(() => {});
|
|
308
|
+
|
|
309
|
+
this.app.use((req, res, next) => {
|
|
310
|
+
if (compressionMiddleware) {
|
|
311
|
+
return compressionMiddleware(req, res, next);
|
|
312
|
+
}
|
|
313
|
+
next();
|
|
314
|
+
});
|
|
282
315
|
}
|
|
283
316
|
|
|
284
|
-
if (this.json)
|
|
285
|
-
|
|
317
|
+
if (this.json) {
|
|
318
|
+
const jsonOpts = typeof this.json === "object" ? this.json : { limit: "256kb" };
|
|
319
|
+
this.app.use(express.json(jsonOpts));
|
|
320
|
+
}
|
|
321
|
+
if (this.urlencoded) {
|
|
322
|
+
const urlOpts = typeof this.urlencoded === "object" ? { extended: true, ...this.urlencoded } : { extended: true, limit: "256kb" };
|
|
323
|
+
this.app.use(express.urlencoded(urlOpts));
|
|
324
|
+
}
|
|
286
325
|
if (this.cookieParser) this.app.use(nativeCookieParser);
|
|
287
326
|
|
|
288
327
|
if (this.security) {
|
|
@@ -371,9 +410,12 @@ class App {
|
|
|
371
410
|
const staticPath = this.static?.path || "public";
|
|
372
411
|
const staticOptions = this.static?.options || {};
|
|
373
412
|
const isDebug = this.debug;
|
|
413
|
+
const resolvedStaticDir = path.isAbsolute(staticPath)
|
|
414
|
+
? staticPath
|
|
415
|
+
: path.resolve(Config.appDir(), staticPath);
|
|
374
416
|
|
|
375
417
|
this.app.use(
|
|
376
|
-
express.static(
|
|
418
|
+
express.static(resolvedStaticDir, {
|
|
377
419
|
...staticOptions,
|
|
378
420
|
setHeaders: (res) => {
|
|
379
421
|
res.setHeader("X-Powered-By", "Blue Bird");
|
|
@@ -385,6 +427,16 @@ class App {
|
|
|
385
427
|
})
|
|
386
428
|
);
|
|
387
429
|
|
|
430
|
+
if (this.desktop) {
|
|
431
|
+
const uploadsDir = path.join(Config.userDataDir(), "uploads");
|
|
432
|
+
if (!fs.existsSync(uploadsDir)) {
|
|
433
|
+
try {
|
|
434
|
+
fs.mkdirSync(uploadsDir, { recursive: true });
|
|
435
|
+
} catch {}
|
|
436
|
+
}
|
|
437
|
+
this.app.use("/uploads", express.static(uploadsDir));
|
|
438
|
+
}
|
|
439
|
+
|
|
388
440
|
this.middlewares.forEach((middleware) => {
|
|
389
441
|
this.app.use(middleware);
|
|
390
442
|
});
|
|
@@ -418,7 +470,7 @@ class App {
|
|
|
418
470
|
});
|
|
419
471
|
}
|
|
420
472
|
|
|
421
|
-
this._dispatchRoutes();
|
|
473
|
+
await this._dispatchRoutes();
|
|
422
474
|
|
|
423
475
|
if (this.notFound) this._notFoundDefault();
|
|
424
476
|
|
|
@@ -478,16 +530,28 @@ class App {
|
|
|
478
530
|
}
|
|
479
531
|
|
|
480
532
|
/**
|
|
481
|
-
* Attaches all registered routes to the Express application, including automatic SEO routes.
|
|
533
|
+
* Attaches all registered routes to the Express application, including automatic view routes and SEO routes.
|
|
482
534
|
* @private
|
|
483
535
|
*/
|
|
484
|
-
_dispatchRoutes() {
|
|
536
|
+
async _dispatchRoutes() {
|
|
485
537
|
if (props.debug) {
|
|
486
538
|
const debug = new Debug();
|
|
487
539
|
const debugRouter = debug.getRouter();
|
|
488
540
|
this.app.use(debugRouter.path, debugRouter.router);
|
|
489
541
|
}
|
|
490
542
|
|
|
543
|
+
let autoViewRouter = null;
|
|
544
|
+
if (this.autoRoutes) {
|
|
545
|
+
try {
|
|
546
|
+
const [{ default: Router }, { default: Render }] = await Promise.all([
|
|
547
|
+
import("./router.js"),
|
|
548
|
+
import("./render.js"),
|
|
549
|
+
]);
|
|
550
|
+
autoViewRouter = new Router("/");
|
|
551
|
+
await Render.autoRoutes(autoViewRouter);
|
|
552
|
+
} catch {}
|
|
553
|
+
}
|
|
554
|
+
|
|
491
555
|
const hasSeoRouter = this.routes.some((r) => r._seo) || Seo.getRoutes().length > 0;
|
|
492
556
|
if (hasSeoRouter) {
|
|
493
557
|
this.app.get("/sitemap.xml", (req, res) => {
|
|
@@ -503,6 +567,10 @@ class App {
|
|
|
503
567
|
this.routes.forEach((route) => {
|
|
504
568
|
this.app.use(route.path, route.router);
|
|
505
569
|
});
|
|
570
|
+
|
|
571
|
+
if (autoViewRouter) {
|
|
572
|
+
this.app.use(autoViewRouter.path, autoViewRouter.router);
|
|
573
|
+
}
|
|
506
574
|
}
|
|
507
575
|
|
|
508
576
|
/**
|
|
@@ -519,7 +587,7 @@ class App {
|
|
|
519
587
|
|
|
520
588
|
/**
|
|
521
589
|
* Starts the HTTP server and listens for incoming connections.
|
|
522
|
-
* In desktop mode, binds
|
|
590
|
+
* In desktop mode, binds to 127.0.0.1 (or 0.0.0.0 if allowLan is true).
|
|
523
591
|
* @returns {Promise<import('http').Server>}
|
|
524
592
|
*/
|
|
525
593
|
async run() {
|
|
@@ -532,23 +600,33 @@ class App {
|
|
|
532
600
|
if (this.desktop) {
|
|
533
601
|
this.appUrl = `http://127.0.0.1:${activePort}`;
|
|
534
602
|
}
|
|
535
|
-
|
|
603
|
+
let banner =
|
|
536
604
|
colors.bold.blue("Blue Bird Server Online\n") +
|
|
537
605
|
colors.bold.cyan("App URL: ") +
|
|
538
606
|
colors.green(`${this.appUrl}`) +
|
|
539
607
|
"\n" +
|
|
540
608
|
colors.bold.cyan("Internal: ") +
|
|
541
609
|
colors.green(`${this.host}:${activePort}`) +
|
|
542
|
-
"\n"
|
|
543
|
-
|
|
544
|
-
)
|
|
610
|
+
"\n";
|
|
611
|
+
|
|
612
|
+
if (this.desktop && this.allowLan) {
|
|
613
|
+
const lanIp = getLanIp();
|
|
614
|
+
banner +=
|
|
615
|
+
colors.bold.cyan("LAN / QR: ") +
|
|
616
|
+
colors.yellow(`http://${lanIp}:${activePort}`) +
|
|
617
|
+
"\n";
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
banner += colors.gray("────────────────────────────────");
|
|
621
|
+
console.log(banner);
|
|
545
622
|
resolve(this.server);
|
|
546
623
|
};
|
|
547
624
|
|
|
548
625
|
this.server.on("error", reject);
|
|
549
626
|
|
|
550
627
|
if (this.desktop) {
|
|
551
|
-
this.
|
|
628
|
+
const bindAddress = this.allowLan ? "0.0.0.0" : "127.0.0.1";
|
|
629
|
+
this.server.listen(this.port, bindAddress, onListening);
|
|
552
630
|
} else {
|
|
553
631
|
this.server.listen(this.port, onListening);
|
|
554
632
|
}
|
|
@@ -556,29 +634,58 @@ class App {
|
|
|
556
634
|
}
|
|
557
635
|
|
|
558
636
|
/**
|
|
559
|
-
* Starts the server in Electron / Desktop mode, binding to 127.0.0.1 (
|
|
560
|
-
* and returning the server, resolved port,
|
|
637
|
+
* Starts the server in Electron / Desktop mode, binding to 127.0.0.1 (or 0.0.0.0 if allowLan is true),
|
|
638
|
+
* using a dynamic port if port is 0, and returning the server, resolved port, local URL, and optional LAN URL
|
|
639
|
+
* ready for Electron's mainWindow.loadURL(url) and mobile QR code access.
|
|
561
640
|
* @param {Object} [options={}]
|
|
641
|
+
* @param {string} [options.appPath] - Optional path from Electron app.getAppPath().
|
|
562
642
|
* @param {string} [options.userDataPath] - Optional path from Electron app.getPath("userData").
|
|
563
|
-
* @
|
|
643
|
+
* @param {boolean} [options.allowLan=false] - Whether to allow local network / mobile access (binds 0.0.0.0).
|
|
644
|
+
* @param {string} [options.host] - Optional custom host override.
|
|
645
|
+
* @param {number} [options.port] - Optional custom port override.
|
|
646
|
+
* @returns {Promise<{server: import('http').Server, port: number, url: string, lanUrl?: string, lanIp?: string}>}
|
|
564
647
|
*/
|
|
565
648
|
async runDesktop(options = {}) {
|
|
649
|
+
if (options.appPath) {
|
|
650
|
+
Config.setAppDir(options.appPath);
|
|
651
|
+
}
|
|
566
652
|
if (options.userDataPath) {
|
|
567
653
|
Config.setUserDataDir(options.userDataPath);
|
|
568
654
|
}
|
|
655
|
+
if (options.port !== undefined) {
|
|
656
|
+
this.port = options.port;
|
|
657
|
+
}
|
|
658
|
+
if (options.allowLan !== undefined) {
|
|
659
|
+
this.allowLan = Boolean(options.allowLan);
|
|
660
|
+
}
|
|
569
661
|
this.desktop = true;
|
|
570
|
-
this.host = "http://127.0.0.1";
|
|
662
|
+
this.host = options.host || (this.allowLan ? "http://0.0.0.0" : "http://127.0.0.1");
|
|
571
663
|
await this.run();
|
|
572
664
|
const port = this.getPort();
|
|
573
665
|
const url = `http://127.0.0.1:${port}`;
|
|
574
|
-
|
|
666
|
+
const result = { server: this.server, port, url };
|
|
667
|
+
if (this.allowLan) {
|
|
668
|
+
const lanIp = getLanIp();
|
|
669
|
+
result.lanIp = lanIp;
|
|
670
|
+
result.lanUrl = `http://${lanIp}:${port}`;
|
|
671
|
+
}
|
|
672
|
+
return result;
|
|
575
673
|
}
|
|
576
674
|
|
|
577
675
|
/**
|
|
578
|
-
* Closes the running HTTP server.
|
|
676
|
+
* Closes the running HTTP server and cleans up database connections.
|
|
579
677
|
* @returns {Promise<void>}
|
|
580
678
|
*/
|
|
581
|
-
close() {
|
|
679
|
+
async close() {
|
|
680
|
+
try {
|
|
681
|
+
const { Database } = await import("./database.js");
|
|
682
|
+
await Database.closeAll();
|
|
683
|
+
} catch {}
|
|
684
|
+
try {
|
|
685
|
+
const { default: Model } = await import("./model.js");
|
|
686
|
+
await Model.close();
|
|
687
|
+
} catch {}
|
|
688
|
+
|
|
582
689
|
return new Promise((resolve, reject) => {
|
|
583
690
|
if (this.server) {
|
|
584
691
|
this.server.close((err) => (err ? reject(err) : resolve()));
|
package/core/auth.js
CHANGED
package/core/cli/doctor.js
CHANGED
|
@@ -60,10 +60,9 @@ function findSampleStaticAsset() {
|
|
|
60
60
|
if (!fs.existsSync(publicDir)) return null;
|
|
61
61
|
|
|
62
62
|
const candidates = [
|
|
63
|
-
"
|
|
64
|
-
"js/
|
|
65
|
-
"favicon.ico",
|
|
66
|
-
"images/logo.png",
|
|
63
|
+
"js/tailwind.js",
|
|
64
|
+
"js/bluebird-client.js",
|
|
65
|
+
"images/favicon.ico",
|
|
67
66
|
];
|
|
68
67
|
|
|
69
68
|
for (const candidate of candidates) {
|