@jant/core 1.0.1 → 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-Gt7_skt6.js → app-CbOZ2Irh.js} +8704 -5180
- package/dist/client/.vite/manifest.json +125 -85
- package/dist/client/_assets/chunks/{confirm-D7GrwsCN.js → confirm-CMe01lne.js} +1 -1
- package/dist/client/_assets/chunks/{create-editor-5Y49x7h5.js → create-editor-hWapNE40.js} +26 -26
- package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
- package/dist/client/_assets/chunks/{jant-locale-picker-DL6mIOT5.js → jant-locale-picker-2L4rkDaz.js} +1 -1
- package/dist/client/_assets/chunks/lit-Bw_voPoW.js +2 -0
- package/dist/client/_assets/chunks/preload-helper-Czpn1I53.js +1 -0
- package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +1 -0
- package/dist/client/_assets/chunks/{settings-paths-h6RjIe3C.js → settings-paths-BBVT8zKj.js} +1 -1
- package/dist/client/_assets/chunks/slug-format-BuCPjSrB.js +1 -0
- 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-html-B5ZEXaxg.js → unsafe-html-CWos3v6N.js} +1 -1
- package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
- package/dist/client/_assets/client-BO8GOFfT.js +354 -0
- package/dist/client/_assets/client-D8Rrsd73.css +2 -0
- package/dist/client/_assets/client-auth-CsUGM718.js +859 -0
- 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-C1eDB1Rc.js → client-manage-CI9-eh-8.js} +176 -176
- package/dist/client/_assets/{client-settings-DIM44Zqh.js → client-settings-BnGPuqwc.js} +166 -166
- package/dist/client/_assets/client-share-0DICJjAc.css +1 -0
- package/dist/client/_assets/client-share-C59mOLJ1.js +149 -0
- package/dist/index.js +1 -1
- package/dist/node.js +3 -2
- package/package.json +2 -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/components/jant-post-menu.ts +80 -13
- package/src/client/components/jant-share-dialog.ts +422 -0
- package/src/client/components/jant-share-menu.ts +188 -0
- package/src/client/compose-bridge.ts +61 -1
- package/src/client/feed-video-player.ts +6 -0
- package/src/client/lazy-entries.ts +21 -1
- package/src/client/post-menu-shared.ts +100 -0
- package/src/client/share-capture.ts +443 -0
- package/src/client/share-launch.ts +133 -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-share.ts +8 -0
- package/src/client.ts +2 -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/discover.ts +31 -0
- 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/share-card.ts +653 -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/version.ts +2 -0
- 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/auth/setup.tsx +37 -1
- 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/partials.tsx +23 -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/style-share.css +16 -0
- package/src/styles/components-author.css +177 -0
- package/src/styles/components.css +25 -3
- package/src/styles/share.css +1103 -0
- package/src/styles/tokens.css +65 -30
- package/src/styles/ui.css +62 -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 +11 -2
- package/src/ui/layouts/SiteLayout.tsx +9 -3
- package/src/ui/share/ShareCard.tsx +298 -0
- package/src/ui/shared/Icon.tsx +23 -3
- package/src/ui/shared/MediaGallery.tsx +31 -4
- package/src/ui/shared/PostFooter.tsx +20 -4
- package/src/ui/shared/icon-collector.ts +28 -0
- package/src/ui/shared/post-article-attributes.ts +3 -0
- package/dist/client/_assets/chunks/footnote-rail-BJwswtVQ.js +0 -1
- package/dist/client/_assets/chunks/lit-DW8VmJAT.js +0 -2
- package/dist/client/_assets/chunks/slug-format-Dsi0e9Xn.js +0 -1
- package/dist/client/_assets/chunks/sortable-list-DWNKv782.js +0 -2
- package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-DTZZEvLU.js +0 -2
- package/dist/client/_assets/client-BaqmpPQ-.css +0 -2
- package/dist/client/_assets/client-C6melOtG.js +0 -296
- package/dist/client/_assets/client-auth-Ccq8kSqq.js +0 -828
- package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
- package/dist/client/_assets/client-compose-oxzPb3SS.js +0 -2004
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What counts as a page view, and the keys a view is filed under.
|
|
3
|
+
*
|
|
4
|
+
* A view is decided in two steps. Here, from the facts of one request and
|
|
5
|
+
* its response, whether it may count at all: a browser's top-level navigation
|
|
6
|
+
* announces itself in the `Sec-Fetch-*` headers every current engine sends
|
|
7
|
+
* and almost no crawler does, and the short User-Agent list below is only a
|
|
8
|
+
* backstop for the headless browsers that still say what they are. Then, in
|
|
9
|
+
* the browser, whether the page was seen: the reader bundle confirms the
|
|
10
|
+
* view after a few seconds in front of the reader or on the first
|
|
11
|
+
* interaction (`lib/view-token.ts`), which a crawler running a real browser
|
|
12
|
+
* never gets to. No cookie, no stored address. This module also reduces what
|
|
13
|
+
* is kept to a counter key: the site's calendar day and hour, the page, and
|
|
14
|
+
* where the reader came from — the source the link was tagged with, or else
|
|
15
|
+
* the referring host, either one folded to the address its source is listed
|
|
16
|
+
* under (`referrer-aliases.ts`), or the site itself.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { ID_PREFIX, isTypeId } from "./ids.js";
|
|
20
|
+
import { canonicalSource } from "./referrer-aliases.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* What the page a route rendered counts as: a post, a collection or a smart
|
|
24
|
+
* collection, by id, so a count survives a change of address and an alias
|
|
25
|
+
* and its canonical path are one page; or any other public page — the home
|
|
26
|
+
* page, the archive, an aggregate selection — by its path. Routes set it on
|
|
27
|
+
* the context (`viewTarget`) and `middleware/view-counter.ts` reads it after
|
|
28
|
+
* the response.
|
|
29
|
+
*/
|
|
30
|
+
export type ViewTarget =
|
|
31
|
+
| { kind: "post" | "collection" | "smartCollection"; id: string }
|
|
32
|
+
| { kind: "path" };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The `page` and `referrer` of a site-day's overflow row, where new
|
|
36
|
+
* page-referrer pairs land once the day has `VIEW_COUNT_ROWS_PER_SITE_DAY`
|
|
37
|
+
* rows. Never a real page or host: a path starts with `/`, an id with its
|
|
38
|
+
* TypeID prefix, and a hostname has no `*`.
|
|
39
|
+
*/
|
|
40
|
+
export const VIEW_OVERFLOW_KEY = "*";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The referrer of a view that came from another page of the site itself:
|
|
44
|
+
* a reader moving on from the home page to a post. Kept apart from a direct
|
|
45
|
+
* visit so that "Direct" means what it says, and never a host, since no
|
|
46
|
+
* hostname is `/`.
|
|
47
|
+
*/
|
|
48
|
+
export const VIEW_INTERNAL_REFERRER = "/";
|
|
49
|
+
|
|
50
|
+
/** What a counter key names, told from its shape alone. */
|
|
51
|
+
export type ViewPageKind =
|
|
52
|
+
"post" | "collection" | "smartCollection" | "path" | "overflow";
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What a counter key names. A key is self-describing: the overflow key,
|
|
56
|
+
* a path, or a TypeID whose prefix says which table the entity is in. The
|
|
57
|
+
* table needs no kind column for it.
|
|
58
|
+
*
|
|
59
|
+
* @param key - A `view_count.page` value
|
|
60
|
+
* @returns Its kind, or `null` for a string that could never be a key
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* viewPageKind("/archive"); // "path"
|
|
64
|
+
* viewPageKind("pst_01h455vb4pex5vsknk084sn02q"); // "post"
|
|
65
|
+
* viewPageKind("nonsense"); // null
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function viewPageKind(key: string): ViewPageKind | null {
|
|
69
|
+
if (key === VIEW_OVERFLOW_KEY) return "overflow";
|
|
70
|
+
if (key.startsWith("/")) return "path";
|
|
71
|
+
if (isTypeId(key, ID_PREFIX.post)) return "post";
|
|
72
|
+
if (isTypeId(key, ID_PREFIX.collection)) return "collection";
|
|
73
|
+
if (isTypeId(key, ID_PREFIX.smartCollection)) return "smartCollection";
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Longest page key stored; a path past this is nothing a site links to. */
|
|
78
|
+
const PAGE_KEY_MAX_LENGTH = 512;
|
|
79
|
+
/** A hostname's own limit. */
|
|
80
|
+
const HOST_MAX_LENGTH = 253;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where a `Referer` may come from: the web, or an Android app that says which
|
|
84
|
+
* app opened the link (`android-app://com.example.app/`). Anything else is a
|
|
85
|
+
* direct visit.
|
|
86
|
+
*/
|
|
87
|
+
const REFERER_SCHEMES = new Set(["http:", "https:", "android-app:"]);
|
|
88
|
+
|
|
89
|
+
const DAY_KEY = /^\d{4}-\d{2}-\d{2}$/;
|
|
90
|
+
const HOUR_KEY = /^\d{4}-\d{2}-\d{2}T\d{2}$/;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Machines that run a browser engine and say so, and HTTP libraries that send
|
|
94
|
+
* a `Sec-Fetch-Dest: document` of their own. Everything else is already kept
|
|
95
|
+
* out by the navigation check, so this list stays short and obvious rather
|
|
96
|
+
* than trying to be complete.
|
|
97
|
+
*/
|
|
98
|
+
const BOT_USER_AGENT = new RegExp(
|
|
99
|
+
[
|
|
100
|
+
"bot",
|
|
101
|
+
"crawl",
|
|
102
|
+
"spider",
|
|
103
|
+
"slurp",
|
|
104
|
+
"scrape",
|
|
105
|
+
"fetch",
|
|
106
|
+
"preview",
|
|
107
|
+
"headless",
|
|
108
|
+
"phantom",
|
|
109
|
+
"selenium",
|
|
110
|
+
"puppeteer",
|
|
111
|
+
"playwright",
|
|
112
|
+
"lighthouse",
|
|
113
|
+
"pagespeed",
|
|
114
|
+
"python",
|
|
115
|
+
"curl/",
|
|
116
|
+
"wget/",
|
|
117
|
+
"java/",
|
|
118
|
+
"go-http-client",
|
|
119
|
+
"okhttp",
|
|
120
|
+
"libwww",
|
|
121
|
+
"httpclient",
|
|
122
|
+
"axios",
|
|
123
|
+
"feed",
|
|
124
|
+
"rss",
|
|
125
|
+
"validator",
|
|
126
|
+
"monitor",
|
|
127
|
+
"uptime",
|
|
128
|
+
"facebookexternalhit",
|
|
129
|
+
"embedly",
|
|
130
|
+
"whatsapp",
|
|
131
|
+
].join("|"),
|
|
132
|
+
"i",
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Whether a User-Agent names a machine rather than a reader's browser.
|
|
137
|
+
*
|
|
138
|
+
* A missing User-Agent counts as a machine: every browser sends one.
|
|
139
|
+
*
|
|
140
|
+
* @param userAgent - The request's `User-Agent`, if any
|
|
141
|
+
* @returns `true` when the request should not count as a view
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* isBotUserAgent("Mozilla/5.0 (compatible; Googlebot/2.1)"); // true
|
|
145
|
+
* isBotUserAgent("Mozilla/5.0 (Macintosh) Safari/605.1.15"); // false
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export function isBotUserAgent(userAgent: string | null | undefined): boolean {
|
|
149
|
+
if (!userAgent) return true;
|
|
150
|
+
return BOT_USER_AGENT.test(userAgent);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The facts of one request and its response that decide a view. */
|
|
154
|
+
export interface ViewCandidate {
|
|
155
|
+
method: string;
|
|
156
|
+
status: number;
|
|
157
|
+
/** The response's `Content-Type`. */
|
|
158
|
+
contentType: string | null;
|
|
159
|
+
isAuthenticated: boolean;
|
|
160
|
+
/** The request headers. */
|
|
161
|
+
headers: Headers;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Whether a request and its response are one reader opening one page.
|
|
166
|
+
*
|
|
167
|
+
* A view is a `GET` that answered `200` with HTML to someone who is not signed
|
|
168
|
+
* in, from a browser's top-level navigation (`Sec-Fetch-Dest: document`,
|
|
169
|
+
* `Sec-Fetch-Mode: navigate`) that is not a prefetch or prerender, with a
|
|
170
|
+
* User-Agent that is not a known machine. The `Sec-Fetch-*` requirement is
|
|
171
|
+
* what keeps out the long tail of crawlers and HTTP clients; it also keeps out
|
|
172
|
+
* browsers from before 2023 that never sent the headers, which by now is a
|
|
173
|
+
* share small enough to accept.
|
|
174
|
+
*
|
|
175
|
+
* @param candidate - The request and response facts
|
|
176
|
+
* @returns `true` when this response counts as a view
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* shouldCountView({
|
|
180
|
+
* method: "GET",
|
|
181
|
+
* status: 200,
|
|
182
|
+
* contentType: "text/html; charset=UTF-8",
|
|
183
|
+
* isAuthenticated: false,
|
|
184
|
+
* headers: request.headers,
|
|
185
|
+
* });
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export function shouldCountView(candidate: ViewCandidate): boolean {
|
|
189
|
+
if (candidate.method !== "GET") return false;
|
|
190
|
+
if (candidate.status !== 200) return false;
|
|
191
|
+
if (!candidate.contentType?.toLowerCase().startsWith("text/html")) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
if (candidate.isAuthenticated) return false;
|
|
195
|
+
|
|
196
|
+
const { headers } = candidate;
|
|
197
|
+
if (headers.get("sec-fetch-dest") !== "document") return false;
|
|
198
|
+
if (headers.get("sec-fetch-mode") !== "navigate") return false;
|
|
199
|
+
|
|
200
|
+
// Speculative loads: Chrome's speculation rules (`Sec-Purpose`), the older
|
|
201
|
+
// `Purpose` and `X-Purpose` hints, Firefox's `X-Moz`. A page fetched ahead
|
|
202
|
+
// of a click is not a page read.
|
|
203
|
+
const purpose = [
|
|
204
|
+
headers.get("sec-purpose"),
|
|
205
|
+
headers.get("purpose"),
|
|
206
|
+
headers.get("x-purpose"),
|
|
207
|
+
headers.get("x-moz"),
|
|
208
|
+
]
|
|
209
|
+
.filter(Boolean)
|
|
210
|
+
.join(" ")
|
|
211
|
+
.toLowerCase();
|
|
212
|
+
if (
|
|
213
|
+
purpose.includes("prefetch") ||
|
|
214
|
+
purpose.includes("prerender") ||
|
|
215
|
+
purpose.includes("preview")
|
|
216
|
+
) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return !isBotUserAgent(headers.get("user-agent"));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function normalizeHost(host: string): string {
|
|
224
|
+
return host
|
|
225
|
+
.toLowerCase()
|
|
226
|
+
.replace(/\.$/, "")
|
|
227
|
+
.replace(/^www\./, "");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The host a view came from, reduced to what the stats keep.
|
|
232
|
+
*
|
|
233
|
+
* Lowercased, with a leading `www.` and a trailing dot removed, then folded
|
|
234
|
+
* to the address its source is listed under (`old.reddit.com` and `redd.it`
|
|
235
|
+
* are both `reddit.com`), so one site is one row. A link opened from an
|
|
236
|
+
* Android app names the app rather than a site; a known app folds to its
|
|
237
|
+
* site, an unknown one is kept by its package id. A link from the site
|
|
238
|
+
* itself is {@link VIEW_INTERNAL_REFERRER}. Empty for a direct visit and for
|
|
239
|
+
* any other scheme — browsers send only the origin across sites by default,
|
|
240
|
+
* and that is all that is wanted anyway.
|
|
241
|
+
*
|
|
242
|
+
* @param referer - The request's `Referer` header, if any
|
|
243
|
+
* @param ownHost - The host the site is being served on
|
|
244
|
+
* @returns The referring host, `/` for the site itself, or `""`
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* referrerHost("https://www.Example.com/post?x=1", "blog.example"); // "example.com"
|
|
248
|
+
* referrerHost("https://old.reddit.com/r/blogs", "blog.example"); // "reddit.com"
|
|
249
|
+
* referrerHost("android-app://com.example.app/", "blog.example"); // "com.example.app"
|
|
250
|
+
* referrerHost("https://blog.example/", "blog.example"); // "/"
|
|
251
|
+
* referrerHost(null, "blog.example"); // ""
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
export function referrerHost(
|
|
255
|
+
referer: string | null | undefined,
|
|
256
|
+
ownHost: string,
|
|
257
|
+
): string {
|
|
258
|
+
if (!referer) return "";
|
|
259
|
+
let url: URL;
|
|
260
|
+
try {
|
|
261
|
+
url = new URL(referer);
|
|
262
|
+
} catch {
|
|
263
|
+
return "";
|
|
264
|
+
}
|
|
265
|
+
if (!REFERER_SCHEMES.has(url.protocol)) return "";
|
|
266
|
+
const host = normalizeHost(url.hostname);
|
|
267
|
+
if (!host) return "";
|
|
268
|
+
if (host === normalizeHost(ownHost)) return VIEW_INTERNAL_REFERRER;
|
|
269
|
+
return canonicalSource(host).slice(0, HOST_MAX_LENGTH);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Query parameters whose value says where a link was shared, in the order tried. */
|
|
273
|
+
const SOURCE_QUERY_PARAMS = ["utm_source", "ref"] as const;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* What a source parameter may look like once normalized: a hostname
|
|
277
|
+
* (`chatgpt.com`) or a plain word someone tagged a link with (`newsletter`,
|
|
278
|
+
* `hacker_news`). Anything else — spaces, slashes, markup, a long string —
|
|
279
|
+
* is not a name worth a row and is ignored.
|
|
280
|
+
*/
|
|
281
|
+
const SOURCE_TOKEN = /^[a-z0-9][a-z0-9._-]{0,62}$/;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The referrer a view is filed under.
|
|
285
|
+
*
|
|
286
|
+
* A `utm_source` or `ref` parameter on the link wins when it names something:
|
|
287
|
+
* whoever tagged the link said where it would be shared, which beats the host
|
|
288
|
+
* the browser happened to report and is often the only signal — an app
|
|
289
|
+
* opening a link sends no `Referer`, and ChatGPT tags the links in its
|
|
290
|
+
* answers with `utm_source=chatgpt.com`. Without a usable tag the `Referer`
|
|
291
|
+
* host decides, as in {@link referrerHost}. Both are normalized and folded
|
|
292
|
+
* the same way, so a `utm_source=chatgpt.com` and a `Referer` from
|
|
293
|
+
* chatgpt.com are one row, and so are `utm_source=twitter` and a `Referer`
|
|
294
|
+
* from t.co. A view with no `Referer` at all is still the site's own when
|
|
295
|
+
* the browser says so in `Sec-Fetch-Site`, which no referrer policy
|
|
296
|
+
* suppresses.
|
|
297
|
+
*
|
|
298
|
+
* @param input - The request's `Referer` header, its URL (for the query
|
|
299
|
+
* string), the host the site is being served on, and the request's
|
|
300
|
+
* `Sec-Fetch-Site` header
|
|
301
|
+
* @returns The referrer key: a host or tag, `/` for the site itself, or
|
|
302
|
+
* `""` for a direct visit
|
|
303
|
+
* @example
|
|
304
|
+
* ```ts
|
|
305
|
+
* viewReferrer({
|
|
306
|
+
* referer: null,
|
|
307
|
+
* requestUrl: "https://blog.example/post?utm_source=chatgpt.com",
|
|
308
|
+
* ownHost: "blog.example",
|
|
309
|
+
* }); // "chatgpt.com"
|
|
310
|
+
* viewReferrer({
|
|
311
|
+
* referer: "https://t.co/abc",
|
|
312
|
+
* requestUrl: "https://blog.example/post?ref=newsletter",
|
|
313
|
+
* ownHost: "blog.example",
|
|
314
|
+
* }); // "newsletter"
|
|
315
|
+
* viewReferrer({
|
|
316
|
+
* referer: "https://news.ycombinator.com/",
|
|
317
|
+
* requestUrl: "https://blog.example/post",
|
|
318
|
+
* ownHost: "blog.example",
|
|
319
|
+
* }); // "news.ycombinator.com"
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
export function viewReferrer(input: {
|
|
323
|
+
referer: string | null | undefined;
|
|
324
|
+
requestUrl: string;
|
|
325
|
+
ownHost: string;
|
|
326
|
+
fetchSite?: string | null;
|
|
327
|
+
}): string {
|
|
328
|
+
const tagged = taggedSource(input.requestUrl, input.ownHost);
|
|
329
|
+
if (tagged !== null) return tagged;
|
|
330
|
+
const host = referrerHost(input.referer, input.ownHost);
|
|
331
|
+
if (host !== "") return host;
|
|
332
|
+
return input.fetchSite === "same-origin" || input.fetchSite === "same-site"
|
|
333
|
+
? VIEW_INTERNAL_REFERRER
|
|
334
|
+
: "";
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The source a link's own parameters name, or `null` when they name none.
|
|
339
|
+
* `""` is a tag naming the site itself, which is a direct visit.
|
|
340
|
+
*/
|
|
341
|
+
function taggedSource(requestUrl: string, ownHost: string): string | null {
|
|
342
|
+
let params: URLSearchParams;
|
|
343
|
+
try {
|
|
344
|
+
params = new URL(requestUrl).searchParams;
|
|
345
|
+
} catch {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
for (const name of SOURCE_QUERY_PARAMS) {
|
|
349
|
+
const value = params.get(name)?.trim();
|
|
350
|
+
if (!value) continue;
|
|
351
|
+
const token = normalizeHost(value);
|
|
352
|
+
if (!SOURCE_TOKEN.test(token)) continue;
|
|
353
|
+
if (token === normalizeHost(ownHost)) return "";
|
|
354
|
+
return canonicalSource(token);
|
|
355
|
+
}
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* The key a non-post page is counted under: its path, with the language
|
|
361
|
+
* view's prefix and any trailing slash removed, so `/en/archive/` and
|
|
362
|
+
* `/archive` are one page.
|
|
363
|
+
*
|
|
364
|
+
* @param path - The request's internal path, site prefix already removed
|
|
365
|
+
* @param viewBase - The language view's prefix (`/en`), or `""` on the root view
|
|
366
|
+
* @returns The page key
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* viewPathKey("/en/archive", "/en"); // "/archive"
|
|
370
|
+
* viewPathKey("/en", "/en"); // "/"
|
|
371
|
+
* viewPathKey("/", ""); // "/"
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
export function viewPathKey(path: string, viewBase: string): string {
|
|
375
|
+
let key = path;
|
|
376
|
+
if (viewBase && (key === viewBase || key.startsWith(`${viewBase}/`))) {
|
|
377
|
+
key = key.slice(viewBase.length) || "/";
|
|
378
|
+
}
|
|
379
|
+
key = key.replace(/\/+$/, "") || "/";
|
|
380
|
+
return key.slice(0, PAGE_KEY_MAX_LENGTH);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const dayFormatters = new Map<string, Intl.DateTimeFormat>();
|
|
384
|
+
|
|
385
|
+
function dayFormatter(timeZone: string): Intl.DateTimeFormat {
|
|
386
|
+
let formatter = dayFormatters.get(timeZone);
|
|
387
|
+
if (!formatter) {
|
|
388
|
+
// `en-CA` writes dates as `YYYY-MM-DD`.
|
|
389
|
+
formatter = new Intl.DateTimeFormat("en-CA", {
|
|
390
|
+
timeZone,
|
|
391
|
+
year: "numeric",
|
|
392
|
+
month: "2-digit",
|
|
393
|
+
day: "2-digit",
|
|
394
|
+
});
|
|
395
|
+
dayFormatters.set(timeZone, formatter);
|
|
396
|
+
}
|
|
397
|
+
return formatter;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* The calendar day a moment falls on in a time zone, as `YYYY-MM-DD`.
|
|
402
|
+
*
|
|
403
|
+
* An unknown time zone falls back to UTC rather than failing: a view is not
|
|
404
|
+
* worth an error, and the site's zone is validated where it is set.
|
|
405
|
+
*
|
|
406
|
+
* @param timestamp - Unix timestamp in seconds
|
|
407
|
+
* @param timeZone - IANA time zone, such as `Asia/Shanghai`
|
|
408
|
+
* @returns The day key
|
|
409
|
+
* @example
|
|
410
|
+
* ```ts
|
|
411
|
+
* formatDayKey(1_762_560_000, "Asia/Shanghai"); // "2025-11-08"
|
|
412
|
+
* formatDayKey(1_762_560_000, "UTC"); // "2025-11-08"
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
415
|
+
export function formatDayKey(timestamp: number, timeZone: string): string {
|
|
416
|
+
const date = new Date(timestamp * 1000);
|
|
417
|
+
try {
|
|
418
|
+
return dayFormatter(timeZone).format(date);
|
|
419
|
+
} catch {
|
|
420
|
+
return date.toISOString().slice(0, 10);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* The hour a moment falls in, in a time zone, as `YYYY-MM-DDTHH`: the day key
|
|
426
|
+
* and the hour of the day, so a day's hours sort and compare as strings.
|
|
427
|
+
*
|
|
428
|
+
* @param timestamp - Unix timestamp in seconds
|
|
429
|
+
* @param timeZone - IANA time zone
|
|
430
|
+
* @returns The hour key
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* formatHourKey(1_762_560_000, "Asia/Shanghai"); // "2025-11-08T08"
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
436
|
+
export function formatHourKey(timestamp: number, timeZone: string): string {
|
|
437
|
+
const date = new Date(timestamp * 1000);
|
|
438
|
+
let hour: string;
|
|
439
|
+
try {
|
|
440
|
+
hour =
|
|
441
|
+
new Intl.DateTimeFormat("en-US", {
|
|
442
|
+
timeZone,
|
|
443
|
+
hour: "2-digit",
|
|
444
|
+
hourCycle: "h23",
|
|
445
|
+
})
|
|
446
|
+
.formatToParts(date)
|
|
447
|
+
.find((part) => part.type === "hour")?.value ?? "00";
|
|
448
|
+
} catch {
|
|
449
|
+
hour = String(date.getUTCHours()).padStart(2, "0");
|
|
450
|
+
}
|
|
451
|
+
return `${formatDayKey(timestamp, timeZone)}T${hour}`;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Whether a string is a `YYYY-MM-DDTHH` hour key.
|
|
456
|
+
*
|
|
457
|
+
* @param value - The string to check
|
|
458
|
+
* @returns `true` for an hour key
|
|
459
|
+
* @example
|
|
460
|
+
* ```ts
|
|
461
|
+
* isHourKey("2026-11-09T14"); // true
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
export function isHourKey(value: string): boolean {
|
|
465
|
+
return HOUR_KEY.test(value);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The 24 hour keys of a day, in order.
|
|
470
|
+
*
|
|
471
|
+
* @param day - The day key
|
|
472
|
+
* @returns `day`'s hours, `T00` through `T23`
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* hourKeysOfDay("2026-11-09")[14]; // "2026-11-09T14"
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
478
|
+
export function hourKeysOfDay(day: string): string[] {
|
|
479
|
+
return Array.from(
|
|
480
|
+
{ length: 24 },
|
|
481
|
+
(_, hour) => `${day}T${String(hour).padStart(2, "0")}`,
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Whether a string is a `YYYY-MM-DD` day key.
|
|
487
|
+
*
|
|
488
|
+
* @param value - The string to check
|
|
489
|
+
* @returns `true` for a day key
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* isDayKey("2026-11-08"); // true
|
|
493
|
+
* isDayKey("2026-11-8"); // false
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
export function isDayKey(value: string): boolean {
|
|
497
|
+
return DAY_KEY.test(value);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* A day key some days before or after another.
|
|
502
|
+
*
|
|
503
|
+
* Calendar arithmetic in UTC, so a local time zone's daylight-saving jumps
|
|
504
|
+
* never land a day on the wrong key.
|
|
505
|
+
*
|
|
506
|
+
* @param day - The starting day key
|
|
507
|
+
* @param days - Days to move; negative moves back
|
|
508
|
+
* @returns The resulting day key
|
|
509
|
+
* @example
|
|
510
|
+
* ```ts
|
|
511
|
+
* shiftDayKey("2026-03-01", -1); // "2026-02-28"
|
|
512
|
+
* shiftDayKey("2026-12-31", 1); // "2027-01-01"
|
|
513
|
+
* ```
|
|
514
|
+
*/
|
|
515
|
+
export function shiftDayKey(day: string, days: number): string {
|
|
516
|
+
const [year, month, date] = day.split("-").map(Number);
|
|
517
|
+
return new Date(Date.UTC(year ?? 1970, (month ?? 1) - 1, (date ?? 1) + days))
|
|
518
|
+
.toISOString()
|
|
519
|
+
.slice(0, 10);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* The run of day keys that ends on a day, oldest first.
|
|
524
|
+
*
|
|
525
|
+
* @param day - The last day of the run
|
|
526
|
+
* @param count - How many days, the last one included
|
|
527
|
+
* @returns `count` day keys ending at `day`
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* dayKeysEndingAt("2026-11-08", 3); // ["2026-11-06", "2026-11-07", "2026-11-08"]
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
export function dayKeysEndingAt(day: string, count: number): string[] {
|
|
534
|
+
const keys: string[] = [];
|
|
535
|
+
for (let offset = count - 1; offset >= 0; offset--) {
|
|
536
|
+
keys.push(shiftDayKey(day, -offset));
|
|
537
|
+
}
|
|
538
|
+
return keys;
|
|
539
|
+
}
|