@seed-hypermedia/client 0.0.67 → 0.0.69

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 (75) hide show
  1. package/LICENSE +201 -0
  2. package/dist/auth.d.ts +79 -0
  3. package/dist/auth.mjs +265 -0
  4. package/dist/auto-link.mjs +20 -0
  5. package/dist/base64.mjs +9 -0
  6. package/dist/blobs.d.ts +168 -0
  7. package/dist/blobs.mjs +50 -0
  8. package/dist/block-diff.d.ts +37 -1
  9. package/dist/block-diff.mjs +17 -0
  10. package/dist/blocks-to-markdown.mjs +22 -0
  11. package/dist/capability.mjs +11 -0
  12. package/dist/cbor.d.ts +13 -0
  13. package/dist/cbor.mjs +13 -0
  14. package/dist/change.mjs +23 -0
  15. package/dist/chunk-2MD3FXKB.mjs +14 -0
  16. package/dist/chunk-3TPCJCVO.mjs +24 -0
  17. package/dist/chunk-52P3E6H6.mjs +459 -0
  18. package/dist/{chunk-KCR3277A.mjs → chunk-5EWV4OPF.mjs} +1 -1
  19. package/dist/chunk-CBELM7KX.mjs +29 -0
  20. package/dist/chunk-DXDF36H5.mjs +289 -0
  21. package/dist/chunk-EY6RQVAP.mjs +138 -0
  22. package/dist/chunk-FYUZZ3HT.mjs +199 -0
  23. package/dist/chunk-GKGWSN6J.mjs +75 -0
  24. package/dist/chunk-H7L4TI6X.mjs +705 -0
  25. package/dist/chunk-HBIVZA6C.mjs +102 -0
  26. package/dist/chunk-I3YUHKYW.mjs +135 -0
  27. package/dist/chunk-KO4XH5L2.mjs +475 -0
  28. package/dist/chunk-LYECQKZE.mjs +49 -0
  29. package/dist/chunk-MKUWQB5W.mjs +297 -0
  30. package/dist/chunk-N3P7S5YZ.mjs +118 -0
  31. package/dist/chunk-NPG76WUS.mjs +348 -0
  32. package/dist/chunk-NX7YFFRX.mjs +124 -0
  33. package/dist/chunk-OZKQOI2X.mjs +334 -0
  34. package/dist/chunk-PPAY4WZX.mjs +40 -0
  35. package/dist/chunk-QS7TPMYT.mjs +411 -0
  36. package/dist/chunk-S2PVJK2P.mjs +517 -0
  37. package/dist/chunk-T5BTAJPW.mjs +725 -0
  38. package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
  39. package/dist/chunk-VN7F33SS.mjs +108 -0
  40. package/dist/chunk-W4OGV22Y.mjs +57 -0
  41. package/dist/chunk-WJQSJ6UM.mjs +159 -0
  42. package/dist/chunk-XI2LXZM7.mjs +112 -0
  43. package/dist/chunk-YNVFRCUH.mjs +171 -0
  44. package/dist/chunk-YZX54L3G.mjs +33 -0
  45. package/dist/client.mjs +14 -0
  46. package/dist/comment.mjs +18 -0
  47. package/dist/contact.mjs +14 -0
  48. package/dist/document-state.mjs +8 -0
  49. package/dist/editor-types.mjs +0 -0
  50. package/dist/editorblock-to-hmblock.mjs +11 -0
  51. package/dist/encryption.mjs +13 -0
  52. package/dist/errors.mjs +11 -0
  53. package/dist/file-to-ipfs.mjs +13 -0
  54. package/dist/grobid.mjs +11 -0
  55. package/dist/hm-resolver.mjs +10 -0
  56. package/dist/hm-types.d.ts +566 -566
  57. package/dist/hmauth.d.ts +169 -0
  58. package/dist/hmauth.mjs +52 -0
  59. package/dist/hmblock-to-editorblock.mjs +12 -0
  60. package/dist/index.d.ts +1 -1
  61. package/dist/index.mjs +126 -5006
  62. package/dist/keyfile.mjs +19 -0
  63. package/dist/markdown-to-blocks.d.ts +7 -0
  64. package/dist/markdown-to-blocks.mjs +15 -0
  65. package/dist/pdf-to-blocks-embedded.mjs +7 -0
  66. package/dist/pdf-to-blocks.mjs +11 -0
  67. package/dist/ref.mjs +12 -0
  68. package/dist/resource-read.mjs +14 -0
  69. package/dist/signing.mjs +13 -0
  70. package/dist/tei-to-blocks.mjs +8 -0
  71. package/dist/unicode.mjs +19 -0
  72. package/dist/vault-local.mjs +7 -6
  73. package/dist/vault.mjs +3 -2
  74. package/package.json +18 -28
  75. package/src/index.ts +0 -137
@@ -0,0 +1,411 @@
1
+ import {
2
+ validateExclusiveAnnotations
3
+ } from "./chunk-N3P7S5YZ.mjs";
4
+ import {
5
+ signObject,
6
+ toPublishInput
7
+ } from "./chunk-YZX54L3G.mjs";
8
+ import {
9
+ hmIdPathToEntityQueryPath,
10
+ packHmId
11
+ } from "./chunk-SQEUO7FO.mjs";
12
+
13
+ // src/comment.ts
14
+ import { decode as cborDecode, encode as cborEncode } from "@ipld/dag-cbor";
15
+ import { CID } from "multiformats";
16
+ import { base58btc } from "multiformats/bases/base58";
17
+ function trimTrailingEmptyBlocks(blocks) {
18
+ let end = blocks.length;
19
+ while (end > 0) {
20
+ const node = blocks[end - 1];
21
+ if (!isEmptyBlockNode(node)) break;
22
+ end--;
23
+ }
24
+ return blocks.slice(0, end);
25
+ }
26
+ function isEmptyBlockNode(node) {
27
+ const { block, children } = node;
28
+ if (children && children.length > 0) return false;
29
+ if (block.type !== "Paragraph" && block.type !== "Heading") return false;
30
+ return !block.text || block.text.trim() === "";
31
+ }
32
+ function resolveQuotingTarget(input) {
33
+ if (input.quoting) {
34
+ if (input.quoting.range && input.quoting.range.start === input.quoting.range.end) {
35
+ return { blockId: input.quoting.blockId };
36
+ }
37
+ return input.quoting;
38
+ }
39
+ if (input.quotingBlockId) return { blockId: input.quotingBlockId };
40
+ return void 0;
41
+ }
42
+ function annotationsToPublishable(annotations) {
43
+ validateExclusiveAnnotations(annotations);
44
+ return annotations.map((annotation) => {
45
+ var _a, _b, _c, _d;
46
+ const { type, starts, ends } = annotation;
47
+ if (type === "Bold") return { type: "Bold", starts, ends };
48
+ if (type === "Italic") return { type: "Italic", starts, ends };
49
+ if (type === "Underline") return { type: "Underline", starts, ends };
50
+ if (type === "Strike") return { type: "Strike", starts, ends };
51
+ if (type === "Code") return { type: "Code", starts, ends };
52
+ if (type === "Link") return { type: "Link", starts, ends, link: annotation.link || "" };
53
+ if (type === "Embed") return { type: "Embed", starts, ends, link: annotation.link || "" };
54
+ if (type === "TextColor")
55
+ return { type: "TextColor", starts, ends, attributes: { value: ((_a = annotation.attributes) == null ? void 0 : _a.value) || "" } };
56
+ if (type === "BackgroundColor")
57
+ return {
58
+ type: "BackgroundColor",
59
+ starts,
60
+ ends,
61
+ attributes: { value: ((_b = annotation.attributes) == null ? void 0 : _b.value) || "" }
62
+ };
63
+ if (type === "TextSize")
64
+ return { type: "TextSize", starts, ends, attributes: { value: ((_c = annotation.attributes) == null ? void 0 : _c.value) || "" } };
65
+ if (type === "TextFamily")
66
+ return { type: "TextFamily", starts, ends, attributes: { value: ((_d = annotation.attributes) == null ? void 0 : _d.value) || "" } };
67
+ throw new Error(`Unsupported annotation type: ${type}`);
68
+ });
69
+ }
70
+ function blockToPublishable(blockNode) {
71
+ var _a;
72
+ const block = blockNode.block;
73
+ if (block.type === "Paragraph") {
74
+ const hasChildren = (blockNode.children || []).length > 0;
75
+ const isListContainer = ((_a = block.attributes) == null ? void 0 : _a.childrenType) !== void 0;
76
+ if ((block.text === "" || block.text === void 0) && !hasChildren && !isListContainer) {
77
+ return null;
78
+ }
79
+ return {
80
+ id: block.id,
81
+ type: "Paragraph",
82
+ text: block.text || "",
83
+ annotations: annotationsToPublishable(block.annotations || []),
84
+ ...block.attributes,
85
+ children: blocksToPublishable(blockNode.children || [])
86
+ };
87
+ }
88
+ if (block.type === "Heading") {
89
+ return {
90
+ id: block.id,
91
+ type: "Heading",
92
+ text: block.text,
93
+ annotations: annotationsToPublishable(block.annotations || []),
94
+ ...block.attributes,
95
+ children: blocksToPublishable(blockNode.children || [])
96
+ };
97
+ }
98
+ if (block.type === "Math") {
99
+ return {
100
+ id: block.id,
101
+ type: "Math",
102
+ text: block.text,
103
+ annotations: annotationsToPublishable(block.annotations || []),
104
+ ...block.attributes,
105
+ children: blocksToPublishable(blockNode.children || [])
106
+ };
107
+ }
108
+ if (block.type === "Code") {
109
+ return {
110
+ id: block.id,
111
+ type: "Code",
112
+ text: block.text,
113
+ annotations: annotationsToPublishable(block.annotations || []),
114
+ ...block.attributes,
115
+ children: blocksToPublishable(blockNode.children || [])
116
+ };
117
+ }
118
+ if (block.type === "Image") {
119
+ return {
120
+ id: block.id,
121
+ type: "Image",
122
+ text: block.text,
123
+ link: block.link,
124
+ annotations: annotationsToPublishable(block.annotations || []),
125
+ ...block.attributes,
126
+ children: blocksToPublishable(blockNode.children || [])
127
+ };
128
+ }
129
+ if (block.type === "Video") {
130
+ return {
131
+ id: block.id,
132
+ type: "Video",
133
+ text: "",
134
+ link: block.link,
135
+ ...block.attributes,
136
+ children: blocksToPublishable(blockNode.children || [])
137
+ };
138
+ }
139
+ if (block.type === "File") {
140
+ return {
141
+ id: block.id,
142
+ type: "File",
143
+ link: block.link,
144
+ ...block.attributes,
145
+ children: blocksToPublishable(blockNode.children || [])
146
+ };
147
+ }
148
+ if (block.type === "Button") {
149
+ return {
150
+ id: block.id,
151
+ type: "Button",
152
+ text: block.text,
153
+ link: block.link,
154
+ ...block.attributes,
155
+ children: blocksToPublishable(blockNode.children || [])
156
+ };
157
+ }
158
+ if (block.type === "Embed") {
159
+ return {
160
+ id: block.id,
161
+ type: "Embed",
162
+ link: block.link,
163
+ ...block.attributes,
164
+ children: blocksToPublishable(blockNode.children || [])
165
+ };
166
+ }
167
+ if (block.type === "WebEmbed") {
168
+ return {
169
+ id: block.id,
170
+ type: "WebEmbed",
171
+ link: block.link,
172
+ ...block.attributes,
173
+ children: blocksToPublishable(blockNode.children || [])
174
+ };
175
+ }
176
+ throw new Error(`Unsupported block type: ${block.type}`);
177
+ }
178
+ function blocksToPublishable(blockNodes) {
179
+ return blockNodes.map((blockNode) => {
180
+ const block = blockToPublishable(blockNode);
181
+ if (!block) return null;
182
+ return block;
183
+ }).filter((blockNode) => blockNode !== null);
184
+ }
185
+ function cleanContentOfUndefined(content) {
186
+ content.forEach((blockNode) => {
187
+ const { block, children } = blockNode;
188
+ if (typeof block.text === "undefined") block.text = "";
189
+ if (children) cleanContentOfUndefined(children);
190
+ });
191
+ }
192
+ function deepStripUndefined(value) {
193
+ if (Array.isArray(value)) {
194
+ return value.map((item) => deepStripUndefined(item));
195
+ }
196
+ if (value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
197
+ const result = {};
198
+ for (const [k, v] of Object.entries(value)) {
199
+ if (v === void 0) continue;
200
+ result[k] = deepStripUndefined(v);
201
+ }
202
+ return result;
203
+ }
204
+ return value;
205
+ }
206
+ function createUnsignedComment({
207
+ content,
208
+ docId,
209
+ docVersion,
210
+ signerKey,
211
+ replyCommentVersion,
212
+ rootReplyCommentVersion,
213
+ visibility
214
+ }) {
215
+ const unsignedComment = {
216
+ type: "Comment",
217
+ body: blocksToPublishable(content),
218
+ space: new Uint8Array(base58btc.decode(docId.uid)),
219
+ version: docVersion,
220
+ signer: new Uint8Array(signerKey),
221
+ ts: BigInt(Date.now()),
222
+ sig: new Uint8Array(64),
223
+ path: hmIdPathToEntityQueryPath(docId.path),
224
+ replyParent: replyCommentVersion || void 0,
225
+ threadRoot: rootReplyCommentVersion || void 0
226
+ };
227
+ if (!unsignedComment.replyParent) delete unsignedComment.replyParent;
228
+ if (!unsignedComment.threadRoot) delete unsignedComment.threadRoot;
229
+ if (visibility) unsignedComment.visibility = visibility;
230
+ return unsignedComment;
231
+ }
232
+ async function createSignedComment(comment, signer) {
233
+ const commentForSigning = {
234
+ ...comment,
235
+ version: comment.version.split(".").map((v) => CID.parse(v))
236
+ };
237
+ if (comment.threadRoot) commentForSigning.threadRoot = CID.parse(comment.threadRoot);
238
+ if (comment.replyParent) commentForSigning.replyParent = CID.parse(comment.replyParent);
239
+ commentForSigning.sig = await signObject(signer, commentForSigning);
240
+ return commentForSigning;
241
+ }
242
+ async function createCommentBlob({
243
+ content,
244
+ docId,
245
+ docVersion,
246
+ signer,
247
+ replyCommentVersion,
248
+ rootReplyCommentVersion,
249
+ visibility
250
+ }) {
251
+ const signerKey = await signer.getPublicKey();
252
+ cleanContentOfUndefined(content);
253
+ const unsignedComment = createUnsignedComment({
254
+ content,
255
+ docId,
256
+ docVersion,
257
+ signerKey,
258
+ replyCommentVersion,
259
+ rootReplyCommentVersion,
260
+ visibility
261
+ });
262
+ const cleanedUnsignedComment = deepStripUndefined(unsignedComment);
263
+ const signedComment = await createSignedComment(cleanedUnsignedComment, signer);
264
+ return cborEncode(signedComment);
265
+ }
266
+ function generateBlockId(length = 8) {
267
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
268
+ let result = "";
269
+ for (let i = 0; i < length; i++) {
270
+ result += characters.charAt(Math.floor(Math.random() * characters.length));
271
+ }
272
+ return result;
273
+ }
274
+ function wrapQuotedContent(content, input) {
275
+ const quoting = resolveQuotingTarget(input);
276
+ if (!quoting) return content;
277
+ const link = packHmId({
278
+ ...input.docId,
279
+ blockRef: quoting.blockId,
280
+ blockRange: quoting.range ?? null,
281
+ version: input.docVersion,
282
+ latest: false
283
+ });
284
+ return [
285
+ {
286
+ block: {
287
+ id: generateBlockId(8),
288
+ type: "Embed",
289
+ text: "",
290
+ attributes: {
291
+ childrenType: "Group",
292
+ view: "Content"
293
+ },
294
+ annotations: [],
295
+ link
296
+ },
297
+ children: content
298
+ }
299
+ ];
300
+ }
301
+ async function resolveCommentContentAndBlobs(input) {
302
+ const defaultPrepareAttachments = async (_binaries) => ({
303
+ blobs: [],
304
+ resultCIDs: []
305
+ });
306
+ if ("getContent" in input) {
307
+ const { blockNodes: rawBlockNodes, blobs } = await input.getContent(
308
+ input.prepareAttachments || defaultPrepareAttachments
309
+ );
310
+ return {
311
+ content: wrapQuotedContent(trimTrailingEmptyBlocks(rawBlockNodes), input),
312
+ blobs
313
+ };
314
+ }
315
+ return {
316
+ content: wrapQuotedContent(trimTrailingEmptyBlocks(input.content), input),
317
+ blobs: input.blobs || []
318
+ };
319
+ }
320
+ async function createComment(input, signer) {
321
+ const { content, blobs } = await resolveCommentContentAndBlobs(input);
322
+ const comment = await createCommentBlob({
323
+ content,
324
+ docId: input.docId,
325
+ docVersion: input.docVersion,
326
+ signer,
327
+ replyCommentVersion: input.replyCommentVersion,
328
+ rootReplyCommentVersion: input.rootReplyCommentVersion,
329
+ visibility: input.visibility
330
+ });
331
+ return toPublishInput(comment, blobs);
332
+ }
333
+ async function deleteComment(input, signer) {
334
+ const parts = input.commentId.split("/");
335
+ const tsid = parts[1];
336
+ if (!tsid) {
337
+ throw new Error(`Invalid comment ID format: ${input.commentId}`);
338
+ }
339
+ const signerKey = await signer.getPublicKey();
340
+ const tombstone = {
341
+ type: "Comment",
342
+ id: tsid,
343
+ body: [],
344
+ space: new Uint8Array(base58btc.decode(input.targetAccount)),
345
+ path: input.targetPath,
346
+ version: input.targetVersion.split(".").map((v) => CID.parse(v)),
347
+ signer: new Uint8Array(signerKey),
348
+ ts: BigInt(Date.now()),
349
+ sig: new Uint8Array(64)
350
+ };
351
+ if (input.visibility) tombstone.visibility = input.visibility;
352
+ const cleanedTombstone = deepStripUndefined(tombstone);
353
+ cleanedTombstone.sig = await signObject(signer, cleanedTombstone);
354
+ const encoded = cborEncode(cleanedTombstone);
355
+ return toPublishInput(encoded, []);
356
+ }
357
+ async function updateComment(input, signer) {
358
+ const parts = input.commentId.split("/");
359
+ const tsid = parts[1];
360
+ if (!tsid) {
361
+ throw new Error(`Invalid comment ID format: ${input.commentId}`);
362
+ }
363
+ const signerKey = await signer.getPublicKey();
364
+ cleanContentOfUndefined(input.content);
365
+ const trimmedContent = trimTrailingEmptyBlocks(input.content);
366
+ const comment = {
367
+ type: "Comment",
368
+ id: tsid,
369
+ body: blocksToPublishable(trimmedContent),
370
+ space: new Uint8Array(base58btc.decode(input.targetAccount)),
371
+ path: input.targetPath,
372
+ version: input.targetVersion.split(".").map((v) => CID.parse(v)),
373
+ signer: new Uint8Array(signerKey),
374
+ ts: BigInt(Date.now()),
375
+ sig: new Uint8Array(64)
376
+ };
377
+ if (input.replyParentVersion) comment.replyParent = CID.parse(input.replyParentVersion);
378
+ if (input.rootReplyCommentVersion) comment.threadRoot = CID.parse(input.rootReplyCommentVersion);
379
+ if (input.visibility) comment.visibility = input.visibility;
380
+ const cleanedComment = deepStripUndefined(comment);
381
+ cleanedComment.sig = await signObject(signer, cleanedComment);
382
+ const encoded = cborEncode(cleanedComment);
383
+ return toPublishInput(encoded, []);
384
+ }
385
+ async function commentRecordIdFromBlob(blobData) {
386
+ const decoded = cborDecode(blobData);
387
+ if (decoded.type !== "Comment") {
388
+ throw new Error(`Expected Comment blob, got "${decoded.type}"`);
389
+ }
390
+ const signerBytes = decoded.signer;
391
+ const ts = BigInt(decoded.ts);
392
+ const authority = base58btc.encode(new Uint8Array(signerBytes));
393
+ const buf = new ArrayBuffer(8);
394
+ new DataView(buf).setBigUint64(0, ts, false);
395
+ const tsBytes = new Uint8Array(buf, 2, 6);
396
+ const hashBuffer = await crypto.subtle.digest("SHA-256", blobData);
397
+ const hashBytes = new Uint8Array(hashBuffer, 0, 4);
398
+ const tsidBytes = new Uint8Array(10);
399
+ tsidBytes.set(tsBytes, 0);
400
+ tsidBytes.set(hashBytes, 6);
401
+ const tsid = base58btc.encode(tsidBytes);
402
+ return `${authority}/${tsid}`;
403
+ }
404
+
405
+ export {
406
+ trimTrailingEmptyBlocks,
407
+ createComment,
408
+ deleteComment,
409
+ updateComment,
410
+ commentRecordIdFromBlob
411
+ };