@broberg/legal 0.9.0 → 0.10.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/dist/index.cjs CHANGED
@@ -1573,6 +1573,37 @@ function renderHtml(doc) {
1573
1573
  return `<article class="legal" lang="${doc.locale}" data-legal-doc="${doc.doc}" data-legal-version="${esc(doc.version)}" data-legal-hash="${doc.contentHash}"><h1>${esc(doc.title)}</h1>${draft}${doc.sections.map((s) => `<section data-legal-id="${esc(s.id)}"><h2>${esc(s.heading)}</h2>${s.blocks.map(block).join("")}</section>`).join("")}</article>`;
1574
1574
  }
1575
1575
 
1576
+ // src/fill.ts
1577
+ var KNOWN2 = new Set(PLACEHOLDERS);
1578
+ var RE = /\{([a-zA-Z.]+)\}/g;
1579
+ function fillPlaceholders(doc, values) {
1580
+ const unknown = Object.keys(values).filter((k) => !KNOWN2.has(k));
1581
+ if (unknown.length) throw new Error(`unknown placeholder value(s): ${unknown.join(", ")}`);
1582
+ const missing = doc.placeholders.filter((p) => typeof values[p] !== "string" || values[p].trim() === "");
1583
+ if (missing.length) throw new Error(`missing or empty placeholder value(s): ${missing.join(", ")}`);
1584
+ const fill = (s) => s.replace(RE, (whole, name) => KNOWN2.has(name) && values[name] !== void 0 ? values[name] : whole);
1585
+ const block2 = (b) => b.type === "list" ? { ...b, items: b.items.map(fill) } : b.type === "table" ? { ...b, head: b.head.map(fill), rows: b.rows.map((r) => r.map(fill)) } : { ...b, text: fill(b.text) };
1586
+ const { contentHash: _old, ...rest } = doc;
1587
+ const body = { ...rest, title: fill(doc.title), sections: doc.sections.map((s) => ({ ...s, heading: fill(s.heading), blocks: s.blocks.map(block2) })), placeholders: [] };
1588
+ return { ...body, contentHash: sha256Hex(canonicalJson(body)) };
1589
+ }
1590
+ function renderText(doc) {
1591
+ const out = [doc.title, ""];
1592
+ for (const s of doc.sections) {
1593
+ out.push(s.heading);
1594
+ for (const b of s.blocks) {
1595
+ if (b.type === "list") out.push(...b.items.map((i) => `- ${i}`));
1596
+ else if (b.type === "table") out.push(b.head.join(" | "), ...b.rows.map((r) => r.join(" | ")));
1597
+ else out.push(b.text);
1598
+ }
1599
+ out.push("");
1600
+ }
1601
+ return out.join("\n");
1602
+ }
1603
+ function textHash(text) {
1604
+ return `sha256:${sha256Hex(text)}`;
1605
+ }
1606
+
1576
1607
  // src/content/example.ts
1577
1608
  var EXAMPLE_FACTS = {
1578
1609
  id: "example",
@@ -1649,6 +1680,7 @@ function createLegalCache(opts) {
1649
1680
 
1650
1681
  // src/bid.ts
1651
1682
  var BID_URL = "https://id.broberg.ai";
1683
+ var MAX_ACCEPTED_TEXT = 2e5;
1652
1684
  var BidConsentError = class extends Error {
1653
1685
  constructor(status, code, message) {
1654
1686
  super(message);
@@ -1681,8 +1713,14 @@ async function call(opts, method, path, body) {
1681
1713
  }
1682
1714
  async function recordAcceptance(opts, a) {
1683
1715
  const sent = { doc: a.doc, version: a.version, contentHash: toBidHash(a.contentHash) };
1716
+ if (a.text !== void 0) {
1717
+ if (a.text.length > MAX_ACCEPTED_TEXT) throw new BidConsentError(0, "text_too_long", `the accepted text is ${a.text.length} characters; BID takes at most ${MAX_ACCEPTED_TEXT}`);
1718
+ if (textHash(a.text) !== sent.contentHash) throw new BidConsentError(0, "hash_mismatch", "contentHash is not textHash(text) \u2014 the hash must be over exactly the text the person saw");
1719
+ sent.text = a.text;
1720
+ }
1721
+ if (a.authorityConfirmed !== void 0) sent.authorityConfirmed = a.authorityConfirmed;
1684
1722
  const row = await call(opts, "POST", "/api/app/terms-acceptance", sent);
1685
- if (row?.doc !== sent.doc || row?.version !== sent.version || row?.contentHash !== sent.contentHash) {
1723
+ if (row?.doc !== sent.doc || row?.version !== sent.version || row?.contentHash !== sent.contentHash || a.authorityConfirmed !== void 0 && row?.authorityConfirmed !== a.authorityConfirmed) {
1686
1724
  throw new BidConsentError(200, "not_echoed", "BID's stored acceptance does not match what was sent");
1687
1725
  }
1688
1726
  return row;
@@ -1747,11 +1785,13 @@ exports.DOC_IDS = DOC_IDS;
1747
1785
  exports.EXAMPLE_FACTS = EXAMPLE_FACTS;
1748
1786
  exports.LEGAL_API_URL = LEGAL_API_URL;
1749
1787
  exports.MASTER_LOCALE = MASTER_LOCALE;
1788
+ exports.MAX_ACCEPTED_TEXT = MAX_ACCEPTED_TEXT;
1750
1789
  exports.PLACEHOLDERS = PLACEHOLDERS;
1751
1790
  exports.SOURCES = SOURCES;
1752
1791
  exports.canonicalJson = canonicalJson;
1753
1792
  exports.createLegalCache = createLegalCache;
1754
1793
  exports.createLibrary = createLibrary;
1794
+ exports.fillPlaceholders = fillPlaceholders;
1755
1795
  exports.getAcceptance = getAcceptance;
1756
1796
  exports.getConsent = getConsent;
1757
1797
  exports.getDocument = getDocument;
@@ -1763,10 +1803,12 @@ exports.placeholdersIn = placeholdersIn;
1763
1803
  exports.recordAcceptance = recordAcceptance;
1764
1804
  exports.recordConsent = recordConsent;
1765
1805
  exports.renderHtml = renderHtml;
1806
+ exports.renderText = renderText;
1766
1807
  exports.resolveTemplate = resolveTemplate;
1767
1808
  exports.sha256Hex = sha256Hex;
1768
1809
  exports.sourceHash = sourceHash;
1769
1810
  exports.templateHash = templateHash;
1811
+ exports.textHash = textHash;
1770
1812
  exports.toBidHash = toBidHash;
1771
1813
  exports.verifyTemplate = verifyTemplate;
1772
1814
  //# sourceMappingURL=index.cjs.map