npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

hot-resource

v1.2.1

Published

[![npm version](https://img.shields.io/npm/v/hot-resource.svg)](https://www.npmjs.com/package/hot-resource) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Downloads

10,689

Readme

hot-resource

npm version License: MIT

A lightweight TypeScript utility for managing resource lifecycle in Node.js and Bun applications. Hot Resource:

  • 🔄 Creates resources on demand and cleans up automatically
  • 🌟 Works with top-level await in modern JS/TS environments
  • 🔥 Integrates seamlessly with Bun's hot module reload (bun --hot)
  • 🧹 Handles application termination signals gracefully (SIGTERM, SIGINT)
  • 🪶 Zero dependencies, TypeScript-first design

Installation

# Using npm
npm install hot-resource

# Using yarn
yarn add hot-resource

# Using bun
bun add hot-resource

Usage

Basic Example with Top-Level Await

import hotResource from "hot-resource";

// Database connection with cleanup function
export const db = await hotResource(async () => [
  await connectToDatabase(),
  async (connection) => {
    await connection.close();
  },
]);

// In another module, import your db directly at module scope, hot-resource will handle release
await db.query("SELECT * FROM users");

process.emit('SIGTERM') // notify close db

With Disposable/AsyncDisposable

import hotResource from "hot-resource";

class MyResource {
  async [Symbol.asyncDispose]() {
    // Cleanup is automatic
  }
}

const resource = await hotResource(() => new MyResource());

Use with Bun's Hot Reloading

When running with bun --hot, hot-resource manages resources across hot reloads:

import hotResource from "hot-resource";
import { createServer } from "http";

// Server managed across hot reloads
const server = await hotResource(() => {
  const server = createServer((req, res) => {
    res.end("Hello from hot-reloaded server!");
  });
  
  server.listen(3000);
  
  return [
    server,
    async (srv) => {
      await new Promise(resolve => srv.close(resolve));
    }
  ];
});

API

hotResource<T>(create)

Creates or returns a cached resource and sets up automatic cleanup.

Parameters

  • create: Function that creates and returns one of:
    • A resource with Disposable/AsyncDisposable interface
    • A tuple of [resource, cleanupFunction]

Returns

  • Promise<T> - The created or cached resource

How It Works

  1. The resource is created once and cached in the global scope
  2. On Bun hot reload, existing resources are properly cleaned up
  3. On process termination (SIGTERM/SIGINT), all resources are released
  4. Resources with the same function signature are reused

Example Use Cases

  • Database connections that auto-close on server shutdown
  • HTTP/WebSocket servers that restart cleanly during development
  • File watchers that properly release resources
  • Any resource that needs lifecycle management

License

MIT © snomiao