@seed-hypermedia/client 0.0.68 → 0.0.70

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