@jayf0x/npm-exists 2.0.6 → 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 CHANGED
@@ -1,13 +1,15 @@
1
1
  # npm-exists
2
2
 
3
- > Check if an npm package name is taken.
3
+ ```
4
+ ⚡ HEAD → 200? true. 404? false. Done.
5
+ ```
4
6
 
5
7
  [![npm version](https://img.shields.io/npm/v/@jayf0x/npm-exists)](https://www.npmjs.com/package/@jayf0x/npm-exists)
6
8
  [![npm downloads](https://img.shields.io/npm/dm/@jayf0x/npm-exists)](https://www.npmjs.com/package/@jayf0x/npm-exists)
7
9
  [![bundle size](https://img.shields.io/bundlephobia/minzip/@jayf0x/npm-exists)](https://bundlephobia.com/package/@jayf0x/npm-exists)
8
10
  [![license](https://img.shields.io/npm/l/@jayf0x/npm-exists)](./LICENSE)
9
11
 
10
- Zero dependencies. Returns the npm package URL or `false`.
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
- const url = await npmExists('react')
24
- // 'https://www.npmjs.com/package/react' | false
25
+ await npmExists('react') // true
26
+ await npmExists('not-a-pkg') // false
25
27
  ```
26
28
 
27
29
  ```js
28
30
  // Custom registry
29
- const url = await npmExists('my-pkg', 'https://my.private.registry.io')
30
- ```
31
+ await npmExists('my-pkg', { registry: 'https://my.private.registry.io' })
31
32
 
32
- ```js
33
- // Suppress errors (network failures return false instead of throwing)
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
- // Build your own fetch
38
+ // Get the registry URL (useful with axios, ky, etc.)
39
39
  import { getNpmUrl } from '@jayf0x/npm-exists'
40
- const url = getNpmUrl('react') // https://registry.npmjs.org/react
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@19.1.0 exists on npm
48
+ # ✓ react exists on npm
48
49
 
49
- npm-exists my-pkg https://my.private.registry.io
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, registryOrOptions?, options?)`
58
+ ### `npmExists(pkg, options?)`
57
59
 
58
- | Param | Type | Description |
59
- |---|---|---|
60
- | `pkg` | `string` | Package name |
61
- | `registryOrOptions` | `string \| { registry?, silent? }` | Registry URL or options object |
62
- | `options` | `{ silent? }` | `silent: true` returns `false` instead of throwing on error |
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<string \| false>`
66
+ Returns `Promise<boolean>`
65
67
 
66
68
  ### `getNpmUrl(pkg, registry?)`
67
69
 
68
- Returns the registry API URL. Useful with custom HTTP clients like axios or ky.
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/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
- return `${registry.replace(/\/$/, "")}/${encodeURIComponent(pkg)}`;
5
+ const base = registry.endsWith("/") ? registry : `${registry}/`;
6
+ return new URL(encodeURIComponent(pkg), base).toString();
7
7
  }
8
- function getPageUrl(pkg, registry) {
9
- const base = !registry || registry === DEFAULT_REGISTRY ? NPM_PAGE_BASE : `${registry.replace(/\/$/, "")}/package`;
10
- return `${base}/${pkg}`;
11
- }
12
- async function npmExists(pkg, registryOrOptions, options) {
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 getPageUrl(pkg, registry);
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
- return `${registry.replace(/\/$/, "")}/${encodeURIComponent(pkg)}`;
3
+ const base = registry.endsWith("/") ? registry : `${registry}/`;
4
+ return new URL(encodeURIComponent(pkg), base).toString();
5
5
  }
6
- function getPageUrl(pkg, registry) {
7
- const base = !registry || registry === DEFAULT_REGISTRY ? NPM_PAGE_BASE : `${registry.replace(/\/$/, "")}/package`;
8
- return `${base}/${pkg}`;
9
- }
10
- async function npmExists(pkg, registryOrOptions, options) {
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 getPageUrl(pkg, registry);
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.0.6",
4
- "description": "Check if an npm package name is taken. Zero dependencies, uses native fetch.",
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": "git+https://github.com/jayF0x/npm-exists.git"
55
+ "url": "https://github.com/jayf0x/npm-exists"
56
56
  },
57
- "homepage": "https://github.com/jayF0x/npm-exists#readme",
57
+ "homepage": "https://github.com/jayf0x/npm-exists#readme",
58
58
  "bugs": {
59
- "url": "https://github.com/jayF0x/npm-exists/issues"
59
+ "url": "https://github.com/jayf0x/npm-exists/issues"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">=18"