• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..20-Sep-2020-

node_modules/safe-buffer/H20-Sep-2020-870590

CHANGELOG.mdH A D26-Oct-19857.8 KiB281120

LICENSE.mdH A D26-Oct-1985755 1713

README.mdH A D26-Oct-198519.4 KiB637430

package.jsonH A D20-Sep-20202.8 KiB105104

README.md

1# npm-registry-fetch [![npm version](https://img.shields.io/npm/v/npm-registry-fetch.svg)](https://npm.im/npm-registry-fetch) [![license](https://img.shields.io/npm/l/npm-registry-fetch.svg)](https://npm.im/npm-registry-fetch) [![Travis](https://img.shields.io/travis/npm/npm-registry-fetch/latest.svg)](https://travis-ci.org/npm/npm-registry-fetch) [![AppVeyor](https://img.shields.io/appveyor/ci/zkat/npm-registry-fetch/latest.svg)](https://ci.appveyor.com/project/npm/npm-registry-fetch) [![Coverage Status](https://coveralls.io/repos/github/npm/npm-registry-fetch/badge.svg?branch=latest)](https://coveralls.io/github/npm/npm-registry-fetch?branch=latest)
2
3[`npm-registry-fetch`](https://github.com/npm/npm-registry-fetch) is a Node.js
4library that implements a `fetch`-like API for accessing npm registry APIs
5consistently. It's able to consume npm-style configuration values and has all
6the necessary logic for picking registries, handling scopes, and dealing with
7authentication details built-in.
8
9This package is meant to replace the older
10[`npm-registry-client`](https://npm.im/npm-registry-client).
11
12## Example
13
14```javascript
15const npmFetch = require('npm-registry-fetch')
16
17console.log(
18  await npmFetch.json('/-/ping')
19)
20```
21
22## Table of Contents
23
24* [Installing](#install)
25* [Example](#example)
26* [Contributing](#contributing)
27* [API](#api)
28  * [`fetch`](#fetch)
29  * [`fetch.json`](#fetch-json)
30  * [`fetch` options](#fetch-opts)
31
32### Install
33
34`$ npm install npm-registry-fetch`
35
36### Contributing
37
38The npm team enthusiastically welcomes contributions and project participation!
39There's a bunch of things you can do if you want to contribute! The [Contributor
40Guide](CONTRIBUTING.md) has all the information you need for everything from
41reporting bugs to contributing entire new features. Please don't hesitate to
42jump in if you'd like to, or even ask us questions if something isn't clear.
43
44All participants and maintainers in this project are expected to follow [Code of
45Conduct](CODE_OF_CONDUCT.md), and just generally be excellent to each other.
46
47Please refer to the [Changelog](CHANGELOG.md) for project history details, too.
48
49Happy hacking!
50
51### API
52
53#### Caching and `write=true` query strings
54
55Before performing any PUT or DELETE operation, npm clients first make a
56GET request to the registry resource being updated, which includes
57the query string `?write=true`.
58
59The semantics of this are, effectively, "I intend to write to this thing,
60and need to know the latest current value, so that my write can land
61cleanly".
62
63The public npm registry handles these `?write=true` requests by ensuring
64that the cache is re-validated before sending a response.  In order to
65maintain the same behavior on the client, and not get tripped up by an
66overeager local cache when we intend to write data to the registry, any
67request that comes through `npm-registry-fetch` that contains `write=true`
68in the query string will forcibly set the `prefer-online` option to `true`,
69and set both `prefer-offline` and `offline` to false, so that any local
70cached value will be revalidated.
71
72#### <a name="fetch"></a> `> fetch(url, [opts]) -> Promise<Response>`
73
74Performs a request to a given URL.
75
76The URL can be either a full URL, or a path to one. The appropriate registry
77will be automatically picked if only a URL path is given.
78
79For available options, please see the section on [`fetch` options](#fetch-opts).
80
81##### Example
82
83```javascript
84const res = await fetch('/-/ping')
85console.log(res.headers)
86res.on('data', d => console.log(d.toString('utf8')))
87```
88
89#### <a name="fetch-json"></a> `> fetch.json(url, [opts]) -> Promise<ResponseJSON>`
90
91Performs a request to a given registry URL, parses the body of the response as
92JSON, and returns it as its final value. This is a utility shorthand for
93`fetch(url).then(res => res.json())`.
94
95For available options, please see the section on [`fetch` options](#fetch-opts).
96
97##### Example
98
99```javascript
100const res = await fetch.json('/-/ping')
101console.log(res) // Body parsed as JSON
102```
103
104#### <a name="fetch-json-stream"></a> `> fetch.json.stream(url, jsonPath, [opts]) -> Stream`
105
106Performs a request to a given registry URL and parses the body of the response
107as JSON, with each entry being emitted through the stream.
108
109The `jsonPath` argument is a [`JSONStream.parse()`
110path](https://github.com/dominictarr/JSONStream#jsonstreamparsepath), and the
111returned stream (unlike default `JSONStream`s), has a valid
112`Symbol.asyncIterator` implementation.
113
114For available options, please see the section on [`fetch` options](#fetch-opts).
115
116##### Example
117
118```javascript
119console.log('https://npm.im/~zkat has access to the following packages:')
120for await (let {key, value} of fetch.json.stream('/-/user/zkat/package', '$*')) {
121  console.log(`https://npm.im/${key} (perms: ${value})`)
122}
123```
124
125#### <a name="fetch-opts"></a> `fetch` Options
126
127Fetch options are optional, and can be passed in as either a Map-like object
128(one with a `.get()` method), a plain javascript object, or a
129[`figgy-pudding`](https://npm.im/figgy-pudding) instance.
130
131##### <a name="opts-agent"></a> `opts.agent`
132
133* Type: http.Agent
134* Default: an appropriate agent based on URL protocol and proxy settings
135
136An [`Agent`](https://nodejs.org/api/http.html#http_class_http_agent) instance to
137be shared across requests. This allows multiple concurrent `fetch` requests to
138happen on the same socket.
139
140You do _not_ need to provide this option unless you want something particularly
141specialized, since proxy configurations and http/https agents are already
142automatically managed internally when this option is not passed through.
143
144##### <a name="opts-body"></a> `opts.body`
145
146* Type: Buffer | Stream | Object
147* Default: null
148
149Request body to send through the outgoing request. Buffers and Streams will be
150passed through as-is, with a default `content-type` of
151`application/octet-stream`. Plain JavaScript objects will be `JSON.stringify`ed
152and the `content-type` will default to `application/json`.
153
154Use [`opts.headers`](#opts-headers) to set the content-type to something else.
155
156##### <a name="opts-ca"></a> `opts.ca`
157
158* Type: String, Array, or null
159* Default: null
160
161The Certificate Authority signing certificate that is trusted for SSL
162connections to the registry. Values should be in PEM format (Windows calls it
163"Base-64 encoded X.509 (.CER)") with newlines replaced by the string `'\n'`. For
164example:
165
166```
167{
168  ca: '-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----'
169}
170```
171
172Set to `null` to only allow "known" registrars, or to a specific CA cert
173to trust only that specific signing authority.
174
175Multiple CAs can be trusted by specifying an array of certificates instead of a
176single string.
177
178See also [`opts.strict-ssl`](#opts-strict-ssl), [`opts.ca`](#opts-ca) and
179[`opts.key`](#opts-key)
180
181##### <a name="opts-cache"></a> `opts.cache`
182
183* Type: path
184* Default: null
185
186The location of the http cache directory. If provided, certain cachable requests
187will be cached according to [IETF RFC 7234](https://tools.ietf.org/html/rfc7234)
188rules. This will speed up future requests, as well as make the cached data
189available offline if necessary/requested.
190
191See also [`offline`](#opts-offline), [`prefer-offline`](#opts-prefer-offline),
192and [`prefer-online`](#opts-prefer-online).
193
194##### <a name="opts-cert"></a> `opts.cert`
195
196* Type: String
197* Default: null
198
199A client certificate to pass when accessing the registry.  Values should be in
200PEM format (Windows calls it "Base-64 encoded X.509 (.CER)") with newlines
201replaced by the string `'\n'`. For example:
202
203```
204{
205  cert: '-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----'
206}
207```
208
209It is _not_ the path to a certificate file (and there is no "certfile" option).
210
211See also: [`opts.ca`](#opts-ca) and [`opts.key`](#opts-key)
212
213##### <a name="opts-fetch-retries"></a> `opts.fetch-retries`
214
215* Type: Number
216* Default: 2
217
218The "retries" config for [`retry`](https://npm.im/retry) to use when fetching
219packages from the registry.
220
221See also [`opts.retry`](#opts-retry) to provide all retry options as a single
222object.
223
224##### <a name="opts-fetch-retry-factor"></a> `opts.fetch-retry-factor`
225
226* Type: Number
227* Default: 10
228
229The "factor" config for [`retry`](https://npm.im/retry) to use when fetching
230packages.
231
232See also [`opts.retry`](#opts-retry) to provide all retry options as a single
233object.
234
235##### <a name="opts-fetch-retry-mintimeout"></a> `opts.fetch-retry-mintimeout`
236
237* Type: Number
238* Default: 10000 (10 seconds)
239
240The "minTimeout" config for [`retry`](https://npm.im/retry) to use when fetching
241packages.
242
243See also [`opts.retry`](#opts-retry) to provide all retry options as a single
244object.
245
246##### <a name="opts-fetch-retry-maxtimeout"></a> `opts.fetch-retry-maxtimeout`
247
248* Type: Number
249* Default: 60000 (1 minute)
250
251The "maxTimeout" config for [`retry`](https://npm.im/retry) to use when fetching
252packages.
253
254See also [`opts.retry`](#opts-retry) to provide all retry options as a single
255object.
256
257##### <a name="opts-force-auth"></a> `opts.force-auth`
258
259* Alias: `opts.forceAuth`
260* Type: Object
261* Default: null
262
263If present, other auth-related values in `opts` will be completely ignored,
264including `alwaysAuth`, `email`, and `otp`, when calculating auth for a request,
265and the auth details in `opts.forceAuth` will be used instead.
266
267##### <a name="opts-gzip"></a> `opts.gzip`
268
269* Type: Boolean
270* Default: false
271
272If true, `npm-registry-fetch` will set the `Content-Encoding` header to `gzip`
273and use `zlib.gzip()` or `zlib.createGzip()` to gzip-encode
274[`opts.body`](#opts-body).
275
276##### <a name="opts-headers"></a> `opts.headers`
277
278* Type: Object
279* Default: null
280
281Additional headers for the outgoing request. This option can also be used to
282override headers automatically generated by `npm-registry-fetch`, such as
283`Content-Type`.
284
285##### <a name="opts-ignore-body"></a> `opts.ignore-body`
286
287* Alias: `opts.ignoreBody`
288* Type: Boolean
289* Default: false
290
291If true, the **response body** will be thrown away and `res.body` set to `null`.
292This will prevent dangling response sockets for requests where you don't usually
293care what the response body is.
294
295##### <a name="opts-integrity"></a> `opts.integrity`
296
297* Type: String | [SRI object](https://npm.im/ssri)
298* Default: null
299
300If provided, the response body's will be verified against this integrity string,
301using [`ssri`](https://npm.im/ssri). If verification succeeds, the response will
302complete as normal. If verification fails, the response body will error with an
303`EINTEGRITY` error.
304
305Body integrity is only verified if the body is actually consumed to completion --
306that is, if you use `res.json()`/`res.buffer()`, or if you consume the default
307`res` stream data to its end.
308
309Cached data will have its integrity automatically verified using the
310previously-generated integrity hash for the saved request information, so
311`EINTEGRITY` errors can happen if [`opts.cache`](#opts-cache) is used, even if
312`opts.integrity` is not passed in.
313
314##### <a name='opts-is-from-ci'></a> `opts.is-from-ci`
315
316* Alias: `opts.isFromCI`
317* Type: Boolean
318* Default: Based on environment variables
319
320This is used to populate the `npm-in-ci` request header sent to the registry.
321
322##### <a name="opts-key"></a> `opts.key`
323
324* Type: String
325* Default: null
326
327A client key to pass when accessing the registry.  Values should be in PEM
328format with newlines replaced by the string `'\n'`. For example:
329
330```
331{
332  key: '-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----'
333}
334```
335
336It is _not_ the path to a key file (and there is no "keyfile" option).
337
338See also: [`opts.ca`](#opts-ca) and [`opts.cert`](#opts-cert)
339
340##### <a name="opts-local-address"></a> `opts.local-address`
341
342* Type: IP Address String
343* Default: null
344
345The IP address of the local interface to use when making connections
346to the registry.
347
348See also [`opts.proxy`](#opts-proxy)
349
350##### <a name="opts-log"></a> `opts.log`
351
352* Type: [`npmlog`](https://npm.im/npmlog)-like
353* Default: null
354
355Logger object to use for logging operation details. Must have the same methods
356as `npmlog`.
357
358##### <a name="opts-map-json"></a> `opts.map-json`
359
360* Alias: `mapJson`, `mapJSON`
361* Type: Function
362* Default: undefined
363
364When using `fetch.json.stream()` (NOT `fetch.json()`), this will be passed down
365to [`JSONStream`](https://npm.im/JSONStream) as the second argument to
366`JSONStream.parse`, and can be used to transform stream data before output.
367
368##### <a name="opts-maxsockets"></a> `opts.maxsockets`
369
370* Alias: `opts.max-sockets`
371* Type: Integer
372* Default: 12
373
374Maximum number of sockets to keep open during requests. Has no effect if
375[`opts.agent`](#opts-agent) is used.
376
377##### <a name="opts-method"></a> `opts.method`
378
379* Type: String
380* Default: 'GET'
381
382HTTP method to use for the outgoing request. Case-insensitive.
383
384##### <a name="opts-noproxy"></a> `opts.noproxy`
385
386* Type: Boolean
387* Default: process.env.NOPROXY
388
389If true, proxying will be disabled even if [`opts.proxy`](#opts-proxy) is used.
390
391##### <a name="opts-npm-session"></a> `opts.npm-session`
392
393* Alias: `opts.npmSession`
394* Type: String
395* Default: null
396
397If provided, will be sent in the `npm-session` header. This header is used by
398the npm registry to identify individual user sessions (usually individual
399invocations of the CLI).
400
401##### <a name="opts-offline"></a> `opts.offline`
402
403* Type: Boolean
404* Default: false
405
406Force offline mode: no network requests will be done during install. To allow
407`npm-registry-fetch` to fill in missing cache data, see
408[`opts.prefer-offline`](#opts-prefer-offline).
409
410This option is only really useful if you're also using
411[`opts.cache`](#opts-cache).
412
413This option is set to `true` when the request includes `write=true` in the
414query string.
415
416##### <a name="opts-otp"></a> `opts.otp`
417
418* Type: Number | String
419* Default: null
420
421This is a one-time password from a two-factor authenticator. It is required for
422certain registry interactions when two-factor auth is enabled for a user
423account.
424
425##### <a name="opts-password"></a> `opts.password`
426
427* Alias: `_password`
428* Type: String
429* Default: null
430
431Password used for basic authentication. For the more modern authentication
432method, please use the (more secure) [`opts.token`](#opts-token)
433
434Can optionally be scoped to a registry by using a "nerf dart" for that registry.
435That is:
436
437```
438{
439  '//registry.npmjs.org/:password': 't0k3nH34r'
440}
441```
442
443See also [`opts.username`](#opts-username)
444
445##### <a name="opts-prefer-offline"></a> `opts.prefer-offline`
446
447* Type: Boolean
448* Default: false
449
450If true, staleness checks for cached data will be bypassed, but missing data
451will be requested from the server. To force full offline mode, use
452[`opts.offline`](#opts-offline).
453
454This option is generally only useful if you're also using
455[`opts.cache`](#opts-cache).
456
457This option is set to `false` when the request includes `write=true` in the
458query string.
459
460##### <a name="opts-prefer-online"></a> `opts.prefer-online`
461
462* Type: Boolean
463* Default: false
464
465If true, staleness checks for cached data will be forced, making the CLI look
466for updates immediately even for fresh package data.
467
468This option is generally only useful if you're also using
469[`opts.cache`](#opts-cache).
470
471This option is set to `true` when the request includes `write=true` in the
472query string.
473
474##### <a name="opts-project-scope"></a> `opts.project-scope`
475
476* Alias: `opts.projectScope`
477* Type: String
478* Default: null
479
480If provided, will be sent in the `npm-scope` header. This header is used by the
481npm registry to identify the toplevel package scope that a particular project
482installation is using.
483
484##### <a name="opts-proxy"></a> `opts.proxy`
485
486* Type: url
487* Default: null
488
489A proxy to use for outgoing http requests. If not passed in, the `HTTP(S)_PROXY`
490environment variable will be used.
491
492##### <a name="opts-query"></a> `opts.query`
493
494* Type: String | Object
495* Default: null
496
497If provided, the request URI will have a query string appended to it using this
498query. If `opts.query` is an object, it will be converted to a query string
499using
500[`querystring.stringify()`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options).
501
502If the request URI already has a query string, it will be merged with
503`opts.query`, preferring `opts.query` values.
504
505##### <a name="opts-refer"></a> `opts.refer`
506
507* Alias: `opts.referer`
508* Type: String
509* Default: null
510
511Value to use for the `Referer` header. The npm CLI itself uses this to serialize
512the npm command line using the given request.
513
514##### <a name="opts-registry"></a> `opts.registry`
515
516* Type: URL
517* Default: `'https://registry.npmjs.org'`
518
519Registry configuration for a request. If a request URL only includes the URL
520path, this registry setting will be prepended. This configuration is also used
521to determine authentication details, so even if the request URL references a
522completely different host, `opts.registry` will be used to find the auth details
523for that request.
524
525See also [`opts.scope`](#opts-scope), [`opts.spec`](#opts-spec), and
526[`opts.<scope>:registry`](#opts-scope-registry) which can all affect the actual
527registry URL used by the outgoing request.
528
529##### <a name="opts-retry"></a> `opts.retry`
530
531* Type: Object
532* Default: null
533
534Single-object configuration for request retry settings. If passed in, will
535override individually-passed `fetch-retry-*` settings.
536
537##### <a name="opts-scope"></a> `opts.scope`
538
539* Type: String
540* Default: null
541
542Associate an operation with a scope for a scoped registry. This option can force
543lookup of scope-specific registries and authentication.
544
545See also [`opts.<scope>:registry`](#opts-scope-registry) and
546[`opts.spec`](#opts-spec) for interactions with this option.
547
548##### <a name="opts-scope-registry"></a> `opts.<scope>:registry`
549
550* Type: String
551* Default: null
552
553This option type can be used to configure the registry used for requests
554involving a particular scope. For example, `opts['@myscope:registry'] =
555'https://scope-specific.registry/'` will make it so requests go out to this
556registry instead of [`opts.registry`](#opts-registry) when
557[`opts.scope`](#opts-scope) is used, or when [`opts.spec`](#opts-spec) is a
558scoped package spec.
559
560The `@` before the scope name is optional, but recommended.
561
562##### <a name="opts-spec"></a> `opts.spec`
563
564* Type: String | [`npm-registry-arg`](https://npm.im/npm-registry-arg) object.
565* Default: null
566
567If provided, can be used to automatically configure [`opts.scope`](#opts-scope)
568based on a specific package name. Non-registry package specs will throw an
569error.
570
571##### <a name="opts-strict-ssl"></a> `opts.strict-ssl`
572
573* Type: Boolean
574* Default: true
575
576Whether or not to do SSL key validation when making requests to the
577registry via https.
578
579See also [`opts.ca`](#opts-ca).
580
581##### <a name="opts-timeout"></a> `opts.timeout`
582
583* Type: Milliseconds
584* Default: 0 (no timeout)
585
586Time before a hanging request times out.
587
588##### <a name="opts-token"></a> `opts.token`
589
590* Alias: `opts._authToken`
591* Type: String
592* Default: null
593
594Authentication token string.
595
596Can be scoped to a registry by using a "nerf dart" for that registry. That is:
597
598```
599{
600  '//registry.npmjs.org/:token': 't0k3nH34r'
601}
602```
603
604##### <a name="opts-user-agent"></a> `opts.user-agent`
605
606* Type: String
607* Default: `'npm-registry-fetch@<version>/node@<node-version>+<arch> (<platform>)'`
608
609User agent string to send in the `User-Agent` header.
610
611##### <a name="opts-username"></a> `opts.username`
612
613* Type: String
614* Default: null
615
616Username used for basic authentication. For the more modern authentication
617method, please use the (more secure) [`opts.token`](#opts-token)
618
619Can optionally be scoped to a registry by using a "nerf dart" for that registry.
620That is:
621
622```
623{
624  '//registry.npmjs.org/:username': 't0k3nH34r'
625}
626```
627
628See also [`opts.password`](#opts-password)
629
630##### <a name="opts-auth"></a> `opts._auth`
631
632* Type: String
633* Default: null
634
635** DEPRECATED ** This is a legacy authentication token supported only for
636compatibility. Please use [`opts.token`](#opts-token) instead.
637