# es-intrinsic-cache

> Get the pristine, unmodified JavaScript built-ins and cache them before anyone can tamper with them

Latest version **1.0.4** (published 2026-01-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install es-intrinsic-cache
pnpm add es-intrinsic-cache
yarn add es-intrinsic-cache
bun add es-intrinsic-cache
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2026-01-13 |
| First published | 2025-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 16 |
| Unpacked size | 50.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Fox Jones |
| Maintainers | xsfj |
| Keywords | intrinsic, intrinsics, cache, es-abstract, javascript, ecmascript, es, js, getintrinsic |

## Links

- npm: https://www.npmjs.com/package/es-intrinsic-cache
- Repository: https://github.com/xsfj/es-intrinsic-cache
- Homepage: https://github.com/xsfj/es-intrinsic-cache#readme
- Issues: https://github.com/xsfj/es-intrinsic-cache/issues
- npm.io page: https://npm.io/package/es-intrinsic-cache

## Dependencies (16)

- [gopd](https://npm.io/package/gopd.md) ^1.2.0
- [hasown](https://npm.io/package/hasown.md) ^2.0.2
- [get-proto-x](https://npm.io/package/get-proto-x.md) ^1.0.0
- [function-bind](https://npm.io/package/function-bind.md) ^1.1.2
- [async-function](https://npm.io/package/async-function.md) ^1.0.0
- [es-object-atoms](https://npm.io/package/es-object-atoms.md) ^1.1.1
- [function.call-x](https://npm.io/package/function.call-x.md) ^1.0.0
- [math-intrinsics](https://npm.io/package/math-intrinsics.md) ^1.1.0
- [function.apply-x](https://npm.io/package/function.apply-x.md) ^1.0.0
- [es-define-property](https://npm.io/package/es-define-property.md) ^1.0.1
- [generator-function](https://npm.io/package/generator-function.md) ^2.0.1
- [es-error-intrinsics](https://npm.io/package/es-error-intrinsics.md) ^1.0.0
- [has-symbol-support-x](https://npm.io/package/has-symbol-support-x.md) ^1.4.2
- [object.getprototypeof-x](https://npm.io/package/object.getprototypeof-x.md) ^1.0.0
- [async-generator-function](https://npm.io/package/async-generator-function.md) ^1.0.0
- [reflect.getprototypeof-x](https://npm.io/package/reflect.getprototypeof-x.md) ^1.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.4 (latest) — 2026-01-13
- 1.0.3 — 2026-01-12
- 1.0.1 — 2025-12-22
- 1.0.0 — 2025-12-21

## README

# es-intrinsic-cache

> Get and robustly cache all JavaScript language-level intrinsics at first require time

[![npm version](https://img.shields.io/npm/v/es-intrinsic-cache.svg)](https://www.npmjs.com/package/es-intrinsic-cache)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

## What is this?

`es-intrinsic-cache` provides safe, cached access to JavaScript's built-in objects and functions (intrinsics) before they can be modified by other code. This is essential for building robust libraries that need to rely on pristine versions of native JavaScript functionality.

## Why would you need this?

JavaScript allows modification of built-in prototypes and globals, which can break your code:

```javascript
// Malicious or buggy code could do this:
Array.prototype.push = function() { 
  console.log("hacked") 
}

// Now normal array operations break:
const arr = [1, 2, 3]
arr.push(4) // Logs "hacked" instead of working correctly
console.log(arr) // Logs [1,2,3] - nothing actually modified
```

`es-intrinsic-cache` solves this by caching the original intrinsics when your module first loads:

```javascript
const GetIntrinsic = require("es-intrinsic-cache")

// Hack the Array.prototype.push
Array.prototype.push = function() { 
  console.log("hacked") 
}

// Get the REAL Array.prototype.push, cached at module load time
const $push = GetIntrinsic("%Array.prototype.push%")

// This always works, even if Array.prototype.push was modified
const arr = [1, 2, 3]
$push.call(arr, 4) // ✅ Works correctly
console.log(arr) // Logs [1, 2, 3, 4]
```

## Installation

Using NPM:
```bash
npm install es-intrinsic-cache
```

Using Yarn:
```bash
yarn add es-intrinsic-cache
```

Using PNPM:
```bash
pnpm add es-intrinsic-cache
```

Using Bun:
```bash
bun add es-intrinsic-cache
```

## Usage

```javascript
const GetIntrinsic = require("es-intrinsic-cache")

// Access intrinsics with % wrapper syntax
const ArrayProto = GetIntrinsic("%Array.prototype%")
const ObjectCreate = GetIntrinsic("%Object.create%")
const JSONParse = GetIntrinsic("%JSON.parse%")

// Or without % wrappers (they're optional)
const Array = GetIntrinsic("Array")
const push = GetIntrinsic("Array.prototype.push")
```

### Accessor Properties

For accessor properties (getters/setters), `GetIntrinsic` returns the getter function. (**Note**: If a getter is emulating a data property (indicated by an `originalValue` property on the getter), the actual value is returned instead.)

Example:

```javascript
const GetIntrinsic = require("es-intrinsic-cache")

// Get the getter for Map.prototype.size
const $mapSize = GetIntrinsic("%Map.prototype.size%")

const myMap = new Map([["a", 1], ["b", 2]])
console.log($mapSize.call(myMap)) // 2
```

### `allowMissing` parameter

Some intrinsics may not exist in all environments. Use the second parameter to avoid errors. This also applies to nested properties: if the base intrinsic exists but the requested property does not, it returns undefined instead of throwing.

```javascript
const GetIntrinsic = require("es-intrinsic-cache")

// Throws TypeError if AsyncGeneratorPrototype doesn't exist
const AsyncGenProto = GetIntrinsic("%AsyncGeneratorPrototype%")

// Returns undefined if AsyncGeneratorPrototype doesn't exist
const AsyncGenProto = GetIntrinsic("%AsyncGeneratorPrototype%", true)
```

## ⚠️ Important Note: Load Order Matters

**`es-intrinsic-cache` must be loaded as early as possible in your application**, ideally before any other code runs. If other code modifies built-ins before `es-intrinsic-cache` is loaded, those modifications will be cached.

```javascript
// ✅ GOOD - Load es-intrinsic-cache first
const GetIntrinsic = require("es-intrinsic-cache")
// ... rest of your application

// ❌ BAD - Other code runs first
require("some-library-that-modifies-prototypes")
const GetIntrinsic = require("es-intrinsic-cache") // Too late!
```

## Available Intrinsics

See the full list of available intrinsics that you can cache with `es-intrinsic-cache` in the TypeScript definitions (`index.d.ts`).

## API

### `GetIntrinsic(name, [allowMissing])`

**Parameters:**
- `name` (string, required): The name of the intrinsic to retrieve. Can be wrapped in `%` or not.
- `allowMissing` (boolean, optional): If `true`, returns `undefined` for missing intrinsics instead of throwing. Default: `false`

**Returns:** The requested intrinsic value.

**Throws:**
- `TypeError` if `name` is not a non-empty string.
- `TypeError` if `allowMissing` is not a boolean.
- `SyntaxError` if `%` appears in the middle of the name (e.g., `%Array%.push`).
- `SyntaxError` if quoted property paths have mismatched or invalid quotes.
- `TypeError` if the intrinsic (or a base property) is missing/unavailable (unless `allowMissing` is true).


## Common Use Case

```javascript
const GetIntrinsic = require("es-intrinsic-cache")

const susLibrary = require("sus-library") // library that we're not sure about that might replace an intrinsic

const $JSONParse = GetIntrinsic("%JSON.parse%")
const $ObjectKeys = GetIntrinsic("%Object.keys%")

function safeJSONParse(str) {
  // Always uses pristine JSON.parse, even if JSON.parse was replaced
  return $JSONParse(str)
}
```

## Why `es-intrinsic-cache` instead of `get-intrinsic`?

`es-intrinsic-cache` is a modernized refactor of `get-intrinsic` that maintains **100% API compatibility** while improving internal clarity, dependency focus, and developer experience. Both libraries cache JavaScript intrinsics to protect against prototype pollution, but they differ in implementation philosophy.

### Key Advantages

- **Cleaner internals** 🧹  
  Removes the legacy `needsEval` / `doEval` mechanism and initializes intrinsics explicitly at module load time. This results in simpler, more readable code with less state-machine complexity.

- **Complete TypeScript definitions** 🧠  
  Includes **240+ fully typed intrinsics**, covering missing entries from `@types/get-intrinsic` (such as `%Float16Array%`, `%FinalizationRegistry%`, `%WeakRef%`, and legacy aliases). Types are auto-generated and always stay in sync with the implementation.

- **Comprehensive JSDoc documentation** 📚  
  Provides detailed JSDoc comments with parameter descriptions, edge cases, and examples, enabling better IDE hover docs and easier contributor onboarding.

- **Smaller, more focused dependencies** 📦  
  Uses single-responsibility helper modules instead of multi-purpose utilities, reducing unused code and dependency surface area.

- **Identical public API** 🔁  
  Fully compatible with `get-intrinsic`, including the same function signature, intrinsic naming syntax, legacy aliases, error handling, and edge-case protections.

## Contributing

Contributions are welcome! Please:

1. Fork the repository
2. Create a feature branch
3. Add tests for any new functionality
4. Ensure all tests pass
5. Submit a pull request

## License

MIT © Fox Jones
See license in [LICENSE](https://github.com/xsfj/es-intrinsic-cache/blob/main/LICENSE) (or don't, because it's just your regular MIT license)

## Support

- 🐛 [Report bugs](https://github.com/xsfj/es-intrinsic-cache/issues)
- 💡 [Request features](https://github.com/xsfj/es-intrinsic-cache/issues)

---

**Remember**: Load `es-intrinsic-cache` early in your application to ensure you get pristine intrinsics! 🛡️

---
_Source: https://npm.io/package/es-intrinsic-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
