Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
- name: Test
run: npm test -- --coverage --coverageReporters=text --coverageReporters=json-summary

- name: Check doc code samples
run: npm run docs:check

# Writes coverage.json (a shields.io endpoint) to the `badges` branch, which the
# README coverage badge reads. Runs on main only, so pull requests never write.
- name: Publish coverage badge
Expand Down
102 changes: 102 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Changelog

All notable changes to RunCache are recorded here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses [Semantic Versioning](https://semver.org/).

Add an entry under **Unreleased** in the same pull request as the change. When a version is tagged, rename that section to the version and date, add a new empty `## [Unreleased]` section above it, and update the links at the bottom: point `[Unreleased]` at `compare/vX.Y.Z...HEAD` and add a `[X.Y.Z]` link comparing the previous tag with the new one.

## [Unreleased]

### Added
- Values of any JSON-serializable type can be cached; strings are stored as-is and other values are serialized on `set()` and parsed again by `get()`. `RunCache.createTypedCache<T>()` returns a typed wrapper ([#39](https://github.com/helloscoopa/run-cache/pull/39), [#67](https://github.com/helloscoopa/run-cache/pull/67)).
- `RunCacheError`, `SourceFnError` and `MiddlewareError` are exported; they keep the original error as `cause`, and `refetch_failure` events carry it as `error` ([#80](https://github.com/helloscoopa/run-cache/pull/80)).
- Storage adapters honour `autoSaveInterval` and `autoLoadOnInit` when attached ([#79](https://github.com/helloscoopa/run-cache/pull/79)).
- `handleProcessSignals` option to shut down (and save) on `SIGINT`/`SIGTERM` or page unload; off by default ([#70](https://github.com/helloscoopa/run-cache/pull/70)).
- `onListenerError` option; event listener errors no longer affect cache operations ([#76](https://github.com/helloscoopa/run-cache/pull/76)).
- Versioned GitBook docs with a version switcher ([#81](https://github.com/helloscoopa/run-cache/pull/81)).
- `CONTRIBUTING.md`, this changelog, and `npm run docs:check`, which checks the code samples in the docs against the exported API in CI ([#56](https://github.com/helloscoopa/run-cache/issues/56)).

### Changed
- Dependents are invalidated automatically when the entry they depend on is updated, refetched, deleted, evicted, invalidated by tag or expires; circular dependencies are rejected ([#78](https://github.com/helloscoopa/run-cache/pull/78)).
- Loading from storage merges into the cache, re-arms TTL timers for the time remaining, and no longer writes source function code or applies the stored configuration ([#79](https://github.com/helloscoopa/run-cache/pull/79)).
- LRU eviction orders by recency and counts writes as a use ([#77](https://github.com/helloscoopa/run-cache/pull/77)).
- Empty strings are valid values ([#80](https://github.com/helloscoopa/run-cache/pull/80)).
- `CacheConfig.defaultTTL` is marked deprecated: it was never applied.

### Fixed
- Storage adapters passed to `configure()` are attached ([#66](https://github.com/helloscoopa/run-cache/pull/66)).
- TTLs longer than the `setTimeout` limit work ([#68](https://github.com/helloscoopa/run-cache/pull/68)).
- Entries changed while a refetch is in flight are not resurrected ([#69](https://github.com/helloscoopa/run-cache/pull/69)).
- The package bundles and runs in browsers ([#71](https://github.com/helloscoopa/run-cache/pull/71)).
- Expired entries are removed when their TTL fires, and timers do not keep Node.js alive ([#73](https://github.com/helloscoopa/run-cache/pull/73)).
- A failed `set()` leaves the existing entry and its timer untouched ([#74](https://github.com/helloscoopa/run-cache/pull/74)).
- `clearEventListeners()` clears only the requested event's listeners ([#75](https://github.com/helloscoopa/run-cache/pull/75)).
- Documentation: removed APIs that do not exist (runtime validators, serialization adapters, `validator`/`metadata` options, `getInternalCache()`), awaited every async call in samples, stopped stringifying values by hand, and removed the duplicated GitBook/Mintlify tooling files ([#56](https://github.com/helloscoopa/run-cache/issues/56)).

## [1.6.1] - 2025-05-26

### Fixed
- Build the minified bundle (`dist/index.min.js`).

## [1.6.0] - 2025-05-13

### Added
- Middleware support with `use()` and `clearMiddleware()` ([#35](https://github.com/helloscoopa/run-cache/pull/35)).
- Tag-based invalidation ([#36](https://github.com/helloscoopa/run-cache/pull/36)).
- Storage adapters for localStorage, IndexedDB and the filesystem ([#37](https://github.com/helloscoopa/run-cache/pull/37)).

### Changed
- Internal refactor for modularity ([#27](https://github.com/helloscoopa/run-cache/pull/27)); linting enabled ([#38](https://github.com/helloscoopa/run-cache/pull/38)).

## [1.5.0] - 2025-05-12

### Added
- Wildcard key patterns ([#23](https://github.com/helloscoopa/run-cache/pull/23)).
- Configurable eviction policies ([#24](https://github.com/helloscoopa/run-cache/pull/24)).
- Graceful shutdown ([#25](https://github.com/helloscoopa/run-cache/pull/25)).
- Logging ([#26](https://github.com/helloscoopa/run-cache/pull/26)).

## [1.4.1] - 2024-09-29

### Fixed
- Use `setInterval` instead of `setTimeout` for auto-refetch ([#18](https://github.com/helloscoopa/run-cache/pull/18)).

## [1.4.0] - 2024-09-19

### Added
- Event listeners `onExpiry`, `onKeyExpiry`, `onRefetch`, `onKeyRefetch` and `clearEventListeners()`, built on an event emitter ([#16](https://github.com/helloscoopa/run-cache/pull/16)).

## [1.3.2] - 2024-09-15

### Added
- Events/triggers ([#9](https://github.com/helloscoopa/run-cache/pull/9)).

### Fixed
- No concurrent `sourceFn` calls between refetches ([#11](https://github.com/helloscoopa/run-cache/pull/11)).

## [1.2.2] - 2024-09-13

### Fixed
- Ship `.d.ts` type declarations ([#5](https://github.com/helloscoopa/run-cache/pull/5)).

## [1.2.0] - 2024-09-13

### Added
- Auto-refetch on expiry ([#4](https://github.com/helloscoopa/run-cache/pull/4)).

### Changed
- `set()` and `setWithSourceFn()` merged into `set()` ([#2](https://github.com/helloscoopa/run-cache/pull/2)).

## [1.0.5] - 2024-09-09

- Initial release.

[Unreleased]: https://github.com/helloscoopa/run-cache/compare/v1.6.1...HEAD
[1.6.1]: https://github.com/helloscoopa/run-cache/compare/v1.6.0...v1.6.1
[1.6.0]: https://github.com/helloscoopa/run-cache/compare/v1.5.0...v1.6.0
[1.5.0]: https://github.com/helloscoopa/run-cache/compare/v1.4.1...v1.5.0
[1.4.1]: https://github.com/helloscoopa/run-cache/compare/v1.4.0...v1.4.1
[1.4.0]: https://github.com/helloscoopa/run-cache/compare/v1.3.2...v1.4.0
[1.3.2]: https://github.com/helloscoopa/run-cache/compare/v1.2.2...v1.3.2
[1.2.2]: https://github.com/helloscoopa/run-cache/compare/v1.2.0...v1.2.2
[1.2.0]: https://github.com/helloscoopa/run-cache/compare/v1.0.5...v1.2.0
[1.0.5]: https://github.com/helloscoopa/run-cache/releases/tag/v1.0.5
26 changes: 15 additions & 11 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ npm run build:min # Minified build only
# Linting
npm run lint # Check for issues
npm run lint:fix # Auto-fix issues

# Docs
npm run docs:check # Check doc code samples against the exported API
```

## Architecture
Expand Down Expand Up @@ -103,13 +106,13 @@ await RunCache.set({
});

// Tag-based invalidation
RunCache.invalidateByTag("user");
await RunCache.invalidateByTag("user");

// Dependency-based invalidation
RunCache.invalidateByDependency("user:session");
await RunCache.invalidateByDependency("user:session");

// Middleware
RunCache.use(async (value, context, next) => {
await RunCache.use(async (value, context, next) => {
console.log(`${context.operation} on ${context.key}`);
return next(value);
});
Expand All @@ -119,7 +122,7 @@ RunCache.use(async (value, context, next) => {
```typescript
import { EvictionPolicy, LocalStorageAdapter } from 'run-cache';

RunCache.configure({
await RunCache.configure({
maxEntries: 1000,
evictionPolicy: EvictionPolicy.LRU,
debug: true,
Expand All @@ -130,12 +133,12 @@ RunCache.configure({
### Event Handling
```typescript
// Global events
RunCache.onExpiry((event) => console.log(`Expired: ${event.key}`));
RunCache.onRefetch((event) => console.log(`Refetched: ${event.key}`));
RunCache.onRefetchFailure((event) => console.error(`Failed: ${event.key}`));
await RunCache.onExpiry((event) => console.log(`Expired: ${event.key}`));
await RunCache.onRefetch((event) => console.log(`Refetched: ${event.key}`));
await RunCache.onRefetchFailure((event) => console.error(`Failed: ${event.key}`));

// Key-specific events (supports wildcards)
RunCache.onKeyExpiry("user:*", (event) => {
await RunCache.onKeyExpiry("user:*", (event) => {
console.log(`User cache expired: ${event.key}`);
});
```
Expand All @@ -147,7 +150,7 @@ RunCache.onKeyExpiry("user:*", (event) => {
2. Implement core logic in `src/core/cache-store.ts`
3. Add static wrapper methods in `src/run-cache.ts`
4. Add comprehensive tests in `.test.ts` files
5. Update documentation in `gitbook/` (see [Documentation & Versioning](#documentation--versioning))
5. Update documentation in `gitbook/` (see [Documentation & Versioning](#documentation--versioning)) and add an entry under `Unreleased` in `CHANGELOG.md`

### Testing Strategy
- Unit tests for each component (`.unit.test.ts`)
Expand Down Expand Up @@ -195,7 +198,7 @@ The docs are published on GitBook with a version menu above the sidebar, so read
- Pushing a tag `vX.Y.Z` runs the **Docs version** workflow: it freezes `gitbook/` at that tag into `gitbook-versions/vX.Y/`, regenerates `gitbook-docs.yaml`, and opens a PR (`docs/version-vX.Y.Z`). Merging it makes vX.Y the new default.
- A patch tag (`vX.Y.Z+1`) replaces `gitbook-versions/vX.Y/` in place; there is one version per minor line.
- Manual equivalent: `node scripts/docs-version.mjs snapshot vX.Y.Z`. The workflow can also be run by hand (workflow_dispatch) with a `tag` input.
- The snapshot drops legacy tooling files (`book.json`, `mint.json`, `setup-gitbook.sh`, `README_GITBOOK.md`, `README_MAIN.md`, `DOCUMENTATION_SUMMARY.md`, `styles/`), strips live shields.io badges, and adds a version hint under the README's first heading.
- The snapshot drops legacy tooling files that older tags still contain (`book.json`, `mint.json`, `setup-gitbook.sh`, `README_GITBOOK.md`, `README_MAIN.md`, `DOCUMENTATION_SUMMARY.md`, `styles/`; GitBook Git Sync is the only docs toolchain, so `gitbook/` no longer has them), strips live shields.io badges, and adds a version hint under the README's first heading.
- Running the snapshot rewrites the whole version directory, so hand fixes made in `gitbook-versions/vX.Y/` are lost if that line is snapshotted again. Fix them in `gitbook/` before tagging, or re-apply after.

### `gitbook-docs.yaml` rules
Expand All @@ -205,6 +208,7 @@ The docs are published on GitBook with a version menu above the sidebar, so read
- Version URL paths use hyphens (`v1-6`, `next`).

### Checks before committing doc changes
- `npm run docs:check` passes. It reads the fenced js/ts blocks in `README.md`, `CLAUDE.md` and `gitbook/`, plus `examples/`, and flags imports the package does not export, `RunCache` methods that do not exist, async calls without `await`, and known-stale patterns. Put `<!-- docs-check-ignore -->` above a block that breaks a rule on purpose.
- `node scripts/docs-version.mjs config` leaves no diff (`git status` is clean for `gitbook-docs.yaml`).
- Every relative `.md` link in the changed space resolves to a file in that space.
- No new pages are missing from the space's `SUMMARY.md`.
Expand All @@ -217,7 +221,7 @@ The docs are published on GitBook with a version menu above the sidebar, so read

### Debug Mode
```typescript
RunCache.configure({ debug: true });
await RunCache.configure({ debug: true });
```

### Common Issues
Expand Down
62 changes: 62 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Contributing to RunCache

Thanks for helping improve RunCache. This guide covers setting up the project, the checks a pull request must pass, and how to keep the documentation accurate.

## Setup

You need Node.js 18 or later (CI runs on Node 18) and npm.

```bash
git clone https://github.com/YOUR-USERNAME/run-cache.git
cd run-cache
npm install
git checkout -b fix/short-description
```

## Commands

| Command | What it does |
| --- | --- |
| `npm test` | Run the Jest test suite |
| `npm run test:watch` | Run tests in watch mode |
| `npm run test:coverage` | Run tests with a coverage report |
| `npm run lint` / `npm run lint:fix` | Check (or fix) `src/` with ESLint |
| `npm run build` | Compile with `tsc`, then build the ESM and minified bundles with esbuild |
| `npm run docs:check` | Check the code samples in the docs against the exported API |

After `npm run build`, you can run the JavaScript example with `node examples/middleware-example.js`.

## Making a change

1. **Open an issue first** for features or behaviour changes, so the approach can be agreed before you write code. Small fixes can go straight to a pull request.
2. **Keep zero dependencies.** RunCache has no runtime dependencies; do not add one.
3. **Add tests.** New behaviour needs tests, and a bug fix needs a test that fails without the fix. Keep coverage above 90%.
4. **Update the docs** in the same pull request (see below).
5. **Add a changelog entry** under `Unreleased` in [CHANGELOG.md](CHANGELOG.md).
6. **Use [Conventional Commits](https://www.conventionalcommits.org/)** for commit messages and PR titles, for example `fix: keep TTL timers when set() fails` or `docs: correct storage adapter options`.

Before opening the pull request, make sure `npm test`, `npm run lint`, `npm run build` and `npm run docs:check` all pass. CI runs them on every pull request.

## Documentation

The docs are published on GitBook from this repository. [CLAUDE.md](CLAUDE.md#documentation--versioning) and [gitbook-versions/README.md](gitbook-versions/README.md) describe the layout in full; the short version:

- `gitbook/` holds the docs for the next release. Edit pages there, and list any new page in `gitbook/SUMMARY.md`.
- `gitbook-versions/vX.Y/` holds frozen docs for released versions. Only fix errors there; never add unreleased features.
- `README.md` is the npm landing page; keep its examples in step with `gitbook/`.

When writing code samples:

- Import only what `src/index.ts` exports, and call only methods that exist on `RunCache`.
- `await` every async `RunCache` call (all of them except `createTypedCache()`), or explicitly return or chain the promise.
- Pass objects directly as `value`; RunCache serializes non-string values itself, and `get()` returns them parsed.

`npm run docs:check` enforces these rules on `README.md`, `CLAUDE.md`, `gitbook/` and `examples/`. If a sample deliberately breaks a rule, for example to show the legacy string-based pattern, put `<!-- docs-check-ignore -->` on the line above its code fence.

## Reporting a bug

Include the RunCache version, your runtime (Node.js version or browser), a minimal code sample that reproduces the problem, and what you expected to happen instead.

## Releases

Maintainers release by bumping the version in `package.json`, moving the `Unreleased` changelog entries under the new version, and pushing a `vX.Y.Z` tag. The tag triggers the **Docs version** workflow, which freezes `gitbook/` into `gitbook-versions/vX.Y/`.
Loading
Loading