@velocity-astro/cms-astro 0.1.0-beta.2 → 0.1.0-beta.4
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/dist/admin-files.d.ts +2 -2
- package/dist/admin-files.d.ts.map +1 -1
- package/dist/admin-files.js +13 -7
- package/dist/admin-files.js.map +1 -1
- package/dist/challenge.d.ts +35 -0
- package/dist/challenge.d.ts.map +1 -0
- package/dist/challenge.js +84 -0
- package/dist/challenge.js.map +1 -0
- package/dist/connection.d.ts +1 -0
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +31 -6
- package/dist/connection.js.map +1 -1
- package/dist/editor.d.ts +2 -0
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +9 -1
- package/dist/editor.js.map +1 -1
- package/dist/form-post.d.ts +9 -1
- package/dist/form-post.d.ts.map +1 -1
- package/dist/form-post.js +10 -3
- package/dist/form-post.js.map +1 -1
- package/dist/hosted.d.ts +76 -0
- package/dist/hosted.d.ts.map +1 -0
- package/dist/hosted.js +104 -0
- package/dist/hosted.js.map +1 -0
- package/dist/image-optimizer.d.ts +35 -0
- package/dist/image-optimizer.d.ts.map +1 -0
- package/dist/image-optimizer.js +76 -0
- package/dist/image-optimizer.js.map +1 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/dist/integration.d.ts +9 -0
- package/dist/integration.d.ts.map +1 -1
- package/dist/integration.js +15 -1
- package/dist/integration.js.map +1 -1
- package/dist/media-store.d.ts +15 -0
- package/dist/media-store.d.ts.map +1 -1
- package/dist/media-store.js +26 -2
- package/dist/media-store.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +13 -6
- package/dist/preview.js.map +1 -1
- package/dist/resend-mailer.d.ts +18 -1
- package/dist/resend-mailer.d.ts.map +1 -1
- package/dist/resend-mailer.js +24 -3
- package/dist/resend-mailer.js.map +1 -1
- package/dist/secret.d.ts +1 -1
- package/dist/secret.d.ts.map +1 -1
- package/dist/secret.js +3 -2
- package/dist/secret.js.map +1 -1
- package/dist/single.d.ts +18 -0
- package/dist/single.d.ts.map +1 -0
- package/dist/single.js +37 -0
- package/dist/single.js.map +1 -0
- package/dist/site-editor.d.ts +1 -1
- package/dist/site-editor.d.ts.map +1 -1
- package/dist/site-editor.js +36 -6
- package/dist/site-editor.js.map +1 -1
- package/dist/site-forms.d.ts +11 -0
- package/dist/site-forms.d.ts.map +1 -1
- package/dist/site-forms.js +37 -19
- package/dist/site-forms.js.map +1 -1
- package/dist/site-mail.d.ts +45 -0
- package/dist/site-mail.d.ts.map +1 -0
- package/dist/site-mail.js +84 -0
- package/dist/site-mail.js.map +1 -0
- package/dist/site-media.d.ts +5 -1
- package/dist/site-media.d.ts.map +1 -1
- package/dist/site-media.js +14 -5
- package/dist/site-media.js.map +1 -1
- package/dist/site-name.d.ts +9 -0
- package/dist/site-name.d.ts.map +1 -0
- package/dist/site-name.js +21 -0
- package/dist/site-name.js.map +1 -0
- package/dist/site-storage.d.ts +34 -0
- package/dist/site-storage.d.ts.map +1 -0
- package/dist/site-storage.js +118 -0
- package/dist/site-storage.js.map +1 -0
- package/package.json +10 -7
- package/routes/editor.ts +7 -6
- package/routes/form-client.js.ts +68 -0
- package/routes/media.ts +2 -2
- package/routes/published.ts +2 -2
- package/routes/sitemap-live.xml.ts +4 -1
- package/routes/sites.ts +11 -0
package/routes/form-client.js.ts
CHANGED
|
@@ -15,6 +15,11 @@ export const prerender = false;
|
|
|
15
15
|
* - a submit button may carry `data-cms-form-sending`, its words while the form is on its way.
|
|
16
16
|
* They replace the button's words, or those of the `[data-cms-form-label]` inside it.
|
|
17
17
|
*
|
|
18
|
+
* - `[data-cms-form-challenge]`: where the check that the sender is a person is drawn, when an
|
|
19
|
+
* owner set one up (Cloudflare Turnstile). Without the mark it is drawn above the submit
|
|
20
|
+
* button. A site with a content security policy allows `https://challenges.cloudflare.com`
|
|
21
|
+
* for scripts and frames.
|
|
22
|
+
*
|
|
18
23
|
* Every word is written as text. Nothing the visitor typed comes back from the site.
|
|
19
24
|
*/
|
|
20
25
|
const SCRIPT = `
|
|
@@ -39,6 +44,66 @@ function say(form, state, message) {
|
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
const CHALLENGE = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
|
|
48
|
+
const challenges = new WeakMap();
|
|
49
|
+
const waiting = new WeakMap();
|
|
50
|
+
let challengeScript;
|
|
51
|
+
|
|
52
|
+
/** Loads the challenge's script once, however many forms a page has. */
|
|
53
|
+
function challengeReady() {
|
|
54
|
+
challengeScript ??= new Promise((resolve, reject) => {
|
|
55
|
+
if (window.turnstile) return resolve();
|
|
56
|
+
const script = document.createElement('script');
|
|
57
|
+
script.src = CHALLENGE;
|
|
58
|
+
script.async = true;
|
|
59
|
+
script.onload = () => resolve();
|
|
60
|
+
script.onerror = () => {
|
|
61
|
+
challengeScript = undefined;
|
|
62
|
+
reject(new Error('The challenge could not be loaded.'));
|
|
63
|
+
};
|
|
64
|
+
document.head.append(script);
|
|
65
|
+
});
|
|
66
|
+
return challengeScript;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Draws the check that the sender is a person, when the form carries a site key for one. */
|
|
70
|
+
function challenge(form) {
|
|
71
|
+
const siteKey = form.elements.namedItem('_challenge')?.value;
|
|
72
|
+
if (!siteKey || challenges.has(form)) return;
|
|
73
|
+
let place = form.querySelector('[data-cms-form-challenge]');
|
|
74
|
+
if (!place) {
|
|
75
|
+
place = document.createElement('div');
|
|
76
|
+
place.dataset.cmsFormChallenge = '';
|
|
77
|
+
const button = form.querySelector('[type="submit"]');
|
|
78
|
+
if (button) button.before(place);
|
|
79
|
+
else form.append(place);
|
|
80
|
+
}
|
|
81
|
+
challenges.set(
|
|
82
|
+
form,
|
|
83
|
+
challengeReady()
|
|
84
|
+
.then(() => window.turnstile.render(place, { sitekey: siteKey, theme: 'auto', size: 'flexible', language: document.documentElement.lang || 'auto', callback: () => waiting.get(form)?.() }))
|
|
85
|
+
// Without the challenge the form still posts. The site decides what to make of it.
|
|
86
|
+
.catch(() => undefined),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Waits, for a while, for the sender to pass a challenge that is still being worked out. */
|
|
91
|
+
async function challengePassed(form) {
|
|
92
|
+
const widget = await challenges.get(form);
|
|
93
|
+
if (widget == null || window.turnstile.getResponse(widget)) return;
|
|
94
|
+
await new Promise((resolve) => {
|
|
95
|
+
waiting.set(form, resolve);
|
|
96
|
+
setTimeout(resolve, 15000);
|
|
97
|
+
});
|
|
98
|
+
waiting.delete(form);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** An answer to the challenge is used once: a new one is asked for after every post. */
|
|
102
|
+
async function challengeAgain(form) {
|
|
103
|
+
const widget = await challenges.get(form);
|
|
104
|
+
if (widget != null) window.turnstile?.reset(widget);
|
|
105
|
+
}
|
|
106
|
+
|
|
42
107
|
async function send(form) {
|
|
43
108
|
const button = form.querySelector('[type="submit"]');
|
|
44
109
|
const words = button?.querySelector('[data-cms-form-label]') ?? button;
|
|
@@ -50,6 +115,7 @@ async function send(form) {
|
|
|
50
115
|
if (button.dataset.cmsFormSending) words.textContent = button.dataset.cmsFormSending;
|
|
51
116
|
}
|
|
52
117
|
try {
|
|
118
|
+
await challengePassed(form);
|
|
53
119
|
const response = await fetch(form.action, { method: 'POST', headers: { accept: 'application/json' }, body: new URLSearchParams(new FormData(form)) });
|
|
54
120
|
const answer = await response.json().catch(() => null);
|
|
55
121
|
if (response.ok && answer?.ok) {
|
|
@@ -75,6 +141,7 @@ async function send(form) {
|
|
|
75
141
|
} catch {
|
|
76
142
|
say(form, 'failed', form.dataset.cmsFormFailed ?? '');
|
|
77
143
|
} finally {
|
|
144
|
+
void challengeAgain(form);
|
|
78
145
|
if (button) {
|
|
79
146
|
button.disabled = false;
|
|
80
147
|
words.textContent = label;
|
|
@@ -86,6 +153,7 @@ function start() {
|
|
|
86
153
|
for (const form of document.querySelectorAll('form[data-cms-form]')) {
|
|
87
154
|
if (form.dataset.cmsFormReady) continue;
|
|
88
155
|
form.dataset.cmsFormReady = 'true';
|
|
156
|
+
challenge(form);
|
|
89
157
|
form.addEventListener('submit', (event) => {
|
|
90
158
|
event.preventDefault();
|
|
91
159
|
if (form.dataset.cmsFormState !== 'sending') void send(form);
|
package/routes/media.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { uploadedFile } from '@velocity-astro/cms-astro';
|
|
|
4
4
|
export const prerender = false;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* An image someone uploaded in the editor. It may be cached, and says so itself: a file never
|
|
7
|
+
* An image someone uploaded in the editor, or with `?w=` a smaller copy of it. It may be cached, and says so itself: a file never
|
|
8
8
|
* changes once it is kept.
|
|
9
9
|
*/
|
|
10
|
-
export const GET: APIRoute = ({ params }) => uploadedFile(params.file ?? '');
|
|
10
|
+
export const GET: APIRoute = ({ params, url }) => uploadedFile(params.file ?? '', url.searchParams.get('w'));
|
package/routes/published.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { APIRoute } from 'astro';
|
|
2
|
-
import { hasSiteSecret, indexPages, indexSite, invalidateLiveContent } from '@velocity-astro/cms-astro';
|
|
2
|
+
import { hasSiteSecret, indexPages, indexSite, invalidateLiveContent, siteCacheTag } from '@velocity-astro/cms-astro';
|
|
3
3
|
import { ownOrigin } from './editor.ts';
|
|
4
4
|
import { addresses, clientDir } from './pages.ts';
|
|
5
5
|
|
|
@@ -18,7 +18,7 @@ interface Notice {
|
|
|
18
18
|
export const POST: APIRoute = async ({ cache, request }) => {
|
|
19
19
|
if (!hasSiteSecret(request)) return new Response(null, { status: 404 });
|
|
20
20
|
invalidateLiveContent();
|
|
21
|
-
await cache.invalidate({ tags: [
|
|
21
|
+
await cache.invalidate({ tags: [siteCacheTag()] });
|
|
22
22
|
const { reindex } = (await request.json().catch(() => ({}))) as Notice;
|
|
23
23
|
const origin = ownOrigin(request);
|
|
24
24
|
if (!reindex || origin === null) return Response.json({});
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { APIRoute } from 'astro';
|
|
2
|
+
import { currentSite } from '@velocity-astro/cms-astro';
|
|
2
3
|
import { addresses } from './pages.ts';
|
|
3
4
|
|
|
4
5
|
export const prerender = false;
|
|
5
6
|
|
|
6
7
|
/** The live pages' share of the sitemap. Point `sitemap-index.xml` here with `customSitemaps`. */
|
|
7
8
|
export const GET: APIRoute = async ({ site, url }) => {
|
|
9
|
+
// On a server that runs many sites, the addresses are the asked site's, not the template's.
|
|
10
|
+
const base = currentSite()?.origin ?? site ?? url;
|
|
8
11
|
const urls = (await addresses())
|
|
9
12
|
.filter((address) => address.listed && !address.fixed)
|
|
10
|
-
.map((address) => `<url><loc>${new URL(address.path,
|
|
13
|
+
.map((address) => `<url><loc>${new URL(address.path, base).href}</loc></url>`);
|
|
11
14
|
const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join('')}</urlset>`;
|
|
12
15
|
return new Response(xml, { headers: { 'Content-Type': 'application/xml; charset=utf-8' } });
|
|
13
16
|
};
|
package/routes/sites.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chooses the site each request is for, on a server that runs many sites from this one copy.
|
|
3
|
+
* Compiled with the site, like every file here, so the site's bundler reads the file that knows
|
|
4
|
+
* the sites; it runs first, before anything reads content.
|
|
5
|
+
*/
|
|
6
|
+
import { hostedRequest, useSiteRegistry } from '@velocity-astro/cms-astro';
|
|
7
|
+
import sites from 'virtual:velocity-cms/sites';
|
|
8
|
+
|
|
9
|
+
useSiteRegistry(sites);
|
|
10
|
+
|
|
11
|
+
export const onRequest = hostedRequest;
|