@jayf0x/npm-exists 2.0.5 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -21
- package/bin/cli.js +19 -0
- package/bin/npm-exists.js +0 -0
- package/dist/index.cjs +8 -17
- package/dist/index.js +8 -17
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
# npm-exists
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```
|
|
4
|
+
⚡ HEAD → 200? true. 404? false. Done.
|
|
5
|
+
```
|
|
4
6
|
|
|
5
7
|
[](https://www.npmjs.com/package/@jayf0x/npm-exists)
|
|
6
8
|
[](https://www.npmjs.com/package/@jayf0x/npm-exists)
|
|
7
9
|
[](https://bundlephobia.com/package/@jayf0x/npm-exists)
|
|
8
10
|
[](./LICENSE)
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
The simplest, fastest possible npm package existence check. One HEAD request. No body. No regex. No deps.
|
|
11
13
|
|
|
12
14
|
## Install
|
|
13
15
|
|
|
@@ -20,52 +22,52 @@ npm install @jayf0x/npm-exists
|
|
|
20
22
|
```js
|
|
21
23
|
import npmExists from '@jayf0x/npm-exists'
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
await npmExists('react') // true
|
|
26
|
+
await npmExists('not-a-pkg') // false
|
|
25
27
|
```
|
|
26
28
|
|
|
27
29
|
```js
|
|
28
30
|
// Custom registry
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
+
await npmExists('my-pkg', { registry: 'https://my.private.registry.io' })
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const url = await npmExists('react', { silent: true })
|
|
33
|
+
// Suppress network errors
|
|
34
|
+
await npmExists('react', { silent: true })
|
|
35
35
|
```
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
|
-
//
|
|
38
|
+
// Get the registry URL (useful with axios, ky, etc.)
|
|
39
39
|
import { getNpmUrl } from '@jayf0x/npm-exists'
|
|
40
|
-
|
|
40
|
+
getNpmUrl('react') // 'https://registry.npmjs.org/react'
|
|
41
|
+
getNpmUrl('@types/node') // 'https://registry.npmjs.org/%40types%2Fnode'
|
|
41
42
|
```
|
|
42
43
|
|
|
43
44
|
## CLI
|
|
44
45
|
|
|
45
46
|
```sh
|
|
46
47
|
npx @jayf0x/npm-exists react
|
|
47
|
-
# ✓ react
|
|
48
|
+
# ✓ react exists on npm
|
|
48
49
|
|
|
49
|
-
npm-exists my-pkg
|
|
50
|
+
npm-exists my-pkg
|
|
51
|
+
# ✗ my-pkg is not registered on npm
|
|
50
52
|
```
|
|
51
53
|
|
|
52
54
|
Exit codes: `0` exists · `1` not found · `2` bad usage
|
|
53
55
|
|
|
54
56
|
## API
|
|
55
57
|
|
|
56
|
-
### `npmExists(pkg,
|
|
58
|
+
### `npmExists(pkg, options?)`
|
|
57
59
|
|
|
58
|
-
| Param | Type | Description |
|
|
59
|
-
|
|
60
|
-
| `pkg` | `string` | Package name |
|
|
61
|
-
| `
|
|
62
|
-
| `options` | `
|
|
60
|
+
| Param | Type | Default | Description |
|
|
61
|
+
|---|---|---|---|
|
|
62
|
+
| `pkg` | `string` | — | Package name |
|
|
63
|
+
| `options.registry` | `string` | `'https://registry.npmjs.org'` | Custom registry URL |
|
|
64
|
+
| `options.silent` | `boolean` | `false` | Return `false` on errors instead of throwing |
|
|
63
65
|
|
|
64
|
-
Returns `Promise<
|
|
66
|
+
Returns `Promise<boolean>`
|
|
65
67
|
|
|
66
68
|
### `getNpmUrl(pkg, registry?)`
|
|
67
69
|
|
|
68
|
-
Returns the registry API URL.
|
|
70
|
+
Returns the registry API URL. Use this directly with your own HTTP client.
|
|
69
71
|
|
|
70
72
|
## License
|
|
71
73
|
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { npmExists } from '../dist/index.js'
|
|
3
|
+
|
|
4
|
+
const [pkg, registry] = process.argv.slice(2)
|
|
5
|
+
|
|
6
|
+
if (!pkg) {
|
|
7
|
+
console.error('Usage: npm-exists <package-name> [registry-url]')
|
|
8
|
+
process.exit(2)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const exists = await npmExists(pkg, registry ? { registry } : {})
|
|
12
|
+
|
|
13
|
+
if (exists) {
|
|
14
|
+
console.log(`✓ ${pkg} exists on npm`)
|
|
15
|
+
process.exit(0)
|
|
16
|
+
} else {
|
|
17
|
+
console.log(`✗ ${pkg} is not registered on npm`)
|
|
18
|
+
process.exit(1)
|
|
19
|
+
}
|
package/bin/npm-exists.js
CHANGED
|
File without changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,29 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
3
|
const DEFAULT_REGISTRY = "https://registry.npmjs.org";
|
|
4
|
-
const NPM_PAGE_BASE = "https://www.npmjs.com/package";
|
|
5
4
|
function getNpmUrl(pkg, registry = DEFAULT_REGISTRY) {
|
|
6
|
-
|
|
5
|
+
const base = registry.endsWith("/") ? registry : `${registry}/`;
|
|
6
|
+
return new URL(encodeURIComponent(pkg), base).toString();
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let registry = DEFAULT_REGISTRY;
|
|
14
|
-
let silent = false;
|
|
15
|
-
if (typeof registryOrOptions === "string") {
|
|
16
|
-
registry = registryOrOptions;
|
|
17
|
-
silent = options?.silent ?? false;
|
|
18
|
-
} else if (registryOrOptions != null) {
|
|
19
|
-
registry = registryOrOptions.registry ?? DEFAULT_REGISTRY;
|
|
20
|
-
silent = registryOrOptions.silent ?? false;
|
|
21
|
-
}
|
|
8
|
+
async function npmExists(pkg, options = {}) {
|
|
9
|
+
const {
|
|
10
|
+
registry = DEFAULT_REGISTRY,
|
|
11
|
+
silent = false
|
|
12
|
+
} = options;
|
|
22
13
|
try {
|
|
23
14
|
const res = await fetch(getNpmUrl(pkg, registry), { method: "HEAD" });
|
|
24
15
|
if (res.status === 404) return false;
|
|
25
16
|
if (!res.ok) throw new Error(`npm registry error: HTTP ${res.status}`);
|
|
26
|
-
return
|
|
17
|
+
return true;
|
|
27
18
|
} catch (err) {
|
|
28
19
|
if (silent) return false;
|
|
29
20
|
throw err;
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
const DEFAULT_REGISTRY = "https://registry.npmjs.org";
|
|
2
|
-
const NPM_PAGE_BASE = "https://www.npmjs.com/package";
|
|
3
2
|
function getNpmUrl(pkg, registry = DEFAULT_REGISTRY) {
|
|
4
|
-
|
|
3
|
+
const base = registry.endsWith("/") ? registry : `${registry}/`;
|
|
4
|
+
return new URL(encodeURIComponent(pkg), base).toString();
|
|
5
5
|
}
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
let registry = DEFAULT_REGISTRY;
|
|
12
|
-
let silent = false;
|
|
13
|
-
if (typeof registryOrOptions === "string") {
|
|
14
|
-
registry = registryOrOptions;
|
|
15
|
-
silent = options?.silent ?? false;
|
|
16
|
-
} else if (registryOrOptions != null) {
|
|
17
|
-
registry = registryOrOptions.registry ?? DEFAULT_REGISTRY;
|
|
18
|
-
silent = registryOrOptions.silent ?? false;
|
|
19
|
-
}
|
|
6
|
+
async function npmExists(pkg, options = {}) {
|
|
7
|
+
const {
|
|
8
|
+
registry = DEFAULT_REGISTRY,
|
|
9
|
+
silent = false
|
|
10
|
+
} = options;
|
|
20
11
|
try {
|
|
21
12
|
const res = await fetch(getNpmUrl(pkg, registry), { method: "HEAD" });
|
|
22
13
|
if (res.status === 404) return false;
|
|
23
14
|
if (!res.ok) throw new Error(`npm registry error: HTTP ${res.status}`);
|
|
24
|
-
return
|
|
15
|
+
return true;
|
|
25
16
|
} catch (err) {
|
|
26
17
|
if (silent) return false;
|
|
27
18
|
throw err;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jayf0x/npm-exists",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"description": "The fastest way to check if an npm package exists. Zero dependencies, lightweight, and uses ultra-fast HTTP HEAD requests.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"test": "vitest run",
|
|
28
28
|
"test:watch": "vitest",
|
|
29
29
|
"release": "bash publish-npm.sh",
|
|
30
|
-
"prepublishOnly": "bun run build && bun test"
|
|
30
|
+
"prepublishOnly": "bun run build && bun run test"
|
|
31
31
|
},
|
|
32
32
|
"keywords": [
|
|
33
33
|
"npm",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"repository": {
|
|
54
54
|
"type": "git",
|
|
55
|
-
"url": "
|
|
55
|
+
"url": "https://github.com/jayf0x/npm-exists"
|
|
56
56
|
},
|
|
57
|
-
"homepage": "https://github.com/
|
|
57
|
+
"homepage": "https://github.com/jayf0x/npm-exists#readme",
|
|
58
58
|
"bugs": {
|
|
59
|
-
"url": "https://github.com/
|
|
59
|
+
"url": "https://github.com/jayf0x/npm-exists/issues"
|
|
60
60
|
},
|
|
61
61
|
"engines": {
|
|
62
62
|
"node": ">=18"
|