@balena/contrato
    Preparing search index...

    Class Blueprint

    Hierarchy (View Summary)

    Index
    • Parameters

      Returns Blueprint

      Blueprint

      module:contrato

      Creates a new Blueprint with a given layout and skeleton

      If no skeleton is given, a default {"type": "meta.context"} skeleton will be used.

      const blueprint = new Blueprint({
      'arch.sw': 1,
      'hw.device-type': 1
      }, {
      type: 'my-context',
      slug: '{{children.arch.sw.slug}}-{{children.hw.device-type.slug}}'
      })
    • Function

      Parameters

      Returns this

      contract

      module:contrato.Contract#addChild

      if the child cannot be nested: its type overlaps an existing child's type, one being a prefix of the other (e.g. adding sw.os.kernel under a contract that already holds sw.os), it has no slug to be keyed by, or its type is not a dotted path. Nothing is added in those cases.

      const contract = new Contract({ ... })
      contract.addChild(new Contract({ ... }))
    • Function

      Parameters

      Returns this

      contract

      module:contrato.Contract#addChildren

      This is a utility method over .addChild().

      if any child cannot be nested, see .addChild(). Nothing is added in that case; the contracts passed in are cloned, so they stay usable.

      const contract = new Contract({ ... })
      contract.addChildren([
      new Contract({ ... }),
      new Contract({ ... }),
      new Contract({ ... })
      ])
    • Function

      Parameters

      • Optionaloptions: { types?: Set<string> } = {}

        options

        • Optionaltypes?: Set<string>

          the types to consider (all by default)

      Returns boolean

      whether the children are satisfied

      module:contrato.Contract#areChildrenSatisfied

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])

      if (contract.areChildrenSatisfied({
      types: new Set([ 'sw.arch' ])
      })) {
      console.log('This contract has all sw.arch requirements satisfied')
      }
    • Function

      Returns Contract

      • the contract clone

      module:contrato.Contract#toJSON

      const contract = new Contract({ ... })
      const clone = contract.clone()
    • Function

      Parameters

      Returns Contract[]

      children

      module:contrato.Contract#findChildren

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])

      const children = contract.findChildren(Contract.createMatcher({
      type: 'sw.os',
      slug: 'debian'
      }))

      children.forEach((child) => {
      console.log(child)
      })
    • Function

      Parameters

      • Optionaloptions: { types?: Set<string> } = {}

        options

        • Optionaltypes?: Set<string>

          the types to consider (all by default)

      Returns any[]

      list of unsatisfied requirements

      module:contrato.Contract#getAllNotSatisfiedChildRequirements

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])

      console.log(contract.getAllNotSatisfiedChildRequirements({
      types: new Set([ 'sw.arch' ])
      }))
    • Function

      Returns Set<string>

      slugs

      module:contrato.Contract#getAllSlugs

      const contract = new Contract({
      type: 'hw.device-type',
      name: 'Raspberry Pi',
      slug: 'raspberrypi',
      aliases: [ 'rpi', 'raspberry-pi' ]
      })

      console.log(contract.getAllSlugs())
      > Set { raspberrypi, rpi, raspberry-pi }
    • Function

      Returns string | undefined

      slug - contract canonical slug or slug if canonical slug doesn't exist

      module:contrato.Contract#getCanonicalSlug

      const contract = new Contract({
      type: 'arch.sw',
      name: 'armv7hf',
      slug: 'armv7hf'
      canonicalSlug: 'raspberry-pi'
      })

      console.log(contract.getCanonicalSlug())
    • Function

      Parameters

      • hash: string

        child contract hash

      Returns Contract | undefined

      child

      module:contrato.Contract#getChildByHash

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])

      const child = contract.getChildByHash('xxxxxxx')

      if (child) {
      console.log(child)
      }
    • Function

      Parameters

      • Optionaloptions: { types?: Set<string> } = {}

        options

        • Optionaltypes?: Set<string>

          children types (all by default)

      Returns Contract[]

      children

      module:contrato.Contract#getChildren

      const contract = new Contract({ ... })
      const children = contract.getChildren({
      types: new Set([ 'arch.sw' ])
      })

      for (const child of children) {
      console.log(child)
      }
    • Function

      Parameters

      • type: string

        contract type

      Returns Contract[]

      children

      module:contrato.Contract#getChildrenByType

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])
      const children = container.getChildrenByType('sw.os')

      children.forEach((child) => {
      console.log(child)
      })
    • Function

      Parameters

      • options: { from?: number; to?: number; type: string; [index: string]: any }

        options

        • [index: string]: any
        • Optionalfrom?: number

          number of contracts per combination (from)

        • Optionalto?: number

          number of contracts per combination (to)

        • type: string

          contract type

      Returns Contract[][]

      combinations

      module:contrato.Contract#getChildrenCombinations

      Note that the client is responsible for evaluating that the combination of contracts is valid with regards to requirements, conflicts, etc. This function simply returns all the possible combinations without any further checks.

      The combinations output by this function is a plain list of contracts from which you can create a contract, or any other application specific data structure.

      const contract = new Contract({ ... })
      contract.addChildren([
      new Contract({
      name: 'Debian Wheezy',
      version: 'wheezy',
      slug: 'debian',
      type: 'sw.os'
      }),
      new Contract({
      name: 'Debian Jessie',
      version: 'jessie',
      slug: 'debian',
      type: 'sw.os'
      }),
      new Contract({
      name: 'Fedora 25',
      version: '25',
      slug: 'fedora',
      type: 'sw.os'
      })
      ])

      const combinations = contract.getChildrenCombinations({
      type: 'sw.os',
      from: 2,
      to: 2
      })

      console.log(combinations)
      > [
      > [
      > new Contract({
      > name: 'Debian Wheezy',
      > version: 'wheezy',
      > slug: 'debian',
      > type: 'sw.os'
      > }),
      > new Contract({
      > name: 'Debian Jessie',
      > version: 'jessie',
      > slug: 'debian',
      > type: 'sw.os'
      > })
      > ],
      > [
      > new Contract({
      > name: 'Debian Wheezy',
      > version: 'wheezy',
      > slug: 'debian',
      > type: 'sw.os'
      > }),
      > new Contract({
      > name: 'Fedora 25',
      > version: '25',
      > slug: 'fedora',
      > type: 'sw.os'
      > })
      > ],
      > [
      > new Contract({
      > name: 'Debian Jessie',
      > version: 'jessie',
      > slug: 'debian',
      > type: 'sw.os'
      > }),
      > new Contract({
      > name: 'Fedora 25',
      > version: '25',
      > slug: 'fedora',
      > type: 'sw.os'
      > })
      > ]
      > ]
    • Function

      Parameters

      • options: { from: Contract; types: Set<string> }

        options

        • from: Contract

          contract to resolve external contracts from

        • types: Set<string>

          types to consider

      Returns Contract[]

      children cross referenced contracts

      module:contrato.Contract#getChildrenCrossReferencedContracts

      const contract = new Contract({ ... })

      contract.addChildren([
      new Contract({
      type: 'arch.sw',
      slug: 'armv7hf',
      name: 'armv7hf'
      }),
      new Contract({
      type: 'sw.os',
      slug: 'raspbian',
      requires: [
      {
      or: [
      {
      type: 'arch.sw',
      slug: 'armv7hf'
      },
      {
      type: 'arch.sw',
      slug: 'rpi'
      }
      ]
      }
      ]
      }),
      new Contract({
      type: 'sw.stack',
      slug: 'nodejs',
      requires: [
      {
      type: 'arch.sw',
      slug: 'armv7hf'
      }
      ]
      })
      ])

      const references = contract.getChildrenCrossReferencedContracts({
      from: contract,
      types: new Set([ 'arch.sw' ])
      })

      console.log(references)
      > [
      > Contract {
      > type: 'arch.sw',
      > slug: 'armv7hf',
      > name: 'armv7hf'
      > }
      > ]
    • Function

      Returns Set<string>

      types

      module:contrato.Contract#getChildrenTypes

      const contract = new Contract({ ... })
      contract.addChildren([ { ... }, { ... } ])
      console.log(contract.getChildrenTypes())
    • Function

      Parameters

      • contract: Contract

        child contract

      • Optionaloptions: { types?: Set<string> } = {}

        options

        • Optionaltypes?: Set<string>

          the types to consider (all by default)

      Returns any[]

      list of unsatisfied requirements

      module:contrato.Contract#getNotSatisfiedChildRequirements

      const contract = new Contract({ ... })
      contract.addChildren([
      new Contract({
      type: 'sw.os',
      name: 'Debian Wheezy',
      version: 'wheezy',
      slug: 'debian'
      }),
      new Contract({
      type: 'sw.os',
      name: 'Fedora 25',
      version: '25',
      slug: 'fedora'
      })
      ])

      const child = new Contract({
      type: 'sw.stack',
      name: 'Node.js',
      version: '4.8.0',
      slug: 'nodejs',
      requires: [
      {
      or: [
      {
      type: 'sw.os',
      slug: 'debian'
      },
      {
      type: 'sw.os',
      slug: 'fedora'
      }
      ]
      }
      ]
      })

      console.log(contract.getNotSatisfiedChildRequirements(child))
    • Function

      Parameters

      • options: { from: Contract; types: Set<string> }

        options

        • from: Contract

          contract to resolve external contracts from

        • types: Set<string>

          types to consider

      Returns { [index: string]: Contract[] }

      referenced contracts

      module:contrato.Contract#getReferencedContracts

      const universe = new Contract({ ... })
      universe.addChildren([ ... ])

      const contract = new Contract({ ... })
      for (const reference of contract.getReferencedContracts({
      types: new Set([ 'arch.sw' ]),
      from: universe
      })) {
      console.log(reference.toJSON())
      }
    • Function

      Returns string

      reference string

      module:contrato.Contract#getReferenceString

      const contract = new Contract({
      type: 'arch.sw',
      name: 'armv7hf',
      slug: 'armv7hf'
      })

      console.log(contract.getReferenceString())
    • Function

      Returns string | undefined

      slug - contract slug

      module:contrato.Contract#getSlug

      const contract = new Contract({
      type: 'arch.sw',
      name: 'armv7hf',
      slug: 'armv7hf'
      })

      console.log(contract.getSlug())
    • Function

      Returns string

      type - contract type

      module:contrato.Contract#getType

      const contract = new Contract({
      type: 'arch.sw',
      name: 'armv7hf',
      slug: 'armv7hf'
      })

      console.log(contract.getType())
    • Function

      Returns string | undefined

      slug - contract version

      module:contrato.Contract#getVersion

      const contract = new Contract({
      type: 'sw.os',
      name: 'Debian Wheezy',
      version: 'wheezy',
      slug: 'debian'
      })

      console.log(contract.getVersion())
    • Function

      Returns boolean

      whether the contract has aliases

      module:contrato.Contract#hasAliases

      const contract = new Contract({
      type: 'hw.device-type',
      name: 'Raspberry Pi',
      slug: 'raspberrypi',
      aliases: [ 'rpi', 'raspberry-pi' ]
      })

      if (contract.hasAliases()) {
      console.log('This contract has aliases')
      }
    • Protected Function

      Returns string

      the contract hash

      module:contrato.Contract#hash

      The hash is computed from the contract's raw object the first time it is requested, and cached on the Rust side afterwards. Operations that mutate the contract invalidate that cache, so the hash is recomputed on the next call.

      const contract = new Contract({ ... })
      console.log(contract.hash())
    • Protected Function

      Returns this

      contract instance

      module:contrato.Contract#interpolate

      if a templated field interpolates to an invalid value, e.g. a slug template resolving to a string with a space. The contract is left untouched in that case.

      const contract = new Contract({ ... })
      contract.interpolate()
    • Function

      Parameters

      Returns this

      parent contract

      module:contrato.Contract#removeChild

      const contract = new Contract({ ... })

      const child = new Contract({ ... })
      contract.addChild(child)
      contract.removeChild(child)
    • Function

      Parameters

      Returns IterableIterator<Contract>

      • an iterable over the valid contexts

      module:contrato.Blueprint#reproduce

      This method will generate a set of contexts that consist of every possible valid combination that matches the blueprint layout. It uses depth first search to calculate the product of contract combinations and returns the results as an iterable. This allows to reduce the memory usage when dealing with a large universe of contracts.

      while iterating, if a generated context is invalid, e.g. a skeleton slug template that interpolates to a value which is not a valid slug. The iterator cannot be resumed past the failure.

      const contract = new Contract({ ... })
      contract.addChildren([ ... ])

      const blueprint = new Blueprint({
      'hw.device-type': 1,
      'arch.sw': 1
      })

      const contexts = blueprint.reproduce(contract)
      for (const context of contexts) {
      console.log(context.toJSON());
      }
    • Function

      Parameters

      • contract: Contract

        child contract

      • Optionaloptions: { types?: Set<string> } = {}

        options

        • Optionaltypes?: Set<string>

          the types to consider (all by default)

      Returns boolean

      whether the contract is satisfied

      module:contrato.Contract#satisfiesChildContract

      const contract = new Contract({ ... })
      contract.addChildren([
      new Contract({
      type: 'sw.os',
      name: 'Debian Wheezy',
      version: 'wheezy',
      slug: 'debian'
      }),
      new Contract({
      type: 'sw.os',
      name: 'Fedora 25',
      version: '25',
      slug: 'fedora'
      })
      ])

      const child = new Contract({
      type: 'sw.stack',
      name: 'Node.js',
      version: '4.8.0',
      slug: 'nodejs',
      requires: [
      {
      or: [
      {
      type: 'sw.os',
      slug: 'debian'
      },
      {
      type: 'sw.os',
      slug: 'fedora'
      }
      ]
      }
      ]
      })

      if (contract.satisfiesChildContract(child)) {
      console.log('The child contract is satisfied!')
      }
    • Function

      Parameters

      Returns Contract[]

      built contracts

      module:contrato.Contract.build

      const contracts = Contract.build({
      name: 'debian {{version}}',
      slug: 'debian',
      type: 'sw.os',
      variants: [
      { version: 'wheezy' },
      { version: 'jessie' },
      { version: 'sid' }
      ]
      })

      contracts.forEach((contract) => {
      if (contract instanceof Contract) {
      console.log('This is a built contract')
      }
      })

      if an expanded contract is invalid, e.g. a variant that completes a templated slug with an illegal value.

    • Function

      Parameters

      Returns MatcherObject

      matcher

      module:contrato.Contract.createMatcher

      A matcher allows to search for child contracts by type, slug, version range and data. It is a plain object handed straight to the WASM boundary, where contrato::ContractMatcher validates it — matchers carrying fields other than type, slug, version and data are rejected there, at findChildren time.

      // find all child contracts with type `hw.device-type` and `data`
      // containing `{arch: 'armv7hf'}`
      mycontract.findChildren(Contract.createMatcher({
      type: 'hw.device-type',
      data: { arch: 'armv7hf' },
      }));
    • Function

      Parameters

      Returns boolean

      whether the contracts are equal

      module:contrato.Contract.isEqual

      const contract1 = new Contract({ ... })
      const contract2 = new Contract({ ... })

      if (Contract.isEqual(contract1, contract2)) {
      console.log('These contracts are equal')
      }