@seed-hypermedia/client 0.0.71 → 0.0.72

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.
@@ -4,10 +4,10 @@ import {
4
4
  documentContainsLinkToChild,
5
5
  documentHasSelfQuery,
6
6
  shouldAutoLinkParent
7
- } from "./chunk-X6OJRM5W.mjs";
7
+ } from "./chunk-GYZGSI4H.mjs";
8
8
  import "./chunk-JT57MV7Y.mjs";
9
9
  import "./chunk-PJ2RFT5A.mjs";
10
- import "./chunk-AOB3PMFZ.mjs";
10
+ import "./chunk-BZ4AXNSZ.mjs";
11
11
  import "./chunk-LVAHHQIK.mjs";
12
12
  import "./chunk-DR3HJ6BV.mjs";
13
13
  import "./chunk-W3FLBRBH.mjs";
@@ -1,4 +1,5 @@
1
1
  import {
2
+ packHmId,
2
3
  unpackHmId
3
4
  } from "./chunk-W3FLBRBH.mjs";
4
5
 
@@ -51,6 +52,55 @@ async function resolveDocumentState(client, targetId) {
51
52
  version
52
53
  };
53
54
  }
55
+ var MAX_REDIRECT_HOPS = 5;
56
+ async function followRedirects(client, id) {
57
+ const seen = /* @__PURE__ */ new Set([packHmId(id)]);
58
+ const redirects = [];
59
+ let current = id;
60
+ for (let hop = 0; hop <= MAX_REDIRECT_HOPS; hop++) {
61
+ const resource = await client.request("Resource", current);
62
+ if (resource.type !== "redirect") {
63
+ return { id, targetId: current, resource, redirects };
64
+ }
65
+ redirects.push({ from: current, to: resource.redirectTarget, republish: resource.republish === true });
66
+ const next = packHmId(resource.redirectTarget);
67
+ if (seen.has(next)) throw new Error(`Redirect cycle detected at ${next}`);
68
+ seen.add(next);
69
+ current = resource.redirectTarget;
70
+ }
71
+ throw new Error(`Too many redirects while resolving ${packHmId(id)} (limit ${MAX_REDIRECT_HOPS})`);
72
+ }
73
+ function describeRedirect(followed) {
74
+ const first = followed.redirects[0];
75
+ if (!first) return null;
76
+ const from = packHmId(followed.id);
77
+ const to = packHmId(followed.targetId);
78
+ const via = followed.redirects.length > 1 ? ` (via ${followed.redirects.length} redirects)` : "";
79
+ if (first.republish) {
80
+ return `${from} republishes ${to}${via}: the content shown is the latest version of ${to}. To edit the shared original, write to ${to}. Writing to ${from} replaces the republish with an independent copy that no longer follows ${to}.`;
81
+ }
82
+ return `${from} has moved to ${to}${via}: the content shown is the latest version of ${to}. Write to ${to}. Writing to ${from} would revive it as an independent copy that no longer follows ${to}.`;
83
+ }
84
+ async function followToDocument(client, id) {
85
+ const followed = await followRedirects(client, id);
86
+ const first = followed.redirects[0];
87
+ if (followed.resource.type !== "document") {
88
+ throw new Error(
89
+ `Cannot edit ${packHmId(followed.targetId)}: resource is ${followed.resource.type}` + (first ? ` (followed redirect from ${packHmId(id)})` : "")
90
+ );
91
+ }
92
+ return {
93
+ id,
94
+ targetId: followed.targetId,
95
+ document: followed.resource.document,
96
+ redirect: first ? { republish: first.republish, target: first.to } : null
97
+ };
98
+ }
99
+ async function resolveEditableDocument(client, id) {
100
+ const followed = await followToDocument(client, id);
101
+ const state = await resolveDocumentState(client, packHmId(followed.targetId));
102
+ return { ...followed, state };
103
+ }
54
104
  function computeDepths(changesResp) {
55
105
  const depthMap = /* @__PURE__ */ new Map();
56
106
  const depsMap = /* @__PURE__ */ new Map();
@@ -98,5 +148,9 @@ function computeDepths(changesResp) {
98
148
  }
99
149
 
100
150
  export {
101
- resolveDocumentState
151
+ resolveDocumentState,
152
+ followRedirects,
153
+ describeRedirect,
154
+ followToDocument,
155
+ resolveEditableDocument
102
156
  };
@@ -7,7 +7,7 @@ import {
7
7
  } from "./chunk-PJ2RFT5A.mjs";
8
8
  import {
9
9
  resolveDocumentState
10
- } from "./chunk-AOB3PMFZ.mjs";
10
+ } from "./chunk-BZ4AXNSZ.mjs";
11
11
  import {
12
12
  entityQueryPathToHmIdPath,
13
13
  packHmId,
@@ -6,6 +6,7 @@
6
6
  * genesis has depth 0, each subsequent change has depth = max(dep depths) + 1.
7
7
  */
8
8
  import type { SeedClient } from './client';
9
+ import type { HMDocument, HMResource, UnpackedHypermediaId } from './hm-types';
9
10
  /** The resolved state of a document's change DAG. */
10
11
  export type DocumentState = {
11
12
  /** CID of the genesis change blob. */
@@ -21,3 +22,82 @@ export type DocumentState = {
21
22
  * Resolves the current document state including head depth.
22
23
  */
23
24
  export declare function resolveDocumentState(client: SeedClient, targetId: string): Promise<DocumentState>;
25
+ /** A document address resolved through redirects to the document it currently presents. */
26
+ export type FollowedDocument = {
27
+ /** The address that was resolved — where a new Ref should be published to affect this path. */
28
+ id: UnpackedHypermediaId;
29
+ /** Where the document actually lives after following redirects (same as `id` for a plain document). */
30
+ targetId: UnpackedHypermediaId;
31
+ /** The current document at the target — the content baseline for edits, forks, and moves. */
32
+ document: HMDocument;
33
+ /**
34
+ * Non-null when `id` currently holds a redirect Ref. Publishing a Version Ref at `id` with the
35
+ * target's genesis and a fresh (current-timestamp) generation replaces the redirect: the path
36
+ * becomes a live document continuing the target's change history, and stops following the target.
37
+ */
38
+ redirect: {
39
+ republish: boolean;
40
+ target: UnpackedHypermediaId;
41
+ } | null;
42
+ };
43
+ /** The full baseline needed to build a new Change at an address: {@link FollowedDocument} plus DAG state. */
44
+ export type EditableDocumentBase = FollowedDocument & {
45
+ /** Change-DAG state of the target (genesis/heads/depth) for building the next Change. */
46
+ state: DocumentState;
47
+ };
48
+ /** One redirect Ref that was followed while resolving an address. */
49
+ export type RedirectHop = {
50
+ /** The address holding the redirect Ref. */
51
+ from: UnpackedHypermediaId;
52
+ /** Where it points. */
53
+ to: UnpackedHypermediaId;
54
+ /**
55
+ * `true` for a "republish" redirect (the path re-publishes the target's latest content as its
56
+ * own), `false` for a move redirect (the path has moved away).
57
+ */
58
+ republish: boolean;
59
+ };
60
+ /** A resource read through any redirect Refs on the way, plus the trail of redirects followed. */
61
+ export type FollowedResource = {
62
+ /** The address that was asked for. */
63
+ id: UnpackedHypermediaId;
64
+ /** The address the resource was actually read from (equals `id` when nothing was followed). */
65
+ targetId: UnpackedHypermediaId;
66
+ /** The resource at `targetId` — never a redirect. */
67
+ resource: Exclude<HMResource, {
68
+ type: 'redirect';
69
+ }>;
70
+ /** Every redirect followed, in order. Empty when `id` holds no redirect. */
71
+ redirects: RedirectHop[];
72
+ };
73
+ /**
74
+ * Reads the resource at an address, following redirect Refs (bounded, cycle-safe) and reporting
75
+ * every hop that was taken.
76
+ *
77
+ * Readers that follow redirects must never present the result as if it lived at the requested
78
+ * address: the content belongs to `targetId`, and a write to `id` behaves differently from a write
79
+ * to `targetId` (see {@link describeRedirect}). Callers surface `redirects` to the user or agent.
80
+ */
81
+ export declare function followRedirects(client: SeedClient, id: UnpackedHypermediaId): Promise<FollowedResource>;
82
+ /**
83
+ * One plain sentence explaining a followed redirect and what a write to either address does —
84
+ * shared by the CLI and the agents read tool so every surface tells the same story.
85
+ */
86
+ export declare function describeRedirect(followed: Pick<FollowedResource, 'id' | 'targetId' | 'redirects'>): string | null;
87
+ /**
88
+ * Resolves an address to the document it currently presents, following redirect Refs.
89
+ *
90
+ * A path that holds a redirect Ref (including a "republish" redirect, which re-publishes the
91
+ * target's latest content at this path) has no document of its own: the `Resource` API reports
92
+ * `type: 'redirect'`. Operations on such a path (edit, fork, move, delete) act on the redirect
93
+ * target's document, so this follows the chain (bounded, cycle-safe) to that document.
94
+ */
95
+ export declare function followToDocument(client: SeedClient, id: UnpackedHypermediaId): Promise<FollowedDocument>;
96
+ /**
97
+ * Resolves an address to the state needed to edit the document there, following redirects.
98
+ *
99
+ * Edits at a redirected address must build the Change on the redirect target's DAG (the source
100
+ * path has no changes of its own — `ListChanges` returns nothing for it) and publish a Version
101
+ * Ref at the source path with a fresh generation, which supersedes the redirect Ref.
102
+ */
103
+ export declare function resolveEditableDocument(client: SeedClient, id: UnpackedHypermediaId): Promise<EditableDocumentBase>;
@@ -1,8 +1,16 @@
1
1
  import {
2
- resolveDocumentState
3
- } from "./chunk-AOB3PMFZ.mjs";
2
+ describeRedirect,
3
+ followRedirects,
4
+ followToDocument,
5
+ resolveDocumentState,
6
+ resolveEditableDocument
7
+ } from "./chunk-BZ4AXNSZ.mjs";
4
8
  import "./chunk-W3FLBRBH.mjs";
5
9
  import "./chunk-7P6ASYW6.mjs";
6
10
  export {
7
- resolveDocumentState
11
+ describeRedirect,
12
+ followRedirects,
13
+ followToDocument,
14
+ resolveDocumentState,
15
+ resolveEditableDocument
8
16
  };
package/dist/index.d.ts CHANGED
@@ -39,8 +39,8 @@ export { createBlocksMap, matchBlockIds, computeReplaceOps, hmBlockNodeToBlockNo
39
39
  export type { APIBlockNode, APIBlock } from './block-diff';
40
40
  export { autoLinkChildToParent, createAutoLinkOps, documentContainsLinkToChild, documentHasSelfQuery, shouldAutoLinkParent, } from './auto-link';
41
41
  export type { AutoLinkChildToParentOptions } from './auto-link';
42
- export { resolveDocumentState } from './document-state';
43
- export type { DocumentState } from './document-state';
42
+ export { describeRedirect, followRedirects, followToDocument, resolveDocumentState, resolveEditableDocument, } from './document-state';
43
+ export type { DocumentState, EditableDocumentBase, FollowedDocument, FollowedResource, RedirectHop, } from './document-state';
44
44
  export { editorBlockToHMBlock, editorBlocksToHMBlockNodes } from './editorblock-to-hmblock';
45
45
  export { hmBlocksToEditorContent, hmBlockToEditorBlock, annotationContains } from './hmblock-to-editorblock';
46
46
  export { AnnotationSet, addSpanToAnnotation, pushSpanToAnnotation } from './unicode';
package/dist/index.mjs CHANGED
@@ -79,7 +79,7 @@ import {
79
79
  documentContainsLinkToChild,
80
80
  documentHasSelfQuery,
81
81
  shouldAutoLinkParent
82
- } from "./chunk-X6OJRM5W.mjs";
82
+ } from "./chunk-GYZGSI4H.mjs";
83
83
  import {
84
84
  createChange,
85
85
  createChangeOps,
@@ -95,8 +95,12 @@ import {
95
95
  createVersionRef
96
96
  } from "./chunk-PJ2RFT5A.mjs";
97
97
  import {
98
- resolveDocumentState
99
- } from "./chunk-AOB3PMFZ.mjs";
98
+ describeRedirect,
99
+ followRedirects,
100
+ followToDocument,
101
+ resolveDocumentState,
102
+ resolveEditableDocument
103
+ } from "./chunk-BZ4AXNSZ.mjs";
100
104
  import "./chunk-2MD3FXKB.mjs";
101
105
  import {
102
106
  computeReplaceOps,
@@ -175,6 +179,7 @@ export {
175
179
  createVersionRef,
176
180
  deleteComment,
177
181
  deleteContact,
182
+ describeRedirect,
178
183
  documentContainsLinkToChild,
179
184
  documentHasSelfQuery,
180
185
  documentToResolvedMarkdown,
@@ -187,6 +192,8 @@ export {
187
192
  fileToIpfsBlobs,
188
193
  filesToIpfsBlobs,
189
194
  flattenToOperations,
195
+ followRedirects,
196
+ followToDocument,
190
197
  getHMQueryString,
191
198
  hasFileLinks,
192
199
  hmBlockNodeToBlockNode,
@@ -213,6 +220,7 @@ export {
213
220
  rebindTableIdentities,
214
221
  resolveCapability,
215
222
  resolveDocumentState,
223
+ resolveEditableDocument,
216
224
  resolveFileLinksInBlocks,
217
225
  resolveHypermediaUrl,
218
226
  resolveId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seed-hypermedia/client",
3
- "version": "0.0.71",
3
+ "version": "0.0.72",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/seed-hypermedia/seed",