jskelet 0.2.5 → 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/AGENTS.md +132 -132
- package/CHANGELOG.md +403 -383
- package/LICENSE +21 -21
- package/bin/jskelet.mjs +103 -103
- package/docs/01-baslangic.md +285 -285
- package/docs/02-mimari.md +293 -287
- package/docs/03-routing.md +486 -480
- package/docs/04-render-ve-sablonlar.md +490 -490
- package/docs/05-islands.md +482 -482
- package/docs/06-cache.md +1231 -1209
- package/docs/07-yapilandirma.md +44 -21
- package/docs/08-build.md +366 -366
- package/docs/09-dev-araclari.md +335 -335
- package/docs/10-dagitim.md +329 -329
- package/docs/11-tasima.md +1 -0
- package/docs/12-panel-ve-oturum.md +384 -384
- package/docs/README.md +105 -105
- package/docs/en/01-getting-started.md +292 -292
- package/docs/en/02-architecture.md +311 -305
- package/docs/en/03-routing.md +503 -497
- package/docs/en/04-rendering.md +504 -504
- package/docs/en/05-islands.md +492 -492
- package/docs/en/06-caching.md +1197 -1198
- package/docs/en/07-configuration.md +1009 -986
- package/docs/en/08-build.md +383 -383
- package/docs/en/09-dev-tools.md +342 -342
- package/docs/en/10-deployment.md +332 -332
- package/docs/en/11-migration.md +360 -359
- package/docs/en/12-dashboards-and-sessions.md +392 -392
- package/docs/en/README.md +112 -112
- package/package.json +102 -102
- package/src/build/ensure-build.mjs +15 -15
- package/src/build/paths.mjs +143 -143
- package/src/build/resolve-peer.mjs +36 -36
- package/src/build/tasks/client.mjs +268 -268
- package/src/build/tasks/css.mjs +124 -124
- package/src/build/tasks/fonts.mjs +146 -146
- package/src/build/tasks/icons.mjs +224 -224
- package/src/build/tasks/images.mjs +244 -244
- package/src/build/tasks/precompress.mjs +78 -78
- package/src/client/{cache-panel → admin}/i18n.js +756 -670
- package/src/client/{cache-panel → admin}/login.html +74 -74
- package/src/client/{cache-panel → admin}/panel.css +804 -756
- package/src/client/admin/panel.html +486 -0
- package/src/client/{cache-panel → admin}/panel.js +1242 -915
- package/src/client/devtools/report.html +185 -185
- package/src/client/devtools/report.js +725 -725
- package/src/client/dom.js +95 -95
- package/src/client/form.js +192 -192
- package/src/client/index.js +35 -35
- package/src/client/registry.js +297 -297
- package/src/client/safe-image.js +91 -91
- package/src/client/store.js +36 -36
- package/src/client/swap.js +188 -188
- package/src/config/defaults.js +16 -7
- package/src/config/index.js +38 -22
- package/src/config/pattern.js +107 -107
- package/src/http/control-flow.js +71 -71
- package/src/http/cookies.js +257 -257
- package/src/http/request-cache.js +46 -46
- package/src/http/request-context.js +162 -162
- package/src/index.js +83 -83
- package/src/init.mjs +221 -221
- package/src/log.mjs +58 -0
- package/src/runtime/alias-hooks.mjs +119 -119
- package/src/runtime/register.mjs +4 -4
- package/src/server/admin/actions.js +229 -0
- package/src/server/admin/auth.js +125 -0
- package/src/server/admin/event-log.js +151 -0
- package/src/server/admin/gate.js +209 -0
- package/src/server/admin/inventory.js +188 -0
- package/src/server/admin/mount.js +56 -0
- package/src/server/admin/router.js +216 -0
- package/src/server/admin/snapshot.js +126 -0
- package/src/server/assets.js +147 -147
- package/src/server/cache-deps.js +42 -42
- package/src/server/cloudflare.js +607 -607
- package/src/server/create-app.js +295 -291
- package/src/server/data-cache.js +462 -462
- package/src/server/dev/report.js +369 -369
- package/src/server/dev/socket.js +170 -170
- package/src/server/dev/version-check.mjs +139 -139
- package/src/server/html-cache.js +817 -817
- package/src/server/metadata.js +102 -102
- package/src/server/middleware/compression.js +205 -205
- package/src/server/middleware/csrf.js +134 -134
- package/src/server/middleware/dev-gate.js +62 -62
- package/src/server/middleware/headers.js +37 -37
- package/src/server/middleware/redirects.js +32 -32
- package/src/server/middleware/static-precompressed.js +100 -100
- package/src/server/middleware/trailing-slash.js +53 -0
- package/src/server/middleware/upstream-proxy.js +141 -141
- package/src/server/prewarm.js +601 -601
- package/src/server/redis.js +569 -569
- package/src/server/router.js +128 -128
- package/src/server/status-page.js +164 -164
- package/src/server/upstream-limiter.js +376 -376
- package/src/server/upstream-tracking.js +166 -166
- package/src/start.mjs +7 -7
- package/src/templates/layout.ejs +44 -44
- package/src/version.mjs +31 -31
- package/src/views/components/loader.js +85 -85
- package/src/views/helpers/html.js +102 -102
- package/src/views/helpers/tags.js +245 -245
- package/src/client/cache-panel/panel.html +0 -308
- package/src/server/cache-panel.js +0 -759
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Admin paneli Express router'ı.
|
|
3
|
+
*/
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import express from "express";
|
|
7
|
+
import { FRAMEWORK_ROOT } from "../../config/index.js";
|
|
8
|
+
import {
|
|
9
|
+
ACTION_HEADER,
|
|
10
|
+
authenticated,
|
|
11
|
+
clearSessionCookie,
|
|
12
|
+
createSession,
|
|
13
|
+
destroySession,
|
|
14
|
+
passwordMatches,
|
|
15
|
+
sessionCookie,
|
|
16
|
+
} from "./auth.js";
|
|
17
|
+
import { clearOffender, gateMiddleware, noteFailure } from "./gate.js";
|
|
18
|
+
import { snapshot } from "./snapshot.js";
|
|
19
|
+
import { runAction } from "./actions.js";
|
|
20
|
+
import {
|
|
21
|
+
list as listLogs,
|
|
22
|
+
subscribeLive,
|
|
23
|
+
} from "./event-log.js";
|
|
24
|
+
import {
|
|
25
|
+
listExpressRoutes,
|
|
26
|
+
listRouteModules,
|
|
27
|
+
listViews,
|
|
28
|
+
routeActivity,
|
|
29
|
+
} from "./inventory.js";
|
|
30
|
+
import {
|
|
31
|
+
cloudflareConfigured,
|
|
32
|
+
fetchCacheAnalytics,
|
|
33
|
+
fetchCloudflareOverview,
|
|
34
|
+
fetchPathEdges,
|
|
35
|
+
} from "../cloudflare.js";
|
|
36
|
+
|
|
37
|
+
const PANEL_DIR = path.join(FRAMEWORK_ROOT, "src", "client", "admin");
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {() => typeof import('../../config/defaults.js').DEFAULT_ADMIN} getSettings
|
|
41
|
+
* @param {import('express').Express} app
|
|
42
|
+
* @returns {import('express').Router}
|
|
43
|
+
*/
|
|
44
|
+
export function createAdminRouter(getSettings, app) {
|
|
45
|
+
const api = express.Router();
|
|
46
|
+
|
|
47
|
+
api.use((req, res, next) => {
|
|
48
|
+
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
|
49
|
+
res.setHeader("X-Robots-Tag", "noindex, nofollow, noarchive, nosnippet");
|
|
50
|
+
res.setHeader("Referrer-Policy", "no-referrer");
|
|
51
|
+
next();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
api.use(gateMiddleware(getSettings));
|
|
55
|
+
|
|
56
|
+
// Soft page routes → aynı shell (client path ile sekme seçer).
|
|
57
|
+
// JSON API'ler ayrı `/api/*` altında: `/logs` hem sayfa hem döküm olamaz.
|
|
58
|
+
const shellPages = ["/", "/cache", "/routes", "/views", "/logs", "/system"];
|
|
59
|
+
for (const page of shellPages) {
|
|
60
|
+
api.get(page, (req, res) => {
|
|
61
|
+
const settings = getSettings();
|
|
62
|
+
const bare = req.originalUrl.split("?")[0];
|
|
63
|
+
if (page === "/" && !bare.endsWith("/")) {
|
|
64
|
+
res.redirect(302, `${settings.basePath}/`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
res.type("html");
|
|
69
|
+
sendFile(res, authenticated(req) ? "panel.html" : "login.html");
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
api.get("/panel.css", (req, res) => {
|
|
74
|
+
res.type("text/css");
|
|
75
|
+
sendFile(res, "panel.css");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
api.get("/i18n.js", (req, res) => {
|
|
79
|
+
res.type("application/javascript");
|
|
80
|
+
sendFile(res, "i18n.js");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
api.get("/logo.png", (req, res) => {
|
|
84
|
+
res.type("image/png");
|
|
85
|
+
fs.createReadStream(path.join(FRAMEWORK_ROOT, "src", "logo.png")).pipe(res);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
api.post("/login", express.json({ limit: "4kb" }), (req, res) => {
|
|
89
|
+
const settings = getSettings();
|
|
90
|
+
const password = typeof req.body?.password === "string" ? req.body.password : "";
|
|
91
|
+
|
|
92
|
+
if (!passwordMatches(password)) {
|
|
93
|
+
noteFailure(req, password ? "wrong password" : "empty password", settings);
|
|
94
|
+
res.status(401).json({ ok: false, message: "Invalid password" });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const { token, maxAge } = createSession(settings.sessionHours);
|
|
99
|
+
clearOffender(req);
|
|
100
|
+
res.setHeader("Set-Cookie", sessionCookie(settings.basePath, token, maxAge));
|
|
101
|
+
res.json({ ok: true });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
api.use((req, res, next) => {
|
|
105
|
+
if (authenticated(req)) return next();
|
|
106
|
+
|
|
107
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
108
|
+
noteFailure(req, `unauthenticated ${req.method} ${req.path}`, getSettings());
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
res.status(404).type("text/plain").send("Not Found");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
api.post("/logout", (req, res) => {
|
|
115
|
+
const settings = getSettings();
|
|
116
|
+
destroySession(req);
|
|
117
|
+
res.setHeader("Set-Cookie", clearSessionCookie(settings.basePath));
|
|
118
|
+
res.json({ ok: true });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
api.get("/panel.js", (req, res) => {
|
|
122
|
+
res.type("application/javascript");
|
|
123
|
+
sendFile(res, "panel.js");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
api.get("/data", async (req, res) => {
|
|
127
|
+
const query = typeof req.query.q === "string" ? req.query.q : "";
|
|
128
|
+
res.json(await snapshot(query));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
api.get("/api/routes", (req, res) => {
|
|
132
|
+
res.json({
|
|
133
|
+
routes: listExpressRoutes(app),
|
|
134
|
+
modules: listRouteModules(),
|
|
135
|
+
activity: routeActivity(),
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
api.get("/api/views", (req, res) => {
|
|
140
|
+
res.json({ views: listViews() });
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
api.get("/api/logs", (req, res) => {
|
|
144
|
+
const after = Number(req.query.after) || 0;
|
|
145
|
+
const limit = Number(req.query.limit) || 200;
|
|
146
|
+
res.json({ entries: listLogs(after, limit) });
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
api.get("/api/logs/stream", (req, res) => {
|
|
150
|
+
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
|
|
151
|
+
res.setHeader("Cache-Control", "no-store, no-cache");
|
|
152
|
+
res.setHeader("Connection", "keep-alive");
|
|
153
|
+
res.flushHeaders?.();
|
|
154
|
+
|
|
155
|
+
const replay = listLogs(0, 100);
|
|
156
|
+
for (const entry of replay) {
|
|
157
|
+
res.write(`data: ${JSON.stringify(entry)}\n\n`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const unsubscribe = subscribeLive((entry) => {
|
|
161
|
+
res.write(`data: ${JSON.stringify(entry)}\n\n`);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const heartbeat = setInterval(() => {
|
|
165
|
+
res.write(": ping\n\n");
|
|
166
|
+
}, 15000);
|
|
167
|
+
|
|
168
|
+
req.on("close", () => {
|
|
169
|
+
clearInterval(heartbeat);
|
|
170
|
+
unsubscribe();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
api.get("/cloudflare", async (req, res) => {
|
|
175
|
+
res.json(await fetchCloudflareOverview({ force: req.query.force === "1" }));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
api.post("/cloudflare/analytics", express.json({ limit: "8kb" }), async (req, res) => {
|
|
179
|
+
if (!cloudflareConfigured()) {
|
|
180
|
+
res.json({ ok: false, error: "not configured" });
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const hours = Number(req.body?.hours) || undefined;
|
|
185
|
+
const pathValue = typeof req.body?.path === "string" ? req.body.path.trim() : "";
|
|
186
|
+
res.json(
|
|
187
|
+
pathValue
|
|
188
|
+
? await fetchPathEdges({ path: pathValue, hours })
|
|
189
|
+
: await fetchCacheAnalytics({ hours }),
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
api.post("/action", express.json({ limit: "64kb" }), async (req, res) => {
|
|
194
|
+
if (req.get(ACTION_HEADER) !== "1") {
|
|
195
|
+
res.status(404).type("text/plain").send("Not Found");
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
res.json(await runAction(req.body ?? {}, req));
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.error("[admin] action failed", error);
|
|
203
|
+
res.status(500).json({ ok: false, message: "Action failed, see server logs" });
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return api;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @param {import('express').Response} res
|
|
212
|
+
* @param {string} name
|
|
213
|
+
*/
|
|
214
|
+
function sendFile(res, name) {
|
|
215
|
+
fs.createReadStream(path.join(PANEL_DIR, name)).pipe(res);
|
|
216
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Admin paneli cache / host / process dökümü.
|
|
3
|
+
*
|
|
4
|
+
* HTML gövdesi ve veri değerleri dönmez — yalnızca meta.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
import { getConfig } from "../../config/index.js";
|
|
10
|
+
import {
|
|
11
|
+
FRAMEWORK_HOMEPAGE,
|
|
12
|
+
FRAMEWORK_LICENSE,
|
|
13
|
+
FRAMEWORK_NODE_RANGE,
|
|
14
|
+
FRAMEWORK_VERSION,
|
|
15
|
+
} from "../../version.mjs";
|
|
16
|
+
import {
|
|
17
|
+
getHtmlCacheEntries,
|
|
18
|
+
getHtmlCacheSize,
|
|
19
|
+
} from "../html-cache.js";
|
|
20
|
+
import {
|
|
21
|
+
getDataCacheEntries,
|
|
22
|
+
getDataCacheSize,
|
|
23
|
+
} from "../data-cache.js";
|
|
24
|
+
import { getRedisDetails, getRedisStatus } from "../redis.js";
|
|
25
|
+
import { getUpstreamLimiterStatus } from "../upstream-limiter.js";
|
|
26
|
+
import { getCloudflareStatus } from "../cloudflare.js";
|
|
27
|
+
import { prewarmProgress } from "../prewarm.js";
|
|
28
|
+
|
|
29
|
+
/** Listelerin üst sınırı: veri önbelleğinde on binlerce anahtar olabiliyor. */
|
|
30
|
+
const MAX_LISTED = 500;
|
|
31
|
+
|
|
32
|
+
export const BOOT_ID = `${Date.now()}-${process.pid}`;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} query Anahtar filtresi (boş → filtre yok).
|
|
36
|
+
* @returns {Promise<object>}
|
|
37
|
+
*/
|
|
38
|
+
export async function snapshot(query) {
|
|
39
|
+
const term = query.trim().toLowerCase();
|
|
40
|
+
/** @param {{ key: string }} entry */
|
|
41
|
+
const matches = (entry) => !term || entry.key.toLowerCase().includes(term);
|
|
42
|
+
|
|
43
|
+
const html = getHtmlCacheEntries();
|
|
44
|
+
const data = getDataCacheEntries();
|
|
45
|
+
|
|
46
|
+
const htmlMatched = html.filter(matches);
|
|
47
|
+
const dataMatched = data.filter(matches);
|
|
48
|
+
const usage = process.memoryUsage();
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
boot: BOOT_ID,
|
|
52
|
+
generatedAt: Date.now(),
|
|
53
|
+
release: {
|
|
54
|
+
version: FRAMEWORK_VERSION,
|
|
55
|
+
license: FRAMEWORK_LICENSE,
|
|
56
|
+
node: FRAMEWORK_NODE_RANGE,
|
|
57
|
+
homepage: FRAMEWORK_HOMEPAGE,
|
|
58
|
+
},
|
|
59
|
+
host: await hostStatus(),
|
|
60
|
+
process: {
|
|
61
|
+
pid: process.pid,
|
|
62
|
+
node: process.version,
|
|
63
|
+
uptime: process.uptime(),
|
|
64
|
+
env: process.env.NODE_ENV ?? "production",
|
|
65
|
+
memory: { rss: usage.rss, heapUsed: usage.heapUsed },
|
|
66
|
+
},
|
|
67
|
+
html: {
|
|
68
|
+
size: getHtmlCacheSize(),
|
|
69
|
+
maxEntries: getConfig().htmlMaxEntries,
|
|
70
|
+
bytes: html.reduce((total, entry) => total + entry.bytes, 0),
|
|
71
|
+
stale: html.filter((entry) => entry.stale).length,
|
|
72
|
+
matched: htmlMatched.length,
|
|
73
|
+
entries: htmlMatched
|
|
74
|
+
.sort((a, b) => b.bytes - a.bytes)
|
|
75
|
+
.slice(0, MAX_LISTED)
|
|
76
|
+
.map((entry) => ({
|
|
77
|
+
...entry,
|
|
78
|
+
url: entry.key.endsWith("?") ? entry.key.slice(0, -1) : entry.key,
|
|
79
|
+
})),
|
|
80
|
+
},
|
|
81
|
+
data: {
|
|
82
|
+
size: getDataCacheSize(),
|
|
83
|
+
maxEntries: Number(getConfig().data?.maxEntries) || 0,
|
|
84
|
+
stale: data.filter((entry) => entry.stale).length,
|
|
85
|
+
matched: dataMatched.length,
|
|
86
|
+
entries: dataMatched
|
|
87
|
+
.sort((a, b) => a.expiresIn - b.expiresIn)
|
|
88
|
+
.slice(0, MAX_LISTED),
|
|
89
|
+
},
|
|
90
|
+
redis: { ...getRedisStatus(), ...getRedisDetails() },
|
|
91
|
+
cloudflare: getCloudflareStatus(),
|
|
92
|
+
upstream: getUpstreamLimiterStatus(),
|
|
93
|
+
prewarm: { ...prewarmProgress },
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @returns {Promise<object>}
|
|
99
|
+
*/
|
|
100
|
+
async function hostStatus() {
|
|
101
|
+
const total = os.totalmem();
|
|
102
|
+
const free = os.freemem();
|
|
103
|
+
|
|
104
|
+
/** @type {{ path: string, total: number, free: number } | null} */
|
|
105
|
+
let disk = null;
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const root = getConfig().root;
|
|
109
|
+
const stats = await fs.promises.statfs(root);
|
|
110
|
+
disk = {
|
|
111
|
+
path: root,
|
|
112
|
+
total: stats.blocks * stats.bsize,
|
|
113
|
+
free: stats.bavail * stats.bsize,
|
|
114
|
+
};
|
|
115
|
+
} catch {
|
|
116
|
+
disk = null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
platform: `${os.platform()} ${os.arch()}`,
|
|
121
|
+
cpus: os.cpus().length,
|
|
122
|
+
load: os.loadavg()[0],
|
|
123
|
+
memory: { total, free, used: total - free },
|
|
124
|
+
disk,
|
|
125
|
+
};
|
|
126
|
+
}
|
package/src/server/assets.js
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Build çıktısı manifest'ine erişim: mantıksal isim → hash'li public URL.
|
|
3
|
-
*
|
|
4
|
-
* Build çalışmadıysa uygulama yine ayağa kalkar. `asset()` `/assets/<isim>`
|
|
5
|
-
* döner ve `hasAsset()` false olur; layout stylesheet/script etiketlerini hiç
|
|
6
|
-
* basmaz. Böylece `jskelet build` unutulduğunda hata yerine stilsiz ama
|
|
7
|
-
* çalışan bir sayfa görürsünüz.
|
|
8
|
-
*/
|
|
9
|
-
import fs from "node:fs";
|
|
10
|
-
import path from "node:path";
|
|
11
|
-
import process from "node:process";
|
|
12
|
-
import { getConfig } from "../config/index.js";
|
|
13
|
-
|
|
14
|
-
const isDev = process.env.NODE_ENV === "development";
|
|
15
|
-
|
|
16
|
-
/** @type {{ manifest: Record<string, string> } | null} */
|
|
17
|
-
let cached = null;
|
|
18
|
-
let warned = false;
|
|
19
|
-
|
|
20
|
-
function load() {
|
|
21
|
-
const { generated } = getConfig().dirs;
|
|
22
|
-
|
|
23
|
-
/** @type {Record<string, string>} */
|
|
24
|
-
let manifest = {};
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
manifest = JSON.parse(
|
|
28
|
-
fs.readFileSync(path.join(generated, "manifest.json"), "utf8"),
|
|
29
|
-
);
|
|
30
|
-
} catch {
|
|
31
|
-
if (!warned) {
|
|
32
|
-
warned = true;
|
|
33
|
-
console.warn("[assets] no manifest — run `jskelet build`.");
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return { manifest };
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function state() {
|
|
41
|
-
// Dev'de her istekte yeniden okunur (watch build hash'leri değiştirir);
|
|
42
|
-
// prod'da bir kez.
|
|
43
|
-
if (isDev || !cached) cached = load();
|
|
44
|
-
return cached;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @param {string} name Örn. "app.css", "main.js", "sprite.svg"
|
|
49
|
-
* @returns {string}
|
|
50
|
-
*/
|
|
51
|
-
export function asset(name) {
|
|
52
|
-
return state().manifest[name] ?? `/assets/${name}`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @param {string} name
|
|
57
|
-
* @returns {boolean}
|
|
58
|
-
*/
|
|
59
|
-
export function hasAsset(name) {
|
|
60
|
-
return Boolean(state().manifest[name]);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** @type {string | null} */
|
|
64
|
-
let buildId = null;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Bu build'i tanımlayan kısa kimlik (`build.json` → `id`).
|
|
68
|
-
*
|
|
69
|
-
* Paylaşımlı önbellek anahtarlarının isim alanı bu: saklanan HTML hash'li
|
|
70
|
-
* varlık yollarını gömdüğü için bir deploy'dan sonra eski HTML **geçersizdir**.
|
|
71
|
-
*
|
|
72
|
-
* Build çalışmadıysa `"dev"` döner — build çıktısı olmadan da ayağa kalkma
|
|
73
|
-
* kuralı burada da geçerli.
|
|
74
|
-
*
|
|
75
|
-
* @returns {string}
|
|
76
|
-
*/
|
|
77
|
-
export function getBuildId() {
|
|
78
|
-
if (!isDev && buildId) return buildId;
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
const file = path.join(getConfig().dirs.generated, "build.json");
|
|
82
|
-
const id = JSON.parse(fs.readFileSync(file, "utf8")).id;
|
|
83
|
-
buildId = typeof id === "string" && id ? id : "dev";
|
|
84
|
-
} catch {
|
|
85
|
-
buildId = "dev";
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return buildId;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* @typedef {{ width: number, height: number,
|
|
93
|
-
* variants: { width: number, url: string }[] }} OptimizedImage
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
/** @type {Record<string, OptimizedImage> | null} */
|
|
97
|
-
let images = null;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* `build/tasks/images.mjs` çıktısı: kaynak yolundan webp varyantlarına.
|
|
101
|
-
* Build çalışmadıysa boş kalır ve görseller orijinalleriyle servis edilir.
|
|
102
|
-
*
|
|
103
|
-
* @param {string} [src] Örn. "/hero.png"
|
|
104
|
-
* @returns {OptimizedImage | undefined}
|
|
105
|
-
*/
|
|
106
|
-
export function optimizedImage(src) {
|
|
107
|
-
if (isDev || !images) {
|
|
108
|
-
try {
|
|
109
|
-
const file = path.join(getConfig().dirs.generated, "images.json");
|
|
110
|
-
images = JSON.parse(fs.readFileSync(file, "utf8")).images ?? {};
|
|
111
|
-
} catch {
|
|
112
|
-
images = {};
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return src ? images[src] : undefined;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** @type {{ url: string, ids: Set<string> } | null} */
|
|
120
|
-
let spriteIds = null;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Sprite'taki sembol kimlikleri. Sprite yalnızca kaynakta **statik olarak**
|
|
124
|
-
* görülen ikon adlarını içerir; adı çalışma anında hesaplanan bir `icon()`
|
|
125
|
-
* çağrısı eksik sembole işaret ederse ekranda sessizce boşluk kalır. Dev'de
|
|
126
|
-
* `icon()` bu kümeye bakıp uyarır.
|
|
127
|
-
*
|
|
128
|
-
* @returns {Set<string>}
|
|
129
|
-
*/
|
|
130
|
-
export function getSpriteIds() {
|
|
131
|
-
const url = asset("sprite.svg");
|
|
132
|
-
if (spriteIds?.url === url) return spriteIds.ids;
|
|
133
|
-
|
|
134
|
-
/** @type {Set<string>} */
|
|
135
|
-
const ids = new Set();
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
const file = path.join(getConfig().dirs.public, url.replace(/^\//, ""));
|
|
139
|
-
const svg = fs.readFileSync(file, "utf8");
|
|
140
|
-
for (const match of svg.matchAll(/<symbol id="([^"]+)"/g)) ids.add(match[1]);
|
|
141
|
-
} catch {
|
|
142
|
-
// Sprite okunamazsa uyarı üretilmez; asıl akış etkilenmez.
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
spriteIds = { url, ids };
|
|
146
|
-
return ids;
|
|
147
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Build çıktısı manifest'ine erişim: mantıksal isim → hash'li public URL.
|
|
3
|
+
*
|
|
4
|
+
* Build çalışmadıysa uygulama yine ayağa kalkar. `asset()` `/assets/<isim>`
|
|
5
|
+
* döner ve `hasAsset()` false olur; layout stylesheet/script etiketlerini hiç
|
|
6
|
+
* basmaz. Böylece `jskelet build` unutulduğunda hata yerine stilsiz ama
|
|
7
|
+
* çalışan bir sayfa görürsünüz.
|
|
8
|
+
*/
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import process from "node:process";
|
|
12
|
+
import { getConfig } from "../config/index.js";
|
|
13
|
+
|
|
14
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
15
|
+
|
|
16
|
+
/** @type {{ manifest: Record<string, string> } | null} */
|
|
17
|
+
let cached = null;
|
|
18
|
+
let warned = false;
|
|
19
|
+
|
|
20
|
+
function load() {
|
|
21
|
+
const { generated } = getConfig().dirs;
|
|
22
|
+
|
|
23
|
+
/** @type {Record<string, string>} */
|
|
24
|
+
let manifest = {};
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
manifest = JSON.parse(
|
|
28
|
+
fs.readFileSync(path.join(generated, "manifest.json"), "utf8"),
|
|
29
|
+
);
|
|
30
|
+
} catch {
|
|
31
|
+
if (!warned) {
|
|
32
|
+
warned = true;
|
|
33
|
+
console.warn("[assets] no manifest — run `jskelet build`.");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { manifest };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function state() {
|
|
41
|
+
// Dev'de her istekte yeniden okunur (watch build hash'leri değiştirir);
|
|
42
|
+
// prod'da bir kez.
|
|
43
|
+
if (isDev || !cached) cached = load();
|
|
44
|
+
return cached;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} name Örn. "app.css", "main.js", "sprite.svg"
|
|
49
|
+
* @returns {string}
|
|
50
|
+
*/
|
|
51
|
+
export function asset(name) {
|
|
52
|
+
return state().manifest[name] ?? `/assets/${name}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {string} name
|
|
57
|
+
* @returns {boolean}
|
|
58
|
+
*/
|
|
59
|
+
export function hasAsset(name) {
|
|
60
|
+
return Boolean(state().manifest[name]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @type {string | null} */
|
|
64
|
+
let buildId = null;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Bu build'i tanımlayan kısa kimlik (`build.json` → `id`).
|
|
68
|
+
*
|
|
69
|
+
* Paylaşımlı önbellek anahtarlarının isim alanı bu: saklanan HTML hash'li
|
|
70
|
+
* varlık yollarını gömdüğü için bir deploy'dan sonra eski HTML **geçersizdir**.
|
|
71
|
+
*
|
|
72
|
+
* Build çalışmadıysa `"dev"` döner — build çıktısı olmadan da ayağa kalkma
|
|
73
|
+
* kuralı burada da geçerli.
|
|
74
|
+
*
|
|
75
|
+
* @returns {string}
|
|
76
|
+
*/
|
|
77
|
+
export function getBuildId() {
|
|
78
|
+
if (!isDev && buildId) return buildId;
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const file = path.join(getConfig().dirs.generated, "build.json");
|
|
82
|
+
const id = JSON.parse(fs.readFileSync(file, "utf8")).id;
|
|
83
|
+
buildId = typeof id === "string" && id ? id : "dev";
|
|
84
|
+
} catch {
|
|
85
|
+
buildId = "dev";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return buildId;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @typedef {{ width: number, height: number,
|
|
93
|
+
* variants: { width: number, url: string }[] }} OptimizedImage
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/** @type {Record<string, OptimizedImage> | null} */
|
|
97
|
+
let images = null;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* `build/tasks/images.mjs` çıktısı: kaynak yolundan webp varyantlarına.
|
|
101
|
+
* Build çalışmadıysa boş kalır ve görseller orijinalleriyle servis edilir.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} [src] Örn. "/hero.png"
|
|
104
|
+
* @returns {OptimizedImage | undefined}
|
|
105
|
+
*/
|
|
106
|
+
export function optimizedImage(src) {
|
|
107
|
+
if (isDev || !images) {
|
|
108
|
+
try {
|
|
109
|
+
const file = path.join(getConfig().dirs.generated, "images.json");
|
|
110
|
+
images = JSON.parse(fs.readFileSync(file, "utf8")).images ?? {};
|
|
111
|
+
} catch {
|
|
112
|
+
images = {};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return src ? images[src] : undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @type {{ url: string, ids: Set<string> } | null} */
|
|
120
|
+
let spriteIds = null;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sprite'taki sembol kimlikleri. Sprite yalnızca kaynakta **statik olarak**
|
|
124
|
+
* görülen ikon adlarını içerir; adı çalışma anında hesaplanan bir `icon()`
|
|
125
|
+
* çağrısı eksik sembole işaret ederse ekranda sessizce boşluk kalır. Dev'de
|
|
126
|
+
* `icon()` bu kümeye bakıp uyarır.
|
|
127
|
+
*
|
|
128
|
+
* @returns {Set<string>}
|
|
129
|
+
*/
|
|
130
|
+
export function getSpriteIds() {
|
|
131
|
+
const url = asset("sprite.svg");
|
|
132
|
+
if (spriteIds?.url === url) return spriteIds.ids;
|
|
133
|
+
|
|
134
|
+
/** @type {Set<string>} */
|
|
135
|
+
const ids = new Set();
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const file = path.join(getConfig().dirs.public, url.replace(/^\//, ""));
|
|
139
|
+
const svg = fs.readFileSync(file, "utf8");
|
|
140
|
+
for (const match of svg.matchAll(/<symbol id="([^"]+)"/g)) ids.add(match[1]);
|
|
141
|
+
} catch {
|
|
142
|
+
// Sprite okunamazsa uyarı üretilmez; asıl akış etkilenmez.
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
spriteIds = { url, ids };
|
|
146
|
+
return ids;
|
|
147
|
+
}
|