@liveblocks/node 1.8.0 → 1.8.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.mjs CHANGED
@@ -1,11 +1,16 @@
1
+ // src/index.ts
2
+ import { detectDupes } from "@liveblocks/core";
3
+
4
+ // src/version.ts
5
+ var PKG_NAME = "@liveblocks/node";
6
+ var PKG_VERSION = "1.8.2";
7
+ var PKG_FORMAT = "esm";
8
+
1
9
  // src/utils.ts
2
10
  var DEFAULT_BASE_URL = "https://api.liveblocks.io";
3
11
  async function fetchPolyfill() {
4
12
  return typeof globalThis.fetch !== "undefined" ? globalThis.fetch : (await import("node-fetch")).default;
5
13
  }
6
- function isSomething(input) {
7
- return input !== null && input !== void 0;
8
- }
9
14
  function isNonEmpty(value) {
10
15
  return typeof value === "string" && value.length > 0;
11
16
  }
@@ -830,309 +835,6 @@ var LiveblocksError = class extends Error {
830
835
  }
831
836
  };
832
837
 
833
- // src/comment-body.ts
834
- function isCommentBodyParagraph(element) {
835
- return "type" in element && element.type === "mention";
836
- }
837
- function isCommentBodyText(element) {
838
- return "text" in element && typeof element.text === "string";
839
- }
840
- function isCommentBodyMention(element) {
841
- return "type" in element && element.type === "mention";
842
- }
843
- function isCommentBodyLink(element) {
844
- return "type" in element && element.type === "link";
845
- }
846
- var commentBodyElementsGuards = {
847
- paragraph: isCommentBodyParagraph,
848
- text: isCommentBodyText,
849
- link: isCommentBodyLink,
850
- mention: isCommentBodyMention
851
- };
852
- var commentBodyElementsTypes = {
853
- paragraph: "block",
854
- text: "inline",
855
- link: "inline",
856
- mention: "inline"
857
- };
858
- function traverseCommentBody(body, elementOrVisitor, possiblyVisitor) {
859
- if (!body || !body?.content) {
860
- return;
861
- }
862
- const element = typeof elementOrVisitor === "string" ? elementOrVisitor : void 0;
863
- const type = element ? commentBodyElementsTypes[element] : "all";
864
- const guard = element ? commentBodyElementsGuards[element] : () => true;
865
- const visitor = typeof elementOrVisitor === "function" ? elementOrVisitor : possiblyVisitor;
866
- for (const block of body.content) {
867
- if (type === "all" || type === "block") {
868
- if (guard(block)) {
869
- visitor?.(block);
870
- }
871
- }
872
- if (type === "all" || type === "inline") {
873
- for (const inline of block.children) {
874
- if (guard(inline)) {
875
- visitor?.(inline);
876
- }
877
- }
878
- }
879
- }
880
- }
881
- function getMentionedIdsFromCommentBody(body) {
882
- const mentionedIds = /* @__PURE__ */ new Set();
883
- traverseCommentBody(
884
- body,
885
- "mention",
886
- (mention) => mentionedIds.add(mention.id)
887
- );
888
- return Array.from(mentionedIds);
889
- }
890
- async function resolveUsersInCommentBody(body, resolveUsers) {
891
- const resolvedUsers = /* @__PURE__ */ new Map();
892
- if (!resolveUsers) {
893
- return resolvedUsers;
894
- }
895
- const userIds = getMentionedIdsFromCommentBody(body);
896
- const users = await resolveUsers({
897
- userIds
898
- });
899
- for (const [index, userId] of userIds.entries()) {
900
- const user = users?.[index];
901
- if (user) {
902
- resolvedUsers.set(userId, user);
903
- }
904
- }
905
- return resolvedUsers;
906
- }
907
- var htmlEscapables = {
908
- "&": "&",
909
- "<": "&lt;",
910
- ">": "&gt;",
911
- '"': "&quot;",
912
- "'": "&#39;"
913
- };
914
- var htmlEscapablesRegex = new RegExp(
915
- Object.keys(htmlEscapables).map((entity) => `\\${entity}`).join("|"),
916
- "g"
917
- );
918
- function htmlSafe(value) {
919
- return new HtmlSafeString([String(value)], []);
920
- }
921
- function joinHtml(strings) {
922
- if (strings.length <= 0) {
923
- return new HtmlSafeString([""], []);
924
- }
925
- return new HtmlSafeString(
926
- ["", ...Array(strings.length - 1).fill(""), ""],
927
- strings
928
- );
929
- }
930
- function escapeHtml(value) {
931
- if (value instanceof HtmlSafeString) {
932
- return value.toString();
933
- }
934
- if (Array.isArray(value)) {
935
- return joinHtml(value).toString();
936
- }
937
- return String(value).replace(
938
- htmlEscapablesRegex,
939
- (character) => htmlEscapables[character]
940
- );
941
- }
942
- var HtmlSafeString = class {
943
- constructor(strings, values) {
944
- this._strings = strings;
945
- this._values = values;
946
- }
947
- toString() {
948
- return this._strings.reduce((result, str, i) => {
949
- return result + escapeHtml(this._values[i - 1]) + str;
950
- });
951
- }
952
- };
953
- function html(strings, ...values) {
954
- return new HtmlSafeString(strings, values);
955
- }
956
- var markdownEscapables = {
957
- _: "\\_",
958
- "*": "\\*",
959
- "#": "\\#",
960
- "`": "\\`",
961
- "~": "\\~",
962
- "!": "\\!",
963
- "|": "\\|",
964
- "(": "\\(",
965
- ")": "\\)",
966
- "{": "\\{",
967
- "}": "\\}",
968
- "[": "\\[",
969
- "]": "\\]"
970
- };
971
- var markdownEscapablesRegex = new RegExp(
972
- Object.keys(markdownEscapables).map((entity) => `\\${entity}`).join("|"),
973
- "g"
974
- );
975
- function joinMarkdown(strings) {
976
- if (strings.length <= 0) {
977
- return new MarkdownSafeString([""], []);
978
- }
979
- return new MarkdownSafeString(
980
- ["", ...Array(strings.length - 1).fill(""), ""],
981
- strings
982
- );
983
- }
984
- function escapeMarkdown(value) {
985
- if (value instanceof MarkdownSafeString) {
986
- return value.toString();
987
- }
988
- if (Array.isArray(value)) {
989
- return joinMarkdown(value).toString();
990
- }
991
- return String(value).replace(
992
- markdownEscapablesRegex,
993
- (character) => markdownEscapables[character]
994
- );
995
- }
996
- var MarkdownSafeString = class {
997
- constructor(strings, values) {
998
- this._strings = strings;
999
- this._values = values;
1000
- }
1001
- toString() {
1002
- return this._strings.reduce((result, str, i) => {
1003
- return result + escapeMarkdown(this._values[i - 1]) + str;
1004
- });
1005
- }
1006
- };
1007
- function markdown(strings, ...values) {
1008
- return new MarkdownSafeString(strings, values);
1009
- }
1010
- function toAbsoluteUrl(url2) {
1011
- if (url2.startsWith("http://") || url2.startsWith("https://")) {
1012
- return url2;
1013
- } else if (url2.startsWith("www.")) {
1014
- return "https://" + url2;
1015
- }
1016
- return;
1017
- }
1018
- var stringifyCommentBodyPlainElements = {
1019
- paragraph: ({ children }) => children,
1020
- text: ({ element }) => element.text,
1021
- link: ({ element }) => element.url,
1022
- mention: ({ element, user }) => {
1023
- return `@${user?.name ?? element.id}`;
1024
- }
1025
- };
1026
- var stringifyCommentBodyHtmlElements = {
1027
- paragraph: ({ children }) => {
1028
- return children ? html`<p>${htmlSafe(children)}</p>` : children;
1029
- },
1030
- text: ({ element }) => {
1031
- let children = element.text;
1032
- if (!children) {
1033
- return children;
1034
- }
1035
- if (element.bold) {
1036
- children = html`<strong>${children}</strong>`;
1037
- }
1038
- if (element.italic) {
1039
- children = html`<em>${children}</em>`;
1040
- }
1041
- if (element.strikethrough) {
1042
- children = html`<s>${children}</s>`;
1043
- }
1044
- if (element.code) {
1045
- children = html`<code>${children}</code>`;
1046
- }
1047
- return children;
1048
- },
1049
- link: ({ element, href }) => {
1050
- return html`<a href="${href}" target="_blank" rel="noopener noreferrer">${element.url}</a>`;
1051
- },
1052
- mention: ({ element, user }) => {
1053
- return html`<span data-mention>@${user?.name ?? element.id}</span>`;
1054
- }
1055
- };
1056
- var stringifyCommentBodyMarkdownElements = {
1057
- paragraph: ({ children }) => {
1058
- return children;
1059
- },
1060
- text: ({ element }) => {
1061
- let children = element.text;
1062
- if (!children) {
1063
- return children;
1064
- }
1065
- if (element.bold) {
1066
- children = markdown`**${children}**`;
1067
- }
1068
- if (element.italic) {
1069
- children = markdown`_${children}_`;
1070
- }
1071
- if (element.strikethrough) {
1072
- children = markdown`~~${children}~~`;
1073
- }
1074
- if (element.code) {
1075
- children = markdown`\`${children}\``;
1076
- }
1077
- return children;
1078
- },
1079
- link: ({ element, href }) => {
1080
- return markdown`[${element.url}](${href})`;
1081
- },
1082
- mention: ({ element, user }) => {
1083
- return markdown`@${user?.name ?? element.id}`;
1084
- }
1085
- };
1086
- async function stringifyCommentBody(body, options) {
1087
- const format = options?.format ?? "plain";
1088
- const separator = options?.separator ?? (format === "markdown" ? "\n\n" : "\n");
1089
- const elements = {
1090
- ...format === "html" ? stringifyCommentBodyHtmlElements : format === "markdown" ? stringifyCommentBodyMarkdownElements : stringifyCommentBodyPlainElements,
1091
- ...options?.elements
1092
- };
1093
- const resolvedUsers = await resolveUsersInCommentBody(
1094
- body,
1095
- options?.resolveUsers
1096
- );
1097
- const blocks = body.content.map((block, blockIndex) => {
1098
- switch (block.type) {
1099
- case "paragraph": {
1100
- const paragraph = block.children.map((inline, inlineIndex) => {
1101
- if (isCommentBodyMention(inline)) {
1102
- return inline.id ? elements.mention(
1103
- {
1104
- element: inline,
1105
- user: resolvedUsers.get(inline.id)
1106
- },
1107
- inlineIndex
1108
- ) : null;
1109
- }
1110
- if (isCommentBodyLink(inline)) {
1111
- return elements.link(
1112
- {
1113
- element: inline,
1114
- href: toAbsoluteUrl(inline.url) ?? inline.url
1115
- },
1116
- inlineIndex
1117
- );
1118
- }
1119
- if (isCommentBodyText(inline)) {
1120
- return elements.text({ element: inline }, inlineIndex);
1121
- }
1122
- return null;
1123
- }).filter(isSomething).join("");
1124
- return elements.paragraph(
1125
- { element: block, children: paragraph },
1126
- blockIndex
1127
- );
1128
- }
1129
- default:
1130
- return null;
1131
- }
1132
- });
1133
- return blocks.filter(isSomething).join(separator);
1134
- }
1135
-
1136
838
  // src/webhooks.ts
1137
839
  import * as base64 from "@stablelib/base64";
1138
840
  import * as sha256 from "fast-sha256";
@@ -1247,6 +949,13 @@ _WebhookHandler.secretPrefix = "whsec_";
1247
949
  var WebhookHandler = _WebhookHandler;
1248
950
  var WEBHOOK_TOLERANCE_IN_SECONDS = 5 * 60;
1249
951
  var isNotUndefined = (value) => value !== void 0;
952
+
953
+ // src/index.ts
954
+ import {
955
+ getMentionedIdsFromCommentBody,
956
+ stringifyCommentBody
957
+ } from "@liveblocks/core";
958
+ detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
1250
959
  export {
1251
960
  Liveblocks,
1252
961
  WebhookHandler,