All files / src/manifest manifest.ts

93.3% Statements 209/224
86.07% Branches 68/79
100% Functions 24/24
93.24% Lines 207/222

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 51633x 33x     33x   33x 33x   33x 33x 33x 33x 33x 33x 33x   33x         136x 136x       11x   11x 2x 2x 2x 2x 2x 2x     9x                 9x   9x 9x   9x 9x 9x 9x   9x 9x   9x 9x   9x       48x     48x 48x 48x 48x   48x 46x   48x   48x 8x         8x 8x 8x     48x       59x 59x 59x 59x 59x 59x   59x 18x 18x     59x                         33x 195x 195x 195x 195x 195x 195x 195x     195x 110x     195x 129x     195x 26x     195x 70x     195x 118x     195x           266x       55x             1x   1x 1x                         5x   5x       5x                   2x   2x       2x 2x   2x 1x     1x             53x 48x 6x     53x 53x 53x           53x 53x 48x   53x 53x 13568x 48x     53x                       53x                           67x 67x   67x                 70x 70x 70x 70x   70x     70x 70x 70x 70x 70x 17920x 59x 59x 59x       70x                     40x 40x     40x 40x 53x 53x 53x   53x 53x   53x 19x     53x 3x     50x                 50x   50x 11x 11x 11x 11x 11x   39x 39x 39x 39x                 3x 3x   3x       3x   3x       3x   3x 3x 1x               10x 2x     8x                       45x 42x   45x 45x   45x             70x 59x     59x 59x 59x 59x 59x 59x               73x   73x             403x   403x 73x     330x   330x 274x     56x                 118x 106x 68x   106x     118x                 1x 1x   1x 3x     1x       48x   48x 32x     48x 23x     48x 7x     48x 8x     48x         59x    
import { Binary, MerkleTree, Optional, Uint8ArrayReader } from 'cafe-utility'
import { Bee, BeeRequestOptions, DownloadOptions, NULL_ADDRESS, UploadOptions, UploadResult } from '..'
import { FeedPayloadResult } from '../modules/feed'
import { Bytes } from '../utils/bytes'
import { BatchId, Reference } from '../utils/typed-bytes'
 
const ENCODER = new TextEncoder()
const DECODER = new TextDecoder()
 
const TYPE_VALUE = 2
const TYPE_EDGE = 4
const TYPE_WITH_PATH_SEPARATOR = 8
const TYPE_WITH_METADATA = 16
const PATH_SEPARATOR = new Uint8Array([47])
const VERSION_02_HASH_HEX = '5768b3b6a7db56d21d1abff40d41cebfc83448fed8d7e9b06ec0d3b073f28f7b'
const VERSION_02_HASH = Binary.hexToUint8Array(VERSION_02_HASH_HEX)
 
export class Fork {
  prefix: Uint8Array
  node: MantarayNode
 
  constructor(prefix: Uint8Array, node: MantarayNode) {
    this.prefix = prefix
    this.node = node
  }
 
  static split(a: Fork, b: Fork): Fork {
    const commonPart = Binary.commonPrefix(a.prefix, b.prefix)
 
    if (commonPart.length === a.prefix.length) {
      const remainingB = b.prefix.slice(commonPart.length)
      b.node.path = b.prefix.slice(commonPart.length)
      b.prefix = b.prefix.slice(commonPart.length)
      b.node.parent = a.node
      a.node.forks.set(remainingB[0], b)
      return a
    }
 
    Iif (commonPart.length === b.prefix.length) {
      const remainingA = a.prefix.slice(commonPart.length)
      a.node.path = a.prefix.slice(commonPart.length)
      a.prefix = a.prefix.slice(commonPart.length)
      a.node.parent = b.node
      b.node.forks.set(remainingA[0], a)
      return b
    }
 
    const node = new MantarayNode({ path: commonPart })
 
    const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node)
    const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node)
 
    a.node.path = a.prefix.slice(commonPart.length)
    b.node.path = b.prefix.slice(commonPart.length)
    a.prefix = a.prefix.slice(commonPart.length)
    b.prefix = b.prefix.slice(commonPart.length)
 
    node.forks.set(newAFork.prefix[0], newAFork)
    node.forks.set(newBFork.prefix[0], newBFork)
 
    newAFork.node.parent = node
    newBFork.node.parent = node
 
    return new Fork(commonPart, node)
  }
 
  marshal(): Uint8Array {
    Iif (!this.node.selfAddress) {
      throw Error('Fork#marshal node.selfAddress is not set')
    }
    const data: Uint8Array[] = []
    data.push(new Uint8Array([this.node.determineType()]))
    data.push(Binary.numberToUint8(this.prefix.length))
    data.push(this.prefix)
 
    if (this.prefix.length < 30) {
      data.push(new Uint8Array(30 - this.prefix.length))
    }
    data.push(this.node.selfAddress)
 
    if (this.node.metadata) {
      const metadataBytes = Binary.padEndToMultiple(
        new Uint8Array([0x00, 0x00, ...ENCODER.encode(JSON.stringify(this.node.metadata))]),
        32,
        0x0a,
      )
      const metadataLengthBytes = Binary.numberToUint16(metadataBytes.length - 2, 'BE')
      metadataBytes.set(metadataLengthBytes, 0)
      data.push(metadataBytes)
    }
 
    return Binary.concatBytes(...data)
  }
 
  static unmarshal(reader: Uint8ArrayReader): Fork {
    const type = Binary.uint8ToNumber(reader.read(1))
    const prefixLength = Binary.uint8ToNumber(reader.read(1))
    const prefix = reader.read(prefixLength)
    reader.read(30 - prefixLength)
    const selfAddress = reader.read(32)
    let metadata: Record<string, string> | undefined = undefined
 
    if (isType(type, TYPE_WITH_METADATA)) {
      const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE')
      metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)))
    }
 
    return new Fork(prefix, new MantarayNode({ selfAddress, metadata, path: prefix }))
  }
}
 
interface MantarayNodeOptions {
  selfAddress?: Uint8Array
  targetAddress?: Uint8Array
  obfuscationKey?: Uint8Array
  metadata?: Record<string, string> | null
  path?: Uint8Array | null
  parent?: MantarayNode | null
}
 
export class MantarayNode {
  public obfuscationKey: Uint8Array = new Uint8Array(32)
  public selfAddress: Uint8Array | null = null
  public targetAddress: Uint8Array = new Uint8Array(32)
  public metadata: Record<string, string> | undefined | null = null
  public path: Uint8Array = new Uint8Array(0)
  public forks: Map<number, Fork> = new Map()
  public parent: MantarayNode | null = null
 
  constructor(options?: MantarayNodeOptions) {
    if (options?.targetAddress) {
      this.targetAddress = options.targetAddress
    }
 
    if (options?.selfAddress) {
      this.selfAddress = options.selfAddress
    }
 
    if (options?.metadata) {
      this.metadata = options.metadata
    }
 
    if (options?.obfuscationKey) {
      this.obfuscationKey = options.obfuscationKey
    }
 
    if (options?.path) {
      this.path = options.path
    }
 
    Iif (options?.parent) {
      this.parent = options.parent
    }
  }
 
  get fullPath(): Uint8Array {
    return Binary.concatBytes(this.parent?.fullPath ?? new Uint8Array(0), this.path)
  }
 
  get fullPathString(): string {
    return DECODER.decode(this.fullPath)
  }
 
  /**
   * Returns the metadata at the `/` path to access idiomatic properties.
   */
  getRootMetadata(): Optional<Record<string, string>> {
    const node = this.find('/')
 
    if (node && node.metadata) {
      return Optional.of(node.metadata)
    }
 
    return Optional.empty()
  }
 
  /**
   * Returns the `swarm-index-document` and `swarm-error-document` metadata values.
   */
  getDocsMetadata(): {
    indexDocument: string | null
    errorDocument: string | null
  } {
    const node = this.find('/')
 
    Iif (!node || !node.metadata) {
      return { indexDocument: null, errorDocument: null }
    }
 
    return {
      indexDocument: node.metadata['website-index-document'] ?? null,
      errorDocument: node.metadata['website-error-document'] ?? null,
    }
  }
 
  /**
   * Attempts to resolve the manifest as a feed, returning the latest update.
   */
  async resolveFeed(bee: Bee, requestOptions?: BeeRequestOptions): Promise<Optional<FeedPayloadResult>> {
    const node = this.find('/')
 
    Iif (!node || !node.metadata) {
      return Optional.empty()
    }
 
    const owner = node.metadata['swarm-feed-owner']
    const topic = node.metadata['swarm-feed-topic']
 
    if (!owner || !topic) {
      return Optional.empty()
    }
 
    return Optional.of(await bee.fetchLatestFeedUpdate(topic, owner, requestOptions))
  }
 
  /**
   * Gets the binary representation of the node.
   */
  async marshal(): Promise<Uint8Array> {
    for (const fork of this.forks.values()) {
      if (!fork.node.selfAddress) {
        fork.node.selfAddress = (await fork.node.calculateSelfAddress()).toUint8Array()
      }
    }
    const header = new Uint8Array(32)
    header.set(VERSION_02_HASH, 0)
    header.set(
      Binary.equals(this.targetAddress, NULL_ADDRESS) && Binary.equals(this.path, new Uint8Array([47]))
        ? Binary.numberToUint8(0)
        : Binary.numberToUint8(this.targetAddress.length),
      31,
    )
    const forkBitmap = new Uint8Array(32)
    for (const fork of this.forks.keys()) {
      Binary.setBit(forkBitmap, fork, 1, 'LE')
    }
    const forks: Uint8Array[] = []
    for (let i = 0; i < 256; i++) {
      if (Binary.getBit(forkBitmap, i, 'LE')) {
        forks.push(this.forks.get(i)!.marshal())
      }
    }
    const data = Binary.xorCypher(
      Binary.concatBytes(
        header,
        Binary.equals(this.targetAddress, NULL_ADDRESS) && Binary.equals(this.path, new Uint8Array([47]))
          ? new Uint8Array(0)
          : this.targetAddress,
        forkBitmap,
        ...forks,
      ),
      this.obfuscationKey,
    )
 
    return Binary.concatBytes(this.obfuscationKey, data)
  }
 
  /**
   * Downloads and unmarshals a MantarayNode from the given reference.
   *
   * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
   */
  static async unmarshal(
    bee: Bee,
    reference: Reference | Uint8Array | string,
    options?: DownloadOptions,
    requestOptions?: BeeRequestOptions,
  ): Promise<MantarayNode> {
    reference = new Reference(reference)
    const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array()
 
    return this.unmarshalFromData(data, reference.toUint8Array())
  }
 
  /**
   * Unmarshals a MantarayNode from the given data.
   *
   * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
   */
  static unmarshalFromData(data: Uint8Array, selfAddress: Uint8Array): MantarayNode {
    const obfuscationKey = data.subarray(0, 32)
    const decrypted = Binary.xorCypher(data.subarray(32), obfuscationKey)
    const reader = new Uint8ArrayReader(decrypted)
    const versionHash = reader.read(31)
 
    Iif (!Binary.equals(versionHash, VERSION_02_HASH.slice(0, 31))) {
      throw new Error('MantarayNode#unmarshal invalid version hash')
    }
    const targetAddressLength = Binary.uint8ToNumber(reader.read(1))
    const targetAddress = targetAddressLength === 0 ? NULL_ADDRESS : reader.read(targetAddressLength)
    const node = new MantarayNode({ selfAddress, targetAddress, obfuscationKey })
    const forkBitmap = reader.read(32)
    for (let i = 0; i < 256; i++) {
      if (Binary.getBit(forkBitmap, i, 'LE')) {
        const newFork = Fork.unmarshal(reader)
        node.forks.set(i, newFork)
        newFork.node.parent = node
      }
    }
 
    return node
  }
 
  /**
   * Adds a fork to the node.
   */
  addFork(
    path: string | Uint8Array,
    reference: string | Uint8Array | Bytes | Reference,
    metadata?: Record<string, string> | null,
  ) {
    this.selfAddress = null
    path = path instanceof Uint8Array ? path : ENCODER.encode(path)
    // TODO: this should not be ignored
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    let tip: MantarayNode = this
    while (path.length) {
      const prefix = path.slice(0, 30)
      path = path.slice(30)
      const isLast = path.length === 0
 
      const [bestMatch, matchedPath] = tip.findClosest(prefix)
      const remainingPath = prefix.slice(matchedPath.length)
 
      if (matchedPath.length) {
        tip = bestMatch
      }
 
      if (!remainingPath.length) {
        continue
      }
 
      const newFork = new Fork(
        remainingPath,
        new MantarayNode({
          targetAddress: isLast ? new Reference(reference).toUint8Array() : undefined,
          metadata: isLast ? metadata : undefined,
          path: remainingPath,
        }),
      )
 
      const existing = bestMatch.forks.get(remainingPath[0])
 
      if (existing) {
        const fork = Fork.split(newFork, existing)
        tip.forks.set(remainingPath[0], fork)
        fork.node.parent = tip
        tip.selfAddress = null
        tip = newFork.node
      } else {
        tip.forks.set(remainingPath[0], newFork)
        newFork.node.parent = tip
        tip.selfAddress = null
        tip = newFork.node
      }
    }
  }
 
  /**
   * Removes a fork from the node.
   */
  removeFork(path: string | Uint8Array) {
    this.selfAddress = null
    path = path instanceof Uint8Array ? path : ENCODER.encode(path)
 
    Iif (path.length === 0) {
      throw Error('MantarayNode#removeFork [path] parameter cannot be empty')
    }
 
    const match = this.find(path)
 
    Iif (!match) {
      throw Error('MantarayNode#removeFork fork not found')
    }
 
    const [parent, matchedPath] = this.findClosest(path.slice(0, path.length - 1))
 
    parent.forks.delete(path.slice(matchedPath.length)[0])
    for (const fork of match.forks.values()) {
      parent.addFork(Binary.concatBytes(match.path, fork.prefix), fork.node.targetAddress, fork.node.metadata)
    }
  }
 
  /**
   * Calculates the self address of the node.
   */
  async calculateSelfAddress(): Promise<Reference> {
    if (this.selfAddress) {
      return new Reference(this.selfAddress)
    }
 
    return new Reference((await MerkleTree.root(await this.marshal())).hash())
  }
 
  /**
   * Saves the node and its children recursively.
   */
  async saveRecursively(
    bee: Bee,
    postageBatchId: string | BatchId,
    options?: UploadOptions,
    requestOptions?: BeeRequestOptions,
  ): Promise<UploadResult> {
    for (const fork of this.forks.values()) {
      await fork.node.saveRecursively(bee, postageBatchId, options, requestOptions)
    }
    const result = await bee.uploadData(postageBatchId, await this.marshal(), options, requestOptions)
    this.selfAddress = result.reference.toUint8Array()
 
    return result
  }
 
  /**
   * Loads the node and its children recursively.
   */
  async loadRecursively(bee: Bee, options?: DownloadOptions, requestOptions?: BeeRequestOptions): Promise<void> {
    for (const fork of this.forks.values()) {
      Iif (!fork.node.selfAddress) {
        throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set')
      }
      const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, options, requestOptions)
      fork.node.targetAddress = node.targetAddress
      fork.node.forks = node.forks
      fork.node.path = fork.prefix
      fork.node.parent = this
      await fork.node.loadRecursively(bee, options, requestOptions)
    }
  }
 
  /**
   * Finds a node in the tree by its path.
   */
  find(path: string | Uint8Array): MantarayNode | null {
    const [closest, match] = this.findClosest(path)
 
    return match.length === path.length ? closest : null
  }
 
  /**
   * Finds the closest node in the tree to the given path.
   */
  findClosest(path: string | Uint8Array, current: Uint8Array = new Uint8Array()): [MantarayNode, Uint8Array] {
    path = path instanceof Uint8Array ? path : ENCODER.encode(path)
 
    if (path.length === 0) {
      return [this, current]
    }
 
    const fork = this.forks.get(path[0])
 
    if (fork && Binary.commonPrefix(fork.prefix, path).length === fork.prefix.length) {
      return fork.node.findClosest(path.slice(fork.prefix.length), Binary.concatBytes(current, fork.prefix))
    }
 
    return [this, current]
  }
 
  /**
   * Returns an array of all nodes in the tree which have a target address set.
   *
   * Must be called after `loadRecursively`.
   */
  collect(nodes: MantarayNode[] = []): MantarayNode[] {
    for (const fork of this.forks.values()) {
      if (!Binary.equals(fork.node.targetAddress, NULL_ADDRESS)) {
        nodes.push(fork.node)
      }
      fork.node.collect(nodes)
    }
 
    return nodes
  }
 
  /**
   * Returns a path:reference map of all nodes in the tree which have a target address set.
   *
   * Must be called after `loadRecursively`.
   */
  collectAndMap(): Record<string, string> {
    const nodes = this.collect()
    const result: Record<string, string> = {}
 
    for (const node of nodes) {
      result[node.fullPathString] = new Reference(node.targetAddress).toHex()
    }
 
    return result
  }
 
  determineType() {
    let type = 0
 
    if (!Binary.equals(this.targetAddress, NULL_ADDRESS) || Binary.equals(this.path, PATH_SEPARATOR)) {
      type |= TYPE_VALUE
    }
 
    if (this.forks.size > 0) {
      type |= TYPE_EDGE
    }
 
    if (Binary.indexOf(this.path, PATH_SEPARATOR) !== -1 && !Binary.equals(this.path, PATH_SEPARATOR)) {
      type |= TYPE_WITH_PATH_SEPARATOR
    }
 
    if (this.metadata) {
      type |= TYPE_WITH_METADATA
    }
 
    return type
  }
}
 
function isType(value: number, type: number): boolean {
  return (value & type) === type
}