@easy-cms/plugin-form-builder 0.44.0 → 0.46.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 (2) hide show
  1. package/dist/index.js +34 -55
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -195,50 +195,19 @@ function fieldBlocks(localized, kinds) {
195
195
  // src/info.ts
196
196
  var INFO = {
197
197
  name: "@easy-cms/plugin-form-builder",
198
- ...true ? { version: "0.44.0" } : {}
198
+ ...true ? { version: "0.46.0" } : {}
199
199
  };
200
200
 
201
201
  // src/spam.ts
202
- import { createHmac, timingSafeEqual } from "crypto";
203
- var MAX_AGE = 24 * 36e5;
204
- var sign = (secret, value) => createHmac("sha256", secret).update(value).digest("base64url");
205
- function formToken(secret, slug, now = Date.now()) {
206
- return `${now}.${sign(secret, `easy-form:${slug}:${now}`)}`;
207
- }
208
- function checkToken(secret, slug, token, minTime, now = Date.now()) {
209
- if (typeof token !== "string") return "invalid";
210
- const [time, signature] = token.split(".");
211
- const issued = Number(time);
212
- if (!Number.isFinite(issued) || !signature) return "invalid";
213
- const expected = Buffer.from(sign(secret, `easy-form:${slug}:${issued}`));
214
- const given = Buffer.from(signature);
215
- if (expected.length !== given.length || !timingSafeEqual(expected, given)) return "invalid";
216
- if (now - issued < minTime) return "too-fast";
217
- if (now - issued > MAX_AGE) return "expired";
218
- return "ok";
219
- }
220
- function rateKeys(secret, ip, form, window, now = Date.now()) {
221
- const bucket = Math.floor(now / (window * 1e3));
222
- const key = (b) => sign(secret, `easy-form-rate:${ip}:${String(form)}:${b}`).slice(0, 32);
223
- return { current: key(bucket), previous: key(bucket - 1) };
224
- }
225
- function honeypotName(names) {
226
- return ["website", "homepage", "url_address"].find((n) => !names.includes(n)) ?? "hp_field";
227
- }
228
- async function verifyTurnstile(secretKey, response, ip, fetcher = fetch) {
229
- if (typeof response !== "string" || !response) return false;
230
- const body = new URLSearchParams({ secret: secretKey, response, ...ip ? { remoteip: ip } : {} });
231
- try {
232
- const result = await fetcher("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
233
- method: "POST",
234
- body,
235
- signal: AbortSignal.timeout(1e4)
236
- });
237
- return (await result.json()).success === true;
238
- } catch {
239
- return false;
240
- }
241
- }
202
+ import {
203
+ checkFormToken,
204
+ formToken as coreFormToken,
205
+ rateKeys as coreRateKeys
206
+ } from "@easy-cms/core";
207
+ import { honeypotName, verifyTurnstile } from "@easy-cms/core";
208
+ var formToken = (secret, slug, now = Date.now()) => coreFormToken(secret, `easy-form:${slug}`, now);
209
+ var checkToken = (secret, slug, token, minTime, now = Date.now()) => checkFormToken(secret, `easy-form:${slug}`, token, minTime, now);
210
+ var rateKeys = (secret, ip, form, window, now = Date.now()) => coreRateKeys(secret, `easy-form-rate:${ip}:${String(form)}`, window, now);
242
211
 
243
212
  // src/validate.ts
244
213
  var MESSAGES = {
@@ -491,9 +460,15 @@ function formBuilderPlugin(options = {}) {
491
460
  }
492
461
  ]
493
462
  };
494
- const findForm = async (cms, slug, locale) => {
463
+ const findForm = async (cms, slug, locale, context) => {
464
+ const scope = await cms.uniqueScope(forms, "slug", { context });
495
465
  const { docs } = await cms.find(forms, {
496
- where: { slug: { equals: slug } },
466
+ where: {
467
+ and: [
468
+ { slug: { equals: slug } },
469
+ ...Object.entries(scope).map(([name, value]) => ({ [name]: { equals: value } }))
470
+ ]
471
+ },
497
472
  limit: 1,
498
473
  depth: 0,
499
474
  ...locale ? { locale } : {}
@@ -561,10 +536,10 @@ function formBuilderPlugin(options = {}) {
561
536
  const getEndpoint = {
562
537
  path: "/form/:slug",
563
538
  method: "get",
564
- handler: async ({ params, url, cms }) => {
539
+ handler: async ({ params, url, cms, context }) => {
565
540
  const slug = params.slug;
566
541
  const locale = localeOf(url.searchParams.get("locale"));
567
- const form = await findForm(cms, slug, locale);
542
+ const form = await findForm(cms, slug, locale, context);
568
543
  if (!form) return fail(404, [{ message: `No published form "${slug}"` }]);
569
544
  return Response.json(publicForm(form, slug, locale), {
570
545
  // The token is fresh on every request.
@@ -575,11 +550,11 @@ function formBuilderPlugin(options = {}) {
575
550
  const submitEndpoint = {
576
551
  path: "/form/:slug/submit",
577
552
  method: "post",
578
- handler: async ({ params, json, ip, cms }) => {
553
+ handler: async ({ params, json, ip, cms, context }) => {
579
554
  const slug = params.slug;
580
555
  const body = await json();
581
556
  const locale = localeOf(body.locale);
582
- const form = await findForm(cms, slug, locale);
557
+ const form = await findForm(cms, slug, locale, context);
583
558
  if (!form) return fail(404, [{ message: `No published form "${slug}"` }]);
584
559
  const confirmation = confirmationOf(form, locale);
585
560
  const reload = locale === "th" ? "\u0E1F\u0E2D\u0E23\u0E4C\u0E21\u0E2B\u0E21\u0E14\u0E2D\u0E32\u0E22\u0E38 \u0E42\u0E1B\u0E23\u0E14\u0E42\u0E2B\u0E25\u0E14\u0E2B\u0E19\u0E49\u0E32\u0E43\u0E2B\u0E21\u0E48\u0E41\u0E25\u0E49\u0E27\u0E25\u0E2D\u0E07\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07" : "The form expired: reload the page and try again";
@@ -619,14 +594,18 @@ function formBuilderPlugin(options = {}) {
619
594
  rateKey = keys.current;
620
595
  }
621
596
  const page = typeof body.page === "string" ? body.page.slice(0, 500) : null;
622
- await cms.create(submissions, {
623
- form: form.id,
624
- data: values,
625
- summary: summarize(rowsOf(form), values),
626
- locale,
627
- page,
628
- ...rateKey ? { rateKey } : {}
629
- });
597
+ await cms.create(
598
+ submissions,
599
+ {
600
+ form: form.id,
601
+ data: values,
602
+ summary: summarize(rowsOf(form), values),
603
+ locale,
604
+ page,
605
+ ...rateKey ? { rateKey } : {}
606
+ },
607
+ { context }
608
+ );
630
609
  for (const email of buildEmails({
631
610
  form,
632
611
  fields: rowsOf(form),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/plugin-form-builder",
3
- "version": "0.44.0",
3
+ "version": "0.46.0",
4
4
  "description": "Forms for Easy CMS: build forms in the admin, collect submissions, email notifications, spam protection and an <easy-form> element",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,15 +23,15 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@easy-cms/richtext": "^0.44.0"
26
+ "@easy-cms/richtext": "^0.46.0"
27
27
  },
28
28
  "peerDependencies": {
29
- "@easy-cms/core": "^0.44.0"
29
+ "@easy-cms/core": "^0.46.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "happy-dom": "^20.14.5",
33
- "@easy-cms/core": "^0.44.0",
34
- "@easy-cms/db-sqlite": "^0.44.0"
33
+ "@easy-cms/core": "^0.46.0",
34
+ "@easy-cms/db-sqlite": "^0.46.0"
35
35
  },
36
36
  "engines": {
37
37
  "node": ">=22.12"