@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.
Files changed (116) hide show
  1. package/bin/lib/site-snapshot.js +10 -0
  2. package/dist/{app-CKQ9PqAr.js → app-CbOZ2Irh.js} +7988 -5166
  3. package/dist/client/.vite/manifest.json +47 -47
  4. package/dist/client/_assets/chunks/{create-editor-BQkrw8Ax.js → create-editor-hWapNE40.js} +25 -25
  5. package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
  6. package/dist/client/_assets/chunks/{settings-paths-B0bNUaih.js → settings-paths-BBVT8zKj.js} +1 -1
  7. package/dist/client/_assets/chunks/{slug-format-CYmQE5ln.js → slug-format-BuCPjSrB.js} +1 -1
  8. package/dist/client/_assets/chunks/sortable-list-DJXwkaOv.js +2 -0
  9. package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
  10. package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
  11. package/dist/client/_assets/{client-Dp46QSht.js → client-BO8GOFfT.js} +12 -12
  12. package/dist/client/_assets/client-D8Rrsd73.css +2 -0
  13. package/dist/client/_assets/{client-auth-CG55rWb5.js → client-auth-CsUGM718.js} +1 -1
  14. package/dist/client/_assets/client-author-QPm1whbO.css +2 -0
  15. package/dist/client/_assets/client-compose-BB4U7Wz0.js +2004 -0
  16. package/dist/client/_assets/{client-manage-DnYdQAhf.js → client-manage-CI9-eh-8.js} +1 -1
  17. package/dist/client/_assets/{client-settings-CHgsIqXJ.js → client-settings-BnGPuqwc.js} +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/node.js +3 -2
  20. package/package.json +1 -1
  21. package/src/app.tsx +9 -0
  22. package/src/client/components/compose-types.ts +1 -0
  23. package/src/client/components/jant-compose-dialog.ts +1 -0
  24. package/src/client/components/jant-compose-editor.ts +30 -0
  25. package/src/client/components/jant-compose-fullscreen.ts +1 -0
  26. package/src/client/compose-bridge.ts +61 -1
  27. package/src/client/feed-video-player.ts +6 -0
  28. package/src/client/tiptap/bubble-menu.ts +25 -2
  29. package/src/client/tiptap/create-editor.ts +2 -0
  30. package/src/client/tiptap/extensions.ts +4 -0
  31. package/src/client/video-processor.ts +106 -52
  32. package/src/client/view-confirm.ts +108 -0
  33. package/src/client.ts +1 -0
  34. package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
  35. package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
  36. package/src/db/migrations/meta/0035_snapshot.json +2877 -0
  37. package/src/db/migrations/meta/0036_snapshot.json +2924 -0
  38. package/src/db/migrations/meta/_journal.json +14 -0
  39. package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
  40. package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
  41. package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
  42. package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
  43. package/src/db/migrations/pg/meta/_journal.json +14 -0
  44. package/src/db/pg/schema.ts +40 -0
  45. package/src/db/schema.ts +61 -0
  46. package/src/i18n/locales/public/en.po +5 -0
  47. package/src/i18n/locales/public/en.ts +1 -1
  48. package/src/i18n/locales/public/zh-Hans.po +5 -0
  49. package/src/i18n/locales/public/zh-Hans.ts +1 -1
  50. package/src/i18n/locales/public/zh-Hant.po +5 -0
  51. package/src/i18n/locales/public/zh-Hant.ts +1 -1
  52. package/src/i18n/locales/settings/en.po +149 -0
  53. package/src/i18n/locales/settings/en.ts +1 -1
  54. package/src/i18n/locales/settings/zh-Hans.po +149 -0
  55. package/src/i18n/locales/settings/zh-Hans.ts +1 -1
  56. package/src/i18n/locales/settings/zh-Hant.po +149 -0
  57. package/src/i18n/locales/settings/zh-Hant.ts +1 -1
  58. package/src/lib/env.ts +20 -0
  59. package/src/lib/github-app-state.ts +1 -1
  60. package/src/lib/github-app.ts +183 -1
  61. package/src/lib/markdown-manager.ts +129 -1
  62. package/src/lib/post-meta.ts +13 -3
  63. package/src/lib/referrer-aliases.ts +311 -0
  64. package/src/lib/render.tsx +10 -0
  65. package/src/lib/resolve-config.ts +14 -0
  66. package/src/lib/summary.ts +152 -54
  67. package/src/lib/tiptap-render.ts +1 -0
  68. package/src/lib/upload.ts +21 -1
  69. package/src/lib/video-playback.ts +55 -0
  70. package/src/lib/view-buffer.ts +201 -0
  71. package/src/lib/view-counting.ts +539 -0
  72. package/src/lib/view-token.ts +177 -0
  73. package/src/lib/view.ts +13 -1
  74. package/src/middleware/view-counter.ts +140 -0
  75. package/src/node/runtime.ts +4 -0
  76. package/src/preset.css +31 -14
  77. package/src/routes/api/media.ts +21 -3
  78. package/src/routes/dash/settings.tsx +433 -168
  79. package/src/routes/pages/archive.tsx +1 -0
  80. package/src/routes/pages/collection.tsx +6 -0
  81. package/src/routes/pages/collections.tsx +1 -0
  82. package/src/routes/pages/featured.tsx +1 -0
  83. package/src/routes/pages/home.tsx +1 -0
  84. package/src/routes/pages/page.tsx +3 -0
  85. package/src/routes/pages/smart-collection.tsx +2 -0
  86. package/src/routes/pages/subscribe.tsx +1 -0
  87. package/src/routes/view-confirm.ts +63 -0
  88. package/src/services/export-theme/assets/client-site.js +1 -1
  89. package/src/services/export-theme/styles/main.css +39 -21
  90. package/src/services/export.ts +2 -0
  91. package/src/services/index.ts +17 -0
  92. package/src/services/mcp.ts +31 -1
  93. package/src/services/media.ts +63 -0
  94. package/src/services/stats.ts +1130 -0
  95. package/src/styles/components-author.css +177 -0
  96. package/src/styles/components.css +11 -3
  97. package/src/styles/tokens.css +65 -30
  98. package/src/styles/ui.css +56 -13
  99. package/src/types/app-context.ts +9 -0
  100. package/src/types/bindings.ts +5 -0
  101. package/src/types/config.ts +54 -0
  102. package/src/types/views.ts +6 -0
  103. package/src/ui/compose/ComposeDialog.tsx +6 -0
  104. package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
  105. package/src/ui/dash/settings/StatsContent.tsx +855 -0
  106. package/src/ui/font-themes.ts +39 -6
  107. package/src/ui/layouts/BaseLayout.tsx +4 -2
  108. package/src/ui/layouts/SiteLayout.tsx +8 -3
  109. package/src/ui/shared/MediaGallery.tsx +19 -3
  110. package/dist/client/_assets/chunks/footnote-rail-B08scyhW.js +0 -2
  111. package/dist/client/_assets/chunks/sortable-list-t11Ma1Ek.js +0 -2
  112. package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
  113. package/dist/client/_assets/chunks/unsafe-svg-DZl-yjas.js +0 -1
  114. package/dist/client/_assets/client-B0-s9sqx.css +0 -2
  115. package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
  116. package/dist/client/_assets/client-compose-DkUTuavR.js +0 -2004
package/src/lib/upload.ts CHANGED
@@ -684,12 +684,32 @@ export function generateSiteAssetStorageKey(
684
684
  return { id, filename, storageKey };
685
685
  }
686
686
 
687
+ /**
688
+ * Where a video's poster is stored.
689
+ *
690
+ * Posters are served `immutable`, so a replacement cannot reuse the key the
691
+ * poster it replaces had: caches would go on serving the old still. A
692
+ * replacement passes a `version`, which makes the key new.
693
+ *
694
+ * @param siteId - Owning site ID
695
+ * @param mediaId - The video's media ID
696
+ * @param ext - Poster file extension
697
+ * @param version - Distinguishes a replacement from the poster before it
698
+ * @returns The storage key
699
+ * @example
700
+ * ```ts
701
+ * getPosterStorageKey("sit_1", "med_1", "webp"); // "media/sit_1/posters/med_1.webp"
702
+ * getPosterStorageKey("sit_1", "med_1", "png", "k3x9"); // "media/sit_1/posters/med_1-k3x9.png"
703
+ * ```
704
+ */
687
705
  export function getPosterStorageKey(
688
706
  siteId: string,
689
707
  mediaId: string,
690
708
  ext = "webp",
709
+ version?: string,
691
710
  ): string {
692
- return `${MEDIA_ROOT_PREFIX}/${siteId}/${MEDIA_POSTERS_STORAGE_PREFIX}/${mediaId}.${ext}`;
711
+ const name = version ? `${mediaId}-${version}` : mediaId;
712
+ return `${MEDIA_ROOT_PREFIX}/${siteId}/${MEDIA_POSTERS_STORAGE_PREFIX}/${name}.${ext}`;
693
713
  }
694
714
 
695
715
  export function getTemporaryUploadStorageKey(
@@ -71,3 +71,58 @@ export function shouldUseShortVideoExperience(media: {
71
71
 
72
72
  return true;
73
73
  }
74
+
75
+ /**
76
+ * Where in a clip its poster frame is taken: a tenth of the way in, so the
77
+ * still is not the fade-from-black most clips open on, and never later than
78
+ * the first second. With no duration to take a tenth of, the opening frame is
79
+ * the only one to name.
80
+ *
81
+ * @param durationSeconds - Clip length in seconds, when known
82
+ * @returns Offset into the clip, in seconds
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * getPosterFrameSeconds(4); // 0.4
87
+ * getPosterFrameSeconds(90); // 1
88
+ * getPosterFrameSeconds(undefined); // 0
89
+ * ```
90
+ */
91
+ export function getPosterFrameSeconds(
92
+ durationSeconds: number | null | undefined,
93
+ ): number {
94
+ if (
95
+ !Number.isFinite(durationSeconds) ||
96
+ !durationSeconds ||
97
+ durationSeconds <= 0
98
+ ) {
99
+ return 0;
100
+ }
101
+
102
+ return Math.min(durationSeconds * 0.1, 1);
103
+ }
104
+
105
+ /**
106
+ * The address a `<video>` loads to show the poster frame itself, for a clip
107
+ * stored without a poster. The media fragment is what makes iOS Safari draw a
108
+ * frame under `preload="metadata"` at all, and a start of 0 is no fragment, so
109
+ * a clip of unknown length starts a tenth of a second in.
110
+ *
111
+ * @param url - The video file's address
112
+ * @param durationSeconds - Clip length in seconds, when known
113
+ * @returns The address with a `#t=` fragment at the poster frame
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * getPosterFrameUrl("https://media.example/clip.mp4", 4);
118
+ * // "https://media.example/clip.mp4#t=0.4"
119
+ * ```
120
+ */
121
+ export function getPosterFrameUrl(
122
+ url: string,
123
+ durationSeconds: number | null | undefined,
124
+ ): string {
125
+ const seconds =
126
+ Math.round(getPosterFrameSeconds(durationSeconds) * 100) / 100;
127
+ return `${url}#t=${seconds > 0 ? seconds : 0.1}`;
128
+ }
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Views wait here for a few seconds and leave as one write.
3
+ *
4
+ * Counting a view costs a database write, and a write is the one thing a
5
+ * well-read page must not multiply. So a hit is added to an in-memory map and
6
+ * the map is written out once, five seconds after its first hit: three
7
+ * hundred readers opening one post from one link in those seconds are one row
8
+ * with `count` three hundred. Under ordinary traffic nothing changes — a view
9
+ * is still one write — and under a spike the writes fall by one or two orders
10
+ * of magnitude.
11
+ *
12
+ * The map lives as long as the process that holds it: the whole run on Node,
13
+ * an isolate's lifetime on Workers, where `keepAlive` hands the pending flush
14
+ * to `waitUntil` so the isolate stays up for it. A process that dies between
15
+ * a hit and its flush loses those few seconds of counts, which is the price
16
+ * of never making a reader wait, and is accepted: the numbers are a guide,
17
+ * not a ledger. The Node runtime flushes on `close()`.
18
+ *
19
+ * The buffer also holds the daily stop: once a flush reports the instance's
20
+ * daily limit reached, hits are dropped until the next UTC day, when the
21
+ * free-tier D1 write quota that the limit protects resets.
22
+ */
23
+
24
+ /** How long a hit waits for company before the buffer is written out. */
25
+ export const VIEW_FLUSH_DELAY_MS = 5_000;
26
+
27
+ /** One reader opening one page, as the counter files it. */
28
+ export interface ViewHit {
29
+ siteId: string;
30
+ /** The site's calendar day, `YYYY-MM-DD`. */
31
+ day: string;
32
+ /** The site's hour, `YYYY-MM-DDTHH`, for today's chart. */
33
+ hour: string;
34
+ /** A post id, or the path of any other page. */
35
+ page: string;
36
+ /** The referring host, or `""`. */
37
+ referrer: string;
38
+ }
39
+
40
+ /** A hit with the number of times it happened. */
41
+ export interface ViewCountEntry extends ViewHit {
42
+ count: number;
43
+ }
44
+
45
+ /** What a sink reports back from writing a batch. */
46
+ export interface ViewRecordResult {
47
+ /** The instance has recorded its daily limit of views; stop until tomorrow. */
48
+ limitReached: boolean;
49
+ }
50
+
51
+ /** Where a flush goes, and how the runtime is kept alive for it. */
52
+ export interface ViewSink {
53
+ record(entries: ViewCountEntry[]): Promise<ViewRecordResult>;
54
+ /**
55
+ * Hands the pending flush to the runtime so it outlives the response that
56
+ * scheduled it. Workers pass `waitUntil`; Node needs nothing.
57
+ */
58
+ keepAlive?(promise: Promise<unknown>): void;
59
+ }
60
+
61
+ interface ScheduledFlush {
62
+ timer: ReturnType<typeof setTimeout>;
63
+ settle: () => void;
64
+ }
65
+
66
+ function entryKey(hit: ViewHit): string {
67
+ return `${hit.siteId}\n${hit.hour}\n${hit.page}\n${hit.referrer}`;
68
+ }
69
+
70
+ function nextUtcMidnight(nowMs: number): number {
71
+ const date = new Date(nowMs);
72
+ return Date.UTC(
73
+ date.getUTCFullYear(),
74
+ date.getUTCMonth(),
75
+ date.getUTCDate() + 1,
76
+ );
77
+ }
78
+
79
+ /**
80
+ * Collects view hits and writes them out in batches.
81
+ *
82
+ * One instance per process is the point; tests construct their own with a
83
+ * shorter delay and call `flush()` themselves.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const buffer = new ViewBuffer();
88
+ * buffer.add(hit, { record: (entries) => services.stats.record(entries, opts) });
89
+ * // five seconds later, one write
90
+ * ```
91
+ */
92
+ export class ViewBuffer {
93
+ readonly #delayMs: number;
94
+ readonly #now: () => number;
95
+ readonly #pending = new Map<string, ViewCountEntry>();
96
+ #sink: ViewSink | null = null;
97
+ #scheduled: ScheduledFlush | null = null;
98
+ #pausedUntil = 0;
99
+
100
+ /**
101
+ * @param options.delayMs - How long the first hit waits before the flush
102
+ * @param options.now - Clock in milliseconds, for tests
103
+ */
104
+ constructor(options: { delayMs?: number; now?: () => number } = {}) {
105
+ this.#delayMs = options.delayMs ?? VIEW_FLUSH_DELAY_MS;
106
+ this.#now = options.now ?? Date.now;
107
+ }
108
+
109
+ /** Hits waiting to be written, as distinct entries. */
110
+ get size(): number {
111
+ return this.#pending.size;
112
+ }
113
+
114
+ /** Whether the daily stop is in force right now. */
115
+ get paused(): boolean {
116
+ return this.#now() < this.#pausedUntil;
117
+ }
118
+
119
+ /**
120
+ * Add one hit and make sure a flush is coming.
121
+ *
122
+ * The sink of the latest hit is the one the flush uses: every sink in one
123
+ * process writes to the same database, so any of them will do.
124
+ *
125
+ * @param hit - The view
126
+ * @param sink - Where the batch goes
127
+ * @returns Nothing
128
+ */
129
+ add(hit: ViewHit, sink: ViewSink): void {
130
+ if (this.paused) return;
131
+ this.#sink = sink;
132
+
133
+ const key = entryKey(hit);
134
+ const entry = this.#pending.get(key);
135
+ if (entry) {
136
+ entry.count += 1;
137
+ } else {
138
+ this.#pending.set(key, { ...hit, count: 1 });
139
+ }
140
+
141
+ if (this.#scheduled) return;
142
+
143
+ let settle!: () => void;
144
+ const done = new Promise<void>((resolve) => {
145
+ settle = resolve;
146
+ });
147
+ const timer = setTimeout(() => {
148
+ this.#scheduled = null;
149
+ void this.flush().finally(settle);
150
+ }, this.#delayMs);
151
+ // A pending flush must not hold a stopping Node process open; the runtime
152
+ // drains the buffer on close instead.
153
+ (timer as { unref?: () => void }).unref?.();
154
+ this.#scheduled = { timer, settle };
155
+ sink.keepAlive?.(done);
156
+ }
157
+
158
+ /**
159
+ * Write out everything pending now.
160
+ *
161
+ * Never rejects: a failed write is logged and its hits are gone, because
162
+ * nothing upstream can do anything with the failure.
163
+ *
164
+ * @returns Nothing, once the write has settled
165
+ */
166
+ async flush(): Promise<void> {
167
+ const scheduled = this.#scheduled;
168
+ if (scheduled) {
169
+ this.#scheduled = null;
170
+ clearTimeout(scheduled.timer);
171
+ }
172
+
173
+ const entries = [...this.#pending.values()];
174
+ this.#pending.clear();
175
+ const sink = this.#sink;
176
+
177
+ try {
178
+ if (entries.length === 0 || !sink) return;
179
+ const { limitReached } = await sink.record(entries);
180
+ if (limitReached && !this.paused) {
181
+ this.#pausedUntil = nextUtcMidnight(this.#now());
182
+ // eslint-disable-next-line no-console -- An operator should see why a day's counts stop.
183
+ console.warn(
184
+ `[Jant] View counting paused until ${new Date(this.#pausedUntil).toISOString()}: STATS_DAILY_LIMIT reached.`,
185
+ );
186
+ }
187
+ } catch (error: unknown) {
188
+ const message = error instanceof Error ? error.message : String(error);
189
+ // eslint-disable-next-line no-console -- Background failures must be visible.
190
+ console.error(`[Jant] View counting flush error: ${message}`);
191
+ } finally {
192
+ // The sink closes over a request's services and context; once nothing
193
+ // waits for it, let it go. A hit added meanwhile brought its own.
194
+ if (this.#sink === sink && this.#pending.size === 0) this.#sink = null;
195
+ scheduled?.settle();
196
+ }
197
+ }
198
+ }
199
+
200
+ /** The process's buffer, shared by every request. */
201
+ export const viewBuffer = new ViewBuffer();