@seip/bluebird-js 0.0.1 → 0.1.1

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 (81) hide show
  1. package/.env_example +53 -53
  2. package/AGENTS.md +426 -403
  3. package/LICENSE +1 -1
  4. package/README.md +292 -285
  5. package/app/routes/api.js +50 -50
  6. package/app/routes/web.js +55 -53
  7. package/app/views/about.js +63 -0
  8. package/app/views/dashboard.js +147 -0
  9. package/app/views/index.js +80 -0
  10. package/app/views/layouts/main/view.js +81 -0
  11. package/app/views/legal/terms.html +19 -0
  12. package/app/views/login.js +80 -0
  13. package/core/app.d.ts +18 -18
  14. package/core/app.js +684 -644
  15. package/core/auth.d.ts +13 -13
  16. package/core/auth.js +326 -326
  17. package/core/cache.d.ts +17 -17
  18. package/core/cache.js +475 -475
  19. package/core/cli/doctor.js +287 -287
  20. package/core/cli/ecosystem.js +162 -162
  21. package/core/cli/init.js +779 -779
  22. package/core/cli/migrate.js +541 -541
  23. package/core/cli/model.js +66 -66
  24. package/core/cli/nginx.js +199 -199
  25. package/core/cli/route.js +120 -120
  26. package/core/cli/view.js +55 -36
  27. package/core/config.d.ts +3 -3
  28. package/core/config.js +111 -111
  29. package/core/csrf.d.ts +3 -3
  30. package/core/csrf.js +114 -114
  31. package/core/database.d.ts +15 -15
  32. package/core/database.js +636 -636
  33. package/core/debug.d.ts +3 -3
  34. package/core/debug.js +249 -249
  35. package/core/hash.d.ts +3 -3
  36. package/core/hash.js +194 -194
  37. package/core/html.d.ts +42 -0
  38. package/core/html.js +217 -0
  39. package/core/index.d.ts +1176 -1071
  40. package/core/logger.d.ts +3 -3
  41. package/core/logger.js +127 -124
  42. package/core/middleware.d.ts +3 -3
  43. package/core/middleware.js +28 -28
  44. package/core/model.d.ts +19 -19
  45. package/core/model.js +600 -600
  46. package/core/queue.d.ts +13 -13
  47. package/core/queue.js +172 -172
  48. package/core/ratelimit.d.ts +3 -3
  49. package/core/ratelimit.js +145 -145
  50. package/core/redis.d.ts +21 -21
  51. package/core/redis.js +230 -230
  52. package/core/render-cache.js +212 -0
  53. package/core/render.d.ts +3 -3
  54. package/core/render.js +199 -555
  55. package/core/router.d.ts +3 -3
  56. package/core/router.js +171 -171
  57. package/core/seo.d.ts +3 -3
  58. package/core/seo.js +106 -106
  59. package/core/swagger.d.ts +3 -3
  60. package/core/swagger.js +60 -60
  61. package/core/upload.d.ts +13 -13
  62. package/core/upload.js +102 -102
  63. package/core/util/colors.js +82 -82
  64. package/core/validate.d.ts +15 -15
  65. package/core/validate.js +364 -364
  66. package/core/view-loader.js +162 -0
  67. package/core/vite.d.ts +3 -3
  68. package/core/vite.js +145 -145
  69. package/core/ws.d.ts +13 -13
  70. package/core/ws.js +245 -245
  71. package/index.js +18 -18
  72. package/jsconfig.json +15 -15
  73. package/package.json +85 -86
  74. package/production.md +273 -273
  75. package/public/css/bluebird.min.css +1 -1
  76. package/public/js/bluebird-client.js +1780 -1662
  77. package/public/js/bluebird.min.js +1072 -1072
  78. package/app/views/about.html +0 -161
  79. package/app/views/dashboard.html +0 -140
  80. package/app/views/index.html +0 -150
  81. package/app/views/login.html +0 -144
@@ -0,0 +1,212 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { getRedisClient } from "./cache.js";
4
+ import Config from "./config.js";
5
+
6
+ const props = Config.props();
7
+ const DEBUG = props.debug;
8
+ const PM2_INSTANCES = parseInt(process.env.PM2_INSTANCES || "1", 10);
9
+ const RENDER_CACHE_MAX = parseInt(process.env.RENDER_CACHE_MAX || "500", 10);
10
+ const RENDER_CACHE_TTL = parseInt(process.env.RENDER_CACHE_TTL || "300", 10);
11
+
12
+ const isMultiInstance = PM2_INSTANCES > 1;
13
+
14
+ /**
15
+ * Resolves the writable render file cache directory.
16
+ * @returns {string}
17
+ */
18
+ function getFileCacheDir() {
19
+ return path.join(Config.userDataDir(), "app/cache");
20
+ }
21
+
22
+ let fileCacheEnabled = false;
23
+
24
+ if (!DEBUG) {
25
+ try {
26
+ const cacheDir = getFileCacheDir();
27
+ fs.mkdirSync(cacheDir, { recursive: true });
28
+ fs.accessSync(cacheDir, fs.constants.W_OK);
29
+ fileCacheEnabled = true;
30
+ } catch {
31
+ console.warn("[RENDER CACHE] app/cache/ is not writable — file cache disabled.");
32
+ }
33
+ }
34
+
35
+ /**
36
+ * In-memory LRU cache map.
37
+ * @type {Map<string, {value: string, expiry: number}>}
38
+ */
39
+ const lru = new Map();
40
+
41
+ /**
42
+ * Evicts the oldest LRU entry when the cap is exceeded.
43
+ */
44
+ function evictLru() {
45
+ if (lru.size >= RENDER_CACHE_MAX) {
46
+ const key = lru.keys().next().value;
47
+ if (key !== undefined) lru.delete(key);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Returns a safe filesystem key derived from the cache key string.
53
+ * @param {string} key
54
+ * @returns {string}
55
+ */
56
+ function toCacheFileName(key) {
57
+ return key.replace(/[^a-z0-9_-]/gi, "_").substring(0, 200) + ".html";
58
+ }
59
+
60
+ /**
61
+ * Reads a cached string from the file cache, respecting TTL.
62
+ * @param {string} key
63
+ * @returns {string|null}
64
+ */
65
+ function readFileCache(key) {
66
+ if (!fileCacheEnabled) return null;
67
+ const file = path.join(getFileCacheDir(), toCacheFileName(key));
68
+ try {
69
+ const stats = fs.statSync(file);
70
+ const age = (Date.now() - stats.mtimeMs) / 1000;
71
+ if (age > RENDER_CACHE_TTL) return null;
72
+ return fs.readFileSync(file, "utf8");
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Writes a string to the file cache.
80
+ * @param {string} key
81
+ * @param {string} value
82
+ */
83
+ function writeFileCache(key, value) {
84
+ if (!fileCacheEnabled) return;
85
+ try {
86
+ const file = path.join(getFileCacheDir(), toCacheFileName(key));
87
+ fs.writeFileSync(file, value, "utf8");
88
+ } catch {}
89
+ }
90
+
91
+ /**
92
+ * Multi-layer rendering cache manager.
93
+ * Provides L1 (In-Memory LRU), L2 (Redis), and L3 (File) caching layers.
94
+ */
95
+ export class RenderCache {
96
+ /**
97
+ * Looks up a cached HTML string across memory, Redis, and file cache.
98
+ * @param {string} key - Cache key.
99
+ * @returns {Promise<string|null>}
100
+ */
101
+ static async lookup(key) {
102
+ const entry = lru.get(key);
103
+ if (entry && entry.expiry > Date.now()) {
104
+ lru.delete(key);
105
+ lru.set(key, entry);
106
+ return entry.value;
107
+ }
108
+ if (entry) lru.delete(key);
109
+
110
+ if (isMultiInstance) {
111
+ const redis = getRedisClient();
112
+ if (redis) {
113
+ try {
114
+ const val = await redis.get(`render:${key}`);
115
+ if (val) {
116
+ evictLru();
117
+ lru.set(key, { value: val, expiry: Date.now() + RENDER_CACHE_TTL * 1000 });
118
+ return val;
119
+ }
120
+ } catch {}
121
+ } else {
122
+ const fromFile = readFileCache(key);
123
+ if (fromFile) {
124
+ evictLru();
125
+ lru.set(key, { value: fromFile, expiry: Date.now() + RENDER_CACHE_TTL * 1000 });
126
+ return fromFile;
127
+ }
128
+ }
129
+ }
130
+
131
+ return null;
132
+ }
133
+
134
+ /**
135
+ * Stores a rendered string in all active cache layers.
136
+ * @param {string} key - Cache key.
137
+ * @param {string} value - HTML string to store.
138
+ * @param {number} [ttl=RENDER_CACHE_TTL] - Time to live in seconds.
139
+ * @returns {Promise<void>}
140
+ */
141
+ static async store(key, value, ttl = RENDER_CACHE_TTL) {
142
+ evictLru();
143
+ lru.set(key, { value, expiry: Date.now() + ttl * 1000 });
144
+
145
+ if (isMultiInstance) {
146
+ const redis = getRedisClient();
147
+ if (redis) {
148
+ try {
149
+ await redis.set(`render:${key}`, value, { EX: ttl });
150
+ return;
151
+ } catch {}
152
+ }
153
+ writeFileCache(key, value);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Clears in-memory LRU cache.
159
+ */
160
+ static clearMemory() {
161
+ lru.clear();
162
+ }
163
+
164
+ /**
165
+ * Invalidates one or more keys across all cache layers.
166
+ * @param {string|string[]} [keys]
167
+ * @returns {Promise<void>}
168
+ */
169
+ static async invalidate(keys) {
170
+ if (!keys) {
171
+ lru.clear();
172
+ return;
173
+ }
174
+
175
+ const list = Array.isArray(keys) ? keys : [keys];
176
+ for (const key of list) {
177
+ const viewKey = `view:${key}`;
178
+ const routeKey = `route:${key}`;
179
+ const partialRouteKey = `route:partial:${key}`;
180
+ const fragmentKey = `fragment:${key}`;
181
+
182
+ lru.delete(key);
183
+ lru.delete(viewKey);
184
+ lru.delete(routeKey);
185
+ lru.delete(partialRouteKey);
186
+ lru.delete(fragmentKey);
187
+
188
+ const redis = getRedisClient();
189
+ if (redis) {
190
+ try {
191
+ await redis.del(`render:${key}`);
192
+ await redis.del(`render:${viewKey}`);
193
+ await redis.del(`render:${routeKey}`);
194
+ await redis.del(`render:${partialRouteKey}`);
195
+ await redis.del(`render:${fragmentKey}`);
196
+ } catch {}
197
+ }
198
+
199
+ if (fileCacheEnabled) {
200
+ const cacheDir = getFileCacheDir();
201
+ for (const k of [key, viewKey, routeKey, partialRouteKey, fragmentKey]) {
202
+ try {
203
+ const f = path.join(cacheDir, toCacheFileName(k));
204
+ if (fs.existsSync(f)) fs.unlinkSync(f);
205
+ } catch {}
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+
212
+ export default RenderCache;
package/core/render.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Render } from "./index.js";
2
- export default Render;
3
- export { Render };
1
+ import { Render, RenderIslandOptions, RenderHtmlOptions } from "./index.js";
2
+ export default Render;
3
+ export { Render, RenderIslandOptions, RenderHtmlOptions };