# safer-web-storage

> Handle all exception when storage is not available due to user's browser settings. Swap to in-memory storage if sessionStorage is not available.

Latest version **2.0.0** (published 2022-11-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install safer-web-storage
pnpm add safer-web-storage
yarn add safer-web-storage
bun add safer-web-storage
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-11-24 |
| First published | 2019-09-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 47.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vitalii Saienko |
| Maintainers | modestfake |
| Keywords | localStorage, sessionStorage, in-memory storage |

## Links

- npm: https://www.npmjs.com/package/safer-web-storage
- Repository: https://github.com/modestfake/safer-web-storage
- Homepage: https://github.com/modestfake/safer-web-storage#readme
- Issues: https://github.com/modestfake/safer-web-storage/issues
- npm.io page: https://npm.io/package/safer-web-storage

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2022-11-24
- 1.1.0 — 2019-12-06
- 1.0.2 — 2019-09-04
- 1.0.1 — 2019-09-03
- 1.0.0 — 2019-09-03

## README

# safer-web-storage

Browsers that support `localStorage` and `sessionStorage` will have a property on the window object named respectively. However, for various reasons, just asserting that property exists may throw exceptions. If it does exist, that is still no guarantee that localStorage is actually available, as various browsers offer settings that disable storages. So a browser may support localStorage, but not make it available to the scripts on the page. One example of that is Safari, which in Private Browsing mode gives us an empty localStorage object with a quota of zero, effectively making it unusable.

## Installation and Usage

```bash
npm install safer-web-storage
```

```javascript
import * as SafeStorage from 'safer-web-storage'

const safeLocalStorage = SafeStorage.createSafeLocalStorage()
const safeSessionStorage = SafeStorage.createSafeSessionStorage()

safeLocalStorage.getItem('apples')
safeSessionStorage.seItem('pineapples', 20)
```

If either `window.sessionStorage` or its methods are not accessible, it swaps to in-memory storage.

This wrapper supports all methods and properties of [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API):

- `getItem`
- `setItem`
- `removeItem`
- `key`
- `clear`
- `length`

## API

### createSafeLocalStorage(options) => Storage

### createSafeSessionStorage(options) => Storage

You can pass the following properties to `options`:

| Name           | Type   | Default                                                                      | Description                                                                |
| -------------- | ------ | ---------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| `errorMessage` | String | Looks like you've disabled \<StorageType\>. Enable it to avoid this warning. | Error message printed to the browser console when storage is not available |

### Storage public API

| Name                    | Type   | Details           |
| ----------------------- | ------ | ----------------- |
| `isNativeStorageUsed`   | getter | Returns `Boolean` |
| `isInMemoryStorageUsed` | getter | Returns `Boolean` |

## Development

To test the library in a browser run:

```bash
npm install
npm start
```

## Publish

```bash
npm ci
npm test
npm run build
npm version [patch|minor|major]
npm publish
```

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