@estiva-app/interop 0.65.0 → 0.65.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.65.0",
3
+ "version": "0.65.1",
4
4
  "description": "Render and act on another app's objects from its published NIP-89 manifest. The owner defines the projection; the consumer decides how it looks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/projection.ts CHANGED
@@ -4031,21 +4031,41 @@ async function markArchivedParents(
4031
4031
  query: QueryFn,
4032
4032
  cache?: ProjectionCache,
4033
4033
  ): Promise<void> {
4034
- type Walk = { object: ForeignObject; manifest?: Manifest; at: string; seen: Set<string> }
4035
- // An `naddr` parent names the same file as its plain address (FEE-2), and
4036
- // only the plain one is what a change's target tag and `with` carry.
4037
- const plain = (reference: string) => {
4038
- const pointer = addressPointerOf(reference)
4039
- return pointer ? `${pointer.kind}:${pointer.pubkey}:${pointer.identifier}` : undefined
4040
- }
4041
- let walking: Walk[] = entries
4042
- .filter(({ object: o }) => !o.unreachable && !o.archived && o.parentRef && plain(o.parentRef))
4043
- .map(({ object, manifest }) => ({
4044
- object,
4045
- manifest,
4046
- at: plain(object.parentRef!)!,
4047
- seen: new Set([plain(object.address ?? '') ?? '']),
4048
- }))
4034
+ await walkArchivedAbove(
4035
+ entries
4036
+ .filter(({ object: o }) => !o.unreachable && !o.archived && o.parentRef && plainAddress(o.parentRef))
4037
+ .map(({ object, manifest }) => ({
4038
+ manifest,
4039
+ at: plainAddress(object.parentRef!)!,
4040
+ seen: new Set([plainAddress(object.address ?? '') ?? '']),
4041
+ mark: (archive: Archive) => {
4042
+ object.archived = archive
4043
+ },
4044
+ })),
4045
+ query,
4046
+ cache,
4047
+ )
4048
+ }
4049
+
4050
+ // An `naddr` parent names the same file as its plain address (FEE-2), and
4051
+ // only the plain one is what a change's target tag and `with` carry.
4052
+ function plainAddress(reference: string) {
4053
+ const pointer = addressPointerOf(reference)
4054
+ return pointer ? `${pointer.kind}:${pointer.pubkey}:${pointer.identifier}` : undefined
4055
+ }
4056
+
4057
+ /**
4058
+ * {@link markArchivedParents}'s walk, from `at` upwards: `mark` is called with
4059
+ * the closest archived file's archive, `with` its address, and not at all when
4060
+ * none is. `manifest` is the app of the file below `at`, `seen` what is below.
4061
+ */
4062
+ async function walkArchivedAbove(
4063
+ starts: { at: string; manifest?: Manifest; seen: Set<string>; mark: (archive: Archive) => void }[],
4064
+ query: QueryFn,
4065
+ cache?: ProjectionCache,
4066
+ ): Promise<void> {
4067
+ type Walk = (typeof starts)[number]
4068
+ let walking: Walk[] = starts
4049
4069
  while (walking.length) {
4050
4070
  // Each child's ancestor is read by that child's app when it draws the
4051
4071
  // ancestor's kind — a project is Ship's as its issue is — and by the
@@ -4057,18 +4077,26 @@ async function markArchivedParents(
4057
4077
  if (own && !drawnBy.includes(own)) drawnBy.push(own)
4058
4078
  return { walk, key: `${walk.at}|${own ? drawnBy.indexOf(own) : ''}`, own }
4059
4079
  })
4060
- // A manifest that cannot be read is an ancestor read by SPEC's rule with no
4061
- // parent: the walk ends there rather than the whole open failing.
4080
+ // A level whose manifest cannot be read ends the walk, its own archive
4081
+ // uncounted (SPEC §6.5): which change archives it is the manifest's to say.
4082
+ // A bare file's is a constant, so a refused opener sweep never ends it.
4062
4083
  const theirOwn = new Map<string, Promise<Manifest | undefined>>()
4063
- const ancestors = await Promise.all(
4084
+ const resolved = await Promise.all(
4064
4085
  [...new Map(keyed.map((entry) => [entry.key, entry])).values()].map(async ({ key, walk, own }) => {
4065
4086
  const pointer = addressPointerOf(walk.at)!
4066
4087
  if (!own && !theirOwn.has(walk.at)) {
4067
- theirOwn.set(walk.at, resolveManifest(pointer, query, cache).then((r) => r?.manifest, () => undefined))
4088
+ theirOwn.set(
4089
+ walk.at,
4090
+ resolveManifest(pointer, query, cache).then(
4091
+ (r) => r?.manifest,
4092
+ () => (pointer.kind === KIND_BARE_FILE ? bareFileManifest().manifest : undefined),
4093
+ ),
4094
+ )
4068
4095
  }
4069
4096
  return { key, address: walk.at, pointer, manifest: own ?? (await theirOwn.get(walk.at)) }
4070
4097
  }),
4071
4098
  )
4099
+ const ancestors = resolved.filter((a): a is typeof a & { manifest: Manifest } => !!a.manifest)
4072
4100
  const rules = ancestors.map(({ manifest }) => archiveRuleOf(manifest))
4073
4101
  const addresses = [...new Set(ancestors.map(({ address }) => address))]
4074
4102
  const roots = new Map<string, { kinds: number[]; authors: string[]; '#d': string[] }>()
@@ -4108,11 +4136,11 @@ async function markArchivedParents(
4108
4136
  const found = read.get(key)
4109
4137
  if (!found) continue
4110
4138
  if (found.archive) {
4111
- walk.object.archived = { ...found.archive, with: walk.at }
4139
+ walk.mark({ ...found.archive, with: walk.at })
4112
4140
  continue
4113
4141
  }
4114
4142
  walk.seen.add(walk.at)
4115
- const parent = found.parent && plain(found.parent)
4143
+ const parent = found.parent && plainAddress(found.parent)
4116
4144
  if (parent && !walk.seen.has(parent)) {
4117
4145
  next.push({ ...walk, manifest: found.manifest, at: parent })
4118
4146
  }
@@ -4121,16 +4149,6 @@ async function markArchivedParents(
4121
4149
  }
4122
4150
  }
4123
4151
 
4124
- /**
4125
- * The rule another file's archive is read by, from an app known to hold it:
4126
- * that app's own, when it draws the file's kind — an issue's project is Ship's,
4127
- * a hive's inspection the hive log's — and SPEC's otherwise.
4128
- */
4129
- function relatedRuleOf(manifest: Pick<Manifest, 'records' | 'projections'> | undefined, address: string): RecordsRule {
4130
- const kind = addressPointerOf(address)?.kind
4131
- return manifest && kind !== undefined && manifest.projections?.[String(kind)] ? archiveRuleOf(manifest) : ARCHIVE_RULE
4132
- }
4133
-
4134
4152
  /**
4135
4153
  * Addresses per filter when reading archive changes. A filter's `limit` caps
4136
4154
  * every change naming its addresses — titles, statuses, moves — not only the
@@ -6873,8 +6891,8 @@ export async function resolveFolderContents(
6873
6891
  const listed: { file: ForeignObject; archive?: Archive }[] = []
6874
6892
  const archiveByAddress = new Map<string, Archive | undefined>()
6875
6893
  const parentByAddress = new Map<string, string | undefined>()
6876
- // How a parent outside this listing is read: its child's app's rule when that app draws it.
6877
- const parentRuleOf = new Map<string, RecordsRule>()
6894
+ // Whose app reads a parent outside this listing: its child's, when that app draws it.
6895
+ const manifestByAddress = new Map<string, Manifest>()
6878
6896
  for (const { address, pointer } of pointers) {
6879
6897
  const resolved = manifests.get(manifestKeyOf(pointer))
6880
6898
  // No app claims this kind, so there is no projection to draw it with.
@@ -6905,8 +6923,9 @@ export async function resolveFolderContents(
6905
6923
  const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), root, nestedAdmins)
6906
6924
  archiveByAddress.set(address, archive)
6907
6925
  const parent = parentRefOf(resolved.manifest, pointer.kind, root, folded)
6908
- parentByAddress.set(address, parent)
6909
- if (parent) parentRuleOf.set(parent, relatedRuleOf(resolved.manifest, parent))
6926
+ // Plain, as the listing's own addresses are, so an `naddr` parent listed here is found here.
6927
+ parentByAddress.set(address, parent && (plainAddress(parent) ?? parent))
6928
+ manifestByAddress.set(address, resolved.manifest)
6910
6929
  // An app hiding a record from its own lists is saying it is not part of the
6911
6930
  // folder any more. `hiddenWhen` is the app's own declaration of that — the
6912
6931
  // archive above is the same statement for a kind whose app declared none.
@@ -6958,36 +6977,63 @@ export async function resolveFolderContents(
6958
6977
  never written to the children, so unarchiving the parent restores
6959
6978
  exactly what was there.
6960
6979
 
6961
- The chain is walked through everything this Folder lists. A parent that
6962
- lives in another Folder is read once for all such parents, one level: a
6963
- project whose record channel lists its issues is the case that matters,
6964
- and there the project is listed too.
6980
+ The chain is walked through everything this Folder lists. Where it
6981
+ leaves the Folder, the rest is walked as a link reads it (SPEC §6.5,
6982
+ PEEKM-2): each ancestor with its root, so its archive is folded by
6983
+ §6.2's `h` rule, at any depth. One read per level for every such exit,
6984
+ and none when the chain stays here or meets an archived file first.
6965
6985
 
6966
6986
  Only by parent. A file this Folder lists is never hidden because some
6967
6987
  *other* Folder that lists it is archived — that is what lets a file
6968
6988
  placed in two Folders stay visible in the one that is not archived.
6969
6989
  */
6970
- const outside = [
6971
- ...new Set(
6972
- listed.flatMap(({ file }) => {
6973
- const parent = file.address && parentByAddress.get(file.address)
6974
- return parent && !archiveByAddress.has(parent) && addressPointerOf(parent) ? [parent] : []
6975
- }),
6976
- ),
6977
- ]
6978
- if (outside.length) {
6979
- const ruleFor = (parent: string) => parentRuleOf.get(parent) ?? ARCHIVE_RULE
6980
- const changes = await readArchiveChanges(outside, query, outside.map(ruleFor))
6981
- // A parent in another Folder is held by address only (SPEC §6.2's exception).
6982
- for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent), undefined))
6990
+ // One walk per exit and the app reading it, as walkArchivedAbove keys its
6991
+ // reads: two children of one outside parent, drawn by different apps, each
6992
+ // read it by their own app's rule.
6993
+ const exits = new Map<string, { at: string; manifest: Manifest | undefined; seen: Set<string>; archive?: Archive }>()
6994
+ const exitOf = new Map<string, string>()
6995
+ const drawnBy: Manifest[] = []
6996
+ for (const { file, archive } of listed) {
6997
+ if (archive || !file.address) continue
6998
+ const seen = new Set([file.address])
6999
+ let child = file.address
7000
+ for (let at = parentByAddress.get(child); at && !seen.has(at); child = at, at = parentByAddress.get(at)) {
7001
+ if (!archiveByAddress.has(at)) {
7002
+ const plain = plainAddress(at)
7003
+ if (!plain) break
7004
+ const manifest = manifestByAddress.get(child)
7005
+ const own = manifest?.projections?.[String(addressPointerOf(plain)!.kind)] ? manifest : undefined
7006
+ if (own && !drawnBy.includes(own)) drawnBy.push(own)
7007
+ const key = `${plain}|${own ? drawnBy.indexOf(own) : ''}`
7008
+ if (!exits.has(key)) exits.set(key, { at: plain, manifest, seen })
7009
+ exitOf.set(file.address, key)
7010
+ break
7011
+ }
7012
+ if (archiveByAddress.get(at)) break
7013
+ seen.add(at)
7014
+ }
6983
7015
  }
7016
+ await walkArchivedAbove(
7017
+ [...exits.values()].map((exit) => ({
7018
+ at: exit.at,
7019
+ manifest: exit.manifest,
7020
+ seen: exit.seen,
7021
+ mark: (found: Archive) => {
7022
+ exit.archive = found
7023
+ },
7024
+ })),
7025
+ query,
7026
+ cache,
7027
+ )
6984
7028
  const files: ForeignObject[] = []
6985
7029
  for (const { file, archive } of listed) {
7030
+ const exit = file.address ? exitOf.get(file.address) : undefined
6986
7031
  const effective =
6987
7032
  archive ??
6988
7033
  (file.address
6989
7034
  ? archivedAncestorOf(file.address, (a) => parentByAddress.get(a), (a) => archiveByAddress.get(a))
6990
- : undefined)
7035
+ : undefined) ??
7036
+ (exit ? exits.get(exit)?.archive : undefined)
6991
7037
  if (!effective) files.push(file)
6992
7038
  else if (options.includeArchived) files.push({ ...file, archived: effective })
6993
7039
  }