Detalhes do pacote

hibp

wKovacs6445.1kMIT15.1.0

An unofficial TypeScript SDK for the 'Have I been pwned?' service.

haveibeenpwned, hibp, pwned, security

readme (leia-me)

logo

hibp

An unofficial TypeScript SDK for Troy Hunt's Have I been pwned? service.

npm Version ![Build Status][ci-image] Code Coverage All Contributors

Installation

In Node.js:

npm install hibp

In Deno:

// Replace x.y.z with the desired hibp version
import * as hibp from 'npm:hibp@x.y.z';

See the browser section below for information on how to use it in the browser.

Features (🔑 = requires an API key)

  • Get the most recently added breach
  • Get a single breach event
  • Get all breaches for an account 🔑
  • Get all breached email addresses for a domain 🔑
  • Get all breach events in the system
  • Get all data classes
  • Get all pastes for an account 🔑
  • Securely check a password to see if it has been exposed in a data breach
  • Check a SHA-1 or NTLM prefix to see if it has been exposed in a data breach
  • Search for an account in both breaches and pastes at the same time 🔑
  • Get all stealer log domains for an email address 🔑
  • Get all stealer log email aliases for an email domain 🔑
  • Get all stealer log email addresses for a website domain 🔑
  • Get all subscribed domains 🔑
  • Get your subscription status 🔑
  • All queries return a Promise
  • Available server-side (e.g., Node.js) and client-side (browser)
  • Written in TypeScript, so all modules come fully typed

Usage

// import individual modules as needed
import { dataClasses, search } from 'hibp';

// or, import all modules into a local namespace
import * as hibp from 'hibp';

The following modules are available:

Please see the API reference for more detailed usage information and examples.

Quick-Start Example

import { search } from 'hibp';

async function main() {
  try {
    const data = await search('someAccountOrEmail', { apiKey: 'my-api-key' });
    if (data.breaches || data.pastes) {
      // Bummer...
      console.log(data);
    } else {
      // Phew! We're clear.
      console.log('Good news — no pwnage found!');
    }
  } catch (err) {
    // Something went wrong.
    console.log(err.message);
  }
}

void main();

Rate Limiting

The haveibeenpwned.com API rate limits requests to prevent abuse. In the event you get rate limited, the module will throw a custom RateLimitError which will include a retryAfterSeconds property so you know when you can try the call again (as a number, unless the remote API did not provide one, in which case it will be undefined - but that should never happen).

Using in the browser

You have a couple of options for using this library in a browser environment:

  1. Bundled

    The most efficient and recommended method is to bundle it with client-side code using a module bundler, most likely dictated by your web application framework of choice.

  2. ESM for Browsers

    Alternatively, you can also import the library directly in your HTML via <script type="module"> tags in modern browsers. The pre-bundled module is available through the unpkg CDN, but you must specify the full path (including the file extension). It's also strongly recommended to include the exact version number as well, otherwise the latest tag will be used, which could be dangerous if/when there are breaking changes made to the API. See unpkg for details and advanced version specification, but you will probably want to do the following (replacing x.y.z with the version you want):

    <script type="module">
      // Replace x.y.z with the desired hibp version      ↓ ↓ ↓
      import { dataClasses } from 'https://unpkg.com/hibp@x.y.z/dist/browser/hibp.module.js';
    
      const logDataClasses = async () => {
        console.table(await dataClasses());
      };
    
      logDataClasses();
    </script>

    For more information on ESM in the browser, check out Using JS modules in the browser.

Try It Out

Test hibp in your browser with StackBlitz.

Projects Using hibp

Send me a PR or an email and I'll add yours to the list!

License

This module is distributed under the MIT License.

[ci-image]: https://img.shields.io/github/actions/workflow/status/wKovacs64/hibp/ci.yml?logo=github&style=flat-square

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Justin Hall
Justin Hall

💻 📖 🚇 🚧 👀 ⚠️
Troy Hunt
Troy Hunt

🔣
Jelle Kralt
Jelle Kralt

💻
Anton W
Anton W

🐛
Daniel Adams
Daniel Adams

💻
Markus Dolic
Markus Dolic

🐛
Jonathan Sharpe
Jonathan Sharpe

💻
Ryan
Ryan

🐛
Stuart McGregor
Stuart McGregor

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog (log de mudanças)

Change Log

15.1.0

Minor Changes

Patch Changes

  • #535 d47d462 Thanks @wKovacs64! - Add IncludesStealerLogs field to SubscriptionStatus interface and docs.

15.0.1

Patch Changes

15.0.0

Major Changes

  • #509 e8d4986 Thanks @wKovacs64! - Drop support for Node 18 and remove the CommonJS and UMD builds:

    • Drop support for Node.js 18 as it is end-of-life, making the new minimum Node.js runtime v20.19.0. Please upgrade your Node.js environment if necessary, or continue using a previous release if you are unable to upgrade your environment.

      • This also allowed us to drop the fetch polyfill that was only necessary in Node 18, which reduced the bundle size by approximately 33%! 📉 The library now officially has zero dependencies. 🎉
    • Remove the CommonJS build since you can now require() ESM as of Node v20.19.0. Consumers in a CommonJS environment should still be able to use the library as before (given the appropriate Node.js version).

    • Remove the UMD build as all modern browsers support importing ESM via <script type="module"> tags. See the "Using in the browser" section of the README for more details.

Minor Changes

  • #506 56fdf38 Thanks @wKovacs64! - Drop JSSHA dependency in favor of a native Web Crypto API SHA-1 hashing implementation. This change reduces the size of the library by approximately 30%! 📉

14.1.3

Patch Changes

14.1.2

Patch Changes

14.1.1

Patch Changes

14.1.0

Minor Changes

  • #462 b6076f2 Thanks @wKovacs64! - Add the timeoutMs option to all modules, allowing the consumer to specify a timeout for the underlying network request (in milliseconds). Requests that take longer than the sppecified timeout period will throw/reject. There is no default timeout, as fetch itself has no timeout by default and providing one would be arbitrary, unexpected, and a breaking change.

  • #458 0a82b8d Thanks @wKovacs64! - Only polyfill global fetch on Node.js v18, and use undici instead of @remix-run/web-fetch. This also enables use of hibp in web workers and extension background threads.

Patch Changes

  • #461 aa90167 Thanks @wKovacs64! - Fix a bug in pwnedPassword and pwnedPasswordRange modules where the addPadding and userAgent options could not be used simultaneously.

14.0.3

Patch Changes

  • #438 3da8b89 Thanks @wKovacs64! - Restore missing TypeScript declarations that were forgotten in v14.0.2 (PR #436).

14.0.2

Patch Changes

  • #436 961d6e0 Thanks @wKovacs64! - Fix CommonJS exports that broke in v14.0.0.

    CommonJS consumers were getting an ERR_REQUIRE_ESM error as of v14.0.0 due to changing the project source to ESM in PR #420. This change resolves that by publishing the CommonJS files in dist/cjs with a .cjs file extension and the ESM files in dist/esm with the .js file extension.

14.0.1

Patch Changes

14.0.0

Major Changes

  • #410 2643a0c Thanks @wKovacs64! - Drop support for Node.js 16 as it is end-of-life, making the new minimum Node.js runtime v18.0.0. Please upgrade your Node.js environment if necessary, or continue using a previous release if you are unable to upgrade your environment.

Minor Changes

Patch Changes

  • #427 173a615 Thanks @wKovacs64! - Fix the subscriptionStatus implementation from PR #425 so that it supports a proxy that inserts the HIBP-API-Key header (via the baseUrl option) in the case where the consumer doesn't have direct access to the API key.

  • #424 a512452 Thanks @wKovacs64! - Properly merge consumer-provided options with internal defaults.

  • #419 799669b Thanks @wKovacs64! - Update the Breach model to include the IsMalware and IsSubscriptionFree fields.

13.0.0

Major Changes

  • #383 b837a57 Thanks @wKovacs64! - Drop support for Node.js 14 as it is end-of-life, making the new minimum Node.js runtime v16.0.0. Please upgrade your Node.js environment if necessary, or continue using a previous release if you are unable to upgrade your environment.

  • #381 6711b59 Thanks @wKovacs64! - Drop support for browsers without native Promise implementations. In reality, this likely didn't change anything.

12.0.1

Patch Changes

12.0.0

Major Changes

  • #368 91909c1 Thanks @wKovacs64! - Drop support for Node.js 12 as it is end-of-life, making the new minimum Node.js runtime v14.13.1. Please upgrade your Node.js environment if necessary, or continue using a previous release if you are unable to upgrade your environment.

Patch Changes

  • #367 95d6217 Thanks @wKovacs64! - Replace the underlying fetch library. This shouldn't be noticeable to consumers, but replacing isomorphic-unfetch with our own conditional wrapper around @remix-run/web-fetch resolves a few compatibility issues the project has been facing.

11.1.1

Patch Changes