@code.store/arcxp-sdk-ts 5.3.0 → 5.3.2

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.js CHANGED
@@ -7,6 +7,7 @@ import FormData from 'form-data';
7
7
  import * as ws from 'ws';
8
8
  import { TextNode, HTMLElement, CommentNode, parse } from 'node-html-parser';
9
9
  import { decode } from 'html-entities';
10
+ import * as xmldoc from 'xmldoc';
10
11
  import encode from 'base32-encode';
11
12
  import { v5 } from 'uuid';
12
13
  import assert from 'node:assert';
@@ -889,7 +890,7 @@ const ArcAPI = (options) => {
889
890
  return API;
890
891
  };
891
892
 
892
- const ContentElement = {
893
+ const ContentElement$1 = {
893
894
  divider: () => {
894
895
  return {
895
896
  type: 'divider',
@@ -1121,7 +1122,7 @@ const ContentElement = {
1121
1122
  },
1122
1123
  };
1123
1124
 
1124
- const BLOCK_ELEMENT_TAGS = [
1125
+ const BLOCK_ELEMENT_TAGS$1 = [
1125
1126
  'ADDRESS',
1126
1127
  'ARTICLE',
1127
1128
  'ASIDE',
@@ -1160,7 +1161,7 @@ const BLOCK_ELEMENT_TAGS = [
1160
1161
 
1161
1162
  var html_constants = /*#__PURE__*/Object.freeze({
1162
1163
  __proto__: null,
1163
- BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1164
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS$1
1164
1165
  });
1165
1166
 
1166
1167
  const socialRegExps = {
@@ -1204,27 +1205,27 @@ function createSocial(url = '') {
1204
1205
  const embeds = [];
1205
1206
  const instagram = instagramURLParser(url);
1206
1207
  if (instagram) {
1207
- embeds.push(ContentElement.instagram(instagram));
1208
+ embeds.push(ContentElement$1.instagram(instagram));
1208
1209
  }
1209
1210
  const twitter = twitterURLParser(url);
1210
1211
  if (twitter) {
1211
- embeds.push(ContentElement.twitter(twitter));
1212
+ embeds.push(ContentElement$1.twitter(twitter));
1212
1213
  }
1213
1214
  const tiktok = tiktokURLParser(url);
1214
1215
  if (tiktok) {
1215
- embeds.push(ContentElement.tiktok(tiktok));
1216
+ embeds.push(ContentElement$1.tiktok(tiktok));
1216
1217
  }
1217
1218
  const youtube = youtubeURLParser(url);
1218
1219
  if (youtube) {
1219
- embeds.push(ContentElement.youtube(youtube));
1220
+ embeds.push(ContentElement$1.youtube(youtube));
1220
1221
  }
1221
1222
  const facebookPost = facebookPostURLParser(url);
1222
1223
  if (facebookPost) {
1223
- embeds.push(ContentElement.facebook_post(facebookPost));
1224
+ embeds.push(ContentElement$1.facebook_post(facebookPost));
1224
1225
  }
1225
1226
  const facebookVideo = facebookVideoURLParser(url);
1226
1227
  if (facebookVideo) {
1227
- embeds.push(ContentElement.facebook_video(facebookVideo));
1228
+ embeds.push(ContentElement$1.facebook_video(facebookVideo));
1228
1229
  }
1229
1230
  return embeds;
1230
1231
  }
@@ -1248,7 +1249,7 @@ var ContentElements = /*#__PURE__*/Object.freeze({
1248
1249
  youtubeURLParser: youtubeURLParser
1249
1250
  });
1250
1251
 
1251
- const isTextNode = (node) => {
1252
+ const isTextNode$1 = (node) => {
1252
1253
  return node instanceof TextNode;
1253
1254
  };
1254
1255
  const isHTMLElement = (node) => {
@@ -1282,7 +1283,7 @@ var html_utils = /*#__PURE__*/Object.freeze({
1282
1283
  htmlToText: htmlToText,
1283
1284
  isCommentNode: isCommentNode,
1284
1285
  isHTMLElement: isHTMLElement,
1285
- isTextNode: isTextNode,
1286
+ isTextNode: isTextNode$1,
1286
1287
  nodeTagIn: nodeTagIn,
1287
1288
  nodeTagIs: nodeTagIs
1288
1289
  });
@@ -1311,6 +1312,7 @@ var html_utils = /*#__PURE__*/Object.freeze({
1311
1312
  */
1312
1313
  class HTMLProcessor {
1313
1314
  constructor() {
1315
+ this.blockElementTags = BLOCK_ELEMENT_TAGS$1;
1314
1316
  this.parallelProcessing = true;
1315
1317
  this.handlers = {
1316
1318
  node: new Map(),
@@ -1430,7 +1432,7 @@ class HTMLProcessor {
1430
1432
  }
1431
1433
  });
1432
1434
  this.handle('text', (node) => {
1433
- if (isTextNode(node)) {
1435
+ if (isTextNode$1(node)) {
1434
1436
  return this.createText(node);
1435
1437
  }
1436
1438
  });
@@ -1608,23 +1610,23 @@ class HTMLProcessor {
1608
1610
  return [];
1609
1611
  }
1610
1612
  async handleTable(node) {
1611
- return [ContentElement.raw_html(node.toString())];
1613
+ return [ContentElement$1.raw_html(node.toString())];
1612
1614
  }
1613
1615
  async handleIframe(node) {
1614
- return [ContentElement.raw_html(node.toString())];
1616
+ return [ContentElement$1.raw_html(node.toString())];
1615
1617
  }
1616
1618
  async handleImage(node) {
1617
- return [ContentElement.raw_html(node.toString())];
1619
+ return [ContentElement$1.raw_html(node.toString())];
1618
1620
  }
1619
1621
  async handleBreak(_) {
1620
- return [ContentElement.divider()];
1622
+ return [ContentElement$1.divider()];
1621
1623
  }
1622
1624
  async createQuote(node) {
1623
1625
  const items = await this.handleNested(node);
1624
- return [ContentElement.quote(items)];
1626
+ return [ContentElement$1.quote(items)];
1625
1627
  }
1626
1628
  async createText(node) {
1627
- const text = ContentElement.text(node.text);
1629
+ const text = ContentElement$1.text(node.text);
1628
1630
  return [text];
1629
1631
  }
1630
1632
  filterListItems(items) {
@@ -1632,16 +1634,16 @@ class HTMLProcessor {
1632
1634
  }
1633
1635
  async createList(node, type) {
1634
1636
  const items = await this.handleNested(node);
1635
- return [ContentElement.list(type, this.filterListItems(items))];
1637
+ return [ContentElement$1.list(type, this.filterListItems(items))];
1636
1638
  }
1637
1639
  async createHeader(node) {
1638
1640
  const level = +node.tagName.split('H')[1] || 3;
1639
- return [ContentElement.header(node.innerText, level)];
1641
+ return [ContentElement$1.header(node.innerText, level)];
1640
1642
  }
1641
1643
  isBlockElement(node) {
1642
1644
  if (!isHTMLElement(node))
1643
1645
  return false;
1644
- const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS);
1646
+ const defaultBlockElements = new Set(this.blockElementTags);
1645
1647
  return defaultBlockElements.has(node.tagName);
1646
1648
  }
1647
1649
  warn(metadata, message) {
@@ -1649,17 +1651,241 @@ class HTMLProcessor {
1649
1651
  }
1650
1652
  }
1651
1653
 
1652
- var index$3 = /*#__PURE__*/Object.freeze({
1654
+ var index$4 = /*#__PURE__*/Object.freeze({
1653
1655
  __proto__: null,
1654
1656
  Constants: html_constants,
1655
1657
  HTMLProcessor: HTMLProcessor,
1656
1658
  Utils: html_utils
1657
1659
  });
1658
1660
 
1661
+ const BLOCK_ELEMENT_TAGS = ['paragraph', 'line', 'header', 'ul', 'ol', 'li', 'embed', 'iframe', 'table'];
1662
+
1663
+ var xml_constants = /*#__PURE__*/Object.freeze({
1664
+ __proto__: null,
1665
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1666
+ });
1667
+
1668
+ const isXmlElement = (node) => {
1669
+ return node?.type === 'element';
1670
+ };
1671
+ const isTextNode = (node) => {
1672
+ return node?.type === 'text';
1673
+ };
1674
+ const nodeNameIs = (node, name) => {
1675
+ return isXmlElement(node) && node.name === name;
1676
+ };
1677
+ const nodeNameIn = (node, names) => {
1678
+ return isXmlElement(node) && names.includes(node.name);
1679
+ };
1680
+
1681
+ var xml_utils = /*#__PURE__*/Object.freeze({
1682
+ __proto__: null,
1683
+ isTextNode: isTextNode,
1684
+ isXmlElement: isXmlElement,
1685
+ nodeNameIn: nodeNameIn,
1686
+ nodeNameIs: nodeNameIs
1687
+ });
1688
+
1689
+ const ContentElement = ContentElement$1;
1690
+ class XMLProcessor {
1691
+ constructor() {
1692
+ this.blockElementTags = BLOCK_ELEMENT_TAGS;
1693
+ this.handlers = {
1694
+ node: new Map(),
1695
+ wrap: new Map(),
1696
+ };
1697
+ }
1698
+ init() {
1699
+ // wrappers are used to wrap the content of nested text nodes
1700
+ // in a specific way
1701
+ this.wrap('link', (node, content) => {
1702
+ return `<a href="${node.attr.url || node.attr.href || '/'}">${content}</a>`;
1703
+ });
1704
+ this.wrap('header', (_node, content) => {
1705
+ return `<h3>${content}</h3>`;
1706
+ });
1707
+ this.wrap('emphasize', (_node, content) => {
1708
+ return `<i>${content}</i>`;
1709
+ });
1710
+ this.wrap('strong', (_node, content) => {
1711
+ return `<b>${content}</b>`;
1712
+ });
1713
+ // handlers are used to handle specific nodes
1714
+ // and return a list of content elements
1715
+ this.handle('default', (node) => {
1716
+ if (nodeNameIn(node, ['section', 'paragraph', 'line', 'header', 'emphasize', 'strong', 'link', 'li'])) {
1717
+ return this.handleNested(node);
1718
+ }
1719
+ });
1720
+ this.handle('text', (node) => {
1721
+ if (isTextNode(node)) {
1722
+ return [ContentElement.text(node.text)];
1723
+ }
1724
+ });
1725
+ this.handle('list', async (node) => {
1726
+ if (nodeNameIn(node, ['ul', 'ol'])) {
1727
+ const listType = node.name === 'ul' ? 'unordered' : 'ordered';
1728
+ return this.createList(node, listType);
1729
+ }
1730
+ });
1731
+ this.handle('table', (node) => {
1732
+ if (nodeNameIs(node, 'table')) {
1733
+ return this.handleTable(node);
1734
+ }
1735
+ });
1736
+ }
1737
+ async parse(xml) {
1738
+ const doc = new xmldoc.XmlDocument(xml);
1739
+ const elements = await this.process(doc);
1740
+ return elements || [];
1741
+ }
1742
+ handle(name, handler) {
1743
+ if (this.handlers.node.has(name)) {
1744
+ throw new Error(`${name} node handler already set`);
1745
+ }
1746
+ this.handlers.node.set(name, handler);
1747
+ }
1748
+ wrap(name, handler) {
1749
+ if (this.handlers.wrap.has(name)) {
1750
+ throw new Error(`${name} wrap handler already set`);
1751
+ }
1752
+ this.handlers.wrap.set(name, handler);
1753
+ }
1754
+ addTextAdditionalProperties(c, parent) {
1755
+ const additionalProperties = c.additional_properties || {};
1756
+ const parentNodeIsBlockElement = this.isBlockElement(parent);
1757
+ c.additional_properties = {
1758
+ ...c.additional_properties,
1759
+ isBlockElement: additionalProperties.isBlockElement || parentNodeIsBlockElement,
1760
+ };
1761
+ return c;
1762
+ }
1763
+ wrapChildrenTextNodes(node, elements) {
1764
+ const wrapped = [];
1765
+ for (const c of elements) {
1766
+ if (!isTextCE(c)) {
1767
+ wrapped.push(c);
1768
+ continue;
1769
+ }
1770
+ this.addTextAdditionalProperties(c, node);
1771
+ const handler = this.handlers.wrap.get(node.name);
1772
+ if (handler) {
1773
+ wrapped.push({
1774
+ ...c,
1775
+ content: handler(node, c.content),
1776
+ });
1777
+ }
1778
+ else {
1779
+ wrapped.push(c);
1780
+ }
1781
+ }
1782
+ return wrapped;
1783
+ }
1784
+ async handleNested(node) {
1785
+ const children = await Promise.all(node.children.map((child) => this.process(child)));
1786
+ const filtered = children.filter(Boolean).flat();
1787
+ const merged = this.mergeParagraphs(filtered);
1788
+ const wrapped = this.wrapChildrenTextNodes(node, merged);
1789
+ return wrapped;
1790
+ }
1791
+ async process(node) {
1792
+ let isKnownNode = false;
1793
+ const elements = [];
1794
+ for (const [name, handler] of this.handlers.node.entries()) {
1795
+ try {
1796
+ const result = await handler(node);
1797
+ if (Array.isArray(result)) {
1798
+ // if handler returns an array of elements, it means that the node was handled properly, even if there is no elements inside
1799
+ isKnownNode = true;
1800
+ elements.push(...result);
1801
+ break;
1802
+ }
1803
+ }
1804
+ catch (error) {
1805
+ this.warn({ node: node.toString(), error: error.toString(), name }, 'HandlerError');
1806
+ }
1807
+ }
1808
+ if (isKnownNode)
1809
+ return elements;
1810
+ this.warn({ node: node.toString(), type: node.type }, 'UnknownNodeError');
1811
+ }
1812
+ mergeParagraphs(items) {
1813
+ const merged = [];
1814
+ let toMerge = [];
1815
+ const merge = () => {
1816
+ if (!toMerge.length)
1817
+ return;
1818
+ const paragraph = toMerge.reduce((acc, p) => {
1819
+ return {
1820
+ ...p,
1821
+ content: acc.content + p.content,
1822
+ };
1823
+ }, { type: 'text', content: '' });
1824
+ merged.push(paragraph);
1825
+ toMerge = [];
1826
+ };
1827
+ for (let i = 0; i < items.length; i++) {
1828
+ const item = items[i];
1829
+ const isBlockElement = item.additional_properties?.isBlockElement;
1830
+ if (isTextCE(item) && !isBlockElement) {
1831
+ toMerge.push(item);
1832
+ }
1833
+ else {
1834
+ merge();
1835
+ merged.push(item);
1836
+ }
1837
+ }
1838
+ merge();
1839
+ return merged;
1840
+ }
1841
+ async handleTable(node) {
1842
+ const html = node.toString({ html: true });
1843
+ return [ContentElement.raw_html(html)];
1844
+ }
1845
+ async createQuote(node) {
1846
+ const items = await this.handleNested(node);
1847
+ return [ContentElement.quote(items)];
1848
+ }
1849
+ async createList(node, type) {
1850
+ const items = await this.handleNested(node);
1851
+ return [ContentElement.list(type, items)];
1852
+ }
1853
+ getNodeInnerText(node) {
1854
+ return node.children.map((n) => this.htmlFromNode(n).innerText.trim());
1855
+ }
1856
+ getNodeInnerHTML(node) {
1857
+ return node.children.map((n) => this.htmlFromNode(n).innerHTML.trim());
1858
+ }
1859
+ htmlFromNode(node) {
1860
+ return parse(node.toString({ html: true }));
1861
+ }
1862
+ getDecodedHTMLFromInnerNodes(node) {
1863
+ const encodedHtml = this.getNodeInnerText(node).join('');
1864
+ const decoded = decodeHTMLEntities(encodedHtml);
1865
+ return decoded;
1866
+ }
1867
+ isBlockElement(node) {
1868
+ const defaultBlockElements = new Set(this.blockElementTags);
1869
+ if (defaultBlockElements.has(node.name))
1870
+ return true;
1871
+ }
1872
+ warn(metadata, message) {
1873
+ console.warn(metadata, message);
1874
+ }
1875
+ }
1876
+
1877
+ var index$3 = /*#__PURE__*/Object.freeze({
1878
+ __proto__: null,
1879
+ Constants: xml_constants,
1880
+ Utils: xml_utils,
1881
+ XMLProcessor: XMLProcessor
1882
+ });
1883
+
1659
1884
  var index$2 = /*#__PURE__*/Object.freeze({
1660
1885
  __proto__: null,
1661
- ContentElement: ContentElement,
1662
- HTML: index$3
1886
+ ContentElement: ContentElement$1,
1887
+ HTML: index$4,
1888
+ XML: index$3
1663
1889
  });
1664
1890
 
1665
1891
  /**