Détail du package

npm-pick-manifest

npm40mISC10.0.0

Resolves a matching manifest from a package metadata document according to standard npm semver resolution rules.

npm, semver, package manager

readme

npm-pick-manifest npm version license Travis Coverage Status

npm-pick-manifest is a standalone implementation of npm's semver range resolution algorithm.

Install

$ npm install --save npm-pick-manifest

Table of Contents

Example

const pickManifest = require('npm-pick-manifest')

fetch('https://registry.npmjs.org/npm-pick-manifest').then(res => {
  return res.json()
}).then(packument => {
  return pickManifest(packument, '^1.0.0')
}) // get same manifest as npm would get if you `npm i npm-pick-manifest@^1.0.0`

Features

  • Uses npm's exact semver resolution algorithm.
  • Supports ranges, tags, and versions.
  • Prefers non-deprecated versions to deprecated versions.
  • Prefers versions whose engines requirements are satisfied over those that will raise a warning or error at install time.

API

> pickManifest(packument, selector, [opts]) -> manifest

Returns the manifest that best matches selector, or throws an error.

Packuments are anything returned by metadata URLs from the npm registry. That is, they're objects with the following shape (only fields used by npm-pick-manifest included):

{
  name: 'some-package',
  'dist-tags': {
    foo: '1.0.1'
  },
  versions: {
    '1.0.0': { version: '1.0.0' },
    '1.0.1': { version: '1.0.1' },
    '1.0.2': { version: '1.0.2' },
    '2.0.0': { version: '2.0.0' }
  }
}

The algorithm will follow npm's algorithm for semver resolution, and only tag, range, and version selectors are supported.

The function will throw ETARGET if there was no matching manifest, and ENOVERSIONS if the packument object has no valid versions in versions. If the only matching manifest is included in a policyRestrictions section of the packument, then an E403 is raised.

Options

All options are optional.

  • includeStaged - Boolean, default false. Include manifests in the stagedVersions.versions set, to support installing staged packages when appropriate. Note that staged packages are always treated as lower priority than actual publishes, even when includeStaged is set.
  • defaultTag - String, default 'latest'. The default dist-tag to install when no specifier is provided. Note that the version indicated by this specifier will be given top priority if it matches a supplied semver range.
  • before - String, Date, or Number, default null. This is passed to new Date(), so anything that works there will be valid. Do not consider any manifests that were published after the date indicated. Note that this is only relevant when the packument includes a time field listing the publish date of all the packages.
  • nodeVersion - String, default process.version. The Node.js version to use when checking manifests for engines requirement satisfaction.
  • npmVersion - String, default null. The npm version to use when checking manifest for engines requirement satisfaction. (If null, then this particular check is skipped.)
  • avoid - String, default null. A SemVer range of versions that should be avoided. An avoided version MAY be selected if there is no other option, so when using this for version selection ensure that you check the result against the range to see if there was no alternative available.
  • avoidStrict Boolean, default false. If set to true, then pickManifest will never return a version in the avoid range. If the only available version in the wanted range is a version that should be avoided, then it will return a version outside the wanted range, preferring to do so without making a SemVer-major jump, if possible. If there are no versions outside the avoid range, then throw an ETARGET error. It does this by calling pickManifest first with the wanted range, then with a ^ affixed to the version returned by the wanted range, and then with a * version range, and throwing if nothing could be found to satisfy the avoidance request.

Return value is the manifest as it exists in the packument, possibly decorated with the following boolean flags:

  • _shouldAvoid The version is in the avoid range. Watch out!
  • _outsideDependencyRange The version is outside the wanted range, because avoidStrict: true was set.
  • _isSemVerMajor The _outsideDependencyRange result is a SemVer-major step up from the version returned by the wanted range.

Algorithm

  1. Create list of all versions in versions, policyRestrictions.versions, and (if includeStaged is set) stagedVersions.versions.
  2. If a dist-tag is requested,
    1. If the manifest is not after the specified before date, then select that from the set.
    2. If the manifest is after the specified before date, then re-start the selection looking for the highest SemVer range that is equal to or less than the dist-tag target.
  3. If a specific version is requested,
    1. If the manifest is not after the specified before date, then select the specified manifest.
    2. If the manifest is after the specified before date, then raise ETARGET error. (NB: this is a breaking change from v5, where a specified version would override the before setting.)
  4. (At this point we know a range is requested.)
  5. If the defaultTag refers to a dist-tag that satisfies the range (or if the range is '*' or ''), and the manifest is published before the before setting, then select that manifest.
  6. If nothing is yet selected, sort by the following heuristics in order, and select the top item:
    1. Prioritize versions that are not in the avoid range over those that are.
    2. Prioritize versions that are not in policyRestrictions over those that are.
    3. Prioritize published versions over staged versions.
    4. Prioritize versions that are not deprecated, and which have a satisfied engines requirement, over those that are either deprecated or have an engines mismatch.
    5. Prioritize versions that have a satisfied engines requirement over those that do not.
    6. Prioritize versions that are not are not deprecated (but have a mismatched engines requirement) over those that are deprecated.
    7. Prioritize higher SemVer precedence over lower SemVer precedence.
  7. If no manifest was selected, raise an ETARGET error.
  8. If the selected item is in the policyRestrictions.versions list, raise an E403 error.
  9. Return the selected manifest.

changelog

Changelog

10.0.0 (2024-09-26)

⚠️ BREAKING CHANGES

  • npm-pick-manifest now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • dd83a53 #145 align to npm 10 node engine range (@reggi)

    Dependencies

  • 6b4df8d #145 npm-package-arg@12.0.0
  • c2ae5b7 #145 npm-normalize-package-bin@4.0.0
  • e948cef #145 npm-install-checks@7.1.0

    Chores

  • 2e1fdb4 #145 run template-oss-apply (@reggi)
  • 7891f6b #140 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • ae2ffc5 #138 postinstall for dependabot template-oss PR (@hashtagchris)
  • 7744312 #138 bump @npmcli/template-oss from 4.22.0 to 4.23.3 (@dependabot[bot])

9.1.0 (2024-07-09)

Features

  • 9f5560f #33 improve heuristics when dist-tags.latest is in range (#33) (@isaacs)

9.0.1 (2024-05-04)

Bug Fixes

Chores

  • 7242a55 #126 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • 581d69b #126 postinstall for dependabot template-oss PR (@lukekarrys)
  • dd5fc17 #125 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

9.0.0 (2023-08-15)

⚠️ BREAKING CHANGES

  • support for node 14 has been removed

Bug Fixes

Dependencies

  • 8bdb3a6 #94 bump npm-package-arg from 10.1.0 to 11.0.0

8.0.2 (2023-07-26)

Bug Fixes

  • 66c3754 #91 fallback for missing packument.versions (#91) (@chernodub)

8.0.1 (2022-10-18)

Dependencies

  • 9a5d485 #73 bump npm-package-arg from 9.1.2 to 10.0.0

8.0.0 (2022-10-14)

⚠️ BREAKING CHANGES

  • npm-pick-manifest is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • a3fc66c #61 postinstall for dependabot template-oss PR (@lukekarrys)

Dependencies

  • cf6ddfd #70 bump npm-install-checks from 5.0.0 to 6.0.0
  • 518aba1 #69 bump npm-normalize-package-bin from 2.0.0 to 3.0.0

7.0.2 (2022-08-25)

Dependencies

  • bump npm-normalize-package-bin from 1.0.1 to 2.0.0 (#60) (933ce0f)

7.0.1 (2022-04-05)

Dependencies

  • bump npm-install-checks from 4.0.0 to 5.0.0 (#53) (e425301)

7.0.0 (2022-02-10)

⚠ BREAKING CHANGES

  • This drops support for node 10 and non-LTS versions of node 12 and node 14

Dependencies

  • @npmcli/template-oss@2.7.1 (73e6f4f)
  • bump npm-package-arg from 8.1.5 to 9.0.0 (82f45ce)
  • update semver requirement from ^7.3.4 to ^7.3.5 (9564d23)

6.1.1 (2020-04-07)

  • normalize package bins in returned manifest

6.1.0 (2020-04-07)

Features

  • add 'avoid' semver range option (c64973d)
  • add avoidStrict option to strictly avoid (c268796), closes #30

6.0.0 (2020-02-18)

⚠ BREAKING CHANGES

  • 'enjoyBy' is no longer an acceptable alias.

Features

  • add GitHub Actions file for ci (8985247)

Bug Fixes

  • Handle edge cases around before:Date and filtering staged publishes (ed2f92e)
  • remove figgy pudding (c24fed2)
  • remove outdated cruft from docs (aae7ef7)
  • update some missing {loose:true} semver configs (4015424)
  • Use canonical 'before' config name (029de59)

5.0.0 (2019-12-15)

⚠ BREAKING CHANGES

  • This drops support for node < 10.

  • normalize settings, drop old nodes, update deps (dc2e61c)

4.0.0 (2019-11-11)

deps

BREAKING CHANGES

  • this drops support for ancient node versions.

3.0.2 (2019-08-30)

3.0.1 (2019-08-28)

Bug Fixes

  • throw 403 for forbidden major/minor versions (003286e), closes #2

3.0.0 (2019-08-20)

Features

  • throw forbidden error when package is blocked by policy (ad2a962), closes #1

BREAKING CHANGES

  • This adds a new error code when package versions are blocked.

PR-URL: https://github.com/npm/npm-pick-manifest/pull/1 Credit: @claudiahdz

2.2.3 (2018-10-31)

Bug Fixes

  • enjoyBy: rework semantics for enjoyBy again (5e89b62)

2.2.2 (2018-10-31)

Bug Fixes

  • enjoyBy: rework semantics for enjoyBy (5684f45)

2.2.1 (2018-10-30)

2.2.0 (2018-10-30)

Bug Fixes

  • audit: npm audit fix --force (d5ae6c4)

Features

  • enjoyBy: add opts.enjoyBy option to filter versions by date (0b8a790)

2.1.0 (2017-10-18)

Features

  • selection: allow manually disabling deprecation skipping (0d239d3)

2.0.1 (2017-10-18)

2.0.0 (2017-10-03)

Bug Fixes

  • license: relicense project according to npm policy (#3) (ed743a0)

Features

  • selection: Avoid matching deprecated packages if possible (3fc6c3a)

BREAKING CHANGES

  • selection: deprecated versions may be skipped now
  • license: This moves the license from CC0 to ISC and properly documents the copyright as belonging to npm, Inc.

1.0.4 (2017-06-29)

Bug Fixes

  • npa: bump npa version for bugfixes (7cdaca7)
  • semver: use loose semver parsing for all ops (bbc0daa)

1.0.3 (2017-05-04)

Bug Fixes

  • semver: use semver.clean() instead (f4133b5)

1.0.2 (2017-05-04)

Bug Fixes

  • picker: spaces in wanted prevented match (97a7d0a)

1.0.1 (2017-04-24)

Bug Fixes

  • deps: forgot to add semver (1876f4f)

1.0.0 (2017-04-24)

Features

  • api: initial implementation. (b086912)

BREAKING CHANGES

  • api: ex nihilo