包详细信息

pacote

npm35.1mISC21.0.0

JavaScript package downloader

packages, npm, git

自述文件

pacote

Fetches package manifests and tarballs from the npm registry.

USAGE

const pacote = require('pacote')

// get a package manifest
pacote.manifest('foo@1.x').then(manifest => console.log('got it', manifest))

// extract a package into a folder
pacote.extract('github:npm/cli', 'some/path', options)
  .then(({from, resolved, integrity}) => {
    console.log('extracted!', from, resolved, integrity)
  })

pacote.tarball('https://server.com/package.tgz').then(data => {
  console.log('got ' + data.length + ' bytes of tarball data')
})

pacote works with any kind of package specifier that npm can install. If you can pass it to the npm CLI, you can pass it to pacote. (In fact, that's exactly what the npm CLI does.)

Anything that you can do with one kind of package, you can do with another.

Data that isn't relevant (like a packument for a tarball) will be simulated.

prepare scripts will be run when generating tarballs from git and directory locations, to simulate what would be published to the registry, so that you get a working package instead of just raw source code that might need to be transpiled.

CLI

This module exports a command line interface that can do most of what is described below. Run pacote -h to learn more.

Pacote - The JavaScript Package Handler, v10.1.1

Usage:

  pacote resolve <spec>
    Resolve a specifier and output the fully resolved target
    Returns integrity and from if '--long' flag is set.

  pacote manifest <spec>
    Fetch a manifest and print to stdout

  pacote packument <spec>
    Fetch a full packument and print to stdout

  pacote tarball <spec> [<filename>]
    Fetch a package tarball and save to <filename>
    If <filename> is missing or '-', the tarball will be streamed to stdout.

  pacote extract <spec> <folder>
    Extract a package to the destination folder.

Configuration values all match the names of configs passed to npm, or
options passed to Pacote.  Additional flags for this executable:

  --long     Print an object from 'resolve', including integrity and spec.
  --json     Print result objects as JSON rather than node's default.
             (This is the default if stdout is not a TTY.)
  --help -h  Print this helpful text.

For example '--cache=/path/to/folder' will use that folder as the cache.

API

The spec refers to any kind of package specifier that npm can install. If you can pass it to the npm CLI, you can pass it to pacote. (In fact, that's exactly what the npm CLI does.)

See below for valid opts values.

  • pacote.resolve(spec, opts) Resolve a specifier like foo@latest or github:user/project all the way to a tarball url, tarball file, or git repo with commit hash.

  • pacote.extract(spec, dest, opts) Extract a package's tarball into a destination folder. Returns a promise that resolves to the {from,resolved,integrity} of the extracted package.

  • pacote.manifest(spec, opts) Fetch (or simulate) a package's manifest (basically, the package.json file, plus a bit of metadata). See below for more on manifests and packuments. Returns a Promise that resolves to the manifest object.

  • pacote.packument(spec, opts) Fetch (or simulate) a package's packument (basically, the top-level package document listing all the manifests that the registry returns). See below for more on manifests and packuments. Returns a Promise that resolves to the packument object.

  • pacote.tarball(spec, opts) Get a package tarball data as a buffer in memory. Returns a Promise that resolves to the tarball data Buffer, with from, resolved, and integrity fields attached.

  • pacote.tarball.file(spec, dest, opts) Save a package tarball data to a file on disk. Returns a Promise that resolves to {from,integrity,resolved} of the fetched tarball.

  • pacote.tarball.stream(spec, streamHandler, opts) Fetch a tarball and make the stream available to the streamHandler function.

    This is mostly an internal function, but it is exposed because it does provide some functionality that may be difficult to achieve otherwise.

    The streamHandler function MUST return a Promise that resolves when the stream (and all associated work) is ended, or rejects if the stream has an error.

    The streamHandler function MAY be called multiple times, as Pacote retries requests in some scenarios, such as cache corruption or retriable network failures.

Options

Options are passed to npm-registry-fetch and cacache, so in addition to these, anything for those modules can be given to pacote as well.

Options object is cloned, and mutated along the way to add integrity, resolved, and other properties, as they are determined.

  • cache Where to store cache entries and temp files. Passed to cacache. Defaults to the same cache directory that npm will use by default, based on platform and environment.
  • where Base folder for resolving relative file: dependencies.
  • resolved Shortcut for looking up resolved values. Should be specified if known.
  • integrity Expected integrity of fetched package tarball. If specified, tarballs with mismatched integrity values will raise an EINTEGRITY error.
  • umask Permission mode mask for extracted files and directories. Defaults to 0o22. See "Extracted File Modes" below.
  • fmode Minimum permission mode for extracted files. Defaults to 0o666. See "Extracted File Modes" below.
  • dmode Minimum permission mode for extracted directories. Defaults to 0o777. See "Extracted File Modes" below.
  • preferOnline Prefer to revalidate cache entries, even when it would not be strictly necessary. Default false.
  • before When picking a manifest from a packument, only consider packages published before the specified date. Default null.
  • defaultTag The default dist-tag to use when choosing a manifest from a packument. Defaults to latest.
  • registry The npm registry to use by default. Defaults to https://registry.npmjs.org/.
  • fullMetadata Fetch the full metadata from the registry for packuments, including information not strictly required for installation (author, description, etc.) Defaults to true when before is set, since the version publish time is part of the extended packument metadata.
  • fullReadJson Use the slower read-package-json package insted of read-package-json-fast in order to include extra fields like "readme" in the manifest. Defaults to false.
  • packumentCache For registry packuments only, you may provide a Map object which will be used to cache packument requests between pacote calls. This allows you to easily avoid hitting the registry multiple times (even just to validate the cache) for a given packument, since it is unlikely to change in the span of a single command.
  • verifySignatures A boolean that will make pacote verify the integrity signature of a manifest, if present. There must be a configured _keys entry in the config that is scoped to the registry the manifest is being fetched from.
  • verifyAttestations A boolean that will make pacote verify Sigstore attestations, if present. There must be a configured _keys entry in the config that is scoped to the registry the manifest is being fetched from.
  • tufCache Where to store metadata/target files when retrieving the package attestation key material via TUF. Defaults to the same cache directory that npm will use by default, based on platform and environment.

Advanced API

Each different type of fetcher is exposed for more advanced usage such as using helper methods from this classes:

  • DirFetcher
  • FileFetcher
  • GitFetcher
  • RegistryFetcher
  • RemoteFetcher

Extracted File Modes

Files are extracted with a mode matching the following formula:

( (tarball entry mode value) | (minimum mode option) ) ~ (umask)

This is in order to prevent unreadable files or unlistable directories from cluttering a project's node_modules folder, even if the package tarball specifies that the file should be inaccessible.

It also prevents files from being group- or world-writable without explicit opt-in by the user, because all file and directory modes are masked against the umask value.

So, a file which is 0o771 in the tarball, using the default fmode of 0o666 and umask of 0o22, will result in a file mode of 0o755:

(0o771 | 0o666) => 0o777
(0o777 ~ 0o22) => 0o755

In almost every case, the defaults are appropriate. To respect exactly what is in the package tarball (even if this makes an unusable system), set both dmode and fmode options to 0. Otherwise, the umask config should be used in most cases where file mode modifications are required, and this functions more or less the same as the umask value in most Unix systems.

Extracted File Ownership

When running as root on Unix systems, all extracted files and folders will have their owning uid and gid values set to match the ownership of the containing folder.

This prevents root-owned files showing up in a project's node_modules folder when a user runs sudo npm install.

Manifests

A manifest is similar to a package.json file. However, it has a few pieces of extra metadata, and sometimes lacks metadata that is inessential to package installation.

In addition to the common package.json fields, manifests include:

  • manifest._resolved The tarball url or file path where the package artifact can be found.
  • manifest._from A normalized form of the spec passed in as an argument.
  • manifest._integrity The integrity value for the package artifact.
  • manifest._id The canonical spec of this package version: name@version.
  • manifest.dist Registry manifests (those included in a packument) have a dist object. Only tarball is required, though at least one of shasum or integrity is almost always present.

    • tarball The url to the associated package artifact. (Copied by Pacote to manifest._resolved.)
    • integrity The integrity SRI string for the artifact. This may not be present for older packages on the npm registry. (Copied by Pacote to manifest._integrity.)
    • shasum Legacy integrity value. Hexadecimal-encoded sha1 hash. (Converted to an SRI string and copied by Pacote to manifest._integrity when dist.integrity is not present.)
    • fileCount Number of files in the tarball.
    • unpackedSize Size on disk of the package when unpacked.
    • signatures Signatures of the shasum. Includes the keyid that correlates to a key from the npm registry

Packuments

A packument is the top-level package document that lists the set of manifests for available versions for a package.

When a packument is fetched with accept: application/vnd.npm.install-v1+json in the HTTP headers, only the most minimum necessary metadata is returned. Additional metadata is returned when fetched with only accept: application/json.

For Pacote's purposes, the following fields are relevant:

  • versions An object where each key is a version, and each value is the manifest for that version.
  • dist-tags An object mapping dist-tags to version numbers. This is how foo@latest gets turned into foo@1.2.3.
  • time In the full packument, an object mapping version numbers to publication times, for the opts.before functionality.

Pacote adds the following field, regardless of the accept header:

  • _contentLength The size of the packument.

更新日志

Changelog

21.0.0 (2024-11-25)

⚠️ BREAKING CHANGES

  • bun.lockb files are now included in the strict ignore list during packing
  • this module is now compatible with the following node versions: ^20.17.0 || >=22.9.0

    Bug Fixes

  • 844dc08 update node engines to ^20.17.0 || >=22.9.0 (#414) (@wraithgar)

    Dependencies

  • 2cb6fa7 #415 npm-packlist@10.0.0 (#415)
  • 47b928c #412 replace node builtin rmSync with rimraf (#412) (@mbtools)

    Chores

  • b6f35a2 #402 bump @npmcli/arborist from 7.5.4 to 8.0.0 (#402) (@dependabot[bot])
  • 1ef54ba #408 support tests on win32 (#408) (@mbtools)
  • 555b000 #401 bump @npmcli/template-oss from 4.23.3 to 4.23.4 (#401) (@dependabot[bot], @npm-cli-bot)

20.0.0 (2024-10-17)

⚠️ BREAKING CHANGES

  • honors ignoreScripts property within options

    Bug Fixes

  • f27af63 #407 honors ignoreScripts option to prevent prepare lifecycle script (@reggi)

19.0.1 (2024-10-15)

Bug Fixes

  • cbf94e8 #389 prepare script respects scriptshell config (#389) (@milaninfy)
  • 2b2948f #403 log tarball retrieval from cache (#403) (@mbtools, @wraithgar)

    Dependencies

  • a9fc4d1 #405 bump sigstore from 2.2.0 to 3.0.0 (#405) (@bdehamer)

19.0.0 (2024-09-27)

⚠️ BREAKING CHANGES

  • pacote now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • 03b31ca #392 align to npm 10 node engine range (@reggi)

    Dependencies

  • f055f71 #395 bump npm-pick-manifest from 9.1.0 to 10.0.0 (#395) (@dependabot[bot])
  • 932b9ab #396 bump @npmcli/package-json from 5.2.1 to 6.0.0 (#396) (@dependabot[bot])
  • a1621f9 #397 bump npm-registry-fetch from 17.1.0 to 18.0.0 (#397) (@dependabot[bot])
  • c776199 #398 bump cacache from 18.0.4 to 19.0.0 (#398) (@dependabot[bot])
  • 6d59022 #399 bump @npmcli/git from 5.0.8 to 6.0.0 (#399)
  • 21ea2d4 #400 bump @npmcli/run-script from 8.1.0 to 9.0.0 (#400)
  • eddbc01 #392 ssri@12.0.0
  • 6c672e9 #392 proc-log@5.0.0
  • 03ba2a2 #392 npm-packlist@9.0.0
  • 2710286 #392 npm-package-arg@12.0.0
  • aa0bd4a #392 @npmcli/promise-spawn@8.0.0
  • df23343 #392 @npmcli/installed-package-contents@3.0.0

    Chores

  • e4ed5cd #392 bump hosted-git-info ^7.0.0 to ^8.0.0 (@reggi)
  • 2871f56 #392 run template-oss-apply (@reggi)
  • 39643f1 #382 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 7e33c82 #383 postinstall for dependabot template-oss PR (@hashtagchris)
  • e4e07bf #383 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

18.0.6 (2024-05-07)

Bug Fixes

  • 79441a5 #371 clean up requires (#371) (@wraithgar)
  • b19aacb #369 isolate full and corgi packuments in packumentCache (#369) (@wraithgar)

18.0.5 (2024-05-06)

Bug Fixes

  • 5e75582 #368 dont set _contentLength if not in headers (#368) (@lukekarrys)
  • 1b6950b #365 move bin to its own directory (@lukekarrys)
  • 1b6950b #365 refactor: symbol cleanup (#365) (@lukekarrys)

18.0.4 (2024-05-04)

Bug Fixes

Chores

  • d867639 #363 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • a235f37 #363 postinstall for dependabot template-oss PR (@lukekarrys)

18.0.3 (2024-04-30)

Dependencies

18.0.2 (2024-04-24)

Bug Fixes

  • 116b277 #358 don't strip underscore attributes in .manifest() (#358) (@wraithgar)

18.0.1 (2024-04-23)

Bug Fixes

  • b547e0d #356 use @npmcli/package-json (#356) (@lukekarrys)

18.0.0 (2024-04-15)

⚠️ BREAKING CHANGES

  • The silent option was used to control whether @npmcli/run-script would write a banner via console.log. Now ouput will be emitted via an process.emit('output').

Features

Dependencies

  • cb3abc2 #352 bump @npmcli/run-script from 7.0.4 to 8.0.0 (@dependabot[bot])

Chores

  • 7089bb1 #355 postinstall for dependabot template-oss PR (@lukekarrys)
  • 4952672 #355 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

17.0.7 (2024-04-12)

Dependencies

17.0.6 (2024-01-16)

Dependencies

  • 0a5920f #343 bump sigstore from 2.0.0 to 2.2.0 (#343) (@bdehamer)

Chores

  • 6fd23ad #342 postinstall for dependabot template-oss PR (@lukekarrys)
  • c3b398a #342 bump @npmcli/template-oss from 4.21.1 to 4.21.3 (@dependabot[bot])
  • 4557919 #337 postinstall for dependabot template-oss PR (@lukekarrys)
  • c7e293c #337 bump @npmcli/template-oss from 4.19.0 to 4.21.1 (@dependabot[bot])

17.0.5 (2023-12-01)

Bug Fixes

  • 0c96b9e #338 bug to support rotated keys in signature/attestation audit (#338) (@feelepxyz)

17.0.4 (2023-08-30)

Dependencies

  • ba8f790 #309 bump @npmcli/promise-spawn from 6.0.2 to 7.0.0
  • 2c0d3ae #308 bump @npmcli/run-script from 6.0.2 to 7.0.0

17.0.3 (2023-08-24)

Dependencies

17.0.2 (2023-08-18)

Dependencies

17.0.1 (2023-08-15)

Dependencies

  • 6ddae13 #302 bump npm-registry-fetch from 15.0.0 to 16.0.0
  • 42bf787 #300 bump npm-pick-manifest from 8.0.2 to 9.0.0

17.0.0 (2023-08-15)

⚠️ BREAKING CHANGES

  • support for node <=16.13 has been removed

Bug Fixes

Dependencies

16.0.0 (2023-07-28)

⚠️ BREAKING CHANGES

  • the underlying fetch module now uses @npmcli/agent. Backwards compatibility should be fully implemented but due to the scope of this change it was made a breaking change out of an abundance of caution.
  • support for node 14 has been removed

Bug Fixes

Dependencies

  • 8dc6a32 bump minipass from 5.0.0 to 7.0.2
  • 7cebf19 bump npm-registry-fetch from 14.0.5 to 15.0.0

15.2.0 (2023-05-03)

Features

  • 3307ad9 #278 configurable TUF cache dir (#278) (@bdehamer)

15.1.3 (2023-04-27)

Dependencies

  • c99db13 #271 bump minipass from 4.2.7 to 5.0.0 (#271)

15.1.2 (2023-04-20)

Documentation

Dependencies

15.1.1 (2023-02-21)

Bug Fixes

  • 8f4e39c #261 always ignore ownership from tar headers (#261) (@nlf)

15.1.0 (2023-02-13)

Features

  • 2916b72 #259 verifyAttestations to registry.manifest (@feelepxyz, @bdehamer)

Dependencies

15.0.8 (2022-12-14)

Dependencies

15.0.7 (2022-12-07)

Dependencies

15.0.6 (2022-11-02)

Dependencies

15.0.5 (2022-11-01)

Dependencies

  • 63797a8 #244 bump @npmcli/promise-spawn from 5.0.0 to 6.0.1 (#244)

15.0.4 (2022-10-26)

Dependencies

  • 854fad1 #239 bump @npmcli/promise-spawn from 4.0.0 to 5.0.0 (#239)

15.0.3 (2022-10-19)

Dependencies

  • 2a95ddb #235 bump @npmcli/installed-package-contents (#235)

15.0.2 (2022-10-18)

Bug Fixes

  • 95f9cd5 handle new npm-package-arg semantics (@wraithgar)

Dependencies

15.0.1 (2022-10-17)

Dependencies

  • 74821c2 #229 bump @npmcli/run-script from 4.2.1 to 5.0.0 (#229)
  • a9844d0 #226 bump @npmcli/promise-spawn from 3.0.0 to 4.0.0 (#226)
  • 1058177 #227 bump read-package-json from 5.0.2 to 6.0.0
  • 0f5ef8a #228 bump @npmcli/installed-package-contents from 1.0.7 to 2.0.0
  • 7e3b4b5 #220 bump ssri from 9.0.1 to 10.0.0
  • 4e7536d #222 bump @npmcli/git from 3.0.2 to 4.0.0
  • 3bc7550 #223 bump npm-pick-manifest from 7.0.2 to 8.0.0
  • 41fab27 #224 bump proc-log from 2.0.1 to 3.0.0
  • 4abf24a #218 bump npm-registry-fetch from 13.3.1 to 14.0.0 (#218)

15.0.0 (2022-10-13)

⚠️ BREAKING CHANGES

  • this package no longer attempts to change file ownership automatically

Features

  • 43ae022 #216 do not alter file ownership (#216) (@nlf)

Dependencies

  • 2ac3980 #213 bump read-package-json-fast from 2.0.3 to 3.0.0

14.0.0 (2022-10-05)

Features

14.0.0-pre.3 (2022-09-28)

⚠️ BREAKING CHANGES

  • a @npmcli/arborist constructor must be passed in if no tree is provided and pacote is going to operate on git dependencies.

Features

  • d6ef5dc #204 require arborist constructor to be passed in for preparing git dirs (#204) (@lukekarrys)

14.0.0-pre.2 (2022-09-27)

⚠️ BREAKING CHANGES

  • pacote now has a peer dependency on @npmcli/arborist.

Features

  • d3517fd #202 pacote now optionally takes a tree when preparing directories (@lukekarrys)

14.0.0-pre.1 (2022-09-22)

⚠️ BREAKING CHANGES

  • the _cached attribute has been removed from packuments.

Bug Fixes

  • 8ca3751 #175 packument: eliminate _cached field (#175) (@jablko)

14.0.0-pre.0 (2022-09-21)

⚠️ BREAKING CHANGES

  • npm-packlist@6.0.0
  • pacote is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • 72e9be4 #197 postinstall for dependabot template-oss PR (@lukekarrys)

Dependencies

13.6.2 (2022-08-16)

Bug Fixes

13.6.1 (2022-06-21)

Dependencies

  • bump @npmcli/run-script from 3.0.3 to 4.1.0 (#185) (d0459ec)

13.6.0 (2022-06-01)

Features

  • allow reuse of external integrity stream (fdb9e5a)
  • replaceRegistryHost can now be a hostname (#177) (a9a4cdd)

Bug Fixes

  • error when passing signature without keys (#176) (d69e524)

Documentation

13.5.0 (2022-05-25)

Features

  • bump npm-packlist for workspace awareness (#178) (316059b)

13.4.1 (2022-05-19)

Bug Fixes

  • pass prefix and workspaces to npm-packlist (#173) (6de3a2b)

13.4.0 (2022-05-17)

Features

  • add verifySignatures to registry.manifest (#170) (4401c58)

13.3.0 (2022-05-04)

Features

  • add _signatures to manifest (3ae73f2)

13.2.0 (2022-05-02)

Features

  • add always option to replaceRegistryHost (#164) (edd1ee5)

13.1.1 (2022-04-06)

Dependencies

  • bump npm-packlist from 4.0.0 to 5.0.0 (#159) (d7f07d6)

13.1.0 (2022-04-05)

Features

  • add option to not replace magic registry host (#143) (f519cf4)

13.0.6 (2022-04-05)

Bug Fixes

  • replace deprecated String.prototype.substr() (e307e17)

Dependencies

  • bump @npmcli/promise-spawn from 1.3.2 to 3.0.0 (#154) (9a0ec63)
  • bump ssri from 8.0.1 to 9.0.0 (#157) (0993b18)

13.0.5 (2022-03-15)

Dependencies

  • bump read-package-json from 4.1.2 to 5.0.0 (#138) (f28c891)

13.0.4 (2022-03-14)

Dependencies

  • bump cacache from 15.3.0 to 16.0.0 (#136) (ed3a069)
  • bump npm-packlist from 3.0.0 to 4.0.0 (#132) (1634e9d)
  • update @npmcli/run-script requirement from ^3.0.0 to ^3.0.1 (#130) (7c84792)
  • update npm-registry-fetch requirement from ^13.0.0 to ^13.0.1 (#129) (d639ed6)
  • update read-package-json requirement from ^4.1.1 to ^4.1.2 (#134) (31093a1)

13.0.3 (2022-02-23)

Bug Fixes

  • ignore integrity values for git dependencies (#123) (3417714)

Dependencies

  • bump @npmcli/run-script from 2.0.0 to 3.0.0 (#124) (6026b73)

13.0.2 (2022-02-16)

Bug Fixes

  • run prepack lifecycle scripts on git fetcher (#121) (82d8afc)

Dependencies

  • bump @npmcli/git from 2.1.0 to 3.0.0 (#120) (56d0c62)

13.0.1 (2022-02-16)

Bug Fixes

  • reify git dependencies that have workspaces (#103) (08348fa)

Dependencies

  • bump npm-registry-fetch from 12.0.2 to 13.0.0 (#118) (25eeb97)

13.0.0 (2022-02-14)

⚠ BREAKING CHANGES

  • It replaces the only use of npmlog.level with a boolean silent which is now used to to suppress @npmcli/run-script banners instead.

Features

Dependencies

  • bump npm-package-arg from 8.1.5 to 9.0.0 (#113) (5b3b82d)
  • bump npm-pick-manifest from 6.1.1 to 7.0.0 (3940b46)
  • update @npmcli/installed-package-contents requirement (0413eff)
  • update cacache requirement from ^15.0.5 to ^15.3.0 (#112) (0321cf0)
  • update minipass requirement from ^3.1.3 to ^3.1.6 (#115) (9548c8c)
  • update mkdirp requirement from ^1.0.3 to ^1.0.4 (c204aa2)
  • update npm-registry-fetch requirement from ^12.0.0 to ^12.0.2 (97e7ab5)
  • update read-package-json-fast requirement from ^2.0.1 to ^2.0.3 (be32161)
  • update tar requirement from ^6.1.0 to ^6.1.11 (#107) (650e188)