@jant/core 1.1.0 → 1.2.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/bin/lib/site-snapshot.js +10 -0
- package/dist/{app-CKQ9PqAr.js → app-CbOZ2Irh.js} +7988 -5166
- package/dist/client/.vite/manifest.json +47 -47
- package/dist/client/_assets/chunks/{create-editor-BQkrw8Ax.js → create-editor-hWapNE40.js} +25 -25
- package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
- package/dist/client/_assets/chunks/{settings-paths-B0bNUaih.js → settings-paths-BBVT8zKj.js} +1 -1
- package/dist/client/_assets/chunks/{slug-format-CYmQE5ln.js → slug-format-BuCPjSrB.js} +1 -1
- package/dist/client/_assets/chunks/sortable-list-DJXwkaOv.js +2 -0
- package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
- package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
- package/dist/client/_assets/{client-Dp46QSht.js → client-BO8GOFfT.js} +12 -12
- package/dist/client/_assets/client-D8Rrsd73.css +2 -0
- package/dist/client/_assets/{client-auth-CG55rWb5.js → client-auth-CsUGM718.js} +1 -1
- package/dist/client/_assets/client-author-QPm1whbO.css +2 -0
- package/dist/client/_assets/client-compose-BB4U7Wz0.js +2004 -0
- package/dist/client/_assets/{client-manage-DnYdQAhf.js → client-manage-CI9-eh-8.js} +1 -1
- package/dist/client/_assets/{client-settings-CHgsIqXJ.js → client-settings-BnGPuqwc.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/node.js +3 -2
- package/package.json +1 -1
- package/src/app.tsx +9 -0
- package/src/client/components/compose-types.ts +1 -0
- package/src/client/components/jant-compose-dialog.ts +1 -0
- package/src/client/components/jant-compose-editor.ts +30 -0
- package/src/client/components/jant-compose-fullscreen.ts +1 -0
- package/src/client/compose-bridge.ts +61 -1
- package/src/client/feed-video-player.ts +6 -0
- package/src/client/tiptap/bubble-menu.ts +25 -2
- package/src/client/tiptap/create-editor.ts +2 -0
- package/src/client/tiptap/extensions.ts +4 -0
- package/src/client/video-processor.ts +106 -52
- package/src/client/view-confirm.ts +108 -0
- package/src/client.ts +1 -0
- package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
- package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
- package/src/db/migrations/meta/0035_snapshot.json +2877 -0
- package/src/db/migrations/meta/0036_snapshot.json +2924 -0
- package/src/db/migrations/meta/_journal.json +14 -0
- package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
- package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
- package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
- package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
- package/src/db/migrations/pg/meta/_journal.json +14 -0
- package/src/db/pg/schema.ts +40 -0
- package/src/db/schema.ts +61 -0
- package/src/i18n/locales/public/en.po +5 -0
- package/src/i18n/locales/public/en.ts +1 -1
- package/src/i18n/locales/public/zh-Hans.po +5 -0
- package/src/i18n/locales/public/zh-Hans.ts +1 -1
- package/src/i18n/locales/public/zh-Hant.po +5 -0
- package/src/i18n/locales/public/zh-Hant.ts +1 -1
- package/src/i18n/locales/settings/en.po +149 -0
- package/src/i18n/locales/settings/en.ts +1 -1
- package/src/i18n/locales/settings/zh-Hans.po +149 -0
- package/src/i18n/locales/settings/zh-Hans.ts +1 -1
- package/src/i18n/locales/settings/zh-Hant.po +149 -0
- package/src/i18n/locales/settings/zh-Hant.ts +1 -1
- package/src/lib/env.ts +20 -0
- package/src/lib/github-app-state.ts +1 -1
- package/src/lib/github-app.ts +183 -1
- package/src/lib/markdown-manager.ts +129 -1
- package/src/lib/post-meta.ts +13 -3
- package/src/lib/referrer-aliases.ts +311 -0
- package/src/lib/render.tsx +10 -0
- package/src/lib/resolve-config.ts +14 -0
- package/src/lib/summary.ts +152 -54
- package/src/lib/tiptap-render.ts +1 -0
- package/src/lib/upload.ts +21 -1
- package/src/lib/video-playback.ts +55 -0
- package/src/lib/view-buffer.ts +201 -0
- package/src/lib/view-counting.ts +539 -0
- package/src/lib/view-token.ts +177 -0
- package/src/lib/view.ts +13 -1
- package/src/middleware/view-counter.ts +140 -0
- package/src/node/runtime.ts +4 -0
- package/src/preset.css +31 -14
- package/src/routes/api/media.ts +21 -3
- package/src/routes/dash/settings.tsx +433 -168
- package/src/routes/pages/archive.tsx +1 -0
- package/src/routes/pages/collection.tsx +6 -0
- package/src/routes/pages/collections.tsx +1 -0
- package/src/routes/pages/featured.tsx +1 -0
- package/src/routes/pages/home.tsx +1 -0
- package/src/routes/pages/page.tsx +3 -0
- package/src/routes/pages/smart-collection.tsx +2 -0
- package/src/routes/pages/subscribe.tsx +1 -0
- package/src/routes/view-confirm.ts +63 -0
- package/src/services/export-theme/assets/client-site.js +1 -1
- package/src/services/export-theme/styles/main.css +39 -21
- package/src/services/export.ts +2 -0
- package/src/services/index.ts +17 -0
- package/src/services/mcp.ts +31 -1
- package/src/services/media.ts +63 -0
- package/src/services/stats.ts +1130 -0
- package/src/styles/components-author.css +177 -0
- package/src/styles/components.css +11 -3
- package/src/styles/tokens.css +65 -30
- package/src/styles/ui.css +56 -13
- package/src/types/app-context.ts +9 -0
- package/src/types/bindings.ts +5 -0
- package/src/types/config.ts +54 -0
- package/src/types/views.ts +6 -0
- package/src/ui/compose/ComposeDialog.tsx +6 -0
- package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
- package/src/ui/dash/settings/StatsContent.tsx +855 -0
- package/src/ui/font-themes.ts +39 -6
- package/src/ui/layouts/BaseLayout.tsx +4 -2
- package/src/ui/layouts/SiteLayout.tsx +8 -3
- package/src/ui/shared/MediaGallery.tsx +19 -3
- package/dist/client/_assets/chunks/footnote-rail-B08scyhW.js +0 -2
- package/dist/client/_assets/chunks/sortable-list-t11Ma1Ek.js +0 -2
- package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-DZl-yjas.js +0 -1
- package/dist/client/_assets/client-B0-s9sqx.css +0 -2
- package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
- package/dist/client/_assets/client-compose-DkUTuavR.js +0 -2004
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token a page carries so the browser that opened it can confirm it was
|
|
3
|
+
* seen.
|
|
4
|
+
*
|
|
5
|
+
* A request alone cannot tell a reader from a crawler that runs a real
|
|
6
|
+
* browser: the headers are the same. What differs is what happens after the
|
|
7
|
+
* HTML arrives. A reader keeps the page in front of them, scrolls, taps; a
|
|
8
|
+
* crawler closes it in a second. So the page is counted only once the
|
|
9
|
+
* browser says it was seen, and this token is how the page says which view
|
|
10
|
+
* that was without the server keeping anything in the meantime.
|
|
11
|
+
*
|
|
12
|
+
* The server decides everything about the view at request time, as before
|
|
13
|
+
* (which page, where the reader came from), seals it into the token with an
|
|
14
|
+
* HMAC over `AUTH_SECRET`, and writes the token into the page. The browser
|
|
15
|
+
* posts the token back after the page has been visible for a while or on the
|
|
16
|
+
* first interaction. The endpoint verifies the signature, checks the token is
|
|
17
|
+
* fresh and is this site's, and counts the view with the day and hour of
|
|
18
|
+
* that moment.
|
|
19
|
+
*
|
|
20
|
+
* The time in the token is a ten-minute slot, not a timestamp, so every
|
|
21
|
+
* response for the same page and source within a slot carries the same
|
|
22
|
+
* token: the HTML stays byte-identical across readers, as it was. A token is
|
|
23
|
+
* accepted for the current slot and the three before it, half an hour or so,
|
|
24
|
+
* long enough to read a page slowly, short enough that a harvested token is
|
|
25
|
+
* worthless the next day.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { hmacHex, timingSafeEqualText } from "./crypto.js";
|
|
29
|
+
|
|
30
|
+
/** The `<meta name>` the page carries the token under. */
|
|
31
|
+
export const VIEW_TOKEN_META_NAME = "jant-view";
|
|
32
|
+
|
|
33
|
+
/** Where the browser posts the token. Under `/_`, with the other reserved paths. */
|
|
34
|
+
export const VIEW_CONFIRM_PATH = "/_/seen";
|
|
35
|
+
|
|
36
|
+
/** Seconds in a token's time slot. */
|
|
37
|
+
export const VIEW_TOKEN_SLOT_SECONDS = 600;
|
|
38
|
+
|
|
39
|
+
/** Slots a token is accepted for: the current one and the ones before it. */
|
|
40
|
+
export const VIEW_TOKEN_SLOTS_ACCEPTED = 4;
|
|
41
|
+
|
|
42
|
+
/** Hex characters of the HMAC kept: 128 bits. */
|
|
43
|
+
const SIGNATURE_LENGTH = 32;
|
|
44
|
+
|
|
45
|
+
/** Longer than any token this module writes; a longer one is not ours. */
|
|
46
|
+
const TOKEN_MAX_LENGTH = 2048;
|
|
47
|
+
|
|
48
|
+
/** What a token says about the view. */
|
|
49
|
+
export interface ViewTokenClaims {
|
|
50
|
+
siteId: string;
|
|
51
|
+
/** The counter key of the page: an id or a path. */
|
|
52
|
+
page: string;
|
|
53
|
+
/** The referrer key: a host or tag, the site itself, or `""`. */
|
|
54
|
+
referrer: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function toBase64Url(text: string): string {
|
|
58
|
+
const bytes = new TextEncoder().encode(text);
|
|
59
|
+
let binary = "";
|
|
60
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
61
|
+
return btoa(binary)
|
|
62
|
+
.replace(/\+/g, "-")
|
|
63
|
+
.replace(/\//g, "_")
|
|
64
|
+
.replace(/=+$/, "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function fromBase64Url(text: string): string | null {
|
|
68
|
+
try {
|
|
69
|
+
const padded =
|
|
70
|
+
text.replace(/-/g, "+").replace(/_/g, "/") +
|
|
71
|
+
"=".repeat((4 - (text.length % 4)) % 4);
|
|
72
|
+
const binary = atob(padded);
|
|
73
|
+
const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
74
|
+
// Malformed UTF-8 decodes to replacement characters and fails the JSON
|
|
75
|
+
// and shape checks that follow.
|
|
76
|
+
return new TextDecoder().decode(bytes);
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The time slot a moment falls in.
|
|
84
|
+
*
|
|
85
|
+
* @param timestamp - Unix timestamp in seconds
|
|
86
|
+
* @returns The slot number
|
|
87
|
+
* @example
|
|
88
|
+
* viewTokenSlot(1_762_560_000); // 2937600
|
|
89
|
+
*/
|
|
90
|
+
export function viewTokenSlot(timestamp: number): number {
|
|
91
|
+
return Math.floor(timestamp / VIEW_TOKEN_SLOT_SECONDS);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function signature(secret: string, payload: string): Promise<string> {
|
|
95
|
+
const hex = await hmacHex(secret, `jant-view:${payload}`);
|
|
96
|
+
return hex.slice(0, SIGNATURE_LENGTH);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The token for a view, to be written into the page.
|
|
101
|
+
*
|
|
102
|
+
* @param secret - The deployment's `AUTH_SECRET`
|
|
103
|
+
* @param claims - Which page, and where the reader came from
|
|
104
|
+
* @param timestamp - Unix timestamp in seconds, now
|
|
105
|
+
* @returns `payload.signature`, both URL-safe
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const token = await signViewToken(secret, { siteId, page: post.id, referrer: "" }, now());
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export async function signViewToken(
|
|
112
|
+
secret: string,
|
|
113
|
+
claims: ViewTokenClaims,
|
|
114
|
+
timestamp: number,
|
|
115
|
+
): Promise<string> {
|
|
116
|
+
const payload = toBase64Url(
|
|
117
|
+
JSON.stringify({
|
|
118
|
+
s: claims.siteId,
|
|
119
|
+
p: claims.page,
|
|
120
|
+
r: claims.referrer,
|
|
121
|
+
t: viewTokenSlot(timestamp),
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
124
|
+
return `${payload}.${await signature(secret, payload)}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* What a token posted back says, if it is ours and still fresh.
|
|
129
|
+
*
|
|
130
|
+
* @param secret - The deployment's `AUTH_SECRET`
|
|
131
|
+
* @param token - What the browser posted
|
|
132
|
+
* @param timestamp - Unix timestamp in seconds, now
|
|
133
|
+
* @returns The claims, or `null` for a token that is forged, altered,
|
|
134
|
+
* malformed, from the future, or past its slots
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* const claims = await verifyViewToken(secret, await c.req.text(), now());
|
|
138
|
+
* if (claims && claims.siteId === site.id) count(claims);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export async function verifyViewToken(
|
|
142
|
+
secret: string,
|
|
143
|
+
token: string,
|
|
144
|
+
timestamp: number,
|
|
145
|
+
): Promise<ViewTokenClaims | null> {
|
|
146
|
+
if (token.length === 0 || token.length > TOKEN_MAX_LENGTH) return null;
|
|
147
|
+
const dot = token.lastIndexOf(".");
|
|
148
|
+
if (dot <= 0) return null;
|
|
149
|
+
const payload = token.slice(0, dot);
|
|
150
|
+
const given = token.slice(dot + 1);
|
|
151
|
+
if (given.length !== SIGNATURE_LENGTH) return null;
|
|
152
|
+
if (!timingSafeEqualText(await signature(secret, payload), given)) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const json = fromBase64Url(payload);
|
|
157
|
+
if (json === null) return null;
|
|
158
|
+
let parsed: unknown;
|
|
159
|
+
try {
|
|
160
|
+
parsed = JSON.parse(json);
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
165
|
+
const { s, p, r, t } = parsed as Record<string, unknown>;
|
|
166
|
+
if (
|
|
167
|
+
typeof s !== "string" ||
|
|
168
|
+
typeof p !== "string" ||
|
|
169
|
+
typeof r !== "string" ||
|
|
170
|
+
typeof t !== "number"
|
|
171
|
+
) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
const age = viewTokenSlot(timestamp) - t;
|
|
175
|
+
if (age < 0 || age >= VIEW_TOKEN_SLOTS_ACCEPTED) return null;
|
|
176
|
+
return { siteId: s, page: p, referrer: r };
|
|
177
|
+
}
|
package/src/lib/view.ts
CHANGED
|
@@ -46,6 +46,7 @@ import {
|
|
|
46
46
|
NOTE_SUMMARY_MIN_HIDDEN_CHARS,
|
|
47
47
|
} from "./summary.js";
|
|
48
48
|
import { renderTiptapDocumentAroundBoundary } from "./tiptap-render.js";
|
|
49
|
+
import { toMediaKind } from "./upload.js";
|
|
49
50
|
import { highlightText } from "./search-snippet.js";
|
|
50
51
|
import { getPostPath, isFullUrl, toPublicPath, toSameSitePath } from "./url.js";
|
|
51
52
|
|
|
@@ -275,7 +276,10 @@ export function toPostView(
|
|
|
275
276
|
//
|
|
276
277
|
// - Titled (article-style) posts get an excerpt teaser (`summaryHtml`) and a
|
|
277
278
|
// "Continue" link to the full page; a `#continue` anchor is spliced into the
|
|
278
|
-
// body for scroll targeting on that page.
|
|
279
|
+
// body for scroll targeting on that page. The teaser keeps one inline image
|
|
280
|
+
// from ahead of the cut, unless the post has an image or video attachment:
|
|
281
|
+
// the card shows that gallery below the teaser, and one card carries one
|
|
282
|
+
// set of pictures.
|
|
279
283
|
// - Untitled notes render their body in full and expand in place. When the
|
|
280
284
|
// body is long enough to truncate (larger limit + tolerance guard) we splice
|
|
281
285
|
// a `data-note-break` marker at the boundary so the feed can clamp the tail
|
|
@@ -287,12 +291,20 @@ export function toPostView(
|
|
|
287
291
|
let bodyHtmlWithAnchor = post.bodyHtml;
|
|
288
292
|
if (post.body) {
|
|
289
293
|
const isArticle = !!post.title;
|
|
294
|
+
const hasVisualAttachment = post.mediaAttachments.some((m) => {
|
|
295
|
+
const kind = toMediaKind(m.mimeType);
|
|
296
|
+
return kind === "image" || kind === "video";
|
|
297
|
+
});
|
|
290
298
|
const result = extractSummaryHtml(
|
|
291
299
|
post.body,
|
|
292
300
|
isArticle ? ARTICLE_SUMMARY_MAX_BLOCKS : NOTE_SUMMARY_MAX_BLOCKS,
|
|
293
301
|
isArticle ? ARTICLE_SUMMARY_MAX_CHARS : NOTE_SUMMARY_MAX_CHARS,
|
|
294
302
|
isArticle ? 0 : NOTE_SUMMARY_MIN_HIDDEN_CHARS,
|
|
295
303
|
{ namespace: id },
|
|
304
|
+
{
|
|
305
|
+
includeImages: isArticle && !hasVisualAttachment,
|
|
306
|
+
stopAtHeading: isArticle,
|
|
307
|
+
},
|
|
296
308
|
);
|
|
297
309
|
if (result && isArticle) {
|
|
298
310
|
summaryHtml = result.html;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* View Counter Middleware
|
|
3
|
+
*
|
|
4
|
+
* Runs after the route and, when the route named what it rendered
|
|
5
|
+
* (`c.var.viewTarget`) and the request was one reader opening that page,
|
|
6
|
+
* writes a signed view token into the page's `<head>`. The view is counted
|
|
7
|
+
* only when the browser posts that token back (`routes/view-confirm.ts`),
|
|
8
|
+
* which the reader bundle does once the page has been in front of the reader
|
|
9
|
+
* for a few seconds or on the first interaction: a request can look exactly
|
|
10
|
+
* like a reader's and still be a crawler running a browser, and what tells
|
|
11
|
+
* them apart happens after the HTML arrives. The reader never waits on any
|
|
12
|
+
* of it.
|
|
13
|
+
*
|
|
14
|
+
* Everything that decides whether a response may count lives in
|
|
15
|
+
* `lib/view-counting.ts`; the token in `lib/view-token.ts`; when a confirmed
|
|
16
|
+
* view is written in `lib/view-buffer.ts`; what is kept and for how long in
|
|
17
|
+
* `services/stats.ts`. This file connects the first two to a response.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Context, MiddlewareHandler } from "hono";
|
|
21
|
+
import type { Bindings } from "../types.js";
|
|
22
|
+
import type { AppVariables } from "../types/app-context.js";
|
|
23
|
+
import { getEnvString } from "../lib/env.js";
|
|
24
|
+
import { now } from "../lib/time.js";
|
|
25
|
+
import type { ViewSink } from "../lib/view-buffer.js";
|
|
26
|
+
import {
|
|
27
|
+
formatDayKey,
|
|
28
|
+
shouldCountView,
|
|
29
|
+
viewPathKey,
|
|
30
|
+
viewReferrer,
|
|
31
|
+
} from "../lib/view-counting.js";
|
|
32
|
+
import { signViewToken, VIEW_TOKEN_META_NAME } from "../lib/view-token.js";
|
|
33
|
+
import { viewBasePath } from "../lib/view-language.js";
|
|
34
|
+
|
|
35
|
+
type Env = { Bindings: Bindings; Variables: AppVariables };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Where a confirmed view's hit goes: the site's stats service, with the
|
|
39
|
+
* flush kept alive past the response on Workers.
|
|
40
|
+
*
|
|
41
|
+
* @param c - The request the hit belongs to
|
|
42
|
+
* @returns The sink for `ViewBuffer.add`
|
|
43
|
+
* @example
|
|
44
|
+
* buffer.add(hit, viewSinkFor(c));
|
|
45
|
+
*/
|
|
46
|
+
export function viewSinkFor(c: Context<Env>): ViewSink {
|
|
47
|
+
const { services, appConfig } = c.var;
|
|
48
|
+
const { dailyLimit } = appConfig.stats;
|
|
49
|
+
return {
|
|
50
|
+
record: (entries) =>
|
|
51
|
+
services.stats.record(entries, {
|
|
52
|
+
dailyLimit,
|
|
53
|
+
today: formatDayKey(now(), "UTC"),
|
|
54
|
+
}),
|
|
55
|
+
keepAlive: (promise) => {
|
|
56
|
+
try {
|
|
57
|
+
c.executionCtx.waitUntil(promise);
|
|
58
|
+
} catch {
|
|
59
|
+
// Node and tests have no ExecutionContext; the timer runs anyway.
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Middleware that marks a page a reader opened with its view token.
|
|
67
|
+
*
|
|
68
|
+
* Mount it after `withConfig()`, since the stats switch comes from the
|
|
69
|
+
* resolved config, and before the page routes, so it wraps them.
|
|
70
|
+
*
|
|
71
|
+
* @returns Hono middleware
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* app.use("*", withConfig());
|
|
76
|
+
* app.use("*", viewCounter());
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function viewCounter(): MiddlewareHandler<Env> {
|
|
80
|
+
return async (c, next) => {
|
|
81
|
+
await next();
|
|
82
|
+
|
|
83
|
+
const target = c.get("viewTarget");
|
|
84
|
+
if (!target) return;
|
|
85
|
+
|
|
86
|
+
const { appConfig } = c.var;
|
|
87
|
+
if (!appConfig.stats.enabled) return;
|
|
88
|
+
if (
|
|
89
|
+
!shouldCountView({
|
|
90
|
+
method: c.req.method,
|
|
91
|
+
status: c.res.status,
|
|
92
|
+
contentType: c.res.headers.get("content-type"),
|
|
93
|
+
isAuthenticated: c.var.isAuthenticated,
|
|
94
|
+
headers: c.req.raw.headers,
|
|
95
|
+
})
|
|
96
|
+
) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const secret = getEnvString(c.env, "AUTH_SECRET");
|
|
100
|
+
if (!secret) return;
|
|
101
|
+
|
|
102
|
+
// `c.req.path` carries no query string: the second page of the home
|
|
103
|
+
// feed and a filtered archive are the page they belong to.
|
|
104
|
+
const page =
|
|
105
|
+
target.kind === "path"
|
|
106
|
+
? viewPathKey(c.req.path, viewBasePath(c))
|
|
107
|
+
: target.id;
|
|
108
|
+
const token = await signViewToken(
|
|
109
|
+
secret,
|
|
110
|
+
{
|
|
111
|
+
siteId: c.var.currentSite.id,
|
|
112
|
+
page,
|
|
113
|
+
referrer: viewReferrer({
|
|
114
|
+
referer: c.req.header("referer"),
|
|
115
|
+
requestUrl: c.req.url,
|
|
116
|
+
ownHost: new URL(c.var.publicRequestUrl).hostname,
|
|
117
|
+
fetchSite: c.req.header("sec-fetch-site"),
|
|
118
|
+
}),
|
|
119
|
+
},
|
|
120
|
+
now(),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// The body is a rendered string; reading it consumes it, so the response
|
|
124
|
+
// is rebuilt either way. The token is base64url and hex: safe as-is in
|
|
125
|
+
// an attribute.
|
|
126
|
+
const html = await c.res.text();
|
|
127
|
+
const head = html.indexOf("</head>");
|
|
128
|
+
const body =
|
|
129
|
+
head === -1
|
|
130
|
+
? html
|
|
131
|
+
: `${html.slice(0, head)}<meta name="${VIEW_TOKEN_META_NAME}" content="${token}">${html.slice(head)}`;
|
|
132
|
+
const headers = new Headers(c.res.headers);
|
|
133
|
+
headers.delete("content-length");
|
|
134
|
+
c.res = new Response(body, {
|
|
135
|
+
status: c.res.status,
|
|
136
|
+
statusText: c.res.statusText,
|
|
137
|
+
headers,
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
}
|
package/src/node/runtime.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
resolvePort,
|
|
9
9
|
} from "./request-handler.js";
|
|
10
10
|
import { registerTelegramPoolWebhooks } from "../lib/telegram-pool-webhooks.js";
|
|
11
|
+
import { viewBuffer } from "../lib/view-buffer.js";
|
|
11
12
|
|
|
12
13
|
export {
|
|
13
14
|
applyNodeRuntimeEnvDefaults,
|
|
@@ -96,6 +97,9 @@ export async function start(
|
|
|
96
97
|
});
|
|
97
98
|
});
|
|
98
99
|
} finally {
|
|
100
|
+
// Counted views still waiting in the buffer go out before the
|
|
101
|
+
// database they go to is closed.
|
|
102
|
+
await viewBuffer.flush();
|
|
99
103
|
await handler.close();
|
|
100
104
|
}
|
|
101
105
|
},
|
package/src/preset.css
CHANGED
|
@@ -249,6 +249,10 @@ html {
|
|
|
249
249
|
margin-bottom: 1.4rem;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
/* A heading binds to the text it introduces: the space above it is two to
|
|
253
|
+
three times the space below, and the space below is less than a paragraph
|
|
254
|
+
gap. With equal margins on both sides, as before, a heading floated
|
|
255
|
+
halfway between the sections. */
|
|
252
256
|
:where(h1) {
|
|
253
257
|
font-size: var(--type-content-display);
|
|
254
258
|
margin-top: 4rem;
|
|
@@ -257,25 +261,36 @@ html {
|
|
|
257
261
|
|
|
258
262
|
:where(h2) {
|
|
259
263
|
font-size: var(--type-content-title);
|
|
260
|
-
margin-top: 2.
|
|
261
|
-
margin-bottom:
|
|
264
|
+
margin-top: 2.6rem;
|
|
265
|
+
margin-bottom: 0.9rem;
|
|
262
266
|
}
|
|
263
267
|
|
|
264
268
|
:where(h3) {
|
|
265
269
|
font-size: var(--type-content-subtitle);
|
|
266
|
-
margin-top: 2rem;
|
|
267
|
-
margin-bottom:
|
|
270
|
+
margin-top: 2.2rem;
|
|
271
|
+
margin-bottom: 0.75rem;
|
|
268
272
|
}
|
|
269
273
|
|
|
270
|
-
:where(h4
|
|
274
|
+
:where(h4) {
|
|
271
275
|
font-size: var(--type-content-body);
|
|
272
|
-
|
|
276
|
+
margin-top: 1.8rem;
|
|
277
|
+
margin-bottom: 0.6rem;
|
|
273
278
|
}
|
|
274
279
|
|
|
275
280
|
:where(h5, h6) {
|
|
281
|
+
font-size: var(--type-content-body);
|
|
282
|
+
font-weight: var(--fw-medium);
|
|
276
283
|
color: var(--site-reading-meta);
|
|
277
284
|
}
|
|
278
285
|
|
|
286
|
+
/* The block after a heading gives up its own top margin, so the heading's
|
|
287
|
+
bottom margin is the whole gap. Left in place, a paragraph's 1.4rem wins
|
|
288
|
+
the margin collapse against any smaller heading margin, and the heading
|
|
289
|
+
floats 1.4rem from both neighbours whatever its margins say. */
|
|
290
|
+
:where(h1, h2, h3, h4, h5, h6) + :where(*) {
|
|
291
|
+
margin-top: 0;
|
|
292
|
+
}
|
|
293
|
+
|
|
279
294
|
:where(ul, ol) {
|
|
280
295
|
margin: 1.25em 0;
|
|
281
296
|
padding-left: 1.4em;
|
|
@@ -507,17 +522,19 @@ html {
|
|
|
507
522
|
*/
|
|
508
523
|
[data-post]:not(.post-detail) [data-post-body].prose {
|
|
509
524
|
:where(h1, h2) {
|
|
510
|
-
font-size: calc(var(--type-content-body) * 1.
|
|
511
|
-
font-weight: var(--
|
|
512
|
-
margin-top: 1.
|
|
513
|
-
margin-bottom: 0.
|
|
525
|
+
font-size: calc(var(--type-content-body) * 1.2);
|
|
526
|
+
font-weight: var(--type-heading-weight);
|
|
527
|
+
margin-top: 1.6rem;
|
|
528
|
+
margin-bottom: 0.6rem;
|
|
514
529
|
}
|
|
515
530
|
|
|
531
|
+
/* Still a step above the body: at body size in the medium weight these
|
|
532
|
+
read as a stray line, since the CJK serif faces render 500 as Regular. */
|
|
516
533
|
:where(h3, h4) {
|
|
517
|
-
font-size: var(--type-content-body);
|
|
518
|
-
font-weight: var(--
|
|
519
|
-
margin-top: 1.
|
|
520
|
-
margin-bottom: 0.
|
|
534
|
+
font-size: calc(var(--type-content-body) * 1.08);
|
|
535
|
+
font-weight: var(--type-heading-weight);
|
|
536
|
+
margin-top: 1.4rem;
|
|
537
|
+
margin-bottom: 0.5rem;
|
|
521
538
|
}
|
|
522
539
|
|
|
523
540
|
:where(h5, h6) {
|
package/src/routes/api/media.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* Media API Routes
|
|
3
3
|
*
|
|
4
4
|
* The author's uploaded files as a resource: list, read, change alt text,
|
|
5
|
-
* delete. Uploading happens at `POST /api/upload`
|
|
6
|
-
* `/api/uploads` (a resumable session); the MCP media tools
|
|
7
|
-
* responses.
|
|
5
|
+
* give a video its poster, delete. Uploading happens at `POST /api/upload`
|
|
6
|
+
* (one request) or `/api/uploads` (a resumable session); the MCP media tools
|
|
7
|
+
* share these responses.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Hono } from "hono";
|
|
@@ -17,6 +17,7 @@ import { assertFound, parseIdParam } from "../../lib/errors.js";
|
|
|
17
17
|
import { ID_PREFIX } from "../../lib/ids.js";
|
|
18
18
|
import { pageLimitSchema } from "../../lib/schemas.js";
|
|
19
19
|
import { toApiMedia } from "../../lib/api-media.js";
|
|
20
|
+
import { requireStorage } from "../../lib/storage.js";
|
|
20
21
|
|
|
21
22
|
type Env = { Bindings: Bindings; Variables: AppVariables };
|
|
22
23
|
|
|
@@ -69,6 +70,23 @@ mediaApiRoutes.put("/:id", validate("json", UpdateMediaSchema), async (c) => {
|
|
|
69
70
|
return c.json(toApiMedia(media, c.var.appConfig));
|
|
70
71
|
});
|
|
71
72
|
|
|
73
|
+
// A video uploaded without a poster can be given one afterwards: the body is
|
|
74
|
+
// the WebP or PNG bytes, as for `PUT /api/uploads/:id/poster`.
|
|
75
|
+
mediaApiRoutes.put("/:id/poster", async (c) => {
|
|
76
|
+
const storage = requireStorage(c.var.storage);
|
|
77
|
+
const id = parseIdParam(c.req.param("id"), ID_PREFIX.media);
|
|
78
|
+
|
|
79
|
+
const media = assertFound(
|
|
80
|
+
await c.var.services.media.setPoster(
|
|
81
|
+
id,
|
|
82
|
+
new Uint8Array(await c.req.arrayBuffer()),
|
|
83
|
+
{ storage },
|
|
84
|
+
),
|
|
85
|
+
"Media",
|
|
86
|
+
);
|
|
87
|
+
return c.json(toApiMedia(media, c.var.appConfig));
|
|
88
|
+
});
|
|
89
|
+
|
|
72
90
|
mediaApiRoutes.delete("/:id", async (c) => {
|
|
73
91
|
const id = parseIdParam(c.req.param("id"), ID_PREFIX.media);
|
|
74
92
|
assertFound(await c.var.services.media.getById(id), "Media");
|